2019-11-05 11:01:00 +00:00
/*************************************************************************/
2020-12-04 18:26:24 +00:00
/* renderer_scene_render_rd.cpp */
2019-11-05 11:01:00 +00:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
2022-01-03 20:27:34 +00:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2019-11-05 11:01:00 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2020-12-04 18:26:24 +00:00
# include "renderer_scene_render_rd.h"
2020-02-12 08:59:06 +00:00
2020-11-07 22:33:38 +00:00
# include "core/config/project_settings.h"
2019-09-07 01:51:27 +00:00
# include "core/os/os.h"
2020-12-04 18:26:24 +00:00
# include "renderer_compositor_rd.h"
2022-06-21 00:08:33 +00:00
# include "servers/rendering/renderer_rd/environment/fog.h"
2022-03-21 11:25:25 +00:00
# include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
# include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
2020-12-03 21:09:47 +00:00
# include "servers/rendering/rendering_server_default.h"
2022-07-31 23:20:24 +00:00
# include "servers/rendering/storage/camera_attributes_storage.h"
2020-07-01 12:18:13 +00:00
2020-04-10 09:30:36 +00:00
void get_vogel_disk ( float * r_kernel , int p_sample_count ) {
const float golden_angle = 2.4 ;
for ( int i = 0 ; i < p_sample_count ; i + + ) {
float r = Math : : sqrt ( float ( i ) + 0.5 ) / Math : : sqrt ( float ( p_sample_count ) ) ;
float theta = float ( i ) * golden_angle ;
r_kernel [ i * 4 ] = Math : : cos ( theta ) * r ;
r_kernel [ i * 4 + 1 ] = Math : : sin ( theta ) * r ;
}
}
2022-08-04 08:40:39 +00:00
void RendererSceneRenderRD : : sdfgi_update ( const Ref < RenderSceneBuffers > & p_render_buffers , RID p_environment , const Vector3 & p_world_position ) {
Ref < RenderSceneBuffersRD > rb = p_render_buffers ;
ERR_FAIL_COND ( rb . is_null ( ) ) ;
Ref < RendererRD : : GI : : SDFGI > sdfgi ;
if ( rb - > has_custom_data ( RB_SCOPE_SDFGI ) ) {
sdfgi = rb - > get_custom_data ( RB_SCOPE_SDFGI ) ;
}
2022-07-27 06:14:23 +00:00
bool needs_sdfgi = p_environment . is_valid ( ) & & environment_get_sdfgi_enabled ( p_environment ) ;
2020-06-25 13:33:28 +00:00
if ( ! needs_sdfgi ) {
2022-08-04 08:40:39 +00:00
if ( sdfgi . is_valid ( ) ) {
// delete it
sdfgi . unref ( ) ;
rb - > set_custom_data ( RB_SCOPE_SDFGI , sdfgi ) ;
2020-06-25 13:33:28 +00:00
}
return ;
}
static const uint32_t history_frames_to_converge [ RS : : ENV_SDFGI_CONVERGE_MAX ] = { 5 , 10 , 15 , 20 , 25 , 30 } ;
2021-02-13 12:08:08 +00:00
uint32_t requested_history_size = history_frames_to_converge [ gi . sdfgi_frames_to_converge ] ;
2020-06-25 13:33:28 +00:00
2022-08-04 08:40:39 +00:00
if ( sdfgi . is_valid ( ) & & ( sdfgi - > num_cascades ! = environment_get_sdfgi_cascades ( p_environment ) | | sdfgi - > min_cell_size ! = environment_get_sdfgi_min_cell_size ( p_environment ) | | requested_history_size ! = sdfgi - > history_size | | sdfgi - > uses_occlusion ! = environment_get_sdfgi_use_occlusion ( p_environment ) | | sdfgi - > y_scale_mode ! = environment_get_sdfgi_y_scale ( p_environment ) ) ) {
2020-06-25 13:33:28 +00:00
//configuration changed, erase
2022-08-04 08:40:39 +00:00
sdfgi . unref ( ) ;
rb - > set_custom_data ( RB_SCOPE_SDFGI , sdfgi ) ;
2020-06-25 13:33:28 +00:00
}
2022-08-04 08:40:39 +00:00
if ( sdfgi . is_null ( ) ) {
2021-02-13 12:08:08 +00:00
// re-create
2022-08-04 08:40:39 +00:00
sdfgi = gi . create_sdfgi ( p_environment , p_world_position , requested_history_size ) ;
rb - > set_custom_data ( RB_SCOPE_SDFGI , sdfgi ) ;
2021-02-13 12:08:08 +00:00
} else {
//check for updates
2022-08-04 08:40:39 +00:00
sdfgi - > update ( p_environment , p_world_position ) ;
2021-02-13 12:08:08 +00:00
}
}
2020-06-25 13:33:28 +00:00
2022-08-04 08:40:39 +00:00
int RendererSceneRenderRD : : sdfgi_get_pending_region_count ( const Ref < RenderSceneBuffers > & p_render_buffers ) const {
Ref < RenderSceneBuffersRD > rb = p_render_buffers ;
ERR_FAIL_COND_V ( rb . is_null ( ) , 0 ) ;
2020-06-25 13:33:28 +00:00
2022-08-04 08:40:39 +00:00
if ( ! rb - > has_custom_data ( RB_SCOPE_SDFGI ) ) {
2021-02-13 12:08:08 +00:00
return 0 ;
}
2022-08-04 08:40:39 +00:00
Ref < RendererRD : : GI : : SDFGI > sdfgi = rb - > get_custom_data ( RB_SCOPE_SDFGI ) ;
2020-06-25 13:33:28 +00:00
2021-02-13 12:08:08 +00:00
int dirty_count = 0 ;
2022-08-04 08:40:39 +00:00
for ( uint32_t i = 0 ; i < sdfgi - > cascades . size ( ) ; i + + ) {
const RendererRD : : GI : : SDFGI : : Cascade & c = sdfgi - > cascades [ i ] ;
2020-06-25 13:33:28 +00:00
2022-05-20 02:52:19 +00:00
if ( c . dirty_regions = = RendererRD : : GI : : SDFGI : : Cascade : : DIRTY_ALL ) {
2020-06-25 13:33:28 +00:00
dirty_count + + ;
} else {
2021-02-13 12:08:08 +00:00
for ( int j = 0 ; j < 3 ; j + + ) {
if ( c . dirty_regions [ j ] ! = 0 ) {
dirty_count + + ;
}
}
2020-03-20 00:32:19 +00:00
}
}
2021-02-13 12:08:08 +00:00
return dirty_count ;
}
2020-03-20 00:32:19 +00:00
2022-08-04 08:40:39 +00:00
AABB RendererSceneRenderRD : : sdfgi_get_pending_region_bounds ( const Ref < RenderSceneBuffers > & p_render_buffers , int p_region ) const {
2021-02-13 12:08:08 +00:00
AABB bounds ;
Vector3i from ;
Vector3i size ;
2020-03-20 00:32:19 +00:00
2022-08-04 08:40:39 +00:00
Ref < RenderSceneBuffersRD > rb = p_render_buffers ;
ERR_FAIL_COND_V ( rb . is_null ( ) , AABB ( ) ) ;
Ref < RendererRD : : GI : : SDFGI > sdfgi = rb - > get_custom_data ( RB_SCOPE_SDFGI ) ;
ERR_FAIL_COND_V ( sdfgi . is_null ( ) , AABB ( ) ) ;
int c = sdfgi - > get_pending_region_data ( p_region , from , size , bounds ) ;
2021-02-13 12:08:08 +00:00
ERR_FAIL_COND_V ( c = = - 1 , AABB ( ) ) ;
return bounds ;
}
2020-03-20 00:32:19 +00:00
2022-08-04 08:40:39 +00:00
uint32_t RendererSceneRenderRD : : sdfgi_get_pending_region_cascade ( const Ref < RenderSceneBuffers > & p_render_buffers , int p_region ) const {
2021-02-13 12:08:08 +00:00
AABB bounds ;
Vector3i from ;
Vector3i size ;
2020-03-20 00:32:19 +00:00
2022-08-04 08:40:39 +00:00
Ref < RenderSceneBuffersRD > rb = p_render_buffers ;
ERR_FAIL_COND_V ( rb . is_null ( ) , - 1 ) ;
Ref < RendererRD : : GI : : SDFGI > sdfgi = rb - > get_custom_data ( RB_SCOPE_SDFGI ) ;
ERR_FAIL_COND_V ( sdfgi . is_null ( ) , - 1 ) ;
return sdfgi - > get_pending_region_data ( p_region , from , size , bounds ) ;
2021-02-13 12:08:08 +00:00
}
2020-03-20 00:32:19 +00:00
2021-02-13 12:08:08 +00:00
RID RendererSceneRenderRD : : sky_allocate ( ) {
return sky . allocate_sky_rid ( ) ;
}
void RendererSceneRenderRD : : sky_initialize ( RID p_rid ) {
sky . initialize_sky_rid ( p_rid ) ;
}
2020-03-20 00:32:19 +00:00
2021-02-13 12:08:08 +00:00
void RendererSceneRenderRD : : sky_set_radiance_size ( RID p_sky , int p_radiance_size ) {
sky . sky_set_radiance_size ( p_sky , p_radiance_size ) ;
2020-03-20 00:32:19 +00:00
}
2021-02-13 12:08:08 +00:00
void RendererSceneRenderRD : : sky_set_mode ( RID p_sky , RS : : SkyMode p_mode ) {
sky . sky_set_mode ( p_sky , p_mode ) ;
}
2020-03-20 00:32:19 +00:00
2021-02-13 12:08:08 +00:00
void RendererSceneRenderRD : : sky_set_material ( RID p_sky , RID p_material ) {
sky . sky_set_material ( p_sky , p_material ) ;
2020-03-20 00:32:19 +00:00
}
2021-02-13 12:08:08 +00:00
Ref < Image > RendererSceneRenderRD : : sky_bake_panorama ( RID p_sky , float p_energy , bool p_bake_irradiance , const Size2i & p_size ) {
return sky . sky_bake_panorama ( p_sky , p_energy , p_bake_irradiance , p_size ) ;
2020-03-20 00:32:19 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : environment_glow_set_use_bicubic_upscale ( bool p_enable ) {
2020-03-30 13:46:03 +00:00
glow_bicubic_upscale = p_enable ;
2019-08-27 18:27:35 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : environment_glow_set_use_high_quality ( bool p_enable ) {
2020-09-01 06:04:45 +00:00
glow_high_quality = p_enable ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : environment_set_volumetric_fog_volume_size ( int p_size , int p_depth ) {
2020-08-13 01:21:01 +00:00
volumetric_fog_size = p_size ;
volumetric_fog_depth = p_depth ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : environment_set_volumetric_fog_filter_active ( bool p_enable ) {
2020-08-13 01:21:01 +00:00
volumetric_fog_filter_active = p_enable ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : environment_set_sdfgi_ray_count ( RS : : EnvironmentSDFGIRayCount p_ray_count ) {
2021-02-13 12:08:08 +00:00
gi . sdfgi_ray_count = p_ray_count ;
2020-06-25 13:33:28 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : environment_set_sdfgi_frames_to_converge ( RS : : EnvironmentSDFGIFramesToConverge p_frames ) {
2021-02-13 12:08:08 +00:00
gi . sdfgi_frames_to_converge = p_frames ;
2020-06-25 13:33:28 +00:00
}
2021-01-22 23:50:24 +00:00
void RendererSceneRenderRD : : environment_set_sdfgi_frames_to_update_light ( RS : : EnvironmentSDFGIFramesToUpdateLight p_update ) {
2021-02-13 12:08:08 +00:00
gi . sdfgi_frames_to_update_light = p_update ;
2021-01-22 23:50:24 +00:00
}
2020-06-25 13:33:28 +00:00
2020-12-04 18:26:24 +00:00
Ref < Image > RendererSceneRenderRD : : environment_bake_panorama ( RID p_env , bool p_bake_irradiance , const Size2i & p_size ) {
2022-07-27 06:14:23 +00:00
ERR_FAIL_COND_V ( p_env . is_null ( ) , Ref < Image > ( ) ) ;
2020-05-01 12:34:23 +00:00
2022-07-27 06:14:23 +00:00
RS : : EnvironmentBG environment_background = environment_get_background ( p_env ) ;
2021-12-24 14:47:42 +00:00
if ( environment_background = = RS : : ENV_BG_CAMERA_FEED | | environment_background = = RS : : ENV_BG_CANVAS | | environment_background = = RS : : ENV_BG_KEEP ) {
2020-05-01 12:34:23 +00:00
return Ref < Image > ( ) ; //nothing to bake
}
2022-07-27 06:14:23 +00:00
RS : : EnvironmentAmbientSource ambient_source = environment_get_ambient_source ( p_env ) ;
2021-12-24 14:47:42 +00:00
bool use_ambient_light = false ;
bool use_cube_map = false ;
if ( ambient_source = = RS : : ENV_AMBIENT_SOURCE_BG & & ( environment_background = = RS : : ENV_BG_CLEAR_COLOR | | environment_background = = RS : : ENV_BG_COLOR ) ) {
use_ambient_light = true ;
} else {
use_cube_map = ( ambient_source = = RS : : ENV_AMBIENT_SOURCE_BG & & environment_background = = RS : : ENV_BG_SKY ) | | ambient_source = = RS : : ENV_AMBIENT_SOURCE_SKY ;
use_ambient_light = use_cube_map | | ambient_source = = RS : : ENV_AMBIENT_SOURCE_COLOR ;
}
2022-07-27 06:14:23 +00:00
use_cube_map = use_cube_map | | ( environment_background = = RS : : ENV_BG_SKY & & environment_get_sky ( p_env ) . is_valid ( ) ) ;
2021-12-24 14:47:42 +00:00
Color ambient_color ;
2022-07-27 06:14:23 +00:00
float ambient_color_sky_mix = 0.0 ;
2021-12-24 14:47:42 +00:00
if ( use_ambient_light ) {
2022-07-27 06:14:23 +00:00
ambient_color_sky_mix = environment_get_ambient_sky_contribution ( p_env ) ;
const float ambient_energy = environment_get_ambient_light_energy ( p_env ) ;
ambient_color = environment_get_ambient_light ( p_env ) ;
2022-04-13 08:37:22 +00:00
ambient_color = ambient_color . srgb_to_linear ( ) ;
2021-12-24 14:47:42 +00:00
ambient_color . r * = ambient_energy ;
ambient_color . g * = ambient_energy ;
ambient_color . b * = ambient_energy ;
}
if ( use_cube_map ) {
2022-07-31 23:20:24 +00:00
Ref < Image > panorama = sky_bake_panorama ( environment_get_sky ( p_env ) , environment_get_bg_energy_multiplier ( p_env ) , p_bake_irradiance , p_size ) ;
2021-12-24 14:47:42 +00:00
if ( use_ambient_light ) {
for ( int x = 0 ; x < p_size . width ; x + + ) {
for ( int y = 0 ; y < p_size . height ; y + + ) {
panorama - > set_pixel ( x , y , ambient_color . lerp ( panorama - > get_pixel ( x , y ) , ambient_color_sky_mix ) ) ;
}
2020-05-01 12:34:23 +00:00
}
}
2021-12-24 14:47:42 +00:00
return panorama ;
} else {
2022-07-31 23:20:24 +00:00
const float bg_energy_multiplier = environment_get_bg_energy_multiplier ( p_env ) ;
2022-07-27 06:14:23 +00:00
Color panorama_color = ( ( environment_background = = RS : : ENV_BG_CLEAR_COLOR ) ? RSG : : texture_storage - > get_default_clear_color ( ) : environment_get_bg_color ( p_env ) ) ;
2022-04-13 08:37:22 +00:00
panorama_color = panorama_color . srgb_to_linear ( ) ;
2022-07-31 23:20:24 +00:00
panorama_color . r * = bg_energy_multiplier ;
panorama_color . g * = bg_energy_multiplier ;
panorama_color . b * = bg_energy_multiplier ;
2021-12-24 14:47:42 +00:00
if ( use_ambient_light ) {
panorama_color = ambient_color . lerp ( panorama_color , ambient_color_sky_mix ) ;
}
2020-05-01 12:34:23 +00:00
2021-12-24 14:47:42 +00:00
Ref < Image > panorama ;
panorama . instantiate ( ) ;
panorama - > create ( p_size . width , p_size . height , false , Image : : FORMAT_RGBAF ) ;
2022-01-08 11:58:15 +00:00
panorama - > fill ( panorama_color ) ;
2021-12-24 14:47:42 +00:00
return panorama ;
2020-05-01 12:34:23 +00:00
}
}
2022-09-12 09:44:48 +00:00
/* REFLECTION PROBE */
RID RendererSceneRenderRD : : reflection_probe_create_framebuffer ( RID p_color , RID p_depth ) {
Vector < RID > fb ;
fb . push_back ( p_color ) ;
fb . push_back ( p_depth ) ;
return RD : : get_singleton ( ) - > framebuffer_create ( fb ) ;
}
/* FOG VOLUME INSTANCE */
2019-09-07 01:51:27 +00:00
2021-10-03 11:28:55 +00:00
RID RendererSceneRenderRD : : fog_volume_instance_create ( RID p_fog_volume ) {
2022-07-19 04:17:58 +00:00
return RendererRD : : Fog : : get_singleton ( ) - > fog_volume_instance_create ( p_fog_volume ) ;
2021-10-03 11:28:55 +00:00
}
2022-07-19 04:17:58 +00:00
2021-10-03 11:28:55 +00:00
void RendererSceneRenderRD : : fog_volume_instance_set_transform ( RID p_fog_volume_instance , const Transform3D & p_transform ) {
2022-09-12 09:44:48 +00:00
RendererRD : : Fog : : get_singleton ( ) - > fog_volume_instance_set_transform ( p_fog_volume_instance , p_transform ) ;
2021-10-03 11:28:55 +00:00
}
2022-09-12 09:44:48 +00:00
2021-10-03 11:28:55 +00:00
void RendererSceneRenderRD : : fog_volume_instance_set_active ( RID p_fog_volume_instance , bool p_active ) {
2022-09-12 09:44:48 +00:00
RendererRD : : Fog : : get_singleton ( ) - > fog_volume_instance_set_active ( p_fog_volume_instance , p_active ) ;
2021-10-03 11:28:55 +00:00
}
RID RendererSceneRenderRD : : fog_volume_instance_get_volume ( RID p_fog_volume_instance ) const {
2022-09-12 09:44:48 +00:00
return RendererRD : : Fog : : get_singleton ( ) - > fog_volume_instance_get_volume ( p_fog_volume_instance ) ;
2021-10-03 11:28:55 +00:00
}
Vector3 RendererSceneRenderRD : : fog_volume_instance_get_position ( RID p_fog_volume_instance ) const {
2022-09-12 09:44:48 +00:00
return RendererRD : : Fog : : get_singleton ( ) - > fog_volume_instance_get_position ( p_fog_volume_instance ) ;
2021-10-03 11:28:55 +00:00
}
2022-09-12 09:44:48 +00:00
/* VOXEL GI */
2021-01-17 16:25:38 +00:00
2022-09-12 09:44:48 +00:00
RID RendererSceneRenderRD : : voxel_gi_instance_create ( RID p_base ) {
return gi . voxel_gi_instance_create ( p_base ) ;
2019-09-09 20:50:51 +00:00
}
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : voxel_gi_instance_set_transform_to_data ( RID p_probe , const Transform3D & p_xform ) {
if ( ! is_dynamic_gi_supported ( ) ) {
return ;
2019-09-09 20:50:51 +00:00
}
2020-12-17 18:56:59 +00:00
2022-09-12 09:44:48 +00:00
gi . voxel_gi_instance_set_transform_to_data ( p_probe , p_xform ) ;
2020-12-17 18:56:59 +00:00
}
2022-09-12 09:44:48 +00:00
bool RendererSceneRenderRD : : voxel_gi_needs_update ( RID p_probe ) const {
if ( ! is_dynamic_gi_supported ( ) ) {
return false ;
}
2021-07-15 20:06:33 +00:00
2022-09-12 09:44:48 +00:00
return gi . voxel_gi_needs_update ( p_probe ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : voxel_gi_update ( RID p_probe , bool p_update_light_instances , const Vector < RID > & p_light_instances , const PagedArray < RenderGeometryInstance * > & p_dynamic_objects ) {
if ( ! is_dynamic_gi_supported ( ) ) {
return ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
gi . voxel_gi_update ( p_probe , p_update_light_instances , p_light_instances , p_dynamic_objects ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _debug_sdfgi_probes ( Ref < RenderSceneBuffersRD > p_render_buffers , RID p_framebuffer , const uint32_t p_view_count , const Projection * p_camera_with_transforms , bool p_will_continue_color , bool p_will_continue_depth ) {
ERR_FAIL_COND ( p_render_buffers . is_null ( ) ) ;
2019-09-09 20:50:51 +00:00
2022-09-12 09:44:48 +00:00
if ( ! p_render_buffers - > has_custom_data ( RB_SCOPE_SDFGI ) ) {
return ; //nothing to debug
2019-09-09 20:50:51 +00:00
}
2022-09-12 09:44:48 +00:00
Ref < RendererRD : : GI : : SDFGI > sdfgi = p_render_buffers - > get_custom_data ( RB_SCOPE_SDFGI ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
sdfgi - > debug_probes ( p_framebuffer , p_view_count , p_camera_with_transforms , p_will_continue_color , p_will_continue_depth ) ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
////////////////////////////////
Ref < RenderSceneBuffers > RendererSceneRenderRD : : render_buffers_create ( ) {
Ref < RenderSceneBuffersRD > rb ;
rb . instantiate ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
rb - > set_can_be_storage ( _render_buffers_can_be_storage ( ) ) ;
rb - > set_max_cluster_elements ( max_cluster_elements ) ;
rb - > set_base_data_format ( _render_buffers_get_color_format ( ) ) ;
if ( vrs ) {
rb - > set_vrs ( vrs ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
setup_render_buffer_data ( rb ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
return rb ;
2019-09-09 20:50:51 +00:00
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _allocate_luminance_textures ( Ref < RenderSceneBuffersRD > rb ) {
ERR_FAIL_COND ( ! rb - > luminance . current . is_null ( ) ) ;
2019-09-09 20:50:51 +00:00
2022-09-12 09:44:48 +00:00
Size2i internal_size = rb - > get_internal_size ( ) ;
int w = internal_size . x ;
int h = internal_size . y ;
2020-02-20 23:27:34 +00:00
2022-09-12 09:44:48 +00:00
while ( true ) {
w = MAX ( w / 8 , 1 ) ;
h = MAX ( h / 8 , 1 ) ;
2021-07-27 13:33:47 +00:00
2022-09-12 09:44:48 +00:00
RD : : TextureFormat tf ;
tf . format = RD : : DATA_FORMAT_R32_SFLOAT ;
tf . width = w ;
tf . height = h ;
2020-02-20 23:27:34 +00:00
2022-09-12 09:44:48 +00:00
bool final = w = = 1 & & h = = 1 ;
2020-03-01 01:16:50 +00:00
2022-09-12 09:44:48 +00:00
if ( _render_buffers_can_be_storage ( ) ) {
tf . usage_bits = RD : : TEXTURE_USAGE_STORAGE_BIT ;
if ( final ) {
tf . usage_bits | = RD : : TEXTURE_USAGE_SAMPLING_BIT ;
2020-03-01 01:16:50 +00:00
}
2022-09-12 09:44:48 +00:00
} else {
tf . usage_bits = RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD : : TEXTURE_USAGE_SAMPLING_BIT ;
2020-03-01 01:16:50 +00:00
}
2022-09-12 09:44:48 +00:00
RID texture = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
2020-03-01 01:16:50 +00:00
2022-09-12 09:44:48 +00:00
rb - > luminance . reduce . push_back ( texture ) ;
if ( ! _render_buffers_can_be_storage ( ) ) {
Vector < RID > fb ;
fb . push_back ( texture ) ;
rb - > luminance . fb . push_back ( RD : : get_singleton ( ) - > framebuffer_create ( fb ) ) ;
2019-09-09 20:50:51 +00:00
}
2019-10-03 20:39:08 +00:00
2022-09-12 09:44:48 +00:00
if ( final ) {
rb - > luminance . current = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( ! _render_buffers_can_be_storage ( ) ) {
Vector < RID > fb ;
fb . push_back ( rb - > luminance . current ) ;
rb - > luminance . current_fb = RD : : get_singleton ( ) - > framebuffer_create ( fb ) ;
2019-09-09 20:50:51 +00:00
}
2022-09-12 09:44:48 +00:00
break ;
2019-09-07 01:51:27 +00:00
}
}
}
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _render_buffers_copy_screen_texture ( const RenderDataRD * p_render_data ) {
Ref < RenderSceneBuffersRD > rb = p_render_data - > render_buffers ;
ERR_FAIL_COND ( rb . is_null ( ) ) ;
RD : : get_singleton ( ) - > draw_command_begin_label ( " Copy screen texture " ) ;
2021-06-26 10:49:25 +00:00
2022-09-12 09:44:48 +00:00
rb - > allocate_blur_textures ( ) ;
2019-09-09 20:50:51 +00:00
2022-09-12 09:44:48 +00:00
bool can_use_storage = _render_buffers_can_be_storage ( ) ;
Size2i size = rb - > get_internal_size ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
for ( uint32_t v = 0 ; v < rb - > get_view_count ( ) ; v + + ) {
RID texture = rb - > get_internal_texture ( v ) ;
int mipmaps = int ( rb - > get_texture_format ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_0 ) . mipmaps ) ;
RID dest = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_0 , v , 0 ) ;
2020-03-20 00:32:19 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_storage ) {
copy_effects - > copy_to_rect ( texture , dest , Rect2i ( 0 , 0 , size . x , size . y ) ) ;
} else {
RID fb = FramebufferCacheRD : : get_singleton ( ) - > get_cache ( dest ) ;
copy_effects - > copy_to_fb_rect ( texture , fb , Rect2i ( 0 , 0 , size . x , size . y ) ) ;
2020-03-01 01:16:50 +00:00
}
2022-09-12 09:44:48 +00:00
for ( int i = 1 ; i < mipmaps ; i + + ) {
RID source = dest ;
dest = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_0 , v , i ) ;
Size2i msize = rb - > get_texture_slice_size ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_0 , v , i ) ;
2020-02-20 23:27:34 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_storage ) {
copy_effects - > make_mipmap ( source , dest , msize ) ;
} else {
copy_effects - > make_mipmap_raster ( source , dest , msize ) ;
}
}
2019-09-07 01:51:27 +00:00
}
2020-03-01 01:16:50 +00:00
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _render_buffers_copy_depth_texture ( const RenderDataRD * p_render_data ) {
Ref < RenderSceneBuffersRD > rb = p_render_data - > render_buffers ;
ERR_FAIL_COND ( rb . is_null ( ) ) ;
2019-10-03 20:39:08 +00:00
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " Copy depth texture " ) ;
2019-10-03 20:39:08 +00:00
2022-09-12 09:44:48 +00:00
// note, this only creates our back depth texture if we haven't already created it.
uint32_t usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT ;
usage_bits | = RD : : TEXTURE_USAGE_CAN_COPY_TO_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT ;
usage_bits | = RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT ; // set this as color attachment because we're copying data into it, it's not actually used as a depth buffer
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
rb - > create_texture ( RB_SCOPE_BUFFERS , RB_TEX_BACK_DEPTH , RD : : DATA_FORMAT_R32_SFLOAT , usage_bits , RD : : TEXTURE_SAMPLES_1 ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
bool can_use_storage = _render_buffers_can_be_storage ( ) ;
Size2i size = rb - > get_internal_size ( ) ;
for ( uint32_t v = 0 ; v < p_render_data - > scene_data - > view_count ; v + + ) {
RID depth_texture = rb - > get_depth_texture ( v ) ;
RID depth_back_texture = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BACK_DEPTH , v , 0 ) ;
2021-01-24 19:00:20 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_storage ) {
copy_effects - > copy_to_rect ( depth_texture , depth_back_texture , Rect2i ( 0 , 0 , size . x , size . y ) ) ;
} else {
RID depth_back_fb = FramebufferCacheRD : : get_singleton ( ) - > get_cache ( depth_back_texture ) ;
copy_effects - > copy_to_fb_rect ( depth_texture , depth_back_fb , Rect2i ( 0 , 0 , size . x , size . y ) ) ;
}
2021-01-24 19:00:20 +00:00
}
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _render_buffers_post_process_and_tonemap ( const RenderDataRD * p_render_data ) {
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
Ref < RenderSceneBuffersRD > rb = p_render_data - > render_buffers ;
ERR_FAIL_COND ( rb . is_null ( ) ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
// Glow, auto exposure and DoF (if enabled).
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
Size2i internal_size = rb - > get_internal_size ( ) ;
Size2i target_size = rb - > get_target_size ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
bool can_use_effects = target_size . x > = 8 & & target_size . y > = 8 ; // FIXME I think this should check internal size, we do all our post processing at this size...
bool can_use_storage = _render_buffers_can_be_storage ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RID render_target = rb - > get_render_target ( ) ;
RID internal_texture = rb - > get_internal_texture ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_effects & & RSG : : camera_attributes - > camera_attributes_uses_dof ( p_render_data - > camera_attributes ) ) {
RENDER_TIMESTAMP ( " Depth of Field " ) ;
RD : : get_singleton ( ) - > draw_command_begin_label ( " DOF " ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
rb - > allocate_blur_textures ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RendererRD : : BokehDOF : : BokehBuffers buffers ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
// Textures we use
buffers . base_texture_size = rb - > get_internal_size ( ) ;
buffers . secondary_texture = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_0 , 0 , 0 ) ;
buffers . half_texture [ 0 ] = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_1 , 0 , 0 ) ;
buffers . half_texture [ 1 ] = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_0 , 0 , 1 ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_storage ) {
for ( uint32_t i = 0 ; i < rb - > get_view_count ( ) ; i + + ) {
buffers . base_texture = rb - > get_internal_texture ( i ) ;
buffers . depth_texture = rb - > get_depth_texture ( i ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
// In stereo p_render_data->z_near and p_render_data->z_far can be offset for our combined frustrum
float z_near = p_render_data - > scene_data - > view_projection [ i ] . get_z_near ( ) ;
float z_far = p_render_data - > scene_data - > view_projection [ i ] . get_z_far ( ) ;
bokeh_dof - > bokeh_dof_compute ( buffers , p_render_data - > camera_attributes , z_near , z_far , p_render_data - > scene_data - > cam_orthogonal ) ;
} ;
} else {
// Set framebuffers.
buffers . secondary_fb = rb - > weight_buffers [ 1 ] . fb ;
buffers . half_fb [ 0 ] = rb - > weight_buffers [ 2 ] . fb ;
buffers . half_fb [ 1 ] = rb - > weight_buffers [ 3 ] . fb ;
buffers . weight_texture [ 0 ] = rb - > weight_buffers [ 0 ] . weight ;
buffers . weight_texture [ 1 ] = rb - > weight_buffers [ 1 ] . weight ;
buffers . weight_texture [ 2 ] = rb - > weight_buffers [ 2 ] . weight ;
buffers . weight_texture [ 3 ] = rb - > weight_buffers [ 3 ] . weight ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
// Set weight buffers.
buffers . base_weight_fb = rb - > weight_buffers [ 0 ] . fb ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
for ( uint32_t i = 0 ; i < rb - > get_view_count ( ) ; i + + ) {
buffers . base_texture = rb - > get_internal_texture ( i ) ;
buffers . depth_texture = rb - > get_depth_texture ( i ) ;
buffers . base_fb = FramebufferCacheRD : : get_singleton ( ) - > get_cache ( buffers . base_texture ) ; // TODO move this into bokeh_dof_raster, we can do this internally
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
// In stereo p_render_data->z_near and p_render_data->z_far can be offset for our combined frustrum
float z_near = p_render_data - > scene_data - > view_projection [ i ] . get_z_near ( ) ;
float z_far = p_render_data - > scene_data - > view_projection [ i ] . get_z_far ( ) ;
bokeh_dof - > bokeh_dof_raster ( buffers , p_render_data - > camera_attributes , z_near , z_far , p_render_data - > scene_data - > cam_orthogonal ) ;
}
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
float auto_exposure_scale = 1.0 ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_effects & & RSG : : camera_attributes - > camera_attributes_uses_auto_exposure ( p_render_data - > camera_attributes ) ) {
RENDER_TIMESTAMP ( " Auto exposure " ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " Auto exposure " ) ;
if ( rb - > luminance . current . is_null ( ) ) {
_allocate_luminance_textures ( rb ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
uint64_t auto_exposure_version = RSG : : camera_attributes - > camera_attributes_get_auto_exposure_version ( p_render_data - > camera_attributes ) ;
bool set_immediate = auto_exposure_version ! = rb - > get_auto_exposure_version ( ) ;
rb - > set_auto_exposure_version ( auto_exposure_version ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
double step = RSG : : camera_attributes - > camera_attributes_get_auto_exposure_adjust_speed ( p_render_data - > camera_attributes ) * time_step ;
float auto_exposure_min_sensitivity = RSG : : camera_attributes - > camera_attributes_get_auto_exposure_min_sensitivity ( p_render_data - > camera_attributes ) ;
float auto_exposure_max_sensitivity = RSG : : camera_attributes - > camera_attributes_get_auto_exposure_max_sensitivity ( p_render_data - > camera_attributes ) ;
if ( can_use_storage ) {
RendererCompositorRD : : singleton - > get_effects ( ) - > luminance_reduction ( internal_texture , internal_size , rb - > luminance . reduce , rb - > luminance . current , auto_exposure_min_sensitivity , auto_exposure_max_sensitivity , step , set_immediate ) ;
} else {
RendererCompositorRD : : singleton - > get_effects ( ) - > luminance_reduction_raster ( internal_texture , internal_size , rb - > luminance . reduce , rb - > luminance . fb , rb - > luminance . current , auto_exposure_min_sensitivity , auto_exposure_max_sensitivity , step , set_immediate ) ;
}
// Swap final reduce with prev luminance.
SWAP ( rb - > luminance . current , rb - > luminance . reduce . write [ rb - > luminance . reduce . size ( ) - 1 ] ) ;
if ( ! can_use_storage ) {
SWAP ( rb - > luminance . current_fb , rb - > luminance . fb . write [ rb - > luminance . fb . size ( ) - 1 ] ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
auto_exposure_scale = RSG : : camera_attributes - > camera_attributes_get_auto_exposure_scale ( p_render_data - > camera_attributes ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RenderingServerDefault : : redraw_request ( ) ; // Redraw all the time if auto exposure rendering is on.
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
int max_glow_level = - 1 ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_effects & & p_render_data - > environment . is_valid ( ) & & environment_get_glow_enabled ( p_render_data - > environment ) ) {
RENDER_TIMESTAMP ( " Glow " ) ;
RD : : get_singleton ( ) - > draw_command_begin_label ( " Gaussian Glow " ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
rb - > allocate_blur_textures ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
for ( int i = 0 ; i < RS : : MAX_GLOW_LEVELS ; i + + ) {
if ( environment_get_glow_levels ( p_render_data - > environment ) [ i ] > 0.0 ) {
int mipmaps = int ( rb - > get_texture_format ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_1 ) . mipmaps ) ;
if ( i > = mipmaps ) {
max_glow_level = mipmaps - 1 ;
} else {
max_glow_level = i ;
2019-09-07 01:51:27 +00:00
}
}
}
2022-09-12 09:44:48 +00:00
float luminance_multiplier = _render_buffers_get_luminance_multiplier ( ) ;
for ( uint32_t l = 0 ; l < rb - > get_view_count ( ) ; l + + ) {
for ( int i = 0 ; i < ( max_glow_level + 1 ) ; i + + ) {
Size2i vp_size = rb - > get_texture_slice_size ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_1 , l , i ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( i = = 0 ) {
RID luminance_texture ;
if ( RSG : : camera_attributes - > camera_attributes_uses_auto_exposure ( p_render_data - > camera_attributes ) & & rb - > luminance . current . is_valid ( ) ) {
luminance_texture = rb - > luminance . current ;
}
RID source = rb - > get_internal_texture ( l ) ;
RID dest = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_1 , l , i ) ;
if ( can_use_storage ) {
copy_effects - > gaussian_glow ( source , dest , vp_size , environment_get_glow_strength ( p_render_data - > environment ) , glow_high_quality , true , environment_get_glow_hdr_luminance_cap ( p_render_data - > environment ) , environment_get_exposure ( p_render_data - > environment ) , environment_get_glow_bloom ( p_render_data - > environment ) , environment_get_glow_hdr_bleed_threshold ( p_render_data - > environment ) , environment_get_glow_hdr_bleed_scale ( p_render_data - > environment ) , luminance_texture , auto_exposure_scale ) ;
} else {
RID half = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_HALF_BLUR , 0 , i ) ; // we can reuse this for each view
copy_effects - > gaussian_glow_raster ( source , half , dest , luminance_multiplier , vp_size , environment_get_glow_strength ( p_render_data - > environment ) , glow_high_quality , true , environment_get_glow_hdr_luminance_cap ( p_render_data - > environment ) , environment_get_exposure ( p_render_data - > environment ) , environment_get_glow_bloom ( p_render_data - > environment ) , environment_get_glow_hdr_bleed_threshold ( p_render_data - > environment ) , environment_get_glow_hdr_bleed_scale ( p_render_data - > environment ) , luminance_texture , auto_exposure_scale ) ;
}
} else {
RID source = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_1 , l , i - 1 ) ;
RID dest = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_1 , l , i ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_storage ) {
copy_effects - > gaussian_glow ( source , dest , vp_size , environment_get_glow_strength ( p_render_data - > environment ) , glow_high_quality ) ;
} else {
RID half = rb - > get_texture_slice ( RB_SCOPE_BUFFERS , RB_TEX_HALF_BLUR , 0 , i ) ; // we can reuse this for each view
copy_effects - > gaussian_glow_raster ( source , half , dest , luminance_multiplier , vp_size , environment_get_glow_strength ( p_render_data - > environment ) , glow_high_quality ) ;
}
}
}
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
{
RENDER_TIMESTAMP ( " Tonemap " ) ;
RD : : get_singleton ( ) - > draw_command_begin_label ( " Tonemap " ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RendererRD : : ToneMapper : : TonemapSettings tonemap ;
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_effects & & RSG : : camera_attributes - > camera_attributes_uses_auto_exposure ( p_render_data - > camera_attributes ) & & rb - > luminance . current . is_valid ( ) ) {
tonemap . use_auto_exposure = true ;
tonemap . exposure_texture = rb - > luminance . current ;
tonemap . auto_exposure_scale = auto_exposure_scale ;
} else {
tonemap . exposure_texture = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_WHITE ) ;
2021-08-04 15:18:06 +00:00
}
2022-09-12 09:44:48 +00:00
if ( can_use_effects & & p_render_data - > environment . is_valid ( ) & & environment_get_glow_enabled ( p_render_data - > environment ) ) {
tonemap . use_glow = true ;
tonemap . glow_mode = RendererRD : : ToneMapper : : TonemapSettings : : GlowMode ( environment_get_glow_blend_mode ( p_render_data - > environment ) ) ;
tonemap . glow_intensity = environment_get_glow_blend_mode ( p_render_data - > environment ) = = RS : : ENV_GLOW_BLEND_MODE_MIX ? environment_get_glow_mix ( p_render_data - > environment ) : environment_get_glow_intensity ( p_render_data - > environment ) ;
for ( int i = 0 ; i < RS : : MAX_GLOW_LEVELS ; i + + ) {
tonemap . glow_levels [ i ] = environment_get_glow_levels ( p_render_data - > environment ) [ i ] ;
}
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
Size2i msize = rb - > get_texture_slice_size ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_1 , 0 , 0 ) ;
tonemap . glow_texture_size . x = msize . width ;
tonemap . glow_texture_size . y = msize . height ;
tonemap . glow_use_bicubic_upscale = glow_bicubic_upscale ;
tonemap . glow_texture = rb - > get_texture ( RB_SCOPE_BUFFERS , RB_TEX_BLUR_1 ) ;
if ( environment_get_glow_map ( p_render_data - > environment ) . is_valid ( ) ) {
tonemap . glow_map_strength = environment_get_glow_map_strength ( p_render_data - > environment ) ;
tonemap . glow_map = texture_storage - > texture_get_rd_texture ( environment_get_glow_map ( p_render_data - > environment ) ) ;
} else {
tonemap . glow_map_strength = 0.0f ;
tonemap . glow_map = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_WHITE ) ;
2021-08-04 15:18:06 +00:00
}
2022-09-12 09:44:48 +00:00
} else {
tonemap . glow_texture = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_BLACK ) ;
tonemap . glow_map = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_WHITE ) ;
}
if ( rb - > get_screen_space_aa ( ) = = RS : : VIEWPORT_SCREEN_SPACE_AA_FXAA ) {
tonemap . use_fxaa = true ;
}
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
tonemap . use_debanding = rb - > get_use_debanding ( ) ;
tonemap . texture_size = Vector2i ( rb - > get_internal_size ( ) . x , rb - > get_internal_size ( ) . y ) ;
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
if ( p_render_data - > environment . is_valid ( ) ) {
tonemap . tonemap_mode = environment_get_tone_mapper ( p_render_data - > environment ) ;
tonemap . white = environment_get_white ( p_render_data - > environment ) ;
tonemap . exposure = environment_get_exposure ( p_render_data - > environment ) ;
}
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
tonemap . use_color_correction = false ;
tonemap . use_1d_color_correction = false ;
tonemap . color_correction_texture = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ;
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_effects & & p_render_data - > environment . is_valid ( ) ) {
tonemap . use_bcs = environment_get_adjustments_enabled ( p_render_data - > environment ) ;
tonemap . brightness = environment_get_adjustments_brightness ( p_render_data - > environment ) ;
tonemap . contrast = environment_get_adjustments_contrast ( p_render_data - > environment ) ;
tonemap . saturation = environment_get_adjustments_saturation ( p_render_data - > environment ) ;
if ( environment_get_adjustments_enabled ( p_render_data - > environment ) & & environment_get_color_correction ( p_render_data - > environment ) . is_valid ( ) ) {
tonemap . use_color_correction = true ;
tonemap . use_1d_color_correction = environment_get_use_1d_color_correction ( p_render_data - > environment ) ;
tonemap . color_correction_texture = texture_storage - > texture_get_rd_texture ( environment_get_color_correction ( p_render_data - > environment ) ) ;
2021-08-04 15:18:06 +00:00
}
}
2022-09-12 09:44:48 +00:00
tonemap . luminance_multiplier = _render_buffers_get_luminance_multiplier ( ) ;
tonemap . view_count = rb - > get_view_count ( ) ;
RID dest_fb ;
if ( fsr & & can_use_effects & & ( internal_size . x ! = target_size . x | | internal_size . y ! = target_size . y ) ) {
// If we use FSR to upscale we need to write our result into an intermediate buffer.
// Note that this is cached so we only create the texture the first time.
RID dest_texture = rb - > create_texture ( SNAME ( " Tonemapper " ) , SNAME ( " destination " ) , _render_buffers_get_color_format ( ) , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT ) ;
dest_fb = FramebufferCacheRD : : get_singleton ( ) - > get_cache ( dest_texture ) ;
} else {
// If we do a bilinear upscale we just render into our render target and our shader will upscale automatically.
// Target size in this case is lying as we never get our real target size communicated.
// Bit nasty but...
dest_fb = texture_storage - > render_target_get_rd_framebuffer ( render_target ) ;
2021-08-04 15:18:06 +00:00
}
2022-09-12 09:44:48 +00:00
tone_mapper - > tonemapper ( internal_texture , dest_fb , tonemap ) ;
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
2021-08-04 15:18:06 +00:00
}
2022-09-12 09:44:48 +00:00
if ( fsr & & can_use_effects & & ( internal_size . x ! = target_size . x | | internal_size . y ! = target_size . y ) ) {
// TODO Investigate? Does this work? We never write into our render target and we've already done so up above in our tonemapper.
// I think FSR should either work before our tonemapper or as an alternative of our tonemapper.
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " FSR 1.0 Upscale " ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
for ( uint32_t v = 0 ; v < rb - > get_view_count ( ) ; v + + ) {
RID source_texture = rb - > get_texture_slice ( SNAME ( " Tonemapper " ) , SNAME ( " destination " ) , v , 0 ) ;
RID dest_texture = texture_storage - > render_target_get_rd_texture_slice ( render_target , v ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
fsr - > fsr_upscale ( rb , source_texture , dest_texture ) ;
}
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
texture_storage - > render_target_disable_clear_request ( render_target ) ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _post_process_subpass ( RID p_source_texture , RID p_framebuffer , const RenderDataRD * p_render_data ) {
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
RD : : get_singleton ( ) - > draw_command_begin_label ( " Post Process Subpass " ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
Ref < RenderSceneBuffersRD > rb = p_render_data - > render_buffers ;
ERR_FAIL_COND ( rb . is_null ( ) ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
// FIXME: Our input it our internal_texture, shouldn't this be using internal_size ??
// Seeing we don't support FSR in our mobile renderer right now target_size = internal_size...
Size2i target_size = rb - > get_target_size ( ) ;
bool can_use_effects = target_size . x > = 8 & & target_size . y > = 8 ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RD : : DrawListID draw_list = RD : : get_singleton ( ) - > draw_list_switch_to_next_pass ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RendererRD : : ToneMapper : : TonemapSettings tonemap ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( p_render_data - > environment . is_valid ( ) ) {
tonemap . tonemap_mode = environment_get_tone_mapper ( p_render_data - > environment ) ;
tonemap . exposure = environment_get_exposure ( p_render_data - > environment ) ;
tonemap . white = environment_get_white ( p_render_data - > environment ) ;
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
// We don't support glow or auto exposure here, if they are needed, don't use subpasses!
// The problem is that we need to use the result so far and process them before we can
// apply this to our results.
if ( can_use_effects & & p_render_data - > environment . is_valid ( ) & & environment_get_glow_enabled ( p_render_data - > environment ) ) {
ERR_FAIL_MSG ( " Glow is not supported when using subpasses. " ) ;
}
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_effects & & RSG : : camera_attributes - > camera_attributes_uses_auto_exposure ( p_render_data - > camera_attributes ) ) {
ERR_FAIL_MSG ( " Auto Exposure is not supported when using subpasses. " ) ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
tonemap . use_glow = false ;
tonemap . glow_texture = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_BLACK ) ;
tonemap . glow_map = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_WHITE ) ;
tonemap . use_auto_exposure = false ;
tonemap . exposure_texture = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_WHITE ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
tonemap . use_color_correction = false ;
tonemap . use_1d_color_correction = false ;
tonemap . color_correction_texture = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( can_use_effects & & p_render_data - > environment . is_valid ( ) ) {
tonemap . use_bcs = environment_get_adjustments_enabled ( p_render_data - > environment ) ;
tonemap . brightness = environment_get_adjustments_brightness ( p_render_data - > environment ) ;
tonemap . contrast = environment_get_adjustments_contrast ( p_render_data - > environment ) ;
tonemap . saturation = environment_get_adjustments_saturation ( p_render_data - > environment ) ;
if ( environment_get_adjustments_enabled ( p_render_data - > environment ) & & environment_get_color_correction ( p_render_data - > environment ) . is_valid ( ) ) {
tonemap . use_color_correction = true ;
tonemap . use_1d_color_correction = environment_get_use_1d_color_correction ( p_render_data - > environment ) ;
tonemap . color_correction_texture = texture_storage - > texture_get_rd_texture ( environment_get_color_correction ( p_render_data - > environment ) ) ;
2019-09-07 01:51:27 +00:00
}
2021-08-04 15:18:06 +00:00
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
tonemap . use_debanding = rb - > get_use_debanding ( ) ;
tonemap . texture_size = Vector2i ( target_size . x , target_size . y ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
tonemap . luminance_multiplier = _render_buffers_get_luminance_multiplier ( ) ;
tonemap . view_count = rb - > get_view_count ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
tone_mapper - > tonemapper ( draw_list , p_source_texture , RD : : get_singleton ( ) - > framebuffer_get_format ( p_framebuffer ) , tonemap ) ;
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _disable_clear_request ( const RenderDataRD * p_render_data ) {
ERR_FAIL_COND ( p_render_data - > render_buffers . is_null ( ) ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
texture_storage - > render_target_disable_clear_request ( p_render_data - > render_buffers - > get_render_target ( ) ) ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _render_buffers_debug_draw ( Ref < RenderSceneBuffersRD > p_render_buffers , RID p_shadow_atlas , RID p_occlusion_buffer ) {
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
ERR_FAIL_COND ( p_render_buffers . is_null ( ) ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
RID render_target = p_render_buffers - > get_render_target ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( debug_draw = = RS : : VIEWPORT_DEBUG_DRAW_SHADOW_ATLAS ) {
if ( p_shadow_atlas . is_valid ( ) ) {
RID shadow_atlas_texture = RendererRD : : LightStorage : : get_singleton ( ) - > shadow_atlas_get_texture ( p_shadow_atlas ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( shadow_atlas_texture . is_null ( ) ) {
shadow_atlas_texture = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_BLACK ) ;
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
Size2 rtsize = texture_storage - > render_target_get_size ( render_target ) ;
copy_effects - > copy_to_fb_rect ( shadow_atlas_texture , texture_storage - > render_target_get_rd_framebuffer ( render_target ) , Rect2i ( Vector2 ( ) , rtsize / 2 ) , false , true ) ;
}
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
if ( debug_draw = = RS : : VIEWPORT_DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS ) {
if ( RendererRD : : LightStorage : : get_singleton ( ) - > directional_shadow_get_texture ( ) . is_valid ( ) ) {
RID shadow_atlas_texture = RendererRD : : LightStorage : : get_singleton ( ) - > directional_shadow_get_texture ( ) ;
Size2 rtsize = texture_storage - > render_target_get_size ( render_target ) ;
2021-08-04 15:18:06 +00:00
2022-09-12 09:44:48 +00:00
copy_effects - > copy_to_fb_rect ( shadow_atlas_texture , texture_storage - > render_target_get_rd_framebuffer ( render_target ) , Rect2i ( Vector2 ( ) , rtsize / 2 ) , false , true ) ;
2021-08-04 15:18:06 +00:00
}
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( debug_draw = = RS : : VIEWPORT_DEBUG_DRAW_DECAL_ATLAS ) {
RID decal_atlas = RendererRD : : TextureStorage : : get_singleton ( ) - > decal_atlas_get_texture ( ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( decal_atlas . is_valid ( ) ) {
Size2 rtsize = texture_storage - > render_target_get_size ( render_target ) ;
2019-09-07 17:38:17 +00:00
2022-09-12 09:44:48 +00:00
copy_effects - > copy_to_fb_rect ( decal_atlas , texture_storage - > render_target_get_rd_framebuffer ( render_target ) , Rect2i ( Vector2 ( ) , rtsize / 2 ) , false , false , true ) ;
2019-09-07 17:38:17 +00:00
}
}
2022-09-12 09:44:48 +00:00
if ( debug_draw = = RS : : VIEWPORT_DEBUG_DRAW_SCENE_LUMINANCE ) {
if ( p_render_buffers - > luminance . current . is_valid ( ) ) {
Size2 rtsize = texture_storage - > render_target_get_size ( render_target ) ;
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
copy_effects - > copy_to_fb_rect ( p_render_buffers - > luminance . current , texture_storage - > render_target_get_rd_framebuffer ( render_target ) , Rect2 ( Vector2 ( ) , rtsize / 8 ) , false , true ) ;
}
2019-09-07 01:51:27 +00:00
}
2022-09-12 09:44:48 +00:00
if ( debug_draw = = RS : : VIEWPORT_DEBUG_DRAW_NORMAL_BUFFER & & _render_buffers_get_normal_texture ( p_render_buffers ) . is_valid ( ) ) {
Size2 rtsize = texture_storage - > render_target_get_size ( render_target ) ;
copy_effects - > copy_to_fb_rect ( _render_buffers_get_normal_texture ( p_render_buffers ) , texture_storage - > render_target_get_rd_framebuffer ( render_target ) , Rect2 ( Vector2 ( ) , rtsize ) , false , false ) ;
2021-07-15 20:06:33 +00:00
}
2019-09-07 01:51:27 +00:00
2022-09-12 09:44:48 +00:00
if ( debug_draw = = RS : : VIEWPORT_DEBUG_DRAW_OCCLUDERS ) {
if ( p_occlusion_buffer . is_valid ( ) ) {
Size2 rtsize = texture_storage - > render_target_get_size ( render_target ) ;
copy_effects - > copy_to_fb_rect ( texture_storage - > texture_get_rd_texture ( p_occlusion_buffer ) , texture_storage - > render_target_get_rd_framebuffer ( render_target ) , Rect2i ( Vector2 ( ) , rtsize ) , true , false ) ;
2021-02-13 12:08:08 +00:00
}
}
2019-10-11 02:14:56 +00:00
2022-09-12 09:44:48 +00:00
if ( debug_draw = = RS : : VIEWPORT_DEBUG_DRAW_MOTION_VECTORS & & _render_buffers_get_velocity_texture ( p_render_buffers ) . is_valid ( ) ) {
Size2 rtsize = texture_storage - > render_target_get_size ( render_target ) ;
copy_effects - > copy_to_fb_rect ( _render_buffers_get_velocity_texture ( p_render_buffers ) , texture_storage - > render_target_get_rd_framebuffer ( render_target ) , Rect2 ( Vector2 ( ) , rtsize ) , false , false ) ;
}
2021-02-13 12:08:08 +00:00
}
2019-10-03 20:39:08 +00:00
2022-09-12 09:44:48 +00:00
RID RendererSceneRenderRD : : render_buffers_get_default_voxel_gi_buffer ( ) {
return gi . default_voxel_gi_buffer ;
2021-02-13 12:08:08 +00:00
}
2019-10-03 20:39:08 +00:00
2022-09-12 09:44:48 +00:00
float RendererSceneRenderRD : : _render_buffers_get_luminance_multiplier ( ) {
return 1.0 ;
2021-02-13 12:08:08 +00:00
}
2019-10-11 02:14:56 +00:00
2022-09-12 09:44:48 +00:00
RD : : DataFormat RendererSceneRenderRD : : _render_buffers_get_color_format ( ) {
return RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
2021-02-13 12:08:08 +00:00
}
2019-10-11 02:14:56 +00:00
2022-09-12 09:44:48 +00:00
bool RendererSceneRenderRD : : _render_buffers_can_be_storage ( ) {
return true ;
2021-02-13 12:08:08 +00:00
}
2019-10-11 02:14:56 +00:00
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : gi_set_use_half_resolution ( bool p_enable ) {
gi . half_resolution = p_enable ;
2019-10-03 20:39:08 +00:00
}
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : positional_soft_shadow_filter_set_quality ( RS : : ShadowQuality p_quality ) {
ERR_FAIL_INDEX_MSG ( p_quality , RS : : SHADOW_QUALITY_MAX , " Shadow quality too high, please see RenderingServer's ShadowQuality enum " ) ;
2021-07-20 11:40:16 +00:00
2022-09-12 09:44:48 +00:00
if ( shadows_quality ! = p_quality ) {
shadows_quality = p_quality ;
2021-07-20 11:40:16 +00:00
2022-09-12 09:44:48 +00:00
switch ( shadows_quality ) {
case RS : : SHADOW_QUALITY_HARD : {
penumbra_shadow_samples = 4 ;
soft_shadow_samples = 0 ;
shadows_quality_radius = 1.0 ;
} break ;
case RS : : SHADOW_QUALITY_SOFT_VERY_LOW : {
penumbra_shadow_samples = 4 ;
soft_shadow_samples = 1 ;
shadows_quality_radius = 1.5 ;
} break ;
case RS : : SHADOW_QUALITY_SOFT_LOW : {
penumbra_shadow_samples = 8 ;
soft_shadow_samples = 4 ;
shadows_quality_radius = 2.0 ;
} break ;
case RS : : SHADOW_QUALITY_SOFT_MEDIUM : {
penumbra_shadow_samples = 12 ;
soft_shadow_samples = 8 ;
shadows_quality_radius = 2.0 ;
} break ;
case RS : : SHADOW_QUALITY_SOFT_HIGH : {
penumbra_shadow_samples = 24 ;
soft_shadow_samples = 16 ;
shadows_quality_radius = 3.0 ;
} break ;
case RS : : SHADOW_QUALITY_SOFT_ULTRA : {
penumbra_shadow_samples = 32 ;
soft_shadow_samples = 32 ;
shadows_quality_radius = 4.0 ;
} break ;
case RS : : SHADOW_QUALITY_MAX :
break ;
2020-01-12 01:26:52 +00:00
}
2022-09-12 09:44:48 +00:00
get_vogel_disk ( penumbra_shadow_kernel , penumbra_shadow_samples ) ;
get_vogel_disk ( soft_shadow_kernel , soft_shadow_samples ) ;
2020-01-12 01:26:52 +00:00
}
2020-01-25 10:18:55 +00:00
2022-09-12 09:44:48 +00:00
_update_shader_quality_settings ( ) ;
2021-08-03 07:07:32 +00:00
}
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : directional_soft_shadow_filter_set_quality ( RS : : ShadowQuality p_quality ) {
ERR_FAIL_INDEX_MSG ( p_quality , RS : : SHADOW_QUALITY_MAX , " Shadow quality too high, please see RenderingServer's ShadowQuality enum " ) ;
2021-08-03 07:07:32 +00:00
2022-09-12 09:44:48 +00:00
if ( directional_shadow_quality ! = p_quality ) {
directional_shadow_quality = p_quality ;
2022-04-04 14:10:22 +00:00
2022-09-12 09:44:48 +00:00
switch ( directional_shadow_quality ) {
case RS : : SHADOW_QUALITY_HARD : {
directional_penumbra_shadow_samples = 4 ;
directional_soft_shadow_samples = 0 ;
directional_shadow_quality_radius = 1.0 ;
2020-07-22 23:39:09 +00:00
} break ;
2022-09-12 09:44:48 +00:00
case RS : : SHADOW_QUALITY_SOFT_VERY_LOW : {
directional_penumbra_shadow_samples = 4 ;
directional_soft_shadow_samples = 1 ;
directional_shadow_quality_radius = 1.5 ;
2021-01-17 16:25:38 +00:00
} break ;
2022-09-12 09:44:48 +00:00
case RS : : SHADOW_QUALITY_SOFT_LOW : {
directional_penumbra_shadow_samples = 8 ;
directional_soft_shadow_samples = 4 ;
directional_shadow_quality_radius = 2.0 ;
2021-01-17 16:25:38 +00:00
} break ;
2022-09-12 09:44:48 +00:00
case RS : : SHADOW_QUALITY_SOFT_MEDIUM : {
directional_penumbra_shadow_samples = 12 ;
directional_soft_shadow_samples = 8 ;
directional_shadow_quality_radius = 2.0 ;
} break ;
case RS : : SHADOW_QUALITY_SOFT_HIGH : {
directional_penumbra_shadow_samples = 24 ;
directional_soft_shadow_samples = 16 ;
directional_shadow_quality_radius = 3.0 ;
} break ;
case RS : : SHADOW_QUALITY_SOFT_ULTRA : {
directional_penumbra_shadow_samples = 32 ;
directional_soft_shadow_samples = 32 ;
directional_shadow_quality_radius = 4.0 ;
} break ;
case RS : : SHADOW_QUALITY_MAX :
break ;
2021-01-17 16:25:38 +00:00
}
2022-09-12 09:44:48 +00:00
get_vogel_disk ( directional_penumbra_shadow_kernel , directional_penumbra_shadow_samples ) ;
get_vogel_disk ( directional_soft_shadow_kernel , directional_soft_shadow_samples ) ;
2020-07-22 23:39:09 +00:00
}
2022-09-12 09:44:48 +00:00
_update_shader_quality_settings ( ) ;
2020-07-22 23:39:09 +00:00
}
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : decals_set_filter ( RenderingServer : : DecalFilter p_filter ) {
if ( decals_filter = = p_filter ) {
return ;
2020-08-13 01:21:01 +00:00
}
2022-09-12 09:44:48 +00:00
decals_filter = p_filter ;
_update_shader_quality_settings ( ) ;
}
void RendererSceneRenderRD : : light_projectors_set_filter ( RenderingServer : : LightProjectorFilter p_filter ) {
if ( light_projectors_filter = = p_filter ) {
2020-08-13 01:21:01 +00:00
return ;
}
2022-09-12 09:44:48 +00:00
light_projectors_filter = p_filter ;
_update_shader_quality_settings ( ) ;
}
int RendererSceneRenderRD : : get_roughness_layers ( ) const {
return sky . roughness_layers ;
}
2020-08-13 01:21:01 +00:00
2022-09-12 09:44:48 +00:00
bool RendererSceneRenderRD : : is_using_radiance_cubemap_array ( ) const {
return sky . sky_use_cubemap_array ;
}
2022-08-04 08:40:39 +00:00
2022-09-12 09:44:48 +00:00
void RendererSceneRenderRD : : _update_vrs ( Ref < RenderSceneBuffersRD > p_render_buffers ) {
if ( p_render_buffers . is_valid ( ) & & vrs ) {
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2022-08-04 08:40:39 +00:00
2022-09-12 09:44:48 +00:00
RS : : ViewportVRSMode vrs_mode = texture_storage - > render_target_get_vrs_mode ( p_render_buffers - > get_render_target ( ) ) ;
if ( vrs_mode ! = RS : : VIEWPORT_VRS_DISABLED ) {
RID vrs_texture = p_render_buffers - > get_texture ( RB_SCOPE_VRS , RB_TEXTURE ) ;
2021-10-03 11:28:55 +00:00
2022-09-12 09:44:48 +00:00
// We use get_cache_multipass instead of get_cache_multiview because the default behavior is for
// our vrs_texture to be used as the VRS attachment. In this particular case we're writing to it
// so it needs to be set as our color attachment
2022-08-04 08:40:39 +00:00
2022-09-12 09:44:48 +00:00
Vector < RID > textures ;
textures . push_back ( vrs_texture ) ;
2022-07-19 04:17:58 +00:00
2022-09-12 09:44:48 +00:00
Vector < RD : : FramebufferPass > passes ;
RD : : FramebufferPass pass ;
pass . color_attachments . push_back ( 0 ) ;
passes . push_back ( pass ) ;
2020-08-13 01:21:01 +00:00
2022-09-12 09:44:48 +00:00
RID vrs_fb = FramebufferCacheRD : : get_singleton ( ) - > get_cache_multipass ( textures , passes , p_render_buffers - > get_view_count ( ) ) ;
2020-08-13 01:21:01 +00:00
2022-09-12 09:44:48 +00:00
vrs - > update_vrs_texture ( vrs_fb , p_render_buffers - > get_render_target ( ) ) ;
}
2020-08-13 01:21:01 +00:00
}
}
2021-05-05 05:41:12 +00:00
bool RendererSceneRenderRD : : _needs_post_prepass_render ( RenderDataRD * p_render_data , bool p_use_gi ) {
if ( p_render_data - > render_buffers . is_valid ( ) ) {
2022-08-04 08:40:39 +00:00
if ( p_render_data - > render_buffers - > has_custom_data ( RB_SCOPE_SDFGI ) ) {
2021-02-02 19:51:36 +00:00
return true ;
}
2020-01-10 00:40:26 +00:00
}
2021-02-02 19:51:36 +00:00
return false ;
}
2019-08-18 22:40:52 +00:00
2021-05-05 05:41:12 +00:00
void RendererSceneRenderRD : : _post_prepass_render ( RenderDataRD * p_render_data , bool p_use_gi ) {
2022-08-04 08:40:39 +00:00
if ( p_render_data - > render_buffers . is_valid ( ) & & p_use_gi ) {
if ( ! p_render_data - > render_buffers - > has_custom_data ( RB_SCOPE_SDFGI ) ) {
return ;
2020-06-25 13:33:28 +00:00
}
2022-08-04 08:40:39 +00:00
Ref < RendererRD : : GI : : SDFGI > sdfgi = p_render_data - > render_buffers - > get_custom_data ( RB_SCOPE_SDFGI ) ;
sdfgi - > update_probes ( p_render_data - > environment , sky . sky_owner . get_or_null ( environment_get_sky ( p_render_data - > environment ) ) ) ;
2020-06-25 13:33:28 +00:00
}
2021-02-02 19:51:36 +00:00
}
2020-06-25 13:33:28 +00:00
2021-05-05 05:41:12 +00:00
void RendererSceneRenderRD : : _pre_resolve_render ( RenderDataRD * p_render_data , bool p_use_gi ) {
if ( p_render_data - > render_buffers . is_valid ( ) ) {
2021-02-02 19:51:36 +00:00
if ( p_use_gi ) {
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
}
}
2020-12-23 16:52:58 +00:00
2022-09-08 00:44:36 +00:00
void RendererSceneRenderRD : : render_scene ( const Ref < RenderSceneBuffers > & p_render_buffers , const CameraData * p_camera_data , const CameraData * p_prev_camera_data , const PagedArray < RenderGeometryInstance * > & p_instances , const PagedArray < RID > & p_lights , const PagedArray < RID > & p_reflection_probes , const PagedArray < RID > & p_voxel_gi_instances , const PagedArray < RID > & p_decals , const PagedArray < RID > & p_lightmaps , const PagedArray < RID > & p_fog_volumes , RID p_environment , RID p_camera_attributes , RID p_shadow_atlas , RID p_occluder_debug_tex , RID p_reflection_atlas , RID p_reflection_probe , int p_reflection_probe_pass , float p_screen_mesh_lod_threshold , const RenderShadowData * p_render_shadows , int p_render_shadow_count , const RenderSDFGIData * p_render_sdfgi_regions , int p_render_sdfgi_region_count , const RenderSDFGIUpdateData * p_sdfgi_update_data , RenderingMethod : : RenderInfo * r_render_info ) {
2022-09-12 09:44:48 +00:00
RendererRD : : LightStorage * light_storage = RendererRD : : LightStorage : : get_singleton ( ) ;
2022-04-07 14:00:51 +00:00
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2021-02-13 12:08:08 +00:00
// getting this here now so we can direct call a bunch of things more easily
2022-08-04 08:40:39 +00:00
Ref < RenderSceneBuffersRD > rb ;
2021-02-13 12:08:08 +00:00
if ( p_render_buffers . is_valid ( ) ) {
2022-08-04 08:40:39 +00:00
rb = p_render_buffers ; // cast it...
ERR_FAIL_COND ( rb . is_null ( ) ) ;
2021-02-13 12:08:08 +00:00
}
2022-09-07 11:23:01 +00:00
// setup scene data
RenderSceneDataRD scene_data ;
2021-02-02 19:51:36 +00:00
{
2021-05-07 13:19:04 +00:00
// Our first camera is used by default
2022-09-07 11:23:01 +00:00
scene_data . cam_transform = p_camera_data - > main_transform ;
scene_data . cam_projection = p_camera_data - > main_projection ;
scene_data . cam_orthogonal = p_camera_data - > is_orthogonal ;
scene_data . taa_jitter = p_camera_data - > taa_jitter ;
2021-05-07 13:19:04 +00:00
2022-09-07 11:23:01 +00:00
scene_data . view_count = p_camera_data - > view_count ;
2021-05-07 13:19:04 +00:00
for ( uint32_t v = 0 ; v < p_camera_data - > view_count ; v + + ) {
2022-09-07 11:23:01 +00:00
scene_data . view_eye_offset [ v ] = p_camera_data - > view_offset [ v ] . origin ;
scene_data . view_projection [ v ] = p_camera_data - > view_projection [ v ] ;
2021-05-07 13:19:04 +00:00
}
2022-09-07 11:23:01 +00:00
scene_data . prev_cam_transform = p_prev_camera_data - > main_transform ;
scene_data . prev_cam_projection = p_prev_camera_data - > main_projection ;
scene_data . prev_taa_jitter = p_prev_camera_data - > taa_jitter ;
2022-04-04 14:10:22 +00:00
for ( uint32_t v = 0 ; v < p_camera_data - > view_count ; v + + ) {
2022-09-07 11:23:01 +00:00
scene_data . prev_view_projection [ v ] = p_prev_camera_data - > view_projection [ v ] ;
}
scene_data . z_near = p_camera_data - > main_projection . get_z_near ( ) ;
scene_data . z_far = p_camera_data - > main_projection . get_z_far ( ) ;
// this should be the same for all cameras..
scene_data . lod_distance_multiplier = p_camera_data - > main_projection . get_lod_multiplier ( ) ;
scene_data . lod_camera_plane = Plane ( - p_camera_data - > main_transform . basis . get_column ( Vector3 : : AXIS_Z ) , p_camera_data - > main_transform . get_origin ( ) ) ;
if ( get_debug_draw_mode ( ) = = RS : : VIEWPORT_DEBUG_DRAW_DISABLE_LOD ) {
scene_data . screen_mesh_lod_threshold = 0.0 ;
} else {
scene_data . screen_mesh_lod_threshold = p_screen_mesh_lod_threshold ;
}
if ( p_shadow_atlas . is_valid ( ) ) {
2022-09-12 09:44:48 +00:00
int shadow_atlas_size = light_storage - > shadow_atlas_get_size ( p_shadow_atlas ) ;
scene_data . shadow_atlas_pixel_size . x = 1.0 / shadow_atlas_size ;
scene_data . shadow_atlas_pixel_size . y = 1.0 / shadow_atlas_size ;
2022-04-04 14:10:22 +00:00
}
2022-09-07 11:23:01 +00:00
{
2022-09-12 09:44:48 +00:00
int directional_shadow_size = light_storage - > directional_shadow_get_size ( ) ;
scene_data . directional_shadow_pixel_size . x = 1.0 / directional_shadow_size ;
scene_data . directional_shadow_pixel_size . y = 1.0 / directional_shadow_size ;
2022-09-07 11:23:01 +00:00
}
scene_data . time = time ;
scene_data . time_step = time_step ;
}
2022-04-04 14:10:22 +00:00
2022-09-07 11:23:01 +00:00
//assign render data
RenderDataRD render_data ;
{
render_data . render_buffers = rb ;
render_data . scene_data = & scene_data ;
2021-05-05 05:41:12 +00:00
render_data . instances = & p_instances ;
render_data . lights = & p_lights ;
render_data . reflection_probes = & p_reflection_probes ;
2021-06-04 22:47:26 +00:00
render_data . voxel_gi_instances = & p_voxel_gi_instances ;
2021-05-05 05:41:12 +00:00
render_data . decals = & p_decals ;
render_data . lightmaps = & p_lightmaps ;
2021-10-03 11:28:55 +00:00
render_data . fog_volumes = & p_fog_volumes ;
2021-05-05 05:41:12 +00:00
render_data . environment = p_environment ;
2022-07-31 23:20:24 +00:00
render_data . camera_attributes = p_camera_attributes ;
2021-05-05 05:41:12 +00:00
render_data . shadow_atlas = p_shadow_atlas ;
2022-09-12 09:44:48 +00:00
render_data . occluder_debug_tex = p_occluder_debug_tex ;
2021-05-05 05:41:12 +00:00
render_data . reflection_atlas = p_reflection_atlas ;
render_data . reflection_probe = p_reflection_probe ;
render_data . reflection_probe_pass = p_reflection_probe_pass ;
2022-09-12 09:44:48 +00:00
render_data . render_shadows = p_render_shadows ;
render_data . render_shadow_count = p_render_shadow_count ;
render_data . render_sdfgi_regions = p_render_sdfgi_regions ;
render_data . render_sdfgi_region_count = p_render_sdfgi_region_count ;
render_data . sdfgi_update_data = p_sdfgi_update_data ;
2021-07-02 23:14:19 +00:00
render_data . render_info = r_render_info ;
2021-02-02 19:51:36 +00:00
}
PagedArray < RID > empty ;
if ( get_debug_draw_mode ( ) = = RS : : VIEWPORT_DEBUG_DRAW_UNSHADED ) {
2021-05-05 05:41:12 +00:00
render_data . lights = & empty ;
render_data . reflection_probes = & empty ;
2021-06-04 22:47:26 +00:00
render_data . voxel_gi_instances = & empty ;
2021-02-02 19:51:36 +00:00
}
Color clear_color ;
if ( p_render_buffers . is_valid ( ) ) {
2022-08-04 08:40:39 +00:00
clear_color = texture_storage - > render_target_get_clear_request_color ( rb - > get_render_target ( ) ) ;
2021-02-02 19:51:36 +00:00
} else {
2022-06-21 00:08:33 +00:00
clear_color = RSG : : texture_storage - > get_default_clear_color ( ) ;
2021-02-02 19:51:36 +00:00
}
//calls _pre_opaque_render between depth pre-pass and opaque pass
2021-05-05 05:41:12 +00:00
_render_scene ( & render_data , clear_color ) ;
2021-02-13 12:08:08 +00:00
}
2021-01-22 23:50:24 +00:00
2022-07-23 14:01:53 +00:00
void RendererSceneRenderRD : : render_material ( const Transform3D & p_cam_transform , const Projection & p_cam_projection , bool p_cam_orthogonal , const PagedArray < RenderGeometryInstance * > & p_instances , RID p_framebuffer , const Rect2i & p_region ) {
2022-07-31 23:20:24 +00:00
_render_material ( p_cam_transform , p_cam_projection , p_cam_orthogonal , p_instances , p_framebuffer , p_region , 1.0 ) ;
2021-02-13 12:08:08 +00:00
}
2021-01-22 23:50:24 +00:00
2022-07-23 14:01:53 +00:00
void RendererSceneRenderRD : : render_particle_collider_heightfield ( RID p_collider , const Transform3D & p_transform , const PagedArray < RenderGeometryInstance * > & p_instances ) {
2022-04-12 11:41:50 +00:00
RendererRD : : ParticlesStorage * particles_storage = RendererRD : : ParticlesStorage : : get_singleton ( ) ;
ERR_FAIL_COND ( ! particles_storage - > particles_collision_is_heightfield ( p_collider ) ) ;
Vector3 extents = particles_storage - > particles_collision_get_extents ( p_collider ) * p_transform . basis . get_scale ( ) ;
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
Projection cm ;
2021-02-13 12:08:08 +00:00
cm . set_orthogonal ( - extents . x , extents . x , - extents . z , extents . z , 0 , extents . y * 2.0 ) ;
2021-01-22 23:50:24 +00:00
2021-02-13 12:08:08 +00:00
Vector3 cam_pos = p_transform . origin ;
cam_pos . y + = extents . y ;
2020-06-25 13:33:28 +00:00
2020-10-17 05:08:21 +00:00
Transform3D cam_xform ;
2022-05-03 12:50:35 +00:00
cam_xform . set_look_at ( cam_pos , cam_pos - p_transform . basis . get_column ( Vector3 : : AXIS_Y ) , - p_transform . basis . get_column ( Vector3 : : AXIS_Z ) . normalized ( ) ) ;
2020-06-25 13:33:28 +00:00
2022-04-12 11:41:50 +00:00
RID fb = particles_storage - > particles_collision_get_heightfield_framebuffer ( p_collider ) ;
2021-02-02 19:51:36 +00:00
2021-02-13 12:08:08 +00:00
_render_particle_collider_heightfield ( fb , cam_xform , cm , p_instances ) ;
2020-06-25 13:33:28 +00:00
}
2020-12-04 18:26:24 +00:00
bool RendererSceneRenderRD : : free ( RID p_rid ) {
2022-08-04 08:40:39 +00:00
if ( is_environment ( p_rid ) ) {
2022-07-27 06:14:23 +00:00
environment_free ( p_rid ) ;
2022-07-31 23:20:24 +00:00
} else if ( RSG : : camera_attributes - > owns_camera_attributes ( p_rid ) ) {
RSG : : camera_attributes - > camera_attributes_free ( p_rid ) ;
2022-08-09 05:18:03 +00:00
} else if ( gi . voxel_gi_instance_owns ( p_rid ) ) {
gi . voxel_gi_instance_free ( p_rid ) ;
2021-02-13 12:08:08 +00:00
} else if ( sky . sky_owner . owns ( p_rid ) ) {
sky . update_dirty_skys ( ) ;
sky . free_sky ( p_rid ) ;
2022-07-19 04:17:58 +00:00
} else if ( RendererRD : : Fog : : get_singleton ( ) - > owns_fog_volume_instance ( p_rid ) ) {
RendererRD : : Fog : : get_singleton ( ) - > fog_instance_free ( p_rid ) ;
2019-08-18 22:40:52 +00:00
} else {
return false ;
}
return true ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : set_debug_draw_mode ( RS : : ViewportDebugDraw p_debug_draw ) {
2020-01-10 00:40:26 +00:00
debug_draw = p_debug_draw ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : update ( ) {
2021-02-13 12:08:08 +00:00
sky . update_dirty_skys ( ) ;
2019-08-26 20:43:58 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : set_time ( double p_time , double p_step ) {
2020-03-20 00:32:19 +00:00
time = p_time ;
2020-01-12 01:26:52 +00:00
time_step = p_step ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : screen_space_roughness_limiter_set_active ( bool p_enable , float p_amount , float p_limit ) {
2020-01-26 23:09:40 +00:00
screen_space_roughness_limiter = p_enable ;
2020-06-25 13:33:28 +00:00
screen_space_roughness_limiter_amount = p_amount ;
screen_space_roughness_limiter_limit = p_limit ;
2020-01-26 23:09:40 +00:00
}
2020-12-04 18:26:24 +00:00
bool RendererSceneRenderRD : : screen_space_roughness_limiter_is_active ( ) const {
2020-01-26 23:09:40 +00:00
return screen_space_roughness_limiter ;
}
2020-12-04 18:26:24 +00:00
float RendererSceneRenderRD : : screen_space_roughness_limiter_get_amount ( ) const {
2020-06-25 13:33:28 +00:00
return screen_space_roughness_limiter_amount ;
}
2020-12-04 18:26:24 +00:00
float RendererSceneRenderRD : : screen_space_roughness_limiter_get_limit ( ) const {
2020-06-25 13:33:28 +00:00
return screen_space_roughness_limiter_limit ;
2020-01-26 23:09:40 +00:00
}
2022-08-31 17:24:04 +00:00
TypedArray < Image > RendererSceneRenderRD : : bake_render_uv2 ( RID p_base , const TypedArray < RID > & p_material_overrides , const Size2i & p_image_size ) {
2020-05-01 12:34:23 +00:00
RD : : TextureFormat tf ;
tf . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tf . width = p_image_size . width ; // Always 64x64
tf . height = p_image_size . height ;
tf . usage_bits = RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
RID albedo_alpha_tex = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
RID normal_tex = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
RID orm_tex = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
tf . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
RID emission_tex = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
tf . format = RD : : DATA_FORMAT_R32_SFLOAT ;
RID depth_write_tex = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
tf . usage_bits = RD : : TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
tf . format = RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_D32_SFLOAT , RD : : TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT ) ? RD : : DATA_FORMAT_D32_SFLOAT : RD : : DATA_FORMAT_X8_D24_UNORM_PACK32 ;
RID depth_tex = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
Vector < RID > fb_tex ;
fb_tex . push_back ( albedo_alpha_tex ) ;
fb_tex . push_back ( normal_tex ) ;
fb_tex . push_back ( orm_tex ) ;
fb_tex . push_back ( emission_tex ) ;
fb_tex . push_back ( depth_write_tex ) ;
fb_tex . push_back ( depth_tex ) ;
RID fb = RD : : get_singleton ( ) - > framebuffer_create ( fb_tex ) ;
//RID sampled_light;
2022-07-23 14:01:53 +00:00
RenderGeometryInstance * gi = geometry_instance_create ( p_base ) ;
2022-09-30 01:55:32 +00:00
ERR_FAIL_NULL_V ( gi , TypedArray < Image > ( ) ) ;
2020-12-31 12:42:56 +00:00
2022-04-02 05:29:04 +00:00
uint32_t sc = RSG : : mesh_storage - > mesh_get_surface_count ( p_base ) ;
2020-12-31 12:42:56 +00:00
Vector < RID > materials ;
materials . resize ( sc ) ;
2020-05-01 12:34:23 +00:00
2020-12-31 12:42:56 +00:00
for ( uint32_t i = 0 ; i < sc ; i + + ) {
if ( i < ( uint32_t ) p_material_overrides . size ( ) ) {
materials . write [ i ] = p_material_overrides [ i ] ;
2020-05-01 12:34:23 +00:00
}
}
2022-07-23 14:01:53 +00:00
gi - > set_surface_materials ( materials ) ;
2020-12-31 12:42:56 +00:00
2020-12-23 16:52:58 +00:00
if ( cull_argument . size ( ) = = 0 ) {
cull_argument . push_back ( nullptr ) ;
}
2020-12-31 12:42:56 +00:00
cull_argument [ 0 ] = gi ;
2020-12-23 16:52:58 +00:00
_render_uv2 ( cull_argument , fb , Rect2i ( 0 , 0 , p_image_size . width , p_image_size . height ) ) ;
2020-05-01 12:34:23 +00:00
2020-12-31 12:42:56 +00:00
geometry_instance_free ( gi ) ;
2020-05-01 12:34:23 +00:00
TypedArray < Image > ret ;
{
PackedByteArray data = RD : : get_singleton ( ) - > texture_get_data ( albedo_alpha_tex , 0 ) ;
Ref < Image > img ;
2021-06-17 22:03:09 +00:00
img . instantiate ( ) ;
2020-05-01 12:34:23 +00:00
img - > create ( p_image_size . width , p_image_size . height , false , Image : : FORMAT_RGBA8 , data ) ;
RD : : get_singleton ( ) - > free ( albedo_alpha_tex ) ;
ret . push_back ( img ) ;
}
{
PackedByteArray data = RD : : get_singleton ( ) - > texture_get_data ( normal_tex , 0 ) ;
Ref < Image > img ;
2021-06-17 22:03:09 +00:00
img . instantiate ( ) ;
2020-05-01 12:34:23 +00:00
img - > create ( p_image_size . width , p_image_size . height , false , Image : : FORMAT_RGBA8 , data ) ;
RD : : get_singleton ( ) - > free ( normal_tex ) ;
ret . push_back ( img ) ;
}
{
PackedByteArray data = RD : : get_singleton ( ) - > texture_get_data ( orm_tex , 0 ) ;
Ref < Image > img ;
2021-06-17 22:03:09 +00:00
img . instantiate ( ) ;
2020-05-01 12:34:23 +00:00
img - > create ( p_image_size . width , p_image_size . height , false , Image : : FORMAT_RGBA8 , data ) ;
RD : : get_singleton ( ) - > free ( orm_tex ) ;
ret . push_back ( img ) ;
}
{
PackedByteArray data = RD : : get_singleton ( ) - > texture_get_data ( emission_tex , 0 ) ;
Ref < Image > img ;
2021-06-17 22:03:09 +00:00
img . instantiate ( ) ;
2020-05-01 12:34:23 +00:00
img - > create ( p_image_size . width , p_image_size . height , false , Image : : FORMAT_RGBAH , data ) ;
RD : : get_singleton ( ) - > free ( emission_tex ) ;
ret . push_back ( img ) ;
}
RD : : get_singleton ( ) - > free ( depth_write_tex ) ;
RD : : get_singleton ( ) - > free ( depth_tex ) ;
return ret ;
}
2020-12-04 18:26:24 +00:00
void RendererSceneRenderRD : : sdfgi_set_debug_probe_select ( const Vector3 & p_position , const Vector3 & p_dir ) {
2021-02-13 12:08:08 +00:00
gi . sdfgi_debug_probe_pos = p_position ;
gi . sdfgi_debug_probe_dir = p_dir ;
2020-06-25 13:33:28 +00:00
}
2020-12-04 18:26:24 +00:00
RendererSceneRenderRD * RendererSceneRenderRD : : singleton = nullptr ;
2020-03-20 00:32:19 +00:00
2022-02-11 11:33:54 +00:00
bool RendererSceneRenderRD : : is_vrs_supported ( ) const {
return RD : : get_singleton ( ) - > has_feature ( RD : : SUPPORTS_ATTACHMENT_VRS ) ;
}
2021-03-23 10:46:31 +00:00
bool RendererSceneRenderRD : : is_dynamic_gi_supported ( ) const {
// usable by default (unless low end = true)
return true ;
}
bool RendererSceneRenderRD : : is_volumetric_supported ( ) const {
// usable by default (unless low end = true)
return true ;
2020-12-07 21:27:38 +00:00
}
2021-03-10 11:23:55 +00:00
uint32_t RendererSceneRenderRD : : get_max_elements ( ) const {
return GLOBAL_GET ( " rendering/limits/cluster_builder/max_clustered_elements " ) ;
}
2022-06-21 00:08:33 +00:00
RendererSceneRenderRD : : RendererSceneRenderRD ( ) {
2020-03-20 00:32:19 +00:00
singleton = this ;
2021-08-16 17:51:29 +00:00
}
void RendererSceneRenderRD : : init ( ) {
max_cluster_elements = get_max_elements ( ) ;
2022-09-12 09:44:48 +00:00
RendererRD : : LightStorage : : get_singleton ( ) - > set_max_cluster_elements ( max_cluster_elements ) ;
2019-08-26 20:43:58 +00:00
2022-09-12 09:44:48 +00:00
/* Forward ID */
forward_id_storage = create_forward_id_storage ( ) ;
2021-01-24 19:00:20 +00:00
2020-03-20 00:32:19 +00:00
/* SKY SHADER */
2022-06-21 00:08:33 +00:00
sky . init ( ) ;
2020-03-20 00:32:19 +00:00
2021-03-11 03:35:05 +00:00
/* GI */
2021-04-07 11:06:48 +00:00
if ( is_dynamic_gi_supported ( ) ) {
2022-06-21 00:08:33 +00:00
gi . init ( & sky ) ;
2020-06-25 13:33:28 +00:00
}
2021-02-01 20:43:08 +00:00
{ //decals
2022-09-12 09:44:48 +00:00
RendererRD : : TextureStorage : : get_singleton ( ) - > set_max_decals ( max_cluster_elements ) ;
2020-07-22 23:39:09 +00:00
}
{ //lights
}
2021-04-07 11:06:48 +00:00
if ( is_volumetric_supported ( ) ) {
2022-09-12 09:44:48 +00:00
RendererRD : : Fog : : get_singleton ( ) - > init_fog_shader ( RendererRD : : LightStorage : : get_singleton ( ) - > get_max_directional_lights ( ) , get_roughness_layers ( ) , is_using_radiance_cubemap_array ( ) ) ;
2020-08-13 01:21:01 +00:00
}
2022-07-31 23:20:24 +00:00
RSG : : camera_attributes - > camera_attributes_set_dof_blur_bokeh_shape ( RS : : DOFBokehShape ( int ( GLOBAL_GET ( " rendering/camera/depth_of_field/depth_of_field_bokeh_shape " ) ) ) ) ;
RSG : : camera_attributes - > camera_attributes_set_dof_blur_quality ( RS : : DOFBlurQuality ( int ( GLOBAL_GET ( " rendering/camera/depth_of_field/depth_of_field_bokeh_quality " ) ) ) , GLOBAL_GET ( " rendering/camera/depth_of_field/depth_of_field_use_jitter " ) ) ;
use_physical_light_units = GLOBAL_GET ( " rendering/lights_and_shadows/use_physical_light_units " ) ;
2021-02-17 16:44:49 +00:00
screen_space_roughness_limiter = GLOBAL_GET ( " rendering/anti_aliasing/screen_space_roughness_limiter/enabled " ) ;
screen_space_roughness_limiter_amount = GLOBAL_GET ( " rendering/anti_aliasing/screen_space_roughness_limiter/amount " ) ;
screen_space_roughness_limiter_limit = GLOBAL_GET ( " rendering/anti_aliasing/screen_space_roughness_limiter/limit " ) ;
glow_bicubic_upscale = int ( GLOBAL_GET ( " rendering/environment/glow/upscale_mode " ) ) > 0 ;
glow_high_quality = GLOBAL_GET ( " rendering/environment/glow/use_high_quality " ) ;
2021-08-03 07:07:32 +00:00
2020-04-10 09:30:36 +00:00
directional_penumbra_shadow_kernel = memnew_arr ( float , 128 ) ;
directional_soft_shadow_kernel = memnew_arr ( float , 128 ) ;
penumbra_shadow_kernel = memnew_arr ( float , 128 ) ;
soft_shadow_kernel = memnew_arr ( float , 128 ) ;
2022-07-31 23:20:24 +00:00
positional_soft_shadow_filter_set_quality ( RS : : ShadowQuality ( int ( GLOBAL_GET ( " rendering/lights_and_shadows/positional_shadow/soft_shadow_filter_quality " ) ) ) ) ;
directional_soft_shadow_filter_set_quality ( RS : : ShadowQuality ( int ( GLOBAL_GET ( " rendering/lights_and_shadows/directional_shadow/soft_shadow_filter_quality " ) ) ) ) ;
2020-08-13 01:21:01 +00:00
2021-02-17 16:44:49 +00:00
environment_set_volumetric_fog_volume_size ( GLOBAL_GET ( " rendering/environment/volumetric_fog/volume_size " ) , GLOBAL_GET ( " rendering/environment/volumetric_fog/volume_depth " ) ) ;
environment_set_volumetric_fog_filter_active ( GLOBAL_GET ( " rendering/environment/volumetric_fog/use_filter " ) ) ;
2020-12-23 16:52:58 +00:00
2021-07-19 19:41:55 +00:00
decals_set_filter ( RS : : DecalFilter ( int ( GLOBAL_GET ( " rendering/textures/decals/filter " ) ) ) ) ;
light_projectors_set_filter ( RS : : LightProjectorFilter ( int ( GLOBAL_GET ( " rendering/textures/light_projectors/filter " ) ) ) ) ;
2020-12-23 16:52:58 +00:00
cull_argument . set_page_pool ( & cull_argument_pool ) ;
2022-04-27 03:42:50 +00:00
2022-04-29 07:10:54 +00:00
bool can_use_storage = _render_buffers_can_be_storage ( ) ;
bokeh_dof = memnew ( RendererRD : : BokehDOF ( ! can_use_storage ) ) ;
copy_effects = memnew ( RendererRD : : CopyEffects ( ! can_use_storage ) ) ;
2022-04-27 03:42:50 +00:00
tone_mapper = memnew ( RendererRD : : ToneMapper ) ;
2022-02-11 11:33:54 +00:00
vrs = memnew ( RendererRD : : VRS ) ;
2022-06-28 09:10:36 +00:00
if ( can_use_storage ) {
2022-08-04 08:40:39 +00:00
fsr = memnew ( RendererRD : : FSR ) ;
2022-06-28 09:10:36 +00:00
}
2019-08-18 22:40:52 +00:00
}
2019-09-07 01:51:27 +00:00
2020-12-04 18:26:24 +00:00
RendererSceneRenderRD : : ~ RendererSceneRenderRD ( ) {
2022-09-12 09:44:48 +00:00
if ( forward_id_storage ) {
memdelete ( forward_id_storage ) ;
}
2022-04-29 07:10:54 +00:00
if ( bokeh_dof ) {
memdelete ( bokeh_dof ) ;
}
if ( copy_effects ) {
memdelete ( copy_effects ) ;
}
2022-04-27 03:42:50 +00:00
if ( tone_mapper ) {
memdelete ( tone_mapper ) ;
}
2022-02-11 11:33:54 +00:00
if ( vrs ) {
memdelete ( vrs ) ;
}
2022-08-04 08:40:39 +00:00
if ( fsr ) {
memdelete ( fsr ) ;
}
2019-10-03 20:39:08 +00:00
2021-02-13 12:08:08 +00:00
if ( sky . sky_scene_state . uniform_set . is_valid ( ) & & RD : : get_singleton ( ) - > uniform_set_is_valid ( sky . sky_scene_state . uniform_set ) ) {
RD : : get_singleton ( ) - > free ( sky . sky_scene_state . uniform_set ) ;
2020-03-20 00:32:19 +00:00
}
2021-04-07 11:06:48 +00:00
if ( is_dynamic_gi_supported ( ) ) {
2021-02-13 12:08:08 +00:00
gi . free ( ) ;
2021-10-03 11:28:55 +00:00
}
2020-06-27 13:08:20 +00:00
2021-10-03 11:28:55 +00:00
if ( is_volumetric_supported ( ) ) {
2022-07-19 04:17:58 +00:00
RendererRD : : Fog : : get_singleton ( ) - > free_fog_shader ( ) ;
2022-03-21 11:25:25 +00:00
}
2020-04-10 09:30:36 +00:00
memdelete_arr ( directional_penumbra_shadow_kernel ) ;
memdelete_arr ( directional_soft_shadow_kernel ) ;
memdelete_arr ( penumbra_shadow_kernel ) ;
memdelete_arr ( soft_shadow_kernel ) ;
2020-07-22 23:39:09 +00:00
2022-09-12 09:44:48 +00:00
RSG : : light_storage - > directional_shadow_atlas_set_size ( 0 ) ;
2020-12-23 16:52:58 +00:00
cull_argument . reset ( ) ; //avoid exit error
2019-09-07 01:51:27 +00:00
}