void FinalPass::run(const GraphicContextPtr &gc, SceneImpl *scene) { ScopeTimeFunction(); //Texture2DPtr &log_average_light_texture = log_average_light.find_log_average_light(gc, lightsource_pass.final_color); inout.bloom_contribution->set_min_filter(filter_linear); inout.bloom_contribution->set_mag_filter(filter_linear); inout.ambient_occlusion->set_min_filter(filter_linear); inout.ambient_occlusion->set_mag_filter(filter_linear); inout.final_color->set_min_filter(filter_nearest); inout.final_color->set_mag_filter(filter_nearest); if (inout.fb_viewport) gc->set_frame_buffer(inout.fb_viewport); gc->set_viewport(inout.viewport, gc->texture_image_y_axis()); gc->set_texture(0, inout.final_color); gc->set_texture(2, inout.bloom_contribution); gc->set_texture(3, inout.ambient_occlusion); gc->set_program_object(present_shader); gc->set_rasterizer_state(rasterizer_state); gc->draw_primitives(type_triangles, 6, rect_primarray); gc->reset_program_object(); gc->reset_texture(0); gc->reset_texture(2); gc->reset_texture(3); gc->reset_frame_buffer(); }
void InstancesBuffer::render_pass(const GraphicContextPtr &gc, SceneImpl *scene, const Mat4f &world_to_eye, const Mat4f &eye_to_projection, FrustumPlanes frustum, const std::function<void(ModelLOD*, int)> &pass_callback) { ScopeTimeFunction(); scene->engine()->render.scene_visits++; std::vector<Model *> models; scene->foreach_object(frustum, [&](SceneObjectImpl *object) { if (object->instance.get_renderer()) { Vec3f light_probe_color; if (object->light_probe_receiver()) { SceneLightProbeImpl *probe = find_nearest_probe(scene, object->position()); if (probe) light_probe_color = probe->color(); } scene->engine()->render.instances_drawn++; bool first_instance = object->instance.get_renderer()->add_instance(frame, object->instance, object->get_object_to_world(), light_probe_color); if (first_instance) { models.push_back(object->instance.get_renderer().get()); scene->engine()->render.models_drawn++; } } }); frame++; clear(); for (size_t i = 0; i < models.size(); i++) add(models[i]->get_instance_vectors_count()); lock(gc); for (size_t i = 0; i < models.size(); i++) models[i]->upload(*this, world_to_eye, eye_to_projection); unlock(gc); gc->set_texture(0, get_indexes()); gc->set_texture(1, get_vectors()); for (Model *model : models) { pass_callback(model->levels[0].get(), model->instances.size()); } // We cannot reset those because GBufferPass::run contains a hack relying on them being bound //gc->set_texture(0, nullptr); //gc->set_texture(1, nullptr); }
void ZMinMax::minmax(const GraphicContextPtr &gc) { update_buffers(gc); normal_z->set_min_filter(filter_nearest); normal_z->set_mag_filter(filter_nearest); normal_z->set_wrap_mode(wrap_clamp_to_edge, wrap_clamp_to_edge); gc->set_primitives_array(prim_array); gc->set_program_object(program0); gc->set_blend_state(blend_state); Size texture_size = find_texture_size(normal_z); for (int i = 0; i < iterations; i++) { if (i == 1) gc->set_program_object(program1); int iteration_width = texture_size.width >> i; int iteration_height = texture_size.height >> i; gc->set_frame_buffer((i % 2 == 0) ? fb0 : fb1); gc->set_viewport(Size(iteration_width, iteration_height), gc->texture_image_y_axis()); if (i == 0) gc->set_texture(0, normal_z); else gc->set_texture(0, (i % 2 == 0) ? texture1 : texture0); gc->draw_primitives_array(type_triangles, 6); } gc->reset_texture(0); gc->reset_program_object(); gc->reset_primitives_array(); gc->reset_frame_buffer(); gc->set_viewport(viewport.size(), gc->texture_image_y_axis()); }
void LightsourcePass::render(const GraphicContextPtr &gc, GPUTimer &timer) { ScopeTimeFunction(); zminmax.viewport = inout.viewport; zminmax.normal_z = inout.normal_z_gbuffer; zminmax.minmax(gc); update_buffers(gc); //timer.begin_time(gc, "light(cull)"); gc->set_uniform_buffer(0, compute_uniforms); gc->set_storage_buffer(0, compute_lights); gc->set_storage_buffer(1, compute_visible_lights); gc->set_texture(0, zminmax.result); gc->set_texture(1, inout.normal_z_gbuffer); gc->set_texture(2, inout.diffuse_color_gbuffer); gc->set_texture(3, inout.specular_color_gbuffer); gc->set_texture(4, inout.specular_level_gbuffer); gc->set_texture(5, inout.shadow_maps); gc->set_texture(6, inout.self_illumination_gbuffer); gc->set_image_texture(0, inout.final_color); gc->set_program_object(cull_tiles_program); gc->dispatch((num_tiles_x + tile_size - 1) / tile_size, (num_tiles_y + tile_size - 1) / tile_size); //timer.end_time(gc); //timer.begin_time(gc, "light(render)"); gc->set_program_object(render_tiles_program); gc->dispatch(num_tiles_x, num_tiles_y); gc->reset_image_texture(0); gc->reset_texture(6); gc->reset_texture(5); gc->reset_texture(4); gc->reset_texture(3); gc->reset_texture(2); gc->reset_texture(1); gc->reset_texture(0); gc->reset_uniform_buffer(2); gc->reset_uniform_buffer(1); gc->reset_uniform_buffer(0); //timer.end_time(gc); }
void LightsourceSimplePass::render(const GraphicContextPtr &gc, GPUTimer &timer) { ScopeTimeFunction(); //timer.begin_time(gc, "light(simple)"); gc->set_frame_buffer(inout.fb_final_color); gc->set_viewport(inout.viewport.size(), gc->texture_image_y_axis()); gc->set_depth_range(0.0f, 0.9f); gc->set_uniform_buffer(0, uniforms); gc->set_texture(0, light_instance_texture); gc->set_texture(1, inout.normal_z_gbuffer); gc->set_texture(2, inout.diffuse_color_gbuffer); gc->set_texture(3, inout.specular_color_gbuffer); gc->set_texture(4, inout.specular_level_gbuffer); gc->set_texture(5, inout.shadow_maps); gc->set_texture(6, inout.self_illumination_gbuffer); gc->set_blend_state(blend_state); gc->clear(); // To do: use icosahedron for smaller lights and when the camera is not inside the light influence sphere // To do: combine multiple lights into the same rect pass to reduce overdraw penalty gc->set_depth_stencil_state(rect_depth_stencil_state); gc->set_rasterizer_state(rect_rasterizer_state); gc->set_program_object(rect_light_program); gc->set_primitives_array(rect_prim_array); gc->draw_primitives_array_instanced(type_triangles, 0, 6, std::max(lights.size(), (size_t)1)); gc->reset_primitives_array(); /* gc->set_depth_stencil_state(icosahedron_depth_stencil_state); gc->set_rasterizer_state(icosahedron_rasterizer_state); gc->set_program_object(icosahedron_light_program); gc->set_primitives_array(icosahedron_prim_array); gc->draw_primitives_elements_instanced(type_triangles, icosahedron->num_elements, icosahedron->elements, 0, lights.size()); gc->reset_primitives_array(); */ gc->set_depth_stencil_state(icosahedron_depth_stencil_state); gc->set_rasterizer_state(icosahedron_rasterizer_state); gc->set_program_object(icosahedron_light_program); gc->set_primitives_array(icosahedron_prim_array); gc->draw_primitives_elements_instanced(type_triangles, icosahedron->num_elements, icosahedron->elements, type_unsigned_int, 0, lights.size()); gc->reset_primitives_array(); //timer.end_time(gc); //timer.begin_time(gc, "light(simple)"); gc->reset_texture(6); gc->reset_texture(5); gc->reset_texture(4); gc->reset_texture(3); gc->reset_texture(2); gc->reset_texture(1); gc->reset_texture(0); gc->reset_uniform_buffer(0); //timer.end_time(gc); }
void RenderBatchTriangle::flush(const GraphicContextPtr &gc) { if (position > 0) { gc->set_program_object(program_sprite); int gpu_index; VertexArrayVector<SpriteVertex> gpu_vertices(batch_buffer->get_vertex_buffer(gc, gpu_index)); if (!prim_array[gpu_index]) { prim_array[gpu_index] = PrimitivesArray::create(gc); prim_array[gpu_index]->set_attributes(0, gpu_vertices, cl_offsetof(SpriteVertex, position)); prim_array[gpu_index]->set_attributes(1, gpu_vertices, cl_offsetof(SpriteVertex, color)); prim_array[gpu_index]->set_attributes(2, gpu_vertices, cl_offsetof(SpriteVertex, texcoord)); prim_array[gpu_index]->set_attributes(3, gpu_vertices, cl_offsetof(SpriteVertex, texindex)); if (!glyph_blend) { BlendStateDescription blend_desc; blend_desc.set_blend_function(blend_constant_color, blend_one_minus_src_color, blend_zero, blend_one); glyph_blend = gc->create_blend_state(blend_desc); } } gpu_vertices.upload_data(gc, 0, vertices, position); for (int i = 0; i < num_current_textures; i++) gc->set_texture(i, current_textures[i]); #ifdef _DEBUG // Suppress a warning when the D3D debug layer is active (to do: add a boolean on gc that can report if a debug layer is present) if (num_current_textures > 0) { for (int i = num_current_textures; i < max_textures; i++) gc->set_texture(i, current_textures[0]); } #endif if (use_glyph_program) { gc->set_blend_state(glyph_blend, constant_color); gc->draw_primitives(type_triangles, position, prim_array[gpu_index]); gc->reset_blend_state(); } else { gc->draw_primitives(type_triangles, position, prim_array[gpu_index]); } for (int i = 0; i < num_current_textures; i++) gc->reset_texture(i); #ifdef _DEBUG // Suppress a warning when the D3D debug layer is active (to do: add a boolean on gc that can report if a debug layer is present) if (num_current_textures > 0) { for (int i = num_current_textures; i < max_textures; i++) gc->reset_texture(i); } #endif gc->reset_program_object(); position = 0; for (int i = 0; i < num_current_textures; i++) current_textures[i] = Texture2DPtr(); num_current_textures = 0; } }