Path: blob/master/servers/rendering/renderer_rd/framebuffer_cache_rd.h
10278 views
/**************************************************************************/1/* framebuffer_cache_rd.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/templates/local_vector.h"33#include "core/templates/paged_allocator.h"34#include "servers/rendering/rendering_device.h"35#include "servers/rendering/rendering_device_binds.h"3637class FramebufferCacheRD : public Object {38GDCLASS(FramebufferCacheRD, Object)3940struct Cache {41Cache *prev = nullptr;42Cache *next = nullptr;43uint32_t hash = 0;44RID cache;45LocalVector<RID> textures;46LocalVector<RD::FramebufferPass> passes;47uint32_t views = 0;48};4950PagedAllocator<Cache> cache_allocator;5152enum {53HASH_TABLE_SIZE = 16381 // Prime54};5556Cache *hash_table[HASH_TABLE_SIZE] = {};5758static _FORCE_INLINE_ uint32_t _hash_pass(const RD::FramebufferPass &p, uint32_t h) {59h = hash_murmur3_one_32(p.depth_attachment, h);6061h = hash_murmur3_one_32(p.color_attachments.size(), h);62for (int i = 0; i < p.color_attachments.size(); i++) {63h = hash_murmur3_one_32(p.color_attachments[i], h);64}6566h = hash_murmur3_one_32(p.resolve_attachments.size(), h);67for (int i = 0; i < p.resolve_attachments.size(); i++) {68h = hash_murmur3_one_32(p.resolve_attachments[i], h);69}7071h = hash_murmur3_one_32(p.preserve_attachments.size(), h);72for (int i = 0; i < p.preserve_attachments.size(); i++) {73h = hash_murmur3_one_32(p.preserve_attachments[i], h);74}7576return h;77}7879static _FORCE_INLINE_ bool _compare_pass(const RD::FramebufferPass &a, const RD::FramebufferPass &b) {80if (a.depth_attachment != b.depth_attachment) {81return false;82}8384if (a.color_attachments.size() != b.color_attachments.size()) {85return false;86}8788for (int i = 0; i < a.color_attachments.size(); i++) {89if (a.color_attachments[i] != b.color_attachments[i]) {90return false;91}92}9394if (a.resolve_attachments.size() != b.resolve_attachments.size()) {95return false;96}9798for (int i = 0; i < a.resolve_attachments.size(); i++) {99if (a.resolve_attachments[i] != b.resolve_attachments[i]) {100return false;101}102}103104if (a.preserve_attachments.size() != b.preserve_attachments.size()) {105return false;106}107108for (int i = 0; i < a.preserve_attachments.size(); i++) {109if (a.preserve_attachments[i] != b.preserve_attachments[i]) {110return false;111}112}113114return true;115}116117_FORCE_INLINE_ uint32_t _hash_rids(uint32_t h, const RID &arg) {118return hash_murmur3_one_64(arg.get_id(), h);119}120121template <typename... Args>122uint32_t _hash_rids(uint32_t h, const RID &arg, Args... args) {123h = hash_murmur3_one_64(arg.get_id(), h);124return _hash_rids(h, args...);125}126127_FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RID> &textures, const RID &arg) {128return textures[idx] == arg;129}130131template <typename... Args>132_FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RID> &textures, const RID &arg, Args... args) {133if (textures[idx] != arg) {134return false;135}136return _compare_args(idx + 1, textures, args...);137}138139static FramebufferCacheRD *singleton;140141uint32_t cache_instances_used = 0;142143void _invalidate(Cache *p_cache);144static void _framebuffer_invalidation_callback(void *p_userdata);145146RID _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) {147RID rid;148if (p_passes.size()) {149rid = RD::get_singleton()->framebuffer_create_multipass(p_textures, p_passes, RD::INVALID_ID, p_views);150} else {151rid = RD::get_singleton()->framebuffer_create(p_textures, RD::INVALID_ID, p_views);152}153154ERR_FAIL_COND_V(rid.is_null(), rid);155156Cache *c = cache_allocator.alloc();157c->views = p_views;158c->cache = rid;159c->hash = p_hash;160c->textures.resize(p_textures.size());161for (uint32_t i = 0; i < c->textures.size(); i++) {162c->textures[i] = p_textures[i];163}164c->passes.resize(p_passes.size());165for (uint32_t i = 0; i < c->passes.size(); i++) {166c->passes[i] = p_passes[i];167}168c->prev = nullptr;169c->next = hash_table[p_table_idx];170if (hash_table[p_table_idx]) {171hash_table[p_table_idx]->prev = c;172}173hash_table[p_table_idx] = c;174175RD::get_singleton()->framebuffer_set_invalidation_callback(rid, _framebuffer_invalidation_callback, c);176177cache_instances_used++;178179return rid;180}181182private:183static void _bind_methods();184185public:186template <typename... Args>187RID get_cache(Args... args) {188uint32_t h = hash_murmur3_one_32(1); //1 view189h = hash_murmur3_one_32(sizeof...(Args), h);190h = _hash_rids(h, args...);191h = hash_murmur3_one_32(0, h); // 0 passes192h = hash_fmix32(h);193194uint32_t table_idx = h % HASH_TABLE_SIZE;195{196const Cache *c = hash_table[table_idx];197198while (c) {199if (c->hash == h && c->passes.is_empty() && c->textures.size() == sizeof...(Args) && c->views == 1 && _compare_args(0, c->textures, args...)) {200return c->cache;201}202c = c->next;203}204}205206// Not in cache, create:207208return _allocate_from_data(1, h, table_idx, Vector<RID>{ args... }, Vector<RD::FramebufferPass>());209}210211template <typename... Args>212RID get_cache_multiview(uint32_t p_views, Args... args) {213uint32_t h = hash_murmur3_one_32(p_views);214h = hash_murmur3_one_32(sizeof...(Args), h);215h = _hash_rids(h, args...);216h = hash_murmur3_one_32(0, h); // 0 passes217h = hash_fmix32(h);218219uint32_t table_idx = h % HASH_TABLE_SIZE;220{221const Cache *c = hash_table[table_idx];222223while (c) {224if (c->hash == h && c->passes.is_empty() && c->textures.size() == sizeof...(Args) && c->views == p_views && _compare_args(0, c->textures, args...)) {225return c->cache;226}227c = c->next;228}229}230231// Not in cache, create:232233return _allocate_from_data(p_views, h, table_idx, Vector<RID>{ args... }, Vector<RD::FramebufferPass>());234}235236RID get_cache_multipass(const Vector<RID> &p_textures, const Vector<RD::FramebufferPass> &p_passes, uint32_t p_views = 1) {237uint32_t h = hash_murmur3_one_32(p_views);238h = hash_murmur3_one_32(p_textures.size(), h);239for (int i = 0; i < p_textures.size(); i++) {240h = hash_murmur3_one_64(p_textures[i].get_id(), h);241}242h = hash_murmur3_one_32(p_passes.size(), h);243for (int i = 0; i < p_passes.size(); i++) {244h = _hash_pass(p_passes[i], h);245}246247h = hash_fmix32(h);248249uint32_t table_idx = h % HASH_TABLE_SIZE;250{251const Cache *c = hash_table[table_idx];252253while (c) {254if (c->hash == h && c->views == p_views && c->textures.size() == (uint32_t)p_textures.size() && c->passes.size() == (uint32_t)p_passes.size()) {255bool all_ok = true;256257for (int i = 0; i < p_textures.size(); i++) {258if (p_textures[i] != c->textures[i]) {259all_ok = false;260break;261}262}263264if (all_ok) {265for (int i = 0; i < p_passes.size(); i++) {266if (!_compare_pass(p_passes[i], c->passes[i])) {267all_ok = false;268break;269}270}271}272273if (all_ok) {274return c->cache;275}276}277c = c->next;278}279}280281// Not in cache, create:282return _allocate_from_data(p_views, h, table_idx, p_textures, p_passes);283}284285static RID get_cache_multipass_array(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_views = 1);286287static FramebufferCacheRD *get_singleton() { return singleton; }288289FramebufferCacheRD();290~FramebufferCacheRD();291};292293294