Path: blob/master/servers/rendering/renderer_rd/effects/copy_effects.cpp
10279 views
/**************************************************************************/1/* copy_effects.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 "copy_effects.h"31#include "core/config/project_settings.h"32#include "servers/rendering/renderer_rd/renderer_compositor_rd.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 "thirdparty/misc/cubemap_coeffs.h"3637using namespace RendererRD;3839CopyEffects *CopyEffects::singleton = nullptr;4041CopyEffects *CopyEffects::get_singleton() {42return singleton;43}4445CopyEffects::CopyEffects(bool p_prefer_raster_effects) {46singleton = this;47prefer_raster_effects = p_prefer_raster_effects;4849if (prefer_raster_effects) {50// init blur shader (on compute use copy shader)5152Vector<String> blur_modes;53blur_modes.push_back("\n#define MODE_MIPMAP\n"); // BLUR_MIPMAP54blur_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n"); // BLUR_MODE_GAUSSIAN_BLUR55blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n"); // BLUR_MODE_GAUSSIAN_GLOW56blur_modes.push_back("\n#define MODE_GAUSSIAN_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n"); // BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE57blur_modes.push_back("\n#define MODE_COPY\n"); // BLUR_MODE_COPY58blur_modes.push_back("\n#define MODE_SET_COLOR\n"); // BLUR_MODE_SET_COLOR5960blur_raster.shader.initialize(blur_modes);61memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));62blur_raster.shader_version = blur_raster.shader.version_create();6364for (int i = 0; i < BLUR_MODE_MAX; i++) {65blur_raster.pipelines[i].setup(blur_raster.shader.version_get_shader(blur_raster.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);66}6768} else {69// not used in clustered70for (int i = 0; i < BLUR_MODE_MAX; i++) {71blur_raster.pipelines[i].clear();72}73}7475{76Vector<String> copy_modes;77copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n");78copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define DST_IMAGE_8BIT\n");79copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n");80copy_modes.push_back("\n#define MODE_GAUSSIAN_BLUR\n#define MODE_GLOW\n#define GLOW_USE_AUTO_EXPOSURE\n");81copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n");82copy_modes.push_back("\n#define MODE_SIMPLE_COPY\n#define DST_IMAGE_8BIT\n");83copy_modes.push_back("\n#define MODE_SIMPLE_COPY_DEPTH\n");84copy_modes.push_back("\n#define MODE_SET_COLOR\n");85copy_modes.push_back("\n#define MODE_SET_COLOR\n#define DST_IMAGE_8BIT\n");86copy_modes.push_back("\n#define MODE_MIPMAP\n");87copy_modes.push_back("\n#define MODE_LINEARIZE_DEPTH_COPY\n");88copy_modes.push_back("\n#define MODE_CUBEMAP_TO_PANORAMA\n");89copy_modes.push_back("\n#define MODE_CUBEMAP_ARRAY_TO_PANORAMA\n");9091copy.shader.initialize(copy_modes);92memset(©.push_constant, 0, sizeof(CopyPushConstant));9394copy.shader_version = copy.shader.version_create();9596for (int i = 0; i < COPY_MODE_MAX; i++) {97if (copy.shader.is_variant_enabled(i)) {98copy.pipelines[i] = RD::get_singleton()->compute_pipeline_create(copy.shader.version_get_shader(copy.shader_version, i));99}100}101}102103{104Vector<String> copy_modes;105copy_modes.push_back("\n"); // COPY_TO_FB_COPY106copy_modes.push_back("\n#define MODE_PANORAMA_TO_DP\n"); // COPY_TO_FB_COPY_PANORAMA_TO_DP107copy_modes.push_back("\n#define MODE_TWO_SOURCES\n"); // COPY_TO_FB_COPY2108copy_modes.push_back("\n#define MODE_SET_COLOR\n"); // COPY_TO_FB_SET_COLOR109copy_modes.push_back("\n#define USE_MULTIVIEW\n"); // COPY_TO_FB_MULTIVIEW110copy_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_TWO_SOURCES\n"); // COPY_TO_FB_MULTIVIEW_WITH_DEPTH111112copy_to_fb.shader.initialize(copy_modes);113114if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {115copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW, false);116copy_to_fb.shader.set_variant_enabled(COPY_TO_FB_MULTIVIEW_WITH_DEPTH, false);117}118119copy_to_fb.shader_version = copy_to_fb.shader.version_create();120121//use additive122123for (int i = 0; i < COPY_TO_FB_MAX; i++) {124if (copy_to_fb.shader.is_variant_enabled(i)) {125copy_to_fb.pipelines[i].setup(copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);126} else {127copy_to_fb.pipelines[i].clear();128}129}130}131132{133// Initialize copier134Vector<String> copy_modes;135copy_modes.push_back("\n");136137cube_to_dp.shader.initialize(copy_modes);138139cube_to_dp.shader_version = cube_to_dp.shader.version_create();140RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);141RD::PipelineDepthStencilState dss;142dss.enable_depth_test = true;143dss.depth_compare_operator = RD::COMPARE_OP_ALWAYS;144dss.enable_depth_write = true;145cube_to_dp.pipeline.setup(shader, RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), dss, RD::PipelineColorBlendState(), 0);146}147148{149//Initialize cubemap downsampler150Vector<String> cubemap_downsampler_modes;151cubemap_downsampler_modes.push_back("");152153if (prefer_raster_effects) {154cubemap_downsampler.raster_shader.initialize(cubemap_downsampler_modes);155156cubemap_downsampler.shader_version = cubemap_downsampler.raster_shader.version_create();157158cubemap_downsampler.raster_pipeline.setup(cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);159} else {160cubemap_downsampler.compute_shader.initialize(cubemap_downsampler_modes);161162cubemap_downsampler.shader_version = cubemap_downsampler.compute_shader.version_create();163164cubemap_downsampler.compute_pipeline = RD::get_singleton()->compute_pipeline_create(cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0));165cubemap_downsampler.raster_pipeline.clear();166}167}168169{170// Initialize cubemap filter171filter.use_high_quality = GLOBAL_GET("rendering/reflections/sky_reflections/fast_filter_high_quality");172173Vector<String> cubemap_filter_modes;174cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n");175cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n");176cubemap_filter_modes.push_back("\n#define USE_HIGH_QUALITY\n#define USE_TEXTURE_ARRAY\n");177cubemap_filter_modes.push_back("\n#define USE_LOW_QUALITY\n#define USE_TEXTURE_ARRAY\n");178179if (filter.use_high_quality) {180filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(high_quality_coeffs));181RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(high_quality_coeffs), &high_quality_coeffs[0]);182} else {183filter.coefficient_buffer = RD::get_singleton()->storage_buffer_create(sizeof(low_quality_coeffs));184RD::get_singleton()->buffer_update(filter.coefficient_buffer, 0, sizeof(low_quality_coeffs), &low_quality_coeffs[0]);185}186187if (prefer_raster_effects) {188filter.raster_shader.initialize(cubemap_filter_modes);189190// array variants are not supported in raster191filter.raster_shader.set_variant_enabled(FILTER_MODE_HIGH_QUALITY_ARRAY, false);192filter.raster_shader.set_variant_enabled(FILTER_MODE_LOW_QUALITY_ARRAY, false);193194filter.shader_version = filter.raster_shader.version_create();195196for (int i = 0; i < FILTER_MODE_MAX; i++) {197if (filter.raster_shader.is_variant_enabled(i)) {198filter.raster_pipelines[i].setup(filter.raster_shader.version_get_shader(filter.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);199} else {200filter.raster_pipelines[i].clear();201}202}203204Vector<RD::Uniform> uniforms;205{206RD::Uniform u;207u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;208u.binding = 0;209u.append_id(filter.coefficient_buffer);210uniforms.push_back(u);211}212filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.raster_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);213} else {214filter.compute_shader.initialize(cubemap_filter_modes);215filter.shader_version = filter.compute_shader.version_create();216217for (int i = 0; i < FILTER_MODE_MAX; i++) {218filter.compute_pipelines[i] = RD::get_singleton()->compute_pipeline_create(filter.compute_shader.version_get_shader(filter.shader_version, i));219filter.raster_pipelines[i].clear();220}221222Vector<RD::Uniform> uniforms;223{224RD::Uniform u;225u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;226u.binding = 0;227u.append_id(filter.coefficient_buffer);228uniforms.push_back(u);229}230filter.uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, filter.use_high_quality ? 0 : 1), 1);231}232}233234{235// Initialize roughness236Vector<String> cubemap_roughness_modes;237cubemap_roughness_modes.push_back("");238239if (prefer_raster_effects) {240roughness.raster_shader.initialize(cubemap_roughness_modes);241242roughness.shader_version = roughness.raster_shader.version_create();243244roughness.raster_pipeline.setup(roughness.raster_shader.version_get_shader(roughness.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);245246} else {247roughness.compute_shader.initialize(cubemap_roughness_modes);248249roughness.shader_version = roughness.compute_shader.version_create();250251roughness.compute_pipeline = RD::get_singleton()->compute_pipeline_create(roughness.compute_shader.version_get_shader(roughness.shader_version, 0));252roughness.raster_pipeline.clear();253}254}255256{257Vector<String> specular_modes;258specular_modes.push_back("\n#define MODE_MERGE\n"); // SPECULAR_MERGE_ADD259specular_modes.push_back("\n#define MODE_MERGE\n#define MODE_SSR\n"); // SPECULAR_MERGE_SSR260specular_modes.push_back("\n"); // SPECULAR_MERGE_ADDITIVE_ADD261specular_modes.push_back("\n#define MODE_SSR\n"); // SPECULAR_MERGE_ADDITIVE_SSR262263specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_MERGE\n"); // SPECULAR_MERGE_ADD_MULTIVIEW264specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_MERGE\n#define MODE_SSR\n"); // SPECULAR_MERGE_SSR_MULTIVIEW265specular_modes.push_back("\n#define USE_MULTIVIEW\n"); // SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW266specular_modes.push_back("\n#define USE_MULTIVIEW\n#define MODE_SSR\n"); // SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW267268specular_merge.shader.initialize(specular_modes);269270if (!RendererCompositorRD::get_singleton()->is_xr_enabled()) {271specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADD_MULTIVIEW, false);272specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_SSR_MULTIVIEW, false);273specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW, false);274specular_merge.shader.set_variant_enabled(SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW, false);275}276277specular_merge.shader_version = specular_merge.shader.version_create();278279//use additive280281RD::PipelineColorBlendState::Attachment ba;282ba.enable_blend = true;283ba.src_color_blend_factor = RD::BLEND_FACTOR_ONE;284ba.dst_color_blend_factor = RD::BLEND_FACTOR_ONE;285ba.src_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;286ba.dst_alpha_blend_factor = RD::BLEND_FACTOR_ZERO;287ba.color_blend_op = RD::BLEND_OP_ADD;288ba.alpha_blend_op = RD::BLEND_OP_ADD;289290RD::PipelineColorBlendState blend_additive;291blend_additive.attachments.push_back(ba);292293for (int i = 0; i < SPECULAR_MERGE_MAX; i++) {294if (specular_merge.shader.is_variant_enabled(i)) {295RD::PipelineColorBlendState blend_state;296if (i == SPECULAR_MERGE_ADDITIVE_ADD || i == SPECULAR_MERGE_ADDITIVE_SSR || i == SPECULAR_MERGE_ADDITIVE_ADD_MULTIVIEW || i == SPECULAR_MERGE_ADDITIVE_SSR_MULTIVIEW) {297blend_state = blend_additive;298} else {299blend_state = RD::PipelineColorBlendState::create_disabled();300}301specular_merge.pipelines[i].setup(specular_merge.shader.version_get_shader(specular_merge.shader_version, i), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), blend_state, 0);302}303}304}305}306307CopyEffects::~CopyEffects() {308if (prefer_raster_effects) {309blur_raster.shader.version_free(blur_raster.shader_version);310cubemap_downsampler.raster_shader.version_free(cubemap_downsampler.shader_version);311filter.raster_shader.version_free(filter.shader_version);312roughness.raster_shader.version_free(roughness.shader_version);313} else {314cubemap_downsampler.compute_shader.version_free(cubemap_downsampler.shader_version);315filter.compute_shader.version_free(filter.shader_version);316roughness.compute_shader.version_free(roughness.shader_version);317}318319copy.shader.version_free(copy.shader_version);320specular_merge.shader.version_free(specular_merge.shader_version);321322RD::get_singleton()->free(filter.coefficient_buffer);323324if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {325RD::get_singleton()->free(filter.image_uniform_set);326}327328if (RD::get_singleton()->uniform_set_is_valid(filter.uniform_set)) {329RD::get_singleton()->free(filter.uniform_set);330}331332copy_to_fb.shader.version_free(copy_to_fb.shader_version);333cube_to_dp.shader.version_free(cube_to_dp.shader_version);334335singleton = nullptr;336}337338void CopyEffects::copy_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_all_source, bool p_8_bit_dst, bool p_alpha_to_one) {339UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();340ERR_FAIL_NULL(uniform_set_cache);341MaterialStorage *material_storage = MaterialStorage::get_singleton();342ERR_FAIL_NULL(material_storage);343344memset(©.push_constant, 0, sizeof(CopyPushConstant));345if (p_flip_y) {346copy.push_constant.flags |= COPY_FLAG_FLIP_Y;347}348349if (p_force_luminance) {350copy.push_constant.flags |= COPY_FLAG_FORCE_LUMINANCE;351}352353if (p_all_source) {354copy.push_constant.flags |= COPY_FLAG_ALL_SOURCE;355}356357if (p_alpha_to_one) {358copy.push_constant.flags |= COPY_FLAG_ALPHA_TO_ONE;359}360361copy.push_constant.section[0] = p_rect.position.x;362copy.push_constant.section[1] = p_rect.position.y;363copy.push_constant.section[2] = p_rect.size.width;364copy.push_constant.section[3] = p_rect.size.height;365copy.push_constant.target[0] = p_rect.position.x;366copy.push_constant.target[1] = p_rect.position.y;367368// setup our uniforms369RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);370371RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));372RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);373374CopyMode mode = p_8_bit_dst ? COPY_MODE_SIMPLY_COPY_8BIT : COPY_MODE_SIMPLY_COPY;375RID shader = copy.shader.version_get_shader(copy.shader_version, mode);376ERR_FAIL_COND(shader.is_null());377378RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();379RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);380RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);381RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);382RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant));383RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);384RD::get_singleton()->compute_list_end();385}386387void CopyEffects::copy_cubemap_to_panorama(RID p_source_cube, RID p_dest_panorama, const Size2i &p_panorama_size, float p_lod, bool p_is_array) {388UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();389ERR_FAIL_NULL(uniform_set_cache);390MaterialStorage *material_storage = MaterialStorage::get_singleton();391ERR_FAIL_NULL(material_storage);392393memset(©.push_constant, 0, sizeof(CopyPushConstant));394395copy.push_constant.section[0] = 0;396copy.push_constant.section[1] = 0;397copy.push_constant.section[2] = p_panorama_size.width;398copy.push_constant.section[3] = p_panorama_size.height;399copy.push_constant.target[0] = 0;400copy.push_constant.target[1] = 0;401copy.push_constant.camera_z_far = p_lod;402403// setup our uniforms404RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);405406RD::Uniform u_source_cube(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cube }));407RD::Uniform u_dest_panorama(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_panorama);408409CopyMode mode = p_is_array ? COPY_MODE_CUBE_ARRAY_TO_PANORAMA : COPY_MODE_CUBE_TO_PANORAMA;410RID shader = copy.shader.version_get_shader(copy.shader_version, mode);411ERR_FAIL_COND(shader.is_null());412413RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();414RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);415RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cube), 0);416RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_panorama), 3);417RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant));418RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_panorama_size.width, p_panorama_size.height, 1);419RD::get_singleton()->compute_list_end();420}421422void CopyEffects::copy_depth_to_rect(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y) {423UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();424ERR_FAIL_NULL(uniform_set_cache);425MaterialStorage *material_storage = MaterialStorage::get_singleton();426ERR_FAIL_NULL(material_storage);427428memset(©.push_constant, 0, sizeof(CopyPushConstant));429if (p_flip_y) {430copy.push_constant.flags |= COPY_FLAG_FLIP_Y;431}432433copy.push_constant.section[0] = 0;434copy.push_constant.section[1] = 0;435copy.push_constant.section[2] = p_rect.size.width;436copy.push_constant.section[3] = p_rect.size.height;437copy.push_constant.target[0] = p_rect.position.x;438copy.push_constant.target[1] = p_rect.position.y;439440// setup our uniforms441RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);442443RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));444RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);445446CopyMode mode = COPY_MODE_SIMPLY_COPY_DEPTH;447RID shader = copy.shader.version_get_shader(copy.shader_version, mode);448ERR_FAIL_COND(shader.is_null());449450RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();451RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);452RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);453RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);454RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant));455RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);456RD::get_singleton()->compute_list_end();457}458459void CopyEffects::copy_depth_to_rect_and_linearize(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_rect, bool p_flip_y, float p_z_near, float p_z_far) {460UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();461ERR_FAIL_NULL(uniform_set_cache);462MaterialStorage *material_storage = MaterialStorage::get_singleton();463ERR_FAIL_NULL(material_storage);464465memset(©.push_constant, 0, sizeof(CopyPushConstant));466if (p_flip_y) {467copy.push_constant.flags |= COPY_FLAG_FLIP_Y;468}469470copy.push_constant.section[0] = 0;471copy.push_constant.section[1] = 0;472copy.push_constant.section[2] = p_rect.size.width;473copy.push_constant.section[3] = p_rect.size.height;474copy.push_constant.target[0] = p_rect.position.x;475copy.push_constant.target[1] = p_rect.position.y;476copy.push_constant.camera_z_far = p_z_far;477copy.push_constant.camera_z_near = p_z_near;478479// setup our uniforms480RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);481482RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));483RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);484485CopyMode mode = COPY_MODE_LINEARIZE_DEPTH;486RID shader = copy.shader.version_get_shader(copy.shader_version, mode);487ERR_FAIL_COND(shader.is_null());488489RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();490RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);491RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);492RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);493RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant));494RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_rect.size.width, p_rect.size.height, 1);495RD::get_singleton()->compute_list_end();496}497498void CopyEffects::copy_to_atlas_fb(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2 &p_uv_rect, RD::DrawListID p_draw_list, bool p_flip_y, bool p_panorama) {499UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();500ERR_FAIL_NULL(uniform_set_cache);501MaterialStorage *material_storage = MaterialStorage::get_singleton();502ERR_FAIL_NULL(material_storage);503504memset(©_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));505506copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_USE_SECTION;507copy_to_fb.push_constant.section[0] = p_uv_rect.position.x;508copy_to_fb.push_constant.section[1] = p_uv_rect.position.y;509copy_to_fb.push_constant.section[2] = p_uv_rect.size.x;510copy_to_fb.push_constant.section[3] = p_uv_rect.size.y;511512if (p_flip_y) {513copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FLIP_Y;514}515516copy_to_fb.push_constant.luminance_multiplier = 1.0;517518// setup our uniforms519RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);520521RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));522523CopyToFBMode mode = p_panorama ? COPY_TO_FB_COPY_PANORAMA_TO_DP : COPY_TO_FB_COPY;524RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);525ERR_FAIL_COND(shader.is_null());526527RD::DrawListID draw_list = p_draw_list;528RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));529RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);530RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());531RD::get_singleton()->draw_list_set_push_constant(draw_list, ©_to_fb.push_constant, sizeof(CopyToFbPushConstant));532RD::get_singleton()->draw_list_draw(draw_list, true);533}534535void CopyEffects::copy_to_fb_rect(RID p_source_rd_texture, RID p_dest_framebuffer, const Rect2i &p_rect, bool p_flip_y, bool p_force_luminance, bool p_alpha_to_zero, bool p_srgb, RID p_secondary, bool p_multiview, bool p_alpha_to_one, bool p_linear, bool p_normal, const Rect2 &p_src_rect) {536UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();537ERR_FAIL_NULL(uniform_set_cache);538MaterialStorage *material_storage = MaterialStorage::get_singleton();539ERR_FAIL_NULL(material_storage);540541memset(©_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));542copy_to_fb.push_constant.luminance_multiplier = 1.0;543544if (p_flip_y) {545copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FLIP_Y;546}547if (p_force_luminance) {548copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_FORCE_LUMINANCE;549}550if (p_alpha_to_zero) {551copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_ALPHA_TO_ZERO;552}553if (p_srgb) {554copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_SRGB;555}556if (p_alpha_to_one) {557copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_ALPHA_TO_ONE;558}559if (p_linear) {560// Used for copying to a linear buffer. In the mobile renderer we divide the contents of the linear buffer561// to allow for a wider effective range.562copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_LINEAR;563copy_to_fb.push_constant.luminance_multiplier = prefer_raster_effects ? 2.0 : 1.0;564}565566if (p_normal) {567copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_NORMAL;568}569570if (p_src_rect != Rect2()) {571copy_to_fb.push_constant.section[0] = p_src_rect.position.x;572copy_to_fb.push_constant.section[1] = p_src_rect.position.y;573copy_to_fb.push_constant.section[2] = p_src_rect.size.x;574copy_to_fb.push_constant.section[3] = p_src_rect.size.y;575copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_USE_SRC_SECTION;576}577578// setup our uniforms579RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);580581RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));582583CopyToFBMode mode;584if (p_multiview) {585mode = p_secondary.is_valid() ? COPY_TO_FB_MULTIVIEW_WITH_DEPTH : COPY_TO_FB_MULTIVIEW;586} else {587mode = p_secondary.is_valid() ? COPY_TO_FB_COPY2 : COPY_TO_FB_COPY;588}589590RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);591ERR_FAIL_COND(shader.is_null());592593RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, p_rect);594RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));595RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);596if (p_secondary.is_valid()) {597// TODO may need to do this differently when reading from depth buffer for multiview598RD::Uniform u_secondary(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_secondary }));599RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_secondary), 1);600}601RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());602RD::get_singleton()->draw_list_set_push_constant(draw_list, ©_to_fb.push_constant, sizeof(CopyToFbPushConstant));603RD::get_singleton()->draw_list_draw(draw_list, true);604RD::get_singleton()->draw_list_end();605}606607void CopyEffects::copy_to_drawlist(RD::DrawListID p_draw_list, RD::FramebufferFormatID p_fb_format, RID p_source_rd_texture, bool p_linear) {608UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();609ERR_FAIL_NULL(uniform_set_cache);610MaterialStorage *material_storage = MaterialStorage::get_singleton();611ERR_FAIL_NULL(material_storage);612613memset(©_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));614copy_to_fb.push_constant.luminance_multiplier = 1.0;615616if (p_linear) {617// Used for copying to a linear buffer. In the mobile renderer we divide the contents of the linear buffer618// to allow for a wider effective range.619copy_to_fb.push_constant.flags |= COPY_TO_FB_FLAG_LINEAR;620copy_to_fb.push_constant.luminance_multiplier = prefer_raster_effects ? 2.0 : 1.0;621}622623// setup our uniforms624RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);625626RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));627628// Multiview not supported here!629CopyToFBMode mode = COPY_TO_FB_COPY;630631RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);632ERR_FAIL_COND(shader.is_null());633634RD::get_singleton()->draw_list_bind_render_pipeline(p_draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, p_fb_format));635RD::get_singleton()->draw_list_bind_uniform_set(p_draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);636RD::get_singleton()->draw_list_bind_index_array(p_draw_list, material_storage->get_quad_index_array());637RD::get_singleton()->draw_list_set_push_constant(p_draw_list, ©_to_fb.push_constant, sizeof(CopyToFbPushConstant));638RD::get_singleton()->draw_list_draw(p_draw_list, true);639}640641void CopyEffects::copy_raster(RID p_source_texture, RID p_dest_framebuffer) {642ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the copy with the clustered renderer.");643644UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();645ERR_FAIL_NULL(uniform_set_cache);646MaterialStorage *material_storage = MaterialStorage::get_singleton();647ERR_FAIL_NULL(material_storage);648649memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));650651// setup our uniforms652RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);653654RD::Uniform u_source_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_texture }));655656RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, BLUR_MODE_COPY);657ERR_FAIL_COND(shader.is_null());658659// Just copy it back (we use our blur raster shader here)..660RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);661RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[BLUR_MODE_COPY].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));662RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_texture), 0);663RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));664665RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);666RD::get_singleton()->draw_list_end();667}668669void CopyEffects::gaussian_blur(RID p_source_rd_texture, RID p_texture, const Rect2i &p_region, const Size2i &p_size, bool p_8bit_dst) {670ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian blur with the mobile renderer.");671672UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();673ERR_FAIL_NULL(uniform_set_cache);674MaterialStorage *material_storage = MaterialStorage::get_singleton();675ERR_FAIL_NULL(material_storage);676677memset(©.push_constant, 0, sizeof(CopyPushConstant));678679copy.push_constant.section[0] = p_region.position.x;680copy.push_constant.section[1] = p_region.position.y;681copy.push_constant.target[0] = p_region.position.x;682copy.push_constant.target[1] = p_region.position.y;683copy.push_constant.section[2] = p_size.width;684copy.push_constant.section[3] = p_size.height;685686// setup our uniforms687RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);688689RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));690RD::Uniform u_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_texture);691692CopyMode mode = p_8bit_dst ? COPY_MODE_GAUSSIAN_COPY_8BIT : COPY_MODE_GAUSSIAN_COPY;693RID shader = copy.shader.version_get_shader(copy.shader_version, mode);694ERR_FAIL_COND(shader.is_null());695696RD::DrawListID compute_list = RD::get_singleton()->compute_list_begin();697RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);698RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);699RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_texture), 3);700701RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant));702703RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);704705RD::get_singleton()->compute_list_end();706}707708void CopyEffects::gaussian_blur_raster(RID p_source_rd_texture, RID p_dest_texture, const Rect2i &p_region, const Size2i &p_size) {709ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian blur with the clustered renderer.");710711UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();712ERR_FAIL_NULL(uniform_set_cache);713MaterialStorage *material_storage = MaterialStorage::get_singleton();714ERR_FAIL_NULL(material_storage);715716RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);717718memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));719720BlurRasterMode blur_mode = BLUR_MODE_GAUSSIAN_BLUR;721722blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);723blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);724725// setup our uniforms726RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);727728RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));729730RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);731ERR_FAIL_COND(shader.is_null());732733RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);734RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));735RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);736737RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));738739RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);740RD::get_singleton()->draw_list_end();741}742743void CopyEffects::gaussian_glow(RID p_source_rd_texture, RID p_back_texture, const Size2i &p_size, float p_strength, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_scale) {744ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the gaussian glow with the mobile renderer.");745746UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();747ERR_FAIL_NULL(uniform_set_cache);748MaterialStorage *material_storage = MaterialStorage::get_singleton();749ERR_FAIL_NULL(material_storage);750751memset(©.push_constant, 0, sizeof(CopyPushConstant));752753CopyMode copy_mode = p_first_pass && p_auto_exposure.is_valid() ? COPY_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : COPY_MODE_GAUSSIAN_GLOW;754uint32_t base_flags = 0;755756copy.push_constant.section[2] = p_size.x;757copy.push_constant.section[3] = p_size.y;758759copy.push_constant.glow_strength = p_strength;760copy.push_constant.glow_bloom = p_bloom;761copy.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;762copy.push_constant.glow_hdr_scale = p_hdr_bleed_scale;763copy.push_constant.glow_exposure = p_exposure;764copy.push_constant.glow_white = 0; //actually unused765copy.push_constant.glow_luminance_cap = p_luminance_cap;766767copy.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also768769// setup our uniforms770RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);771772RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));773RD::Uniform u_back_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_back_texture);774775RID shader = copy.shader.version_get_shader(copy.shader_version, copy_mode);776ERR_FAIL_COND(shader.is_null());777778RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();779RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[copy_mode]);780RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);781RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_back_texture), 3);782if (p_auto_exposure.is_valid() && p_first_pass) {783RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));784RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);785}786787copy.push_constant.flags = base_flags | (p_first_pass ? COPY_FLAG_GLOW_FIRST_PASS : 0);788RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant));789790RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);791RD::get_singleton()->compute_list_end();792}793794void CopyEffects::gaussian_glow_raster(RID p_source_rd_texture, RID p_half_texture, RID p_dest_texture, float p_luminance_multiplier, const Size2i &p_size, float p_strength, bool p_first_pass, float p_luminance_cap, float p_exposure, float p_bloom, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, RID p_auto_exposure, float p_auto_exposure_scale) {795ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the gaussian glow with the clustered renderer.");796797UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();798ERR_FAIL_NULL(uniform_set_cache);799MaterialStorage *material_storage = MaterialStorage::get_singleton();800ERR_FAIL_NULL(material_storage);801802RID half_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_half_texture);803RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);804805memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));806807BlurRasterMode blur_mode = p_first_pass && p_auto_exposure.is_valid() ? BLUR_MODE_GAUSSIAN_GLOW_AUTO_EXPOSURE : BLUR_MODE_GAUSSIAN_GLOW;808uint32_t base_flags = 0;809810blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);811blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);812813blur_raster.push_constant.glow_strength = p_strength;814blur_raster.push_constant.glow_bloom = p_bloom;815blur_raster.push_constant.glow_hdr_threshold = p_hdr_bleed_threshold;816blur_raster.push_constant.glow_hdr_scale = p_hdr_bleed_scale;817blur_raster.push_constant.glow_exposure = p_exposure;818blur_raster.push_constant.glow_white = 0; //actually unused819blur_raster.push_constant.glow_luminance_cap = p_luminance_cap;820821blur_raster.push_constant.glow_auto_exposure_scale = p_auto_exposure_scale; //unused also822823blur_raster.push_constant.luminance_multiplier = p_luminance_multiplier;824825// setup our uniforms826RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);827828RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));829RD::Uniform u_half_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_half_texture }));830831RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);832ERR_FAIL_COND(shader.is_null());833834//HORIZONTAL835RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(half_framebuffer);836RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(half_framebuffer)));837RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);838if (p_auto_exposure.is_valid() && p_first_pass) {839RD::Uniform u_auto_exposure(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_auto_exposure }));840RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_auto_exposure), 1);841}842843blur_raster.push_constant.flags = base_flags | BLUR_FLAG_HORIZONTAL | (p_first_pass ? BLUR_FLAG_GLOW_FIRST_PASS : 0);844RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));845846RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);847RD::get_singleton()->draw_list_end();848849blur_mode = BLUR_MODE_GAUSSIAN_GLOW;850851shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, blur_mode);852ERR_FAIL_COND(shader.is_null());853854//VERTICAL855draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);856RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[blur_mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));857RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_half_texture), 0);858859blur_raster.push_constant.flags = base_flags;860RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));861862RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);863RD::get_singleton()->draw_list_end();864}865866void CopyEffects::make_mipmap(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {867ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the make_mipmap shader with the mobile renderer.");868869UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();870ERR_FAIL_NULL(uniform_set_cache);871MaterialStorage *material_storage = MaterialStorage::get_singleton();872ERR_FAIL_NULL(material_storage);873874memset(©.push_constant, 0, sizeof(CopyPushConstant));875876copy.push_constant.section[0] = 0;877copy.push_constant.section[1] = 0;878copy.push_constant.section[2] = p_size.width;879copy.push_constant.section[3] = p_size.height;880881// setup our uniforms882RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);883884RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));885RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);886887CopyMode mode = COPY_MODE_MIPMAP;888RID shader = copy.shader.version_get_shader(copy.shader_version, mode);889ERR_FAIL_COND(shader.is_null());890891RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();892RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);893RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);894RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);895RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant));896RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_size.width, p_size.height, 1);897RD::get_singleton()->compute_list_end();898}899900void CopyEffects::make_mipmap_raster(RID p_source_rd_texture, RID p_dest_texture, const Size2i &p_size) {901ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of mipmap with the clustered renderer.");902903RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);904905UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();906ERR_FAIL_NULL(uniform_set_cache);907MaterialStorage *material_storage = MaterialStorage::get_singleton();908ERR_FAIL_NULL(material_storage);909910memset(&blur_raster.push_constant, 0, sizeof(BlurRasterPushConstant));911912BlurRasterMode mode = BLUR_MIPMAP;913914blur_raster.push_constant.pixel_size[0] = 1.0 / float(p_size.x);915blur_raster.push_constant.pixel_size[1] = 1.0 / float(p_size.y);916917// setup our uniforms918RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);919920RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));921922RID shader = blur_raster.shader.version_get_shader(blur_raster.shader_version, mode);923ERR_FAIL_COND(shader.is_null());924925RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer);926RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, blur_raster.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));927RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);928RD::get_singleton()->draw_list_set_push_constant(draw_list, &blur_raster.push_constant, sizeof(BlurRasterPushConstant));929930RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);931RD::get_singleton()->draw_list_end();932}933934void CopyEffects::set_color(RID p_dest_texture, const Color &p_color, const Rect2i &p_region, bool p_8bit_dst) {935ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use the compute version of the set_color shader with the mobile renderer.");936937UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();938ERR_FAIL_NULL(uniform_set_cache);939940memset(©.push_constant, 0, sizeof(CopyPushConstant));941942copy.push_constant.section[0] = 0;943copy.push_constant.section[1] = 0;944copy.push_constant.section[2] = p_region.size.width;945copy.push_constant.section[3] = p_region.size.height;946copy.push_constant.target[0] = p_region.position.x;947copy.push_constant.target[1] = p_region.position.y;948copy.push_constant.set_color[0] = p_color.r;949copy.push_constant.set_color[1] = p_color.g;950copy.push_constant.set_color[2] = p_color.b;951copy.push_constant.set_color[3] = p_color.a;952953// setup our uniforms954RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, p_dest_texture);955956CopyMode mode = p_8bit_dst ? COPY_MODE_SET_COLOR_8BIT : COPY_MODE_SET_COLOR;957RID shader = copy.shader.version_get_shader(copy.shader_version, mode);958ERR_FAIL_COND(shader.is_null());959960RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();961RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, copy.pipelines[mode]);962RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 3, u_dest_texture), 3);963RD::get_singleton()->compute_list_set_push_constant(compute_list, ©.push_constant, sizeof(CopyPushConstant));964RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_region.size.width, p_region.size.height, 1);965RD::get_singleton()->compute_list_end();966}967968void CopyEffects::set_color_raster(RID p_dest_texture, const Color &p_color, const Rect2i &p_region) {969ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use the raster version of the set_color shader with the clustered renderer.");970971UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();972ERR_FAIL_NULL(uniform_set_cache);973MaterialStorage *material_storage = MaterialStorage::get_singleton();974ERR_FAIL_NULL(material_storage);975976memset(©_to_fb.push_constant, 0, sizeof(CopyToFbPushConstant));977978copy_to_fb.push_constant.set_color[0] = p_color.r;979copy_to_fb.push_constant.set_color[1] = p_color.g;980copy_to_fb.push_constant.set_color[2] = p_color.b;981copy_to_fb.push_constant.set_color[3] = p_color.a;982983RID dest_framebuffer = FramebufferCacheRD::get_singleton()->get_cache(p_dest_texture);984985CopyToFBMode mode = COPY_TO_FB_SET_COLOR;986987RID shader = copy_to_fb.shader.version_get_shader(copy_to_fb.shader_version, mode);988ERR_FAIL_COND(shader.is_null());989990RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(dest_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, p_region);991RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, copy_to_fb.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(dest_framebuffer)));992RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());993RD::get_singleton()->draw_list_set_push_constant(draw_list, ©_to_fb.push_constant, sizeof(CopyToFbPushConstant));994RD::get_singleton()->draw_list_draw(draw_list, true);995RD::get_singleton()->draw_list_end();996}997998void CopyEffects::copy_cubemap_to_dp(RID p_source_rd_texture, RID p_dst_framebuffer, const Rect2 &p_rect, const Vector2 &p_dst_size, float p_z_near, float p_z_far, bool p_dp_flip) {999UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();1000ERR_FAIL_NULL(uniform_set_cache);1001MaterialStorage *material_storage = MaterialStorage::get_singleton();1002ERR_FAIL_NULL(material_storage);10031004Rect2i screen_rect;1005float atlas_width = p_dst_size.width / p_rect.size.width;1006float atlas_height = p_dst_size.height / p_rect.size.height;1007screen_rect.position.x = (int32_t)(Math::round(p_rect.position.x * atlas_width));1008screen_rect.position.y = (int32_t)(Math::round(p_rect.position.y * atlas_height));1009screen_rect.size.width = (int32_t)(Math::round(p_dst_size.width));1010screen_rect.size.height = (int32_t)(Math::round(p_dst_size.height));10111012CopyToDPPushConstant push_constant;1013push_constant.z_far = p_z_far;1014push_constant.z_near = p_z_near;1015push_constant.texel_size[0] = 1.0f / p_dst_size.width;1016push_constant.texel_size[1] = 1.0f / p_dst_size.height;1017push_constant.texel_size[0] *= p_dp_flip ? -1.0f : 1.0f; // Encode dp flip as x size sign10181019// setup our uniforms1020RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);10211022RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));10231024RID shader = cube_to_dp.shader.version_get_shader(cube_to_dp.shader_version, 0);1025ERR_FAIL_COND(shader.is_null());10261027RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dst_framebuffer, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, screen_rect);1028RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cube_to_dp.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dst_framebuffer)));1029RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);1030RD::get_singleton()->draw_list_bind_index_array(draw_list, material_storage->get_quad_index_array());10311032RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CopyToDPPushConstant));1033RD::get_singleton()->draw_list_draw(draw_list, true);1034RD::get_singleton()->draw_list_end();1035}10361037void CopyEffects::cubemap_downsample(RID p_source_cubemap, RID p_dest_cubemap, const Size2i &p_size) {1038ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap downsample with the mobile renderer.");10391040UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();1041ERR_FAIL_NULL(uniform_set_cache);1042MaterialStorage *material_storage = MaterialStorage::get_singleton();1043ERR_FAIL_NULL(material_storage);10441045cubemap_downsampler.push_constant.face_size = p_size.x;1046cubemap_downsampler.push_constant.face_id = 0; // we render all 6 sides to each layer in one call10471048// setup our uniforms1049RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);10501051RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));1052RD::Uniform u_dest_cubemap(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_cubemap }));10531054RID shader = cubemap_downsampler.compute_shader.version_get_shader(cubemap_downsampler.shader_version, 0);1055ERR_FAIL_COND(shader.is_null());10561057RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();1058RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, cubemap_downsampler.compute_pipeline);1059RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);1060RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_cubemap), 1);10611062int x_groups = Math::division_round_up(p_size.x, 8);1063int y_groups = Math::division_round_up(p_size.y, 8);10641065RD::get_singleton()->compute_list_set_push_constant(compute_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));10661067RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, 6); // one z_group for each face10681069RD::get_singleton()->compute_list_end();1070}10711072void CopyEffects::cubemap_downsample_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, const Size2i &p_size) {1073ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap downsample with the clustered renderer.");1074ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap downsample must process one side at a time.");10751076UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();1077ERR_FAIL_NULL(uniform_set_cache);1078MaterialStorage *material_storage = MaterialStorage::get_singleton();1079ERR_FAIL_NULL(material_storage);10801081cubemap_downsampler.push_constant.face_size = p_size.x;1082cubemap_downsampler.push_constant.face_id = p_face_id;10831084// setup our uniforms1085RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);10861087RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_cubemap }));10881089RID shader = cubemap_downsampler.raster_shader.version_get_shader(cubemap_downsampler.shader_version, 0);1090ERR_FAIL_COND(shader.is_null());10911092RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);1093RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, cubemap_downsampler.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));1094RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);10951096RD::get_singleton()->draw_list_set_push_constant(draw_list, &cubemap_downsampler.push_constant, sizeof(CubemapDownsamplerPushConstant));10971098RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);1099RD::get_singleton()->draw_list_end();1100}11011102void CopyEffects::cubemap_filter(RID p_source_cubemap, Vector<RID> p_dest_cubemap, bool p_use_array) {1103ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap filter with the mobile renderer.");11041105UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();1106ERR_FAIL_NULL(uniform_set_cache);1107MaterialStorage *material_storage = MaterialStorage::get_singleton();1108ERR_FAIL_NULL(material_storage);11091110Vector<RD::Uniform> uniforms;1111for (int i = 0; i < p_dest_cubemap.size(); i++) {1112RD::Uniform u;1113u.uniform_type = RD::UNIFORM_TYPE_IMAGE;1114u.binding = i;1115u.append_id(p_dest_cubemap[i]);1116uniforms.push_back(u);1117}1118if (RD::get_singleton()->uniform_set_is_valid(filter.image_uniform_set)) {1119RD::get_singleton()->free(filter.image_uniform_set);1120}1121filter.image_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, filter.compute_shader.version_get_shader(filter.shader_version, 0), 2);11221123// setup our uniforms1124RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);11251126RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));11271128int mode = p_use_array ? FILTER_MODE_HIGH_QUALITY_ARRAY : FILTER_MODE_HIGH_QUALITY;1129mode = filter.use_high_quality ? mode : mode + 1;11301131RID shader = filter.compute_shader.version_get_shader(filter.shader_version, mode);1132ERR_FAIL_COND(shader.is_null());11331134RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();1135RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, filter.compute_pipelines[mode]);1136RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);1137RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.uniform_set, 1);1138RD::get_singleton()->compute_list_bind_uniform_set(compute_list, filter.image_uniform_set, 2);11391140int x_groups = p_use_array ? 1792 : 342; // (128 * 128 * 7) / 64 : (128*128 + 64*64 + 32*32 + 16*16 + 8*8 + 4*4 + 2*2) / 6411411142RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, 6, 1); // one y_group for each face11431144RD::get_singleton()->compute_list_end();1145}11461147void CopyEffects::cubemap_filter_raster(RID p_source_cubemap, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_mip_level) {1148ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap filter with the clustered renderer.");1149ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap filter must process one side at a time.");11501151UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();1152ERR_FAIL_NULL(uniform_set_cache);1153MaterialStorage *material_storage = MaterialStorage::get_singleton();1154ERR_FAIL_NULL(material_storage);11551156// TODO implement!1157CubemapFilterRasterPushConstant push_constant;1158push_constant.mip_level = p_mip_level;1159push_constant.face_id = p_face_id;11601161// setup our uniforms1162RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);11631164RD::Uniform u_source_cubemap(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_cubemap }));11651166CubemapFilterMode mode = filter.use_high_quality ? FILTER_MODE_HIGH_QUALITY : FILTER_MODE_LOW_QUALITY;11671168RID shader = filter.raster_shader.version_get_shader(filter.shader_version, mode);1169ERR_FAIL_COND(shader.is_null());11701171RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);1172RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, filter.raster_pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));1173RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_cubemap), 0);1174RD::get_singleton()->draw_list_bind_uniform_set(draw_list, filter.uniform_set, 1);11751176RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(CubemapFilterRasterPushConstant));11771178RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);1179RD::get_singleton()->draw_list_end();1180}11811182void CopyEffects::cubemap_roughness(RID p_source_rd_texture, RID p_dest_texture, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {1183ERR_FAIL_COND_MSG(prefer_raster_effects, "Can't use compute based cubemap roughness with the mobile renderer.");11841185UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();1186ERR_FAIL_NULL(uniform_set_cache);1187MaterialStorage *material_storage = MaterialStorage::get_singleton();1188ERR_FAIL_NULL(material_storage);11891190memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));11911192roughness.push_constant.face_id = p_face_id > 9 ? 0 : p_face_id;1193// Remap to perceptual-roughness^2 to create more detail in lower mips and match the mapping of cubemap_filter.1194roughness.push_constant.roughness = p_roughness * p_roughness;1195roughness.push_constant.sample_count = p_sample_count;1196roughness.push_constant.use_direct_write = p_roughness == 0.0;1197roughness.push_constant.face_size = p_size;11981199// setup our uniforms1200RID default_mipmap_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);12011202RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_mipmap_sampler, p_source_rd_texture }));1203RD::Uniform u_dest_texture(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_dest_texture }));12041205RID shader = roughness.compute_shader.version_get_shader(roughness.shader_version, 0);1206ERR_FAIL_COND(shader.is_null());12071208RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();1209RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, roughness.compute_pipeline);12101211RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);1212RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 1, u_dest_texture), 1);12131214RD::get_singleton()->compute_list_set_push_constant(compute_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));12151216int x_groups = Math::division_round_up(p_size, 8);1217int y_groups = x_groups;12181219RD::get_singleton()->compute_list_dispatch(compute_list, x_groups, y_groups, p_face_id > 9 ? 6 : 1);12201221RD::get_singleton()->compute_list_end();1222}12231224void CopyEffects::cubemap_roughness_raster(RID p_source_rd_texture, RID p_dest_framebuffer, uint32_t p_face_id, uint32_t p_sample_count, float p_roughness, float p_size) {1225ERR_FAIL_COND_MSG(!prefer_raster_effects, "Can't use raster based cubemap roughness with the clustered renderer.");1226ERR_FAIL_COND_MSG(p_face_id >= 6, "Raster implementation of cubemap roughness must process one side at a time.");12271228UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();1229ERR_FAIL_NULL(uniform_set_cache);1230MaterialStorage *material_storage = MaterialStorage::get_singleton();1231ERR_FAIL_NULL(material_storage);12321233memset(&roughness.push_constant, 0, sizeof(CubemapRoughnessPushConstant));12341235roughness.push_constant.face_id = p_face_id;1236roughness.push_constant.roughness = p_roughness * p_roughness; // Shader expects roughness, not perceptual roughness, so multiply before passing in.1237roughness.push_constant.sample_count = p_sample_count;1238roughness.push_constant.use_direct_write = p_roughness == 0.0;1239roughness.push_constant.face_size = p_size;12401241// Setup our uniforms.1242RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);12431244RD::Uniform u_source_rd_texture(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_source_rd_texture }));12451246RID shader = roughness.raster_shader.version_get_shader(roughness.shader_version, 0);1247ERR_FAIL_COND(shader.is_null());12481249RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);1250RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, roughness.raster_pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));1251RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_rd_texture), 0);12521253RD::get_singleton()->draw_list_set_push_constant(draw_list, &roughness.push_constant, sizeof(CubemapRoughnessPushConstant));12541255RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);1256RD::get_singleton()->draw_list_end();1257}12581259void CopyEffects::merge_specular(RID p_dest_framebuffer, RID p_specular, RID p_base, RID p_reflection, uint32_t p_view_count) {1260UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();1261ERR_FAIL_NULL(uniform_set_cache);1262MaterialStorage *material_storage = MaterialStorage::get_singleton();1263ERR_FAIL_NULL(material_storage);12641265RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);12661267RD::get_singleton()->draw_command_begin_label("Merge specular");12681269RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_framebuffer);12701271int mode;1272if (p_reflection.is_valid()) {1273if (p_base.is_valid()) {1274mode = SPECULAR_MERGE_SSR;1275} else {1276mode = SPECULAR_MERGE_ADDITIVE_SSR;1277}1278} else {1279if (p_base.is_valid()) {1280mode = SPECULAR_MERGE_ADD;1281} else {1282mode = SPECULAR_MERGE_ADDITIVE_ADD;1283}1284}12851286if (p_view_count > 1) {1287mode += SPECULAR_MERGE_ADD_MULTIVIEW;1288}12891290RID shader = specular_merge.shader.version_get_shader(specular_merge.shader_version, mode);1291RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, specular_merge.pipelines[mode].get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_framebuffer)));12921293if (p_base.is_valid()) {1294RD::Uniform u_base(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_base }));1295RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 2, u_base), 2);1296}12971298RD::Uniform u_specular(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_specular }));1299RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_specular), 0);13001301if (p_reflection.is_valid()) {1302RD::Uniform u_reflection(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_reflection }));1303RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 1, u_reflection), 1);1304}13051306RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);1307RD::get_singleton()->draw_list_end();13081309RD::get_singleton()->draw_command_end_label();1310}131113121313