Debian Codegraph @

servers/rendering/renderer_rd/effects/ss_effects.h

raw symbols SCIP
1 /**************************************************************************/
2 /* ss_effects.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 "servers/rendering/renderer_rd/pipeline_deferred_rd.h"
34 #include "servers/rendering/renderer_rd/shaders/effects/screen_space_reflection.glsl.gen.h"
35 #include "servers/rendering/renderer_rd/shaders/effects/screen_space_reflection_downsample.glsl.gen.h"
36 #include "servers/rendering/renderer_rd/shaders/effects/screen_space_reflection_filter.glsl.gen.h"
37 #include "servers/rendering/renderer_rd/shaders/effects/screen_space_reflection_hiz.glsl.gen.h"
38 #include "servers/rendering/renderer_rd/shaders/effects/screen_space_reflection_resolve.glsl.gen.h"
39 #include "servers/rendering/renderer_rd/shaders/effects/ss_effects_downsample.glsl.gen.h"
40 #include "servers/rendering/renderer_rd/shaders/effects/ssao.glsl.gen.h"
41 #include "servers/rendering/renderer_rd/shaders/effects/ssao_blur.glsl.gen.h"
42 #include "servers/rendering/renderer_rd/shaders/effects/ssao_importance_map.glsl.gen.h"
43 #include "servers/rendering/renderer_rd/shaders/effects/ssao_interleave.glsl.gen.h"
44 #include "servers/rendering/renderer_rd/shaders/effects/ssil.glsl.gen.h"
45 #include "servers/rendering/renderer_rd/shaders/effects/ssil_blur.glsl.gen.h"
46 #include "servers/rendering/renderer_rd/shaders/effects/ssil_importance_map.glsl.gen.h"
47 #include "servers/rendering/renderer_rd/shaders/effects/ssil_interleave.glsl.gen.h"
48 #include "servers/rendering/renderer_rd/shaders/effects/subsurface_scattering.glsl.gen.h"
49 #include "servers/rendering/rendering_server.h"
50
51 #define RB_SCOPE_SSLF SNAME("rb_sslf")
52 #define RB_SCOPE_SSDS SNAME("rb_ssds")
53 #define RB_SCOPE_SSIL SNAME("rb_ssil")
54 #define RB_SCOPE_SSAO SNAME("rb_ssao")
55 #define RB_SCOPE_SSR SNAME("rb_ssr")
56
57 #define RB_LINEAR_DEPTH SNAME("linear_depth")
58 #define RB_FINAL SNAME("final")
59 #define RB_LAST_FRAME SNAME("last_frame")
60 #define RB_DEINTERLEAVED SNAME("deinterleaved")
61 #define RB_DEINTERLEAVED_PONG SNAME("deinterleaved_pong")
62 #define RB_EDGES SNAME("edges")
63 #define RB_IMPORTANCE_MAP SNAME("importance_map")
64 #define RB_IMPORTANCE_PONG SNAME("importance_pong")
65
66 #define RB_NORMAL_ROUGHNESS SNAME("normal_roughness")
67 #define RB_HIZ SNAME("hiz")
68 #define RB_SSR SNAME("ssr")
69 #define RB_MIP_LEVEL SNAME("mip_level")
70
71 class RenderSceneBuffersRD;
72
73 namespace RendererRD {
74
75 class CopyEffects;
76
77 class SSEffects {
78 private:
79 static SSEffects *singleton;
80
81 public:
82 static SSEffects *get_singleton() { return singleton; }
83
84 SSEffects();
85 ~SSEffects();
86
87 /* Last Frame */
88
89 void allocate_last_frame_buffer(Ref<RenderSceneBuffersRD> p_render_buffers, bool p_use_ssil, bool p_use_ssr);
90 void copy_internal_texture_to_last_frame(Ref<RenderSceneBuffersRD> p_render_buffers, CopyEffects &p_copy_effects);
91
92 /* SS Downsampler */
93
94 void downsample_depth(Ref<RenderSceneBuffersRD> p_render_buffers, uint32_t p_view, const Projection &p_projection);
95
96 /* SSIL */
97 void ssil_set_quality(RS::EnvironmentSSILQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to);
98
99 struct SSILRenderBuffers {
100 bool half_size = false;
101 int buffer_width;
102 int buffer_height;
103 int half_buffer_width;
104 int half_buffer_height;
105 };
106
107 struct SSILSettings {
108 float radius = 1.0;
109 float intensity = 2.0;
110 float sharpness = 0.98;
111 float normal_rejection = 1.0;
112
113 Size2i full_screen_size;
114 };
115
116 void ssil_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSILRenderBuffers &p_ssil_buffers, const SSILSettings &p_settings);
117 void screen_space_indirect_lighting(Ref<RenderSceneBuffersRD> p_render_buffers, SSILRenderBuffers &p_ssil_buffers, uint32_t p_view, RID p_normal_buffer, const Projection &p_projection, const Projection &p_last_projection, const SSILSettings &p_settings);
118
119 /* SSAO */
120 void ssao_set_quality(RS::EnvironmentSSAOQuality p_quality, bool p_half_size, float p_adaptive_target, int p_blur_passes, float p_fadeout_from, float p_fadeout_to);
121
122 struct SSAORenderBuffers {
123 bool half_size = false;
124 int buffer_width;
125 int buffer_height;
126 int half_buffer_width;
127 int half_buffer_height;
128 };
129
130 struct SSAOSettings {
131 float radius = 1.0;
132 float intensity = 2.0;
133 float power = 1.5;
134 float detail = 0.5;
135 float horizon = 0.06;
136 float sharpness = 0.98;
137
138 Size2i full_screen_size;
139 };
140
141 void ssao_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSAORenderBuffers &p_ssao_buffers, const SSAOSettings &p_settings);
142 void generate_ssao(Ref<RenderSceneBuffersRD> p_render_buffers, SSAORenderBuffers &p_ssao_buffers, uint32_t p_view, RID p_normal_buffer, const Projection &p_projection, const SSAOSettings &p_settings);
143
144 /* Screen Space Reflection */
145 void ssr_set_half_size(bool p_half_size);
146
147 struct SSRRenderBuffers {
148 Size2i size;
149 uint32_t mipmaps = 1;
150 bool half_size = false;
151 };
152
153 void ssr_allocate_buffers(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RD::DataFormat p_color_format);
154 void screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffers, SSRRenderBuffers &p_ssr_buffers, const RID *p_normal_roughness_slices, int p_max_steps, float p_fade_in, float p_fade_out, float p_tolerance, const Projection *p_projections, const Projection *p_reprojections, const Vector3 *p_eye_offsets, RendererRD::CopyEffects &p_copy_effects);
155
156 /* subsurface scattering */
157 void sss_set_quality(RS::SubSurfaceScatteringQuality p_quality);
158 RS::SubSurfaceScatteringQuality sss_get_quality() const;
159 void sss_set_scale(float p_scale, float p_depth_scale);
160
161 void sub_surface_scattering(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_diffuse, RID p_depth, const Projection &p_camera, const Size2i &p_screen_size);
162
163 private:
164 /* Settings */
165
166 RS::EnvironmentSSAOQuality ssao_quality = RS::ENV_SSAO_QUALITY_MEDIUM;
167 bool ssao_half_size = false;
168 float ssao_adaptive_target = 0.5;
169 int ssao_blur_passes = 2;
170 float ssao_fadeout_from = 50.0;
171 float ssao_fadeout_to = 300.0;
172
173 RS::EnvironmentSSILQuality ssil_quality = RS::ENV_SSIL_QUALITY_MEDIUM;
174 bool ssil_half_size = false;
175 float ssil_adaptive_target = 0.5;
176 int ssil_blur_passes = 4;
177 float ssil_fadeout_from = 50.0;
178 float ssil_fadeout_to = 300.0;
179
180 bool ssr_half_size = false;
181
182 RS::SubSurfaceScatteringQuality sss_quality = RS::SUB_SURFACE_SCATTERING_QUALITY_MEDIUM;
183 float sss_scale = 0.05;
184 float sss_depth_scale = 0.01;
185
186 /* SS Downsampler */
187
188 struct SSEffectsDownsamplePushConstant {
189 float pixel_size[2];
190 float z_far;
191 float z_near;
192 uint32_t orthogonal;
193 float radius_sq;
194 uint32_t pad[2];
195 };
196
197 enum SSEffectsMode {
198 SS_EFFECTS_DOWNSAMPLE,
199 SS_EFFECTS_DOWNSAMPLE_HALF_RES,
200 SS_EFFECTS_DOWNSAMPLE_MIPMAP,
201 SS_EFFECTS_DOWNSAMPLE_MIPMAP_HALF_RES,
202 SS_EFFECTS_DOWNSAMPLE_HALF,
203 SS_EFFECTS_DOWNSAMPLE_HALF_RES_HALF,
204 SS_EFFECTS_DOWNSAMPLE_FULL_MIPS,
205 SS_EFFECTS_MAX
206 };
207
208 struct SSEffectsGatherConstants {
209 float rotation_matrices[80]; //5 vec4s * 4
210 };
211
212 struct SSEffectsShader {
213 SSEffectsDownsamplePushConstant downsample_push_constant;
214 SsEffectsDownsampleShaderRD downsample_shader;
215 RID downsample_shader_version;
216 bool used_half_size_last_frame = false;
217 bool used_mips_last_frame = false;
218 bool used_full_mips_last_frame = false;
219
220 RID gather_constants_buffer;
221
222 RID mirror_sampler;
223
224 PipelineDeferredRD pipelines[SS_EFFECTS_MAX];
225 } ss_effects;
226
227 /* SSIL */
228
229 enum SSILMode {
230 SSIL_GATHER,
231 SSIL_GATHER_BASE,
232 SSIL_GATHER_ADAPTIVE,
233 SSIL_GENERATE_IMPORTANCE_MAP,
234 SSIL_PROCESS_IMPORTANCE_MAPA,
235 SSIL_PROCESS_IMPORTANCE_MAPB,
236 SSIL_BLUR_PASS,
237 SSIL_BLUR_PASS_SMART,
238 SSIL_BLUR_PASS_WIDE,
239 SSIL_INTERLEAVE,
240 SSIL_INTERLEAVE_SMART,
241 SSIL_INTERLEAVE_HALF,
242 SSIL_MAX
243 };
244
245 struct SSILGatherPushConstant {
246 int32_t screen_size[2];
247 int pass;
248 int quality;
249
250 float half_screen_pixel_size[2];
251 float half_screen_pixel_size_x025[2];
252
253 float NDC_to_view_mul[2];
254 float NDC_to_view_add[2];
255
256 float pad2[2];
257 float z_near;
258 float z_far;
259
260 float radius;
261 float intensity;
262 int size_multiplier;
263 int pad;
264
265 float fade_out_mul;
266 float fade_out_add;
267 float normal_rejection_amount;
268 float inv_radius_near_limit;
269
270 uint32_t is_orthogonal;
271 float neg_inv_radius;
272 float load_counter_avg_div;
273 float adaptive_sample_limit;
274
275 int32_t pass_coord_offset[2];
276 float pass_uv_offset[2];
277 };
278
279 struct SSILImportanceMapPushConstant {
280 float half_screen_pixel_size[2];
281 float intensity;
282 float pad;
283 };
284
285 struct SSILBlurPushConstant {
286 float edge_sharpness;
287 float pad;
288 float half_screen_pixel_size[2];
289 };
290
291 struct SSILInterleavePushConstant {
292 float inv_sharpness;
293 uint32_t size_modifier;
294 float pixel_size[2];
295 };
296
297 struct SSILProjectionUniforms {
298 float inv_last_frame_projection_matrix[16];
299 };
300
301 struct SSIL {
302 SSILGatherPushConstant gather_push_constant;
303 SsilShaderRD gather_shader;
304 RID gather_shader_version;
305 RID projection_uniform_buffer;
306
307 SSILImportanceMapPushConstant importance_map_push_constant;
308 SsilImportanceMapShaderRD importance_map_shader;
309 RID importance_map_shader_version;
310 RID importance_map_load_counter;
311 RID counter_uniform_set;
312
313 SSILBlurPushConstant blur_push_constant;
314 SsilBlurShaderRD blur_shader;
315 RID blur_shader_version;
316
317 SSILInterleavePushConstant interleave_push_constant;
318 SsilInterleaveShaderRD interleave_shader;
319 RID interleave_shader_version;
320
321 PipelineDeferredRD pipelines[SSIL_MAX];
322 } ssil;
323
324 void gather_ssil(RD::ComputeListID p_compute_list, const RID *p_ssil_slices, const RID *p_edges_slices, const SSILSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set, RID p_projection_uniform_set);
325
326 /* SSAO */
327
328 enum SSAOMode {
329 SSAO_GATHER,
330 SSAO_GATHER_BASE,
331 SSAO_GATHER_ADAPTIVE,
332 SSAO_GENERATE_IMPORTANCE_MAP,
333 SSAO_PROCESS_IMPORTANCE_MAPA,
334 SSAO_PROCESS_IMPORTANCE_MAPB,
335 SSAO_BLUR_PASS,
336 SSAO_BLUR_PASS_SMART,
337 SSAO_BLUR_PASS_WIDE,
338 SSAO_INTERLEAVE,
339 SSAO_INTERLEAVE_SMART,
340 SSAO_INTERLEAVE_HALF,
341 SSAO_MAX
342 };
343
344 struct SSAOGatherPushConstant {
345 int32_t screen_size[2];
346 int pass;
347 int quality;
348
349 float half_screen_pixel_size[2];
350 int size_multiplier;
351 float detail_intensity;
352
353 float NDC_to_view_mul[2];
354 float NDC_to_view_add[2];
355
356 float pad[2];
357 float half_screen_pixel_size_x025[2];
358
359 float radius;
360 float intensity;
361 float shadow_power;
362 float shadow_clamp;
363
364 float fade_out_mul;
365 float fade_out_add;
366 float horizon_angle_threshold;
367 float inv_radius_near_limit;
368
369 uint32_t is_orthogonal;
370 float neg_inv_radius;
371 float load_counter_avg_div;
372 float adaptive_sample_limit;
373
374 int32_t pass_coord_offset[2];
375 float pass_uv_offset[2];
376 };
377
378 struct SSAOImportanceMapPushConstant {
379 float half_screen_pixel_size[2];
380 float intensity;
381 float power;
382 };
383
384 struct SSAOBlurPushConstant {
385 float edge_sharpness;
386 float pad;
387 float half_screen_pixel_size[2];
388 };
389
390 struct SSAOInterleavePushConstant {
391 float inv_sharpness;
392 uint32_t size_modifier;
393 float pixel_size[2];
394 };
395
396 struct SSAO {
397 SSAOGatherPushConstant gather_push_constant;
398 SsaoShaderRD gather_shader;
399 RID gather_shader_version;
400
401 SSAOImportanceMapPushConstant importance_map_push_constant;
402 SsaoImportanceMapShaderRD importance_map_shader;
403 RID importance_map_shader_version;
404 RID importance_map_load_counter;
405 RID counter_uniform_set;
406
407 SSAOBlurPushConstant blur_push_constant;
408 SsaoBlurShaderRD blur_shader;
409 RID blur_shader_version;
410
411 SSAOInterleavePushConstant interleave_push_constant;
412 SsaoInterleaveShaderRD interleave_shader;
413 RID interleave_shader_version;
414
415 PipelineDeferredRD pipelines[SSAO_MAX];
416 } ssao;
417
418 void gather_ssao(RD::ComputeListID p_compute_list, const RID *p_ao_slices, const SSAOSettings &p_settings, bool p_adaptive_base_pass, RID p_gather_uniform_set, RID p_importance_map_uniform_set);
419
420 /* Screen Space Reflection */
421
422 enum ScreenSpaceReflectionDownsampleMode {
423 SCREEN_SPACE_REFLECTION_DOWNSAMPLE_DEFAULT,
424 SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_WIDTH,
425 SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_HEIGHT,
426 SCREEN_SPACE_REFLECTION_DOWNSAMPLE_ODD_WIDTH_AND_HEIGHT,
427 SCREEN_SPACE_REFLECTION_DOWNSAMPLE_MAX
428 };
429
430 struct ScreenSpaceReflectionDownsamplePushConstant {
431 int32_t screen_size[2];
432 int32_t pad[2];
433 };
434
435 enum ScreenSpaceReflectionHizMode {
436 SCREEN_SPACE_REFLECTION_HIZ_DEFAULT,
437 SCREEN_SPACE_REFLECTION_HIZ_ODD_WIDTH,
438 SCREEN_SPACE_REFLECTION_HIZ_ODD_HEIGHT,
439 SCREEN_SPACE_REFLECTION_HIZ_ODD_WIDTH_AND_HEIGHT,
440 SCREEN_SPACE_REFLECTION_HIZ_MAX
441 };
442
443 struct ScreenSpaceReflectionHizPushConstant {
444 int32_t screen_size[2];
445 int32_t pad[2];
446 };
447
448 struct ScreenSpaceReflectionSceneData {
449 float projection[2][16];
450 float inv_projection[2][16];
451 float reprojection[2][16];
452 float eye_offset[2][4];
453 };
454
455 struct ScreenSpaceReflectionPushConstant {
456 int32_t screen_size[2];
457 int32_t mipmaps;
458 int32_t num_steps;
459 float distance_fade;
460 float curve_fade_in;
461 float depth_tolerance;
462 int32_t orthogonal;
463 uint32_t view_index;
464 int32_t pad[3];
465 };
466
467 struct ScreenSpaceReflectionFilterPushConstant {
468 int32_t screen_size[2];
469 uint32_t mip_level;
470 int32_t pad;
471 };
472
473 struct ScreenSpaceReflectionResolvePushConstant {
474 int32_t screen_size[2];
475 int32_t pad[2];
476 };
477
478 struct ScreenSpaceReflection {
479 ScreenSpaceReflectionDownsampleShaderRD downsample_shader;
480 RID downsample_shader_version;
481 PipelineDeferredRD downsample_pipelines[SCREEN_SPACE_REFLECTION_DOWNSAMPLE_MAX];
482
483 ScreenSpaceReflectionHizShaderRD hiz_shader;
484 RID hiz_shader_version;
485 PipelineDeferredRD hiz_pipelines[SCREEN_SPACE_REFLECTION_HIZ_MAX];
486
487 ScreenSpaceReflectionShaderRD ssr_shader;
488 RID ssr_shader_version;
489 PipelineDeferredRD ssr_pipeline;
490 RID ubo;
491
492 ScreenSpaceReflectionFilterShaderRD filter_shader;
493 RID filter_shader_version;
494 PipelineDeferredRD filter_pipeline;
495
496 ScreenSpaceReflectionResolveShaderRD resolve_shader;
497 RID resolve_shader_version;
498 PipelineDeferredRD resolve_pipeline;
499 } ssr;
500
501 /* Subsurface scattering */
502
503 enum SSSMode {
504 SUBSURFACE_SCATTERING_MODE_LOW_QUALITY,
505 SUBSURFACE_SCATTERING_MODE_MEDIUM_QUALITY,
506 SUBSURFACE_SCATTERING_MODE_HIGH_QUALITY,
507 SUBSURFACE_SCATTERING_MODE_MAX
508 };
509
510 struct SubSurfaceScatteringPushConstant {
511 int32_t screen_size[2];
512 float camera_z_far;
513 float camera_z_near;
514
515 uint32_t vertical;
516 uint32_t orthogonal;
517 float unit_size;
518 float scale;
519
520 float depth_scale;
521 uint32_t pad[3];
522 };
523
524 struct SubSurfaceScattering {
525 SubSurfaceScatteringPushConstant push_constant;
526 SubsurfaceScatteringShaderRD shader;
527 RID shader_version;
528 PipelineDeferredRD pipelines[SUBSURFACE_SCATTERING_MODE_MAX];
529 } sss;
530 };
531
532 } // namespace RendererRD