Debian Codegraph @

servers/rendering/instance_uniforms.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* instance_uniforms.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "instance_uniforms.h"
32
33 #include "rendering_server_globals.h"
34
35 void InstanceUniforms::free(RID p_self) {
36 ERR_FAIL_COND(p_self.is_null());
37
38 if (is_allocated()) {
39 RSG::material_storage->global_shader_parameters_instance_free(p_self);
40 _location = -1;
41 }
42
43 _invalidate_items();
44 }
45
46 void InstanceUniforms::materials_start() {
47 _invalidate_items();
48 }
49
50 void InstanceUniforms::materials_append(RID p_material) {
51 ERR_FAIL_COND(p_material.is_null());
52
53 List<RendererMaterialStorage::InstanceShaderParam> params;
54 RSG::material_storage->material_get_instance_shader_parameters(p_material, &params);
55
56 for (const RendererMaterialStorage::InstanceShaderParam &srcp : params) {
57 StringName name = srcp.info.name;
58 if (Item *ptr = _parameters.getptr(name); ptr) {
59 if (!ptr->is_valid()) {
60 _init_param(*ptr, srcp);
61 } else if (ptr->index != srcp.index) {
62 WARN_PRINT("More than one material in instance export the same instance shader uniform '" + srcp.info.name +
63 "', but they do it with different indices. Only the first one (in order) will display correctly.");
64 } else if (ptr->info.type != srcp.info.type) {
65 WARN_PRINT("More than one material in instance export the same instance shader uniform '" + srcp.info.name +
66 "', but they do it with different data types. Only the first one (in order) will display correctly.");
67 }
68 } else {
69 Item i;
70 _init_param(i, srcp);
71 _parameters[name] = i;
72 }
73 }
74 }
75
76 bool InstanceUniforms::materials_finish(RID p_self) {
77 ERR_FAIL_COND_V(p_self.is_null(), false);
78
79 if (_parameters.is_empty()) {
80 if (is_allocated()) {
81 free(p_self);
82 return true;
83 }
84 return false;
85 }
86
87 const bool should_alloc = !is_allocated();
88
89 if (should_alloc) {
90 _location = RSG::material_storage->global_shader_parameters_instance_allocate(p_self);
91 }
92
93 for (KeyValue<StringName, Item> &kv : _parameters) {
94 Item &i = kv.value;
95 if (i.is_valid()) {
96 RSG::material_storage->global_shader_parameters_instance_update(p_self, i.index, i.value, i.flags);
97 }
98 }
99
100 return should_alloc;
101 }
102
103 Variant InstanceUniforms::get(const StringName &p_name) const {
104 if (const Item *ptr = _parameters.getptr(p_name); ptr) {
105 return ptr->value;
106 }
107 return Variant();
108 }
109
110 void InstanceUniforms::set(RID p_self, const StringName &p_name, const Variant &p_value) {
111 ERR_FAIL_COND(p_self.is_null());
112 ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
113
114 if (Item *ptr = _parameters.getptr(p_name); ptr) {
115 ptr->value = p_value;
116 if (ptr->is_valid()) {
117 RSG::material_storage->global_shader_parameters_instance_update(p_self, ptr->index, ptr->value, ptr->flags);
118 }
119 } else {
120 Item i; // Initialize in materials_finish.
121 i.value = p_value;
122 _parameters[p_name] = i;
123 }
124 }
125
126 Variant InstanceUniforms::get_default(const StringName &p_name) const {
127 if (const Item *ptr = _parameters.getptr(p_name); ptr) {
128 return ptr->default_value;
129 }
130 return Variant();
131 }
132
133 void InstanceUniforms::get_property_list(List<PropertyInfo> &r_parameters) const {
134 Vector<StringName> names;
135
136 // Invalid items won't be saved, but will remain in memory in case of shader compilation failure.
137 for (const KeyValue<StringName, Item> &kv : _parameters) {
138 if (kv.value.is_valid()) {
139 names.push_back(kv.key);
140 }
141 }
142
143 names.sort_custom<StringName::AlphCompare>();
144
145 for (const StringName &n : names) {
146 PropertyInfo pinfo = _parameters[n].info;
147 r_parameters.push_back(pinfo);
148 }
149 }
150
151 void InstanceUniforms::_init_param(Item &r_item, const RendererMaterialStorage::InstanceShaderParam &p_param) const {
152 r_item.index = p_param.index;
153 r_item.flags = 0;
154 r_item.info = p_param.info;
155 r_item.default_value = p_param.default_value;
156
157 if (r_item.default_value.get_type() == Variant::NIL) {
158 Callable::CallError cerr;
159 Variant::construct(r_item.info.type, r_item.default_value, nullptr, 0, cerr);
160 }
161
162 if (r_item.value.get_type() == Variant::NIL) {
163 r_item.value = r_item.default_value;
164 }
165
166 if (r_item.info.hint == PROPERTY_HINT_FLAGS) {
167 // HACK: Detect boolean flags count and prevent overhead.
168 switch (r_item.info.hint_string.length()) {
169 case 3: // "x,y"
170 r_item.flags = 1;
171 break;
172 case 5: // "x,y,z"
173 r_item.flags = 2;
174 break;
175 case 7: // "x,y,z,w"
176 r_item.flags = 3;
177 break;
178 }
179 }
180 }
181
182 void InstanceUniforms::_invalidate_items() {
183 for (KeyValue<StringName, Item> &kv : _parameters) {
184 kv.value.index = -1;
185 }
186 }