Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/effects/copy_effects.cpp
10279 views
1
/**************************************************************************/
2
/* copy_effects.cpp */
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
#include "copy_effects.h"
32
#include "core/config/project_settings.h"
33
#include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
34
#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
35
#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
36
#include "thirdparty/misc/cubemap_coeffs.h"
37
38
using namespace RendererRD;
39
40
CopyEffects *CopyEffects::singleton = nullptr;
41
42
CopyEffects *CopyEffects::get_singleton() {
43
return singleton;
44
}
45
46
CopyEffects::CopyEffects(bool p_prefer_raster_effects) {
47
singleton = this;
48
prefer_raster_effects = p_prefer_raster_effects;
49
50
if (prefer_raster_effects) {
51
// init blur shader (on compute use copy shader)
52
53
Vector<String> blur_modes;
54
blur_modes.push_back("\n#define MODE_MIPMAP\n"); // BLUR_MIPMAP
55
blur_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n"); // BLUR_MODE_GAUSSIAN_BLUR
56
blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n"); // BLUR_MODE_GAUSSIAN_GLOW
57
blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n"); // BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE
58
blur_modes.push_back("\n#define MODE_COPY\n"); // BLUR_MODE_COPY
59
blur_modes.push_back("\n#define MODE_SET_COLOR\n"); // BLUR_MODE_SET_COLOR
60
61
blur_raster.shader.initialize(blur_modes);
62
memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
63
blur_raster.shader_version = blur_raster.shader.version_create();
64
65
for (int i = 0; i < BLUR_MODE_MAX; i++) {
66
blur_raster.pipelines[i].setup(blur_raster.shader.version_get_shader(blur_raster.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
67
}
68
69
} else {
70
// not used in clustered
71
for (int i = 0; i < BLUR_MODE_MAX; i++) {
72
blur_raster.pipelines[i].clear();
73
}
74
}
75
76
{
77
Vector<String> copy_modes;
78
copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");
79
copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");
80
copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n");
81
copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");
82
copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");
83
copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");
84
copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");
85
copy_modes.push_back("\n#define MODE_SET_COLOR\n");
86
copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");
87
copy_modes.push_back("\n#define MODE_MIPMAP\n");
88
copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");
89
copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");
90
copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");
91
92
copy.shader.initialize(copy_modes);
93
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
94
95
copy.shader_version = copy.shader.version_create();
96
97
for (int i = 0; i < COPY_MODE_MAX; i++) {
98
if (copy.shader.is_variant_enabled(i)) {
99
copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));
100
}
101
}
102
}
103
104
{
105
Vector<String> copy_modes;
106
copy_modes.push_back("\n"); // COPY_TO_FB_COPY
107
copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n"); // COPY_TO_FB_COPY_PANORAMA_TO_DP
108
copy_modes.push_back("\n#define MODE_TWO_SOURCES\n"); // COPY_TO_FB_COPY2
109
copy_modes.push_back("\n#define MODE_SET_COLOR\n"); // COPY_TO_FB_SET_COLOR
110
copy_modes.push_back("\n#define USE_MULTIVIEW\n"); // COPY_TO_FB_MULTIVIEW
111
copy_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_TWO_SOURCES\n"); // COPY_TO_FB_MULTIVIEW_WITH_DEPTH
112
113
copy_to_fb.shader.initialize(copy_modes);
114
115
if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
116
copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW, false);
117
copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW_WITH_DEPTH, false);
118
}
119
120
copy_to_fb.shader_version = copy_to_fb.shader.version_create();
121
122
//use additive
123
124
for (int i = 0; i < COPY_TO_FB_MAX; i++) {
125
if (copy_to_fb.shader.is_variant_enabled(i)) {
126
copy_to_fb.pipelines[i].setup(copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
127
} else {
128
copy_to_fb.pipelines[i].clear();
129
}
130
}
131
}
132
133
{
134
// Initialize copier
135
Vector<String> copy_modes;
136
copy_modes.push_back("\n");
137
138
cube_to_dp.shader.initialize(copy_modes);
139
140
cube_to_dp.shader_version = cube_to_dp.shader.version_create();
141
RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
142
RD::PipelineDepthStencilState dss;
143
dss.enable_depth_test = true;
144
dss.depth_compare_operator = RD::COMPARE_OP_ALWAYS;
145
dss.enable_depth_write = true;
146
cube_to_dp.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), dss, RD::PipelineColorBlendState(), 0);
147
}
148
149
{
150
//Initialize cubemap downsampler
151
Vector<String> cubemap_downsampler_modes;
152
cubemap_downsampler_modes.push_back("");
153
154
if (prefer_raster_effects) {
155
cubemap_downsampler.raster_shader.initialize(cubemap_downsampler_modes);
156
157
cubemap_downsampler.shader_version = cubemap_downsampler.raster_shader.version_create();
158
159
cubemap_downsampler.raster_pipeline.setup(cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
160
} else {
161
cubemap_downsampler.compute_shader.initialize(cubemap_downsampler_modes);
162
163
cubemap_downsampler.shader_version = cubemap_downsampler.compute_shader.version_create();
164
165
cubemap_downsampler.compute_pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0));
166
cubemap_downsampler.raster_pipeline.clear();
167
}
168
}
169
170
{
171
// Initialize cubemap filter
172
filter.use_high_quality = GLOBAL_GET("rendering/reflections/sky_reflections/fast_filter_high_quality");
173
174
Vector<String> cubemap_filter_modes;
175
cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");
176
cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");
177
cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");
178
cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");
179
180
if (filter.use_high_quality) {
181
filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));
182
RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0]);
183
} else {
184
filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));
185
RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0]);
186
}
187
188
if (prefer_raster_effects) {
189
filter.raster_shader.initialize(cubemap_filter_modes);
190
191
// array variants are not supported in raster
192
filter.raster_shader.set_variant_enabled(FILTER_MODE_HIGH_QUALITY_ARRAY, false);
193
filter.raster_shader.set_variant_enabled(FILTER_MODE_LOW_QUALITY_ARRAY, false);
194
195
filter.shader_version = filter.raster_shader.version_create();
196
197
for (int i = 0; i < FILTER_MODE_MAX; i++) {
198
if (filter.raster_shader.is_variant_enabled(i)) {
199
filter.raster_pipelines[i].setup(filter.raster_shader.version_get_shader(filter.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
200
} else {
201
filter.raster_pipelines[i].clear();
202
}
203
}
204
205
Vector<RD::Uniform> uniforms;
206
{
207
RD::Uniform u;
208
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
209
u.binding = 0;
210
u.append_id(filter.coefficient_buffer);
211
uniforms.push_back(u);
212
}
213
filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.raster_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
214
} else {
215
filter.compute_shader.initialize(cubemap_filter_modes);
216
filter.shader_version = filter.compute_shader.version_create();
217
218
for (int i = 0; i < FILTER_MODE_MAX; i++) {
219
filter.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.compute_shader.version_get_shader(filter.shader_version, i));
220
filter.raster_pipelines[i].clear();
221
}
222
223
Vector<RD::Uniform> uniforms;
224
{
225
RD::Uniform u;
226
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
227
u.binding = 0;
228
u.append_id(filter.coefficient_buffer);
229
uniforms.push_back(u);
230
}
231
filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);
232
}
233
}
234
235
{
236
// Initialize roughness
237
Vector<String> cubemap_roughness_modes;
238
cubemap_roughness_modes.push_back("");
239
240
if (prefer_raster_effects) {
241
roughness.raster_shader.initialize(cubemap_roughness_modes);
242
243
roughness.shader_version = roughness.raster_shader.version_create();
244
245
roughness.raster_pipeline.setup(roughness.raster_shader.version_get_shader(roughness.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
246
247
} else {
248
roughness.compute_shader.initialize(cubemap_roughness_modes);
249
250
roughness.shader_version = roughness.compute_shader.version_create();
251
252
roughness.compute_pipeline = RD::get_singleton()->compute_pipeline_create(roughness.compute_shader.version_get_shader(roughness.shader_version, 0));
253
roughness.raster_pipeline.clear();
254
}
255
}
256
257
{
258
Vector<String> specular_modes;
259
specular_modes.push_back("\n#define MODE_MERGE\n"); // SPECULAR_MERGE_ADD
260
specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n"); // SPECULAR_MERGE_SSR
261
specular_modes.push_back("\n"); // SPECULAR_MERGE_ADDITIVE_ADD
262
specular_modes.push_back("\n#define MODE_SSR\n"); // SPECULAR_MERGE_ADDITIVE_SSR
263
264
specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_MERGE\n"); // SPECULAR_MERGE_ADD_MULTIVIEW
265
specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_MERGE\n#define MODE_SSR\n"); // SPECULAR_MERGE_SSR_MULTIVIEW
266
specular_modes.push_back("\n#define USE_MULTIVIEW\n"); // SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW
267
specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_SSR\n"); // SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW
268
269
specular_merge.shader.initialize(specular_modes);
270
271
if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {
272
specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADD_MULTIVIEW, false);
273
specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_SSR_MULTIVIEW, false);
274
specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW, false);
275
specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW, false);
276
}
277
278
specular_merge.shader_version = specular_merge.shader.version_create();
279
280
//use additive
281
282
RD::PipelineColorBlendState::Attachment ba;
283
ba.enable_blend = true;
284
ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;
285
ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;
286
ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
287
ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;
288
ba.color_blend_op = RD::BLEND_OP_ADD;
289
ba.alpha_blend_op = RD::BLEND_OP_ADD;
290
291
RD::PipelineColorBlendState blend_additive;
292
blend_additive.attachments.push_back(ba);
293
294
for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {
295
if (specular_merge.shader.is_variant_enabled(i)) {
296
RD::PipelineColorBlendState blend_state;
297
if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR || i == SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW || i == SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW) {
298
blend_state = blend_additive;
299
} else {
300
blend_state = RD::PipelineColorBlendState::create_disabled();
301
}
302
specular_merge.pipelines[i].setup(specular_merge.shader.version_get_shader(specular_merge.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
303
}
304
}
305
}
306
}
307
308
CopyEffects::~CopyEffects() {
309
if (prefer_raster_effects) {
310
blur_raster.shader.version_free(blur_raster.shader_version);
311
cubemap_downsampler.raster_shader.version_free(cubemap_downsampler.shader_version);
312
filter.raster_shader.version_free(filter.shader_version);
313
roughness.raster_shader.version_free(roughness.shader_version);
314
} else {
315
cubemap_downsampler.compute_shader.version_free(cubemap_downsampler.shader_version);
316
filter.compute_shader.version_free(filter.shader_version);
317
roughness.compute_shader.version_free(roughness.shader_version);
318
}
319
320
copy.shader.version_free(copy.shader_version);
321
specular_merge.shader.version_free(specular_merge.shader_version);
322
323
RD::get_singleton()->free(filter.coefficient_buffer);
324
325
if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
326
RD::get_singleton()->free(filter.image_uniform_set);
327
}
328
329
if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {
330
RD::get_singleton()->free(filter.uniform_set);
331
}
332
333
copy_to_fb.shader.version_free(copy_to_fb.shader_version);
334
cube_to_dp.shader.version_free(cube_to_dp.shader_version);
335
336
singleton = nullptr;
337
}
338
339
void CopyEffects::copy_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_all_source, bool p_8_bit_dst, bool p_alpha_to_one) {
340
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
341
ERR_FAIL_NULL(uniform_set_cache);
342
MaterialStorage *material_storage = MaterialStorage::get_singleton();
343
ERR_FAIL_NULL(material_storage);
344
345
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
346
if (p_flip_y) {
347
copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
348
}
349
350
if (p_force_luminance) {
351
copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;
352
}
353
354
if (p_all_source) {
355
copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;
356
}
357
358
if (p_alpha_to_one) {
359
copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;
360
}
361
362
copy.push_constant.section[0] = p_rect.position.x;
363
copy.push_constant.section[1] = p_rect.position.y;
364
copy.push_constant.section[2] = p_rect.size.width;
365
copy.push_constant.section[3] = p_rect.size.height;
366
copy.push_constant.target[0] = p_rect.position.x;
367
copy.push_constant.target[1] = p_rect.position.y;
368
369
// setup our uniforms
370
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
371
372
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
373
RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
374
375
CopyMode mode = p_8_bit_dst ? COPY_MODE_SIMPLY_COPY_8BIT : COPY_MODE_SIMPLY_COPY;
376
RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
377
ERR_FAIL_COND(shader.is_null());
378
379
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
380
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
381
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
382
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
383
RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
384
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
385
RD::get_singleton()->compute_list_end();
386
}
387
388
void CopyEffects::copy_cubemap_to_panorama(RID p_source_cube, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array) {
389
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
390
ERR_FAIL_NULL(uniform_set_cache);
391
MaterialStorage *material_storage = MaterialStorage::get_singleton();
392
ERR_FAIL_NULL(material_storage);
393
394
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
395
396
copy.push_constant.section[0] = 0;
397
copy.push_constant.section[1] = 0;
398
copy.push_constant.section[2] = p_panorama_size.width;
399
copy.push_constant.section[3] = p_panorama_size.height;
400
copy.push_constant.target[0] = 0;
401
copy.push_constant.target[1] = 0;
402
copy.push_constant.camera_z_far = p_lod;
403
404
// setup our uniforms
405
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
406
407
RD::Uniform u_source_cube(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cube }));
408
RD::Uniform u_dest_panorama(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_panorama);
409
410
CopyMode mode = p_is_array ? COPY_MODE_CUBE_ARRAY_TO_PANORAMA : COPY_MODE_CUBE_TO_PANORAMA;
411
RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
412
ERR_FAIL_COND(shader.is_null());
413
414
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
415
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
416
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cube), 0);
417
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_panorama), 3);
418
RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
419
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_panorama_size.width, p_panorama_size.height, 1);
420
RD::get_singleton()->compute_list_end();
421
}
422
423
void CopyEffects::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {
424
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
425
ERR_FAIL_NULL(uniform_set_cache);
426
MaterialStorage *material_storage = MaterialStorage::get_singleton();
427
ERR_FAIL_NULL(material_storage);
428
429
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
430
if (p_flip_y) {
431
copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
432
}
433
434
copy.push_constant.section[0] = 0;
435
copy.push_constant.section[1] = 0;
436
copy.push_constant.section[2] = p_rect.size.width;
437
copy.push_constant.section[3] = p_rect.size.height;
438
copy.push_constant.target[0] = p_rect.position.x;
439
copy.push_constant.target[1] = p_rect.position.y;
440
441
// setup our uniforms
442
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
443
444
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
445
RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
446
447
CopyMode mode = COPY_MODE_SIMPLY_COPY_DEPTH;
448
RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
449
ERR_FAIL_COND(shader.is_null());
450
451
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
452
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
453
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
454
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
455
RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
456
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
457
RD::get_singleton()->compute_list_end();
458
}
459
460
void CopyEffects::copy_depth_to_rect_and_linearize(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, float p_z_near, float p_z_far) {
461
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
462
ERR_FAIL_NULL(uniform_set_cache);
463
MaterialStorage *material_storage = MaterialStorage::get_singleton();
464
ERR_FAIL_NULL(material_storage);
465
466
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
467
if (p_flip_y) {
468
copy.push_constant.flags |= COPY_FLAG_FLIP_Y;
469
}
470
471
copy.push_constant.section[0] = 0;
472
copy.push_constant.section[1] = 0;
473
copy.push_constant.section[2] = p_rect.size.width;
474
copy.push_constant.section[3] = p_rect.size.height;
475
copy.push_constant.target[0] = p_rect.position.x;
476
copy.push_constant.target[1] = p_rect.position.y;
477
copy.push_constant.camera_z_far = p_z_far;
478
copy.push_constant.camera_z_near = p_z_near;
479
480
// setup our uniforms
481
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
482
483
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
484
RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
485
486
CopyMode mode = COPY_MODE_LINEARIZE_DEPTH;
487
RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
488
ERR_FAIL_COND(shader.is_null());
489
490
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
491
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
492
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
493
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
494
RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
495
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);
496
RD::get_singleton()->compute_list_end();
497
}
498
499
void CopyEffects::copy_to_atlas_fb(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_uv_rect, RD::DrawListID p_draw_list, bool p_flip_y, bool p_panorama) {
500
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
501
ERR_FAIL_NULL(uniform_set_cache);
502
MaterialStorage *material_storage = MaterialStorage::get_singleton();
503
ERR_FAIL_NULL(material_storage);
504
505
memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
506
507
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_USE_SECTION;
508
copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;
509
copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;
510
copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;
511
copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;
512
513
if (p_flip_y) {
514
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FLIP_Y;
515
}
516
517
copy_to_fb.push_constant.luminance_multiplier = 1.0;
518
519
// setup our uniforms
520
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
521
522
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
523
524
CopyToFBMode mode = p_panorama ? COPY_TO_FB_COPY_PANORAMA_TO_DP : COPY_TO_FB_COPY;
525
RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
526
ERR_FAIL_COND(shader.is_null());
527
528
RD::DrawListID draw_list = p_draw_list;
529
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
530
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
531
RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
532
RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
533
RD::get_singleton()->draw_list_draw(draw_list, true);
534
}
535
536
void CopyEffects::copy_to_fb_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_alpha_to_zero, bool p_srgb, RID p_secondary, bool p_multiview, bool p_alpha_to_one, bool p_linear, bool p_normal, const Rect2 &p_src_rect) {
537
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
538
ERR_FAIL_NULL(uniform_set_cache);
539
MaterialStorage *material_storage = MaterialStorage::get_singleton();
540
ERR_FAIL_NULL(material_storage);
541
542
memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
543
copy_to_fb.push_constant.luminance_multiplier = 1.0;
544
545
if (p_flip_y) {
546
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FLIP_Y;
547
}
548
if (p_force_luminance) {
549
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FORCE_LUMINANCE;
550
}
551
if (p_alpha_to_zero) {
552
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_ALPHA_TO_ZERO;
553
}
554
if (p_srgb) {
555
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_SRGB;
556
}
557
if (p_alpha_to_one) {
558
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_ALPHA_TO_ONE;
559
}
560
if (p_linear) {
561
// Used for copying to a linear buffer. In the mobile renderer we divide the contents of the linear buffer
562
// to allow for a wider effective range.
563
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_LINEAR;
564
copy_to_fb.push_constant.luminance_multiplier = prefer_raster_effects ? 2.0 : 1.0;
565
}
566
567
if (p_normal) {
568
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_NORMAL;
569
}
570
571
if (p_src_rect != Rect2()) {
572
copy_to_fb.push_constant.section[0] = p_src_rect.position.x;
573
copy_to_fb.push_constant.section[1] = p_src_rect.position.y;
574
copy_to_fb.push_constant.section[2] = p_src_rect.size.x;
575
copy_to_fb.push_constant.section[3] = p_src_rect.size.y;
576
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_USE_SRC_SECTION;
577
}
578
579
// setup our uniforms
580
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
581
582
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
583
584
CopyToFBMode mode;
585
if (p_multiview) {
586
mode = p_secondary.is_valid() ? COPY_TO_FB_MULTIVIEW_WITH_DEPTH : COPY_TO_FB_MULTIVIEW;
587
} else {
588
mode = p_secondary.is_valid() ? COPY_TO_FB_COPY2 : COPY_TO_FB_COPY;
589
}
590
591
RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
592
ERR_FAIL_COND(shader.is_null());
593
594
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, p_rect);
595
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
596
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
597
if (p_secondary.is_valid()) {
598
// TODO may need to do this differently when reading from depth buffer for multiview
599
RD::Uniform u_secondary(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_secondary }));
600
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_secondary), 1);
601
}
602
RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
603
RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
604
RD::get_singleton()->draw_list_draw(draw_list, true);
605
RD::get_singleton()->draw_list_end();
606
}
607
608
void CopyEffects::copy_to_drawlist(RD::DrawListID p_draw_list, RD::FramebufferFormatID p_fb_format, RID p_source_rd_texture, bool p_linear) {
609
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
610
ERR_FAIL_NULL(uniform_set_cache);
611
MaterialStorage *material_storage = MaterialStorage::get_singleton();
612
ERR_FAIL_NULL(material_storage);
613
614
memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
615
copy_to_fb.push_constant.luminance_multiplier = 1.0;
616
617
if (p_linear) {
618
// Used for copying to a linear buffer. In the mobile renderer we divide the contents of the linear buffer
619
// to allow for a wider effective range.
620
copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_LINEAR;
621
copy_to_fb.push_constant.luminance_multiplier = prefer_raster_effects ? 2.0 : 1.0;
622
}
623
624
// setup our uniforms
625
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
626
627
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
628
629
// Multiview not supported here!
630
CopyToFBMode mode = COPY_TO_FB_COPY;
631
632
RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
633
ERR_FAIL_COND(shader.is_null());
634
635
RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, p_fb_format));
636
RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
637
RD::get_singleton()->draw_list_bind_index_array(p_draw_list, material_storage->get_quad_index_array());
638
RD::get_singleton()->draw_list_set_push_constant(p_draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
639
RD::get_singleton()->draw_list_draw(p_draw_list, true);
640
}
641
642
void CopyEffects::copy_raster(RID p_source_texture, RID p_dest_framebuffer) {
643
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the copy with the clustered renderer.");
644
645
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
646
ERR_FAIL_NULL(uniform_set_cache);
647
MaterialStorage *material_storage = MaterialStorage::get_singleton();
648
ERR_FAIL_NULL(material_storage);
649
650
memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
651
652
// setup our uniforms
653
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
654
655
RD::Uniform u_source_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_texture }));
656
657
RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, BLUR_MODE_COPY);
658
ERR_FAIL_COND(shader.is_null());
659
660
// Just copy it back (we use our blur raster shader here)..
661
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
662
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[BLUR_MODE_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
663
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_texture), 0);
664
RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
665
666
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
667
RD::get_singleton()->draw_list_end();
668
}
669
670
void CopyEffects::gaussian_blur(RID p_source_rd_texture, RID p_texture, const Rect2i &p_region, const Size2i &p_size, bool p_8bit_dst) {
671
ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian blur with the mobile renderer.");
672
673
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
674
ERR_FAIL_NULL(uniform_set_cache);
675
MaterialStorage *material_storage = MaterialStorage::get_singleton();
676
ERR_FAIL_NULL(material_storage);
677
678
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
679
680
copy.push_constant.section[0] = p_region.position.x;
681
copy.push_constant.section[1] = p_region.position.y;
682
copy.push_constant.target[0] = p_region.position.x;
683
copy.push_constant.target[1] = p_region.position.y;
684
copy.push_constant.section[2] = p_size.width;
685
copy.push_constant.section[3] = p_size.height;
686
687
// setup our uniforms
688
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
689
690
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
691
RD::Uniform u_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_texture);
692
693
CopyMode mode = p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY;
694
RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
695
ERR_FAIL_COND(shader.is_null());
696
697
RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();
698
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
699
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
700
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_texture), 3);
701
702
RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
703
704
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
705
706
RD::get_singleton()->compute_list_end();
707
}
708
709
void CopyEffects::gaussian_blur_raster(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_region, const Size2i &p_size) {
710
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian blur with the clustered renderer.");
711
712
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
713
ERR_FAIL_NULL(uniform_set_cache);
714
MaterialStorage *material_storage = MaterialStorage::get_singleton();
715
ERR_FAIL_NULL(material_storage);
716
717
RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
718
719
memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
720
721
BlurRasterMode blur_mode = BLUR_MODE_GAUSSIAN_BLUR;
722
723
blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
724
blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
725
726
// setup our uniforms
727
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
728
729
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
730
731
RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
732
ERR_FAIL_COND(shader.is_null());
733
734
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);
735
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));
736
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
737
738
RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
739
740
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
741
RD::get_singleton()->draw_list_end();
742
}
743
744
void CopyEffects::gaussian_glow(RID p_source_rd_texture, RID p_back_texture, const Size2i &p_size, float p_strength, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_scale) {
745
ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian glow with the mobile renderer.");
746
747
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
748
ERR_FAIL_NULL(uniform_set_cache);
749
MaterialStorage *material_storage = MaterialStorage::get_singleton();
750
ERR_FAIL_NULL(material_storage);
751
752
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
753
754
CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;
755
uint32_t base_flags = 0;
756
757
copy.push_constant.section[2] = p_size.x;
758
copy.push_constant.section[3] = p_size.y;
759
760
copy.push_constant.glow_strength = p_strength;
761
copy.push_constant.glow_bloom = p_bloom;
762
copy.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
763
copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
764
copy.push_constant.glow_exposure = p_exposure;
765
copy.push_constant.glow_white = 0; //actually unused
766
copy.push_constant.glow_luminance_cap = p_luminance_cap;
767
768
copy.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also
769
770
// setup our uniforms
771
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
772
773
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
774
RD::Uniform u_back_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_back_texture);
775
776
RID shader = copy.shader.version_get_shader(copy.shader_version, copy_mode);
777
ERR_FAIL_COND(shader.is_null());
778
779
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
780
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);
781
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
782
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_back_texture), 3);
783
if (p_auto_exposure.is_valid() && p_first_pass) {
784
RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
785
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
786
}
787
788
copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0);
789
RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
790
791
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
792
RD::get_singleton()->compute_list_end();
793
}
794
795
void CopyEffects::gaussian_glow_raster(RID p_source_rd_texture, RID p_half_texture, RID p_dest_texture, float p_luminance_multiplier, const Size2i &p_size, float p_strength, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_scale) {
796
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian glow with the clustered renderer.");
797
798
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
799
ERR_FAIL_NULL(uniform_set_cache);
800
MaterialStorage *material_storage = MaterialStorage::get_singleton();
801
ERR_FAIL_NULL(material_storage);
802
803
RID half_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_half_texture);
804
RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
805
806
memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
807
808
BlurRasterMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;
809
uint32_t base_flags = 0;
810
811
blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
812
blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
813
814
blur_raster.push_constant.glow_strength = p_strength;
815
blur_raster.push_constant.glow_bloom = p_bloom;
816
blur_raster.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;
817
blur_raster.push_constant.glow_hdr_scale = p_hdr_bleed_scale;
818
blur_raster.push_constant.glow_exposure = p_exposure;
819
blur_raster.push_constant.glow_white = 0; //actually unused
820
blur_raster.push_constant.glow_luminance_cap = p_luminance_cap;
821
822
blur_raster.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also
823
824
blur_raster.push_constant.luminance_multiplier = p_luminance_multiplier;
825
826
// setup our uniforms
827
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
828
829
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
830
RD::Uniform u_half_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_half_texture }));
831
832
RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
833
ERR_FAIL_COND(shader.is_null());
834
835
//HORIZONTAL
836
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(half_framebuffer);
837
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(half_framebuffer)));
838
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
839
if (p_auto_exposure.is_valid() && p_first_pass) {
840
RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));
841
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);
842
}
843
844
blur_raster.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);
845
RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
846
847
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
848
RD::get_singleton()->draw_list_end();
849
850
blur_mode = BLUR_MODE_GAUSSIAN_GLOW;
851
852
shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);
853
ERR_FAIL_COND(shader.is_null());
854
855
//VERTICAL
856
draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);
857
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));
858
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture), 0);
859
860
blur_raster.push_constant.flags = base_flags;
861
RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
862
863
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
864
RD::get_singleton()->draw_list_end();
865
}
866
867
void CopyEffects::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
868
ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the make_mipmap shader with the mobile renderer.");
869
870
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
871
ERR_FAIL_NULL(uniform_set_cache);
872
MaterialStorage *material_storage = MaterialStorage::get_singleton();
873
ERR_FAIL_NULL(material_storage);
874
875
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
876
877
copy.push_constant.section[0] = 0;
878
copy.push_constant.section[1] = 0;
879
copy.push_constant.section[2] = p_size.width;
880
copy.push_constant.section[3] = p_size.height;
881
882
// setup our uniforms
883
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
884
885
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
886
RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
887
888
CopyMode mode = COPY_MODE_MIPMAP;
889
RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
890
ERR_FAIL_COND(shader.is_null());
891
892
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
893
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
894
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
895
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
896
RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
897
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);
898
RD::get_singleton()->compute_list_end();
899
}
900
901
void CopyEffects::make_mipmap_raster(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {
902
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of mipmap with the clustered renderer.");
903
904
RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
905
906
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
907
ERR_FAIL_NULL(uniform_set_cache);
908
MaterialStorage *material_storage = MaterialStorage::get_singleton();
909
ERR_FAIL_NULL(material_storage);
910
911
memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));
912
913
BlurRasterMode mode = BLUR_MIPMAP;
914
915
blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);
916
blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);
917
918
// setup our uniforms
919
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
920
921
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
922
923
RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, mode);
924
ERR_FAIL_COND(shader.is_null());
925
926
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);
927
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));
928
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
929
RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));
930
931
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
932
RD::get_singleton()->draw_list_end();
933
}
934
935
void CopyEffects::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {
936
ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the set_color shader with the mobile renderer.");
937
938
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
939
ERR_FAIL_NULL(uniform_set_cache);
940
941
memset(&copy.push_constant, 0, sizeof(CopyPushConstant));
942
943
copy.push_constant.section[0] = 0;
944
copy.push_constant.section[1] = 0;
945
copy.push_constant.section[2] = p_region.size.width;
946
copy.push_constant.section[3] = p_region.size.height;
947
copy.push_constant.target[0] = p_region.position.x;
948
copy.push_constant.target[1] = p_region.position.y;
949
copy.push_constant.set_color[0] = p_color.r;
950
copy.push_constant.set_color[1] = p_color.g;
951
copy.push_constant.set_color[2] = p_color.b;
952
copy.push_constant.set_color[3] = p_color.a;
953
954
// setup our uniforms
955
RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);
956
957
CopyMode mode = p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR;
958
RID shader = copy.shader.version_get_shader(copy.shader_version, mode);
959
ERR_FAIL_COND(shader.is_null());
960
961
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
962
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);
963
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);
964
RD::get_singleton()->compute_list_set_push_constant(compute_list, &copy.push_constant, sizeof(CopyPushConstant));
965
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);
966
RD::get_singleton()->compute_list_end();
967
}
968
969
void CopyEffects::set_color_raster(RID p_dest_texture, const Color &p_color, const Rect2i &p_region) {
970
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the set_color shader with the clustered renderer.");
971
972
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
973
ERR_FAIL_NULL(uniform_set_cache);
974
MaterialStorage *material_storage = MaterialStorage::get_singleton();
975
ERR_FAIL_NULL(material_storage);
976
977
memset(&copy_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));
978
979
copy_to_fb.push_constant.set_color[0] = p_color.r;
980
copy_to_fb.push_constant.set_color[1] = p_color.g;
981
copy_to_fb.push_constant.set_color[2] = p_color.b;
982
copy_to_fb.push_constant.set_color[3] = p_color.a;
983
984
RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);
985
986
CopyToFBMode mode = COPY_TO_FB_SET_COLOR;
987
988
RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);
989
ERR_FAIL_COND(shader.is_null());
990
991
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, p_region);
992
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));
993
RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
994
RD::get_singleton()->draw_list_set_push_constant(draw_list, &copy_to_fb.push_constant, sizeof(CopyToFbPushConstant));
995
RD::get_singleton()->draw_list_draw(draw_list, true);
996
RD::get_singleton()->draw_list_end();
997
}
998
999
void CopyEffects::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dst_framebuffer, const Rect2 &p_rect, const Vector2 &p_dst_size, float p_z_near, float p_z_far, bool p_dp_flip) {
1000
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
1001
ERR_FAIL_NULL(uniform_set_cache);
1002
MaterialStorage *material_storage = MaterialStorage::get_singleton();
1003
ERR_FAIL_NULL(material_storage);
1004
1005
Rect2i screen_rect;
1006
float atlas_width = p_dst_size.width / p_rect.size.width;
1007
float atlas_height = p_dst_size.height / p_rect.size.height;
1008
screen_rect.position.x = (int32_t)(Math::round(p_rect.position.x * atlas_width));
1009
screen_rect.position.y = (int32_t)(Math::round(p_rect.position.y * atlas_height));
1010
screen_rect.size.width = (int32_t)(Math::round(p_dst_size.width));
1011
screen_rect.size.height = (int32_t)(Math::round(p_dst_size.height));
1012
1013
CopyToDPPushConstant push_constant;
1014
push_constant.z_far = p_z_far;
1015
push_constant.z_near = p_z_near;
1016
push_constant.texel_size[0] = 1.0f / p_dst_size.width;
1017
push_constant.texel_size[1] = 1.0f / p_dst_size.height;
1018
push_constant.texel_size[0] *= p_dp_flip ? -1.0f : 1.0f; // Encode dp flip as x size sign
1019
1020
// setup our uniforms
1021
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
1022
1023
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
1024
1025
RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);
1026
ERR_FAIL_COND(shader.is_null());
1027
1028
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, screen_rect);
1029
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cube_to_dp.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer)));
1030
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
1031
RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());
1032
1033
RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));
1034
RD::get_singleton()->draw_list_draw(draw_list, true);
1035
RD::get_singleton()->draw_list_end();
1036
}
1037
1038
void CopyEffects::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {
1039
ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap downsample with the mobile renderer.");
1040
1041
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
1042
ERR_FAIL_NULL(uniform_set_cache);
1043
MaterialStorage *material_storage = MaterialStorage::get_singleton();
1044
ERR_FAIL_NULL(material_storage);
1045
1046
cubemap_downsampler.push_constant.face_size = p_size.x;
1047
cubemap_downsampler.push_constant.face_id = 0; // we render all 6 sides to each layer in one call
1048
1049
// setup our uniforms
1050
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
1051
1052
RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));
1053
RD::Uniform u_dest_cubemap(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_cubemap }));
1054
1055
RID shader = cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0);
1056
ERR_FAIL_COND(shader.is_null());
1057
1058
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1059
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.compute_pipeline);
1060
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
1061
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_cubemap), 1);
1062
1063
int x_groups = Math::division_round_up(p_size.x, 8);
1064
int y_groups = Math::division_round_up(p_size.y, 8);
1065
1066
RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
1067
1068
RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face
1069
1070
RD::get_singleton()->compute_list_end();
1071
}
1072
1073
void CopyEffects::cubemap_downsample_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, const Size2i &p_size) {
1074
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap downsample with the clustered renderer.");
1075
ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap downsample must process one side at a time.");
1076
1077
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
1078
ERR_FAIL_NULL(uniform_set_cache);
1079
MaterialStorage *material_storage = MaterialStorage::get_singleton();
1080
ERR_FAIL_NULL(material_storage);
1081
1082
cubemap_downsampler.push_constant.face_size = p_size.x;
1083
cubemap_downsampler.push_constant.face_id = p_face_id;
1084
1085
// setup our uniforms
1086
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
1087
1088
RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));
1089
1090
RID shader = cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0);
1091
ERR_FAIL_COND(shader.is_null());
1092
1093
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
1094
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cubemap_downsampler.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
1095
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
1096
1097
RD::get_singleton()->draw_list_set_push_constant(draw_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));
1098
1099
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
1100
RD::get_singleton()->draw_list_end();
1101
}
1102
1103
void CopyEffects::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {
1104
ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap filter with the mobile renderer.");
1105
1106
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
1107
ERR_FAIL_NULL(uniform_set_cache);
1108
MaterialStorage *material_storage = MaterialStorage::get_singleton();
1109
ERR_FAIL_NULL(material_storage);
1110
1111
Vector<RD::Uniform> uniforms;
1112
for (int i = 0; i < p_dest_cubemap.size(); i++) {
1113
RD::Uniform u;
1114
u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
1115
u.binding = i;
1116
u.append_id(p_dest_cubemap[i]);
1117
uniforms.push_back(u);
1118
}
1119
if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {
1120
RD::get_singleton()->free(filter.image_uniform_set);
1121
}
1122
filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, 0), 2);
1123
1124
// setup our uniforms
1125
RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
1126
1127
RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));
1128
1129
int mode = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;
1130
mode = filter.use_high_quality ? mode : mode + 1;
1131
1132
RID shader = filter.compute_shader.version_get_shader(filter.shader_version, mode);
1133
ERR_FAIL_COND(shader.is_null());
1134
1135
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1136
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.compute_pipelines[mode]);
1137
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
1138
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);
1139
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);
1140
1141
int x_groups = p_use_array ? 1792 : 342; // (128 * 128 * 7) / 64 : (128*128 + 64*64 + 32*32 + 16*16 + 8*8 + 4*4 + 2*2) / 64
1142
1143
RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face
1144
1145
RD::get_singleton()->compute_list_end();
1146
}
1147
1148
void CopyEffects::cubemap_filter_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_mip_level) {
1149
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap filter with the clustered renderer.");
1150
ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap filter must process one side at a time.");
1151
1152
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
1153
ERR_FAIL_NULL(uniform_set_cache);
1154
MaterialStorage *material_storage = MaterialStorage::get_singleton();
1155
ERR_FAIL_NULL(material_storage);
1156
1157
// TODO implement!
1158
CubemapFilterRasterPushConstant push_constant;
1159
push_constant.mip_level = p_mip_level;
1160
push_constant.face_id = p_face_id;
1161
1162
// setup our uniforms
1163
RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
1164
1165
RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));
1166
1167
CubemapFilterMode mode = filter.use_high_quality ? FILTER_MODE_HIGH_QUALITY : FILTER_MODE_LOW_QUALITY;
1168
1169
RID shader = filter.raster_shader.version_get_shader(filter.shader_version, mode);
1170
ERR_FAIL_COND(shader.is_null());
1171
1172
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
1173
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, filter.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
1174
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);
1175
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, filter.uniform_set, 1);
1176
1177
RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CubemapFilterRasterPushConstant));
1178
1179
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
1180
RD::get_singleton()->draw_list_end();
1181
}
1182
1183
void CopyEffects::cubemap_roughness(RID p_source_rd_texture, RID p_dest_texture, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {
1184
ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap roughness with the mobile renderer.");
1185
1186
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
1187
ERR_FAIL_NULL(uniform_set_cache);
1188
MaterialStorage *material_storage = MaterialStorage::get_singleton();
1189
ERR_FAIL_NULL(material_storage);
1190
1191
memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
1192
1193
roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;
1194
// Remap to perceptual-roughness^2 to create more detail in lower mips and match the mapping of cubemap_filter.
1195
roughness.push_constant.roughness = p_roughness * p_roughness;
1196
roughness.push_constant.sample_count = p_sample_count;
1197
roughness.push_constant.use_direct_write = p_roughness == 0.0;
1198
roughness.push_constant.face_size = p_size;
1199
1200
// setup our uniforms
1201
RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
1202
1203
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_rd_texture }));
1204
RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_texture }));
1205
1206
RID shader = roughness.compute_shader.version_get_shader(roughness.shader_version, 0);
1207
ERR_FAIL_COND(shader.is_null());
1208
1209
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
1210
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.compute_pipeline);
1211
1212
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
1213
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_texture), 1);
1214
1215
RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
1216
1217
int x_groups = Math::division_round_up(p_size, 8);
1218
int y_groups = x_groups;
1219
1220
RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);
1221
1222
RD::get_singleton()->compute_list_end();
1223
}
1224
1225
void CopyEffects::cubemap_roughness_raster(RID p_source_rd_texture, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {
1226
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap roughness with the clustered renderer.");
1227
ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap roughness must process one side at a time.");
1228
1229
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
1230
ERR_FAIL_NULL(uniform_set_cache);
1231
MaterialStorage *material_storage = MaterialStorage::get_singleton();
1232
ERR_FAIL_NULL(material_storage);
1233
1234
memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));
1235
1236
roughness.push_constant.face_id = p_face_id;
1237
roughness.push_constant.roughness = p_roughness * p_roughness; // Shader expects roughness, not perceptual roughness, so multiply before passing in.
1238
roughness.push_constant.sample_count = p_sample_count;
1239
roughness.push_constant.use_direct_write = p_roughness == 0.0;
1240
roughness.push_constant.face_size = p_size;
1241
1242
// Setup our uniforms.
1243
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
1244
1245
RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));
1246
1247
RID shader = roughness.raster_shader.version_get_shader(roughness.shader_version, 0);
1248
ERR_FAIL_COND(shader.is_null());
1249
1250
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
1251
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, roughness.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
1252
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);
1253
1254
RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));
1255
1256
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
1257
RD::get_singleton()->draw_list_end();
1258
}
1259
1260
void CopyEffects::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection, uint32_t p_view_count) {
1261
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
1262
ERR_FAIL_NULL(uniform_set_cache);
1263
MaterialStorage *material_storage = MaterialStorage::get_singleton();
1264
ERR_FAIL_NULL(material_storage);
1265
1266
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
1267
1268
RD::get_singleton()->draw_command_begin_label("Merge specular");
1269
1270
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);
1271
1272
int mode;
1273
if (p_reflection.is_valid()) {
1274
if (p_base.is_valid()) {
1275
mode = SPECULAR_MERGE_SSR;
1276
} else {
1277
mode = SPECULAR_MERGE_ADDITIVE_SSR;
1278
}
1279
} else {
1280
if (p_base.is_valid()) {
1281
mode = SPECULAR_MERGE_ADD;
1282
} else {
1283
mode = SPECULAR_MERGE_ADDITIVE_ADD;
1284
}
1285
}
1286
1287
if (p_view_count > 1) {
1288
mode += SPECULAR_MERGE_ADD_MULTIVIEW;
1289
}
1290
1291
RID shader = specular_merge.shader.version_get_shader(specular_merge.shader_version, mode);
1292
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));
1293
1294
if (p_base.is_valid()) {
1295
RD::Uniform u_base(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_base }));
1296
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_base), 2);
1297
}
1298
1299
RD::Uniform u_specular(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_specular }));
1300
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_specular), 0);
1301
1302
if (p_reflection.is_valid()) {
1303
RD::Uniform u_reflection(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_reflection }));
1304
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_reflection), 1);
1305
}
1306
1307
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
1308
RD::get_singleton()->draw_list_end();
1309
1310
RD::get_singleton()->draw_command_end_label();
1311
}
1312
1313