This commit is contained in:
lachrymaLF 2024-05-24 05:42:43 -04:00
parent c78087ffe7
commit 6eaa92cdf8
4 changed files with 53 additions and 49 deletions

View file

@ -3,14 +3,14 @@
#include "srell.hpp" #include "srell.hpp"
namespace { namespace {
K::String build_node(const K::ShaderGraph::NodeInstance& n) { K::String BuildNode(const K::ShaderGraph::NodeInstance& n) {
K::String r = n.node->shader_template; K::String r = n.node->shader_template;
for (u32 i = 0; i < n.node->inputs.size(); i++) { for (u32 i = 0; i < n.node->inputs.size(); i++) {
K::String sub{}; K::String sub{};
if (n.inputs[i] != nullptr) if (n.inputs[i] != nullptr)
sub = build_node(*n.inputs[i]); sub = BuildNode(*n.inputs[i]);
else else
sub = K::ShaderGraph::var_to_string(n.values[i]); // should be made into uniform instead !! sub = K::ShaderGraph::VarToString(n.values[i]); // should be made into uniform instead !!
r = srell::regex_replace(r, srell::regex("\\{" + std::to_string(i) + "\\}"), sub); r = srell::regex_replace(r, srell::regex("\\{" + std::to_string(i) + "\\}"), sub);
} }
return r; return r;
@ -18,23 +18,23 @@ namespace {
} }
namespace K::ShaderGraph { namespace K::ShaderGraph {
String var_to_string(const std::variant<i32, f32, String, RGBA, XYZ>& var) { String VarToString(const T_Map<T_Count>::type& var) {
return std::visit([](auto&& arg) -> String { return std::visit([](auto&& arg) -> String {
using T = std::decay_t<decltype(arg)>; using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, T_Map<T_Int>::type>) if constexpr (std::is_same_v<T, T_Map<T_Float>::type>)
return std::to_string(arg);
else if constexpr (std::is_same_v<T, T_Map<T_Float>::type>)
return std::to_string(arg) + "f"; return std::to_string(arg) + "f";
else if constexpr (std::is_same_v<T, T_Map<T_String>::type>) else if constexpr (std::is_same_v<T, T_Map<T_Int>::type>)
return arg; return std::to_string(arg);
else if constexpr (std::is_same_v<T, T_Map<T_RGBA>::type>) else if constexpr (std::is_same_v<T, T_Map<T_RGBA>::type>)
return "vec4(" + std::to_string(arg.r) + "," + std::to_string(arg.g) + "," + std::to_string(arg.b) + "," + std::to_string(arg.a) + ")"; return "vec4(" + std::to_string(arg.r) + "," + std::to_string(arg.g) + "," + std::to_string(arg.b) + "," + std::to_string(arg.a) + ")";
else if constexpr (std::is_same_v<T, T_Map<T_XY>::type>)
return "vec2(" + std::to_string(arg.x) + "," + std::to_string(arg.y) + ")";
else if constexpr (std::is_same_v<T, T_Map<T_XYZ>::type>) else if constexpr (std::is_same_v<T, T_Map<T_XYZ>::type>)
return "vec3(" + std::to_string(arg.x) + "," + std::to_string(arg.y) + "," + std::to_string(arg.z) + ")"; return "vec3(" + std::to_string(arg.x) + "," + std::to_string(arg.y) + "," + std::to_string(arg.z) + ")";
}, var); }, var);
} }
String ShaderGraph::generate_shader() const { String ShaderGraph::generate_shader() const {
if (RGBA_node->node->out_types[RGBA_node_out_index] != Type::T_RGBA) return {}; if (RGBA_node->node->out_types[RGBA_node_out_index] != Type::T_RGBA) return {};
return build_node(*RGBA_node); return BuildNode(*RGBA_node);
} }
} }

View file

@ -99,6 +99,7 @@ namespace K::UI {
for (auto& uniform : layer.track.uniforms) { for (auto& uniform : layer.track.uniforms) {
bgfx::destroy(uniform.second.first); bgfx::destroy(uniform.second.first);
} }
layer.track.uniforms.clear();
} }
} }
@ -355,14 +356,14 @@ namespace K::UI {
ImGui::TableNextColumn(); ImGui::TableNextColumn();
std::visit([it](auto&& arg) { std::visit([it](auto&& arg) {
using T = std::decay_t<decltype(arg)>; using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Int>::type>) if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Float>::type>)
ImGui::InputInt(("##" + it->first).c_str(), &arg);
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Float>::type>)
ImGui::DragFloat(("##" + it->first).c_str(), &arg, 0.005f); ImGui::DragFloat(("##" + it->first).c_str(), &arg, 0.005f);
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_String>::type>) else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Int>::type>)
ImGui::InputText(("##" + it->first).c_str(), &arg); ImGui::InputInt(("##" + it->first).c_str(), &arg);
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_RGBA>::type>) else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_RGBA>::type>)
ImGui::InputFloat4(("##" + it->first).c_str(), &arg.r); ImGui::InputFloat4(("##" + it->first).c_str(), &arg.r);
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XY>::type>)
ImGui::InputFloat2(("##" + it->first).c_str(), &arg.x);
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XYZ>::type>) else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XYZ>::type>)
ImGui::InputFloat3(("##" + it->first).c_str(), &arg.x); ImGui::InputFloat3(("##" + it->first).c_str(), &arg.x);
}, it->second.second); }, it->second.second);

