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