2019-06-22 16:34:26 +00:00
/*************************************************************************/
2020-12-04 18:26:24 +00:00
/* renderer_storage_rd.cpp */
2019-06-22 16:34:26 +00:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
2021-01-01 19:13:46 +00:00
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2019-06-22 16:34:26 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2020-12-04 18:26:24 +00:00
# include "renderer_storage_rd.h"
2020-03-31 11:50:25 +00:00
2020-11-07 22:33:38 +00:00
# include "core/config/engine.h"
# include "core/config/project_settings.h"
2020-04-17 02:52:00 +00:00
# include "core/io/resource_loader.h"
2021-05-19 12:12:55 +00:00
# include "core/math/math_defs.h"
2020-12-04 18:26:24 +00:00
# include "renderer_compositor_rd.h"
2020-03-27 18:21:27 +00:00
# include "servers/rendering/shader_language.h"
2019-06-11 18:43:37 +00:00
2021-02-09 16:19:03 +00:00
bool RendererStorageRD : : can_create_resources_async ( ) const {
return true ;
}
2020-12-04 18:26:24 +00:00
Ref < Image > RendererStorageRD : : _validate_texture_format ( const Ref < Image > & p_image , TextureToRDFormat & r_format ) {
2019-06-16 02:45:24 +00:00
Ref < Image > image = p_image - > duplicate ( ) ;
switch ( p_image - > get_format ( ) ) {
case Image : : FORMAT_L8 : {
r_format . format = RD : : DATA_FORMAT_R8_UNORM ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //luminance
case Image : : FORMAT_LA8 : {
r_format . format = RD : : DATA_FORMAT_R8G8_UNORM ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_G ;
} break ; //luminance-alpha
case Image : : FORMAT_R8 : {
r_format . format = RD : : DATA_FORMAT_R8_UNORM ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_RG8 : {
r_format . format = RD : : DATA_FORMAT_R8G8_UNORM ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_RGB8 : {
//this format is not mandatory for specification, check if supported first
2019-06-19 20:03:19 +00:00
if ( false & & RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_R8G8B8_UNORM , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) & & RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_R8G8B8_SRGB , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
2019-06-16 02:45:24 +00:00
r_format . format = RD : : DATA_FORMAT_R8G8B8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8_SRGB ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_RGBA8 : {
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
case Image : : FORMAT_RGBA4444 : {
r_format . format = RD : : DATA_FORMAT_B4G4R4A4_UNORM_PACK16 ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_B ; //needs swizzle
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
2019-09-27 02:16:44 +00:00
case Image : : FORMAT_RGB565 : {
r_format . format = RD : : DATA_FORMAT_B5G6R5_UNORM_PACK16 ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_B ;
2019-06-16 02:45:24 +00:00
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
2019-09-27 02:16:44 +00:00
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_R ;
2019-06-16 02:45:24 +00:00
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
case Image : : FORMAT_RF : {
r_format . format = RD : : DATA_FORMAT_R32_SFLOAT ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //float
case Image : : FORMAT_RGF : {
r_format . format = RD : : DATA_FORMAT_R32G32_SFLOAT ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_RGBF : {
//this format is not mandatory for specification, check if supported first
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_R32G32B32_SFLOAT , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_R32G32B32_SFLOAT ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R32G32B32A32_SFLOAT ;
image - > convert ( Image : : FORMAT_RGBAF ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_RGBAF : {
r_format . format = RD : : DATA_FORMAT_R32G32B32A32_SFLOAT ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
case Image : : FORMAT_RH : {
r_format . format = RD : : DATA_FORMAT_R16_SFLOAT ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //half float
case Image : : FORMAT_RGH : {
r_format . format = RD : : DATA_FORMAT_R16G16_SFLOAT ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_RGBH : {
//this format is not mandatory for specification, check if supported first
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_R16G16B16_SFLOAT , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_R16G16B16_SFLOAT ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
image - > convert ( Image : : FORMAT_RGBAH ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_RGBAH : {
r_format . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
case Image : : FORMAT_RGBE9995 : {
r_format . format = RD : : DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32 ;
2019-06-22 16:34:26 +00:00
# ifndef _MSC_VER
2019-06-16 02:45:24 +00:00
# warning TODO need to make a function in Image to swap bits for this
2019-06-22 16:34:26 +00:00
# endif
2019-06-16 02:45:24 +00:00
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_IDENTITY ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_IDENTITY ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_IDENTITY ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_IDENTITY ;
} break ;
case Image : : FORMAT_DXT1 : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC1_RGB_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC1_RGB_UNORM_BLOCK ;
2019-09-14 03:37:42 +00:00
r_format . format_srgb = RD : : DATA_FORMAT_BC1_RGB_SRGB_BLOCK ;
2019-06-16 02:45:24 +00:00
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //s3tc bc1
case Image : : FORMAT_DXT3 : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC2_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC2_UNORM_BLOCK ;
r_format . format_srgb = RD : : DATA_FORMAT_BC2_SRGB_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ; //bc2
case Image : : FORMAT_DXT5 : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC3_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC3_UNORM_BLOCK ;
r_format . format_srgb = RD : : DATA_FORMAT_BC3_SRGB_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ; //bc3
case Image : : FORMAT_RGTC_R : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC4_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC4_UNORM_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8_UNORM ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_R8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_RGTC_RG : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC5_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC5_UNORM_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8_UNORM ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RG8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_BPTC_RGBA : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC7_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC7_UNORM_BLOCK ;
r_format . format_srgb = RD : : DATA_FORMAT_BC7_SRGB_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ; //btpc bc7
case Image : : FORMAT_BPTC_RGBF : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC6H_SFLOAT_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC6H_SFLOAT_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBAH ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //float bc6h
case Image : : FORMAT_BPTC_RGBFU : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC6H_UFLOAT_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC6H_UFLOAT_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBAH ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //unsigned float bc6hu
2020-12-10 22:01:00 +00:00
case Image : : FORMAT_PVRTC1_2 : {
2019-06-16 02:45:24 +00:00
//this is not properly supported by MoltekVK it seems, so best to use ETC2
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG ;
r_format . format_srgb = RD : : DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //pvrtc
2020-12-10 22:01:00 +00:00
case Image : : FORMAT_PVRTC1_2A : {
2019-06-16 02:45:24 +00:00
//this is not properly supported by MoltekVK it seems, so best to use ETC2
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG ;
r_format . format_srgb = RD : : DATA_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
2020-12-10 22:01:00 +00:00
case Image : : FORMAT_PVRTC1_4 : {
2019-06-16 02:45:24 +00:00
//this is not properly supported by MoltekVK it seems, so best to use ETC2
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG ;
r_format . format_srgb = RD : : DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
2020-12-10 22:01:00 +00:00
case Image : : FORMAT_PVRTC1_4A : {
2019-06-16 02:45:24 +00:00
//this is not properly supported by MoltekVK it seems, so best to use ETC2
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG ;
r_format . format_srgb = RD : : DATA_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
case Image : : FORMAT_ETC2_R11 : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_EAC_R11_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_EAC_R11_UNORM_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8_UNORM ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_R8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //etc2
case Image : : FORMAT_ETC2_R11S : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_EAC_R11_SNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_EAC_R11_SNORM_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8_SNORM ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_R8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ; //signed: {} break; NOT srgb.
case Image : : FORMAT_ETC2_RG11 : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_EAC_R11G11_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_EAC_R11G11_UNORM_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8_UNORM ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RG8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_ETC2_RG11S : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_EAC_R11G11_SNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_EAC_R11G11_SNORM_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8_SNORM ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RG8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_ETC :
case Image : : FORMAT_ETC2_RGB8 : {
//ETC2 is backwards compatible with ETC1, and all modern platforms support it
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK ;
r_format . format_srgb = RD : : DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_ETC2_RGBA8 : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK ;
r_format . format_srgb = RD : : DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
case Image : : FORMAT_ETC2_RGB8A1 : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK ;
r_format . format_srgb = RD : : DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_G ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_B ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_A ;
} break ;
2019-09-27 02:16:44 +00:00
case Image : : FORMAT_ETC2_RA_AS_RG : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK ;
r_format . format_srgb = RD : : DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_A ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
case Image : : FORMAT_DXT5_RA_AS_RG : {
if ( RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC3_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ) ) {
r_format . format = RD : : DATA_FORMAT_BC3_UNORM_BLOCK ;
r_format . format_srgb = RD : : DATA_FORMAT_BC3_SRGB_BLOCK ;
} else {
//not supported, reconvert
r_format . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
r_format . format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
image - > decompress ( ) ;
image - > convert ( Image : : FORMAT_RGBA8 ) ;
}
r_format . swizzle_r = RD : : TEXTURE_SWIZZLE_R ;
r_format . swizzle_g = RD : : TEXTURE_SWIZZLE_A ;
r_format . swizzle_b = RD : : TEXTURE_SWIZZLE_ZERO ;
r_format . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
} break ;
2019-06-16 02:45:24 +00:00
default : {
}
}
return image ;
}
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : texture_allocate ( ) {
return texture_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : texture_2d_initialize ( RID p_texture , const Ref < Image > & p_image ) {
ERR_FAIL_COND ( p_image . is_null ( ) ) ;
ERR_FAIL_COND ( p_image - > is_empty ( ) ) ;
2019-06-11 18:43:37 +00:00
2019-06-16 02:45:24 +00:00
TextureToRDFormat ret_format ;
Ref < Image > image = _validate_texture_format ( p_image , ret_format ) ;
Texture texture ;
texture . type = Texture : : TYPE_2D ;
texture . width = p_image - > get_width ( ) ;
texture . height = p_image - > get_height ( ) ;
texture . layers = 1 ;
2019-06-19 20:03:19 +00:00
texture . mipmaps = p_image - > get_mipmap_count ( ) + 1 ;
2019-06-16 02:45:24 +00:00
texture . depth = 1 ;
2019-06-24 19:13:06 +00:00
texture . format = p_image - > get_format ( ) ;
2019-06-25 22:49:52 +00:00
texture . validated_format = image - > get_format ( ) ;
2019-06-16 02:45:24 +00:00
texture . rd_type = RD : : TEXTURE_TYPE_2D ;
texture . rd_format = ret_format . format ;
texture . rd_format_srgb = ret_format . format_srgb ;
RD : : TextureFormat rd_format ;
RD : : TextureView rd_view ;
{ //attempt register
rd_format . format = texture . rd_format ;
rd_format . width = texture . width ;
rd_format . height = texture . height ;
rd_format . depth = 1 ;
rd_format . array_layers = 1 ;
rd_format . mipmaps = texture . mipmaps ;
2020-11-27 03:50:05 +00:00
rd_format . texture_type = texture . rd_type ;
2019-06-16 02:45:24 +00:00
rd_format . samples = RD : : TEXTURE_SAMPLES_1 ;
2019-07-27 13:23:24 +00:00
rd_format . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
2019-06-19 20:03:19 +00:00
if ( texture . rd_format_srgb ! = RD : : DATA_FORMAT_MAX ) {
rd_format . shareable_formats . push_back ( texture . rd_format ) ;
rd_format . shareable_formats . push_back ( texture . rd_format_srgb ) ;
}
2019-06-16 02:45:24 +00:00
}
{
rd_view . swizzle_r = ret_format . swizzle_r ;
rd_view . swizzle_g = ret_format . swizzle_g ;
rd_view . swizzle_b = ret_format . swizzle_b ;
rd_view . swizzle_a = ret_format . swizzle_a ;
}
2020-02-17 21:06:54 +00:00
Vector < uint8_t > data = image - > get_data ( ) ; //use image data
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > data_slices ;
2019-06-16 02:45:24 +00:00
data_slices . push_back ( data ) ;
texture . rd_texture = RD : : get_singleton ( ) - > texture_create ( rd_format , rd_view , data_slices ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( texture . rd_texture . is_null ( ) ) ;
2019-06-16 02:45:24 +00:00
if ( texture . rd_format_srgb ! = RD : : DATA_FORMAT_MAX ) {
rd_view . format_override = texture . rd_format_srgb ;
texture . rd_texture_srgb = RD : : get_singleton ( ) - > texture_create_shared ( rd_view , texture . rd_texture ) ;
if ( texture . rd_texture_srgb . is_null ( ) ) {
RD : : get_singleton ( ) - > free ( texture . rd_texture ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( texture . rd_texture_srgb . is_null ( ) ) ;
2019-06-16 02:45:24 +00:00
}
}
//used for 2D, overridable
texture . width_2d = texture . width ;
texture . height_2d = texture . height ;
texture . is_render_target = false ;
2019-06-24 19:13:06 +00:00
texture . rd_view = rd_view ;
texture . is_proxy = false ;
2019-07-29 21:19:31 +00:00
2021-02-09 16:19:03 +00:00
texture_owner . initialize_rid ( p_texture , texture ) ;
2019-06-11 18:43:37 +00:00
}
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : texture_2d_layered_initialize ( RID p_texture , const Vector < Ref < Image > > & p_layers , RS : : TextureLayeredType p_layered_type ) {
ERR_FAIL_COND ( p_layers . size ( ) = = 0 ) ;
2020-05-01 12:34:23 +00:00
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( p_layered_type = = RS : : TEXTURE_LAYERED_CUBEMAP & & p_layers . size ( ) ! = 6 ) ;
ERR_FAIL_COND ( p_layered_type = = RS : : TEXTURE_LAYERED_CUBEMAP_ARRAY & & ( p_layers . size ( ) < 6 | | ( p_layers . size ( ) % 6 ) ! = 0 ) ) ;
2020-05-01 12:34:23 +00:00
TextureToRDFormat ret_format ;
Vector < Ref < Image > > images ;
{
int valid_width = 0 ;
int valid_height = 0 ;
bool valid_mipmaps = false ;
Image : : Format valid_format = Image : : FORMAT_MAX ;
for ( int i = 0 ; i < p_layers . size ( ) ; i + + ) {
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( p_layers [ i ] - > is_empty ( ) ) ;
2020-05-01 12:34:23 +00:00
if ( i = = 0 ) {
valid_width = p_layers [ i ] - > get_width ( ) ;
valid_height = p_layers [ i ] - > get_height ( ) ;
valid_format = p_layers [ i ] - > get_format ( ) ;
valid_mipmaps = p_layers [ i ] - > has_mipmaps ( ) ;
} else {
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( p_layers [ i ] - > get_width ( ) ! = valid_width ) ;
ERR_FAIL_COND ( p_layers [ i ] - > get_height ( ) ! = valid_height ) ;
ERR_FAIL_COND ( p_layers [ i ] - > get_format ( ) ! = valid_format ) ;
ERR_FAIL_COND ( p_layers [ i ] - > has_mipmaps ( ) ! = valid_mipmaps ) ;
2020-05-01 12:34:23 +00:00
}
images . push_back ( _validate_texture_format ( p_layers [ i ] , ret_format ) ) ;
}
}
Texture texture ;
texture . type = Texture : : TYPE_LAYERED ;
texture . layered_type = p_layered_type ;
texture . width = p_layers [ 0 ] - > get_width ( ) ;
texture . height = p_layers [ 0 ] - > get_height ( ) ;
texture . layers = p_layers . size ( ) ;
texture . mipmaps = p_layers [ 0 ] - > get_mipmap_count ( ) + 1 ;
texture . depth = 1 ;
texture . format = p_layers [ 0 ] - > get_format ( ) ;
texture . validated_format = images [ 0 ] - > get_format ( ) ;
switch ( p_layered_type ) {
case RS : : TEXTURE_LAYERED_2D_ARRAY : {
texture . rd_type = RD : : TEXTURE_TYPE_2D_ARRAY ;
} break ;
case RS : : TEXTURE_LAYERED_CUBEMAP : {
texture . rd_type = RD : : TEXTURE_TYPE_CUBE ;
} break ;
case RS : : TEXTURE_LAYERED_CUBEMAP_ARRAY : {
texture . rd_type = RD : : TEXTURE_TYPE_CUBE_ARRAY ;
} break ;
}
texture . rd_format = ret_format . format ;
texture . rd_format_srgb = ret_format . format_srgb ;
RD : : TextureFormat rd_format ;
RD : : TextureView rd_view ;
{ //attempt register
rd_format . format = texture . rd_format ;
rd_format . width = texture . width ;
rd_format . height = texture . height ;
rd_format . depth = 1 ;
rd_format . array_layers = texture . layers ;
rd_format . mipmaps = texture . mipmaps ;
2020-11-27 03:50:05 +00:00
rd_format . texture_type = texture . rd_type ;
2020-05-01 12:34:23 +00:00
rd_format . samples = RD : : TEXTURE_SAMPLES_1 ;
rd_format . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
if ( texture . rd_format_srgb ! = RD : : DATA_FORMAT_MAX ) {
rd_format . shareable_formats . push_back ( texture . rd_format ) ;
rd_format . shareable_formats . push_back ( texture . rd_format_srgb ) ;
}
}
{
rd_view . swizzle_r = ret_format . swizzle_r ;
rd_view . swizzle_g = ret_format . swizzle_g ;
rd_view . swizzle_b = ret_format . swizzle_b ;
rd_view . swizzle_a = ret_format . swizzle_a ;
}
Vector < Vector < uint8_t > > data_slices ;
for ( int i = 0 ; i < images . size ( ) ; i + + ) {
Vector < uint8_t > data = images [ i ] - > get_data ( ) ; //use image data
data_slices . push_back ( data ) ;
}
texture . rd_texture = RD : : get_singleton ( ) - > texture_create ( rd_format , rd_view , data_slices ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( texture . rd_texture . is_null ( ) ) ;
2020-05-01 12:34:23 +00:00
if ( texture . rd_format_srgb ! = RD : : DATA_FORMAT_MAX ) {
rd_view . format_override = texture . rd_format_srgb ;
texture . rd_texture_srgb = RD : : get_singleton ( ) - > texture_create_shared ( rd_view , texture . rd_texture ) ;
if ( texture . rd_texture_srgb . is_null ( ) ) {
RD : : get_singleton ( ) - > free ( texture . rd_texture ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( texture . rd_texture_srgb . is_null ( ) ) ;
2020-05-01 12:34:23 +00:00
}
}
//used for 2D, overridable
texture . width_2d = texture . width ;
texture . height_2d = texture . height ;
texture . is_render_target = false ;
texture . rd_view = rd_view ;
texture . is_proxy = false ;
2021-02-09 16:19:03 +00:00
texture_owner . initialize_rid ( p_texture , texture ) ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : texture_3d_initialize ( RID p_texture , Image : : Format p_format , int p_width , int p_height , int p_depth , bool p_mipmaps , const Vector < Ref < Image > > & p_data ) {
ERR_FAIL_COND ( p_data . size ( ) = = 0 ) ;
2020-09-09 15:40:51 +00:00
Image : : Image3DValidateError verr = Image : : validate_3d_image ( p_format , p_width , p_height , p_depth , p_mipmaps , p_data ) ;
if ( verr ! = Image : : VALIDATE_3D_OK ) {
2021-02-09 16:19:03 +00:00
ERR_FAIL_MSG ( Image : : get_3d_image_validation_error_text ( verr ) ) ;
2020-09-09 15:40:51 +00:00
}
TextureToRDFormat ret_format ;
Image : : Format validated_format = Image : : FORMAT_MAX ;
Vector < uint8_t > all_data ;
uint32_t mipmap_count = 0 ;
Vector < Texture : : BufferSlice3D > slices ;
{
Vector < Ref < Image > > images ;
uint32_t all_data_size = 0 ;
images . resize ( p_data . size ( ) ) ;
for ( int i = 0 ; i < p_data . size ( ) ; i + + ) {
TextureToRDFormat f ;
images . write [ i ] = _validate_texture_format ( p_data [ i ] , f ) ;
if ( i = = 0 ) {
ret_format = f ;
validated_format = images [ 0 ] - > get_format ( ) ;
}
all_data_size + = images [ i ] - > get_data ( ) . size ( ) ;
}
all_data . resize ( all_data_size ) ; //consolidate all data here
uint32_t offset = 0 ;
Size2i prev_size ;
for ( int i = 0 ; i < p_data . size ( ) ; i + + ) {
uint32_t s = images [ i ] - > get_data ( ) . size ( ) ;
2021-04-27 14:19:21 +00:00
memcpy ( & all_data . write [ offset ] , images [ i ] - > get_data ( ) . ptr ( ) , s ) ;
2020-09-09 15:40:51 +00:00
{
Texture : : BufferSlice3D slice ;
slice . size . width = images [ i ] - > get_width ( ) ;
slice . size . height = images [ i ] - > get_height ( ) ;
slice . offset = offset ;
slice . buffer_size = s ;
slices . push_back ( slice ) ;
}
offset + = s ;
Size2i img_size ( images [ i ] - > get_width ( ) , images [ i ] - > get_height ( ) ) ;
if ( img_size ! = prev_size ) {
mipmap_count + + ;
}
prev_size = img_size ;
}
}
Texture texture ;
texture . type = Texture : : TYPE_3D ;
texture . width = p_width ;
texture . height = p_height ;
texture . depth = p_depth ;
texture . mipmaps = mipmap_count ;
texture . format = p_data [ 0 ] - > get_format ( ) ;
texture . validated_format = validated_format ;
texture . buffer_size_3d = all_data . size ( ) ;
texture . buffer_slices_3d = slices ;
texture . rd_type = RD : : TEXTURE_TYPE_3D ;
texture . rd_format = ret_format . format ;
texture . rd_format_srgb = ret_format . format_srgb ;
RD : : TextureFormat rd_format ;
RD : : TextureView rd_view ;
{ //attempt register
rd_format . format = texture . rd_format ;
rd_format . width = texture . width ;
rd_format . height = texture . height ;
rd_format . depth = texture . depth ;
rd_format . array_layers = 1 ;
rd_format . mipmaps = texture . mipmaps ;
2020-11-27 03:50:05 +00:00
rd_format . texture_type = texture . rd_type ;
2020-09-09 15:40:51 +00:00
rd_format . samples = RD : : TEXTURE_SAMPLES_1 ;
rd_format . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
if ( texture . rd_format_srgb ! = RD : : DATA_FORMAT_MAX ) {
rd_format . shareable_formats . push_back ( texture . rd_format ) ;
rd_format . shareable_formats . push_back ( texture . rd_format_srgb ) ;
}
}
{
rd_view . swizzle_r = ret_format . swizzle_r ;
rd_view . swizzle_g = ret_format . swizzle_g ;
rd_view . swizzle_b = ret_format . swizzle_b ;
rd_view . swizzle_a = ret_format . swizzle_a ;
}
Vector < Vector < uint8_t > > data_slices ;
data_slices . push_back ( all_data ) ; //one slice
texture . rd_texture = RD : : get_singleton ( ) - > texture_create ( rd_format , rd_view , data_slices ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( texture . rd_texture . is_null ( ) ) ;
2020-09-09 15:40:51 +00:00
if ( texture . rd_format_srgb ! = RD : : DATA_FORMAT_MAX ) {
rd_view . format_override = texture . rd_format_srgb ;
texture . rd_texture_srgb = RD : : get_singleton ( ) - > texture_create_shared ( rd_view , texture . rd_texture ) ;
if ( texture . rd_texture_srgb . is_null ( ) ) {
RD : : get_singleton ( ) - > free ( texture . rd_texture ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( texture . rd_texture_srgb . is_null ( ) ) ;
2020-09-09 15:40:51 +00:00
}
}
//used for 2D, overridable
texture . width_2d = texture . width ;
texture . height_2d = texture . height ;
texture . is_render_target = false ;
texture . rd_view = rd_view ;
texture . is_proxy = false ;
2021-02-09 16:19:03 +00:00
texture_owner . initialize_rid ( p_texture , texture ) ;
2019-06-11 18:43:37 +00:00
}
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : texture_proxy_initialize ( RID p_texture , RID p_base ) {
2019-06-24 19:13:06 +00:00
Texture * tex = texture_owner . getornull ( p_base ) ;
2021-02-09 16:19:03 +00:00
ERR_FAIL_COND ( ! tex ) ;
2019-06-24 19:13:06 +00:00
Texture proxy_tex = * tex ;
proxy_tex . rd_view . format_override = tex - > rd_format ;
proxy_tex . rd_texture = RD : : get_singleton ( ) - > texture_create_shared ( proxy_tex . rd_view , tex - > rd_texture ) ;
if ( proxy_tex . rd_texture_srgb . is_valid ( ) ) {
proxy_tex . rd_view . format_override = tex - > rd_format_srgb ;
proxy_tex . rd_texture_srgb = RD : : get_singleton ( ) - > texture_create_shared ( proxy_tex . rd_view , tex - > rd_texture ) ;
}
proxy_tex . proxy_to = p_base ;
proxy_tex . is_render_target = false ;
proxy_tex . is_proxy = true ;
proxy_tex . proxies . clear ( ) ;
2021-02-09 16:19:03 +00:00
texture_owner . initialize_rid ( p_texture , proxy_tex ) ;
2019-07-29 21:19:31 +00:00
2021-02-09 16:19:03 +00:00
tex - > proxies . push_back ( p_texture ) ;
2019-06-24 19:13:06 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _texture_2d_update ( RID p_texture , const Ref < Image > & p_image , int p_layer , bool p_immediate ) {
2020-12-15 12:04:21 +00:00
ERR_FAIL_COND ( p_image . is_null ( ) | | p_image - > is_empty ( ) ) ;
2019-06-16 02:45:24 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
ERR_FAIL_COND ( tex - > is_render_target ) ;
ERR_FAIL_COND ( p_image - > get_width ( ) ! = tex - > width | | p_image - > get_height ( ) ! = tex - > height ) ;
ERR_FAIL_COND ( p_image - > get_format ( ) ! = tex - > format ) ;
if ( tex - > type = = Texture : : TYPE_LAYERED ) {
ERR_FAIL_INDEX ( p_layer , tex - > layers ) ;
}
2019-06-25 22:49:52 +00:00
# ifdef TOOLS_ENABLED
tex - > image_cache_2d . unref ( ) ;
# endif
2019-06-16 02:45:24 +00:00
TextureToRDFormat f ;
Ref < Image > validated = _validate_texture_format ( p_image , f ) ;
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > texture_update ( tex - > rd_texture , p_layer , validated - > get_data ( ) ) ;
2019-06-16 02:45:24 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_2d_update_immediate ( RID p_texture , const Ref < Image > & p_image , int p_layer ) {
2019-06-16 02:45:24 +00:00
_texture_2d_update ( p_texture , p_image , p_layer , true ) ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_2d_update ( RID p_texture , const Ref < Image > & p_image , int p_layer ) {
2019-06-16 02:45:24 +00:00
_texture_2d_update ( p_texture , p_image , p_layer , false ) ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_3d_update ( RID p_texture , const Vector < Ref < Image > > & p_data ) {
2020-09-09 15:40:51 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
ERR_FAIL_COND ( tex - > type ! = Texture : : TYPE_3D ) ;
Image : : Image3DValidateError verr = Image : : validate_3d_image ( tex - > format , tex - > width , tex - > height , tex - > depth , tex - > mipmaps > 1 , p_data ) ;
if ( verr ! = Image : : VALIDATE_3D_OK ) {
ERR_FAIL_MSG ( Image : : get_3d_image_validation_error_text ( verr ) ) ;
}
Vector < uint8_t > all_data ;
{
Vector < Ref < Image > > images ;
uint32_t all_data_size = 0 ;
images . resize ( p_data . size ( ) ) ;
for ( int i = 0 ; i < p_data . size ( ) ; i + + ) {
Ref < Image > image = p_data [ i ] ;
if ( image - > get_format ( ) ! = tex - > validated_format ) {
image = image - > duplicate ( ) ;
image - > convert ( tex - > validated_format ) ;
}
all_data_size + = images [ i ] - > get_data ( ) . size ( ) ;
images . push_back ( image ) ;
}
all_data . resize ( all_data_size ) ; //consolidate all data here
uint32_t offset = 0 ;
for ( int i = 0 ; i < p_data . size ( ) ; i + + ) {
uint32_t s = images [ i ] - > get_data ( ) . size ( ) ;
2021-04-27 14:19:21 +00:00
memcpy ( & all_data . write [ offset ] , images [ i ] - > get_data ( ) . ptr ( ) , s ) ;
2020-09-09 15:40:51 +00:00
offset + = s ;
}
}
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > texture_update ( tex - > rd_texture , 0 , all_data ) ;
2019-06-11 18:43:37 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_proxy_update ( RID p_texture , RID p_proxy_to ) {
2019-06-24 19:13:06 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
ERR_FAIL_COND ( ! tex - > is_proxy ) ;
Texture * proxy_to = texture_owner . getornull ( p_proxy_to ) ;
ERR_FAIL_COND ( ! proxy_to ) ;
ERR_FAIL_COND ( proxy_to - > is_proxy ) ;
if ( tex - > proxy_to . is_valid ( ) ) {
//unlink proxy
if ( RD : : get_singleton ( ) - > texture_is_valid ( tex - > rd_texture ) ) {
RD : : get_singleton ( ) - > free ( tex - > rd_texture ) ;
tex - > rd_texture = RID ( ) ;
}
if ( RD : : get_singleton ( ) - > texture_is_valid ( tex - > rd_texture_srgb ) ) {
RD : : get_singleton ( ) - > free ( tex - > rd_texture_srgb ) ;
tex - > rd_texture_srgb = RID ( ) ;
}
Texture * prev_tex = texture_owner . getornull ( tex - > proxy_to ) ;
ERR_FAIL_COND ( ! prev_tex ) ;
prev_tex - > proxies . erase ( p_texture ) ;
}
* tex = * proxy_to ;
tex - > proxy_to = p_proxy_to ;
tex - > is_render_target = false ;
tex - > is_proxy = true ;
tex - > proxies . clear ( ) ;
proxy_to - > proxies . push_back ( p_texture ) ;
tex - > rd_view . format_override = tex - > rd_format ;
tex - > rd_texture = RD : : get_singleton ( ) - > texture_create_shared ( tex - > rd_view , proxy_to - > rd_texture ) ;
if ( tex - > rd_texture_srgb . is_valid ( ) ) {
tex - > rd_view . format_override = tex - > rd_format_srgb ;
tex - > rd_texture_srgb = RD : : get_singleton ( ) - > texture_create_shared ( tex - > rd_view , proxy_to - > rd_texture ) ;
}
}
2019-06-11 18:43:37 +00:00
//these two APIs can be used together or in combination with the others.
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : texture_2d_placeholder_initialize ( RID p_texture ) {
2019-06-16 02:45:24 +00:00
//this could be better optimized to reuse an existing image , done this way
//for now to get it working
Ref < Image > image ;
2021-06-17 22:03:09 +00:00
image . instantiate ( ) ;
2019-06-16 02:45:24 +00:00
image - > create ( 4 , 4 , false , Image : : FORMAT_RGBA8 ) ;
2020-02-17 21:06:54 +00:00
2019-06-16 02:45:24 +00:00
for ( int i = 0 ; i < 4 ; i + + ) {
for ( int j = 0 ; j < 4 ; j + + ) {
image - > set_pixel ( i , j , Color ( 1 , 0 , 1 , 1 ) ) ;
}
}
2021-02-09 16:19:03 +00:00
texture_2d_initialize ( p_texture , image ) ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : texture_2d_layered_placeholder_initialize ( RID p_texture , RS : : TextureLayeredType p_layered_type ) {
2020-05-01 12:34:23 +00:00
//this could be better optimized to reuse an existing image , done this way
//for now to get it working
Ref < Image > image ;
2021-06-17 22:03:09 +00:00
image . instantiate ( ) ;
2020-05-01 12:34:23 +00:00
image - > create ( 4 , 4 , false , Image : : FORMAT_RGBA8 ) ;
for ( int i = 0 ; i < 4 ; i + + ) {
for ( int j = 0 ; j < 4 ; j + + ) {
image - > set_pixel ( i , j , Color ( 1 , 0 , 1 , 1 ) ) ;
}
}
Vector < Ref < Image > > images ;
if ( p_layered_type = = RS : : TEXTURE_LAYERED_2D_ARRAY ) {
images . push_back ( image ) ;
} else {
//cube
for ( int i = 0 ; i < 6 ; i + + ) {
images . push_back ( image ) ;
}
}
2021-02-09 16:19:03 +00:00
texture_2d_layered_initialize ( p_texture , images , p_layered_type ) ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : texture_3d_placeholder_initialize ( RID p_texture ) {
2020-09-09 15:40:51 +00:00
//this could be better optimized to reuse an existing image , done this way
//for now to get it working
Ref < Image > image ;
2021-06-17 22:03:09 +00:00
image . instantiate ( ) ;
2020-09-09 15:40:51 +00:00
image - > create ( 4 , 4 , false , Image : : FORMAT_RGBA8 ) ;
for ( int i = 0 ; i < 4 ; i + + ) {
for ( int j = 0 ; j < 4 ; j + + ) {
image - > set_pixel ( i , j , Color ( 1 , 0 , 1 , 1 ) ) ;
}
}
Vector < Ref < Image > > images ;
//cube
for ( int i = 0 ; i < 4 ; i + + ) {
images . push_back ( image ) ;
}
2021-02-09 16:19:03 +00:00
texture_3d_initialize ( p_texture , Image : : FORMAT_RGBA8 , 4 , 4 , 4 , false , images ) ;
2019-06-11 18:43:37 +00:00
}
2020-12-04 18:26:24 +00:00
Ref < Image > RendererStorageRD : : texture_2d_get ( RID p_texture ) const {
2019-06-16 02:45:24 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND_V ( ! tex , Ref < Image > ( ) ) ;
2019-06-25 22:49:52 +00:00
# ifdef TOOLS_ENABLED
if ( tex - > image_cache_2d . is_valid ( ) ) {
return tex - > image_cache_2d ;
}
# endif
2020-02-17 21:06:54 +00:00
Vector < uint8_t > data = RD : : get_singleton ( ) - > texture_get_data ( tex - > rd_texture , 0 ) ;
2019-06-25 22:49:52 +00:00
ERR_FAIL_COND_V ( data . size ( ) = = 0 , Ref < Image > ( ) ) ;
Ref < Image > image ;
2021-06-17 22:03:09 +00:00
image . instantiate ( ) ;
2019-06-25 22:49:52 +00:00
image - > create ( tex - > width , tex - > height , tex - > mipmaps > 1 , tex - > validated_format , data ) ;
2020-12-15 12:04:21 +00:00
ERR_FAIL_COND_V ( image - > is_empty ( ) , Ref < Image > ( ) ) ;
2019-06-25 22:49:52 +00:00
if ( tex - > format ! = tex - > validated_format ) {
image - > convert ( tex - > format ) ;
}
# ifdef TOOLS_ENABLED
if ( Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
tex - > image_cache_2d = image ;
}
# endif
return image ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Ref < Image > RendererStorageRD : : texture_2d_layer_get ( RID p_texture , int p_layer ) const {
2020-09-09 15:40:51 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND_V ( ! tex , Ref < Image > ( ) ) ;
Vector < uint8_t > data = RD : : get_singleton ( ) - > texture_get_data ( tex - > rd_texture , p_layer ) ;
ERR_FAIL_COND_V ( data . size ( ) = = 0 , Ref < Image > ( ) ) ;
Ref < Image > image ;
2021-06-17 22:03:09 +00:00
image . instantiate ( ) ;
2020-09-09 15:40:51 +00:00
image - > create ( tex - > width , tex - > height , tex - > mipmaps > 1 , tex - > validated_format , data ) ;
2020-12-15 12:04:21 +00:00
ERR_FAIL_COND_V ( image - > is_empty ( ) , Ref < Image > ( ) ) ;
2020-09-09 15:40:51 +00:00
if ( tex - > format ! = tex - > validated_format ) {
image - > convert ( tex - > format ) ;
}
return image ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Vector < Ref < Image > > RendererStorageRD : : texture_3d_get ( RID p_texture ) const {
2020-09-09 15:40:51 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND_V ( ! tex , Vector < Ref < Image > > ( ) ) ;
ERR_FAIL_COND_V ( tex - > type ! = Texture : : TYPE_3D , Vector < Ref < Image > > ( ) ) ;
Vector < uint8_t > all_data = RD : : get_singleton ( ) - > texture_get_data ( tex - > rd_texture , 0 ) ;
ERR_FAIL_COND_V ( all_data . size ( ) ! = ( int ) tex - > buffer_size_3d , Vector < Ref < Image > > ( ) ) ;
Vector < Ref < Image > > ret ;
for ( int i = 0 ; i < tex - > buffer_slices_3d . size ( ) ; i + + ) {
const Texture : : BufferSlice3D & bs = tex - > buffer_slices_3d [ i ] ;
ERR_FAIL_COND_V ( bs . offset > = ( uint32_t ) all_data . size ( ) , Vector < Ref < Image > > ( ) ) ;
ERR_FAIL_COND_V ( bs . offset + bs . buffer_size > ( uint32_t ) all_data . size ( ) , Vector < Ref < Image > > ( ) ) ;
Vector < uint8_t > sub_region = all_data . subarray ( bs . offset , bs . offset + bs . buffer_size - 1 ) ;
Ref < Image > img ;
2021-06-17 22:03:09 +00:00
img . instantiate ( ) ;
2020-09-09 15:40:51 +00:00
img - > create ( bs . size . width , bs . size . height , false , tex - > validated_format , sub_region ) ;
2020-12-15 12:04:21 +00:00
ERR_FAIL_COND_V ( img - > is_empty ( ) , Vector < Ref < Image > > ( ) ) ;
2020-09-09 15:40:51 +00:00
if ( tex - > format ! = tex - > validated_format ) {
img - > convert ( tex - > format ) ;
}
ret . push_back ( img ) ;
}
return ret ;
2019-06-11 18:43:37 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_replace ( RID p_texture , RID p_by_texture ) {
2019-06-16 02:45:24 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
2020-07-16 16:54:15 +00:00
ERR_FAIL_COND ( tex - > proxy_to . is_valid ( ) ) ; //can't replace proxy
2019-06-16 02:45:24 +00:00
Texture * by_tex = texture_owner . getornull ( p_by_texture ) ;
ERR_FAIL_COND ( ! by_tex ) ;
2020-07-16 16:54:15 +00:00
ERR_FAIL_COND ( by_tex - > proxy_to . is_valid ( ) ) ; //can't replace proxy
2019-06-16 02:45:24 +00:00
if ( tex = = by_tex ) {
return ;
}
if ( tex - > rd_texture_srgb . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( tex - > rd_texture_srgb ) ;
}
2019-06-24 19:13:06 +00:00
RD : : get_singleton ( ) - > free ( tex - > rd_texture ) ;
2020-10-24 15:15:43 +00:00
if ( tex - > canvas_texture ) {
memdelete ( tex - > canvas_texture ) ;
tex - > canvas_texture = nullptr ;
}
2019-06-24 19:13:06 +00:00
Vector < RID > proxies_to_update = tex - > proxies ;
Vector < RID > proxies_to_redirect = by_tex - > proxies ;
2019-06-16 02:45:24 +00:00
* tex = * by_tex ;
2019-06-24 19:13:06 +00:00
tex - > proxies = proxies_to_update ; //restore proxies, so they can be updated
2020-10-24 15:15:43 +00:00
if ( tex - > canvas_texture ) {
tex - > canvas_texture - > diffuse = p_texture ; //update
}
2019-06-24 19:13:06 +00:00
for ( int i = 0 ; i < proxies_to_update . size ( ) ; i + + ) {
texture_proxy_update ( proxies_to_update [ i ] , p_texture ) ;
}
for ( int i = 0 ; i < proxies_to_redirect . size ( ) ; i + + ) {
texture_proxy_update ( proxies_to_redirect [ i ] , p_texture ) ;
}
//delete last, so proxies can be updated
2019-06-16 02:45:24 +00:00
texture_owner . free ( p_by_texture ) ;
2020-04-14 03:05:21 +00:00
if ( decal_atlas . textures . has ( p_texture ) ) {
//belongs to decal atlas..
decal_atlas . dirty = true ; //mark it dirty since it was most likely modified
}
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_set_size_override ( RID p_texture , int p_width , int p_height ) {
2019-06-16 02:45:24 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
ERR_FAIL_COND ( tex - > type ! = Texture : : TYPE_2D ) ;
tex - > width_2d = p_width ;
2019-06-24 19:13:06 +00:00
tex - > height_2d = p_height ;
2019-06-11 18:43:37 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_set_path ( RID p_texture , const String & p_path ) {
2019-09-14 03:37:42 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
tex - > path = p_path ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
String RendererStorageRD : : texture_get_path ( RID p_texture ) const {
2019-06-11 18:43:37 +00:00
return String ( ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_set_detect_3d_callback ( RID p_texture , RS : : TextureDetectCallback p_callback , void * p_userdata ) {
2019-09-14 03:37:42 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
tex - > detect_3d_callback_ud = p_userdata ;
tex - > detect_3d_callback = p_callback ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_set_detect_normal_callback ( RID p_texture , RS : : TextureDetectCallback p_callback , void * p_userdata ) {
2019-09-14 03:37:42 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
tex - > detect_normal_callback_ud = p_userdata ;
tex - > detect_normal_callback = p_callback ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_set_detect_roughness_callback ( RID p_texture , RS : : TextureDetectRoughnessCallback p_callback , void * p_userdata ) {
2019-09-14 03:37:42 +00:00
Texture * tex = texture_owner . getornull ( p_texture ) ;
ERR_FAIL_COND ( ! tex ) ;
tex - > detect_roughness_callback_ud = p_userdata ;
tex - > detect_roughness_callback = p_callback ;
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_debug_usage ( List < RS : : TextureInfo > * r_info ) {
2019-06-11 18:43:37 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_set_proxy ( RID p_proxy , RID p_base ) {
2019-06-11 18:43:37 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_set_force_redraw_if_visible ( RID p_texture , bool p_enable ) {
2019-06-11 18:43:37 +00:00
}
2020-12-04 18:26:24 +00:00
Size2 RendererStorageRD : : texture_size_with_proxy ( RID p_proxy ) {
2019-06-24 19:13:06 +00:00
return texture_2d_get_size ( p_proxy ) ;
2019-06-11 18:43:37 +00:00
}
2019-06-16 02:45:24 +00:00
2020-10-24 15:15:43 +00:00
/* CANVAS TEXTURE */
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : CanvasTexture : : clear_sets ( ) {
2020-10-24 15:15:43 +00:00
if ( cleared_cache ) {
return ;
}
for ( int i = 1 ; i < RS : : CANVAS_ITEM_TEXTURE_FILTER_MAX ; i + + ) {
for ( int j = 1 ; j < RS : : CANVAS_ITEM_TEXTURE_REPEAT_MAX ; j + + ) {
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( uniform_sets [ i ] [ j ] ) ) {
RD : : get_singleton ( ) - > free ( uniform_sets [ i ] [ j ] ) ;
uniform_sets [ i ] [ j ] = RID ( ) ;
}
}
}
cleared_cache = true ;
}
2020-12-04 18:26:24 +00:00
RendererStorageRD : : CanvasTexture : : ~ CanvasTexture ( ) {
2020-10-24 15:15:43 +00:00
clear_sets ( ) ;
}
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : canvas_texture_allocate ( ) {
return canvas_texture_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : canvas_texture_initialize ( RID p_rid ) {
canvas_texture_owner . initialize_rid ( p_rid , memnew ( CanvasTexture ) ) ;
2020-10-24 15:15:43 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : canvas_texture_set_channel ( RID p_canvas_texture , RS : : CanvasTextureChannel p_channel , RID p_texture ) {
2020-10-24 15:15:43 +00:00
CanvasTexture * ct = canvas_texture_owner . getornull ( p_canvas_texture ) ;
switch ( p_channel ) {
case RS : : CANVAS_TEXTURE_CHANNEL_DIFFUSE : {
ct - > diffuse = p_texture ;
} break ;
case RS : : CANVAS_TEXTURE_CHANNEL_NORMAL : {
2020-12-23 09:34:26 +00:00
ct - > normal_map = p_texture ;
2020-10-24 15:15:43 +00:00
} break ;
case RS : : CANVAS_TEXTURE_CHANNEL_SPECULAR : {
ct - > specular = p_texture ;
} break ;
}
ct - > clear_sets ( ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : canvas_texture_set_shading_parameters ( RID p_canvas_texture , const Color & p_specular_color , float p_shininess ) {
2020-10-24 15:15:43 +00:00
CanvasTexture * ct = canvas_texture_owner . getornull ( p_canvas_texture ) ;
ct - > specular_color . r = p_specular_color . r ;
ct - > specular_color . g = p_specular_color . g ;
ct - > specular_color . b = p_specular_color . b ;
ct - > specular_color . a = p_shininess ;
ct - > clear_sets ( ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : canvas_texture_set_texture_filter ( RID p_canvas_texture , RS : : CanvasItemTextureFilter p_filter ) {
2020-10-24 15:15:43 +00:00
CanvasTexture * ct = canvas_texture_owner . getornull ( p_canvas_texture ) ;
ct - > texture_filter = p_filter ;
ct - > clear_sets ( ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : canvas_texture_set_texture_repeat ( RID p_canvas_texture , RS : : CanvasItemTextureRepeat p_repeat ) {
2020-10-24 15:15:43 +00:00
CanvasTexture * ct = canvas_texture_owner . getornull ( p_canvas_texture ) ;
ct - > texture_repeat = p_repeat ;
ct - > clear_sets ( ) ;
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : canvas_texture_get_uniform_set ( RID p_texture , RS : : CanvasItemTextureFilter p_base_filter , RS : : CanvasItemTextureRepeat p_base_repeat , RID p_base_shader , int p_base_set , RID & r_uniform_set , Size2i & r_size , Color & r_specular_shininess , bool & r_use_normal , bool & r_use_specular ) {
2020-10-24 15:15:43 +00:00
CanvasTexture * ct = nullptr ;
Texture * t = texture_owner . getornull ( p_texture ) ;
if ( t ) {
//regular texture
if ( ! t - > canvas_texture ) {
t - > canvas_texture = memnew ( CanvasTexture ) ;
t - > canvas_texture - > diffuse = p_texture ;
}
ct = t - > canvas_texture ;
} else {
ct = canvas_texture_owner . getornull ( p_texture ) ;
}
if ( ! ct ) {
return false ; //invalid texture RID
}
RS : : CanvasItemTextureFilter filter = ct - > texture_filter ! = RS : : CANVAS_ITEM_TEXTURE_FILTER_DEFAULT ? ct - > texture_filter : p_base_filter ;
ERR_FAIL_COND_V ( filter = = RS : : CANVAS_ITEM_TEXTURE_FILTER_DEFAULT , false ) ;
RS : : CanvasItemTextureRepeat repeat = ct - > texture_repeat ! = RS : : CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT ? ct - > texture_repeat : p_base_repeat ;
ERR_FAIL_COND_V ( repeat = = RS : : CANVAS_ITEM_TEXTURE_REPEAT_DEFAULT , false ) ;
RID uniform_set = ct - > uniform_sets [ filter ] [ repeat ] ;
if ( ! RD : : get_singleton ( ) - > uniform_set_is_valid ( uniform_set ) ) {
//create and update
Vector < RD : : Uniform > uniforms ;
{ //diffuse
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2020-10-24 15:15:43 +00:00
u . binding = 0 ;
t = texture_owner . getornull ( ct - > diffuse ) ;
if ( ! t ) {
u . ids . push_back ( texture_rd_get_default ( DEFAULT_RD_TEXTURE_WHITE ) ) ;
ct - > size_cache = Size2i ( 1 , 1 ) ;
} else {
u . ids . push_back ( t - > rd_texture ) ;
ct - > size_cache = Size2i ( t - > width_2d , t - > height_2d ) ;
}
uniforms . push_back ( u ) ;
}
{ //normal
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2020-10-24 15:15:43 +00:00
u . binding = 1 ;
2020-12-23 09:34:26 +00:00
t = texture_owner . getornull ( ct - > normal_map ) ;
2020-10-24 15:15:43 +00:00
if ( ! t ) {
u . ids . push_back ( texture_rd_get_default ( DEFAULT_RD_TEXTURE_NORMAL ) ) ;
ct - > use_normal_cache = false ;
} else {
u . ids . push_back ( t - > rd_texture ) ;
ct - > use_normal_cache = true ;
}
uniforms . push_back ( u ) ;
}
{ //specular
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2020-10-24 15:15:43 +00:00
u . binding = 2 ;
t = texture_owner . getornull ( ct - > specular ) ;
if ( ! t ) {
u . ids . push_back ( texture_rd_get_default ( DEFAULT_RD_TEXTURE_WHITE ) ) ;
ct - > use_specular_cache = false ;
} else {
u . ids . push_back ( t - > rd_texture ) ;
ct - > use_specular_cache = true ;
}
uniforms . push_back ( u ) ;
}
{ //sampler
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
2020-10-24 15:15:43 +00:00
u . binding = 3 ;
u . ids . push_back ( sampler_rd_get_default ( filter , repeat ) ) ;
uniforms . push_back ( u ) ;
}
uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , p_base_shader , p_base_set ) ;
ct - > uniform_sets [ filter ] [ repeat ] = uniform_set ;
ct - > cleared_cache = false ;
}
r_uniform_set = uniform_set ;
r_size = ct - > size_cache ;
r_specular_shininess = ct - > specular_color ;
r_use_normal = ct - > use_normal_cache ;
r_use_specular = ct - > use_specular_cache ;
return true ;
}
2019-07-21 14:31:30 +00:00
/* SHADER API */
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : shader_allocate ( ) {
return shader_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : shader_initialize ( RID p_rid ) {
2019-07-21 14:31:30 +00:00
Shader shader ;
2020-04-01 23:20:12 +00:00
shader . data = nullptr ;
2019-07-21 14:31:30 +00:00
shader . type = SHADER_TYPE_MAX ;
2021-02-09 16:19:03 +00:00
shader_owner . initialize_rid ( p_rid , shader ) ;
2019-07-21 14:31:30 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : shader_set_code ( RID p_shader , const String & p_code ) {
2019-07-21 14:31:30 +00:00
Shader * shader = shader_owner . getornull ( p_shader ) ;
ERR_FAIL_COND ( ! shader ) ;
shader - > code = p_code ;
String mode_string = ShaderLanguage : : get_shader_type ( p_code ) ;
ShaderType new_type ;
2020-05-14 14:41:43 +00:00
if ( mode_string = = " canvas_item " ) {
2019-07-21 14:31:30 +00:00
new_type = SHADER_TYPE_2D ;
2020-05-14 14:41:43 +00:00
} else if ( mode_string = = " particles " ) {
2019-07-21 14:31:30 +00:00
new_type = SHADER_TYPE_PARTICLES ;
2020-05-14 14:41:43 +00:00
} else if ( mode_string = = " spatial " ) {
2019-07-21 14:31:30 +00:00
new_type = SHADER_TYPE_3D ;
2020-05-14 14:41:43 +00:00
} else if ( mode_string = = " sky " ) {
2019-09-15 09:58:38 +00:00
new_type = SHADER_TYPE_SKY ;
2020-05-14 14:41:43 +00:00
} else {
2019-07-21 14:31:30 +00:00
new_type = SHADER_TYPE_MAX ;
2020-05-14 14:41:43 +00:00
}
2019-07-21 14:31:30 +00:00
if ( new_type ! = shader - > type ) {
if ( shader - > data ) {
memdelete ( shader - > data ) ;
2020-04-01 23:20:12 +00:00
shader - > data = nullptr ;
2019-07-21 14:31:30 +00:00
}
for ( Set < Material * > : : Element * E = shader - > owners . front ( ) ; E ; E = E - > next ( ) ) {
Material * material = E - > get ( ) ;
material - > shader_type = new_type ;
if ( material - > data ) {
memdelete ( material - > data ) ;
2020-04-01 23:20:12 +00:00
material - > data = nullptr ;
2019-07-21 14:31:30 +00:00
}
}
shader - > type = new_type ;
if ( new_type < SHADER_TYPE_MAX & & shader_data_request_func [ new_type ] ) {
shader - > data = shader_data_request_func [ new_type ] ( ) ;
} else {
shader - > type = SHADER_TYPE_MAX ; //invalid
}
for ( Set < Material * > : : Element * E = shader - > owners . front ( ) ; E ; E = E - > next ( ) ) {
Material * material = E - > get ( ) ;
if ( shader - > data ) {
material - > data = material_data_request_func [ new_type ] ( shader - > data ) ;
2020-04-17 02:52:00 +00:00
material - > data - > self = material - > self ;
2019-07-21 14:31:30 +00:00
material - > data - > set_next_pass ( material - > next_pass ) ;
material - > data - > set_render_priority ( material - > priority ) ;
}
material - > shader_type = new_type ;
}
2020-09-14 08:48:53 +00:00
2021-06-14 05:00:35 +00:00
if ( shader - > data ) {
for ( Map < StringName , RID > : : Element * E = shader - > default_texture_parameter . front ( ) ; E ; E = E - > next ( ) ) {
shader - > data - > set_default_texture_param ( E - > key ( ) , E - > get ( ) ) ;
}
2020-09-14 08:48:53 +00:00
}
2019-07-21 14:31:30 +00:00
}
if ( shader - > data ) {
shader - > data - > set_code ( p_code ) ;
}
for ( Set < Material * > : : Element * E = shader - > owners . front ( ) ; E ; E = E - > next ( ) ) {
Material * material = E - > get ( ) ;
2021-01-04 12:33:25 +00:00
material - > dependency . changed_notify ( DEPENDENCY_CHANGED_MATERIAL ) ;
2019-07-21 14:31:30 +00:00
_material_queue_update ( material , true , true ) ;
}
}
2020-12-04 18:26:24 +00:00
String RendererStorageRD : : shader_get_code ( RID p_shader ) const {
2019-07-21 14:31:30 +00:00
Shader * shader = shader_owner . getornull ( p_shader ) ;
ERR_FAIL_COND_V ( ! shader , String ( ) ) ;
return shader - > code ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : shader_get_param_list ( RID p_shader , List < PropertyInfo > * p_param_list ) const {
2019-07-21 14:31:30 +00:00
Shader * shader = shader_owner . getornull ( p_shader ) ;
ERR_FAIL_COND ( ! shader ) ;
if ( shader - > data ) {
return shader - > data - > get_param_list ( p_param_list ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : shader_set_default_texture_param ( RID p_shader , const StringName & p_name , RID p_texture ) {
2019-07-21 14:31:30 +00:00
Shader * shader = shader_owner . getornull ( p_shader ) ;
ERR_FAIL_COND ( ! shader ) ;
if ( p_texture . is_valid ( ) & & texture_owner . owns ( p_texture ) ) {
shader - > default_texture_parameter [ p_name ] = p_texture ;
} else {
shader - > default_texture_parameter . erase ( p_name ) ;
}
2020-09-14 08:48:53 +00:00
if ( shader - > data ) {
shader - > data - > set_default_texture_param ( p_name , p_texture ) ;
}
2019-07-21 14:31:30 +00:00
for ( Set < Material * > : : Element * E = shader - > owners . front ( ) ; E ; E = E - > next ( ) ) {
Material * material = E - > get ( ) ;
_material_queue_update ( material , false , true ) ;
}
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : shader_get_default_texture_param ( RID p_shader , const StringName & p_name ) const {
2019-07-21 14:31:30 +00:00
Shader * shader = shader_owner . getornull ( p_shader ) ;
ERR_FAIL_COND_V ( ! shader , RID ( ) ) ;
if ( shader - > default_texture_parameter . has ( p_name ) ) {
return shader - > default_texture_parameter [ p_name ] ;
}
return RID ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Variant RendererStorageRD : : shader_get_param_default ( RID p_shader , const StringName & p_param ) const {
2019-07-21 14:31:30 +00:00
Shader * shader = shader_owner . getornull ( p_shader ) ;
ERR_FAIL_COND_V ( ! shader , Variant ( ) ) ;
if ( shader - > data ) {
return shader - > data - > get_default_parameter ( p_param ) ;
}
return Variant ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : shader_set_data_request_function ( ShaderType p_shader_type , ShaderDataRequestFunction p_function ) {
2019-07-21 14:31:30 +00:00
ERR_FAIL_INDEX ( p_shader_type , SHADER_TYPE_MAX ) ;
shader_data_request_func [ p_shader_type ] = p_function ;
}
2021-01-05 23:01:50 +00:00
RS : : ShaderNativeSourceCode RendererStorageRD : : shader_get_native_source_code ( RID p_shader ) const {
Shader * shader = shader_owner . getornull ( p_shader ) ;
ERR_FAIL_COND_V ( ! shader , RS : : ShaderNativeSourceCode ( ) ) ;
if ( shader - > data ) {
return shader - > data - > get_native_source_code ( ) ;
}
return RS : : ShaderNativeSourceCode ( ) ;
}
2019-07-21 14:31:30 +00:00
/* COMMON MATERIAL API */
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : material_allocate ( ) {
return material_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : material_initialize ( RID p_rid ) {
2019-07-21 14:31:30 +00:00
Material material ;
2020-04-01 23:20:12 +00:00
material . data = nullptr ;
material . shader = nullptr ;
2019-07-21 14:31:30 +00:00
material . shader_type = SHADER_TYPE_MAX ;
2020-04-01 23:20:12 +00:00
material . update_next = nullptr ;
2019-07-21 14:31:30 +00:00
material . update_requested = false ;
material . uniform_dirty = false ;
material . texture_dirty = false ;
material . priority = 0 ;
2021-02-09 16:19:03 +00:00
material . self = p_rid ;
material_owner . initialize_rid ( p_rid , material ) ;
2019-07-21 14:31:30 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _material_queue_update ( Material * material , bool p_uniform , bool p_texture ) {
2019-07-21 14:31:30 +00:00
if ( material - > update_requested ) {
return ;
}
material - > update_next = material_update_list ;
material_update_list = material ;
material - > update_requested = true ;
2020-04-17 02:52:00 +00:00
material - > uniform_dirty = material - > uniform_dirty | | p_uniform ;
material - > texture_dirty = material - > texture_dirty | | p_texture ;
2019-07-21 14:31:30 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : material_set_shader ( RID p_material , RID p_shader ) {
2019-07-21 14:31:30 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND ( ! material ) ;
if ( material - > data ) {
memdelete ( material - > data ) ;
2020-04-01 23:20:12 +00:00
material - > data = nullptr ;
2019-07-21 14:31:30 +00:00
}
if ( material - > shader ) {
material - > shader - > owners . erase ( material ) ;
2020-04-01 23:20:12 +00:00
material - > shader = nullptr ;
2019-07-21 14:31:30 +00:00
material - > shader_type = SHADER_TYPE_MAX ;
}
if ( p_shader . is_null ( ) ) {
2021-01-04 12:33:25 +00:00
material - > dependency . changed_notify ( DEPENDENCY_CHANGED_MATERIAL ) ;
material - > shader_id = 0 ;
2019-07-21 14:31:30 +00:00
return ;
}
Shader * shader = shader_owner . getornull ( p_shader ) ;
ERR_FAIL_COND ( ! shader ) ;
material - > shader = shader ;
material - > shader_type = shader - > type ;
2021-01-04 12:33:25 +00:00
material - > shader_id = p_shader . get_local_index ( ) ;
2019-07-21 14:31:30 +00:00
shader - > owners . insert ( material ) ;
if ( shader - > type = = SHADER_TYPE_MAX ) {
return ;
}
2020-04-01 23:20:12 +00:00
ERR_FAIL_COND ( shader - > data = = nullptr ) ;
2019-07-21 14:31:30 +00:00
material - > data = material_data_request_func [ shader - > type ] ( shader - > data ) ;
2020-04-17 02:52:00 +00:00
material - > data - > self = p_material ;
2019-07-21 14:31:30 +00:00
material - > data - > set_next_pass ( material - > next_pass ) ;
material - > data - > set_render_priority ( material - > priority ) ;
//updating happens later
2021-01-04 12:33:25 +00:00
material - > dependency . changed_notify ( DEPENDENCY_CHANGED_MATERIAL ) ;
2019-07-21 14:31:30 +00:00
_material_queue_update ( material , true , true ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : material_set_param ( RID p_material , const StringName & p_param , const Variant & p_value ) {
2019-07-21 14:31:30 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND ( ! material ) ;
if ( p_value . get_type ( ) = = Variant : : NIL ) {
material - > params . erase ( p_param ) ;
} else {
material - > params [ p_param ] = p_value ;
}
if ( material - > shader & & material - > shader - > data ) { //shader is valid
bool is_texture = material - > shader - > data - > is_param_texture ( p_param ) ;
_material_queue_update ( material , ! is_texture , is_texture ) ;
} else {
_material_queue_update ( material , true , true ) ;
}
}
2020-12-04 18:26:24 +00:00
Variant RendererStorageRD : : material_get_param ( RID p_material , const StringName & p_param ) const {
2019-07-21 14:31:30 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND_V ( ! material , Variant ( ) ) ;
if ( material - > params . has ( p_param ) ) {
return material - > params [ p_param ] ;
} else {
return Variant ( ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : material_set_next_pass ( RID p_material , RID p_next_material ) {
2019-07-21 14:31:30 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND ( ! material ) ;
if ( material - > next_pass = = p_next_material ) {
return ;
}
material - > next_pass = p_next_material ;
if ( material - > data ) {
material - > data - > set_next_pass ( p_next_material ) ;
}
2021-01-04 12:33:25 +00:00
material - > dependency . changed_notify ( DEPENDENCY_CHANGED_MATERIAL ) ;
2019-07-21 14:31:30 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : material_set_render_priority ( RID p_material , int priority ) {
2019-07-21 14:31:30 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND ( ! material ) ;
material - > priority = priority ;
if ( material - > data ) {
material - > data - > set_render_priority ( priority ) ;
}
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : material_is_animated ( RID p_material ) {
2019-07-21 14:31:30 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND_V ( ! material , false ) ;
if ( material - > shader & & material - > shader - > data ) {
if ( material - > shader - > data - > is_animated ( ) ) {
return true ;
} else if ( material - > next_pass . is_valid ( ) ) {
return material_is_animated ( material - > next_pass ) ;
}
}
return false ; //by default nothing is animated
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : material_casts_shadows ( RID p_material ) {
2019-07-21 14:31:30 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND_V ( ! material , true ) ;
if ( material - > shader & & material - > shader - > data ) {
if ( material - > shader - > data - > casts_shadows ( ) ) {
return true ;
} else if ( material - > next_pass . is_valid ( ) ) {
return material_casts_shadows ( material - > next_pass ) ;
}
}
return true ; //by default everything casts shadows
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : material_get_instance_shader_parameters ( RID p_material , List < InstanceShaderParam > * r_parameters ) {
2020-04-17 02:52:00 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND ( ! material ) ;
if ( material - > shader & & material - > shader - > data ) {
material - > shader - > data - > get_instance_param_list ( r_parameters ) ;
if ( material - > next_pass . is_valid ( ) ) {
material_get_instance_shader_parameters ( material - > next_pass , r_parameters ) ;
}
}
}
2021-01-04 12:33:25 +00:00
void RendererStorageRD : : material_update_dependency ( RID p_material , DependencyTracker * p_instance ) {
2019-07-21 14:31:30 +00:00
Material * material = material_owner . getornull ( p_material ) ;
ERR_FAIL_COND ( ! material ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & material - > dependency ) ;
2019-07-21 14:31:30 +00:00
if ( material - > next_pass . is_valid ( ) ) {
material_update_dependency ( material - > next_pass , p_instance ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : material_set_data_request_function ( ShaderType p_shader_type , MaterialDataRequestFunction p_function ) {
2019-07-21 14:31:30 +00:00
ERR_FAIL_INDEX ( p_shader_type , SHADER_TYPE_MAX ) ;
material_data_request_func [ p_shader_type ] = p_function ;
}
_FORCE_INLINE_ static void _fill_std140_variant_ubo_value ( ShaderLanguage : : DataType type , const Variant & value , uint8_t * data , bool p_linear_color ) {
switch ( type ) {
case ShaderLanguage : : TYPE_BOOL : {
bool v = value ;
uint32_t * gui = ( uint32_t * ) data ;
* gui = v ? 1 : 0 ;
} break ;
case ShaderLanguage : : TYPE_BVEC2 : {
int v = value ;
uint32_t * gui = ( uint32_t * ) data ;
gui [ 0 ] = v & 1 ? 1 : 0 ;
gui [ 1 ] = v & 2 ? 1 : 0 ;
} break ;
case ShaderLanguage : : TYPE_BVEC3 : {
int v = value ;
uint32_t * gui = ( uint32_t * ) data ;
gui [ 0 ] = ( v & 1 ) ? 1 : 0 ;
gui [ 1 ] = ( v & 2 ) ? 1 : 0 ;
gui [ 2 ] = ( v & 4 ) ? 1 : 0 ;
} break ;
case ShaderLanguage : : TYPE_BVEC4 : {
int v = value ;
uint32_t * gui = ( uint32_t * ) data ;
gui [ 0 ] = ( v & 1 ) ? 1 : 0 ;
gui [ 1 ] = ( v & 2 ) ? 1 : 0 ;
gui [ 2 ] = ( v & 4 ) ? 1 : 0 ;
gui [ 3 ] = ( v & 8 ) ? 1 : 0 ;
} break ;
case ShaderLanguage : : TYPE_INT : {
int v = value ;
int32_t * gui = ( int32_t * ) data ;
gui [ 0 ] = v ;
} break ;
case ShaderLanguage : : TYPE_IVEC2 : {
2020-02-17 21:06:54 +00:00
Vector < int > iv = value ;
2019-07-21 14:31:30 +00:00
int s = iv . size ( ) ;
int32_t * gui = ( int32_t * ) data ;
2020-02-17 21:06:54 +00:00
const int * r = iv . ptr ( ) ;
2019-07-21 14:31:30 +00:00
for ( int i = 0 ; i < 2 ; i + + ) {
2020-05-14 14:41:43 +00:00
if ( i < s ) {
2019-07-21 14:31:30 +00:00
gui [ i ] = r [ i ] ;
2020-05-14 14:41:43 +00:00
} else {
2019-07-21 14:31:30 +00:00
gui [ i ] = 0 ;
2020-05-14 14:41:43 +00:00
}
2019-07-21 14:31:30 +00:00
}
} break ;
case ShaderLanguage : : TYPE_IVEC3 : {
2020-02-17 21:06:54 +00:00
Vector < int > iv = value ;
2019-07-21 14:31:30 +00:00
int s = iv . size ( ) ;
int32_t * gui = ( int32_t * ) data ;
2020-02-17 21:06:54 +00:00
const int * r = iv . ptr ( ) ;
2019-07-21 14:31:30 +00:00
for ( int i = 0 ; i < 3 ; i + + ) {
2020-05-14 14:41:43 +00:00
if ( i < s ) {
2019-07-21 14:31:30 +00:00
gui [ i ] = r [ i ] ;
2020-05-14 14:41:43 +00:00
} else {
2019-07-21 14:31:30 +00:00
gui [ i ] = 0 ;
2020-05-14 14:41:43 +00:00
}
2019-07-21 14:31:30 +00:00
}
} break ;
case ShaderLanguage : : TYPE_IVEC4 : {
2020-02-17 21:06:54 +00:00
Vector < int > iv = value ;
2019-07-21 14:31:30 +00:00
int s = iv . size ( ) ;
int32_t * gui = ( int32_t * ) data ;
2020-02-17 21:06:54 +00:00
const int * r = iv . ptr ( ) ;
2019-07-21 14:31:30 +00:00
for ( int i = 0 ; i < 4 ; i + + ) {
2020-05-14 14:41:43 +00:00
if ( i < s ) {
2019-07-21 14:31:30 +00:00
gui [ i ] = r [ i ] ;
2020-05-14 14:41:43 +00:00
} else {
2019-07-21 14:31:30 +00:00
gui [ i ] = 0 ;
2020-05-14 14:41:43 +00:00
}
2019-07-21 14:31:30 +00:00
}
} break ;
case ShaderLanguage : : TYPE_UINT : {
int v = value ;
uint32_t * gui = ( uint32_t * ) data ;
gui [ 0 ] = v ;
} break ;
case ShaderLanguage : : TYPE_UVEC2 : {
2020-02-17 21:06:54 +00:00
Vector < int > iv = value ;
2019-07-21 14:31:30 +00:00
int s = iv . size ( ) ;
uint32_t * gui = ( uint32_t * ) data ;
2020-02-17 21:06:54 +00:00
const int * r = iv . ptr ( ) ;
2019-07-21 14:31:30 +00:00
for ( int i = 0 ; i < 2 ; i + + ) {
2020-05-14 14:41:43 +00:00
if ( i < s ) {
2019-07-21 14:31:30 +00:00
gui [ i ] = r [ i ] ;
2020-05-14 14:41:43 +00:00
} else {
2019-07-21 14:31:30 +00:00
gui [ i ] = 0 ;
2020-05-14 14:41:43 +00:00
}
2019-07-21 14:31:30 +00:00
}
} break ;
case ShaderLanguage : : TYPE_UVEC3 : {
2020-02-17 21:06:54 +00:00
Vector < int > iv = value ;
2019-07-21 14:31:30 +00:00
int s = iv . size ( ) ;
uint32_t * gui = ( uint32_t * ) data ;
2020-02-17 21:06:54 +00:00
const int * r = iv . ptr ( ) ;
2019-07-21 14:31:30 +00:00
for ( int i = 0 ; i < 3 ; i + + ) {
2020-05-14 14:41:43 +00:00
if ( i < s ) {
2019-07-21 14:31:30 +00:00
gui [ i ] = r [ i ] ;
2020-05-14 14:41:43 +00:00
} else {
2019-07-21 14:31:30 +00:00
gui [ i ] = 0 ;
2020-05-14 14:41:43 +00:00
}
2019-07-21 14:31:30 +00:00
}
} break ;
case ShaderLanguage : : TYPE_UVEC4 : {
2020-02-17 21:06:54 +00:00
Vector < int > iv = value ;
2019-07-21 14:31:30 +00:00
int s = iv . size ( ) ;
uint32_t * gui = ( uint32_t * ) data ;
2020-02-17 21:06:54 +00:00
const int * r = iv . ptr ( ) ;
2019-07-21 14:31:30 +00:00
for ( int i = 0 ; i < 4 ; i + + ) {
2020-05-14 14:41:43 +00:00
if ( i < s ) {
2019-07-21 14:31:30 +00:00
gui [ i ] = r [ i ] ;
2020-05-14 14:41:43 +00:00
} else {
2019-07-21 14:31:30 +00:00
gui [ i ] = 0 ;
2020-05-14 14:41:43 +00:00
}
2019-07-21 14:31:30 +00:00
}
} break ;
case ShaderLanguage : : TYPE_FLOAT : {
float v = value ;
float * gui = ( float * ) data ;
gui [ 0 ] = v ;
} break ;
case ShaderLanguage : : TYPE_VEC2 : {
Vector2 v = value ;
float * gui = ( float * ) data ;
gui [ 0 ] = v . x ;
gui [ 1 ] = v . y ;
} break ;
case ShaderLanguage : : TYPE_VEC3 : {
Vector3 v = value ;
float * gui = ( float * ) data ;
gui [ 0 ] = v . x ;
gui [ 1 ] = v . y ;
gui [ 2 ] = v . z ;
} break ;
case ShaderLanguage : : TYPE_VEC4 : {
float * gui = ( float * ) data ;
if ( value . get_type ( ) = = Variant : : COLOR ) {
Color v = value ;
if ( p_linear_color ) {
v = v . to_linear ( ) ;
}
gui [ 0 ] = v . r ;
gui [ 1 ] = v . g ;
gui [ 2 ] = v . b ;
gui [ 3 ] = v . a ;
} else if ( value . get_type ( ) = = Variant : : RECT2 ) {
Rect2 v = value ;
gui [ 0 ] = v . position . x ;
gui [ 1 ] = v . position . y ;
gui [ 2 ] = v . size . x ;
gui [ 3 ] = v . size . y ;
2021-01-20 07:02:02 +00:00
} else if ( value . get_type ( ) = = Variant : : QUATERNION ) {
Quaternion v = value ;
2019-07-21 14:31:30 +00:00
gui [ 0 ] = v . x ;
gui [ 1 ] = v . y ;
gui [ 2 ] = v . z ;
gui [ 3 ] = v . w ;
} else {
Plane v = value ;
gui [ 0 ] = v . normal . x ;
gui [ 1 ] = v . normal . y ;
gui [ 2 ] = v . normal . z ;
2020-05-10 14:47:11 +00:00
gui [ 3 ] = v . d ;
2019-07-21 14:31:30 +00:00
}
} break ;
case ShaderLanguage : : TYPE_MAT2 : {
Transform2D v = value ;
float * gui = ( float * ) data ;
//in std140 members of mat2 are treated as vec4s
gui [ 0 ] = v . elements [ 0 ] [ 0 ] ;
gui [ 1 ] = v . elements [ 0 ] [ 1 ] ;
gui [ 2 ] = 0 ;
gui [ 3 ] = 0 ;
gui [ 4 ] = v . elements [ 1 ] [ 0 ] ;
gui [ 5 ] = v . elements [ 1 ] [ 1 ] ;
gui [ 6 ] = 0 ;
gui [ 7 ] = 0 ;
} break ;
case ShaderLanguage : : TYPE_MAT3 : {
Basis v = value ;
float * gui = ( float * ) data ;
gui [ 0 ] = v . elements [ 0 ] [ 0 ] ;
gui [ 1 ] = v . elements [ 1 ] [ 0 ] ;
gui [ 2 ] = v . elements [ 2 ] [ 0 ] ;
gui [ 3 ] = 0 ;
gui [ 4 ] = v . elements [ 0 ] [ 1 ] ;
gui [ 5 ] = v . elements [ 1 ] [ 1 ] ;
gui [ 6 ] = v . elements [ 2 ] [ 1 ] ;
gui [ 7 ] = 0 ;
gui [ 8 ] = v . elements [ 0 ] [ 2 ] ;
gui [ 9 ] = v . elements [ 1 ] [ 2 ] ;
gui [ 10 ] = v . elements [ 2 ] [ 2 ] ;
gui [ 11 ] = 0 ;
} break ;
case ShaderLanguage : : TYPE_MAT4 : {
2020-10-17 05:08:21 +00:00
Transform3D v = value ;
2019-07-21 14:31:30 +00:00
float * gui = ( float * ) data ;
gui [ 0 ] = v . basis . elements [ 0 ] [ 0 ] ;
gui [ 1 ] = v . basis . elements [ 1 ] [ 0 ] ;
gui [ 2 ] = v . basis . elements [ 2 ] [ 0 ] ;
gui [ 3 ] = 0 ;
gui [ 4 ] = v . basis . elements [ 0 ] [ 1 ] ;
gui [ 5 ] = v . basis . elements [ 1 ] [ 1 ] ;
gui [ 6 ] = v . basis . elements [ 2 ] [ 1 ] ;
gui [ 7 ] = 0 ;
gui [ 8 ] = v . basis . elements [ 0 ] [ 2 ] ;
gui [ 9 ] = v . basis . elements [ 1 ] [ 2 ] ;
gui [ 10 ] = v . basis . elements [ 2 ] [ 2 ] ;
gui [ 11 ] = 0 ;
gui [ 12 ] = v . origin . x ;
gui [ 13 ] = v . origin . y ;
gui [ 14 ] = v . origin . z ;
gui [ 15 ] = 1 ;
} break ;
default : {
}
}
}
_FORCE_INLINE_ static void _fill_std140_ubo_value ( ShaderLanguage : : DataType type , const Vector < ShaderLanguage : : ConstantNode : : Value > & value , uint8_t * data ) {
switch ( type ) {
case ShaderLanguage : : TYPE_BOOL : {
uint32_t * gui = ( uint32_t * ) data ;
* gui = value [ 0 ] . boolean ? 1 : 0 ;
} break ;
case ShaderLanguage : : TYPE_BVEC2 : {
uint32_t * gui = ( uint32_t * ) data ;
gui [ 0 ] = value [ 0 ] . boolean ? 1 : 0 ;
gui [ 1 ] = value [ 1 ] . boolean ? 1 : 0 ;
} break ;
case ShaderLanguage : : TYPE_BVEC3 : {
uint32_t * gui = ( uint32_t * ) data ;
gui [ 0 ] = value [ 0 ] . boolean ? 1 : 0 ;
gui [ 1 ] = value [ 1 ] . boolean ? 1 : 0 ;
gui [ 2 ] = value [ 2 ] . boolean ? 1 : 0 ;
} break ;
case ShaderLanguage : : TYPE_BVEC4 : {
uint32_t * gui = ( uint32_t * ) data ;
gui [ 0 ] = value [ 0 ] . boolean ? 1 : 0 ;
gui [ 1 ] = value [ 1 ] . boolean ? 1 : 0 ;
gui [ 2 ] = value [ 2 ] . boolean ? 1 : 0 ;
gui [ 3 ] = value [ 3 ] . boolean ? 1 : 0 ;
} break ;
case ShaderLanguage : : TYPE_INT : {
int32_t * gui = ( int32_t * ) data ;
gui [ 0 ] = value [ 0 ] . sint ;
} break ;
case ShaderLanguage : : TYPE_IVEC2 : {
int32_t * gui = ( int32_t * ) data ;
for ( int i = 0 ; i < 2 ; i + + ) {
gui [ i ] = value [ i ] . sint ;
}
} break ;
case ShaderLanguage : : TYPE_IVEC3 : {
int32_t * gui = ( int32_t * ) data ;
for ( int i = 0 ; i < 3 ; i + + ) {
gui [ i ] = value [ i ] . sint ;
}
} break ;
case ShaderLanguage : : TYPE_IVEC4 : {
int32_t * gui = ( int32_t * ) data ;
for ( int i = 0 ; i < 4 ; i + + ) {
gui [ i ] = value [ i ] . sint ;
}
} break ;
case ShaderLanguage : : TYPE_UINT : {
uint32_t * gui = ( uint32_t * ) data ;
gui [ 0 ] = value [ 0 ] . uint ;
} break ;
case ShaderLanguage : : TYPE_UVEC2 : {
int32_t * gui = ( int32_t * ) data ;
for ( int i = 0 ; i < 2 ; i + + ) {
gui [ i ] = value [ i ] . uint ;
}
} break ;
case ShaderLanguage : : TYPE_UVEC3 : {
int32_t * gui = ( int32_t * ) data ;
for ( int i = 0 ; i < 3 ; i + + ) {
gui [ i ] = value [ i ] . uint ;
}
} break ;
case ShaderLanguage : : TYPE_UVEC4 : {
int32_t * gui = ( int32_t * ) data ;
for ( int i = 0 ; i < 4 ; i + + ) {
gui [ i ] = value [ i ] . uint ;
}
} break ;
case ShaderLanguage : : TYPE_FLOAT : {
float * gui = ( float * ) data ;
gui [ 0 ] = value [ 0 ] . real ;
} break ;
case ShaderLanguage : : TYPE_VEC2 : {
float * gui = ( float * ) data ;
for ( int i = 0 ; i < 2 ; i + + ) {
gui [ i ] = value [ i ] . real ;
}
} break ;
case ShaderLanguage : : TYPE_VEC3 : {
float * gui = ( float * ) data ;
for ( int i = 0 ; i < 3 ; i + + ) {
gui [ i ] = value [ i ] . real ;
}
} break ;
case ShaderLanguage : : TYPE_VEC4 : {
float * gui = ( float * ) data ;
for ( int i = 0 ; i < 4 ; i + + ) {
gui [ i ] = value [ i ] . real ;
}
} break ;
case ShaderLanguage : : TYPE_MAT2 : {
float * gui = ( float * ) data ;
//in std140 members of mat2 are treated as vec4s
gui [ 0 ] = value [ 0 ] . real ;
gui [ 1 ] = value [ 1 ] . real ;
gui [ 2 ] = 0 ;
gui [ 3 ] = 0 ;
gui [ 4 ] = value [ 2 ] . real ;
gui [ 5 ] = value [ 3 ] . real ;
gui [ 6 ] = 0 ;
gui [ 7 ] = 0 ;
} break ;
case ShaderLanguage : : TYPE_MAT3 : {
float * gui = ( float * ) data ;
gui [ 0 ] = value [ 0 ] . real ;
gui [ 1 ] = value [ 1 ] . real ;
gui [ 2 ] = value [ 2 ] . real ;
gui [ 3 ] = 0 ;
gui [ 4 ] = value [ 3 ] . real ;
gui [ 5 ] = value [ 4 ] . real ;
gui [ 6 ] = value [ 5 ] . real ;
gui [ 7 ] = 0 ;
gui [ 8 ] = value [ 6 ] . real ;
gui [ 9 ] = value [ 7 ] . real ;
gui [ 10 ] = value [ 8 ] . real ;
gui [ 11 ] = 0 ;
} break ;
case ShaderLanguage : : TYPE_MAT4 : {
float * gui = ( float * ) data ;
for ( int i = 0 ; i < 16 ; i + + ) {
gui [ i ] = value [ i ] . real ;
}
} break ;
default : {
}
}
}
_FORCE_INLINE_ static void _fill_std140_ubo_empty ( ShaderLanguage : : DataType type , uint8_t * data ) {
switch ( type ) {
case ShaderLanguage : : TYPE_BOOL :
case ShaderLanguage : : TYPE_INT :
case ShaderLanguage : : TYPE_UINT :
case ShaderLanguage : : TYPE_FLOAT : {
2021-04-27 14:19:21 +00:00
memset ( data , 0 , 4 ) ;
2019-07-21 14:31:30 +00:00
} break ;
case ShaderLanguage : : TYPE_BVEC2 :
case ShaderLanguage : : TYPE_IVEC2 :
case ShaderLanguage : : TYPE_UVEC2 :
case ShaderLanguage : : TYPE_VEC2 : {
2021-04-27 14:19:21 +00:00
memset ( data , 0 , 8 ) ;
2019-07-21 14:31:30 +00:00
} break ;
case ShaderLanguage : : TYPE_BVEC3 :
case ShaderLanguage : : TYPE_IVEC3 :
case ShaderLanguage : : TYPE_UVEC3 :
case ShaderLanguage : : TYPE_VEC3 :
case ShaderLanguage : : TYPE_BVEC4 :
case ShaderLanguage : : TYPE_IVEC4 :
case ShaderLanguage : : TYPE_UVEC4 :
case ShaderLanguage : : TYPE_VEC4 : {
2021-04-27 14:19:21 +00:00
memset ( data , 0 , 16 ) ;
2019-07-21 14:31:30 +00:00
} break ;
case ShaderLanguage : : TYPE_MAT2 : {
2021-04-27 14:19:21 +00:00
memset ( data , 0 , 32 ) ;
2019-07-21 14:31:30 +00:00
} break ;
case ShaderLanguage : : TYPE_MAT3 : {
2021-04-27 14:19:21 +00:00
memset ( data , 0 , 48 ) ;
2019-07-21 14:31:30 +00:00
} break ;
case ShaderLanguage : : TYPE_MAT4 : {
2021-04-27 14:19:21 +00:00
memset ( data , 0 , 64 ) ;
2019-07-21 14:31:30 +00:00
} break ;
default : {
}
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : MaterialData : : update_uniform_buffer ( const Map < StringName , ShaderLanguage : : ShaderNode : : Uniform > & p_uniforms , const uint32_t * p_uniform_offsets , const Map < StringName , Variant > & p_parameters , uint8_t * p_buffer , uint32_t p_buffer_size , bool p_use_linear_color ) {
2020-04-17 02:52:00 +00:00
bool uses_global_buffer = false ;
2019-07-21 14:31:30 +00:00
for ( Map < StringName , ShaderLanguage : : ShaderNode : : Uniform > : : Element * E = p_uniforms . front ( ) ; E ; E = E - > next ( ) ) {
2020-05-14 14:41:43 +00:00
if ( E - > get ( ) . order < 0 ) {
2019-07-21 14:31:30 +00:00
continue ; // texture, does not go here
2020-05-14 14:41:43 +00:00
}
2019-07-21 14:31:30 +00:00
2020-04-17 02:52:00 +00:00
if ( E - > get ( ) . scope = = ShaderLanguage : : ShaderNode : : Uniform : : SCOPE_INSTANCE ) {
continue ; //instance uniforms don't appear in the bufferr
}
if ( E - > get ( ) . scope = = ShaderLanguage : : ShaderNode : : Uniform : : SCOPE_GLOBAL ) {
//this is a global variable, get the index to it
2020-12-04 18:26:24 +00:00
RendererStorageRD * rs = base_singleton ;
2020-04-17 02:52:00 +00:00
GlobalVariables : : Variable * gv = rs - > global_variables . variables . getptr ( E - > key ( ) ) ;
uint32_t index = 0 ;
if ( gv ) {
index = gv - > buffer_index ;
} else {
WARN_PRINT ( " Shader uses global uniform ' " + E - > key ( ) + " ', but it was removed at some point. Material will not display correctly. " ) ;
}
uint32_t offset = p_uniform_offsets [ E - > get ( ) . order ] ;
uint32_t * intptr = ( uint32_t * ) & p_buffer [ offset ] ;
* intptr = index ;
uses_global_buffer = true ;
continue ;
}
2019-07-21 14:31:30 +00:00
//regular uniform
uint32_t offset = p_uniform_offsets [ E - > get ( ) . order ] ;
# ifdef DEBUG_ENABLED
uint32_t size = ShaderLanguage : : get_type_size ( E - > get ( ) . type ) ;
ERR_CONTINUE ( offset + size > p_buffer_size ) ;
# endif
uint8_t * data = & p_buffer [ offset ] ;
const Map < StringName , Variant > : : Element * V = p_parameters . find ( E - > key ( ) ) ;
if ( V ) {
//user provided
_fill_std140_variant_ubo_value ( E - > get ( ) . type , V - > get ( ) , data , p_use_linear_color ) ;
} else if ( E - > get ( ) . default_value . size ( ) ) {
//default value
_fill_std140_ubo_value ( E - > get ( ) . type , E - > get ( ) . default_value , data ) ;
//value=E->get().default_value;
} else {
//zero because it was not provided
if ( E - > get ( ) . type = = ShaderLanguage : : TYPE_VEC4 & & E - > get ( ) . hint = = ShaderLanguage : : ShaderNode : : Uniform : : HINT_COLOR ) {
//colors must be set as black, with alpha as 1.0
_fill_std140_variant_ubo_value ( E - > get ( ) . type , Color ( 0 , 0 , 0 , 1 ) , data , p_use_linear_color ) ;
} else {
//else just zero it out
_fill_std140_ubo_empty ( E - > get ( ) . type , data ) ;
}
}
}
2020-04-17 02:52:00 +00:00
if ( uses_global_buffer ! = ( global_buffer_E ! = nullptr ) ) {
2020-12-04 18:26:24 +00:00
RendererStorageRD * rs = base_singleton ;
2020-04-17 02:52:00 +00:00
if ( uses_global_buffer ) {
global_buffer_E = rs - > global_variables . materials_using_buffer . push_back ( self ) ;
} else {
rs - > global_variables . materials_using_buffer . erase ( global_buffer_E ) ;
global_buffer_E = nullptr ;
}
}
}
2020-12-04 18:26:24 +00:00
RendererStorageRD : : MaterialData : : ~ MaterialData ( ) {
2020-04-17 02:52:00 +00:00
if ( global_buffer_E ) {
//unregister global buffers
2020-12-04 18:26:24 +00:00
RendererStorageRD * rs = base_singleton ;
2020-04-17 02:52:00 +00:00
rs - > global_variables . materials_using_buffer . erase ( global_buffer_E ) ;
}
if ( global_texture_E ) {
//unregister global textures
2020-12-04 18:26:24 +00:00
RendererStorageRD * rs = base_singleton ;
2020-04-17 02:52:00 +00:00
for ( Map < StringName , uint64_t > : : Element * E = used_global_textures . front ( ) ; E ; E = E - > next ( ) ) {
GlobalVariables : : Variable * v = rs - > global_variables . variables . getptr ( E - > key ( ) ) ;
if ( v ) {
v - > texture_materials . erase ( self ) ;
}
}
//unregister material from those using global textures
rs - > global_variables . materials_using_texture . erase ( global_texture_E ) ;
}
2019-07-21 14:31:30 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : MaterialData : : update_textures ( const Map < StringName , Variant > & p_parameters , const Map < StringName , RID > & p_default_textures , const Vector < ShaderCompilerRD : : GeneratedCode : : Texture > & p_texture_uniforms , RID * p_textures , bool p_use_linear_color ) {
RendererStorageRD * singleton = ( RendererStorageRD * ) RendererStorage : : base_singleton ;
2019-09-14 03:37:42 +00:00
# ifdef TOOLS_ENABLED
Texture * roughness_detect_texture = nullptr ;
2021-01-02 05:27:12 +00:00
RS : : TextureDetectRoughnessChannel roughness_channel = RS : : TEXTURE_DETECT_ROUGHNESS_R ;
2019-09-14 03:37:42 +00:00
Texture * normal_detect_texture = nullptr ;
# endif
2019-07-21 14:31:30 +00:00
2020-04-17 02:52:00 +00:00
bool uses_global_textures = false ;
global_textures_pass + + ;
2019-07-21 14:31:30 +00:00
for ( int i = 0 ; i < p_texture_uniforms . size ( ) ; i + + ) {
const StringName & uniform_name = p_texture_uniforms [ i ] . name ;
RID texture ;
2020-04-17 02:52:00 +00:00
if ( p_texture_uniforms [ i ] . global ) {
2020-12-04 18:26:24 +00:00
RendererStorageRD * rs = base_singleton ;
2019-09-15 04:01:52 +00:00
2020-04-17 02:52:00 +00:00
uses_global_textures = true ;
GlobalVariables : : Variable * v = rs - > global_variables . variables . getptr ( uniform_name ) ;
if ( v ) {
if ( v - > buffer_index > = 0 ) {
WARN_PRINT ( " Shader uses global uniform texture ' " + String ( uniform_name ) + " ', but it changed type and is no longer a texture!. " ) ;
} else {
Map < StringName , uint64_t > : : Element * E = used_global_textures . find ( uniform_name ) ;
if ( ! E ) {
E = used_global_textures . insert ( uniform_name , global_textures_pass ) ;
v - > texture_materials . insert ( self ) ;
} else {
E - > get ( ) = global_textures_pass ;
}
texture = v - > override . get_type ( ) ! = Variant : : NIL ? v - > override : v - > value ;
}
} else {
WARN_PRINT ( " Shader uses global uniform texture ' " + String ( uniform_name ) + " ', but it was removed at some point. Material will not display correctly. " ) ;
}
} else {
if ( ! texture . is_valid ( ) ) {
const Map < StringName , Variant > : : Element * V = p_parameters . find ( uniform_name ) ;
if ( V ) {
texture = V - > get ( ) ;
}
}
if ( ! texture . is_valid ( ) ) {
const Map < StringName , RID > : : Element * W = p_default_textures . find ( uniform_name ) ;
if ( W ) {
texture = W - > get ( ) ;
}
2019-07-21 14:31:30 +00:00
}
}
RID rd_texture ;
if ( texture . is_null ( ) ) {
//check default usage
switch ( p_texture_uniforms [ i ] . hint ) {
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_BLACK :
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_BLACK_ALBEDO : {
rd_texture = singleton - > texture_rd_get_default ( DEFAULT_RD_TEXTURE_BLACK ) ;
} break ;
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_NONE : {
rd_texture = singleton - > texture_rd_get_default ( DEFAULT_RD_TEXTURE_NORMAL ) ;
} break ;
case ShaderLanguage : : ShaderNode : : Uniform : : HINT_ANISO : {
rd_texture = singleton - > texture_rd_get_default ( DEFAULT_RD_TEXTURE_ANISO ) ;
} break ;
default : {
rd_texture = singleton - > texture_rd_get_default ( DEFAULT_RD_TEXTURE_WHITE ) ;
} break ;
}
} else {
2019-09-15 04:01:52 +00:00
bool srgb = p_use_linear_color & & ( p_texture_uniforms [ i ] . hint = = ShaderLanguage : : ShaderNode : : Uniform : : HINT_ALBEDO | | p_texture_uniforms [ i ] . hint = = ShaderLanguage : : ShaderNode : : Uniform : : HINT_BLACK_ALBEDO ) ;
2019-09-14 03:37:42 +00:00
Texture * tex = singleton - > texture_owner . getornull ( texture ) ;
if ( tex ) {
rd_texture = ( srgb & & tex - > rd_texture_srgb . is_valid ( ) ) ? tex - > rd_texture_srgb : tex - > rd_texture ;
# ifdef TOOLS_ENABLED
if ( tex - > detect_3d_callback & & p_use_linear_color ) {
tex - > detect_3d_callback ( tex - > detect_3d_callback_ud ) ;
}
if ( tex - > detect_normal_callback & & ( p_texture_uniforms [ i ] . hint = = ShaderLanguage : : ShaderNode : : Uniform : : HINT_NORMAL | | p_texture_uniforms [ i ] . hint = = ShaderLanguage : : ShaderNode : : Uniform : : HINT_ROUGHNESS_NORMAL ) ) {
if ( p_texture_uniforms [ i ] . hint = = ShaderLanguage : : ShaderNode : : Uniform : : HINT_ROUGHNESS_NORMAL ) {
normal_detect_texture = tex ;
}
tex - > detect_normal_callback ( tex - > detect_normal_callback_ud ) ;
}
if ( tex - > detect_roughness_callback & & ( p_texture_uniforms [ i ] . hint > = ShaderLanguage : : ShaderNode : : Uniform : : HINT_ROUGHNESS_R | | p_texture_uniforms [ i ] . hint < = ShaderLanguage : : ShaderNode : : Uniform : : HINT_ROUGHNESS_GRAY ) ) {
//find the normal texture
roughness_detect_texture = tex ;
2020-03-27 18:21:27 +00:00
roughness_channel = RS : : TextureDetectRoughnessChannel ( p_texture_uniforms [ i ] . hint - ShaderLanguage : : ShaderNode : : Uniform : : HINT_ROUGHNESS_R ) ;
2019-09-14 03:37:42 +00:00
}
# endif
}
2019-07-21 14:31:30 +00:00
if ( rd_texture . is_null ( ) ) {
//wtf
rd_texture = singleton - > texture_rd_get_default ( DEFAULT_RD_TEXTURE_WHITE ) ;
}
}
p_textures [ i ] = rd_texture ;
}
2019-09-14 03:37:42 +00:00
# ifdef TOOLS_ENABLED
if ( roughness_detect_texture & & normal_detect_texture & & normal_detect_texture - > path ! = String ( ) ) {
roughness_detect_texture - > detect_roughness_callback ( roughness_detect_texture - > detect_roughness_callback_ud , normal_detect_texture - > path , roughness_channel ) ;
}
# endif
2020-04-17 02:52:00 +00:00
{
//for textures no longer used, unregister them
List < Map < StringName , uint64_t > : : Element * > to_delete ;
2020-12-04 18:26:24 +00:00
RendererStorageRD * rs = base_singleton ;
2020-04-17 02:52:00 +00:00
for ( Map < StringName , uint64_t > : : Element * E = used_global_textures . front ( ) ; E ; E = E - > next ( ) ) {
if ( E - > get ( ) ! = global_textures_pass ) {
to_delete . push_back ( E ) ;
GlobalVariables : : Variable * v = rs - > global_variables . variables . getptr ( E - > key ( ) ) ;
if ( v ) {
v - > texture_materials . erase ( self ) ;
}
}
}
while ( to_delete . front ( ) ) {
used_global_textures . erase ( to_delete . front ( ) - > get ( ) ) ;
to_delete . pop_front ( ) ;
}
//handle registering/unregistering global textures
if ( uses_global_textures ! = ( global_texture_E ! = nullptr ) ) {
if ( uses_global_textures ) {
global_texture_E = rs - > global_variables . materials_using_texture . push_back ( self ) ;
} else {
rs - > global_variables . materials_using_texture . erase ( global_texture_E ) ;
global_texture_E = nullptr ;
}
}
}
2019-07-21 14:31:30 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : material_force_update_textures ( RID p_material , ShaderType p_shader_type ) {
2019-07-29 18:29:43 +00:00
Material * material = material_owner . getornull ( p_material ) ;
if ( material - > shader_type ! = p_shader_type ) {
return ;
}
if ( material - > data ) {
material - > data - > update_parameters ( material - > params , false , true ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _update_queued_materials ( ) {
2019-07-21 14:31:30 +00:00
Material * material = material_update_list ;
while ( material ) {
Material * next = material - > update_next ;
if ( material - > data ) {
material - > data - > update_parameters ( material - > params , material - > uniform_dirty , material - > texture_dirty ) ;
}
material - > update_requested = false ;
material - > texture_dirty = false ;
material - > uniform_dirty = false ;
2020-04-01 23:20:12 +00:00
material - > update_next = nullptr ;
2019-07-21 14:31:30 +00:00
material = next ;
}
2020-04-01 23:20:12 +00:00
material_update_list = nullptr ;
2019-07-21 14:31:30 +00:00
}
2020-05-14 12:29:06 +00:00
2019-08-18 22:40:52 +00:00
/* MESH API */
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : mesh_allocate ( ) {
return mesh_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : mesh_initialize ( RID p_rid ) {
mesh_owner . initialize_rid ( p_rid , Mesh ( ) ) ;
2019-08-18 22:40:52 +00:00
}
2020-12-16 14:07:08 +00:00
void RendererStorageRD : : mesh_set_blend_shape_count ( RID p_mesh , int p_blend_shape_count ) {
ERR_FAIL_COND ( p_blend_shape_count < 0 ) ;
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND ( ! mesh ) ;
ERR_FAIL_COND ( mesh - > surface_count > 0 ) ; //surfaces already exist
mesh - > blend_shape_count = p_blend_shape_count ;
}
2019-08-18 22:40:52 +00:00
/// Returns stride
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : mesh_add_surface ( RID p_mesh , const RS : : SurfaceData & p_surface ) {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND ( ! mesh ) ;
2019-09-23 19:01:05 +00:00
# ifdef DEBUG_ENABLED
//do a validation, to catch errors first
{
uint32_t stride = 0 ;
2020-12-02 01:40:47 +00:00
uint32_t attrib_stride = 0 ;
uint32_t skin_stride = 0 ;
2019-09-23 19:01:05 +00:00
2020-03-27 18:21:27 +00:00
for ( int i = 0 ; i < RS : : ARRAY_WEIGHTS ; i + + ) {
2019-09-23 19:01:05 +00:00
if ( ( p_surface . format & ( 1 < < i ) ) ) {
switch ( i ) {
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_VERTEX : {
if ( p_surface . format & RS : : ARRAY_FLAG_USE_2D_VERTICES ) {
2019-09-23 19:01:05 +00:00
stride + = sizeof ( float ) * 2 ;
} else {
stride + = sizeof ( float ) * 3 ;
}
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_NORMAL : {
2020-12-02 01:40:47 +00:00
stride + = sizeof ( int32_t ) ;
2019-09-23 19:01:05 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TANGENT : {
2020-12-02 01:40:47 +00:00
stride + = sizeof ( int32_t ) ;
2019-09-23 19:01:05 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_COLOR : {
2020-12-02 01:40:47 +00:00
attrib_stride + = sizeof ( int16_t ) * 4 ;
2019-09-23 19:01:05 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TEX_UV : {
2020-12-02 01:40:47 +00:00
attrib_stride + = sizeof ( float ) * 2 ;
2019-09-23 19:01:05 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TEX_UV2 : {
2020-12-02 01:40:47 +00:00
attrib_stride + = sizeof ( float ) * 2 ;
2019-09-23 19:01:05 +00:00
} break ;
2020-12-02 01:40:47 +00:00
case RS : : ARRAY_CUSTOM0 :
case RS : : ARRAY_CUSTOM1 :
case RS : : ARRAY_CUSTOM2 :
case RS : : ARRAY_CUSTOM3 : {
int idx = i - RS : : ARRAY_CUSTOM0 ;
uint32_t fmt_shift [ RS : : ARRAY_CUSTOM_COUNT ] = { RS : : ARRAY_FORMAT_CUSTOM0_SHIFT , RS : : ARRAY_FORMAT_CUSTOM1_SHIFT , RS : : ARRAY_FORMAT_CUSTOM2_SHIFT , RS : : ARRAY_FORMAT_CUSTOM3_SHIFT } ;
uint32_t fmt = ( p_surface . format > > fmt_shift [ idx ] ) & RS : : ARRAY_FORMAT_CUSTOM_MASK ;
uint32_t fmtsize [ RS : : ARRAY_CUSTOM_MAX ] = { 4 , 4 , 4 , 8 , 4 , 8 , 12 , 16 } ;
attrib_stride + = fmtsize [ fmt ] ;
2019-09-23 19:01:05 +00:00
} break ;
2020-12-02 01:40:47 +00:00
case RS : : ARRAY_WEIGHTS :
case RS : : ARRAY_BONES : {
//uses a separate array
bool use_8 = p_surface . format & RS : : ARRAY_FLAG_USE_8_BONE_WEIGHTS ;
2020-12-16 14:07:08 +00:00
skin_stride + = sizeof ( int16_t ) * ( use_8 ? 16 : 8 ) ;
2020-12-02 01:40:47 +00:00
} break ;
2019-09-23 19:01:05 +00:00
}
}
}
int expected_size = stride * p_surface . vertex_count ;
2020-12-02 01:40:47 +00:00
ERR_FAIL_COND_MSG ( expected_size ! = p_surface . vertex_data . size ( ) , " Size of vertex data provided ( " + itos ( p_surface . vertex_data . size ( ) ) + " ) does not match expected ( " + itos ( expected_size ) + " ) " ) ;
2020-12-16 14:07:08 +00:00
int bs_expected_size = expected_size * mesh - > blend_shape_count ;
ERR_FAIL_COND_MSG ( bs_expected_size ! = p_surface . blend_shape_data . size ( ) , " Size of blend shape data provided ( " + itos ( p_surface . blend_shape_data . size ( ) ) + " ) does not match expected ( " + itos ( bs_expected_size ) + " ) " ) ;
2020-12-02 01:40:47 +00:00
int expected_attrib_size = attrib_stride * p_surface . vertex_count ;
ERR_FAIL_COND_MSG ( expected_attrib_size ! = p_surface . attribute_data . size ( ) , " Size of attribute data provided ( " + itos ( p_surface . attribute_data . size ( ) ) + " ) does not match expected ( " + itos ( expected_attrib_size ) + " ) " ) ;
if ( ( p_surface . format & RS : : ARRAY_FORMAT_WEIGHTS ) & & ( p_surface . format & RS : : ARRAY_FORMAT_BONES ) ) {
expected_size = skin_stride * p_surface . vertex_count ;
ERR_FAIL_COND_MSG ( expected_size ! = p_surface . skin_data . size ( ) , " Size of skin data provided ( " + itos ( p_surface . skin_data . size ( ) ) + " ) does not match expected ( " + itos ( expected_size ) + " ) " ) ;
}
2019-09-23 19:01:05 +00:00
}
# endif
2019-08-18 22:40:52 +00:00
Mesh : : Surface * s = memnew ( Mesh : : Surface ) ;
s - > format = p_surface . format ;
s - > primitive = p_surface . primitive ;
2020-12-16 14:07:08 +00:00
bool use_as_storage = ( p_surface . skin_data . size ( ) | | mesh - > blend_shape_count > 0 ) ;
s - > vertex_buffer = RD : : get_singleton ( ) - > vertex_buffer_create ( p_surface . vertex_data . size ( ) , p_surface . vertex_data , use_as_storage ) ;
s - > vertex_buffer_size = p_surface . vertex_data . size ( ) ;
2020-12-02 01:40:47 +00:00
if ( p_surface . attribute_data . size ( ) ) {
s - > attribute_buffer = RD : : get_singleton ( ) - > vertex_buffer_create ( p_surface . attribute_data . size ( ) , p_surface . attribute_data ) ;
}
if ( p_surface . skin_data . size ( ) ) {
2020-12-16 14:07:08 +00:00
s - > skin_buffer = RD : : get_singleton ( ) - > vertex_buffer_create ( p_surface . skin_data . size ( ) , p_surface . skin_data , use_as_storage ) ;
s - > skin_buffer_size = p_surface . skin_data . size ( ) ;
2020-12-02 01:40:47 +00:00
}
2020-12-16 14:07:08 +00:00
2019-08-18 22:40:52 +00:00
s - > vertex_count = p_surface . vertex_count ;
2020-12-16 14:07:08 +00:00
if ( p_surface . format & RS : : ARRAY_FORMAT_BONES ) {
mesh - > has_bone_weights = true ;
}
2019-08-18 22:40:52 +00:00
if ( p_surface . index_count ) {
bool is_index_16 = p_surface . vertex_count < = 65536 ;
s - > index_buffer = RD : : get_singleton ( ) - > index_buffer_create ( p_surface . index_count , is_index_16 ? RD : : INDEX_BUFFER_FORMAT_UINT16 : RD : : INDEX_BUFFER_FORMAT_UINT32 , p_surface . index_data , false ) ;
s - > index_count = p_surface . index_count ;
s - > index_array = RD : : get_singleton ( ) - > index_array_create ( s - > index_buffer , 0 , s - > index_count ) ;
if ( p_surface . lods . size ( ) ) {
s - > lods = memnew_arr ( Mesh : : Surface : : LOD , p_surface . lods . size ( ) ) ;
s - > lod_count = p_surface . lods . size ( ) ;
for ( int i = 0 ; i < p_surface . lods . size ( ) ; i + + ) {
uint32_t indices = p_surface . lods [ i ] . index_data . size ( ) / ( is_index_16 ? 2 : 4 ) ;
s - > lods [ i ] . index_buffer = RD : : get_singleton ( ) - > index_buffer_create ( indices , is_index_16 ? RD : : INDEX_BUFFER_FORMAT_UINT16 : RD : : INDEX_BUFFER_FORMAT_UINT32 , p_surface . lods [ i ] . index_data ) ;
s - > lods [ i ] . index_array = RD : : get_singleton ( ) - > index_array_create ( s - > lods [ i ] . index_buffer , 0 , indices ) ;
s - > lods [ i ] . edge_length = p_surface . lods [ i ] . edge_length ;
}
}
}
s - > aabb = p_surface . aabb ;
s - > bone_aabbs = p_surface . bone_aabbs ; //only really useful for returning them.
2020-12-16 14:07:08 +00:00
if ( mesh - > blend_shape_count > 0 ) {
s - > blend_shape_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( p_surface . blend_shape_data . size ( ) , p_surface . blend_shape_data ) ;
}
if ( use_as_storage ) {
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 0 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . ids . push_back ( s - > vertex_buffer ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 1 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
if ( s - > skin_buffer . is_valid ( ) ) {
u . ids . push_back ( s - > skin_buffer ) ;
} else {
u . ids . push_back ( default_rd_storage_buffer ) ;
}
uniforms . push_back ( u ) ;
2020-03-02 18:17:20 +00:00
}
2020-12-16 14:07:08 +00:00
{
RD : : Uniform u ;
u . binding = 2 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
if ( s - > blend_shape_buffer . is_valid ( ) ) {
u . ids . push_back ( s - > blend_shape_buffer ) ;
} else {
u . ids . push_back ( default_rd_storage_buffer ) ;
}
uniforms . push_back ( u ) ;
}
s - > uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , skeleton_shader . version_shader [ 0 ] , SkeletonShader : : UNIFORM_SET_SURFACE ) ;
2019-08-18 22:40:52 +00:00
}
if ( mesh - > surface_count = = 0 ) {
mesh - > bone_aabbs = p_surface . bone_aabbs ;
mesh - > aabb = p_surface . aabb ;
} else {
2020-12-24 08:01:07 +00:00
if ( mesh - > bone_aabbs . size ( ) < p_surface . bone_aabbs . size ( ) ) {
// ArrayMesh::_surface_set_data only allocates bone_aabbs up to max_bone
// Each surface may affect different numbers of bones.
mesh - > bone_aabbs . resize ( p_surface . bone_aabbs . size ( ) ) ;
}
2019-08-18 22:40:52 +00:00
for ( int i = 0 ; i < p_surface . bone_aabbs . size ( ) ; i + + ) {
mesh - > bone_aabbs . write [ i ] . merge_with ( p_surface . bone_aabbs [ i ] ) ;
}
mesh - > aabb . merge_with ( p_surface . aabb ) ;
}
2019-10-03 20:39:08 +00:00
s - > material = p_surface . material ;
2019-08-18 22:40:52 +00:00
mesh - > surfaces = ( Mesh : : Surface * * ) memrealloc ( mesh - > surfaces , sizeof ( Mesh : : Surface * ) * ( mesh - > surface_count + 1 ) ) ;
mesh - > surfaces [ mesh - > surface_count ] = s ;
mesh - > surface_count + + ;
2020-12-16 14:07:08 +00:00
for ( List < MeshInstance * > : : Element * E = mesh - > instances . front ( ) ; E ; E = E - > next ( ) ) {
//update instances
MeshInstance * mi = E - > get ( ) ;
_mesh_instance_add_surface ( mi , mesh , mesh - > surface_count - 1 ) ;
}
2021-01-04 12:33:25 +00:00
mesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_MESH ) ;
2019-08-18 22:40:52 +00:00
2021-01-25 15:20:11 +00:00
for ( Set < Mesh * > : : Element * E = mesh - > shadow_owners . front ( ) ; E ; E = E - > next ( ) ) {
Mesh * shadow_owner = E - > get ( ) ;
shadow_owner - > shadow_mesh = RID ( ) ;
shadow_owner - > dependency . changed_notify ( DEPENDENCY_CHANGED_MESH ) ;
}
2019-08-18 22:40:52 +00:00
mesh - > material_cache . clear ( ) ;
}
2020-12-04 18:26:24 +00:00
int RendererStorageRD : : mesh_get_blend_shape_count ( RID p_mesh ) const {
2019-08-18 22:40:52 +00:00
const Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND_V ( ! mesh , - 1 ) ;
return mesh - > blend_shape_count ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : mesh_set_blend_shape_mode ( RID p_mesh , RS : : BlendShapeMode p_mode ) {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND ( ! mesh ) ;
2020-02-18 13:57:11 +00:00
ERR_FAIL_INDEX ( ( int ) p_mode , 2 ) ;
2019-08-18 22:40:52 +00:00
mesh - > blend_shape_mode = p_mode ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
RS : : BlendShapeMode RendererStorageRD : : mesh_get_blend_shape_mode ( RID p_mesh ) const {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND_V ( ! mesh , RS : : BLEND_SHAPE_MODE_NORMALIZED ) ;
2019-08-18 22:40:52 +00:00
return mesh - > blend_shape_mode ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : mesh_surface_update_region ( RID p_mesh , int p_surface , int p_offset , const Vector < uint8_t > & p_data ) {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND ( ! mesh ) ;
2020-02-13 14:53:32 +00:00
ERR_FAIL_UNSIGNED_INDEX ( ( uint32_t ) p_surface , mesh - > surface_count ) ;
2019-08-18 22:40:52 +00:00
ERR_FAIL_COND ( p_data . size ( ) = = 0 ) ;
uint64_t data_size = p_data . size ( ) ;
2020-02-17 21:06:54 +00:00
const uint8_t * r = p_data . ptr ( ) ;
2019-08-18 22:40:52 +00:00
2020-02-17 21:06:54 +00:00
RD : : get_singleton ( ) - > buffer_update ( mesh - > surfaces [ p_surface ] - > vertex_buffer , p_offset , data_size , r ) ;
2019-08-18 22:40:52 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : mesh_surface_set_material ( RID p_mesh , int p_surface , RID p_material ) {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND ( ! mesh ) ;
2020-02-13 14:53:32 +00:00
ERR_FAIL_UNSIGNED_INDEX ( ( uint32_t ) p_surface , mesh - > surface_count ) ;
2019-08-18 22:40:52 +00:00
mesh - > surfaces [ p_surface ] - > material = p_material ;
2021-01-04 12:33:25 +00:00
mesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_MATERIAL ) ;
2019-08-18 22:40:52 +00:00
mesh - > material_cache . clear ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : mesh_surface_get_material ( RID p_mesh , int p_surface ) const {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND_V ( ! mesh , RID ( ) ) ;
2020-02-13 14:53:32 +00:00
ERR_FAIL_UNSIGNED_INDEX_V ( ( uint32_t ) p_surface , mesh - > surface_count , RID ( ) ) ;
2019-08-18 22:40:52 +00:00
return mesh - > surfaces [ p_surface ] - > material ;
}
2020-12-04 18:26:24 +00:00
RS : : SurfaceData RendererStorageRD : : mesh_get_surface ( RID p_mesh , int p_surface ) const {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND_V ( ! mesh , RS : : SurfaceData ( ) ) ;
ERR_FAIL_UNSIGNED_INDEX_V ( ( uint32_t ) p_surface , mesh - > surface_count , RS : : SurfaceData ( ) ) ;
2019-08-18 22:40:52 +00:00
Mesh : : Surface & s = * mesh - > surfaces [ p_surface ] ;
2020-03-27 18:21:27 +00:00
RS : : SurfaceData sd ;
2019-08-18 22:40:52 +00:00
sd . format = s . format ;
sd . vertex_data = RD : : get_singleton ( ) - > buffer_get_data ( s . vertex_buffer ) ;
2020-12-02 01:40:47 +00:00
if ( s . attribute_buffer . is_valid ( ) ) {
sd . attribute_data = RD : : get_singleton ( ) - > buffer_get_data ( s . attribute_buffer ) ;
}
if ( s . skin_buffer . is_valid ( ) ) {
sd . skin_data = RD : : get_singleton ( ) - > buffer_get_data ( s . skin_buffer ) ;
}
2019-08-18 22:40:52 +00:00
sd . vertex_count = s . vertex_count ;
sd . index_count = s . index_count ;
2019-10-03 20:39:08 +00:00
sd . primitive = s . primitive ;
2019-08-18 22:40:52 +00:00
if ( sd . index_count ) {
sd . index_data = RD : : get_singleton ( ) - > buffer_get_data ( s . index_buffer ) ;
}
sd . aabb = s . aabb ;
for ( uint32_t i = 0 ; i < s . lod_count ; i + + ) {
2020-03-27 18:21:27 +00:00
RS : : SurfaceData : : LOD lod ;
2019-08-18 22:40:52 +00:00
lod . edge_length = s . lods [ i ] . edge_length ;
lod . index_data = RD : : get_singleton ( ) - > buffer_get_data ( s . lods [ i ] . index_buffer ) ;
sd . lods . push_back ( lod ) ;
}
sd . bone_aabbs = s . bone_aabbs ;
2020-12-02 01:40:47 +00:00
if ( s . blend_shape_buffer . is_valid ( ) ) {
sd . blend_shape_data = RD : : get_singleton ( ) - > buffer_get_data ( s . blend_shape_buffer ) ;
2019-08-18 22:40:52 +00:00
}
return sd ;
}
2020-12-04 18:26:24 +00:00
int RendererStorageRD : : mesh_get_surface_count ( RID p_mesh ) const {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND_V ( ! mesh , 0 ) ;
return mesh - > surface_count ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : mesh_set_custom_aabb ( RID p_mesh , const AABB & p_aabb ) {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND ( ! mesh ) ;
mesh - > custom_aabb = p_aabb ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : mesh_get_custom_aabb ( RID p_mesh ) const {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND_V ( ! mesh , AABB ( ) ) ;
return mesh - > custom_aabb ;
}
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : mesh_get_aabb ( RID p_mesh , RID p_skeleton ) {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND_V ( ! mesh , AABB ( ) ) ;
if ( mesh - > custom_aabb ! = AABB ( ) ) {
return mesh - > custom_aabb ;
}
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
if ( ! skeleton | | skeleton - > size = = 0 ) {
2019-08-18 22:40:52 +00:00
return mesh - > aabb ;
}
2019-09-23 21:53:05 +00:00
AABB aabb ;
2020-01-25 10:18:55 +00:00
for ( uint32_t i = 0 ; i < mesh - > surface_count ; i + + ) {
2019-09-23 21:53:05 +00:00
AABB laabb ;
2020-03-27 18:21:27 +00:00
if ( ( mesh - > surfaces [ i ] - > format & RS : : ARRAY_FORMAT_BONES ) & & mesh - > surfaces [ i ] - > bone_aabbs . size ( ) ) {
2019-09-23 21:53:05 +00:00
int bs = mesh - > surfaces [ i ] - > bone_aabbs . size ( ) ;
const AABB * skbones = mesh - > surfaces [ i ] - > bone_aabbs . ptr ( ) ;
int sbs = skeleton - > size ;
ERR_CONTINUE ( bs > sbs ) ;
const float * baseptr = skeleton - > data . ptr ( ) ;
bool first = true ;
if ( skeleton - > use_2d ) {
for ( int j = 0 ; j < bs ; j + + ) {
2020-05-14 14:41:43 +00:00
if ( skbones [ 0 ] . size = = Vector3 ( ) ) {
2019-09-23 21:53:05 +00:00
continue ; //bone is unused
2020-05-14 14:41:43 +00:00
}
2019-09-23 21:53:05 +00:00
const float * dataptr = baseptr + j * 8 ;
2020-10-17 05:08:21 +00:00
Transform3D mtx ;
2019-09-23 21:53:05 +00:00
mtx . basis . elements [ 0 ] . x = dataptr [ 0 ] ;
mtx . basis . elements [ 1 ] . x = dataptr [ 1 ] ;
mtx . origin . x = dataptr [ 3 ] ;
mtx . basis . elements [ 0 ] . y = dataptr [ 4 ] ;
mtx . basis . elements [ 1 ] . y = dataptr [ 5 ] ;
mtx . origin . y = dataptr [ 7 ] ;
AABB baabb = mtx . xform ( skbones [ j ] ) ;
if ( first ) {
laabb = baabb ;
first = false ;
} else {
laabb . merge_with ( baabb ) ;
}
}
} else {
for ( int j = 0 ; j < bs ; j + + ) {
2020-05-14 14:41:43 +00:00
if ( skbones [ 0 ] . size = = Vector3 ( ) ) {
2019-09-23 21:53:05 +00:00
continue ; //bone is unused
2020-05-14 14:41:43 +00:00
}
2019-09-23 21:53:05 +00:00
const float * dataptr = baseptr + j * 12 ;
2020-10-17 05:08:21 +00:00
Transform3D mtx ;
2019-09-23 21:53:05 +00:00
mtx . basis . elements [ 0 ] [ 0 ] = dataptr [ 0 ] ;
mtx . basis . elements [ 0 ] [ 1 ] = dataptr [ 1 ] ;
mtx . basis . elements [ 0 ] [ 2 ] = dataptr [ 2 ] ;
mtx . origin . x = dataptr [ 3 ] ;
mtx . basis . elements [ 1 ] [ 0 ] = dataptr [ 4 ] ;
mtx . basis . elements [ 1 ] [ 1 ] = dataptr [ 5 ] ;
mtx . basis . elements [ 1 ] [ 2 ] = dataptr [ 6 ] ;
mtx . origin . y = dataptr [ 7 ] ;
mtx . basis . elements [ 2 ] [ 0 ] = dataptr [ 8 ] ;
mtx . basis . elements [ 2 ] [ 1 ] = dataptr [ 9 ] ;
mtx . basis . elements [ 2 ] [ 2 ] = dataptr [ 10 ] ;
mtx . origin . z = dataptr [ 11 ] ;
AABB baabb = mtx . xform ( skbones [ j ] ) ;
if ( first ) {
laabb = baabb ;
first = false ;
} else {
laabb . merge_with ( baabb ) ;
}
}
}
if ( laabb . size = = Vector3 ( ) ) {
laabb = mesh - > surfaces [ i ] - > aabb ;
}
} else {
laabb = mesh - > surfaces [ i ] - > aabb ;
}
if ( i = = 0 ) {
aabb = laabb ;
} else {
aabb . merge_with ( laabb ) ;
}
}
return aabb ;
2019-08-18 22:40:52 +00:00
}
2021-01-25 15:20:11 +00:00
void RendererStorageRD : : mesh_set_shadow_mesh ( RID p_mesh , RID p_shadow_mesh ) {
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND ( ! mesh ) ;
Mesh * shadow_mesh = mesh_owner . getornull ( mesh - > shadow_mesh ) ;
if ( shadow_mesh ) {
shadow_mesh - > shadow_owners . erase ( mesh ) ;
}
mesh - > shadow_mesh = p_shadow_mesh ;
shadow_mesh = mesh_owner . getornull ( mesh - > shadow_mesh ) ;
if ( shadow_mesh ) {
shadow_mesh - > shadow_owners . insert ( mesh ) ;
}
mesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_MESH ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : mesh_clear ( RID p_mesh ) {
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND ( ! mesh ) ;
for ( uint32_t i = 0 ; i < mesh - > surface_count ; i + + ) {
Mesh : : Surface & s = * mesh - > surfaces [ i ] ;
RD : : get_singleton ( ) - > free ( s . vertex_buffer ) ; //clears arrays as dependency automatically, including all versions
2020-12-02 01:40:47 +00:00
if ( s . attribute_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( s . attribute_buffer ) ;
}
if ( s . skin_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( s . skin_buffer ) ;
}
2019-08-18 22:40:52 +00:00
if ( s . versions ) {
memfree ( s . versions ) ; //reallocs, so free with memfree.
}
if ( s . index_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( s . index_buffer ) ;
}
if ( s . lod_count ) {
for ( uint32_t j = 0 ; j < s . lod_count ; j + + ) {
RD : : get_singleton ( ) - > free ( s . lods [ j ] . index_buffer ) ;
}
memdelete_arr ( s . lods ) ;
}
2020-12-02 01:40:47 +00:00
if ( s . blend_shape_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( s . blend_shape_buffer ) ;
2019-08-18 22:40:52 +00:00
}
memdelete ( mesh - > surfaces [ i ] ) ;
}
if ( mesh - > surfaces ) {
memfree ( mesh - > surfaces ) ;
}
mesh - > surfaces = nullptr ;
mesh - > surface_count = 0 ;
mesh - > material_cache . clear ( ) ;
2020-12-16 14:07:08 +00:00
//clear instance data
for ( List < MeshInstance * > : : Element * E = mesh - > instances . front ( ) ; E ; E = E - > next ( ) ) {
MeshInstance * mi = E - > get ( ) ;
_mesh_instance_clear ( mi ) ;
}
mesh - > has_bone_weights = false ;
2021-01-04 12:33:25 +00:00
mesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_MESH ) ;
2021-01-25 15:20:11 +00:00
for ( Set < Mesh * > : : Element * E = mesh - > shadow_owners . front ( ) ; E ; E = E - > next ( ) ) {
Mesh * shadow_owner = E - > get ( ) ;
shadow_owner - > shadow_mesh = RID ( ) ;
shadow_owner - > dependency . changed_notify ( DEPENDENCY_CHANGED_MESH ) ;
}
2020-12-16 14:07:08 +00:00
}
bool RendererStorageRD : : mesh_needs_instance ( RID p_mesh , bool p_has_skeleton ) {
Mesh * mesh = mesh_owner . getornull ( p_mesh ) ;
ERR_FAIL_COND_V ( ! mesh , false ) ;
return mesh - > blend_shape_count > 0 | | ( mesh - > has_bone_weights & & p_has_skeleton ) ;
}
/* MESH INSTANCE */
RID RendererStorageRD : : mesh_instance_create ( RID p_base ) {
Mesh * mesh = mesh_owner . getornull ( p_base ) ;
ERR_FAIL_COND_V ( ! mesh , RID ( ) ) ;
MeshInstance * mi = memnew ( MeshInstance ) ;
mi - > mesh = mesh ;
for ( uint32_t i = 0 ; i < mesh - > surface_count ; i + + ) {
_mesh_instance_add_surface ( mi , mesh , i ) ;
}
mi - > I = mesh - > instances . push_back ( mi ) ;
mi - > dirty = true ;
return mesh_instance_owner . make_rid ( mi ) ;
}
void RendererStorageRD : : mesh_instance_set_skeleton ( RID p_mesh_instance , RID p_skeleton ) {
MeshInstance * mi = mesh_instance_owner . getornull ( p_mesh_instance ) ;
if ( mi - > skeleton = = p_skeleton ) {
return ;
}
mi - > skeleton = p_skeleton ;
mi - > skeleton_version = 0 ;
mi - > dirty = true ;
2019-08-18 22:40:52 +00:00
}
2020-12-16 14:07:08 +00:00
void RendererStorageRD : : mesh_instance_set_blend_shape_weight ( RID p_mesh_instance , int p_shape , float p_weight ) {
MeshInstance * mi = mesh_instance_owner . getornull ( p_mesh_instance ) ;
ERR_FAIL_COND ( ! mi ) ;
ERR_FAIL_INDEX ( p_shape , ( int ) mi - > blend_weights . size ( ) ) ;
mi - > blend_weights [ p_shape ] = p_weight ;
mi - > weights_dirty = true ;
//will be eventually updated
}
void RendererStorageRD : : _mesh_instance_clear ( MeshInstance * mi ) {
for ( uint32_t i = 0 ; i < mi - > surfaces . size ( ) ; i + + ) {
if ( mi - > surfaces [ i ] . vertex_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( mi - > surfaces [ i ] . vertex_buffer ) ;
}
if ( mi - > surfaces [ i ] . versions ) {
for ( uint32_t j = 0 ; j < mi - > surfaces [ i ] . version_count ; j + + ) {
RD : : get_singleton ( ) - > free ( mi - > surfaces [ i ] . versions [ j ] . vertex_array ) ;
}
memfree ( mi - > surfaces [ i ] . versions ) ;
}
}
mi - > surfaces . clear ( ) ;
if ( mi - > blend_weights_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( mi - > blend_weights_buffer ) ;
}
mi - > blend_weights . clear ( ) ;
mi - > weights_dirty = false ;
mi - > skeleton_version = 0 ;
}
void RendererStorageRD : : _mesh_instance_add_surface ( MeshInstance * mi , Mesh * mesh , uint32_t p_surface ) {
if ( mesh - > blend_shape_count > 0 & & mi - > blend_weights_buffer . is_null ( ) ) {
mi - > blend_weights . resize ( mesh - > blend_shape_count ) ;
for ( uint32_t i = 0 ; i < mi - > blend_weights . size ( ) ; i + + ) {
mi - > blend_weights [ i ] = 0 ;
}
mi - > blend_weights_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( float ) * mi - > blend_weights . size ( ) , mi - > blend_weights . to_byte_array ( ) ) ;
mi - > weights_dirty = true ;
}
2019-08-18 22:40:52 +00:00
2020-12-16 14:07:08 +00:00
MeshInstance : : Surface s ;
if ( mesh - > blend_shape_count > 0 | | ( mesh - > surfaces [ p_surface ] - > format & RS : : ARRAY_FORMAT_BONES ) ) {
//surface warrants transform
s . vertex_buffer = RD : : get_singleton ( ) - > vertex_buffer_create ( mesh - > surfaces [ p_surface ] - > vertex_buffer_size , Vector < uint8_t > ( ) , true ) ;
2019-08-18 22:40:52 +00:00
2020-12-16 14:07:08 +00:00
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 1 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . ids . push_back ( s . vertex_buffer ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . binding = 2 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
if ( mi - > blend_weights_buffer . is_valid ( ) ) {
u . ids . push_back ( mi - > blend_weights_buffer ) ;
} else {
u . ids . push_back ( default_rd_storage_buffer ) ;
}
uniforms . push_back ( u ) ;
}
s . uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , skeleton_shader . version_shader [ 0 ] , SkeletonShader : : UNIFORM_SET_INSTANCE ) ;
}
mi - > surfaces . push_back ( s ) ;
mi - > dirty = true ;
}
void RendererStorageRD : : mesh_instance_check_for_update ( RID p_mesh_instance ) {
MeshInstance * mi = mesh_instance_owner . getornull ( p_mesh_instance ) ;
bool needs_update = mi - > dirty ;
if ( mi - > weights_dirty & & ! mi - > weight_update_list . in_list ( ) ) {
dirty_mesh_instance_weights . add ( & mi - > weight_update_list ) ;
needs_update = true ;
}
if ( mi - > array_update_list . in_list ( ) ) {
return ;
}
if ( ! needs_update & & mi - > skeleton . is_valid ( ) ) {
Skeleton * sk = skeleton_owner . getornull ( mi - > skeleton ) ;
if ( sk & & sk - > version ! = mi - > skeleton_version ) {
needs_update = true ;
}
}
if ( needs_update ) {
dirty_mesh_instance_arrays . add ( & mi - > array_update_list ) ;
}
}
void RendererStorageRD : : update_mesh_instances ( ) {
while ( dirty_mesh_instance_weights . first ( ) ) {
MeshInstance * mi = dirty_mesh_instance_weights . first ( ) - > self ( ) ;
if ( mi - > blend_weights_buffer . is_valid ( ) ) {
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > buffer_update ( mi - > blend_weights_buffer , 0 , mi - > blend_weights . size ( ) * sizeof ( float ) , mi - > blend_weights . ptr ( ) ) ;
2020-12-16 14:07:08 +00:00
}
dirty_mesh_instance_weights . remove ( & mi - > weight_update_list ) ;
mi - > weights_dirty = false ;
}
if ( dirty_mesh_instance_arrays . first ( ) = = nullptr ) {
return ; //nothing to do
}
//process skeletons and blend shapes
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
while ( dirty_mesh_instance_arrays . first ( ) ) {
MeshInstance * mi = dirty_mesh_instance_arrays . first ( ) - > self ( ) ;
Skeleton * sk = skeleton_owner . getornull ( mi - > skeleton ) ;
for ( uint32_t i = 0 ; i < mi - > surfaces . size ( ) ; i + + ) {
if ( mi - > surfaces [ i ] . uniform_set = = RID ( ) | | mi - > mesh - > surfaces [ i ] - > uniform_set = = RID ( ) ) {
continue ;
}
bool array_is_2d = mi - > mesh - > surfaces [ i ] - > format & RS : : ARRAY_FLAG_USE_2D_VERTICES ;
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , skeleton_shader . pipeline [ array_is_2d ? SkeletonShader : : SHADER_MODE_2D : SkeletonShader : : SHADER_MODE_3D ] ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , mi - > surfaces [ i ] . uniform_set , SkeletonShader : : UNIFORM_SET_INSTANCE ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , mi - > mesh - > surfaces [ i ] - > uniform_set , SkeletonShader : : UNIFORM_SET_SURFACE ) ;
if ( sk & & sk - > uniform_set_mi . is_valid ( ) ) {
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , sk - > uniform_set_mi , SkeletonShader : : UNIFORM_SET_SKELETON ) ;
} else {
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , skeleton_shader . default_skeleton_uniform_set , SkeletonShader : : UNIFORM_SET_SKELETON ) ;
}
SkeletonShader : : PushConstant push_constant ;
push_constant . has_normal = mi - > mesh - > surfaces [ i ] - > format & RS : : ARRAY_FORMAT_NORMAL ;
push_constant . has_tangent = mi - > mesh - > surfaces [ i ] - > format & RS : : ARRAY_FORMAT_TANGENT ;
push_constant . has_skeleton = sk ! = nullptr & & sk - > use_2d = = array_is_2d & & ( mi - > mesh - > surfaces [ i ] - > format & RS : : ARRAY_FORMAT_BONES ) ;
push_constant . has_blend_shape = mi - > mesh - > blend_shape_count > 0 ;
push_constant . vertex_count = mi - > mesh - > surfaces [ i ] - > vertex_count ;
push_constant . vertex_stride = ( mi - > mesh - > surfaces [ i ] - > vertex_buffer_size / mi - > mesh - > surfaces [ i ] - > vertex_count ) / 4 ;
push_constant . skin_stride = ( mi - > mesh - > surfaces [ i ] - > skin_buffer_size / mi - > mesh - > surfaces [ i ] - > vertex_count ) / 4 ;
push_constant . skin_weight_offset = ( mi - > mesh - > surfaces [ i ] - > format & RS : : ARRAY_FLAG_USE_8_BONE_WEIGHTS ) ? 4 : 2 ;
push_constant . blend_shape_count = mi - > mesh - > blend_shape_count ;
push_constant . normalized_blend_shapes = mi - > mesh - > blend_shape_mode = = RS : : BLEND_SHAPE_MODE_NORMALIZED ;
push_constant . pad0 = 0 ;
push_constant . pad1 = 0 ;
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( SkeletonShader : : PushConstant ) ) ;
//dispatch without barrier, so all is done at the same time
2021-02-02 19:51:36 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , push_constant . vertex_count , 1 , 1 ) ;
2020-12-16 14:07:08 +00:00
}
mi - > dirty = false ;
if ( sk ) {
mi - > skeleton_version = sk - > version ;
}
dirty_mesh_instance_arrays . remove ( & mi - > array_update_list ) ;
}
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
void RendererStorageRD : : _mesh_surface_generate_version_for_input_mask ( Mesh : : Surface : : Version & v , Mesh : : Surface * s , uint32_t p_input_mask , MeshInstance : : Surface * mis ) {
2020-04-21 15:16:45 +00:00
Vector < RD : : VertexAttribute > attributes ;
2019-08-18 22:40:52 +00:00
Vector < RID > buffers ;
uint32_t stride = 0 ;
2020-12-02 01:40:47 +00:00
uint32_t attribute_stride = 0 ;
uint32_t skin_stride = 0 ;
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
for ( int i = 0 ; i < RS : : ARRAY_INDEX ; i + + ) {
2020-04-21 15:16:45 +00:00
RD : : VertexAttribute vd ;
2019-08-18 22:40:52 +00:00
RID buffer ;
vd . location = i ;
if ( ! ( s - > format & ( 1 < < i ) ) ) {
2019-08-20 20:54:03 +00:00
// Not supplied by surface, use default value
2019-08-18 22:40:52 +00:00
buffer = mesh_default_rd_buffers [ i ] ;
2020-12-02 01:40:47 +00:00
vd . stride = 0 ;
2019-08-18 22:40:52 +00:00
switch ( i ) {
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_VERTEX : {
2019-08-18 22:40:52 +00:00
vd . format = RD : : DATA_FORMAT_R32G32B32_SFLOAT ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_NORMAL : {
2019-08-18 22:40:52 +00:00
vd . format = RD : : DATA_FORMAT_R32G32B32_SFLOAT ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TANGENT : {
2019-08-18 22:40:52 +00:00
vd . format = RD : : DATA_FORMAT_R32G32B32A32_SFLOAT ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_COLOR : {
2019-08-18 22:40:52 +00:00
vd . format = RD : : DATA_FORMAT_R32G32B32A32_SFLOAT ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TEX_UV : {
2019-08-18 22:40:52 +00:00
vd . format = RD : : DATA_FORMAT_R32G32_SFLOAT ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TEX_UV2 : {
2019-08-18 22:40:52 +00:00
vd . format = RD : : DATA_FORMAT_R32G32_SFLOAT ;
} break ;
2020-12-02 01:40:47 +00:00
case RS : : ARRAY_CUSTOM0 :
case RS : : ARRAY_CUSTOM1 :
case RS : : ARRAY_CUSTOM2 :
case RS : : ARRAY_CUSTOM3 : {
//assumed weights too
vd . format = RD : : DATA_FORMAT_R32G32B32A32_SFLOAT ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_BONES : {
2019-08-18 22:40:52 +00:00
//assumed weights too
vd . format = RD : : DATA_FORMAT_R32G32B32A32_UINT ;
} break ;
2020-12-02 01:40:47 +00:00
case RS : : ARRAY_WEIGHTS : {
//assumed weights too
vd . format = RD : : DATA_FORMAT_R32G32B32A32_UINT ;
} break ;
2019-08-18 22:40:52 +00:00
}
} else {
2019-08-20 20:54:03 +00:00
//Supplied, use it
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
vd . stride = 1 ; //mark that it needs a stride set (default uses 0)
2019-08-18 22:40:52 +00:00
switch ( i ) {
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_VERTEX : {
2020-12-02 01:40:47 +00:00
vd . offset = stride ;
2020-03-27 18:21:27 +00:00
if ( s - > format & RS : : ARRAY_FLAG_USE_2D_VERTICES ) {
2019-08-18 22:40:52 +00:00
vd . format = RD : : DATA_FORMAT_R32G32_SFLOAT ;
stride + = sizeof ( float ) * 2 ;
} else {
vd . format = RD : : DATA_FORMAT_R32G32B32_SFLOAT ;
stride + = sizeof ( float ) * 3 ;
}
2020-12-16 14:07:08 +00:00
if ( mis ) {
buffer = mis - > vertex_buffer ;
} else {
buffer = s - > vertex_buffer ;
}
2020-12-02 01:40:47 +00:00
2019-08-18 22:40:52 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_NORMAL : {
2020-12-02 01:40:47 +00:00
vd . offset = stride ;
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
vd . format = RD : : DATA_FORMAT_A2B10G10R10_UNORM_PACK32 ;
stride + = sizeof ( uint32_t ) ;
2020-12-16 14:07:08 +00:00
if ( mis ) {
buffer = mis - > vertex_buffer ;
} else {
buffer = s - > vertex_buffer ;
}
2019-08-18 22:40:52 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TANGENT : {
2020-12-02 01:40:47 +00:00
vd . offset = stride ;
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
vd . format = RD : : DATA_FORMAT_A2B10G10R10_UNORM_PACK32 ;
stride + = sizeof ( uint32_t ) ;
2020-12-16 14:07:08 +00:00
if ( mis ) {
buffer = mis - > vertex_buffer ;
} else {
buffer = s - > vertex_buffer ;
}
2019-08-18 22:40:52 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_COLOR : {
2020-12-02 01:40:47 +00:00
vd . offset = attribute_stride ;
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
vd . format = RD : : DATA_FORMAT_R16G16B16A16_SFLOAT ;
attribute_stride + = sizeof ( int16_t ) * 4 ;
buffer = s - > attribute_buffer ;
2019-08-18 22:40:52 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TEX_UV : {
2020-12-02 01:40:47 +00:00
vd . offset = attribute_stride ;
vd . format = RD : : DATA_FORMAT_R32G32_SFLOAT ;
attribute_stride + = sizeof ( float ) * 2 ;
buffer = s - > attribute_buffer ;
2019-08-18 22:40:52 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_TEX_UV2 : {
2020-12-02 01:40:47 +00:00
vd . offset = attribute_stride ;
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
vd . format = RD : : DATA_FORMAT_R32G32_SFLOAT ;
attribute_stride + = sizeof ( float ) * 2 ;
buffer = s - > attribute_buffer ;
} break ;
case RS : : ARRAY_CUSTOM0 :
case RS : : ARRAY_CUSTOM1 :
case RS : : ARRAY_CUSTOM2 :
case RS : : ARRAY_CUSTOM3 : {
vd . offset = attribute_stride ;
int idx = i - RS : : ARRAY_CUSTOM0 ;
uint32_t fmt_shift [ RS : : ARRAY_CUSTOM_COUNT ] = { RS : : ARRAY_FORMAT_CUSTOM0_SHIFT , RS : : ARRAY_FORMAT_CUSTOM1_SHIFT , RS : : ARRAY_FORMAT_CUSTOM2_SHIFT , RS : : ARRAY_FORMAT_CUSTOM3_SHIFT } ;
uint32_t fmt = ( s - > format > > fmt_shift [ idx ] ) & RS : : ARRAY_FORMAT_CUSTOM_MASK ;
uint32_t fmtsize [ RS : : ARRAY_CUSTOM_MAX ] = { 4 , 4 , 4 , 8 , 4 , 8 , 12 , 16 } ;
RD : : DataFormat fmtrd [ RS : : ARRAY_CUSTOM_MAX ] = { RD : : DATA_FORMAT_R8G8B8A8_UNORM , RD : : DATA_FORMAT_R8G8B8A8_SNORM , RD : : DATA_FORMAT_R16G16_SFLOAT , RD : : DATA_FORMAT_R16G16B16A16_SFLOAT , RD : : DATA_FORMAT_R32_SFLOAT , RD : : DATA_FORMAT_R32G32_SFLOAT , RD : : DATA_FORMAT_R32G32B32_SFLOAT , RD : : DATA_FORMAT_R32G32B32A32_SFLOAT } ;
vd . format = fmtrd [ fmt ] ;
attribute_stride + = fmtsize [ fmt ] ;
buffer = s - > attribute_buffer ;
2019-08-18 22:40:52 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : ARRAY_BONES : {
2020-12-02 01:40:47 +00:00
vd . offset = skin_stride ;
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
vd . format = RD : : DATA_FORMAT_R16G16B16A16_UINT ;
skin_stride + = sizeof ( int16_t ) * 4 ;
buffer = s - > skin_buffer ;
} break ;
case RS : : ARRAY_WEIGHTS : {
vd . offset = skin_stride ;
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
vd . format = RD : : DATA_FORMAT_R16G16B16A16_UNORM ;
skin_stride + = sizeof ( int16_t ) * 4 ;
buffer = s - > skin_buffer ;
2019-08-18 22:40:52 +00:00
} break ;
}
}
2019-08-20 20:54:03 +00:00
if ( ! ( p_input_mask & ( 1 < < i ) ) ) {
2020-12-02 01:40:47 +00:00
continue ; // Shader does not need this, skip it (but computing stride was important anyway)
2019-08-20 20:54:03 +00:00
}
2019-08-18 22:40:52 +00:00
attributes . push_back ( vd ) ;
buffers . push_back ( buffer ) ;
}
//update final stride
for ( int i = 0 ; i < attributes . size ( ) ; i + + ) {
2020-12-02 01:40:47 +00:00
if ( attributes [ i ] . stride = = 0 ) {
continue ; //default location
}
int loc = attributes [ i ] . location ;
if ( loc < RS : : ARRAY_COLOR ) {
2019-08-18 22:40:52 +00:00
attributes . write [ i ] . stride = stride ;
2020-12-02 01:40:47 +00:00
} else if ( loc < RS : : ARRAY_BONES ) {
attributes . write [ i ] . stride = attribute_stride ;
} else {
attributes . write [ i ] . stride = skin_stride ;
2019-08-18 22:40:52 +00:00
}
}
v . input_mask = p_input_mask ;
v . vertex_format = RD : : get_singleton ( ) - > vertex_format_create ( attributes ) ;
v . vertex_array = RD : : get_singleton ( ) - > vertex_array_create ( s - > vertex_count , v . vertex_format , buffers ) ;
}
2019-07-21 14:31:30 +00:00
2019-09-14 03:37:42 +00:00
////////////////// MULTIMESH
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : multimesh_allocate ( ) {
return multimesh_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : multimesh_initialize ( RID p_rid ) {
multimesh_owner . initialize_rid ( p_rid , MultiMesh ( ) ) ;
2019-09-14 03:37:42 +00:00
}
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : multimesh_allocate_data ( RID p_multimesh , int p_instances , RS : : MultimeshTransformFormat p_transform_format , bool p_use_colors , bool p_use_custom_data ) {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND ( ! multimesh ) ;
if ( multimesh - > instances = = p_instances & & multimesh - > xform_format = = p_transform_format & & multimesh - > uses_colors = = p_use_colors & & multimesh - > uses_custom_data = = p_use_custom_data ) {
return ;
}
if ( multimesh - > buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( multimesh - > buffer ) ;
multimesh - > buffer = RID ( ) ;
multimesh - > uniform_set_3d = RID ( ) ; //cleared by dependency
}
if ( multimesh - > data_cache_dirty_regions ) {
memdelete_arr ( multimesh - > data_cache_dirty_regions ) ;
multimesh - > data_cache_dirty_regions = nullptr ;
multimesh - > data_cache_used_dirty_regions = 0 ;
}
multimesh - > instances = p_instances ;
multimesh - > xform_format = p_transform_format ;
multimesh - > uses_colors = p_use_colors ;
2020-03-27 18:21:27 +00:00
multimesh - > color_offset_cache = p_transform_format = = RS : : MULTIMESH_TRANSFORM_2D ? 8 : 12 ;
2019-09-14 03:37:42 +00:00
multimesh - > uses_custom_data = p_use_custom_data ;
multimesh - > custom_data_offset_cache = multimesh - > color_offset_cache + ( p_use_colors ? 4 : 0 ) ;
multimesh - > stride_cache = multimesh - > custom_data_offset_cache + ( p_use_custom_data ? 4 : 0 ) ;
multimesh - > buffer_set = false ;
2020-03-27 18:21:27 +00:00
//print_line("allocate, elements: " + itos(p_instances) + " 2D: " + itos(p_transform_format == RS::MULTIMESH_TRANSFORM_2D) + " colors " + itos(multimesh->uses_colors) + " data " + itos(multimesh->uses_custom_data) + " stride " + itos(multimesh->stride_cache) + " total size " + itos(multimesh->stride_cache * multimesh->instances));
2020-02-17 21:06:54 +00:00
multimesh - > data_cache = Vector < float > ( ) ;
2019-09-14 03:37:42 +00:00
multimesh - > aabb = AABB ( ) ;
multimesh - > aabb_dirty = false ;
multimesh - > visible_instances = MIN ( multimesh - > visible_instances , multimesh - > instances ) ;
if ( multimesh - > instances ) {
2019-09-23 19:01:05 +00:00
multimesh - > buffer = RD : : get_singleton ( ) - > storage_buffer_create ( multimesh - > instances * multimesh - > stride_cache * 4 ) ;
2019-09-14 03:37:42 +00:00
}
2021-01-04 12:33:25 +00:00
multimesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_MULTIMESH ) ;
2019-09-14 03:37:42 +00:00
}
2020-12-04 18:26:24 +00:00
int RendererStorageRD : : multimesh_get_instance_count ( RID p_multimesh ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND_V ( ! multimesh , 0 ) ;
return multimesh - > instances ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : multimesh_set_mesh ( RID p_multimesh , RID p_mesh ) {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND ( ! multimesh ) ;
if ( multimesh - > mesh = = p_mesh ) {
return ;
}
multimesh - > mesh = p_mesh ;
if ( multimesh - > instances = = 0 ) {
return ;
}
if ( multimesh - > data_cache . size ( ) ) {
//we have a data cache, just mark it dirt
_multimesh_mark_all_dirty ( multimesh , false , true ) ;
} else if ( multimesh - > instances ) {
//need to re-create AABB unfortunately, calling this has a penalty
2019-09-23 19:01:05 +00:00
if ( multimesh - > buffer_set ) {
2020-02-17 21:06:54 +00:00
Vector < uint8_t > buffer = RD : : get_singleton ( ) - > buffer_get_data ( multimesh - > buffer ) ;
const uint8_t * r = buffer . ptr ( ) ;
const float * data = ( const float * ) r ;
2019-09-14 03:37:42 +00:00
_multimesh_re_create_aabb ( multimesh , data , multimesh - > instances ) ;
}
}
2021-01-04 12:33:25 +00:00
multimesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_MESH ) ;
2019-09-14 03:37:42 +00:00
}
# define MULTIMESH_DIRTY_REGION_SIZE 512
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _multimesh_make_local ( MultiMesh * multimesh ) const {
2019-09-14 03:37:42 +00:00
if ( multimesh - > data_cache . size ( ) > 0 ) {
return ; //already local
}
ERR_FAIL_COND ( multimesh - > data_cache . size ( ) > 0 ) ;
// this means that the user wants to load/save individual elements,
// for this, the data must reside on CPU, so just copy it there.
multimesh - > data_cache . resize ( multimesh - > instances * multimesh - > stride_cache ) ;
{
2020-02-17 21:06:54 +00:00
float * w = multimesh - > data_cache . ptrw ( ) ;
2019-09-14 03:37:42 +00:00
if ( multimesh - > buffer_set ) {
2020-02-17 21:06:54 +00:00
Vector < uint8_t > buffer = RD : : get_singleton ( ) - > buffer_get_data ( multimesh - > buffer ) ;
2019-09-14 03:37:42 +00:00
{
2020-02-17 21:06:54 +00:00
const uint8_t * r = buffer . ptr ( ) ;
2021-04-27 14:19:21 +00:00
memcpy ( w , r , buffer . size ( ) ) ;
2019-09-14 03:37:42 +00:00
}
} else {
2021-04-27 14:19:21 +00:00
memset ( w , 0 , multimesh - > instances * multimesh - > stride_cache * sizeof ( float ) ) ;
2019-09-14 03:37:42 +00:00
}
}
uint32_t data_cache_dirty_region_count = ( multimesh - > instances - 1 ) / MULTIMESH_DIRTY_REGION_SIZE + 1 ;
multimesh - > data_cache_dirty_regions = memnew_arr ( bool , data_cache_dirty_region_count ) ;
for ( uint32_t i = 0 ; i < data_cache_dirty_region_count ; i + + ) {
2020-05-14 09:00:19 +00:00
multimesh - > data_cache_dirty_regions [ i ] = false ;
2019-09-14 03:37:42 +00:00
}
multimesh - > data_cache_used_dirty_regions = 0 ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _multimesh_mark_dirty ( MultiMesh * multimesh , int p_index , bool p_aabb ) {
2019-09-14 03:37:42 +00:00
uint32_t region_index = p_index / MULTIMESH_DIRTY_REGION_SIZE ;
# ifdef DEBUG_ENABLED
uint32_t data_cache_dirty_region_count = ( multimesh - > instances - 1 ) / MULTIMESH_DIRTY_REGION_SIZE + 1 ;
2020-02-13 14:53:32 +00:00
ERR_FAIL_UNSIGNED_INDEX ( region_index , data_cache_dirty_region_count ) ; //bug
2019-09-14 03:37:42 +00:00
# endif
if ( ! multimesh - > data_cache_dirty_regions [ region_index ] ) {
2020-02-14 16:48:48 +00:00
multimesh - > data_cache_dirty_regions [ region_index ] = true ;
2019-09-14 03:37:42 +00:00
multimesh - > data_cache_used_dirty_regions + + ;
}
if ( p_aabb ) {
multimesh - > aabb_dirty = true ;
}
if ( ! multimesh - > dirty ) {
multimesh - > dirty_list = multimesh_dirty_list ;
multimesh_dirty_list = multimesh ;
multimesh - > dirty = true ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _multimesh_mark_all_dirty ( MultiMesh * multimesh , bool p_data , bool p_aabb ) {
2019-09-14 03:37:42 +00:00
if ( p_data ) {
uint32_t data_cache_dirty_region_count = ( multimesh - > instances - 1 ) / MULTIMESH_DIRTY_REGION_SIZE + 1 ;
for ( uint32_t i = 0 ; i < data_cache_dirty_region_count ; i + + ) {
if ( ! multimesh - > data_cache_dirty_regions [ i ] ) {
multimesh - > data_cache_dirty_regions [ i ] = true ;
multimesh - > data_cache_used_dirty_regions + + ;
}
}
}
if ( p_aabb ) {
multimesh - > aabb_dirty = true ;
}
if ( ! multimesh - > dirty ) {
multimesh - > dirty_list = multimesh_dirty_list ;
multimesh_dirty_list = multimesh ;
multimesh - > dirty = true ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _multimesh_re_create_aabb ( MultiMesh * multimesh , const float * p_data , int p_instances ) {
2019-09-14 03:37:42 +00:00
ERR_FAIL_COND ( multimesh - > mesh . is_null ( ) ) ;
AABB aabb ;
AABB mesh_aabb = mesh_get_aabb ( multimesh - > mesh ) ;
for ( int i = 0 ; i < p_instances ; i + + ) {
const float * data = p_data + multimesh - > stride_cache * i ;
2020-10-17 05:08:21 +00:00
Transform3D t ;
2019-09-14 03:37:42 +00:00
2020-03-27 18:21:27 +00:00
if ( multimesh - > xform_format = = RS : : MULTIMESH_TRANSFORM_3D ) {
2019-09-23 19:01:05 +00:00
t . basis . elements [ 0 ] [ 0 ] = data [ 0 ] ;
t . basis . elements [ 0 ] [ 1 ] = data [ 1 ] ;
t . basis . elements [ 0 ] [ 2 ] = data [ 2 ] ;
t . origin . x = data [ 3 ] ;
t . basis . elements [ 1 ] [ 0 ] = data [ 4 ] ;
t . basis . elements [ 1 ] [ 1 ] = data [ 5 ] ;
t . basis . elements [ 1 ] [ 2 ] = data [ 6 ] ;
t . origin . y = data [ 7 ] ;
t . basis . elements [ 2 ] [ 0 ] = data [ 8 ] ;
t . basis . elements [ 2 ] [ 1 ] = data [ 9 ] ;
t . basis . elements [ 2 ] [ 2 ] = data [ 10 ] ;
2019-09-14 03:37:42 +00:00
t . origin . z = data [ 11 ] ;
} else {
t . basis . elements [ 0 ] . x = data [ 0 ] ;
t . basis . elements [ 1 ] . x = data [ 1 ] ;
t . origin . x = data [ 3 ] ;
t . basis . elements [ 0 ] . y = data [ 4 ] ;
t . basis . elements [ 1 ] . y = data [ 5 ] ;
t . origin . y = data [ 7 ] ;
}
if ( i = = 0 ) {
aabb = t . xform ( mesh_aabb ) ;
} else {
aabb . merge_with ( t . xform ( mesh_aabb ) ) ;
}
}
multimesh - > aabb = aabb ;
}
2020-10-17 05:08:21 +00:00
void RendererStorageRD : : multimesh_instance_set_transform ( RID p_multimesh , int p_index , const Transform3D & p_transform ) {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND ( ! multimesh ) ;
ERR_FAIL_INDEX ( p_index , multimesh - > instances ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND ( multimesh - > xform_format ! = RS : : MULTIMESH_TRANSFORM_3D ) ;
2019-09-14 03:37:42 +00:00
_multimesh_make_local ( multimesh ) ;
{
2020-02-17 21:06:54 +00:00
float * w = multimesh - > data_cache . ptrw ( ) ;
2019-09-14 03:37:42 +00:00
2020-02-17 21:06:54 +00:00
float * dataptr = w + p_index * multimesh - > stride_cache ;
2019-09-14 03:37:42 +00:00
2019-09-23 19:01:05 +00:00
dataptr [ 0 ] = p_transform . basis . elements [ 0 ] [ 0 ] ;
dataptr [ 1 ] = p_transform . basis . elements [ 0 ] [ 1 ] ;
dataptr [ 2 ] = p_transform . basis . elements [ 0 ] [ 2 ] ;
dataptr [ 3 ] = p_transform . origin . x ;
dataptr [ 4 ] = p_transform . basis . elements [ 1 ] [ 0 ] ;
dataptr [ 5 ] = p_transform . basis . elements [ 1 ] [ 1 ] ;
dataptr [ 6 ] = p_transform . basis . elements [ 1 ] [ 2 ] ;
dataptr [ 7 ] = p_transform . origin . y ;
dataptr [ 8 ] = p_transform . basis . elements [ 2 ] [ 0 ] ;
dataptr [ 9 ] = p_transform . basis . elements [ 2 ] [ 1 ] ;
dataptr [ 10 ] = p_transform . basis . elements [ 2 ] [ 2 ] ;
2019-09-14 03:37:42 +00:00
dataptr [ 11 ] = p_transform . origin . z ;
}
_multimesh_mark_dirty ( multimesh , p_index , true ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : multimesh_instance_set_transform_2d ( RID p_multimesh , int p_index , const Transform2D & p_transform ) {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND ( ! multimesh ) ;
ERR_FAIL_INDEX ( p_index , multimesh - > instances ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND ( multimesh - > xform_format ! = RS : : MULTIMESH_TRANSFORM_2D ) ;
2019-09-14 03:37:42 +00:00
_multimesh_make_local ( multimesh ) ;
{
2020-02-17 21:06:54 +00:00
float * w = multimesh - > data_cache . ptrw ( ) ;
2019-09-14 03:37:42 +00:00
2020-02-17 21:06:54 +00:00
float * dataptr = w + p_index * multimesh - > stride_cache ;
2019-09-14 03:37:42 +00:00
2019-09-23 19:01:05 +00:00
dataptr [ 0 ] = p_transform . elements [ 0 ] [ 0 ] ;
dataptr [ 1 ] = p_transform . elements [ 1 ] [ 0 ] ;
2019-09-14 03:37:42 +00:00
dataptr [ 2 ] = 0 ;
2019-09-23 19:01:05 +00:00
dataptr [ 3 ] = p_transform . elements [ 2 ] [ 0 ] ;
dataptr [ 4 ] = p_transform . elements [ 0 ] [ 1 ] ;
dataptr [ 5 ] = p_transform . elements [ 1 ] [ 1 ] ;
2019-09-14 03:37:42 +00:00
dataptr [ 6 ] = 0 ;
2019-09-23 19:01:05 +00:00
dataptr [ 7 ] = p_transform . elements [ 2 ] [ 1 ] ;
2019-09-14 03:37:42 +00:00
}
_multimesh_mark_dirty ( multimesh , p_index , true ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : multimesh_instance_set_color ( RID p_multimesh , int p_index , const Color & p_color ) {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND ( ! multimesh ) ;
ERR_FAIL_INDEX ( p_index , multimesh - > instances ) ;
2019-12-20 10:18:51 +00:00
ERR_FAIL_COND ( ! multimesh - > uses_colors ) ;
2019-09-14 03:37:42 +00:00
_multimesh_make_local ( multimesh ) ;
{
2020-02-17 21:06:54 +00:00
float * w = multimesh - > data_cache . ptrw ( ) ;
2019-09-14 03:37:42 +00:00
2020-02-17 21:06:54 +00:00
float * dataptr = w + p_index * multimesh - > stride_cache + multimesh - > color_offset_cache ;
2019-09-14 03:37:42 +00:00
dataptr [ 0 ] = p_color . r ;
dataptr [ 1 ] = p_color . g ;
dataptr [ 2 ] = p_color . b ;
dataptr [ 3 ] = p_color . a ;
}
_multimesh_mark_dirty ( multimesh , p_index , false ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : multimesh_instance_set_custom_data ( RID p_multimesh , int p_index , const Color & p_color ) {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND ( ! multimesh ) ;
ERR_FAIL_INDEX ( p_index , multimesh - > instances ) ;
2020-02-18 13:57:11 +00:00
ERR_FAIL_COND ( ! multimesh - > uses_custom_data ) ;
2019-09-14 03:37:42 +00:00
_multimesh_make_local ( multimesh ) ;
{
2020-02-17 21:06:54 +00:00
float * w = multimesh - > data_cache . ptrw ( ) ;
2019-09-14 03:37:42 +00:00
2020-02-17 21:06:54 +00:00
float * dataptr = w + p_index * multimesh - > stride_cache + multimesh - > custom_data_offset_cache ;
2019-09-14 03:37:42 +00:00
dataptr [ 0 ] = p_color . r ;
dataptr [ 1 ] = p_color . g ;
dataptr [ 2 ] = p_color . b ;
dataptr [ 3 ] = p_color . a ;
}
_multimesh_mark_dirty ( multimesh , p_index , false ) ;
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : multimesh_get_mesh ( RID p_multimesh ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND_V ( ! multimesh , RID ( ) ) ;
return multimesh - > mesh ;
}
2020-10-17 05:08:21 +00:00
Transform3D RendererStorageRD : : multimesh_instance_get_transform ( RID p_multimesh , int p_index ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
2020-10-17 05:08:21 +00:00
ERR_FAIL_COND_V ( ! multimesh , Transform3D ( ) ) ;
ERR_FAIL_INDEX_V ( p_index , multimesh - > instances , Transform3D ( ) ) ;
ERR_FAIL_COND_V ( multimesh - > xform_format ! = RS : : MULTIMESH_TRANSFORM_3D , Transform3D ( ) ) ;
2019-09-14 03:37:42 +00:00
_multimesh_make_local ( multimesh ) ;
2020-10-17 05:08:21 +00:00
Transform3D t ;
2019-09-14 03:37:42 +00:00
{
2020-02-17 21:06:54 +00:00
const float * r = multimesh - > data_cache . ptr ( ) ;
2019-09-14 03:37:42 +00:00
2020-02-17 21:06:54 +00:00
const float * dataptr = r + p_index * multimesh - > stride_cache ;
2019-09-14 03:37:42 +00:00
2019-09-23 19:01:05 +00:00
t . basis . elements [ 0 ] [ 0 ] = dataptr [ 0 ] ;
t . basis . elements [ 0 ] [ 1 ] = dataptr [ 1 ] ;
t . basis . elements [ 0 ] [ 2 ] = dataptr [ 2 ] ;
t . origin . x = dataptr [ 3 ] ;
t . basis . elements [ 1 ] [ 0 ] = dataptr [ 4 ] ;
t . basis . elements [ 1 ] [ 1 ] = dataptr [ 5 ] ;
t . basis . elements [ 1 ] [ 2 ] = dataptr [ 6 ] ;
t . origin . y = dataptr [ 7 ] ;
t . basis . elements [ 2 ] [ 0 ] = dataptr [ 8 ] ;
t . basis . elements [ 2 ] [ 1 ] = dataptr [ 9 ] ;
t . basis . elements [ 2 ] [ 2 ] = dataptr [ 10 ] ;
2019-09-14 03:37:42 +00:00
t . origin . z = dataptr [ 11 ] ;
}
return t ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Transform2D RendererStorageRD : : multimesh_instance_get_transform_2d ( RID p_multimesh , int p_index ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND_V ( ! multimesh , Transform2D ( ) ) ;
ERR_FAIL_INDEX_V ( p_index , multimesh - > instances , Transform2D ( ) ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND_V ( multimesh - > xform_format ! = RS : : MULTIMESH_TRANSFORM_2D , Transform2D ( ) ) ;
2019-09-14 03:37:42 +00:00
_multimesh_make_local ( multimesh ) ;
Transform2D t ;
{
2020-02-17 21:06:54 +00:00
const float * r = multimesh - > data_cache . ptr ( ) ;
2019-09-14 03:37:42 +00:00
2020-02-17 21:06:54 +00:00
const float * dataptr = r + p_index * multimesh - > stride_cache ;
2019-09-14 03:37:42 +00:00
2019-09-23 19:01:05 +00:00
t . elements [ 0 ] [ 0 ] = dataptr [ 0 ] ;
t . elements [ 1 ] [ 0 ] = dataptr [ 1 ] ;
t . elements [ 2 ] [ 0 ] = dataptr [ 3 ] ;
t . elements [ 0 ] [ 1 ] = dataptr [ 4 ] ;
t . elements [ 1 ] [ 1 ] = dataptr [ 5 ] ;
t . elements [ 2 ] [ 1 ] = dataptr [ 7 ] ;
2019-09-14 03:37:42 +00:00
}
return t ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Color RendererStorageRD : : multimesh_instance_get_color ( RID p_multimesh , int p_index ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND_V ( ! multimesh , Color ( ) ) ;
ERR_FAIL_INDEX_V ( p_index , multimesh - > instances , Color ( ) ) ;
2020-02-18 13:57:11 +00:00
ERR_FAIL_COND_V ( ! multimesh - > uses_colors , Color ( ) ) ;
2019-09-14 03:37:42 +00:00
_multimesh_make_local ( multimesh ) ;
Color c ;
{
2020-02-17 21:06:54 +00:00
const float * r = multimesh - > data_cache . ptr ( ) ;
2019-09-14 03:37:42 +00:00
2020-02-17 21:06:54 +00:00
const float * dataptr = r + p_index * multimesh - > stride_cache + multimesh - > color_offset_cache ;
2019-09-14 03:37:42 +00:00
c . r = dataptr [ 0 ] ;
c . g = dataptr [ 1 ] ;
c . b = dataptr [ 2 ] ;
c . a = dataptr [ 3 ] ;
}
return c ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Color RendererStorageRD : : multimesh_instance_get_custom_data ( RID p_multimesh , int p_index ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND_V ( ! multimesh , Color ( ) ) ;
ERR_FAIL_INDEX_V ( p_index , multimesh - > instances , Color ( ) ) ;
2020-02-18 13:57:11 +00:00
ERR_FAIL_COND_V ( ! multimesh - > uses_custom_data , Color ( ) ) ;
2019-09-14 03:37:42 +00:00
_multimesh_make_local ( multimesh ) ;
Color c ;
{
2020-02-17 21:06:54 +00:00
const float * r = multimesh - > data_cache . ptr ( ) ;
2019-09-14 03:37:42 +00:00
2020-02-17 21:06:54 +00:00
const float * dataptr = r + p_index * multimesh - > stride_cache + multimesh - > custom_data_offset_cache ;
2019-09-14 03:37:42 +00:00
c . r = dataptr [ 0 ] ;
c . g = dataptr [ 1 ] ;
c . b = dataptr [ 2 ] ;
c . a = dataptr [ 3 ] ;
}
return c ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : multimesh_set_buffer ( RID p_multimesh , const Vector < float > & p_buffer ) {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND ( ! multimesh ) ;
ERR_FAIL_COND ( p_buffer . size ( ) ! = ( multimesh - > instances * ( int ) multimesh - > stride_cache ) ) ;
{
2020-02-17 21:06:54 +00:00
const float * r = p_buffer . ptr ( ) ;
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > buffer_update ( multimesh - > buffer , 0 , p_buffer . size ( ) * sizeof ( float ) , r ) ;
2019-09-14 03:37:42 +00:00
multimesh - > buffer_set = true ;
}
if ( multimesh - > data_cache . size ( ) ) {
//if we have a data cache, just update it
multimesh - > data_cache = p_buffer ;
{
//clear dirty since nothing will be dirty anymore
uint32_t data_cache_dirty_region_count = ( multimesh - > instances - 1 ) / MULTIMESH_DIRTY_REGION_SIZE + 1 ;
for ( uint32_t i = 0 ; i < data_cache_dirty_region_count ; i + + ) {
multimesh - > data_cache_dirty_regions [ i ] = false ;
}
multimesh - > data_cache_used_dirty_regions = 0 ;
}
_multimesh_mark_all_dirty ( multimesh , false , true ) ; //update AABB
} else if ( multimesh - > mesh . is_valid ( ) ) {
//if we have a mesh set, we need to re-generate the AABB from the new data
2020-02-17 21:06:54 +00:00
const float * data = p_buffer . ptr ( ) ;
2019-09-14 03:37:42 +00:00
_multimesh_re_create_aabb ( multimesh , data , multimesh - > instances ) ;
2021-01-04 12:33:25 +00:00
multimesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2019-09-14 03:37:42 +00:00
}
}
2020-12-04 18:26:24 +00:00
Vector < float > RendererStorageRD : : multimesh_get_buffer ( RID p_multimesh ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
2020-02-17 21:06:54 +00:00
ERR_FAIL_COND_V ( ! multimesh , Vector < float > ( ) ) ;
2019-09-14 03:37:42 +00:00
if ( multimesh - > buffer . is_null ( ) ) {
2020-02-17 21:06:54 +00:00
return Vector < float > ( ) ;
2019-09-14 03:37:42 +00:00
} else if ( multimesh - > data_cache . size ( ) ) {
return multimesh - > data_cache ;
} else {
//get from memory
2020-02-17 21:06:54 +00:00
Vector < uint8_t > buffer = RD : : get_singleton ( ) - > buffer_get_data ( multimesh - > buffer ) ;
Vector < float > ret ;
2020-02-21 00:51:57 +00:00
ret . resize ( multimesh - > instances * multimesh - > stride_cache ) ;
2019-09-14 03:37:42 +00:00
{
2020-02-21 00:51:57 +00:00
float * w = ret . ptrw ( ) ;
2020-02-17 21:06:54 +00:00
const uint8_t * r = buffer . ptr ( ) ;
2021-04-27 14:19:21 +00:00
memcpy ( w , r , buffer . size ( ) ) ;
2019-09-14 03:37:42 +00:00
}
return ret ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : multimesh_set_visible_instances ( RID p_multimesh , int p_visible ) {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND ( ! multimesh ) ;
ERR_FAIL_COND ( p_visible < - 1 | | p_visible > multimesh - > instances ) ;
if ( multimesh - > visible_instances = = p_visible ) {
return ;
}
if ( multimesh - > data_cache . size ( ) ) {
//there is a data cache..
_multimesh_mark_all_dirty ( multimesh , false , true ) ;
}
multimesh - > visible_instances = p_visible ;
2021-01-04 12:33:25 +00:00
multimesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES ) ;
2019-09-14 03:37:42 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
int RendererStorageRD : : multimesh_get_visible_instances ( RID p_multimesh ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND_V ( ! multimesh , 0 ) ;
return multimesh - > visible_instances ;
}
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : multimesh_get_aabb ( RID p_multimesh ) const {
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_multimesh ) ;
ERR_FAIL_COND_V ( ! multimesh , AABB ( ) ) ;
if ( multimesh - > aabb_dirty ) {
2020-12-04 18:26:24 +00:00
const_cast < RendererStorageRD * > ( this ) - > _update_dirty_multimeshes ( ) ;
2019-09-14 03:37:42 +00:00
}
return multimesh - > aabb ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _update_dirty_multimeshes ( ) {
2019-09-14 03:37:42 +00:00
while ( multimesh_dirty_list ) {
MultiMesh * multimesh = multimesh_dirty_list ;
if ( multimesh - > data_cache . size ( ) ) { //may have been cleared, so only process if it exists
2020-02-17 21:06:54 +00:00
const float * data = multimesh - > data_cache . ptr ( ) ;
2019-09-14 03:37:42 +00:00
uint32_t visible_instances = multimesh - > visible_instances > = 0 ? multimesh - > visible_instances : multimesh - > instances ;
if ( multimesh - > data_cache_used_dirty_regions ) {
uint32_t data_cache_dirty_region_count = ( multimesh - > instances - 1 ) / MULTIMESH_DIRTY_REGION_SIZE + 1 ;
2021-06-14 05:19:40 +00:00
uint32_t visible_region_count = visible_instances = = 0 ? 0 : ( visible_instances - 1 ) / MULTIMESH_DIRTY_REGION_SIZE + 1 ;
2019-09-14 03:37:42 +00:00
uint32_t region_size = multimesh - > stride_cache * MULTIMESH_DIRTY_REGION_SIZE * sizeof ( float ) ;
if ( multimesh - > data_cache_used_dirty_regions > 32 | | multimesh - > data_cache_used_dirty_regions > visible_region_count / 2 ) {
//if there too many dirty regions, or represent the majority of regions, just copy all, else transfer cost piles up too much
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > buffer_update ( multimesh - > buffer , 0 , MIN ( visible_region_count * region_size , multimesh - > instances * multimesh - > stride_cache * sizeof ( float ) ) , data ) ;
2019-09-14 03:37:42 +00:00
} else {
//not that many regions? update them all
for ( uint32_t i = 0 ; i < visible_region_count ; i + + ) {
if ( multimesh - > data_cache_dirty_regions [ i ] ) {
uint64_t offset = i * region_size ;
2019-09-23 19:01:05 +00:00
uint64_t size = multimesh - > stride_cache * multimesh - > instances * sizeof ( float ) ;
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > buffer_update ( multimesh - > buffer , offset , MIN ( region_size , size - offset ) , & data [ i * region_size ] ) ;
2019-09-14 03:37:42 +00:00
}
}
}
for ( uint32_t i = 0 ; i < data_cache_dirty_region_count ; i + + ) {
multimesh - > data_cache_dirty_regions [ i ] = false ;
}
multimesh - > data_cache_used_dirty_regions = 0 ;
}
if ( multimesh - > aabb_dirty ) {
//aabb is dirty..
_multimesh_re_create_aabb ( multimesh , data , visible_instances ) ;
multimesh - > aabb_dirty = false ;
2021-01-04 12:33:25 +00:00
multimesh - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2019-09-14 03:37:42 +00:00
}
}
multimesh_dirty_list = multimesh - > dirty_list ;
multimesh - > dirty_list = nullptr ;
multimesh - > dirty = false ;
}
multimesh_dirty_list = nullptr ;
}
2020-08-19 13:38:24 +00:00
/* PARTICLES */
2019-09-23 21:53:05 +00:00
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : particles_allocate ( ) {
return particles_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : particles_initialize ( RID p_rid ) {
particles_owner . initialize_rid ( p_rid , Particles ( ) ) ;
2020-08-19 13:38:24 +00:00
}
2021-05-10 16:12:44 +00:00
void RendererStorageRD : : particles_set_mode ( RID p_particles , RS : : ParticlesMode p_mode ) {
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
if ( particles - > mode = = p_mode ) {
return ;
}
_particles_free_data ( particles ) ;
particles - > mode = p_mode ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_emitting ( RID p_particles , bool p_emitting ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > emitting = p_emitting ;
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : particles_get_emitting ( RID p_particles ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND_V ( ! particles , false ) ;
return particles - > emitting ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _particles_free_data ( Particles * particles ) {
2021-04-27 15:43:49 +00:00
if ( particles - > particle_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( particles - > particle_buffer ) ;
particles - > particle_buffer = RID ( ) ;
RD : : get_singleton ( ) - > free ( particles - > particle_instance_buffer ) ;
particles - > particle_instance_buffer = RID ( ) ;
}
if ( particles - > frame_params_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( particles - > frame_params_buffer ) ;
particles - > frame_params_buffer = RID ( ) ;
2020-09-06 12:18:10 +00:00
}
particles - > particles_transforms_buffer_uniform_set = RID ( ) ;
2021-04-27 15:43:49 +00:00
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( particles - > trail_bind_pose_uniform_set ) ) {
RD : : get_singleton ( ) - > free ( particles - > trail_bind_pose_uniform_set ) ;
}
particles - > trail_bind_pose_uniform_set = RID ( ) ;
if ( particles - > trail_bind_pose_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( particles - > trail_bind_pose_buffer ) ;
particles - > trail_bind_pose_buffer = RID ( ) ;
}
2020-10-08 00:29:49 +00:00
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( particles - > collision_textures_uniform_set ) ) {
RD : : get_singleton ( ) - > free ( particles - > collision_textures_uniform_set ) ;
}
2021-04-27 15:43:49 +00:00
particles - > collision_textures_uniform_set = RID ( ) ;
2020-10-08 00:29:49 +00:00
2020-09-06 12:18:10 +00:00
if ( particles - > particles_sort_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( particles - > particles_sort_buffer ) ;
particles - > particles_sort_buffer = RID ( ) ;
2021-04-27 15:43:49 +00:00
particles - > particles_sort_uniform_set = RID ( ) ;
2020-09-06 12:18:10 +00:00
}
if ( particles - > emission_buffer ! = nullptr ) {
particles - > emission_buffer = nullptr ;
particles - > emission_buffer_data . clear ( ) ;
RD : : get_singleton ( ) - > free ( particles - > emission_storage_buffer ) ;
particles - > emission_storage_buffer = RID ( ) ;
}
2021-04-27 15:43:49 +00:00
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( particles - > particles_material_uniform_set ) ) {
//will need to be re-created
RD : : get_singleton ( ) - > free ( particles - > particles_material_uniform_set ) ;
}
particles - > particles_material_uniform_set = RID ( ) ;
2020-09-06 12:18:10 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_amount ( RID p_particles , int p_amount ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
2020-09-06 12:18:10 +00:00
if ( particles - > amount = = p_amount ) {
return ;
}
2020-08-19 13:38:24 +00:00
2020-09-06 12:18:10 +00:00
_particles_free_data ( particles ) ;
2020-08-19 13:38:24 +00:00
2020-09-06 12:18:10 +00:00
particles - > amount = p_amount ;
2020-08-19 13:38:24 +00:00
particles - > prev_ticks = 0 ;
particles - > phase = 0 ;
particles - > prev_phase = 0 ;
particles - > clear = true ;
2021-04-27 15:43:49 +00:00
particles - > dependency . changed_notify ( DEPENDENCY_CHANGED_PARTICLES ) ;
2020-08-19 13:38:24 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_lifetime ( RID p_particles , float p_lifetime ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > lifetime = p_lifetime ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_one_shot ( RID p_particles , bool p_one_shot ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > one_shot = p_one_shot ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_pre_process_time ( RID p_particles , float p_time ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > pre_process_time = p_time ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_explosiveness_ratio ( RID p_particles , float p_ratio ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > explosiveness = p_ratio ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_randomness_ratio ( RID p_particles , float p_ratio ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > randomness = p_ratio ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_custom_aabb ( RID p_particles , const AABB & p_aabb ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > custom_aabb = p_aabb ;
2021-01-04 12:33:25 +00:00
particles - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2020-08-19 13:38:24 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_speed_scale ( RID p_particles , float p_scale ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > speed_scale = p_scale ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_use_local_coordinates ( RID p_particles , bool p_enable ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > use_local_coords = p_enable ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_fixed_fps ( RID p_particles , int p_fps ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > fixed_fps = p_fps ;
2021-04-27 15:43:49 +00:00
_particles_free_data ( particles ) ;
particles - > prev_ticks = 0 ;
particles - > phase = 0 ;
particles - > prev_phase = 0 ;
particles - > clear = true ;
particles - > dependency . changed_notify ( DEPENDENCY_CHANGED_PARTICLES ) ;
}
void RendererStorageRD : : particles_set_interpolate ( RID p_particles , bool p_enable ) {
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > interpolate = p_enable ;
2020-08-19 13:38:24 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_fractional_delta ( RID p_particles , bool p_enable ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > fractional_delta = p_enable ;
}
2021-04-27 15:43:49 +00:00
void RendererStorageRD : : particles_set_trails ( RID p_particles , bool p_enable , float p_length ) {
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
ERR_FAIL_COND ( p_length < 0.1 ) ;
p_length = MIN ( 10.0 , p_length ) ;
particles - > trails_enabled = p_enable ;
particles - > trail_length = p_length ;
_particles_free_data ( particles ) ;
particles - > prev_ticks = 0 ;
particles - > phase = 0 ;
particles - > prev_phase = 0 ;
particles - > clear = true ;
particles - > dependency . changed_notify ( DEPENDENCY_CHANGED_PARTICLES ) ;
}
2020-10-17 05:08:21 +00:00
void RendererStorageRD : : particles_set_trail_bind_poses ( RID p_particles , const Vector < Transform3D > & p_bind_poses ) {
2021-04-27 15:43:49 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
if ( particles - > trail_bind_pose_buffer . is_valid ( ) & & particles - > trail_bind_poses . size ( ) ! = p_bind_poses . size ( ) ) {
_particles_free_data ( particles ) ;
particles - > prev_ticks = 0 ;
particles - > phase = 0 ;
particles - > prev_phase = 0 ;
particles - > clear = true ;
}
particles - > trail_bind_poses = p_bind_poses ;
particles - > trail_bind_poses_dirty = true ;
particles - > dependency . changed_notify ( DEPENDENCY_CHANGED_PARTICLES ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_collision_base_size ( RID p_particles , float p_size ) {
2020-10-08 00:29:49 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > collision_base_size = p_size ;
}
2021-04-27 15:43:49 +00:00
void RendererStorageRD : : particles_set_transform_align ( RID p_particles , RS : : ParticlesTransformAlign p_transform_align ) {
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > transform_align = p_transform_align ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_process_material ( RID p_particles , RID p_material ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > process_material = p_material ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_draw_order ( RID p_particles , RS : : ParticlesDrawOrder p_order ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > draw_order = p_order ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_draw_passes ( RID p_particles , int p_passes ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > draw_passes . resize ( p_passes ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_draw_pass_mesh ( RID p_particles , int p_pass , RID p_mesh ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
ERR_FAIL_INDEX ( p_pass , particles - > draw_passes . size ( ) ) ;
particles - > draw_passes . write [ p_pass ] = p_mesh ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_restart ( RID p_particles ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > restart_request = true ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _particles_allocate_emission_buffer ( Particles * particles ) {
2020-09-06 12:18:10 +00:00
ERR_FAIL_COND ( particles - > emission_buffer ! = nullptr ) ;
particles - > emission_buffer_data . resize ( sizeof ( ParticleEmissionBuffer : : Data ) * particles - > amount + sizeof ( uint32_t ) * 4 ) ;
2021-04-27 14:19:21 +00:00
memset ( particles - > emission_buffer_data . ptrw ( ) , 0 , particles - > emission_buffer_data . size ( ) ) ;
2020-09-06 12:18:10 +00:00
particles - > emission_buffer = ( ParticleEmissionBuffer * ) particles - > emission_buffer_data . ptrw ( ) ;
particles - > emission_buffer - > particle_max = particles - > amount ;
particles - > emission_storage_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( particles - > emission_buffer_data . size ( ) , particles - > emission_buffer_data ) ;
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( particles - > particles_material_uniform_set ) ) {
//will need to be re-created
RD : : get_singleton ( ) - > free ( particles - > particles_material_uniform_set ) ;
particles - > particles_material_uniform_set = RID ( ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_set_subemitter ( RID p_particles , RID p_subemitter_particles ) {
2020-09-06 12:18:10 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
ERR_FAIL_COND ( p_particles = = p_subemitter_particles ) ;
particles - > sub_emitter = p_subemitter_particles ;
if ( RD : : get_singleton ( ) - > uniform_set_is_valid ( particles - > particles_material_uniform_set ) ) {
RD : : get_singleton ( ) - > free ( particles - > particles_material_uniform_set ) ;
particles - > particles_material_uniform_set = RID ( ) ; //clear and force to re create sub emitting
}
}
2020-10-17 05:08:21 +00:00
void RendererStorageRD : : particles_emit ( RID p_particles , const Transform3D & p_transform , const Vector3 & p_velocity , const Color & p_color , const Color & p_custom , uint32_t p_emit_flags ) {
2020-09-06 12:18:10 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
ERR_FAIL_COND ( particles - > amount = = 0 ) ;
if ( particles - > emitting ) {
particles - > clear = true ;
particles - > emitting = false ;
}
if ( particles - > emission_buffer = = nullptr ) {
_particles_allocate_emission_buffer ( particles ) ;
}
if ( particles - > inactive ) {
//in case it was inactive, make active again
particles - > inactive = false ;
particles - > inactive_time = 0 ;
}
int32_t idx = particles - > emission_buffer - > particle_count ;
if ( idx < particles - > emission_buffer - > particle_max ) {
store_transform ( p_transform , particles - > emission_buffer - > data [ idx ] . xform ) ;
particles - > emission_buffer - > data [ idx ] . velocity [ 0 ] = p_velocity . x ;
particles - > emission_buffer - > data [ idx ] . velocity [ 1 ] = p_velocity . y ;
particles - > emission_buffer - > data [ idx ] . velocity [ 2 ] = p_velocity . z ;
particles - > emission_buffer - > data [ idx ] . custom [ 0 ] = p_custom . r ;
particles - > emission_buffer - > data [ idx ] . custom [ 1 ] = p_custom . g ;
particles - > emission_buffer - > data [ idx ] . custom [ 2 ] = p_custom . b ;
particles - > emission_buffer - > data [ idx ] . custom [ 3 ] = p_custom . a ;
particles - > emission_buffer - > data [ idx ] . color [ 0 ] = p_color . r ;
particles - > emission_buffer - > data [ idx ] . color [ 1 ] = p_color . g ;
particles - > emission_buffer - > data [ idx ] . color [ 2 ] = p_color . b ;
particles - > emission_buffer - > data [ idx ] . color [ 3 ] = p_color . a ;
particles - > emission_buffer - > data [ idx ] . flags = p_emit_flags ;
particles - > emission_buffer - > particle_count + + ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_request_process ( RID p_particles ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
if ( ! particles - > dirty ) {
particles - > dirty = true ;
particles - > update_list = particle_update_list ;
particle_update_list = particles ;
}
}
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : particles_get_current_aabb ( RID p_particles ) {
2020-08-19 13:38:24 +00:00
const Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND_V ( ! particles , AABB ( ) ) ;
2021-04-27 15:43:49 +00:00
int total_amount = particles - > amount ;
if ( particles - > trails_enabled & & particles - > trail_bind_poses . size ( ) > 1 ) {
total_amount * = particles - > trail_bind_poses . size ( ) ;
}
2020-08-19 13:38:24 +00:00
Vector < ParticleData > data ;
2021-04-27 15:43:49 +00:00
data . resize ( total_amount ) ;
2020-08-19 13:38:24 +00:00
Vector < uint8_t > buffer = RD : : get_singleton ( ) - > buffer_get_data ( particles - > particle_buffer ) ;
2020-10-17 05:08:21 +00:00
Transform3D inv = particles - > emission_transform . affine_inverse ( ) ;
2020-08-19 13:38:24 +00:00
AABB aabb ;
if ( buffer . size ( ) ) {
bool first = true ;
2021-04-27 15:43:49 +00:00
2020-08-19 13:38:24 +00:00
const ParticleData * particle_data = ( const ParticleData * ) data . ptr ( ) ;
2021-04-27 15:43:49 +00:00
for ( int i = 0 ; i < total_amount ; i + + ) {
2020-08-19 13:38:24 +00:00
if ( particle_data [ i ] . active ) {
Vector3 pos = Vector3 ( particle_data [ i ] . xform [ 12 ] , particle_data [ i ] . xform [ 13 ] , particle_data [ i ] . xform [ 14 ] ) ;
if ( ! particles - > use_local_coords ) {
pos = inv . xform ( pos ) ;
}
if ( first ) {
aabb . position = pos ;
first = false ;
} else {
aabb . expand_to ( pos ) ;
}
}
}
}
float longest_axis_size = 0 ;
for ( int i = 0 ; i < particles - > draw_passes . size ( ) ; i + + ) {
if ( particles - > draw_passes [ i ] . is_valid ( ) ) {
AABB maabb = mesh_get_aabb ( particles - > draw_passes [ i ] , RID ( ) ) ;
longest_axis_size = MAX ( maabb . get_longest_axis_size ( ) , longest_axis_size ) ;
}
}
aabb . grow_by ( longest_axis_size ) ;
return aabb ;
}
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : particles_get_aabb ( RID p_particles ) const {
2020-08-19 13:38:24 +00:00
const Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND_V ( ! particles , AABB ( ) ) ;
return particles - > custom_aabb ;
}
2020-10-17 05:08:21 +00:00
void RendererStorageRD : : particles_set_emission_transform ( RID p_particles , const Transform3D & p_transform ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > emission_transform = p_transform ;
}
2020-12-04 18:26:24 +00:00
int RendererStorageRD : : particles_get_draw_passes ( RID p_particles ) const {
2020-08-19 13:38:24 +00:00
const Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND_V ( ! particles , 0 ) ;
return particles - > draw_passes . size ( ) ;
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : particles_get_draw_pass_mesh ( RID p_particles , int p_pass ) const {
2020-08-19 13:38:24 +00:00
const Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND_V ( ! particles , RID ( ) ) ;
ERR_FAIL_INDEX_V ( p_pass , particles - > draw_passes . size ( ) , RID ( ) ) ;
return particles - > draw_passes [ p_pass ] ;
}
2020-12-31 12:42:56 +00:00
void RendererStorageRD : : particles_add_collision ( RID p_particles , RID p_particles_collision_instance ) {
2020-10-08 00:29:49 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
2020-12-31 12:42:56 +00:00
particles - > collisions . insert ( p_particles_collision_instance ) ;
2020-10-08 00:29:49 +00:00
}
2020-12-31 12:42:56 +00:00
void RendererStorageRD : : particles_remove_collision ( RID p_particles , RID p_particles_collision_instance ) {
2020-10-08 00:29:49 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
2020-12-31 12:42:56 +00:00
particles - > collisions . erase ( p_particles_collision_instance ) ;
2020-10-08 00:29:49 +00:00
}
2021-05-20 14:25:06 +00:00
void RendererStorageRD : : particles_set_canvas_sdf_collision ( RID p_particles , bool p_enable , const Transform2D & p_xform , const Rect2 & p_to_screen , RID p_texture ) {
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
particles - > has_sdf_collision = p_enable ;
particles - > sdf_collision_transform = p_xform ;
particles - > sdf_collision_to_screen = p_to_screen ;
particles - > sdf_collision_texture = p_texture ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _particles_process ( Particles * p_particles , float p_delta ) {
2020-09-06 12:18:10 +00:00
if ( p_particles - > particles_material_uniform_set . is_null ( ) | | ! RD : : get_singleton ( ) - > uniform_set_is_valid ( p_particles - > particles_material_uniform_set ) ) {
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2020-09-06 12:18:10 +00:00
u . binding = 0 ;
u . ids . push_back ( p_particles - > frame_params_buffer ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2020-09-06 12:18:10 +00:00
u . binding = 1 ;
u . ids . push_back ( p_particles - > particle_buffer ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2020-09-06 12:18:10 +00:00
u . binding = 2 ;
if ( p_particles - > emission_storage_buffer . is_valid ( ) ) {
u . ids . push_back ( p_particles - > emission_storage_buffer ) ;
} else {
u . ids . push_back ( default_rd_storage_buffer ) ;
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2020-09-06 12:18:10 +00:00
u . binding = 3 ;
Particles * sub_emitter = particles_owner . getornull ( p_particles - > sub_emitter ) ;
if ( sub_emitter ) {
if ( sub_emitter - > emission_buffer = = nullptr ) { //no emission buffer, allocate emission buffer
_particles_allocate_emission_buffer ( sub_emitter ) ;
}
u . ids . push_back ( sub_emitter - > emission_storage_buffer ) ;
} else {
u . ids . push_back ( default_rd_storage_buffer ) ;
}
uniforms . push_back ( u ) ;
}
p_particles - > particles_material_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , particles_shader . default_shader_rd , 1 ) ;
}
2020-08-19 13:38:24 +00:00
float new_phase = Math : : fmod ( ( float ) p_particles - > phase + ( p_delta / p_particles - > lifetime ) * p_particles - > speed_scale , ( float ) 1.0 ) ;
2021-04-27 15:43:49 +00:00
//move back history (if there is any)
for ( uint32_t i = p_particles - > frame_history . size ( ) - 1 ; i > 0 ; i - - ) {
p_particles - > frame_history [ i ] = p_particles - > frame_history [ i - 1 ] ;
}
//update current frame
ParticlesFrameParams & frame_params = p_particles - > frame_history [ 0 ] ;
2020-08-19 13:38:24 +00:00
if ( p_particles - > clear ) {
p_particles - > cycle_number = 0 ;
p_particles - > random_seed = Math : : rand ( ) ;
} else if ( new_phase < p_particles - > phase ) {
if ( p_particles - > one_shot ) {
p_particles - > emitting = false ;
}
p_particles - > cycle_number + + ;
}
frame_params . emitting = p_particles - > emitting ;
frame_params . system_phase = new_phase ;
frame_params . prev_system_phase = p_particles - > phase ;
p_particles - > phase = new_phase ;
2020-12-04 18:26:24 +00:00
frame_params . time = RendererCompositorRD : : singleton - > get_total_time ( ) ;
2020-08-19 13:38:24 +00:00
frame_params . delta = p_delta * p_particles - > speed_scale ;
frame_params . random_seed = p_particles - > random_seed ;
frame_params . explosiveness = p_particles - > explosiveness ;
frame_params . randomness = p_particles - > randomness ;
if ( p_particles - > use_local_coords ) {
2020-10-17 05:08:21 +00:00
store_transform ( Transform3D ( ) , frame_params . emission_transform ) ;
2020-08-19 13:38:24 +00:00
} else {
store_transform ( p_particles - > emission_transform , frame_params . emission_transform ) ;
}
frame_params . cycle = p_particles - > cycle_number ;
2021-04-27 15:43:49 +00:00
frame_params . frame = p_particles - > frame_counter + + ;
frame_params . pad0 = 0 ;
frame_params . pad1 = 0 ;
frame_params . pad2 = 0 ;
2020-08-19 13:38:24 +00:00
2020-10-08 00:29:49 +00:00
{ //collision and attractors
frame_params . collider_count = 0 ;
frame_params . attractor_count = 0 ;
frame_params . particle_size = p_particles - > collision_base_size ;
RID collision_3d_textures [ ParticlesFrameParams : : MAX_3D_TEXTURES ] ;
RID collision_heightmap_texture ;
2020-10-17 05:08:21 +00:00
Transform3D to_particles ;
2020-10-08 00:29:49 +00:00
if ( p_particles - > use_local_coords ) {
to_particles = p_particles - > emission_transform . affine_inverse ( ) ;
}
2021-05-20 14:25:06 +00:00
if ( p_particles - > has_sdf_collision & & RD : : get_singleton ( ) - > texture_is_valid ( p_particles - > sdf_collision_texture ) ) {
//2D collision
Transform2D xform = p_particles - > sdf_collision_transform ; //will use dotproduct manually so invert beforehand
Transform2D revert = xform . affine_inverse ( ) ;
frame_params . collider_count = 1 ;
frame_params . colliders [ 0 ] . transform [ 0 ] = xform . elements [ 0 ] [ 0 ] ;
frame_params . colliders [ 0 ] . transform [ 1 ] = xform . elements [ 0 ] [ 1 ] ;
frame_params . colliders [ 0 ] . transform [ 2 ] = 0 ;
frame_params . colliders [ 0 ] . transform [ 3 ] = xform . elements [ 2 ] [ 0 ] ;
frame_params . colliders [ 0 ] . transform [ 4 ] = xform . elements [ 1 ] [ 0 ] ;
frame_params . colliders [ 0 ] . transform [ 5 ] = xform . elements [ 1 ] [ 1 ] ;
frame_params . colliders [ 0 ] . transform [ 6 ] = 0 ;
frame_params . colliders [ 0 ] . transform [ 7 ] = xform . elements [ 2 ] [ 1 ] ;
frame_params . colliders [ 0 ] . transform [ 8 ] = revert . elements [ 0 ] [ 0 ] ;
frame_params . colliders [ 0 ] . transform [ 9 ] = revert . elements [ 0 ] [ 1 ] ;
frame_params . colliders [ 0 ] . transform [ 10 ] = 0 ;
frame_params . colliders [ 0 ] . transform [ 11 ] = revert . elements [ 2 ] [ 0 ] ;
frame_params . colliders [ 0 ] . transform [ 12 ] = revert . elements [ 1 ] [ 0 ] ;
frame_params . colliders [ 0 ] . transform [ 13 ] = revert . elements [ 1 ] [ 1 ] ;
frame_params . colliders [ 0 ] . transform [ 14 ] = 0 ;
frame_params . colliders [ 0 ] . transform [ 15 ] = revert . elements [ 2 ] [ 1 ] ;
frame_params . colliders [ 0 ] . extents [ 0 ] = p_particles - > sdf_collision_to_screen . size . x ;
frame_params . colliders [ 0 ] . extents [ 1 ] = p_particles - > sdf_collision_to_screen . size . y ;
frame_params . colliders [ 0 ] . extents [ 2 ] = p_particles - > sdf_collision_to_screen . position . x ;
frame_params . colliders [ 0 ] . scale = p_particles - > sdf_collision_to_screen . position . y ;
frame_params . colliders [ 0 ] . texture_index = 0 ;
frame_params . colliders [ 0 ] . type = ParticlesFrameParams : : COLLISION_TYPE_2D_SDF ;
collision_heightmap_texture = p_particles - > sdf_collision_texture ;
//replace in all other history frames where used because parameters are no longer valid if screen moves
for ( uint32_t i = 1 ; i < p_particles - > frame_history . size ( ) ; i + + ) {
if ( p_particles - > frame_history [ i ] . collider_count > 0 & & p_particles - > frame_history [ i ] . colliders [ 0 ] . type = = ParticlesFrameParams : : COLLISION_TYPE_2D_SDF ) {
p_particles - > frame_history [ i ] . colliders [ 0 ] = frame_params . colliders [ 0 ] ;
}
}
}
2020-10-08 00:29:49 +00:00
uint32_t collision_3d_textures_used = 0 ;
2020-12-31 12:42:56 +00:00
for ( const Set < RID > : : Element * E = p_particles - > collisions . front ( ) ; E ; E = E - > next ( ) ) {
ParticlesCollisionInstance * pci = particles_collision_instance_owner . getornull ( E - > get ( ) ) ;
if ( ! pci | | ! pci - > active ) {
continue ;
}
ParticlesCollision * pc = particles_collision_owner . getornull ( pci - > collision ) ;
ERR_CONTINUE ( ! pc ) ;
2020-10-17 05:08:21 +00:00
Transform3D to_collider = pci - > transform ;
2020-10-08 00:29:49 +00:00
if ( p_particles - > use_local_coords ) {
to_collider = to_particles * to_collider ;
}
Vector3 scale = to_collider . basis . get_scale ( ) ;
to_collider . basis . orthonormalize ( ) ;
if ( pc - > type < = RS : : PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT ) {
//attractor
if ( frame_params . attractor_count > = ParticlesFrameParams : : MAX_ATTRACTORS ) {
continue ;
}
ParticlesFrameParams : : Attractor & attr = frame_params . attractors [ frame_params . attractor_count ] ;
store_transform ( to_collider , attr . transform ) ;
attr . strength = pc - > attractor_strength ;
attr . attenuation = pc - > attractor_attenuation ;
attr . directionality = pc - > attractor_directionality ;
switch ( pc - > type ) {
case RS : : PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT : {
attr . type = ParticlesFrameParams : : ATTRACTOR_TYPE_SPHERE ;
float radius = pc - > radius ;
radius * = ( scale . x + scale . y + scale . z ) / 3.0 ;
attr . extents [ 0 ] = radius ;
attr . extents [ 1 ] = radius ;
attr . extents [ 2 ] = radius ;
} break ;
case RS : : PARTICLES_COLLISION_TYPE_BOX_ATTRACT : {
attr . type = ParticlesFrameParams : : ATTRACTOR_TYPE_BOX ;
Vector3 extents = pc - > extents * scale ;
attr . extents [ 0 ] = extents . x ;
attr . extents [ 1 ] = extents . y ;
attr . extents [ 2 ] = extents . z ;
} break ;
case RS : : PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT : {
if ( collision_3d_textures_used > = ParticlesFrameParams : : MAX_3D_TEXTURES ) {
continue ;
}
attr . type = ParticlesFrameParams : : ATTRACTOR_TYPE_VECTOR_FIELD ;
Vector3 extents = pc - > extents * scale ;
attr . extents [ 0 ] = extents . x ;
attr . extents [ 1 ] = extents . y ;
attr . extents [ 2 ] = extents . z ;
attr . texture_index = collision_3d_textures_used ;
collision_3d_textures [ collision_3d_textures_used ] = pc - > field_texture ;
collision_3d_textures_used + + ;
} break ;
default : {
}
}
frame_params . attractor_count + + ;
} else {
//collider
if ( frame_params . collider_count > = ParticlesFrameParams : : MAX_COLLIDERS ) {
continue ;
}
ParticlesFrameParams : : Collider & col = frame_params . colliders [ frame_params . collider_count ] ;
store_transform ( to_collider , col . transform ) ;
switch ( pc - > type ) {
case RS : : PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE : {
col . type = ParticlesFrameParams : : COLLISION_TYPE_SPHERE ;
float radius = pc - > radius ;
radius * = ( scale . x + scale . y + scale . z ) / 3.0 ;
col . extents [ 0 ] = radius ;
col . extents [ 1 ] = radius ;
col . extents [ 2 ] = radius ;
} break ;
case RS : : PARTICLES_COLLISION_TYPE_BOX_COLLIDE : {
col . type = ParticlesFrameParams : : COLLISION_TYPE_BOX ;
Vector3 extents = pc - > extents * scale ;
col . extents [ 0 ] = extents . x ;
col . extents [ 1 ] = extents . y ;
col . extents [ 2 ] = extents . z ;
} break ;
case RS : : PARTICLES_COLLISION_TYPE_SDF_COLLIDE : {
if ( collision_3d_textures_used > = ParticlesFrameParams : : MAX_3D_TEXTURES ) {
continue ;
}
col . type = ParticlesFrameParams : : COLLISION_TYPE_SDF ;
Vector3 extents = pc - > extents * scale ;
col . extents [ 0 ] = extents . x ;
col . extents [ 1 ] = extents . y ;
col . extents [ 2 ] = extents . z ;
col . texture_index = collision_3d_textures_used ;
col . scale = ( scale . x + scale . y + scale . z ) * 0.333333333333 ; //non uniform scale non supported
collision_3d_textures [ collision_3d_textures_used ] = pc - > field_texture ;
collision_3d_textures_used + + ;
} break ;
case RS : : PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE : {
if ( collision_heightmap_texture ! = RID ( ) ) { //already taken
continue ;
}
col . type = ParticlesFrameParams : : COLLISION_TYPE_HEIGHT_FIELD ;
Vector3 extents = pc - > extents * scale ;
col . extents [ 0 ] = extents . x ;
col . extents [ 1 ] = extents . y ;
col . extents [ 2 ] = extents . z ;
collision_heightmap_texture = pc - > heightfield_texture ;
} break ;
default : {
}
}
frame_params . collider_count + + ;
}
}
bool different = false ;
if ( collision_3d_textures_used = = p_particles - > collision_3d_textures_used ) {
for ( int i = 0 ; i < ParticlesFrameParams : : MAX_3D_TEXTURES ; i + + ) {
if ( p_particles - > collision_3d_textures [ i ] ! = collision_3d_textures [ i ] ) {
different = true ;
break ;
}
}
}
if ( collision_heightmap_texture ! = p_particles - > collision_heightmap_texture ) {
different = true ;
}
bool uniform_set_valid = RD : : get_singleton ( ) - > uniform_set_is_valid ( p_particles - > collision_textures_uniform_set ) ;
if ( different | | ! uniform_set_valid ) {
if ( uniform_set_valid ) {
RD : : get_singleton ( ) - > free ( p_particles - > collision_textures_uniform_set ) ;
}
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2020-10-08 00:29:49 +00:00
u . binding = 0 ;
for ( uint32_t i = 0 ; i < ParticlesFrameParams : : MAX_3D_TEXTURES ; i + + ) {
RID rd_tex ;
if ( i < collision_3d_textures_used ) {
Texture * t = texture_owner . getornull ( collision_3d_textures [ i ] ) ;
if ( t & & t - > type = = Texture : : TYPE_3D ) {
rd_tex = t - > rd_texture ;
}
}
if ( rd_tex = = RID ( ) ) {
rd_tex = default_rd_textures [ DEFAULT_RD_TEXTURE_3D_WHITE ] ;
}
u . ids . push_back ( rd_tex ) ;
}
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2020-10-08 00:29:49 +00:00
u . binding = 1 ;
if ( collision_heightmap_texture . is_valid ( ) ) {
u . ids . push_back ( collision_heightmap_texture ) ;
} else {
u . ids . push_back ( default_rd_textures [ DEFAULT_RD_TEXTURE_BLACK ] ) ;
}
uniforms . push_back ( u ) ;
}
p_particles - > collision_textures_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , particles_shader . default_shader_rd , 2 ) ;
}
}
2020-08-19 13:38:24 +00:00
ParticlesShader : : PushConstant push_constant ;
2021-04-27 15:43:49 +00:00
int process_amount = p_particles - > amount ;
if ( p_particles - > trails_enabled & & p_particles - > trail_bind_poses . size ( ) > 1 ) {
process_amount * = p_particles - > trail_bind_poses . size ( ) ;
}
2020-08-19 13:38:24 +00:00
push_constant . clear = p_particles - > clear ;
push_constant . total_particles = p_particles - > amount ;
push_constant . lifetime = p_particles - > lifetime ;
2021-04-27 15:43:49 +00:00
push_constant . trail_size = p_particles - > trail_params . size ( ) ;
2020-08-19 13:38:24 +00:00
push_constant . use_fractional_delta = p_particles - > fractional_delta ;
2020-09-06 12:18:10 +00:00
push_constant . sub_emitter_mode = ! p_particles - > emitting & & p_particles - > emission_buffer & & ( p_particles - > emission_buffer - > particle_count > 0 | | p_particles - > force_sub_emit ) ;
2021-04-27 15:43:49 +00:00
push_constant . trail_pass = false ;
2020-09-06 12:18:10 +00:00
p_particles - > force_sub_emit = false ; //reset
Particles * sub_emitter = particles_owner . getornull ( p_particles - > sub_emitter ) ;
if ( sub_emitter & & sub_emitter - > emission_storage_buffer . is_valid ( ) ) {
// print_line("updating subemitter buffer");
int32_t zero [ 4 ] = { 0 , sub_emitter - > amount , 0 , 0 } ;
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > buffer_update ( sub_emitter - > emission_storage_buffer , 0 , sizeof ( uint32_t ) * 4 , zero ) ;
2020-09-06 12:18:10 +00:00
push_constant . can_emit = true ;
if ( sub_emitter - > emitting ) {
sub_emitter - > emitting = false ;
sub_emitter - > clear = true ; //will need to clear if it was emitting, sorry
}
//make sure the sub emitter processes particles too
sub_emitter - > inactive = false ;
sub_emitter - > inactive_time = 0 ;
sub_emitter - > force_sub_emit = true ;
} else {
push_constant . can_emit = false ;
}
if ( p_particles - > emission_buffer & & p_particles - > emission_buffer - > particle_count ) {
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > buffer_update ( p_particles - > emission_storage_buffer , 0 , sizeof ( uint32_t ) * 4 + sizeof ( ParticleEmissionBuffer : : Data ) * p_particles - > emission_buffer - > particle_count , p_particles - > emission_buffer ) ;
2020-09-06 12:18:10 +00:00
p_particles - > emission_buffer - > particle_count = 0 ;
}
2020-08-19 13:38:24 +00:00
p_particles - > clear = false ;
2021-04-27 15:43:49 +00:00
if ( p_particles - > trail_params . size ( ) > 1 ) {
//fill the trail params
for ( uint32_t i = 0 ; i < p_particles - > trail_params . size ( ) ; i + + ) {
uint32_t src_idx = i * p_particles - > frame_history . size ( ) / p_particles - > trail_params . size ( ) ;
p_particles - > trail_params [ i ] = p_particles - > frame_history [ src_idx ] ;
}
} else {
p_particles - > trail_params [ 0 ] = p_particles - > frame_history [ 0 ] ;
}
RD : : get_singleton ( ) - > buffer_update ( p_particles - > frame_params_buffer , 0 , sizeof ( ParticlesFrameParams ) * p_particles - > trail_params . size ( ) , p_particles - > trail_params . ptr ( ) ) ;
2020-08-19 13:38:24 +00:00
ParticlesMaterialData * m = ( ParticlesMaterialData * ) material_get_data ( p_particles - > process_material , SHADER_TYPE_PARTICLES ) ;
if ( ! m ) {
m = ( ParticlesMaterialData * ) material_get_data ( particles_shader . default_material , SHADER_TYPE_PARTICLES ) ;
}
ERR_FAIL_COND ( ! m ) ;
2021-05-20 14:25:06 +00:00
p_particles - > has_collision_cache = m - > shader_data - > uses_collision ;
2020-08-19 13:38:24 +00:00
//todo should maybe compute all particle systems together?
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , m - > shader_data - > pipeline ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles_shader . base_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , p_particles - > particles_material_uniform_set , 1 ) ;
2020-10-08 00:29:49 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , p_particles - > collision_textures_uniform_set , 2 ) ;
2020-08-19 13:38:24 +00:00
if ( m - > uniform_set . is_valid ( ) ) {
2020-10-08 00:29:49 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , m - > uniform_set , 3 ) ;
2020-08-19 13:38:24 +00:00
}
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( ParticlesShader : : PushConstant ) ) ;
2021-04-27 15:43:49 +00:00
if ( p_particles - > trails_enabled & & p_particles - > trail_bind_poses . size ( ) > 1 ) {
//trails requires two passes in order to catch particle starts
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , process_amount / p_particles - > trail_bind_poses . size ( ) , 1 , 1 ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
push_constant . trail_pass = true ;
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( ParticlesShader : : PushConstant ) ) ;
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , process_amount - p_particles - > amount , 1 , 1 ) ;
} else {
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , process_amount , 1 , 1 ) ;
}
2020-08-19 13:38:24 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
2021-04-27 15:43:49 +00:00
void RendererStorageRD : : particles_set_view_axis ( RID p_particles , const Vector3 & p_axis , const Vector3 & p_up_axis ) {
2020-08-19 13:38:24 +00:00
Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND ( ! particles ) ;
2021-04-27 15:43:49 +00:00
if ( particles - > draw_order ! = RS : : PARTICLES_DRAW_ORDER_VIEW_DEPTH & & particles - > transform_align ! = RS : : PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD & & particles - > transform_align ! = RS : : PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY ) {
return ;
}
if ( particles - > particle_buffer . is_null ( ) ) {
return ; //particles have not processed yet
2020-08-19 13:38:24 +00:00
}
2021-04-27 15:43:49 +00:00
bool do_sort = particles - > draw_order = = RS : : PARTICLES_DRAW_ORDER_VIEW_DEPTH ;
2020-08-19 13:38:24 +00:00
//copy to sort buffer
2021-04-27 15:43:49 +00:00
if ( do_sort & & particles - > particles_sort_buffer = = RID ( ) ) {
2020-08-19 13:38:24 +00:00
uint32_t size = particles - > amount ;
if ( size & 1 ) {
size + + ; //make multiple of 16
}
size * = sizeof ( float ) * 2 ;
particles - > particles_sort_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( size ) ;
2021-04-27 15:43:49 +00:00
2020-08-19 13:38:24 +00:00
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2020-08-19 13:38:24 +00:00
u . binding = 0 ;
u . ids . push_back ( particles - > particles_sort_buffer ) ;
uniforms . push_back ( u ) ;
}
particles - > particles_sort_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , particles_shader . copy_shader . version_get_shader ( particles_shader . copy_shader_version , ParticlesShader : : COPY_MODE_FILL_SORT_BUFFER ) , 1 ) ;
}
}
2021-04-27 15:43:49 +00:00
ParticlesShader : : CopyPushConstant copy_push_constant ;
if ( particles - > trails_enabled & & particles - > trail_bind_poses . size ( ) > 1 ) {
int fixed_fps = 60.0 ;
if ( particles - > fixed_fps > 0 ) {
fixed_fps = particles - > fixed_fps ;
}
copy_push_constant . trail_size = particles - > trail_bind_poses . size ( ) ;
copy_push_constant . trail_total = particles - > frame_history . size ( ) ;
copy_push_constant . frame_delta = 1.0 / fixed_fps ;
} else {
copy_push_constant . trail_size = 1 ;
copy_push_constant . trail_total = 1 ;
copy_push_constant . frame_delta = 0.0 ;
}
2021-05-20 14:25:06 +00:00
copy_push_constant . order_by_lifetime = ( particles - > draw_order = = RS : : PARTICLES_DRAW_ORDER_LIFETIME | | particles - > draw_order = = RS : : PARTICLES_DRAW_ORDER_REVERSE_LIFETIME ) ;
copy_push_constant . lifetime_split = MIN ( particles - > amount * particles - > phase , particles - > amount - 1 ) ;
copy_push_constant . lifetime_reverse = particles - > draw_order = = RS : : PARTICLES_DRAW_ORDER_REVERSE_LIFETIME ;
2021-04-27 15:43:49 +00:00
copy_push_constant . frame_remainder = particles - > interpolate ? particles - > frame_remainder : 0.0 ;
copy_push_constant . total_particles = particles - > amount ;
2020-08-19 13:38:24 +00:00
Vector3 axis = - p_axis ; // cameras look to z negative
if ( particles - > use_local_coords ) {
axis = particles - > emission_transform . basis . xform_inv ( axis ) . normalized ( ) ;
}
copy_push_constant . sort_direction [ 0 ] = axis . x ;
copy_push_constant . sort_direction [ 1 ] = axis . y ;
copy_push_constant . sort_direction [ 2 ] = axis . z ;
2021-04-27 15:43:49 +00:00
copy_push_constant . align_up [ 0 ] = p_up_axis . x ;
copy_push_constant . align_up [ 1 ] = p_up_axis . y ;
copy_push_constant . align_up [ 2 ] = p_up_axis . z ;
2020-08-19 13:38:24 +00:00
2021-04-27 15:43:49 +00:00
copy_push_constant . align_mode = particles - > transform_align ;
2020-08-19 13:38:24 +00:00
2021-04-27 15:43:49 +00:00
if ( do_sort ) {
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , particles_shader . copy_pipelines [ ParticlesShader : : COPY_MODE_FILL_SORT_BUFFER ] ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles - > particles_copy_uniform_set , 0 ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles - > particles_sort_uniform_set , 1 ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles - > trail_bind_pose_uniform_set , 2 ) ;
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & copy_push_constant , sizeof ( ParticlesShader : : CopyPushConstant ) ) ;
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , particles - > amount , 1 , 1 ) ;
2020-08-19 13:38:24 +00:00
2021-04-27 15:43:49 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
effects . sort_buffer ( particles - > particles_sort_uniform_set , particles - > amount ) ;
}
copy_push_constant . total_particles * = copy_push_constant . total_particles ;
2020-08-19 13:38:24 +00:00
2021-04-27 15:43:49 +00:00
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
2021-05-10 16:12:44 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , particles_shader . copy_pipelines [ do_sort ? ParticlesShader : : COPY_MODE_FILL_INSTANCES_WITH_SORT_BUFFER : ( particles - > mode = = RS : : PARTICLES_MODE_2D ? ParticlesShader : : COPY_MODE_FILL_INSTANCES_2D : ParticlesShader : : COPY_MODE_FILL_INSTANCES ) ] ) ;
2020-08-19 13:38:24 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles - > particles_copy_uniform_set , 0 ) ;
2021-04-27 15:43:49 +00:00
if ( do_sort ) {
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles - > particles_sort_uniform_set , 1 ) ;
}
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles - > trail_bind_pose_uniform_set , 2 ) ;
2020-08-19 13:38:24 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & copy_push_constant , sizeof ( ParticlesShader : : CopyPushConstant ) ) ;
2021-04-27 15:43:49 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , copy_push_constant . total_particles , 1 , 1 ) ;
2020-08-19 13:38:24 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
2021-04-27 15:43:49 +00:00
void RendererStorageRD : : _particles_update_buffers ( Particles * particles ) {
if ( particles - > amount > 0 & & particles - > particle_buffer . is_null ( ) ) {
int total_amount = particles - > amount ;
if ( particles - > trails_enabled & & particles - > trail_bind_poses . size ( ) > 1 ) {
total_amount * = particles - > trail_bind_poses . size ( ) ;
}
2021-05-10 16:12:44 +00:00
uint32_t xform_size = particles - > mode = = RS : : PARTICLES_MODE_2D ? 2 : 3 ;
2021-04-27 15:43:49 +00:00
particles - > particle_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( ParticleData ) * total_amount ) ;
2021-05-10 16:12:44 +00:00
particles - > particle_instance_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( float ) * 4 * ( xform_size + 1 + 1 ) * total_amount ) ;
2021-04-27 15:43:49 +00:00
//needs to clear it
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 1 ;
u . ids . push_back ( particles - > particle_buffer ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 2 ;
u . ids . push_back ( particles - > particle_instance_buffer ) ;
uniforms . push_back ( u ) ;
}
particles - > particles_copy_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , particles_shader . copy_shader . version_get_shader ( particles_shader . copy_shader_version , 0 ) , 0 ) ;
}
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : update_particles ( ) {
2020-08-19 13:38:24 +00:00
while ( particle_update_list ) {
//use transform feedback to process particles
Particles * particles = particle_update_list ;
//take and remove
particle_update_list = particles - > update_list ;
particles - > update_list = nullptr ;
particles - > dirty = false ;
2021-04-27 15:43:49 +00:00
_particles_update_buffers ( particles ) ;
2020-08-19 13:38:24 +00:00
if ( particles - > restart_request ) {
particles - > prev_ticks = 0 ;
particles - > phase = 0 ;
particles - > prev_phase = 0 ;
particles - > clear = true ;
particles - > restart_request = false ;
}
if ( particles - > inactive & & ! particles - > emitting ) {
//go next
continue ;
}
if ( particles - > emitting ) {
if ( particles - > inactive ) {
//restart system from scratch
particles - > prev_ticks = 0 ;
particles - > phase = 0 ;
particles - > prev_phase = 0 ;
particles - > clear = true ;
}
particles - > inactive = false ;
particles - > inactive_time = 0 ;
} else {
2020-12-04 18:26:24 +00:00
particles - > inactive_time + = particles - > speed_scale * RendererCompositorRD : : singleton - > get_frame_delta_time ( ) ;
2020-08-19 13:38:24 +00:00
if ( particles - > inactive_time > particles - > lifetime * 1.2 ) {
particles - > inactive = true ;
continue ;
}
}
2021-04-27 15:43:49 +00:00
# ifndef _MSC_VER
# warning Should use display refresh rate for all this
# endif
float screen_hz = 60 ;
int fixed_fps = 0 ;
if ( particles - > fixed_fps > 0 ) {
fixed_fps = particles - > fixed_fps ;
} else if ( particles - > trails_enabled & & particles - > trail_bind_poses . size ( ) > 1 ) {
fixed_fps = screen_hz ;
}
{
//update trails
int history_size = 1 ;
int trail_steps = 1 ;
if ( particles - > trails_enabled & & particles - > trail_bind_poses . size ( ) > 1 ) {
history_size = MAX ( 1 , int ( particles - > trail_length * fixed_fps ) ) ;
trail_steps = particles - > trail_bind_poses . size ( ) ;
}
if ( uint32_t ( history_size ) ! = particles - > frame_history . size ( ) ) {
particles - > frame_history . resize ( history_size ) ;
memset ( particles - > frame_history . ptr ( ) , 0 , sizeof ( ParticlesFrameParams ) * history_size ) ;
}
if ( uint32_t ( trail_steps ) ! = particles - > trail_params . size ( ) | | particles - > frame_params_buffer . is_null ( ) ) {
particles - > trail_params . resize ( trail_steps ) ;
if ( particles - > frame_params_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( particles - > frame_params_buffer ) ;
}
particles - > frame_params_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( ParticlesFrameParams ) * trail_steps ) ;
}
if ( particles - > trail_bind_poses . size ( ) > 1 & & particles - > trail_bind_pose_buffer . is_null ( ) ) {
particles - > trail_bind_pose_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( float ) * 16 * particles - > trail_bind_poses . size ( ) ) ;
particles - > trail_bind_poses_dirty = true ;
}
if ( particles - > trail_bind_pose_uniform_set . is_null ( ) ) {
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . binding = 0 ;
if ( particles - > trail_bind_pose_buffer . is_valid ( ) ) {
u . ids . push_back ( particles - > trail_bind_pose_buffer ) ;
} else {
u . ids . push_back ( default_rd_storage_buffer ) ;
}
uniforms . push_back ( u ) ;
}
particles - > trail_bind_pose_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , particles_shader . copy_shader . version_get_shader ( particles_shader . copy_shader_version , 0 ) , 2 ) ;
}
if ( particles - > trail_bind_pose_buffer . is_valid ( ) & & particles - > trail_bind_poses_dirty ) {
if ( particles_shader . pose_update_buffer . size ( ) < uint32_t ( particles - > trail_bind_poses . size ( ) ) * 16 ) {
particles_shader . pose_update_buffer . resize ( particles - > trail_bind_poses . size ( ) * 16 ) ;
}
for ( int i = 0 ; i < particles - > trail_bind_poses . size ( ) ; i + + ) {
store_transform ( particles - > trail_bind_poses [ i ] , & particles_shader . pose_update_buffer [ i * 16 ] ) ;
}
RD : : get_singleton ( ) - > buffer_update ( particles - > trail_bind_pose_buffer , 0 , particles - > trail_bind_poses . size ( ) * 16 * sizeof ( float ) , particles_shader . pose_update_buffer . ptr ( ) ) ;
}
}
2020-08-19 13:38:24 +00:00
bool zero_time_scale = Engine : : get_singleton ( ) - > get_time_scale ( ) < = 0.0 ;
if ( particles - > clear & & particles - > pre_process_time > 0.0 ) {
float frame_time ;
2021-04-27 15:43:49 +00:00
if ( fixed_fps > 0 ) {
frame_time = 1.0 / fixed_fps ;
2021-04-05 12:09:59 +00:00
} else {
2020-08-19 13:38:24 +00:00
frame_time = 1.0 / 30.0 ;
2021-04-05 12:09:59 +00:00
}
2020-08-19 13:38:24 +00:00
float todo = particles - > pre_process_time ;
while ( todo > = 0 ) {
_particles_process ( particles , frame_time ) ;
todo - = frame_time ;
}
}
2021-04-27 15:43:49 +00:00
if ( fixed_fps > 0 ) {
2020-08-19 13:38:24 +00:00
float frame_time ;
float decr ;
if ( zero_time_scale ) {
frame_time = 0.0 ;
2021-04-27 15:43:49 +00:00
decr = 1.0 / fixed_fps ;
2020-08-19 13:38:24 +00:00
} else {
2021-04-27 15:43:49 +00:00
frame_time = 1.0 / fixed_fps ;
2020-08-19 13:38:24 +00:00
decr = frame_time ;
}
2020-12-04 18:26:24 +00:00
float delta = RendererCompositorRD : : singleton - > get_frame_delta_time ( ) ;
2020-08-19 13:38:24 +00:00
if ( delta > 0.1 ) { //avoid recursive stalls if fps goes below 10
delta = 0.1 ;
} else if ( delta < = 0.0 ) { //unlikely but..
delta = 0.001 ;
}
float todo = particles - > frame_remainder + delta ;
while ( todo > = frame_time ) {
_particles_process ( particles , frame_time ) ;
todo - = decr ;
}
particles - > frame_remainder = todo ;
} else {
2021-04-05 12:09:59 +00:00
if ( zero_time_scale ) {
2020-08-19 13:38:24 +00:00
_particles_process ( particles , 0.0 ) ;
2021-04-05 12:09:59 +00:00
} else {
2020-12-04 18:26:24 +00:00
_particles_process ( particles , RendererCompositorRD : : singleton - > get_frame_delta_time ( ) ) ;
2021-04-05 12:09:59 +00:00
}
2020-08-19 13:38:24 +00:00
}
//copy particles to instance buffer
2021-04-27 15:43:49 +00:00
if ( particles - > draw_order ! = RS : : PARTICLES_DRAW_ORDER_VIEW_DEPTH & & particles - > transform_align ! = RS : : PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD & & particles - > transform_align ! = RS : : PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY ) {
//does not need view dependent operation, do copy here
2020-08-19 13:38:24 +00:00
ParticlesShader : : CopyPushConstant copy_push_constant ;
2021-04-27 15:43:49 +00:00
int total_amount = particles - > amount ;
if ( particles - > trails_enabled & & particles - > trail_bind_poses . size ( ) > 1 ) {
total_amount * = particles - > trail_bind_poses . size ( ) ;
}
copy_push_constant . total_particles = total_amount ;
copy_push_constant . frame_remainder = particles - > interpolate ? particles - > frame_remainder : 0.0 ;
copy_push_constant . align_mode = particles - > transform_align ;
copy_push_constant . align_up [ 0 ] = 0 ;
copy_push_constant . align_up [ 1 ] = 0 ;
copy_push_constant . align_up [ 2 ] = 0 ;
if ( particles - > trails_enabled & & particles - > trail_bind_poses . size ( ) > 1 ) {
copy_push_constant . trail_size = particles - > trail_bind_poses . size ( ) ;
copy_push_constant . trail_total = particles - > frame_history . size ( ) ;
copy_push_constant . frame_delta = 1.0 / fixed_fps ;
} else {
copy_push_constant . trail_size = 1 ;
copy_push_constant . trail_total = 1 ;
copy_push_constant . frame_delta = 0.0 ;
}
2020-08-19 13:38:24 +00:00
2021-05-20 14:25:06 +00:00
copy_push_constant . order_by_lifetime = ( particles - > draw_order = = RS : : PARTICLES_DRAW_ORDER_LIFETIME | | particles - > draw_order = = RS : : PARTICLES_DRAW_ORDER_REVERSE_LIFETIME ) ;
copy_push_constant . lifetime_split = MIN ( particles - > amount * particles - > phase , particles - > amount - 1 ) ;
copy_push_constant . lifetime_reverse = particles - > draw_order = = RS : : PARTICLES_DRAW_ORDER_REVERSE_LIFETIME ;
2020-08-19 13:38:24 +00:00
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
2021-05-10 16:12:44 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , particles_shader . copy_pipelines [ particles - > mode = = RS : : PARTICLES_MODE_2D ? ParticlesShader : : COPY_MODE_FILL_INSTANCES_2D : ParticlesShader : : COPY_MODE_FILL_INSTANCES ] ) ;
2020-08-19 13:38:24 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles - > particles_copy_uniform_set , 0 ) ;
2021-04-27 15:43:49 +00:00
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , particles - > trail_bind_pose_uniform_set , 2 ) ;
2020-08-19 13:38:24 +00:00
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & copy_push_constant , sizeof ( ParticlesShader : : CopyPushConstant ) ) ;
2021-04-27 15:43:49 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , total_amount , 1 , 1 ) ;
2020-08-19 13:38:24 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
2021-01-04 12:33:25 +00:00
particles - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2020-08-19 13:38:24 +00:00
}
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : particles_is_inactive ( RID p_particles ) const {
2020-08-19 13:38:24 +00:00
const Particles * particles = particles_owner . getornull ( p_particles ) ;
ERR_FAIL_COND_V ( ! particles , false ) ;
return ! particles - > emitting & & particles - > inactive ;
}
/* SKY SHADER */
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : ParticlesShaderData : : set_code ( const String & p_code ) {
2020-08-19 13:38:24 +00:00
//compile
code = p_code ;
valid = false ;
ubo_size = 0 ;
uniforms . clear ( ) ;
2021-05-20 14:25:06 +00:00
uses_collision = false ;
2020-08-19 13:38:24 +00:00
if ( code = = String ( ) ) {
return ; //just invalid, but no error
}
ShaderCompilerRD : : GeneratedCode gen_code ;
ShaderCompilerRD : : IdentifierActions actions ;
2021-04-17 15:21:03 +00:00
actions . entry_point_stages [ " start " ] = ShaderCompilerRD : : STAGE_COMPUTE ;
2021-04-13 20:01:43 +00:00
actions . entry_point_stages [ " process " ] = ShaderCompilerRD : : STAGE_COMPUTE ;
2020-08-19 13:38:24 +00:00
/*
uses_time = false ;
actions . render_mode_flags [ " use_half_res_pass " ] = & uses_half_res ;
actions . render_mode_flags [ " use_quarter_res_pass " ] = & uses_quarter_res ;
actions . usage_flag_pointers [ " TIME " ] = & uses_time ;
*/
2021-05-20 14:25:06 +00:00
actions . usage_flag_pointers [ " COLLIDED " ] = & uses_collision ;
2020-08-19 13:38:24 +00:00
actions . uniforms = & uniforms ;
Error err = base_singleton - > particles_shader . compiler . compile ( RS : : SHADER_PARTICLES , code , & actions , path , gen_code ) ;
ERR_FAIL_COND ( err ! = OK ) ;
if ( version . is_null ( ) ) {
version = base_singleton - > particles_shader . shader . version_create ( ) ;
}
2021-04-13 20:01:43 +00:00
base_singleton - > particles_shader . shader . version_set_compute_code ( version , gen_code . code , gen_code . uniforms , gen_code . stage_globals [ ShaderCompilerRD : : STAGE_COMPUTE ] , gen_code . defines ) ;
2020-08-19 13:38:24 +00:00
ERR_FAIL_COND ( ! base_singleton - > particles_shader . shader . version_is_valid ( version ) ) ;
ubo_size = gen_code . uniform_total_size ;
ubo_offsets = gen_code . uniform_offsets ;
texture_uniforms = gen_code . texture_uniforms ;
//update pipelines
pipeline = RD : : get_singleton ( ) - > compute_pipeline_create ( base_singleton - > particles_shader . shader . version_get_shader ( version , 0 ) ) ;
valid = true ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : ParticlesShaderData : : set_default_texture_param ( const StringName & p_name , RID p_texture ) {
2020-08-19 13:38:24 +00:00
if ( ! p_texture . is_valid ( ) ) {
default_texture_params . erase ( p_name ) ;
} else {
default_texture_params [ p_name ] = p_texture ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : ParticlesShaderData : : get_param_list ( List < PropertyInfo > * p_param_list ) const {
2020-08-19 13:38:24 +00:00
Map < int , StringName > order ;
for ( Map < StringName , ShaderLanguage : : ShaderNode : : Uniform > : : Element * E = uniforms . front ( ) ; E ; E = E - > next ( ) ) {
if ( E - > get ( ) . scope = = ShaderLanguage : : ShaderNode : : Uniform : : SCOPE_GLOBAL | | E - > get ( ) . scope = = ShaderLanguage : : ShaderNode : : Uniform : : SCOPE_INSTANCE ) {
continue ;
}
if ( E - > get ( ) . texture_order > = 0 ) {
order [ E - > get ( ) . texture_order + 100000 ] = E - > key ( ) ;
} else {
order [ E - > get ( ) . order ] = E - > key ( ) ;
}
}
for ( Map < int , StringName > : : Element * E = order . front ( ) ; E ; E = E - > next ( ) ) {
PropertyInfo pi = ShaderLanguage : : uniform_to_property_info ( uniforms [ E - > get ( ) ] ) ;
pi . name = E - > get ( ) ;
p_param_list - > push_back ( pi ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : ParticlesShaderData : : get_instance_param_list ( List < RendererStorage : : InstanceShaderParam > * p_param_list ) const {
2020-08-19 13:38:24 +00:00
for ( Map < StringName , ShaderLanguage : : ShaderNode : : Uniform > : : Element * E = uniforms . front ( ) ; E ; E = E - > next ( ) ) {
if ( E - > get ( ) . scope ! = ShaderLanguage : : ShaderNode : : Uniform : : SCOPE_INSTANCE ) {
continue ;
}
2020-12-04 18:26:24 +00:00
RendererStorage : : InstanceShaderParam p ;
2020-08-19 13:38:24 +00:00
p . info = ShaderLanguage : : uniform_to_property_info ( E - > get ( ) ) ;
p . info . name = E - > key ( ) ; //supply name
p . index = E - > get ( ) . instance_index ;
p . default_value = ShaderLanguage : : constant_value_to_variant ( E - > get ( ) . default_value , E - > get ( ) . type , E - > get ( ) . hint ) ;
p_param_list - > push_back ( p ) ;
}
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : ParticlesShaderData : : is_param_texture ( const StringName & p_param ) const {
2020-08-19 13:38:24 +00:00
if ( ! uniforms . has ( p_param ) ) {
return false ;
}
return uniforms [ p_param ] . texture_order > = 0 ;
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : ParticlesShaderData : : is_animated ( ) const {
2020-08-19 13:38:24 +00:00
return false ;
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : ParticlesShaderData : : casts_shadows ( ) const {
2020-08-19 13:38:24 +00:00
return false ;
}
2020-12-04 18:26:24 +00:00
Variant RendererStorageRD : : ParticlesShaderData : : get_default_parameter ( const StringName & p_parameter ) const {
2020-08-19 13:38:24 +00:00
if ( uniforms . has ( p_parameter ) ) {
ShaderLanguage : : ShaderNode : : Uniform uniform = uniforms [ p_parameter ] ;
Vector < ShaderLanguage : : ConstantNode : : Value > default_value = uniform . default_value ;
return ShaderLanguage : : constant_value_to_variant ( default_value , uniform . type , uniform . hint ) ;
}
return Variant ( ) ;
}
2021-01-05 23:01:50 +00:00
RS : : ShaderNativeSourceCode RendererStorageRD : : ParticlesShaderData : : get_native_source_code ( ) const {
return base_singleton - > particles_shader . shader . version_get_native_source_code ( version ) ;
}
2020-12-04 18:26:24 +00:00
RendererStorageRD : : ParticlesShaderData : : ParticlesShaderData ( ) {
2020-08-19 13:38:24 +00:00
valid = false ;
}
2020-12-04 18:26:24 +00:00
RendererStorageRD : : ParticlesShaderData : : ~ ParticlesShaderData ( ) {
2020-08-19 13:38:24 +00:00
//pipeline variants will clear themselves if shader is gone
if ( version . is_valid ( ) ) {
base_singleton - > particles_shader . shader . version_free ( version ) ;
}
}
2020-12-04 18:26:24 +00:00
RendererStorageRD : : ShaderData * RendererStorageRD : : _create_particles_shader_func ( ) {
2020-08-19 13:38:24 +00:00
ParticlesShaderData * shader_data = memnew ( ParticlesShaderData ) ;
return shader_data ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : ParticlesMaterialData : : update_parameters ( const Map < StringName , Variant > & p_parameters , bool p_uniform_dirty , bool p_textures_dirty ) {
2020-08-19 13:38:24 +00:00
uniform_set_updated = true ;
if ( ( uint32_t ) ubo_data . size ( ) ! = shader_data - > ubo_size ) {
p_uniform_dirty = true ;
if ( uniform_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( uniform_buffer ) ;
uniform_buffer = RID ( ) ;
}
ubo_data . resize ( shader_data - > ubo_size ) ;
if ( ubo_data . size ( ) ) {
uniform_buffer = RD : : get_singleton ( ) - > uniform_buffer_create ( ubo_data . size ( ) ) ;
memset ( ubo_data . ptrw ( ) , 0 , ubo_data . size ( ) ) ; //clear
}
//clear previous uniform set
if ( uniform_set . is_valid ( ) & & RD : : get_singleton ( ) - > uniform_set_is_valid ( uniform_set ) ) {
RD : : get_singleton ( ) - > free ( uniform_set ) ;
uniform_set = RID ( ) ;
}
}
//check whether buffer changed
if ( p_uniform_dirty & & ubo_data . size ( ) ) {
update_uniform_buffer ( shader_data - > uniforms , shader_data - > ubo_offsets . ptr ( ) , p_parameters , ubo_data . ptrw ( ) , ubo_data . size ( ) , false ) ;
RD : : get_singleton ( ) - > buffer_update ( uniform_buffer , 0 , ubo_data . size ( ) , ubo_data . ptrw ( ) ) ;
}
uint32_t tex_uniform_count = shader_data - > texture_uniforms . size ( ) ;
if ( ( uint32_t ) texture_cache . size ( ) ! = tex_uniform_count ) {
texture_cache . resize ( tex_uniform_count ) ;
p_textures_dirty = true ;
//clear previous uniform set
if ( uniform_set . is_valid ( ) & & RD : : get_singleton ( ) - > uniform_set_is_valid ( uniform_set ) ) {
RD : : get_singleton ( ) - > free ( uniform_set ) ;
uniform_set = RID ( ) ;
}
}
if ( p_textures_dirty & & tex_uniform_count ) {
update_textures ( p_parameters , shader_data - > default_texture_params , shader_data - > texture_uniforms , texture_cache . ptrw ( ) , true ) ;
}
if ( shader_data - > ubo_size = = 0 & & shader_data - > texture_uniforms . size ( ) = = 0 ) {
// This material does not require an uniform set, so don't create it.
return ;
}
if ( ! p_textures_dirty & & uniform_set . is_valid ( ) & & RD : : get_singleton ( ) - > uniform_set_is_valid ( uniform_set ) ) {
//no reason to update uniform set, only UBO (or nothing) was needed to update
return ;
}
Vector < RD : : Uniform > uniforms ;
{
if ( shader_data - > ubo_size ) {
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_UNIFORM_BUFFER ;
2020-08-19 13:38:24 +00:00
u . binding = 0 ;
u . ids . push_back ( uniform_buffer ) ;
uniforms . push_back ( u ) ;
}
const RID * textures = texture_cache . ptrw ( ) ;
for ( uint32_t i = 0 ; i < tex_uniform_count ; i + + ) {
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_TEXTURE ;
2020-08-19 13:38:24 +00:00
u . binding = 1 + i ;
u . ids . push_back ( textures [ i ] ) ;
uniforms . push_back ( u ) ;
}
}
2020-10-08 00:29:49 +00:00
uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , base_singleton - > particles_shader . shader . version_get_shader ( shader_data - > version , 0 ) , 3 ) ;
2020-08-19 13:38:24 +00:00
}
2020-12-04 18:26:24 +00:00
RendererStorageRD : : ParticlesMaterialData : : ~ ParticlesMaterialData ( ) {
2020-08-19 13:38:24 +00:00
if ( uniform_set . is_valid ( ) & & RD : : get_singleton ( ) - > uniform_set_is_valid ( uniform_set ) ) {
RD : : get_singleton ( ) - > free ( uniform_set ) ;
}
if ( uniform_buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( uniform_buffer ) ;
}
}
2020-12-04 18:26:24 +00:00
RendererStorageRD : : MaterialData * RendererStorageRD : : _create_particles_material_func ( ParticlesShaderData * p_shader ) {
2020-08-19 13:38:24 +00:00
ParticlesMaterialData * material_data = memnew ( ParticlesMaterialData ) ;
material_data - > shader_data = p_shader ;
material_data - > last_frame = false ;
//update will happen later anyway so do nothing.
return material_data ;
}
////////
2020-10-08 00:29:49 +00:00
/* PARTICLES COLLISION API */
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : particles_collision_allocate ( ) {
return particles_collision_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : particles_collision_initialize ( RID p_rid ) {
particles_collision_owner . initialize_rid ( p_rid , ParticlesCollision ( ) ) ;
2020-10-08 00:29:49 +00:00
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : particles_collision_get_heightfield_framebuffer ( RID p_particles_collision ) const {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND_V ( ! particles_collision , RID ( ) ) ;
ERR_FAIL_COND_V ( particles_collision - > type ! = RS : : PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE , RID ( ) ) ;
if ( particles_collision - > heightfield_texture = = RID ( ) ) {
//create
int resolutions [ RS : : PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX ] = { 256 , 512 , 1024 , 2048 , 4096 , 8192 } ;
Size2i size ;
if ( particles_collision - > extents . x > particles_collision - > extents . z ) {
size . x = resolutions [ particles_collision - > heightfield_resolution ] ;
size . y = int32_t ( particles_collision - > extents . z / particles_collision - > extents . x * size . x ) ;
} else {
size . y = resolutions [ particles_collision - > heightfield_resolution ] ;
size . x = int32_t ( particles_collision - > extents . x / particles_collision - > extents . z * size . y ) ;
}
RD : : TextureFormat tf ;
tf . format = RD : : DATA_FORMAT_D32_SFLOAT ;
tf . width = size . x ;
tf . height = size . y ;
2020-11-27 03:50:05 +00:00
tf . texture_type = RD : : TEXTURE_TYPE_2D ;
2020-10-08 00:29:49 +00:00
tf . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT ;
particles_collision - > heightfield_texture = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
Vector < RID > fb_tex ;
fb_tex . push_back ( particles_collision - > heightfield_texture ) ;
particles_collision - > heightfield_fb = RD : : get_singleton ( ) - > framebuffer_create ( fb_tex ) ;
particles_collision - > heightfield_fb_size = size ;
}
return particles_collision - > heightfield_fb ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_collision_type ( RID p_particles_collision , RS : : ParticlesCollisionType p_type ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
if ( p_type = = particles_collision - > type ) {
return ;
}
if ( particles_collision - > heightfield_texture . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( particles_collision - > heightfield_texture ) ;
particles_collision - > heightfield_texture = RID ( ) ;
}
particles_collision - > type = p_type ;
2021-01-04 12:33:25 +00:00
particles_collision - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2020-10-08 00:29:49 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_cull_mask ( RID p_particles_collision , uint32_t p_cull_mask ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
particles_collision - > cull_mask = p_cull_mask ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_sphere_radius ( RID p_particles_collision , float p_radius ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
particles_collision - > radius = p_radius ;
2021-01-04 12:33:25 +00:00
particles_collision - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2020-10-08 00:29:49 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_box_extents ( RID p_particles_collision , const Vector3 & p_extents ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
particles_collision - > extents = p_extents ;
2021-01-04 12:33:25 +00:00
particles_collision - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2020-10-08 00:29:49 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_attractor_strength ( RID p_particles_collision , float p_strength ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
particles_collision - > attractor_strength = p_strength ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_attractor_directionality ( RID p_particles_collision , float p_directionality ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
particles_collision - > attractor_directionality = p_directionality ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_attractor_attenuation ( RID p_particles_collision , float p_curve ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
particles_collision - > attractor_attenuation = p_curve ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_field_texture ( RID p_particles_collision , RID p_texture ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
particles_collision - > field_texture = p_texture ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_height_field_update ( RID p_particles_collision ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
2021-01-04 12:33:25 +00:00
particles_collision - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2020-10-08 00:29:49 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : particles_collision_set_height_field_resolution ( RID p_particles_collision , RS : : ParticlesCollisionHeightfieldResolution p_resolution ) {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND ( ! particles_collision ) ;
2021-03-17 13:46:25 +00:00
ERR_FAIL_INDEX ( p_resolution , RS : : PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX ) ;
2020-10-08 00:29:49 +00:00
if ( particles_collision - > heightfield_resolution = = p_resolution ) {
return ;
}
particles_collision - > heightfield_resolution = p_resolution ;
if ( particles_collision - > heightfield_texture . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( particles_collision - > heightfield_texture ) ;
particles_collision - > heightfield_texture = RID ( ) ;
}
}
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : particles_collision_get_aabb ( RID p_particles_collision ) const {
2020-10-08 00:29:49 +00:00
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND_V ( ! particles_collision , AABB ( ) ) ;
switch ( particles_collision - > type ) {
case RS : : PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT :
case RS : : PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE : {
AABB aabb ;
aabb . position = - Vector3 ( 1 , 1 , 1 ) * particles_collision - > radius ;
aabb . size = Vector3 ( 2 , 2 , 2 ) * particles_collision - > radius ;
return aabb ;
}
default : {
AABB aabb ;
aabb . position = - particles_collision - > extents ;
aabb . size = particles_collision - > extents * 2 ;
return aabb ;
}
}
return AABB ( ) ;
}
2020-12-04 18:26:24 +00:00
Vector3 RendererStorageRD : : particles_collision_get_extents ( RID p_particles_collision ) const {
2020-10-08 00:29:49 +00:00
const ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND_V ( ! particles_collision , Vector3 ( ) ) ;
return particles_collision - > extents ;
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : particles_collision_is_heightfield ( RID p_particles_collision ) const {
2020-10-08 00:29:49 +00:00
const ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_particles_collision ) ;
ERR_FAIL_COND_V ( ! particles_collision , false ) ;
return particles_collision - > type = = RS : : PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE ;
}
2020-12-31 12:42:56 +00:00
RID RendererStorageRD : : particles_collision_instance_create ( RID p_collision ) {
ParticlesCollisionInstance pci ;
pci . collision = p_collision ;
return particles_collision_instance_owner . make_rid ( pci ) ;
}
2020-10-17 05:08:21 +00:00
void RendererStorageRD : : particles_collision_instance_set_transform ( RID p_collision_instance , const Transform3D & p_transform ) {
2020-12-31 12:42:56 +00:00
ParticlesCollisionInstance * pci = particles_collision_instance_owner . getornull ( p_collision_instance ) ;
ERR_FAIL_COND ( ! pci ) ;
pci - > transform = p_transform ;
}
void RendererStorageRD : : particles_collision_instance_set_active ( RID p_collision_instance , bool p_active ) {
ParticlesCollisionInstance * pci = particles_collision_instance_owner . getornull ( p_collision_instance ) ;
ERR_FAIL_COND ( ! pci ) ;
pci - > active = p_active ;
}
2021-06-16 18:43:02 +00:00
/* VISIBILITY NOTIFIER */
RID RendererStorageRD : : visibility_notifier_allocate ( ) {
return visibility_notifier_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : visibility_notifier_initialize ( RID p_notifier ) {
visibility_notifier_owner . initialize_rid ( p_notifier , VisibilityNotifier ( ) ) ;
}
void RendererStorageRD : : visibility_notifier_set_aabb ( RID p_notifier , const AABB & p_aabb ) {
VisibilityNotifier * vn = visibility_notifier_owner . getornull ( p_notifier ) ;
ERR_FAIL_COND ( ! vn ) ;
vn - > aabb = p_aabb ;
vn - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
}
void RendererStorageRD : : visibility_notifier_set_callbacks ( RID p_notifier , const Callable & p_enter_callbable , const Callable & p_exit_callable ) {
VisibilityNotifier * vn = visibility_notifier_owner . getornull ( p_notifier ) ;
ERR_FAIL_COND ( ! vn ) ;
vn - > enter_callback = p_enter_callbable ;
vn - > exit_callback = p_exit_callable ;
}
AABB RendererStorageRD : : visibility_notifier_get_aabb ( RID p_notifier ) const {
const VisibilityNotifier * vn = visibility_notifier_owner . getornull ( p_notifier ) ;
ERR_FAIL_COND_V ( ! vn , AABB ( ) ) ;
return vn - > aabb ;
}
void RendererStorageRD : : visibility_notifier_call ( RID p_notifier , bool p_enter , bool p_deferred ) {
VisibilityNotifier * vn = visibility_notifier_owner . getornull ( p_notifier ) ;
ERR_FAIL_COND ( ! vn ) ;
if ( p_enter ) {
if ( ! vn - > enter_callback . is_null ( ) ) {
if ( p_deferred ) {
vn - > enter_callback . call_deferred ( nullptr , 0 ) ;
} else {
Variant r ;
Callable : : CallError ce ;
vn - > enter_callback . call ( nullptr , 0 , r , ce ) ;
}
}
} else {
if ( ! vn - > exit_callback . is_null ( ) ) {
if ( p_deferred ) {
vn - > exit_callback . call_deferred ( nullptr , 0 ) ;
} else {
Variant r ;
Callable : : CallError ce ;
vn - > exit_callback . call ( nullptr , 0 , r , ce ) ;
}
}
}
}
2019-09-23 21:53:05 +00:00
/* SKELETON API */
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : skeleton_allocate ( ) {
return skeleton_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : skeleton_initialize ( RID p_rid ) {
skeleton_owner . initialize_rid ( p_rid , Skeleton ( ) ) ;
2019-09-23 21:53:05 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _skeleton_make_dirty ( Skeleton * skeleton ) {
2019-09-23 21:53:05 +00:00
if ( ! skeleton - > dirty ) {
skeleton - > dirty = true ;
skeleton - > dirty_list = skeleton_dirty_list ;
skeleton_dirty_list = skeleton ;
}
}
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : skeleton_allocate_data ( RID p_skeleton , int p_bones , bool p_2d_skeleton ) {
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
ERR_FAIL_COND ( ! skeleton ) ;
ERR_FAIL_COND ( p_bones < 0 ) ;
2020-05-14 14:41:43 +00:00
if ( skeleton - > size = = p_bones & & skeleton - > use_2d = = p_2d_skeleton ) {
2019-09-23 21:53:05 +00:00
return ;
2020-05-14 14:41:43 +00:00
}
2019-09-23 21:53:05 +00:00
skeleton - > size = p_bones ;
skeleton - > use_2d = p_2d_skeleton ;
skeleton - > uniform_set_3d = RID ( ) ;
if ( skeleton - > buffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( skeleton - > buffer ) ;
skeleton - > buffer = RID ( ) ;
skeleton - > data . resize ( 0 ) ;
2020-12-16 14:07:08 +00:00
skeleton - > uniform_set_mi = RID ( ) ;
2019-09-23 21:53:05 +00:00
}
if ( skeleton - > size ) {
skeleton - > data . resize ( skeleton - > size * ( skeleton - > use_2d ? 8 : 12 ) ) ;
skeleton - > buffer = RD : : get_singleton ( ) - > storage_buffer_create ( skeleton - > data . size ( ) * sizeof ( float ) ) ;
2021-04-27 14:19:21 +00:00
memset ( skeleton - > data . ptrw ( ) , 0 , skeleton - > data . size ( ) * sizeof ( float ) ) ;
2019-09-23 21:53:05 +00:00
_skeleton_make_dirty ( skeleton ) ;
2020-12-16 14:07:08 +00:00
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 0 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . ids . push_back ( skeleton - > buffer ) ;
uniforms . push_back ( u ) ;
}
skeleton - > uniform_set_mi = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , skeleton_shader . version_shader [ 0 ] , SkeletonShader : : UNIFORM_SET_SKELETON ) ;
}
2019-09-23 21:53:05 +00:00
}
2021-01-04 12:33:25 +00:00
skeleton - > dependency . changed_notify ( DEPENDENCY_CHANGED_SKELETON_DATA ) ;
2019-09-23 21:53:05 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
int RendererStorageRD : : skeleton_get_bone_count ( RID p_skeleton ) const {
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
ERR_FAIL_COND_V ( ! skeleton , 0 ) ;
return skeleton - > size ;
}
2020-10-17 05:08:21 +00:00
void RendererStorageRD : : skeleton_bone_set_transform ( RID p_skeleton , int p_bone , const Transform3D & p_transform ) {
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
ERR_FAIL_COND ( ! skeleton ) ;
ERR_FAIL_INDEX ( p_bone , skeleton - > size ) ;
ERR_FAIL_COND ( skeleton - > use_2d ) ;
float * dataptr = skeleton - > data . ptrw ( ) + p_bone * 12 ;
dataptr [ 0 ] = p_transform . basis . elements [ 0 ] [ 0 ] ;
dataptr [ 1 ] = p_transform . basis . elements [ 0 ] [ 1 ] ;
dataptr [ 2 ] = p_transform . basis . elements [ 0 ] [ 2 ] ;
dataptr [ 3 ] = p_transform . origin . x ;
dataptr [ 4 ] = p_transform . basis . elements [ 1 ] [ 0 ] ;
dataptr [ 5 ] = p_transform . basis . elements [ 1 ] [ 1 ] ;
dataptr [ 6 ] = p_transform . basis . elements [ 1 ] [ 2 ] ;
dataptr [ 7 ] = p_transform . origin . y ;
dataptr [ 8 ] = p_transform . basis . elements [ 2 ] [ 0 ] ;
dataptr [ 9 ] = p_transform . basis . elements [ 2 ] [ 1 ] ;
dataptr [ 10 ] = p_transform . basis . elements [ 2 ] [ 2 ] ;
dataptr [ 11 ] = p_transform . origin . z ;
_skeleton_make_dirty ( skeleton ) ;
}
2020-10-17 05:08:21 +00:00
Transform3D RendererStorageRD : : skeleton_bone_get_transform ( RID p_skeleton , int p_bone ) const {
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
2020-10-17 05:08:21 +00:00
ERR_FAIL_COND_V ( ! skeleton , Transform3D ( ) ) ;
ERR_FAIL_INDEX_V ( p_bone , skeleton - > size , Transform3D ( ) ) ;
ERR_FAIL_COND_V ( skeleton - > use_2d , Transform3D ( ) ) ;
2019-09-23 21:53:05 +00:00
const float * dataptr = skeleton - > data . ptr ( ) + p_bone * 12 ;
2020-10-17 05:08:21 +00:00
Transform3D t ;
2019-09-23 21:53:05 +00:00
t . basis . elements [ 0 ] [ 0 ] = dataptr [ 0 ] ;
t . basis . elements [ 0 ] [ 1 ] = dataptr [ 1 ] ;
t . basis . elements [ 0 ] [ 2 ] = dataptr [ 2 ] ;
t . origin . x = dataptr [ 3 ] ;
t . basis . elements [ 1 ] [ 0 ] = dataptr [ 4 ] ;
t . basis . elements [ 1 ] [ 1 ] = dataptr [ 5 ] ;
t . basis . elements [ 1 ] [ 2 ] = dataptr [ 6 ] ;
t . origin . y = dataptr [ 7 ] ;
t . basis . elements [ 2 ] [ 0 ] = dataptr [ 8 ] ;
t . basis . elements [ 2 ] [ 1 ] = dataptr [ 9 ] ;
t . basis . elements [ 2 ] [ 2 ] = dataptr [ 10 ] ;
t . origin . z = dataptr [ 11 ] ;
return t ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : skeleton_bone_set_transform_2d ( RID p_skeleton , int p_bone , const Transform2D & p_transform ) {
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
ERR_FAIL_COND ( ! skeleton ) ;
ERR_FAIL_INDEX ( p_bone , skeleton - > size ) ;
ERR_FAIL_COND ( ! skeleton - > use_2d ) ;
float * dataptr = skeleton - > data . ptrw ( ) + p_bone * 8 ;
dataptr [ 0 ] = p_transform . elements [ 0 ] [ 0 ] ;
dataptr [ 1 ] = p_transform . elements [ 1 ] [ 0 ] ;
dataptr [ 2 ] = 0 ;
dataptr [ 3 ] = p_transform . elements [ 2 ] [ 0 ] ;
dataptr [ 4 ] = p_transform . elements [ 0 ] [ 1 ] ;
dataptr [ 5 ] = p_transform . elements [ 1 ] [ 1 ] ;
dataptr [ 6 ] = 0 ;
dataptr [ 7 ] = p_transform . elements [ 2 ] [ 1 ] ;
_skeleton_make_dirty ( skeleton ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Transform2D RendererStorageRD : : skeleton_bone_get_transform_2d ( RID p_skeleton , int p_bone ) const {
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
ERR_FAIL_COND_V ( ! skeleton , Transform2D ( ) ) ;
ERR_FAIL_INDEX_V ( p_bone , skeleton - > size , Transform2D ( ) ) ;
ERR_FAIL_COND_V ( ! skeleton - > use_2d , Transform2D ( ) ) ;
const float * dataptr = skeleton - > data . ptr ( ) + p_bone * 8 ;
Transform2D t ;
t . elements [ 0 ] [ 0 ] = dataptr [ 0 ] ;
t . elements [ 1 ] [ 0 ] = dataptr [ 1 ] ;
t . elements [ 2 ] [ 0 ] = dataptr [ 3 ] ;
t . elements [ 0 ] [ 1 ] = dataptr [ 4 ] ;
t . elements [ 1 ] [ 1 ] = dataptr [ 5 ] ;
t . elements [ 2 ] [ 1 ] = dataptr [ 7 ] ;
return t ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : skeleton_set_base_transform_2d ( RID p_skeleton , const Transform2D & p_base_transform ) {
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
ERR_FAIL_COND ( ! skeleton - > use_2d ) ;
skeleton - > base_transform_2d = p_base_transform ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _update_dirty_skeletons ( ) {
2019-09-23 21:53:05 +00:00
while ( skeleton_dirty_list ) {
Skeleton * skeleton = skeleton_dirty_list ;
if ( skeleton - > size ) {
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > buffer_update ( skeleton - > buffer , 0 , skeleton - > data . size ( ) * sizeof ( float ) , skeleton - > data . ptr ( ) ) ;
2019-09-23 21:53:05 +00:00
}
skeleton_dirty_list = skeleton - > dirty_list ;
2021-01-04 12:33:25 +00:00
skeleton - > dependency . changed_notify ( DEPENDENCY_CHANGED_SKELETON_BONES ) ;
2020-12-16 14:07:08 +00:00
skeleton - > version + + ;
2019-09-23 21:53:05 +00:00
skeleton - > dirty = false ;
skeleton - > dirty_list = nullptr ;
}
skeleton_dirty_list = nullptr ;
}
2019-09-07 01:51:27 +00:00
/* LIGHT */
2021-02-09 16:19:03 +00:00
void RendererStorageRD : : _light_initialize ( RID p_light , RS : : LightType p_type ) {
2019-09-07 01:51:27 +00:00
Light light ;
light . type = p_type ;
2020-03-27 18:21:27 +00:00
light . param [ RS : : LIGHT_PARAM_ENERGY ] = 1.0 ;
light . param [ RS : : LIGHT_PARAM_INDIRECT_ENERGY ] = 1.0 ;
light . param [ RS : : LIGHT_PARAM_SPECULAR ] = 0.5 ;
light . param [ RS : : LIGHT_PARAM_RANGE ] = 1.0 ;
2020-04-09 18:11:15 +00:00
light . param [ RS : : LIGHT_PARAM_SIZE ] = 0.0 ;
2020-12-31 12:42:56 +00:00
light . param [ RS : : LIGHT_PARAM_ATTENUATION ] = 1.0 ;
2020-03-27 18:21:27 +00:00
light . param [ RS : : LIGHT_PARAM_SPOT_ANGLE ] = 45 ;
2020-12-31 12:42:56 +00:00
light . param [ RS : : LIGHT_PARAM_SPOT_ATTENUATION ] = 1.0 ;
2020-03-27 18:21:27 +00:00
light . param [ RS : : LIGHT_PARAM_SHADOW_MAX_DISTANCE ] = 0 ;
light . param [ RS : : LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET ] = 0.1 ;
light . param [ RS : : LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET ] = 0.3 ;
light . param [ RS : : LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET ] = 0.6 ;
light . param [ RS : : LIGHT_PARAM_SHADOW_FADE_START ] = 0.8 ;
2020-04-08 01:51:52 +00:00
light . param [ RS : : LIGHT_PARAM_SHADOW_NORMAL_BIAS ] = 1.0 ;
2020-12-31 12:42:56 +00:00
light . param [ RS : : LIGHT_PARAM_SHADOW_BIAS ] = 0.02 ;
light . param [ RS : : LIGHT_PARAM_SHADOW_BLUR ] = 0 ;
2020-04-08 01:51:52 +00:00
light . param [ RS : : LIGHT_PARAM_SHADOW_PANCAKE_SIZE ] = 20.0 ;
2021-02-06 14:51:56 +00:00
light . param [ RS : : LIGHT_PARAM_SHADOW_VOLUMETRIC_FOG_FADE ] = 0.1 ;
2020-12-31 12:42:56 +00:00
light . param [ RS : : LIGHT_PARAM_TRANSMITTANCE_BIAS ] = 0.05 ;
2019-09-07 01:51:27 +00:00
2021-02-09 16:19:03 +00:00
light_owner . initialize_rid ( p_light , light ) ;
}
RID RendererStorageRD : : directional_light_allocate ( ) {
return light_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : directional_light_initialize ( RID p_light ) {
_light_initialize ( p_light , RS : : LIGHT_DIRECTIONAL ) ;
}
RID RendererStorageRD : : omni_light_allocate ( ) {
return light_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : omni_light_initialize ( RID p_light ) {
_light_initialize ( p_light , RS : : LIGHT_OMNI ) ;
}
RID RendererStorageRD : : spot_light_allocate ( ) {
return light_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : spot_light_initialize ( RID p_light ) {
_light_initialize ( p_light , RS : : LIGHT_SPOT ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_color ( RID p_light , const Color & p_color ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > color = p_color ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_param ( RID p_light , RS : : LightParam p_param , float p_value ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_INDEX ( p_param , RS : : LIGHT_PARAM_MAX ) ;
2019-09-07 01:51:27 +00:00
switch ( p_param ) {
2020-03-27 18:21:27 +00:00
case RS : : LIGHT_PARAM_RANGE :
case RS : : LIGHT_PARAM_SPOT_ANGLE :
case RS : : LIGHT_PARAM_SHADOW_MAX_DISTANCE :
case RS : : LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET :
case RS : : LIGHT_PARAM_SHADOW_SPLIT_2_OFFSET :
case RS : : LIGHT_PARAM_SHADOW_SPLIT_3_OFFSET :
case RS : : LIGHT_PARAM_SHADOW_NORMAL_BIAS :
2020-04-08 01:51:52 +00:00
case RS : : LIGHT_PARAM_SHADOW_PANCAKE_SIZE :
2020-03-27 18:21:27 +00:00
case RS : : LIGHT_PARAM_SHADOW_BIAS : {
2019-09-07 01:51:27 +00:00
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2019-09-07 01:51:27 +00:00
} break ;
default : {
}
}
light - > param [ p_param ] = p_value ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_shadow ( RID p_light , bool p_enabled ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > shadow = p_enabled ;
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_shadow_color ( RID p_light , const Color & p_color ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > shadow_color = p_color ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_projector ( RID p_light , RID p_texture ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
2020-04-14 20:05:45 +00:00
if ( light - > projector = = p_texture ) {
return ;
}
if ( light - > type ! = RS : : LIGHT_DIRECTIONAL & & light - > projector . is_valid ( ) ) {
texture_remove_from_decal_atlas ( light - > projector , light - > type = = RS : : LIGHT_OMNI ) ;
}
2019-09-07 01:51:27 +00:00
light - > projector = p_texture ;
2020-04-14 20:05:45 +00:00
if ( light - > type ! = RS : : LIGHT_DIRECTIONAL & & light - > projector . is_valid ( ) ) {
texture_add_to_decal_atlas ( light - > projector , light - > type = = RS : : LIGHT_OMNI ) ;
}
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_negative ( RID p_light , bool p_enable ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > negative = p_enable ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_cull_mask ( RID p_light , uint32_t p_mask ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > cull_mask = p_mask ;
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_reverse_cull_face_mode ( RID p_light , bool p_enabled ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > reverse_cull = p_enabled ;
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_bake_mode ( RID p_light , RS : : LightBakeMode p_bake_mode ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
2020-06-25 13:33:28 +00:00
light - > bake_mode = p_bake_mode ;
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2020-06-25 13:33:28 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_set_max_sdfgi_cascade ( RID p_light , uint32_t p_cascade ) {
2020-06-25 13:33:28 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > max_sdfgi_cascade = p_cascade ;
2019-09-07 01:51:27 +00:00
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2019-09-07 01:51:27 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_omni_set_shadow_mode ( RID p_light , RS : : LightOmniShadowMode p_mode ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > omni_shadow_mode = p_mode ;
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
RS : : LightOmniShadowMode RendererStorageRD : : light_omni_get_shadow_mode ( RID p_light ) {
2019-09-07 01:51:27 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND_V ( ! light , RS : : LIGHT_OMNI_SHADOW_CUBE ) ;
2019-09-07 01:51:27 +00:00
return light - > omni_shadow_mode ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_directional_set_shadow_mode ( RID p_light , RS : : LightDirectionalShadowMode p_mode ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > directional_shadow_mode = p_mode ;
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_directional_set_blend_splits ( RID p_light , bool p_enable ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > directional_blend_splits = p_enable ;
light - > version + + ;
2021-01-04 12:33:25 +00:00
light - > dependency . changed_notify ( DEPENDENCY_CHANGED_LIGHT ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : light_directional_get_blend_splits ( RID p_light ) const {
2019-09-07 01:51:27 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND_V ( ! light , false ) ;
return light - > directional_blend_splits ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_directional_set_sky_only ( RID p_light , bool p_sky_only ) {
2020-10-22 06:22:53 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > directional_sky_only = p_sky_only ;
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : light_directional_is_sky_only ( RID p_light ) const {
2020-10-22 06:22:53 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND_V ( ! light , false ) ;
return light - > directional_sky_only ;
}
2020-12-04 18:26:24 +00:00
RS : : LightDirectionalShadowMode RendererStorageRD : : light_directional_get_shadow_mode ( RID p_light ) {
2019-09-07 01:51:27 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND_V ( ! light , RS : : LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL ) ;
2019-09-07 01:51:27 +00:00
return light - > directional_shadow_mode ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : light_directional_set_shadow_depth_range_mode ( RID p_light , RS : : LightDirectionalShadowDepthRangeMode p_range_mode ) {
2019-09-07 01:51:27 +00:00
Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND ( ! light ) ;
light - > directional_range_mode = p_range_mode ;
}
2020-12-04 18:26:24 +00:00
RS : : LightDirectionalShadowDepthRangeMode RendererStorageRD : : light_directional_get_shadow_depth_range_mode ( RID p_light ) const {
2019-09-07 01:51:27 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND_V ( ! light , RS : : LIGHT_DIRECTIONAL_SHADOW_DEPTH_RANGE_STABLE ) ;
2019-09-07 01:51:27 +00:00
return light - > directional_range_mode ;
}
2020-12-04 18:26:24 +00:00
uint32_t RendererStorageRD : : light_get_max_sdfgi_cascade ( RID p_light ) {
2020-06-25 13:33:28 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND_V ( ! light , 0 ) ;
return light - > max_sdfgi_cascade ;
}
2020-12-04 18:26:24 +00:00
RS : : LightBakeMode RendererStorageRD : : light_get_bake_mode ( RID p_light ) {
2020-06-25 13:33:28 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND_V ( ! light , RS : : LIGHT_BAKE_DISABLED ) ;
2019-09-07 01:51:27 +00:00
2020-06-25 13:33:28 +00:00
return light - > bake_mode ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
uint64_t RendererStorageRD : : light_get_version ( RID p_light ) const {
2019-09-07 01:51:27 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND_V ( ! light , 0 ) ;
return light - > version ;
}
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : light_get_aabb ( RID p_light ) const {
2019-09-07 01:51:27 +00:00
const Light * light = light_owner . getornull ( p_light ) ;
ERR_FAIL_COND_V ( ! light , AABB ( ) ) ;
switch ( light - > type ) {
2020-03-27 18:21:27 +00:00
case RS : : LIGHT_SPOT : {
float len = light - > param [ RS : : LIGHT_PARAM_RANGE ] ;
float size = Math : : tan ( Math : : deg2rad ( light - > param [ RS : : LIGHT_PARAM_SPOT_ANGLE ] ) ) * len ;
2019-09-07 01:51:27 +00:00
return AABB ( Vector3 ( - size , - size , - len ) , Vector3 ( size * 2 , size * 2 , len ) ) ;
} ;
2020-03-27 18:21:27 +00:00
case RS : : LIGHT_OMNI : {
float r = light - > param [ RS : : LIGHT_PARAM_RANGE ] ;
2019-09-07 01:51:27 +00:00
return AABB ( - Vector3 ( r , r , r ) , Vector3 ( r , r , r ) * 2 ) ;
} ;
2020-03-27 18:21:27 +00:00
case RS : : LIGHT_DIRECTIONAL : {
2019-09-07 01:51:27 +00:00
return AABB ( ) ;
} ;
}
ERR_FAIL_V ( AABB ( ) ) ;
}
/* REFLECTION PROBE */
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : reflection_probe_allocate ( ) {
return reflection_probe_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : reflection_probe_initialize ( RID p_reflection_probe ) {
reflection_probe_owner . initialize_rid ( p_reflection_probe , ReflectionProbe ( ) ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_update_mode ( RID p_probe , RS : : ReflectionProbeUpdateMode p_mode ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > update_mode = p_mode ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . changed_notify ( DEPENDENCY_CHANGED_REFLECTION_PROBE ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_intensity ( RID p_probe , float p_intensity ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > intensity = p_intensity ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_ambient_mode ( RID p_probe , RS : : ReflectionProbeAmbientMode p_mode ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
2020-06-25 13:33:28 +00:00
reflection_probe - > ambient_mode = p_mode ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_ambient_color ( RID p_probe , const Color & p_color ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
2020-06-25 13:33:28 +00:00
reflection_probe - > ambient_color = p_color ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_ambient_energy ( RID p_probe , float p_energy ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
2020-06-25 13:33:28 +00:00
reflection_probe - > ambient_color_energy = p_energy ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_max_distance ( RID p_probe , float p_distance ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > max_distance = p_distance ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . changed_notify ( DEPENDENCY_CHANGED_REFLECTION_PROBE ) ;
2019-09-07 01:51:27 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_extents ( RID p_probe , const Vector3 & p_extents ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
2020-10-08 00:29:49 +00:00
if ( reflection_probe - > extents = = p_extents ) {
return ;
}
2019-09-07 01:51:27 +00:00
reflection_probe - > extents = p_extents ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . changed_notify ( DEPENDENCY_CHANGED_REFLECTION_PROBE ) ;
2019-09-07 01:51:27 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_origin_offset ( RID p_probe , const Vector3 & p_offset ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > origin_offset = p_offset ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . changed_notify ( DEPENDENCY_CHANGED_REFLECTION_PROBE ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_as_interior ( RID p_probe , bool p_enable ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > interior = p_enable ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . changed_notify ( DEPENDENCY_CHANGED_REFLECTION_PROBE ) ;
2019-09-07 01:51:27 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_enable_box_projection ( RID p_probe , bool p_enable ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > box_projection = p_enable ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_enable_shadows ( RID p_probe , bool p_enable ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > enable_shadows = p_enable ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . changed_notify ( DEPENDENCY_CHANGED_REFLECTION_PROBE ) ;
2019-09-07 01:51:27 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_cull_mask ( RID p_probe , uint32_t p_layers ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > cull_mask = p_layers ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . changed_notify ( DEPENDENCY_CHANGED_REFLECTION_PROBE ) ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : reflection_probe_set_resolution ( RID p_probe , int p_resolution ) {
2019-09-07 01:51:27 +00:00
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
ERR_FAIL_COND ( p_resolution < 32 ) ;
reflection_probe - > resolution = p_resolution ;
}
2020-12-17 18:56:59 +00:00
void RendererStorageRD : : reflection_probe_set_lod_threshold ( RID p_probe , float p_ratio ) {
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND ( ! reflection_probe ) ;
reflection_probe - > lod_threshold = p_ratio ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . changed_notify ( DEPENDENCY_CHANGED_REFLECTION_PROBE ) ;
2020-12-17 18:56:59 +00:00
}
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : reflection_probe_get_aabb ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , AABB ( ) ) ;
AABB aabb ;
aabb . position = - reflection_probe - > extents ;
aabb . size = reflection_probe - > extents * 2.0 ;
return aabb ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
RS : : ReflectionProbeUpdateMode RendererStorageRD : : reflection_probe_get_update_mode ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
2020-03-27 18:21:27 +00:00
ERR_FAIL_COND_V ( ! reflection_probe , RS : : REFLECTION_PROBE_UPDATE_ALWAYS ) ;
2019-09-07 01:51:27 +00:00
return reflection_probe - > update_mode ;
}
2020-12-04 18:26:24 +00:00
uint32_t RendererStorageRD : : reflection_probe_get_cull_mask ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , 0 ) ;
return reflection_probe - > cull_mask ;
}
2020-12-04 18:26:24 +00:00
Vector3 RendererStorageRD : : reflection_probe_get_extents ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , Vector3 ( ) ) ;
return reflection_probe - > extents ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Vector3 RendererStorageRD : : reflection_probe_get_origin_offset ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , Vector3 ( ) ) ;
return reflection_probe - > origin_offset ;
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : reflection_probe_renders_shadows ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , false ) ;
return reflection_probe - > enable_shadows ;
}
2020-12-04 18:26:24 +00:00
float RendererStorageRD : : reflection_probe_get_origin_max_distance ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , 0 ) ;
return reflection_probe - > max_distance ;
}
2020-12-17 18:56:59 +00:00
float RendererStorageRD : : reflection_probe_get_lod_threshold ( RID p_probe ) const {
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , 0 ) ;
return reflection_probe - > lod_threshold ;
}
2020-12-04 18:26:24 +00:00
int RendererStorageRD : : reflection_probe_get_resolution ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , 0 ) ;
return reflection_probe - > resolution ;
}
2020-12-04 18:26:24 +00:00
float RendererStorageRD : : reflection_probe_get_intensity ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , 0 ) ;
return reflection_probe - > intensity ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : reflection_probe_is_interior ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , false ) ;
return reflection_probe - > interior ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : reflection_probe_is_box_projection ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , false ) ;
return reflection_probe - > box_projection ;
}
2020-12-04 18:26:24 +00:00
RS : : ReflectionProbeAmbientMode RendererStorageRD : : reflection_probe_get_ambient_mode ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
2020-06-25 13:33:28 +00:00
ERR_FAIL_COND_V ( ! reflection_probe , RS : : REFLECTION_PROBE_AMBIENT_DISABLED ) ;
return reflection_probe - > ambient_mode ;
2019-09-07 01:51:27 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Color RendererStorageRD : : reflection_probe_get_ambient_color ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
2020-06-25 13:33:28 +00:00
ERR_FAIL_COND_V ( ! reflection_probe , Color ( ) ) ;
2019-09-07 01:51:27 +00:00
2020-06-25 13:33:28 +00:00
return reflection_probe - > ambient_color ;
2019-09-07 01:51:27 +00:00
}
2020-12-04 18:26:24 +00:00
float RendererStorageRD : : reflection_probe_get_ambient_color_energy ( RID p_probe ) const {
2019-09-07 01:51:27 +00:00
const ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_probe ) ;
ERR_FAIL_COND_V ( ! reflection_probe , 0 ) ;
2020-06-25 13:33:28 +00:00
return reflection_probe - > ambient_color_energy ;
2019-09-07 01:51:27 +00:00
}
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : decal_allocate ( ) {
return decal_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : decal_initialize ( RID p_decal ) {
decal_owner . initialize_rid ( p_decal , Decal ( ) ) ;
2020-04-14 03:05:21 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_extents ( RID p_decal , const Vector3 & p_extents ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
decal - > extents = p_extents ;
2021-01-04 12:33:25 +00:00
decal - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2020-04-14 03:05:21 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_texture ( RID p_decal , RS : : DecalTexture p_type , RID p_texture ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
ERR_FAIL_INDEX ( p_type , RS : : DECAL_TEXTURE_MAX ) ;
if ( decal - > textures [ p_type ] = = p_texture ) {
return ;
}
ERR_FAIL_COND ( p_texture . is_valid ( ) & & ! texture_owner . owns ( p_texture ) ) ;
if ( decal - > textures [ p_type ] . is_valid ( ) & & texture_owner . owns ( decal - > textures [ p_type ] ) ) {
texture_remove_from_decal_atlas ( decal - > textures [ p_type ] ) ;
}
decal - > textures [ p_type ] = p_texture ;
if ( decal - > textures [ p_type ] . is_valid ( ) ) {
texture_add_to_decal_atlas ( decal - > textures [ p_type ] ) ;
}
2021-01-04 12:33:25 +00:00
decal - > dependency . changed_notify ( DEPENDENCY_CHANGED_DECAL ) ;
2020-04-14 03:05:21 +00:00
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_emission_energy ( RID p_decal , float p_energy ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
decal - > emission_energy = p_energy ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_albedo_mix ( RID p_decal , float p_mix ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
decal - > albedo_mix = p_mix ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_modulate ( RID p_decal , const Color & p_modulate ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
decal - > modulate = p_modulate ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_cull_mask ( RID p_decal , uint32_t p_layers ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
decal - > cull_mask = p_layers ;
2021-01-04 12:33:25 +00:00
decal - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2020-04-14 03:05:21 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_distance_fade ( RID p_decal , bool p_enabled , float p_begin , float p_length ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
decal - > distance_fade = p_enabled ;
decal - > distance_fade_begin = p_begin ;
decal - > distance_fade_length = p_length ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_fade ( RID p_decal , float p_above , float p_below ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
decal - > upper_fade = p_above ;
decal - > lower_fade = p_below ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : decal_set_normal_fade ( RID p_decal , float p_fade ) {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND ( ! decal ) ;
decal - > normal_fade = p_fade ;
}
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : decal_get_aabb ( RID p_decal ) const {
2020-04-14 03:05:21 +00:00
Decal * decal = decal_owner . getornull ( p_decal ) ;
ERR_FAIL_COND_V ( ! decal , AABB ( ) ) ;
return AABB ( - decal - > extents , decal - > extents * 2.0 ) ;
}
2021-06-04 22:47:26 +00:00
RID RendererStorageRD : : voxel_gi_allocate ( ) {
return voxel_gi_owner . allocate_rid ( ) ;
2021-02-09 16:19:03 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_initialize ( RID p_voxel_gi ) {
voxel_gi_owner . initialize_rid ( p_voxel_gi , VoxelGI ( ) ) ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : 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 . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +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 ) ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
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 ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
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 ;
2019-10-03 20:39:08 +00:00
2019-10-11 02:14:56 +00:00
if ( p_octree_cells . size ( ) ) {
2019-10-03 20:39:08 +00:00
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 ;
2020-02-13 14:53:32 +00:00
ERR_FAIL_COND ( p_data_cells . size ( ) ! = ( int ) cell_count * 16 ) ; //see that data size matches
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
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 ( ) ;
2019-10-11 02:14:56 +00:00
2019-10-13 00:24:03 +00:00
if ( p_distance_field . size ( ) ) {
RD : : TextureFormat tf ;
tf . format = RD : : DATA_FORMAT_R8_UNORM ;
2021-06-04 22:47:26 +00:00
tf . width = voxel_gi - > octree_size . x ;
tf . height = voxel_gi - > octree_size . y ;
tf . depth = voxel_gi - > octree_size . z ;
2020-11-27 03:50:05 +00:00
tf . texture_type = RD : : TEXTURE_TYPE_3D ;
2019-10-13 00:24:03 +00:00
tf . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > s ;
2019-10-13 00:24:03 +00:00
s . push_back ( p_distance_field ) ;
2021-06-04 22:47:26 +00:00
voxel_gi - > sdf_texture = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) , s ) ;
2019-10-13 00:24:03 +00:00
}
#if 0
2019-10-11 02:14:56 +00:00
{
RD : : TextureFormat tf ;
tf . format = RD : : DATA_FORMAT_R8_UNORM ;
2021-06-04 22:47:26 +00:00
tf . width = voxel_gi - > octree_size . x ;
tf . height = voxel_gi - > octree_size . y ;
tf . depth = voxel_gi - > octree_size . z ;
2019-10-11 02:14:56 +00:00
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 ) ;
2021-06-04 22:47:26 +00:00
voxel_gi - > sdf_texture = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
2019-10-11 02:14:56 +00:00
}
RID shared_tex ;
{
RD : : TextureView tv ;
tv . format_override = RD : : DATA_FORMAT_R8_UINT ;
2021-06-04 22:47:26 +00:00
shared_tex = RD : : get_singleton ( ) - > texture_create_shared ( tv , voxel_gi - > sdf_texture ) ;
2019-10-11 02:14:56 +00:00
}
//update SDF texture
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2019-10-11 02:14:56 +00:00
u . binding = 1 ;
2021-06-04 22:47:26 +00:00
u . ids . push_back ( voxel_gi - > octree_buffer ) ;
2019-10-11 02:14:56 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2019-10-11 02:14:56 +00:00
u . binding = 2 ;
2021-06-04 22:47:26 +00:00
u . ids . push_back ( voxel_gi - > data_buffer ) ;
2019-10-11 02:14:56 +00:00
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
2019-10-11 02:14:56 +00:00
u . binding = 3 ;
u . ids . push_back ( shared_tex ) ;
uniforms . push_back ( u ) ;
}
2021-06-04 22:47:26 +00:00
RID uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , voxel_gi_sdf_shader_version_shader , 0 ) ;
2019-10-11 02:14:56 +00:00
{
uint32_t push_constant [ 4 ] = { 0 , 0 , 0 , 0 } ;
2021-06-04 22:47:26 +00:00
for ( int i = 0 ; i < voxel_gi - > level_counts . size ( ) - 1 ; i + + ) {
push_constant [ 0 ] + = voxel_gi - > level_counts [ i ] ;
2019-10-11 02:14:56 +00:00
}
2021-06-04 22:47:26 +00:00
push_constant [ 1 ] = push_constant [ 0 ] + voxel_gi - > level_counts [ voxel_gi - > level_counts . size ( ) - 1 ] ;
2019-10-11 02:14:56 +00:00
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 ( ) ;
2021-06-04 22:47:26 +00:00
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , voxel_gi_sdf_shader_pipeline ) ;
2019-10-11 02:14:56 +00:00
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 ) ;
2021-06-04 22:47:26 +00:00
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 ) ;
2019-10-11 02:14:56 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
RD : : get_singleton ( ) - > free ( uniform_set ) ;
RD : : get_singleton ( ) - > free ( shared_tex ) ;
}
2019-10-13 00:24:03 +00:00
# endif
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
voxel_gi - > version + + ;
voxel_gi - > data_version + + ;
2019-10-11 02:14:56 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > dependency . changed_notify ( DEPENDENCY_CHANGED_AABB ) ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
AABB RendererStorageRD : : voxel_gi_get_bounds ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , AABB ( ) ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
return voxel_gi - > bounds ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
Vector3i RendererStorageRD : : voxel_gi_get_octree_size ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , Vector3i ( ) ) ;
return voxel_gi - > octree_size ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
Vector < uint8_t > RendererStorageRD : : voxel_gi_get_octree_cells ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , Vector < uint8_t > ( ) ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > octree_buffer . is_valid ( ) ) {
return RD : : get_singleton ( ) - > buffer_get_data ( voxel_gi - > octree_buffer ) ;
2019-10-03 20:39:08 +00:00
}
2020-02-17 21:06:54 +00:00
return Vector < uint8_t > ( ) ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
Vector < uint8_t > RendererStorageRD : : voxel_gi_get_data_cells ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , Vector < uint8_t > ( ) ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > data_buffer . is_valid ( ) ) {
return RD : : get_singleton ( ) - > buffer_get_data ( voxel_gi - > data_buffer ) ;
2019-10-03 20:39:08 +00:00
}
2020-02-17 21:06:54 +00:00
return Vector < uint8_t > ( ) ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
Vector < uint8_t > RendererStorageRD : : voxel_gi_get_distance_field ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , Vector < uint8_t > ( ) ) ;
2019-10-13 00:24:03 +00:00
2021-06-04 22:47:26 +00:00
if ( voxel_gi - > data_buffer . is_valid ( ) ) {
return RD : : get_singleton ( ) - > texture_get_data ( voxel_gi - > sdf_texture , 0 ) ;
2019-10-13 00:24:03 +00:00
}
2020-02-17 21:06:54 +00:00
return Vector < uint8_t > ( ) ;
2019-10-13 00:24:03 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
Vector < int > RendererStorageRD : : voxel_gi_get_level_counts ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , Vector < int > ( ) ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
return voxel_gi - > level_counts ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
Transform3D RendererStorageRD : : voxel_gi_get_to_cell_xform ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , Transform3D ( ) ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
return voxel_gi - > to_cell_xform ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_dynamic_range ( RID p_voxel_gi , float p_range ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > dynamic_range = p_range ;
voxel_gi - > version + + ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
float RendererStorageRD : : voxel_gi_get_dynamic_range ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
return voxel_gi - > dynamic_range ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_propagation ( RID p_voxel_gi , float p_range ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > propagation = p_range ;
voxel_gi - > version + + ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
float RendererStorageRD : : voxel_gi_get_propagation ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > propagation ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_energy ( RID p_voxel_gi , float p_energy ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > energy = p_energy ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
float RendererStorageRD : : voxel_gi_get_energy ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > energy ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_ao ( RID p_voxel_gi , float p_ao ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-14 06:45:44 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > ao = p_ao ;
2019-10-14 06:45:44 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
float RendererStorageRD : : voxel_gi_get_ao ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > ao ;
2019-10-14 06:45:44 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_ao_size ( RID p_voxel_gi , float p_strength ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-14 06:45:44 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > ao_size = p_strength ;
2019-10-14 06:45:44 +00:00
}
2021-06-04 22:47:26 +00:00
float RendererStorageRD : : voxel_gi_get_ao_size ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > ao_size ;
2019-10-14 06:45:44 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_bias ( RID p_voxel_gi , float p_bias ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > bias = p_bias ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
float RendererStorageRD : : voxel_gi_get_bias ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > bias ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_normal_bias ( RID p_voxel_gi , float p_normal_bias ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > normal_bias = p_normal_bias ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
float RendererStorageRD : : voxel_gi_get_normal_bias ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > normal_bias ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_anisotropy_strength ( RID p_voxel_gi , float p_strength ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > anisotropy_strength = p_strength ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
float RendererStorageRD : : voxel_gi_get_anisotropy_strength ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > anisotropy_strength ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_interior ( RID p_voxel_gi , bool p_enable ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > interior = p_enable ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
void RendererStorageRD : : voxel_gi_set_use_two_bounces ( RID p_voxel_gi , bool p_enable ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND ( ! voxel_gi ) ;
2019-10-03 20:39:08 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi - > use_two_bounces = p_enable ;
voxel_gi - > version + + ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
bool RendererStorageRD : : voxel_gi_is_using_two_bounces ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , false ) ;
return voxel_gi - > use_two_bounces ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
bool RendererStorageRD : : voxel_gi_is_interior ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > interior ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
uint32_t RendererStorageRD : : voxel_gi_get_version ( RID p_voxel_gi ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > version ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
uint32_t RendererStorageRD : : voxel_gi_get_data_version ( RID p_voxel_gi ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , 0 ) ;
return voxel_gi - > data_version ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
RID RendererStorageRD : : voxel_gi_get_octree_buffer ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , RID ( ) ) ;
return voxel_gi - > octree_buffer ;
2019-10-03 20:39:08 +00:00
}
2020-05-14 12:29:06 +00:00
2021-06-04 22:47:26 +00:00
RID RendererStorageRD : : voxel_gi_get_data_buffer ( RID p_voxel_gi ) const {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , RID ( ) ) ;
return voxel_gi - > data_buffer ;
2019-10-03 20:39:08 +00:00
}
2021-06-04 22:47:26 +00:00
RID RendererStorageRD : : voxel_gi_get_sdf_texture ( RID p_voxel_gi ) {
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_voxel_gi ) ;
ERR_FAIL_COND_V ( ! voxel_gi , RID ( ) ) ;
2019-10-11 02:14:56 +00:00
2021-06-04 22:47:26 +00:00
return voxel_gi - > sdf_texture ;
2019-10-11 02:14:56 +00:00
}
2020-05-14 12:29:06 +00:00
2020-05-01 12:34:23 +00:00
/* LIGHTMAP API */
2021-02-09 16:19:03 +00:00
RID RendererStorageRD : : lightmap_allocate ( ) {
return lightmap_owner . allocate_rid ( ) ;
}
void RendererStorageRD : : lightmap_initialize ( RID p_lightmap ) {
lightmap_owner . initialize_rid ( p_lightmap , Lightmap ( ) ) ;
2020-05-01 12:34:23 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : lightmap_set_textures ( RID p_lightmap , RID p_light , bool p_uses_spherical_haromics ) {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND ( ! lm ) ;
lightmap_array_version + + ;
//erase lightmap users
if ( lm - > light_texture . is_valid ( ) ) {
Texture * t = texture_owner . getornull ( lm - > light_texture ) ;
if ( t ) {
t - > lightmap_users . erase ( p_lightmap ) ;
}
}
Texture * t = texture_owner . getornull ( p_light ) ;
lm - > light_texture = p_light ;
lm - > uses_spherical_harmonics = p_uses_spherical_haromics ;
RID default_2d_array = default_rd_textures [ DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE ] ;
if ( ! t ) {
if ( using_lightmap_array ) {
if ( lm - > array_index > = 0 ) {
lightmap_textures . write [ lm - > array_index ] = default_2d_array ;
lm - > array_index = - 1 ;
}
}
return ;
}
t - > lightmap_users . insert ( p_lightmap ) ;
if ( using_lightmap_array ) {
if ( lm - > array_index < 0 ) {
//not in array, try to put in array
for ( int i = 0 ; i < lightmap_textures . size ( ) ; i + + ) {
if ( lightmap_textures [ i ] = = default_2d_array ) {
lm - > array_index = i ;
break ;
}
}
}
ERR_FAIL_COND_MSG ( lm - > array_index < 0 , " Maximum amount of lightmaps in use ( " + itos ( lightmap_textures . size ( ) ) + " ) has been exceeded, lightmap will nod display properly. " ) ;
lightmap_textures . write [ lm - > array_index ] = t - > rd_texture ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : lightmap_set_probe_bounds ( RID p_lightmap , const AABB & p_bounds ) {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND ( ! lm ) ;
lm - > bounds = p_bounds ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : lightmap_set_probe_interior ( RID p_lightmap , bool p_interior ) {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND ( ! lm ) ;
lm - > interior = p_interior ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : lightmap_set_probe_capture_data ( RID p_lightmap , const PackedVector3Array & p_points , const PackedColorArray & p_point_sh , const PackedInt32Array & p_tetrahedra , const PackedInt32Array & p_bsp_tree ) {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND ( ! lm ) ;
if ( p_points . size ( ) ) {
ERR_FAIL_COND ( p_points . size ( ) * 9 ! = p_point_sh . size ( ) ) ;
ERR_FAIL_COND ( ( p_tetrahedra . size ( ) % 4 ) ! = 0 ) ;
ERR_FAIL_COND ( ( p_bsp_tree . size ( ) % 6 ) ! = 0 ) ;
}
lm - > points = p_points ;
lm - > bsp_tree = p_bsp_tree ;
lm - > point_sh = p_point_sh ;
lm - > tetrahedra = p_tetrahedra ;
}
2020-12-04 18:26:24 +00:00
PackedVector3Array RendererStorageRD : : lightmap_get_probe_capture_points ( RID p_lightmap ) const {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND_V ( ! lm , PackedVector3Array ( ) ) ;
return lm - > points ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
PackedColorArray RendererStorageRD : : lightmap_get_probe_capture_sh ( RID p_lightmap ) const {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND_V ( ! lm , PackedColorArray ( ) ) ;
return lm - > point_sh ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
PackedInt32Array RendererStorageRD : : lightmap_get_probe_capture_tetrahedra ( RID p_lightmap ) const {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND_V ( ! lm , PackedInt32Array ( ) ) ;
return lm - > tetrahedra ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
PackedInt32Array RendererStorageRD : : lightmap_get_probe_capture_bsp_tree ( RID p_lightmap ) const {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND_V ( ! lm , PackedInt32Array ( ) ) ;
return lm - > bsp_tree ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : lightmap_set_probe_capture_update_speed ( float p_speed ) {
2020-05-01 12:34:23 +00:00
lightmap_probe_capture_update_speed = p_speed ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : lightmap_tap_sh_light ( RID p_lightmap , const Vector3 & p_point , Color * r_sh ) {
2020-05-01 12:34:23 +00:00
Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND ( ! lm ) ;
for ( int i = 0 ; i < 9 ; i + + ) {
r_sh [ i ] = Color ( 0 , 0 , 0 , 0 ) ;
}
if ( ! lm - > points . size ( ) | | ! lm - > bsp_tree . size ( ) | | ! lm - > tetrahedra . size ( ) ) {
return ;
}
static_assert ( sizeof ( Lightmap : : BSP ) = = 24 ) ;
const Lightmap : : BSP * bsp = ( const Lightmap : : BSP * ) lm - > bsp_tree . ptr ( ) ;
int32_t node = 0 ;
while ( node > = 0 ) {
if ( Plane ( bsp [ node ] . plane [ 0 ] , bsp [ node ] . plane [ 1 ] , bsp [ node ] . plane [ 2 ] , bsp [ node ] . plane [ 3 ] ) . is_point_over ( p_point ) ) {
# ifdef DEBUG_ENABLED
ERR_FAIL_COND ( bsp [ node ] . over > = 0 & & bsp [ node ] . over < node ) ;
# endif
node = bsp [ node ] . over ;
} else {
# ifdef DEBUG_ENABLED
ERR_FAIL_COND ( bsp [ node ] . under > = 0 & & bsp [ node ] . under < node ) ;
# endif
node = bsp [ node ] . under ;
}
}
if ( node = = Lightmap : : BSP : : EMPTY_LEAF ) {
return ; //nothing could be done
}
node = ABS ( node ) - 1 ;
uint32_t * tetrahedron = ( uint32_t * ) & lm - > tetrahedra [ node * 4 ] ;
Vector3 points [ 4 ] = { lm - > points [ tetrahedron [ 0 ] ] , lm - > points [ tetrahedron [ 1 ] ] , lm - > points [ tetrahedron [ 2 ] ] , lm - > points [ tetrahedron [ 3 ] ] } ;
const Color * sh_colors [ 4 ] { & lm - > point_sh [ tetrahedron [ 0 ] * 9 ] , & lm - > point_sh [ tetrahedron [ 1 ] * 9 ] , & lm - > point_sh [ tetrahedron [ 2 ] * 9 ] , & lm - > point_sh [ tetrahedron [ 3 ] * 9 ] } ;
2020-05-25 17:20:45 +00:00
Color barycentric = Geometry3D : : tetrahedron_get_barycentric_coords ( points [ 0 ] , points [ 1 ] , points [ 2 ] , points [ 3 ] , p_point ) ;
2020-05-01 12:34:23 +00:00
for ( int i = 0 ; i < 4 ; i + + ) {
float c = CLAMP ( barycentric [ i ] , 0.0 , 1.0 ) ;
for ( int j = 0 ; j < 9 ; j + + ) {
r_sh [ j ] + = sh_colors [ i ] [ j ] * c ;
}
}
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : lightmap_is_interior ( RID p_lightmap ) const {
2020-05-01 12:34:23 +00:00
const Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND_V ( ! lm , false ) ;
return lm - > interior ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
AABB RendererStorageRD : : lightmap_get_aabb ( RID p_lightmap ) const {
2020-05-01 12:34:23 +00:00
const Lightmap * lm = lightmap_owner . getornull ( p_lightmap ) ;
ERR_FAIL_COND_V ( ! lm , AABB ( ) ) ;
return lm - > bounds ;
}
2019-10-11 02:14:56 +00:00
2019-06-16 02:45:24 +00:00
/* RENDER TARGET API */
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _clear_render_target ( RenderTarget * rt ) {
2019-06-16 02:45:24 +00:00
//free in reverse dependency order
if ( rt - > framebuffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( rt - > framebuffer ) ;
2020-10-24 15:15:43 +00:00
rt - > framebuffer_uniform_set = RID ( ) ; //chain deleted
2019-06-16 02:45:24 +00:00
}
if ( rt - > color . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( rt - > color ) ;
}
2019-07-27 13:23:24 +00:00
if ( rt - > backbuffer . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( rt - > backbuffer ) ;
rt - > backbuffer = RID ( ) ;
for ( int i = 0 ; i < rt - > backbuffer_mipmaps . size ( ) ; i + + ) {
//just erase copies, since the rest are erased by dependency
RD : : get_singleton ( ) - > free ( rt - > backbuffer_mipmaps [ i ] . mipmap_copy ) ;
}
rt - > backbuffer_mipmaps . clear ( ) ;
2020-10-24 15:15:43 +00:00
rt - > backbuffer_uniform_set = RID ( ) ; //chain deleted
2019-07-27 13:23:24 +00:00
}
2020-11-26 12:50:21 +00:00
_render_target_clear_sdf ( rt ) ;
2019-06-16 02:45:24 +00:00
rt - > framebuffer = RID ( ) ;
rt - > color = RID ( ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _update_render_target ( RenderTarget * rt ) {
2019-06-24 19:13:06 +00:00
if ( rt - > texture . is_null ( ) ) {
//create a placeholder until updated
2021-02-09 16:19:03 +00:00
rt - > texture = texture_allocate ( ) ;
texture_2d_placeholder_initialize ( rt - > texture ) ;
2019-06-24 19:13:06 +00:00
Texture * tex = texture_owner . getornull ( rt - > texture ) ;
tex - > is_render_target = true ;
}
2019-06-16 02:45:24 +00:00
_clear_render_target ( rt ) ;
2019-06-19 20:03:19 +00:00
if ( rt - > size . width = = 0 | | rt - > size . height = = 0 ) {
return ;
}
2020-03-11 17:59:18 +00:00
//until we implement support for HDR monitors (and render target is attached to screen), this is enough.
2019-06-16 02:45:24 +00:00
rt - > color_format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
rt - > color_format_srgb = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
rt - > image_format = rt - > flags [ RENDER_TARGET_TRANSPARENT ] ? Image : : FORMAT_RGBA8 : Image : : FORMAT_RGB8 ;
RD : : TextureFormat rd_format ;
RD : : TextureView rd_view ;
{ //attempt register
rd_format . format = rt - > color_format ;
rd_format . width = rt - > size . width ;
rd_format . height = rt - > size . height ;
rd_format . depth = 1 ;
2021-05-07 13:19:04 +00:00
rd_format . array_layers = rt - > view_count ; // for stereo we create two (or more) layers, need to see if we can make fallback work like this too if we don't have multiview
2019-06-16 02:45:24 +00:00
rd_format . mipmaps = 1 ;
2021-05-07 13:19:04 +00:00
if ( rd_format . array_layers > 1 ) { // why are we not using rt->texture_type ??
rd_format . texture_type = RD : : TEXTURE_TYPE_2D_ARRAY ;
} else {
rd_format . texture_type = RD : : TEXTURE_TYPE_2D ;
}
2019-06-16 02:45:24 +00:00
rd_format . samples = RD : : TEXTURE_SAMPLES_1 ;
2019-07-27 13:23:24 +00:00
rd_format . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD : : TEXTURE_USAGE_CAN_COPY_FROM_BIT ;
2019-06-19 20:03:19 +00:00
rd_format . shareable_formats . push_back ( rt - > color_format ) ;
rd_format . shareable_formats . push_back ( rt - > color_format_srgb ) ;
2019-06-16 02:45:24 +00:00
}
rt - > color = RD : : get_singleton ( ) - > texture_create ( rd_format , rd_view ) ;
ERR_FAIL_COND ( rt - > color . is_null ( ) ) ;
Vector < RID > fb_textures ;
fb_textures . push_back ( rt - > color ) ;
2021-05-07 13:19:04 +00:00
rt - > framebuffer = RD : : get_singleton ( ) - > framebuffer_create ( fb_textures , RenderingDevice : : INVALID_ID , rt - > view_count ) ;
2019-06-16 02:45:24 +00:00
if ( rt - > framebuffer . is_null ( ) ) {
_clear_render_target ( rt ) ;
ERR_FAIL_COND ( rt - > framebuffer . is_null ( ) ) ;
}
2019-06-19 20:03:19 +00:00
2019-06-24 19:13:06 +00:00
{ //update texture
Texture * tex = texture_owner . getornull ( rt - > texture ) ;
//free existing textures
if ( RD : : get_singleton ( ) - > texture_is_valid ( tex - > rd_texture ) ) {
RD : : get_singleton ( ) - > free ( tex - > rd_texture ) ;
}
if ( RD : : get_singleton ( ) - > texture_is_valid ( tex - > rd_texture_srgb ) ) {
RD : : get_singleton ( ) - > free ( tex - > rd_texture_srgb ) ;
}
tex - > rd_texture = RID ( ) ;
tex - > rd_texture_srgb = RID ( ) ;
//create shared textures to the color buffer,
//so transparent can be supported
RD : : TextureView view ;
view . format_override = rt - > color_format ;
if ( ! rt - > flags [ RENDER_TARGET_TRANSPARENT ] ) {
view . swizzle_a = RD : : TEXTURE_SWIZZLE_ONE ;
}
tex - > rd_texture = RD : : get_singleton ( ) - > texture_create_shared ( view , rt - > color ) ;
if ( rt - > color_format_srgb ! = RD : : DATA_FORMAT_MAX ) {
view . format_override = rt - > color_format_srgb ;
tex - > rd_texture_srgb = RD : : get_singleton ( ) - > texture_create_shared ( view , rt - > color ) ;
}
tex - > rd_view = view ;
tex - > width = rt - > size . width ;
tex - > height = rt - > size . height ;
tex - > width_2d = rt - > size . width ;
tex - > height_2d = rt - > size . height ;
tex - > rd_format = rt - > color_format ;
tex - > rd_format_srgb = rt - > color_format_srgb ;
tex - > format = rt - > image_format ;
Vector < RID > proxies = tex - > proxies ; //make a copy, since update may change it
for ( int i = 0 ; i < proxies . size ( ) ; i + + ) {
texture_proxy_update ( proxies [ i ] , rt - > texture ) ;
}
}
2019-06-16 02:45:24 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _create_render_target_backbuffer ( RenderTarget * rt ) {
2019-07-27 13:23:24 +00:00
ERR_FAIL_COND ( rt - > backbuffer . is_valid ( ) ) ;
uint32_t mipmaps_required = Image : : get_image_required_mipmaps ( rt - > size . width , rt - > size . height , Image : : FORMAT_RGBA8 ) ;
RD : : TextureFormat tf ;
tf . format = rt - > color_format ;
tf . width = rt - > size . width ;
tf . height = rt - > size . height ;
2020-11-27 03:50:05 +00:00
tf . texture_type = RD : : TEXTURE_TYPE_2D ;
2020-10-28 18:34:27 +00:00
tf . usage_bits = RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_COPY_TO_BIT ;
2019-07-27 13:23:24 +00:00
tf . mipmaps = mipmaps_required ;
rt - > backbuffer = RD : : get_singleton ( ) - > texture_create ( tf , RD : : TextureView ( ) ) ;
2020-04-11 17:43:12 +00:00
rt - > backbuffer_mipmap0 = RD : : get_singleton ( ) - > texture_create_shared_from_slice ( RD : : TextureView ( ) , rt - > backbuffer , 0 , 0 ) ;
2019-07-27 13:23:24 +00:00
2020-10-28 18:34:27 +00:00
{
Vector < RID > fb_tex ;
fb_tex . push_back ( rt - > backbuffer_mipmap0 ) ;
rt - > backbuffer_fb = RD : : get_singleton ( ) - > framebuffer_create ( fb_tex ) ;
}
2020-10-24 15:15:43 +00:00
if ( rt - > framebuffer_uniform_set . is_valid ( ) & & RD : : get_singleton ( ) - > uniform_set_is_valid ( rt - > framebuffer_uniform_set ) ) {
//the new one will require the backbuffer.
RD : : get_singleton ( ) - > free ( rt - > framebuffer_uniform_set ) ;
rt - > framebuffer_uniform_set = RID ( ) ;
}
2019-07-27 13:23:24 +00:00
//create mipmaps
for ( uint32_t i = 1 ; i < mipmaps_required ; i + + ) {
RenderTarget : : BackbufferMipmap mm ;
{
mm . mipmap = RD : : get_singleton ( ) - > texture_create_shared_from_slice ( RD : : TextureView ( ) , rt - > backbuffer , 0 , i ) ;
}
{
Size2 mm_size = Image : : get_image_mipmap_size ( tf . width , tf . height , Image : : FORMAT_RGBA8 , i ) ;
RD : : TextureFormat mmtf = tf ;
mmtf . width = mm_size . width ;
mmtf . height = mm_size . height ;
mmtf . mipmaps = 1 ;
mm . mipmap_copy = RD : : get_singleton ( ) - > texture_create ( mmtf , RD : : TextureView ( ) ) ;
}
rt - > backbuffer_mipmaps . push_back ( mm ) ;
}
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_create ( ) {
2019-06-16 02:45:24 +00:00
RenderTarget render_target ;
2019-06-25 01:24:07 +00:00
2019-06-16 02:45:24 +00:00
render_target . was_used = false ;
2019-06-24 19:13:06 +00:00
render_target . clear_requested = false ;
2019-06-16 02:45:24 +00:00
for ( int i = 0 ; i < RENDER_TARGET_FLAG_MAX ; i + + ) {
render_target . flags [ i ] = false ;
}
2019-06-25 01:24:07 +00:00
_update_render_target ( & render_target ) ;
2019-06-16 02:45:24 +00:00
return render_target_owner . make_rid ( render_target ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_set_position ( RID p_render_target , int p_x , int p_y ) {
2019-06-16 02:45:24 +00:00
//unused for this render target
}
2021-05-07 13:19:04 +00:00
void RendererStorageRD : : render_target_set_size ( RID p_render_target , int p_width , int p_height , uint32_t p_view_count ) {
2019-06-16 02:45:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
2021-05-07 13:19:04 +00:00
if ( rt - > size . x ! = p_width | | rt - > size . y ! = p_height | | rt - > view_count ! = p_view_count ) {
rt - > size . x = p_width ;
rt - > size . y = p_height ;
rt - > view_count = p_view_count ;
_update_render_target ( rt ) ;
}
2019-06-16 02:45:24 +00:00
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_texture ( RID p_render_target ) {
2019-06-16 02:45:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
return rt - > texture ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_set_external_texture ( RID p_render_target , unsigned int p_texture_id ) {
2019-06-16 02:45:24 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_set_flag ( RID p_render_target , RenderTargetFlags p_flag , bool p_value ) {
2019-06-16 02:45:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
rt - > flags [ p_flag ] = p_value ;
2019-06-25 01:24:07 +00:00
_update_render_target ( rt ) ;
2019-06-16 02:45:24 +00:00
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : render_target_was_used ( RID p_render_target ) {
2019-06-16 02:45:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , false ) ;
return rt - > was_used ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_set_as_unused ( RID p_render_target ) {
2019-06-16 02:45:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
rt - > was_used = false ;
}
2020-12-04 18:26:24 +00:00
Size2 RendererStorageRD : : render_target_get_size ( RID p_render_target ) {
2019-06-16 02:45:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , Size2 ( ) ) ;
return rt - > size ;
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_rd_framebuffer ( RID p_render_target ) {
2019-06-16 02:45:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
return rt - > framebuffer ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_rd_texture ( RID p_render_target ) {
2020-04-11 17:43:12 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
2019-06-16 02:45:24 +00:00
2020-04-11 17:43:12 +00:00
return rt - > color ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_rd_backbuffer ( RID p_render_target ) {
2020-10-24 15:15:43 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
return rt - > backbuffer ;
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_rd_backbuffer_framebuffer ( RID p_render_target ) {
2020-10-28 18:34:27 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
if ( ! rt - > backbuffer . is_valid ( ) ) {
_create_render_target_backbuffer ( rt ) ;
}
return rt - > backbuffer_fb ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_request_clear ( RID p_render_target , const Color & p_clear_color ) {
2019-06-24 19:13:06 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
rt - > clear_requested = true ;
rt - > clear_color = p_clear_color ;
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : render_target_is_clear_requested ( RID p_render_target ) {
2019-06-24 19:13:06 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , false ) ;
return rt - > clear_requested ;
}
2020-12-04 18:26:24 +00:00
Color RendererStorageRD : : render_target_get_clear_request_color ( RID p_render_target ) {
2019-06-24 19:13:06 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , Color ( ) ) ;
return rt - > clear_color ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_disable_clear_request ( RID p_render_target ) {
2019-06-24 19:13:06 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
rt - > clear_requested = false ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_do_clear_request ( RID p_render_target ) {
2019-06-25 01:24:07 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
if ( ! rt - > clear_requested ) {
return ;
}
Vector < Color > clear_colors ;
clear_colors . push_back ( rt - > clear_color ) ;
2019-10-03 20:39:08 +00:00
RD : : get_singleton ( ) - > draw_list_begin ( rt - > framebuffer , RD : : INITIAL_ACTION_CLEAR , RD : : FINAL_ACTION_READ , RD : : INITIAL_ACTION_KEEP , RD : : FINAL_ACTION_DISCARD , clear_colors ) ;
2019-06-25 01:24:07 +00:00
RD : : get_singleton ( ) - > draw_list_end ( ) ;
rt - > clear_requested = false ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_set_sdf_size_and_scale ( RID p_render_target , RS : : ViewportSDFOversize p_size , RS : : ViewportSDFScale p_scale ) {
2020-11-26 12:50:21 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
if ( rt - > sdf_oversize = = p_size & & rt - > sdf_scale = = p_scale ) {
return ;
}
rt - > sdf_oversize = p_size ;
rt - > sdf_scale = p_scale ;
_render_target_clear_sdf ( rt ) ;
}
2020-12-04 18:26:24 +00:00
Rect2i RendererStorageRD : : _render_target_get_sdf_rect ( const RenderTarget * rt ) const {
2020-11-26 12:50:21 +00:00
Size2i margin ;
int scale ;
switch ( rt - > sdf_oversize ) {
case RS : : VIEWPORT_SDF_OVERSIZE_100_PERCENT : {
scale = 100 ;
} break ;
case RS : : VIEWPORT_SDF_OVERSIZE_120_PERCENT : {
scale = 120 ;
} break ;
case RS : : VIEWPORT_SDF_OVERSIZE_150_PERCENT : {
scale = 150 ;
} break ;
case RS : : VIEWPORT_SDF_OVERSIZE_200_PERCENT : {
scale = 200 ;
} break ;
default : {
}
}
margin = ( rt - > size * scale / 100 ) - rt - > size ;
Rect2i r ( Vector2i ( ) , rt - > size ) ;
r . position - = margin ;
r . size + = margin * 2 ;
return r ;
}
2020-12-04 18:26:24 +00:00
Rect2i RendererStorageRD : : render_target_get_sdf_rect ( RID p_render_target ) const {
2020-11-26 12:50:21 +00:00
const RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , Rect2i ( ) ) ;
return _render_target_get_sdf_rect ( rt ) ;
}
2021-05-20 14:25:06 +00:00
void RendererStorageRD : : render_target_mark_sdf_enabled ( RID p_render_target , bool p_enabled ) {
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
rt - > sdf_enabled = p_enabled ;
}
bool RendererStorageRD : : render_target_is_sdf_enabled ( RID p_render_target ) const {
const RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , false ) ;
return rt - > sdf_enabled ;
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_sdf_texture ( RID p_render_target ) {
2020-11-26 12:50:21 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
if ( rt - > sdf_buffer_read . is_null ( ) ) {
// no texture, create a dummy one for the 2D uniform set
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tformat . width = 4 ;
tformat . height = 4 ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_2D ;
2020-11-26 12:50:21 +00:00
Vector < uint8_t > pv ;
pv . resize ( 16 * 4 ) ;
2021-04-27 14:19:21 +00:00
memset ( pv . ptrw ( ) , 0 , 16 * 4 ) ;
2020-11-26 12:50:21 +00:00
Vector < Vector < uint8_t > > vpv ;
rt - > sdf_buffer_read = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
return rt - > sdf_buffer_read ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _render_target_allocate_sdf ( RenderTarget * rt ) {
2020-11-26 12:50:21 +00:00
ERR_FAIL_COND ( rt - > sdf_buffer_write_fb . is_valid ( ) ) ;
if ( rt - > sdf_buffer_read . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( rt - > sdf_buffer_read ) ;
rt - > sdf_buffer_read = RID ( ) ;
}
Size2i size = _render_target_get_sdf_rect ( rt ) . size ;
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8_UNORM ;
tformat . width = size . width ;
tformat . height = size . height ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT | RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_2D ;
2020-11-26 12:50:21 +00:00
rt - > sdf_buffer_write = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) ) ;
{
Vector < RID > write_fb ;
write_fb . push_back ( rt - > sdf_buffer_write ) ;
rt - > sdf_buffer_write_fb = RD : : get_singleton ( ) - > framebuffer_create ( write_fb ) ;
}
int scale ;
switch ( rt - > sdf_scale ) {
case RS : : VIEWPORT_SDF_SCALE_100_PERCENT : {
scale = 100 ;
} break ;
case RS : : VIEWPORT_SDF_SCALE_50_PERCENT : {
scale = 50 ;
} break ;
case RS : : VIEWPORT_SDF_SCALE_25_PERCENT : {
scale = 25 ;
} break ;
default : {
scale = 100 ;
} break ;
}
rt - > process_size = size * scale / 100 ;
rt - > process_size . x = MAX ( rt - > process_size . x , 1 ) ;
rt - > process_size . y = MAX ( rt - > process_size . y , 1 ) ;
2021-05-20 14:25:06 +00:00
tformat . format = RD : : DATA_FORMAT_R16G16_SINT ;
2020-11-26 12:50:21 +00:00
tformat . width = rt - > process_size . width ;
tformat . height = rt - > process_size . height ;
tformat . usage_bits = RD : : TEXTURE_USAGE_STORAGE_BIT ;
rt - > sdf_buffer_process [ 0 ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) ) ;
rt - > sdf_buffer_process [ 1 ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) ) ;
2021-05-20 14:25:06 +00:00
tformat . format = RD : : DATA_FORMAT_R16_SNORM ;
2020-11-26 12:50:21 +00:00
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_STORAGE_BIT ;
rt - > sdf_buffer_read = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) ) ;
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
2020-11-26 12:50:21 +00:00
u . binding = 1 ;
u . ids . push_back ( rt - > sdf_buffer_write ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
2020-11-26 12:50:21 +00:00
u . binding = 2 ;
u . ids . push_back ( rt - > sdf_buffer_read ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
2020-11-26 12:50:21 +00:00
u . binding = 3 ;
u . ids . push_back ( rt - > sdf_buffer_process [ 0 ] ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_IMAGE ;
2020-11-26 12:50:21 +00:00
u . binding = 4 ;
u . ids . push_back ( rt - > sdf_buffer_process [ 1 ] ) ;
uniforms . push_back ( u ) ;
}
rt - > sdf_buffer_process_uniform_sets [ 0 ] = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , rt_sdf . shader . version_get_shader ( rt_sdf . shader_version , 0 ) , 0 ) ;
SWAP ( uniforms . write [ 2 ] . ids . write [ 0 ] , uniforms . write [ 3 ] . ids . write [ 0 ] ) ;
rt - > sdf_buffer_process_uniform_sets [ 1 ] = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , rt_sdf . shader . version_get_shader ( rt_sdf . shader_version , 0 ) , 0 ) ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _render_target_clear_sdf ( RenderTarget * rt ) {
2020-11-26 12:50:21 +00:00
if ( rt - > sdf_buffer_read . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( rt - > sdf_buffer_read ) ;
rt - > sdf_buffer_read = RID ( ) ;
}
if ( rt - > sdf_buffer_write_fb . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( rt - > sdf_buffer_write ) ;
RD : : get_singleton ( ) - > free ( rt - > sdf_buffer_process [ 0 ] ) ;
RD : : get_singleton ( ) - > free ( rt - > sdf_buffer_process [ 1 ] ) ;
rt - > sdf_buffer_write = RID ( ) ;
rt - > sdf_buffer_write_fb = RID ( ) ;
rt - > sdf_buffer_process [ 0 ] = RID ( ) ;
rt - > sdf_buffer_process [ 1 ] = RID ( ) ;
rt - > sdf_buffer_process_uniform_sets [ 0 ] = RID ( ) ;
rt - > sdf_buffer_process_uniform_sets [ 1 ] = RID ( ) ;
}
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_sdf_framebuffer ( RID p_render_target ) {
2020-11-26 12:50:21 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
if ( rt - > sdf_buffer_write_fb . is_null ( ) ) {
_render_target_allocate_sdf ( rt ) ;
}
return rt - > sdf_buffer_write_fb ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_sdf_process ( RID p_render_target ) {
2020-11-26 12:50:21 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
ERR_FAIL_COND ( rt - > sdf_buffer_write_fb . is_null ( ) ) ;
RenderTargetSDF : : PushConstant push_constant ;
Rect2i r = _render_target_get_sdf_rect ( rt ) ;
push_constant . size [ 0 ] = r . size . width ;
push_constant . size [ 1 ] = r . size . height ;
push_constant . stride = 0 ;
push_constant . shift = 0 ;
push_constant . base_size [ 0 ] = r . size . width ;
push_constant . base_size [ 1 ] = r . size . height ;
bool shrink = false ;
switch ( rt - > sdf_scale ) {
case RS : : VIEWPORT_SDF_SCALE_50_PERCENT : {
push_constant . size [ 0 ] > > = 1 ;
push_constant . size [ 1 ] > > = 1 ;
push_constant . shift = 1 ;
shrink = true ;
} break ;
case RS : : VIEWPORT_SDF_SCALE_25_PERCENT : {
push_constant . size [ 0 ] > > = 2 ;
push_constant . size [ 1 ] > > = 2 ;
push_constant . shift = 2 ;
shrink = true ;
} break ;
default : {
} ;
}
RD : : ComputeListID compute_list = RD : : get_singleton ( ) - > compute_list_begin ( ) ;
/* Load */
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , rt_sdf . pipelines [ shrink ? RenderTargetSDF : : SHADER_LOAD_SHRINK : RenderTargetSDF : : SHADER_LOAD ] ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , rt - > sdf_buffer_process_uniform_sets [ 1 ] , 0 ) ; //fill [0]
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( RenderTargetSDF : : PushConstant ) ) ;
2021-02-02 19:51:36 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , push_constant . size [ 0 ] , push_constant . size [ 1 ] , 1 ) ;
2020-11-26 12:50:21 +00:00
/* Process */
int stride = nearest_power_of_2_templated ( MAX ( push_constant . size [ 0 ] , push_constant . size [ 1 ] ) / 2 ) ;
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , rt_sdf . pipelines [ RenderTargetSDF : : SHADER_PROCESS ] ) ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
bool swap = false ;
//jumpflood
while ( stride > 0 ) {
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , rt - > sdf_buffer_process_uniform_sets [ swap ? 1 : 0 ] , 0 ) ;
push_constant . stride = stride ;
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( RenderTargetSDF : : PushConstant ) ) ;
2021-02-02 19:51:36 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , push_constant . size [ 0 ] , push_constant . size [ 1 ] , 1 ) ;
2020-11-26 12:50:21 +00:00
stride / = 2 ;
swap = ! swap ;
RD : : get_singleton ( ) - > compute_list_add_barrier ( compute_list ) ;
}
/* Store */
RD : : get_singleton ( ) - > compute_list_bind_compute_pipeline ( compute_list , rt_sdf . pipelines [ shrink ? RenderTargetSDF : : SHADER_STORE_SHRINK : RenderTargetSDF : : SHADER_STORE ] ) ;
RD : : get_singleton ( ) - > compute_list_bind_uniform_set ( compute_list , rt - > sdf_buffer_process_uniform_sets [ swap ? 1 : 0 ] , 0 ) ;
RD : : get_singleton ( ) - > compute_list_set_push_constant ( compute_list , & push_constant , sizeof ( RenderTargetSDF : : PushConstant ) ) ;
2021-02-02 19:51:36 +00:00
RD : : get_singleton ( ) - > compute_list_dispatch_threads ( compute_list , push_constant . size [ 0 ] , push_constant . size [ 1 ] , 1 ) ;
2020-11-26 12:50:21 +00:00
RD : : get_singleton ( ) - > compute_list_end ( ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_copy_to_back_buffer ( RID p_render_target , const Rect2i & p_region , bool p_gen_mipmaps ) {
2019-07-27 13:23:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
if ( ! rt - > backbuffer . is_valid ( ) ) {
_create_render_target_backbuffer ( rt ) ;
}
2020-10-28 18:34:27 +00:00
Rect2i region ;
if ( p_region = = Rect2i ( ) ) {
2019-07-27 13:23:24 +00:00
region . size = rt - > size ;
2020-10-28 18:34:27 +00:00
} else {
2020-12-19 12:43:35 +00:00
region = Rect2i ( Size2i ( ) , rt - > size ) . intersection ( p_region ) ;
2020-10-28 18:34:27 +00:00
if ( region . size = = Size2i ( ) ) {
return ; //nothing to do
}
2019-07-27 13:23:24 +00:00
}
//single texture copy for backbuffer
2020-10-28 18:34:27 +00:00
//RD::get_singleton()->texture_copy(rt->color, rt->backbuffer_mipmap0, Vector3(region.position.x, region.position.y, 0), Vector3(region.position.x, region.position.y, 0), Vector3(region.size.x, region.size.y, 1), 0, 0, 0, 0, true);
effects . copy_to_rect ( rt - > color , rt - > backbuffer_mipmap0 , region , false , false , false , true , true ) ;
if ( ! p_gen_mipmaps ) {
return ;
}
2019-07-27 13:23:24 +00:00
//then mipmap blur
RID prev_texture = rt - > color ; //use color, not backbuffer, as bb has mipmaps.
for ( int i = 0 ; i < rt - > backbuffer_mipmaps . size ( ) ; i + + ) {
2020-04-11 17:43:12 +00:00
region . position . x > > = 1 ;
region . position . y > > = 1 ;
region . size . x = MAX ( 1 , region . size . x > > 1 ) ;
region . size . y = MAX ( 1 , region . size . y > > 1 ) ;
2019-07-27 13:23:24 +00:00
const RenderTarget : : BackbufferMipmap & mm = rt - > backbuffer_mipmaps [ i ] ;
2020-04-11 17:43:12 +00:00
effects . gaussian_blur ( prev_texture , mm . mipmap , mm . mipmap_copy , region , true ) ;
2019-07-27 13:23:24 +00:00
prev_texture = mm . mipmap ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_clear_back_buffer ( RID p_render_target , const Rect2i & p_region , const Color & p_color ) {
2020-10-28 18:34:27 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
if ( ! rt - > backbuffer . is_valid ( ) ) {
_create_render_target_backbuffer ( rt ) ;
}
Rect2i region ;
if ( p_region = = Rect2i ( ) ) {
region . size = rt - > size ;
} else {
2020-12-19 12:43:35 +00:00
region = Rect2i ( Size2i ( ) , rt - > size ) . intersection ( p_region ) ;
2020-10-28 18:34:27 +00:00
if ( region . size = = Size2i ( ) ) {
return ; //nothing to do
}
}
//single texture copy for backbuffer
effects . set_color ( rt - > backbuffer_mipmap0 , p_color , region , true ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_gen_back_buffer_mipmaps ( RID p_render_target , const Rect2i & p_region ) {
2020-10-28 18:34:27 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
if ( ! rt - > backbuffer . is_valid ( ) ) {
_create_render_target_backbuffer ( rt ) ;
}
Rect2i region ;
if ( p_region = = Rect2i ( ) ) {
region . size = rt - > size ;
} else {
2020-12-19 12:43:35 +00:00
region = Rect2i ( Size2i ( ) , rt - > size ) . intersection ( p_region ) ;
2020-10-28 18:34:27 +00:00
if ( region . size = = Size2i ( ) ) {
return ; //nothing to do
}
}
//then mipmap blur
RID prev_texture = rt - > backbuffer_mipmap0 ;
for ( int i = 0 ; i < rt - > backbuffer_mipmaps . size ( ) ; i + + ) {
region . position . x > > = 1 ;
region . position . y > > = 1 ;
region . size . x = MAX ( 1 , region . size . x > > 1 ) ;
region . size . y = MAX ( 1 , region . size . y > > 1 ) ;
const RenderTarget : : BackbufferMipmap & mm = rt - > backbuffer_mipmaps [ i ] ;
effects . gaussian_blur ( prev_texture , mm . mipmap , mm . mipmap_copy , region , true ) ;
prev_texture = mm . mipmap ;
}
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_framebuffer_uniform_set ( RID p_render_target ) {
2020-10-24 15:15:43 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
return rt - > framebuffer_uniform_set ;
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : render_target_get_backbuffer_uniform_set ( RID p_render_target ) {
2019-07-27 13:23:24 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND_V ( ! rt , RID ( ) ) ;
return rt - > backbuffer_uniform_set ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_set_framebuffer_uniform_set ( RID p_render_target , RID p_uniform_set ) {
2020-10-24 15:15:43 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
rt - > framebuffer_uniform_set = p_uniform_set ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : render_target_set_backbuffer_uniform_set ( RID p_render_target , RID p_uniform_set ) {
2020-10-24 15:15:43 +00:00
RenderTarget * rt = render_target_owner . getornull ( p_render_target ) ;
ERR_FAIL_COND ( ! rt ) ;
rt - > backbuffer_uniform_set = p_uniform_set ;
}
2021-01-04 12:33:25 +00:00
void RendererStorageRD : : base_update_dependency ( RID p_base , DependencyTracker * p_instance ) {
2019-08-18 22:40:52 +00:00
if ( mesh_owner . owns ( p_base ) ) {
Mesh * mesh = mesh_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & mesh - > dependency ) ;
2019-09-14 03:37:42 +00:00
} else if ( multimesh_owner . owns ( p_base ) ) {
MultiMesh * multimesh = multimesh_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & multimesh - > dependency ) ;
2019-09-14 03:37:42 +00:00
if ( multimesh - > mesh . is_valid ( ) ) {
base_update_dependency ( multimesh - > mesh , p_instance ) ;
}
2019-09-07 01:51:27 +00:00
} else if ( reflection_probe_owner . owns ( p_base ) ) {
ReflectionProbe * rp = reflection_probe_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & rp - > dependency ) ;
2020-04-14 03:05:21 +00:00
} else if ( decal_owner . owns ( p_base ) ) {
Decal * decal = decal_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & decal - > dependency ) ;
2021-06-04 22:47:26 +00:00
} else if ( voxel_gi_owner . owns ( p_base ) ) {
VoxelGI * gip = voxel_gi_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & gip - > dependency ) ;
2020-05-01 12:34:23 +00:00
} else if ( lightmap_owner . owns ( p_base ) ) {
Lightmap * lm = lightmap_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & lm - > dependency ) ;
2019-09-07 01:51:27 +00:00
} else if ( light_owner . owns ( p_base ) ) {
Light * l = light_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & l - > dependency ) ;
2020-08-19 13:38:24 +00:00
} else if ( particles_owner . owns ( p_base ) ) {
Particles * p = particles_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & p - > dependency ) ;
2020-10-08 00:29:49 +00:00
} else if ( particles_collision_owner . owns ( p_base ) ) {
ParticlesCollision * pc = particles_collision_owner . getornull ( p_base ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & pc - > dependency ) ;
2021-06-16 18:43:02 +00:00
} else if ( visibility_notifier_owner . owns ( p_base ) ) {
VisibilityNotifier * vn = visibility_notifier_owner . getornull ( p_base ) ;
p_instance - > update_dependency ( & vn - > dependency ) ;
2019-08-18 22:40:52 +00:00
}
}
2021-01-04 12:33:25 +00:00
void RendererStorageRD : : skeleton_update_dependency ( RID p_skeleton , DependencyTracker * p_instance ) {
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_skeleton ) ;
ERR_FAIL_COND ( ! skeleton ) ;
2021-01-04 12:33:25 +00:00
p_instance - > update_dependency ( & skeleton - > dependency ) ;
2019-09-23 21:53:05 +00:00
}
2020-12-04 18:26:24 +00:00
RS : : InstanceType RendererStorageRD : : get_base_type ( RID p_rid ) const {
2019-08-20 20:54:03 +00:00
if ( mesh_owner . owns ( p_rid ) ) {
2020-03-27 18:21:27 +00:00
return RS : : INSTANCE_MESH ;
2019-08-20 20:54:03 +00:00
}
2019-09-14 03:37:42 +00:00
if ( multimesh_owner . owns ( p_rid ) ) {
2020-03-27 18:21:27 +00:00
return RS : : INSTANCE_MULTIMESH ;
2019-09-14 03:37:42 +00:00
}
2019-09-07 01:51:27 +00:00
if ( reflection_probe_owner . owns ( p_rid ) ) {
2020-03-27 18:21:27 +00:00
return RS : : INSTANCE_REFLECTION_PROBE ;
2019-09-07 01:51:27 +00:00
}
2020-04-14 03:05:21 +00:00
if ( decal_owner . owns ( p_rid ) ) {
return RS : : INSTANCE_DECAL ;
}
2021-06-04 22:47:26 +00:00
if ( voxel_gi_owner . owns ( p_rid ) ) {
return RS : : INSTANCE_VOXEL_GI ;
2019-10-03 20:39:08 +00:00
}
2019-09-07 01:51:27 +00:00
if ( light_owner . owns ( p_rid ) ) {
2020-03-27 18:21:27 +00:00
return RS : : INSTANCE_LIGHT ;
2019-09-07 01:51:27 +00:00
}
2020-05-01 12:34:23 +00:00
if ( lightmap_owner . owns ( p_rid ) ) {
return RS : : INSTANCE_LIGHTMAP ;
}
2020-08-19 13:38:24 +00:00
if ( particles_owner . owns ( p_rid ) ) {
return RS : : INSTANCE_PARTICLES ;
}
2020-10-08 00:29:49 +00:00
if ( particles_collision_owner . owns ( p_rid ) ) {
return RS : : INSTANCE_PARTICLES_COLLISION ;
}
2021-06-16 18:43:02 +00:00
if ( visibility_notifier_owner . owns ( p_rid ) ) {
return RS : : INSTANCE_VISIBLITY_NOTIFIER ;
}
2019-09-07 01:51:27 +00:00
2020-03-27 18:21:27 +00:00
return RS : : INSTANCE_NONE ;
2019-08-20 20:54:03 +00:00
}
2020-04-14 03:05:21 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_add_to_decal_atlas ( RID p_texture , bool p_panorama_to_dp ) {
2020-04-14 03:05:21 +00:00
if ( ! decal_atlas . textures . has ( p_texture ) ) {
DecalAtlas : : Texture t ;
t . users = 1 ;
2020-04-14 20:05:45 +00:00
t . panorama_to_dp_users = p_panorama_to_dp ? 1 : 0 ;
2020-04-14 03:05:21 +00:00
decal_atlas . textures [ p_texture ] = t ;
decal_atlas . dirty = true ;
} else {
DecalAtlas : : Texture * t = decal_atlas . textures . getptr ( p_texture ) ;
t - > users + + ;
2020-04-14 20:05:45 +00:00
if ( p_panorama_to_dp ) {
t - > panorama_to_dp_users + + ;
}
2020-04-14 03:05:21 +00:00
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : texture_remove_from_decal_atlas ( RID p_texture , bool p_panorama_to_dp ) {
2020-04-14 03:05:21 +00:00
DecalAtlas : : Texture * t = decal_atlas . textures . getptr ( p_texture ) ;
ERR_FAIL_COND ( ! t ) ;
t - > users - - ;
2020-04-14 20:05:45 +00:00
if ( p_panorama_to_dp ) {
ERR_FAIL_COND ( t - > panorama_to_dp_users = = 0 ) ;
t - > panorama_to_dp_users - - ;
}
2020-04-14 03:05:21 +00:00
if ( t - > users = = 0 ) {
decal_atlas . textures . erase ( p_texture ) ;
//do not mark it dirty, there is no need to since it remains working
}
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : decal_atlas_get_texture ( ) const {
2020-04-14 03:05:21 +00:00
return decal_atlas . texture ;
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : decal_atlas_get_texture_srgb ( ) const {
2020-10-04 05:56:00 +00:00
return decal_atlas . texture_srgb ;
2020-04-14 03:05:21 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _update_decal_atlas ( ) {
2020-04-14 03:05:21 +00:00
if ( ! decal_atlas . dirty ) {
return ; //nothing to do
}
decal_atlas . dirty = false ;
if ( decal_atlas . texture . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( decal_atlas . texture ) ;
decal_atlas . texture = RID ( ) ;
decal_atlas . texture_srgb = RID ( ) ;
decal_atlas . texture_mipmaps . clear ( ) ;
}
int border = 1 < < decal_atlas . mipmaps ;
if ( decal_atlas . textures . size ( ) ) {
//generate atlas
Vector < DecalAtlas : : SortItem > itemsv ;
itemsv . resize ( decal_atlas . textures . size ( ) ) ;
int base_size = 8 ;
2020-05-14 08:15:48 +00:00
const RID * K = nullptr ;
2020-04-14 03:05:21 +00:00
int idx = 0 ;
while ( ( K = decal_atlas . textures . next ( K ) ) ) {
DecalAtlas : : SortItem & si = itemsv . write [ idx ] ;
Texture * src_tex = texture_owner . getornull ( * K ) ;
si . size . width = ( src_tex - > width / border ) + 1 ;
si . size . height = ( src_tex - > height / border ) + 1 ;
si . pixel_size = Size2i ( src_tex - > width , src_tex - > height ) ;
if ( base_size < si . size . width ) {
base_size = nearest_power_of_2_templated ( si . size . width ) ;
}
si . texture = * K ;
idx + + ;
}
//sort items by size
itemsv . sort ( ) ;
//attempt to create atlas
int item_count = itemsv . size ( ) ;
DecalAtlas : : SortItem * items = itemsv . ptrw ( ) ;
int atlas_height = 0 ;
while ( true ) {
Vector < int > v_offsetsv ;
v_offsetsv . resize ( base_size ) ;
int * v_offsets = v_offsetsv . ptrw ( ) ;
2021-04-27 14:19:21 +00:00
memset ( v_offsets , 0 , sizeof ( int ) * base_size ) ;
2020-04-14 03:05:21 +00:00
int max_height = 0 ;
for ( int i = 0 ; i < item_count ; i + + ) {
//best fit
DecalAtlas : : SortItem & si = items [ i ] ;
int best_idx = - 1 ;
int best_height = 0x7FFFFFFF ;
for ( int j = 0 ; j < = base_size - si . size . width ; j + + ) {
int height = 0 ;
for ( int k = 0 ; k < si . size . width ; k + + ) {
int h = v_offsets [ k + j ] ;
if ( h > height ) {
height = h ;
if ( height > best_height ) {
break ; //already bad
}
}
}
if ( height < best_height ) {
best_height = height ;
best_idx = j ;
}
}
//update
for ( int k = 0 ; k < si . size . width ; k + + ) {
v_offsets [ k + best_idx ] = best_height + si . size . height ;
}
si . pos . x = best_idx ;
si . pos . y = best_height ;
if ( si . pos . y + si . size . height > max_height ) {
max_height = si . pos . y + si . size . height ;
}
}
if ( max_height < = base_size * 2 ) {
atlas_height = max_height ;
break ; //good ratio, break;
}
base_size * = 2 ;
}
decal_atlas . size . width = base_size * border ;
decal_atlas . size . height = nearest_power_of_2_templated ( atlas_height * border ) ;
for ( int i = 0 ; i < item_count ; i + + ) {
DecalAtlas : : Texture * t = decal_atlas . textures . getptr ( items [ i ] . texture ) ;
t - > uv_rect . position = items [ i ] . pos * border + Vector2i ( border / 2 , border / 2 ) ;
t - > uv_rect . size = items [ i ] . pixel_size ;
2020-05-01 12:34:23 +00:00
2020-04-14 03:05:21 +00:00
t - > uv_rect . position / = Size2 ( decal_atlas . size ) ;
t - > uv_rect . size / = Size2 ( decal_atlas . size ) ;
}
} else {
//use border as size, so it at least has enough mipmaps
decal_atlas . size . width = border ;
decal_atlas . size . height = border ;
}
//blit textures
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tformat . width = decal_atlas . size . width ;
tformat . height = decal_atlas . size . height ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | RD : : TEXTURE_USAGE_CAN_COPY_TO_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_2D ;
2020-04-14 03:05:21 +00:00
tformat . mipmaps = decal_atlas . mipmaps ;
tformat . shareable_formats . push_back ( RD : : DATA_FORMAT_R8G8B8A8_UNORM ) ;
tformat . shareable_formats . push_back ( RD : : DATA_FORMAT_R8G8B8A8_SRGB ) ;
decal_atlas . texture = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) ) ;
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > texture_clear ( decal_atlas . texture , Color ( 0 , 0 , 0 , 0 ) , 0 , decal_atlas . mipmaps , 0 , 1 ) ;
2020-04-14 03:05:21 +00:00
{
//create the framebuffer
Size2i s = decal_atlas . size ;
for ( int i = 0 ; i < decal_atlas . mipmaps ; i + + ) {
DecalAtlas : : MipMap mm ;
mm . texture = RD : : get_singleton ( ) - > texture_create_shared_from_slice ( RD : : TextureView ( ) , decal_atlas . texture , 0 , i ) ;
Vector < RID > fb ;
fb . push_back ( mm . texture ) ;
mm . fb = RD : : get_singleton ( ) - > framebuffer_create ( fb ) ;
mm . size = s ;
decal_atlas . texture_mipmaps . push_back ( mm ) ;
s . width = MAX ( 1 , s . width > > 1 ) ;
s . height = MAX ( 1 , s . height > > 1 ) ;
}
{
//create the SRGB variant
RD : : TextureView rd_view ;
rd_view . format_override = RD : : DATA_FORMAT_R8G8B8A8_SRGB ;
decal_atlas . texture_srgb = RD : : get_singleton ( ) - > texture_create_shared ( rd_view , decal_atlas . texture ) ;
}
}
RID prev_texture ;
for ( int i = 0 ; i < decal_atlas . texture_mipmaps . size ( ) ; i + + ) {
const DecalAtlas : : MipMap & mm = decal_atlas . texture_mipmaps [ i ] ;
Color clear_color ( 0 , 0 , 0 , 0 ) ;
if ( decal_atlas . textures . size ( ) ) {
if ( i = = 0 ) {
Vector < Color > cc ;
cc . push_back ( clear_color ) ;
RD : : DrawListID draw_list = RD : : get_singleton ( ) - > draw_list_begin ( mm . fb , RD : : INITIAL_ACTION_CLEAR , RD : : FINAL_ACTION_READ , RD : : INITIAL_ACTION_DROP , RD : : FINAL_ACTION_DISCARD , cc ) ;
2020-05-14 08:15:48 +00:00
const RID * K = nullptr ;
2020-04-14 03:05:21 +00:00
while ( ( K = decal_atlas . textures . next ( K ) ) ) {
DecalAtlas : : Texture * t = decal_atlas . textures . getptr ( * K ) ;
Texture * src_tex = texture_owner . getornull ( * K ) ;
2020-04-14 20:05:45 +00:00
effects . copy_to_atlas_fb ( src_tex - > rd_texture , mm . fb , t - > uv_rect , draw_list , false , t - > panorama_to_dp_users > 0 ) ;
2020-04-14 03:05:21 +00:00
}
RD : : get_singleton ( ) - > draw_list_end ( ) ;
prev_texture = mm . texture ;
} else {
effects . copy_to_fb_rect ( prev_texture , mm . fb , Rect2i ( Point2i ( ) , mm . size ) ) ;
prev_texture = mm . texture ;
}
} else {
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > texture_clear ( mm . texture , clear_color , 0 , 1 , 0 , 1 ) ;
2020-04-14 03:05:21 +00:00
}
}
}
2020-12-04 18:26:24 +00:00
int32_t RendererStorageRD : : _global_variable_allocate ( uint32_t p_elements ) {
2020-04-17 02:52:00 +00:00
int32_t idx = 0 ;
while ( idx + p_elements < = global_variables . buffer_size ) {
if ( global_variables . buffer_usage [ idx ] . elements = = 0 ) {
bool valid = true ;
for ( uint32_t i = 1 ; i < p_elements ; i + + ) {
if ( global_variables . buffer_usage [ idx + i ] . elements > 0 ) {
valid = false ;
idx + = i + global_variables . buffer_usage [ idx + i ] . elements ;
break ;
}
}
if ( ! valid ) {
continue ; //if not valid, idx is in new position
}
return idx ;
} else {
idx + = global_variables . buffer_usage [ idx ] . elements ;
}
}
return - 1 ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _global_variable_store_in_buffer ( int32_t p_index , RS : : GlobalVariableType p_type , const Variant & p_value ) {
2020-04-17 02:52:00 +00:00
switch ( p_type ) {
case RS : : GLOBAL_VAR_TYPE_BOOL : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
bool b = p_value ;
bv . x = b ? 1.0 : 0.0 ;
bv . y = 0.0 ;
bv . z = 0.0 ;
bv . w = 0.0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_BVEC2 : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
uint32_t bvec = p_value ;
bv . x = ( bvec & 1 ) ? 1.0 : 0.0 ;
bv . y = ( bvec & 2 ) ? 1.0 : 0.0 ;
bv . z = 0.0 ;
bv . w = 0.0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_BVEC3 : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
uint32_t bvec = p_value ;
bv . x = ( bvec & 1 ) ? 1.0 : 0.0 ;
bv . y = ( bvec & 2 ) ? 1.0 : 0.0 ;
bv . z = ( bvec & 4 ) ? 1.0 : 0.0 ;
bv . w = 0.0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_BVEC4 : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
uint32_t bvec = p_value ;
bv . x = ( bvec & 1 ) ? 1.0 : 0.0 ;
bv . y = ( bvec & 2 ) ? 1.0 : 0.0 ;
bv . z = ( bvec & 4 ) ? 1.0 : 0.0 ;
bv . w = ( bvec & 8 ) ? 1.0 : 0.0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_INT : {
GlobalVariables : : ValueInt & bv = * ( GlobalVariables : : ValueInt * ) & global_variables . buffer_values [ p_index ] ;
int32_t v = p_value ;
bv . x = v ;
bv . y = 0 ;
bv . z = 0 ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_IVEC2 : {
GlobalVariables : : ValueInt & bv = * ( GlobalVariables : : ValueInt * ) & global_variables . buffer_values [ p_index ] ;
Vector2i v = p_value ;
bv . x = v . x ;
bv . y = v . y ;
bv . z = 0 ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_IVEC3 : {
GlobalVariables : : ValueInt & bv = * ( GlobalVariables : : ValueInt * ) & global_variables . buffer_values [ p_index ] ;
Vector3i v = p_value ;
bv . x = v . x ;
bv . y = v . y ;
bv . z = v . z ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_IVEC4 : {
GlobalVariables : : ValueInt & bv = * ( GlobalVariables : : ValueInt * ) & global_variables . buffer_values [ p_index ] ;
Vector < int32_t > v = p_value ;
bv . x = v . size ( ) > = 1 ? v [ 0 ] : 0 ;
bv . y = v . size ( ) > = 2 ? v [ 1 ] : 0 ;
bv . z = v . size ( ) > = 3 ? v [ 2 ] : 0 ;
bv . w = v . size ( ) > = 4 ? v [ 3 ] : 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_RECT2I : {
GlobalVariables : : ValueInt & bv = * ( GlobalVariables : : ValueInt * ) & global_variables . buffer_values [ p_index ] ;
Rect2i v = p_value ;
bv . x = v . position . x ;
bv . y = v . position . y ;
bv . z = v . size . x ;
bv . w = v . size . y ;
} break ;
case RS : : GLOBAL_VAR_TYPE_UINT : {
GlobalVariables : : ValueUInt & bv = * ( GlobalVariables : : ValueUInt * ) & global_variables . buffer_values [ p_index ] ;
uint32_t v = p_value ;
bv . x = v ;
bv . y = 0 ;
bv . z = 0 ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_UVEC2 : {
GlobalVariables : : ValueUInt & bv = * ( GlobalVariables : : ValueUInt * ) & global_variables . buffer_values [ p_index ] ;
Vector2i v = p_value ;
bv . x = v . x ;
bv . y = v . y ;
bv . z = 0 ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_UVEC3 : {
GlobalVariables : : ValueUInt & bv = * ( GlobalVariables : : ValueUInt * ) & global_variables . buffer_values [ p_index ] ;
Vector3i v = p_value ;
bv . x = v . x ;
bv . y = v . y ;
bv . z = v . z ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_UVEC4 : {
GlobalVariables : : ValueUInt & bv = * ( GlobalVariables : : ValueUInt * ) & global_variables . buffer_values [ p_index ] ;
Vector < int32_t > v = p_value ;
bv . x = v . size ( ) > = 1 ? v [ 0 ] : 0 ;
bv . y = v . size ( ) > = 2 ? v [ 1 ] : 0 ;
bv . z = v . size ( ) > = 3 ? v [ 2 ] : 0 ;
bv . w = v . size ( ) > = 4 ? v [ 3 ] : 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_FLOAT : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
float v = p_value ;
bv . x = v ;
bv . y = 0 ;
bv . z = 0 ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_VEC2 : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
Vector2 v = p_value ;
bv . x = v . x ;
bv . y = v . y ;
bv . z = 0 ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_VEC3 : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
Vector3 v = p_value ;
bv . x = v . x ;
bv . y = v . y ;
bv . z = v . z ;
bv . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_VEC4 : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
Plane v = p_value ;
bv . x = v . normal . x ;
bv . y = v . normal . y ;
bv . z = v . normal . z ;
2020-05-10 14:47:11 +00:00
bv . w = v . d ;
2020-04-17 02:52:00 +00:00
} break ;
case RS : : GLOBAL_VAR_TYPE_COLOR : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
Color v = p_value ;
bv . x = v . r ;
bv . y = v . g ;
bv . z = v . b ;
bv . w = v . a ;
GlobalVariables : : Value & bv_linear = global_variables . buffer_values [ p_index + 1 ] ;
v = v . to_linear ( ) ;
bv_linear . x = v . r ;
bv_linear . y = v . g ;
bv_linear . z = v . b ;
bv_linear . w = v . a ;
} break ;
case RS : : GLOBAL_VAR_TYPE_RECT2 : {
GlobalVariables : : Value & bv = global_variables . buffer_values [ p_index ] ;
Rect2 v = p_value ;
bv . x = v . position . x ;
bv . y = v . position . y ;
bv . z = v . size . x ;
bv . w = v . size . y ;
} break ;
case RS : : GLOBAL_VAR_TYPE_MAT2 : {
GlobalVariables : : Value * bv = & global_variables . buffer_values [ p_index ] ;
Vector < float > m2 = p_value ;
if ( m2 . size ( ) < 4 ) {
m2 . resize ( 4 ) ;
}
bv [ 0 ] . x = m2 [ 0 ] ;
bv [ 0 ] . y = m2 [ 1 ] ;
bv [ 0 ] . z = 0 ;
bv [ 0 ] . w = 0 ;
bv [ 1 ] . x = m2 [ 2 ] ;
bv [ 1 ] . y = m2 [ 3 ] ;
bv [ 1 ] . z = 0 ;
bv [ 1 ] . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_MAT3 : {
GlobalVariables : : Value * bv = & global_variables . buffer_values [ p_index ] ;
Basis v = p_value ;
bv [ 0 ] . x = v . elements [ 0 ] [ 0 ] ;
bv [ 0 ] . y = v . elements [ 1 ] [ 0 ] ;
bv [ 0 ] . z = v . elements [ 2 ] [ 0 ] ;
bv [ 0 ] . w = 0 ;
bv [ 1 ] . x = v . elements [ 0 ] [ 1 ] ;
bv [ 1 ] . y = v . elements [ 1 ] [ 1 ] ;
bv [ 1 ] . z = v . elements [ 2 ] [ 1 ] ;
bv [ 1 ] . w = 0 ;
bv [ 2 ] . x = v . elements [ 0 ] [ 2 ] ;
bv [ 2 ] . y = v . elements [ 1 ] [ 2 ] ;
bv [ 2 ] . z = v . elements [ 2 ] [ 2 ] ;
bv [ 2 ] . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_MAT4 : {
GlobalVariables : : Value * bv = & global_variables . buffer_values [ p_index ] ;
Vector < float > m2 = p_value ;
if ( m2 . size ( ) < 16 ) {
m2 . resize ( 16 ) ;
}
bv [ 0 ] . x = m2 [ 0 ] ;
bv [ 0 ] . y = m2 [ 1 ] ;
bv [ 0 ] . z = m2 [ 2 ] ;
bv [ 0 ] . w = m2 [ 3 ] ;
bv [ 1 ] . x = m2 [ 4 ] ;
bv [ 1 ] . y = m2 [ 5 ] ;
bv [ 1 ] . z = m2 [ 6 ] ;
bv [ 1 ] . w = m2 [ 7 ] ;
bv [ 2 ] . x = m2 [ 8 ] ;
bv [ 2 ] . y = m2 [ 9 ] ;
bv [ 2 ] . z = m2 [ 10 ] ;
bv [ 2 ] . w = m2 [ 11 ] ;
bv [ 3 ] . x = m2 [ 12 ] ;
bv [ 3 ] . y = m2 [ 13 ] ;
bv [ 3 ] . z = m2 [ 14 ] ;
bv [ 3 ] . w = m2 [ 15 ] ;
} break ;
case RS : : GLOBAL_VAR_TYPE_TRANSFORM_2D : {
GlobalVariables : : Value * bv = & global_variables . buffer_values [ p_index ] ;
Transform2D v = p_value ;
bv [ 0 ] . x = v . elements [ 0 ] [ 0 ] ;
bv [ 0 ] . y = v . elements [ 0 ] [ 1 ] ;
bv [ 0 ] . z = 0 ;
bv [ 0 ] . w = 0 ;
bv [ 1 ] . x = v . elements [ 1 ] [ 0 ] ;
bv [ 1 ] . y = v . elements [ 1 ] [ 1 ] ;
bv [ 1 ] . z = 0 ;
bv [ 1 ] . w = 0 ;
bv [ 2 ] . x = v . elements [ 2 ] [ 0 ] ;
bv [ 2 ] . y = v . elements [ 2 ] [ 1 ] ;
bv [ 2 ] . z = 1 ;
bv [ 2 ] . w = 0 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_TRANSFORM : {
GlobalVariables : : Value * bv = & global_variables . buffer_values [ p_index ] ;
2020-10-17 05:08:21 +00:00
Transform3D v = p_value ;
2020-04-17 02:52:00 +00:00
bv [ 0 ] . x = v . basis . elements [ 0 ] [ 0 ] ;
bv [ 0 ] . y = v . basis . elements [ 1 ] [ 0 ] ;
bv [ 0 ] . z = v . basis . elements [ 2 ] [ 0 ] ;
bv [ 0 ] . w = 0 ;
bv [ 1 ] . x = v . basis . elements [ 0 ] [ 1 ] ;
bv [ 1 ] . y = v . basis . elements [ 1 ] [ 1 ] ;
bv [ 1 ] . z = v . basis . elements [ 2 ] [ 1 ] ;
bv [ 1 ] . w = 0 ;
bv [ 2 ] . x = v . basis . elements [ 0 ] [ 2 ] ;
bv [ 2 ] . y = v . basis . elements [ 1 ] [ 2 ] ;
bv [ 2 ] . z = v . basis . elements [ 2 ] [ 2 ] ;
bv [ 2 ] . w = 0 ;
2020-04-28 16:26:57 +00:00
bv [ 3 ] . x = v . origin . x ;
bv [ 3 ] . y = v . origin . y ;
bv [ 3 ] . z = v . origin . z ;
bv [ 3 ] . w = 1 ;
2020-04-17 02:52:00 +00:00
} break ;
default : {
ERR_FAIL ( ) ;
}
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _global_variable_mark_buffer_dirty ( int32_t p_index , int32_t p_elements ) {
2020-04-17 02:52:00 +00:00
int32_t prev_chunk = - 1 ;
for ( int32_t i = 0 ; i < p_elements ; i + + ) {
int32_t chunk = ( p_index + i ) / GlobalVariables : : BUFFER_DIRTY_REGION_SIZE ;
if ( chunk ! = prev_chunk ) {
if ( ! global_variables . buffer_dirty_regions [ chunk ] ) {
global_variables . buffer_dirty_regions [ chunk ] = true ;
global_variables . buffer_dirty_region_count + + ;
}
}
prev_chunk = chunk ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : global_variable_add ( const StringName & p_name , RS : : GlobalVariableType p_type , const Variant & p_value ) {
2020-04-17 02:52:00 +00:00
ERR_FAIL_COND ( global_variables . variables . has ( p_name ) ) ;
GlobalVariables : : Variable gv ;
gv . type = p_type ;
gv . value = p_value ;
gv . buffer_index = - 1 ;
if ( p_type > = RS : : GLOBAL_VAR_TYPE_SAMPLER2D ) {
//is texture
2020-07-16 16:54:15 +00:00
global_variables . must_update_texture_materials = true ; //normally there are none
2020-04-17 02:52:00 +00:00
} else {
gv . buffer_elements = 1 ;
if ( p_type = = RS : : GLOBAL_VAR_TYPE_COLOR | | p_type = = RS : : GLOBAL_VAR_TYPE_MAT2 ) {
//color needs to elements to store srgb and linear
gv . buffer_elements = 2 ;
}
if ( p_type = = RS : : GLOBAL_VAR_TYPE_MAT3 | | p_type = = RS : : GLOBAL_VAR_TYPE_TRANSFORM_2D ) {
//color needs to elements to store srgb and linear
gv . buffer_elements = 3 ;
}
if ( p_type = = RS : : GLOBAL_VAR_TYPE_MAT4 | | p_type = = RS : : GLOBAL_VAR_TYPE_TRANSFORM ) {
//color needs to elements to store srgb and linear
gv . buffer_elements = 4 ;
}
//is vector, allocate in buffer and update index
gv . buffer_index = _global_variable_allocate ( gv . buffer_elements ) ;
ERR_FAIL_COND_MSG ( gv . buffer_index < 0 , vformat ( " Failed allocating global variable '%s' out of buffer memory. Consider increasing it in the Project Settings. " , String ( p_name ) ) ) ;
global_variables . buffer_usage [ gv . buffer_index ] . elements = gv . buffer_elements ;
_global_variable_store_in_buffer ( gv . buffer_index , gv . type , gv . value ) ;
_global_variable_mark_buffer_dirty ( gv . buffer_index , gv . buffer_elements ) ;
2020-07-16 16:54:15 +00:00
global_variables . must_update_buffer_materials = true ; //normally there are none
2020-04-17 02:52:00 +00:00
}
global_variables . variables [ p_name ] = gv ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : global_variable_remove ( const StringName & p_name ) {
2020-04-17 02:52:00 +00:00
if ( ! global_variables . variables . has ( p_name ) ) {
return ;
}
GlobalVariables : : Variable & gv = global_variables . variables [ p_name ] ;
if ( gv . buffer_index > = 0 ) {
global_variables . buffer_usage [ gv . buffer_index ] . elements = 0 ;
global_variables . must_update_buffer_materials = true ;
} else {
global_variables . must_update_texture_materials = true ;
}
global_variables . variables . erase ( p_name ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
Vector < StringName > RendererStorageRD : : global_variable_get_list ( ) const {
2020-04-17 02:52:00 +00:00
if ( ! Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
ERR_FAIL_V_MSG ( Vector < StringName > ( ) , " This function should never be used outside the editor, it can severely damage performance. " ) ;
}
2020-05-14 08:15:48 +00:00
const StringName * K = nullptr ;
2020-04-17 02:52:00 +00:00
Vector < StringName > names ;
while ( ( K = global_variables . variables . next ( K ) ) ) {
names . push_back ( * K ) ;
}
names . sort_custom < StringName : : AlphCompare > ( ) ;
return names ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : global_variable_set ( const StringName & p_name , const Variant & p_value ) {
2020-04-17 02:52:00 +00:00
ERR_FAIL_COND ( ! global_variables . variables . has ( p_name ) ) ;
GlobalVariables : : Variable & gv = global_variables . variables [ p_name ] ;
gv . value = p_value ;
if ( gv . override . get_type ( ) = = Variant : : NIL ) {
if ( gv . buffer_index > = 0 ) {
//buffer
_global_variable_store_in_buffer ( gv . buffer_index , gv . type , gv . value ) ;
_global_variable_mark_buffer_dirty ( gv . buffer_index , gv . buffer_elements ) ;
} else {
//texture
for ( Set < RID > : : Element * E = gv . texture_materials . front ( ) ; E ; E = E - > next ( ) ) {
Material * material = material_owner . getornull ( E - > get ( ) ) ;
ERR_CONTINUE ( ! material ) ;
_material_queue_update ( material , false , true ) ;
}
}
}
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : global_variable_set_override ( const StringName & p_name , const Variant & p_value ) {
2020-04-17 02:52:00 +00:00
if ( ! global_variables . variables . has ( p_name ) ) {
return ; //variable may not exist
}
GlobalVariables : : Variable & gv = global_variables . variables [ p_name ] ;
gv . override = p_value ;
if ( gv . buffer_index > = 0 ) {
//buffer
if ( gv . override . get_type ( ) = = Variant : : NIL ) {
_global_variable_store_in_buffer ( gv . buffer_index , gv . type , gv . value ) ;
} else {
_global_variable_store_in_buffer ( gv . buffer_index , gv . type , gv . override ) ;
}
_global_variable_mark_buffer_dirty ( gv . buffer_index , gv . buffer_elements ) ;
} else {
//texture
for ( Set < RID > : : Element * E = gv . texture_materials . front ( ) ; E ; E = E - > next ( ) ) {
Material * material = material_owner . getornull ( E - > get ( ) ) ;
ERR_CONTINUE ( ! material ) ;
_material_queue_update ( material , false , true ) ;
}
}
}
2020-12-04 18:26:24 +00:00
Variant RendererStorageRD : : global_variable_get ( const StringName & p_name ) const {
2020-04-17 02:52:00 +00:00
if ( ! Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
ERR_FAIL_V_MSG ( Variant ( ) , " This function should never be used outside the editor, it can severely damage performance. " ) ;
}
if ( ! global_variables . variables . has ( p_name ) ) {
return Variant ( ) ;
}
return global_variables . variables [ p_name ] . value ;
}
2020-12-04 18:26:24 +00:00
RS : : GlobalVariableType RendererStorageRD : : global_variable_get_type_internal ( const StringName & p_name ) const {
2020-04-17 02:52:00 +00:00
if ( ! global_variables . variables . has ( p_name ) ) {
return RS : : GLOBAL_VAR_TYPE_MAX ;
}
return global_variables . variables [ p_name ] . type ;
}
2020-12-04 18:26:24 +00:00
RS : : GlobalVariableType RendererStorageRD : : global_variable_get_type ( const StringName & p_name ) const {
2020-04-17 02:52:00 +00:00
if ( ! Engine : : get_singleton ( ) - > is_editor_hint ( ) ) {
ERR_FAIL_V_MSG ( RS : : GLOBAL_VAR_TYPE_MAX , " This function should never be used outside the editor, it can severely damage performance. " ) ;
}
return global_variable_get_type_internal ( p_name ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : global_variables_load_settings ( bool p_load_textures ) {
2020-04-17 02:52:00 +00:00
List < PropertyInfo > settings ;
ProjectSettings : : get_singleton ( ) - > get_property_list ( & settings ) ;
for ( List < PropertyInfo > : : Element * E = settings . front ( ) ; E ; E = E - > next ( ) ) {
if ( E - > get ( ) . name . begins_with ( " shader_globals/ " ) ) {
StringName name = E - > get ( ) . name . get_slice ( " / " , 1 ) ;
Dictionary d = ProjectSettings : : get_singleton ( ) - > get ( E - > get ( ) . name ) ;
ERR_CONTINUE ( ! d . has ( " type " ) ) ;
ERR_CONTINUE ( ! d . has ( " value " ) ) ;
String type = d [ " type " ] ;
static const char * global_var_type_names [ RS : : GLOBAL_VAR_TYPE_MAX ] = {
" bool " ,
" bvec2 " ,
" bvec3 " ,
" bvec4 " ,
" int " ,
" ivec2 " ,
" ivec3 " ,
" ivec4 " ,
" rect2i " ,
" uint " ,
" uvec2 " ,
" uvec3 " ,
" uvec4 " ,
" float " ,
" vec2 " ,
" vec3 " ,
" vec4 " ,
" color " ,
" rect2 " ,
" mat2 " ,
" mat3 " ,
" mat4 " ,
" transform_2d " ,
" transform " ,
" sampler2D " ,
" sampler2DArray " ,
" sampler3D " ,
" samplerCube " ,
} ;
RS : : GlobalVariableType gvtype = RS : : GLOBAL_VAR_TYPE_MAX ;
for ( int i = 0 ; i < RS : : GLOBAL_VAR_TYPE_MAX ; i + + ) {
if ( global_var_type_names [ i ] = = type ) {
gvtype = RS : : GlobalVariableType ( i ) ;
break ;
}
}
ERR_CONTINUE ( gvtype = = RS : : GLOBAL_VAR_TYPE_MAX ) ; //type invalid
Variant value = d [ " value " ] ;
if ( gvtype > = RS : : GLOBAL_VAR_TYPE_SAMPLER2D ) {
//textire
if ( ! p_load_textures ) {
value = RID ( ) ;
continue ;
}
String path = value ;
RES resource = ResourceLoader : : load ( path ) ;
ERR_CONTINUE ( resource . is_null ( ) ) ;
value = resource ;
}
if ( global_variables . variables . has ( name ) ) {
//has it, update it
global_variable_set ( name , value ) ;
} else {
global_variable_add ( name , gvtype , value ) ;
}
}
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : global_variables_clear ( ) {
2020-04-17 02:52:00 +00:00
global_variables . variables . clear ( ) ; //not right but for now enough
}
2020-12-04 18:26:24 +00:00
RID RendererStorageRD : : global_variables_get_storage_buffer ( ) const {
2020-04-17 02:52:00 +00:00
return global_variables . buffer ;
}
2020-12-04 18:26:24 +00:00
int32_t RendererStorageRD : : global_variables_instance_allocate ( RID p_instance ) {
2020-04-17 02:52:00 +00:00
ERR_FAIL_COND_V ( global_variables . instance_buffer_pos . has ( p_instance ) , - 1 ) ;
int32_t pos = _global_variable_allocate ( ShaderLanguage : : MAX_INSTANCE_UNIFORM_INDICES ) ;
global_variables . instance_buffer_pos [ p_instance ] = pos ; //save anyway
ERR_FAIL_COND_V_MSG ( pos < 0 , - 1 , " Too many instances using shader instance variables. Increase buffer size in Project Settings. " ) ;
global_variables . buffer_usage [ pos ] . elements = ShaderLanguage : : MAX_INSTANCE_UNIFORM_INDICES ;
return pos ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : global_variables_instance_free ( RID p_instance ) {
2020-04-17 02:52:00 +00:00
ERR_FAIL_COND ( ! global_variables . instance_buffer_pos . has ( p_instance ) ) ;
int32_t pos = global_variables . instance_buffer_pos [ p_instance ] ;
if ( pos > = 0 ) {
global_variables . buffer_usage [ pos ] . elements = 0 ;
}
global_variables . instance_buffer_pos . erase ( p_instance ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : global_variables_instance_update ( RID p_instance , int p_index , const Variant & p_value ) {
2020-04-17 02:52:00 +00:00
if ( ! global_variables . instance_buffer_pos . has ( p_instance ) ) {
return ; //just not allocated, ignore
}
int32_t pos = global_variables . instance_buffer_pos [ p_instance ] ;
if ( pos < 0 ) {
return ; //again, not allocated, ignore
}
ERR_FAIL_INDEX ( p_index , ShaderLanguage : : MAX_INSTANCE_UNIFORM_INDICES ) ;
ERR_FAIL_COND_MSG ( p_value . get_type ( ) > Variant : : COLOR , " Unsupported variant type for instance parameter: " + Variant : : get_type_name ( p_value . get_type ( ) ) ) ; //anything greater not supported
ShaderLanguage : : DataType datatype_from_value [ Variant : : COLOR + 1 ] = {
ShaderLanguage : : TYPE_MAX , //nil
ShaderLanguage : : TYPE_BOOL , //bool
ShaderLanguage : : TYPE_INT , //int
ShaderLanguage : : TYPE_FLOAT , //float
ShaderLanguage : : TYPE_MAX , //string
ShaderLanguage : : TYPE_VEC2 , //vec2
ShaderLanguage : : TYPE_IVEC2 , //vec2i
ShaderLanguage : : TYPE_VEC4 , //rect2
ShaderLanguage : : TYPE_IVEC4 , //rect2i
ShaderLanguage : : TYPE_VEC3 , // vec3
ShaderLanguage : : TYPE_IVEC3 , //vec3i
ShaderLanguage : : TYPE_MAX , //xform2d not supported here
ShaderLanguage : : TYPE_VEC4 , //plane
ShaderLanguage : : TYPE_VEC4 , //quat
ShaderLanguage : : TYPE_MAX , //aabb not supported here
ShaderLanguage : : TYPE_MAX , //basis not supported here
ShaderLanguage : : TYPE_MAX , //xform not supported here
ShaderLanguage : : TYPE_VEC4 //color
} ;
ShaderLanguage : : DataType datatype = datatype_from_value [ p_value . get_type ( ) ] ;
ERR_FAIL_COND_MSG ( datatype = = ShaderLanguage : : TYPE_MAX , " Unsupported variant type for instance parameter: " + Variant : : get_type_name ( p_value . get_type ( ) ) ) ; //anything greater not supported
pos + = p_index ;
_fill_std140_variant_ubo_value ( datatype , p_value , ( uint8_t * ) & global_variables . buffer_values [ pos ] , true ) ; //instances always use linear color in this renderer
_global_variable_mark_buffer_dirty ( pos , 1 ) ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : _update_global_variables ( ) {
2020-04-17 02:52:00 +00:00
if ( global_variables . buffer_dirty_region_count > 0 ) {
uint32_t total_regions = global_variables . buffer_size / GlobalVariables : : BUFFER_DIRTY_REGION_SIZE ;
if ( total_regions / global_variables . buffer_dirty_region_count < = 4 ) {
// 25% of regions dirty, just update all buffer
RD : : get_singleton ( ) - > buffer_update ( global_variables . buffer , 0 , sizeof ( GlobalVariables : : Value ) * global_variables . buffer_size , global_variables . buffer_values ) ;
2021-04-27 14:19:21 +00:00
memset ( global_variables . buffer_dirty_regions , 0 , sizeof ( bool ) * total_regions ) ;
2020-04-17 02:52:00 +00:00
} else {
uint32_t region_byte_size = sizeof ( GlobalVariables : : Value ) * GlobalVariables : : BUFFER_DIRTY_REGION_SIZE ;
for ( uint32_t i = 0 ; i < total_regions ; i + + ) {
if ( global_variables . buffer_dirty_regions [ i ] ) {
RD : : get_singleton ( ) - > buffer_update ( global_variables . buffer , i * region_byte_size , region_byte_size , global_variables . buffer_values ) ;
global_variables . buffer_dirty_regions [ i ] = false ;
}
}
}
global_variables . buffer_dirty_region_count = 0 ;
}
if ( global_variables . must_update_buffer_materials ) {
// only happens in the case of a buffer variable added or removed,
// so not often.
for ( List < RID > : : Element * E = global_variables . materials_using_buffer . front ( ) ; E ; E = E - > next ( ) ) {
Material * material = material_owner . getornull ( E - > get ( ) ) ;
ERR_CONTINUE ( ! material ) ; //wtf
_material_queue_update ( material , true , false ) ;
}
global_variables . must_update_buffer_materials = false ;
}
if ( global_variables . must_update_texture_materials ) {
// only happens in the case of a buffer variable added or removed,
// so not often.
for ( List < RID > : : Element * E = global_variables . materials_using_texture . front ( ) ; E ; E = E - > next ( ) ) {
Material * material = material_owner . getornull ( E - > get ( ) ) ;
ERR_CONTINUE ( ! material ) ; //wtf
_material_queue_update ( material , false , true ) ;
print_line ( " update material texture? " ) ;
}
global_variables . must_update_texture_materials = false ;
}
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : update_dirty_resources ( ) {
2020-04-17 02:52:00 +00:00
_update_global_variables ( ) ; //must do before materials, so it can queue them for update
2019-07-21 14:31:30 +00:00
_update_queued_materials ( ) ;
2019-09-14 03:37:42 +00:00
_update_dirty_multimeshes ( ) ;
2019-09-23 21:53:05 +00:00
_update_dirty_skeletons ( ) ;
2020-04-14 03:05:21 +00:00
_update_decal_atlas ( ) ;
2019-07-21 14:31:30 +00:00
}
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : has_os_feature ( const String & p_feature ) const {
2019-09-27 02:16:44 +00:00
if ( p_feature = = " rgtc " & & RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC5_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT ) ) {
return true ;
}
2019-09-14 03:37:42 +00:00
if ( p_feature = = " s3tc " & & RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC1_RGB_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT ) ) {
return true ;
}
if ( p_feature = = " bptc " & & RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_BC7_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT ) ) {
return true ;
}
if ( ( p_feature = = " etc " | | p_feature = = " etc2 " ) & & RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK , RD : : TEXTURE_USAGE_SAMPLING_BIT ) ) {
return true ;
}
if ( p_feature = = " pvrtc " & & RD : : get_singleton ( ) - > texture_is_format_supported_for_usage ( RD : : DATA_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG , RD : : TEXTURE_USAGE_SAMPLING_BIT ) ) {
return true ;
}
return false ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
bool RendererStorageRD : : free ( RID p_rid ) {
2019-06-16 02:45:24 +00:00
if ( texture_owner . owns ( p_rid ) ) {
Texture * t = texture_owner . getornull ( p_rid ) ;
2021-06-11 18:12:23 +00:00
ERR_FAIL_COND_V ( ! t , false ) ;
2019-06-16 02:45:24 +00:00
ERR_FAIL_COND_V ( t - > is_render_target , false ) ;
2019-07-29 21:19:31 +00:00
if ( RD : : get_singleton ( ) - > texture_is_valid ( t - > rd_texture_srgb ) ) {
2019-06-24 19:13:06 +00:00
//erase this first, as it's a dependency of the one below
2019-06-16 02:45:24 +00:00
RD : : get_singleton ( ) - > free ( t - > rd_texture_srgb ) ;
}
2019-07-29 21:19:31 +00:00
if ( RD : : get_singleton ( ) - > texture_is_valid ( t - > rd_texture ) ) {
RD : : get_singleton ( ) - > free ( t - > rd_texture ) ;
}
2019-06-24 19:13:06 +00:00
2019-09-07 01:51:27 +00:00
if ( t - > is_proxy & & t - > proxy_to . is_valid ( ) ) {
Texture * proxy_to = texture_owner . getornull ( t - > proxy_to ) ;
if ( proxy_to ) {
proxy_to - > proxies . erase ( p_rid ) ;
}
}
2020-04-14 03:05:21 +00:00
if ( decal_atlas . textures . has ( p_rid ) ) {
decal_atlas . textures . erase ( p_rid ) ;
//there is not much a point of making it dirty, just let it be.
}
2019-06-24 19:13:06 +00:00
for ( int i = 0 ; i < t - > proxies . size ( ) ; i + + ) {
Texture * p = texture_owner . getornull ( t - > proxies [ i ] ) ;
ERR_CONTINUE ( ! p ) ;
p - > proxy_to = RID ( ) ;
p - > rd_texture = RID ( ) ;
p - > rd_texture_srgb = RID ( ) ;
}
2020-10-24 15:15:43 +00:00
if ( t - > canvas_texture ) {
memdelete ( t - > canvas_texture ) ;
}
2019-06-16 02:45:24 +00:00
texture_owner . free ( p_rid ) ;
2020-10-24 15:15:43 +00:00
} else if ( canvas_texture_owner . owns ( p_rid ) ) {
CanvasTexture * ct = canvas_texture_owner . getornull ( p_rid ) ;
memdelete ( ct ) ;
canvas_texture_owner . free ( p_rid ) ;
2019-07-21 14:31:30 +00:00
} else if ( shader_owner . owns ( p_rid ) ) {
Shader * shader = shader_owner . getornull ( p_rid ) ;
//make material unreference this
while ( shader - > owners . size ( ) ) {
material_set_shader ( shader - > owners . front ( ) - > get ( ) - > self , RID ( ) ) ;
}
//clear data if exists
if ( shader - > data ) {
memdelete ( shader - > data ) ;
}
shader_owner . free ( p_rid ) ;
} else if ( material_owner . owns ( p_rid ) ) {
Material * material = material_owner . getornull ( p_rid ) ;
if ( material - > update_requested ) {
_update_queued_materials ( ) ;
}
material_set_shader ( p_rid , RID ( ) ) ; //clean up shader
2021-01-04 12:33:25 +00:00
material - > dependency . deleted_notify ( p_rid ) ;
2019-07-29 21:19:31 +00:00
material_owner . free ( p_rid ) ;
2019-08-18 22:40:52 +00:00
} else if ( mesh_owner . owns ( p_rid ) ) {
mesh_clear ( p_rid ) ;
2021-01-25 15:20:11 +00:00
mesh_set_shadow_mesh ( p_rid , RID ( ) ) ;
2019-08-18 22:40:52 +00:00
Mesh * mesh = mesh_owner . getornull ( p_rid ) ;
2021-01-04 12:33:25 +00:00
mesh - > dependency . deleted_notify ( p_rid ) ;
2020-12-16 14:07:08 +00:00
if ( mesh - > instances . size ( ) ) {
ERR_PRINT ( " deleting mesh with active instances " ) ;
}
2021-01-25 15:20:11 +00:00
if ( mesh - > shadow_owners . size ( ) ) {
for ( Set < Mesh * > : : Element * E = mesh - > shadow_owners . front ( ) ; E ; E = E - > next ( ) ) {
Mesh * shadow_owner = E - > get ( ) ;
shadow_owner - > shadow_mesh = RID ( ) ;
shadow_owner - > dependency . changed_notify ( DEPENDENCY_CHANGED_MESH ) ;
}
}
2019-08-18 22:40:52 +00:00
mesh_owner . free ( p_rid ) ;
2020-12-16 14:07:08 +00:00
} else if ( mesh_instance_owner . owns ( p_rid ) ) {
MeshInstance * mi = mesh_instance_owner . getornull ( p_rid ) ;
_mesh_instance_clear ( mi ) ;
mi - > mesh - > instances . erase ( mi - > I ) ;
mi - > I = nullptr ;
2021-01-25 15:20:11 +00:00
2020-12-16 14:07:08 +00:00
mesh_instance_owner . free ( p_rid ) ;
memdelete ( mi ) ;
2019-09-14 03:37:42 +00:00
} else if ( multimesh_owner . owns ( p_rid ) ) {
_update_dirty_multimeshes ( ) ;
2021-02-09 16:19:03 +00:00
multimesh_allocate_data ( p_rid , 0 , RS : : MULTIMESH_TRANSFORM_2D ) ;
2019-09-14 03:37:42 +00:00
MultiMesh * multimesh = multimesh_owner . getornull ( p_rid ) ;
2021-01-04 12:33:25 +00:00
multimesh - > dependency . deleted_notify ( p_rid ) ;
2019-09-14 03:37:42 +00:00
multimesh_owner . free ( p_rid ) ;
2019-09-23 21:53:05 +00:00
} else if ( skeleton_owner . owns ( p_rid ) ) {
_update_dirty_skeletons ( ) ;
2021-02-09 16:19:03 +00:00
skeleton_allocate_data ( p_rid , 0 ) ;
2019-09-23 21:53:05 +00:00
Skeleton * skeleton = skeleton_owner . getornull ( p_rid ) ;
2021-01-04 12:33:25 +00:00
skeleton - > dependency . deleted_notify ( p_rid ) ;
2019-09-23 21:53:05 +00:00
skeleton_owner . free ( p_rid ) ;
2019-09-07 01:51:27 +00:00
} else if ( reflection_probe_owner . owns ( p_rid ) ) {
ReflectionProbe * reflection_probe = reflection_probe_owner . getornull ( p_rid ) ;
2021-01-04 12:33:25 +00:00
reflection_probe - > dependency . deleted_notify ( p_rid ) ;
2019-09-07 01:51:27 +00:00
reflection_probe_owner . free ( p_rid ) ;
2020-04-14 03:05:21 +00:00
} else if ( decal_owner . owns ( p_rid ) ) {
Decal * decal = decal_owner . getornull ( p_rid ) ;
for ( int i = 0 ; i < RS : : DECAL_TEXTURE_MAX ; i + + ) {
if ( decal - > textures [ i ] . is_valid ( ) & & texture_owner . owns ( decal - > textures [ i ] ) ) {
texture_remove_from_decal_atlas ( decal - > textures [ i ] ) ;
}
}
2021-01-04 12:33:25 +00:00
decal - > dependency . deleted_notify ( p_rid ) ;
2020-04-14 03:05:21 +00:00
decal_owner . free ( p_rid ) ;
2021-06-04 22:47:26 +00:00
} else if ( voxel_gi_owner . owns ( p_rid ) ) {
voxel_gi_allocate_data ( p_rid , Transform3D ( ) , AABB ( ) , Vector3i ( ) , Vector < uint8_t > ( ) , Vector < uint8_t > ( ) , Vector < uint8_t > ( ) , Vector < int > ( ) ) ; //deallocate
VoxelGI * voxel_gi = voxel_gi_owner . getornull ( p_rid ) ;
voxel_gi - > dependency . deleted_notify ( p_rid ) ;
voxel_gi_owner . free ( p_rid ) ;
2020-05-01 12:34:23 +00:00
} else if ( lightmap_owner . owns ( p_rid ) ) {
lightmap_set_textures ( p_rid , RID ( ) , false ) ;
Lightmap * lightmap = lightmap_owner . getornull ( p_rid ) ;
2021-01-04 12:33:25 +00:00
lightmap - > dependency . deleted_notify ( p_rid ) ;
2020-05-01 12:34:23 +00:00
lightmap_owner . free ( p_rid ) ;
2019-09-07 01:51:27 +00:00
} else if ( light_owner . owns ( p_rid ) ) {
2020-04-14 20:05:45 +00:00
light_set_projector ( p_rid , RID ( ) ) ; //clear projector
2019-09-07 01:51:27 +00:00
// delete the texture
Light * light = light_owner . getornull ( p_rid ) ;
2021-01-04 12:33:25 +00:00
light - > dependency . deleted_notify ( p_rid ) ;
2019-09-07 01:51:27 +00:00
light_owner . free ( p_rid ) ;
2020-09-06 12:18:10 +00:00
} else if ( particles_owner . owns ( p_rid ) ) {
2021-04-27 15:43:49 +00:00
update_particles ( ) ;
2020-09-06 12:18:10 +00:00
Particles * particles = particles_owner . getornull ( p_rid ) ;
2021-01-04 12:33:25 +00:00
particles - > dependency . deleted_notify ( p_rid ) ;
2021-04-27 15:43:49 +00:00
_particles_free_data ( particles ) ;
2020-09-06 12:18:10 +00:00
particles_owner . free ( p_rid ) ;
2020-10-08 00:29:49 +00:00
} else if ( particles_collision_owner . owns ( p_rid ) ) {
ParticlesCollision * particles_collision = particles_collision_owner . getornull ( p_rid ) ;
if ( particles_collision - > heightfield_texture . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( particles_collision - > heightfield_texture ) ;
}
2021-01-04 12:33:25 +00:00
particles_collision - > dependency . deleted_notify ( p_rid ) ;
2020-10-08 00:29:49 +00:00
particles_collision_owner . free ( p_rid ) ;
2021-06-16 18:43:02 +00:00
} else if ( visibility_notifier_owner . owns ( p_rid ) ) {
VisibilityNotifier * vn = visibility_notifier_owner . getornull ( p_rid ) ;
vn - > dependency . deleted_notify ( p_rid ) ;
visibility_notifier_owner . free ( p_rid ) ;
2020-12-31 12:42:56 +00:00
} else if ( particles_collision_instance_owner . owns ( p_rid ) ) {
particles_collision_instance_owner . free ( p_rid ) ;
2019-06-16 02:45:24 +00:00
} else if ( render_target_owner . owns ( p_rid ) ) {
RenderTarget * rt = render_target_owner . getornull ( p_rid ) ;
_clear_render_target ( rt ) ;
if ( rt - > texture . is_valid ( ) ) {
2019-06-25 01:24:07 +00:00
Texture * tex = texture_owner . getornull ( rt - > texture ) ;
tex - > is_render_target = false ;
free ( rt - > texture ) ;
2019-06-16 02:45:24 +00:00
}
render_target_owner . free ( p_rid ) ;
} else {
return false ;
}
return true ;
}
2019-07-21 14:31:30 +00:00
2020-12-04 18:26:24 +00:00
EffectsRD * RendererStorageRD : : get_effects ( ) {
2019-07-27 13:23:24 +00:00
return & effects ;
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : capture_timestamps_begin ( ) {
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > capture_timestamp ( " Frame Begin " ) ;
2019-09-20 20:58:06 +00:00
}
2020-12-04 18:26:24 +00:00
void RendererStorageRD : : capture_timestamp ( const String & p_name ) {
2021-01-26 00:52:58 +00:00
RD : : get_singleton ( ) - > capture_timestamp ( p_name ) ;
2019-09-20 20:58:06 +00:00
}
2020-12-04 18:26:24 +00:00
uint32_t RendererStorageRD : : get_captured_timestamps_count ( ) const {
2019-09-20 20:58:06 +00:00
return RD : : get_singleton ( ) - > get_captured_timestamps_count ( ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
uint64_t RendererStorageRD : : get_captured_timestamps_frame ( ) const {
2019-09-20 20:58:06 +00:00
return RD : : get_singleton ( ) - > get_captured_timestamps_frame ( ) ;
}
2020-12-04 18:26:24 +00:00
uint64_t RendererStorageRD : : get_captured_timestamp_gpu_time ( uint32_t p_index ) const {
2019-09-20 20:58:06 +00:00
return RD : : get_singleton ( ) - > get_captured_timestamp_gpu_time ( p_index ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
uint64_t RendererStorageRD : : get_captured_timestamp_cpu_time ( uint32_t p_index ) const {
2019-09-20 20:58:06 +00:00
return RD : : get_singleton ( ) - > get_captured_timestamp_cpu_time ( p_index ) ;
}
2020-05-14 12:29:06 +00:00
2020-12-04 18:26:24 +00:00
String RendererStorageRD : : get_captured_timestamp_name ( uint32_t p_index ) const {
2019-09-20 20:58:06 +00:00
return RD : : get_singleton ( ) - > get_captured_timestamp_name ( p_index ) ;
}
2020-12-04 18:26:24 +00:00
RendererStorageRD * RendererStorageRD : : base_singleton = nullptr ;
2020-04-17 02:52:00 +00:00
2020-12-04 18:26:24 +00:00
RendererStorageRD : : RendererStorageRD ( ) {
2020-04-17 02:52:00 +00:00
base_singleton = this ;
2019-07-12 13:18:30 +00:00
for ( int i = 0 ; i < SHADER_TYPE_MAX ; i + + ) {
2020-04-01 23:20:12 +00:00
shader_data_request_func [ i ] = nullptr ;
2019-07-12 13:18:30 +00:00
}
2020-04-17 02:52:00 +00:00
static_assert ( sizeof ( GlobalVariables : : Value ) = = 16 ) ;
2021-02-17 16:44:49 +00:00
global_variables . buffer_size = GLOBAL_GET ( " rendering/limits/global_shader_variables/buffer_size " ) ;
2020-04-17 02:52:00 +00:00
global_variables . buffer_size = MAX ( 4096 , global_variables . buffer_size ) ;
global_variables . buffer_values = memnew_arr ( GlobalVariables : : Value , global_variables . buffer_size ) ;
2021-04-27 14:19:21 +00:00
memset ( global_variables . buffer_values , 0 , sizeof ( GlobalVariables : : Value ) * global_variables . buffer_size ) ;
2020-04-17 02:52:00 +00:00
global_variables . buffer_usage = memnew_arr ( GlobalVariables : : ValueUsage , global_variables . buffer_size ) ;
global_variables . buffer_dirty_regions = memnew_arr ( bool , global_variables . buffer_size / GlobalVariables : : BUFFER_DIRTY_REGION_SIZE ) ;
2021-04-27 14:19:21 +00:00
memset ( global_variables . buffer_dirty_regions , 0 , sizeof ( bool ) * global_variables . buffer_size / GlobalVariables : : BUFFER_DIRTY_REGION_SIZE ) ;
2020-04-17 02:52:00 +00:00
global_variables . buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( GlobalVariables : : Value ) * global_variables . buffer_size ) ;
2020-04-01 23:20:12 +00:00
material_update_list = nullptr ;
2019-07-21 14:31:30 +00:00
{ //create default textures
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tformat . width = 4 ;
tformat . height = 4 ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_2D ;
2019-07-21 14:31:30 +00:00
2020-02-17 21:06:54 +00:00
Vector < uint8_t > pv ;
2019-07-21 14:31:30 +00:00
pv . resize ( 16 * 4 ) ;
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 255 ) ;
pv . set ( i * 4 + 1 , 255 ) ;
pv . set ( i * 4 + 2 , 255 ) ;
pv . set ( i * 4 + 3 , 255 ) ;
}
{
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > vpv ;
2019-07-21 14:31:30 +00:00
vpv . push_back ( pv ) ;
default_rd_textures [ DEFAULT_RD_TEXTURE_WHITE ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 0 ) ;
pv . set ( i * 4 + 1 , 0 ) ;
pv . set ( i * 4 + 2 , 0 ) ;
pv . set ( i * 4 + 3 , 255 ) ;
}
{
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > vpv ;
2019-07-21 14:31:30 +00:00
vpv . push_back ( pv ) ;
default_rd_textures [ DEFAULT_RD_TEXTURE_BLACK ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
2020-04-14 03:05:21 +00:00
//take the chance and initialize decal atlas to something
decal_atlas . texture = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
decal_atlas . texture_srgb = decal_atlas . texture ;
2019-07-21 14:31:30 +00:00
}
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 128 ) ;
pv . set ( i * 4 + 1 , 128 ) ;
pv . set ( i * 4 + 2 , 255 ) ;
pv . set ( i * 4 + 3 , 255 ) ;
}
{
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > vpv ;
2019-07-21 14:31:30 +00:00
vpv . push_back ( pv ) ;
default_rd_textures [ DEFAULT_RD_TEXTURE_NORMAL ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 255 ) ;
pv . set ( i * 4 + 1 , 128 ) ;
pv . set ( i * 4 + 2 , 255 ) ;
pv . set ( i * 4 + 3 , 255 ) ;
}
{
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > vpv ;
2019-07-21 14:31:30 +00:00
vpv . push_back ( pv ) ;
default_rd_textures [ DEFAULT_RD_TEXTURE_ANISO ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 0 ) ;
pv . set ( i * 4 + 1 , 0 ) ;
pv . set ( i * 4 + 2 , 0 ) ;
pv . set ( i * 4 + 3 , 0 ) ;
}
default_rd_textures [ DEFAULT_RD_TEXTURE_MULTIMESH_BUFFER ] = RD : : get_singleton ( ) - > texture_buffer_create ( 16 , RD : : DATA_FORMAT_R8G8B8A8_UNORM , pv ) ;
2020-06-25 13:33:28 +00:00
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 0 ) ;
pv . set ( i * 4 + 1 , 0 ) ;
pv . set ( i * 4 + 2 , 0 ) ;
pv . set ( i * 4 + 3 , 0 ) ;
}
{
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UINT ;
Vector < Vector < uint8_t > > vpv ;
vpv . push_back ( pv ) ;
default_rd_textures [ DEFAULT_RD_TEXTURE_2D_UINT ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
2019-07-21 14:31:30 +00:00
}
2019-08-26 20:43:58 +00:00
{ //create default cubemap
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tformat . width = 4 ;
tformat . height = 4 ;
tformat . array_layers = 6 ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_CUBE_ARRAY ;
2019-08-26 20:43:58 +00:00
2020-02-17 21:06:54 +00:00
Vector < uint8_t > pv ;
2019-08-26 20:43:58 +00:00
pv . resize ( 16 * 4 ) ;
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 0 ) ;
pv . set ( i * 4 + 1 , 0 ) ;
pv . set ( i * 4 + 2 , 0 ) ;
pv . set ( i * 4 + 3 , 0 ) ;
}
{
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > vpv ;
2019-08-26 20:43:58 +00:00
for ( int i = 0 ; i < 6 ; i + + ) {
vpv . push_back ( pv ) ;
}
default_rd_textures [ DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
}
{ //create default cubemap array
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tformat . width = 4 ;
tformat . height = 4 ;
tformat . array_layers = 6 ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_CUBE ;
2019-08-26 20:43:58 +00:00
2020-02-17 21:06:54 +00:00
Vector < uint8_t > pv ;
2019-08-26 20:43:58 +00:00
pv . resize ( 16 * 4 ) ;
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 0 ) ;
pv . set ( i * 4 + 1 , 0 ) ;
pv . set ( i * 4 + 2 , 0 ) ;
pv . set ( i * 4 + 3 , 0 ) ;
}
{
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > vpv ;
2019-08-26 20:43:58 +00:00
for ( int i = 0 ; i < 6 ; i + + ) {
vpv . push_back ( pv ) ;
}
default_rd_textures [ DEFAULT_RD_TEXTURE_CUBEMAP_BLACK ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
}
2019-08-27 18:27:35 +00:00
2020-11-19 20:09:27 +00:00
{ //create default cubemap white array
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tformat . width = 4 ;
tformat . height = 4 ;
tformat . array_layers = 6 ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_CUBE ;
2020-11-19 20:09:27 +00:00
Vector < uint8_t > pv ;
pv . resize ( 16 * 4 ) ;
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 255 ) ;
pv . set ( i * 4 + 1 , 255 ) ;
pv . set ( i * 4 + 2 , 255 ) ;
pv . set ( i * 4 + 3 , 255 ) ;
}
{
Vector < Vector < uint8_t > > vpv ;
for ( int i = 0 ; i < 6 ; i + + ) {
vpv . push_back ( pv ) ;
}
default_rd_textures [ DEFAULT_RD_TEXTURE_CUBEMAP_WHITE ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
}
2019-08-27 18:27:35 +00:00
{ //create default 3D
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tformat . width = 4 ;
tformat . height = 4 ;
tformat . depth = 4 ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_3D ;
2019-08-27 18:27:35 +00:00
2020-02-17 21:06:54 +00:00
Vector < uint8_t > pv ;
2019-08-27 18:27:35 +00:00
pv . resize ( 64 * 4 ) ;
for ( int i = 0 ; i < 64 ; i + + ) {
pv . set ( i * 4 + 0 , 0 ) ;
pv . set ( i * 4 + 1 , 0 ) ;
pv . set ( i * 4 + 2 , 0 ) ;
pv . set ( i * 4 + 3 , 0 ) ;
}
{
2020-03-17 06:33:00 +00:00
Vector < Vector < uint8_t > > vpv ;
2019-08-27 18:27:35 +00:00
vpv . push_back ( pv ) ;
default_rd_textures [ DEFAULT_RD_TEXTURE_3D_WHITE ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
}
2020-05-01 12:34:23 +00:00
{ //create default array
RD : : TextureFormat tformat ;
tformat . format = RD : : DATA_FORMAT_R8G8B8A8_UNORM ;
tformat . width = 4 ;
tformat . height = 4 ;
tformat . array_layers = 1 ;
tformat . usage_bits = RD : : TEXTURE_USAGE_SAMPLING_BIT | RD : : TEXTURE_USAGE_CAN_UPDATE_BIT ;
2020-11-27 03:50:05 +00:00
tformat . texture_type = RD : : TEXTURE_TYPE_2D_ARRAY ;
2020-05-01 12:34:23 +00:00
Vector < uint8_t > pv ;
pv . resize ( 16 * 4 ) ;
for ( int i = 0 ; i < 16 ; i + + ) {
pv . set ( i * 4 + 0 , 255 ) ;
pv . set ( i * 4 + 1 , 255 ) ;
pv . set ( i * 4 + 2 , 255 ) ;
pv . set ( i * 4 + 3 , 255 ) ;
}
{
Vector < Vector < uint8_t > > vpv ;
vpv . push_back ( pv ) ;
default_rd_textures [ DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE ] = RD : : get_singleton ( ) - > texture_create ( tformat , RD : : TextureView ( ) , vpv ) ;
}
}
2019-07-21 14:31:30 +00:00
//default samplers
2020-03-27 18:21:27 +00:00
for ( int i = 1 ; i < RS : : CANVAS_ITEM_TEXTURE_FILTER_MAX ; i + + ) {
for ( int j = 1 ; j < RS : : CANVAS_ITEM_TEXTURE_REPEAT_MAX ; j + + ) {
2019-07-21 14:31:30 +00:00
RD : : SamplerState sampler_state ;
switch ( i ) {
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST : {
2019-07-21 14:31:30 +00:00
sampler_state . mag_filter = RD : : SAMPLER_FILTER_NEAREST ;
sampler_state . min_filter = RD : : SAMPLER_FILTER_NEAREST ;
sampler_state . max_lod = 0 ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR : {
2019-07-21 14:31:30 +00:00
sampler_state . mag_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . min_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . max_lod = 0 ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS : {
2019-07-21 14:31:30 +00:00
sampler_state . mag_filter = RD : : SAMPLER_FILTER_NEAREST ;
sampler_state . min_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . mip_filter = RD : : SAMPLER_FILTER_LINEAR ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS : {
2019-07-21 14:31:30 +00:00
sampler_state . mag_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . min_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . mip_filter = RD : : SAMPLER_FILTER_LINEAR ;
} break ;
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC : {
2019-07-21 14:31:30 +00:00
sampler_state . mag_filter = RD : : SAMPLER_FILTER_NEAREST ;
sampler_state . min_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . mip_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . use_anisotropy = true ;
2021-02-17 16:44:49 +00:00
sampler_state . anisotropy_max = 1 < < int ( GLOBAL_GET ( " rendering/textures/default_filters/anisotropic_filtering_level " ) ) ;
2019-07-21 14:31:30 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC : {
2019-07-21 14:31:30 +00:00
sampler_state . mag_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . min_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . mip_filter = RD : : SAMPLER_FILTER_LINEAR ;
sampler_state . use_anisotropy = true ;
2021-02-17 16:44:49 +00:00
sampler_state . anisotropy_max = 1 < < int ( GLOBAL_GET ( " rendering/textures/default_filters/anisotropic_filtering_level " ) ) ;
2019-07-21 14:31:30 +00:00
} break ;
default : {
}
}
switch ( j ) {
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED : {
2019-07-21 14:31:30 +00:00
sampler_state . repeat_u = RD : : SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE ;
sampler_state . repeat_v = RD : : SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE ;
2020-09-10 00:59:51 +00:00
sampler_state . repeat_w = RD : : SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE ;
2019-07-21 14:31:30 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_REPEAT_ENABLED : {
2019-07-21 14:31:30 +00:00
sampler_state . repeat_u = RD : : SAMPLER_REPEAT_MODE_REPEAT ;
sampler_state . repeat_v = RD : : SAMPLER_REPEAT_MODE_REPEAT ;
2020-09-10 00:59:51 +00:00
sampler_state . repeat_w = RD : : SAMPLER_REPEAT_MODE_REPEAT ;
2019-07-21 14:31:30 +00:00
} break ;
2020-03-27 18:21:27 +00:00
case RS : : CANVAS_ITEM_TEXTURE_REPEAT_MIRROR : {
2019-07-21 14:31:30 +00:00
sampler_state . repeat_u = RD : : SAMPLER_REPEAT_MODE_MIRRORED_REPEAT ;
sampler_state . repeat_v = RD : : SAMPLER_REPEAT_MODE_MIRRORED_REPEAT ;
2020-09-10 00:59:51 +00:00
sampler_state . repeat_w = RD : : SAMPLER_REPEAT_MODE_MIRRORED_REPEAT ;
2019-07-21 14:31:30 +00:00
} break ;
default : {
}
}
default_rd_samplers [ i ] [ j ] = RD : : get_singleton ( ) - > sampler_create ( sampler_state ) ;
}
}
2019-08-18 22:40:52 +00:00
//default rd buffers
{
2020-05-01 12:34:23 +00:00
Vector < uint8_t > buffer ;
2020-04-14 03:05:21 +00:00
{
2020-05-01 12:34:23 +00:00
buffer . resize ( sizeof ( float ) * 3 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
float * fptr = ( float * ) w ;
fptr [ 0 ] = 0.0 ;
fptr [ 1 ] = 0.0 ;
fptr [ 2 ] = 0.0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_VERTEX ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
2020-04-14 03:05:21 +00:00
2020-05-01 12:34:23 +00:00
{ //normal
buffer . resize ( sizeof ( float ) * 3 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
float * fptr = ( float * ) w ;
fptr [ 0 ] = 1.0 ;
fptr [ 1 ] = 0.0 ;
fptr [ 2 ] = 0.0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_NORMAL ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
2019-08-18 22:40:52 +00:00
2020-05-01 12:34:23 +00:00
{ //tangent
buffer . resize ( sizeof ( float ) * 4 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
float * fptr = ( float * ) w ;
fptr [ 0 ] = 1.0 ;
fptr [ 1 ] = 0.0 ;
fptr [ 2 ] = 0.0 ;
fptr [ 3 ] = 0.0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_TANGENT ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
2019-08-18 22:40:52 +00:00
2020-05-01 12:34:23 +00:00
{ //color
buffer . resize ( sizeof ( float ) * 4 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
float * fptr = ( float * ) w ;
fptr [ 0 ] = 1.0 ;
fptr [ 1 ] = 1.0 ;
fptr [ 2 ] = 1.0 ;
fptr [ 3 ] = 1.0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_COLOR ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
2019-08-18 22:40:52 +00:00
2020-05-01 12:34:23 +00:00
{ //tex uv 1
buffer . resize ( sizeof ( float ) * 2 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
float * fptr = ( float * ) w ;
fptr [ 0 ] = 0.0 ;
fptr [ 1 ] = 0.0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_TEX_UV ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
{ //tex uv 2
buffer . resize ( sizeof ( float ) * 2 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
float * fptr = ( float * ) w ;
fptr [ 0 ] = 0.0 ;
fptr [ 1 ] = 0.0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_TEX_UV2 ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
2019-08-18 22:40:52 +00:00
2020-12-02 01:40:47 +00:00
for ( int i = 0 ; i < RS : : ARRAY_CUSTOM_COUNT ; i + + ) {
buffer . resize ( sizeof ( float ) * 4 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
float * fptr = ( float * ) w ;
fptr [ 0 ] = 0.0 ;
fptr [ 1 ] = 0.0 ;
fptr [ 2 ] = 0.0 ;
fptr [ 3 ] = 0.0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_CUSTOM0 + i ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
2020-05-01 12:34:23 +00:00
{ //bones
buffer . resize ( sizeof ( uint32_t ) * 4 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
uint32_t * fptr = ( uint32_t * ) w ;
fptr [ 0 ] = 0 ;
fptr [ 1 ] = 0 ;
fptr [ 2 ] = 0 ;
fptr [ 3 ] = 0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_BONES ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
{ //weights
buffer . resize ( sizeof ( float ) * 4 ) ;
{
uint8_t * w = buffer . ptrw ( ) ;
float * fptr = ( float * ) w ;
fptr [ 0 ] = 0.0 ;
fptr [ 1 ] = 0.0 ;
fptr [ 2 ] = 0.0 ;
fptr [ 3 ] = 0.0 ;
}
mesh_default_rd_buffers [ DEFAULT_RD_BUFFER_WEIGHTS ] = RD : : get_singleton ( ) - > vertex_buffer_create ( buffer . size ( ) , buffer ) ;
}
2019-10-11 02:14:56 +00:00
}
2019-08-18 22:40:52 +00:00
2019-10-11 02:14:56 +00:00
{
2020-05-01 12:34:23 +00:00
Vector < String > sdf_versions ;
sdf_versions . push_back ( " " ) ; //one only
2021-06-04 22:47:26 +00:00
voxel_gi_sdf_shader . initialize ( sdf_versions ) ;
voxel_gi_sdf_shader_version = voxel_gi_sdf_shader . version_create ( ) ;
voxel_gi_sdf_shader_version_shader = voxel_gi_sdf_shader . version_get_shader ( voxel_gi_sdf_shader_version , 0 ) ;
voxel_gi_sdf_shader_pipeline = RD : : get_singleton ( ) - > compute_pipeline_create ( voxel_gi_sdf_shader_version_shader ) ;
2019-08-18 22:40:52 +00:00
}
2019-10-11 02:14:56 +00:00
2020-05-01 12:34:23 +00:00
using_lightmap_array = true ; // high end
if ( using_lightmap_array ) {
uint32_t textures_per_stage = RD : : get_singleton ( ) - > limit_get ( RD : : LIMIT_MAX_TEXTURES_PER_SHADER_STAGE ) ;
if ( textures_per_stage < = 256 ) {
lightmap_textures . resize ( 32 ) ;
} else {
lightmap_textures . resize ( 1024 ) ;
}
for ( int i = 0 ; i < lightmap_textures . size ( ) ; i + + ) {
lightmap_textures . write [ i ] = default_rd_textures [ DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE ] ;
}
2019-10-11 02:14:56 +00:00
}
2021-02-17 16:44:49 +00:00
lightmap_probe_capture_update_speed = GLOBAL_GET ( " rendering/lightmapping/probe_capture/update_speed " ) ;
2020-08-19 13:38:24 +00:00
/* Particles */
{
// Initialize particles
Vector < String > particles_modes ;
particles_modes . push_back ( " " ) ;
particles_shader . shader . initialize ( particles_modes , String ( ) ) ;
}
2020-12-04 18:26:24 +00:00
shader_set_data_request_function ( RendererStorageRD : : SHADER_TYPE_PARTICLES , _create_particles_shader_funcs ) ;
material_set_data_request_function ( RendererStorageRD : : SHADER_TYPE_PARTICLES , _create_particles_material_funcs ) ;
2020-08-19 13:38:24 +00:00
{
ShaderCompilerRD : : DefaultIdentifierActions actions ;
actions . renames [ " COLOR " ] = " PARTICLE.color " ;
actions . renames [ " VELOCITY " ] = " PARTICLE.velocity " ;
//actions.renames["MASS"] = "mass"; ?
2021-04-27 15:43:49 +00:00
actions . renames [ " ACTIVE " ] = " particle_active " ;
2020-08-19 13:38:24 +00:00
actions . renames [ " RESTART " ] = " restart " ;
actions . renames [ " CUSTOM " ] = " PARTICLE.custom " ;
actions . renames [ " TRANSFORM " ] = " PARTICLE.xform " ;
actions . renames [ " TIME " ] = " FRAME.time " ;
2021-05-19 12:12:55 +00:00
actions . renames [ " PI " ] = _MKSTR ( Math_PI ) ;
actions . renames [ " TAU " ] = _MKSTR ( Math_TAU ) ;
actions . renames [ " E " ] = _MKSTR ( Math_E ) ;
2020-08-19 13:38:24 +00:00
actions . renames [ " LIFETIME " ] = " params.lifetime " ;
actions . renames [ " DELTA " ] = " local_delta " ;
2021-04-27 15:43:49 +00:00
actions . renames [ " NUMBER " ] = " particle_number " ;
2020-08-19 13:38:24 +00:00
actions . renames [ " INDEX " ] = " index " ;
//actions.renames["GRAVITY"] = "current_gravity";
actions . renames [ " EMISSION_TRANSFORM " ] = " FRAME.emission_transform " ;
actions . renames [ " RANDOM_SEED " ] = " FRAME.random_seed " ;
2020-09-06 12:18:10 +00:00
actions . renames [ " FLAG_EMIT_POSITION " ] = " EMISSION_FLAG_HAS_POSITION " ;
actions . renames [ " FLAG_EMIT_ROT_SCALE " ] = " EMISSION_FLAG_HAS_ROTATION_SCALE " ;
actions . renames [ " FLAG_EMIT_VELOCITY " ] = " EMISSION_FLAG_HAS_VELOCITY " ;
actions . renames [ " FLAG_EMIT_COLOR " ] = " EMISSION_FLAG_HAS_COLOR " ;
actions . renames [ " FLAG_EMIT_CUSTOM " ] = " EMISSION_FLAG_HAS_CUSTOM " ;
actions . renames [ " RESTART_POSITION " ] = " restart_position " ;
actions . renames [ " RESTART_ROT_SCALE " ] = " restart_rotation_scale " ;
actions . renames [ " RESTART_VELOCITY " ] = " restart_velocity " ;
actions . renames [ " RESTART_COLOR " ] = " restart_color " ;
actions . renames [ " RESTART_CUSTOM " ] = " restart_custom " ;
2021-01-12 06:23:04 +00:00
actions . renames [ " emit_subparticle " ] = " emit_subparticle " ;
2020-10-08 00:29:49 +00:00
actions . renames [ " COLLIDED " ] = " collided " ;
actions . renames [ " COLLISION_NORMAL " ] = " collision_normal " ;
actions . renames [ " COLLISION_DEPTH " ] = " collision_depth " ;
actions . renames [ " ATTRACTOR_FORCE " ] = " attractor_force " ;
2020-08-19 13:38:24 +00:00
actions . render_mode_defines [ " disable_force " ] = " #define DISABLE_FORCE \n " ;
actions . render_mode_defines [ " disable_velocity " ] = " #define DISABLE_VELOCITY \n " ;
actions . render_mode_defines [ " keep_data " ] = " #define ENABLE_KEEP_DATA \n " ;
2020-10-08 00:29:49 +00:00
actions . render_mode_defines [ " collision_use_scale " ] = " #define USE_COLLISON_SCALE \n " ;
2020-08-19 13:38:24 +00:00
actions . sampler_array_name = " material_samplers " ;
actions . base_texture_binding_index = 1 ;
2020-10-08 00:29:49 +00:00
actions . texture_layout_set = 3 ;
2020-08-19 13:38:24 +00:00
actions . base_uniform_string = " material. " ;
actions . base_varying_index = 10 ;
actions . default_filter = ShaderLanguage : : FILTER_LINEAR_MIPMAP ;
actions . default_repeat = ShaderLanguage : : REPEAT_ENABLE ;
actions . global_buffer_array_variable = " global_variables.data " ;
particles_shader . compiler . initialize ( actions ) ;
}
{
// default material and shader for particles shader
2021-02-09 16:19:03 +00:00
particles_shader . default_shader = shader_allocate ( ) ;
shader_initialize ( particles_shader . default_shader ) ;
2021-04-13 20:01:43 +00:00
shader_set_code ( particles_shader . default_shader , " shader_type particles; void process() { COLOR = vec4(1.0); } \n " ) ;
2021-02-09 16:19:03 +00:00
particles_shader . default_material = material_allocate ( ) ;
material_initialize ( particles_shader . default_material ) ;
2020-08-19 13:38:24 +00:00
material_set_shader ( particles_shader . default_material , particles_shader . default_shader ) ;
2020-12-04 18:26:24 +00:00
ParticlesMaterialData * md = ( ParticlesMaterialData * ) material_get_data ( particles_shader . default_material , RendererStorageRD : : SHADER_TYPE_PARTICLES ) ;
2020-08-19 13:38:24 +00:00
particles_shader . default_shader_rd = particles_shader . shader . version_get_shader ( md - > shader_data - > version , 0 ) ;
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_SAMPLER ;
2020-08-19 13:38:24 +00:00
u . binding = 1 ;
u . ids . resize ( 12 ) ;
RID * ids_ptr = u . ids . ptrw ( ) ;
ids_ptr [ 0 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ;
ids_ptr [ 1 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ;
ids_ptr [ 2 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ;
ids_ptr [ 3 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ;
ids_ptr [ 4 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ;
ids_ptr [ 5 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC , RS : : CANVAS_ITEM_TEXTURE_REPEAT_DISABLED ) ;
ids_ptr [ 6 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST , RS : : CANVAS_ITEM_TEXTURE_REPEAT_ENABLED ) ;
ids_ptr [ 7 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR , RS : : CANVAS_ITEM_TEXTURE_REPEAT_ENABLED ) ;
ids_ptr [ 8 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_ENABLED ) ;
ids_ptr [ 9 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS , RS : : CANVAS_ITEM_TEXTURE_REPEAT_ENABLED ) ;
ids_ptr [ 10 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC , RS : : CANVAS_ITEM_TEXTURE_REPEAT_ENABLED ) ;
ids_ptr [ 11 ] = sampler_rd_get_default ( RS : : CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC , RS : : CANVAS_ITEM_TEXTURE_REPEAT_ENABLED ) ;
uniforms . push_back ( u ) ;
}
{
RD : : Uniform u ;
2020-10-17 01:19:21 +00:00
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
2020-08-19 13:38:24 +00:00
u . binding = 2 ;
u . ids . push_back ( global_variables_get_storage_buffer ( ) ) ;
uniforms . push_back ( u ) ;
}
particles_shader . base_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , particles_shader . default_shader_rd , 0 ) ;
}
2020-09-06 12:18:10 +00:00
default_rd_storage_buffer = RD : : get_singleton ( ) - > storage_buffer_create ( sizeof ( uint32_t ) * 4 ) ;
2020-08-19 13:38:24 +00:00
{
Vector < String > copy_modes ;
copy_modes . push_back ( " \n #define MODE_FILL_INSTANCES \n " ) ;
2021-05-10 16:12:44 +00:00
copy_modes . push_back ( " \n #define MODE_FILL_INSTANCES \n #define MODE_2D \n " ) ;
2020-08-19 13:38:24 +00:00
copy_modes . push_back ( " \n #define MODE_FILL_SORT_BUFFER \n #define USE_SORT_BUFFER \n " ) ;
copy_modes . push_back ( " \n #define MODE_FILL_INSTANCES \n #define USE_SORT_BUFFER \n " ) ;
particles_shader . copy_shader . initialize ( copy_modes ) ;
particles_shader . copy_shader_version = particles_shader . copy_shader . version_create ( ) ;
for ( int i = 0 ; i < ParticlesShader : : COPY_MODE_MAX ; i + + ) {
particles_shader . copy_pipelines [ i ] = RD : : get_singleton ( ) - > compute_pipeline_create ( particles_shader . copy_shader . version_get_shader ( particles_shader . copy_shader_version , i ) ) ;
}
}
2020-11-26 12:50:21 +00:00
{
Vector < String > sdf_modes ;
sdf_modes . push_back ( " \n #define MODE_LOAD \n " ) ;
sdf_modes . push_back ( " \n #define MODE_LOAD_SHRINK \n " ) ;
sdf_modes . push_back ( " \n #define MODE_PROCESS \n " ) ;
sdf_modes . push_back ( " \n #define MODE_PROCESS_OPTIMIZED \n " ) ;
sdf_modes . push_back ( " \n #define MODE_STORE \n " ) ;
sdf_modes . push_back ( " \n #define MODE_STORE_SHRINK \n " ) ;
rt_sdf . shader . initialize ( sdf_modes ) ;
rt_sdf . shader_version = rt_sdf . shader . version_create ( ) ;
for ( int i = 0 ; i < RenderTargetSDF : : SHADER_MAX ; i + + ) {
rt_sdf . pipelines [ i ] = RD : : get_singleton ( ) - > compute_pipeline_create ( rt_sdf . shader . version_get_shader ( rt_sdf . shader_version , i ) ) ;
}
}
2020-12-16 14:07:08 +00:00
{
Vector < String > skeleton_modes ;
skeleton_modes . push_back ( " \n #define MODE_2D \n " ) ;
skeleton_modes . push_back ( " " ) ;
skeleton_shader . shader . initialize ( skeleton_modes ) ;
skeleton_shader . version = skeleton_shader . shader . version_create ( ) ;
for ( int i = 0 ; i < SkeletonShader : : SHADER_MODE_MAX ; i + + ) {
skeleton_shader . version_shader [ i ] = skeleton_shader . shader . version_get_shader ( skeleton_shader . version , i ) ;
skeleton_shader . pipeline [ i ] = RD : : get_singleton ( ) - > compute_pipeline_create ( skeleton_shader . version_shader [ i ] ) ;
}
{
Vector < RD : : Uniform > uniforms ;
{
RD : : Uniform u ;
u . binding = 0 ;
u . uniform_type = RD : : UNIFORM_TYPE_STORAGE_BUFFER ;
u . ids . push_back ( default_rd_storage_buffer ) ;
uniforms . push_back ( u ) ;
}
skeleton_shader . default_skeleton_uniform_set = RD : : get_singleton ( ) - > uniform_set_create ( uniforms , skeleton_shader . version_shader [ 0 ] , SkeletonShader : : UNIFORM_SET_SKELETON ) ;
}
}
2019-07-21 14:31:30 +00:00
}
2020-12-04 18:26:24 +00:00
RendererStorageRD : : ~ RendererStorageRD ( ) {
2020-04-17 02:52:00 +00:00
memdelete_arr ( global_variables . buffer_values ) ;
memdelete_arr ( global_variables . buffer_usage ) ;
memdelete_arr ( global_variables . buffer_dirty_regions ) ;
RD : : get_singleton ( ) - > free ( global_variables . buffer ) ;
2019-07-21 14:31:30 +00:00
//def textures
for ( int i = 0 ; i < DEFAULT_RD_TEXTURE_MAX ; i + + ) {
RD : : get_singleton ( ) - > free ( default_rd_textures [ i ] ) ;
}
//def samplers
2020-03-27 18:21:27 +00:00
for ( int i = 1 ; i < RS : : CANVAS_ITEM_TEXTURE_FILTER_MAX ; i + + ) {
for ( int j = 1 ; j < RS : : CANVAS_ITEM_TEXTURE_REPEAT_MAX ; j + + ) {
2019-07-21 14:31:30 +00:00
RD : : get_singleton ( ) - > free ( default_rd_samplers [ i ] [ j ] ) ;
}
}
2019-08-18 22:40:52 +00:00
//def buffers
for ( int i = 0 ; i < DEFAULT_RD_BUFFER_MAX ; i + + ) {
RD : : get_singleton ( ) - > free ( mesh_default_rd_buffers [ i ] ) ;
}
2020-10-12 07:47:12 +00:00
2021-06-04 22:47:26 +00:00
voxel_gi_sdf_shader . version_free ( voxel_gi_sdf_shader_version ) ;
2020-10-12 07:47:12 +00:00
particles_shader . copy_shader . version_free ( particles_shader . copy_shader_version ) ;
2020-12-05 08:27:44 +00:00
rt_sdf . shader . version_free ( rt_sdf . shader_version ) ;
2020-04-14 03:05:21 +00:00
2020-12-16 14:07:08 +00:00
skeleton_shader . shader . version_free ( skeleton_shader . version ) ;
2020-10-12 16:06:47 +00:00
RenderingServer : : get_singleton ( ) - > free ( particles_shader . default_material ) ;
RenderingServer : : get_singleton ( ) - > free ( particles_shader . default_shader ) ;
2020-09-06 12:18:10 +00:00
RD : : get_singleton ( ) - > free ( default_rd_storage_buffer ) ;
2020-04-14 03:05:21 +00:00
if ( decal_atlas . textures . size ( ) ) {
ERR_PRINT ( " Decal Atlas: " + itos ( decal_atlas . textures . size ( ) ) + " textures were not removed from the atlas. " ) ;
}
if ( decal_atlas . texture . is_valid ( ) ) {
RD : : get_singleton ( ) - > free ( decal_atlas . texture ) ;
}
2019-07-21 14:31:30 +00:00
}