Path: blob/master/servers/rendering/renderer_rd/uniform_set_cache_rd.cpp
10278 views
/**************************************************************************/1/* uniform_set_cache_rd.cpp */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#include "uniform_set_cache_rd.h"3132UniformSetCacheRD *UniformSetCacheRD::singleton = nullptr;3334void UniformSetCacheRD::_bind_methods() {35ClassDB::bind_static_method("UniformSetCacheRD", D_METHOD("get_cache", "shader", "set", "uniforms"), &UniformSetCacheRD::get_cache_array);36}3738RID UniformSetCacheRD::get_cache_array(RID p_shader, uint32_t p_set, const TypedArray<RDUniform> &p_uniforms) {39thread_local LocalVector<RD::Uniform> uniforms;40uniforms.clear();4142for (int i = 0; i < p_uniforms.size(); i++) {43Ref<RDUniform> uniform = p_uniforms[i];44if (uniform.is_valid()) {45uniforms.push_back(uniform->base);46}47}4849return UniformSetCacheRD::get_singleton()->get_cache_vec(p_shader, p_set, uniforms);50}5152void UniformSetCacheRD::_invalidate(Cache *p_cache) {53if (p_cache->prev) {54p_cache->prev->next = p_cache->next;55} else {56// At beginning of table57uint32_t table_idx = p_cache->hash % HASH_TABLE_SIZE;58hash_table[table_idx] = p_cache->next;59}6061if (p_cache->next) {62p_cache->next->prev = p_cache->prev;63}6465cache_allocator.free(p_cache);66cache_instances_used--;67}68void UniformSetCacheRD::_uniform_set_invalidation_callback(void *p_userdata) {69singleton->_invalidate(reinterpret_cast<Cache *>(p_userdata));70}7172UniformSetCacheRD::UniformSetCacheRD() {73ERR_FAIL_COND(singleton != nullptr);74singleton = this;75}7677UniformSetCacheRD::~UniformSetCacheRD() {78if (cache_instances_used > 0) {79ERR_PRINT("At exit: " + itos(cache_instances_used) + " uniform set cache instance(s) still in use.");80}81}828384