View file

@ -8,47 +8,48 @@
namespace K::ShaderGraph { namespace K::ShaderGraph {
enum Type { enum Type {
T_Int,
T_Float, T_Float,
T_String, T_Int,
T_RGBA, T_RGBA,
T_XY,
T_XYZ, T_XYZ,
T_Count T_Count
}; };
static const char *Type_To_Str[] = { static const char *Type_To_Str[] = {
"Int",
"Float", "Float",
"String", "Int",
"RGBA", "RGBA",
"XY",
"XYZ", "XYZ",
"Error" "Error"
}; };
struct RGBA { f32 r, g, b, a; }; struct RGBA { f32 r, g, b, a; };
struct XYZ { f32 x, y, z; }; struct XYZ { f32 x, y, z; };
struct XY { f32 x, y; };
template<Type> struct T_Map; template<Type> struct T_Map;
template<> struct T_Map<T_Int> {
using type = i32;
};
template<> struct T_Map<T_Float> { template<> struct T_Map<T_Float> {
using type = f32; using type = f32;
}; };
template<> struct T_Map<T_String> { template<> struct T_Map<T_Int> {
using type = String; using type = i32;
}; };
template<> struct T_Map<T_RGBA> { template<> struct T_Map<T_RGBA> {
using type = RGBA; using type = RGBA;
}; };
template<> struct T_Map<T_XY> {
using type = XY;
};
template<> struct T_Map<T_XYZ> { template<> struct T_Map<T_XYZ> {
using type = XYZ; using type = XYZ;
}; };
template<> struct T_Map<T_Count> { template<> struct T_Map<T_Count> {
using type = std::variant<i32, f32, String, RGBA, XYZ>; using type = std::variant<f32, i32, RGBA, XY, XYZ>;
}; };
String var_to_string(const std::variant<i32, f32, String, RGBA, XYZ>& var); String VarToString(const T_Map<T_Count>::type& var);
inline T_Map<T_Count>::type expand_type(Type i) { inline T_Map<T_Count>::type expand_type(Type i) {
static constexpr T_Map<T_Count>::type table[] = {i32{}, f32{}, String{}, ShaderGraph::RGBA{}, ShaderGraph::XYZ{}}; static constexpr T_Map<T_Count>::type table[] = {f32{}, i32{}, ShaderGraph::RGBA{}, ShaderGraph::XY{}, ShaderGraph::XYZ{}};
return table[i]; return table[i];
} }

View file

@ -41,17 +41,19 @@ namespace K {
f32 pack[4]{}; f32 pack[4]{};
std::visit([&pack](auto&& arg) { std::visit([&pack](auto&& arg) {
using T = std::decay_t<decltype(arg)>; using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Int>::type> || std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Float>::type>) if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Float>::type> || std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Int>::type>)
pack[0] = static_cast<f32>(arg); pack[0] = static_cast<f32>(arg);
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_String>::type>)
LogError("String passed to shader, verify specification."); // ??? damn bro
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_RGBA>::type>) { else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_RGBA>::type>) {
pack[0] = arg.r; pack[0] = arg.r;
pack[1] = arg.g; pack[1] = arg.g;
pack[2] = arg.b; pack[2] = arg.b;
pack[3] = arg.a; pack[3] = arg.a;
} }
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XYZ>::type>){ else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XY>::type>) {
pack[0] = arg.x;
pack[1] = arg.y;
}
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XYZ>::type>) {
pack[0] = arg.x; pack[0] = arg.x;
pack[1] = arg.y; pack[1] = arg.y;
pack[2] = arg.z; pack[2] = arg.z;
@ -79,19 +81,19 @@ namespace K {
f << "uniform vec4 " << "__" << u.first << ";\n"; f << "uniform vec4 " << "__" << u.first << ";\n";
} }
for (auto& u : uniforms) { for (auto& u : uniforms) {
f << "# define " << u.first << " __" << u.first;
std::visit([&u, &f](auto&& arg) { std::visit([&u, &f](auto&& arg) {
using T = std::decay_t<decltype(arg)>; using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Int>::type>) if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Float>::type> || std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Int>::type>)
f << "int " << u.first << " = __" << u.first << ".x" << ";\n"; f << ".x";
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_Float>::type>)
f << "float " << u.first << " = __" << u.first << ".x" << ";\n";
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_String>::type>)
;
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_RGBA>::type>) else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_RGBA>::type>)
f << "vec4 " << u.first << " = __" << u.first << ";\n"; ;
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XY>::type>)
f << ".xy";
else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XYZ>::type>) else if constexpr (std::is_same_v<T, ShaderGraph::T_Map<ShaderGraph::T_XYZ>::type>)
f << "vec3 " << u.first << " = __" << u.first << ".xyz" << ";\n"; f << ".xyz";
}, u.second.second); }, u.second.second);
f << "\n";
} }
f << shader; f << shader;
f.close(); f.close();