Commit 477d0866 authored by Lázár Bence Kis's avatar Lázár Bence Kis
Browse files

WIP

parent 87ad0980
Loading
Loading
Loading
Loading
+43 −1
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ struct ShaderGraph final : public Shader {
    // INPUT NODES
    // ===================================
    struct UniformNode;
    struct UBONode;
    struct TimeNode;

    struct TextureNode;
@@ -48,6 +49,7 @@ struct ShaderGraph final : public Shader {
    struct VaryingWeightNode;
    struct VaryingColorNode;
    struct VaryingBoneNode;
    struct VaryingBoneMatrixUBONode;

    struct WorldPositionNode;

@@ -94,7 +96,10 @@ struct ShaderGraph final : public Shader {

    struct Node {
        struct DataType {
            enum ElementType { BOOL, INT, SCALAR, VEC2, VEC3, VEC4, UVEC2, UVEC3, UVEC4, MAT2X2, MAT3X3, MAT4X4 };
            enum ElementType { BOOL, INT, SCALAR, VEC2, VEC3, VEC4, VEC4_ARRAY, UVEC2, UVEC3, UVEC4, MAT2X2, MAT3X3, MAT4X4 };
            bool array = false;
            std::size_t size = 0;

            using elem_variant_t = std::variant<bool, int, scalar, vec2, vec3, vec4, mat2x2, mat3x3, mat4x4>;

            static std::string to_string(ElementType type) {
@@ -110,6 +115,7 @@ struct ShaderGraph final : public Shader {
                case VEC3:
                    return "vec3";
                case VEC4:
                case VEC4_ARRAY:
                    return "vec4";
                case UVEC2:
                    return "uvec2";
@@ -138,6 +144,7 @@ struct ShaderGraph final : public Shader {
            template<typename Unused> struct ToValueType<VEC2, Unused> { using value_type = vec2; };
            template<typename Unused> struct ToValueType<VEC3, Unused> { using value_type = vec3; };
            template<typename Unused> struct ToValueType<VEC4, Unused> { using value_type = vec4; };
            template<typename Unused> struct ToValueType<VEC4_ARRAY, Unused> { using value_type = std::vector<vec4>; };
            template<typename Unused> struct ToValueType<UVEC2, Unused> { using value_type = vec2u; };
            template<typename Unused> struct ToValueType<UVEC3, Unused> { using value_type = vec3u; };
            template<typename Unused> struct ToValueType<UVEC4, Unused> { using value_type = vec4u; };
@@ -152,6 +159,7 @@ struct ShaderGraph final : public Shader {
            template<typename Unused> struct ToElementType<vec2, Unused> { static constexpr ElementType value = VEC2; };
            template<typename Unused> struct ToElementType<vec3, Unused> { static constexpr ElementType value = VEC3; };
            template<typename Unused> struct ToElementType<vec4, Unused> { static constexpr ElementType value = VEC4; };
            template<typename Unused> struct ToElementType<std::vector<vec4>, Unused> { static constexpr ElementType value = VEC4_ARRAY; };
            // template<typename Unused> struct ToElementType<vec2u, Unused> { static constexpr ElementType value = UVEC2; };
            // template<typename Unused> struct ToElementType<vec3u, Unused> { static constexpr ElementType value = UVEC3; };
            // template<typename Unused> struct ToElementType<vec4u, Unused> { static constexpr ElementType value = UVEC4; };
@@ -315,6 +323,40 @@ struct ShaderGraph final : public Shader {
        ShaderGraph *m_shader = nullptr;
    };

    struct UniformArrayNode : public UniformNode {
        UniformArrayNode(const std::string &name, DataType::ElementType e, std::size_t size)
          : UniformNode(name, e)
          , m_size(size) {}

        void add_code(StagesCode &code, Stage stage) const override;

      private:
        std::size_t m_size;
    };

    struct UBONode : public InputNode {
        UBONode(const std::string &name, DataType::ElementType e)
          : InputNode({ name, name, { e } }) {}

        void add_code(StagesCode &code, Stage stage) const override;

        template <typename T>
        void set_uniform(T const &value) const {
            ASSUMPTION(m_outputs[0].definition.type.element_type == DataType::ToElementType<T>::value);
            ASSUMPTION(m_shader->gl_shader() != nullptr);

            bool in_vert = m_shader->maybe_set_uniform(stage_name(Stage::VERTEX), value);
            bool in_frag = m_shader->maybe_set_uniform(stage_name(Stage::FRAGMENT), value);
            ASSUMPTION(in_vert || in_frag);
        }

        DataType output_type() const { return m_outputs[0].definition.type; }

      private:
        friend struct ShaderGraph;
        ShaderGraph *m_shader = nullptr;
    };

    struct TextureNode : public Node {
        TextureNode(const com::ContextPath &tex_file, DataType::ElementType out_t,
            std::string override_sampler_name = "");
+13 −0
Original line number Diff line number Diff line
@@ -347,6 +347,19 @@ struct ShaderGraph::VaryingBoneNode : public InputVaryingNode {
    std::string universal_name() const override { return "vertex_bones"; }
};

struct ShaderGraph::VaryingBoneMatrixUBONode : public InputVaryingNode {
    VaryingBoneMatrixUBONode()
        : InputVaryingNode(
              { "bone",
                "The IDs of bones associated with the currently processed vertex.",
                { element_type } },
              default_layout) {}
    static constexpr Node::DataType::ElementType element_type = Node::DataType::UVEC4;
    static constexpr std::uint8_t default_layout = 6;

    std::string universal_name() const override { return "vertex_bones"; }
};

struct ShaderGraph::ErrorColorNode : public ConstantNode {
    static vec3 const error_color;
    ErrorColorNode() : ConstantNode{ error_color } {}
+5 −0
Original line number Diff line number Diff line
@@ -12,6 +12,11 @@ void ShaderGraph::UniformNode::add_code(StagesCode &code, Stage stage) const {
    code[stage].code.push_back(m_outputs[0].type_name() + ' ' + variable_name() + " = " + stage_name(stage));
}

void ShaderGraph::UniformArrayNode::add_code(StagesCode &code, Stage stage) const {
    code[stage].decl.push_back("uniform " + m_outputs[0].type_name() + ' ' + stage_name(stage) + '[' + std::to_string(m_size) + ']');
    code[stage].code.push_back(m_outputs[0].type_name() + ' ' + variable_name() + '[' + std::to_string(m_size) + "] = " + stage_name(stage));
}

ShaderGraph::Stage ShaderGraph::MasterNodeUnlit::output_to_input_stage(Stage out, std::size_t in) const {
    Stage s;
    switch (in) {