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