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