Debian Codegraph @

servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.h

raw symbols SCIP
1 /**************************************************************************/
2 /* scene_shader_forward_mobile.h */
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 #pragma once
32
33 #include "../storage_rd/material_storage.h"
34 #include "servers/rendering/renderer_rd/pipeline_hash_map_rd.h"
35 #include "servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl.gen.h"
36
37 namespace RendererSceneRenderImplementation {
38
39 class SceneShaderForwardMobile {
40 private:
41 static SceneShaderForwardMobile *singleton;
42 static Mutex singleton_mutex;
43
44 public:
45 enum ShaderVersion {
46 SHADER_VERSION_COLOR_PASS,
47 SHADER_VERSION_LIGHTMAP_COLOR_PASS,
48 SHADER_VERSION_SHADOW_PASS,
49 SHADER_VERSION_SHADOW_PASS_DP,
50 SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL,
51
52 SHADER_VERSION_COLOR_PASS_MULTIVIEW,
53 SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW,
54 SHADER_VERSION_SHADOW_PASS_MULTIVIEW,
55 SHADER_VERSION_MOTION_VECTORS_MULTIVIEW,
56
57 SHADER_VERSION_MAX
58 };
59
60 enum ShaderCount {
61 SHADER_COUNT_NONE,
62 SHADER_COUNT_SINGLE,
63 SHADER_COUNT_MULTIPLE
64 };
65
66 _FORCE_INLINE_ static ShaderCount shader_count_for(uint32_t p_count) {
67 if (p_count == 0) {
68 return SHADER_COUNT_NONE;
69 } else if (p_count == 1) {
70 return SHADER_COUNT_SINGLE;
71 } else {
72 return SHADER_COUNT_MULTIPLE;
73 }
74 }
75
76 enum ShaderGroup {
77 SHADER_GROUP_FP32,
78 SHADER_GROUP_FP32_MULTIVIEW,
79 SHADER_GROUP_FP16,
80 SHADER_GROUP_FP16_MULTIVIEW,
81 };
82
83 struct ShaderSpecialization {
84 union {
85 uint32_t packed_0;
86
87 struct {
88 uint32_t use_light_projector : 1;
89 uint32_t use_light_soft_shadows : 1;
90 uint32_t use_directional_soft_shadows : 1;
91 uint32_t decal_use_mipmaps : 1;
92
93 uint32_t projector_use_mipmaps : 1;
94 uint32_t disable_fog : 1;
95 uint32_t use_depth_fog : 1;
96 uint32_t use_fog_aerial_perspective : 1;
97
98 uint32_t use_fog_sun_scatter : 1;
99 uint32_t use_fog_height_density : 1;
100 uint32_t use_lightmap_bicubic_filter : 1;
101 uint32_t use_material_debanding : 1;
102
103 uint32_t multimesh : 1;
104 uint32_t multimesh_format_2d : 1;
105 uint32_t multimesh_has_color : 1;
106 uint32_t multimesh_has_custom_data : 1;
107
108 uint32_t scene_use_ambient_cubemap : 1;
109 uint32_t scene_use_reflection_cubemap : 1;
110 uint32_t scene_roughness_limiter_enabled : 1;
111 uint32_t padding_0 : 1;
112
113 uint32_t soft_shadow_samples : 6;
114 uint32_t penumbra_shadow_samples : 6;
115 };
116 };
117
118 union {
119 uint32_t packed_1;
120
121 struct {
122 uint32_t directional_soft_shadow_samples : 6;
123 uint32_t directional_penumbra_shadow_samples : 6;
124 uint32_t omni_lights : 2;
125 uint32_t spot_lights : 2;
126 uint32_t reflection_probes : 2;
127 uint32_t directional_lights : 2;
128 uint32_t decals : 1;
129 uint32_t directional_light_blend_splits : 8;
130 uint32_t padding_1 : 3;
131 };
132 };
133
134 union {
135 float packed_2;
136 float luminance_multiplier;
137 };
138 };
139
140 struct UbershaderConstants {
141 union {
142 uint32_t packed_0;
143
144 struct {
145 uint32_t cull_mode : 2;
146 };
147 };
148 };
149
150 struct ShaderData : public RendererRD::MaterialStorage::ShaderData {
151 enum DepthDraw {
152 DEPTH_DRAW_DISABLED,
153 DEPTH_DRAW_OPAQUE,
154 DEPTH_DRAW_ALWAYS
155 };
156
157 enum DepthTest {
158 DEPTH_TEST_DISABLED,
159 DEPTH_TEST_ENABLED,
160 DEPTH_TEST_ENABLED_INVERTED,
161 };
162
163 enum CullVariant {
164 CULL_VARIANT_NORMAL,
165 CULL_VARIANT_REVERSED,
166 CULL_VARIANT_DOUBLE_SIDED,
167 CULL_VARIANT_MAX
168
169 };
170
171 enum AlphaAntiAliasing {
172 ALPHA_ANTIALIASING_OFF,
173 ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE,
174 ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE
175 };
176
177 enum StencilFlags {
178 STENCIL_FLAG_READ = 1,
179 STENCIL_FLAG_WRITE = 2,
180 STENCIL_FLAG_WRITE_DEPTH_FAIL = 4,
181 };
182
183 enum StencilCompare {
184 STENCIL_COMPARE_LESS,
185 STENCIL_COMPARE_EQUAL,
186 STENCIL_COMPARE_LESS_OR_EQUAL,
187 STENCIL_COMPARE_GREATER,
188 STENCIL_COMPARE_NOT_EQUAL,
189 STENCIL_COMPARE_GREATER_OR_EQUAL,
190 STENCIL_COMPARE_ALWAYS,
191 STENCIL_COMPARE_MAX // Not an actual operator, just the amount of operators.
192 };
193
194 struct PipelineKey {
195 RD::VertexFormatID vertex_format_id;
196 RD::FramebufferFormatID framebuffer_format_id;
197 RD::PolygonCullMode cull_mode = RD::POLYGON_CULL_MAX;
198 RS::PrimitiveType primitive_type = RS::PRIMITIVE_MAX;
199 ShaderSpecialization shader_specialization = {};
200 ShaderVersion version = SHADER_VERSION_MAX;
201 uint32_t render_pass = 0;
202 uint32_t wireframe = false;
203 uint32_t ubershader = false;
204
205 uint32_t hash() const {
206 uint32_t h = hash_murmur3_one_32(vertex_format_id);
207 h = hash_murmur3_one_32(framebuffer_format_id, h);
208 h = hash_murmur3_one_32(cull_mode, h);
209 h = hash_murmur3_one_32(primitive_type, h);
210 h = hash_murmur3_one_32(shader_specialization.packed_0, h);
211 h = hash_murmur3_one_32(shader_specialization.packed_1, h);
212 h = hash_murmur3_one_float(shader_specialization.packed_2, h);
213 h = hash_murmur3_one_32(version, h);
214 h = hash_murmur3_one_32(render_pass, h);
215 h = hash_murmur3_one_32(wireframe, h);
216 h = hash_murmur3_one_32(ubershader, h);
217 return hash_fmix32(h);
218 }
219 };
220
221 void _create_pipeline(PipelineKey p_pipeline_key);
222 PipelineHashMapRD<PipelineKey, ShaderData, void (ShaderData::*)(PipelineKey)> pipeline_hash_map;
223
224 RID version;
225
226 static const uint32_t VERTEX_INPUT_MASKS_SIZE = SHADER_VERSION_MAX * 2;
227 std::atomic<uint64_t> vertex_input_masks[VERTEX_INPUT_MASKS_SIZE] = {};
228
229 Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
230
231 Vector<uint32_t> ubo_offsets;
232 uint32_t ubo_size = 0;
233
234 String code;
235
236 DepthDraw depth_draw;
237 DepthTest depth_test;
238
239 int blend_mode = BLEND_MODE_MIX;
240 int depth_test_disabledi = 0;
241 int depth_test_invertedi = 0;
242 int alpha_antialiasing_mode = ALPHA_ANTIALIASING_OFF;
243 int cull_mode = RS::CULL_MODE_BACK;
244
245 bool uses_point_size = false;
246 bool uses_alpha = false;
247 bool uses_blend_alpha = false;
248 bool uses_alpha_clip = false;
249 bool uses_alpha_antialiasing = false;
250 bool uses_depth_prepass_alpha = false;
251 bool uses_discard = false;
252 bool uses_roughness = false;
253 bool uses_normal = false;
254 bool uses_tangent = false;
255 bool uses_particle_trails = false;
256 bool uses_normal_map = false;
257 bool uses_bent_normal_map = false;
258 bool wireframe = false;
259
260 bool unshaded = false;
261 bool uses_vertex = false;
262 bool uses_sss = false;
263 bool uses_transmittance = false;
264 bool uses_screen_texture = false;
265 bool uses_depth_texture = false;
266 bool uses_normal_texture = false;
267 bool uses_screen_texture_mipmaps = false;
268 bool uses_time = false;
269 bool uses_vertex_time = false;
270 bool uses_fragment_time = false;
271 bool writes_modelview_or_projection = false;
272 bool uses_world_coordinates = false;
273
274 bool stencil_enabled = false;
275 uint32_t stencil_flags = 0;
276 StencilCompare stencil_compare = STENCIL_COMPARE_LESS;
277 uint32_t stencil_reference = 0;
278
279 uint64_t last_pass = 0;
280 uint32_t index = 0;
281
282 _FORCE_INLINE_ bool uses_alpha_pass() const {
283 bool has_read_screen_alpha = uses_screen_texture || uses_depth_texture || uses_normal_texture;
284 bool has_base_alpha = (uses_alpha && (!uses_alpha_clip || uses_alpha_antialiasing));
285 bool has_blend_alpha = uses_blend_alpha;
286 bool has_alpha = has_base_alpha || has_blend_alpha;
287 bool no_depth_draw = depth_draw == DEPTH_DRAW_DISABLED;
288 bool no_depth_test = depth_test != DEPTH_TEST_ENABLED;
289 return has_alpha || has_read_screen_alpha || no_depth_draw || no_depth_test;
290 }
291
292 _FORCE_INLINE_ bool uses_depth_in_alpha_pass() const {
293 bool no_depth_draw = depth_draw == DEPTH_DRAW_DISABLED;
294 bool no_depth_test = depth_test != DEPTH_TEST_ENABLED;
295 return (uses_depth_prepass_alpha || uses_alpha_antialiasing) && !(no_depth_draw || no_depth_test);
296 }
297
298 _FORCE_INLINE_ bool uses_shared_shadow_material() const {
299 bool backface_culling = cull_mode == RS::CULL_MODE_BACK;
300 return !uses_particle_trails && !writes_modelview_or_projection && !uses_vertex && !uses_discard && !uses_depth_prepass_alpha && !uses_alpha_clip && !uses_alpha_antialiasing && !uses_point_size && !uses_world_coordinates && !wireframe && !stencil_enabled && backface_culling;
301 }
302
303 virtual void set_code(const String &p_Code);
304 virtual bool is_animated() const;
305 virtual bool casts_shadows() const;
306 virtual RS::ShaderNativeSourceCode get_native_source_code() const;
307 virtual Pair<ShaderRD *, RID> get_native_shader_and_version() const;
308 RD::PolygonCullMode get_cull_mode_from_cull_variant(CullVariant p_cull_variant);
309 void _clear_vertex_input_mask_cache();
310 RID get_shader_variant(ShaderVersion p_shader_version, bool p_ubershader) const;
311 uint64_t get_vertex_input_mask(ShaderVersion p_shader_version, bool p_ubershader);
312 bool is_valid() const;
313
314 SelfList<ShaderData> shader_list_element;
315
316 ShaderData();
317 virtual ~ShaderData();
318 };
319
320 RendererRD::MaterialStorage::ShaderData *_create_shader_func();
321 static RendererRD::MaterialStorage::ShaderData *_create_shader_funcs() {
322 return static_cast<SceneShaderForwardMobile *>(singleton)->_create_shader_func();
323 }
324
325 struct MaterialData : public RendererRD::MaterialStorage::MaterialData {
326 ShaderData *shader_data = nullptr;
327 RID uniform_set;
328 uint64_t last_pass = 0;
329 uint32_t index = 0;
330 RID next_pass;
331 uint8_t priority;
332 virtual void set_render_priority(int p_priority);
333 virtual void set_next_pass(RID p_pass);
334 virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
335 virtual ~MaterialData();
336 };
337
338 SelfList<ShaderData>::List shader_list;
339
340 RendererRD::MaterialStorage::MaterialData *_create_material_func(ShaderData *p_shader);
341 static RendererRD::MaterialStorage::MaterialData *_create_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) {
342 return static_cast<SceneShaderForwardMobile *>(singleton)->_create_material_func(static_cast<ShaderData *>(p_shader));
343 }
344
345 SceneForwardMobileShaderRD shader;
346 ShaderCompiler compiler;
347 bool use_fp16 = false;
348 bool emulate_point_size = false;
349
350 RID default_shader;
351 RID default_material;
352 RID overdraw_material_shader;
353 RID overdraw_material;
354 RID debug_shadow_splits_material_shader;
355 RID debug_shadow_splits_material;
356 RID default_shader_rd;
357
358 RID default_vec4_xform_buffer;
359 RID default_vec4_xform_uniform_set;
360
361 RID shadow_sampler;
362
363 RID default_material_uniform_set;
364 ShaderData *default_material_shader_ptr = nullptr;
365
366 RID overdraw_material_uniform_set;
367 ShaderData *overdraw_material_shader_ptr = nullptr;
368
369 RID debug_shadow_splits_material_uniform_set;
370 ShaderData *debug_shadow_splits_material_shader_ptr = nullptr;
371
372 SceneShaderForwardMobile();
373 ~SceneShaderForwardMobile();
374
375 ShaderSpecialization default_specialization = {};
376
377 uint32_t pipeline_compilations[RS::PIPELINE_SOURCE_MAX] = {};
378
379 void init(const String p_defines);
380 void set_default_specialization(const ShaderSpecialization &p_specialization);
381 uint32_t get_pipeline_compilations(RS::PipelineSource p_source);
382 void enable_fp32_shader_group();
383 void enable_fp16_shader_group();
384 void enable_multiview_shader_group();
385 bool is_multiview_shader_group_enabled() const;
386 };
387
388 } // namespace RendererSceneRenderImplementation