From 4d8d87defcfea121d525bece347cbb2177f1e544 Mon Sep 17 00:00:00 2001 From: Joxno Date: Sat, 22 Oct 2022 21:27:57 +0100 Subject: [PATCH] Restored linear depth calculation Changed mode to push constant instead after feedback. Changed method of separate copy function to boolean flag instead after feedback. --- doc/classes/RenderingServer.xml | 45 ++++++++++--------- doc/classes/Viewport.xml | 42 ++++++++--------- scene/main/viewport.cpp | 1 + .../renderer_rd/effects/copy_effects.cpp | 43 +----------------- .../renderer_rd/effects/copy_effects.h | 5 ++- .../renderer_rd/renderer_scene_render_rd.cpp | 4 +- .../renderer_rd/shaders/effects/copy.glsl | 19 ++++---- 7 files changed, 64 insertions(+), 95 deletions(-) diff --git a/doc/classes/RenderingServer.xml b/doc/classes/RenderingServer.xml index 4ab511b5a908..097278c72ede 100644 --- a/doc/classes/RenderingServer.xml +++ b/doc/classes/RenderingServer.xml @@ -4727,69 +4727,72 @@ Normal buffer is drawn instead of regular scene so you can see the per-pixel normals that will be used by post-processing effects. - + + Depth buffer is drawn instead of regular scene so you can see the per-pixel depth. + + Objects are displayed with only the albedo value from [VoxelGI]s. - + Objects are displayed with only the lighting value from [VoxelGI]s. - + Objects are displayed with only the emission color from [VoxelGI]s. - + Draws the shadow atlas that stores shadows from [OmniLight3D]s and [SpotLight3D]s in the upper left quadrant of the [Viewport]. - + Draws the shadow atlas that stores shadows from [DirectionalLight3D]s in the upper left quadrant of the [Viewport]. The slice of the camera frustum related to the shadow map cascade is superimposed to visualize coverage. The color of each slice matches the colors used for [constant VIEWPORT_DEBUG_DRAW_PSSM_SPLITS]. When shadow cascades are blended the overlap is taken into account when drawing the frustum slices. The last cascade shows all frustum slices to illustrate the coverage of all slices. - + Draws the estimated scene luminance. This is a 1×1 texture that is generated when autoexposure is enabled to control the scene's exposure. - + Draws the screen space ambient occlusion texture instead of the scene so that you can clearly see how it is affecting objects. In order for this display mode to work, you must have [member Environment.ssao_enabled] set in your [WorldEnvironment]. - + Draws the screen space indirect lighting texture instead of the scene so that you can clearly see how it is affecting objects. In order for this display mode to work, you must have [member Environment.ssil_enabled] set in your [WorldEnvironment]. - + Colors each PSSM split for the [DirectionalLight3D]s in the scene a different color so you can see where the splits are. In order they will be colored red, green, blue, yellow. - + Draws the decal atlas that stores decal textures from [Decal]s. - + Draws SDFGI cascade data. This is the data structure that is used to bounce lighting against and create reflections. - + Draws SDFGI probe data. This is the data structure that is used to give indirect lighting dynamic objects moving within the scene. - + Draws the global illumination buffer ([VoxelGI] or SDFGI). - + Disable mesh LOD. All meshes are drawn with full detail, which can be used to compare performance. - + Draws the [OmniLight3D] cluster. Clustering determines where lights are positioned in screen-space, which allows the engine to only process these portions of the screen for lighting. - + Draws the [SpotLight3D] cluster. Clustering determines where lights are positioned in screen-space, which allows the engine to only process these portions of the screen for lighting. - + Draws the [Decal] cluster. Clustering determines where decals are positioned in screen-space, which allows the engine to only process these portions of the screen for decals. - + Draws the [ReflectionProbe] cluster. Clustering determines where reflection probes are positioned in screen-space, which allows the engine to only process these portions of the screen for reflection probes. - + Draws the occlusion culling buffer. This low-resolution occlusion culling buffer is rasterized on the CPU and is used to check whether instances are occluded by other objects. - + Draws the motion vectors buffer. This is used by temporal antialiasing to correct for motion that occurs during gameplay. - + Internal buffer is drawn instead of regular scene so you can see the per-pixel output that will be used by post-processing effects. diff --git a/doc/classes/Viewport.xml b/doc/classes/Viewport.xml index 6d8ada9370d3..b5ed1e693665 100644 --- a/doc/classes/Viewport.xml +++ b/doc/classes/Viewport.xml @@ -519,54 +519,56 @@ - + + + Objects are displayed with only the albedo value from [VoxelGI]s. - + Objects are displayed with only the lighting value from [VoxelGI]s. - + Objects are displayed with only the emission color from [VoxelGI]s. - + Draws the shadow atlas that stores shadows from [OmniLight3D]s and [SpotLight3D]s in the upper left quadrant of the [Viewport]. - + Draws the shadow atlas that stores shadows from [DirectionalLight3D]s in the upper left quadrant of the [Viewport]. - + - + Draws the screen-space ambient occlusion texture instead of the scene so that you can clearly see how it is affecting objects. In order for this display mode to work, you must have [member Environment.ssao_enabled] set in your [WorldEnvironment]. - + Draws the screen-space indirect lighting texture instead of the scene so that you can clearly see how it is affecting objects. In order for this display mode to work, you must have [member Environment.ssil_enabled] set in your [WorldEnvironment]. - + Colors each PSSM split for the [DirectionalLight3D]s in the scene a different color so you can see where the splits are. In order, they will be colored red, green, blue, and yellow. - + Draws the decal atlas used by [Decal]s and light projector textures in the upper left quadrant of the [Viewport]. - + - + - + - + - + - + - + - + - + - + Draws the internal resolution buffer of the scene before post-processing is applied. diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 9e8047ce4987..97aad3bd7672 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -4848,6 +4848,7 @@ void Viewport::_bind_methods() { BIND_ENUM_CONSTANT(DEBUG_DRAW_OVERDRAW); BIND_ENUM_CONSTANT(DEBUG_DRAW_WIREFRAME); BIND_ENUM_CONSTANT(DEBUG_DRAW_NORMAL_BUFFER); + BIND_ENUM_CONSTANT(DEBUG_DRAW_DEPTH_BUFFER); BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_ALBEDO); BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_LIGHTING); BIND_ENUM_CONSTANT(DEBUG_DRAW_VOXEL_GI_EMISSION); diff --git a/servers/rendering/renderer_rd/effects/copy_effects.cpp b/servers/rendering/renderer_rd/effects/copy_effects.cpp index 9d67f5593ef1..e2a998ac7948 100644 --- a/servers/rendering/renderer_rd/effects/copy_effects.cpp +++ b/servers/rendering/renderer_rd/effects/copy_effects.cpp @@ -86,7 +86,6 @@ CopyEffects::CopyEffects(bool p_prefer_raster_effects) { copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n"); copy_modes.push_back("\n#define MODE_MIPMAP\n"); copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n"); - copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY_INVERSED\n"); copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n"); copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n"); @@ -477,6 +476,7 @@ void CopyEffects::copy_depth_to_rect_and_linearize(RID p_source_rd_texture, RID copy.push_constant.target[1] = p_rect.position.y; copy.push_constant.camera_z_far = p_z_far; copy.push_constant.camera_z_near = p_z_near; + copy.push_constant.inverse_depth = p_inverse_depth; // setup our uniforms RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); @@ -497,47 +497,6 @@ void CopyEffects::copy_depth_to_rect_and_linearize(RID p_source_rd_texture, RID RD::get_singleton()->compute_list_end(); } -void CopyEffects::copy_depth_to_rect_and_linearize_reversed(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, float p_z_near, float p_z_far) { - ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the copy_depth_to_rect_and_linearize shader with the mobile renderer."); - - UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); - ERR_FAIL_NULL(uniform_set_cache); - MaterialStorage *material_storage = MaterialStorage::get_singleton(); - ERR_FAIL_NULL(material_storage); - - memset(©.push_constant, 0, sizeof(CopyPushConstant)); - if (p_flip_y) { - copy.push_constant.flags |= COPY_FLAG_FLIP_Y; - } - - copy.push_constant.section[0] = 0; - copy.push_constant.section[1] = 0; - copy.push_constant.section[2] = p_rect.size.width; - copy.push_constant.section[3] = p_rect.size.height; - copy.push_constant.target[0] = p_rect.position.x; - copy.push_constant.target[1] = p_rect.position.y; - copy.push_constant.camera_z_far = p_z_far; - copy.push_constant.camera_z_near = p_z_near; - - // setup our uniforms - RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); - - RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector({ default_sampler, p_source_rd_texture })); - RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture); - - CopyMode mode = COPY_MODE_LINEARIZE_DEPTH_INVERSED; - RID shader = copy.shader.version_get_shader(copy.shader_version, mode); - ERR_FAIL_COND(shader.is_null()); - - RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); - RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]); - RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0); - RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3); - RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant)); - RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1); - RD::get_singleton()->compute_list_end(); -} - void CopyEffects::copy_to_atlas_fb(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_uv_rect, RD::DrawListID p_draw_list, bool p_flip_y, bool p_panorama) { UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); ERR_FAIL_NULL(uniform_set_cache); diff --git a/servers/rendering/renderer_rd/effects/copy_effects.h b/servers/rendering/renderer_rd/effects/copy_effects.h index f13945ad444f..31bf5aea60dd 100644 --- a/servers/rendering/renderer_rd/effects/copy_effects.h +++ b/servers/rendering/renderer_rd/effects/copy_effects.h @@ -117,7 +117,6 @@ class CopyEffects { COPY_MODE_SET_COLOR_8BIT, COPY_MODE_MIPMAP, COPY_MODE_LINEARIZE_DEPTH, - COPY_MODE_LINEARIZE_DEPTH_INVERSED, COPY_MODE_CUBE_TO_PANORAMA, COPY_MODE_CUBE_ARRAY_TO_PANORAMA, COPY_MODE_MAX, @@ -157,6 +156,9 @@ class CopyEffects { uint32_t pad2[2]; //SET color float set_color[4]; + // Depth. + bool inverse_depth; + uint32_t pad3[3]; }; struct Copy { @@ -328,7 +330,6 @@ class CopyEffects { void copy_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y = false, bool p_force_luminance = false, bool p_all_source = false, bool p_8_bit_dst = false, bool p_alpha_to_one = false); void copy_cubemap_to_panorama(RID p_source_cube, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array); - void copy_depth_to_rect_and_linearize_reversed(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, float p_z_near, float p_z_far); void copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2i &p_rect, bool p_flip_y = false); void copy_depth_to_rect_and_linearize(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, float p_z_near, float p_z_far); void copy_to_fb_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2i &p_rect, bool p_flip_y = false, bool p_force_luminance = false, bool p_alpha_to_zero = false, bool p_srgb = false, RID p_secondary = RID(), bool p_multiview = false, bool alpha_to_one = false, bool p_linear = false, bool p_normal = false); diff --git a/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp b/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp index 44363bac4f22..1c2ac51eb691 100644 --- a/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp @@ -785,12 +785,12 @@ void RendererSceneRenderRD::_render_buffers_debug_draw(const RenderDataRD *p_ren RID debug_depth_texture = p_render_buffers->has_texture(RB_SCOPE_FORWARD_CLUSTERED, SNAME("debug_depth")) ? p_render_buffers->get_texture(RB_SCOPE_FORWARD_CLUSTERED, SNAME("debug_depth")) : p_render_buffers->create_texture(RB_SCOPE_FORWARD_CLUSTERED, SNAME("debug_depth"), format, usage_bits, texture_samples); - copy_effects->copy_depth_to_rect_and_linearize_reversed( + copy_effects->copy_depth_to_rect_and_linearize( _render_buffers_get_depth_texture(p_render_buffers), debug_depth_texture, Rect2(Vector2(), rtsize), false, - 0.1, 1.0); + 0.1, 1.0, true); copy_effects->copy_to_fb_rect( debug_depth_texture, texture_storage->render_target_get_rd_framebuffer(render_target), diff --git a/servers/rendering/renderer_rd/shaders/effects/copy.glsl b/servers/rendering/renderer_rd/shaders/effects/copy.glsl index 779ed6c60e8c..102a5a7db359 100644 --- a/servers/rendering/renderer_rd/shaders/effects/copy.glsl +++ b/servers/rendering/renderer_rd/shaders/effects/copy.glsl @@ -36,7 +36,12 @@ layout(push_constant, std430) uniform Params { float camera_z_near; uint pad2[2]; + // Color vec4 set_color; + + // Depth. + bool inverse_depth; + uint pad3[3]; } params; @@ -242,18 +247,16 @@ void main() { #endif // MODE_SIMPLE_COPY_DEPTH -#if defined(MODE_LINEARIZE_DEPTH_COPY) || defined(MODE_LINEARIZE_DEPTH_COPY_INVERSED) +#ifdef MODE_LINEARIZE_DEPTH_COPY float depth = texelFetch(source_color, pos + params.section.xy, 0).r; + depth = 2.0 * params.camera_z_near * params.camera_z_far / + (params.camera_z_far + params.camera_z_near - depth * (params.camera_z_far - params.camera_z_near)); - depth = params.camera_z_near * params.camera_z_far / - (params.camera_z_far + depth * (params.camera_z_near - params.camera_z_far)); - -#ifdef MODE_LINEARIZE_DEPTH_COPY_INVERSED - vec4 color = vec4(1.0 - (depth / params.camera_z_far)); -#else vec4 color = vec4(depth / params.camera_z_far); -#endif + if (params.inverse_depth) { + color = 1.0 - color; + } if (bool(params.flags & FLAG_FLIP_Y)) { pos.y = params.section.w - pos.y - 1;