Path: blob/master/servers/rendering/renderer_rd/effects/bokeh_dof.cpp
10279 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] = RD::get_singleton()->compute_pipeline_create(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() {79if (prefer_raster_effects) {80bokeh.raster_shader.version_free(bokeh.shader_version);81} else {82bokeh.compute_shader.version_free(bokeh.shader_version);83}84}8586void BokehDOF::bokeh_dof_compute(const BokehBuffers &p_buffers, RID p_camera_attributes, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {87ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute version of bokeh depth of field with the mobile renderer.");8889UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();90ERR_FAIL_NULL(uniform_set_cache);91MaterialStorage *material_storage = MaterialStorage::get_singleton();92ERR_FAIL_NULL(material_storage);9394bool dof_far = RSG::camera_attributes->camera_attributes_get_dof_far_enabled(p_camera_attributes);95float dof_far_begin = RSG::camera_attributes->camera_attributes_get_dof_far_distance(p_camera_attributes);96float dof_far_size = RSG::camera_attributes->camera_attributes_get_dof_far_transition(p_camera_attributes);97bool dof_near = RSG::camera_attributes->camera_attributes_get_dof_near_enabled(p_camera_attributes);98float dof_near_begin = RSG::camera_attributes->camera_attributes_get_dof_near_distance(p_camera_attributes);99float dof_near_size = RSG::camera_attributes->camera_attributes_get_dof_near_transition(p_camera_attributes);100float bokeh_size = RSG::camera_attributes->camera_attributes_get_dof_blur_amount(p_camera_attributes) * 64; // Base 64 pixel radius.101102bool use_jitter = RSG::camera_attributes->camera_attributes_get_dof_blur_use_jitter();103RS::DOFBokehShape bokeh_shape = RSG::camera_attributes->camera_attributes_get_dof_blur_bokeh_shape();104RS::DOFBlurQuality blur_quality = RSG::camera_attributes->camera_attributes_get_dof_blur_quality();105106// setup our push constant107memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));108bokeh.push_constant.blur_far_active = dof_far;109bokeh.push_constant.blur_far_begin = dof_far_begin;110bokeh.push_constant.blur_far_end = dof_far_begin + dof_far_size; // Only used with non-physically-based.111bokeh.push_constant.use_physical_far = dof_far_size < 0.0;112bokeh.push_constant.blur_size_far = bokeh_size; // Only used with physically-based.113114bokeh.push_constant.blur_near_active = dof_near;115bokeh.push_constant.blur_near_begin = dof_near_begin;116bokeh.push_constant.blur_near_end = dof_near_begin - dof_near_size; // Only used with non-physically-based.117bokeh.push_constant.use_physical_near = dof_near_size < 0.0;118bokeh.push_constant.blur_size_near = bokeh_size; // Only used with physically-based.119120bokeh.push_constant.use_jitter = use_jitter;121bokeh.push_constant.jitter_seed = Math::randf() * 1000.0;122123bokeh.push_constant.z_near = p_cam_znear;124bokeh.push_constant.z_far = p_cam_zfar;125bokeh.push_constant.orthogonal = p_cam_orthogonal;126bokeh.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.127128bokeh.push_constant.second_pass = false;129bokeh.push_constant.half_size = false;130131bokeh.push_constant.blur_scale = 0.5;132133// setup our uniforms134RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);135136RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));137RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));138RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));139RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));140RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));141142RD::Uniform u_base_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.base_texture);143RD::Uniform u_secondary_image(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.secondary_texture);144RD::Uniform u_half_image0(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[0]);145RD::Uniform u_half_image1(RD::UNIFORM_TYPE_IMAGE, 0, p_buffers.half_texture[1]);146147RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();148149/* FIRST PASS */150// The alpha channel of the source color texture is filled with the expected circle size151// If used for DOF far, the size is positive, if used for near, its negative.152153RID shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);154ERR_FAIL_COND(shader.is_null());155156RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BLUR_SIZE]);157158RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);159RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_depth_texture), 1);160161bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;162bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;163164RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));165166RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);167RD::get_singleton()->compute_list_add_barrier(compute_list);168169if (bokeh_shape == RS::DOF_BOKEH_BOX || bokeh_shape == RS::DOF_BOKEH_HEXAGON) {170//second pass171BokehMode mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;172shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, mode);173ERR_FAIL_COND(shader.is_null());174175RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[mode]);176177static const int quality_samples[4] = { 6, 12, 12, 24 };178179bokeh.push_constant.steps = quality_samples[blur_quality];180181if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {182//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)183184RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);185RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);186187bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;188bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;189bokeh.push_constant.half_size = true;190bokeh.push_constant.blur_size *= 0.5;191192} else {193//medium and high quality use full size194RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_secondary_image), 0);195RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);196}197198RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));199200RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);201RD::get_singleton()->compute_list_add_barrier(compute_list);202203//third pass204bokeh.push_constant.second_pass = true;205206if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {207RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image1), 0);208RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);209} else {210RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);211RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_secondary_texture), 1);212}213214RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));215216RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);217RD::get_singleton()->compute_list_add_barrier(compute_list);218219if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {220//forth pass, upscale for low quality221222shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);223ERR_FAIL_COND(shader.is_null());224225RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);226227RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);228RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture1), 1);229230bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;231bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;232bokeh.push_constant.half_size = false;233bokeh.push_constant.second_pass = false;234235RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));236237RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);238}239} else {240//circle241242shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BOKEH_CIRCULAR);243ERR_FAIL_COND(shader.is_null());244245//second pass246RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_GEN_BOKEH_CIRCULAR]);247248static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };249250bokeh.push_constant.steps = 0;251bokeh.push_constant.blur_scale = quality_scale[blur_quality];252253//circle always runs in half size, otherwise too expensive254255RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_half_image0), 0);256RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_base_texture), 1);257258bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;259bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;260bokeh.push_constant.half_size = true;261262RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));263264RD::get_singleton()->compute_list_dispatch_threads(compute_list, bokeh.push_constant.size[0], bokeh.push_constant.size[1], 1);265RD::get_singleton()->compute_list_add_barrier(compute_list);266267//circle is just one pass, then upscale268269// upscale270271shader = bokeh.compute_shader.version_get_shader(bokeh.shader_version, BOKEH_COMPOSITE);272ERR_FAIL_COND(shader.is_null());273274RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, bokeh.compute_pipelines[BOKEH_COMPOSITE]);275276RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_base_image), 0);277RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_half_texture0), 1);278279bokeh.push_constant.size[0] = p_buffers.base_texture_size.x;280bokeh.push_constant.size[1] = p_buffers.base_texture_size.y;281bokeh.push_constant.half_size = false;282bokeh.push_constant.second_pass = false;283284RD::get_singleton()->compute_list_set_push_constant(compute_list, &bokeh.push_constant, sizeof(BokehPushConstant));285286RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_buffers.base_texture_size.x, p_buffers.base_texture_size.y, 1);287}288289RD::get_singleton()->compute_list_end();290}291292void BokehDOF::bokeh_dof_raster(const BokehBuffers &p_buffers, RID p_camera_attributes, float p_cam_znear, float p_cam_zfar, bool p_cam_orthogonal) {293ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't blur-based depth of field with the clustered renderer.");294295UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();296ERR_FAIL_NULL(uniform_set_cache);297MaterialStorage *material_storage = MaterialStorage::get_singleton();298ERR_FAIL_NULL(material_storage);299300bool dof_far = RSG::camera_attributes->camera_attributes_get_dof_far_enabled(p_camera_attributes);301float dof_far_begin = RSG::camera_attributes->camera_attributes_get_dof_far_distance(p_camera_attributes);302float dof_far_size = RSG::camera_attributes->camera_attributes_get_dof_far_transition(p_camera_attributes);303bool dof_near = RSG::camera_attributes->camera_attributes_get_dof_near_enabled(p_camera_attributes);304float dof_near_begin = RSG::camera_attributes->camera_attributes_get_dof_near_distance(p_camera_attributes);305float dof_near_size = RSG::camera_attributes->camera_attributes_get_dof_near_transition(p_camera_attributes);306float bokeh_size = RSG::camera_attributes->camera_attributes_get_dof_blur_amount(p_camera_attributes) * 64; // Base 64 pixel radius.307308RS::DOFBokehShape bokeh_shape = RSG::camera_attributes->camera_attributes_get_dof_blur_bokeh_shape();309RS::DOFBlurQuality blur_quality = RSG::camera_attributes->camera_attributes_get_dof_blur_quality();310311// setup our base push constant312memset(&bokeh.push_constant, 0, sizeof(BokehPushConstant));313314bokeh.push_constant.orthogonal = p_cam_orthogonal;315bokeh.push_constant.size[0] = p_buffers.base_texture_size.width;316bokeh.push_constant.size[1] = p_buffers.base_texture_size.height;317bokeh.push_constant.z_far = p_cam_zfar;318bokeh.push_constant.z_near = p_cam_znear;319320bokeh.push_constant.second_pass = false;321bokeh.push_constant.half_size = false;322bokeh.push_constant.blur_size = bokeh_size;323324// setup our uniforms325RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);326327RD::Uniform u_base_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.base_texture }));328RD::Uniform u_depth_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.depth_texture }));329RD::Uniform u_secondary_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.secondary_texture }));330RD::Uniform u_half_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[0] }));331RD::Uniform u_half_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.half_texture[1] }));332RD::Uniform u_weight_texture0(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[0] }));333RD::Uniform u_weight_texture1(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[1] }));334RD::Uniform u_weight_texture2(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[2] }));335RD::Uniform u_weight_texture3(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_buffers.weight_texture[3] }));336337if (dof_far || dof_near) {338if (dof_far) {339bokeh.push_constant.blur_far_active = true;340bokeh.push_constant.blur_far_begin = dof_far_begin;341bokeh.push_constant.blur_far_end = dof_far_begin + dof_far_size;342}343344if (dof_near) {345bokeh.push_constant.blur_near_active = true;346bokeh.push_constant.blur_near_begin = dof_near_begin;347bokeh.push_constant.blur_near_end = dof_near_begin - dof_near_size;348}349350{351// generate our depth data352RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, BOKEH_GEN_BLUR_SIZE);353ERR_FAIL_COND(shader.is_null());354355RID framebuffer = p_buffers.base_weight_fb;356RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);357RD::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)));358RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_depth_texture), 0);359360RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));361362RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);363RD::get_singleton()->draw_list_end();364}365366if (bokeh_shape == RS::DOF_BOKEH_BOX || bokeh_shape == RS::DOF_BOKEH_HEXAGON) {367// double pass approach368BokehMode mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX : BOKEH_GEN_BOKEH_HEXAGONAL;369370RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);371ERR_FAIL_COND(shader.is_null());372373if (blur_quality == RS::DOF_BLUR_QUALITY_VERY_LOW || blur_quality == RS::DOF_BLUR_QUALITY_LOW) {374//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)375bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;376bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;377bokeh.push_constant.half_size = true;378bokeh.push_constant.blur_size *= 0.5;379}380381static const int quality_samples[4] = { 6, 12, 12, 24 };382bokeh.push_constant.blur_scale = 0.5;383bokeh.push_constant.steps = quality_samples[blur_quality];384385RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;386387// Pass 1388RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);389RD::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)));390RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);391RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);392393RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));394395RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);396RD::get_singleton()->draw_list_end();397398// Pass 2399if (!bokeh.push_constant.half_size) {400// do not output weight, we're writing back into our base buffer401mode = bokeh_shape == RS::DOF_BOKEH_BOX ? BOKEH_GEN_BOKEH_BOX_NOWEIGHT : BOKEH_GEN_BOKEH_HEXAGONAL_NOWEIGHT;402403shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);404ERR_FAIL_COND(shader.is_null());405}406bokeh.push_constant.second_pass = true;407408framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[1] : p_buffers.base_fb;409RD::Uniform texture = bokeh.push_constant.half_size ? u_half_texture0 : u_secondary_texture;410RD::Uniform weight = bokeh.push_constant.half_size ? u_weight_texture2 : u_weight_texture1;411412draw_list = RD::get_singleton()->draw_list_begin(framebuffer);413RD::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)));414RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, texture), 0);415RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, weight), 1);416417RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));418419RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);420RD::get_singleton()->draw_list_end();421422if (bokeh.push_constant.half_size) {423// Compose pass424mode = BOKEH_COMPOSITE;425shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);426ERR_FAIL_COND(shader.is_null());427428framebuffer = p_buffers.base_fb;429430draw_list = RD::get_singleton()->draw_list_begin(framebuffer);431RD::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)));432RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture1), 0);433RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture3), 1);434RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);435436RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));437438RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);439RD::get_singleton()->draw_list_end();440}441442} else {443// circular is a single pass approach444BokehMode mode = BOKEH_GEN_BOKEH_CIRCULAR;445446RID shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);447ERR_FAIL_COND(shader.is_null());448449{450// circle always runs in half size, otherwise too expensive (though the code below does support making this optional)451bokeh.push_constant.size[0] = p_buffers.base_texture_size.x >> 1;452bokeh.push_constant.size[1] = p_buffers.base_texture_size.y >> 1;453bokeh.push_constant.half_size = true;454// bokeh.push_constant.blur_size *= 0.5;455}456457static const float quality_scale[4] = { 8.0, 4.0, 1.0, 0.5 };458bokeh.push_constant.blur_scale = quality_scale[blur_quality];459bokeh.push_constant.steps = 0.0;460461RID framebuffer = bokeh.push_constant.half_size ? p_buffers.half_fb[0] : p_buffers.secondary_fb;462463RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(framebuffer);464RD::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)));465RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_base_texture), 0);466RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture0), 1);467468RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));469470RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);471RD::get_singleton()->draw_list_end();472473if (bokeh.push_constant.half_size) {474// Compose475mode = BOKEH_COMPOSITE;476shader = bokeh.raster_shader.version_get_shader(bokeh.shader_version, mode);477ERR_FAIL_COND(shader.is_null());478479framebuffer = p_buffers.base_fb;480481draw_list = RD::get_singleton()->draw_list_begin(framebuffer);482RD::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)));483RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture0), 0);484RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_weight_texture2), 1);485RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_weight_texture0), 2);486487RD::get_singleton()->draw_list_set_push_constant(draw_list, &bokeh.push_constant, sizeof(BokehPushConstant));488489RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);490RD::get_singleton()->draw_list_end();491} else {492CopyEffects::get_singleton()->copy_raster(p_buffers.secondary_texture, p_buffers.base_fb);493}494}495}496}497498499