1/**************************************************************************/
2/* scene_shader_forward_clustered.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 "scene_shader_forward_clustered.h"
32#include "core/config/project_settings.h"
33#include "core/math/math_defs.h"
34#include "render_forward_clustered.h"
35#include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
36#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
37
38using namespace RendererSceneRenderImplementation;
39
40void SceneShaderForwardClustered::ShaderData::set_code(const String &p_code) {
41 //compile
42
43 code = p_code;
44 valid = false;
45 ubo_size = 0;
46 uniforms.clear();
47
48 if (code.is_empty()) {
49 return; //just invalid, but no error
50 }
51
52 ShaderCompiler::GeneratedCode gen_code;
53
54 int blend_mode = BLEND_MODE_MIX;
55 int depth_testi = DEPTH_TEST_ENABLED;
56 int alpha_antialiasing_mode = ALPHA_ANTIALIASING_OFF;
57 int cull_modei = CULL_BACK;
58
59 uses_point_size = false;
60 uses_alpha = false;
61 uses_alpha_clip = false;
62 uses_alpha_antialiasing = false;
63 uses_blend_alpha = false;
64 uses_depth_prepass_alpha = false;
65 uses_discard = false;
66 uses_roughness = false;
67 uses_normal = false;
68 bool wireframe = false;
69
70 unshaded = false;
71 uses_vertex = false;
72 uses_position = false;
73 uses_sss = false;
74 uses_transmittance = false;
75 uses_time = false;
76 writes_modelview_or_projection = false;
77 uses_world_coordinates = false;
78 uses_particle_trails = false;
79
80 int depth_drawi = DEPTH_DRAW_OPAQUE;
81
82 ShaderCompiler::IdentifierActions actions;
83 actions.entry_point_stages["vertex"] = ShaderCompiler::STAGE_VERTEX;
84 actions.entry_point_stages["fragment"] = ShaderCompiler::STAGE_FRAGMENT;
85 actions.entry_point_stages["light"] = ShaderCompiler::STAGE_FRAGMENT;
86
87 actions.render_mode_values["blend_add"] = Pair<int *, int>(&blend_mode, BLEND_MODE_ADD);
88 actions.render_mode_values["blend_mix"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MIX);
89 actions.render_mode_values["blend_sub"] = Pair<int *, int>(&blend_mode, BLEND_MODE_SUB);
90 actions.render_mode_values["blend_mul"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MUL);
91
92 actions.render_mode_values["alpha_to_coverage"] = Pair<int *, int>(&alpha_antialiasing_mode, ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE);
93 actions.render_mode_values["alpha_to_coverage_and_one"] = Pair<int *, int>(&alpha_antialiasing_mode, ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE);
94
95 actions.render_mode_values["depth_draw_never"] = Pair<int *, int>(&depth_drawi, DEPTH_DRAW_DISABLED);
96 actions.render_mode_values["depth_draw_opaque"] = Pair<int *, int>(&depth_drawi, DEPTH_DRAW_OPAQUE);
97 actions.render_mode_values["depth_draw_always"] = Pair<int *, int>(&depth_drawi, DEPTH_DRAW_ALWAYS);
98
99 actions.render_mode_values["depth_test_disabled"] = Pair<int *, int>(&depth_testi, DEPTH_TEST_DISABLED);
100
101 actions.render_mode_values["cull_disabled"] = Pair<int *, int>(&cull_modei, CULL_DISABLED);
102 actions.render_mode_values["cull_front"] = Pair<int *, int>(&cull_modei, CULL_FRONT);
103 actions.render_mode_values["cull_back"] = Pair<int *, int>(&cull_modei, CULL_BACK);
104
105 actions.render_mode_flags["unshaded"] = &unshaded;
106 actions.render_mode_flags["wireframe"] = &wireframe;
107 actions.render_mode_flags["particle_trails"] = &uses_particle_trails;
108
109 actions.usage_flag_pointers["ALPHA"] = &uses_alpha;
110 actions.usage_flag_pointers["ALPHA_SCISSOR_THRESHOLD"] = &uses_alpha_clip;
111 actions.usage_flag_pointers["ALPHA_HASH_SCALE"] = &uses_alpha_clip;
112 actions.usage_flag_pointers["ALPHA_ANTIALIASING_EDGE"] = &uses_alpha_antialiasing;
113 actions.usage_flag_pointers["ALPHA_TEXTURE_COORDINATE"] = &uses_alpha_antialiasing;
114 actions.render_mode_flags["depth_prepass_alpha"] = &uses_depth_prepass_alpha;
115
116 actions.usage_flag_pointers["SSS_STRENGTH"] = &uses_sss;
117 actions.usage_flag_pointers["SSS_TRANSMITTANCE_DEPTH"] = &uses_transmittance;
118
119 actions.usage_flag_pointers["DISCARD"] = &uses_discard;
120 actions.usage_flag_pointers["TIME"] = &uses_time;
121 actions.usage_flag_pointers["ROUGHNESS"] = &uses_roughness;
122 actions.usage_flag_pointers["NORMAL"] = &uses_normal;
123 actions.usage_flag_pointers["NORMAL_MAP"] = &uses_normal;
124
125 actions.usage_flag_pointers["POINT_SIZE"] = &uses_point_size;
126 actions.usage_flag_pointers["POINT_COORD"] = &uses_point_size;
127
128 actions.write_flag_pointers["MODELVIEW_MATRIX"] = &writes_modelview_or_projection;
129 actions.write_flag_pointers["PROJECTION_MATRIX"] = &writes_modelview_or_projection;
130 actions.write_flag_pointers["VERTEX"] = &uses_vertex;
131 actions.write_flag_pointers["POSITION"] = &uses_position;
132
133 actions.uniforms = &uniforms;
134
135 SceneShaderForwardClustered *shader_singleton = (SceneShaderForwardClustered *)SceneShaderForwardClustered::singleton;
136 Error err = shader_singleton->compiler.compile(RS::SHADER_SPATIAL, code, &actions, path, gen_code);
137 ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");
138
139 if (version.is_null()) {
140 version = shader_singleton->shader.version_create();
141 }
142
143 depth_draw = DepthDraw(depth_drawi);
144 depth_test = DepthTest(depth_testi);
145 cull_mode = Cull(cull_modei);
146 uses_screen_texture_mipmaps = gen_code.uses_screen_texture_mipmaps;
147 uses_screen_texture = gen_code.uses_screen_texture;
148 uses_depth_texture = gen_code.uses_depth_texture;
149 uses_normal_texture = gen_code.uses_normal_roughness_texture;
150 uses_vertex_time = gen_code.uses_vertex_time;
151 uses_fragment_time = gen_code.uses_fragment_time;
152
153#if 0
154 print_line("**compiling shader:");
155 print_line("**defines:\n");
156 for (int i = 0; i < gen_code.defines.size(); i++) {
157 print_line(gen_code.defines[i]);
158 }
159
160 HashMap<String, String>::Iterator el = gen_code.code.begin();
161 while (el) {
162 print_line("\n**code " + el->key + ":\n" + el->value);
163 ++el;
164 }
165
166 print_line("\n**uniforms:\n" + gen_code.uniforms);
167 print_line("\n**vertex_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX]);
168 print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]);
169#endif
170 shader_singleton->shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines);
171 ERR_FAIL_COND(!shader_singleton->shader.version_is_valid(version));
172
173 ubo_size = gen_code.uniform_total_size;
174 ubo_offsets = gen_code.uniform_offsets;
175 texture_uniforms = gen_code.texture_uniforms;
176
177 //blend modes
178
179 // if any form of Alpha Antialiasing is enabled, set the blend mode to alpha to coverage
180 if (alpha_antialiasing_mode != ALPHA_ANTIALIASING_OFF) {
181 blend_mode = BLEND_MODE_ALPHA_TO_COVERAGE;
182 }
183
184 RD::PipelineColorBlendState::Attachment blend_attachment;
185
186 switch (blend_mode) {
187 case BLEND_MODE_MIX: {
188 blend_attachment.enable_blend = true;
189 blend_attachment.alpha_blend_op = RD::BLEND_OP_ADD;
190 blend_attachment.color_blend_op = RD::BLEND_OP_ADD;
191 blend_attachment.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA;
192 blend_attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
193 blend_attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
194 blend_attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
195
196 } break;
197 case BLEND_MODE_ADD: {
198 blend_attachment.enable_blend = true;
199 blend_attachment.alpha_blend_op = RD::BLEND_OP_ADD;
200 blend_attachment.color_blend_op = RD::BLEND_OP_ADD;
201 blend_attachment.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA;
202 blend_attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
203 blend_attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA;
204 blend_attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
205 uses_blend_alpha = true; //force alpha used because of blend
206
207 } break;
208 case BLEND_MODE_SUB: {
209 blend_attachment.enable_blend = true;
210 blend_attachment.alpha_blend_op = RD::BLEND_OP_REVERSE_SUBTRACT;
211 blend_attachment.color_blend_op = RD::BLEND_OP_REVERSE_SUBTRACT;
212 blend_attachment.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA;
213 blend_attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
214 blend_attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA;
215 blend_attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
216 uses_blend_alpha = true; //force alpha used because of blend
217
218 } break;
219 case BLEND_MODE_MUL: {
220 blend_attachment.enable_blend = true;
221 blend_attachment.alpha_blend_op = RD::BLEND_OP_ADD;
222 blend_attachment.color_blend_op = RD::BLEND_OP_ADD;
223 blend_attachment.src_color_blend_factor = RD::BLEND_FACTOR_DST_COLOR;
224 blend_attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ZERO;
225 blend_attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_DST_ALPHA;
226 blend_attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
227 uses_blend_alpha = true; //force alpha used because of blend
228 } break;
229 case BLEND_MODE_ALPHA_TO_COVERAGE: {
230 blend_attachment.enable_blend = true;
231 blend_attachment.alpha_blend_op = RD::BLEND_OP_ADD;
232 blend_attachment.color_blend_op = RD::BLEND_OP_ADD;
233 blend_attachment.src_color_blend_factor = RD::BLEND_FACTOR_SRC_ALPHA;
234 blend_attachment.dst_color_blend_factor = RD::BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
235 blend_attachment.src_alpha_blend_factor = RD::BLEND_FACTOR_ONE;
236 blend_attachment.dst_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
237 }
238 }
239
240 // Color pass -> attachment 0: Color/Diffuse, attachment 1: Separate Specular, attachment 2: Motion Vectors
241 RD::PipelineColorBlendState blend_state_color_blend;
242 blend_state_color_blend.attachments = { blend_attachment, RD::PipelineColorBlendState::Attachment(), RD::PipelineColorBlendState::Attachment() };
243 RD::PipelineColorBlendState blend_state_color_opaque = RD::PipelineColorBlendState::create_disabled(3);
244 RD::PipelineColorBlendState blend_state_depth_normal_roughness = RD::PipelineColorBlendState::create_disabled(1);
245 RD::PipelineColorBlendState blend_state_depth_normal_roughness_giprobe = RD::PipelineColorBlendState::create_disabled(2);
246
247 //update pipelines
248
249 RD::PipelineDepthStencilState depth_stencil_state;
250
251 if (depth_test != DEPTH_TEST_DISABLED) {
252 depth_stencil_state.enable_depth_test = true;
253 depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_LESS_OR_EQUAL;
254 depth_stencil_state.enable_depth_write = depth_draw != DEPTH_DRAW_DISABLED ? true : false;
255 }
256 bool depth_pre_pass_enabled = bool(GLOBAL_GET("rendering/driver/depth_prepass/enable"));
257
258 for (int i = 0; i < CULL_VARIANT_MAX; i++) {
259 RD::PolygonCullMode cull_mode_rd_table[CULL_VARIANT_MAX][3] = {
260 { RD::POLYGON_CULL_DISABLED, RD::POLYGON_CULL_FRONT, RD::POLYGON_CULL_BACK },
261 { RD::POLYGON_CULL_DISABLED, RD::POLYGON_CULL_BACK, RD::POLYGON_CULL_FRONT },
262 { RD::POLYGON_CULL_DISABLED, RD::POLYGON_CULL_DISABLED, RD::POLYGON_CULL_DISABLED }
263 };
264
265 RD::PolygonCullMode cull_mode_rd = cull_mode_rd_table[i][cull_mode];
266
267 for (int j = 0; j < RS::PRIMITIVE_MAX; j++) {
268 RD::RenderPrimitive primitive_rd_table[RS::PRIMITIVE_MAX] = {
269 RD::RENDER_PRIMITIVE_POINTS,
270 RD::RENDER_PRIMITIVE_LINES,
271 RD::RENDER_PRIMITIVE_LINESTRIPS,
272 RD::RENDER_PRIMITIVE_TRIANGLES,
273 RD::RENDER_PRIMITIVE_TRIANGLE_STRIPS,
274 };
275
276 RD::RenderPrimitive primitive_rd = uses_point_size ? RD::RENDER_PRIMITIVE_POINTS : primitive_rd_table[j];
277
278 for (int k = 0; k < PIPELINE_VERSION_MAX; k++) {
279 ShaderVersion shader_version;
280 static const ShaderVersion shader_version_table[PIPELINE_VERSION_MAX] = {
281 SHADER_VERSION_DEPTH_PASS,
282 SHADER_VERSION_DEPTH_PASS_DP,
283 SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS,
284 SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI,
285 SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL,
286 SHADER_VERSION_DEPTH_PASS_WITH_SDF,
287 SHADER_VERSION_DEPTH_PASS_MULTIVIEW,
288 SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_MULTIVIEW,
289 SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI_MULTIVIEW,
290 SHADER_VERSION_COLOR_PASS,
291 };
292
293 shader_version = shader_version_table[k];
294
295 if (!static_cast<SceneShaderForwardClustered *>(singleton)->shader.is_variant_enabled(shader_version)) {
296 continue;
297 }
298 RD::PipelineRasterizationState raster_state;
299 raster_state.cull_mode = cull_mode_rd;
300 raster_state.wireframe = wireframe;
301
302 if (k == PIPELINE_VERSION_COLOR_PASS) {
303 for (int l = 0; l < PIPELINE_COLOR_PASS_FLAG_COUNT; l++) {
304 if (!shader_singleton->valid_color_pass_pipelines[l]) {
305 continue;
306 }
307
308 RD::PipelineDepthStencilState depth_stencil = depth_stencil_state;
309
310 RD::PipelineColorBlendState blend_state;
311 RD::PipelineMultisampleState multisample_state;
312
313 int shader_flags = 0;
314 if (l & PIPELINE_COLOR_PASS_FLAG_TRANSPARENT) {
315 if (alpha_antialiasing_mode == ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE) {
316 multisample_state.enable_alpha_to_coverage = true;
317 } else if (alpha_antialiasing_mode == ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE) {
318 multisample_state.enable_alpha_to_coverage = true;
319 multisample_state.enable_alpha_to_one = true;
320 }
321
322 blend_state = blend_state_color_blend;
323
324 if (depth_draw == DEPTH_DRAW_OPAQUE) {
325 depth_stencil.enable_depth_write = false; //alpha does not draw depth
326 }
327 } else {
328 blend_state = blend_state_color_opaque;
329
330 if (depth_pre_pass_enabled) {
331 // We already have a depth from the depth pre-pass, there is no need to write it again.
332 // In addition we can use COMPARE_OP_EQUAL instead of COMPARE_OP_LESS_OR_EQUAL.
333 // This way we can use the early depth test to discard transparent fragments before the fragment shader even starts.
334 depth_stencil.depth_compare_operator = RD::COMPARE_OP_EQUAL;
335 depth_stencil.enable_depth_write = false;
336 }
337
338 if (l & PIPELINE_COLOR_PASS_FLAG_SEPARATE_SPECULAR) {
339 shader_flags |= SHADER_COLOR_PASS_FLAG_SEPARATE_SPECULAR;
340 }
341 }
342
343 if (l & PIPELINE_COLOR_PASS_FLAG_MOTION_VECTORS) {
344 shader_flags |= SHADER_COLOR_PASS_FLAG_MOTION_VECTORS;
345 }
346
347 if (l & PIPELINE_COLOR_PASS_FLAG_LIGHTMAP) {
348 shader_flags |= SHADER_COLOR_PASS_FLAG_LIGHTMAP;
349 }
350
351 if (l & PIPELINE_COLOR_PASS_FLAG_MULTIVIEW) {
352 shader_flags |= SHADER_COLOR_PASS_FLAG_MULTIVIEW;
353 }
354
355 int variant = shader_version + shader_flags;
356
357 if (!static_cast<SceneShaderForwardClustered *>(singleton)->shader.is_variant_enabled(variant)) {
358 continue;
359 }
360
361 RID shader_variant = shader_singleton->shader.version_get_shader(version, variant);
362 color_pipelines[i][j][l].setup(shader_variant, primitive_rd, raster_state, multisample_state, depth_stencil, blend_state, 0, singleton->default_specialization_constants);
363 }
364 } else {
365 RD::PipelineColorBlendState blend_state;
366 RD::PipelineDepthStencilState depth_stencil = depth_stencil_state;
367 RD::PipelineMultisampleState multisample_state;
368
369 if (k == PIPELINE_VERSION_DEPTH_PASS || k == PIPELINE_VERSION_DEPTH_PASS_DP || k == PIPELINE_VERSION_DEPTH_PASS_MULTIVIEW) {
370 //none, leave empty
371 } else if (k == PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS || k == PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_MULTIVIEW) {
372 blend_state = blend_state_depth_normal_roughness;
373 } else if (k == PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI || k == PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI_MULTIVIEW) {
374 blend_state = blend_state_depth_normal_roughness_giprobe;
375 } else if (k == PIPELINE_VERSION_DEPTH_PASS_WITH_MATERIAL) {
376 blend_state = RD::PipelineColorBlendState::create_disabled(5); //writes to normal and roughness in opaque way
377 } else if (k == PIPELINE_VERSION_DEPTH_PASS_WITH_SDF) {
378 blend_state = RD::PipelineColorBlendState(); //no color targets for SDF
379 }
380
381 RID shader_variant = shader_singleton->shader.version_get_shader(version, shader_version);
382 pipelines[i][j][k].setup(shader_variant, primitive_rd, raster_state, multisample_state, depth_stencil, blend_state, 0, singleton->default_specialization_constants);
383 }
384 }
385 }
386 }
387
388 valid = true;
389}
390
391bool SceneShaderForwardClustered::ShaderData::is_animated() const {
392 return (uses_fragment_time && uses_discard) || (uses_vertex_time && uses_vertex);
393}
394
395bool SceneShaderForwardClustered::ShaderData::casts_shadows() const {
396 bool has_read_screen_alpha = uses_screen_texture || uses_depth_texture || uses_normal_texture;
397 bool has_base_alpha = (uses_alpha && (!uses_alpha_clip || uses_alpha_antialiasing)) || has_read_screen_alpha;
398 bool has_alpha = has_base_alpha || uses_blend_alpha;
399
400 return !has_alpha || (uses_depth_prepass_alpha && !(depth_draw == DEPTH_DRAW_DISABLED || depth_test == DEPTH_TEST_DISABLED));
401}
402
403RS::ShaderNativeSourceCode SceneShaderForwardClustered::ShaderData::get_native_source_code() const {
404 SceneShaderForwardClustered *shader_singleton = (SceneShaderForwardClustered *)SceneShaderForwardClustered::singleton;
405
406 return shader_singleton->shader.version_get_native_source_code(version);
407}
408
409SceneShaderForwardClustered::ShaderData::ShaderData() :
410 shader_list_element(this) {
411}
412
413SceneShaderForwardClustered::ShaderData::~ShaderData() {
414 SceneShaderForwardClustered *shader_singleton = (SceneShaderForwardClustered *)SceneShaderForwardClustered::singleton;
415 ERR_FAIL_COND(!shader_singleton);
416 //pipeline variants will clear themselves if shader is gone
417 if (version.is_valid()) {
418 shader_singleton->shader.version_free(version);
419 }
420}
421
422RendererRD::MaterialStorage::ShaderData *SceneShaderForwardClustered::_create_shader_func() {
423 ShaderData *shader_data = memnew(ShaderData);
424 singleton->shader_list.add(&shader_data->shader_list_element);
425 return shader_data;
426}
427
428void SceneShaderForwardClustered::MaterialData::set_render_priority(int p_priority) {
429 priority = p_priority - RS::MATERIAL_RENDER_PRIORITY_MIN; //8 bits
430}
431
432void SceneShaderForwardClustered::MaterialData::set_next_pass(RID p_pass) {
433 next_pass = p_pass;
434}
435
436bool SceneShaderForwardClustered::MaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
437 SceneShaderForwardClustered *shader_singleton = (SceneShaderForwardClustered *)SceneShaderForwardClustered::singleton;
438
439 return update_parameters_uniform_set(p_parameters, p_uniform_dirty, p_textures_dirty, shader_data->uniforms, shader_data->ubo_offsets.ptr(), shader_data->texture_uniforms, shader_data->default_texture_params, shader_data->ubo_size, uniform_set, shader_singleton->shader.version_get_shader(shader_data->version, 0), RenderForwardClustered::MATERIAL_UNIFORM_SET, true, true, RD::BARRIER_MASK_RASTER);
440}
441
442SceneShaderForwardClustered::MaterialData::~MaterialData() {
443 free_parameters_uniform_set(uniform_set);
444}
445
446RendererRD::MaterialStorage::MaterialData *SceneShaderForwardClustered::_create_material_func(ShaderData *p_shader) {
447 MaterialData *material_data = memnew(MaterialData);
448 material_data->shader_data = p_shader;
449 //update will happen later anyway so do nothing.
450 return material_data;
451}
452
453SceneShaderForwardClustered *SceneShaderForwardClustered::singleton = nullptr;
454
455SceneShaderForwardClustered::SceneShaderForwardClustered() {
456 // there should be only one of these, contained within our RenderFM singleton.
457 singleton = this;
458}
459
460SceneShaderForwardClustered::~SceneShaderForwardClustered() {
461 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
462
463 RD::get_singleton()->free(default_vec4_xform_buffer);
464 RD::get_singleton()->free(shadow_sampler);
465
466 material_storage->shader_free(overdraw_material_shader);
467 material_storage->shader_free(default_shader);
468 material_storage->shader_free(debug_shadow_splits_material_shader);
469
470 material_storage->material_free(overdraw_material);
471 material_storage->material_free(default_material);
472 material_storage->material_free(debug_shadow_splits_material);
473}
474
475void SceneShaderForwardClustered::init(const String p_defines) {
476 RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton();
477
478 {
479 Vector<ShaderRD::VariantDefine> shader_versions;
480 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_BASE, "\n#define MODE_RENDER_DEPTH\n", true)); // SHADER_VERSION_DEPTH_PASS
481 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_BASE, "\n#define MODE_RENDER_DEPTH\n#define MODE_DUAL_PARABOLOID\n", true)); // SHADER_VERSION_DEPTH_PASS_DP
482 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_BASE, "\n#define MODE_RENDER_DEPTH\n#define MODE_RENDER_NORMAL_ROUGHNESS\n", true)); // SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS
483 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_ADVANCED, "\n#define MODE_RENDER_DEPTH\n#define MODE_RENDER_NORMAL_ROUGHNESS\n#define MODE_RENDER_VOXEL_GI\n", false)); // SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI
484 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_ADVANCED, "\n#define MODE_RENDER_DEPTH\n#define MODE_RENDER_MATERIAL\n", false)); // SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL
485 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_ADVANCED, "\n#define MODE_RENDER_DEPTH\n#define MODE_RENDER_SDF\n", false)); // SHADER_VERSION_DEPTH_PASS_WITH_SDF
486 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_MULTIVIEW, "\n#define USE_MULTIVIEW\n#define MODE_RENDER_DEPTH\n", false)); // SHADER_VERSION_DEPTH_PASS_MULTIVIEW
487 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_MULTIVIEW, "\n#define USE_MULTIVIEW\n#define MODE_RENDER_DEPTH\n#define MODE_RENDER_NORMAL_ROUGHNESS\n", false)); // SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_MULTIVIEW
488 shader_versions.push_back(ShaderRD::VariantDefine(SHADER_GROUP_MULTIVIEW, "\n#define USE_MULTIVIEW\n#define MODE_RENDER_DEPTH\n#define MODE_RENDER_NORMAL_ROUGHNESS\n#define MODE_RENDER_VOXEL_GI\n", false)); // SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI_MULTIVIEW
489
490 Vector<String> color_pass_flags = {
491 "\n#define MODE_SEPARATE_SPECULAR\n", // SHADER_COLOR_PASS_FLAG_SEPARATE_SPECULAR
492 "\n#define USE_LIGHTMAP\n", // SHADER_COLOR_PASS_FLAG_LIGHTMAP
493 "\n#define USE_MULTIVIEW\n", // SHADER_COLOR_PASS_FLAG_MULTIVIEW
494 "\n#define MOTION_VECTORS\n", // SHADER_COLOR_PASS_FLAG_MOTION_VECTORS
495 };
496
497 for (int i = 0; i < SHADER_COLOR_PASS_FLAG_COUNT; i++) {
498 String version = "";
499 for (int j = 0; (1 << j) < SHADER_COLOR_PASS_FLAG_COUNT; j += 1) {
500 if ((1 << j) & i) {
501 version += color_pass_flags[j];
502 }
503 }
504
505 // Assign a group based on what features this pass contains.
506 ShaderGroup group = SHADER_GROUP_BASE;
507 bool advanced_group = (i & SHADER_COLOR_PASS_FLAG_SEPARATE_SPECULAR) || (i & SHADER_COLOR_PASS_FLAG_LIGHTMAP) || (i & SHADER_COLOR_PASS_FLAG_MOTION_VECTORS);
508 bool multiview_group = i & SHADER_COLOR_PASS_FLAG_MULTIVIEW;
509 if (advanced_group && multiview_group) {
510 group = SHADER_GROUP_ADVANCED_MULTIVIEW;
511 } else if (advanced_group) {
512 group = SHADER_GROUP_ADVANCED;
513 } else if (multiview_group) {
514 group = SHADER_GROUP_MULTIVIEW;
515 }
516
517 shader_versions.push_back(ShaderRD::VariantDefine(group, version, false));
518 }
519
520 shader.initialize(shader_versions, p_defines);
521
522 if (RendererCompositorRD::get_singleton()->is_xr_enabled()) {
523 shader.enable_group(SHADER_GROUP_MULTIVIEW);
524 }
525 }
526
527 // Set flag to true if a combination is valid.
528 // The only invalid combinations are those that include both TRANSPARENT and SEPARATE_SPECULAR.
529 for (int i = 0; i < PIPELINE_COLOR_PASS_FLAG_COUNT; i++) {
530 if ((i & PIPELINE_COLOR_PASS_FLAG_TRANSPARENT) && (i & PIPELINE_COLOR_PASS_FLAG_SEPARATE_SPECULAR)) {
531 valid_color_pass_pipelines[i] = false;
532 } else {
533 valid_color_pass_pipelines[i] = true;
534 }
535 }
536
537 material_storage->shader_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_3D, _create_shader_funcs);
538 material_storage->material_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_3D, _create_material_funcs);
539
540 {
541 //shader compiler
542 ShaderCompiler::DefaultIdentifierActions actions;
543
544 actions.renames["MODEL_MATRIX"] = "read_model_matrix";
545 actions.renames["MODEL_NORMAL_MATRIX"] = "model_normal_matrix";
546 actions.renames["VIEW_MATRIX"] = "read_view_matrix";
547 actions.renames["INV_VIEW_MATRIX"] = "inv_view_matrix";
548 actions.renames["PROJECTION_MATRIX"] = "projection_matrix";
549 actions.renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
550 actions.renames["MODELVIEW_MATRIX"] = "modelview";
551 actions.renames["MODELVIEW_NORMAL_MATRIX"] = "modelview_normal";
552
553 actions.renames["VERTEX"] = "vertex";
554 actions.renames["NORMAL"] = "normal";
555 actions.renames["TANGENT"] = "tangent";
556 actions.renames["BINORMAL"] = "binormal";
557 actions.renames["POSITION"] = "position";
558 actions.renames["UV"] = "uv_interp";
559 actions.renames["UV2"] = "uv2_interp";
560 actions.renames["COLOR"] = "color_interp";
561 actions.renames["POINT_SIZE"] = "gl_PointSize";
562 actions.renames["INSTANCE_ID"] = "gl_InstanceIndex";
563 actions.renames["VERTEX_ID"] = "gl_VertexIndex";
564
565 actions.renames["ALPHA_SCISSOR_THRESHOLD"] = "alpha_scissor_threshold";
566 actions.renames["ALPHA_HASH_SCALE"] = "alpha_hash_scale";
567 actions.renames["ALPHA_ANTIALIASING_EDGE"] = "alpha_antialiasing_edge";
568 actions.renames["ALPHA_TEXTURE_COORDINATE"] = "alpha_texture_coordinate";
569
570 //builtins
571
572 actions.renames["TIME"] = "global_time";
573 actions.renames["EXPOSURE"] = "(1.0 / scene_data_block.data.emissive_exposure_normalization)";
574 actions.renames["PI"] = _MKSTR(Math_PI);
575 actions.renames["TAU"] = _MKSTR(Math_TAU);
576 actions.renames["E"] = _MKSTR(Math_E);
577 actions.renames["VIEWPORT_SIZE"] = "read_viewport_size";
578
579 actions.renames["FRAGCOORD"] = "gl_FragCoord";
580 actions.renames["FRONT_FACING"] = "gl_FrontFacing";
581 actions.renames["NORMAL_MAP"] = "normal_map";
582 actions.renames["NORMAL_MAP_DEPTH"] = "normal_map_depth";
583 actions.renames["ALBEDO"] = "albedo";
584 actions.renames["ALPHA"] = "alpha";
585 actions.renames["METALLIC"] = "metallic";
586 actions.renames["SPECULAR"] = "specular";
587 actions.renames["ROUGHNESS"] = "roughness";
588 actions.renames["RIM"] = "rim";
589 actions.renames["RIM_TINT"] = "rim_tint";
590 actions.renames["CLEARCOAT"] = "clearcoat";
591 actions.renames["CLEARCOAT_ROUGHNESS"] = "clearcoat_roughness";
592 actions.renames["ANISOTROPY"] = "anisotropy";
593 actions.renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
594 actions.renames["SSS_STRENGTH"] = "sss_strength";
595 actions.renames["SSS_TRANSMITTANCE_COLOR"] = "transmittance_color";
596 actions.renames["SSS_TRANSMITTANCE_DEPTH"] = "transmittance_depth";
597 actions.renames["SSS_TRANSMITTANCE_BOOST"] = "transmittance_boost";
598 actions.renames["BACKLIGHT"] = "backlight";
599 actions.renames["AO"] = "ao";
600 actions.renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
601 actions.renames["EMISSION"] = "emission";
602 actions.renames["POINT_COORD"] = "gl_PointCoord";
603 actions.renames["INSTANCE_CUSTOM"] = "instance_custom";
604 actions.renames["SCREEN_UV"] = "screen_uv";
605 actions.renames["DEPTH"] = "gl_FragDepth";
606 actions.renames["FOG"] = "fog";
607 actions.renames["RADIANCE"] = "custom_radiance";
608 actions.renames["IRRADIANCE"] = "custom_irradiance";
609 actions.renames["BONE_INDICES"] = "bone_attrib";
610 actions.renames["BONE_WEIGHTS"] = "weight_attrib";
611 actions.renames["CUSTOM0"] = "custom0_attrib";
612 actions.renames["CUSTOM1"] = "custom1_attrib";
613 actions.renames["CUSTOM2"] = "custom2_attrib";
614 actions.renames["CUSTOM3"] = "custom3_attrib";
615 actions.renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
616
617 actions.renames["NODE_POSITION_WORLD"] = "read_model_matrix[3].xyz";
618 actions.renames["CAMERA_POSITION_WORLD"] = "scene_data.inv_view_matrix[3].xyz";
619 actions.renames["CAMERA_DIRECTION_WORLD"] = "scene_data.view_matrix[3].xyz";
620 actions.renames["CAMERA_VISIBLE_LAYERS"] = "scene_data.camera_visible_layers";
621 actions.renames["NODE_POSITION_VIEW"] = "(scene_data.view_matrix * read_model_matrix)[3].xyz";
622
623 actions.renames["VIEW_INDEX"] = "ViewIndex";
624 actions.renames["VIEW_MONO_LEFT"] = "0";
625 actions.renames["VIEW_RIGHT"] = "1";
626 actions.renames["EYE_OFFSET"] = "eye_offset";
627
628 //for light
629 actions.renames["VIEW"] = "view";
630 actions.renames["SPECULAR_AMOUNT"] = "specular_amount";
631 actions.renames["LIGHT_COLOR"] = "light_color";
632 actions.renames["LIGHT_IS_DIRECTIONAL"] = "is_directional";
633 actions.renames["LIGHT"] = "light";
634 actions.renames["ATTENUATION"] = "attenuation";
635 actions.renames["DIFFUSE_LIGHT"] = "diffuse_light";
636 actions.renames["SPECULAR_LIGHT"] = "specular_light";
637
638 actions.usage_defines["NORMAL"] = "#define NORMAL_USED\n";
639 actions.usage_defines["TANGENT"] = "#define TANGENT_USED\n";
640 actions.usage_defines["BINORMAL"] = "@TANGENT";
641 actions.usage_defines["RIM"] = "#define LIGHT_RIM_USED\n";
642 actions.usage_defines["RIM_TINT"] = "@RIM";
643 actions.usage_defines["CLEARCOAT"] = "#define LIGHT_CLEARCOAT_USED\n";
644 actions.usage_defines["CLEARCOAT_ROUGHNESS"] = "@CLEARCOAT";
645 actions.usage_defines["ANISOTROPY"] = "#define LIGHT_ANISOTROPY_USED\n";
646 actions.usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
647 actions.usage_defines["AO"] = "#define AO_USED\n";
648 actions.usage_defines["AO_LIGHT_AFFECT"] = "#define AO_USED\n";
649 actions.usage_defines["UV"] = "#define UV_USED\n";
650 actions.usage_defines["UV2"] = "#define UV2_USED\n";
651 actions.usage_defines["BONE_INDICES"] = "#define BONES_USED\n";
652 actions.usage_defines["BONE_WEIGHTS"] = "#define WEIGHTS_USED\n";
653 actions.usage_defines["CUSTOM0"] = "#define CUSTOM0_USED\n";
654 actions.usage_defines["CUSTOM1"] = "#define CUSTOM1_USED\n";
655 actions.usage_defines["CUSTOM2"] = "#define CUSTOM2_USED\n";
656 actions.usage_defines["CUSTOM3"] = "#define CUSTOM3_USED\n";
657 actions.usage_defines["NORMAL_MAP"] = "#define NORMAL_MAP_USED\n";
658 actions.usage_defines["NORMAL_MAP_DEPTH"] = "@NORMAL_MAP";
659 actions.usage_defines["COLOR"] = "#define COLOR_USED\n";
660 actions.usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
661 actions.usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
662
663 actions.usage_defines["ALPHA_SCISSOR_THRESHOLD"] = "#define ALPHA_SCISSOR_USED\n";
664 actions.usage_defines["ALPHA_HASH_SCALE"] = "#define ALPHA_HASH_USED\n";
665 actions.usage_defines["ALPHA_ANTIALIASING_EDGE"] = "#define ALPHA_ANTIALIASING_EDGE_USED\n";
666 actions.usage_defines["ALPHA_TEXTURE_COORDINATE"] = "@ALPHA_ANTIALIASING_EDGE";
667
668 actions.usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
669 actions.usage_defines["SSS_TRANSMITTANCE_DEPTH"] = "#define ENABLE_TRANSMITTANCE\n";
670 actions.usage_defines["BACKLIGHT"] = "#define LIGHT_BACKLIGHT_USED\n";
671 actions.usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
672
673 actions.usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
674 actions.usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
675
676 actions.usage_defines["FOG"] = "#define CUSTOM_FOG_USED\n";
677 actions.usage_defines["RADIANCE"] = "#define CUSTOM_RADIANCE_USED\n";
678 actions.usage_defines["IRRADIANCE"] = "#define CUSTOM_IRRADIANCE_USED\n";
679
680 actions.usage_defines["MODEL_MATRIX"] = "#define MODEL_MATRIX_USED\n";
681
682 actions.render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
683 actions.render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
684 actions.render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
685 actions.render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
686 actions.render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
687 actions.render_mode_defines["particle_trails"] = "#define USE_PARTICLE_TRAILS\n";
688 actions.render_mode_defines["depth_prepass_alpha"] = "#define USE_OPAQUE_PREPASS\n";
689
690 bool force_lambert = GLOBAL_GET("rendering/shading/overrides/force_lambert_over_burley");
691
692 if (!force_lambert) {
693 actions.render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
694 }
695
696 actions.render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
697 actions.render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
698
699 actions.render_mode_defines["sss_mode_skin"] = "#define SSS_MODE_SKIN\n";
700
701 actions.render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
702
703 actions.render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
704 actions.render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
705 actions.render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
706 actions.render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
707 actions.render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
708 actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n";
709 actions.render_mode_defines["debug_shadow_splits"] = "#define DEBUG_DRAW_PSSM_SPLITS\n";
710 actions.render_mode_defines["fog_disabled"] = "#define FOG_DISABLED\n";
711
712 actions.base_texture_binding_index = 1;
713 actions.texture_layout_set = RenderForwardClustered::MATERIAL_UNIFORM_SET;
714 actions.base_uniform_string = "material.";
715 actions.base_varying_index = 12;
716
717 actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
718 actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
719 actions.global_buffer_array_variable = "global_shader_uniforms.data";
720 actions.instance_uniform_index_variable = "instances.data[instance_index_interp].instance_uniforms_ofs";
721
722 actions.check_multiview_samplers = RendererCompositorRD::get_singleton()->is_xr_enabled(); // Make sure we check sampling multiview textures.
723
724 compiler.initialize(actions);
725 }
726
727 {
728 //default material and shader
729 default_shader = material_storage->shader_allocate();
730 material_storage->shader_initialize(default_shader);
731 material_storage->shader_set_code(default_shader, R"(
732// Default 3D material shader (clustered).
733
734shader_type spatial;
735
736void vertex() {
737 ROUGHNESS = 0.8;
738}
739
740void fragment() {
741 ALBEDO = vec3(0.6);
742 ROUGHNESS = 0.8;
743 METALLIC = 0.2;
744}
745)");
746 default_material = material_storage->material_allocate();
747 material_storage->material_initialize(default_material);
748 material_storage->material_set_shader(default_material, default_shader);
749
750 MaterialData *md = static_cast<MaterialData *>(material_storage->material_get_data(default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D));
751 default_shader_rd = shader.version_get_shader(md->shader_data->version, SHADER_VERSION_COLOR_PASS);
752 default_shader_sdfgi_rd = shader.version_get_shader(md->shader_data->version, SHADER_VERSION_DEPTH_PASS_WITH_SDF);
753
754 default_material_shader_ptr = md->shader_data;
755 default_material_uniform_set = md->uniform_set;
756 }
757
758 {
759 overdraw_material_shader = material_storage->shader_allocate();
760 material_storage->shader_initialize(overdraw_material_shader);
761 // Use relatively low opacity so that more "layers" of overlapping objects can be distinguished.
762 material_storage->shader_set_code(overdraw_material_shader, R"(
763// 3D editor Overdraw debug draw mode shader (clustered).
764
765shader_type spatial;
766
767render_mode blend_add, unshaded;
768
769void fragment() {
770 ALBEDO = vec3(0.4, 0.8, 0.8);
771 ALPHA = 0.1;
772}
773)");
774 overdraw_material = material_storage->material_allocate();
775 material_storage->material_initialize(overdraw_material);
776 material_storage->material_set_shader(overdraw_material, overdraw_material_shader);
777
778 MaterialData *md = static_cast<MaterialData *>(material_storage->material_get_data(overdraw_material, RendererRD::MaterialStorage::SHADER_TYPE_3D));
779 overdraw_material_shader_ptr = md->shader_data;
780 overdraw_material_uniform_set = md->uniform_set;
781 }
782
783 {
784 debug_shadow_splits_material_shader = material_storage->shader_allocate();
785 material_storage->shader_initialize(debug_shadow_splits_material_shader);
786 material_storage->shader_set_code(debug_shadow_splits_material_shader, R"(
787// 3D debug shadow splits mode shader(mobile).
788
789shader_type spatial;
790
791render_mode debug_shadow_splits;
792
793void fragment() {
794 ALBEDO = vec3(1.0, 1.0, 1.0);
795}
796)");
797 debug_shadow_splits_material = material_storage->material_allocate();
798 material_storage->material_initialize(debug_shadow_splits_material);
799 material_storage->material_set_shader(debug_shadow_splits_material, debug_shadow_splits_material_shader);
800
801 MaterialData *md = static_cast<MaterialData *>(material_storage->material_get_data(debug_shadow_splits_material, RendererRD::MaterialStorage::SHADER_TYPE_3D));
802 debug_shadow_splits_material_shader_ptr = md->shader_data;
803 debug_shadow_splits_material_uniform_set = md->uniform_set;
804 }
805
806 {
807 default_vec4_xform_buffer = RD::get_singleton()->storage_buffer_create(256);
808 Vector<RD::Uniform> uniforms;
809 RD::Uniform u;
810 u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
811 u.append_id(default_vec4_xform_buffer);
812 u.binding = 0;
813 uniforms.push_back(u);
814
815 default_vec4_xform_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, default_shader_rd, RenderForwardClustered::TRANSFORMS_UNIFORM_SET);
816 }
817 {
818 RD::SamplerState sampler;
819 sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR;
820 sampler.min_filter = RD::SAMPLER_FILTER_LINEAR;
821 sampler.enable_compare = true;
822 sampler.compare_op = RD::COMPARE_OP_LESS;
823 shadow_sampler = RD::get_singleton()->sampler_create(sampler);
824 }
825}
826
827void SceneShaderForwardClustered::set_default_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_constants) {
828 default_specialization_constants = p_constants;
829 for (SelfList<ShaderData> *E = shader_list.first(); E; E = E->next()) {
830 for (int i = 0; i < ShaderData::CULL_VARIANT_MAX; i++) {
831 for (int j = 0; j < RS::PRIMITIVE_MAX; j++) {
832 for (int k = 0; k < SHADER_VERSION_MAX; k++) {
833 E->self()->pipelines[i][j][k].update_specialization_constants(default_specialization_constants);
834 }
835 for (int k = 0; k < PIPELINE_COLOR_PASS_FLAG_COUNT; k++) {
836 E->self()->color_pipelines[i][j][k].update_specialization_constants(default_specialization_constants);
837 }
838 }
839 }
840 }
841}
842
843void SceneShaderForwardClustered::enable_advanced_shader_group(bool p_needs_multiview) {
844 if (p_needs_multiview || RendererCompositorRD::get_singleton()->is_xr_enabled()) {
845 shader.enable_group(SHADER_GROUP_ADVANCED_MULTIVIEW);
846 } else {
847 shader.enable_group(SHADER_GROUP_ADVANCED);
848 }
849}
850