2021-02-13 12:08:08 +00:00
/**************************************************************************/
2022-05-20 02:52:19 +00:00
/* gi.cpp */
2021-02-13 12:08:08 +00:00
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
2022-05-20 02:52:19 +00:00
# include "gi.h"
2021-02-13 12:08:08 +00:00
# include "core/config/project_settings.h"
2022-05-20 02:52:19 +00:00
# include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
2021-02-13 12:08:08 +00:00
# include "servers/rendering/renderer_rd/renderer_scene_render_rd.h"
2022-04-12 11:41:50 +00:00
# include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
2022-08-04 08:40:39 +00:00
# include "servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h"
2022-04-07 14:00:51 +00:00
# include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
2021-02-13 12:08:08 +00:00
# include "servers/rendering/rendering_server_default.h"
2022-05-20 02:52:19 +00:00
using namespace RendererRD ;
const Vector3i GI : : SDFGI : : Cascade : : DIRTY_ALL = Vector3i ( 0x7FFFFFFF , 0x7FFFFFFF , 0x7FFFFFFF ) ;
GI * GI : : singleton = nullptr ;
////////////////////////////////////////////////////////////////////////////////
// VOXEL GI STORAGE
RID GI : : voxel_gi_allocate ( ) {
return voxel_gi_owner . allocate_rid ( ) ;
}
void GI : : voxel_gi_free ( RID p_voxel_gi ) {
voxel_gi_allocate_data ( p_voxel_gi , Transform3D ( ) , AABB ( ) , Vector3i ( ) , Vector < uint8_t > ( ) , Vector < uint8_t > ( ) , Vector < uint8_t > ( ) , Vector < int > ( ) ) ; //deallocate
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
voxel_gi - > dependency . deleted_notify ( p_voxel_gi ) ;
voxel_gi_owner . free ( p_voxel_gi ) ;
}
void GI : : voxel_gi_initialize ( RID p_voxel_gi ) {
voxel_gi_owner . initialize_rid ( p_voxel_gi , VoxelGI ( ) ) ;
}
void GI : : voxel_gi_allocate_data ( RID p_voxel_gi , const Transform3D & p_to_cell_xform , const AABB & p_aabb , const Vector3i & p_octree_size , const Vector < uint8_t > & p_octree_cells , const Vector < uint8_t > & p_data_cells , const Vector < uint8_t > & p_distance_field , const Vector < int > & p_level_counts ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-05-20 02:52:19 +00:00
if ( voxel_gi - > octree_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( voxel_gi - > octree_buffer ) ;
RD : : get_singleton ( ) - > free ( voxel_gi - > data_buffer ) ;
if ( voxel_gi - > sdf_texture . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( voxel_gi - > sdf_texture ) ;
}
voxel_gi - > sdf_texture = RID ( ) ;
voxel_gi - > octree_buffer = RID ( ) ;
voxel_gi - > data_buffer = RID ( ) ;
voxel_gi - > octree_buffer_size = 0 ;
voxel_gi - > data_buffer_size = 0 ;
voxel_gi - > cell_count = 0 ;
}
voxel_gi - > to_cell_xform = p_to_cell_xform ;
voxel_gi - > bounds = p_aabb ;
voxel_gi - > octree_size = p_octree_size ;
voxel_gi - > level_counts = p_level_counts ;
if ( p_octree_cells . size ( ) ) {
ERR_FAIL_COND ( p_octree_cells . size ( ) % 32 ! = 0 ) ; //cells size must be a multiple of 32
uint32_t cell_count = p_octree_cells . size ( ) / 32 ;
ERR_FAIL_COND ( p_data_cells . size ( ) ! = ( int ) cell_count * 16 ) ; //see that data size matches
voxel_gi - > cell_count = cell_count ;
voxel_gi - > octree_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( p_octree_cells . size ( ) , p_octree_cells ) ;
voxel_gi - > octree_buffer_size = p_octree_cells . size ( ) ;
voxel_gi - > data_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( p_data_cells . size ( ) , p_data_cells ) ;
voxel_gi - > data_buffer_size = p_data_cells . size ( ) ;
if ( p_distance_field . size ( ) ) {
RD : : TextureFormat tf ;
tf . format = RD : : DATA_FORMAT_R8_UNORM ;
tf . width = voxel_gi - > octree_size . x ;
tf . height = voxel_gi - > octree_size . y ;
tf . depth = voxel_gi - > octree_size . z ;
tf . texture_type = RD : : TEXTURE_TYPE_3D ;
tf . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
Vector < Vector < uint8_t > > s ;
s . push_back ( p_distance_field ) ;
voxel_gi - > sdf_texture = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) , s ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( voxel_gi - > sdf_texture , " VoxelGI SDF Texture " ) ;
2022-05-20 02:52:19 +00:00
}
#if 0
{
RD : : TextureFormat tf ;
tf . format = RD : : DATA_FORMAT_R8_UNORM ;
tf . width = voxel_gi - > octree_size . x ;
tf . height = voxel_gi - > octree_size . y ;
tf . depth = voxel_gi - > octree_size . z ;
tf . type = RD : : TEXTURE_TYPE_3D ;
tf . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_TO_BIT ;
tf . shareable_formats . push_back ( RD : : DATA_FORMAT_R8_UNORM ) ;
tf . shareable_formats . push_back ( RD : : DATA_FORMAT_R8_UINT ) ;
voxel_gi - > sdf_texture = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( voxel_gi - > sdf_texture , " VoxelGI SDF Texture " ) ;
2022-05-20 02:52:19 +00:00
}
RID shared_tex ;
{
RD : : TextureView tv ;
tv . format_override = RD : : DATA_FORMAT_R8_UINT ;
shared_tex = RD : : get_singleton ( ) - > texture_create_shared ( tv , voxel_gi - > sdf_texture ) ;
}
//update SDF texture
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 1 ;
u . append_id ( voxel_gi - > octree_buffer ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 2 ;
u . append_id ( voxel_gi - > data_buffer ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 3 ;
u . append_id ( shared_tex ) ;
uniforms . push_back ( u ) ;
}
RID uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , voxel_gi_sdf_shader_version_shader , 0 ) ;
{
uint32_t push_constant [ 4 ] = { 0 , 0 , 0 , 0 } ;
for ( int i = 0 ; i < voxel_gi - > level_counts . size ( ) - 1 ; i + + ) {
push_constant [ 0 ] + = voxel_gi - > level_counts [ i ] ;
}
push_constant [ 1 ] = push_constant [ 0 ] + voxel_gi - > level_counts [ voxel_gi - > level_counts . size ( ) - 1 ] ;
print_line ( " offset: " + itos ( push_constant [ 0 ] ) ) ;
print_line ( " size: " + itos ( push_constant [ 1 ] ) ) ;
//create SDF
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , voxel_gi_sdf_shader_pipeline ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , uniform_set , 0 ) ;
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , push_constant , sizeof ( uint32_t ) * 4 ) ;
RD : : get_singleton ( ) - > compute_list_dispatch ( compute_list , voxel_gi - > octree_size . x / 4 , voxel_gi - > octree_size . y / 4 , voxel_gi - > octree_size . z / 4 ) ;
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
RD : : get_singleton ( ) - > free ( uniform_set ) ;
RD : : get_singleton ( ) - > free ( shared_tex ) ;
}
# endif
}
voxel_gi - > version + + ;
voxel_gi - > data_version + + ;
2022-06-21 00:08:33 +00:00
voxel_gi - > dependency . changed_notify ( Dependency : : DEPENDENCY_CHANGED_AABB ) ;
2022-05-20 02:52:19 +00:00
}
AABB GI : : voxel_gi_get_bounds ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , AABB ( ) ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > bounds ;
}
Vector3i GI : : voxel_gi_get_octree_size ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , Vector3i ( ) ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > octree_size ;
}
Vector < uint8_t > GI : : voxel_gi_get_octree_cells ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , Vector < uint8_t > ( ) ) ;
2022-05-20 02:52:19 +00:00
if ( voxel_gi - > octree_buffer . is_valid ( ) ) {
return RD : : get_singleton ( ) - > buffer_get_data ( voxel_gi - > octree_buffer ) ;
}
return Vector < uint8_t > ( ) ;
}
Vector < uint8_t > GI : : voxel_gi_get_data_cells ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , Vector < uint8_t > ( ) ) ;
2022-05-20 02:52:19 +00:00
if ( voxel_gi - > data_buffer . is_valid ( ) ) {
return RD : : get_singleton ( ) - > buffer_get_data ( voxel_gi - > data_buffer ) ;
}
return Vector < uint8_t > ( ) ;
}
Vector < uint8_t > GI : : voxel_gi_get_distance_field ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , Vector < uint8_t > ( ) ) ;
2022-05-20 02:52:19 +00:00
if ( voxel_gi - > data_buffer . is_valid ( ) ) {
return RD : : get_singleton ( ) - > texture_get_data ( voxel_gi - > sdf_texture , 0 ) ;
}
return Vector < uint8_t > ( ) ;
}
Vector < int > GI : : voxel_gi_get_level_counts ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , Vector < int > ( ) ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > level_counts ;
}
Transform3D GI : : voxel_gi_get_to_cell_xform ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , Transform3D ( ) ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > to_cell_xform ;
}
void GI : : voxel_gi_set_dynamic_range ( RID p_voxel_gi , float p_range ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-05-20 02:52:19 +00:00
voxel_gi - > dynamic_range = p_range ;
voxel_gi - > version + + ;
}
float GI : : voxel_gi_get_dynamic_range ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > dynamic_range ;
}
void GI : : voxel_gi_set_propagation ( RID p_voxel_gi , float p_range ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-05-20 02:52:19 +00:00
voxel_gi - > propagation = p_range ;
voxel_gi - > version + + ;
}
float GI : : voxel_gi_get_propagation ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > propagation ;
}
void GI : : voxel_gi_set_energy ( RID p_voxel_gi , float p_energy ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-05-20 02:52:19 +00:00
voxel_gi - > energy = p_energy ;
}
float GI : : voxel_gi_get_energy ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > energy ;
}
2022-07-31 23:20:24 +00:00
void GI : : voxel_gi_set_baked_exposure_normalization ( RID p_voxel_gi , float p_baked_exposure ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-07-31 23:20:24 +00:00
voxel_gi - > baked_exposure = p_baked_exposure ;
}
float GI : : voxel_gi_get_baked_exposure_normalization ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-07-31 23:20:24 +00:00
return voxel_gi - > baked_exposure ;
}
2022-05-20 02:52:19 +00:00
void GI : : voxel_gi_set_bias ( RID p_voxel_gi , float p_bias ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-05-20 02:52:19 +00:00
voxel_gi - > bias = p_bias ;
}
float GI : : voxel_gi_get_bias ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > bias ;
}
void GI : : voxel_gi_set_normal_bias ( RID p_voxel_gi , float p_normal_bias ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-05-20 02:52:19 +00:00
voxel_gi - > normal_bias = p_normal_bias ;
}
float GI : : voxel_gi_get_normal_bias ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > normal_bias ;
}
void GI : : voxel_gi_set_interior ( RID p_voxel_gi , bool p_enable ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-05-20 02:52:19 +00:00
voxel_gi - > interior = p_enable ;
}
void GI : : voxel_gi_set_use_two_bounces ( RID p_voxel_gi , bool p_enable ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2022-05-20 02:52:19 +00:00
voxel_gi - > use_two_bounces = p_enable ;
voxel_gi - > version + + ;
}
bool GI : : voxel_gi_is_using_two_bounces ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , false ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > use_two_bounces ;
}
bool GI : : voxel_gi_is_interior ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > interior ;
}
uint32_t GI : : voxel_gi_get_version ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > version ;
}
uint32_t GI : : voxel_gi_get_data_version ( RID p_voxel_gi ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , 0 ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > data_version ;
}
RID GI : : voxel_gi_get_octree_buffer ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , RID ( ) ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > octree_buffer ;
}
RID GI : : voxel_gi_get_data_buffer ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , RID ( ) ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > data_buffer ;
}
RID GI : : voxel_gi_get_sdf_texture ( RID p_voxel_gi ) {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , RID ( ) ) ;
2022-05-20 02:52:19 +00:00
return voxel_gi - > sdf_texture ;
}
2021-02-13 12:08:08 +00:00
2022-08-04 08:40:39 +00:00
Dependency * GI : : voxel_gi_get_dependency ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , nullptr ) ;
2022-08-04 08:40:39 +00:00
return & voxel_gi - > dependency ;
}
2023-08-30 13:25:27 +00:00
void GI : : sdfgi_reset ( ) {
sdfgi_current_version + + ;
}
2021-02-13 12:08:08 +00:00
////////////////////////////////////////////////////////////////////////////////
// SDFGI
2023-08-22 10:45:23 +00:00
static RID create_clear_texture ( const RD : : TextureFormat & p_format , const String & p_name ) {
RID texture = RD : : get_singleton ( ) - > texture_create ( p_format , RD : : TextureView ( ) ) ;
ERR_FAIL_COND_V_MSG ( texture . is_null ( ) , RID ( ) , String ( " Cannot create texture: " ) + p_name ) ;
RD : : get_singleton ( ) - > set_resource_name ( texture , p_name ) ;
RD : : get_singleton ( ) - > texture_clear ( texture , Color ( 0 , 0 , 0 , 0 ) , 0 , p_format . mipmaps , 0 , p_format . array_layers ) ;
return texture ;
}
2022-07-27 06:14:23 +00:00
void GI : : SDFGI : : create ( RID p_env , const Vector3 & p_world_position , uint32_t p_requested_history_size , GI * p_gi ) {
2022-03-12 11:19:59 +00:00
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2022-04-12 11:41:50 +00:00
RendererRD : : MaterialStorage * material_storage = RendererRD : : MaterialStorage : : get_singleton ( ) ;
2021-02-13 12:08:08 +00:00
gi = p_gi ;
2022-07-27 06:14:23 +00:00
num_cascades = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_cascades ( p_env ) ;
min_cell_size = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_min_cell_size ( p_env ) ;
uses_occlusion = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_use_occlusion ( p_env ) ;
y_scale_mode = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_y_scale ( p_env ) ;
2021-05-23 17:10:26 +00:00
static const float y_scale [ 3 ] = { 2.0 , 1.5 , 1.0 } ;
2021-02-13 12:08:08 +00:00
y_mult = y_scale [ y_scale_mode ] ;
2023-08-30 13:25:27 +00:00
version = gi - > sdfgi_current_version ;
2022-01-13 18:46:14 +00:00
cascades . resize ( num_cascades ) ;
2021-02-13 12:08:08 +00:00
probe_axis_count = SDFGI : : PROBE_DIVISOR + 1 ;
solid_cell_ratio = gi - > sdfgi_solid_cell_ratio ;
solid_cell_count = uint32_t ( float ( cascade_size * cascade_size * cascade_size ) * solid_cell_ratio ) ;
float base_cell_size = min_cell_size ;
RD : : TextureFormat tf_sdf ;
tf_sdf . format = RD : : DATA_FORMAT_R8_UNORM ;
tf_sdf . width = cascade_size ; // Always 64x64
tf_sdf . height = cascade_size ;
tf_sdf . depth = cascade_size ;
tf_sdf . texture_type = RD : : TEXTURE_TYPE_3D ;
tf_sdf . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_TO_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
{
RD : : TextureFormat tf_render = tf_sdf ;
tf_render . format = RD : : DATA_FORMAT_R16_UINT ;
2023-08-22 10:45:23 +00:00
render_albedo = create_clear_texture ( tf_render , " SDFGI Render Albedo " ) ;
2021-02-13 12:08:08 +00:00
tf_render . format = RD : : DATA_FORMAT_R32_UINT ;
2023-08-22 10:45:23 +00:00
render_emission = create_clear_texture ( tf_render , " SDFGI Render Emission " ) ;
render_emission_aniso = create_clear_texture ( tf_render , " SDFGI Render Emission Aniso " ) ;
2021-02-13 12:08:08 +00:00
tf_render . format = RD : : DATA_FORMAT_R8_UNORM ; //at least its easy to visualize
for ( int i = 0 ; i < 8 ; i + + ) {
2023-08-22 10:45:23 +00:00
render_occlusion [ i ] = create_clear_texture ( tf_render , String ( " SDFGI Render Occlusion " ) + itos ( i ) ) ;
2021-02-13 12:08:08 +00:00
}
tf_render . format = RD : : DATA_FORMAT_R32_UINT ;
2023-08-22 10:45:23 +00:00
render_geom_facing = create_clear_texture ( tf_render , " SDFGI Render Geometry Facing " ) ;
2021-02-13 12:08:08 +00:00
tf_render . format = RD : : DATA_FORMAT_R8G8B8A8_UINT ;
2023-08-22 10:45:23 +00:00
render_sdf [ 0 ] = create_clear_texture ( tf_render , " SDFGI Render SDF 0 " ) ;
render_sdf [ 1 ] = create_clear_texture ( tf_render , " SDFGI Render SDF 1 " ) ;
2021-02-13 12:08:08 +00:00
tf_render . width / = 2 ;
tf_render . height / = 2 ;
tf_render . depth / = 2 ;
2023-08-22 10:45:23 +00:00
render_sdf_half [ 0 ] = create_clear_texture ( tf_render , " SDFGI Render SDF Half 0 " ) ;
render_sdf_half [ 1 ] = create_clear_texture ( tf_render , " SDFGI Render SDF Half 1 " ) ;
2021-02-13 12:08:08 +00:00
}
RD : : TextureFormat tf_occlusion = tf_sdf ;
tf_occlusion . format = RD : : DATA_FORMAT_R16_UINT ;
tf_occlusion . shareable_formats . push_back ( RD : : DATA_FORMAT_R16_UINT ) ;
tf_occlusion . shareable_formats . push_back ( RD : : DATA_FORMAT_R4G4B4A4_UNORM_PACK16 ) ;
tf_occlusion . depth * = cascades . size ( ) ; //use depth for occlusion slices
tf_occlusion . width * = 2 ; //use width for the other half
RD : : TextureFormat tf_light = tf_sdf ;
tf_light . format = RD : : DATA_FORMAT_R32_UINT ;
tf_light . shareable_formats . push_back ( RD : : DATA_FORMAT_R32_UINT ) ;
tf_light . shareable_formats . push_back ( RD : : DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32 ) ;
RD : : TextureFormat tf_aniso0 = tf_sdf ;
tf_aniso0 . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
RD : : TextureFormat tf_aniso1 = tf_sdf ;
tf_aniso1 . format = RD : : DATA_FORMAT_R8G8_UNORM ;
int passes = nearest_shift ( cascade_size ) - 1 ;
//store lightprobe SH
RD : : TextureFormat tf_probes ;
tf_probes . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
tf_probes . width = probe_axis_count * probe_axis_count ;
tf_probes . height = probe_axis_count * SDFGI : : SH_SIZE ;
tf_probes . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_TO_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
tf_probes . texture_type = RD : : TEXTURE_TYPE_2D_ARRAY ;
history_size = p_requested_history_size ;
RD : : TextureFormat tf_probe_history = tf_probes ;
tf_probe_history . format = RD : : DATA_FORMAT_R16G16B16A16_SINT ; //signed integer because SH are signed
tf_probe_history . array_layers = history_size ;
RD : : TextureFormat tf_probe_average = tf_probes ;
tf_probe_average . format = RD : : DATA_FORMAT_R32G32B32A32_SINT ; //signed integer because SH are signed
tf_probe_average . texture_type = RD : : TEXTURE_TYPE_2D ;
2023-08-22 10:45:23 +00:00
lightprobe_history_scroll = create_clear_texture ( tf_probe_history , " SDFGI LightProbe History Scroll " ) ;
lightprobe_average_scroll = create_clear_texture ( tf_probe_average , " SDFGI LightProbe Average Scroll " ) ;
2021-02-13 12:08:08 +00:00
{
//octahedral lightprobes
RD : : TextureFormat tf_octprobes = tf_probes ;
tf_octprobes . array_layers = cascades . size ( ) * 2 ;
tf_octprobes . format = RD : : DATA_FORMAT_R32_UINT ; //pack well with RGBE
tf_octprobes . width = probe_axis_count * probe_axis_count * ( SDFGI : : LIGHTPROBE_OCT_SIZE + 2 ) ;
tf_octprobes . height = probe_axis_count * ( SDFGI : : LIGHTPROBE_OCT_SIZE + 2 ) ;
tf_octprobes . shareable_formats . push_back ( RD : : DATA_FORMAT_R32_UINT ) ;
tf_octprobes . shareable_formats . push_back ( RD : : DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32 ) ;
//lightprobe texture is an octahedral texture
2023-08-22 10:45:23 +00:00
lightprobe_data = create_clear_texture ( tf_octprobes , " SDFGI LightProbe Data " ) ;
2021-02-13 12:08:08 +00:00
RD : : TextureView tv ;
tv . format_override = RD : : DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32 ;
lightprobe_texture = RD : : get_singleton ( ) - > texture_create_shared ( tv , lightprobe_data ) ;
//texture handling ambient data, to integrate with volumetric foc
RD : : TextureFormat tf_ambient = tf_probes ;
tf_ambient . array_layers = cascades . size ( ) ;
tf_ambient . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ; //pack well with RGBE
tf_ambient . width = probe_axis_count * probe_axis_count ;
tf_ambient . height = probe_axis_count ;
tf_ambient . texture_type = RD : : TEXTURE_TYPE_2D_ARRAY ;
//lightprobe texture is an octahedral texture
2023-08-22 10:45:23 +00:00
ambient_texture = create_clear_texture ( tf_ambient , " SDFGI Ambient Texture " ) ;
2021-02-13 12:08:08 +00:00
}
cascades_ubo = RD : : get_singleton ( ) - > uniform_buffer_create ( sizeof ( SDFGI : : Cascade : : UBO ) * SDFGI : : MAX_CASCADES ) ;
2023-08-22 10:45:23 +00:00
occlusion_data = create_clear_texture ( tf_occlusion , " SDFGI Occlusion Data " ) ;
2021-02-13 12:08:08 +00:00
{
RD : : TextureView tv ;
tv . format_override = RD : : DATA_FORMAT_R4G4B4A4_UNORM_PACK16 ;
occlusion_texture = RD : : get_singleton ( ) - > texture_create_shared ( tv , occlusion_data ) ;
}
2022-12-29 00:24:45 +00:00
for ( SDFGI : : Cascade & cascade : cascades ) {
2021-02-13 12:08:08 +00:00
/* 3D Textures */
2023-08-22 10:45:23 +00:00
cascade . sdf_tex = create_clear_texture ( tf_sdf , " SDFGI Cascade SDF Texture " ) ;
2021-02-13 12:08:08 +00:00
2023-08-22 10:45:23 +00:00
cascade . light_data = create_clear_texture ( tf_light , " SDFGI Cascade Light Data " ) ;
2021-02-13 12:08:08 +00:00
2023-08-22 10:45:23 +00:00
cascade . light_aniso_0_tex = create_clear_texture ( tf_aniso0 , " SDFGI Cascade Light Aniso 0 Texture " ) ;
cascade . light_aniso_1_tex = create_clear_texture ( tf_aniso1 , " SDFGI Cascade Light Aniso 1 Texture " ) ;
2021-02-13 12:08:08 +00:00
{
RD : : TextureView tv ;
tv . format_override = RD : : DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32 ;
cascade . light_tex = RD : : get_singleton ( ) - > texture_create_shared ( tv , cascade . light_data ) ;
}
cascade . cell_size = base_cell_size ;
Vector3 world_position = p_world_position ;
world_position . y * = y_mult ;
int32_t probe_cells = cascade_size / SDFGI : : PROBE_DIVISOR ;
Vector3 probe_size = Vector3 ( 1 , 1 , 1 ) * cascade . cell_size * probe_cells ;
Vector3i probe_pos = Vector3i ( ( world_position / probe_size + Vector3 ( 0.5 , 0.5 , 0.5 ) ) . floor ( ) ) ;
cascade . position = probe_pos * probe_cells ;
cascade . dirty_regions = SDFGI : : Cascade : : DIRTY_ALL ;
base_cell_size * = 2.0 ;
/* Probe History */
cascade . lightprobe_history_tex = RD : : get_singleton ( ) - > texture_create ( tf_probe_history , RD : : TextureView ( ) ) ;
2023-08-22 10:45:23 +00:00
RD : : get_singleton ( ) - > set_resource_name ( cascade . lightprobe_history_tex , " SDFGI Cascade LightProbe History Texture " ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > texture_clear ( cascade . lightprobe_history_tex , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , tf_probe_history . array_layers ) ; //needs to be cleared for average to work
cascade . lightprobe_average_tex = RD : : get_singleton ( ) - > texture_create ( tf_probe_average , RD : : TextureView ( ) ) ;
2023-08-22 10:45:23 +00:00
RD : : get_singleton ( ) - > set_resource_name ( cascade . lightprobe_average_tex , " SDFGI Cascade LightProbe Average Texture " ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > texture_clear ( cascade . lightprobe_average_tex , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , 1 ) ; //needs to be cleared for average to work
/* Buffers */
cascade . solid_cell_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( SDFGI : : Cascade : : SolidCell ) * solid_cell_count ) ;
2023-11-24 11:23:22 +00:00
cascade . solid_cell_dispatch_buffer_storage = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( uint32_t ) * 4 , Vector < uint8_t > ( ) ) ;
cascade . solid_cell_dispatch_buffer_call = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( uint32_t ) * 4 , Vector < uint8_t > ( ) , RD : : STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT ) ;
2021-03-04 00:53:09 +00:00
cascade . lights_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( SDFGIShader : : Light ) * MAX ( SDFGI : : MAX_STATIC_LIGHTS , SDFGI : : MAX_DYNAMIC_LIGHTS ) ) ;
2021-02-13 12:08:08 +00:00
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf [ ( passes & 1 ) ? 1 : 0 ] ) ; //if passes are even, we read from buffer 0, else we read from buffer 1
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_albedo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 3 ;
for ( int j = 0 ; j < 8 ; j + + ) {
2022-03-06 11:57:09 +00:00
u . append_id ( render_occlusion [ j ] ) ;
2021-02-13 12:08:08 +00:00
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 4 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_emission ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 5 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_emission_aniso ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 6 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_geom_facing ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 7 ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascade . sdf_tex ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 8 ;
2022-03-06 11:57:09 +00:00
u . append_id ( occlusion_data ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 10 ;
2023-11-24 11:23:22 +00:00
u . append_id ( cascade . solid_cell_dispatch_buffer_storage ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 11 ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascade . solid_cell_buffer ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-03-04 00:53:09 +00:00
cascade . sdf_store_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_STORE ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_albedo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_geom_facing ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 3 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_emission ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 4 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_emission_aniso ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 5 ;
2023-11-24 11:23:22 +00:00
u . append_id ( cascade . solid_cell_dispatch_buffer_storage ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 6 ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascade . solid_cell_buffer ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-03-04 00:53:09 +00:00
cascade . scroll_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_SCROLL ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
for ( int j = 0 ; j < 8 ; j + + ) {
2022-03-06 11:57:09 +00:00
u . append_id ( render_occlusion [ j ] ) ;
2021-02-13 12:08:08 +00:00
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( occlusion_data ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-03-04 00:53:09 +00:00
cascade . scroll_occlusion_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_SCROLL_OCCLUSION ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
}
//direct light
2022-12-29 00:24:45 +00:00
for ( SDFGI : : Cascade & cascade : cascades ) {
2021-02-13 12:08:08 +00:00
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 1 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
if ( j < cascades . size ( ) ) {
2022-03-06 11:57:09 +00:00
u . append_id ( cascades [ j ] . sdf_tex ) ;
2021-02-13 12:08:08 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2021-02-13 12:08:08 +00:00
}
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 2 ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
2022-04-12 11:41:50 +00:00
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 3 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2023-11-24 11:23:22 +00:00
u . append_id ( cascade . solid_cell_dispatch_buffer_storage ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 4 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascade . solid_cell_buffer ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 5 ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascade . light_data ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 6 ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascade . light_aniso_0_tex ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 7 ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascade . light_aniso_1_tex ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 8 ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascades_ubo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 9 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascade . lights_buffer ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 10 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2022-03-06 11:57:09 +00:00
u . append_id ( lightprobe_texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 11 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2022-03-06 11:57:09 +00:00
u . append_id ( occlusion_texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2022-07-09 11:27:24 +00:00
cascade . sdf_direct_light_static_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . direct_light . version_get_shader ( gi - > sdfgi_shader . direct_light_shader , SDFGIShader : : DIRECT_LIGHT_MODE_STATIC ) , 0 ) ;
cascade . sdf_direct_light_dynamic_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . direct_light . version_get_shader ( gi - > sdfgi_shader . direct_light_shader , SDFGIShader : : DIRECT_LIGHT_MODE_DYNAMIC ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
//preprocess initialize uniform set
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_albedo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf [ 0 ] ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-03-04 00:53:09 +00:00
sdf_initialize_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_INITIALIZE ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_albedo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf_half [ 0 ] ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-03-04 00:53:09 +00:00
sdf_initialize_half_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
//jump flood uniform set
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf [ 0 ] ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf [ 1 ] ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-03-04 00:53:09 +00:00
jump_flood_uniform_set [ 0 ] = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_JUMP_FLOOD ) , 0 ) ;
2022-03-06 11:57:09 +00:00
RID aux0 = uniforms . write [ 0 ] . get_id ( 0 ) ;
RID aux1 = uniforms . write [ 1 ] . get_id ( 0 ) ;
uniforms . write [ 0 ] . set_id ( 0 , aux1 ) ;
uniforms . write [ 1 ] . set_id ( 0 , aux0 ) ;
2021-03-04 00:53:09 +00:00
jump_flood_uniform_set [ 1 ] = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_JUMP_FLOOD ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
//jump flood half uniform set
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf_half [ 0 ] ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf_half [ 1 ] ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-03-04 00:53:09 +00:00
jump_flood_half_uniform_set [ 0 ] = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_JUMP_FLOOD ) , 0 ) ;
2022-03-06 11:57:09 +00:00
RID aux0 = uniforms . write [ 0 ] . get_id ( 0 ) ;
RID aux1 = uniforms . write [ 1 ] . get_id ( 0 ) ;
uniforms . write [ 0 ] . set_id ( 0 , aux1 ) ;
uniforms . write [ 1 ] . set_id ( 0 , aux0 ) ;
2021-03-04 00:53:09 +00:00
jump_flood_half_uniform_set [ 1 ] = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_JUMP_FLOOD ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
//upscale half size sdf
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_albedo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf_half [ ( passes & 1 ) ? 0 : 1 ] ) ; //reverse pass order because half size
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 3 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_sdf [ ( passes & 1 ) ? 0 : 1 ] ) ; //reverse pass order because it needs an extra JFA pass
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
upscale_jfa_uniform_set_index = ( passes & 1 ) ? 0 : 1 ;
2021-03-04 00:53:09 +00:00
sdf_upscale_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_UPSCALE ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
//occlusion uniform set
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 1 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_albedo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 2 ;
for ( int i = 0 ; i < 8 ; i + + ) {
2022-03-06 11:57:09 +00:00
u . append_id ( render_occlusion [ i ] ) ;
2021-02-13 12:08:08 +00:00
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 3 ;
2022-03-06 11:57:09 +00:00
u . append_id ( render_geom_facing ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-03-04 00:53:09 +00:00
occlusion_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . preprocess . version_get_shader ( gi - > sdfgi_shader . preprocess_shader , SDFGIShader : : PRE_PROCESS_OCCLUSION ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
for ( uint32_t i = 0 ; i < cascades . size ( ) ; i + + ) {
//integrate uniform
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 1 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
if ( j < cascades . size ( ) ) {
2022-03-06 11:57:09 +00:00
u . append_id ( cascades [ j ] . sdf_tex ) ;
2021-02-13 12:08:08 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2021-02-13 12:08:08 +00:00
}
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 2 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
if ( j < cascades . size ( ) ) {
2022-03-06 11:57:09 +00:00
u . append_id ( cascades [ j ] . light_tex ) ;
2021-02-13 12:08:08 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2021-02-13 12:08:08 +00:00
}
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 3 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
if ( j < cascades . size ( ) ) {
2022-03-06 11:57:09 +00:00
u . append_id ( cascades [ j ] . light_aniso_0_tex ) ;
2021-02-13 12:08:08 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2021-02-13 12:08:08 +00:00
}
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 4 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
if ( j < cascades . size ( ) ) {
2022-03-06 11:57:09 +00:00
u . append_id ( cascades [ j ] . light_aniso_1_tex ) ;
2021-02-13 12:08:08 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2021-02-13 12:08:08 +00:00
}
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 6 ;
2022-04-12 11:41:50 +00:00
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
u . binding = 7 ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascades_ubo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 8 ;
2022-03-06 11:57:09 +00:00
u . append_id ( lightprobe_data ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 9 ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascades [ i ] . lightprobe_history_tex ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 10 ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascades [ i ] . lightprobe_average_tex ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 11 ;
2022-03-06 11:57:09 +00:00
u . append_id ( lightprobe_history_scroll ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 12 ;
2022-03-06 11:57:09 +00:00
u . append_id ( lightprobe_average_scroll ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 13 ;
RID parent_average ;
2022-01-13 18:46:14 +00:00
if ( cascades . size ( ) = = 1 ) {
// If there is only one SDFGI cascade, we can't use the previous cascade for blending.
parent_average = cascades [ i ] . lightprobe_average_tex ;
} else if ( i < cascades . size ( ) - 1 ) {
2021-02-13 12:08:08 +00:00
parent_average = cascades [ i + 1 ] . lightprobe_average_tex ;
} else {
parent_average = cascades [ i - 1 ] . lightprobe_average_tex ; //to use something, but it won't be used
}
2022-03-06 11:57:09 +00:00
u . append_id ( parent_average ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 14 ;
2022-03-06 11:57:09 +00:00
u . append_id ( ambient_texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
cascades [ i ] . integrate_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . integrate . version_get_shader ( gi - > sdfgi_shader . integrate_shader , 0 ) , 0 ) ;
}
2022-07-27 06:14:23 +00:00
bounce_feedback = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_bounce_feedback ( p_env ) ;
energy = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_energy ( p_env ) ;
normal_bias = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_normal_bias ( p_env ) ;
probe_bias = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_probe_bias ( p_env ) ;
reads_sky = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_read_sky_light ( p_env ) ;
2021-02-13 12:08:08 +00:00
}
2022-08-04 08:40:39 +00:00
void GI : : SDFGI : : free_data ( ) {
// we don't free things here, we handle SDFGI differently at the moment destructing the object when it needs to change.
}
GI : : SDFGI : : ~ SDFGI ( ) {
2022-12-29 00:24:45 +00:00
for ( const SDFGI : : Cascade & c : cascades ) {
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > free ( c . light_data ) ;
RD : : get_singleton ( ) - > free ( c . light_aniso_0_tex ) ;
RD : : get_singleton ( ) - > free ( c . light_aniso_1_tex ) ;
RD : : get_singleton ( ) - > free ( c . sdf_tex ) ;
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > free ( c . solid_cell_dispatch_buffer_storage ) ;
RD : : get_singleton ( ) - > free ( c . solid_cell_dispatch_buffer_call ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > free ( c . solid_cell_buffer ) ;
RD : : get_singleton ( ) - > free ( c . lightprobe_history_tex ) ;
RD : : get_singleton ( ) - > free ( c . lightprobe_average_tex ) ;
RD : : get_singleton ( ) - > free ( c . lights_buffer ) ;
}
RD : : get_singleton ( ) - > free ( render_albedo ) ;
RD : : get_singleton ( ) - > free ( render_emission ) ;
RD : : get_singleton ( ) - > free ( render_emission_aniso ) ;
RD : : get_singleton ( ) - > free ( render_sdf [ 0 ] ) ;
RD : : get_singleton ( ) - > free ( render_sdf [ 1 ] ) ;
RD : : get_singleton ( ) - > free ( render_sdf_half [ 0 ] ) ;
RD : : get_singleton ( ) - > free ( render_sdf_half [ 1 ] ) ;
for ( int i = 0 ; i < 8 ; i + + ) {
RD : : get_singleton ( ) - > free ( render_occlusion [ i ] ) ;
}
RD : : get_singleton ( ) - > free ( render_geom_facing ) ;
RD : : get_singleton ( ) - > free ( lightprobe_data ) ;
RD : : get_singleton ( ) - > free ( lightprobe_history_scroll ) ;
2022-08-09 05:18:03 +00:00
RD : : get_singleton ( ) - > free ( lightprobe_average_scroll ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > free ( occlusion_data ) ;
RD : : get_singleton ( ) - > free ( ambient_texture ) ;
RD : : get_singleton ( ) - > free ( cascades_ubo ) ;
2022-05-20 02:52:19 +00:00
for ( uint32_t v = 0 ; v < RendererSceneRender : : MAX_RENDER_VIEWS ; v + + ) {
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( debug_uniform_set [ v ] ) ) {
RD : : get_singleton ( ) - > free ( debug_uniform_set [ v ] ) ;
}
debug_uniform_set [ v ] = RID ( ) ;
}
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( debug_probes_uniform_set ) ) {
RD : : get_singleton ( ) - > free ( debug_probes_uniform_set ) ;
}
debug_probes_uniform_set = RID ( ) ;
if ( debug_probes_scene_data_ubo . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( debug_probes_scene_data_ubo ) ;
debug_probes_scene_data_ubo = RID ( ) ;
}
2021-02-13 12:08:08 +00:00
}
2022-07-27 06:14:23 +00:00
void GI : : SDFGI : : update ( RID p_env , const Vector3 & p_world_position ) {
bounce_feedback = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_bounce_feedback ( p_env ) ;
energy = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_energy ( p_env ) ;
normal_bias = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_normal_bias ( p_env ) ;
probe_bias = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_probe_bias ( p_env ) ;
reads_sky = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_sdfgi_read_sky_light ( p_env ) ;
2021-02-13 12:08:08 +00:00
int32_t drag_margin = ( cascade_size / SDFGI : : PROBE_DIVISOR ) / 2 ;
2022-12-29 00:24:45 +00:00
for ( SDFGI : : Cascade & cascade : cascades ) {
2021-02-13 12:08:08 +00:00
cascade . dirty_regions = Vector3i ( ) ;
Vector3 probe_half_size = Vector3 ( 1 , 1 , 1 ) * cascade . cell_size * float ( cascade_size / SDFGI : : PROBE_DIVISOR ) * 0.5 ;
probe_half_size = Vector3 ( 0 , 0 , 0 ) ;
Vector3 world_position = p_world_position ;
world_position . y * = y_mult ;
Vector3i pos_in_cascade = Vector3i ( ( world_position + probe_half_size ) / cascade . cell_size ) ;
for ( int j = 0 ; j < 3 ; j + + ) {
if ( pos_in_cascade [ j ] < cascade . position [ j ] ) {
while ( pos_in_cascade [ j ] < ( cascade . position [ j ] - drag_margin ) ) {
cascade . position [ j ] - = drag_margin * 2 ;
cascade . dirty_regions [ j ] + = drag_margin * 2 ;
}
} else if ( pos_in_cascade [ j ] > cascade . position [ j ] ) {
while ( pos_in_cascade [ j ] > ( cascade . position [ j ] + drag_margin ) ) {
cascade . position [ j ] + = drag_margin * 2 ;
cascade . dirty_regions [ j ] - = drag_margin * 2 ;
}
}
if ( cascade . dirty_regions [ j ] = = 0 ) {
continue ; // not dirty
} else if ( uint32_t ( ABS ( cascade . dirty_regions [ j ] ) ) > = cascade_size ) {
//moved too much, just redraw everything (make all dirty)
cascade . dirty_regions = SDFGI : : Cascade : : DIRTY_ALL ;
break ;
}
}
if ( cascade . dirty_regions ! = Vector3i ( ) & & cascade . dirty_regions ! = SDFGI : : Cascade : : DIRTY_ALL ) {
//see how much the total dirty volume represents from the total volume
uint32_t total_volume = cascade_size * cascade_size * cascade_size ;
uint32_t safe_volume = 1 ;
for ( int j = 0 ; j < 3 ; j + + ) {
safe_volume * = cascade_size - ABS ( cascade . dirty_regions [ j ] ) ;
}
uint32_t dirty_volume = total_volume - safe_volume ;
if ( dirty_volume > ( safe_volume / 2 ) ) {
//more than half the volume is dirty, make all dirty so its only rendered once
cascade . dirty_regions = SDFGI : : Cascade : : DIRTY_ALL ;
}
}
}
}
2022-05-20 02:52:19 +00:00
void GI : : SDFGI : : update_light ( ) {
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " SDFGI Update dynamic Light " ) ;
2023-11-24 11:23:22 +00:00
for ( uint32_t i = 0 ; i < cascades . size ( ) ; i + + ) {
RD : : get_singleton ( ) - > buffer_copy ( cascades [ i ] . solid_cell_dispatch_buffer_storage , cascades [ i ] . solid_cell_dispatch_buffer_call , 0 , 0 , sizeof ( uint32_t ) * 4 ) ;
}
2021-02-13 12:08:08 +00:00
/* Update dynamic light */
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . direct_light_pipeline [ SDFGIShader : : DIRECT_LIGHT_MODE_DYNAMIC ] ) ;
2021-02-13 12:08:08 +00:00
2021-03-04 00:53:09 +00:00
SDFGIShader : : DirectLightPushConstant push_constant ;
2021-02-13 12:08:08 +00:00
push_constant . grid_size [ 0 ] = cascade_size ;
push_constant . grid_size [ 1 ] = cascade_size ;
push_constant . grid_size [ 2 ] = cascade_size ;
push_constant . max_cascades = cascades . size ( ) ;
push_constant . probe_axis_size = probe_axis_count ;
push_constant . bounce_feedback = bounce_feedback ;
push_constant . y_mult = y_mult ;
push_constant . use_occlusion = uses_occlusion ;
for ( uint32_t i = 0 ; i < cascades . size ( ) ; i + + ) {
SDFGI : : Cascade & cascade = cascades [ i ] ;
push_constant . light_count = cascade_dynamic_light_count [ i ] ;
push_constant . cascade = i ;
if ( cascades [ i ] . all_dynamic_lights_dirty | | gi - > sdfgi_frames_to_update_light = = RS : : ENV_SDFGI_UPDATE_LIGHT_IN_1_FRAME ) {
push_constant . process_offset = 0 ;
push_constant . process_increment = 1 ;
} else {
2022-04-07 10:23:40 +00:00
static const uint32_t frames_to_update_table [ RS : : ENV_SDFGI_UPDATE_LIGHT_MAX ] = {
2021-02-13 12:08:08 +00:00
1 , 2 , 4 , 8 , 16
} ;
uint32_t frames_to_update = frames_to_update_table [ gi - > sdfgi_frames_to_update_light ] ;
push_constant . process_offset = RSG : : rasterizer - > get_frame_number ( ) % frames_to_update ;
push_constant . process_increment = frames_to_update ;
}
cascades [ i ] . all_dynamic_lights_dirty = false ;
2022-07-09 11:27:24 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascade . sdf_direct_light_dynamic_uniform_set , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : DirectLightPushConstant ) ) ;
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_indirect ( compute_list , cascade . solid_cell_dispatch_buffer_call , 0 ) ;
2021-02-13 12:08:08 +00:00
}
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
2022-07-27 06:14:23 +00:00
void GI : : SDFGI : : update_probes ( RID p_env , SkyRD : : Sky * p_sky ) {
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " SDFGI Update Probes " ) ;
2021-03-04 00:53:09 +00:00
SDFGIShader : : IntegratePushConstant push_constant ;
2021-02-13 12:08:08 +00:00
push_constant . grid_size [ 1 ] = cascade_size ;
push_constant . grid_size [ 2 ] = cascade_size ;
push_constant . grid_size [ 0 ] = cascade_size ;
push_constant . max_cascades = cascades . size ( ) ;
push_constant . probe_axis_size = probe_axis_count ;
push_constant . history_index = render_pass % history_size ;
push_constant . history_size = history_size ;
static const uint32_t ray_count [ RS : : ENV_SDFGI_RAY_COUNT_MAX ] = { 4 , 8 , 16 , 32 , 64 , 96 , 128 } ;
push_constant . ray_count = ray_count [ gi - > sdfgi_ray_count ] ;
push_constant . ray_bias = probe_bias ;
push_constant . image_size [ 0 ] = probe_axis_count * probe_axis_count ;
push_constant . image_size [ 1 ] = probe_axis_count ;
2022-07-27 06:14:23 +00:00
push_constant . store_ambient_texture = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_volumetric_fog_enabled ( p_env ) ;
2021-02-13 12:08:08 +00:00
RID sky_uniform_set = gi - > sdfgi_shader . integrate_default_sky_uniform_set ;
2021-03-04 00:53:09 +00:00
push_constant . sky_mode = SDFGIShader : : IntegratePushConstant : : SKY_MODE_DISABLED ;
2021-02-13 12:08:08 +00:00
push_constant . y_mult = y_mult ;
2022-07-27 06:14:23 +00:00
if ( reads_sky & & p_env . is_valid ( ) ) {
2022-07-31 23:20:24 +00:00
push_constant . sky_energy = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_bg_energy_multiplier ( p_env ) ;
2021-02-13 12:08:08 +00:00
2022-07-27 06:14:23 +00:00
if ( RendererSceneRenderRD : : get_singleton ( ) - > environment_get_background ( p_env ) = = RS : : ENV_BG_CLEAR_COLOR ) {
2021-03-04 00:53:09 +00:00
push_constant . sky_mode = SDFGIShader : : IntegratePushConstant : : SKY_MODE_COLOR ;
2022-06-21 00:08:33 +00:00
Color c = RSG : : texture_storage - > get_default_clear_color ( ) . srgb_to_linear ( ) ;
2021-02-13 12:08:08 +00:00
push_constant . sky_color [ 0 ] = c . r ;
push_constant . sky_color [ 1 ] = c . g ;
push_constant . sky_color [ 2 ] = c . b ;
2022-07-27 06:14:23 +00:00
} else if ( RendererSceneRenderRD : : get_singleton ( ) - > environment_get_background ( p_env ) = = RS : : ENV_BG_COLOR ) {
2021-03-04 00:53:09 +00:00
push_constant . sky_mode = SDFGIShader : : IntegratePushConstant : : SKY_MODE_COLOR ;
2022-07-27 06:14:23 +00:00
Color c = RendererSceneRenderRD : : get_singleton ( ) - > environment_get_bg_color ( p_env ) ;
2021-02-13 12:08:08 +00:00
push_constant . sky_color [ 0 ] = c . r ;
push_constant . sky_color [ 1 ] = c . g ;
push_constant . sky_color [ 2 ] = c . b ;
2022-07-27 06:14:23 +00:00
} else if ( RendererSceneRenderRD : : get_singleton ( ) - > environment_get_background ( p_env ) = = RS : : ENV_BG_SKY ) {
2021-02-13 12:08:08 +00:00
if ( p_sky & & p_sky - > radiance . is_valid ( ) ) {
if ( integrate_sky_uniform_set . is_null ( ) | | ! RD : : get_singleton ( ) - > uniform_set_is_valid ( integrate_sky_uniform_set ) ) {
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 0 ;
2022-03-06 11:57:09 +00:00
u . append_id ( p_sky - > radiance ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 1 ;
2022-04-12 11:41:50 +00:00
u . append_id ( RendererRD : : MaterialStorage : : get_singleton ( ) - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
integrate_sky_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . integrate . version_get_shader ( gi - > sdfgi_shader . integrate_shader , 0 ) , 1 ) ;
}
sky_uniform_set = integrate_sky_uniform_set ;
2021-03-04 00:53:09 +00:00
push_constant . sky_mode = SDFGIShader : : IntegratePushConstant : : SKY_MODE_SKY ;
2021-02-13 12:08:08 +00:00
}
}
}
render_pass + + ;
2023-11-24 11:23:22 +00:00
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . integrate_pipeline [ SDFGIShader : : INTEGRATE_MODE_PROCESS ] ) ;
2021-02-13 12:08:08 +00:00
int32_t probe_divisor = cascade_size / SDFGI : : PROBE_DIVISOR ;
for ( uint32_t i = 0 ; i < cascades . size ( ) ; i + + ) {
push_constant . cascade = i ;
push_constant . world_offset [ 0 ] = cascades [ i ] . position . x / probe_divisor ;
push_constant . world_offset [ 1 ] = cascades [ i ] . position . y / probe_divisor ;
push_constant . world_offset [ 2 ] = cascades [ i ] . position . z / probe_divisor ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascades [ i ] . integrate_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , sky_uniform_set , 1 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : IntegratePushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , probe_axis_count * probe_axis_count , probe_axis_count , 1 ) ;
}
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
2022-05-20 02:52:19 +00:00
void GI : : SDFGI : : store_probes ( ) {
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " SDFGI Store Probes " ) ;
2021-03-04 00:53:09 +00:00
SDFGIShader : : IntegratePushConstant push_constant ;
2021-02-13 12:08:08 +00:00
push_constant . grid_size [ 1 ] = cascade_size ;
push_constant . grid_size [ 2 ] = cascade_size ;
push_constant . grid_size [ 0 ] = cascade_size ;
push_constant . max_cascades = cascades . size ( ) ;
push_constant . probe_axis_size = probe_axis_count ;
push_constant . history_index = render_pass % history_size ;
push_constant . history_size = history_size ;
static const uint32_t ray_count [ RS : : ENV_SDFGI_RAY_COUNT_MAX ] = { 4 , 8 , 16 , 32 , 64 , 96 , 128 } ;
push_constant . ray_count = ray_count [ gi - > sdfgi_ray_count ] ;
push_constant . ray_bias = probe_bias ;
push_constant . image_size [ 0 ] = probe_axis_count * probe_axis_count ;
push_constant . image_size [ 1 ] = probe_axis_count ;
push_constant . store_ambient_texture = false ;
push_constant . sky_mode = 0 ;
push_constant . y_mult = y_mult ;
// Then store values into the lightprobe texture. Separating these steps has a small performance hit, but it allows for multiple bounces
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " Average SDFGI Probes " ) ;
2021-02-13 12:08:08 +00:00
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . integrate_pipeline [ SDFGIShader : : INTEGRATE_MODE_STORE ] ) ;
2021-02-13 12:08:08 +00:00
//convert to octahedral to store
push_constant . image_size [ 0 ] * = SDFGI : : LIGHTPROBE_OCT_SIZE ;
push_constant . image_size [ 1 ] * = SDFGI : : LIGHTPROBE_OCT_SIZE ;
for ( uint32_t i = 0 ; i < cascades . size ( ) ; i + + ) {
push_constant . cascade = i ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascades [ i ] . integrate_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , gi - > sdfgi_shader . integrate_default_sky_uniform_set , 1 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : IntegratePushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , probe_axis_count * probe_axis_count * SDFGI : : LIGHTPROBE_OCT_SIZE , probe_axis_count * SDFGI : : LIGHTPROBE_OCT_SIZE , 1 ) ;
}
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
2022-05-20 02:52:19 +00:00
int GI : : SDFGI : : get_pending_region_data ( int p_region , Vector3i & r_local_offset , Vector3i & r_local_size , AABB & r_bounds ) const {
2021-02-13 12:08:08 +00:00
int dirty_count = 0 ;
for ( uint32_t i = 0 ; i < cascades . size ( ) ; i + + ) {
const SDFGI : : Cascade & c = cascades [ i ] ;
if ( c . dirty_regions = = SDFGI : : Cascade : : DIRTY_ALL ) {
if ( dirty_count = = p_region ) {
r_local_offset = Vector3i ( ) ;
r_local_size = Vector3i ( 1 , 1 , 1 ) * cascade_size ;
r_bounds . position = Vector3 ( ( Vector3i ( 1 , 1 , 1 ) * - int32_t ( cascade_size > > 1 ) + c . position ) ) * c . cell_size * Vector3 ( 1 , 1.0 / y_mult , 1 ) ;
r_bounds . size = Vector3 ( r_local_size ) * c . cell_size * Vector3 ( 1 , 1.0 / y_mult , 1 ) ;
return i ;
}
dirty_count + + ;
} else {
for ( int j = 0 ; j < 3 ; j + + ) {
if ( c . dirty_regions [ j ] ! = 0 ) {
if ( dirty_count = = p_region ) {
Vector3i from = Vector3i ( 0 , 0 , 0 ) ;
Vector3i to = Vector3i ( 1 , 1 , 1 ) * cascade_size ;
if ( c . dirty_regions [ j ] > 0 ) {
//fill from the beginning
to [ j ] = c . dirty_regions [ j ] ;
} else {
//fill from the end
from [ j ] = to [ j ] + c . dirty_regions [ j ] ;
}
for ( int k = 0 ; k < j ; k + + ) {
// "chip" away previous regions to avoid re-voxelizing the same thing
if ( c . dirty_regions [ k ] > 0 ) {
from [ k ] + = c . dirty_regions [ k ] ;
} else if ( c . dirty_regions [ k ] < 0 ) {
to [ k ] + = c . dirty_regions [ k ] ;
}
}
r_local_offset = from ;
r_local_size = to - from ;
r_bounds . position = Vector3 ( from + Vector3i ( 1 , 1 , 1 ) * - int32_t ( cascade_size > > 1 ) + c . position ) * c . cell_size * Vector3 ( 1 , 1.0 / y_mult , 1 ) ;
r_bounds . size = Vector3 ( r_local_size ) * c . cell_size * Vector3 ( 1 , 1.0 / y_mult , 1 ) ;
return i ;
}
dirty_count + + ;
}
}
}
}
return - 1 ;
}
2022-05-20 02:52:19 +00:00
void GI : : SDFGI : : update_cascades ( ) {
2021-02-13 12:08:08 +00:00
//update cascades
SDFGI : : Cascade : : UBO cascade_data [ SDFGI : : MAX_CASCADES ] ;
int32_t probe_divisor = cascade_size / SDFGI : : PROBE_DIVISOR ;
for ( uint32_t i = 0 ; i < cascades . size ( ) ; i + + ) {
Vector3 pos = Vector3 ( ( Vector3i ( 1 , 1 , 1 ) * - int32_t ( cascade_size > > 1 ) + cascades [ i ] . position ) ) * cascades [ i ] . cell_size ;
cascade_data [ i ] . offset [ 0 ] = pos . x ;
cascade_data [ i ] . offset [ 1 ] = pos . y ;
cascade_data [ i ] . offset [ 2 ] = pos . z ;
cascade_data [ i ] . to_cell = 1.0 / cascades [ i ] . cell_size ;
cascade_data [ i ] . probe_offset [ 0 ] = cascades [ i ] . position . x / probe_divisor ;
cascade_data [ i ] . probe_offset [ 1 ] = cascades [ i ] . position . y / probe_divisor ;
cascade_data [ i ] . probe_offset [ 2 ] = cascades [ i ] . position . z / probe_divisor ;
cascade_data [ i ] . pad = 0 ;
}
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > buffer_update ( cascades_ubo , 0 , sizeof ( SDFGI : : Cascade : : UBO ) * SDFGI : : MAX_CASCADES , cascade_data ) ;
2021-02-13 12:08:08 +00:00
}
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
void GI : : SDFGI : : debug_draw ( uint32_t p_view_count , const Projection * p_projections , const Transform3D & p_transform , int p_width , int p_height , RID p_render_target , RID p_texture , const Vector < RID > & p_texture_views ) {
2022-03-12 11:19:59 +00:00
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2022-04-12 11:41:50 +00:00
RendererRD : : MaterialStorage * material_storage = RendererRD : : MaterialStorage : : get_singleton ( ) ;
2022-04-29 07:10:54 +00:00
RendererRD : : CopyEffects * copy_effects = RendererRD : : CopyEffects : : get_singleton ( ) ;
2022-03-12 11:19:59 +00:00
2022-05-20 02:52:19 +00:00
for ( uint32_t v = 0 ; v < p_view_count ; v + + ) {
if ( ! debug_uniform_set [ v ] . is_valid ( ) | | ! RD : : get_singleton ( ) - > uniform_set_is_valid ( debug_uniform_set [ v ] ) ) {
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 1 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t i = 0 ; i < SDFGI : : MAX_CASCADES ; i + + ) {
if ( i < cascades . size ( ) ) {
u . append_id ( cascades [ i ] . sdf_tex ) ;
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2022-05-20 02:52:19 +00:00
}
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
{
RD : : Uniform u ;
u . binding = 2 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t i = 0 ; i < SDFGI : : MAX_CASCADES ; i + + ) {
if ( i < cascades . size ( ) ) {
u . append_id ( cascades [ i ] . light_tex ) ;
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2022-05-20 02:52:19 +00:00
}
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
{
RD : : Uniform u ;
u . binding = 3 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t i = 0 ; i < SDFGI : : MAX_CASCADES ; i + + ) {
if ( i < cascades . size ( ) ) {
u . append_id ( cascades [ i ] . light_aniso_0_tex ) ;
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2022-05-20 02:52:19 +00:00
}
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
{
RD : : Uniform u ;
u . binding = 4 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t i = 0 ; i < SDFGI : : MAX_CASCADES ; i + + ) {
if ( i < cascades . size ( ) ) {
u . append_id ( cascades [ i ] . light_aniso_1_tex ) ;
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2022-05-20 02:52:19 +00:00
}
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
{
RD : : Uniform u ;
u . binding = 5 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . append_id ( occlusion_texture ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 8 ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 9 ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
u . append_id ( cascades_ubo ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 10 ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . append_id ( p_texture_views [ v ] ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 11 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . append_id ( lightprobe_texture ) ;
uniforms . push_back ( u ) ;
}
debug_uniform_set [ v ] = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . debug_shader_version , 0 ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . debug_pipeline ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , debug_uniform_set [ v ] , 0 ) ;
SDFGIShader : : DebugPushConstant push_constant ;
push_constant . grid_size [ 0 ] = cascade_size ;
push_constant . grid_size [ 1 ] = cascade_size ;
push_constant . grid_size [ 2 ] = cascade_size ;
push_constant . max_cascades = cascades . size ( ) ;
push_constant . screen_size [ 0 ] = p_width ;
push_constant . screen_size [ 1 ] = p_height ;
push_constant . y_mult = y_mult ;
push_constant . z_near = - p_projections [ v ] . get_z_near ( ) ;
2022-07-29 11:25:11 +00:00
for ( int i = 0 ; i < 3 ; i + + ) {
for ( int j = 0 ; j < 3 ; j + + ) {
push_constant . cam_basis [ i ] [ j ] = p_transform . basis . rows [ j ] [ i ] ;
}
}
push_constant . cam_origin [ 0 ] = p_transform . origin [ 0 ] ;
push_constant . cam_origin [ 1 ] = p_transform . origin [ 1 ] ;
push_constant . cam_origin [ 2 ] = p_transform . origin [ 2 ] ;
2022-05-20 02:52:19 +00:00
// need to properly unproject for asymmetric projection matrices in stereo..
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 inv_projection = p_projections [ v ] . inverse ( ) ;
2022-05-20 02:52:19 +00:00
for ( int i = 0 ; i < 4 ; i + + ) {
2022-07-29 11:25:11 +00:00
for ( int j = 0 ; j < 3 ; j + + ) {
2022-10-04 16:44:48 +00:00
push_constant . inv_projection [ j ] [ i ] = inv_projection . columns [ i ] [ j ] ;
2022-05-20 02:52:19 +00:00
}
}
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : DebugPushConstant ) ) ;
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , p_width , p_height , 1 ) ;
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
2021-02-13 12:08:08 +00:00
2022-09-01 08:10:53 +00:00
Size2i rtsize = texture_storage - > render_target_get_size ( p_render_target ) ;
copy_effects - > copy_to_fb_rect ( p_texture , texture_storage - > render_target_get_rd_framebuffer ( p_render_target ) , Rect2i ( Point2i ( ) , rtsize ) , true , false , false , false , RID ( ) , p_view_count > 1 ) ;
2021-02-13 12:08:08 +00:00
}
2023-11-24 11:23:22 +00:00
void GI : : SDFGI : : debug_probes ( RID p_framebuffer , const uint32_t p_view_count , const Projection * p_camera_with_transforms ) {
2022-04-12 11:41:50 +00:00
RendererRD : : MaterialStorage * material_storage = RendererRD : : MaterialStorage : : get_singleton ( ) ;
2022-05-20 02:52:19 +00:00
// setup scene data
{
SDFGIShader : : DebugProbesSceneData scene_data ;
2021-02-13 12:08:08 +00:00
2022-05-20 02:52:19 +00:00
if ( debug_probes_scene_data_ubo . is_null ( ) ) {
debug_probes_scene_data_ubo = RD : : get_singleton ( ) - > uniform_buffer_create ( sizeof ( SDFGIShader : : DebugProbesSceneData ) ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
for ( uint32_t v = 0 ; v < p_view_count ; v + + ) {
2022-06-21 00:08:33 +00:00
RendererRD : : MaterialStorage : : store_camera ( p_camera_with_transforms [ v ] , scene_data . projection [ v ] ) ;
2022-05-20 02:52:19 +00:00
}
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > buffer_update ( debug_probes_scene_data_ubo , 0 , sizeof ( SDFGIShader : : DebugProbesSceneData ) , & scene_data ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
// setup push constant
SDFGIShader : : DebugProbesPushConstant push_constant ;
2021-02-13 12:08:08 +00:00
//gen spheres from strips
uint32_t band_points = 16 ;
push_constant . band_power = 4 ;
push_constant . sections_in_band = ( ( band_points / 2 ) - 1 ) ;
push_constant . band_mask = band_points - 2 ;
push_constant . section_arc = Math_TAU / float ( push_constant . sections_in_band ) ;
push_constant . y_mult = y_mult ;
uint32_t total_points = push_constant . sections_in_band * band_points ;
uint32_t total_probes = probe_axis_count * probe_axis_count * probe_axis_count ;
push_constant . grid_size [ 0 ] = cascade_size ;
push_constant . grid_size [ 1 ] = cascade_size ;
push_constant . grid_size [ 2 ] = cascade_size ;
push_constant . cascade = 0 ;
push_constant . probe_axis_size = probe_axis_count ;
if ( ! debug_probes_uniform_set . is_valid ( ) | | ! RD : : get_singleton ( ) - > uniform_set_is_valid ( debug_probes_uniform_set ) ) {
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 1 ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
2022-03-06 11:57:09 +00:00
u . append_id ( cascades_ubo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 2 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2022-03-06 11:57:09 +00:00
u . append_id ( lightprobe_texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 3 ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
2022-04-12 11:41:50 +00:00
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 4 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2022-03-06 11:57:09 +00:00
u . append_id ( occlusion_texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2022-05-20 02:52:19 +00:00
{
RD : : Uniform u ;
u . binding = 5 ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
u . append_id ( debug_probes_scene_data_ubo ) ;
uniforms . push_back ( u ) ;
}
2021-02-13 12:08:08 +00:00
debug_probes_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > sdfgi_shader . debug_probes . version_get_shader ( gi - > sdfgi_shader . debug_probes_shader , 0 ) , 0 ) ;
}
2022-05-20 02:52:19 +00:00
SDFGIShader : : ProbeDebugMode mode = p_view_count > 1 ? SDFGIShader : : PROBE_DEBUG_PROBES_MULTIVIEW : SDFGIShader : : PROBE_DEBUG_PROBES ;
2023-11-24 11:23:22 +00:00
RD : : DrawListID draw_list = RD : : get_singleton ( ) - > draw_list_begin ( p_framebuffer , RD : : INITIAL_ACTION_LOAD , RD : : FINAL_ACTION_STORE , RD : : INITIAL_ACTION_LOAD , RD : : FINAL_ACTION_STORE ) ;
2022-05-20 02:52:19 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " Debug SDFGI " ) ;
RD : : get_singleton ( ) - > draw_list_bind_render_pipeline ( draw_list , gi - > sdfgi_shader . debug_probes_pipeline [ mode ] . get_render_pipeline ( RD : : INVALID_FORMAT_ID , RD : : get_singleton ( ) - > framebuffer_get_format ( p_framebuffer ) ) ) ;
RD : : get_singleton ( ) - > draw_list_bind_uniform_set ( draw_list , debug_probes_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > draw_list_set_push_constant ( draw_list , & push_constant , sizeof ( SDFGIShader : : DebugProbesPushConstant ) ) ;
RD : : get_singleton ( ) - > draw_list_draw ( draw_list , false , total_probes , total_points ) ;
2021-02-13 12:08:08 +00:00
if ( gi - > sdfgi_debug_probe_dir ! = Vector3 ( ) ) {
uint32_t cascade = 0 ;
Vector3 offset = Vector3 ( ( Vector3i ( 1 , 1 , 1 ) * - int32_t ( cascade_size > > 1 ) + cascades [ cascade ] . position ) ) * cascades [ cascade ] . cell_size * Vector3 ( 1.0 , 1.0 / y_mult , 1.0 ) ;
Vector3 probe_size = cascades [ cascade ] . cell_size * ( cascade_size / SDFGI : : PROBE_DIVISOR ) * Vector3 ( 1.0 , 1.0 / y_mult , 1.0 ) ;
Vector3 ray_from = gi - > sdfgi_debug_probe_pos ;
Vector3 ray_to = gi - > sdfgi_debug_probe_pos + gi - > sdfgi_debug_probe_dir * cascades [ cascade ] . cell_size * Math : : sqrt ( 3.0 ) * cascade_size ;
float sphere_radius = 0.2 ;
float closest_dist = 1e20 ;
gi - > sdfgi_debug_probe_enabled = false ;
Vector3i probe_from = cascades [ cascade ] . position / ( cascade_size / SDFGI : : PROBE_DIVISOR ) ;
for ( int i = 0 ; i < ( SDFGI : : PROBE_DIVISOR + 1 ) ; i + + ) {
for ( int j = 0 ; j < ( SDFGI : : PROBE_DIVISOR + 1 ) ; j + + ) {
for ( int k = 0 ; k < ( SDFGI : : PROBE_DIVISOR + 1 ) ; k + + ) {
Vector3 pos = offset + probe_size * Vector3 ( i , j , k ) ;
Vector3 res ;
if ( Geometry3D : : segment_intersects_sphere ( ray_from , ray_to , pos , sphere_radius , & res ) ) {
float d = ray_from . distance_to ( res ) ;
if ( d < closest_dist ) {
closest_dist = d ;
gi - > sdfgi_debug_probe_enabled = true ;
gi - > sdfgi_debug_probe_index = probe_from + Vector3i ( i , j , k ) ;
}
}
}
}
}
gi - > sdfgi_debug_probe_dir = Vector3 ( ) ;
}
if ( gi - > sdfgi_debug_probe_enabled ) {
uint32_t cascade = 0 ;
uint32_t probe_cells = ( cascade_size / SDFGI : : PROBE_DIVISOR ) ;
Vector3i probe_from = cascades [ cascade ] . position / probe_cells ;
Vector3i ofs = gi - > sdfgi_debug_probe_index - probe_from ;
if ( ofs . x < 0 | | ofs . y < 0 | | ofs . z < 0 ) {
return ;
}
if ( ofs . x > SDFGI : : PROBE_DIVISOR | | ofs . y > SDFGI : : PROBE_DIVISOR | | ofs . z > SDFGI : : PROBE_DIVISOR ) {
return ;
}
uint32_t mult = ( SDFGI : : PROBE_DIVISOR + 1 ) ;
uint32_t index = ofs . z * mult * mult + ofs . y * mult + ofs . x ;
push_constant . probe_debug_index = index ;
uint32_t cell_count = probe_cells * 2 * probe_cells * 2 * probe_cells * 2 ;
2022-05-20 02:52:19 +00:00
RD : : get_singleton ( ) - > draw_list_bind_render_pipeline ( draw_list , gi - > sdfgi_shader . debug_probes_pipeline [ p_view_count > 1 ? SDFGIShader : : PROBE_DEBUG_VISIBILITY_MULTIVIEW : SDFGIShader : : PROBE_DEBUG_VISIBILITY ] . get_render_pipeline ( RD : : INVALID_FORMAT_ID , RD : : get_singleton ( ) - > framebuffer_get_format ( p_framebuffer ) ) ) ;
RD : : get_singleton ( ) - > draw_list_bind_uniform_set ( draw_list , debug_probes_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > draw_list_set_push_constant ( draw_list , & push_constant , sizeof ( SDFGIShader : : DebugProbesPushConstant ) ) ;
RD : : get_singleton ( ) - > draw_list_draw ( draw_list , false , cell_count , total_points ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
RD : : get_singleton ( ) - > draw_list_end ( ) ;
2021-02-13 12:08:08 +00:00
}
2022-09-12 09:44:48 +00:00
void GI : : SDFGI : : pre_process_gi ( const Transform3D & p_transform , RenderDataRD * p_render_data ) {
RendererRD : : LightStorage * light_storage = RendererRD : : LightStorage : : get_singleton ( ) ;
2021-02-13 12:08:08 +00:00
/* Update general SDFGI Buffer */
SDFGIData sdfgi_data ;
sdfgi_data . grid_size [ 0 ] = cascade_size ;
sdfgi_data . grid_size [ 1 ] = cascade_size ;
sdfgi_data . grid_size [ 2 ] = cascade_size ;
sdfgi_data . max_cascades = cascades . size ( ) ;
sdfgi_data . probe_axis_size = probe_axis_count ;
sdfgi_data . cascade_probe_size [ 0 ] = sdfgi_data . probe_axis_size - 1 ; //float version for performance
sdfgi_data . cascade_probe_size [ 1 ] = sdfgi_data . probe_axis_size - 1 ;
sdfgi_data . cascade_probe_size [ 2 ] = sdfgi_data . probe_axis_size - 1 ;
float csize = cascade_size ;
sdfgi_data . probe_to_uvw = 1.0 / float ( sdfgi_data . cascade_probe_size [ 0 ] ) ;
sdfgi_data . use_occlusion = uses_occlusion ;
//sdfgi_data.energy = energy;
sdfgi_data . y_mult = y_mult ;
float cascade_voxel_size = ( csize / sdfgi_data . cascade_probe_size [ 0 ] ) ;
float occlusion_clamp = ( cascade_voxel_size - 0.5 ) / cascade_voxel_size ;
sdfgi_data . occlusion_clamp [ 0 ] = occlusion_clamp ;
sdfgi_data . occlusion_clamp [ 1 ] = occlusion_clamp ;
sdfgi_data . occlusion_clamp [ 2 ] = occlusion_clamp ;
sdfgi_data . normal_bias = ( normal_bias / csize ) * sdfgi_data . cascade_probe_size [ 0 ] ;
//vec2 tex_pixel_size = 1.0 / vec2(ivec2( (OCT_SIZE+2) * params.probe_axis_size * params.probe_axis_size, (OCT_SIZE+2) * params.probe_axis_size ) );
//vec3 probe_uv_offset = (ivec3(OCT_SIZE+2,OCT_SIZE+2,(OCT_SIZE+2) * params.probe_axis_size)) * tex_pixel_size.xyx;
uint32_t oct_size = SDFGI : : LIGHTPROBE_OCT_SIZE ;
sdfgi_data . lightprobe_tex_pixel_size [ 0 ] = 1.0 / ( ( oct_size + 2 ) * sdfgi_data . probe_axis_size * sdfgi_data . probe_axis_size ) ;
sdfgi_data . lightprobe_tex_pixel_size [ 1 ] = 1.0 / ( ( oct_size + 2 ) * sdfgi_data . probe_axis_size ) ;
sdfgi_data . lightprobe_tex_pixel_size [ 2 ] = 1.0 ;
sdfgi_data . energy = energy ;
sdfgi_data . lightprobe_uv_offset [ 0 ] = float ( oct_size + 2 ) * sdfgi_data . lightprobe_tex_pixel_size [ 0 ] ;
sdfgi_data . lightprobe_uv_offset [ 1 ] = float ( oct_size + 2 ) * sdfgi_data . lightprobe_tex_pixel_size [ 1 ] ;
sdfgi_data . lightprobe_uv_offset [ 2 ] = float ( ( oct_size + 2 ) * sdfgi_data . probe_axis_size ) * sdfgi_data . lightprobe_tex_pixel_size [ 0 ] ;
sdfgi_data . occlusion_renormalize [ 0 ] = 0.5 ;
sdfgi_data . occlusion_renormalize [ 1 ] = 1.0 ;
sdfgi_data . occlusion_renormalize [ 2 ] = 1.0 / float ( sdfgi_data . max_cascades ) ;
int32_t probe_divisor = cascade_size / SDFGI : : PROBE_DIVISOR ;
for ( uint32_t i = 0 ; i < sdfgi_data . max_cascades ; i + + ) {
SDFGIData : : ProbeCascadeData & c = sdfgi_data . cascades [ i ] ;
Vector3 pos = Vector3 ( ( Vector3i ( 1 , 1 , 1 ) * - int32_t ( cascade_size > > 1 ) + cascades [ i ] . position ) ) * cascades [ i ] . cell_size ;
Vector3 cam_origin = p_transform . origin ;
cam_origin . y * = y_mult ;
pos - = cam_origin ; //make pos local to camera, to reduce numerical error
c . position [ 0 ] = pos . x ;
c . position [ 1 ] = pos . y ;
c . position [ 2 ] = pos . z ;
c . to_probe = 1.0 / ( float ( cascade_size ) * cascades [ i ] . cell_size / float ( probe_axis_count - 1 ) ) ;
Vector3i probe_ofs = cascades [ i ] . position / probe_divisor ;
c . probe_world_offset [ 0 ] = probe_ofs . x ;
c . probe_world_offset [ 1 ] = probe_ofs . y ;
c . probe_world_offset [ 2 ] = probe_ofs . z ;
c . to_cell = 1.0 / cascades [ i ] . cell_size ;
2022-07-31 23:20:24 +00:00
c . exposure_normalization = 1.0 ;
if ( p_render_data - > camera_attributes . is_valid ( ) ) {
float exposure_normalization = RSG : : camera_attributes - > camera_attributes_get_exposure_normalization_factor ( p_render_data - > camera_attributes ) ;
c . exposure_normalization = exposure_normalization / cascades [ i ] . baked_exposure_normalization ;
}
2021-02-13 12:08:08 +00:00
}
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > buffer_update ( gi - > sdfgi_ubo , 0 , sizeof ( SDFGIData ) , & sdfgi_data ) ;
2021-02-13 12:08:08 +00:00
/* Update dynamic lights in SDFGI cascades */
for ( uint32_t i = 0 ; i < cascades . size ( ) ; i + + ) {
SDFGI : : Cascade & cascade = cascades [ i ] ;
2021-03-04 00:53:09 +00:00
SDFGIShader : : Light lights [ SDFGI : : MAX_DYNAMIC_LIGHTS ] ;
2021-02-13 12:08:08 +00:00
uint32_t idx = 0 ;
2022-09-12 09:44:48 +00:00
for ( uint32_t j = 0 ; j < ( uint32_t ) p_render_data - > sdfgi_update_data - > directional_lights - > size ( ) ; j + + ) {
2021-02-13 12:08:08 +00:00
if ( idx = = SDFGI : : MAX_DYNAMIC_LIGHTS ) {
break ;
}
2022-09-12 09:44:48 +00:00
RID light_instance = p_render_data - > sdfgi_update_data - > directional_lights - > get ( j ) ;
ERR_CONTINUE ( ! light_storage - > owns_light_instance ( light_instance ) ) ;
2021-02-13 12:08:08 +00:00
2022-09-12 09:44:48 +00:00
RID light = light_storage - > light_instance_get_base_light ( light_instance ) ;
Transform3D light_transform = light_storage - > light_instance_get_base_transform ( light_instance ) ;
if ( RSG : : light_storage - > light_directional_get_sky_mode ( light ) = = RS : : LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY ) {
2021-02-13 12:08:08 +00:00
continue ;
}
2022-09-12 09:44:48 +00:00
Vector3 dir = - light_transform . basis . get_column ( Vector3 : : AXIS_Z ) ;
2021-02-13 12:08:08 +00:00
dir . y * = y_mult ;
dir . normalize ( ) ;
lights [ idx ] . direction [ 0 ] = dir . x ;
lights [ idx ] . direction [ 1 ] = dir . y ;
lights [ idx ] . direction [ 2 ] = dir . z ;
2022-09-12 09:44:48 +00:00
Color color = RSG : : light_storage - > light_get_color ( light ) ;
2022-04-13 08:37:22 +00:00
color = color . srgb_to_linear ( ) ;
2021-02-13 12:08:08 +00:00
lights [ idx ] . color [ 0 ] = color . r ;
lights [ idx ] . color [ 1 ] = color . g ;
lights [ idx ] . color [ 2 ] = color . b ;
lights [ idx ] . type = RS : : LIGHT_DIRECTIONAL ;
2022-09-12 09:44:48 +00:00
lights [ idx ] . energy = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_ENERGY ) * RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) ;
if ( RendererSceneRenderRD : : get_singleton ( ) - > is_using_physical_light_units ( ) ) {
lights [ idx ] . energy * = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_INTENSITY ) ;
2022-07-31 23:20:24 +00:00
}
if ( p_render_data - > camera_attributes . is_valid ( ) ) {
lights [ idx ] . energy * = RSG : : camera_attributes - > camera_attributes_get_exposure_normalization_factor ( p_render_data - > camera_attributes ) ;
}
2022-09-12 09:44:48 +00:00
lights [ idx ] . has_shadow = RSG : : light_storage - > light_has_shadow ( light ) ;
2021-02-13 12:08:08 +00:00
idx + + ;
}
AABB cascade_aabb ;
cascade_aabb . position = Vector3 ( ( Vector3i ( 1 , 1 , 1 ) * - int32_t ( cascade_size > > 1 ) + cascade . position ) ) * cascade . cell_size ;
cascade_aabb . size = Vector3 ( 1 , 1 , 1 ) * cascade_size * cascade . cell_size ;
2022-09-12 09:44:48 +00:00
for ( uint32_t j = 0 ; j < p_render_data - > sdfgi_update_data - > positional_light_count ; j + + ) {
2021-02-13 12:08:08 +00:00
if ( idx = = SDFGI : : MAX_DYNAMIC_LIGHTS ) {
break ;
}
2022-09-12 09:44:48 +00:00
RID light_instance = p_render_data - > sdfgi_update_data - > positional_light_instances [ j ] ;
ERR_CONTINUE ( ! light_storage - > owns_light_instance ( light_instance ) ) ;
RID light = light_storage - > light_instance_get_base_light ( light_instance ) ;
AABB light_aabb = light_storage - > light_instance_get_base_aabb ( light_instance ) ;
Transform3D light_transform = light_storage - > light_instance_get_base_transform ( light_instance ) ;
2021-02-13 12:08:08 +00:00
2022-09-12 09:44:48 +00:00
uint32_t max_sdfgi_cascade = RSG : : light_storage - > light_get_max_sdfgi_cascade ( light ) ;
2021-02-13 12:08:08 +00:00
if ( i > max_sdfgi_cascade ) {
continue ;
}
2022-09-12 09:44:48 +00:00
if ( ! cascade_aabb . intersects ( light_aabb ) ) {
2021-02-13 12:08:08 +00:00
continue ;
}
2022-09-12 09:44:48 +00:00
Vector3 dir = - light_transform . basis . get_column ( Vector3 : : AXIS_Z ) ;
2021-02-13 12:08:08 +00:00
//faster to not do this here
//dir.y *= y_mult;
//dir.normalize();
lights [ idx ] . direction [ 0 ] = dir . x ;
lights [ idx ] . direction [ 1 ] = dir . y ;
lights [ idx ] . direction [ 2 ] = dir . z ;
2022-09-12 09:44:48 +00:00
Vector3 pos = light_transform . origin ;
2021-02-13 12:08:08 +00:00
pos . y * = y_mult ;
lights [ idx ] . position [ 0 ] = pos . x ;
lights [ idx ] . position [ 1 ] = pos . y ;
lights [ idx ] . position [ 2 ] = pos . z ;
2022-09-12 09:44:48 +00:00
Color color = RSG : : light_storage - > light_get_color ( light ) ;
2022-04-13 08:37:22 +00:00
color = color . srgb_to_linear ( ) ;
2021-02-13 12:08:08 +00:00
lights [ idx ] . color [ 0 ] = color . r ;
lights [ idx ] . color [ 1 ] = color . g ;
lights [ idx ] . color [ 2 ] = color . b ;
2022-09-12 09:44:48 +00:00
lights [ idx ] . type = RSG : : light_storage - > light_get_type ( light ) ;
2022-07-31 23:20:24 +00:00
2022-09-12 09:44:48 +00:00
lights [ idx ] . energy = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_ENERGY ) * RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) ;
if ( RendererSceneRenderRD : : get_singleton ( ) - > is_using_physical_light_units ( ) ) {
lights [ idx ] . energy * = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_INTENSITY ) ;
2022-07-31 23:20:24 +00:00
// Convert from Luminous Power to Luminous Intensity
if ( lights [ idx ] . type = = RS : : LIGHT_OMNI ) {
lights [ idx ] . energy * = 1.0 / ( Math_PI * 4.0 ) ;
} else if ( lights [ idx ] . type = = RS : : LIGHT_SPOT ) {
// Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
// We make this assumption to keep them easy to control.
lights [ idx ] . energy * = 1.0 / Math_PI ;
}
}
if ( p_render_data - > camera_attributes . is_valid ( ) ) {
lights [ idx ] . energy * = RSG : : camera_attributes - > camera_attributes_get_exposure_normalization_factor ( p_render_data - > camera_attributes ) ;
}
2022-09-12 09:44:48 +00:00
lights [ idx ] . has_shadow = RSG : : light_storage - > light_has_shadow ( light ) ;
lights [ idx ] . attenuation = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_ATTENUATION ) ;
lights [ idx ] . radius = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_RANGE ) ;
lights [ idx ] . cos_spot_angle = Math : : cos ( Math : : deg_to_rad ( RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_SPOT_ANGLE ) ) ) ;
lights [ idx ] . inv_spot_attenuation = 1.0f / RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ;
2021-02-13 12:08:08 +00:00
idx + + ;
}
if ( idx > 0 ) {
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > buffer_update ( cascade . lights_buffer , 0 , idx * sizeof ( SDFGIShader : : Light ) , lights ) ;
2021-02-13 12:08:08 +00:00
}
cascade_dynamic_light_count [ i ] = idx ;
}
}
2022-09-12 09:44:48 +00:00
void GI : : SDFGI : : render_region ( Ref < RenderSceneBuffersRD > p_render_buffers , int p_region , const PagedArray < RenderGeometryInstance * > & p_instances , float p_exposure_normalization ) {
2021-02-13 12:08:08 +00:00
//print_line("rendering region " + itos(p_region));
2022-08-04 08:40:39 +00:00
ERR_FAIL_COND ( p_render_buffers . is_null ( ) ) ; // we wouldn't be here if this failed but...
2021-02-13 12:08:08 +00:00
AABB bounds ;
Vector3i from ;
Vector3i size ;
int cascade_prev = get_pending_region_data ( p_region - 1 , from , size , bounds ) ;
int cascade_next = get_pending_region_data ( p_region + 1 , from , size , bounds ) ;
int cascade = get_pending_region_data ( p_region , from , size , bounds ) ;
ERR_FAIL_COND ( cascade < 0 ) ;
if ( cascade_prev ! = cascade ) {
//initialize render
RD : : get_singleton ( ) - > texture_clear ( render_albedo , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , 1 ) ;
RD : : get_singleton ( ) - > texture_clear ( render_emission , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , 1 ) ;
RD : : get_singleton ( ) - > texture_clear ( render_emission_aniso , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , 1 ) ;
RD : : get_singleton ( ) - > texture_clear ( render_geom_facing , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , 1 ) ;
}
//print_line("rendering cascade " + itos(p_region) + " objects: " + itos(p_cull_count) + " bounds: " + bounds + " from: " + from + " size: " + size + " cell size: " + rtos(cascades[cascade].cell_size));
2022-09-12 09:44:48 +00:00
RendererSceneRenderRD : : get_singleton ( ) - > _render_sdfgi ( p_render_buffers , from , size , bounds , p_instances , render_albedo , render_emission , render_emission_aniso , render_geom_facing , p_exposure_normalization ) ;
2021-02-13 12:08:08 +00:00
if ( cascade_next ! = cascade ) {
RD : : get_singleton ( ) - > draw_command_begin_label ( " SDFGI Pre-Process Cascade " ) ;
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " > SDFGI Update SDF " ) ;
2021-02-13 12:08:08 +00:00
//done rendering! must update SDF
//clear dispatch indirect data
2021-03-04 00:53:09 +00:00
SDFGIShader : : PreprocessPushConstant push_constant ;
2021-04-27 14:19:21 +00:00
memset ( & push_constant , 0 , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " SDFGI Scroll SDF " ) ;
2021-02-13 12:08:08 +00:00
//scroll
if ( cascades [ cascade ] . dirty_regions ! = SDFGI : : Cascade : : DIRTY_ALL ) {
//for scroll
Vector3i dirty = cascades [ cascade ] . dirty_regions ;
push_constant . scroll [ 0 ] = dirty . x ;
push_constant . scroll [ 1 ] = dirty . y ;
push_constant . scroll [ 2 ] = dirty . z ;
} else {
//for no scroll
push_constant . scroll [ 0 ] = 0 ;
push_constant . scroll [ 1 ] = 0 ;
push_constant . scroll [ 2 ] = 0 ;
}
cascades [ cascade ] . all_dynamic_lights_dirty = true ;
2022-07-31 23:20:24 +00:00
cascades [ cascade ] . baked_exposure_normalization = p_exposure_normalization ;
2021-02-13 12:08:08 +00:00
push_constant . grid_size = cascade_size ;
push_constant . cascade = cascade ;
if ( cascades [ cascade ] . dirty_regions ! = SDFGI : : Cascade : : DIRTY_ALL ) {
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > buffer_copy ( cascades [ cascade ] . solid_cell_dispatch_buffer_storage , cascades [ cascade ] . solid_cell_dispatch_buffer_call , 0 , 0 , sizeof ( uint32_t ) * 4 ) ;
2021-02-13 12:08:08 +00:00
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
//must pre scroll existing data because not all is dirty
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_SCROLL ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascades [ cascade ] . scroll_uniform_set , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_indirect ( compute_list , cascades [ cascade ] . solid_cell_dispatch_buffer_call , 0 ) ;
2021-02-13 12:08:08 +00:00
// no barrier do all together
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_SCROLL_OCCLUSION ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascades [ cascade ] . scroll_occlusion_uniform_set , 0 ) ;
Vector3i dirty = cascades [ cascade ] . dirty_regions ;
Vector3i groups ;
groups . x = cascade_size - ABS ( dirty . x ) ;
groups . y = cascade_size - ABS ( dirty . y ) ;
groups . z = cascade_size - ABS ( dirty . z ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , groups . x , groups . y , groups . z ) ;
//no barrier, continue together
{
//scroll probes and their history also
2021-03-04 00:53:09 +00:00
SDFGIShader : : IntegratePushConstant ipush_constant ;
2021-02-13 12:08:08 +00:00
ipush_constant . grid_size [ 1 ] = cascade_size ;
ipush_constant . grid_size [ 2 ] = cascade_size ;
ipush_constant . grid_size [ 0 ] = cascade_size ;
ipush_constant . max_cascades = cascades . size ( ) ;
ipush_constant . probe_axis_size = probe_axis_count ;
ipush_constant . history_index = 0 ;
ipush_constant . history_size = history_size ;
ipush_constant . ray_count = 0 ;
ipush_constant . ray_bias = 0 ;
ipush_constant . sky_mode = 0 ;
ipush_constant . sky_energy = 0 ;
ipush_constant . sky_color [ 0 ] = 0 ;
ipush_constant . sky_color [ 1 ] = 0 ;
ipush_constant . sky_color [ 2 ] = 0 ;
ipush_constant . y_mult = y_mult ;
ipush_constant . store_ambient_texture = false ;
ipush_constant . image_size [ 0 ] = probe_axis_count * probe_axis_count ;
ipush_constant . image_size [ 1 ] = probe_axis_count ;
int32_t probe_divisor = cascade_size / SDFGI : : PROBE_DIVISOR ;
ipush_constant . cascade = cascade ;
ipush_constant . world_offset [ 0 ] = cascades [ cascade ] . position . x / probe_divisor ;
ipush_constant . world_offset [ 1 ] = cascades [ cascade ] . position . y / probe_divisor ;
ipush_constant . world_offset [ 2 ] = cascades [ cascade ] . position . z / probe_divisor ;
ipush_constant . scroll [ 0 ] = dirty . x / probe_divisor ;
ipush_constant . scroll [ 1 ] = dirty . y / probe_divisor ;
ipush_constant . scroll [ 2 ] = dirty . z / probe_divisor ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . integrate_pipeline [ SDFGIShader : : INTEGRATE_MODE_SCROLL ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascades [ cascade ] . integrate_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , gi - > sdfgi_shader . integrate_default_sky_uniform_set , 1 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & ipush_constant , sizeof ( SDFGIShader : : IntegratePushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , probe_axis_count * probe_axis_count , probe_axis_count , 1 ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . integrate_pipeline [ SDFGIShader : : INTEGRATE_MODE_SCROLL_STORE ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascades [ cascade ] . integrate_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , gi - > sdfgi_shader . integrate_default_sky_uniform_set , 1 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & ipush_constant , sizeof ( SDFGIShader : : IntegratePushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , probe_axis_count * probe_axis_count , probe_axis_count , 1 ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
if ( bounce_feedback > 0.0 ) {
//multibounce requires this to be stored so direct light can read from it
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . integrate_pipeline [ SDFGIShader : : INTEGRATE_MODE_STORE ] ) ;
2021-02-13 12:08:08 +00:00
//convert to octahedral to store
ipush_constant . image_size [ 0 ] * = SDFGI : : LIGHTPROBE_OCT_SIZE ;
ipush_constant . image_size [ 1 ] * = SDFGI : : LIGHTPROBE_OCT_SIZE ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascades [ cascade ] . integrate_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , gi - > sdfgi_shader . integrate_default_sky_uniform_set , 1 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & ipush_constant , sizeof ( SDFGIShader : : IntegratePushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , probe_axis_count * probe_axis_count * SDFGI : : LIGHTPROBE_OCT_SIZE , probe_axis_count * SDFGI : : LIGHTPROBE_OCT_SIZE , 1 ) ;
}
}
//ok finally barrier
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
//clear dispatch indirect data
uint32_t dispatch_indirct_data [ 4 ] = { 0 , 0 , 0 , 0 } ;
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > buffer_update ( cascades [ cascade ] . solid_cell_dispatch_buffer_storage , 0 , sizeof ( uint32_t ) * 4 , dispatch_indirct_data ) ;
2021-02-13 12:08:08 +00:00
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
bool half_size = true ; //much faster, very little difference
static const int optimized_jf_group_size = 8 ;
if ( half_size ) {
push_constant . grid_size > > = 1 ;
uint32_t cascade_half_size = cascade_size > > 1 ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_INITIALIZE_HALF ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , sdf_initialize_half_uniform_set , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_half_size , cascade_half_size , cascade_half_size ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
//must start with regular jumpflood
push_constant . half_size = true ;
{
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " SDFGI Jump Flood (Half-Size) " ) ;
2021-02-13 12:08:08 +00:00
uint32_t s = cascade_half_size ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_JUMP_FLOOD ] ) ;
2021-02-13 12:08:08 +00:00
int jf_us = 0 ;
//start with regular jump flood for very coarse reads, as this is impossible to optimize
while ( s > 1 ) {
s / = 2 ;
push_constant . step_size = s ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , jump_flood_half_uniform_set [ jf_us ] , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_half_size , cascade_half_size , cascade_half_size ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
jf_us = jf_us = = 0 ? 1 : 0 ;
if ( cascade_half_size / ( s / 2 ) > = optimized_jf_group_size ) {
break ;
}
}
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " SDFGI Jump Flood Optimized (Half-Size) " ) ;
2021-02-13 12:08:08 +00:00
//continue with optimized jump flood for smaller reads
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_OPTIMIZED ] ) ;
2021-02-13 12:08:08 +00:00
while ( s > 1 ) {
s / = 2 ;
push_constant . step_size = s ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , jump_flood_half_uniform_set [ jf_us ] , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_half_size , cascade_half_size , cascade_half_size ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
jf_us = jf_us = = 0 ? 1 : 0 ;
}
}
// restore grid size for last passes
push_constant . grid_size = cascade_size ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_UPSCALE ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , sdf_upscale_uniform_set , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_size , cascade_size , cascade_size ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
2023-08-07 10:59:23 +00:00
//run one pass of fullsize jumpflood to fix up half size artifacts
2021-02-13 12:08:08 +00:00
push_constant . half_size = false ;
push_constant . step_size = 1 ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_OPTIMIZED ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , jump_flood_uniform_set [ upscale_jfa_uniform_set_index ] , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_size , cascade_size , cascade_size ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
} else {
//full size jumpflood
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " SDFGI Jump Flood (Full-Size) " ) ;
2021-02-13 12:08:08 +00:00
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_INITIALIZE ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , sdf_initialize_uniform_set , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_size , cascade_size , cascade_size ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
push_constant . half_size = false ;
{
uint32_t s = cascade_size ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_JUMP_FLOOD ] ) ;
2021-02-13 12:08:08 +00:00
int jf_us = 0 ;
//start with regular jump flood for very coarse reads, as this is impossible to optimize
while ( s > 1 ) {
s / = 2 ;
push_constant . step_size = s ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , jump_flood_uniform_set [ jf_us ] , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_size , cascade_size , cascade_size ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
jf_us = jf_us = = 0 ? 1 : 0 ;
if ( cascade_size / ( s / 2 ) > = optimized_jf_group_size ) {
break ;
}
}
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " SDFGI Jump Flood Optimized (Full-Size) " ) ;
2021-02-13 12:08:08 +00:00
//continue with optimized jump flood for smaller reads
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_JUMP_FLOOD_OPTIMIZED ] ) ;
2021-02-13 12:08:08 +00:00
while ( s > 1 ) {
s / = 2 ;
push_constant . step_size = s ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , jump_flood_uniform_set [ jf_us ] , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_size , cascade_size , cascade_size ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
jf_us = jf_us = = 0 ? 1 : 0 ;
}
}
}
RENDER_TIMESTAMP ( " SDFGI Occlusion " ) ;
// occlusion
{
uint32_t probe_size = cascade_size / SDFGI : : PROBE_DIVISOR ;
Vector3i probe_global_pos = cascades [ cascade ] . position / probe_size ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_OCCLUSION ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , occlusion_uniform_set , 0 ) ;
for ( int i = 0 ; i < 8 ; i + + ) {
//dispatch all at once for performance
Vector3i offset ( i & 1 , ( i > > 1 ) & 1 , ( i > > 2 ) & 1 ) ;
if ( ( probe_global_pos . x & 1 ) ! = 0 ) {
offset . x = ( offset . x + 1 ) & 1 ;
}
if ( ( probe_global_pos . y & 1 ) ! = 0 ) {
offset . y = ( offset . y + 1 ) & 1 ;
}
if ( ( probe_global_pos . z & 1 ) ! = 0 ) {
offset . z = ( offset . z + 1 ) & 1 ;
}
push_constant . probe_offset [ 0 ] = offset . x ;
push_constant . probe_offset [ 1 ] = offset . y ;
push_constant . probe_offset [ 2 ] = offset . z ;
push_constant . occlusion_index = i ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
Vector3i groups = Vector3i ( probe_size + 1 , probe_size + 1 , probe_size + 1 ) - offset ; //if offset, it's one less probe per axis to compute
RD : : get_singleton ( ) - > compute_list_dispatch ( compute_list , groups . x , groups . y , groups . z ) ;
}
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
}
RENDER_TIMESTAMP ( " SDFGI Store " ) ;
// store
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . preprocess_pipeline [ SDFGIShader : : PRE_PROCESS_STORE ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cascades [ cascade ] . sdf_store_uniform_set , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SDFGIShader : : PreprocessPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , cascade_size , cascade_size , cascade_size ) ;
RD : : get_singleton ( ) - > compute_list_end ( ) ;
//clear these textures, as they will have previous garbage on next draw
RD : : get_singleton ( ) - > texture_clear ( cascades [ cascade ] . light_tex , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , 1 ) ;
RD : : get_singleton ( ) - > texture_clear ( cascades [ cascade ] . light_aniso_0_tex , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , 1 ) ;
RD : : get_singleton ( ) - > texture_clear ( cascades [ cascade ] . light_aniso_1_tex , Color ( 0 , 0 , 0 , 0 ) , 0 , 1 , 0 , 1 ) ;
#if 0
Vector < uint8_t > data = RD : : get_singleton ( ) - > texture_get_data ( cascades [ cascade ] . sdf , 0 ) ;
Ref < Image > img ;
2021-06-17 22:03:09 +00:00
img . instantiate ( ) ;
2021-02-13 12:08:08 +00:00
for ( uint32_t i = 0 ; i < cascade_size ; i + + ) {
2021-11-27 01:18:26 +00:00
Vector < uint8_t > subarr = data . slice ( 128 * 128 * i , 128 * 128 * ( i + 1 ) ) ;
2022-07-22 18:06:19 +00:00
img - > set_data ( cascade_size , cascade_size , false , Image : : FORMAT_L8 , subarr ) ;
2021-02-13 12:08:08 +00:00
img - > save_png ( " res://cascade_sdf_ " + itos ( cascade ) + " _ " + itos ( i ) + " .png " ) ;
}
//finalize render and update sdf
# endif
#if 0
Vector < uint8_t > data = RD : : get_singleton ( ) - > texture_get_data ( render_albedo , 0 ) ;
Ref < Image > img ;
2021-06-17 22:03:09 +00:00
img . instantiate ( ) ;
2021-02-13 12:08:08 +00:00
for ( uint32_t i = 0 ; i < cascade_size ; i + + ) {
2021-11-27 01:18:26 +00:00
Vector < uint8_t > subarr = data . slice ( 128 * 128 * i * 2 , 128 * 128 * ( i + 1 ) * 2 ) ;
2021-02-13 12:08:08 +00:00
img - > createcascade_size , cascade_size , false , Image : : FORMAT_RGB565 , subarr ) ;
img - > convert ( Image : : FORMAT_RGBA8 ) ;
img - > save_png ( " res://cascade_ " + itos ( cascade ) + " _ " + itos ( i ) + " .png " ) ;
}
//finalize render and update sdf
# endif
2022-03-04 07:44:29 +00:00
RENDER_TIMESTAMP ( " < SDFGI Update SDF " ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
}
2022-09-12 09:44:48 +00:00
void GI : : SDFGI : : render_static_lights ( RenderDataRD * p_render_data , Ref < RenderSceneBuffersRD > p_render_buffers , uint32_t p_cascade_count , const uint32_t * p_cascade_indices , const PagedArray < RID > * p_positional_light_cull_result ) {
2022-08-04 08:40:39 +00:00
ERR_FAIL_COND ( p_render_buffers . is_null ( ) ) ; // we wouldn't be here if this failed but...
2021-02-13 12:08:08 +00:00
2022-09-12 09:44:48 +00:00
RendererRD : : LightStorage * light_storage = RendererRD : : LightStorage : : get_singleton ( ) ;
2022-03-04 07:44:29 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " SDFGI Render Static Lights " ) ;
2021-02-13 12:08:08 +00:00
update_cascades ( ) ;
2021-03-04 00:53:09 +00:00
SDFGIShader : : Light lights [ SDFGI : : MAX_STATIC_LIGHTS ] ;
2021-02-13 12:08:08 +00:00
uint32_t light_count [ SDFGI : : MAX_STATIC_LIGHTS ] ;
for ( uint32_t i = 0 ; i < p_cascade_count ; i + + ) {
ERR_CONTINUE ( p_cascade_indices [ i ] > = cascades . size ( ) ) ;
SDFGI : : Cascade & cc = cascades [ p_cascade_indices [ i ] ] ;
{ //fill light buffer
AABB cascade_aabb ;
cascade_aabb . position = Vector3 ( ( Vector3i ( 1 , 1 , 1 ) * - int32_t ( cascade_size > > 1 ) + cc . position ) ) * cc . cell_size ;
cascade_aabb . size = Vector3 ( 1 , 1 , 1 ) * cascade_size * cc . cell_size ;
int idx = 0 ;
for ( uint32_t j = 0 ; j < ( uint32_t ) p_positional_light_cull_result [ i ] . size ( ) ; j + + ) {
if ( idx = = SDFGI : : MAX_STATIC_LIGHTS ) {
break ;
}
2022-09-12 09:44:48 +00:00
RID light_instance = p_positional_light_cull_result [ i ] [ j ] ;
ERR_CONTINUE ( ! light_storage - > owns_light_instance ( light_instance ) ) ;
RID light = light_storage - > light_instance_get_base_light ( light_instance ) ;
AABB light_aabb = light_storage - > light_instance_get_base_aabb ( light_instance ) ;
Transform3D light_transform = light_storage - > light_instance_get_base_transform ( light_instance ) ;
2021-02-13 12:08:08 +00:00
2022-09-12 09:44:48 +00:00
uint32_t max_sdfgi_cascade = RSG : : light_storage - > light_get_max_sdfgi_cascade ( light ) ;
2021-02-13 12:08:08 +00:00
if ( p_cascade_indices [ i ] > max_sdfgi_cascade ) {
continue ;
}
2022-09-12 09:44:48 +00:00
if ( ! cascade_aabb . intersects ( light_aabb ) ) {
2021-02-13 12:08:08 +00:00
continue ;
}
2022-09-12 09:44:48 +00:00
lights [ idx ] . type = RSG : : light_storage - > light_get_type ( light ) ;
2021-02-13 12:08:08 +00:00
2022-09-12 09:44:48 +00:00
Vector3 dir = - light_transform . basis . get_column ( Vector3 : : AXIS_Z ) ;
2021-02-13 12:08:08 +00:00
if ( lights [ idx ] . type = = RS : : LIGHT_DIRECTIONAL ) {
dir . y * = y_mult ; //only makes sense for directional
dir . normalize ( ) ;
}
lights [ idx ] . direction [ 0 ] = dir . x ;
lights [ idx ] . direction [ 1 ] = dir . y ;
lights [ idx ] . direction [ 2 ] = dir . z ;
2022-09-12 09:44:48 +00:00
Vector3 pos = light_transform . origin ;
2021-02-13 12:08:08 +00:00
pos . y * = y_mult ;
lights [ idx ] . position [ 0 ] = pos . x ;
lights [ idx ] . position [ 1 ] = pos . y ;
lights [ idx ] . position [ 2 ] = pos . z ;
2022-09-12 09:44:48 +00:00
Color color = RSG : : light_storage - > light_get_color ( light ) ;
2022-04-13 08:37:22 +00:00
color = color . srgb_to_linear ( ) ;
2021-02-13 12:08:08 +00:00
lights [ idx ] . color [ 0 ] = color . r ;
lights [ idx ] . color [ 1 ] = color . g ;
lights [ idx ] . color [ 2 ] = color . b ;
2022-07-31 23:20:24 +00:00
2022-09-12 09:44:48 +00:00
lights [ idx ] . energy = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_ENERGY ) * RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) ;
if ( RendererSceneRenderRD : : get_singleton ( ) - > is_using_physical_light_units ( ) ) {
lights [ idx ] . energy * = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_INTENSITY ) ;
2022-07-31 23:20:24 +00:00
// Convert from Luminous Power to Luminous Intensity
if ( lights [ idx ] . type = = RS : : LIGHT_OMNI ) {
lights [ idx ] . energy * = 1.0 / ( Math_PI * 4.0 ) ;
} else if ( lights [ idx ] . type = = RS : : LIGHT_SPOT ) {
// Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
// We make this assumption to keep them easy to control.
lights [ idx ] . energy * = 1.0 / Math_PI ;
}
}
if ( p_render_data - > camera_attributes . is_valid ( ) ) {
lights [ idx ] . energy * = RSG : : camera_attributes - > camera_attributes_get_exposure_normalization_factor ( p_render_data - > camera_attributes ) ;
}
2022-09-12 09:44:48 +00:00
lights [ idx ] . has_shadow = RSG : : light_storage - > light_has_shadow ( light ) ;
lights [ idx ] . attenuation = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_ATTENUATION ) ;
lights [ idx ] . radius = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_RANGE ) ;
lights [ idx ] . cos_spot_angle = Math : : cos ( Math : : deg_to_rad ( RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_SPOT_ANGLE ) ) ) ;
lights [ idx ] . inv_spot_attenuation = 1.0f / RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ;
2021-02-13 12:08:08 +00:00
idx + + ;
}
if ( idx > 0 ) {
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > buffer_update ( cc . lights_buffer , 0 , idx * sizeof ( SDFGIShader : : Light ) , lights ) ;
2021-02-13 12:08:08 +00:00
}
light_count [ i ] = idx ;
}
}
2023-11-24 11:23:22 +00:00
for ( uint32_t i = 0 ; i < p_cascade_count ; i + + ) {
ERR_CONTINUE ( p_cascade_indices [ i ] > = cascades . size ( ) ) ;
SDFGI : : Cascade & cc = cascades [ p_cascade_indices [ i ] ] ;
if ( light_count [ i ] > 0 ) {
RD : : get_singleton ( ) - > buffer_copy ( cc . solid_cell_dispatch_buffer_storage , cc . solid_cell_dispatch_buffer_call , 0 , 0 , sizeof ( uint32_t ) * 4 ) ;
}
}
2021-02-13 12:08:08 +00:00
/* Static Lights */
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > sdfgi_shader . direct_light_pipeline [ SDFGIShader : : DIRECT_LIGHT_MODE_STATIC ] ) ;
2021-02-13 12:08:08 +00:00
2021-03-04 00:53:09 +00:00
SDFGIShader : : DirectLightPushConstant dl_push_constant ;
2021-02-13 12:08:08 +00:00
dl_push_constant . grid_size [ 0 ] = cascade_size ;
dl_push_constant . grid_size [ 1 ] = cascade_size ;
dl_push_constant . grid_size [ 2 ] = cascade_size ;
dl_push_constant . max_cascades = cascades . size ( ) ;
dl_push_constant . probe_axis_size = probe_axis_count ;
dl_push_constant . bounce_feedback = 0.0 ; // this is static light, do not multibounce yet
dl_push_constant . y_mult = y_mult ;
dl_push_constant . use_occlusion = uses_occlusion ;
//all must be processed
dl_push_constant . process_offset = 0 ;
dl_push_constant . process_increment = 1 ;
for ( uint32_t i = 0 ; i < p_cascade_count ; i + + ) {
ERR_CONTINUE ( p_cascade_indices [ i ] > = cascades . size ( ) ) ;
SDFGI : : Cascade & cc = cascades [ p_cascade_indices [ i ] ] ;
dl_push_constant . light_count = light_count [ i ] ;
dl_push_constant . cascade = p_cascade_indices [ i ] ;
if ( dl_push_constant . light_count > 0 ) {
2022-07-09 11:27:24 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , cc . sdf_direct_light_static_uniform_set , 0 ) ;
2021-03-04 00:53:09 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & dl_push_constant , sizeof ( SDFGIShader : : DirectLightPushConstant ) ) ;
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_indirect ( compute_list , cc . solid_cell_dispatch_buffer_call , 0 ) ;
2021-02-13 12:08:08 +00:00
}
}
RD : : get_singleton ( ) - > compute_list_end ( ) ;
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
////////////////////////////////////////////////////////////////////////////////
2021-06-04 22:47:26 +00:00
// VoxelGIInstance
2021-02-13 12:08:08 +00:00
2022-09-12 09:44:48 +00:00
void GI : : VoxelGIInstance : : update ( bool p_update_light_instances , const Vector < RID > & p_light_instances , const PagedArray < RenderGeometryInstance * > & p_dynamic_objects ) {
RendererRD : : LightStorage * light_storage = RendererRD : : LightStorage : : get_singleton ( ) ;
2022-04-12 11:41:50 +00:00
RendererRD : : MaterialStorage * material_storage = RendererRD : : MaterialStorage : : get_singleton ( ) ;
2022-05-20 02:52:19 +00:00
uint32_t data_version = gi - > voxel_gi_get_data_version ( probe ) ;
2021-02-13 12:08:08 +00:00
// (RE)CREATE IF NEEDED
if ( last_probe_data_version ! = data_version ) {
//need to re-create everything
2022-08-09 05:18:03 +00:00
free_resources ( ) ;
2021-02-13 12:08:08 +00:00
2022-05-20 02:52:19 +00:00
Vector3i octree_size = gi - > voxel_gi_get_octree_size ( probe ) ;
2021-02-13 12:08:08 +00:00
if ( octree_size ! = Vector3i ( ) ) {
//can create a 3D texture
2022-05-20 02:52:19 +00:00
Vector < int > levels = gi - > voxel_gi_get_level_counts ( probe ) ;
2021-02-13 12:08:08 +00:00
RD : : TextureFormat tf ;
tf . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tf . width = octree_size . x ;
tf . height = octree_size . y ;
tf . depth = octree_size . z ;
tf . texture_type = RD : : TEXTURE_TYPE_3D ;
tf . mipmaps = levels . size ( ) ;
tf . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_TO_BIT ;
texture = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( texture , " VoxelGI Instance Texture " ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > texture_clear ( texture , Color ( 0 , 0 , 0 , 0 ) , 0 , levels . size ( ) , 0 , 1 ) ;
{
int total_elements = 0 ;
for ( int i = 0 ; i < levels . size ( ) ; i + + ) {
total_elements + = levels [ i ] ;
}
write_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( total_elements * 16 ) ;
}
for ( int i = 0 ; i < levels . size ( ) ; i + + ) {
2021-06-04 22:47:26 +00:00
VoxelGIInstance : : Mipmap mipmap ;
2021-08-03 07:07:32 +00:00
mipmap . texture = RD : : get_singleton ( ) - > texture_create_shared_from_slice ( RD : : TextureView ( ) , texture , 0 , i , 1 , RD : : TEXTURE_SLICE_3D ) ;
2021-02-13 12:08:08 +00:00
mipmap . level = levels . size ( ) - i - 1 ;
mipmap . cell_offset = 0 ;
for ( uint32_t j = 0 ; j < mipmap . level ; j + + ) {
mipmap . cell_offset + = levels [ j ] ;
}
mipmap . cell_count = levels [ mipmap . level ] ;
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 1 ;
2022-05-20 02:52:19 +00:00
u . append_id ( gi - > voxel_gi_get_octree_buffer ( probe ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 2 ;
2022-05-20 02:52:19 +00:00
u . append_id ( gi - > voxel_gi_get_data_buffer ( probe ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 4 ;
2022-03-06 11:57:09 +00:00
u . append_id ( write_buffer ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 9 ;
2022-05-20 02:52:19 +00:00
u . append_id ( gi - > voxel_gi_get_sdf_texture ( probe ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 10 ;
2022-04-12 11:41:50 +00:00
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
Vector < RD : : Uniform > copy_uniforms = uniforms ;
if ( i = = 0 ) {
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
u . binding = 3 ;
2022-03-06 11:57:09 +00:00
u . append_id ( gi - > voxel_gi_lights_uniform ) ;
2021-02-13 12:08:08 +00:00
copy_uniforms . push_back ( u ) ;
}
2021-06-04 22:47:26 +00:00
mipmap . uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( copy_uniforms , gi - > voxel_gi_lighting_shader_version_shaders [ VOXEL_GI_SHADER_VERSION_COMPUTE_LIGHT ] , 0 ) ;
2021-02-13 12:08:08 +00:00
copy_uniforms = uniforms ; //restore
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 5 ;
2022-03-06 11:57:09 +00:00
u . append_id ( texture ) ;
2021-02-13 12:08:08 +00:00
copy_uniforms . push_back ( u ) ;
}
2021-06-04 22:47:26 +00:00
mipmap . second_bounce_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( copy_uniforms , gi - > voxel_gi_lighting_shader_version_shaders [ VOXEL_GI_SHADER_VERSION_COMPUTE_SECOND_BOUNCE ] , 0 ) ;
2021-02-13 12:08:08 +00:00
} else {
2021-06-04 22:47:26 +00:00
mipmap . uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( copy_uniforms , gi - > voxel_gi_lighting_shader_version_shaders [ VOXEL_GI_SHADER_VERSION_COMPUTE_MIPMAP ] , 0 ) ;
2021-02-13 12:08:08 +00:00
}
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 5 ;
2022-03-06 11:57:09 +00:00
u . append_id ( mipmap . texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-06-04 22:47:26 +00:00
mipmap . write_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > voxel_gi_lighting_shader_version_shaders [ VOXEL_GI_SHADER_VERSION_WRITE_TEXTURE ] , 0 ) ;
2021-02-13 12:08:08 +00:00
mipmaps . push_back ( mipmap ) ;
}
{
uint32_t dynamic_map_size = MAX ( MAX ( octree_size . x , octree_size . y ) , octree_size . z ) ;
uint32_t oversample = nearest_power_of_2_templated ( 4 ) ;
int mipmap_index = 0 ;
while ( mipmap_index < mipmaps . size ( ) ) {
2021-06-04 22:47:26 +00:00
VoxelGIInstance : : DynamicMap dmap ;
2021-02-13 12:08:08 +00:00
if ( oversample > 0 ) {
dmap . size = dynamic_map_size * ( 1 < < oversample ) ;
dmap . mipmap = - 1 ;
oversample - - ;
} else {
dmap . size = dynamic_map_size > > mipmap_index ;
dmap . mipmap = mipmap_index ;
mipmap_index + + ;
}
RD : : TextureFormat dtf ;
dtf . width = dmap . size ;
dtf . height = dmap . size ;
dtf . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
dtf . usage_bits = RD : : TEXTURE_USAGE_STORAGE_BIT ;
if ( dynamic_maps . size ( ) = = 0 ) {
dtf . usage_bits | = RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT ;
}
dmap . texture = RD : : get_singleton ( ) - > texture_create ( dtf , RD : : TextureView ( ) ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( dmap . texture , " VoxelGI Instance DMap Texture " ) ;
2021-02-13 12:08:08 +00:00
if ( dynamic_maps . size ( ) = = 0 ) {
2021-11-28 17:51:36 +00:00
// Render depth for first one.
// Use 16-bit depth when supported to improve performance.
dtf . format = RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_D16_UNORM , RD : : TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT ) ? RD : : DATA_FORMAT_D16_UNORM : RD : : DATA_FORMAT_X8_D24_UNORM_PACK32 ;
2021-02-13 12:08:08 +00:00
dtf . usage_bits = RD : : TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT ;
dmap . fb_depth = RD : : get_singleton ( ) - > texture_create ( dtf , RD : : TextureView ( ) ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( dmap . fb_depth , " VoxelGI Instance DMap FB Depth " ) ;
2021-02-13 12:08:08 +00:00
}
//just use depth as-is
dtf . format = RD : : DATA_FORMAT_R32_SFLOAT ;
dtf . usage_bits = RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT ;
dmap . depth = RD : : get_singleton ( ) - > texture_create ( dtf , RD : : TextureView ( ) ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( dmap . depth , " VoxelGI Instance DMap Depth " ) ;
2021-02-13 12:08:08 +00:00
if ( dynamic_maps . size ( ) = = 0 ) {
dtf . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
dtf . usage_bits = RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT ;
dmap . albedo = RD : : get_singleton ( ) - > texture_create ( dtf , RD : : TextureView ( ) ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( dmap . albedo , " VoxelGI Instance DMap Albedo " ) ;
2021-02-13 12:08:08 +00:00
dmap . normal = RD : : get_singleton ( ) - > texture_create ( dtf , RD : : TextureView ( ) ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( dmap . normal , " VoxelGI Instance DMap Normal " ) ;
2021-02-13 12:08:08 +00:00
dmap . orm = RD : : get_singleton ( ) - > texture_create ( dtf , RD : : TextureView ( ) ) ;
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > set_resource_name ( dmap . orm , " VoxelGI Instance DMap ORM " ) ;
2021-02-13 12:08:08 +00:00
Vector < RID > fb ;
fb . push_back ( dmap . albedo ) ;
fb . push_back ( dmap . normal ) ;
fb . push_back ( dmap . orm ) ;
fb . push_back ( dmap . texture ) ; //emission
fb . push_back ( dmap . depth ) ;
fb . push_back ( dmap . fb_depth ) ;
dmap . fb = RD : : get_singleton ( ) - > framebuffer_create ( fb ) ;
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
u . binding = 3 ;
2022-03-06 11:57:09 +00:00
u . append_id ( gi - > voxel_gi_lights_uniform ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 5 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dmap . albedo ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 6 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dmap . normal ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 7 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dmap . orm ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 8 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dmap . fb_depth ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 9 ;
2022-05-20 02:52:19 +00:00
u . append_id ( gi - > voxel_gi_get_sdf_texture ( probe ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 10 ;
2022-04-12 11:41:50 +00:00
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 11 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dmap . texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 12 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dmap . depth ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
2021-06-04 22:47:26 +00:00
dmap . uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > voxel_gi_lighting_shader_version_shaders [ VOXEL_GI_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING ] , 0 ) ;
2021-02-13 12:08:08 +00:00
}
} else {
bool plot = dmap . mipmap > = 0 ;
bool write = dmap . mipmap < ( mipmaps . size ( ) - 1 ) ;
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 5 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dynamic_maps [ dynamic_maps . size ( ) - 1 ] . texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 6 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dynamic_maps [ dynamic_maps . size ( ) - 1 ] . depth ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
if ( write ) {
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 7 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dmap . texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 8 ;
2022-03-06 11:57:09 +00:00
u . append_id ( dmap . depth ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 9 ;
2022-05-20 02:52:19 +00:00
u . append_id ( gi - > voxel_gi_get_sdf_texture ( probe ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 10 ;
2022-04-12 11:41:50 +00:00
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
if ( plot ) {
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 11 ;
2022-03-06 11:57:09 +00:00
u . append_id ( mipmaps [ dmap . mipmap ] . texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
}
dmap . uniform_set = RD : : get_singleton ( ) - > uniform_set_create (
uniforms ,
2021-06-04 22:47:26 +00:00
gi - > voxel_gi_lighting_shader_version_shaders [ ( write & & plot ) ? VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT : ( write ? VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE : VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_PLOT ) ] ,
2021-02-13 12:08:08 +00:00
0 ) ;
}
dynamic_maps . push_back ( dmap ) ;
}
}
}
last_probe_data_version = data_version ;
p_update_light_instances = true ; //just in case
2022-09-12 09:44:48 +00:00
RendererSceneRenderRD : : get_singleton ( ) - > base_uniforms_changed ( ) ;
2021-02-13 12:08:08 +00:00
}
// UDPDATE TIME
if ( has_dynamic_object_data ) {
//if it has dynamic object data, it needs to be cleared
RD : : get_singleton ( ) - > texture_clear ( texture , Color ( 0 , 0 , 0 , 0 ) , 0 , mipmaps . size ( ) , 0 , 1 ) ;
}
uint32_t light_count = 0 ;
if ( p_update_light_instances | | p_dynamic_objects . size ( ) > 0 ) {
2021-06-04 22:47:26 +00:00
light_count = MIN ( gi - > voxel_gi_max_lights , ( uint32_t ) p_light_instances . size ( ) ) ;
2021-02-13 12:08:08 +00:00
{
2022-05-20 02:52:19 +00:00
Transform3D to_cell = gi - > voxel_gi_get_to_cell_xform ( probe ) ;
2022-10-05 08:56:19 +00:00
Transform3D to_probe_xform = to_cell * transform . affine_inverse ( ) ;
2021-02-13 12:08:08 +00:00
//update lights
for ( uint32_t i = 0 ; i < light_count ; i + + ) {
2021-06-04 22:47:26 +00:00
VoxelGILight & l = gi - > voxel_gi_lights [ i ] ;
2021-02-13 12:08:08 +00:00
RID light_instance = p_light_instances [ i ] ;
2022-09-12 09:44:48 +00:00
RID light = light_storage - > light_instance_get_base_light ( light_instance ) ;
2021-02-13 12:08:08 +00:00
2022-04-09 09:34:31 +00:00
l . type = RSG : : light_storage - > light_get_type ( light ) ;
if ( l . type = = RS : : LIGHT_DIRECTIONAL & & RSG : : light_storage - > light_directional_get_sky_mode ( light ) = = RS : : LIGHT_DIRECTIONAL_SKY_MODE_SKY_ONLY ) {
2021-02-13 12:08:08 +00:00
light_count - - ;
continue ;
}
2022-04-09 09:34:31 +00:00
l . attenuation = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_ATTENUATION ) ;
l . energy = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_ENERGY ) * RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_INDIRECT_ENERGY ) ;
2022-07-31 23:20:24 +00:00
2022-09-12 09:44:48 +00:00
if ( RendererSceneRenderRD : : get_singleton ( ) - > is_using_physical_light_units ( ) ) {
2022-07-31 23:20:24 +00:00
l . energy * = RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_INTENSITY ) ;
l . energy * = gi - > voxel_gi_get_baked_exposure_normalization ( probe ) ;
// Convert from Luminous Power to Luminous Intensity
if ( l . type = = RS : : LIGHT_OMNI ) {
l . energy * = 1.0 / ( Math_PI * 4.0 ) ;
} else if ( l . type = = RS : : LIGHT_SPOT ) {
// Spot Lights are not physically accurate, Luminous Intensity should change in relation to the cone angle.
// We make this assumption to keep them easy to control.
l . energy * = 1.0 / Math_PI ;
}
}
2022-04-09 09:34:31 +00:00
l . radius = to_cell . basis . xform ( Vector3 ( RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_RANGE ) , 0 , 0 ) ) . length ( ) ;
Color color = RSG : : light_storage - > light_get_color ( light ) . srgb_to_linear ( ) ;
2021-02-13 12:08:08 +00:00
l . color [ 0 ] = color . r ;
l . color [ 1 ] = color . g ;
l . color [ 2 ] = color . b ;
2022-08-13 15:45:42 +00:00
l . cos_spot_angle = Math : : cos ( Math : : deg_to_rad ( RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_SPOT_ANGLE ) ) ) ;
2022-04-09 09:34:31 +00:00
l . inv_spot_attenuation = 1.0f / RSG : : light_storage - > light_get_param ( light , RS : : LIGHT_PARAM_SPOT_ATTENUATION ) ;
2021-02-13 12:08:08 +00:00
2022-09-12 09:44:48 +00:00
Transform3D xform = light_storage - > light_instance_get_base_transform ( light_instance ) ;
2021-02-13 12:08:08 +00:00
Vector3 pos = to_probe_xform . xform ( xform . origin ) ;
2022-05-03 12:50:35 +00:00
Vector3 dir = to_probe_xform . basis . xform ( - xform . basis . get_column ( 2 ) ) . normalized ( ) ;
2021-02-13 12:08:08 +00:00
l . position [ 0 ] = pos . x ;
l . position [ 1 ] = pos . y ;
l . position [ 2 ] = pos . z ;
l . direction [ 0 ] = dir . x ;
l . direction [ 1 ] = dir . y ;
l . direction [ 2 ] = dir . z ;
2022-04-09 09:34:31 +00:00
l . has_shadow = RSG : : light_storage - > light_has_shadow ( light ) ;
2021-02-13 12:08:08 +00:00
}
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > buffer_update ( gi - > voxel_gi_lights_uniform , 0 , sizeof ( VoxelGILight ) * light_count , gi - > voxel_gi_lights ) ;
2021-02-13 12:08:08 +00:00
}
}
if ( has_dynamic_object_data | | p_update_light_instances | | p_dynamic_objects . size ( ) ) {
// PROCESS MIPMAPS
if ( mipmaps . size ( ) ) {
//can update mipmaps
2022-05-20 02:52:19 +00:00
Vector3i probe_size = gi - > voxel_gi_get_octree_size ( probe ) ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
VoxelGIPushConstant push_constant ;
2021-02-13 12:08:08 +00:00
push_constant . limits [ 0 ] = probe_size . x ;
push_constant . limits [ 1 ] = probe_size . y ;
push_constant . limits [ 2 ] = probe_size . z ;
push_constant . stack_size = mipmaps . size ( ) ;
push_constant . emission_scale = 1.0 ;
2022-05-20 02:52:19 +00:00
push_constant . propagation = gi - > voxel_gi_get_propagation ( probe ) ;
push_constant . dynamic_range = gi - > voxel_gi_get_dynamic_range ( probe ) ;
2021-02-13 12:08:08 +00:00
push_constant . light_count = light_count ;
push_constant . aniso_strength = 0 ;
/* print_line("probe update to version " + itos(last_probe_version));
print_line ( " propagation " + rtos ( push_constant . propagation ) ) ;
print_line ( " dynrange " + rtos ( push_constant . dynamic_range ) ) ;
*/
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
int passes ;
if ( p_update_light_instances ) {
2022-05-20 02:52:19 +00:00
passes = gi - > voxel_gi_is_using_two_bounces ( probe ) ? 2 : 1 ;
2021-02-13 12:08:08 +00:00
} else {
passes = 1 ; //only re-blitting is necessary
}
int wg_size = 64 ;
2022-03-17 20:42:35 +00:00
int64_t wg_limit_x = ( int64_t ) RD : : get_singleton ( ) - > limit_get ( RD : : LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X ) ;
2021-02-13 12:08:08 +00:00
for ( int pass = 0 ; pass < passes ; pass + + ) {
if ( p_update_light_instances ) {
for ( int i = 0 ; i < mipmaps . size ( ) ; i + + ) {
if ( i = = 0 ) {
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > voxel_gi_lighting_shader_version_pipelines [ pass = = 0 ? VOXEL_GI_SHADER_VERSION_COMPUTE_LIGHT : VOXEL_GI_SHADER_VERSION_COMPUTE_SECOND_BOUNCE ] ) ;
2021-02-13 12:08:08 +00:00
} else if ( i = = 1 ) {
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > voxel_gi_lighting_shader_version_pipelines [ VOXEL_GI_SHADER_VERSION_COMPUTE_MIPMAP ] ) ;
2021-02-13 12:08:08 +00:00
}
if ( pass = = 1 | | i > 0 ) {
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ; //wait til previous step is done
}
if ( pass = = 0 | | i > 0 ) {
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , mipmaps [ i ] . uniform_set , 0 ) ;
} else {
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , mipmaps [ i ] . second_bounce_uniform_set , 0 ) ;
}
push_constant . cell_offset = mipmaps [ i ] . cell_offset ;
push_constant . cell_count = mipmaps [ i ] . cell_count ;
2023-08-07 02:21:58 +00:00
int64_t wg_todo = ( mipmaps [ i ] . cell_count + wg_size - 1 ) / wg_size ;
2021-02-13 12:08:08 +00:00
while ( wg_todo ) {
2022-03-17 20:42:35 +00:00
int64_t wg_count = MIN ( wg_todo , wg_limit_x ) ;
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( VoxelGIPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch ( compute_list , wg_count , 1 , 1 ) ;
wg_todo - = wg_count ;
push_constant . cell_offset + = wg_count * wg_size ;
}
}
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ; //wait til previous step is done
}
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > voxel_gi_lighting_shader_version_pipelines [ VOXEL_GI_SHADER_VERSION_WRITE_TEXTURE ] ) ;
2021-02-13 12:08:08 +00:00
for ( int i = 0 ; i < mipmaps . size ( ) ; i + + ) {
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , mipmaps [ i ] . write_uniform_set , 0 ) ;
push_constant . cell_offset = mipmaps [ i ] . cell_offset ;
push_constant . cell_count = mipmaps [ i ] . cell_count ;
2023-08-07 02:21:58 +00:00
int64_t wg_todo = ( mipmaps [ i ] . cell_count + wg_size - 1 ) / wg_size ;
2021-02-13 12:08:08 +00:00
while ( wg_todo ) {
2022-03-10 17:43:27 +00:00
int64_t wg_count = MIN ( wg_todo , wg_limit_x ) ;
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( VoxelGIPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch ( compute_list , wg_count , 1 , 1 ) ;
wg_todo - = wg_count ;
push_constant . cell_offset + = wg_count * wg_size ;
}
}
}
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
}
has_dynamic_object_data = false ; //clear until dynamic object data is used again
if ( p_dynamic_objects . size ( ) & & dynamic_maps . size ( ) ) {
2022-05-20 02:52:19 +00:00
Vector3i octree_size = gi - > voxel_gi_get_octree_size ( probe ) ;
2021-02-13 12:08:08 +00:00
int multiplier = dynamic_maps [ 0 ] . size / MAX ( MAX ( octree_size . x , octree_size . y ) , octree_size . z ) ;
2020-10-17 05:08:21 +00:00
Transform3D oversample_scale ;
2021-02-13 12:08:08 +00:00
oversample_scale . basis . scale ( Vector3 ( multiplier , multiplier , multiplier ) ) ;
2022-05-20 02:52:19 +00:00
Transform3D to_cell = oversample_scale * gi - > voxel_gi_get_to_cell_xform ( probe ) ;
2020-10-17 05:08:21 +00:00
Transform3D to_world_xform = transform * to_cell . affine_inverse ( ) ;
Transform3D to_probe_xform = to_world_xform . affine_inverse ( ) ;
2021-02-13 12:08:08 +00:00
AABB probe_aabb ( Vector3 ( ) , octree_size ) ;
//this could probably be better parallelized in compute..
for ( int i = 0 ; i < ( int ) p_dynamic_objects . size ( ) ; i + + ) {
2022-07-23 14:01:53 +00:00
RenderGeometryInstance * instance = p_dynamic_objects [ i ] ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
//transform aabb to voxel_gi
2022-07-23 14:01:53 +00:00
AABB aabb = ( to_probe_xform * instance - > get_transform ( ) ) . xform ( instance - > get_aabb ( ) ) ;
2021-02-13 12:08:08 +00:00
//this needs to wrap to grid resolution to avoid jitter
//also extend margin a bit just in case
Vector3i begin = aabb . position - Vector3i ( 1 , 1 , 1 ) ;
Vector3i end = aabb . position + aabb . size + Vector3i ( 1 , 1 , 1 ) ;
for ( int j = 0 ; j < 3 ; j + + ) {
if ( ( end [ j ] - begin [ j ] ) & 1 ) {
end [ j ] + + ; //for half extents split, it needs to be even
}
begin [ j ] = MAX ( begin [ j ] , 0 ) ;
end [ j ] = MIN ( end [ j ] , octree_size [ j ] * multiplier ) ;
}
//aabb = aabb.intersection(probe_aabb); //intersect
aabb . position = begin ;
aabb . size = end - begin ;
//print_line("aabb: " + aabb);
for ( int j = 0 ; j < 6 ; j + + ) {
//if (j != 0 && j != 3) {
// continue;
//}
static const Vector3 render_z [ 6 ] = {
Vector3 ( 1 , 0 , 0 ) ,
Vector3 ( 0 , 1 , 0 ) ,
Vector3 ( 0 , 0 , 1 ) ,
Vector3 ( - 1 , 0 , 0 ) ,
Vector3 ( 0 , - 1 , 0 ) ,
Vector3 ( 0 , 0 , - 1 ) ,
} ;
static const Vector3 render_up [ 6 ] = {
Vector3 ( 0 , 1 , 0 ) ,
Vector3 ( 0 , 0 , 1 ) ,
Vector3 ( 0 , 1 , 0 ) ,
Vector3 ( 0 , 1 , 0 ) ,
Vector3 ( 0 , 0 , 1 ) ,
Vector3 ( 0 , 1 , 0 ) ,
} ;
Vector3 render_dir = render_z [ j ] ;
Vector3 up_dir = render_up [ j ] ;
2021-09-20 18:48:52 +00:00
Vector3 center = aabb . get_center ( ) ;
2020-10-17 05:08:21 +00:00
Transform3D xform ;
2021-02-13 12:08:08 +00:00
xform . set_look_at ( center - aabb . size * 0.5 * render_dir , center , up_dir ) ;
2022-05-03 12:50:35 +00:00
Vector3 x_dir = xform . basis . get_column ( 0 ) . abs ( ) ;
2021-02-13 12:08:08 +00:00
int x_axis = int ( Vector3 ( 0 , 1 , 2 ) . dot ( x_dir ) ) ;
2022-05-03 12:50:35 +00:00
Vector3 y_dir = xform . basis . get_column ( 1 ) . abs ( ) ;
2021-02-13 12:08:08 +00:00
int y_axis = int ( Vector3 ( 0 , 1 , 2 ) . dot ( y_dir ) ) ;
2022-05-03 12:50:35 +00:00
Vector3 z_dir = - xform . basis . get_column ( 2 ) ;
2021-02-13 12:08:08 +00:00
int z_axis = int ( Vector3 ( 0 , 1 , 2 ) . dot ( z_dir . abs ( ) ) ) ;
Rect2i rect ( aabb . position [ x_axis ] , aabb . position [ y_axis ] , aabb . size [ x_axis ] , aabb . size [ y_axis ] ) ;
2022-05-03 12:50:35 +00:00
bool x_flip = bool ( Vector3 ( 1 , 1 , 1 ) . dot ( xform . basis . get_column ( 0 ) ) < 0 ) ;
bool y_flip = bool ( Vector3 ( 1 , 1 , 1 ) . dot ( xform . basis . get_column ( 1 ) ) < 0 ) ;
bool z_flip = bool ( Vector3 ( 1 , 1 , 1 ) . dot ( xform . basis . get_column ( 2 ) ) > 0 ) ;
2021-02-13 12:08:08 +00:00
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 ( - rect . size . width / 2 , rect . size . width / 2 , - rect . size . height / 2 , rect . size . height / 2 , 0.0001 , aabb . size [ z_axis ] ) ;
2022-09-12 09:44:48 +00:00
if ( RendererSceneRenderRD : : get_singleton ( ) - > cull_argument . size ( ) = = 0 ) {
RendererSceneRenderRD : : get_singleton ( ) - > cull_argument . push_back ( nullptr ) ;
2021-02-13 12:08:08 +00:00
}
2022-09-12 09:44:48 +00:00
RendererSceneRenderRD : : get_singleton ( ) - > cull_argument [ 0 ] = instance ;
2021-02-13 12:08:08 +00:00
2022-07-31 23:20:24 +00:00
float exposure_normalization = 1.0 ;
2022-09-12 09:44:48 +00:00
if ( RendererSceneRenderRD : : get_singleton ( ) - > is_using_physical_light_units ( ) ) {
2022-07-31 23:20:24 +00:00
exposure_normalization = gi - > voxel_gi_get_baked_exposure_normalization ( probe ) ;
}
2022-09-12 09:44:48 +00:00
RendererSceneRenderRD : : get_singleton ( ) - > _render_material ( to_world_xform * xform , cm , true , RendererSceneRenderRD : : get_singleton ( ) - > cull_argument , dynamic_maps [ 0 ] . fb , Rect2i ( Vector2i ( ) , rect . size ) , exposure_normalization ) ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
VoxelGIDynamicPushConstant push_constant ;
memset ( & push_constant , 0 , sizeof ( VoxelGIDynamicPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
push_constant . limits [ 0 ] = octree_size . x ;
push_constant . limits [ 1 ] = octree_size . y ;
push_constant . limits [ 2 ] = octree_size . z ;
push_constant . light_count = p_light_instances . size ( ) ;
push_constant . x_dir [ 0 ] = x_dir [ 0 ] ;
push_constant . x_dir [ 1 ] = x_dir [ 1 ] ;
push_constant . x_dir [ 2 ] = x_dir [ 2 ] ;
push_constant . y_dir [ 0 ] = y_dir [ 0 ] ;
push_constant . y_dir [ 1 ] = y_dir [ 1 ] ;
push_constant . y_dir [ 2 ] = y_dir [ 2 ] ;
push_constant . z_dir [ 0 ] = z_dir [ 0 ] ;
push_constant . z_dir [ 1 ] = z_dir [ 1 ] ;
push_constant . z_dir [ 2 ] = z_dir [ 2 ] ;
push_constant . z_base = xform . origin [ z_axis ] ;
push_constant . z_sign = ( z_flip ? - 1.0 : 1.0 ) ;
push_constant . pos_multiplier = float ( 1.0 ) / multiplier ;
2022-05-20 02:52:19 +00:00
push_constant . dynamic_range = gi - > voxel_gi_get_dynamic_range ( probe ) ;
2021-02-13 12:08:08 +00:00
push_constant . flip_x = x_flip ;
push_constant . flip_y = y_flip ;
push_constant . rect_pos [ 0 ] = rect . position [ 0 ] ;
push_constant . rect_pos [ 1 ] = rect . position [ 1 ] ;
push_constant . rect_size [ 0 ] = rect . size [ 0 ] ;
push_constant . rect_size [ 1 ] = rect . size [ 1 ] ;
push_constant . prev_rect_ofs [ 0 ] = 0 ;
push_constant . prev_rect_ofs [ 1 ] = 0 ;
push_constant . prev_rect_size [ 0 ] = 0 ;
push_constant . prev_rect_size [ 1 ] = 0 ;
push_constant . on_mipmap = false ;
2022-05-20 02:52:19 +00:00
push_constant . propagation = gi - > voxel_gi_get_propagation ( probe ) ;
2021-02-13 12:08:08 +00:00
push_constant . pad [ 0 ] = 0 ;
push_constant . pad [ 1 ] = 0 ;
push_constant . pad [ 2 ] = 0 ;
//process lighting
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > voxel_gi_lighting_shader_version_pipelines [ VOXEL_GI_SHADER_VERSION_DYNAMIC_OBJECT_LIGHTING ] ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , dynamic_maps [ 0 ] . uniform_set , 0 ) ;
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( VoxelGIDynamicPushConstant ) ) ;
2023-08-07 18:19:20 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch ( compute_list , Math : : division_round_up ( rect . size . x , 8 ) , Math : : division_round_up ( rect . size . y , 8 ) , 1 ) ;
2021-02-13 12:08:08 +00:00
//print_line("rect: " + itos(i) + ": " + rect);
for ( int k = 1 ; k < dynamic_maps . size ( ) ; k + + ) {
// enlarge the rect if needed so all pixels fit when downscaled,
// this ensures downsampling is smooth and optimal because no pixels are left behind
//x
if ( rect . position . x & 1 ) {
rect . size . x + + ;
push_constant . prev_rect_ofs [ 0 ] = 1 ; //this is used to ensure reading is also optimal
} else {
push_constant . prev_rect_ofs [ 0 ] = 0 ;
}
if ( rect . size . x & 1 ) {
rect . size . x + + ;
}
rect . position . x > > = 1 ;
rect . size . x = MAX ( 1 , rect . size . x > > 1 ) ;
//y
if ( rect . position . y & 1 ) {
rect . size . y + + ;
push_constant . prev_rect_ofs [ 1 ] = 1 ;
} else {
push_constant . prev_rect_ofs [ 1 ] = 0 ;
}
if ( rect . size . y & 1 ) {
rect . size . y + + ;
}
rect . position . y > > = 1 ;
rect . size . y = MAX ( 1 , rect . size . y > > 1 ) ;
//shrink limits to ensure plot does not go outside map
if ( dynamic_maps [ k ] . mipmap > 0 ) {
for ( int l = 0 ; l < 3 ; l + + ) {
push_constant . limits [ l ] = MAX ( 1 , push_constant . limits [ l ] > > 1 ) ;
}
}
//print_line("rect: " + itos(i) + ": " + rect);
push_constant . rect_pos [ 0 ] = rect . position [ 0 ] ;
push_constant . rect_pos [ 1 ] = rect . position [ 1 ] ;
push_constant . prev_rect_size [ 0 ] = push_constant . rect_size [ 0 ] ;
push_constant . prev_rect_size [ 1 ] = push_constant . rect_size [ 1 ] ;
push_constant . rect_size [ 0 ] = rect . size [ 0 ] ;
push_constant . rect_size [ 1 ] = rect . size [ 1 ] ;
push_constant . on_mipmap = dynamic_maps [ k ] . mipmap > 0 ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
if ( dynamic_maps [ k ] . mipmap < 0 ) {
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > voxel_gi_lighting_shader_version_pipelines [ VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE ] ) ;
2021-02-13 12:08:08 +00:00
} else if ( k < dynamic_maps . size ( ) - 1 ) {
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > voxel_gi_lighting_shader_version_pipelines [ VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_WRITE_PLOT ] ) ;
2021-02-13 12:08:08 +00:00
} else {
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , gi - > voxel_gi_lighting_shader_version_pipelines [ VOXEL_GI_SHADER_VERSION_DYNAMIC_SHRINK_PLOT ] ) ;
2021-02-13 12:08:08 +00:00
}
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , dynamic_maps [ k ] . uniform_set , 0 ) ;
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( VoxelGIDynamicPushConstant ) ) ;
2023-08-07 18:19:20 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch ( compute_list , Math : : division_round_up ( rect . size . x , 8 ) , Math : : division_round_up ( rect . size . y , 8 ) , 1 ) ;
2021-02-13 12:08:08 +00:00
}
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
}
has_dynamic_object_data = true ; //clear until dynamic object data is used again
}
2022-05-20 02:52:19 +00:00
last_probe_version = gi - > voxel_gi_get_version ( probe ) ;
2021-02-13 12:08:08 +00:00
}
2022-08-09 05:18:03 +00:00
void GI : : VoxelGIInstance : : free_resources ( ) {
if ( texture . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( texture ) ;
RD : : get_singleton ( ) - > free ( write_buffer ) ;
texture = RID ( ) ;
write_buffer = RID ( ) ;
mipmaps . clear ( ) ;
}
for ( int i = 0 ; i < dynamic_maps . size ( ) ; i + + ) {
RD : : get_singleton ( ) - > free ( dynamic_maps [ i ] . texture ) ;
RD : : get_singleton ( ) - > free ( dynamic_maps [ i ] . depth ) ;
// these only exist on the first level...
if ( dynamic_maps [ i ] . fb_depth . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( dynamic_maps [ i ] . fb_depth ) ;
}
if ( dynamic_maps [ i ] . albedo . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( dynamic_maps [ i ] . albedo ) ;
}
if ( dynamic_maps [ i ] . normal . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( dynamic_maps [ i ] . normal ) ;
}
if ( dynamic_maps [ i ] . orm . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( dynamic_maps [ i ] . orm ) ;
}
}
dynamic_maps . clear ( ) ;
}
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
void GI : : VoxelGIInstance : : debug ( RD : : DrawListID p_draw_list , RID p_framebuffer , const Projection & p_camera_with_transform , bool p_lighting , bool p_emission , float p_alpha ) {
2022-04-12 11:41:50 +00:00
RendererRD : : MaterialStorage * material_storage = RendererRD : : MaterialStorage : : get_singleton ( ) ;
2021-02-13 12:08:08 +00:00
if ( mipmaps . size ( ) = = 0 ) {
return ;
}
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 cam_transform = ( p_camera_with_transform * Projection ( transform ) ) * Projection ( gi - > voxel_gi_get_to_cell_xform ( probe ) . affine_inverse ( ) ) ;
2021-02-13 12:08:08 +00:00
int level = 0 ;
2022-05-20 02:52:19 +00:00
Vector3i octree_size = gi - > voxel_gi_get_octree_size ( probe ) ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
VoxelGIDebugPushConstant push_constant ;
2021-02-13 12:08:08 +00:00
push_constant . alpha = p_alpha ;
2022-05-20 02:52:19 +00:00
push_constant . dynamic_range = gi - > voxel_gi_get_dynamic_range ( probe ) ;
2021-02-13 12:08:08 +00:00
push_constant . cell_offset = mipmaps [ level ] . cell_offset ;
push_constant . level = level ;
push_constant . bounds [ 0 ] = octree_size . x > > level ;
push_constant . bounds [ 1 ] = octree_size . y > > level ;
push_constant . bounds [ 2 ] = octree_size . z > > level ;
push_constant . pad = 0 ;
for ( int i = 0 ; i < 4 ; i + + ) {
for ( int j = 0 ; j < 4 ; j + + ) {
2022-10-04 16:44:48 +00:00
push_constant . projection [ i * 4 + j ] = cam_transform . columns [ i ] [ j ] ;
2021-02-13 12:08:08 +00:00
}
}
2021-06-04 22:47:26 +00:00
if ( gi - > voxel_gi_debug_uniform_set . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( gi - > voxel_gi_debug_uniform_set ) ;
2021-02-13 12:08:08 +00:00
}
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 1 ;
2022-05-20 02:52:19 +00:00
u . append_id ( gi - > voxel_gi_get_data_buffer ( probe ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 2 ;
2022-03-06 11:57:09 +00:00
u . append_id ( texture ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 3 ;
2022-04-12 11:41:50 +00:00
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
int cell_count ;
if ( ! p_emission & & p_lighting & & has_dynamic_object_data ) {
cell_count = push_constant . bounds [ 0 ] * push_constant . bounds [ 1 ] * push_constant . bounds [ 2 ] ;
} else {
cell_count = mipmaps [ level ] . cell_count ;
}
2021-06-04 22:47:26 +00:00
gi - > voxel_gi_debug_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , gi - > voxel_gi_debug_shader_version_shaders [ 0 ] , 0 ) ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
int voxel_gi_debug_pipeline = VOXEL_GI_DEBUG_COLOR ;
2021-02-13 12:08:08 +00:00
if ( p_emission ) {
2021-06-04 22:47:26 +00:00
voxel_gi_debug_pipeline = VOXEL_GI_DEBUG_EMISSION ;
2021-02-13 12:08:08 +00:00
} else if ( p_lighting ) {
2021-06-04 22:47:26 +00:00
voxel_gi_debug_pipeline = has_dynamic_object_data ? VOXEL_GI_DEBUG_LIGHT_FULL : VOXEL_GI_DEBUG_LIGHT ;
2021-02-13 12:08:08 +00:00
}
RD : : get_singleton ( ) - > draw_list_bind_render_pipeline (
p_draw_list ,
2021-06-04 22:47:26 +00:00
gi - > voxel_gi_debug_shader_version_pipelines [ voxel_gi_debug_pipeline ] . get_render_pipeline ( RD : : INVALID_ID , RD : : get_singleton ( ) - > framebuffer_get_format ( p_framebuffer ) ) ) ;
RD : : get_singleton ( ) - > draw_list_bind_uniform_set ( p_draw_list , gi - > voxel_gi_debug_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > draw_list_set_push_constant ( p_draw_list , & push_constant , sizeof ( VoxelGIDebugPushConstant ) ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_list_draw ( p_draw_list , false , cell_count , 36 ) ;
}
////////////////////////////////////////////////////////////////////////////////
2022-05-20 02:52:19 +00:00
// GI
GI : : GI ( ) {
singleton = this ;
2021-02-13 12:08:08 +00:00
sdfgi_ray_count = RS : : EnvironmentSDFGIRayCount ( CLAMP ( int32_t ( GLOBAL_GET ( " rendering/global_illumination/sdfgi/probe_ray_count " ) ) , 0 , int32_t ( RS : : ENV_SDFGI_RAY_COUNT_MAX - 1 ) ) ) ;
sdfgi_frames_to_converge = RS : : EnvironmentSDFGIFramesToConverge ( CLAMP ( int32_t ( GLOBAL_GET ( " rendering/global_illumination/sdfgi/frames_to_converge " ) ) , 0 , int32_t ( RS : : ENV_SDFGI_CONVERGE_MAX - 1 ) ) ) ;
sdfgi_frames_to_update_light = RS : : EnvironmentSDFGIFramesToUpdateLight ( CLAMP ( int32_t ( GLOBAL_GET ( " rendering/global_illumination/sdfgi/frames_to_update_lights " ) ) , 0 , int32_t ( RS : : ENV_SDFGI_UPDATE_LIGHT_MAX - 1 ) ) ) ;
}
2022-05-20 02:52:19 +00:00
GI : : ~ GI ( ) {
singleton = nullptr ;
2021-02-13 12:08:08 +00:00
}
2022-07-19 04:17:58 +00:00
void GI : : init ( SkyRD * p_sky ) {
2022-03-12 11:19:59 +00:00
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2022-04-12 11:41:50 +00:00
RendererRD : : MaterialStorage * material_storage = RendererRD : : MaterialStorage : : get_singleton ( ) ;
2022-03-12 11:19:59 +00:00
2021-03-11 03:35:05 +00:00
/* GI */
2021-02-13 12:08:08 +00:00
{
//kinda complicated to compute the amount of slots, we try to use as many as we can
2021-06-04 22:47:26 +00:00
voxel_gi_lights = memnew_arr ( VoxelGILight , voxel_gi_max_lights ) ;
voxel_gi_lights_uniform = RD : : get_singleton ( ) - > uniform_buffer_create ( voxel_gi_max_lights * sizeof ( VoxelGILight ) ) ;
voxel_gi_quality = RS : : VoxelGIQuality ( CLAMP ( int ( GLOBAL_GET ( " rendering/global_illumination/voxel_gi/quality " ) ) , 0 , 1 ) ) ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
String defines = " \n #define MAX_LIGHTS " + itos ( voxel_gi_max_lights ) + " \n " ;
2021-02-13 12:08:08 +00:00
Vector < String > versions ;
versions . push_back ( " \n #define MODE_COMPUTE_LIGHT \n " ) ;
versions . push_back ( " \n #define MODE_SECOND_BOUNCE \n " ) ;
versions . push_back ( " \n #define MODE_UPDATE_MIPMAPS \n " ) ;
versions . push_back ( " \n #define MODE_WRITE_TEXTURE \n " ) ;
versions . push_back ( " \n #define MODE_DYNAMIC \n #define MODE_DYNAMIC_LIGHTING \n " ) ;
versions . push_back ( " \n #define MODE_DYNAMIC \n #define MODE_DYNAMIC_SHRINK \n #define MODE_DYNAMIC_SHRINK_WRITE \n " ) ;
versions . push_back ( " \n #define MODE_DYNAMIC \n #define MODE_DYNAMIC_SHRINK \n #define MODE_DYNAMIC_SHRINK_PLOT \n " ) ;
versions . push_back ( " \n #define MODE_DYNAMIC \n #define MODE_DYNAMIC_SHRINK \n #define MODE_DYNAMIC_SHRINK_PLOT \n #define MODE_DYNAMIC_SHRINK_WRITE \n " ) ;
2021-06-04 22:47:26 +00:00
voxel_gi_shader . initialize ( versions , defines ) ;
voxel_gi_lighting_shader_version = voxel_gi_shader . version_create ( ) ;
for ( int i = 0 ; i < VOXEL_GI_SHADER_VERSION_MAX ; i + + ) {
voxel_gi_lighting_shader_version_shaders [ i ] = voxel_gi_shader . version_get_shader ( voxel_gi_lighting_shader_version , i ) ;
voxel_gi_lighting_shader_version_pipelines [ i ] = RD : : get_singleton ( ) - > compute_pipeline_create ( voxel_gi_lighting_shader_version_shaders [ i ] ) ;
2021-02-13 12:08:08 +00:00
}
}
{
String defines ;
Vector < String > versions ;
versions . push_back ( " \n #define MODE_DEBUG_COLOR \n " ) ;
versions . push_back ( " \n #define MODE_DEBUG_LIGHT \n " ) ;
versions . push_back ( " \n #define MODE_DEBUG_EMISSION \n " ) ;
versions . push_back ( " \n #define MODE_DEBUG_LIGHT \n #define MODE_DEBUG_LIGHT_FULL \n " ) ;
2021-06-04 22:47:26 +00:00
voxel_gi_debug_shader . initialize ( versions , defines ) ;
voxel_gi_debug_shader_version = voxel_gi_debug_shader . version_create ( ) ;
for ( int i = 0 ; i < VOXEL_GI_DEBUG_MAX ; i + + ) {
voxel_gi_debug_shader_version_shaders [ i ] = voxel_gi_debug_shader . version_get_shader ( voxel_gi_debug_shader_version , i ) ;
2021-02-13 12:08:08 +00:00
RD : : PipelineRasterizationState rs ;
rs . cull_mode = RD : : POLYGON_CULL_FRONT ;
RD : : PipelineDepthStencilState ds ;
ds . enable_depth_test = true ;
ds . enable_depth_write = true ;
2024-02-14 13:39:39 +00:00
ds . depth_compare_operator = RD : : COMPARE_OP_GREATER_OR_EQUAL ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi_debug_shader_version_pipelines [ i ] . setup ( voxel_gi_debug_shader_version_shaders [ i ] , RD : : RENDER_PRIMITIVE_TRIANGLES , rs , RD : : PipelineMultisampleState ( ) , ds , RD : : PipelineColorBlendState : : create_disabled ( ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
}
2021-03-11 03:35:05 +00:00
/* SDGFI */
2021-02-13 12:08:08 +00:00
{
Vector < String > preprocess_modes ;
preprocess_modes . push_back ( " \n #define MODE_SCROLL \n " ) ;
preprocess_modes . push_back ( " \n #define MODE_SCROLL_OCCLUSION \n " ) ;
preprocess_modes . push_back ( " \n #define MODE_INITIALIZE_JUMP_FLOOD \n " ) ;
preprocess_modes . push_back ( " \n #define MODE_INITIALIZE_JUMP_FLOOD_HALF \n " ) ;
preprocess_modes . push_back ( " \n #define MODE_JUMPFLOOD \n " ) ;
preprocess_modes . push_back ( " \n #define MODE_JUMPFLOOD_OPTIMIZED \n " ) ;
preprocess_modes . push_back ( " \n #define MODE_UPSCALE_JUMP_FLOOD \n " ) ;
preprocess_modes . push_back ( " \n #define MODE_OCCLUSION \n " ) ;
preprocess_modes . push_back ( " \n #define MODE_STORE \n " ) ;
String defines = " \n #define OCCLUSION_SIZE " + itos ( SDFGI : : CASCADE_SIZE / SDFGI : : PROBE_DIVISOR ) + " \n " ;
sdfgi_shader . preprocess . initialize ( preprocess_modes , defines ) ;
sdfgi_shader . preprocess_shader = sdfgi_shader . preprocess . version_create ( ) ;
2021-03-04 00:53:09 +00:00
for ( int i = 0 ; i < SDFGIShader : : PRE_PROCESS_MAX ; i + + ) {
2021-02-13 12:08:08 +00:00
sdfgi_shader . preprocess_pipeline [ i ] = RD : : get_singleton ( ) - > compute_pipeline_create ( sdfgi_shader . preprocess . version_get_shader ( sdfgi_shader . preprocess_shader , i ) ) ;
}
}
{
//calculate tables
String defines = " \n #define OCT_SIZE " + itos ( SDFGI : : LIGHTPROBE_OCT_SIZE ) + " \n " ;
Vector < String > direct_light_modes ;
direct_light_modes . push_back ( " \n #define MODE_PROCESS_STATIC \n " ) ;
direct_light_modes . push_back ( " \n #define MODE_PROCESS_DYNAMIC \n " ) ;
sdfgi_shader . direct_light . initialize ( direct_light_modes , defines ) ;
sdfgi_shader . direct_light_shader = sdfgi_shader . direct_light . version_create ( ) ;
2021-03-04 00:53:09 +00:00
for ( int i = 0 ; i < SDFGIShader : : DIRECT_LIGHT_MODE_MAX ; i + + ) {
2021-02-13 12:08:08 +00:00
sdfgi_shader . direct_light_pipeline [ i ] = RD : : get_singleton ( ) - > compute_pipeline_create ( sdfgi_shader . direct_light . version_get_shader ( sdfgi_shader . direct_light_shader , i ) ) ;
}
}
{
//calculate tables
String defines = " \n #define OCT_SIZE " + itos ( SDFGI : : LIGHTPROBE_OCT_SIZE ) + " \n " ;
defines + = " \n #define SH_SIZE " + itos ( SDFGI : : SH_SIZE ) + " \n " ;
if ( p_sky - > sky_use_cubemap_array ) {
defines + = " \n #define USE_CUBEMAP_ARRAY \n " ;
}
Vector < String > integrate_modes ;
integrate_modes . push_back ( " \n #define MODE_PROCESS \n " ) ;
integrate_modes . push_back ( " \n #define MODE_STORE \n " ) ;
integrate_modes . push_back ( " \n #define MODE_SCROLL \n " ) ;
integrate_modes . push_back ( " \n #define MODE_SCROLL_STORE \n " ) ;
sdfgi_shader . integrate . initialize ( integrate_modes , defines ) ;
sdfgi_shader . integrate_shader = sdfgi_shader . integrate . version_create ( ) ;
2021-03-04 00:53:09 +00:00
for ( int i = 0 ; i < SDFGIShader : : INTEGRATE_MODE_MAX ; i + + ) {
2021-02-13 12:08:08 +00:00
sdfgi_shader . integrate_pipeline [ i ] = RD : : get_singleton ( ) - > compute_pipeline_create ( sdfgi_shader . integrate . version_get_shader ( sdfgi_shader . integrate_shader , i ) ) ;
}
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 0 ;
2022-07-08 10:25:14 +00:00
if ( p_sky - > sky_use_cubemap_array ) {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_WHITE ) ) ;
2022-07-08 10:25:14 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_CUBEMAP_WHITE ) ) ;
2022-07-08 10:25:14 +00:00
}
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 1 ;
2022-04-12 11:41:50 +00:00
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
2021-02-13 12:08:08 +00:00
uniforms . push_back ( u ) ;
}
sdfgi_shader . integrate_default_sky_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , sdfgi_shader . integrate . version_get_shader ( sdfgi_shader . integrate_shader , 0 ) , 1 ) ;
}
}
//GK
{
//calculate tables
String defines = " \n #define SDFGI_OCT_SIZE " + itos ( SDFGI : : LIGHTPROBE_OCT_SIZE ) + " \n " ;
2022-12-15 20:34:19 +00:00
if ( RendererSceneRenderRD : : get_singleton ( ) - > is_vrs_supported ( ) ) {
defines + = " \n #define USE_VRS \n " ;
}
2023-04-25 12:14:01 +00:00
if ( ! RD : : get_singleton ( ) - > sampler_is_format_supported_for_filter ( RD : : DATA_FORMAT_R8G8_UINT , RD : : SAMPLER_FILTER_LINEAR ) ) {
defines + = " \n #define SAMPLE_VOXEL_GI_NEAREST \n " ;
}
2022-12-15 20:34:19 +00:00
2021-02-13 12:08:08 +00:00
Vector < String > gi_modes ;
2022-02-11 11:33:54 +00:00
2022-05-20 02:52:19 +00:00
gi_modes . push_back ( " \n #define USE_VOXEL_GI_INSTANCES \n " ) ; // MODE_VOXEL_GI
gi_modes . push_back ( " \n #define USE_SDFGI \n " ) ; // MODE_SDFGI
gi_modes . push_back ( " \n #define USE_SDFGI \n \n #define USE_VOXEL_GI_INSTANCES \n " ) ; // MODE_COMBINED
2021-02-13 12:08:08 +00:00
shader . initialize ( gi_modes , defines ) ;
2022-02-11 11:33:54 +00:00
shader_version = shader . version_create ( ) ;
Vector < RD : : PipelineSpecializationConstant > specialization_constants ;
{
RD : : PipelineSpecializationConstant sc ;
sc . type = RD : : PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL ;
sc . constant_id = 0 ; // SHADER_SPECIALIZATION_HALF_RES
sc . bool_value = false ;
specialization_constants . push_back ( sc ) ;
2022-05-20 02:52:19 +00:00
2022-02-11 11:33:54 +00:00
sc . type = RD : : PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL ;
sc . constant_id = 1 ; // SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX
sc . bool_value = false ;
specialization_constants . push_back ( sc ) ;
sc . type = RD : : PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL ;
sc . constant_id = 2 ; // SHADER_SPECIALIZATION_USE_VRS
sc . bool_value = false ;
specialization_constants . push_back ( sc ) ;
2022-05-20 02:52:19 +00:00
}
2022-02-11 11:33:54 +00:00
for ( int v = 0 ; v < SHADER_SPECIALIZATION_VARIATIONS ; v + + ) {
specialization_constants . ptrw ( ) [ 0 ] . bool_value = ( v & SHADER_SPECIALIZATION_HALF_RES ) ? true : false ;
specialization_constants . ptrw ( ) [ 1 ] . bool_value = ( v & SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX ) ? true : false ;
specialization_constants . ptrw ( ) [ 2 ] . bool_value = ( v & SHADER_SPECIALIZATION_USE_VRS ) ? true : false ;
for ( int i = 0 ; i < MODE_MAX ; i + + ) {
pipelines [ v ] [ i ] = RD : : get_singleton ( ) - > compute_pipeline_create ( shader . version_get_shader ( shader_version , i ) , specialization_constants ) ;
2022-05-20 02:52:19 +00:00
}
2021-02-13 12:08:08 +00:00
}
sdfgi_ubo = RD : : get_singleton ( ) - > uniform_buffer_create ( sizeof ( SDFGIData ) ) ;
}
{
String defines = " \n #define OCT_SIZE " + itos ( SDFGI : : LIGHTPROBE_OCT_SIZE ) + " \n " ;
Vector < String > debug_modes ;
debug_modes . push_back ( " " ) ;
sdfgi_shader . debug . initialize ( debug_modes , defines ) ;
sdfgi_shader . debug_shader = sdfgi_shader . debug . version_create ( ) ;
sdfgi_shader . debug_shader_version = sdfgi_shader . debug . version_get_shader ( sdfgi_shader . debug_shader , 0 ) ;
sdfgi_shader . debug_pipeline = RD : : get_singleton ( ) - > compute_pipeline_create ( sdfgi_shader . debug_shader_version ) ;
}
{
String defines = " \n #define OCT_SIZE " + itos ( SDFGI : : LIGHTPROBE_OCT_SIZE ) + " \n " ;
Vector < String > versions ;
versions . push_back ( " \n #define MODE_PROBES \n " ) ;
2022-05-20 02:52:19 +00:00
versions . push_back ( " \n #define MODE_PROBES \n #define USE_MULTIVIEW \n " ) ;
2021-02-13 12:08:08 +00:00
versions . push_back ( " \n #define MODE_VISIBILITY \n " ) ;
2022-05-20 02:52:19 +00:00
versions . push_back ( " \n #define MODE_VISIBILITY \n #define USE_MULTIVIEW \n " ) ;
2021-02-13 12:08:08 +00:00
sdfgi_shader . debug_probes . initialize ( versions , defines ) ;
2022-05-20 02:52:19 +00:00
// TODO disable multiview versions if turned off
2021-02-13 12:08:08 +00:00
sdfgi_shader . debug_probes_shader = sdfgi_shader . debug_probes . version_create ( ) ;
{
RD : : PipelineRasterizationState rs ;
rs . cull_mode = RD : : POLYGON_CULL_DISABLED ;
RD : : PipelineDepthStencilState ds ;
ds . enable_depth_test = true ;
ds . enable_depth_write = true ;
2024-02-14 13:39:39 +00:00
ds . depth_compare_operator = RD : : COMPARE_OP_GREATER_OR_EQUAL ;
2021-03-04 00:53:09 +00:00
for ( int i = 0 ; i < SDFGIShader : : PROBE_DEBUG_MAX ; i + + ) {
2022-05-20 02:52:19 +00:00
// TODO check if version is enabled
2021-02-13 12:08:08 +00:00
RID debug_probes_shader_version = sdfgi_shader . debug_probes . version_get_shader ( sdfgi_shader . debug_probes_shader , i ) ;
sdfgi_shader . debug_probes_pipeline [ i ] . setup ( debug_probes_shader_version , RD : : RENDER_PRIMITIVE_TRIANGLE_STRIPS , rs , RD : : PipelineMultisampleState ( ) , ds , RD : : PipelineColorBlendState : : create_disabled ( ) , 0 ) ;
}
}
}
2021-06-04 22:47:26 +00:00
default_voxel_gi_buffer = RD : : get_singleton ( ) - > uniform_buffer_create ( sizeof ( VoxelGIData ) * MAX_VOXEL_GI_INSTANCES ) ;
2021-03-23 10:46:31 +00:00
half_resolution = GLOBAL_GET ( " rendering/global_illumination/gi/use_half_resolution " ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
void GI : : free ( ) {
2023-05-22 02:59:34 +00:00
if ( default_voxel_gi_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( default_voxel_gi_buffer ) ;
}
if ( voxel_gi_lights_uniform . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( voxel_gi_lights_uniform ) ;
}
if ( sdfgi_ubo . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( sdfgi_ubo ) ;
}
if ( voxel_gi_debug_shader_version . is_valid ( ) ) {
voxel_gi_debug_shader . version_free ( voxel_gi_debug_shader_version ) ;
}
if ( voxel_gi_lighting_shader_version . is_valid ( ) ) {
voxel_gi_shader . version_free ( voxel_gi_lighting_shader_version ) ;
}
if ( shader_version . is_valid ( ) ) {
shader . version_free ( shader_version ) ;
}
if ( sdfgi_shader . debug_probes_shader . is_valid ( ) ) {
sdfgi_shader . debug_probes . version_free ( sdfgi_shader . debug_probes_shader ) ;
}
if ( sdfgi_shader . debug_shader . is_valid ( ) ) {
sdfgi_shader . debug . version_free ( sdfgi_shader . debug_shader ) ;
}
if ( sdfgi_shader . direct_light_shader . is_valid ( ) ) {
sdfgi_shader . direct_light . version_free ( sdfgi_shader . direct_light_shader ) ;
}
if ( sdfgi_shader . integrate_shader . is_valid ( ) ) {
sdfgi_shader . integrate . version_free ( sdfgi_shader . integrate_shader ) ;
}
if ( sdfgi_shader . preprocess_shader . is_valid ( ) ) {
sdfgi_shader . preprocess . version_free ( sdfgi_shader . preprocess_shader ) ;
}
2021-02-13 12:08:08 +00:00
2021-08-16 17:51:29 +00:00
if ( voxel_gi_lights ) {
memdelete_arr ( voxel_gi_lights ) ;
}
2021-02-13 12:08:08 +00:00
}
2022-08-04 08:40:39 +00:00
Ref < GI : : SDFGI > GI : : create_sdfgi ( RID p_env , const Vector3 & p_world_position , uint32_t p_requested_history_size ) {
Ref < SDFGI > sdfgi ;
sdfgi . instantiate ( ) ;
2021-02-13 12:08:08 +00:00
sdfgi - > create ( p_env , p_world_position , p_requested_history_size , this ) ;
return sdfgi ;
}
2022-09-12 09:44:48 +00:00
void GI : : setup_voxel_gi_instances ( RenderDataRD * p_render_data , Ref < RenderSceneBuffersRD > p_render_buffers , const Transform3D & p_transform , const PagedArray < RID > & p_voxel_gi_instances , uint32_t & r_voxel_gi_instances_used ) {
2022-08-04 08:40:39 +00:00
ERR_FAIL_COND ( p_render_buffers . is_null ( ) ) ;
2022-03-12 11:19:59 +00:00
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2022-08-04 08:40:39 +00:00
ERR_FAIL_NULL ( texture_storage ) ;
2022-03-12 11:19:59 +00:00
2021-06-04 22:47:26 +00:00
r_voxel_gi_instances_used = 0 ;
2021-02-13 12:08:08 +00:00
2022-08-04 08:40:39 +00:00
Ref < RenderBuffersGI > rbgi = p_render_buffers - > get_custom_data ( RB_SCOPE_GI ) ;
ERR_FAIL_COND ( rbgi . is_null ( ) ) ;
2021-02-13 12:08:08 +00:00
2022-08-04 08:40:39 +00:00
RID voxel_gi_buffer = rbgi - > get_voxel_gi_buffer ( ) ;
2021-06-04 22:47:26 +00:00
VoxelGIData voxel_gi_data [ MAX_VOXEL_GI_INSTANCES ] ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
bool voxel_gi_instances_changed = false ;
2021-02-13 12:08:08 +00:00
2020-10-17 05:08:21 +00:00
Transform3D to_camera ;
2021-02-13 12:08:08 +00:00
to_camera . origin = p_transform . origin ; //only translation, make local
2021-06-04 22:47:26 +00:00
for ( int i = 0 ; i < MAX_VOXEL_GI_INSTANCES ; i + + ) {
2021-02-13 12:08:08 +00:00
RID texture ;
2021-06-04 22:47:26 +00:00
if ( i < ( int ) p_voxel_gi_instances . size ( ) ) {
2022-08-04 08:40:39 +00:00
VoxelGIInstance * gipi = voxel_gi_instance_owner . get_or_null ( p_voxel_gi_instances [ i ] ) ;
2021-02-13 12:08:08 +00:00
if ( gipi ) {
texture = gipi - > texture ;
2021-06-04 22:47:26 +00:00
VoxelGIData & gipd = voxel_gi_data [ i ] ;
2021-02-13 12:08:08 +00:00
RID base_probe = gipi - > probe ;
2022-05-20 02:52:19 +00:00
Transform3D to_cell = voxel_gi_get_to_cell_xform ( gipi - > probe ) * gipi - > transform . affine_inverse ( ) * to_camera ;
2021-02-13 12:08:08 +00:00
2022-04-24 22:07:35 +00:00
gipd . xform [ 0 ] = to_cell . basis . rows [ 0 ] [ 0 ] ;
gipd . xform [ 1 ] = to_cell . basis . rows [ 1 ] [ 0 ] ;
gipd . xform [ 2 ] = to_cell . basis . rows [ 2 ] [ 0 ] ;
2021-02-13 12:08:08 +00:00
gipd . xform [ 3 ] = 0 ;
2022-04-24 22:07:35 +00:00
gipd . xform [ 4 ] = to_cell . basis . rows [ 0 ] [ 1 ] ;
gipd . xform [ 5 ] = to_cell . basis . rows [ 1 ] [ 1 ] ;
gipd . xform [ 6 ] = to_cell . basis . rows [ 2 ] [ 1 ] ;
2021-02-13 12:08:08 +00:00
gipd . xform [ 7 ] = 0 ;
2022-04-24 22:07:35 +00:00
gipd . xform [ 8 ] = to_cell . basis . rows [ 0 ] [ 2 ] ;
gipd . xform [ 9 ] = to_cell . basis . rows [ 1 ] [ 2 ] ;
gipd . xform [ 10 ] = to_cell . basis . rows [ 2 ] [ 2 ] ;
2021-02-13 12:08:08 +00:00
gipd . xform [ 11 ] = 0 ;
gipd . xform [ 12 ] = to_cell . origin . x ;
gipd . xform [ 13 ] = to_cell . origin . y ;
gipd . xform [ 14 ] = to_cell . origin . z ;
gipd . xform [ 15 ] = 1 ;
2022-05-20 02:52:19 +00:00
Vector3 bounds = voxel_gi_get_octree_size ( base_probe ) ;
2021-02-13 12:08:08 +00:00
gipd . bounds [ 0 ] = bounds . x ;
gipd . bounds [ 1 ] = bounds . y ;
gipd . bounds [ 2 ] = bounds . z ;
2022-05-20 02:52:19 +00:00
gipd . dynamic_range = voxel_gi_get_dynamic_range ( base_probe ) * voxel_gi_get_energy ( base_probe ) ;
gipd . bias = voxel_gi_get_bias ( base_probe ) ;
gipd . normal_bias = voxel_gi_get_normal_bias ( base_probe ) ;
gipd . blend_ambient = ! voxel_gi_is_interior ( base_probe ) ;
2021-02-13 12:08:08 +00:00
gipd . mipmaps = gipi - > mipmaps . size ( ) ;
2022-07-31 23:20:24 +00:00
gipd . exposure_normalization = 1.0 ;
if ( p_render_data - > camera_attributes . is_valid ( ) ) {
float exposure_normalization = RSG : : camera_attributes - > camera_attributes_get_exposure_normalization_factor ( p_render_data - > camera_attributes ) ;
gipd . exposure_normalization = exposure_normalization / voxel_gi_get_baked_exposure_normalization ( base_probe ) ;
}
2021-02-13 12:08:08 +00:00
}
2021-06-04 22:47:26 +00:00
r_voxel_gi_instances_used + + ;
2021-02-13 12:08:08 +00:00
}
if ( texture = = RID ( ) ) {
2022-08-01 01:59:14 +00:00
texture = texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ;
2021-02-13 12:08:08 +00:00
}
2022-08-04 08:40:39 +00:00
if ( texture ! = rbgi - > voxel_gi_textures [ i ] ) {
2021-06-04 22:47:26 +00:00
voxel_gi_instances_changed = true ;
2022-08-04 08:40:39 +00:00
rbgi - > voxel_gi_textures [ i ] = texture ;
2021-02-13 12:08:08 +00:00
}
}
2021-06-04 22:47:26 +00:00
if ( voxel_gi_instances_changed ) {
2022-05-20 02:52:19 +00:00
for ( uint32_t v = 0 ; v < RendererSceneRender : : MAX_RENDER_VIEWS ; v + + ) {
2022-08-04 08:40:39 +00:00
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( rbgi - > uniform_set [ v ] ) ) {
RD : : get_singleton ( ) - > free ( rbgi - > uniform_set [ v ] ) ;
2022-05-20 02:52:19 +00:00
}
2022-08-04 08:40:39 +00:00
rbgi - > uniform_set [ v ] = RID ( ) ;
2021-02-13 12:08:08 +00:00
}
2023-12-11 08:34:29 +00:00
if ( p_render_buffers - > has_custom_data ( RB_SCOPE_FOG ) ) {
// VoxelGI instances have changed, so we need to update volumetric fog.
Ref < RendererRD : : Fog : : VolumetricFog > fog = p_render_buffers - > get_custom_data ( RB_SCOPE_FOG ) ;
fog - > sync_gi_dependent_sets_validity ( true ) ;
}
2021-02-13 12:08:08 +00:00
}
2021-06-04 22:47:26 +00:00
if ( p_voxel_gi_instances . size ( ) > 0 ) {
2021-07-04 00:24:37 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " VoxelGIs Setup " ) ;
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > buffer_update ( voxel_gi_buffer , 0 , sizeof ( VoxelGIData ) * MIN ( ( uint64_t ) MAX_VOXEL_GI_INSTANCES , p_voxel_gi_instances . size ( ) ) , voxel_gi_data ) ;
2021-02-13 12:08:08 +00:00
2021-07-04 00:24:37 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
2021-02-13 12:08:08 +00:00
}
2022-08-04 08:40:39 +00:00
RID GI : : RenderBuffersGI : : get_voxel_gi_buffer ( ) {
if ( voxel_gi_buffer . is_null ( ) ) {
voxel_gi_buffer = RD : : get_singleton ( ) - > uniform_buffer_create ( sizeof ( GI : : VoxelGIData ) * GI : : MAX_VOXEL_GI_INSTANCES ) ;
}
return voxel_gi_buffer ;
}
void GI : : RenderBuffersGI : : free_data ( ) {
2022-05-20 02:52:19 +00:00
for ( uint32_t v = 0 ; v < RendererSceneRender : : MAX_RENDER_VIEWS ; v + + ) {
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( uniform_set [ v ] ) ) {
RD : : get_singleton ( ) - > free ( uniform_set [ v ] ) ;
}
uniform_set [ v ] = RID ( ) ;
}
if ( scene_data_ubo . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( scene_data_ubo ) ;
scene_data_ubo = RID ( ) ;
}
if ( voxel_gi_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( voxel_gi_buffer ) ;
voxel_gi_buffer = RID ( ) ;
}
}
2022-08-04 08:40:39 +00:00
void GI : : process_gi ( Ref < RenderSceneBuffersRD > p_render_buffers , const RID * p_normal_roughness_slices , RID p_voxel_gi_buffer , RID p_environment , uint32_t p_view_count , const Projection * p_projections , const Vector3 * p_eye_offsets , const Transform3D & p_cam_transform , const PagedArray < RID > & p_voxel_gi_instances ) {
2022-03-12 11:19:59 +00:00
RendererRD : : TextureStorage * texture_storage = RendererRD : : TextureStorage : : get_singleton ( ) ;
2022-04-12 11:41:50 +00:00
RendererRD : : MaterialStorage * material_storage = RendererRD : : MaterialStorage : : get_singleton ( ) ;
2022-03-12 11:19:59 +00:00
2022-05-20 02:52:19 +00:00
ERR_FAIL_COND_MSG ( p_view_count > 2 , " Maximum of 2 views supported for Processing GI. " ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_begin_label ( " GI Render " ) ;
2022-08-04 08:40:39 +00:00
ERR_FAIL_COND ( p_render_buffers . is_null ( ) ) ;
2021-02-13 12:08:08 +00:00
2022-08-04 08:40:39 +00:00
Ref < RenderBuffersGI > rbgi = p_render_buffers - > get_custom_data ( RB_SCOPE_GI ) ;
ERR_FAIL_COND ( rbgi . is_null ( ) ) ;
2022-02-11 11:33:54 +00:00
2022-08-04 08:40:39 +00:00
Size2i internal_size = p_render_buffers - > get_internal_size ( ) ;
if ( rbgi - > using_half_size_gi ! = half_resolution ) {
p_render_buffers - > clear_context ( RB_SCOPE_GI ) ;
}
2021-02-13 12:08:08 +00:00
2022-08-04 08:40:39 +00:00
if ( ! p_render_buffers - > has_texture ( RB_SCOPE_GI , RB_TEX_AMBIENT ) ) {
Size2i size = internal_size ;
uint32_t usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT ;
2022-05-20 02:52:19 +00:00
2021-02-13 12:08:08 +00:00
if ( half_resolution ) {
2022-08-04 08:40:39 +00:00
size . x > > = 1 ;
size . y > > = 1 ;
2021-02-13 12:08:08 +00:00
}
2022-08-04 08:40:39 +00:00
p_render_buffers - > create_texture ( RB_SCOPE_GI , RB_TEX_AMBIENT , RD : : DATA_FORMAT_R16G16B16A16_SFLOAT , usage_bits , RD : : TEXTURE_SAMPLES_1 , size ) ;
p_render_buffers - > create_texture ( RB_SCOPE_GI , RB_TEX_REFLECTION , RD : : DATA_FORMAT_R16G16B16A16_SFLOAT , usage_bits , RD : : TEXTURE_SAMPLES_1 , size ) ;
rbgi - > using_half_size_gi = half_resolution ;
2022-05-20 02:52:19 +00:00
}
// Setup our scene data
{
SceneData scene_data ;
2022-08-04 08:40:39 +00:00
if ( rbgi - > scene_data_ubo . is_null ( ) ) {
rbgi - > scene_data_ubo = RD : : get_singleton ( ) - > uniform_buffer_create ( sizeof ( SceneData ) ) ;
2022-05-20 02:52:19 +00:00
}
2024-02-14 13:39:39 +00:00
Projection correction ;
correction . set_depth_correction ( false ) ;
2022-05-20 02:52:19 +00:00
for ( uint32_t v = 0 ; v < p_view_count ; v + + ) {
2024-02-14 13:39:39 +00:00
Projection temp = correction * p_projections [ v ] ;
RendererRD : : MaterialStorage : : store_camera ( temp . inverse ( ) , scene_data . inv_projection [ v ] ) ;
2022-05-20 02:52:19 +00:00
scene_data . eye_offset [ v ] [ 0 ] = p_eye_offsets [ v ] . x ;
scene_data . eye_offset [ v ] [ 1 ] = p_eye_offsets [ v ] . y ;
scene_data . eye_offset [ v ] [ 2 ] = p_eye_offsets [ v ] . z ;
scene_data . eye_offset [ v ] [ 3 ] = 0.0 ;
}
// Note that we will be ignoring the origin of this transform.
2022-06-21 00:08:33 +00:00
RendererRD : : MaterialStorage : : store_transform ( p_cam_transform , scene_data . cam_transform ) ;
2022-05-20 02:52:19 +00:00
2022-08-04 08:40:39 +00:00
scene_data . screen_size [ 0 ] = internal_size . x ;
scene_data . screen_size [ 1 ] = internal_size . y ;
2022-05-20 02:52:19 +00:00
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > buffer_update ( rbgi - > scene_data_ubo , 0 , sizeof ( SceneData ) , & scene_data ) ;
2022-05-20 02:52:19 +00:00
}
// Now compute the contents of our buffers.
2023-11-24 11:23:22 +00:00
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
2022-05-20 02:52:19 +00:00
2022-07-21 11:37:00 +00:00
// Render each eye separately.
2022-02-11 11:33:54 +00:00
// We need to look into whether we can make our compute shader use Multiview but not sure that works or makes a difference..
2022-05-20 02:52:19 +00:00
2022-02-11 11:33:54 +00:00
// setup our push constant
2022-05-20 02:52:19 +00:00
2022-02-11 11:33:54 +00:00
PushConstant push_constant ;
2022-05-20 02:52:19 +00:00
2022-02-11 11:33:54 +00:00
push_constant . max_voxel_gi_instances = MIN ( ( uint64_t ) MAX_VOXEL_GI_INSTANCES , p_voxel_gi_instances . size ( ) ) ;
push_constant . high_quality_vct = voxel_gi_quality = = RS : : VOXEL_GI_QUALITY_HIGH ;
2022-05-20 02:52:19 +00:00
2022-02-11 11:33:54 +00:00
// these should be the same for all views
push_constant . orthogonal = p_projections [ 0 ] . is_orthogonal ( ) ;
push_constant . z_near = p_projections [ 0 ] . get_z_near ( ) ;
push_constant . z_far = p_projections [ 0 ] . get_z_far ( ) ;
2022-05-20 02:52:19 +00:00
2022-02-11 11:33:54 +00:00
// these are only used if we have 1 view, else we use the projections in our scene data
2022-10-04 16:44:48 +00:00
push_constant . proj_info [ 0 ] = - 2.0f / ( internal_size . x * p_projections [ 0 ] . columns [ 0 ] [ 0 ] ) ;
push_constant . proj_info [ 1 ] = - 2.0f / ( internal_size . y * p_projections [ 0 ] . columns [ 1 ] [ 1 ] ) ;
push_constant . proj_info [ 2 ] = ( 1.0f - p_projections [ 0 ] . columns [ 0 ] [ 2 ] ) / p_projections [ 0 ] . columns [ 0 ] [ 0 ] ;
push_constant . proj_info [ 3 ] = ( 1.0f + p_projections [ 0 ] . columns [ 1 ] [ 2 ] ) / p_projections [ 0 ] . columns [ 1 ] [ 1 ] ;
2022-05-20 02:52:19 +00:00
2022-08-04 08:40:39 +00:00
bool use_sdfgi = p_render_buffers - > has_custom_data ( RB_SCOPE_SDFGI ) ;
2022-02-11 11:33:54 +00:00
bool use_voxel_gi_instances = push_constant . max_voxel_gi_instances > 0 ;
2022-08-04 08:40:39 +00:00
Ref < SDFGI > sdfgi ;
if ( use_sdfgi ) {
sdfgi = p_render_buffers - > get_custom_data ( RB_SCOPE_SDFGI ) ;
}
2022-02-11 11:33:54 +00:00
uint32_t pipeline_specialization = 0 ;
2022-08-04 08:40:39 +00:00
if ( rbgi - > using_half_size_gi ) {
2022-02-11 11:33:54 +00:00
pipeline_specialization | = SHADER_SPECIALIZATION_HALF_RES ;
}
if ( p_view_count > 1 ) {
pipeline_specialization | = SHADER_SPECIALIZATION_USE_FULL_PROJECTION_MATRIX ;
}
2022-08-04 08:40:39 +00:00
bool has_vrs_texture = p_render_buffers - > has_texture ( RB_SCOPE_VRS , RB_TEXTURE ) ;
if ( has_vrs_texture ) {
2022-02-11 11:33:54 +00:00
pipeline_specialization | = SHADER_SPECIALIZATION_USE_VRS ;
}
Mode mode = ( use_sdfgi & & use_voxel_gi_instances ) ? MODE_COMBINED : ( use_sdfgi ? MODE_SDFGI : MODE_VOXEL_GI ) ;
for ( uint32_t v = 0 ; v < p_view_count ; v + + ) {
push_constant . view_index = v ;
2022-05-20 02:52:19 +00:00
// setup our uniform set
2022-08-04 08:40:39 +00:00
if ( rbgi - > uniform_set [ v ] . is_null ( ) | | ! RD : : get_singleton ( ) - > uniform_set_is_valid ( rbgi - > uniform_set [ v ] ) ) {
2022-05-20 02:52:19 +00:00
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 1 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
2022-08-04 08:40:39 +00:00
if ( use_sdfgi & & j < sdfgi - > cascades . size ( ) ) {
u . append_id ( sdfgi - > cascades [ j ] . sdf_tex ) ;
2022-05-20 02:52:19 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2022-05-20 02:52:19 +00:00
}
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
{
RD : : Uniform u ;
u . binding = 2 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
2022-08-04 08:40:39 +00:00
if ( use_sdfgi & & j < sdfgi - > cascades . size ( ) ) {
u . append_id ( sdfgi - > cascades [ j ] . light_tex ) ;
2022-05-20 02:52:19 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2022-05-20 02:52:19 +00:00
}
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 3 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
2022-08-04 08:40:39 +00:00
if ( use_sdfgi & & j < sdfgi - > cascades . size ( ) ) {
u . append_id ( sdfgi - > cascades [ j ] . light_aniso_0_tex ) ;
2022-05-20 02:52:19 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2022-05-20 02:52:19 +00:00
}
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 4 ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
for ( uint32_t j = 0 ; j < SDFGI : : MAX_CASCADES ; j + + ) {
2022-08-04 08:40:39 +00:00
if ( use_sdfgi & & j < sdfgi - > cascades . size ( ) ) {
u . append_id ( sdfgi - > cascades [ j ] . light_aniso_1_tex ) ;
2022-05-20 02:52:19 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2022-05-20 02:52:19 +00:00
}
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 5 ;
2022-08-04 08:40:39 +00:00
if ( use_sdfgi ) {
u . append_id ( sdfgi - > occlusion_texture ) ;
2021-02-13 12:08:08 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_3D_WHITE ) ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 6 ;
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
u . binding = 7 ;
u . append_id ( material_storage - > sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 9 ;
2022-08-04 08:40:39 +00:00
u . append_id ( p_render_buffers - > get_texture_slice ( RB_SCOPE_GI , RB_TEX_AMBIENT , v , 0 ) ) ;
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 10 ;
2022-08-04 08:40:39 +00:00
u . append_id ( p_render_buffers - > get_texture_slice ( RB_SCOPE_GI , RB_TEX_REFLECTION , v , 0 ) ) ;
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 11 ;
2022-08-04 08:40:39 +00:00
if ( use_sdfgi ) {
u . append_id ( sdfgi - > lightprobe_texture ) ;
2021-02-13 12:08:08 +00:00
} else {
2022-08-01 01:59:14 +00:00
u . append_id ( texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE ) ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
2021-02-13 12:08:08 +00:00
}
2022-05-20 02:52:19 +00:00
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 12 ;
2022-08-04 08:40:39 +00:00
u . append_id ( p_render_buffers - > get_depth_texture ( v ) ) ;
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 13 ;
2022-02-11 11:33:54 +00:00
u . append_id ( p_normal_roughness_slices [ v ] ) ;
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 14 ;
2022-08-01 01:59:14 +00:00
RID buffer = p_voxel_gi_buffer . is_valid ( ) ? p_voxel_gi_buffer : texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_BLACK ) ;
2022-05-20 02:52:19 +00:00
u . append_id ( buffer ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
u . binding = 15 ;
u . append_id ( sdfgi_ubo ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
u . binding = 16 ;
2022-08-04 08:40:39 +00:00
u . append_id ( rbgi - > get_voxel_gi_buffer ( ) ) ;
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
u . binding = 17 ;
for ( int i = 0 ; i < MAX_VOXEL_GI_INSTANCES ; i + + ) {
2022-08-04 08:40:39 +00:00
u . append_id ( rbgi - > voxel_gi_textures [ i ] ) ;
2022-05-20 02:52:19 +00:00
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
u . binding = 18 ;
2022-08-04 08:40:39 +00:00
u . append_id ( rbgi - > scene_data_ubo ) ;
2022-05-20 02:52:19 +00:00
uniforms . push_back ( u ) ;
2021-02-13 12:08:08 +00:00
}
2022-12-15 20:34:19 +00:00
if ( RendererSceneRenderRD : : get_singleton ( ) - > is_vrs_supported ( ) ) {
2022-02-11 11:33:54 +00:00
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
u . binding = 19 ;
2022-08-04 08:40:39 +00:00
RID buffer = has_vrs_texture ? p_render_buffers - > get_texture_slice ( RB_SCOPE_VRS , RB_TEXTURE , v , 0 ) : texture_storage - > texture_rd_get_default ( RendererRD : : TextureStorage : : DEFAULT_RD_TEXTURE_VRS ) ;
2022-02-11 11:33:54 +00:00
u . append_id ( buffer ) ;
uniforms . push_back ( u ) ;
}
2021-02-13 12:08:08 +00:00
2022-08-04 08:40:39 +00:00
rbgi - > uniform_set [ v ] = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , shader . version_get_shader ( shader_version , 0 ) , 0 ) ;
2021-02-13 12:08:08 +00:00
}
2022-02-11 11:33:54 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , pipelines [ pipeline_specialization ] [ mode ] ) ;
2022-08-04 08:40:39 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , rbgi - > uniform_set [ v ] , 0 ) ;
2022-05-20 02:52:19 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( PushConstant ) ) ;
2021-02-13 12:08:08 +00:00
2022-08-04 08:40:39 +00:00
if ( rbgi - > using_half_size_gi ) {
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , internal_size . x > > 1 , internal_size . y > > 1 , 1 ) ;
2022-05-20 02:52:19 +00:00
} else {
2022-08-04 08:40:39 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , internal_size . x , internal_size . y , 1 ) ;
2022-05-20 02:52:19 +00:00
}
2021-02-13 12:08:08 +00:00
}
2021-07-04 00:24:37 +00:00
2023-11-24 11:23:22 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
2021-02-13 12:08:08 +00:00
RD : : get_singleton ( ) - > draw_command_end_label ( ) ;
}
2022-05-20 02:52:19 +00:00
RID GI : : voxel_gi_instance_create ( RID p_base ) {
2021-06-04 22:47:26 +00:00
VoxelGIInstance voxel_gi ;
voxel_gi . gi = this ;
voxel_gi . probe = p_base ;
RID rid = voxel_gi_instance_owner . make_rid ( voxel_gi ) ;
2021-02-13 12:08:08 +00:00
return rid ;
}
2022-08-09 05:18:03 +00:00
void GI : : voxel_gi_instance_free ( RID p_rid ) {
GI : : VoxelGIInstance * voxel_gi = voxel_gi_instance_owner . get_or_null ( p_rid ) ;
voxel_gi - > free_resources ( ) ;
voxel_gi_instance_owner . free ( p_rid ) ;
}
2022-05-20 02:52:19 +00:00
void GI : : voxel_gi_instance_set_transform_to_data ( RID p_probe , const Transform3D & p_xform ) {
2022-08-04 08:40:39 +00:00
VoxelGIInstance * voxel_gi = voxel_gi_instance_owner . get_or_null ( p_probe ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2021-03-04 00:53:09 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > transform = p_xform ;
2021-03-04 00:53:09 +00:00
}
2022-05-20 02:52:19 +00:00
bool GI : : voxel_gi_needs_update ( RID p_probe ) const {
2022-08-04 08:40:39 +00:00
VoxelGIInstance * voxel_gi = voxel_gi_instance_owner . get_or_null ( p_probe ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL_V ( voxel_gi , false ) ;
2021-03-04 00:53:09 +00:00
2022-05-20 02:52:19 +00:00
return voxel_gi - > last_probe_version ! = voxel_gi_get_version ( voxel_gi - > probe ) ;
2021-03-04 00:53:09 +00:00
}
2022-09-12 09:44:48 +00:00
void GI : : voxel_gi_update ( RID p_probe , bool p_update_light_instances , const Vector < RID > & p_light_instances , const PagedArray < RenderGeometryInstance * > & p_dynamic_objects ) {
2022-08-04 08:40:39 +00:00
VoxelGIInstance * voxel_gi = voxel_gi_instance_owner . get_or_null ( p_probe ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2021-03-04 00:53:09 +00:00
2022-09-12 09:44:48 +00:00
voxel_gi - > update ( p_update_light_instances , p_light_instances , p_dynamic_objects ) ;
2021-03-04 00:53:09 +00:00
}
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
void GI : : debug_voxel_gi ( RID p_voxel_gi , RD : : DrawListID p_draw_list , RID p_framebuffer , const Projection & p_camera_with_transform , bool p_lighting , bool p_emission , float p_alpha ) {
2021-09-29 17:08:41 +00:00
VoxelGIInstance * voxel_gi = voxel_gi_instance_owner . get_or_null ( p_voxel_gi ) ;
2023-09-09 15:04:18 +00:00
ERR_FAIL_NULL ( voxel_gi ) ;
2021-02-13 12:08:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > debug ( p_draw_list , p_framebuffer , p_camera_with_transform , p_lighting , p_emission , p_alpha ) ;
2021-02-13 12:08:08 +00:00
}