2023-01-05 13:25:55 +01:00
/**************************************************************************/
/* shader_globals_override.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2020-05-10 12:53:46 +02:00
2020-04-17 04:52:00 +02:00
# include "shader_globals_override.h"
2021-08-13 01:05:59 +02:00
# include "scene/3d/node_3d.h"
2020-04-17 04:52:00 +02:00
# include "scene/scene_string_names.h"
StringName * ShaderGlobalsOverride : : _remap ( const StringName & p_name ) const {
StringName * r = param_remaps . getptr ( p_name ) ;
if ( ! r ) {
//not cached, do caching
String p = p_name ;
if ( p . begins_with ( " params/ " ) ) {
String q = p . replace_first ( " params/ " , " " ) ;
param_remaps [ p ] = q ;
r = param_remaps . getptr ( q ) ;
}
}
return r ;
}
2020-05-14 14:29:06 +02:00
2020-04-17 04:52:00 +02:00
bool ShaderGlobalsOverride : : _set ( const StringName & p_name , const Variant & p_value ) {
StringName * r = _remap ( p_name ) ;
if ( r ) {
Override * o = overrides . getptr ( * r ) ;
if ( ! o ) {
Override ov ;
ov . in_use = false ;
overrides [ * r ] = ov ;
o = overrides . getptr ( * r ) ;
}
if ( o ) {
o - > override = p_value ;
if ( active ) {
2021-07-06 03:40:29 +02:00
if ( o - > override . get_type ( ) = = Variant : : OBJECT ) {
RID tex_rid = p_value ;
2022-08-27 11:22:43 +02:00
RS : : get_singleton ( ) - > global_shader_parameter_set_override ( * r , tex_rid ) ;
2021-07-06 03:40:29 +02:00
} else {
2022-08-27 11:22:43 +02:00
RS : : get_singleton ( ) - > global_shader_parameter_set_override ( * r , p_value ) ;
2021-07-06 03:40:29 +02:00
}
2020-04-17 04:52:00 +02:00
}
o - > in_use = p_value . get_type ( ) ! = Variant : : NIL ;
return true ;
}
}
return false ;
}
bool ShaderGlobalsOverride : : _get ( const StringName & p_name , Variant & r_ret ) const {
StringName * r = _remap ( p_name ) ;
if ( r ) {
const Override * o = overrides . getptr ( * r ) ;
if ( o ) {
r_ret = o - > override ;
return true ;
}
}
return false ;
}
void ShaderGlobalsOverride : : _get_property_list ( List < PropertyInfo > * p_list ) const {
Vector < StringName > variables ;
2022-08-27 11:22:43 +02:00
variables = RS : : get_singleton ( ) - > global_shader_parameter_get_list ( ) ;
2020-04-17 04:52:00 +02:00
for ( int i = 0 ; i < variables . size ( ) ; i + + ) {
PropertyInfo pinfo ;
pinfo . name = " params/ " + variables [ i ] ;
pinfo . usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE ;
2022-08-27 11:22:43 +02:00
switch ( RS : : get_singleton ( ) - > global_shader_parameter_get_type ( variables [ i ] ) ) {
2020-04-17 04:52:00 +02:00
case RS : : GLOBAL_VAR_TYPE_BOOL : {
pinfo . type = Variant : : BOOL ;
} break ;
case RS : : GLOBAL_VAR_TYPE_BVEC2 : {
pinfo . type = Variant : : INT ;
pinfo . hint = PROPERTY_HINT_FLAGS ;
pinfo . hint_string = " x,y " ;
} break ;
case RS : : GLOBAL_VAR_TYPE_BVEC3 : {
pinfo . type = Variant : : INT ;
pinfo . hint = PROPERTY_HINT_FLAGS ;
pinfo . hint_string = " x,y,z " ;
} break ;
case RS : : GLOBAL_VAR_TYPE_BVEC4 : {
pinfo . type = Variant : : INT ;
pinfo . hint = PROPERTY_HINT_FLAGS ;
pinfo . hint_string = " x,y,z,w " ;
} break ;
case RS : : GLOBAL_VAR_TYPE_INT : {
pinfo . type = Variant : : INT ;
} break ;
case RS : : GLOBAL_VAR_TYPE_IVEC2 : {
pinfo . type = Variant : : VECTOR2I ;
} break ;
case RS : : GLOBAL_VAR_TYPE_IVEC3 : {
pinfo . type = Variant : : VECTOR3I ;
} break ;
case RS : : GLOBAL_VAR_TYPE_IVEC4 : {
pinfo . type = Variant : : PACKED_INT32_ARRAY ;
} break ;
case RS : : GLOBAL_VAR_TYPE_RECT2I : {
pinfo . type = Variant : : RECT2I ;
} break ;
case RS : : GLOBAL_VAR_TYPE_UINT : {
pinfo . type = Variant : : INT ;
} break ;
case RS : : GLOBAL_VAR_TYPE_UVEC2 : {
pinfo . type = Variant : : VECTOR2I ;
} break ;
case RS : : GLOBAL_VAR_TYPE_UVEC3 : {
pinfo . type = Variant : : VECTOR3I ;
} break ;
case RS : : GLOBAL_VAR_TYPE_UVEC4 : {
pinfo . type = Variant : : PACKED_INT32_ARRAY ;
} break ;
case RS : : GLOBAL_VAR_TYPE_FLOAT : {
pinfo . type = Variant : : FLOAT ;
} break ;
case RS : : GLOBAL_VAR_TYPE_VEC2 : {
pinfo . type = Variant : : VECTOR2 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_VEC3 : {
pinfo . type = Variant : : VECTOR3 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_VEC4 : {
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-20 01:11:13 +02:00
pinfo . type = Variant : : VECTOR4 ;
2020-04-17 04:52:00 +02:00
} break ;
case RS : : GLOBAL_VAR_TYPE_RECT2 : {
pinfo . type = Variant : : RECT2 ;
} break ;
case RS : : GLOBAL_VAR_TYPE_COLOR : {
pinfo . type = Variant : : COLOR ;
} break ;
case RS : : GLOBAL_VAR_TYPE_MAT2 : {
pinfo . type = Variant : : PACKED_INT32_ARRAY ;
} break ;
case RS : : GLOBAL_VAR_TYPE_MAT3 : {
pinfo . type = Variant : : BASIS ;
} break ;
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-20 01:11:13 +02:00
case RS : : GLOBAL_VAR_TYPE_MAT4 : {
pinfo . type = Variant : : PROJECTION ;
} break ;
2020-04-17 04:52:00 +02:00
case RS : : GLOBAL_VAR_TYPE_TRANSFORM_2D : {
pinfo . type = Variant : : TRANSFORM2D ;
} break ;
case RS : : GLOBAL_VAR_TYPE_TRANSFORM : {
2021-04-28 09:36:08 +02:00
pinfo . type = Variant : : TRANSFORM3D ;
2020-04-17 04:52:00 +02:00
} break ;
case RS : : GLOBAL_VAR_TYPE_SAMPLER2D : {
pinfo . type = Variant : : OBJECT ;
pinfo . hint = PROPERTY_HINT_RESOURCE_TYPE ;
pinfo . hint_string = " Texture2D " ;
} break ;
case RS : : GLOBAL_VAR_TYPE_SAMPLER2DARRAY : {
pinfo . type = Variant : : OBJECT ;
pinfo . hint = PROPERTY_HINT_RESOURCE_TYPE ;
pinfo . hint_string = " Texture2DArray " ;
} break ;
case RS : : GLOBAL_VAR_TYPE_SAMPLER3D : {
pinfo . type = Variant : : OBJECT ;
pinfo . hint = PROPERTY_HINT_RESOURCE_TYPE ;
pinfo . hint_string = " Texture3D " ;
} break ;
case RS : : GLOBAL_VAR_TYPE_SAMPLERCUBE : {
pinfo . type = Variant : : OBJECT ;
pinfo . hint = PROPERTY_HINT_RESOURCE_TYPE ;
pinfo . hint_string = " Cubemap " ;
} break ;
default : {
} break ;
}
if ( ! overrides . has ( variables [ i ] ) ) {
Override o ;
o . in_use = false ;
Callable : : CallError ce ;
2020-11-09 04:19:09 +01:00
Variant : : construct ( pinfo . type , o . override , nullptr , 0 , ce ) ;
2020-04-17 04:52:00 +02:00
overrides [ variables [ i ] ] = o ;
}
Override * o = overrides . getptr ( variables [ i ] ) ;
if ( o - > in_use & & o - > override . get_type ( ) ! = Variant : : NIL ) {
pinfo . usage | = PROPERTY_USAGE_CHECKED ;
pinfo . usage | = PROPERTY_USAGE_STORAGE ;
}
p_list - > push_back ( pinfo ) ;
}
}
void ShaderGlobalsOverride : : _activate ( ) {
2022-02-10 18:22:20 +01:00
ERR_FAIL_NULL ( get_tree ( ) ) ;
2020-04-17 04:52:00 +02:00
List < Node * > nodes ;
get_tree ( ) - > get_nodes_in_group ( SceneStringNames : : get_singleton ( ) - > shader_overrides_group_active , & nodes ) ;
if ( nodes . size ( ) = = 0 ) {
//good we are the only override, enable all
active = true ;
add_to_group ( SceneStringNames : : get_singleton ( ) - > shader_overrides_group_active ) ;
2022-05-08 10:09:19 +02:00
for ( const KeyValue < StringName , Override > & E : overrides ) {
const Override * o = & E . value ;
2020-04-17 04:52:00 +02:00
if ( o - > in_use & & o - > override . get_type ( ) ! = Variant : : NIL ) {
2021-07-06 03:40:29 +02:00
if ( o - > override . get_type ( ) = = Variant : : OBJECT ) {
RID tex_rid = o - > override ;
2022-08-27 11:22:43 +02:00
RS : : get_singleton ( ) - > global_shader_parameter_set_override ( E . key , tex_rid ) ;
2021-07-06 03:40:29 +02:00
} else {
2022-08-27 11:22:43 +02:00
RS : : get_singleton ( ) - > global_shader_parameter_set_override ( E . key , o - > override ) ;
2021-07-06 03:40:29 +02:00
}
2020-04-17 04:52:00 +02:00
}
2022-05-08 10:09:19 +02:00
update_configuration_warnings ( ) ; //may have activated
}
2020-04-17 04:52:00 +02:00
}
}
void ShaderGlobalsOverride : : _notification ( int p_what ) {
2022-02-15 18:06:48 +01:00
switch ( p_what ) {
case Node3D : : NOTIFICATION_ENTER_TREE : {
add_to_group ( SceneStringNames : : get_singleton ( ) - > shader_overrides_group ) ;
_activate ( ) ;
} break ;
2020-04-17 04:52:00 +02:00
2022-02-15 18:06:48 +01:00
case Node3D : : NOTIFICATION_EXIT_TREE : {
if ( active ) {
//remove overrides
2022-05-08 10:09:19 +02:00
for ( const KeyValue < StringName , Override > & E : overrides ) {
const Override * o = & E . value ;
2022-02-15 18:06:48 +01:00
if ( o - > in_use ) {
2022-08-27 11:22:43 +02:00
RS : : get_singleton ( ) - > global_shader_parameter_set_override ( E . key , Variant ( ) ) ;
2022-02-15 18:06:48 +01:00
}
2020-04-17 04:52:00 +02:00
}
}
2022-02-15 18:06:48 +01:00
remove_from_group ( SceneStringNames : : get_singleton ( ) - > shader_overrides_group_active ) ;
remove_from_group ( SceneStringNames : : get_singleton ( ) - > shader_overrides_group ) ;
2021-08-12 23:40:13 +02:00
get_tree ( ) - > call_group_flags ( SceneTree : : GROUP_CALL_DEFERRED , SceneStringNames : : get_singleton ( ) - > shader_overrides_group , " _activate " ) ; //another may want to activate when this is removed
2022-02-15 18:06:48 +01:00
active = false ;
} break ;
2020-04-17 04:52:00 +02:00
}
}
2022-09-19 17:43:15 +02:00
PackedStringArray ShaderGlobalsOverride : : get_configuration_warnings ( ) const {
PackedStringArray warnings = Node : : get_configuration_warnings ( ) ;
2020-05-14 22:59:27 +02:00
2020-04-17 04:52:00 +02:00
if ( ! active ) {
2022-03-28 15:24:14 +02:00
warnings . push_back ( RTR ( " ShaderGlobalsOverride is not active because another node of the same type is in the scene. " ) ) ;
2020-04-17 04:52:00 +02:00
}
2020-10-29 11:01:28 +01:00
return warnings ;
2020-04-17 04:52:00 +02:00
}
void ShaderGlobalsOverride : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " _activate " ) , & ShaderGlobalsOverride : : _activate ) ;
}
2021-02-09 18:24:36 +01:00
ShaderGlobalsOverride : : ShaderGlobalsOverride ( ) { }