diff options
author | Juan Linietsky <reduzio@gmail.com> | 2022-08-05 11:59:58 +0200 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2022-08-05 13:37:29 +0200 |
commit | f999f52f0af98ad37f0a837f25786513e2862f79 (patch) | |
tree | 68a622b4c11c45a0e2405ff5f133860b3d613155 /servers | |
parent | 9c8e7031bf57d30a632f570c467c791836b73b2c (diff) |
Add a Framebuffer cache
Adds a FramebufferCache singletion that operates the same way as UniformSetCache.
Allows creating framebuffers on the fly (and keep them cached if re-requested) such as:
```C++
RID fb = FramebufferCache::get_singleton()->get_cache(texture1,texture2);
```
Diffstat (limited to 'servers')
-rw-r--r-- | servers/rendering/renderer_rd/framebuffer_cache_rd.cpp | 64 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/framebuffer_cache_rd.h | 310 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/renderer_compositor_rd.cpp | 2 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/renderer_compositor_rd.h | 2 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/uniform_set_cache_rd.h | 4 | ||||
-rw-r--r-- | servers/rendering/rendering_device.cpp | 1 | ||||
-rw-r--r-- | servers/rendering/rendering_device.h | 11 |
7 files changed, 388 insertions, 6 deletions
diff --git a/servers/rendering/renderer_rd/framebuffer_cache_rd.cpp b/servers/rendering/renderer_rd/framebuffer_cache_rd.cpp new file mode 100644 index 0000000000..9baa86a32d --- /dev/null +++ b/servers/rendering/renderer_rd/framebuffer_cache_rd.cpp @@ -0,0 +1,64 @@ +/*************************************************************************/ +/* framebuffer_cache_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 "framebuffer_cache_rd.h" + +FramebufferCacheRD *FramebufferCacheRD::singleton = nullptr; + +void FramebufferCacheRD::_invalidate(Cache *p_cache) { + if (p_cache->prev) { + p_cache->prev->next = p_cache->next; + } else { + // At beginning of table + uint32_t table_idx = p_cache->hash % HASH_TABLE_SIZE; + hash_table[table_idx] = p_cache->next; + } + + if (p_cache->next) { + p_cache->next->prev = p_cache->prev; + } + + cache_allocator.free(p_cache); + cache_instances_used--; +} +void FramebufferCacheRD::_framebuffer_invalidation_callback(void *p_userdata) { + singleton->_invalidate(reinterpret_cast<Cache *>(p_userdata)); +} + +FramebufferCacheRD::FramebufferCacheRD() { + ERR_FAIL_COND(singleton != nullptr); + singleton = this; +} + +FramebufferCacheRD::~FramebufferCacheRD() { + if (cache_instances_used > 0) { + ERR_PRINT("At exit: " + itos(cache_instances_used) + " framebuffer cache instance(s) still in use."); + } +} diff --git a/servers/rendering/renderer_rd/framebuffer_cache_rd.h b/servers/rendering/renderer_rd/framebuffer_cache_rd.h new file mode 100644 index 0000000000..f360e0fc6b --- /dev/null +++ b/servers/rendering/renderer_rd/framebuffer_cache_rd.h @@ -0,0 +1,310 @@ +/*************************************************************************/ +/* framebuffer_cache_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 FRAMEBUFFER_CACHE_RD_H +#define FRAMEBUFFER_CACHE_RD_H + +#include "core/templates/local_vector.h" +#include "core/templates/paged_allocator.h" +#include "servers/rendering/rendering_device.h" + +class FramebufferCacheRD : public Object { + GDCLASS(FramebufferCacheRD, Object) + + struct Cache { + Cache *prev = nullptr; + Cache *next = nullptr; + uint32_t hash = 0; + RID cache; + LocalVector<RID> textures; + LocalVector<RD::FramebufferPass> passes; + uint32_t views = 0; + }; + + PagedAllocator<Cache> cache_allocator; + + enum { + HASH_TABLE_SIZE = 16381 // Prime + }; + + Cache *hash_table[HASH_TABLE_SIZE] = {}; + + static _FORCE_INLINE_ uint32_t _hash_pass(const RD::FramebufferPass &p, uint32_t h) { + h = hash_murmur3_one_32(p.depth_attachment, h); + h = hash_murmur3_one_32(p.vrs_attachment, h); + + h = hash_murmur3_one_32(p.color_attachments.size(), h); + for (int i = 0; i < p.color_attachments.size(); i++) { + h = hash_murmur3_one_32(p.color_attachments[i], h); + } + + h = hash_murmur3_one_32(p.resolve_attachments.size(), h); + for (int i = 0; i < p.resolve_attachments.size(); i++) { + h = hash_murmur3_one_32(p.resolve_attachments[i], h); + } + + h = hash_murmur3_one_32(p.preserve_attachments.size(), h); + for (int i = 0; i < p.preserve_attachments.size(); i++) { + h = hash_murmur3_one_32(p.preserve_attachments[i], h); + } + + return h; + } + + static _FORCE_INLINE_ bool _compare_pass(const RD::FramebufferPass &a, const RD::FramebufferPass &b) { + if (a.depth_attachment != b.depth_attachment) { + return false; + } + + if (a.vrs_attachment != b.vrs_attachment) { + return false; + } + + if (a.color_attachments.size() != b.color_attachments.size()) { + return false; + } + + for (int i = 0; i < a.color_attachments.size(); i++) { + if (a.color_attachments[i] != b.color_attachments[i]) { + return false; + } + } + + if (a.resolve_attachments.size() != b.resolve_attachments.size()) { + return false; + } + + for (int i = 0; i < a.resolve_attachments.size(); i++) { + if (a.resolve_attachments[i] != b.resolve_attachments[i]) { + return false; + } + } + + if (a.preserve_attachments.size() != b.preserve_attachments.size()) { + return false; + } + + for (int i = 0; i < a.preserve_attachments.size(); i++) { + if (a.preserve_attachments[i] != b.preserve_attachments[i]) { + return false; + } + } + + return true; + } + + _FORCE_INLINE_ uint32_t _hash_rids(uint32_t h, const RID &arg) { + return hash_murmur3_one_64(arg.get_id(), h); + } + + template <typename... Args> + uint32_t _hash_rids(uint32_t h, const RID &arg, Args... args) { + h = hash_murmur3_one_64(arg.get_id(), h); + return _hash_rids(h, args...); + } + + _FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RID> &textures, const RID &arg) { + return textures[idx] == arg; + } + + template <typename... Args> + _FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RID> &textures, const RID &arg, Args... args) { + if (textures[idx] != arg) { + return false; + } + return _compare_args(idx + 1, textures, args...); + } + + _FORCE_INLINE_ void _create_args(Vector<RID> &textures, const RID &arg) { + textures.push_back(arg); + } + + template <typename... Args> + _FORCE_INLINE_ void _create_args(Vector<RID> &textures, const RID &arg, Args... args) { + textures.push_back(arg); + _create_args(textures, args...); + } + + static FramebufferCacheRD *singleton; + + uint32_t cache_instances_used = 0; + + void _invalidate(Cache *p_cache); + static void _framebuffer_invalidation_callback(void *p_userdata); + + RID _allocate_from_data(uint32_t p_views, uint32_t p_hash, uint32_t p_table_idx, const Vector<RID> &p_textures, const Vector<RD::FramebufferPass> &p_passes) { + RID rid; + if (p_passes.size()) { + rid = RD::get_singleton()->framebuffer_create_multipass(p_textures, p_passes, RD::INVALID_ID, p_views); + } else { + rid = RD::get_singleton()->framebuffer_create(p_textures, RD::INVALID_ID, p_views); + } + + ERR_FAIL_COND_V(rid.is_null(), rid); + + Cache *c = cache_allocator.alloc(); + c->views = p_views; + c->cache = rid; + c->hash = p_hash; + c->textures.resize(p_textures.size()); + for (uint32_t i = 0; i < c->textures.size(); i++) { + c->textures[i] = p_textures[i]; + } + c->passes.resize(p_passes.size()); + for (uint32_t i = 0; i < c->passes.size(); i++) { + c->passes[i] = p_passes[i]; + } + c->prev = nullptr; + c->next = hash_table[p_table_idx]; + if (hash_table[p_table_idx]) { + hash_table[p_table_idx]->prev = c; + } + hash_table[p_table_idx] = c; + + RD::get_singleton()->framebuffer_set_invalidation_callback(rid, _framebuffer_invalidation_callback, c); + + cache_instances_used++; + + return rid; + } + +public: + template <typename... Args> + RID get_cache(Args... args) { + uint32_t h = hash_murmur3_one_32(1); //1 view + h = hash_murmur3_one_32(sizeof...(Args), h); + h = _hash_args(h, args...); + h = hash_murmur3_one_32(0, h); // 0 passes + h = hash_fmix32(h); + + uint32_t table_idx = h % HASH_TABLE_SIZE; + { + const Cache *c = hash_table[table_idx]; + + while (c) { + if (c->hash == h && c->passes.size() == 0 && c->textures.size() == sizeof...(Args) && c->views == 1 && _compare_args(0, c->textures, args...)) { + return c->cache; + } + c = c->next; + } + } + + // Not in cache, create: + + Vector<RID> textures; + _create_args(textures, args...); + + return _allocate_from_data(1, h, table_idx, textures, Vector<RD::FramebufferPass>()); + } + + template <typename... Args> + RID get_cache_multiview(uint32_t p_views, Args... args) { + uint32_t h = hash_murmur3_one_32(p_views); + h = hash_murmur3_one_32(sizeof...(Args), h); + h = _hash_args(h, args...); + h = hash_murmur3_one_32(0, h); // 0 passes + h = hash_fmix32(h); + + uint32_t table_idx = h % HASH_TABLE_SIZE; + { + const Cache *c = hash_table[table_idx]; + + while (c) { + if (c->hash == h && c->passes.size() == 0 && c->textures.size() == sizeof...(Args) && c->views == p_views && _compare_args(0, c->textures, args...)) { + return c->cache; + } + c = c->next; + } + } + + // Not in cache, create: + + Vector<RID> textures; + _create_args(textures, args...); + + return _allocate_from_data(p_views, h, table_idx, textures, Vector<RD::FramebufferPass>()); + } + + RID get_cache_multipass(const Vector<RID> &p_textures, const Vector<RD::FramebufferPass> &p_passes, uint32_t p_views = 1) { + uint32_t h = hash_murmur3_one_32(p_views); + h = hash_murmur3_one_32(p_textures.size()); + for (int i = 0; i < p_textures.size(); i++) { + h = hash_murmur3_one_64(p_textures[i].get_id(), h); + } + h = hash_murmur3_one_32(p_passes.size()); + for (int i = 0; i < p_passes.size(); i++) { + h = _hash_pass(p_passes[i], h); + } + + h = hash_fmix32(h); + + uint32_t table_idx = h % HASH_TABLE_SIZE; + { + const Cache *c = hash_table[table_idx]; + + while (c) { + if (c->hash == h && c->views == p_views && c->textures.size() == (uint32_t)p_textures.size() && c->passes.size() == (uint32_t)p_passes.size()) { + bool all_ok = true; + + for (int i = 0; i < p_textures.size(); i++) { + if (p_textures[i] != c->textures[i]) { + all_ok = false; + break; + } + } + + if (all_ok) { + for (int i = 0; i < p_passes.size(); i++) { + if (!_compare_pass(p_passes[i], c->passes[i])) { + all_ok = false; + break; + } + } + } + + if (all_ok) { + return c->cache; + } + } + c = c->next; + } + } + + // Not in cache, create: + return _allocate_from_data(p_views, h, table_idx, p_textures, p_passes); + } + + static FramebufferCacheRD *get_singleton() { return singleton; } + + FramebufferCacheRD(); + ~FramebufferCacheRD(); +}; + +#endif // FRAMEBUFFER_CACHE_RD_H diff --git a/servers/rendering/renderer_rd/renderer_compositor_rd.cpp b/servers/rendering/renderer_rd/renderer_compositor_rd.cpp index a61172c8f5..967b725b9e 100644 --- a/servers/rendering/renderer_rd/renderer_compositor_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_compositor_rd.cpp @@ -249,6 +249,7 @@ RendererCompositorRD *RendererCompositorRD::singleton = nullptr; RendererCompositorRD::RendererCompositorRD() { uniform_set_cache = memnew(UniformSetCacheRD); + framebuffer_cache = memnew(FramebufferCacheRD); { String shader_cache_dir = Engine::get_singleton()->get_shader_cache_path(); @@ -316,5 +317,6 @@ RendererCompositorRD::RendererCompositorRD() { RendererCompositorRD::~RendererCompositorRD() { memdelete(uniform_set_cache); + memdelete(framebuffer_cache); ShaderRD::set_shader_cache_dir(String()); } diff --git a/servers/rendering/renderer_rd/renderer_compositor_rd.h b/servers/rendering/renderer_rd/renderer_compositor_rd.h index 564c26bfe4..a28335f800 100644 --- a/servers/rendering/renderer_rd/renderer_compositor_rd.h +++ b/servers/rendering/renderer_rd/renderer_compositor_rd.h @@ -37,6 +37,7 @@ #include "servers/rendering/renderer_rd/environment/fog.h" #include "servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.h" #include "servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h" +#include "servers/rendering/renderer_rd/framebuffer_cache_rd.h" #include "servers/rendering/renderer_rd/renderer_canvas_render_rd.h" #include "servers/rendering/renderer_rd/shaders/blit.glsl.gen.h" #include "servers/rendering/renderer_rd/storage_rd/light_storage.h" @@ -50,6 +51,7 @@ class RendererCompositorRD : public RendererCompositor { protected: UniformSetCacheRD *uniform_set_cache = nullptr; + FramebufferCacheRD *framebuffer_cache = nullptr; RendererCanvasRenderRD *canvas = nullptr; RendererRD::Utilities *utilities = nullptr; RendererRD::LightStorage *light_storage = nullptr; diff --git a/servers/rendering/renderer_rd/uniform_set_cache_rd.h b/servers/rendering/renderer_rd/uniform_set_cache_rd.h index abf110730b..bca8b02178 100644 --- a/servers/rendering/renderer_rd/uniform_set_cache_rd.h +++ b/servers/rendering/renderer_rd/uniform_set_cache_rd.h @@ -163,7 +163,7 @@ public: const Cache *c = hash_table[table_idx]; while (c) { - if (c->hash == h && c->set == p_set && c->shader == p_shader && _compare_args(0, c->uniforms, args...)) { + if (c->hash == h && c->set == p_set && c->shader == p_shader && sizeof...(Args) == c->uniforms.size() && _compare_args(0, c->uniforms, args...)) { return c->cache; } c = c->next; @@ -193,7 +193,7 @@ public: const Cache *c = hash_table[table_idx]; while (c) { - if (c->hash == h && c->set == p_set && c->shader == p_shader) { + if (c->hash == h && c->set == p_set && c->shader == p_shader && (uint32_t)p_uniforms.size() == c->uniforms.size()) { bool all_ok = true; for (int i = 0; i < p_uniforms.size(); i++) { if (!_compare_uniform(p_uniforms[i], c->uniforms[i])) { diff --git a/servers/rendering/rendering_device.cpp b/servers/rendering/rendering_device.cpp index 0b76bb3051..c07a783302 100644 --- a/servers/rendering/rendering_device.cpp +++ b/servers/rendering/rendering_device.cpp @@ -387,6 +387,7 @@ void RenderingDevice::_bind_methods() { ClassDB::bind_method(D_METHOD("framebuffer_create_multipass", "textures", "passes", "validate_with_format", "view_count"), &RenderingDevice::_framebuffer_create_multipass, DEFVAL(INVALID_FORMAT_ID), DEFVAL(1)); ClassDB::bind_method(D_METHOD("framebuffer_create_empty", "size", "samples", "validate_with_format"), &RenderingDevice::framebuffer_create_empty, DEFVAL(TEXTURE_SAMPLES_1), DEFVAL(INVALID_FORMAT_ID)); ClassDB::bind_method(D_METHOD("framebuffer_get_format", "framebuffer"), &RenderingDevice::framebuffer_get_format); + ClassDB::bind_method(D_METHOD("framebuffer_is_valid", "framebuffer"), &RenderingDevice::framebuffer_is_valid); ClassDB::bind_method(D_METHOD("sampler_create", "state"), &RenderingDevice::_sampler_create); diff --git a/servers/rendering/rendering_device.h b/servers/rendering/rendering_device.h index 03aa6f7644..a864cfa74c 100644 --- a/servers/rendering/rendering_device.h +++ b/servers/rendering/rendering_device.h @@ -129,6 +129,8 @@ public: typedef Vector<uint8_t> (*ShaderCompileToSPIRVFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language, String *r_error, const RenderingDevice *p_render_device); typedef Vector<uint8_t> (*ShaderCacheFunction)(ShaderStage p_stage, const String &p_source_code, ShaderLanguage p_language); + typedef void (*InvalidationCallback)(void *); + private: static ShaderCompileToSPIRVFunction compile_to_spirv_function; static ShaderCacheFunction cache_function; @@ -547,13 +549,15 @@ public: int32_t vrs_attachment = ATTACHMENT_UNUSED; // density map for VRS, only used if supported }; - virtual FramebufferFormatID framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, Vector<FramebufferPass> &p_passes, uint32_t p_view_count = 1) = 0; + virtual FramebufferFormatID framebuffer_format_create_multipass(const Vector<AttachmentFormat> &p_attachments, const Vector<FramebufferPass> &p_passes, uint32_t p_view_count = 1) = 0; virtual FramebufferFormatID framebuffer_format_create_empty(TextureSamples p_samples = TEXTURE_SAMPLES_1) = 0; virtual TextureSamples framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass = 0) = 0; virtual RID framebuffer_create(const Vector<RID> &p_texture_attachments, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1) = 0; - virtual RID framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1) = 0; + virtual RID framebuffer_create_multipass(const Vector<RID> &p_texture_attachments, const Vector<FramebufferPass> &p_passes, FramebufferFormatID p_format_check = INVALID_ID, uint32_t p_view_count = 1) = 0; virtual RID framebuffer_create_empty(const Size2i &p_size, TextureSamples p_samples = TEXTURE_SAMPLES_1, FramebufferFormatID p_format_check = INVALID_ID) = 0; + virtual bool framebuffer_is_valid(RID p_framebuffer) const = 0; + virtual void framebuffer_set_invalidation_callback(RID p_framebuffer, InvalidationCallback p_callback, void *p_userdata) = 0; virtual FramebufferFormatID framebuffer_get_format(RID p_framebuffer) = 0; @@ -793,8 +797,7 @@ public: virtual RID uniform_set_create(const Vector<Uniform> &p_uniforms, RID p_shader, uint32_t p_shader_set) = 0; virtual bool uniform_set_is_valid(RID p_uniform_set) = 0; - typedef void (*UniformSetInvalidatedCallback)(void *); - virtual void uniform_set_set_invalidation_callback(RID p_uniform_set, UniformSetInvalidatedCallback p_callback, void *p_userdata) = 0; + virtual void uniform_set_set_invalidation_callback(RID p_uniform_set, InvalidationCallback p_callback, void *p_userdata) = 0; virtual Error buffer_update(RID p_buffer, uint32_t p_offset, uint32_t p_size, const void *p_data, uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0; virtual Error buffer_clear(RID p_buffer, uint32_t p_offset, uint32_t p_size, uint32_t p_post_barrier = BARRIER_MASK_ALL) = 0; |