Path: blob/master/servers/rendering/renderer_rd/effects/bokeh_dof.cpp
20997 views
/**************************************************************************/1/* bokeh_dof.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "bokeh_dof.h"31#include "copy_effects.h"32#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"33#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"34#include "servers/rendering/storage/camera_attributes_storage.h"3536using namespace RendererRD;3738BokehDOF::BokehDOF(bool p_prefer_raster_effects) {39prefer_raster_effects = p_prefer_raster_effects;4041// Initialize bokeh42Vector<String> bokeh_modes;43bokeh_modes.push_back("\n#define MODE_GEN_BLUR_SIZE\n");44bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n#define OUTPUT_WEIGHT\n");45bokeh_modes.push_back("\n#define MODE_BOKEH_BOX\n");46bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n#define OUTPUT_WEIGHT\n");47bokeh_modes.push_back("\n#define MODE_BOKEH_HEXAGONAL\n");48bokeh_modes.push_back("\n#define MODE_BOKEH_CIRCULAR\n#define OUTPUT_WEIGHT\n");49bokeh_modes.push_back("\n#define MODE_COMPOSITE_BOKEH\n");50if (prefer_raster_effects) {51bokeh.raster_shader.initialize(bokeh_modes);5253bokeh.shader_version = bokeh.raster_shader.version_create();5455const int att_count[BOKEH_MAX] = { 1, 2, 1, 2, 1, 2, 1 };56for (int i = 0; i < BOKEH_MAX; i++) {57RD::PipelineColorBlendState blend_state = (i == BOKEH_COMPOSITE) ? RD::PipelineColorBlendState::create_blend(att_count[i]) : RD::PipelineColorBlendState::create_disabled(att_count[i]);58bokeh.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);59}60} else {61bokeh.compute_shader.initialize(bokeh_modes);62bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_BOX_NOWEIGHT, false);63bokeh.compute_shader.set_variant_enabled(BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT, false);64bokeh.shader_version = bokeh.compute_shader.version_create();6566for (int i = 0; i < BOKEH_MAX; i++) {67if (bokeh.compute_shader.is_variant_enabled(i)) {68bokeh.compute_pipelines[i].create_compute_pipeline(bokeh.compute_shader.version_get_shader(bokeh.shader_version, i));69}70}7172for (int i = 0; i < BOKEH_MAX; i++) {73bokeh.raster_pipelines[i].clear();74}75}76}7778BokehDOF::~BokehDOF() {79for (int i = 0; i < BOKEH_MAX; i++) {80bokeh.compute_pipelines[i].free();81}8283if (prefer_raster_effects) {84bokeh.raster_shader.version_free(bokeh.shader_version);85} else {86bokeh.compute_shader.version_free(bokeh.shader_version);87}88}8990void BokehDOF::bokeh_dof_compute(const BokehBuffers &p_buffers, RID p_camera_attributes, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {91ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of bokeh depth of field with the mobile renderer.");9293UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();94ERR_FAIL_NULL(uniform_set_cache);95MaterialStorage *material_storage = MaterialStorage::get_singleton();96ERR_FAIL_NULL(material_storage);9798bool dof_far = RSG::camera_attributes->camera_attributes_get_dof_far_enabled(p_camera_attributes);99float dof_far_begin = RSG::camera_attributes->camera_attributes_get_dof_far_distance(p_camera_attributes);100float dof_far_size = RSG::camera_attributes->camera_attributes_get_dof_far_transition(p_camera_attributes);101bool dof_near = RSG::camera_attributes->camera_attributes_get_dof_near_enabled(p_camera_attributes);102float dof_near_begin = RSG::camera_attributes->camera_attributes_get_dof_near_distance(p_camera_attributes);103float dof_near_size = RSG::camera_attributes->camera_attributes_get_dof_near_transition(p_camera_attributes);104float bokeh_size = RSG::camera_attributes->camera_attributes_get_dof_blur_amount(p_camera_attributes) * 64; // Base 64 pixel radius.105106bool use_jitter = RSG::camera_attributes->camera_attributes_get_dof_blur_use_jitter();107RS::DOFBokehShape bokeh_shape = RSG::camera_attributes->camera_attributes_get_dof_blur_bokeh_shape();108RS::DOFBlurQuality blur_quality = RSG::camera_attributes->camera_attributes_get_dof_blur_quality();109110// setup our push constant111memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));112bokeh.push_constant.blur_far_active = dof_far;113bokeh.push_constant.blur_far_begin = dof_far_begin;114bokeh.push_constant.blur_far_end = dof_far_begin + dof_far_size; // Only used with non-physically-based.115bokeh.push_constant.use_physical_far = dof_far_size < 0.0;116bokeh.push_constant.blur_size_far = bokeh_size; // Only used with physically-based.117118bokeh.push_constant.blur_near_active = dof_near;119bokeh.push_constant.blur_near_begin = dof_near_begin;120bokeh.push_constant.blur_near_end = dof_near_begin - dof_near_size; // Only used with non-physically-based.121bokeh.push_constant.use_physical_near = dof_near_size < 0.0;122bokeh.push_constant.blur_size_near = bokeh_size; // Only used with physically-based.123124bokeh.push_constant.use_jitter = use_jitter;125bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;126127bokeh.push_constant.z_near = p_cam_znear;128bokeh.push_constant.z_far = p_cam_zfar;129bokeh.push_constant.orthogonal = p_cam_orthogonal;130bokeh.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.131132bokeh.push_constant.second_pass = false;133bokeh.push_constant.half_size = false;134135bokeh.push_constant.blur_scale = 0.5;136137// setup our uniforms138RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);139140RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));141RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));142RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));143RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));144RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));145146RD::Uniform u_base_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.base_texture);147RD::Uniform u_secondary_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.secondary_texture);148RD::Uniform u_half_image0(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[0]);149RD::Uniform u_half_image1(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[1]);150151RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();152153/* FIRST PASS */154// The alpha channel of the source color texture is filled with the expected circle size155// If used for DOF far, the size is positive, if used for near, its negative.156157RID shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);158ERR_FAIL_COND(shader.is_null());159160RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BLUR_SIZE].get_rid());161162RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);163RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth_texture), 1);164165bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;166bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;167168RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));169170RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);171RD::get_singleton()->compute_list_add_barrier(compute_list);172173if (bokeh_shape == RS::DOF_BOKEH_BOX || bokeh_shape == RS::DOF_BOKEH_HEXAGON) {174//second pass175BokehMode mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;176shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, mode);177ERR_FAIL_COND(shader.is_null());178179RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[mode].get_rid());180181static const int quality_samples[4] = { 6, 12, 12, 24 };182183bokeh.push_constant.steps = quality_samples[blur_quality];184185if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {186//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)187188RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);189RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);190191bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;192bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;193bokeh.push_constant.half_size = true;194bokeh.push_constant.blur_size *= 0.5;195196} else {197//medium and high quality use full size198RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_secondary_image), 0);199RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);200}201202RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));203204RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);205RD::get_singleton()->compute_list_add_barrier(compute_list);206207//third pass208bokeh.push_constant.second_pass = true;209210if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {211RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image1), 0);212RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);213} else {214RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);215RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_secondary_texture), 1);216}217218RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));219220RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);221RD::get_singleton()->compute_list_add_barrier(compute_list);222223if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {224//forth pass, upscale for low quality225226shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);227ERR_FAIL_COND(shader.is_null());228229RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE].get_rid());230231RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);232RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture1), 1);233234bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;235bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;236bokeh.push_constant.half_size = false;237bokeh.push_constant.second_pass = false;238239RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));240241RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);242}243} else {244//circle245246shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BOKEH_CIRCULAR);247ERR_FAIL_COND(shader.is_null());248249//second pass250RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BOKEH_CIRCULAR].get_rid());251252static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };253254bokeh.push_constant.steps = 0;255bokeh.push_constant.blur_scale = quality_scale[blur_quality];256257//circle always runs in half size, otherwise too expensive258259RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);260RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);261262bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;263bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;264bokeh.push_constant.half_size = true;265266RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));267268RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);269RD::get_singleton()->compute_list_add_barrier(compute_list);270271//circle is just one pass, then upscale272273// upscale274275shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);276ERR_FAIL_COND(shader.is_null());277278RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE].get_rid());279280RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);281RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);282283bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;284bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;285bokeh.push_constant.half_size = false;286bokeh.push_constant.second_pass = false;287288RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));289290RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);291}292293RD::get_singleton()->compute_list_end();294}295296void BokehDOF::bokeh_dof_raster(const BokehBuffers &p_buffers, RID p_camera_attributes, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {297ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't blur-based depth of field with the clustered renderer.");298299UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();300ERR_FAIL_NULL(uniform_set_cache);301MaterialStorage *material_storage = MaterialStorage::get_singleton();302ERR_FAIL_NULL(material_storage);303304bool dof_far = RSG::camera_attributes->camera_attributes_get_dof_far_enabled(p_camera_attributes);305float dof_far_begin = RSG::camera_attributes->camera_attributes_get_dof_far_distance(p_camera_attributes);306float dof_far_size = RSG::camera_attributes->camera_attributes_get_dof_far_transition(p_camera_attributes);307bool dof_near = RSG::camera_attributes->camera_attributes_get_dof_near_enabled(p_camera_attributes);308float dof_near_begin = RSG::camera_attributes->camera_attributes_get_dof_near_distance(p_camera_attributes);309float dof_near_size = RSG::camera_attributes->camera_attributes_get_dof_near_transition(p_camera_attributes);310float bokeh_size = RSG::camera_attributes->camera_attributes_get_dof_blur_amount(p_camera_attributes) * 64; // Base 64 pixel radius.311312RS::DOFBokehShape bokeh_shape = RSG::camera_attributes->camera_attributes_get_dof_blur_bokeh_shape();313RS::DOFBlurQuality blur_quality = RSG::camera_attributes->camera_attributes_get_dof_blur_quality();314315// setup our base push constant316memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));317318bokeh.push_constant.orthogonal = p_cam_orthogonal;319bokeh.push_constant.size[0] = p_buffers.base_texture_size.width;320bokeh.push_constant.size[1] = p_buffers.base_texture_size.height;321bokeh.push_constant.z_far = p_cam_zfar;322bokeh.push_constant.z_near = p_cam_znear;323324bokeh.push_constant.second_pass = false;325bokeh.push_constant.half_size = false;326bokeh.push_constant.blur_size = bokeh_size;327328// setup our uniforms329RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);330331RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));332RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));333RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));334RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));335RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));336RD::Uniform u_weight_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[0] }));337RD::Uniform u_weight_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[1] }));338RD::Uniform u_weight_texture2(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[2] }));339RD::Uniform u_weight_texture3(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[3] }));340341if (dof_far || dof_near) {342if (dof_far) {343bokeh.push_constant.blur_far_active = true;344bokeh.push_constant.blur_far_begin = dof_far_begin;345bokeh.push_constant.blur_far_end = dof_far_begin + dof_far_size;346}347348if (dof_near) {349bokeh.push_constant.blur_near_active = true;350bokeh.push_constant.blur_near_begin = dof_near_begin;351bokeh.push_constant.blur_near_end = dof_near_begin - dof_near_size;352}353354{355// generate our depth data356RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);357ERR_FAIL_COND(shader.is_null());358359RID framebuffer = p_buffers.base_weight_fb;360RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);361RD::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)));362RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_depth_texture), 0);363364RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));365366RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);367RD::get_singleton()->draw_list_end();368}369370if (bokeh_shape == RS::DOF_BOKEH_BOX || bokeh_shape == RS::DOF_BOKEH_HEXAGON) {371// double pass approach372BokehMode mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;373374RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);375ERR_FAIL_COND(shader.is_null());376377if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {378//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)379bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;380bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;381bokeh.push_constant.half_size = true;382bokeh.push_constant.blur_size *= 0.5;383}384385static const int quality_samples[4] = { 6, 12, 12, 24 };386bokeh.push_constant.blur_scale = 0.5;387bokeh.push_constant.steps = quality_samples[blur_quality];388389RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;390391// Pass 1392RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);393RD::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)));394RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);395RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);396397RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));398399RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);400RD::get_singleton()->draw_list_end();401402// Pass 2403if (!bokeh.push_constant.half_size) {404// do not output weight, we're writing back into our base buffer405mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX_NOWEIGHT : BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT;406407shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);408ERR_FAIL_COND(shader.is_null());409}410bokeh.push_constant.second_pass = true;411412framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[1] : p_buffers.base_fb;413RD::Uniform texture = bokeh.push_constant.half_size ? u_half_texture0 : u_secondary_texture;414RD::Uniform weight = bokeh.push_constant.half_size ? u_weight_texture2 : u_weight_texture1;415416draw_list = RD::get_singleton()->draw_list_begin(framebuffer);417RD::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)));418RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, texture), 0);419RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, weight), 1);420421RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));422423RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);424RD::get_singleton()->draw_list_end();425426if (bokeh.push_constant.half_size) {427// Compose pass428mode = BOKEH_COMPOSITE;429shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);430ERR_FAIL_COND(shader.is_null());431432framebuffer = p_buffers.base_fb;433434draw_list = RD::get_singleton()->draw_list_begin(framebuffer);435RD::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)));436RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture1), 0);437RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture3), 1);438RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);439440RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));441442RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);443RD::get_singleton()->draw_list_end();444}445446} else {447// circular is a single pass approach448BokehMode mode = BOKEH_GEN_BOKEH_CIRCULAR;449450RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);451ERR_FAIL_COND(shader.is_null());452453{454// circle always runs in half size, otherwise too expensive (though the code below does support making this optional)455bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;456bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;457bokeh.push_constant.half_size = true;458// bokeh.push_constant.blur_size *= 0.5;459}460461static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };462bokeh.push_constant.blur_scale = quality_scale[blur_quality];463bokeh.push_constant.steps = 0.0;464465RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;466467RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);468RD::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)));469RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);470RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);471472RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));473474RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);475RD::get_singleton()->draw_list_end();476477if (bokeh.push_constant.half_size) {478// Compose479mode = BOKEH_COMPOSITE;480shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);481ERR_FAIL_COND(shader.is_null());482483framebuffer = p_buffers.base_fb;484485draw_list = RD::get_singleton()->draw_list_begin(framebuffer);486RD::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)));487RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture0), 0);488RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture2), 1);489RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);490491RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));492493RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);494RD::get_singleton()->draw_list_end();495} else {496CopyEffects::get_singleton()->copy_raster(p_buffers.secondary_texture, p_buffers.base_fb);497}498}499}500}501502503