From db251a3f99eadbd26a015bb8a9e701f5723df08c Mon Sep 17 00:00:00 2001 From: Chaosus Date: Sun, 4 Aug 2024 14:43:55 +0300 Subject: [PATCH] Allow setting a cubemap as default parameter to shader --- doc/classes/Shader.xml | 4 +- doc/classes/VisualShaderNodeCubemap.xml | 2 +- .../VisualShaderNodeTexture2DArray.xml | 2 +- .../plugins/visual_shader_editor_plugin.cpp | 2 +- .../4.3-stable.expected | 15 +++++ scene/resources/shader.compat.inc | 46 ++++++++++++++ scene/resources/shader.cpp | 9 +-- scene/resources/shader.h | 12 +++- scene/resources/visual_shader.cpp | 2 +- scene/resources/visual_shader.h | 2 +- .../resources/visual_shader_nodes.compat.inc | 63 +++++++++++++++++++ scene/resources/visual_shader_nodes.cpp | 13 ++-- scene/resources/visual_shader_nodes.h | 25 ++++++-- 13 files changed, 171 insertions(+), 26 deletions(-) create mode 100644 scene/resources/shader.compat.inc create mode 100644 scene/resources/visual_shader_nodes.compat.inc diff --git a/doc/classes/Shader.xml b/doc/classes/Shader.xml index b71f9ca1b02..68176dea143 100644 --- a/doc/classes/Shader.xml +++ b/doc/classes/Shader.xml @@ -12,7 +12,7 @@ - + @@ -38,7 +38,7 @@ - + Sets the default texture to be used with a texture uniform. The default is used if a texture is not set in the [ShaderMaterial]. diff --git a/doc/classes/VisualShaderNodeCubemap.xml b/doc/classes/VisualShaderNodeCubemap.xml index 4e6cf2120a8..6f3df9865aa 100644 --- a/doc/classes/VisualShaderNodeCubemap.xml +++ b/doc/classes/VisualShaderNodeCubemap.xml @@ -9,7 +9,7 @@ - + The [Cubemap] texture to sample when using [constant SOURCE_TEXTURE] as [member source]. diff --git a/doc/classes/VisualShaderNodeTexture2DArray.xml b/doc/classes/VisualShaderNodeTexture2DArray.xml index 8852cb7cb4d..bdf5a428211 100644 --- a/doc/classes/VisualShaderNodeTexture2DArray.xml +++ b/doc/classes/VisualShaderNodeTexture2DArray.xml @@ -9,7 +9,7 @@ - + A source texture array. Used if [member VisualShaderNodeSample3D.source] is set to [constant VisualShaderNodeSample3D.SOURCE_TEXTURE]. diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index c183aceb136..45714b78a1c 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -7978,7 +7978,7 @@ void VisualShaderNodePortPreview::_shader_changed() { preview_shader->set_code(shader_code); for (int i = 0; i < default_textures.size(); i++) { int j = 0; - for (List>::ConstIterator itr = default_textures[i].params.begin(); itr != default_textures[i].params.end(); ++itr, ++j) { + for (List>::ConstIterator itr = default_textures[i].params.begin(); itr != default_textures[i].params.end(); ++itr, ++j) { preview_shader->set_default_texture_parameter(default_textures[i].name, *itr, j); } } diff --git a/misc/extension_api_validation/4.3-stable.expected b/misc/extension_api_validation/4.3-stable.expected index 733d85c46e7..d734a74976f 100644 --- a/misc/extension_api_validation/4.3-stable.expected +++ b/misc/extension_api_validation/4.3-stable.expected @@ -26,3 +26,18 @@ Validate extension JSON: Error: Field 'classes/RenderingDevice/methods/draw_list draw_list_begin added a new optional debug argument called breadcrumb. There used to be an Array argument as arg #9 initially, then changed to typedarray::RID in 4.1, and finally removed in 4.3. Since we're adding a new one at the same location, we need to silence those warnings for 4.1 and 4.3. + + +GH-95126 +-------- +Validate extension JSON: Error: Field 'classes/Shader/methods/get_default_texture_parameter/return_value': type changed value in new API, from "Texture2D" to "Texture". +Validate extension JSON: Error: Field 'classes/Shader/methods/set_default_texture_parameter/arguments/1': type changed value in new API, from "Texture2D" to "Texture". +Validate extension JSON: Error: Field 'classes/VisualShaderNodeCubemap/methods/get_cube_map/return_value': type changed value in new API, from "Cubemap" to "TextureLayered". +Validate extension JSON: Error: Field 'classes/VisualShaderNodeCubemap/methods/set_cube_map/arguments/0': type changed value in new API, from "Cubemap" to "TextureLayered". +Validate extension JSON: Error: Field 'classes/VisualShaderNodeCubemap/properties/cube_map': type changed value in new API, from "Cubemap" to "Cubemap,CompressedCubemap,PlaceholderCubemap,TextureCubemapRD". +Validate extension JSON: Error: Field 'classes/VisualShaderNodeTexture2DArray/methods/get_texture_array/return_value': type changed value in new API, from "Texture2DArray" to "TextureLayered". +Validate extension JSON: Error: Field 'classes/VisualShaderNodeTexture2DArray/methods/set_texture_array/arguments/0': type changed value in new API, from "Texture2DArray" to "TextureLayered". +Validate extension JSON: Error: Field 'classes/VisualShaderNodeTexture2DArray/properties/texture_array': type changed value in new API, from "Texture2DArray" to "Texture2DArray,CompressedTexture2DArray,PlaceholderTexture2DArray,Texture2DArrayRD". + +Allow setting a cubemap as default parameter to shader. +Compatibility methods registered. diff --git a/scene/resources/shader.compat.inc b/scene/resources/shader.compat.inc new file mode 100644 index 00000000000..b68020605f9 --- /dev/null +++ b/scene/resources/shader.compat.inc @@ -0,0 +1,46 @@ +/**************************************************************************/ +/* shader.compat.inc */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#ifndef DISABLE_DEPRECATED + +void Shader::_set_default_texture_parameter_bind_compat_95126(const StringName &p_name, const Ref &p_texture, int p_index) { + set_default_texture_parameter(p_name, p_texture, p_index); +} + +Ref Shader::_get_default_texture_parameter_bind_compat_95126(const StringName &p_name, int p_index) const { + return get_default_texture_parameter(p_name, p_index); +} + +void Shader::_bind_compatibility_methods() { + ClassDB::bind_compatibility_method(D_METHOD("set_default_texture_parameter", "name", "texture", "index"), &Shader::_set_default_texture_parameter_bind_compat_95126, DEFVAL(0)); + ClassDB::bind_compatibility_method(D_METHOD("get_default_texture_parameter", "name", "index"), &Shader::_get_default_texture_parameter_bind_compat_95126, DEFVAL(0)); +} + +#endif // DISABLE_DEPRECATED diff --git a/scene/resources/shader.cpp b/scene/resources/shader.cpp index dfe5bd4a472..f343229cd88 100644 --- a/scene/resources/shader.cpp +++ b/scene/resources/shader.cpp @@ -29,6 +29,7 @@ /**************************************************************************/ #include "shader.h" +#include "shader.compat.inc" #include "core/io/file_access.h" #include "servers/rendering/shader_language.h" @@ -185,10 +186,10 @@ RID Shader::get_rid() const { return shader; } -void Shader::set_default_texture_parameter(const StringName &p_name, const Ref &p_texture, int p_index) { +void Shader::set_default_texture_parameter(const StringName &p_name, const Ref &p_texture, int p_index) { if (p_texture.is_valid()) { if (!default_textures.has(p_name)) { - default_textures[p_name] = HashMap>(); + default_textures[p_name] = HashMap>(); } default_textures[p_name][p_index] = p_texture; RS::get_singleton()->shader_set_default_texture_parameter(shader, p_name, p_texture->get_rid(), p_index); @@ -206,7 +207,7 @@ void Shader::set_default_texture_parameter(const StringName &p_name, const Ref Shader::get_default_texture_parameter(const StringName &p_name, int p_index) const { +Ref Shader::get_default_texture_parameter(const StringName &p_name, int p_index) const { if (default_textures.has(p_name) && default_textures[p_name].has(p_index)) { return default_textures[p_name][p_index]; } @@ -214,7 +215,7 @@ Ref Shader::get_default_texture_parameter(const StringName &p_name, i } void Shader::get_default_texture_parameter_list(List *r_textures) const { - for (const KeyValue>> &E : default_textures) { + for (const KeyValue>> &E : default_textures) { r_textures->push_back(E.key); } } diff --git a/scene/resources/shader.h b/scene/resources/shader.h index 921143c219c..682fbd7ea6a 100644 --- a/scene/resources/shader.h +++ b/scene/resources/shader.h @@ -58,7 +58,7 @@ private: String code; String include_path; - HashMap>> default_textures; + HashMap>> default_textures; void _dependency_changed(); void _recompile(); @@ -66,6 +66,12 @@ private: Array _get_shader_uniform_list(bool p_get_groups = false); protected: +#ifndef DISABLE_DEPRECATED + void _set_default_texture_parameter_bind_compat_95126(const StringName &p_name, const Ref &p_texture, int p_index = 0); + Ref _get_default_texture_parameter_bind_compat_95126(const StringName &p_name, int p_index = 0) const; + static void _bind_compatibility_methods(); +#endif // DISABLE_DEPRECATED + static void _bind_methods(); public: @@ -80,8 +86,8 @@ public: void get_shader_uniform_list(List *p_params, bool p_get_groups = false) const; - void set_default_texture_parameter(const StringName &p_name, const Ref &p_texture, int p_index = 0); - Ref get_default_texture_parameter(const StringName &p_name, int p_index = 0) const; + void set_default_texture_parameter(const StringName &p_name, const Ref &p_texture, int p_index = 0); + Ref get_default_texture_parameter(const StringName &p_name, int p_index = 0) const; void get_default_texture_parameter_list(List *r_textures) const; virtual bool is_text_shader() const; diff --git a/scene/resources/visual_shader.cpp b/scene/resources/visual_shader.cpp index a144c5ba830..005b5b5c0bc 100644 --- a/scene/resources/visual_shader.cpp +++ b/scene/resources/visual_shader.cpp @@ -2953,7 +2953,7 @@ void VisualShader::_update_shader() const { const_cast(this)->set_code(final_code); for (int i = 0; i < default_tex_params.size(); i++) { int j = 0; - for (List>::ConstIterator itr = default_tex_params[i].params.begin(); itr != default_tex_params[i].params.end(); ++itr, ++j) { + for (List>::ConstIterator itr = default_tex_params[i].params.begin(); itr != default_tex_params[i].params.end(); ++itr, ++j) { const_cast(this)->set_default_texture_parameter(default_tex_params[i].name, *itr, j); } } diff --git a/scene/resources/visual_shader.h b/scene/resources/visual_shader.h index 2b213948def..8ec52fcaaa0 100644 --- a/scene/resources/visual_shader.h +++ b/scene/resources/visual_shader.h @@ -66,7 +66,7 @@ public: struct DefaultTextureParam { StringName name; - List> params; + List> params; }; enum VaryingMode { diff --git a/scene/resources/visual_shader_nodes.compat.inc b/scene/resources/visual_shader_nodes.compat.inc new file mode 100644 index 00000000000..31d96d9c0f7 --- /dev/null +++ b/scene/resources/visual_shader_nodes.compat.inc @@ -0,0 +1,63 @@ +/**************************************************************************/ +/* visual_shader_nodes.compat.inc */ +/**************************************************************************/ +/* 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. */ +/**************************************************************************/ + +#ifndef DISABLE_DEPRECATED + +// VisualShaderNodeCubemap + +void VisualShaderNodeCubemap::_set_cube_map_bind_compat_95126(Ref p_cube_map) { + set_cube_map(p_cube_map); +} + +Ref VisualShaderNodeCubemap::_get_cube_map_bind_compat_95126() const { + return cube_map; +} + +void VisualShaderNodeCubemap::_bind_compatibility_methods() { + ClassDB::bind_compatibility_method(D_METHOD("set_cube_map", "value"), &VisualShaderNodeCubemap::_set_cube_map_bind_compat_95126); + ClassDB::bind_compatibility_method(D_METHOD("get_cube_map"), &VisualShaderNodeCubemap::_get_cube_map_bind_compat_95126); +} + +// VisualShaderNodeTexture2DArray + +void VisualShaderNodeTexture2DArray::_set_texture_array_bind_compat_95126(Ref p_texture_array) { + set_texture_array(p_texture_array); +} + +Ref VisualShaderNodeTexture2DArray::_get_texture_array_bind_compat_95126() const { + return texture_array; +} + +void VisualShaderNodeTexture2DArray::_bind_compatibility_methods() { + ClassDB::bind_compatibility_method(D_METHOD("set_texture_array", "value"), &VisualShaderNodeTexture2DArray::_set_texture_array_bind_compat_95126); + ClassDB::bind_compatibility_method(D_METHOD("get_texture_array"), &VisualShaderNodeTexture2DArray::_get_texture_array_bind_compat_95126); +} + +#endif // DISABLE_DEPRECATED diff --git a/scene/resources/visual_shader_nodes.cpp b/scene/resources/visual_shader_nodes.cpp index 5e148c9276d..26666538af4 100644 --- a/scene/resources/visual_shader_nodes.cpp +++ b/scene/resources/visual_shader_nodes.cpp @@ -29,6 +29,7 @@ /**************************************************************************/ #include "visual_shader_nodes.h" +#include "visual_shader_nodes.compat.inc" #include "scene/resources/image_texture.h" @@ -1353,12 +1354,12 @@ String VisualShaderNodeTexture2DArray::generate_global(Shader::Mode p_mode, Visu return String(); } -void VisualShaderNodeTexture2DArray::set_texture_array(Ref p_texture_array) { +void VisualShaderNodeTexture2DArray::set_texture_array(Ref p_texture_array) { texture_array = p_texture_array; emit_changed(); } -Ref VisualShaderNodeTexture2DArray::get_texture_array() const { +Ref VisualShaderNodeTexture2DArray::get_texture_array() const { return texture_array; } @@ -1375,7 +1376,7 @@ void VisualShaderNodeTexture2DArray::_bind_methods() { ClassDB::bind_method(D_METHOD("set_texture_array", "value"), &VisualShaderNodeTexture2DArray::set_texture_array); ClassDB::bind_method(D_METHOD("get_texture_array"), &VisualShaderNodeTexture2DArray::get_texture_array); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_array", PROPERTY_HINT_RESOURCE_TYPE, "Texture2DArray"), "set_texture_array", "get_texture_array"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_array", PROPERTY_HINT_RESOURCE_TYPE, "Texture2DArray,CompressedTexture2DArray,PlaceholderTexture2DArray,Texture2DArrayRD"), "set_texture_array", "get_texture_array"); } VisualShaderNodeTexture2DArray::VisualShaderNodeTexture2DArray() { @@ -1568,12 +1569,12 @@ VisualShaderNodeCubemap::Source VisualShaderNodeCubemap::get_source() const { return source; } -void VisualShaderNodeCubemap::set_cube_map(Ref p_cube_map) { +void VisualShaderNodeCubemap::set_cube_map(Ref p_cube_map) { cube_map = p_cube_map; emit_changed(); } -Ref VisualShaderNodeCubemap::get_cube_map() const { +Ref VisualShaderNodeCubemap::get_cube_map() const { return cube_map; } @@ -1618,7 +1619,7 @@ void VisualShaderNodeCubemap::_bind_methods() { ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeCubemap::get_texture_type); ADD_PROPERTY(PropertyInfo(Variant::INT, "source", PROPERTY_HINT_ENUM, "Texture,SamplerPort"), "set_source", "get_source"); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "cube_map", PROPERTY_HINT_RESOURCE_TYPE, "Cubemap"), "set_cube_map", "get_cube_map"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "cube_map", PROPERTY_HINT_RESOURCE_TYPE, "Cubemap,CompressedCubemap,PlaceholderCubemap,TextureCubemapRD"), "set_cube_map", "get_cube_map"); ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normal Map"), "set_texture_type", "get_texture_type"); BIND_ENUM_CONSTANT(SOURCE_TEXTURE); diff --git a/scene/resources/visual_shader_nodes.h b/scene/resources/visual_shader_nodes.h index 279599ef9c7..ff02e55fb2c 100644 --- a/scene/resources/visual_shader_nodes.h +++ b/scene/resources/visual_shader_nodes.h @@ -31,6 +31,7 @@ #ifndef VISUAL_SHADER_NODES_H #define VISUAL_SHADER_NODES_H +#include "scene/resources/compressed_texture.h" #include "scene/resources/curve_texture.h" #include "scene/resources/visual_shader.h" @@ -562,9 +563,15 @@ VARIANT_ENUM_CAST(VisualShaderNodeSample3D::Source) class VisualShaderNodeTexture2DArray : public VisualShaderNodeSample3D { GDCLASS(VisualShaderNodeTexture2DArray, VisualShaderNodeSample3D); - Ref texture_array; + Ref texture_array; protected: +#ifndef DISABLE_DEPRECATED + void _set_texture_array_bind_compat_95126(Ref p_texture_array); + Ref _get_texture_array_bind_compat_95126() const; + static void _bind_compatibility_methods(); +#endif // DISABLE_DEPRECATED + static void _bind_methods(); public: @@ -575,8 +582,8 @@ public: virtual Vector get_default_texture_parameters(VisualShader::Type p_type, int p_id) const override; virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const override; - void set_texture_array(Ref p_texture_array); - Ref get_texture_array() const; + void set_texture_array(Ref p_texture_array); + Ref get_texture_array() const; virtual Vector get_editable_properties() const override; @@ -608,7 +615,7 @@ public: class VisualShaderNodeCubemap : public VisualShaderNode { GDCLASS(VisualShaderNodeCubemap, VisualShaderNode); - Ref cube_map; + Ref cube_map; public: enum Source { @@ -629,6 +636,12 @@ private: TextureType texture_type = TYPE_DATA; protected: +#ifndef DISABLE_DEPRECATED + void _set_cube_map_bind_compat_95126(Ref p_cube_map); + Ref _get_cube_map_bind_compat_95126() const; + static void _bind_compatibility_methods(); +#endif // DISABLE_DEPRECATED + static void _bind_methods(); public: @@ -650,8 +663,8 @@ public: void set_source(Source p_source); Source get_source() const; - void set_cube_map(Ref p_cube_map); - Ref get_cube_map() const; + void set_cube_map(Ref p_cube_map); + Ref get_cube_map() const; void set_texture_type(TextureType p_texture_type); TextureType get_texture_type() const;