Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/effects/bokeh_dof.cpp
10279 views
1
/**************************************************************************/
2
/* bokeh_dof.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 "bokeh_dof.h"
32
#include "copy_effects.h"
33
#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
34
#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
35
#include "servers/rendering/storage/camera_attributes_storage.h"
36
37
using namespace RendererRD;
38
39
BokehDOF::BokehDOF(bool p_prefer_raster_effects) {
40
prefer_raster_effects = p_prefer_raster_effects;
41
42
// Initialize bokeh
43
Vector<String> bokeh_modes;
44
bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");
45
bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n#define OUTPUT_WEIGHT\n");
46
bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");
47
bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n#define OUTPUT_WEIGHT\n");
48
bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");
49
bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n#define OUTPUT_WEIGHT\n");
50
bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");
51
if (prefer_raster_effects) {
52
bokeh.raster_shader.initialize(bokeh_modes);
53
54
bokeh.shader_version = bokeh.raster_shader.version_create();
55
56
const int att_count[BOKEH_MAX] = { 1, 2, 1, 2, 1, 2, 1 };
57
for (int i = 0; i < BOKEH_MAX; i++) {
58
RD::PipelineColorBlendState blend_state = (i == BOKEH_COMPOSITE) ? RD::PipelineColorBlendState::create_blend(att_count[i]) : RD::PipelineColorBlendState::create_disabled(att_count[i]);
59
bokeh.raster_pipelines[i].setup(bokeh.raster_shader.version_get_shader(bokeh.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);
60
}
61
} else {
62
bokeh.compute_shader.initialize(bokeh_modes);
63
bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_BOX_NOWEIGHT, false);
64
bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT, false);
65
bokeh.shader_version = bokeh.compute_shader.version_create();
66
67
for (int i = 0; i < BOKEH_MAX; i++) {
68
if (bokeh.compute_shader.is_variant_enabled(i)) {
69
bokeh.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(bokeh.compute_shader.version_get_shader(bokeh.shader_version, i));
70
}
71
}
72
73
for (int i = 0; i < BOKEH_MAX; i++) {
74
bokeh.raster_pipelines[i].clear();
75
}
76
}
77
}
78
79
BokehDOF::~BokehDOF() {
80
if (prefer_raster_effects) {
81
bokeh.raster_shader.version_free(bokeh.shader_version);
82
} else {
83
bokeh.compute_shader.version_free(bokeh.shader_version);
84
}
85
}
86
87
void BokehDOF::bokeh_dof_compute(const BokehBuffers &p_buffers, RID p_camera_attributes, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
88
ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of bokeh depth of field with the mobile renderer.");
89
90
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
91
ERR_FAIL_NULL(uniform_set_cache);
92
MaterialStorage *material_storage = MaterialStorage::get_singleton();
93
ERR_FAIL_NULL(material_storage);
94
95
bool dof_far = RSG::camera_attributes->camera_attributes_get_dof_far_enabled(p_camera_attributes);
96
float dof_far_begin = RSG::camera_attributes->camera_attributes_get_dof_far_distance(p_camera_attributes);
97
float dof_far_size = RSG::camera_attributes->camera_attributes_get_dof_far_transition(p_camera_attributes);
98
bool dof_near = RSG::camera_attributes->camera_attributes_get_dof_near_enabled(p_camera_attributes);
99
float dof_near_begin = RSG::camera_attributes->camera_attributes_get_dof_near_distance(p_camera_attributes);
100
float dof_near_size = RSG::camera_attributes->camera_attributes_get_dof_near_transition(p_camera_attributes);
101
float bokeh_size = RSG::camera_attributes->camera_attributes_get_dof_blur_amount(p_camera_attributes) * 64; // Base 64 pixel radius.
102
103
bool use_jitter = RSG::camera_attributes->camera_attributes_get_dof_blur_use_jitter();
104
RS::DOFBokehShape bokeh_shape = RSG::camera_attributes->camera_attributes_get_dof_blur_bokeh_shape();
105
RS::DOFBlurQuality blur_quality = RSG::camera_attributes->camera_attributes_get_dof_blur_quality();
106
107
// setup our push constant
108
memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));
109
bokeh.push_constant.blur_far_active = dof_far;
110
bokeh.push_constant.blur_far_begin = dof_far_begin;
111
bokeh.push_constant.blur_far_end = dof_far_begin + dof_far_size; // Only used with non-physically-based.
112
bokeh.push_constant.use_physical_far = dof_far_size < 0.0;
113
bokeh.push_constant.blur_size_far = bokeh_size; // Only used with physically-based.
114
115
bokeh.push_constant.blur_near_active = dof_near;
116
bokeh.push_constant.blur_near_begin = dof_near_begin;
117
bokeh.push_constant.blur_near_end = dof_near_begin - dof_near_size; // Only used with non-physically-based.
118
bokeh.push_constant.use_physical_near = dof_near_size < 0.0;
119
bokeh.push_constant.blur_size_near = bokeh_size; // Only used with physically-based.
120
121
bokeh.push_constant.use_jitter = use_jitter;
122
bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;
123
124
bokeh.push_constant.z_near = p_cam_znear;
125
bokeh.push_constant.z_far = p_cam_zfar;
126
bokeh.push_constant.orthogonal = p_cam_orthogonal;
127
bokeh.push_constant.blur_size = (dof_near_size < 0.0 && dof_far_size < 0.0) ? 32 : bokeh_size; // Cap with physically-based to keep performance reasonable.
128
129
bokeh.push_constant.second_pass = false;
130
bokeh.push_constant.half_size = false;
131
132
bokeh.push_constant.blur_scale = 0.5;
133
134
// setup our uniforms
135
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
136
137
RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));
138
RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));
139
RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));
140
RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));
141
RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));
142
143
RD::Uniform u_base_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.base_texture);
144
RD::Uniform u_secondary_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.secondary_texture);
145
RD::Uniform u_half_image0(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[0]);
146
RD::Uniform u_half_image1(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[1]);
147
148
RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
149
150
/* FIRST PASS */
151
// The alpha channel of the source color texture is filled with the expected circle size
152
// If used for DOF far, the size is positive, if used for near, its negative.
153
154
RID shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);
155
ERR_FAIL_COND(shader.is_null());
156
157
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BLUR_SIZE]);
158
159
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
160
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth_texture), 1);
161
162
bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
163
bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
164
165
RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
166
167
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
168
RD::get_singleton()->compute_list_add_barrier(compute_list);
169
170
if (bokeh_shape == RS::DOF_BOKEH_BOX || bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
171
//second pass
172
BokehMode mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;
173
shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, mode);
174
ERR_FAIL_COND(shader.is_null());
175
176
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[mode]);
177
178
static const int quality_samples[4] = { 6, 12, 12, 24 };
179
180
bokeh.push_constant.steps = quality_samples[blur_quality];
181
182
if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {
183
//box and hexagon are more or less the same, and they can work in either half (very low and low quality) or full (medium and high quality_ sizes)
184
185
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);
186
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
187
188
bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
189
bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
190
bokeh.push_constant.half_size = true;
191
bokeh.push_constant.blur_size *= 0.5;
192
193
} else {
194
//medium and high quality use full size
195
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_secondary_image), 0);
196
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
197
}
198
199
RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
200
201
RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
202
RD::get_singleton()->compute_list_add_barrier(compute_list);
203
204
//third pass
205
bokeh.push_constant.second_pass = true;
206
207
if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {
208
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image1), 0);
209
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);
210
} else {
211
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
212
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_secondary_texture), 1);
213
}
214
215
RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
216
217
RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
218
RD::get_singleton()->compute_list_add_barrier(compute_list);
219
220
if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {
221
//forth pass, upscale for low quality
222
223
shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);
224
ERR_FAIL_COND(shader.is_null());
225
226
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);
227
228
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
229
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture1), 1);
230
231
bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
232
bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
233
bokeh.push_constant.half_size = false;
234
bokeh.push_constant.second_pass = false;
235
236
RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
237
238
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
239
}
240
} else {
241
//circle
242
243
shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BOKEH_CIRCULAR);
244
ERR_FAIL_COND(shader.is_null());
245
246
//second pass
247
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);
248
249
static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
250
251
bokeh.push_constant.steps = 0;
252
bokeh.push_constant.blur_scale = quality_scale[blur_quality];
253
254
//circle always runs in half size, otherwise too expensive
255
256
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);
257
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);
258
259
bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
260
bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
261
bokeh.push_constant.half_size = true;
262
263
RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
264
265
RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);
266
RD::get_singleton()->compute_list_add_barrier(compute_list);
267
268
//circle is just one pass, then upscale
269
270
// upscale
271
272
shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);
273
ERR_FAIL_COND(shader.is_null());
274
275
RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);
276
277
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);
278
RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);
279
280
bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;
281
bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;
282
bokeh.push_constant.half_size = false;
283
bokeh.push_constant.second_pass = false;
284
285
RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));
286
287
RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);
288
}
289
290
RD::get_singleton()->compute_list_end();
291
}
292
293
void BokehDOF::bokeh_dof_raster(const BokehBuffers &p_buffers, RID p_camera_attributes, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {
294
ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't blur-based depth of field with the clustered renderer.");
295
296
UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
297
ERR_FAIL_NULL(uniform_set_cache);
298
MaterialStorage *material_storage = MaterialStorage::get_singleton();
299
ERR_FAIL_NULL(material_storage);
300
301
bool dof_far = RSG::camera_attributes->camera_attributes_get_dof_far_enabled(p_camera_attributes);
302
float dof_far_begin = RSG::camera_attributes->camera_attributes_get_dof_far_distance(p_camera_attributes);
303
float dof_far_size = RSG::camera_attributes->camera_attributes_get_dof_far_transition(p_camera_attributes);
304
bool dof_near = RSG::camera_attributes->camera_attributes_get_dof_near_enabled(p_camera_attributes);
305
float dof_near_begin = RSG::camera_attributes->camera_attributes_get_dof_near_distance(p_camera_attributes);
306
float dof_near_size = RSG::camera_attributes->camera_attributes_get_dof_near_transition(p_camera_attributes);
307
float bokeh_size = RSG::camera_attributes->camera_attributes_get_dof_blur_amount(p_camera_attributes) * 64; // Base 64 pixel radius.
308
309
RS::DOFBokehShape bokeh_shape = RSG::camera_attributes->camera_attributes_get_dof_blur_bokeh_shape();
310
RS::DOFBlurQuality blur_quality = RSG::camera_attributes->camera_attributes_get_dof_blur_quality();
311
312
// setup our base push constant
313
memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));
314
315
bokeh.push_constant.orthogonal = p_cam_orthogonal;
316
bokeh.push_constant.size[0] = p_buffers.base_texture_size.width;
317
bokeh.push_constant.size[1] = p_buffers.base_texture_size.height;
318
bokeh.push_constant.z_far = p_cam_zfar;
319
bokeh.push_constant.z_near = p_cam_znear;
320
321
bokeh.push_constant.second_pass = false;
322
bokeh.push_constant.half_size = false;
323
bokeh.push_constant.blur_size = bokeh_size;
324
325
// setup our uniforms
326
RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
327
328
RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));
329
RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));
330
RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));
331
RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));
332
RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));
333
RD::Uniform u_weight_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[0] }));
334
RD::Uniform u_weight_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[1] }));
335
RD::Uniform u_weight_texture2(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[2] }));
336
RD::Uniform u_weight_texture3(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[3] }));
337
338
if (dof_far || dof_near) {
339
if (dof_far) {
340
bokeh.push_constant.blur_far_active = true;
341
bokeh.push_constant.blur_far_begin = dof_far_begin;
342
bokeh.push_constant.blur_far_end = dof_far_begin + dof_far_size;
343
}
344
345
if (dof_near) {
346
bokeh.push_constant.blur_near_active = true;
347
bokeh.push_constant.blur_near_begin = dof_near_begin;
348
bokeh.push_constant.blur_near_end = dof_near_begin - dof_near_size;
349
}
350
351
{
352
// generate our depth data
353
RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);
354
ERR_FAIL_COND(shader.is_null());
355
356
RID framebuffer = p_buffers.base_weight_fb;
357
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
358
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[BOKEH_GEN_BLUR_SIZE].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
359
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_depth_texture), 0);
360
361
RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
362
363
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
364
RD::get_singleton()->draw_list_end();
365
}
366
367
if (bokeh_shape == RS::DOF_BOKEH_BOX || bokeh_shape == RS::DOF_BOKEH_HEXAGON) {
368
// double pass approach
369
BokehMode mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;
370
371
RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
372
ERR_FAIL_COND(shader.is_null());
373
374
if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {
375
//box and hexagon are more or less the same, and they can work in either half (very low and low quality) or full (medium and high quality_ sizes)
376
bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
377
bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
378
bokeh.push_constant.half_size = true;
379
bokeh.push_constant.blur_size *= 0.5;
380
}
381
382
static const int quality_samples[4] = { 6, 12, 12, 24 };
383
bokeh.push_constant.blur_scale = 0.5;
384
bokeh.push_constant.steps = quality_samples[blur_quality];
385
386
RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
387
388
// Pass 1
389
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
390
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
391
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);
392
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);
393
394
RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
395
396
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
397
RD::get_singleton()->draw_list_end();
398
399
// Pass 2
400
if (!bokeh.push_constant.half_size) {
401
// do not output weight, we're writing back into our base buffer
402
mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX_NOWEIGHT : BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT;
403
404
shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
405
ERR_FAIL_COND(shader.is_null());
406
}
407
bokeh.push_constant.second_pass = true;
408
409
framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[1] : p_buffers.base_fb;
410
RD::Uniform texture = bokeh.push_constant.half_size ? u_half_texture0 : u_secondary_texture;
411
RD::Uniform weight = bokeh.push_constant.half_size ? u_weight_texture2 : u_weight_texture1;
412
413
draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
414
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
415
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, texture), 0);
416
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, weight), 1);
417
418
RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
419
420
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
421
RD::get_singleton()->draw_list_end();
422
423
if (bokeh.push_constant.half_size) {
424
// Compose pass
425
mode = BOKEH_COMPOSITE;
426
shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
427
ERR_FAIL_COND(shader.is_null());
428
429
framebuffer = p_buffers.base_fb;
430
431
draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
432
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
433
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture1), 0);
434
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture3), 1);
435
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);
436
437
RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
438
439
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
440
RD::get_singleton()->draw_list_end();
441
}
442
443
} else {
444
// circular is a single pass approach
445
BokehMode mode = BOKEH_GEN_BOKEH_CIRCULAR;
446
447
RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
448
ERR_FAIL_COND(shader.is_null());
449
450
{
451
// circle always runs in half size, otherwise too expensive (though the code below does support making this optional)
452
bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;
453
bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;
454
bokeh.push_constant.half_size = true;
455
// bokeh.push_constant.blur_size *= 0.5;
456
}
457
458
static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };
459
bokeh.push_constant.blur_scale = quality_scale[blur_quality];
460
bokeh.push_constant.steps = 0.0;
461
462
RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;
463
464
RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
465
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
466
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);
467
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);
468
469
RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
470
471
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
472
RD::get_singleton()->draw_list_end();
473
474
if (bokeh.push_constant.half_size) {
475
// Compose
476
mode = BOKEH_COMPOSITE;
477
shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);
478
ERR_FAIL_COND(shader.is_null());
479
480
framebuffer = p_buffers.base_fb;
481
482
draw_list = RD::get_singleton()->draw_list_begin(framebuffer);
483
RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, bokeh.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(framebuffer)));
484
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture0), 0);
485
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture2), 1);
486
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);
487
488
RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));
489
490
RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
491
RD::get_singleton()->draw_list_end();
492
} else {
493
CopyEffects::get_singleton()->copy_raster(p_buffers.secondary_texture, p_buffers.base_fb);
494
}
495
}
496
}
497
}
498
499