Path: blob/master/servers/rendering/renderer_rd/uniform_set_cache_rd.h
10278 views
/**************************************************************************/1/* uniform_set_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 UniformSetCacheRD : public Object {38GDCLASS(UniformSetCacheRD, Object)3940struct Cache {41Cache *prev = nullptr;42Cache *next = nullptr;43uint32_t hash = 0;44RID shader;45uint32_t set = 0;46RID cache;47LocalVector<RD::Uniform> uniforms;48};4950PagedAllocator<Cache> cache_allocator;5152enum {53HASH_TABLE_SIZE = 16381 // Prime54};5556Cache *hash_table[HASH_TABLE_SIZE] = {};5758static _FORCE_INLINE_ uint32_t _hash_uniform(const RD::Uniform &u, uint32_t h) {59h = hash_murmur3_one_32(u.uniform_type, h);60h = hash_murmur3_one_32(u.binding, h);61uint32_t rsize = u.get_id_count();62for (uint32_t j = 0; j < rsize; j++) {63h = hash_murmur3_one_64(u.get_id(j).get_id(), h);64}65return hash_fmix32(h);66}6768static _FORCE_INLINE_ bool _compare_uniform(const RD::Uniform &a, const RD::Uniform &b) {69if (a.binding != b.binding) {70return false;71}72if (a.uniform_type != b.uniform_type) {73return false;74}75uint32_t rsize = a.get_id_count();76if (rsize != b.get_id_count()) {77return false;78}79for (uint32_t j = 0; j < rsize; j++) {80if (a.get_id(j) != b.get_id(j)) {81return false;82}83}84return true;85}8687_FORCE_INLINE_ uint32_t _hash_args(uint32_t h, const RD::Uniform &arg) {88return _hash_uniform(arg, h);89}9091template <typename... Args>92uint32_t _hash_args(uint32_t h, const RD::Uniform &arg, Args... args) {93h = _hash_uniform(arg, h);94return _hash_args(h, args...);95}9697_FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RD::Uniform> &uniforms, const RD::Uniform &arg) {98return _compare_uniform(uniforms[idx], arg);99}100101template <typename... Args>102_FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RD::Uniform> &uniforms, const RD::Uniform &arg, Args... args) {103if (!_compare_uniform(uniforms[idx], arg)) {104return false;105}106return _compare_args(idx + 1, uniforms, args...);107}108109static UniformSetCacheRD *singleton;110111uint32_t cache_instances_used = 0;112113void _invalidate(Cache *p_cache);114static void _uniform_set_invalidation_callback(void *p_userdata);115116template <typename Collection>117RID _allocate_from_uniforms(RID p_shader, uint32_t p_set, uint32_t p_hash, uint32_t p_table_idx, const Collection &p_uniforms) {118RID rid = RD::get_singleton()->uniform_set_create(p_uniforms, p_shader, p_set);119ERR_FAIL_COND_V(rid.is_null(), rid);120121Cache *c = cache_allocator.alloc();122c->hash = p_hash;123c->set = p_set;124c->shader = p_shader;125c->cache = rid;126c->uniforms.resize(p_uniforms.size());127for (uint32_t i = 0; i < c->uniforms.size(); i++) {128c->uniforms[i] = p_uniforms[i];129}130c->prev = nullptr;131c->next = hash_table[p_table_idx];132if (hash_table[p_table_idx]) {133hash_table[p_table_idx]->prev = c;134}135hash_table[p_table_idx] = c;136137RD::get_singleton()->uniform_set_set_invalidation_callback(rid, _uniform_set_invalidation_callback, c);138139cache_instances_used++;140141return rid;142}143144private:145static void _bind_methods();146147public:148template <typename... Args>149RID get_cache(RID p_shader, uint32_t p_set, Args... args) {150uint32_t h = hash_murmur3_one_64(p_shader.get_id());151h = hash_murmur3_one_32(p_set, h);152h = _hash_args(h, args...);153154uint32_t table_idx = h % HASH_TABLE_SIZE;155{156const Cache *c = hash_table[table_idx];157158while (c) {159if (c->hash == h && c->set == p_set && c->shader == p_shader && sizeof...(Args) == c->uniforms.size() && _compare_args(0, c->uniforms, args...)) {160return c->cache;161}162c = c->next;163}164}165166// Not in cache, create:167168return _allocate_from_uniforms(p_shader, p_set, h, table_idx, Vector<RD::Uniform>{ args... });169}170171template <typename... Args>172RID get_cache_vec(RID p_shader, uint32_t p_set, const LocalVector<RD::Uniform> &p_uniforms) {173uint32_t h = hash_murmur3_one_64(p_shader.get_id());174h = hash_murmur3_one_32(p_set, h);175for (uint32_t i = 0; i < p_uniforms.size(); i++) {176h = _hash_uniform(p_uniforms[i], h);177}178179h = hash_fmix32(h);180181uint32_t table_idx = h % HASH_TABLE_SIZE;182{183const Cache *c = hash_table[table_idx];184185while (c) {186if (c->hash == h && c->set == p_set && c->shader == p_shader && (uint32_t)p_uniforms.size() == c->uniforms.size()) {187bool all_ok = true;188for (uint32_t i = 0; i < p_uniforms.size(); i++) {189if (!_compare_uniform(p_uniforms[i], c->uniforms[i])) {190all_ok = false;191break;192}193}194195if (all_ok) {196return c->cache;197}198}199c = c->next;200}201}202203// Not in cache, create:204return _allocate_from_uniforms(p_shader, p_set, h, table_idx, p_uniforms);205}206207static RID get_cache_array(RID p_shader, uint32_t p_set, const TypedArray<RDUniform> &p_uniforms);208209static UniformSetCacheRD *get_singleton() { return singleton; }210211UniformSetCacheRD();212~UniformSetCacheRD();213};214215216