Skip to content

Commit

Permalink
feat(graph_shader): add vec3 global input parameter node
Browse files Browse the repository at this point in the history
  • Loading branch information
Silverlan committed Dec 22, 2024
1 parent 7de4c9e commit 09f8352
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace pragma::rendering {
auto *prop = m_inputDescriptor->FindProperty(name.data());
if(!prop)
return false;
if(!m_inputData->SetValue<float>(name.data(), val))
if(!m_inputData->SetValue<T>(name.data(), val))
return false;
m_dirtyTracker.MarkRange(prop->offset, sizeof(T));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace pragma::rendering::shader_graph {
class DLLCLIENT InputDataModule : public pragma::rendering::ShaderGraphModule {
public:
static void set_shader_input_value(const std::string &name, float val);
static void set_shader_input_value(const std::string &name, const Vector3 &val);
InputDataModule(prosper::Shader &shader);
virtual ~InputDataModule() override;
virtual void InitializeGfxPipelineDescriptorSets() override;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Silverlan
*/

#ifndef __PRAGMA_BASE_SHADER_GRAPH_NODES_INPUT_PARAMETER_HPP__
#define __PRAGMA_BASE_SHADER_GRAPH_NODES_INPUT_PARAMETER_HPP__

#include "pragma/clientdefinitions.h"
#include <cinttypes>
#include <string_view>
#include <string>

import pragma.shadergraph;

namespace pragma::rendering::shader_graph {
class DLLCLIENT BaseInputParameterNode : public pragma::shadergraph::Node {
public:
enum class Scope : uint32_t {
Global = 0,
Object,
Material,
};

static constexpr const char *CONST_NAME = "name";
static constexpr const char *CONST_SCOPE = "scope";
static constexpr const char *CONST_DEFAULT = "default";

static constexpr const char *OUT_VALUE = "value";

BaseInputParameterNode(const std::string_view &type);
virtual pragma::shadergraph::DataType GetParameterType() const = 0;
virtual std::string DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const override;
};
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,19 @@
#ifndef __PRAGMA_SHADER_GRAPH_NODES_INPUT_PARAMETER_FLOAT_HPP__
#define __PRAGMA_SHADER_GRAPH_NODES_INPUT_PARAMETER_FLOAT_HPP__

#include "pragma/clientdefinitions.h"
#include "pragma/rendering/shader_graph/nodes/base_input_parameter.hpp"

import pragma.shadergraph;

namespace pragma::rendering::shader_graph {
class DLLCLIENT InputParameterFloatNode : public pragma::shadergraph::Node {
class DLLCLIENT InputParameterFloatNode : public BaseInputParameterNode {
public:
enum class Scope : uint32_t {
Global = 0,
Object,
Material,
};

static constexpr const char *CONST_NAME = "name";
static constexpr const char *CONST_SCOPE = "scope";
static constexpr const char *CONST_DEFAULT = "default";
static constexpr const char *CONST_MIN = "min";
static constexpr const char *CONST_MAX = "max";
static constexpr const char *CONST_STEP_SIZE = "stepSize";

static constexpr const char *OUT_VALUE = "value";

InputParameterFloatNode(const std::string_view &type);

virtual pragma::shadergraph::DataType GetParameterType() const override { return pragma::shadergraph::DataType::Float; }
virtual std::string DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const override;
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Silverlan
*/

#ifndef __PRAGMA_SHADER_GRAPH_NODES_INPUT_PARAMETER_VEC3_HPP__
#define __PRAGMA_SHADER_GRAPH_NODES_INPUT_PARAMETER_VEC3_HPP__

#include "pragma/rendering/shader_graph/nodes/base_input_parameter.hpp"

import pragma.shadergraph;

namespace pragma::rendering::shader_graph {
class DLLCLIENT InputParameterVec3Node : public BaseInputParameterNode {
public:
InputParameterVec3Node(const std::string_view &type);
virtual pragma::shadergraph::DataType GetParameterType() const override { return pragma::shadergraph::DataType::Vector; }
virtual std::string DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const override;
};
};

#endif
3 changes: 2 additions & 1 deletion core/client/src/lua/c_luaclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ static luabind::object shader_mat_value_to_lua_object(lua_State *l, const pragma
#include "pragma/rendering/shader_graph/modules/input_data.hpp"
static void register_shader_graph(lua_State *l, luabind::module_ &modShader)
{
modShader[luabind::def("set_shader_input_value", &pragma::rendering::shader_graph::InputDataModule::set_shader_input_value)];
modShader[luabind::def("set_shader_input_value", static_cast<void (*)(const std::string &, float)>(&pragma::rendering::shader_graph::InputDataModule::set_shader_input_value))];
modShader[luabind::def("set_shader_input_value", static_cast<void (*)(const std::string &, const Vector3 &)>(&pragma::rendering::shader_graph::InputDataModule::set_shader_input_value))];

modShader[luabind::def(
"get_test_node_register", +[]() -> std::shared_ptr<pragma::shadergraph::NodeRegistry> {
Expand Down
59 changes: 36 additions & 23 deletions core/client/src/rendering/global_shader_input_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ void pragma::rendering::GlobalShaderInputDataManager::PopulateProperties(const p
{
std::vector<pragma::shadergraph::GraphNode *> globalParamNodes;
for(auto &node : graph.GetNodes()) {
auto *floatNode = dynamic_cast<const pragma::rendering::shader_graph::InputParameterFloatNode *>(&node->node);
if(!floatNode)
auto *paramNode = dynamic_cast<const pragma::rendering::shader_graph::BaseInputParameterNode *>(&node->node);
if(!paramNode)
continue;
pragma::rendering::shader_graph::InputParameterFloatNode::Scope scope;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_SCOPE, scope))
pragma::rendering::shader_graph::BaseInputParameterNode::Scope scope;
if(!node->GetInputValue(pragma::rendering::shader_graph::BaseInputParameterNode::CONST_SCOPE, scope))
continue;
if(scope != pragma::rendering::shader_graph::InputParameterFloatNode::Scope::Global)
if(scope != pragma::rendering::shader_graph::BaseInputParameterNode::Scope::Global)
continue;
if(globalParamNodes.size() == globalParamNodes.capacity())
globalParamNodes.reserve(globalParamNodes.size() * 2 + 10);
Expand All @@ -42,9 +42,9 @@ void pragma::rendering::GlobalShaderInputDataManager::PopulateProperties(const p
std::vector<pragma::rendering::Property> params;
params.reserve(globalParamNodes.size());
for(auto *node : globalParamNodes) {
auto &floatNode = *dynamic_cast<const pragma::rendering::shader_graph::InputParameterFloatNode *>(&node->node);
auto &paramNode = *dynamic_cast<const pragma::rendering::shader_graph::BaseInputParameterNode *>(&node->node);
std::string name;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_NAME, name))
if(!node->GetInputValue(pragma::rendering::shader_graph::BaseInputParameterNode::CONST_NAME, name))
continue;

if(name.empty())
Expand All @@ -53,23 +53,36 @@ void pragma::rendering::GlobalShaderInputDataManager::PopulateProperties(const p
if(m_inputDescriptor->FindProperty(name.c_str()) != nullptr)
continue; // TODO: What if a parameter is used in multiple shader graphs with different types?

float defaultVal;
float minVal;
float maxVal;
float stepSize;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_DEFAULT, defaultVal))
auto type = paramNode.GetParameterType();
pragma::rendering::Property prop {name, type};

auto res = pragma::shadergraph::visit(type, [this, node, &prop](auto tag) -> bool {
using T = typename decltype(tag)::type;

T defaultVal;
if(!node->GetInputValue(pragma::rendering::shader_graph::BaseInputParameterNode::CONST_DEFAULT, defaultVal))
return false;
prop->defaultValue.Set(defaultVal);

if constexpr(std::is_same_v<T, float>) {
float minVal;
float maxVal;
float stepSize;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_MIN, minVal))
return false;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_MAX, maxVal))
return false;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_STEP_SIZE, stepSize))
return false;
prop->min = minVal;
prop->max = maxVal;
// prop->stepSize = stepSize;
}

return true;
});
if(!res)
continue;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_MIN, minVal))
continue;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_MAX, maxVal))
continue;
if(!node->GetInputValue(pragma::rendering::shader_graph::InputParameterFloatNode::CONST_STEP_SIZE, stepSize))
continue;

pragma::rendering::Property prop {name, pragma::shadergraph::DataType::Float};
prop->defaultValue.Set(defaultVal);
prop->min = minVal;
prop->max = maxVal;
params.push_back(prop);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ void ImageTextureModule::InitializeGfxPipelineDescriptorSets()
std::string fileName;
node->GetInputValue<std::string>(pragma::rendering::shader_graph::ImageTextureNode::IN_FILENAME, fileName);
auto tex = texManager.LoadAsset(fileName);
ds.SetBindingTexture(*tex->GetVkTexture(), bindingIdx++);
std::shared_ptr<prosper::Texture> prosperTex;
if(tex)
prosperTex = tex->GetVkTexture();
if(!prosperTex)
prosperTex = context.GetDummyTexture();
ds.SetBindingTexture(*prosperTex, bindingIdx++);
}

m_dsg = dsg;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Silverlan
*/

#include "stdafx_client.h"
#include "pragma/rendering/shader_graph/nodes/base_input_parameter.hpp"

using namespace pragma::rendering::shader_graph;

BaseInputParameterNode::BaseInputParameterNode(const std::string_view &type) : Node {type}
{
AddInput(CONST_NAME, pragma::shadergraph::DataType::String, "");
AddSocketEnum<Scope>(CONST_SCOPE, Scope::Global);

AddModuleDependency("input_data");
}

std::string BaseInputParameterNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &gn) const
{
std::ostringstream code;

std::string name;
gn.GetInputValue<std::string>(CONST_NAME, name);

code << gn.GetGlslOutputDeclaration(OUT_VALUE) << " = ";
// TODO: Check if name exists in global input data
if(!name.empty())
code << "u_globalInputData." << name << ";\n";
else
code << "0.0;\n";

return code.str();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,14 @@

using namespace pragma::rendering::shader_graph;

InputParameterFloatNode::InputParameterFloatNode(const std::string_view &type) : Node {type}
InputParameterFloatNode::InputParameterFloatNode(const std::string_view &type) : BaseInputParameterNode {type}
{
// Global parameter buffer
AddInput(CONST_NAME, pragma::shadergraph::DataType::String, "");
AddSocketEnum<Scope>(CONST_SCOPE, Scope::Global);
AddInput(CONST_DEFAULT, pragma::shadergraph::DataType::Float, 0.f);
AddInput(CONST_MIN, pragma::shadergraph::DataType::Float, 0.f);
AddInput(CONST_MAX, pragma::shadergraph::DataType::Float, 1.f);
AddInput(CONST_STEP_SIZE, pragma::shadergraph::DataType::Float, 0.1f);
AddInput(CONST_DEFAULT, pragma::shadergraph::DataType::Float, 0.f);

AddOutput(OUT_VALUE, pragma::shadergraph::DataType::Float);

AddModuleDependency("input_data");
}

std::string InputParameterFloatNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &gn) const
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024 Silverlan
*/

#include "stdafx_client.h"
#include "pragma/rendering/shader_graph/nodes/input_parameter_vec3.hpp"

using namespace pragma::rendering::shader_graph;

InputParameterVec3Node::InputParameterVec3Node(const std::string_view &type) : BaseInputParameterNode {type}
{
AddInput(CONST_DEFAULT, pragma::shadergraph::DataType::Vector, Vector3 {0.f, 0.f, 0.f});

AddOutput(OUT_VALUE, pragma::shadergraph::DataType::Vector);
}

std::string InputParameterVec3Node::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &gn) const
{
std::ostringstream code;

std::string name;
gn.GetInputValue<std::string>(CONST_NAME, name);

code << gn.GetGlslOutputDeclaration(OUT_VALUE) << " = ";
// TODO: Check if name exists in global input data
if(!name.empty())
code << "u_globalInputData." << name << ";\n";
else
code << "0.0;\n";

return code.str();
}

0 comments on commit 09f8352

Please sign in to comment.