Skip to content

Commit

Permalink
Support more types in shader
Browse files Browse the repository at this point in the history
  • Loading branch information
iaomw committed Dec 6, 2024
1 parent 35ab479 commit 7685cb4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
21 changes: 10 additions & 11 deletions zeno/include/zeno/extra/ShaderNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ struct ShaderNode : INode {
ZENO_API ShaderNode();
ZENO_API ~ShaderNode() override;
};
using ShaderDataTypeList = std::tuple<bool, int, unsigned int, float, vec2f, vec3f, vec4f>;

inline const auto ShaderDataTypeNames = std::array { "bool", "int", "uint", "float", "vec2", "vec3", "vec4" };

static const inline std::map<std::string, int> TypeHint {

Expand All @@ -34,18 +37,14 @@ static const inline std::map<std::string, int> TypeHint {
{"vec4", 4}
};

static const inline std::map<int, std::string> TypeHintReverse {

{0, "bool"},
{10, "int"},
{11, "uint"},

{1, "float"},
{2, "vec2"},
{3, "vec3"},
{4, "vec4"}
};
static const inline std::map<int, std::string> TypeHintReverse = []() {

std::map<int, std::string> result {};
for (auto& [k, v] : TypeHint) {
result[v] = k;
}
return result;
} ();

template <class Derived>
struct ShaderNodeClone : ShaderNode {
Expand Down
31 changes: 13 additions & 18 deletions zeno/src/extra/ShaderNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <zeno/extra/ShaderNode.h>
#include <zeno/types/ShaderObject.h>
#include <zeno/types/NumericObject.h>
#include <zeno/utils/type_traits.h>
#include <sstream>
#include <cassert>

Expand Down Expand Up @@ -38,27 +39,21 @@ ZENO_API int EmissionPass::determineType(IObject *object) {

int type = std::visit([&] (auto const &value) -> int {
using T = std::decay_t<decltype(value)>;
size_t typeIdx = 0;

if constexpr (std::is_same_v<bool, T>) {
return 0;
} else if constexpr (std::is_same_v<int, T>) {
return 10;
} else if constexpr (std::is_same_v<unsigned int, T>) {
return 11;
}
zeno::static_for<0, std::tuple_size_v<ShaderDataTypeList>>([&] (auto i) {
using ThisType = std::tuple_element_t<i, ShaderDataTypeList>;

if constexpr (std::is_same_v<float, T>) {
return 1;
} else if constexpr (std::is_same_v<vec2f, T>) {
return 2;
} else if constexpr (std::is_same_v<vec3f, T>) {
return 3;
} else if constexpr (std::is_same_v<vec4f, T>) {
return 4;
} else {
throw zeno::Exception("bad numeric object type: " + (std::string)typeid(T).name());
}
if (std::is_same_v<ThisType, T>) {
typeIdx = i;
return true;
}
return false;
});

return TypeHint.at(ShaderDataTypeNames.at(typeIdx));
}, num->value);

constmap[num] = constants.size();
constants.push_back(ConstInfo{type, num->value});
return type;
Expand Down

0 comments on commit 7685cb4

Please sign in to comment.