Path: blob/master/servers/rendering/instance_uniforms.cpp
10277 views
/**************************************************************************/1/* instance_uniforms.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 "instance_uniforms.h"3132#include "rendering_server_globals.h"3334void InstanceUniforms::free(RID p_self) {35ERR_FAIL_COND(p_self.is_null());3637if (is_allocated()) {38RSG::material_storage->global_shader_parameters_instance_free(p_self);39_location = -1;40}4142_invalidate_items();43}4445void InstanceUniforms::materials_start() {46_invalidate_items();47}4849void InstanceUniforms::materials_append(RID p_material) {50ERR_FAIL_COND(p_material.is_null());5152List<RendererMaterialStorage::InstanceShaderParam> params;53RSG::material_storage->material_get_instance_shader_parameters(p_material, ¶ms);5455for (const RendererMaterialStorage::InstanceShaderParam &srcp : params) {56StringName name = srcp.info.name;57if (Item *ptr = _parameters.getptr(name); ptr) {58if (!ptr->is_valid()) {59_init_param(*ptr, srcp);60} else if (ptr->index != srcp.index) {61WARN_PRINT("More than one material in instance export the same instance shader uniform '" + srcp.info.name +62"', but they do it with different indices. Only the first one (in order) will display correctly.");63} else if (ptr->info.type != srcp.info.type) {64WARN_PRINT("More than one material in instance export the same instance shader uniform '" + srcp.info.name +65"', but they do it with different data types. Only the first one (in order) will display correctly.");66}67} else {68Item i;69_init_param(i, srcp);70_parameters[name] = i;71}72}73}7475bool InstanceUniforms::materials_finish(RID p_self) {76ERR_FAIL_COND_V(p_self.is_null(), false);7778if (_parameters.is_empty()) {79if (is_allocated()) {80free(p_self);81return true;82}83return false;84}8586const bool should_alloc = !is_allocated();8788if (should_alloc) {89_location = RSG::material_storage->global_shader_parameters_instance_allocate(p_self);90}9192for (KeyValue<StringName, Item> &kv : _parameters) {93Item &i = kv.value;94if (i.is_valid()) {95RSG::material_storage->global_shader_parameters_instance_update(p_self, i.index, i.value, i.flags);96}97}9899return should_alloc;100}101102Variant InstanceUniforms::get(const StringName &p_name) const {103if (const Item *ptr = _parameters.getptr(p_name); ptr) {104return ptr->value;105}106return Variant();107}108109void InstanceUniforms::set(RID p_self, const StringName &p_name, const Variant &p_value) {110ERR_FAIL_COND(p_self.is_null());111ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);112113if (Item *ptr = _parameters.getptr(p_name); ptr) {114ptr->value = p_value;115if (ptr->is_valid()) {116RSG::material_storage->global_shader_parameters_instance_update(p_self, ptr->index, ptr->value, ptr->flags);117}118} else {119Item i; // Initialize in materials_finish.120i.value = p_value;121_parameters[p_name] = i;122}123}124125Variant InstanceUniforms::get_default(const StringName &p_name) const {126if (const Item *ptr = _parameters.getptr(p_name); ptr) {127return ptr->default_value;128}129return Variant();130}131132void InstanceUniforms::get_property_list(List<PropertyInfo> &r_parameters) const {133Vector<StringName> names;134135// Invalid items won't be saved, but will remain in memory in case of shader compilation failure.136for (const KeyValue<StringName, Item> &kv : _parameters) {137if (kv.value.is_valid()) {138names.push_back(kv.key);139}140}141142names.sort_custom<StringName::AlphCompare>();143144for (const StringName &n : names) {145PropertyInfo pinfo = _parameters[n].info;146r_parameters.push_back(pinfo);147}148}149150void InstanceUniforms::_init_param(Item &r_item, const RendererMaterialStorage::InstanceShaderParam &p_param) const {151r_item.index = p_param.index;152r_item.flags = 0;153r_item.info = p_param.info;154r_item.default_value = p_param.default_value;155156if (r_item.default_value.get_type() == Variant::NIL) {157Callable::CallError cerr;158Variant::construct(r_item.info.type, r_item.default_value, nullptr, 0, cerr);159}160161if (r_item.value.get_type() == Variant::NIL) {162r_item.value = r_item.default_value;163}164165if (r_item.info.hint == PROPERTY_HINT_FLAGS) {166// HACK: Detect boolean flags count and prevent overhead.167switch (r_item.info.hint_string.length()) {168case 3: // "x,y"169r_item.flags = 1;170break;171case 5: // "x,y,z"172r_item.flags = 2;173break;174case 7: // "x,y,z,w"175r_item.flags = 3;176break;177}178}179}180181void InstanceUniforms::_invalidate_items() {182for (KeyValue<StringName, Item> &kv : _parameters) {183kv.value.index = -1;184}185}186187188