Moved Shader helpers to their own file

This commit is contained in:
Marc Gilleron 2022-08-15 21:22:59 +01:00
parent abc7653b88
commit 4cbfaf3df7
3 changed files with 88 additions and 54 deletions

View File

@ -10,6 +10,7 @@
#include "../../storage/voxel_buffer_gd.h" #include "../../storage/voxel_buffer_gd.h"
#include "../../util/container_funcs.h" #include "../../util/container_funcs.h"
#include "../../util/godot/funcs.h" #include "../../util/godot/funcs.h"
#include "../../util/godot/shader.h"
#include "../../util/log.h" #include "../../util/log.h"
#include "../../util/math/color.h" #include "../../util/math/color.h"
#include "../../util/math/conv.h" #include "../../util/math/conv.h"
@ -2268,60 +2269,6 @@ bool VoxelLodTerrain::get_octahedral_normal_encoding() const {
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
// TODO Cannot use `Shader.has_uniform()` because it is unreliable.
// See https://github.com/godotengine/godot/issues/64467
static bool shader_has_uniform(const Shader &shader, StringName uniform_name) {
List<PropertyInfo> params;
RenderingServer::get_singleton()->shader_get_shader_uniform_list(shader.get_rid(), &params);
for (const PropertyInfo &pi : params) {
if (pi.name == uniform_name) {
return true;
}
}
return false;
}
static String get_missing_uniform_names(Span<const StringName> expected_uniforms, const Shader &shader) {
String missing_uniforms;
// TODO Cannot use `Shader.has_uniform()` because it is unreliable.
// See https://github.com/godotengine/godot/issues/64467
// for (unsigned int i = 0; i < expected_uniforms.size(); ++i) {
// StringName uniform_name = expected_uniforms[i];
// ZN_ASSERT_CONTINUE(uniform_name != StringName());
// if (!shader.has_uniform(uniform_name)) {
// if (missing_uniforms.size() > 0) {
// missing_uniforms += ", ";
// }
// missing_uniforms += uniform_name;
// }
// }
List<PropertyInfo> params;
RenderingServer::get_singleton()->shader_get_shader_uniform_list(shader.get_rid(), &params);
for (unsigned int i = 0; i < expected_uniforms.size(); ++i) {
const String name = expected_uniforms[i];
bool found = false;
for (const PropertyInfo &pi : params) {
if (pi.name == name) {
found = true;
break;
}
}
if (!found) {
if (missing_uniforms.size() > 0) {
missing_uniforms += ", ";
}
missing_uniforms += name;
}
}
return missing_uniforms;
}
TypedArray<String> VoxelLodTerrain::get_configuration_warnings() const { TypedArray<String> VoxelLodTerrain::get_configuration_warnings() const {
TypedArray<String> warnings = VoxelNode::get_configuration_warnings(); TypedArray<String> warnings = VoxelNode::get_configuration_warnings();
if (!warnings.is_empty()) { if (!warnings.is_empty()) {

64
util/godot/shader.cpp Normal file
View File

@ -0,0 +1,64 @@
#include "shader.h"
#include <scene/resources/shader.h>
namespace zylann {
#ifdef TOOLS_ENABLED
// TODO Cannot use `Shader.has_uniform()` because it is unreliable.
// See https://github.com/godotengine/godot/issues/64467
bool shader_has_uniform(const Shader &shader, StringName uniform_name) {
List<PropertyInfo> params;
RenderingServer::get_singleton()->shader_get_shader_uniform_list(shader.get_rid(), &params);
for (const PropertyInfo &pi : params) {
if (pi.name == uniform_name) {
return true;
}
}
return false;
}
String get_missing_uniform_names(Span<const StringName> expected_uniforms, const Shader &shader) {
String missing_uniforms;
// TODO Cannot use `Shader.has_uniform()` because it is unreliable.
// See https://github.com/godotengine/godot/issues/64467
// for (unsigned int i = 0; i < expected_uniforms.size(); ++i) {
// StringName uniform_name = expected_uniforms[i];
// ZN_ASSERT_CONTINUE(uniform_name != StringName());
// if (!shader.has_uniform(uniform_name)) {
// if (missing_uniforms.size() > 0) {
// missing_uniforms += ", ";
// }
// missing_uniforms += uniform_name;
// }
// }
List<PropertyInfo> params;
RenderingServer::get_singleton()->shader_get_shader_uniform_list(shader.get_rid(), &params);
for (unsigned int i = 0; i < expected_uniforms.size(); ++i) {
const String name = expected_uniforms[i];
bool found = false;
for (const PropertyInfo &pi : params) {
if (pi.name == name) {
found = true;
break;
}
}
if (!found) {
if (missing_uniforms.size() > 0) {
missing_uniforms += ", ";
}
missing_uniforms += name;
}
}
return missing_uniforms;
}
#endif
} // namespace zylann

23
util/godot/shader.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef ZN_GODOT_SHADER_H
#define ZN_GODOT_SHADER_H
#include "../span.h"
#include <core/string/string_name.h>
class Shader;
namespace zylann {
#ifdef TOOLS_ENABLED
// TODO Cannot use `Shader.has_uniform()` because it is unreliable.
// See https://github.com/godotengine/godot/issues/64467
bool shader_has_uniform(const Shader &shader, StringName uniform_name);
String get_missing_uniform_names(Span<const StringName> expected_uniforms, const Shader &shader);
#endif
} // namespace zylann
#endif // ZN_GODOT_SHADER_H