| 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 | |
| 38 | using namespace RendererSceneRenderImplementation; |
| 39 | |
| 40 | /* ShaderData */ |
| 41 | |
| 42 | void SceneShaderForwardMobile::ShaderData::set_code(const String &p_code) { |
| 43 | //compile |
| 44 | |
| 45 | code = p_code; |
| 46 | ubo_size = 0; |
| 47 | uniforms.clear(); |
| 48 | _clear_vertex_input_mask_cache(); |
| 49 | |
| 50 | if (code.is_empty()) { |
| 51 | return; //just invalid, but no error |
| 52 | } |
| 53 | |
| 54 | ShaderCompiler::GeneratedCode gen_code; |
| 55 | |
| 56 | blend_mode = BLEND_MODE_MIX; |
| 57 | depth_test_disabledi = 0; |
| 58 | depth_test_invertedi = 0; |
| 59 | alpha_antialiasing_mode = ALPHA_ANTIALIASING_OFF; |
| 60 | cull_mode = RS::CULL_MODE_BACK; |
| 61 | |
| 62 | uses_point_size = false; |
| 63 | uses_alpha = false; |
| 64 | uses_alpha_clip = false; |
| 65 | uses_alpha_antialiasing = false; |
| 66 | uses_blend_alpha = false; |
| 67 | uses_depth_prepass_alpha = false; |
| 68 | uses_discard = false; |
| 69 | uses_roughness = false; |
| 70 | uses_normal = false; |
| 71 | uses_tangent = false; |
| 72 | uses_normal_map = false; |
| 73 | uses_bent_normal_map = false; |
| 74 | wireframe = false; |
| 75 | |
| 76 | unshaded = false; |
| 77 | uses_vertex = false; |
| 78 | uses_sss = false; |
| 79 | uses_transmittance = false; |
| 80 | uses_time = false; |
| 81 | writes_modelview_or_projection = false; |
| 82 | uses_world_coordinates = false; |
| 83 | uses_particle_trails = false; |
| 84 | |
| 85 | int depth_drawi = DEPTH_DRAW_OPAQUE; |
| 86 | |
| 87 | int stencil_readi = 0; |
| 88 | int stencil_writei = 0; |
| 89 | int stencil_write_depth_faili = 0; |
| 90 | int stencil_comparei = STENCIL_COMPARE_ALWAYS; |
| 91 | int stencil_referencei = -1; |
| 92 | |
| 93 | ShaderCompiler::IdentifierActions actions; |
| 94 | actions.entry_point_stages["vertex"] = ShaderCompiler::STAGE_VERTEX; |
| 95 | actions.entry_point_stages["fragment"] = ShaderCompiler::STAGE_FRAGMENT; |
| 96 | actions.entry_point_stages["light"] = ShaderCompiler::STAGE_FRAGMENT; |
| 97 | |
| 98 | actions.render_mode_values["blend_add"] = Pair<int *, int>(&blend_mode, BLEND_MODE_ADD); |
| 99 | actions.render_mode_values["blend_mix"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MIX); |
| 100 | actions.render_mode_values["blend_sub"] = Pair<int *, int>(&blend_mode, BLEND_MODE_SUB); |
| 101 | actions.render_mode_values["blend_mul"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MUL); |
| 102 | actions.render_mode_values["blend_premul_alpha"] = Pair<int *, int>(&blend_mode, BLEND_MODE_PREMULTIPLIED_ALPHA); |
| 103 | |
| 104 | actions.render_mode_values["alpha_to_coverage"] = Pair<int *, int>(&alpha_antialiasing_mode, ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE); |
| 105 | actions.render_mode_values["alpha_to_coverage_and_one"] = Pair<int *, int>(&alpha_antialiasing_mode, ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE); |
| 106 | |
| 107 | actions.render_mode_values["depth_draw_never"] = Pair<int *, int>(&depth_drawi, DEPTH_DRAW_DISABLED); |
| 108 | actions.render_mode_values["depth_draw_opaque"] = Pair<int *, int>(&depth_drawi, DEPTH_DRAW_OPAQUE); |
| 109 | actions.render_mode_values["depth_draw_always"] = Pair<int *, int>(&depth_drawi, DEPTH_DRAW_ALWAYS); |
| 110 | |
| 111 | actions.render_mode_values["depth_test_disabled"] = Pair<int *, int>(&depth_test_disabledi, 1); |
| 112 | actions.render_mode_values["depth_test_inverted"] = Pair<int *, int>(&depth_test_invertedi, 1); |
| 113 | |
| 114 | actions.render_mode_values["cull_disabled"] = Pair<int *, int>(&cull_mode, RS::CULL_MODE_DISABLED); |
| 115 | actions.render_mode_values["cull_front"] = Pair<int *, int>(&cull_mode, RS::CULL_MODE_FRONT); |
| 116 | actions.render_mode_values["cull_back"] = Pair<int *, int>(&cull_mode, RS::CULL_MODE_BACK); |
| 117 | |
| 118 | actions.render_mode_flags["unshaded"] = &unshaded; |
| 119 | actions.render_mode_flags["wireframe"] = &wireframe; |
| 120 | actions.render_mode_flags["particle_trails"] = &uses_particle_trails; |
| 121 | actions.render_mode_flags["world_vertex_coords"] = &uses_world_coordinates; |
| 122 | |
| 123 | actions.usage_flag_pointers["ALPHA"] = &uses_alpha; |
| 124 | actions.usage_flag_pointers["ALPHA_SCISSOR_THRESHOLD"] = &uses_alpha_clip; |
| 125 | actions.usage_flag_pointers["ALPHA_HASH_SCALE"] = &uses_alpha_clip; |
| 126 | actions.usage_flag_pointers["ALPHA_ANTIALIASING_EDGE"] = &uses_alpha_antialiasing; |
| 127 | actions.usage_flag_pointers["ALPHA_TEXTURE_COORDINATE"] = &uses_alpha_antialiasing; |
| 128 | actions.render_mode_flags["depth_prepass_alpha"] = &uses_depth_prepass_alpha; |
| 129 | |
| 130 | actions.usage_flag_pointers["SSS_STRENGTH"] = &uses_sss; |
| 131 | actions.usage_flag_pointers["SSS_TRANSMITTANCE_DEPTH"] = &uses_transmittance; |
| 132 | |
| 133 | actions.usage_flag_pointers["DISCARD"] = &uses_discard; |
| 134 | actions.usage_flag_pointers["TIME"] = &uses_time; |
| 135 | actions.usage_flag_pointers["ROUGHNESS"] = &uses_roughness; |
| 136 | actions.usage_flag_pointers["NORMAL"] = &uses_normal; |
| 137 | actions.usage_flag_pointers["NORMAL_MAP"] = &uses_normal_map; |
| 138 | actions.usage_flag_pointers["BENT_NORMAL_MAP"] = &uses_bent_normal_map; |
| 139 | |
| 140 | actions.usage_flag_pointers["TANGENT"] = &uses_tangent; |
| 141 | actions.usage_flag_pointers["BINORMAL"] = &uses_tangent; |
| 142 | actions.usage_flag_pointers["ANISOTROPY"] = &uses_tangent; |
| 143 | actions.usage_flag_pointers["ANISOTROPY_FLOW"] = &uses_tangent; |
| 144 | |
| 145 | actions.usage_flag_pointers["POINT_SIZE"] = &uses_point_size; |
| 146 | actions.usage_flag_pointers["POINT_COORD"] = &uses_point_size; |
| 147 | |
| 148 | actions.write_flag_pointers["MODELVIEW_MATRIX"] = &writes_modelview_or_projection; |
| 149 | actions.write_flag_pointers["PROJECTION_MATRIX"] = &writes_modelview_or_projection; |
| 150 | actions.write_flag_pointers["VERTEX"] = &uses_vertex; |
| 151 | |
| 152 | actions.stencil_mode_values["read"] = Pair<int *, int>(&stencil_readi, STENCIL_FLAG_READ); |
| 153 | actions.stencil_mode_values["write"] = Pair<int *, int>(&stencil_writei, STENCIL_FLAG_WRITE); |
| 154 | actions.stencil_mode_values["write_depth_fail"] = Pair<int *, int>(&stencil_write_depth_faili, STENCIL_FLAG_WRITE_DEPTH_FAIL); |
| 155 | |
| 156 | actions.stencil_mode_values["compare_less"] = Pair<int *, int>(&stencil_comparei, STENCIL_COMPARE_LESS); |
| 157 | actions.stencil_mode_values["compare_equal"] = Pair<int *, int>(&stencil_comparei, STENCIL_COMPARE_EQUAL); |
| 158 | actions.stencil_mode_values["compare_less_or_equal"] = Pair<int *, int>(&stencil_comparei, STENCIL_COMPARE_LESS_OR_EQUAL); |
| 159 | actions.stencil_mode_values["compare_greater"] = Pair<int *, int>(&stencil_comparei, STENCIL_COMPARE_GREATER); |
| 160 | actions.stencil_mode_values["compare_not_equal"] = Pair<int *, int>(&stencil_comparei, STENCIL_COMPARE_NOT_EQUAL); |
| 161 | actions.stencil_mode_values["compare_greater_or_equal"] = Pair<int *, int>(&stencil_comparei, STENCIL_COMPARE_GREATER_OR_EQUAL); |
| 162 | actions.stencil_mode_values["compare_always"] = Pair<int *, int>(&stencil_comparei, STENCIL_COMPARE_ALWAYS); |
| 163 | |
| 164 | actions.stencil_reference = &stencil_referencei; |
| 165 | |
| 166 | actions.uniforms = &uniforms; |
| 167 | |
| 168 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 169 | Error err = SceneShaderForwardMobile::singleton->compiler.compile(RS::SHADER_SPATIAL, code, &actions, path, gen_code); |
| 170 | |
| 171 | if (err != OK) { |
| 172 | if (version.is_valid()) { |
| 173 | SceneShaderForwardMobile::singleton->shader.version_free(version); |
| 174 | version = RID(); |
| 175 | } |
| 176 | ERR_FAIL_MSG("Shader compilation failed."); |
| 177 | } |
| 178 | |
| 179 | if (version.is_null()) { |
| 180 | version = SceneShaderForwardMobile::singleton->shader.version_create(false); |
| 181 | } |
| 182 | |
| 183 | depth_draw = DepthDraw(depth_drawi); |
| 184 | if (depth_test_disabledi) { |
| 185 | depth_test = DEPTH_TEST_DISABLED; |
| 186 | } else if (depth_test_invertedi) { |
| 187 | depth_test = DEPTH_TEST_ENABLED_INVERTED; |
| 188 | } else { |
| 189 | depth_test = DEPTH_TEST_ENABLED; |
| 190 | } |
| 191 | uses_vertex_time = gen_code.uses_vertex_time; |
| 192 | uses_fragment_time = gen_code.uses_fragment_time; |
| 193 | uses_screen_texture_mipmaps = gen_code.uses_screen_texture_mipmaps; |
| 194 | uses_screen_texture = gen_code.uses_screen_texture; |
| 195 | uses_depth_texture = gen_code.uses_depth_texture; |
| 196 | uses_normal_texture = gen_code.uses_normal_roughness_texture; |
| 197 | uses_normal |= uses_normal_map; |
| 198 | uses_normal |= uses_bent_normal_map; |
| 199 | uses_tangent |= uses_normal_map; |
| 200 | uses_tangent |= uses_bent_normal_map; |
| 201 | |
| 202 | stencil_enabled = stencil_referencei != -1; |
| 203 | stencil_flags = stencil_readi | stencil_writei | stencil_write_depth_faili; |
| 204 | stencil_compare = StencilCompare(stencil_comparei); |
| 205 | stencil_reference = stencil_referencei; |
| 206 | |
| 207 | #ifdef DEBUG_ENABLED |
| 208 | if (uses_sss) { |
| 209 | WARN_PRINT_ONCE_ED("Subsurface scattering is only available when using the Forward+ renderer."); |
| 210 | } |
| 211 | |
| 212 | if (uses_transmittance) { |
| 213 | WARN_PRINT_ONCE_ED("Transmittance is only available when using the Forward+ renderer."); |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | #if 0 |
| 218 | print_line("**compiling shader:"); |
| 219 | print_line("**defines:\n"); |
| 220 | for (int i = 0; i < gen_code.defines.size(); i++) { |
| 221 | print_line(gen_code.defines[i]); |
| 222 | } |
| 223 | |
| 224 | HashMap<String, String>::Iterator el = gen_code.code.begin(); |
| 225 | while (el) { |
| 226 | print_line("\n**code " + el->key + ":\n" + el->value); |
| 227 | ++el; |
| 228 | } |
| 229 | |
| 230 | print_line("\n**uniforms:\n" + gen_code.uniforms); |
| 231 | print_line("\n**vertex_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX]); |
| 232 | print_line("\n**fragment_globals:\n" + gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT]); |
| 233 | #endif |
| 234 | |
| 235 | SceneShaderForwardMobile::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); |
| 236 | |
| 237 | ubo_size = gen_code.uniform_total_size; |
| 238 | ubo_offsets = gen_code.uniform_offsets; |
| 239 | texture_uniforms = gen_code.texture_uniforms; |
| 240 | |
| 241 | pipeline_hash_map.clear_pipelines(); |
| 242 | |
| 243 | // If any form of Alpha Antialiasing is enabled, set the blend mode to alpha to coverage. |
| 244 | if (alpha_antialiasing_mode != ALPHA_ANTIALIASING_OFF) { |
| 245 | blend_mode = BLEND_MODE_ALPHA_TO_COVERAGE; |
| 246 | } |
| 247 | |
| 248 | uses_blend_alpha = blend_mode_uses_blend_alpha(BlendMode(blend_mode)); |
| 249 | } |
| 250 | |
| 251 | bool SceneShaderForwardMobile::ShaderData::is_animated() const { |
| 252 | return (uses_fragment_time && uses_discard) || (uses_vertex_time && uses_vertex); |
| 253 | } |
| 254 | |
| 255 | bool SceneShaderForwardMobile::ShaderData::casts_shadows() const { |
| 256 | bool has_read_screen_alpha = uses_screen_texture || uses_depth_texture || uses_normal_texture; |
| 257 | bool has_base_alpha = (uses_alpha && (!uses_alpha_clip || uses_alpha_antialiasing)) || has_read_screen_alpha; |
| 258 | bool has_alpha = has_base_alpha || uses_blend_alpha; |
| 259 | |
| 260 | return !has_alpha || (uses_depth_prepass_alpha && !(depth_draw == DEPTH_DRAW_DISABLED || depth_test != DEPTH_TEST_ENABLED)); |
| 261 | } |
| 262 | |
| 263 | RS::ShaderNativeSourceCode SceneShaderForwardMobile::ShaderData::get_native_source_code() const { |
| 264 | if (version.is_valid()) { |
| 265 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 266 | return SceneShaderForwardMobile::singleton->shader.version_get_native_source_code(version); |
| 267 | } else { |
| 268 | return RS::ShaderNativeSourceCode(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | Pair<ShaderRD *, RID> SceneShaderForwardMobile::ShaderData::get_native_shader_and_version() const { |
| 273 | if (version.is_valid()) { |
| 274 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 275 | return { &SceneShaderForwardMobile::singleton->shader, version }; |
| 276 | } else { |
| 277 | return {}; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void SceneShaderForwardMobile::ShaderData::_create_pipeline(PipelineKey p_pipeline_key) { |
| 282 | #if PRINT_PIPELINE_COMPILATION_KEYS |
| 283 | print_line( |
| 284 | "HASH:", p_pipeline_key.hash(), |
| 285 | "VERSION:", version, |
| 286 | "VERTEX:", p_pipeline_key.vertex_format_id, |
| 287 | "FRAMEBUFFER:", p_pipeline_key.framebuffer_format_id, |
| 288 | "CULL:", p_pipeline_key.cull_mode, |
| 289 | "PRIMITIVE:", p_pipeline_key.primitive_type, |
| 290 | "VERSION:", p_pipeline_key.version, |
| 291 | "SPEC PACKED #0:", p_pipeline_key.shader_specialization.packed_0, |
| 292 | "SPEC PACKED #1:", p_pipeline_key.shader_specialization.packed_1, |
| 293 | "SPEC PACKED #2:", p_pipeline_key.shader_specialization.packed_2, |
| 294 | "RENDER PASS:", p_pipeline_key.render_pass, |
| 295 | "WIREFRAME:", p_pipeline_key.wireframe); |
| 296 | #endif |
| 297 | |
| 298 | RD::PipelineColorBlendState::Attachment blend_attachment = blend_mode_to_blend_attachment(BlendMode(blend_mode)); |
| 299 | RD::PipelineColorBlendState blend_state_blend; |
| 300 | blend_state_blend.attachments.push_back(blend_attachment); |
| 301 | RD::PipelineColorBlendState blend_state_opaque = RD::PipelineColorBlendState::create_disabled(1); |
| 302 | RD::PipelineColorBlendState blend_state_opaque_specular = RD::PipelineColorBlendState::create_disabled(2); |
| 303 | RD::PipelineColorBlendState blend_state_depth_normal_roughness = RD::PipelineColorBlendState::create_disabled(1); |
| 304 | RD::PipelineColorBlendState blend_state_depth_normal_roughness_giprobe = RD::PipelineColorBlendState::create_disabled(2); |
| 305 | |
| 306 | //update pipelines |
| 307 | |
| 308 | RD::PipelineDepthStencilState depth_stencil_state; |
| 309 | |
| 310 | if (depth_test != DEPTH_TEST_DISABLED) { |
| 311 | depth_stencil_state.enable_depth_test = true; |
| 312 | depth_stencil_state.enable_depth_write = depth_draw != DEPTH_DRAW_DISABLED ? true : false; |
| 313 | depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_GREATER_OR_EQUAL; |
| 314 | |
| 315 | if (depth_test == DEPTH_TEST_ENABLED_INVERTED) { |
| 316 | depth_stencil_state.depth_compare_operator = RD::COMPARE_OP_LESS; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | RD::RenderPrimitive primitive_rd_table[RS::PRIMITIVE_MAX] = { |
| 321 | RD::RENDER_PRIMITIVE_POINTS, |
| 322 | RD::RENDER_PRIMITIVE_LINES, |
| 323 | RD::RENDER_PRIMITIVE_LINESTRIPS, |
| 324 | RD::RENDER_PRIMITIVE_TRIANGLES, |
| 325 | RD::RENDER_PRIMITIVE_TRIANGLE_STRIPS, |
| 326 | }; |
| 327 | |
| 328 | depth_stencil_state.enable_stencil = stencil_enabled; |
| 329 | if (stencil_enabled) { |
| 330 | static const RD::CompareOperator stencil_compare_rd_table[STENCIL_COMPARE_MAX] = { |
| 331 | RD::COMPARE_OP_LESS, |
| 332 | RD::COMPARE_OP_EQUAL, |
| 333 | RD::COMPARE_OP_LESS_OR_EQUAL, |
| 334 | RD::COMPARE_OP_GREATER, |
| 335 | RD::COMPARE_OP_NOT_EQUAL, |
| 336 | RD::COMPARE_OP_GREATER_OR_EQUAL, |
| 337 | RD::COMPARE_OP_ALWAYS, |
| 338 | }; |
| 339 | |
| 340 | uint32_t stencil_mask = 255; |
| 341 | |
| 342 | RD::PipelineDepthStencilState::StencilOperationState op; |
| 343 | op.fail = RD::STENCIL_OP_KEEP; |
| 344 | op.pass = RD::STENCIL_OP_KEEP; |
| 345 | op.depth_fail = RD::STENCIL_OP_KEEP; |
| 346 | op.compare = stencil_compare_rd_table[stencil_compare]; |
| 347 | op.compare_mask = 0; |
| 348 | op.write_mask = 0; |
| 349 | op.reference = stencil_reference; |
| 350 | |
| 351 | if (stencil_flags & STENCIL_FLAG_READ) { |
| 352 | op.compare_mask = stencil_mask; |
| 353 | } |
| 354 | |
| 355 | if (stencil_flags & STENCIL_FLAG_WRITE) { |
| 356 | op.pass = RD::STENCIL_OP_REPLACE; |
| 357 | op.write_mask = stencil_mask; |
| 358 | } |
| 359 | |
| 360 | if (stencil_flags & STENCIL_FLAG_WRITE_DEPTH_FAIL) { |
| 361 | op.depth_fail = RD::STENCIL_OP_REPLACE; |
| 362 | op.write_mask = stencil_mask; |
| 363 | } |
| 364 | |
| 365 | depth_stencil_state.front_op = op; |
| 366 | depth_stencil_state.back_op = op; |
| 367 | } |
| 368 | |
| 369 | bool emulate_point_size_flag = uses_point_size && SceneShaderForwardMobile::singleton->emulate_point_size; |
| 370 | |
| 371 | RD::RenderPrimitive primitive_rd; |
| 372 | if (uses_point_size) { |
| 373 | primitive_rd = emulate_point_size_flag ? RD::RENDER_PRIMITIVE_TRIANGLES : RD::RENDER_PRIMITIVE_POINTS; |
| 374 | } else { |
| 375 | primitive_rd = primitive_rd_table[p_pipeline_key.primitive_type]; |
| 376 | } |
| 377 | |
| 378 | RD::PipelineRasterizationState raster_state; |
| 379 | raster_state.cull_mode = p_pipeline_key.cull_mode; |
| 380 | raster_state.wireframe = wireframe || p_pipeline_key.wireframe; |
| 381 | |
| 382 | RD::PipelineMultisampleState multisample_state; |
| 383 | multisample_state.sample_count = RD::get_singleton()->framebuffer_format_get_texture_samples(p_pipeline_key.framebuffer_format_id, 0); |
| 384 | |
| 385 | RD::PipelineColorBlendState blend_state; |
| 386 | if (uses_alpha || uses_blend_alpha) { |
| 387 | // These flags should only go through if we have some form of MSAA. |
| 388 | if (alpha_antialiasing_mode == ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE) { |
| 389 | multisample_state.enable_alpha_to_coverage = true; |
| 390 | } else if (alpha_antialiasing_mode == ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE) { |
| 391 | multisample_state.enable_alpha_to_coverage = true; |
| 392 | multisample_state.enable_alpha_to_one = true; |
| 393 | } |
| 394 | |
| 395 | if (p_pipeline_key.version == SHADER_VERSION_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_MOTION_VECTORS_MULTIVIEW) { |
| 396 | blend_state = blend_state_blend; |
| 397 | if (depth_draw == DEPTH_DRAW_OPAQUE && !uses_alpha_clip) { |
| 398 | // Alpha does not write to depth. |
| 399 | depth_stencil_state.enable_depth_write = false; |
| 400 | } |
| 401 | } else if (p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS || p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS_DP) { |
| 402 | // Contains nothing. |
| 403 | } else if (p_pipeline_key.version == SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL) { |
| 404 | // Writes to normal and roughness in opaque way. |
| 405 | blend_state = RD::PipelineColorBlendState::create_disabled(5); |
| 406 | } else { |
| 407 | // Do not use this version (error case). |
| 408 | } |
| 409 | } else { |
| 410 | if (p_pipeline_key.version == SHADER_VERSION_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_MOTION_VECTORS_MULTIVIEW) { |
| 411 | blend_state = blend_state_opaque; |
| 412 | } else if (p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS || p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS_DP) { |
| 413 | // Contains nothing. |
| 414 | } else if (p_pipeline_key.version == SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL) { |
| 415 | // Writes to normal and roughness in opaque way. |
| 416 | blend_state = RD::PipelineColorBlendState::create_disabled(5); |
| 417 | } else { |
| 418 | // Unknown pipeline version. |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Convert the specialization from the key to pipeline specialization constants. |
| 423 | Vector<RD::PipelineSpecializationConstant> specialization_constants; |
| 424 | RD::PipelineSpecializationConstant sc; |
| 425 | sc.constant_id = 0; |
| 426 | sc.int_value = p_pipeline_key.shader_specialization.packed_0; |
| 427 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT; |
| 428 | specialization_constants.push_back(sc); |
| 429 | |
| 430 | sc.constant_id = 1; |
| 431 | sc.int_value = p_pipeline_key.shader_specialization.packed_1; |
| 432 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT; |
| 433 | specialization_constants.push_back(sc); |
| 434 | |
| 435 | sc.constant_id = 2; |
| 436 | sc.float_value = p_pipeline_key.shader_specialization.packed_2; |
| 437 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT; |
| 438 | specialization_constants.push_back(sc); |
| 439 | |
| 440 | sc = {}; // Sanitize value bits. "bool_value" only assigns 8 bits and keeps the remaining bits intact. |
| 441 | sc.constant_id = 3; |
| 442 | sc.bool_value = emulate_point_size_flag; |
| 443 | sc.type = RD::PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL; |
| 444 | specialization_constants.push_back(sc); |
| 445 | |
| 446 | RID shader_rid = get_shader_variant(p_pipeline_key.version, p_pipeline_key.ubershader); |
| 447 | ERR_FAIL_COND(shader_rid.is_null()); |
| 448 | |
| 449 | RID pipeline = RD::get_singleton()->render_pipeline_create(shader_rid, p_pipeline_key.framebuffer_format_id, p_pipeline_key.vertex_format_id, primitive_rd, raster_state, multisample_state, depth_stencil_state, blend_state, 0, p_pipeline_key.render_pass, specialization_constants); |
| 450 | ERR_FAIL_COND(pipeline.is_null()); |
| 451 | |
| 452 | pipeline_hash_map.add_compiled_pipeline(p_pipeline_key.hash(), pipeline); |
| 453 | } |
| 454 | |
| 455 | RD::PolygonCullMode SceneShaderForwardMobile::ShaderData::get_cull_mode_from_cull_variant(CullVariant p_cull_variant) { |
| 456 | const RD::PolygonCullMode cull_mode_rd_table[CULL_VARIANT_MAX][3] = { |
| 457 | { RD::POLYGON_CULL_DISABLED, RD::POLYGON_CULL_FRONT, RD::POLYGON_CULL_BACK }, |
| 458 | { RD::POLYGON_CULL_DISABLED, RD::POLYGON_CULL_BACK, RD::POLYGON_CULL_FRONT }, |
| 459 | { RD::POLYGON_CULL_DISABLED, RD::POLYGON_CULL_DISABLED, RD::POLYGON_CULL_DISABLED } |
| 460 | }; |
| 461 | |
| 462 | return cull_mode_rd_table[p_cull_variant][cull_mode]; |
| 463 | } |
| 464 | |
| 465 | void SceneShaderForwardMobile::ShaderData::_clear_vertex_input_mask_cache() { |
| 466 | for (uint32_t i = 0; i < VERTEX_INPUT_MASKS_SIZE; i++) { |
| 467 | vertex_input_masks[i].store(0); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | RID SceneShaderForwardMobile::ShaderData::get_shader_variant(ShaderVersion p_shader_version, bool p_ubershader) const { |
| 472 | if (version.is_valid()) { |
| 473 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 474 | ERR_FAIL_NULL_V(SceneShaderForwardMobile::singleton, RID()); |
| 475 | return SceneShaderForwardMobile::singleton->shader.version_get_shader(version, p_shader_version + (SceneShaderForwardMobile::singleton->use_fp16 ? SHADER_VERSION_MAX * 2 : 0) + (p_ubershader ? SHADER_VERSION_MAX : 0)); |
| 476 | } else { |
| 477 | return RID(); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | uint64_t SceneShaderForwardMobile::ShaderData::get_vertex_input_mask(ShaderVersion p_shader_version, bool p_ubershader) { |
| 482 | // Vertex input masks require knowledge of the shader. Since querying the shader can be expensive due to high contention and the necessary mutex, we cache the result instead. |
| 483 | // It is intentional for the range of the input masks to be different than the versions available in the shaders as it'll only ever use the regular variants or the FP16 ones. |
| 484 | uint32_t input_mask_index = p_shader_version + (p_ubershader ? SHADER_VERSION_MAX : 0); |
| 485 | uint64_t input_mask = vertex_input_masks[input_mask_index].load(std::memory_order_relaxed); |
| 486 | if (input_mask == 0) { |
| 487 | RID shader_rid = get_shader_variant(p_shader_version, p_ubershader); |
| 488 | ERR_FAIL_COND_V(shader_rid.is_null(), 0); |
| 489 | |
| 490 | input_mask = RD::get_singleton()->shader_get_vertex_input_attribute_mask(shader_rid); |
| 491 | vertex_input_masks[input_mask_index].store(input_mask, std::memory_order_relaxed); |
| 492 | } |
| 493 | |
| 494 | return input_mask; |
| 495 | } |
| 496 | |
| 497 | bool SceneShaderForwardMobile::ShaderData::is_valid() const { |
| 498 | if (version.is_valid()) { |
| 499 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 500 | ERR_FAIL_NULL_V(SceneShaderForwardMobile::singleton, false); |
| 501 | return SceneShaderForwardMobile::singleton->shader.version_is_valid(version); |
| 502 | } else { |
| 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | SceneShaderForwardMobile::ShaderData::ShaderData() : |
| 508 | shader_list_element(this) { |
| 509 | pipeline_hash_map.set_creation_object_and_function(this, &ShaderData::_create_pipeline); |
| 510 | pipeline_hash_map.set_compilations(SceneShaderForwardMobile::singleton->pipeline_compilations, &SceneShaderForwardMobile::singleton_mutex); |
| 511 | } |
| 512 | |
| 513 | SceneShaderForwardMobile::ShaderData::~ShaderData() { |
| 514 | pipeline_hash_map.clear_pipelines(); |
| 515 | |
| 516 | if (version.is_valid()) { |
| 517 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 518 | ERR_FAIL_NULL(SceneShaderForwardMobile::singleton); |
| 519 | SceneShaderForwardMobile::singleton->shader.version_free(version); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | RendererRD::MaterialStorage::ShaderData *SceneShaderForwardMobile::_create_shader_func() { |
| 524 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 525 | ShaderData *shader_data = memnew(ShaderData); |
| 526 | singleton->shader_list.add(&shader_data->shader_list_element); |
| 527 | return shader_data; |
| 528 | } |
| 529 | |
| 530 | void SceneShaderForwardMobile::MaterialData::set_render_priority(int p_priority) { |
| 531 | priority = p_priority - RS::MATERIAL_RENDER_PRIORITY_MIN; //8 bits |
| 532 | } |
| 533 | |
| 534 | void SceneShaderForwardMobile::MaterialData::set_next_pass(RID p_pass) { |
| 535 | next_pass = p_pass; |
| 536 | } |
| 537 | |
| 538 | bool SceneShaderForwardMobile::MaterialData::update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) { |
| 539 | if (shader_data->version.is_valid()) { |
| 540 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 541 | RID base_shader = SceneShaderForwardMobile::singleton->shader.version_get_shader(shader_data->version, (SceneShaderForwardMobile::singleton->use_fp16 ? SHADER_VERSION_MAX * 2 : 0)); |
| 542 | 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, base_shader, RenderForwardMobile::MATERIAL_UNIFORM_SET, true, true); |
| 543 | } else { |
| 544 | return false; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | SceneShaderForwardMobile::MaterialData::~MaterialData() { |
| 549 | free_parameters_uniform_set(uniform_set); |
| 550 | } |
| 551 | |
| 552 | RendererRD::MaterialStorage::MaterialData *SceneShaderForwardMobile::_create_material_func(ShaderData *p_shader) { |
| 553 | MaterialData *material_data = memnew(MaterialData); |
| 554 | material_data->shader_data = p_shader; |
| 555 | //update will happen later anyway so do nothing. |
| 556 | return material_data; |
| 557 | } |
| 558 | |
| 559 | /* Scene Shader */ |
| 560 | |
| 561 | SceneShaderForwardMobile *SceneShaderForwardMobile::singleton = nullptr; |
| 562 | Mutex SceneShaderForwardMobile::singleton_mutex; |
| 563 | |
| 564 | SceneShaderForwardMobile::SceneShaderForwardMobile() { |
| 565 | // there should be only one of these, contained within our RenderForwardMobile singleton. |
| 566 | singleton = this; |
| 567 | } |
| 568 | |
| 569 | void SceneShaderForwardMobile::init(const String p_defines) { |
| 570 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 571 | |
| 572 | // Store whether the shader will prefer using the FP16 variant. |
| 573 | use_fp16 = RD::get_singleton()->has_feature(RD::SUPPORTS_HALF_FLOAT); |
| 574 | |
| 575 | emulate_point_size = !RD::get_singleton()->has_feature(RD::SUPPORTS_POINT_SIZE); |
| 576 | |
| 577 | // Immutable samplers : create the shadow sampler to be passed when creating the pipeline. |
| 578 | { |
| 579 | RD::SamplerState sampler; |
| 580 | sampler.mag_filter = RD::SAMPLER_FILTER_LINEAR; |
| 581 | sampler.min_filter = RD::SAMPLER_FILTER_LINEAR; |
| 582 | sampler.enable_compare = true; |
| 583 | sampler.compare_op = RD::COMPARE_OP_GREATER; |
| 584 | shadow_sampler = RD::get_singleton()->sampler_create(sampler); |
| 585 | } |
| 586 | |
| 587 | /* SCENE SHADER */ |
| 588 | |
| 589 | { |
| 590 | Vector<ShaderRD::VariantDefine> shader_versions; |
| 591 | for (uint32_t fp16 = 0; fp16 < 2; fp16++) { |
| 592 | for (uint32_t ubershader = 0; ubershader < 2; ubershader++) { |
| 593 | String base_define = fp16 ? "\n#define EXPLICIT_FP16\n" : ""; |
| 594 | int shader_group = fp16 ? SHADER_GROUP_FP16 : SHADER_GROUP_FP32; |
| 595 | int shader_group_multiview = fp16 ? SHADER_GROUP_FP16_MULTIVIEW : SHADER_GROUP_FP32_MULTIVIEW; |
| 596 | base_define += ubershader ? "\n#define UBERSHADER\n" : ""; |
| 597 | |
| 598 | bool default_enabled = (uint32_t(use_fp16) == fp16); |
| 599 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group, base_define + "", default_enabled)); // SHADER_VERSION_COLOR_PASS |
| 600 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group, base_define + "\n#define USE_LIGHTMAP\n", default_enabled)); // SHADER_VERSION_LIGHTMAP_COLOR_PASS |
| 601 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group, base_define + "\n#define MODE_RENDER_DEPTH\n#define SHADOW_PASS\n", default_enabled)); // SHADER_VERSION_SHADOW_PASS, should probably change this to MODE_RENDER_SHADOW because we don't have a depth pass here... |
| 602 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group, base_define + "\n#define MODE_RENDER_DEPTH\n#define MODE_DUAL_PARABOLOID\n#define SHADOW_PASS\n", default_enabled)); // SHADER_VERSION_SHADOW_PASS_DP |
| 603 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group, base_define + "\n#define MODE_RENDER_DEPTH\n#define MODE_RENDER_MATERIAL\n", default_enabled)); // SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL |
| 604 | |
| 605 | // Multiview versions of our shaders. |
| 606 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group_multiview, base_define + "\n#define USE_MULTIVIEW\n", false)); // SHADER_VERSION_COLOR_PASS_MULTIVIEW |
| 607 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group_multiview, base_define + "\n#define USE_MULTIVIEW\n#define USE_LIGHTMAP\n", false)); // SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW |
| 608 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group_multiview, base_define + "\n#define USE_MULTIVIEW\n#define MODE_RENDER_DEPTH\n#define SHADOW_PASS\n", false)); // SHADER_VERSION_SHADOW_PASS_MULTIVIEW |
| 609 | shader_versions.push_back(ShaderRD::VariantDefine(shader_group_multiview, base_define + "\n#define USE_MULTIVIEW\n#define MODE_RENDER_MOTION_VECTORS\n", false)); // SHADER_VERSION_MOTION_VECTORS_MULTIVIEW |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | Vector<RD::PipelineImmutableSampler> immutable_samplers; |
| 614 | RD::PipelineImmutableSampler immutable_shadow_sampler; |
| 615 | immutable_shadow_sampler.binding = 2; |
| 616 | immutable_shadow_sampler.append_id(shadow_sampler); |
| 617 | immutable_shadow_sampler.uniform_type = RenderingDeviceCommons::UNIFORM_TYPE_SAMPLER; |
| 618 | immutable_samplers.push_back(immutable_shadow_sampler); |
| 619 | Vector<uint64_t> dynamic_buffers; |
| 620 | dynamic_buffers.push_back(ShaderRD::DynamicBuffer::encode(RenderForwardMobile::RENDER_PASS_UNIFORM_SET, 0)); |
| 621 | dynamic_buffers.push_back(ShaderRD::DynamicBuffer::encode(RenderForwardMobile::RENDER_PASS_UNIFORM_SET, 1)); |
| 622 | shader.initialize(shader_versions, p_defines, immutable_samplers, dynamic_buffers); |
| 623 | |
| 624 | if (RendererCompositorRD::get_singleton()->is_xr_enabled()) { |
| 625 | enable_multiview_shader_group(); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | material_storage->shader_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_3D, _create_shader_funcs); |
| 630 | material_storage->material_set_data_request_function(RendererRD::MaterialStorage::SHADER_TYPE_3D, _create_material_funcs); |
| 631 | |
| 632 | { |
| 633 | //shader compiler |
| 634 | ShaderCompiler::DefaultIdentifierActions actions; |
| 635 | |
| 636 | actions.renames["MODEL_MATRIX"] = "read_model_matrix"; |
| 637 | actions.renames["MODEL_NORMAL_MATRIX"] = "model_normal_matrix"; |
| 638 | actions.renames["VIEW_MATRIX"] = "read_view_matrix"; |
| 639 | actions.renames["INV_VIEW_MATRIX"] = "inv_view_matrix"; |
| 640 | actions.renames["PROJECTION_MATRIX"] = "projection_matrix"; |
| 641 | actions.renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix"; |
| 642 | actions.renames["MODELVIEW_MATRIX"] = "modelview"; |
| 643 | actions.renames["MODELVIEW_NORMAL_MATRIX"] = "modelview_normal"; |
| 644 | actions.renames["MAIN_CAM_INV_VIEW_MATRIX"] = "scene_data.main_cam_inv_view_matrix"; |
| 645 | |
| 646 | actions.renames["VERTEX"] = "vertex"; |
| 647 | actions.renames["NORMAL"] = "normal_highp"; |
| 648 | actions.renames["TANGENT"] = "tangent_highp"; |
| 649 | actions.renames["BINORMAL"] = "binormal_highp"; |
| 650 | actions.renames["POSITION"] = "position"; |
| 651 | actions.renames["UV"] = "uv_interp"; |
| 652 | actions.renames["UV2"] = "uv2_interp"; |
| 653 | actions.renames["COLOR"] = "color_highp"; |
| 654 | actions.renames["POINT_SIZE"] = "point_size"; |
| 655 | actions.renames["INSTANCE_ID"] = "INSTANCE_INDEX"; |
| 656 | actions.renames["VERTEX_ID"] = "VERTEX_INDEX"; |
| 657 | actions.renames["Z_CLIP_SCALE"] = "z_clip_scale"; |
| 658 | |
| 659 | actions.renames["ALPHA_SCISSOR_THRESHOLD"] = "alpha_scissor_threshold_highp"; |
| 660 | actions.renames["ALPHA_HASH_SCALE"] = "alpha_hash_scale_highp"; |
| 661 | actions.renames["ALPHA_ANTIALIASING_EDGE"] = "alpha_antialiasing_edge_highp"; |
| 662 | actions.renames["ALPHA_TEXTURE_COORDINATE"] = "alpha_texture_coordinate"; |
| 663 | |
| 664 | //builtins |
| 665 | |
| 666 | actions.renames["TIME"] = "scene_data_block.data.time"; |
| 667 | actions.renames["EXPOSURE"] = "(1.0 / scene_data_block.data.emissive_exposure_normalization)"; |
| 668 | actions.renames["PI"] = String::num(Math::PI); |
| 669 | actions.renames["TAU"] = String::num(Math::TAU); |
| 670 | actions.renames["E"] = String::num(Math::E); |
| 671 | actions.renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB"; |
| 672 | actions.renames["CLIP_SPACE_FAR"] = "SHADER_SPACE_FAR"; |
| 673 | actions.renames["IN_SHADOW_PASS"] = "IN_SHADOW_PASS"; |
| 674 | actions.renames["VIEWPORT_SIZE"] = "read_viewport_size"; |
| 675 | |
| 676 | actions.renames["FRAGCOORD"] = "gl_FragCoord"; |
| 677 | actions.renames["FRONT_FACING"] = "gl_FrontFacing"; |
| 678 | actions.renames["NORMAL_MAP"] = "normal_map_highp"; |
| 679 | actions.renames["NORMAL_MAP_DEPTH"] = "normal_map_depth_highp"; |
| 680 | actions.renames["BENT_NORMAL_MAP"] = "bent_normal_map_highp"; |
| 681 | actions.renames["ALBEDO"] = "albedo_highp"; |
| 682 | actions.renames["ALPHA"] = "alpha_highp"; |
| 683 | actions.renames["PREMUL_ALPHA_FACTOR"] = "premul_alpha_highp"; |
| 684 | actions.renames["METALLIC"] = "metallic_highp"; |
| 685 | actions.renames["SPECULAR"] = "specular_highp"; |
| 686 | actions.renames["ROUGHNESS"] = "roughness_highp"; |
| 687 | actions.renames["RIM"] = "rim_highp"; |
| 688 | actions.renames["RIM_TINT"] = "rim_tint_highp"; |
| 689 | actions.renames["CLEARCOAT"] = "clearcoat_highp"; |
| 690 | actions.renames["CLEARCOAT_ROUGHNESS"] = "clearcoat_roughness_highp"; |
| 691 | actions.renames["ANISOTROPY"] = "anisotropy_highp"; |
| 692 | actions.renames["ANISOTROPY_FLOW"] = "anisotropy_flow_highp"; |
| 693 | actions.renames["SSS_STRENGTH"] = "sss_strength_highp"; |
| 694 | actions.renames["SSS_TRANSMITTANCE_COLOR"] = "transmittance_color_highp"; |
| 695 | actions.renames["SSS_TRANSMITTANCE_DEPTH"] = "transmittance_depth_highp"; |
| 696 | actions.renames["SSS_TRANSMITTANCE_BOOST"] = "transmittance_boost_highp"; |
| 697 | actions.renames["BACKLIGHT"] = "backlight_highp"; |
| 698 | actions.renames["AO"] = "ao_highp"; |
| 699 | actions.renames["AO_LIGHT_AFFECT"] = "ao_light_affect_highp"; |
| 700 | actions.renames["EMISSION"] = "emission_highp"; |
| 701 | actions.renames["POINT_COORD"] = "point_coord"; |
| 702 | actions.renames["INSTANCE_CUSTOM"] = "instance_custom"; |
| 703 | actions.renames["SCREEN_UV"] = "screen_uv"; |
| 704 | actions.renames["DEPTH"] = "gl_FragDepth"; |
| 705 | actions.renames["FOG"] = "fog_highp"; |
| 706 | actions.renames["RADIANCE"] = "custom_radiance_highp"; |
| 707 | actions.renames["IRRADIANCE"] = "custom_irradiance_highp"; |
| 708 | actions.renames["BONE_INDICES"] = "bone_attrib"; |
| 709 | actions.renames["BONE_WEIGHTS"] = "weight_attrib"; |
| 710 | actions.renames["CUSTOM0"] = "custom0_attrib"; |
| 711 | actions.renames["CUSTOM1"] = "custom1_attrib"; |
| 712 | actions.renames["CUSTOM2"] = "custom2_attrib"; |
| 713 | actions.renames["CUSTOM3"] = "custom3_attrib"; |
| 714 | actions.renames["LIGHT_VERTEX"] = "light_vertex"; |
| 715 | |
| 716 | actions.renames["NODE_POSITION_WORLD"] = "read_model_matrix[3].xyz"; |
| 717 | actions.renames["CAMERA_POSITION_WORLD"] = "inv_view_matrix[3].xyz"; |
| 718 | actions.renames["CAMERA_DIRECTION_WORLD"] = "inv_view_matrix[2].xyz"; |
| 719 | actions.renames["CAMERA_VISIBLE_LAYERS"] = "scene_data.camera_visible_layers"; |
| 720 | actions.renames["NODE_POSITION_VIEW"] = "(read_view_matrix * read_model_matrix)[3].xyz"; |
| 721 | |
| 722 | actions.renames["IS_MULTIVIEW"] = "OUTPUT_IS_MULTIVIEW"; |
| 723 | actions.renames["VIEW_INDEX"] = "ViewIndex"; |
| 724 | actions.renames["VIEW_MONO_LEFT"] = "0"; |
| 725 | actions.renames["VIEW_RIGHT"] = "1"; |
| 726 | actions.renames["EYE_OFFSET"] = "eye_offset"; |
| 727 | |
| 728 | //for light |
| 729 | actions.renames["VIEW"] = "view_highp"; |
| 730 | actions.renames["SPECULAR_AMOUNT"] = "specular_amount_highp"; |
| 731 | actions.renames["LIGHT_COLOR"] = "light_color_highp"; |
| 732 | actions.renames["LIGHT_IS_DIRECTIONAL"] = "is_directional"; |
| 733 | actions.renames["LIGHT"] = "light_highp"; |
| 734 | actions.renames["ATTENUATION"] = "attenuation_highp"; |
| 735 | actions.renames["DIFFUSE_LIGHT"] = "diffuse_light_highp"; |
| 736 | actions.renames["SPECULAR_LIGHT"] = "specular_light_highp"; |
| 737 | |
| 738 | actions.usage_defines["NORMAL"] = "#define NORMAL_USED\n"; |
| 739 | actions.usage_defines["TANGENT"] = "#define TANGENT_USED\n"; |
| 740 | actions.usage_defines["BINORMAL"] = "@TANGENT"; |
| 741 | actions.usage_defines["RIM"] = "#define LIGHT_RIM_USED\n"; |
| 742 | actions.usage_defines["RIM_TINT"] = "@RIM"; |
| 743 | actions.usage_defines["CLEARCOAT"] = "#define LIGHT_CLEARCOAT_USED\n"; |
| 744 | actions.usage_defines["CLEARCOAT_ROUGHNESS"] = "@CLEARCOAT"; |
| 745 | actions.usage_defines["ANISOTROPY"] = "#define LIGHT_ANISOTROPY_USED\n"; |
| 746 | actions.usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY"; |
| 747 | actions.usage_defines["AO"] = "#define AO_USED\n"; |
| 748 | actions.usage_defines["AO_LIGHT_AFFECT"] = "#define AO_USED\n"; |
| 749 | actions.usage_defines["UV"] = "#define UV_USED\n"; |
| 750 | actions.usage_defines["UV2"] = "#define UV2_USED\n"; |
| 751 | actions.usage_defines["BONE_INDICES"] = "#define BONES_USED\n"; |
| 752 | actions.usage_defines["BONE_WEIGHTS"] = "#define WEIGHTS_USED\n"; |
| 753 | actions.usage_defines["CUSTOM0"] = "#define CUSTOM0_USED\n"; |
| 754 | actions.usage_defines["CUSTOM1"] = "#define CUSTOM1_USED\n"; |
| 755 | actions.usage_defines["CUSTOM2"] = "#define CUSTOM2_USED\n"; |
| 756 | actions.usage_defines["CUSTOM3"] = "#define CUSTOM3_USED\n"; |
| 757 | actions.usage_defines["NORMAL_MAP"] = "#define NORMAL_MAP_USED\n"; |
| 758 | actions.usage_defines["NORMAL_MAP_DEPTH"] = "@NORMAL_MAP"; |
| 759 | actions.usage_defines["BENT_NORMAL_MAP"] = "#define BENT_NORMAL_MAP_USED\n"; |
| 760 | actions.usage_defines["COLOR"] = "#define COLOR_USED\n"; |
| 761 | actions.usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n"; |
| 762 | actions.usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n"; |
| 763 | actions.usage_defines["LIGHT_VERTEX"] = "#define LIGHT_VERTEX_USED\n"; |
| 764 | actions.usage_defines["Z_CLIP_SCALE"] = "#define Z_CLIP_SCALE_USED\n"; |
| 765 | |
| 766 | actions.usage_defines["ALPHA_SCISSOR_THRESHOLD"] = "#define ALPHA_SCISSOR_USED\n"; |
| 767 | actions.usage_defines["ALPHA_HASH_SCALE"] = "#define ALPHA_HASH_USED\n"; |
| 768 | actions.usage_defines["ALPHA_ANTIALIASING_EDGE"] = "#define ALPHA_ANTIALIASING_EDGE_USED\n"; |
| 769 | actions.usage_defines["ALPHA_TEXTURE_COORDINATE"] = "@ALPHA_ANTIALIASING_EDGE"; |
| 770 | actions.usage_defines["PREMUL_ALPHA_FACTOR"] = "#define PREMUL_ALPHA_USED"; |
| 771 | |
| 772 | actions.usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n"; |
| 773 | actions.usage_defines["SSS_TRANSMITTANCE_DEPTH"] = "#define ENABLE_TRANSMITTANCE\n"; |
| 774 | actions.usage_defines["BACKLIGHT"] = "#define LIGHT_BACKLIGHT_USED\n"; |
| 775 | actions.usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n"; |
| 776 | |
| 777 | actions.usage_defines["FOG"] = "#define CUSTOM_FOG_USED\n"; |
| 778 | actions.usage_defines["RADIANCE"] = "#define CUSTOM_RADIANCE_USED\n"; |
| 779 | actions.usage_defines["IRRADIANCE"] = "#define CUSTOM_IRRADIANCE_USED\n"; |
| 780 | |
| 781 | actions.usage_defines["MODEL_MATRIX"] = "#define MODEL_MATRIX_USED\n"; |
| 782 | |
| 783 | actions.usage_defines["POINT_SIZE"] = "#define POINT_SIZE_USED\n"; |
| 784 | actions.usage_defines["POINT_COORD"] = "#define POINT_COORD_USED\n"; |
| 785 | |
| 786 | actions.render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n"; |
| 787 | actions.render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n"; |
| 788 | actions.render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n"; |
| 789 | actions.render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n"; |
| 790 | actions.render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n"; |
| 791 | actions.render_mode_defines["particle_trails"] = "#define USE_PARTICLE_TRAILS\n"; |
| 792 | actions.render_mode_defines["depth_prepass_alpha"] = "#define USE_OPAQUE_PREPASS\n"; |
| 793 | |
| 794 | bool force_lambert = GLOBAL_GET("rendering/shading/overrides/force_lambert_over_burley"); |
| 795 | if (!force_lambert) { |
| 796 | actions.render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n"; |
| 797 | } |
| 798 | |
| 799 | actions.render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n"; |
| 800 | actions.render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n"; |
| 801 | |
| 802 | actions.render_mode_defines["sss_mode_skin"] = "#define SSS_MODE_SKIN\n"; |
| 803 | |
| 804 | actions.render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n"; |
| 805 | |
| 806 | actions.render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n"; |
| 807 | actions.render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n"; |
| 808 | actions.render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n"; |
| 809 | actions.render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n"; |
| 810 | actions.render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n"; |
| 811 | actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n"; |
| 812 | |
| 813 | bool force_vertex_shading = GLOBAL_GET("rendering/shading/overrides/force_vertex_shading"); |
| 814 | if (!force_vertex_shading) { |
| 815 | // If forcing vertex shading, this will be defined already. |
| 816 | actions.render_mode_defines["vertex_lighting"] = "#define USE_VERTEX_LIGHTING\n"; |
| 817 | } |
| 818 | |
| 819 | actions.render_mode_defines["debug_shadow_splits"] = "#define DEBUG_DRAW_PSSM_SPLITS\n"; |
| 820 | actions.render_mode_defines["fog_disabled"] = "#define FOG_DISABLED\n"; |
| 821 | |
| 822 | actions.render_mode_defines["specular_occlusion_disabled"] = "#define SPECULAR_OCCLUSION_DISABLED\n"; |
| 823 | |
| 824 | actions.base_texture_binding_index = 1; |
| 825 | actions.texture_layout_set = RenderForwardMobile::MATERIAL_UNIFORM_SET; |
| 826 | actions.base_uniform_string = "material."; |
| 827 | actions.base_varying_index = 15; |
| 828 | |
| 829 | actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP; |
| 830 | actions.default_repeat = ShaderLanguage::REPEAT_ENABLE; |
| 831 | actions.global_buffer_array_variable = "global_shader_uniforms.data"; |
| 832 | actions.instance_uniform_index_variable = "instances.data[draw_call.instance_index].instance_uniforms_ofs"; |
| 833 | |
| 834 | actions.apply_luminance_multiplier = true; // apply luminance multiplier to screen texture |
| 835 | actions.check_multiview_samplers = true; |
| 836 | |
| 837 | compiler.initialize(actions); |
| 838 | } |
| 839 | |
| 840 | { |
| 841 | //default material and shader |
| 842 | default_shader = material_storage->shader_allocate(); |
| 843 | material_storage->shader_initialize(default_shader); |
| 844 | material_storage->shader_set_code(default_shader, R"( |
| 845 | // Default 3D material shader (Mobile). |
| 846 | |
| 847 | shader_type spatial; |
| 848 | |
| 849 | void vertex() { |
| 850 | ROUGHNESS = 0.8; |
| 851 | } |
| 852 | |
| 853 | void fragment() { |
| 854 | ALBEDO = vec3(0.6); |
| 855 | ROUGHNESS = 0.8; |
| 856 | METALLIC = 0.2; |
| 857 | } |
| 858 | )"); |
| 859 | default_material = material_storage->material_allocate(); |
| 860 | material_storage->material_initialize(default_material); |
| 861 | material_storage->material_set_shader(default_material, default_shader); |
| 862 | |
| 863 | MaterialData *md = static_cast<MaterialData *>(material_storage->material_get_data(default_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 864 | default_shader_rd = shader.version_get_shader(md->shader_data->version, (use_fp16 ? SHADER_VERSION_MAX * 2 : 0) + SHADER_VERSION_COLOR_PASS); |
| 865 | |
| 866 | default_material_shader_ptr = md->shader_data; |
| 867 | default_material_uniform_set = md->uniform_set; |
| 868 | } |
| 869 | |
| 870 | { |
| 871 | overdraw_material_shader = material_storage->shader_allocate(); |
| 872 | material_storage->shader_initialize(overdraw_material_shader); |
| 873 | // Use relatively low opacity so that more "layers" of overlapping objects can be distinguished. |
| 874 | material_storage->shader_set_code(overdraw_material_shader, R"( |
| 875 | // 3D editor Overdraw debug draw mode shader (Mobile). |
| 876 | |
| 877 | shader_type spatial; |
| 878 | |
| 879 | render_mode blend_add, unshaded, fog_disabled; |
| 880 | |
| 881 | void fragment() { |
| 882 | ALBEDO = vec3(0.4, 0.8, 0.8); |
| 883 | ALPHA = 0.1; |
| 884 | } |
| 885 | )"); |
| 886 | overdraw_material = material_storage->material_allocate(); |
| 887 | material_storage->material_initialize(overdraw_material); |
| 888 | material_storage->material_set_shader(overdraw_material, overdraw_material_shader); |
| 889 | |
| 890 | MaterialData *md = static_cast<MaterialData *>(material_storage->material_get_data(overdraw_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 891 | overdraw_material_shader_ptr = md->shader_data; |
| 892 | overdraw_material_uniform_set = md->uniform_set; |
| 893 | } |
| 894 | |
| 895 | { |
| 896 | debug_shadow_splits_material_shader = material_storage->shader_allocate(); |
| 897 | material_storage->shader_initialize(debug_shadow_splits_material_shader); |
| 898 | // Use relatively low opacity so that more "layers" of overlapping objects can be distinguished. |
| 899 | material_storage->shader_set_code(debug_shadow_splits_material_shader, R"( |
| 900 | // 3D debug shadow splits mode shader (Mobile). |
| 901 | |
| 902 | shader_type spatial; |
| 903 | |
| 904 | render_mode debug_shadow_splits, fog_disabled; |
| 905 | |
| 906 | void fragment() { |
| 907 | ALBEDO = vec3(1.0, 1.0, 1.0); |
| 908 | } |
| 909 | )"); |
| 910 | debug_shadow_splits_material = material_storage->material_allocate(); |
| 911 | material_storage->material_initialize(debug_shadow_splits_material); |
| 912 | material_storage->material_set_shader(debug_shadow_splits_material, debug_shadow_splits_material_shader); |
| 913 | |
| 914 | MaterialData *md = static_cast<MaterialData *>(material_storage->material_get_data(debug_shadow_splits_material, RendererRD::MaterialStorage::SHADER_TYPE_3D)); |
| 915 | debug_shadow_splits_material_shader_ptr = md->shader_data; |
| 916 | debug_shadow_splits_material_uniform_set = md->uniform_set; |
| 917 | } |
| 918 | |
| 919 | { |
| 920 | default_vec4_xform_buffer = RD::get_singleton()->storage_buffer_create(256); |
| 921 | Vector<RD::Uniform> uniforms; |
| 922 | RD::Uniform u; |
| 923 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 924 | u.append_id(default_vec4_xform_buffer); |
| 925 | u.binding = 0; |
| 926 | uniforms.push_back(u); |
| 927 | |
| 928 | default_vec4_xform_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, default_shader_rd, RenderForwardMobile::TRANSFORMS_UNIFORM_SET); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | void SceneShaderForwardMobile::set_default_specialization(const ShaderSpecialization &p_specialization) { |
| 933 | default_specialization = p_specialization; |
| 934 | |
| 935 | for (SelfList<ShaderData> *E = shader_list.first(); E; E = E->next()) { |
| 936 | E->self()->pipeline_hash_map.clear_pipelines(); |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | uint32_t SceneShaderForwardMobile::get_pipeline_compilations(RS::PipelineSource p_source) { |
| 941 | MutexLock lock(SceneShaderForwardMobile::singleton_mutex); |
| 942 | return pipeline_compilations[p_source]; |
| 943 | } |
| 944 | |
| 945 | void SceneShaderForwardMobile::enable_fp32_shader_group() { |
| 946 | shader.enable_group(SHADER_GROUP_FP32); |
| 947 | |
| 948 | if (is_multiview_shader_group_enabled()) { |
| 949 | enable_multiview_shader_group(); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | void SceneShaderForwardMobile::enable_fp16_shader_group() { |
| 954 | shader.enable_group(SHADER_GROUP_FP16); |
| 955 | |
| 956 | if (is_multiview_shader_group_enabled()) { |
| 957 | enable_multiview_shader_group(); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | void SceneShaderForwardMobile::enable_multiview_shader_group() { |
| 962 | if (shader.is_group_enabled(SHADER_GROUP_FP32)) { |
| 963 | shader.enable_group(SHADER_GROUP_FP32_MULTIVIEW); |
| 964 | } |
| 965 | |
| 966 | if (shader.is_group_enabled(SHADER_GROUP_FP16)) { |
| 967 | shader.enable_group(SHADER_GROUP_FP16_MULTIVIEW); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | bool SceneShaderForwardMobile::is_multiview_shader_group_enabled() const { |
| 972 | return shader.is_group_enabled(SHADER_GROUP_FP32_MULTIVIEW) || shader.is_group_enabled(SHADER_GROUP_FP16_MULTIVIEW); |
| 973 | } |
| 974 | |
| 975 | SceneShaderForwardMobile::~SceneShaderForwardMobile() { |
| 976 | RendererRD::MaterialStorage *material_storage = RendererRD::MaterialStorage::get_singleton(); |
| 977 | |
| 978 | RD::get_singleton()->free_rid(default_vec4_xform_buffer); |
| 979 | RD::get_singleton()->free_rid(shadow_sampler); |
| 980 | |
| 981 | material_storage->shader_free(overdraw_material_shader); |
| 982 | material_storage->shader_free(default_shader); |
| 983 | material_storage->shader_free(debug_shadow_splits_material_shader); |
| 984 | |
| 985 | material_storage->material_free(overdraw_material); |
| 986 | material_storage->material_free(default_material); |
| 987 | material_storage->material_free(debug_shadow_splits_material); |
| 988 | } |