diff options
Diffstat (limited to 'servers/rendering')
11 files changed, 313 insertions, 73 deletions
diff --git a/servers/rendering/dummy/storage/texture_storage.h b/servers/rendering/dummy/storage/texture_storage.h index f5bdccca68..98acd5ad74 100644 --- a/servers/rendering/dummy/storage/texture_storage.h +++ b/servers/rendering/dummy/storage/texture_storage.h @@ -157,14 +157,17 @@ public: virtual RID render_target_create() override { return RID(); } virtual void render_target_free(RID p_rid) override {} virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) override {} + virtual Point2i render_target_get_position(RID p_render_target) const override { return Point2i(); } virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override {} - virtual RID render_target_get_texture(RID p_render_target) override { return RID(); } - virtual void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) override {} + virtual Size2i render_target_get_size(RID p_render_target) const override { return Size2i(); } virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override {} + virtual bool render_target_get_transparent(RID p_render_target) const override { return false; } virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override {} - virtual bool render_target_was_used(RID p_render_target) override { return false; } + virtual bool render_target_get_direct_to_screen(RID p_render_target) const override { return false; } + virtual bool render_target_was_used(RID p_render_target) const override { return false; } virtual void render_target_set_as_unused(RID p_render_target) override {} virtual void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) override {} + virtual RS::ViewportMSAA render_target_get_msaa(RID p_render_target) const override { return RS::VIEWPORT_MSAA_DISABLED; } virtual void render_target_request_clear(RID p_render_target, const Color &p_clear_color) override {} virtual bool render_target_is_clear_requested(RID p_render_target) override { return false; } @@ -176,8 +179,19 @@ public: virtual Rect2i render_target_get_sdf_rect(RID p_render_target) const override { return Rect2i(); } virtual void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) override {} - virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) override{}; - virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override{}; + virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) override {} + virtual RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const override { return RS::VIEWPORT_VRS_DISABLED; } + virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override {} + virtual RID render_target_get_vrs_texture(RID p_render_target) const override { return RID(); } + + virtual void render_target_set_override_color(RID p_render_target, RID p_texture) override {} + virtual RID render_target_get_override_color(RID p_render_target) const override { return RID(); } + virtual void render_target_set_override_depth(RID p_render_target, RID p_texture) override {} + virtual RID render_target_get_override_depth(RID p_render_target) const override { return RID(); } + virtual void render_target_set_override_velocity(RID p_render_target, RID p_texture) override {} + virtual RID render_target_get_override_velocity(RID p_render_target) const override { return RID(); } + + virtual RID render_target_get_texture(RID p_render_target) override { return RID(); } }; } // namespace RendererDummy diff --git a/servers/rendering/renderer_rd/environment/gi.cpp b/servers/rendering/renderer_rd/environment/gi.cpp index 0853460861..ee6e5c27f2 100644 --- a/servers/rendering/renderer_rd/environment/gi.cpp +++ b/servers/rendering/renderer_rd/environment/gi.cpp @@ -1644,8 +1644,8 @@ void GI::SDFGI::debug_draw(uint32_t p_view_count, const Projection *p_projection RD::get_singleton()->compute_list_end(); } - Size2 rtsize = texture_storage->render_target_get_size(p_render_target); - copy_effects->copy_to_fb_rect(p_texture, texture_storage->render_target_get_rd_framebuffer(p_render_target), Rect2(Vector2(), rtsize), true, false, false, false, RID(), p_view_count > 1); + Size2i rtsize = texture_storage->render_target_get_size(p_render_target); + copy_effects->copy_to_fb_rect(p_texture, texture_storage->render_target_get_rd_framebuffer(p_render_target), Rect2i(Point2i(), rtsize), true, false, false, false, RID(), p_view_count > 1); } void GI::SDFGI::debug_probes(RID p_framebuffer, const uint32_t p_view_count, const Projection *p_camera_with_transforms, bool p_will_continue_color, bool p_will_continue_depth) { diff --git a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp index c1a7818921..cf08949d99 100644 --- a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp +++ b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp @@ -2095,17 +2095,17 @@ void RenderForwardClustered::_render_buffers_debug_draw(Ref<RenderSceneBuffersRD RID render_target = p_render_buffers->get_render_target(); if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SSAO && rb_data->ss_effects_data.ssao.ao_final.is_valid()) { - Size2 rtsize = texture_storage->render_target_get_size(render_target); + Size2i rtsize = texture_storage->render_target_get_size(render_target); copy_effects->copy_to_fb_rect(rb_data->ss_effects_data.ssao.ao_final, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, true); } if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_SSIL && rb_data->ss_effects_data.ssil.ssil_final.is_valid()) { - Size2 rtsize = texture_storage->render_target_get_size(render_target); + Size2i rtsize = texture_storage->render_target_get_size(render_target); copy_effects->copy_to_fb_rect(rb_data->ss_effects_data.ssil.ssil_final, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, false); } if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_GI_BUFFER && p_render_buffers->has_texture(RB_SCOPE_GI, RB_TEX_AMBIENT)) { - Size2 rtsize = texture_storage->render_target_get_size(render_target); + Size2i rtsize = texture_storage->render_target_get_size(render_target); RID ambient_texture = p_render_buffers->get_texture(RB_SCOPE_GI, RB_TEX_AMBIENT); RID reflection_texture = p_render_buffers->get_texture(RB_SCOPE_GI, RB_TEX_REFLECTION); copy_effects->copy_to_fb_rect(ambient_texture, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, false, false, true, reflection_texture, p_render_buffers->get_view_count() > 1); diff --git a/servers/rendering/renderer_rd/renderer_compositor_rd.cpp b/servers/rendering/renderer_rd/renderer_compositor_rd.cpp index a50742f91a..dd3f62e509 100644 --- a/servers/rendering/renderer_rd/renderer_compositor_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_compositor_rd.cpp @@ -44,13 +44,9 @@ void RendererCompositorRD::blit_render_targets_to_screen(DisplayServer::WindowID } for (int i = 0; i < p_amount; i++) { - RID texture = texture_storage->render_target_get_texture(p_render_targets[i].render_target); - ERR_CONTINUE(texture.is_null()); - RID rd_texture = texture_storage->texture_get_rd_texture(texture); + RID rd_texture = texture_storage->render_target_get_rd_texture(p_render_targets[i].render_target); ERR_CONTINUE(rd_texture.is_null()); - // TODO if keep_3d_linear was set when rendering to this render target we need to add a linear->sRGB conversion in. - if (!render_target_descriptors.has(rd_texture) || !RD::get_singleton()->uniform_set_is_valid(render_target_descriptors[rd_texture])) { Vector<RD::Uniform> uniforms; RD::Uniform u; diff --git a/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp b/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp index acc964b3f3..6157d7d840 100644 --- a/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_scene_render_rd.cpp @@ -828,7 +828,7 @@ void RendererSceneRenderRD::_render_buffers_debug_draw(Ref<RenderSceneBuffersRD> if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS) { if (RendererRD::LightStorage::get_singleton()->directional_shadow_get_texture().is_valid()) { RID shadow_atlas_texture = RendererRD::LightStorage::get_singleton()->directional_shadow_get_texture(); - Size2 rtsize = texture_storage->render_target_get_size(render_target); + Size2i rtsize = texture_storage->render_target_get_size(render_target); copy_effects->copy_to_fb_rect(shadow_atlas_texture, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2i(Vector2(), rtsize / 2), false, true); } @@ -838,7 +838,7 @@ void RendererSceneRenderRD::_render_buffers_debug_draw(Ref<RenderSceneBuffersRD> RID decal_atlas = RendererRD::TextureStorage::get_singleton()->decal_atlas_get_texture(); if (decal_atlas.is_valid()) { - Size2 rtsize = texture_storage->render_target_get_size(render_target); + Size2i rtsize = texture_storage->render_target_get_size(render_target); copy_effects->copy_to_fb_rect(decal_atlas, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2i(Vector2(), rtsize / 2), false, false, true); } @@ -846,7 +846,7 @@ void RendererSceneRenderRD::_render_buffers_debug_draw(Ref<RenderSceneBuffersRD> if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_SCENE_LUMINANCE) { if (p_render_buffers->luminance.current.is_valid()) { - Size2 rtsize = texture_storage->render_target_get_size(render_target); + Size2i rtsize = texture_storage->render_target_get_size(render_target); copy_effects->copy_to_fb_rect(p_render_buffers->luminance.current, texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize / 8), false, true); } @@ -859,13 +859,13 @@ void RendererSceneRenderRD::_render_buffers_debug_draw(Ref<RenderSceneBuffersRD> if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_OCCLUDERS) { if (p_occlusion_buffer.is_valid()) { - Size2 rtsize = texture_storage->render_target_get_size(render_target); + Size2i rtsize = texture_storage->render_target_get_size(render_target); copy_effects->copy_to_fb_rect(texture_storage->texture_get_rd_texture(p_occlusion_buffer), texture_storage->render_target_get_rd_framebuffer(render_target), Rect2i(Vector2(), rtsize), true, false); } } if (debug_draw == RS::VIEWPORT_DEBUG_DRAW_MOTION_VECTORS && _render_buffers_get_velocity_texture(p_render_buffers).is_valid()) { - Size2 rtsize = texture_storage->render_target_get_size(render_target); + Size2i rtsize = texture_storage->render_target_get_size(render_target); copy_effects->copy_to_fb_rect(_render_buffers_get_velocity_texture(p_render_buffers), texture_storage->render_target_get_rd_framebuffer(render_target), Rect2(Vector2(), rtsize), false, false); } } diff --git a/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp index 1b2237b505..0c2092f03e 100644 --- a/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp +++ b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp @@ -178,7 +178,7 @@ void RenderSceneBuffersRD::configure(RID p_render_target, const Size2i p_interna // Create our depth buffer { - // TODO If we have depth buffer supplied externally, pick this up + // TODO Lazy create this in case we've got an external depth buffer RD::DataFormat format; uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT; @@ -490,6 +490,28 @@ Ref<RenderBufferCustomDataRD> RenderSceneBuffersRD::get_custom_data(const String return ret; } +// Depth texture + +RID RenderSceneBuffersRD::get_depth_texture() { + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + RID depth = texture_storage->render_target_get_override_depth(render_target); + if (depth.is_valid()) { + return depth; + } else { + return get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH); + } +} + +RID RenderSceneBuffersRD::get_depth_texture(const uint32_t p_layer) { + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + RID depth_slice = texture_storage->render_target_get_override_depth_slice(render_target, p_layer); + if (depth_slice.is_valid()) { + return depth_slice; + } else { + return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_DEPTH, p_layer, 0); + } +} + // Velocity texture. void RenderSceneBuffersRD::ensure_velocity() { @@ -516,6 +538,20 @@ void RenderSceneBuffersRD::ensure_velocity() { } } +bool RenderSceneBuffersRD::has_velocity_buffer(bool p_has_msaa) { + if (p_has_msaa) { + return has_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY_MSAA); + } else { + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + RID velocity = texture_storage->render_target_get_override_velocity(render_target); + if (velocity.is_valid()) { + return true; + } else { + return has_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY); + } + } +} + RID RenderSceneBuffersRD::get_velocity_buffer(bool p_get_msaa) { if (p_get_msaa) { if (!has_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY_MSAA)) { @@ -524,10 +560,28 @@ RID RenderSceneBuffersRD::get_velocity_buffer(bool p_get_msaa) { return get_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY_MSAA); } } else { - if (!has_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY)) { + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + RID velocity = texture_storage->render_target_get_override_velocity(render_target); + if (velocity.is_valid()) { + return velocity; + } else if (!has_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY)) { return RID(); } else { return get_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY); } } } + +RID RenderSceneBuffersRD::get_velocity_buffer(bool p_get_msaa, uint32_t p_layer) { + if (p_get_msaa) { + return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY_MSAA, p_layer, 0); + } else { + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + RID velocity_slice = texture_storage->render_target_get_override_velocity_slice(render_target, p_layer); + if (velocity_slice.is_valid()) { + return velocity_slice; + } else { + return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY, p_layer, 0); + } + } +} diff --git a/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h index 169ee2e2b1..6907f69b93 100644 --- a/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h +++ b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h @@ -189,12 +189,8 @@ public: return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_COLOR, p_layer, 0); } - _FORCE_INLINE_ RID get_depth_texture() const { - return get_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH); - } - _FORCE_INLINE_ RID get_depth_texture(const uint32_t p_layer) { - return get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_DEPTH, p_layer, 0); - } + RID get_depth_texture(); + RID get_depth_texture(const uint32_t p_layer); // back buffer (color) RID get_back_buffer_texture() const { return has_texture(RB_SCOPE_BUFFERS, RB_TEX_BLUR_0) ? get_texture(RB_SCOPE_BUFFERS, RB_TEX_BLUR_0) : RID(); } // We (re)use our blur texture here. @@ -202,9 +198,9 @@ public: // Velocity, currently only used by TAA (Clustered) but we'll be using this in other places soon too. void ensure_velocity(); - bool has_velocity_buffer(bool p_has_msaa) { return has_texture(RB_SCOPE_BUFFERS, p_has_msaa ? RB_TEX_VELOCITY_MSAA : RB_TEX_VELOCITY); } + bool has_velocity_buffer(bool p_has_msaa); RID get_velocity_buffer(bool p_get_msaa); - RID get_velocity_buffer(bool p_get_msaa, uint32_t p_layer) { return get_texture_slice(RB_SCOPE_BUFFERS, p_get_msaa ? RB_TEX_VELOCITY_MSAA : RB_TEX_VELOCITY, p_layer, 0); } + RID get_velocity_buffer(bool p_get_msaa, uint32_t p_layer); //////////////////////////////////////////////////////////////////////////////////////////////////////////// // Everything after this needs to be re-evaluated, this is all old implementation diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp index 14e41a0d6b..b3a7e05ff5 100644 --- a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp +++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp @@ -30,6 +30,7 @@ #include "texture_storage.h" #include "../effects/copy_effects.h" +#include "../framebuffer_cache_rd.h" #include "material_storage.h" #include "servers/rendering/renderer_rd/renderer_scene_render_rd.h" @@ -2359,10 +2360,26 @@ void TextureStorage::update_decal_buffer(const PagedArray<RID> &p_decals, const /* RENDER TARGET API */ +RID TextureStorage::RenderTarget::get_framebuffer() { + // Note that if we're using an overridden color buffer, we're likely cycling through a texture chain. + // this is where our framebuffer cache comes in clutch.. + + if (msaa != RS::VIEWPORT_MSAA_DISABLED) { + return FramebufferCacheRD::get_singleton()->get_cache_multiview(view_count, color_multisample, overridden.color.is_valid() ? overridden.color : color); + } else { + return FramebufferCacheRD::get_singleton()->get_cache_multiview(view_count, overridden.color.is_valid() ? overridden.color : color); + } +} + void TextureStorage::_clear_render_target(RenderTarget *rt) { - //free in reverse dependency order - if (rt->framebuffer.is_valid()) { - RD::get_singleton()->free(rt->framebuffer); + // clear overrides, we assume these are freed by the object that created them + rt->overridden.color = RID(); + rt->overridden.depth = RID(); + rt->overridden.velocity = RID(); + rt->overridden.cached_slices.clear(); // these are automatically freed when their parent textures are freed so just clear + + // free in reverse dependency order + if (rt->framebuffer_uniform_set.is_valid()) { rt->framebuffer_uniform_set = RID(); //chain deleted } @@ -2384,7 +2401,6 @@ void TextureStorage::_clear_render_target(RenderTarget *rt) { _render_target_clear_sdf(rt); - rt->framebuffer = RID(); rt->color = RID(); rt->color_multisample = RID(); } @@ -2432,11 +2448,10 @@ void TextureStorage::_update_render_target(RenderTarget *rt) { } } + // TODO see if we can lazy create this once we actually use it as we may not need to create this if we have an overridden color buffer... rt->color = RD::get_singleton()->texture_create(rd_color_attachment_format, rd_view); ERR_FAIL_COND(rt->color.is_null()); - Vector<RID> fb_textures; - if (rt->msaa != RS::VIEWPORT_MSAA_DISABLED) { // Use the texture format of the color attachment for the multisample color attachment. RD::TextureFormat rd_color_multisample_format = rd_color_attachment_format; @@ -2450,15 +2465,8 @@ void TextureStorage::_update_render_target(RenderTarget *rt) { RD::TextureView rd_view_multisample; rd_color_multisample_format.is_resolve_buffer = false; rt->color_multisample = RD::get_singleton()->texture_create(rd_color_multisample_format, rd_view_multisample); - fb_textures.push_back(rt->color_multisample); ERR_FAIL_COND(rt->color_multisample.is_null()); } - fb_textures.push_back(rt->color); - rt->framebuffer = RD::get_singleton()->framebuffer_create(fb_textures, RenderingDevice::INVALID_ID, rt->view_count); - if (rt->framebuffer.is_null()) { - _clear_render_target(rt); - ERR_FAIL_COND(rt->framebuffer.is_null()); - } { //update texture @@ -2568,6 +2576,11 @@ void TextureStorage::render_target_set_position(RID p_render_target, int p_x, in //unused for this render target } +Point2i TextureStorage::render_target_get_position(RID p_render_target) const { + //unused for this render target + return Point2i(); +} + void TextureStorage::render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); ERR_FAIL_COND(!rt); @@ -2579,6 +2592,13 @@ void TextureStorage::render_target_set_size(RID p_render_target, int p_width, in } } +Size2i TextureStorage::render_target_get_size(RID p_render_target) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND_V(!rt, Size2i()); + + return rt->size; +} + RID TextureStorage::render_target_get_texture(RID p_render_target) { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); ERR_FAIL_COND_V(!rt, RID()); @@ -2586,7 +2606,84 @@ RID TextureStorage::render_target_get_texture(RID p_render_target) { return rt->texture; } -void TextureStorage::render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) { +void TextureStorage::render_target_set_override_color(RID p_render_target, RID p_texture) { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND(!rt); + + rt->overridden.color = p_texture; +} + +RID TextureStorage::render_target_get_override_color(RID p_render_target) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND_V(!rt, RID()); + + return rt->overridden.color; +} + +void TextureStorage::render_target_set_override_depth(RID p_render_target, RID p_texture) { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND(!rt); + + rt->overridden.depth = p_texture; +} + +RID TextureStorage::render_target_get_override_depth(RID p_render_target) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND_V(!rt, RID()); + + return rt->overridden.depth; +} + +RID TextureStorage::render_target_get_override_depth_slice(RID p_render_target, const uint32_t p_layer) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND_V(!rt, RID()); + + if (rt->overridden.depth.is_null()) { + return RID(); + } else if (rt->view_count == 1) { + return rt->overridden.depth; + } else { + RenderTarget::RTOverridden::SliceKey key(rt->overridden.depth, p_layer); + + if (!rt->overridden.cached_slices.has(key)) { + rt->overridden.cached_slices[key] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->overridden.depth, p_layer, 0); + } + + return rt->overridden.cached_slices[key]; + } +} + +void TextureStorage::render_target_set_override_velocity(RID p_render_target, RID p_texture) { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND(!rt); + + rt->overridden.velocity = p_texture; +} + +RID TextureStorage::render_target_get_override_velocity(RID p_render_target) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND_V(!rt, RID()); + + return rt->overridden.velocity; +} + +RID TextureStorage::render_target_get_override_velocity_slice(RID p_render_target, const uint32_t p_layer) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND_V(!rt, RID()); + + if (rt->overridden.velocity.is_null()) { + return RID(); + } else if (rt->view_count == 1) { + return rt->overridden.velocity; + } else { + RenderTarget::RTOverridden::SliceKey key(rt->overridden.velocity, p_layer); + + if (!rt->overridden.cached_slices.has(key)) { + rt->overridden.cached_slices[key] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->overridden.velocity, p_layer, 0); + } + + return rt->overridden.cached_slices[key]; + } } void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_is_transparent) { @@ -2596,10 +2693,21 @@ void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_i _update_render_target(rt); } +bool TextureStorage::render_target_get_transparent(RID p_render_target) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND_V(!rt, false); + + return rt->is_transparent; +} + void TextureStorage::render_target_set_direct_to_screen(RID p_render_target, bool p_value) { } -bool TextureStorage::render_target_was_used(RID p_render_target) { +bool TextureStorage::render_target_get_direct_to_screen(RID p_render_target) const { + return false; +} + +bool TextureStorage::render_target_was_used(RID p_render_target) const { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); ERR_FAIL_COND_V(!rt, false); return rt->was_used; @@ -2622,25 +2730,29 @@ void TextureStorage::render_target_set_msaa(RID p_render_target, RS::ViewportMSA _update_render_target(rt); } -Size2 TextureStorage::render_target_get_size(RID p_render_target) { +RS::ViewportMSAA TextureStorage::render_target_get_msaa(RID p_render_target) const { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); - ERR_FAIL_COND_V(!rt, Size2()); + ERR_FAIL_COND_V(!rt, RS::VIEWPORT_MSAA_DISABLED); - return rt->size; + return rt->msaa; } RID TextureStorage::render_target_get_rd_framebuffer(RID p_render_target) { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); ERR_FAIL_COND_V(!rt, RID()); - return rt->framebuffer; + return rt->get_framebuffer(); } RID TextureStorage::render_target_get_rd_texture(RID p_render_target) { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); ERR_FAIL_COND_V(!rt, RID()); - return rt->color; + if (rt->overridden.color.is_valid()) { + return rt->overridden.color; + } else { + return rt->color; + } } RID TextureStorage::render_target_get_rd_texture_slice(RID p_render_target, uint32_t p_layer) { @@ -2711,7 +2823,7 @@ void TextureStorage::render_target_do_clear_request(RID p_render_target) { } Vector<Color> clear_colors; clear_colors.push_back(rt->clear_color); - RD::get_singleton()->draw_list_begin(rt->framebuffer, RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, clear_colors); + RD::get_singleton()->draw_list_begin(rt->get_framebuffer(), RD::INITIAL_ACTION_CLEAR, RD::FINAL_ACTION_READ, RD::INITIAL_ACTION_KEEP, RD::FINAL_ACTION_DISCARD, clear_colors); RD::get_singleton()->draw_list_end(); rt->clear_requested = false; } @@ -3140,18 +3252,18 @@ void TextureStorage::render_target_set_vrs_mode(RID p_render_target, RS::Viewpor rt->vrs_mode = p_mode; } -void TextureStorage::render_target_set_vrs_texture(RID p_render_target, RID p_texture) { +RS::ViewportVRSMode TextureStorage::render_target_get_vrs_mode(RID p_render_target) const { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); - ERR_FAIL_COND(!rt); + ERR_FAIL_COND_V(!rt, RS::VIEWPORT_VRS_DISABLED); - rt->vrs_texture = p_texture; + return rt->vrs_mode; } -RS::ViewportVRSMode TextureStorage::render_target_get_vrs_mode(RID p_render_target) const { +void TextureStorage::render_target_set_vrs_texture(RID p_render_target, RID p_texture) { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); - ERR_FAIL_COND_V(!rt, RS::VIEWPORT_VRS_DISABLED); + ERR_FAIL_COND(!rt); - return rt->vrs_mode; + rt->vrs_texture = p_texture; } RID TextureStorage::render_target_get_vrs_texture(RID p_render_target) const { diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.h b/servers/rendering/renderer_rd/storage_rd/texture_storage.h index 327a21a1fc..00b4e50737 100644 --- a/servers/rendering/renderer_rd/storage_rd/texture_storage.h +++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.h @@ -301,7 +301,6 @@ private: struct RenderTarget { Size2i size; uint32_t view_count; - RID framebuffer; RID color; Vector<RID> color_slices; RID color_multisample; // Needed when MSAA is enabled. @@ -339,6 +338,43 @@ private: RS::ViewportVRSMode vrs_mode = RS::VIEWPORT_VRS_DISABLED; RID vrs_texture; + // overridden textures + struct RTOverridden { + RID color; + RID depth; + RID velocity; + + // In a multiview scenario, which is the most likely where we + // override our destination textures, we need to obtain slices + // for each layer of these textures. + // These are likely changing every frame as we loop through + // texture chains hence we add a cache to manage these slices. + // For this we define a key using the RID of the texture and + // the layer for which we create a slice. + struct SliceKey { + RID rid; + uint32_t layer = 0; + + bool operator==(const SliceKey &p_val) const { + return (rid == p_val.rid) && (layer == p_val.layer); + } + + static uint32_t hash(const SliceKey &p_val) { + uint32_t h = hash_one_uint64(p_val.rid.get_id()); + h = hash_murmur3_one_32(p_val.layer, h); + return hash_fmix32(h); + } + + SliceKey() {} + SliceKey(RID p_rid, uint32_t p_layer) { + rid = p_rid; + layer = p_layer; + } + }; + + mutable HashMap<SliceKey, RID, SliceKey> cached_slices; + } overridden; + //texture generated for this owner (nor RD). RID texture; bool was_used; @@ -346,6 +382,8 @@ private: //clear request bool clear_requested; Color clear_color; + + RID get_framebuffer(); }; mutable RID_Owner<RenderTarget> render_target_owner; @@ -644,14 +682,17 @@ public: virtual void render_target_free(RID p_rid) override; virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) override; + virtual Point2i render_target_get_position(RID p_render_target) const override; virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) override; - virtual RID render_target_get_texture(RID p_render_target) override; - virtual void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) override; + virtual Size2i render_target_get_size(RID p_render_target) const override; virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) override; + virtual bool render_target_get_transparent(RID p_render_target) const override; virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) override; - virtual bool render_target_was_used(RID p_render_target) override; + virtual bool render_target_get_direct_to_screen(RID p_render_target) const override; + virtual bool render_target_was_used(RID p_render_target) const override; virtual void render_target_set_as_unused(RID p_render_target) override; virtual void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) override; + virtual RS::ViewportMSAA render_target_get_msaa(RID p_render_target) const override; void render_target_copy_to_back_buffer(RID p_render_target, const Rect2i &p_region, bool p_gen_mipmaps); void render_target_clear_back_buffer(RID p_render_target, const Rect2i &p_region, const Color &p_color); @@ -673,12 +714,21 @@ public: bool render_target_is_sdf_enabled(RID p_render_target) const; virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) override; + virtual RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const override; virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override; + virtual RID render_target_get_vrs_texture(RID p_render_target) const override; - RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const; - RID render_target_get_vrs_texture(RID p_render_target) const; + virtual void render_target_set_override_color(RID p_render_target, RID p_texture) override; + virtual RID render_target_get_override_color(RID p_render_target) const override; + virtual void render_target_set_override_depth(RID p_render_target, RID p_texture) override; + virtual RID render_target_get_override_depth(RID p_render_target) const override; + RID render_target_get_override_depth_slice(RID p_render_target, const uint32_t p_layer) const; + virtual void render_target_set_override_velocity(RID p_render_target, RID p_texture) override; + virtual RID render_target_get_override_velocity(RID p_render_target) const override; + RID render_target_get_override_velocity_slice(RID p_render_target, const uint32_t p_layer) const; + + virtual RID render_target_get_texture(RID p_render_target) override; - Size2 render_target_get_size(RID p_render_target); RID render_target_get_rd_framebuffer(RID p_render_target); RID render_target_get_rd_texture(RID p_render_target); RID render_target_get_rd_texture_slice(RID p_render_target, uint32_t p_layer); diff --git a/servers/rendering/renderer_viewport.cpp b/servers/rendering/renderer_viewport.cpp index 1db018b647..4f7683493b 100644 --- a/servers/rendering/renderer_viewport.cpp +++ b/servers/rendering/renderer_viewport.cpp @@ -664,9 +664,9 @@ void RendererViewport::draw_viewports() { RSG::texture_storage->render_target_set_as_unused(vp->render_target); if (vp->use_xr && xr_interface.is_valid()) { - // check for an external texture destination (disabled for now, not yet supported) - // RSG::texture_storage->render_target_set_external_texture(vp->render_target, xr_interface->get_external_texture_for_eye(leftOrMono)); - RSG::texture_storage->render_target_set_external_texture(vp->render_target, 0); + RSG::texture_storage->render_target_set_override_color(vp->render_target, xr_interface->get_color_texture()); + RSG::texture_storage->render_target_set_override_depth(vp->render_target, xr_interface->get_depth_texture()); + RSG::texture_storage->render_target_set_override_velocity(vp->render_target, xr_interface->get_velocity_texture()); // render... RSG::scene->set_debug_draw_mode(vp->debug_draw); @@ -695,7 +695,9 @@ void RendererViewport::draw_viewports() { } } } else { - RSG::texture_storage->render_target_set_external_texture(vp->render_target, 0); + RSG::texture_storage->render_target_set_override_color(vp->render_target, RID()); // TODO if fullscreen output, we can set this to our texture chain + RSG::texture_storage->render_target_set_override_depth(vp->render_target, RID()); + RSG::texture_storage->render_target_set_override_velocity(vp->render_target, RID()); RSG::scene->set_debug_draw_mode(vp->debug_draw); diff --git a/servers/rendering/storage/texture_storage.h b/servers/rendering/storage/texture_storage.h index 5024a76c09..635f44786c 100644 --- a/servers/rendering/storage/texture_storage.h +++ b/servers/rendering/storage/texture_storage.h @@ -131,15 +131,18 @@ public: virtual RID render_target_create() = 0; virtual void render_target_free(RID p_rid) = 0; - virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) = 0; - virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) = 0; - virtual RID render_target_get_texture(RID p_render_target) = 0; - virtual void render_target_set_external_texture(RID p_render_target, unsigned int p_texture_id) = 0; + virtual void render_target_set_position(RID p_render_target, int p_x, int p_y) = 0; // Q change input to const Point2i &p_position ? + virtual Point2i render_target_get_position(RID p_render_target) const = 0; + virtual void render_target_set_size(RID p_render_target, int p_width, int p_height, uint32_t p_view_count) = 0; // Q change input to const Size2i &p_size ? + virtual Size2i render_target_get_size(RID p_render_target) const = 0; virtual void render_target_set_transparent(RID p_render_target, bool p_is_transparent) = 0; + virtual bool render_target_get_transparent(RID p_render_target) const = 0; virtual void render_target_set_direct_to_screen(RID p_render_target, bool p_direct_to_screen) = 0; - virtual bool render_target_was_used(RID p_render_target) = 0; + virtual bool render_target_get_direct_to_screen(RID p_render_target) const = 0; + virtual bool render_target_was_used(RID p_render_target) const = 0; virtual void render_target_set_as_unused(RID p_render_target) = 0; virtual void render_target_set_msaa(RID p_render_target, RS::ViewportMSAA p_msaa) = 0; + virtual RS::ViewportMSAA render_target_get_msaa(RID p_render_target) const = 0; virtual void render_target_request_clear(RID p_render_target, const Color &p_clear_color) = 0; virtual bool render_target_is_clear_requested(RID p_render_target) = 0; @@ -152,7 +155,20 @@ public: virtual void render_target_mark_sdf_enabled(RID p_render_target, bool p_enabled) = 0; virtual void render_target_set_vrs_mode(RID p_render_target, RS::ViewportVRSMode p_mode) = 0; + virtual RS::ViewportVRSMode render_target_get_vrs_mode(RID p_render_target) const = 0; virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) = 0; + virtual RID render_target_get_vrs_texture(RID p_render_target) const = 0; + + // override color, depth and velocity buffers (depth and velocity only for 3D) + virtual void render_target_set_override_color(RID p_render_target, RID p_texture) = 0; + virtual RID render_target_get_override_color(RID p_render_target) const = 0; + virtual void render_target_set_override_depth(RID p_render_target, RID p_texture) = 0; + virtual RID render_target_get_override_depth(RID p_render_target) const = 0; + virtual void render_target_set_override_velocity(RID p_render_target, RID p_texture) = 0; + virtual RID render_target_get_override_velocity(RID p_render_target) const = 0; + + // get textures + virtual RID render_target_get_texture(RID p_render_target) = 0; }; #endif // TEXTURE_STORAGE_H |