From c7656978ba66e1a90029c7e58ed757ff0a3c872f Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Thu, 1 Sep 2022 18:10:53 +1000 Subject: Adding getters to RenderTarget and implementing override functionality for XR --- .../storage_rd/render_scene_buffers_rd.cpp | 58 ++++++- .../storage_rd/render_scene_buffers_rd.h | 12 +- .../renderer_rd/storage_rd/texture_storage.cpp | 166 +++++++++++++++++---- .../renderer_rd/storage_rd/texture_storage.h | 64 +++++++- 4 files changed, 256 insertions(+), 44 deletions(-) (limited to 'servers/rendering/renderer_rd/storage_rd') 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 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 &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 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 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 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 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 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); -- cgit v1.2.3