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#ifndef SCENE_SHADER_FORWARD_CLUSTERED_H
32#define SCENE_SHADER_FORWARD_CLUSTERED_H
33
34#include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
35#include "servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl.gen.h"
36
37namespace RendererSceneRenderImplementation {
38
39class SceneShaderForwardClustered {
40private:
41 static SceneShaderForwardClustered *singleton;
42
43public:
44 enum ShaderGroup {
45 SHADER_GROUP_BASE, // Always compiled at the beginning.
46 SHADER_GROUP_ADVANCED,
47 SHADER_GROUP_MULTIVIEW,
48 SHADER_GROUP_ADVANCED_MULTIVIEW,
49 };
50
51 enum ShaderVersion {
52 SHADER_VERSION_DEPTH_PASS,
53 SHADER_VERSION_DEPTH_PASS_DP,
54 SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS,
55 SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI,
56 SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL,
57 SHADER_VERSION_DEPTH_PASS_WITH_SDF,
58 SHADER_VERSION_DEPTH_PASS_MULTIVIEW,
59 SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_MULTIVIEW,
60 SHADER_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI_MULTIVIEW,
61 SHADER_VERSION_COLOR_PASS,
62 SHADER_VERSION_MAX
63 };
64
65 enum ShaderColorPassFlags {
66 SHADER_COLOR_PASS_FLAG_SEPARATE_SPECULAR = 1 << 0,
67 SHADER_COLOR_PASS_FLAG_LIGHTMAP = 1 << 1,
68 SHADER_COLOR_PASS_FLAG_MULTIVIEW = 1 << 2,
69 SHADER_COLOR_PASS_FLAG_MOTION_VECTORS = 1 << 3,
70 SHADER_COLOR_PASS_FLAG_COUNT = 1 << 4
71 };
72
73 enum PipelineVersion {
74 PIPELINE_VERSION_DEPTH_PASS,
75 PIPELINE_VERSION_DEPTH_PASS_DP,
76 PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS,
77 PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI,
78 PIPELINE_VERSION_DEPTH_PASS_WITH_MATERIAL,
79 PIPELINE_VERSION_DEPTH_PASS_WITH_SDF,
80 PIPELINE_VERSION_DEPTH_PASS_MULTIVIEW,
81 PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_MULTIVIEW,
82 PIPELINE_VERSION_DEPTH_PASS_WITH_NORMAL_AND_ROUGHNESS_AND_VOXEL_GI_MULTIVIEW,
83 PIPELINE_VERSION_COLOR_PASS,
84 PIPELINE_VERSION_MAX
85 };
86
87 enum PipelineColorPassFlags {
88 PIPELINE_COLOR_PASS_FLAG_TRANSPARENT = 1 << 0, // Can't combine with SEPARATE_SPECULAR.
89 PIPELINE_COLOR_PASS_FLAG_SEPARATE_SPECULAR = 1 << 1, // Can't combine with TRANSPARENT.
90 PIPELINE_COLOR_PASS_FLAG_LIGHTMAP = 1 << 2,
91 PIPELINE_COLOR_PASS_FLAG_MULTIVIEW = 1 << 3,
92 PIPELINE_COLOR_PASS_FLAG_MOTION_VECTORS = 1 << 4,
93 PIPELINE_COLOR_PASS_FLAG_COUNT = 1 << 5,
94 };
95
96 enum ShaderSpecializations {
97 SHADER_SPECIALIZATION_FORWARD_GI = 1 << 0,
98 SHADER_SPECIALIZATION_PROJECTOR = 1 << 1,
99 SHADER_SPECIALIZATION_SOFT_SHADOWS = 1 << 2,
100 SHADER_SPECIALIZATION_DIRECTIONAL_SOFT_SHADOWS = 1 << 3,
101 };
102
103 struct ShaderData : public RendererRD::MaterialStorage::ShaderData {
104 enum BlendMode { //used internally
105 BLEND_MODE_MIX,
106 BLEND_MODE_ADD,
107 BLEND_MODE_SUB,
108 BLEND_MODE_MUL,
109 BLEND_MODE_ALPHA_TO_COVERAGE
110 };
111
112 enum DepthDraw {
113 DEPTH_DRAW_DISABLED,
114 DEPTH_DRAW_OPAQUE,
115 DEPTH_DRAW_ALWAYS
116 };
117
118 enum DepthTest {
119 DEPTH_TEST_DISABLED,
120 DEPTH_TEST_ENABLED
121 };
122
123 enum Cull {
124 CULL_DISABLED,
125 CULL_FRONT,
126 CULL_BACK
127 };
128
129 enum CullVariant {
130 CULL_VARIANT_NORMAL,
131 CULL_VARIANT_REVERSED,
132 CULL_VARIANT_DOUBLE_SIDED,
133 CULL_VARIANT_MAX
134
135 };
136
137 enum AlphaAntiAliasing {
138 ALPHA_ANTIALIASING_OFF,
139 ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE,
140 ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE
141 };
142
143 bool valid = false;
144 RID version;
145 uint32_t vertex_input_mask = 0;
146 PipelineCacheRD pipelines[CULL_VARIANT_MAX][RS::PRIMITIVE_MAX][PIPELINE_VERSION_MAX];
147 PipelineCacheRD color_pipelines[CULL_VARIANT_MAX][RS::PRIMITIVE_MAX][PIPELINE_COLOR_PASS_FLAG_COUNT];
148
149 Vector<ShaderCompiler::GeneratedCode::Texture> texture_uniforms;
150
151 Vector<uint32_t> ubo_offsets;
152 uint32_t ubo_size = 0;
153
154 String code;
155
156 DepthDraw depth_draw = DEPTH_DRAW_OPAQUE;
157 DepthTest depth_test = DEPTH_TEST_ENABLED;
158
159 bool uses_point_size = false;
160 bool uses_alpha = false;
161 bool uses_blend_alpha = false;
162 bool uses_alpha_clip = false;
163 bool uses_alpha_antialiasing = false;
164 bool uses_depth_prepass_alpha = false;
165 bool uses_discard = false;
166 bool uses_roughness = false;
167 bool uses_normal = false;
168 bool uses_particle_trails = false;
169
170 bool unshaded = false;
171 bool uses_vertex = false;
172 bool uses_position = false;
173 bool uses_sss = false;
174 bool uses_transmittance = false;
175 bool uses_screen_texture = false;
176 bool uses_depth_texture = false;
177 bool uses_normal_texture = false;
178 bool uses_time = false;
179 bool uses_vertex_time = false;
180 bool uses_fragment_time = false;
181 bool writes_modelview_or_projection = false;
182 bool uses_world_coordinates = false;
183 bool uses_screen_texture_mipmaps = false;
184 Cull cull_mode = CULL_DISABLED;
185
186 uint64_t last_pass = 0;
187 uint32_t index = 0;
188
189 virtual void set_code(const String &p_Code);
190
191 virtual bool is_animated() const;
192 virtual bool casts_shadows() const;
193 virtual RS::ShaderNativeSourceCode get_native_source_code() const;
194
195 SelfList<ShaderData> shader_list_element;
196 ShaderData();
197 virtual ~ShaderData();
198 };
199
200 SelfList<ShaderData>::List shader_list;
201
202 RendererRD::MaterialStorage::ShaderData *_create_shader_func();
203 static RendererRD::MaterialStorage::ShaderData *_create_shader_funcs() {
204 return static_cast<SceneShaderForwardClustered *>(singleton)->_create_shader_func();
205 }
206
207 struct MaterialData : public RendererRD::MaterialStorage::MaterialData {
208 ShaderData *shader_data = nullptr;
209 RID uniform_set;
210 uint64_t last_pass = 0;
211 uint32_t index = 0;
212 RID next_pass;
213 uint8_t priority;
214 virtual void set_render_priority(int p_priority);
215 virtual void set_next_pass(RID p_pass);
216 virtual bool update_parameters(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty);
217 virtual ~MaterialData();
218 };
219
220 RendererRD::MaterialStorage::MaterialData *_create_material_func(ShaderData *p_shader);
221 static RendererRD::MaterialStorage::MaterialData *_create_material_funcs(RendererRD::MaterialStorage::ShaderData *p_shader) {
222 return static_cast<SceneShaderForwardClustered *>(singleton)->_create_material_func(static_cast<ShaderData *>(p_shader));
223 }
224
225 SceneForwardClusteredShaderRD shader;
226 ShaderCompiler compiler;
227
228 RID default_shader;
229 RID default_material;
230 RID overdraw_material_shader;
231 RID overdraw_material;
232 RID debug_shadow_splits_material_shader;
233 RID debug_shadow_splits_material;
234 RID default_shader_rd;
235 RID default_shader_sdfgi_rd;
236
237 RID default_vec4_xform_buffer;
238 RID default_vec4_xform_uniform_set;
239
240 RID shadow_sampler;
241
242 RID default_material_uniform_set;
243 ShaderData *default_material_shader_ptr = nullptr;
244
245 RID overdraw_material_uniform_set;
246 ShaderData *overdraw_material_shader_ptr = nullptr;
247
248 RID debug_shadow_splits_material_uniform_set;
249 ShaderData *debug_shadow_splits_material_shader_ptr = nullptr;
250
251 Vector<RD::PipelineSpecializationConstant> default_specialization_constants;
252 bool valid_color_pass_pipelines[PIPELINE_COLOR_PASS_FLAG_COUNT];
253 SceneShaderForwardClustered();
254 ~SceneShaderForwardClustered();
255
256 void init(const String p_defines);
257 void set_default_specialization_constants(const Vector<RD::PipelineSpecializationConstant> &p_constants);
258 void enable_advanced_shader_group(bool p_needs_multiview = false);
259};
260
261} // namespace RendererSceneRenderImplementation
262
263#endif // SCENE_SHADER_FORWARD_CLUSTERED_H
264