diff --git a/drivers/vulkan/rendering_device_vulkan.cpp b/drivers/vulkan/rendering_device_vulkan.cpp index 98b1bc1115e..13a0948687c 100644 --- a/drivers/vulkan/rendering_device_vulkan.cpp +++ b/drivers/vulkan/rendering_device_vulkan.cpp @@ -5751,6 +5751,8 @@ void RenderingDeviceVulkan::draw_list_bind_render_pipeline(DrawListID p_list, RI dl->validation.pipeline_push_constant_suppplied = false; #endif } + + dl->state.pipeline_shader = pipeline->shader; } #ifdef DEBUG_ENABLED @@ -5975,8 +5977,8 @@ void RenderingDeviceVulkan::draw_list_draw(DrawListID p_list, bool p_use_indices if (p_procedural_vertices > 0) { #ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(dl->validation.pipeline_vertex_format == INVALID_ID, - "Procedural vertices requested, but pipeline expects a vertex array."); + ERR_FAIL_COND_MSG(dl->validation.pipeline_vertex_format == INVALID_ID, + "Procedural vertices requested, but pipeline expects a vertex array."); #endif to_draw = p_procedural_vertices; } else { @@ -6153,6 +6155,8 @@ void RenderingDeviceVulkan::compute_list_bind_compute_pipeline(ComputeListID p_l cl->validation.pipeline_push_constant_suppplied = false; #endif } + + cl->state.pipeline_shader = pipeline->shader; } #ifdef DEBUG_ENABLED diff --git a/editor/plugins/spatial_editor_plugin.cpp b/editor/plugins/spatial_editor_plugin.cpp index 74190e32c5e..7711231be84 100644 --- a/editor/plugins/spatial_editor_plugin.cpp +++ b/editor/plugins/spatial_editor_plugin.cpp @@ -2768,7 +2768,8 @@ void SpatialEditorViewport::_menu_option(int p_option) { case VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS: case VIEW_DISPLAY_DEBUG_GIPROBE_ALBEDO: case VIEW_DISPLAY_DEBUG_GIPROBE_LIGHTING: - case VIEW_DISPLAY_DEBUG_GIPROBE_EMISSION: { + case VIEW_DISPLAY_DEBUG_GIPROBE_EMISSION: + case VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE: { static const int display_options[] = { VIEW_DISPLAY_NORMAL, @@ -2782,6 +2783,7 @@ void SpatialEditorViewport::_menu_option(int p_option) { VIEW_DISPLAY_DEBUG_GIPROBE_ALBEDO, VIEW_DISPLAY_DEBUG_GIPROBE_LIGHTING, VIEW_DISPLAY_DEBUG_GIPROBE_EMISSION, + VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE, VIEW_MAX }; static const Viewport::DebugDraw debug_draw_modes[] = { @@ -2795,7 +2797,8 @@ void SpatialEditorViewport::_menu_option(int p_option) { Viewport::DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS, Viewport::DEBUG_DRAW_GI_PROBE_ALBEDO, Viewport::DEBUG_DRAW_GI_PROBE_LIGHTING, - Viewport::DEBUG_DRAW_GI_PROBE_EMISSION + Viewport::DEBUG_DRAW_GI_PROBE_EMISSION, + Viewport::DEBUG_DRAW_SCENE_LUMINANCE }; int idx = 0; @@ -3639,6 +3642,8 @@ SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, Ed display_submenu->add_radio_check_item(TTR("GIProbe Lighting"), VIEW_DISPLAY_DEBUG_GIPROBE_LIGHTING); display_submenu->add_radio_check_item(TTR("GIProbe Albedo"), VIEW_DISPLAY_DEBUG_GIPROBE_ALBEDO); display_submenu->add_radio_check_item(TTR("GIProbe Emission"), VIEW_DISPLAY_DEBUG_GIPROBE_EMISSION); + display_submenu->add_separator(); + display_submenu->add_radio_check_item(TTR("Scene Luminance"), VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE); display_submenu->set_name("display_advanced"); view_menu->get_popup()->add_submenu_item(TTR("Display Advanced..."), "display_advanced"); view_menu->get_popup()->add_separator(); diff --git a/editor/plugins/spatial_editor_plugin.h b/editor/plugins/spatial_editor_plugin.h index 8f09bdfeced..0eb56989e08 100644 --- a/editor/plugins/spatial_editor_plugin.h +++ b/editor/plugins/spatial_editor_plugin.h @@ -173,6 +173,7 @@ class SpatialEditorViewport : public Control { VIEW_DISPLAY_DEBUG_GIPROBE_ALBEDO, VIEW_DISPLAY_DEBUG_GIPROBE_LIGHTING, VIEW_DISPLAY_DEBUG_GIPROBE_EMISSION, + VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE, VIEW_LOCK_ROTATION, VIEW_CINEMATIC_PREVIEW, VIEW_MAX diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 19edf556fbc..9e9b77bab4f 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -3208,10 +3208,6 @@ void Viewport::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "handle_input_locally"), "set_handle_input_locally", "is_handling_input_locally"); ADD_GROUP("Rendering", ""); ADD_PROPERTY(PropertyInfo(Variant::INT, "msaa", PROPERTY_HINT_ENUM, "Disabled,2x,4x,8x,16x,AndroidVR 2x,AndroidVR 4x"), "set_msaa", "get_msaa"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hdr"), "set_hdr", "get_hdr"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_3d"), "set_disable_3d", "is_3d_disabled"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_3d_linear"), "set_keep_3d_linear", "get_keep_3d_linear"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "usage", PROPERTY_HINT_ENUM, "2D,2D No-Sampling,3D,3D No-Effects"), "set_usage", "get_usage"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "render_direct_to_screen"), "set_use_render_direct_to_screen", "is_using_render_direct_to_screen"); ADD_PROPERTY(PropertyInfo(Variant::INT, "debug_draw", PROPERTY_HINT_ENUM, "Disabled,Unshaded,Overdraw,Wireframe"), "set_debug_draw", "get_debug_draw"); ADD_GROUP("Render Target", "render_target_"); @@ -3267,6 +3263,13 @@ void Viewport::_bind_methods() { BIND_ENUM_CONSTANT(DEBUG_DRAW_OVERDRAW); BIND_ENUM_CONSTANT(DEBUG_DRAW_WIREFRAME); + BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_PROBE_ALBEDO); + BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_PROBE_LIGHTING); + BIND_ENUM_CONSTANT(DEBUG_DRAW_GI_PROBE_EMISSION); + BIND_ENUM_CONSTANT(DEBUG_DRAW_SHADOW_ATLAS); + BIND_ENUM_CONSTANT(DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS); + BIND_ENUM_CONSTANT(DEBUG_DRAW_SCENE_LUMINANCE); + BIND_ENUM_CONSTANT(MSAA_DISABLED); BIND_ENUM_CONSTANT(MSAA_2X); BIND_ENUM_CONSTANT(MSAA_4X); diff --git a/scene/main/viewport.h b/scene/main/viewport.h index 87bb8344b9a..53a024f5554 100644 --- a/scene/main/viewport.h +++ b/scene/main/viewport.h @@ -136,6 +136,7 @@ public: DEBUG_DRAW_GI_PROBE_EMISSION, DEBUG_DRAW_SHADOW_ATLAS, DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS, + DEBUG_DRAW_SCENE_LUMINANCE, }; diff --git a/servers/visual/rasterizer.h b/servers/visual/rasterizer.h index ad515c06b82..e7746eec90a 100644 --- a/servers/visual/rasterizer.h +++ b/servers/visual/rasterizer.h @@ -246,7 +246,7 @@ public: virtual void render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region) = 0; virtual void set_scene_pass(uint64_t p_pass) = 0; - virtual void set_time(double p_time) = 0; + virtual void set_time(double p_time, double p_step) = 0; virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw) = 0; virtual RID render_buffers_create() = 0; diff --git a/servers/visual/rasterizer_rd/rasterizer_effects_rd.cpp b/servers/visual/rasterizer_rd/rasterizer_effects_rd.cpp index 65b82da97a9..72329d8a131 100644 --- a/servers/visual/rasterizer_rd/rasterizer_effects_rd.cpp +++ b/servers/visual/rasterizer_rd/rasterizer_effects_rd.cpp @@ -45,6 +45,28 @@ static _FORCE_INLINE_ void store_transform_3x3(const Basis &p_basis, float *p_ar p_array[11] = 0; } +RID RasterizerEffectsRD::_get_uniform_set_from_image(RID p_image) { + + if (image_to_uniform_set_cache.has(p_image)) { + RID uniform_set = image_to_uniform_set_cache[p_image]; + if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) { + return uniform_set; + } + } + Vector uniforms; + RD::Uniform u; + u.type = RD::UNIFORM_TYPE_IMAGE; + u.binding = 0; + u.ids.push_back(p_image); + uniforms.push_back(u); + //any thing with the same configuration (one texture in binding 0 for set 0), is good + RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 1); + + image_to_uniform_set_cache[p_image] = uniform_set; + + return uniform_set; +} + RID RasterizerEffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) { if (texture_to_uniform_set_cache.has(p_texture)) { @@ -69,6 +91,30 @@ RID RasterizerEffectsRD::_get_uniform_set_from_texture(RID p_texture, bool p_use return uniform_set; } +RID RasterizerEffectsRD::_get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps) { + + if (texture_to_compute_uniform_set_cache.has(p_texture)) { + RID uniform_set = texture_to_compute_uniform_set_cache[p_texture]; + if (RD::get_singleton()->uniform_set_is_valid(uniform_set)) { + return uniform_set; + } + } + + Vector uniforms; + RD::Uniform u; + u.type = RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE; + u.binding = 0; + u.ids.push_back(p_use_mipmaps ? default_mipmap_sampler : default_sampler); + u.ids.push_back(p_texture); + uniforms.push_back(u); + //any thing with the same configuration (one texture in binding 0 for set 0), is good + RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, 0), 0); + + texture_to_compute_uniform_set_cache[p_texture] = uniform_set; + + return uniform_set; +} + void RasterizerEffectsRD::copy_to_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_rect, bool p_flip_y) { zeromem(&blur.push_constant, sizeof(BlurPushConstant)); @@ -147,7 +193,7 @@ void RasterizerEffectsRD::gaussian_blur(RID p_source_rd_texture, RID p_framebuff RD::get_singleton()->draw_list_end(); } -void RasterizerEffectsRD::gaussian_glow(RID p_source_rd_texture, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, const Vector2 &p_pixel_size, float p_strength, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_treshold, float p_hdr_bleed_scale, RID p_auto_exposure) { +void RasterizerEffectsRD::gaussian_glow(RID p_source_rd_texture, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, const Vector2 &p_pixel_size, float p_strength, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_treshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_grey) { zeromem(&blur.push_constant, sizeof(BlurPushConstant)); @@ -164,7 +210,7 @@ void RasterizerEffectsRD::gaussian_glow(RID p_source_rd_texture, RID p_framebuff blur.push_constant.glow_exposure = p_exposure; blur.push_constant.glow_white = 0; //actually unused blur.push_constant.glow_luminance_cap = p_luminance_cap; - blur.push_constant.glow_auto_exposure_grey = 0; //unused also + blur.push_constant.glow_auto_exposure_grey = p_auto_exposure_grey; //unused also //HORIZONTAL RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_framebuffer_half, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD); @@ -315,6 +361,51 @@ void RasterizerEffectsRD::tonemapper(RID p_source_color, RID p_dst_framebuffer, RD::get_singleton()->draw_list_end(); } +void RasterizerEffectsRD::luminance_reduction(RID p_source_texture, const Size2i p_source_size, const Vector p_reduce, RID p_prev_luminance, float p_min_luminance, float p_max_luminance, float p_adjust, bool p_set) { + + luminance_reduce.push_constant.source_size[0] = p_source_size.x; + luminance_reduce.push_constant.source_size[1] = p_source_size.y; + luminance_reduce.push_constant.max_luminance = p_max_luminance; + luminance_reduce.push_constant.min_luminance = p_min_luminance; + luminance_reduce.push_constant.exposure_adjust = p_adjust; + + RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); + + for (int i = 0; i < p_reduce.size(); i++) { + + if (i == 0) { + RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_READ]); + RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_source_texture), 0); + } else { + + RD::get_singleton()->compute_list_add_barrier(compute_list); //needs barrier, wait until previous is done + + if (i == p_reduce.size() - 1 && !p_set) { + RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE_WRITE]); + RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_compute_uniform_set_from_texture(p_prev_luminance), 2); + } else { + RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, luminance_reduce.pipelines[LUMINANCE_REDUCE]); + } + + RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i - 1]), 0); + } + + RD::get_singleton()->compute_list_bind_uniform_set(compute_list, _get_uniform_set_from_image(p_reduce[i]), 1); + + RD::get_singleton()->compute_list_set_push_constant(compute_list, &luminance_reduce.push_constant, sizeof(LuminanceReducePushConstant)); + + int32_t x_groups = (luminance_reduce.push_constant.source_size[0] - 1) / 8 + 1; + int32_t y_groups = (luminance_reduce.push_constant.source_size[1] - 1) / 8 + 1; + + RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 1); + + luminance_reduce.push_constant.source_size[0] = MAX(luminance_reduce.push_constant.source_size[0] / 8, 1); + luminance_reduce.push_constant.source_size[1] = MAX(luminance_reduce.push_constant.source_size[1] / 8, 1); + } + + RD::get_singleton()->compute_list_end(); +} + RasterizerEffectsRD::RasterizerEffectsRD() { { @@ -390,6 +481,22 @@ RasterizerEffectsRD::RasterizerEffectsRD() { } } + { + // Initialize luminance_reduce + Vector luminance_reduce_modes; + luminance_reduce_modes.push_back("\n#define READ_TEXTURE\n"); + luminance_reduce_modes.push_back("\n"); + luminance_reduce_modes.push_back("\n#define WRITE_LUMINANCE\n"); + + luminance_reduce.shader.initialize(luminance_reduce_modes); + + luminance_reduce.shader_version = luminance_reduce.shader.version_create(); + + for (int i = 0; i < LUMINANCE_REDUCE_MAX; i++) { + luminance_reduce.pipelines[i] = RD::get_singleton()->compute_pipeline_create(luminance_reduce.shader.version_get_shader(luminance_reduce.shader_version, i)); + } + } + { // Initialize copier Vector copy_modes; diff --git a/servers/visual/rasterizer_rd/rasterizer_effects_rd.h b/servers/visual/rasterizer_rd/rasterizer_effects_rd.h index 74d18683a5d..1fee77e41c9 100644 --- a/servers/visual/rasterizer_rd/rasterizer_effects_rd.h +++ b/servers/visual/rasterizer_rd/rasterizer_effects_rd.h @@ -36,6 +36,7 @@ #include "servers/visual/rasterizer_rd/shaders/blur.glsl.gen.h" #include "servers/visual/rasterizer_rd/shaders/copy.glsl.gen.h" #include "servers/visual/rasterizer_rd/shaders/cubemap_roughness.glsl.gen.h" +#include "servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl.gen.h" #include "servers/visual/rasterizer_rd/shaders/sky.glsl.gen.h" #include "servers/visual/rasterizer_rd/shaders/tonemap.glsl.gen.h" #include "servers/visual_server.h" @@ -178,6 +179,29 @@ class RasterizerEffectsRD { RenderPipelineVertexFormatCacheRD pipelines[TONEMAP_MODE_MAX]; } tonemap; + enum LuminanceReduceMode { + LUMINANCE_REDUCE_READ, + LUMINANCE_REDUCE, + LUMINANCE_REDUCE_WRITE, + LUMINANCE_REDUCE_MAX + }; + + struct LuminanceReducePushConstant { + int32_t source_size[2]; + float max_luminance; + float min_luminance; + float exposure_adjust; + float pad[3]; + }; + + struct LuminanceReduce { + + LuminanceReducePushConstant push_constant; + LuminanceReduceShaderRD shader; + RID shader_version; + RID pipelines[LUMINANCE_REDUCE_MAX]; + } luminance_reduce; + struct CopyToDPPushConstant { float bias; float z_far; @@ -204,7 +228,12 @@ class RasterizerEffectsRD { Map texture_to_uniform_set_cache; + Map image_to_uniform_set_cache; + Map texture_to_compute_uniform_set_cache; + + RID _get_uniform_set_from_image(RID p_texture); RID _get_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps = false); + RID _get_compute_uniform_set_from_texture(RID p_texture, bool p_use_mipmaps = false); public: //TODO must re-do most of the shaders in compute @@ -212,12 +241,13 @@ public: void region_copy(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_region); void copy_to_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_rect, bool p_flip_y = false); void gaussian_blur(RID p_source_rd_texture, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, const Vector2 &p_pixel_size, const Rect2 &p_region); - void gaussian_glow(RID p_source_rd_texture, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, const Vector2 &p_pixel_size, float p_strength = 1.0, bool p_first_pass = false, float p_luminance_cap = 16.0, float p_exposure = 1.0, float p_bloom = 0.0, float p_hdr_bleed_treshold = 1.0, float p_hdr_bleed_scale = 1.0, RID p_auto_exposure = RID()); + void gaussian_glow(RID p_source_rd_texture, RID p_framebuffer_half, RID p_rd_texture_half, RID p_dest_framebuffer, const Vector2 &p_pixel_size, float p_strength = 1.0, bool p_first_pass = false, float p_luminance_cap = 16.0, float p_exposure = 1.0, float p_bloom = 0.0, float p_hdr_bleed_treshold = 1.0, float p_hdr_bleed_scale = 1.0, RID p_auto_exposure = RID(), float p_auto_exposure_grey = 1.0); void cubemap_roughness(RID p_source_rd_texture, bool p_source_is_panorama, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness); void render_panorama(RD::DrawListID p_list, RenderingDevice::FramebufferFormatID p_fb_format, RID p_panorama, const CameraMatrix &p_camera, const Basis &p_orientation, float p_alpha, float p_multipler); void make_mipmap(RID p_source_rd_texture, RID p_framebuffer_half, const Vector2 &p_pixel_size); void copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_rect, float p_z_near, float p_z_far, float p_bias, bool p_dp_flip); + void luminance_reduction(RID p_source_texture, const Size2i p_source_size, const Vector p_reduce, RID p_prev_luminance, float p_min_luminance, float p_max_luminance, float p_adjust, bool p_set = false); struct TonemapSettings { diff --git a/servers/visual/rasterizer_rd/rasterizer_rd.cpp b/servers/visual/rasterizer_rd/rasterizer_rd.cpp index 89465b580ce..b9fe0caf9d4 100644 --- a/servers/visual/rasterizer_rd/rasterizer_rd.cpp +++ b/servers/visual/rasterizer_rd/rasterizer_rd.cpp @@ -79,7 +79,7 @@ void RasterizerRD::begin_frame(double frame_step) { frame++; time += frame_step; canvas->set_time(time); - scene->set_time(time); + scene->set_time(time, frame_step); } void RasterizerRD::end_frame(bool p_swap_buffers) { diff --git a/servers/visual/rasterizer_rd/rasterizer_scene_forward_rd.cpp b/servers/visual/rasterizer_rd/rasterizer_scene_forward_rd.cpp index 0159146bfc1..248a3efede1 100644 --- a/servers/visual/rasterizer_rd/rasterizer_scene_forward_rd.cpp +++ b/servers/visual/rasterizer_rd/rasterizer_scene_forward_rd.cpp @@ -2225,8 +2225,9 @@ void RasterizerSceneForwardRD::_setup_render_pass_uniform_set(RID p_depth_buffer RasterizerSceneForwardRD *RasterizerSceneForwardRD::singleton = NULL; -void RasterizerSceneForwardRD::set_time(double p_time) { +void RasterizerSceneForwardRD::set_time(double p_time, double p_step) { time = p_time; + RasterizerSceneRD::set_time(p_time, p_step); } RasterizerSceneForwardRD::RasterizerSceneForwardRD(RasterizerStorageRD *p_storage) : diff --git a/servers/visual/rasterizer_rd/rasterizer_scene_forward_rd.h b/servers/visual/rasterizer_rd/rasterizer_scene_forward_rd.h index 0494a8f1b4c..8517e4201c6 100644 --- a/servers/visual/rasterizer_rd/rasterizer_scene_forward_rd.h +++ b/servers/visual/rasterizer_rd/rasterizer_scene_forward_rd.h @@ -543,7 +543,7 @@ protected: virtual void _render_material(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID p_framebuffer, const Rect2i &p_region); public: - virtual void set_time(double p_time); + virtual void set_time(double p_time, double p_step); virtual bool free(RID p_rid); diff --git a/servers/visual/rasterizer_rd/rasterizer_scene_rd.cpp b/servers/visual/rasterizer_rd/rasterizer_scene_rd.cpp index 0b24cb28880..a97c68a6193 100644 --- a/servers/visual/rasterizer_rd/rasterizer_scene_rd.cpp +++ b/servers/visual/rasterizer_rd/rasterizer_scene_rd.cpp @@ -31,6 +31,9 @@ #include "rasterizer_scene_rd.h" #include "core/os/os.h" #include "core/project_settings.h" +#include "servers/visual/visual_server_raster.h" + +uint64_t RasterizerSceneRD::auto_exposure_counter = 2; void RasterizerSceneRD::_clear_reflection_data(ReflectionData &rd) { @@ -460,6 +463,9 @@ void RasterizerSceneRD::environment_set_tonemap(RID p_env, VS::EnvironmentToneMa ERR_FAIL_COND(!env); env->exposure = p_exposure; env->tone_mapper = p_tone_mapper; + if (!env->auto_exposure && p_auto_exposure) { + env->auto_exposure_version = ++auto_exposure_counter; + } env->auto_exposure = p_auto_exposure; env->white = p_white; env->min_luminance = p_min_luminance; @@ -2211,6 +2217,39 @@ void RasterizerSceneRD::_allocate_blur_textures(RenderBuffers *rb) { } } +void RasterizerSceneRD::_allocate_luminance_textures(RenderBuffers *rb) { + ERR_FAIL_COND(!rb->luminance.current.is_null()); + + int w = rb->width; + int h = rb->height; + + while (true) { + w = MAX(w / 8, 1); + h = MAX(h / 8, 1); + + RD::TextureFormat tf; + tf.format = RD::DATA_FORMAT_R32_SFLOAT; + tf.width = w; + tf.height = h; + tf.usage_bits = RD::TEXTURE_USAGE_STORAGE_BIT; + + bool final = w == 1 && h == 1; + + if (final) { + tf.usage_bits |= RD::TEXTURE_USAGE_SAMPLING_BIT; + } + + RID texture = RD::get_singleton()->texture_create(tf, RD::TextureView()); + + rb->luminance.reduce.push_back(texture); + + if (final) { + rb->luminance.current = RD::get_singleton()->texture_create(tf, RD::TextureView()); + break; + } + } +} + void RasterizerSceneRD::_free_render_buffer_data(RenderBuffers *rb) { if (rb->texture.is_valid()) { @@ -2229,6 +2268,9 @@ void RasterizerSceneRD::_free_render_buffer_data(RenderBuffers *rb) { RD::get_singleton()->free(rb->luminance.reduce[i]); } + for (int i = 0; i < rb->luminance.reduce.size(); i++) { + RD::get_singleton()->free(rb->luminance.reduce[i]); + } rb->luminance.reduce.clear(); if (rb->luminance.current.is_valid()) { @@ -2245,10 +2287,29 @@ void RasterizerSceneRD::render_buffers_post_process_and_tonemap(RID p_render_buf Environent *env = environment_owner.getornull(p_environment); //glow (if enabled) + bool can_use_effects = rb->width >= 8 && rb->height >= 8; + + if (can_use_effects && env && env->auto_exposure) { + + if (rb->luminance.current.is_null()) { + _allocate_luminance_textures(rb); + } + + bool set_immediate = env->auto_exposure_version != rb->auto_exposure_version; + rb->auto_exposure_version = env->auto_exposure_version; + + double step = env->auto_exp_speed * time_step; + storage->get_effects()->luminance_reduction(rb->texture, Size2i(rb->width, rb->height), rb->luminance.reduce, rb->luminance.current, env->min_luminance, env->max_luminance, step, set_immediate); + + //swap final reduce with prev luminance + SWAP(rb->luminance.current, rb->luminance.reduce.write[rb->luminance.reduce.size() - 1]); + VisualServerRaster::redraw_request(); //redraw all the time if auto exposure rendering is on + } + int max_glow_level = -1; int glow_mask = 0; - if (env && env->glow_enabled) { + if (can_use_effects && env && env->glow_enabled) { /* see that blur textures are allocated */ @@ -2276,7 +2337,11 @@ void RasterizerSceneRD::render_buffers_post_process_and_tonemap(RID p_render_buf int vp_h = rb->blur[1].mipmaps[i].height; if (i == 0) { - storage->get_effects()->gaussian_glow(rb->texture, rb->blur[0].mipmaps[i + 1].framebuffer, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].framebuffer, Vector2(1.0 / vp_w, 1.0 / vp_h), env->glow_strength, true, env->glow_hdr_luminance_cap, env->exposure, env->glow_bloom, env->glow_hdr_bleed_threshold, env->glow_hdr_bleed_scale, RID()); + RID luminance_texture; + if (env->auto_exposure && rb->luminance.current.is_valid()) { + luminance_texture = rb->luminance.current; + } + storage->get_effects()->gaussian_glow(rb->texture, rb->blur[0].mipmaps[i + 1].framebuffer, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].framebuffer, Vector2(1.0 / vp_w, 1.0 / vp_h), env->glow_strength, true, env->glow_hdr_luminance_cap, env->exposure, env->glow_bloom, env->glow_hdr_bleed_threshold, env->glow_hdr_bleed_scale, luminance_texture, env->auto_exp_scale); } else { storage->get_effects()->gaussian_glow(rb->blur[1].mipmaps[i - 1].texture, rb->blur[0].mipmaps[i + 1].framebuffer, rb->blur[0].mipmaps[i + 1].texture, rb->blur[1].mipmaps[i].framebuffer, Vector2(1.0 / vp_w, 1.0 / vp_h), env->glow_strength); } @@ -2288,9 +2353,17 @@ void RasterizerSceneRD::render_buffers_post_process_and_tonemap(RID p_render_buf RasterizerEffectsRD::TonemapSettings tonemap; tonemap.color_correction_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_3D_WHITE); - tonemap.exposure_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE); - if (env && env->glow_enabled) { + if (can_use_effects && env && env->auto_exposure && rb->luminance.current.is_valid()) { + tonemap.use_auto_exposure = true; + tonemap.exposure_texture = rb->luminance.current; + tonemap.auto_exposure_grey = env->auto_exp_scale; + } else { + + tonemap.exposure_texture = storage->texture_rd_get_default(RasterizerStorageRD::DEFAULT_RD_TEXTURE_WHITE); + } + + if (can_use_effects && env && env->glow_enabled) { tonemap.use_glow = true; tonemap.glow_mode = RasterizerEffectsRD::TonemapSettings::GlowMode(env->glow_blend_mode); tonemap.glow_intensity = env->glow_blend_mode == VS::GLOW_BLEND_MODE_MIX ? env->glow_mix : env->glow_intensity; @@ -2338,6 +2411,14 @@ void RasterizerSceneRD::render_buffers_debug_draw(RID p_render_buffers, RID p_sh effects->copy_to_rect(shadow_atlas_texture, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 2)); } } + + if (debug_draw == VS::VIEWPORT_DEBUG_DRAW_SCENE_LUMINANCE) { + if (rb->luminance.current.is_valid()) { + Size2 rtsize = storage->render_target_get_size(rb->render_target); + + effects->copy_to_rect(rb->luminance.current, storage->render_target_get_rd_framebuffer(rb->render_target), Rect2(Vector2(), rtsize / 8)); + } + } } RID RasterizerSceneRD::render_buffers_get_back_buffer_texture(RID p_render_buffers) { @@ -2663,6 +2744,10 @@ void RasterizerSceneRD::update() { _update_dirty_skys(); } +void RasterizerSceneRD::set_time(double p_time, double p_step) { + time_step = p_step; +} + RasterizerSceneRD::RasterizerSceneRD(RasterizerStorageRD *p_storage) { storage = p_storage; diff --git a/servers/visual/rasterizer_rd/rasterizer_scene_rd.h b/servers/visual/rasterizer_rd/rasterizer_scene_rd.h index 3be67d8a922..ae4bcde4b4b 100644 --- a/servers/visual/rasterizer_rd/rasterizer_scene_rd.h +++ b/servers/visual/rasterizer_rd/rasterizer_scene_rd.h @@ -64,6 +64,7 @@ protected: private: VS::ViewportDebugDraw debug_draw = VS::VIEWPORT_DEBUG_DRAW_DISABLED; + double time_step = 0; int roughness_layers; @@ -465,6 +466,7 @@ private: float max_luminance = 8.0; float auto_exp_speed = 0.2; float auto_exp_scale = 0.5; + uint64_t auto_exposure_version = 0; /// Glow @@ -481,6 +483,8 @@ private: bool glow_bicubic_upscale = false; }; + static uint64_t auto_exposure_counter; + mutable RID_Owner environment_owner; /* RENDER BUFFERS */ @@ -492,6 +496,8 @@ private: VS::ViewportMSAA msaa = VS::VIEWPORT_MSAA_DISABLED; RID render_target; + uint64_t auto_exposure_version = 1; + RID texture; //main texture for rendering to, must be filled after done rendering //built-in textures used for ping pong image processing and blurring @@ -521,6 +527,7 @@ private: void _free_render_buffer_data(RenderBuffers *rb); void _allocate_blur_textures(RenderBuffers *rb); + void _allocate_luminance_textures(RenderBuffers *rb); uint64_t scene_pass = 0; uint64_t shadow_atlas_realloc_tolerance_msec = 500; @@ -861,6 +868,8 @@ public: virtual void set_debug_draw_mode(VS::ViewportDebugDraw p_debug_draw); _FORCE_INLINE_ VS::ViewportDebugDraw get_debug_draw_mode() const { return debug_draw; } + virtual void set_time(double p_time, double p_step); + RasterizerSceneRD(RasterizerStorageRD *p_storage); ~RasterizerSceneRD(); }; diff --git a/servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl b/servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl index 157ea91a36b..d3001e6fcb9 100644 --- a/servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl +++ b/servers/visual/rasterizer_rd/shaders/luminance_reduce.glsl @@ -15,20 +15,27 @@ shared float tmp_data[BLOCK_SIZE*BLOCK_SIZE]; #ifdef READ_TEXTURE //use for main texture -layout(set = 0, binding = 1) uniform texture2D source_texture; -layout(set = 0, binding = 2) uniform sampler source_sampler; +layout(set = 0, binding = 0) uniform sampler2D source_texture; #else //use for intermediate textures -layout(r32f, set = 0, binding = 1) uniform restrict readonly image2D source_luminance; +layout(r32f, set = 0, binding = 0) uniform restrict readonly image2D source_luminance; #endif -layout(r32f, set = 0, binding = 3) uniform restrict writeonly image2D dest_luminance; +layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D dest_luminance; -layout(push_constant, binding = 0, std430) uniform Params { +#ifdef WRITE_LUMINANCE +layout(set = 2, binding = 0) uniform sampler2D prev_luminance; +#endif + +layout(push_constant, binding = 1, std430) uniform Params { ivec2 source_size; + float max_luminance; + float min_luminance; + float exposure_adjust; + float pad[3]; } params; void main() { @@ -40,11 +47,10 @@ void main() { if (any(lessThan(pos,params.source_size))) { #ifdef READ_TEXTURE - vec3 v = texelFetch(sampler2D(source_texture,source_sampler),pos).rgb; - avg += max(v.r,max(v.g,v.b)); - tmp_data[t] = 0.0; + vec3 v = texelFetch(source_texture,pos,0).rgb; + tmp_data[t] = max(v.r,max(v.g,v.b)); #else - tmp_data[t] = imageLoad(source_luminance, pos); + tmp_data[t] = imageLoad(source_luminance, pos).r; #endif } else { tmp_data[t] = 0.0; @@ -64,13 +70,18 @@ void main() { size>>=1; - } while(size>1); + } while(size>=1); if (t==0) { //compute rect size - ivec2 rect_size = max(params.source_size - pos,ivec2(BLOCK_SIZE)); + ivec2 rect_size = min(params.source_size - pos,ivec2(BLOCK_SIZE)); float avg = tmp_data[0] / float(rect_size.x*rect_size.y); + //float avg = tmp_data[0] / float(BLOCK_SIZE*BLOCK_SIZE); pos/=ivec2(BLOCK_SIZE); +#ifdef WRITE_LUMINANCE + float prev_lum = texelFetch(prev_luminance, ivec2(0, 0), 0).r; //1 pixel previous exposure + avg= clamp(prev_lum + (avg - prev_lum) * params.exposure_adjust, params.min_luminance, params.max_luminance); +#endif imageStore(dest_luminance, pos, vec4(avg)); } } diff --git a/servers/visual_server.h b/servers/visual_server.h index f79933e31cb..da6e98854e5 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -662,6 +662,7 @@ public: VIEWPORT_DEBUG_DRAW_GI_PROBE_EMISSION, VIEWPORT_DEBUG_DRAW_SHADOW_ATLAS, VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS, + VIEWPORT_DEBUG_DRAW_SCENE_LUMINANCE, };