Debian Codegraph @

servers/rendering/renderer_rd/forward_clustered/scene_shader_forward_clustered.h

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