From 2cd84be64da98b093ec248fd061702783adfd8b0 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Thu, 4 Aug 2022 18:40:39 +1000 Subject: Extracting render buffers and changing it to a more generic solution --- .../storage_rd/render_buffer_custom_data_rd.h | 48 ++ .../storage_rd/render_scene_buffers_rd.cpp | 559 +++++++++++++++++++++ .../storage_rd/render_scene_buffers_rd.h | 256 ++++++++++ .../renderer_rd/storage_rd/texture_storage.cpp | 20 + .../renderer_rd/storage_rd/texture_storage.h | 2 + .../rendering/renderer_rd/storage_rd/utilities.cpp | 4 +- 6 files changed, 887 insertions(+), 2 deletions(-) create mode 100644 servers/rendering/renderer_rd/storage_rd/render_buffer_custom_data_rd.h create mode 100644 servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp create mode 100644 servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h (limited to 'servers/rendering/renderer_rd/storage_rd') diff --git a/servers/rendering/renderer_rd/storage_rd/render_buffer_custom_data_rd.h b/servers/rendering/renderer_rd/storage_rd/render_buffer_custom_data_rd.h new file mode 100644 index 0000000000..d904012914 --- /dev/null +++ b/servers/rendering/renderer_rd/storage_rd/render_buffer_custom_data_rd.h @@ -0,0 +1,48 @@ +/*************************************************************************/ +/* render_buffer_custom_data_rd.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef RENDER_BUFFER_CUSTOM_DATA_RD_H +#define RENDER_BUFFER_CUSTOM_DATA_RD_H + +#include "core/object/ref_counted.h" + +class RenderSceneBuffersRD; + +class RenderBufferCustomDataRD : public RefCounted { + GDCLASS(RenderBufferCustomDataRD, RefCounted); + +public: + virtual void configure(RenderSceneBuffersRD *p_render_buffers) = 0; + virtual void free_data() = 0; // called on cleanup + +private: +}; + +#endif // RENDER_BUFFER_CUSTOM_DATA_RD_H 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 new file mode 100644 index 0000000000..576ec81124 --- /dev/null +++ b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp @@ -0,0 +1,559 @@ +/*************************************************************************/ +/* render_scene_buffers_rd.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "render_scene_buffers_rd.h" +#include "servers/rendering/renderer_rd/renderer_scene_render_rd.h" +#include "servers/rendering/renderer_rd/storage_rd/material_storage.h" +#include "servers/rendering/renderer_rd/storage_rd/texture_storage.h" + +RenderSceneBuffersRD::RenderSceneBuffersRD() { +} + +RenderSceneBuffersRD::~RenderSceneBuffersRD() { + cleanup(); + + data_buffers.clear(); + + // need to investigate if we can remove these things. + if (cluster_builder) { + memdelete(cluster_builder); + } +} + +void RenderSceneBuffersRD::_bind_methods() { + ClassDB::bind_method(D_METHOD("has_texture", "context", "name"), &RenderSceneBuffersRD::has_texture); + // FIXME we can't pass RD::DataFormat, RD::TextureSamples and RD::TextureView in ClassDB, need to solve views differently... + // ClassDB::bind_method(D_METHOD("create_texture", "context", "name", "data_format", "usage_bits", "texture_samples", "size", "layers", "mipmaps", "unique"), &RenderSceneBuffersRD::create_texture); + // ClassDB::bind_method(D_METHOD("create_texture_from_format", "context", "name", "format", "view", "unique"), &RenderSceneBuffersRD::create_texture_from_format); + // ClassDB::bind_method(D_METHOD("create_texture_view", "context", "name", "view_name", "view"), &RenderSceneBuffersRD::has_texture); + ClassDB::bind_method(D_METHOD("get_texture", "context", "name"), &RenderSceneBuffersRD::get_texture); + // ClassDB::bind_method(D_METHOD("get_texture_format", "context", "name"), &RenderSceneBuffersRD::get_texture_format); + ClassDB::bind_method(D_METHOD("get_texture_slice", "context", "name", "layer", "mipmap"), &RenderSceneBuffersRD::get_texture_slice); + ClassDB::bind_method(D_METHOD("get_texture_slice_size", "context", "name", "layer", "mipmap"), &RenderSceneBuffersRD::get_texture_slice_size); + ClassDB::bind_method(D_METHOD("clear_context", "context"), &RenderSceneBuffersRD::clear_context); +} + +void RenderSceneBuffersRD::update_sizes(NamedTexture &p_named_texture) { + ERR_FAIL_COND(p_named_texture.texture.is_null()); + + uint32_t size = p_named_texture.format.array_layers * p_named_texture.format.mipmaps; + p_named_texture.sizes.resize(size); + + Size2i mipmap_size = Size2i(p_named_texture.format.width, p_named_texture.format.height); + + for (uint32_t mipmap = 0; mipmap < p_named_texture.format.mipmaps; mipmap++) { + for (uint32_t layer = 0; layer < p_named_texture.format.array_layers; layer++) { + uint32_t index = layer * p_named_texture.format.mipmaps + mipmap; + + p_named_texture.sizes.ptrw()[index] = mipmap_size; + } + + mipmap_size.width = MAX(1, mipmap_size.width >> 1); + mipmap_size.height = MAX(1, mipmap_size.height >> 1); + } +} + +void RenderSceneBuffersRD::free_named_texture(NamedTexture &p_named_texture) { + if (p_named_texture.texture.is_valid()) { + RD::get_singleton()->free(p_named_texture.texture); + } + p_named_texture.texture = RID(); + p_named_texture.slices.clear(); // slices should be freed automatically as dependents... +} + +void RenderSceneBuffersRD::cleanup() { + // Free our data buffers (but don't destroy them) + for (KeyValue> &E : data_buffers) { + E.value->free_data(); + } + + // Clear our named textures + for (KeyValue &E : named_textures) { + free_named_texture(E.value); + } + named_textures.clear(); + + // old stuff, to be re-evaluated... + + for (int i = 0; i < luminance.fb.size(); i++) { + RD::get_singleton()->free(luminance.fb[i]); + } + luminance.fb.clear(); + + for (int i = 0; i < luminance.reduce.size(); i++) { + RD::get_singleton()->free(luminance.reduce[i]); + } + luminance.reduce.clear(); + + if (luminance.current_fb.is_valid()) { + RD::get_singleton()->free(luminance.current_fb); + luminance.current_fb = RID(); + } + + if (luminance.current.is_valid()) { + RD::get_singleton()->free(luminance.current); + luminance.current = RID(); + } + + if (ss_effects.linear_depth.is_valid()) { + RD::get_singleton()->free(ss_effects.linear_depth); + ss_effects.linear_depth = RID(); + ss_effects.linear_depth_slices.clear(); + } + + sse->ssao_free(ss_effects.ssao); + sse->ssil_free(ss_effects.ssil); + sse->ssr_free(ssr); +} + +void RenderSceneBuffersRD::configure(RID p_render_target, const Size2i p_internal_size, const Size2i p_target_size, float p_fsr_sharpness, float p_texture_mipmap_bias, RS::ViewportMSAA p_msaa_3d, RenderingServer::ViewportScreenSpaceAA p_screen_space_aa, bool p_use_taa, bool p_use_debanding, uint32_t p_view_count) { + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); + + ERR_FAIL_COND_MSG(p_view_count == 0, "Must have at least 1 view"); + + target_size = p_target_size; + internal_size = p_internal_size; + + // FIXME, right now we do this because only our clustered renderer supports FSR upscale + // this does mean that with linear upscale if we use subpasses, we could get into trouble. + if (!can_be_storage) { + internal_size = target_size; + } + + if (p_use_taa) { + // Use negative mipmap LOD bias when TAA is enabled to compensate for loss of sharpness. + // This restores sharpness in still images to be roughly at the same level as without TAA, + // but moving scenes will still be blurrier. + p_texture_mipmap_bias -= 0.5; + } + + if (p_screen_space_aa == RS::VIEWPORT_SCREEN_SPACE_AA_FXAA) { + // Use negative mipmap LOD bias when FXAA is enabled to compensate for loss of sharpness. + // If both TAA and FXAA are enabled, combine their negative LOD biases together. + p_texture_mipmap_bias -= 0.25; + } + + material_storage->sampler_rd_configure_custom(p_texture_mipmap_bias); + + // need to check if we really need to do this here.. + RendererSceneRenderRD::get_singleton()->update_uniform_sets(); + + render_target = p_render_target; + fsr_sharpness = p_fsr_sharpness; + msaa_3d = p_msaa_3d; + screen_space_aa = p_screen_space_aa; + use_taa = p_use_taa; + use_debanding = p_use_debanding; + view_count = p_view_count; + + /* may move this into our clustered renderer data object */ + if (can_be_storage) { + if (cluster_builder == nullptr) { + cluster_builder = memnew(ClusterBuilderRD); + } + cluster_builder->set_shared(RendererSceneRenderRD::get_singleton()->get_cluster_builder_shared()); + } + + // cleanout any old buffers we had. + cleanup(); + + // create our 3D render buffers + { + // Create our color buffer(s) + uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | (can_be_storage ? RD::TEXTURE_USAGE_STORAGE_BIT : 0) | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; + usage_bits |= RD::TEXTURE_USAGE_INPUT_ATTACHMENT_BIT; // only needed when using subpasses in the mobile renderer + + // our internal texture should have MSAA support if applicable + if (msaa_3d != RS::VIEWPORT_MSAA_DISABLED) { + usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT; + } + + create_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR, base_data_format, usage_bits); + } + + // Create our depth buffer + { + // TODO If we have depth buffer supplied externally, pick this up + + RD::DataFormat format; + uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT; + + if (msaa_3d == RS::VIEWPORT_MSAA_DISABLED) { + format = RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_D24_UNORM_S8_UINT, (RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT)) ? RD::DATA_FORMAT_D24_UNORM_S8_UINT : RD::DATA_FORMAT_D32_SFLOAT_S8_UINT; + usage_bits |= RD::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; + } else { + format = RD::DATA_FORMAT_R32_SFLOAT; + usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_STORAGE_BIT; + } + + create_texture(RB_SCOPE_BUFFERS, RB_TEX_DEPTH, format, usage_bits); + } + + // VRS (note, our vrs object will only be set if VRS is supported) + RID vrs_texture; + RS::ViewportVRSMode vrs_mode = texture_storage->render_target_get_vrs_mode(p_render_target); + if (vrs && vrs_mode != RS::VIEWPORT_VRS_DISABLED) { + uint32_t usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_VRS_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT; + vrs_texture = create_texture(RB_SCOPE_VRS, RB_TEXTURE, RD::DATA_FORMAT_R8_UINT, usage_bits, RD::TEXTURE_SAMPLES_1, vrs->get_vrs_texture_size(internal_size)); + } + + for (KeyValue> &E : data_buffers) { + E.value->configure(this); + } + + if (cluster_builder) { + RID sampler = RendererRD::MaterialStorage::get_singleton()->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); + cluster_builder->setup(internal_size, max_cluster_elements, get_depth_texture(), sampler, get_internal_texture()); + } +} + +void RenderSceneBuffersRD::set_fsr_sharpness(float p_fsr_sharpness) { + fsr_sharpness = p_fsr_sharpness; +} + +void RenderSceneBuffersRD::set_texture_mipmap_bias(float p_texture_mipmap_bias) { + RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); + material_storage->sampler_rd_configure_custom(p_texture_mipmap_bias); +} + +void RenderSceneBuffersRD::set_use_debanding(bool p_use_debanding) { + use_debanding = p_use_debanding; +} + +// Named textures + +bool RenderSceneBuffersRD::has_texture(const StringName &p_context, const StringName &p_texture_name) const { + NTKey key(p_context, p_texture_name); + + return named_textures.has(key); +} + +RID RenderSceneBuffersRD::create_texture(const StringName &p_context, const StringName &p_texture_name, const RD::DataFormat p_data_format, const uint32_t p_usage_bits, const RD::TextureSamples p_texture_samples, const Size2i p_size, const uint32_t p_layers, const uint32_t p_mipmaps, bool p_unique) { + // Keep some useful data, we use default values when these are 0. + Size2i size = p_size == Size2i(0, 0) ? internal_size : p_size; + uint32_t layers = p_layers == 0 ? view_count : p_layers; + uint32_t mipmaps = p_mipmaps == 0 ? 1 : p_mipmaps; + + // Create our texture + RD::TextureFormat tf; + tf.format = p_data_format; + if (layers > 1) { + tf.texture_type = RD::TEXTURE_TYPE_2D_ARRAY; + } + + tf.width = size.x; + tf.height = size.y; + tf.depth = 1; + tf.array_layers = layers; + tf.mipmaps = mipmaps; + tf.usage_bits = p_usage_bits; + tf.samples = p_texture_samples; + + return create_texture_from_format(p_context, p_texture_name, tf, RD::TextureView(), p_unique); +} + +RID RenderSceneBuffersRD::create_texture_from_format(const StringName &p_context, const StringName &p_texture_name, const RD::TextureFormat &p_texture_format, RD::TextureView p_view, bool p_unique) { + // TODO p_unique, if p_unique is true, this is a texture that can be shared. This will be implemented later as an optimisation. + + NTKey key(p_context, p_texture_name); + + // check if this is a known texture + if (named_textures.has(key)) { + return named_textures[key].texture; + } + + // Add a new entry.. + NamedTexture &named_texture = named_textures[key]; + named_texture.format = p_texture_format; + named_texture.is_unique = p_unique; + named_texture.texture = RD::get_singleton()->texture_create(p_texture_format, p_view); + + Array arr; + arr.push_back(p_context); + arr.push_back(p_texture_name); + RD::get_singleton()->set_resource_name(named_texture.texture, String("RenderBuffer {0}/{1}").format(arr)); + + update_sizes(named_texture); + + // The rest is lazy created.. + + return named_texture.texture; +} + +RID RenderSceneBuffersRD::create_texture_view(const StringName &p_context, const StringName &p_texture_name, const StringName p_view_name, RD::TextureView p_view) { + NTKey view_key(p_context, p_view_name); + + // check if this is a known texture + if (named_textures.has(view_key)) { + return named_textures[view_key].texture; + } + + NTKey key(p_context, p_texture_name); + + ERR_FAIL_COND_V(!named_textures.has(key), RID()); + + NamedTexture &named_texture = named_textures[key]; + NamedTexture &view_texture = named_textures[view_key]; + + view_texture.format = named_texture.format; + view_texture.is_unique = named_texture.is_unique; + + view_texture.texture = RD::get_singleton()->texture_create_shared(p_view, named_texture.texture); + + Array arr; + arr.push_back(p_context); + arr.push_back(p_view_name); + RD::get_singleton()->set_resource_name(view_texture.texture, String("RenderBuffer View {0}/{1}").format(arr)); + + update_sizes(named_texture); + + return view_texture.texture; +} + +RID RenderSceneBuffersRD::get_texture(const StringName &p_context, const StringName &p_texture_name) const { + NTKey key(p_context, p_texture_name); + + ERR_FAIL_COND_V(!named_textures.has(key), RID()); + + return named_textures[key].texture; +} + +const RD::TextureFormat RenderSceneBuffersRD::get_texture_format(const StringName &p_context, const StringName &p_texture_name) const { + NTKey key(p_context, p_texture_name); + + ERR_FAIL_COND_V(!named_textures.has(key), RD::TextureFormat()); + + return named_textures[key].format; +} + +RID RenderSceneBuffersRD::get_texture_slice(const StringName &p_context, const StringName &p_texture_name, const uint32_t p_layer, const uint32_t p_mipmap) { + NTKey key(p_context, p_texture_name); + + // check if this is a known texture + ERR_FAIL_COND_V(!named_textures.has(key), RID()); + NamedTexture &named_texture = named_textures[key]; + ERR_FAIL_COND_V(named_texture.texture.is_null(), RID()); + + // check if we're in bounds + ERR_FAIL_UNSIGNED_INDEX_V(p_layer, named_texture.format.array_layers, RID()); + ERR_FAIL_UNSIGNED_INDEX_V(p_mipmap, named_texture.format.mipmaps, RID()); + + // if we don't have multiple layers or mipmaps, we can just return our texture as is + if (named_texture.format.array_layers == 1 && named_texture.format.mipmaps == 1) { + return named_texture.texture; + } + + // get our index and make sure we have enough entries in our slices vector + uint32_t index = p_layer * named_texture.format.mipmaps + p_mipmap; + while (named_texture.slices.size() <= int(index)) { + named_texture.slices.push_back(RID()); + } + + // create our slice if we don't have it already + if (named_texture.slices[index].is_null()) { + named_texture.slices.ptrw()[index] = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), named_texture.texture, p_layer, p_mipmap); + + Array arr; + arr.push_back(p_context); + arr.push_back(p_texture_name); + arr.push_back(itos(p_layer)); + arr.push_back(itos(p_mipmap)); + RD::get_singleton()->set_resource_name(named_texture.slices[index], String("RenderBuffer {0}/{1} slice {2}/{3}").format(arr)); + } + + // and return our slice + return named_texture.slices[index]; +} + +Size2i RenderSceneBuffersRD::get_texture_slice_size(const StringName &p_context, const StringName &p_texture_name, const uint32_t p_layer, const uint32_t p_mipmap) { + NTKey key(p_context, p_texture_name); + + // check if this is a known texture + ERR_FAIL_COND_V(!named_textures.has(key), Size2i()); + NamedTexture &named_texture = named_textures[key]; + ERR_FAIL_COND_V(named_texture.texture.is_null(), Size2i()); + + // check if we're in bounds + ERR_FAIL_UNSIGNED_INDEX_V(p_layer, named_texture.format.array_layers, Size2i()); + ERR_FAIL_UNSIGNED_INDEX_V(p_mipmap, named_texture.format.mipmaps, Size2i()); + + // get our index + uint32_t index = p_layer * named_texture.format.mipmaps + p_mipmap; + + // and return our size + return named_texture.sizes[index]; +} + +void RenderSceneBuffersRD::clear_context(const StringName &p_context) { + Vector to_free; // free these + + // Find all entries for our context, we don't want to free them yet or our loop fails. + for (KeyValue &E : named_textures) { + if (E.key.context == p_context) { + to_free.push_back(E.key); + } + } + + // Now free these and remove them from our textures + for (NTKey &key : to_free) { + free_named_texture(named_textures[key]); + named_textures.erase(key); + } +} + +// Allocate shared buffers +void RenderSceneBuffersRD::allocate_blur_textures() { + if (has_texture(RB_SCOPE_BUFFERS, RB_TEX_BLUR_0)) { + // already allocated... + return; + } + + uint32_t mipmaps_required = Image::get_image_required_mipmaps(internal_size.x, internal_size.y, Image::FORMAT_RGBAH); + + uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT; + if (can_be_storage) { + usage_bits += RD::TEXTURE_USAGE_STORAGE_BIT; + } else { + usage_bits += RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; + } + + create_texture(RB_SCOPE_BUFFERS, RB_TEX_BLUR_0, base_data_format, usage_bits, RD::TEXTURE_SAMPLES_1, internal_size, view_count, mipmaps_required); + create_texture(RB_SCOPE_BUFFERS, RB_TEX_BLUR_1, base_data_format, usage_bits, RD::TEXTURE_SAMPLES_1, Size2i(internal_size.x >> 1, internal_size.y >> 1), view_count, mipmaps_required - 1); + + // if !can_be_storage we need a half width version + if (!can_be_storage) { + create_texture(RB_SCOPE_BUFFERS, RB_TEX_HALF_BLUR, base_data_format, usage_bits, RD::TEXTURE_SAMPLES_1, Size2i(internal_size.x >> 1, internal_size.y), 1, mipmaps_required); + } + + // TODO redo this: + if (!can_be_storage) { + // create 4 weight textures, 2 full size, 2 half size + + RD::TextureFormat tf; + tf.format = RD::DATA_FORMAT_R16_SFLOAT; // We could probably use DATA_FORMAT_R8_SNORM if we don't pre-multiply by blur_size but that depends on whether we can remove DEPTH_GAP + tf.width = internal_size.x; + tf.height = internal_size.y; + tf.texture_type = RD::TEXTURE_TYPE_2D; + tf.array_layers = 1; // Our DOF effect handles one eye per turn + tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; + tf.mipmaps = 1; + for (uint32_t i = 0; i < 4; i++) { + // associated blur texture + RID texture; + if (i == 1) { + texture = get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_BLUR_0, 0, 0); + } else if (i == 2) { + texture = get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_BLUR_1, 0, 0); + } else if (i == 3) { + texture = get_texture_slice(RB_SCOPE_BUFFERS, RB_TEX_BLUR_0, 0, 1); + } + + // create weight texture + weight_buffers[i].weight = RD::get_singleton()->texture_create(tf, RD::TextureView()); + + // create frame buffer + Vector fb; + if (i != 0) { + fb.push_back(texture); + } + fb.push_back(weight_buffers[i].weight); + weight_buffers[i].fb = RD::get_singleton()->framebuffer_create(fb); + + if (i == 1) { + // next 2 are half size + tf.width = MAX(1u, tf.width >> 1); + tf.height = MAX(1u, tf.height >> 1); + } + } + } +} + +// Data buffers + +bool RenderSceneBuffersRD::has_custom_data(const StringName &p_name) { + return data_buffers.has(p_name); +} + +void RenderSceneBuffersRD::set_custom_data(const StringName &p_name, Ref p_data) { + if (p_data.is_valid()) { + data_buffers[p_name] = p_data; + } else if (has_custom_data(p_name)) { + data_buffers.erase(p_name); + } +} + +Ref RenderSceneBuffersRD::get_custom_data(const StringName &p_name) const { + ERR_FAIL_COND_V(!data_buffers.has(p_name), Ref()); + + Ref ret = data_buffers[p_name]; + + return ret; +} + +// Velocity texture. + +void RenderSceneBuffersRD::ensure_velocity() { + if (!has_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY)) { + uint32_t usage_bits = RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT; + + if (msaa_3d != RS::VIEWPORT_MSAA_DISABLED) { + uint32_t msaa_usage_bits = usage_bits | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT; + usage_bits |= RD::TEXTURE_USAGE_CAN_COPY_TO_BIT; + + const RD::TextureSamples ts[RS::VIEWPORT_MSAA_MAX] = { + RD::TEXTURE_SAMPLES_1, + RD::TEXTURE_SAMPLES_2, + RD::TEXTURE_SAMPLES_4, + RD::TEXTURE_SAMPLES_8, + }; + + create_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY_MSAA, RD::DATA_FORMAT_R16G16_SFLOAT, msaa_usage_bits, ts[msaa_3d]); + } + + create_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY, RD::DATA_FORMAT_R16G16_SFLOAT, usage_bits); + } +} + +RID RenderSceneBuffersRD::get_velocity_buffer(bool p_get_msaa) { + if (p_get_msaa) { + if (!has_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY_MSAA)) { + return RID(); + } else { + return get_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY_MSAA); + } + } else { + if (!has_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY)) { + return RID(); + } else { + return get_texture(RB_SCOPE_BUFFERS, RB_TEX_VELOCITY); + } + } +} 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 new file mode 100644 index 0000000000..adaf075f80 --- /dev/null +++ b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h @@ -0,0 +1,256 @@ +/*************************************************************************/ +/* render_scene_buffers_rd.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef RENDER_SCENE_BUFFERS_RD_H +#define RENDER_SCENE_BUFFERS_RD_H + +#include "core/templates/hash_map.h" +#include "servers/rendering/renderer_rd/effects/vrs.h" +#include "servers/rendering/renderer_rd/framebuffer_cache_rd.h" +#include "servers/rendering/renderer_rd/storage_rd/render_buffer_custom_data_rd.h" +#include "servers/rendering/renderer_scene.h" +#include "servers/rendering/rendering_device.h" +#include "servers/rendering/storage/render_scene_buffers.h" + +// These can be retired in due time +#include "servers/rendering/renderer_rd/cluster_builder_rd.h" +#include "servers/rendering/renderer_rd/effects/ss_effects.h" +#include "servers/rendering/renderer_rd/environment/fog.h" + +#define RB_SCOPE_BUFFERS SNAME("render_buffers") +#define RB_SCOPE_VRS SNAME("VRS") + +#define RB_TEXTURE SNAME("texture") +#define RB_TEX_COLOR SNAME("color") +#define RB_TEX_COLOR_MSAA SNAME("color_msaa") +#define RB_TEX_DEPTH SNAME("depth") +#define RB_TEX_DEPTH_MSAA SNAME("depth_msaa") +#define RB_TEX_VELOCITY SNAME("velocity") +#define RB_TEX_VELOCITY_MSAA SNAME("velocity_msaa") + +#define RB_TEX_BLUR_0 SNAME("blur_0") +#define RB_TEX_BLUR_1 SNAME("blur_1") +#define RB_TEX_HALF_BLUR SNAME("half_blur") // only for raster! + +#define RB_TEX_BACK_DEPTH SNAME("back_depth") + +class RenderSceneBuffersRD : public RenderSceneBuffers { + GDCLASS(RenderSceneBuffersRD, RenderSceneBuffers); + +private: + bool can_be_storage = true; + uint32_t max_cluster_elements = 512; + RD::DataFormat base_data_format = RD::DATA_FORMAT_R16G16B16A16_SFLOAT; + RendererRD::SSEffects *sse = nullptr; + RendererRD::VRS *vrs = nullptr; + uint64_t auto_exposure_version = 1; + + // Our render target represents our final destination that we display on screen. + RID render_target; + Size2i target_size = Size2i(0, 0); + uint32_t view_count = 1; + + // The internal size of the textures we render 3D to in case we render at a lower resolution and upscale + Size2i internal_size = Size2i(0, 0); + float fsr_sharpness = 0.2f; + + // Aliassing settings + RS::ViewportMSAA msaa_3d = RS::VIEWPORT_MSAA_DISABLED; + RS::ViewportScreenSpaceAA screen_space_aa = RS::VIEWPORT_SCREEN_SPACE_AA_DISABLED; + bool use_taa = false; + bool use_debanding = false; + + // Named Textures + + struct NTKey { + StringName context; + StringName buffer_name; + + bool operator==(const NTKey &p_val) const { + return (context == p_val.context) && (buffer_name == p_val.buffer_name); + } + + static uint32_t hash(const NTKey &p_val) { + // FIXME, properly hash two stringnames together + uint32_t h = p_val.context.hash(); + h = hash_murmur3_one_32(p_val.buffer_name.hash(), h); + return hash_fmix32(h); + } + + NTKey() {} + NTKey(const StringName p_context, const StringName p_texture_name) { + context = p_context; + buffer_name = p_texture_name; + } + }; + + struct NamedTexture { + // Cache the data used to create our texture + RD::TextureFormat format; + bool is_unique; // If marked as unique, we return it into our pool + + // Our texture objects, slices are lazy (i.e. only created when requested). + RID texture; + Vector slices; + Vector sizes; + }; + + mutable HashMap named_textures; + void update_sizes(NamedTexture &p_named_texture); + void free_named_texture(NamedTexture &p_named_texture); + + // Data buffers + mutable HashMap> data_buffers; + +protected: + static void _bind_methods(); + +public: + RenderSceneBuffersRD(); + virtual ~RenderSceneBuffersRD(); + + // info from our renderer + void set_can_be_storage(const bool p_can_be_storage) { can_be_storage = p_can_be_storage; } + void set_max_cluster_elements(const uint32_t p_max_elements) { max_cluster_elements = p_max_elements; } + void set_base_data_format(const RD::DataFormat p_base_data_format) { base_data_format = p_base_data_format; } + RD::DataFormat get_base_data_format() const { return base_data_format; } + void set_sseffects(RendererRD::SSEffects *p_ss_effects) { sse = p_ss_effects; } + void set_vrs(RendererRD::VRS *p_vrs) { vrs = p_vrs; } + + void cleanup(); + virtual void configure(RID p_render_target, const Size2i p_internal_size, const Size2i p_target_size, float p_fsr_sharpness, float p_texture_mipmap_bias, RS::ViewportMSAA p_msaa_3d, RenderingServer::ViewportScreenSpaceAA p_screen_space_aa, bool p_use_taa, bool p_use_debanding, uint32_t p_view_count) override; + virtual void set_fsr_sharpness(float p_fsr_sharpness) override; + virtual void set_texture_mipmap_bias(float p_texture_mipmap_bias) override; + virtual void set_use_debanding(bool p_use_debanding) override; + + // Named Textures + + bool has_texture(const StringName &p_context, const StringName &p_texture_name) const; + RID create_texture(const StringName &p_context, const StringName &p_texture_name, const RD::DataFormat p_data_format, const uint32_t p_usage_bits, const RD::TextureSamples p_texture_samples = RD::TEXTURE_SAMPLES_1, const Size2i p_size = Size2i(0, 0), const uint32_t p_layers = 0, const uint32_t p_mipmaps = 1, bool p_unique = true); + RID create_texture_from_format(const StringName &p_context, const StringName &p_texture_name, const RD::TextureFormat &p_texture_format, RD::TextureView p_view = RD::TextureView(), bool p_unique = true); + RID create_texture_view(const StringName &p_context, const StringName &p_texture_name, const StringName p_view_name, RD::TextureView p_view = RD::TextureView()); + RID get_texture(const StringName &p_context, const StringName &p_texture_name) const; + const RD::TextureFormat get_texture_format(const StringName &p_context, const StringName &p_texture_name) const; + RID get_texture_slice(const StringName &p_context, const StringName &p_texture_name, const uint32_t p_layer, const uint32_t p_mipmap); + Size2i get_texture_slice_size(const StringName &p_context, const StringName &p_texture_name, const uint32_t p_layer, const uint32_t p_mipmap); + + void clear_context(const StringName &p_context); + + // Allocate shared buffers + void allocate_blur_textures(); + + // Custom data + bool has_custom_data(const StringName &p_name); + void set_custom_data(const StringName &p_name, Ref p_data); + Ref get_custom_data(const StringName &p_name) const; + + // Getters + + _FORCE_INLINE_ RID get_render_target() const { return render_target; } + _FORCE_INLINE_ uint32_t get_view_count() const { return view_count; } + _FORCE_INLINE_ Size2i get_internal_size() const { return internal_size; } + _FORCE_INLINE_ Size2i get_target_size() const { return target_size; } + _FORCE_INLINE_ float get_fsr_sharpness() const { return fsr_sharpness; } + _FORCE_INLINE_ RS::ViewportMSAA get_msaa_3d() const { return msaa_3d; } + _FORCE_INLINE_ RS::ViewportScreenSpaceAA get_screen_space_aa() const { return screen_space_aa; } + _FORCE_INLINE_ bool get_use_taa() const { return use_taa; } + _FORCE_INLINE_ bool get_use_debanding() const { return use_debanding; } + + uint64_t get_auto_exposure_version() const { return auto_exposure_version; } + void set_auto_exposure_version(const uint64_t p_auto_exposure_version) { auto_exposure_version = p_auto_exposure_version; } + + // For our internal textures we provide some easy access methods. + + _FORCE_INLINE_ RID get_internal_texture() const { + return get_texture(RB_SCOPE_BUFFERS, RB_TEX_COLOR); + } + _FORCE_INLINE_ RID get_internal_texture(const uint32_t p_layer) { + 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); + } + + // 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. + + // 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); } + 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); } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Everything after this needs to be re-evaluated, this is all old implementation + + ClusterBuilderRD *cluster_builder = nullptr; + + struct WeightBuffers { + RID weight; + RID fb; // FB with both texture and weight writing into one level lower + }; + + // 2 full size, 2 half size + WeightBuffers weight_buffers[4]; // Only used in raster + + struct Luminance { + Vector reduce; + RID current; + + // used only on mobile renderer + Vector fb; + RID current_fb; + } luminance; + + struct SSEffects { + RID linear_depth; + Vector linear_depth_slices; + + RID downsample_uniform_set; + + Projection last_frame_projection; + Transform3D last_frame_transform; + + RendererRD::SSEffects::SSAORenderBuffers ssao; + RendererRD::SSEffects::SSILRenderBuffers ssil; + } ss_effects; + + RendererRD::SSEffects::SSRRenderBuffers ssr; + + RID get_ao_texture() const { return ss_effects.ssao.ao_final; } + RID get_ssil_texture() const { return ss_effects.ssil.ssil_final; } +}; + +#endif // RENDER_SCENE_BUFFERS_RD_H diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp index ce19187071..b87b4d4a0f 100644 --- a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp +++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp @@ -2120,6 +2120,7 @@ void TextureStorage::_clear_render_target(RenderTarget *rt) { if (rt->color.is_valid()) { RD::get_singleton()->free(rt->color); } + rt->color_slices.clear(); // these are automatically freed. if (rt->color_multisample.is_valid()) { RD::get_singleton()->free(rt->color_multisample); @@ -2174,6 +2175,7 @@ void TextureStorage::_update_render_target(RenderTarget *rt) { } rd_color_attachment_format.samples = RD::TEXTURE_SAMPLES_1; rd_color_attachment_format.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT; + rd_color_attachment_format.usage_bits |= RD::TEXTURE_USAGE_STORAGE_BIT; // FIXME we need this only when FSR is enabled rd_color_attachment_format.shareable_formats.push_back(rt->color_format); rd_color_attachment_format.shareable_formats.push_back(rt->color_format_srgb); if (rt->msaa != RS::VIEWPORT_MSAA_DISABLED) { @@ -2392,6 +2394,24 @@ RID TextureStorage::render_target_get_rd_texture(RID p_render_target) { return rt->color; } +RID TextureStorage::render_target_get_rd_texture_slice(RID p_render_target, uint32_t p_layer) { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_COND_V(!rt, RID()); + + if (rt->view_count == 1) { + return rt->color; + } else { + ERR_FAIL_UNSIGNED_INDEX_V(p_layer, rt->view_count, RID()); + if (rt->color_slices.size() == 0) { + for (uint32_t v = 0; v < rt->view_count; v++) { + RID slice = RD::get_singleton()->texture_create_shared_from_slice(RD::TextureView(), rt->color, v, 0); + rt->color_slices.push_back(slice); + } + } + return rt->color_slices[p_layer]; + } +} + RID TextureStorage::render_target_get_rd_backbuffer(RID p_render_target) { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); ERR_FAIL_COND_V(!rt, RID()); diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.h b/servers/rendering/renderer_rd/storage_rd/texture_storage.h index 25d355d878..a3acad30f3 100644 --- a/servers/rendering/renderer_rd/storage_rd/texture_storage.h +++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.h @@ -254,6 +254,7 @@ private: uint32_t view_count; RID framebuffer; RID color; + Vector color_slices; RID color_multisample; // Needed when MSAA is enabled. RS::ViewportMSAA msaa = RS::VIEWPORT_MSAA_DISABLED; @@ -589,6 +590,7 @@ public: 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); RID render_target_get_rd_backbuffer(RID p_render_target); RID render_target_get_rd_backbuffer_framebuffer(RID p_render_target); diff --git a/servers/rendering/renderer_rd/storage_rd/utilities.cpp b/servers/rendering/renderer_rd/storage_rd/utilities.cpp index fcef2f24bf..b80bcd514f 100644 --- a/servers/rendering/renderer_rd/storage_rd/utilities.cpp +++ b/servers/rendering/renderer_rd/storage_rd/utilities.cpp @@ -155,8 +155,8 @@ void Utilities::base_update_dependency(RID p_base, DependencyTracker *p_instance Dependency *dependency = TextureStorage::get_singleton()->decal_get_dependency(p_base); p_instance->update_dependency(dependency); } else if (GI::get_singleton()->owns_voxel_gi(p_base)) { - GI::VoxelGI *gip = GI::get_singleton()->get_voxel_gi(p_base); - p_instance->update_dependency(&gip->dependency); + Dependency *dependency = GI::get_singleton()->voxel_gi_get_dependency(p_base); + p_instance->update_dependency(dependency); } else if (LightStorage::get_singleton()->owns_lightmap(p_base)) { Dependency *dependency = LightStorage::get_singleton()->lightmap_get_dependency(p_base); p_instance->update_dependency(dependency); -- cgit v1.2.3