Allow setting a cubemap as default parameter to shader

This commit is contained in:
Chaosus 2024-08-04 14:43:55 +03:00
parent db76de5de8
commit db251a3f99
13 changed files with 171 additions and 26 deletions

View file

@ -12,7 +12,7 @@
</tutorials>
<methods>
<method name="get_default_texture_parameter" qualifiers="const">
<return type="Texture2D" />
<return type="Texture" />
<param index="0" name="name" type="StringName" />
<param index="1" name="index" type="int" default="0" />
<description>
@ -38,7 +38,7 @@
<method name="set_default_texture_parameter">
<return type="void" />
<param index="0" name="name" type="StringName" />
<param index="1" name="texture" type="Texture2D" />
<param index="1" name="texture" type="Texture" />
<param index="2" name="index" type="int" default="0" />
<description>
Sets the default texture to be used with a texture uniform. The default is used if a texture is not set in the [ShaderMaterial].

View file

@ -9,7 +9,7 @@
<tutorials>
</tutorials>
<members>
<member name="cube_map" type="Cubemap" setter="set_cube_map" getter="get_cube_map">
<member name="cube_map" type="TextureLayered" setter="set_cube_map" getter="get_cube_map">
The [Cubemap] texture to sample when using [constant SOURCE_TEXTURE] as [member source].
</member>
<member name="source" type="int" setter="set_source" getter="get_source" enum="VisualShaderNodeCubemap.Source" default="0">

View file

@ -9,7 +9,7 @@
<tutorials>
</tutorials>
<members>
<member name="texture_array" type="Texture2DArray" setter="set_texture_array" getter="get_texture_array">
<member name="texture_array" type="TextureLayered" setter="set_texture_array" getter="get_texture_array">
A source texture array. Used if [member VisualShaderNodeSample3D.source] is set to [constant VisualShaderNodeSample3D.SOURCE_TEXTURE].
</member>
</members>

View file

@ -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<Ref<Texture2D>>::ConstIterator itr = default_textures[i].params.begin(); itr != default_textures[i].params.end(); ++itr, ++j) {
for (List<Ref<Texture>>::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);
}
}

View file

@ -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.

View file

@ -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<Texture2D> &p_texture, int p_index) {
set_default_texture_parameter(p_name, p_texture, p_index);
}
Ref<Texture2D> 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

View file

@ -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<Texture2D> &p_texture, int p_index) {
void Shader::set_default_texture_parameter(const StringName &p_name, const Ref<Texture> &p_texture, int p_index) {
if (p_texture.is_valid()) {
if (!default_textures.has(p_name)) {
default_textures[p_name] = HashMap<int, Ref<Texture2D>>();
default_textures[p_name] = HashMap<int, Ref<Texture>>();
}
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<T
emit_changed();
}
Ref<Texture2D> Shader::get_default_texture_parameter(const StringName &p_name, int p_index) const {
Ref<Texture> 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<Texture2D> Shader::get_default_texture_parameter(const StringName &p_name, i
}
void Shader::get_default_texture_parameter_list(List<StringName> *r_textures) const {
for (const KeyValue<StringName, HashMap<int, Ref<Texture2D>>> &E : default_textures) {
for (const KeyValue<StringName, HashMap<int, Ref<Texture>>> &E : default_textures) {
r_textures->push_back(E.key);
}
}

View file

@ -58,7 +58,7 @@ private:
String code;
String include_path;
HashMap<StringName, HashMap<int, Ref<Texture2D>>> default_textures;
HashMap<StringName, HashMap<int, Ref<Texture>>> 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<Texture2D> &p_texture, int p_index = 0);
Ref<Texture2D> _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<PropertyInfo> *p_params, bool p_get_groups = false) const;
void set_default_texture_parameter(const StringName &p_name, const Ref<Texture2D> &p_texture, int p_index = 0);
Ref<Texture2D> get_default_texture_parameter(const StringName &p_name, int p_index = 0) const;
void set_default_texture_parameter(const StringName &p_name, const Ref<Texture> &p_texture, int p_index = 0);
Ref<Texture> get_default_texture_parameter(const StringName &p_name, int p_index = 0) const;
void get_default_texture_parameter_list(List<StringName> *r_textures) const;
virtual bool is_text_shader() const;

View file

@ -2953,7 +2953,7 @@ void VisualShader::_update_shader() const {
const_cast<VisualShader *>(this)->set_code(final_code);
for (int i = 0; i < default_tex_params.size(); i++) {
int j = 0;
for (List<Ref<Texture2D>>::ConstIterator itr = default_tex_params[i].params.begin(); itr != default_tex_params[i].params.end(); ++itr, ++j) {
for (List<Ref<Texture>>::ConstIterator itr = default_tex_params[i].params.begin(); itr != default_tex_params[i].params.end(); ++itr, ++j) {
const_cast<VisualShader *>(this)->set_default_texture_parameter(default_tex_params[i].name, *itr, j);
}
}

View file

@ -66,7 +66,7 @@ public:
struct DefaultTextureParam {
StringName name;
List<Ref<Texture2D>> params;
List<Ref<Texture>> params;
};
enum VaryingMode {

View file

@ -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<Cubemap> p_cube_map) {
set_cube_map(p_cube_map);
}
Ref<Cubemap> 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<Texture2DArray> p_texture_array) {
set_texture_array(p_texture_array);
}
Ref<Texture2DArray> 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

View file

@ -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<Texture2DArray> p_texture_array) {
void VisualShaderNodeTexture2DArray::set_texture_array(Ref<TextureLayered> p_texture_array) {
texture_array = p_texture_array;
emit_changed();
}
Ref<Texture2DArray> VisualShaderNodeTexture2DArray::get_texture_array() const {
Ref<TextureLayered> 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<Cubemap> p_cube_map) {
void VisualShaderNodeCubemap::set_cube_map(Ref<TextureLayered> p_cube_map) {
cube_map = p_cube_map;
emit_changed();
}
Ref<Cubemap> VisualShaderNodeCubemap::get_cube_map() const {
Ref<TextureLayered> 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);

View file

@ -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<Texture2DArray> texture_array;
Ref<TextureLayered> texture_array;
protected:
#ifndef DISABLE_DEPRECATED
void _set_texture_array_bind_compat_95126(Ref<Texture2DArray> p_texture_array);
Ref<Texture2DArray> _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<VisualShader::DefaultTextureParam> 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<Texture2DArray> p_texture_array);
Ref<Texture2DArray> get_texture_array() const;
void set_texture_array(Ref<TextureLayered> p_texture_array);
Ref<TextureLayered> get_texture_array() const;
virtual Vector<StringName> get_editable_properties() const override;
@ -608,7 +615,7 @@ public:
class VisualShaderNodeCubemap : public VisualShaderNode {
GDCLASS(VisualShaderNodeCubemap, VisualShaderNode);
Ref<Cubemap> cube_map;
Ref<TextureLayered> 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<Cubemap> p_cube_map);
Ref<Cubemap> _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<Cubemap> p_cube_map);
Ref<Cubemap> get_cube_map() const;
void set_cube_map(Ref<TextureLayered> p_cube_map);
Ref<TextureLayered> get_cube_map() const;
void set_texture_type(TextureType p_texture_type);
TextureType get_texture_type() const;