Skip to content

Commit

Permalink
feat(shader_graph): add geometry, texture_coordinate and vector_trans…
Browse files Browse the repository at this point in the history
…form nodes
  • Loading branch information
Silverlan committed Nov 27, 2024
1 parent 44db04d commit aa9aa5a
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 18 deletions.
1 change: 1 addition & 0 deletions assets/shaders/modules/entity.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "/common/inputs/entity.glsl"
1 change: 1 addition & 0 deletions assets/shaders/modules/uv_data.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "/common/inputs/textures/parallax_map.glsl"
1 change: 1 addition & 0 deletions assets/shaders/modules/vertex_data.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "/common/vertex_outputs/vertex_data.glsl"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* 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_GEOMETRY_HPP__
#define __PRAGMA_SHADER_GRAPH_NODES_GEOMETRY_HPP__

#include "pragma/clientdefinitions.h"

import pragma.shadergraph;

namespace pragma::rendering::shader_graph {
class DLLCLIENT GeometryNode : public pragma::shadergraph::Node {
public:
static constexpr const char *OUT_POSITION_WS = "position_ws";
static constexpr const char *OUT_NORMAL_WS = "normal_ws";
static constexpr const char *OUT_NORMAL_CS = "normal_cs";
static constexpr const char *OUT_TANGENT_WS = "tangent_ws";

GeometryNode(const std::string_view &type);

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
@@ -0,0 +1,26 @@
/* 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_TEXTURE_COORDINATE_HPP__
#define __PRAGMA_SHADER_GRAPH_NODES_TEXTURE_COORDINATE_HPP__

#include "pragma/clientdefinitions.h"

import pragma.shadergraph;

namespace pragma::rendering::shader_graph {
class DLLCLIENT TextureCoordinateNode : public pragma::shadergraph::Node {
public:
static constexpr const char *OUT_UV = "uv";

TextureCoordinateNode(const std::string_view &type);

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
@@ -0,0 +1,43 @@
/* 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_VECTOR_TRANSFORM_HPP__
#define __PRAGMA_SHADER_GRAPH_NODES_VECTOR_TRANSFORM_HPP__

#include "pragma/clientdefinitions.h"

import pragma.shadergraph;

namespace pragma::rendering::shader_graph {
class DLLCLIENT VectorTransformNode : public pragma::shadergraph::Node {
public:
enum class Type : uint8_t {
Vector = 0,
Point,
Normal,
};

enum class Space : uint8_t {
World = 0,
Object,
Camera,
};

static constexpr const char *IN_TRANSFORM_TYPE = "transformType";
static constexpr const char *IN_CONVERT_FROM = "convertForm";
static constexpr const char *IN_CONVERT_TO = "convertTo";
static constexpr const char *IN_VECTOR = "vector";

static constexpr const char *OUT_VECTOR = "vector";

VectorTransformNode(const std::string_view &type);

virtual std::string DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const override;
};
};

#endif
15 changes: 14 additions & 1 deletion core/client/src/c_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ static const auto SEPARATE_JOYSTICK_AXES = true;
#include "pragma/rendering/shader_graph/nodes/object.hpp"
#include "pragma/rendering/shader_graph/nodes/time.hpp"
#include "pragma/rendering/shader_graph/nodes/pbr.hpp"
#include "pragma/rendering/shader_graph/nodes/image_texture.hpp"
#include "pragma/rendering/shader_graph/nodes/texture_coordinate.hpp"
#include "pragma/rendering/shader_graph/nodes/vector_transform.hpp"
#include "pragma/rendering/shader_graph/nodes/geometry.hpp"
#include "pragma/rendering/shader_graph/modules/pbr.hpp"
#include "pragma/rendering/shader_graph/modules/image_texture.hpp"

CEngine::CEngine(int argc, char *argv[])
: Engine(argc, argv), pragma::RenderContext(), m_nearZ(pragma::BaseEnvCameraComponent::DEFAULT_NEAR_Z), //10.0f), //0.1f
Expand Down Expand Up @@ -197,16 +202,23 @@ CEngine::CEngine(int argc, char *argv[])
{
auto regBase = std::make_shared<pragma::shadergraph::NodeRegistry>();
regBase->RegisterNode<pragma::shadergraph::MathNode>("math");
regBase->RegisterNode<pragma::shadergraph::CombineXyzNode>("combine_xyz");
regBase->RegisterNode<pragma::shadergraph::SeparateXyzNode>("separate_xyz");
regBase->RegisterNode<pragma::shadergraph::VectorMathNode>("vector_math");
regBase->RegisterNode<pragma::shadergraph::MixNode>("mix");

auto regScene = std::make_shared<pragma::shadergraph::NodeRegistry>();
regScene->RegisterNode<pragma::rendering::shader_graph::SceneOutputNode>("output");
regScene->RegisterNode<pragma::shadergraph::CombineXyzNode>("combine_xyz");
regScene->RegisterNode<pragma::rendering::shader_graph::CameraNode>("camera");
regScene->RegisterNode<pragma::rendering::shader_graph::FogNode>("fog");
regScene->RegisterNode<pragma::rendering::shader_graph::LightmapNode>("lightmap");
regScene->RegisterNode<pragma::rendering::shader_graph::ObjectNode>("object");
regScene->RegisterNode<pragma::rendering::shader_graph::TimeNode>("time");
regScene->RegisterNode<pragma::rendering::shader_graph::PbrNode>("pbr");
regScene->RegisterNode<pragma::rendering::shader_graph::ImageTextureNode>("image_texture");
regScene->RegisterNode<pragma::rendering::shader_graph::TextureCoordinateNode>("texture_coordinate");
regScene->RegisterNode<pragma::rendering::shader_graph::VectorTransformNode>("vector_transform");
regScene->RegisterNode<pragma::rendering::shader_graph::GeometryNode>("geometry");

auto shaderMat = pragma::rendering::shader_material::get_cache().Load("pbr");
auto node = std::make_shared<pragma::rendering::shader_graph::ShaderMaterialNode>("test", *shaderMat);
Expand All @@ -222,6 +234,7 @@ CEngine::CEngine(int argc, char *argv[])
m_shaderGraphManager->RegisterGraphTypeManager("object", regScene);

m_shaderGraphManager->GetModuleManager().RegisterFactory("pbr", [](prosper::Shader &shader) -> std::unique_ptr<pragma::rendering::ShaderGraphModule> { return std::make_unique<pragma::rendering::shader_graph::PbrModule>(shader); });
m_shaderGraphManager->GetModuleManager().RegisterFactory("image_texture", [](prosper::Shader &shader) -> std::unique_ptr<pragma::rendering::ShaderGraphModule> { return std::make_unique<pragma::rendering::shader_graph::ImageTextureModule>(shader); });
}
}

Expand Down
14 changes: 7 additions & 7 deletions core/client/src/rendering/shader_graph/nodes/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ CameraNode::CameraNode(const std::string_view &type) : Node {type}
std::string CameraNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const
{
std::ostringstream code;
code << instance.GetOutputVarName(OUT_POSITION) << " = u_renderSettings.posCam.xyz;\n";
code << instance.GetOutputVarName(OUT_FOV) << " = u_renderSettings.posCam.w;\n";
code << instance.GetOutputVarName(OUT_NEARZ) << " = u_renderSettings.nearZ;\n";
code << instance.GetOutputVarName(OUT_FARZ) << " = u_renderSettings.farZ;\n";
code << instance.GetOutputVarName(OUT_VIEW_MATRIX) << " = u_camera.V;\n";
code << instance.GetOutputVarName(OUT_PROJECTION_MATRIX) << " = u_camera.P;\n";
code << instance.GetOutputVarName(OUT_VIEW_PROJECTION_MATRIX) << " = u_camera.VP;\n";
code << instance.GetGlslOutputDeclaration(OUT_POSITION) << " = u_renderSettings.posCam.xyz;\n";
code << instance.GetGlslOutputDeclaration(OUT_FOV) << " = u_renderSettings.posCam.w;\n";
code << instance.GetGlslOutputDeclaration(OUT_NEARZ) << " = u_renderSettings.nearZ;\n";
code << instance.GetGlslOutputDeclaration(OUT_FARZ) << " = u_renderSettings.farZ;\n";
code << instance.GetGlslOutputDeclaration(OUT_VIEW_MATRIX) << " = u_camera.V;\n";
code << instance.GetGlslOutputDeclaration(OUT_PROJECTION_MATRIX) << " = u_camera.P;\n";
code << instance.GetGlslOutputDeclaration(OUT_VIEW_PROJECTION_MATRIX) << " = u_camera.VP;\n";
return code.str();
}
8 changes: 4 additions & 4 deletions core/client/src/rendering/shader_graph/nodes/fog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ FogNode::FogNode(const std::string_view &type) : Node {type}
std::string FogNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const
{
std::ostringstream code;
code << instance.GetOutputVarName(OUT_COLOR) << " = u_fog.color.rgb;\n";
code << instance.GetOutputVarName(OUT_START_DISTANCE) << " = u_fog.start;\n";
code << instance.GetOutputVarName(OUT_END_DISTANCE) << " = u_fog.end;\n";
code << instance.GetOutputVarName(OUT_DENSITY) << " = u_fog.density;\n";
code << instance.GetGlslOutputDeclaration(OUT_COLOR) << " = u_fog.color.rgb;\n";
code << instance.GetGlslOutputDeclaration(OUT_START_DISTANCE) << " = u_fog.start;\n";
code << instance.GetGlslOutputDeclaration(OUT_END_DISTANCE) << " = u_fog.end;\n";
code << instance.GetGlslOutputDeclaration(OUT_DENSITY) << " = u_fog.density;\n";
return code.str();
}
35 changes: 35 additions & 0 deletions core/client/src/rendering/shader_graph/nodes/geometry.cpp
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/geometry.hpp"

using namespace pragma::rendering::shader_graph;

GeometryNode::GeometryNode(const std::string_view &type) : Node {type}
{
AddOutput(OUT_POSITION_WS, pragma::shadergraph::SocketType::Vector);
AddOutput(OUT_NORMAL_WS, pragma::shadergraph::SocketType::Vector);
AddOutput(OUT_NORMAL_CS, pragma::shadergraph::SocketType::Vector);
AddOutput(OUT_TANGENT_WS, pragma::shadergraph::SocketType::Vector);

AddModuleDependency("vertex_data");
}

std::string GeometryNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &gn) const
{
std::ostringstream code;
code << gn.GetGlslOutputDeclaration(OUT_POSITION_WS) << " = get_vertex_position_ws();\n";
code << gn.GetGlslOutputDeclaration(OUT_NORMAL_WS) << " = get_vertex_normal();\n";
code << gn.GetGlslOutputDeclaration(OUT_NORMAL_CS) << " = get_vertex_normal_cs();\n";

auto prefix = gn.GetBaseVarName() + "_";
std::string tbn = prefix + "tbn";
code << "mat3 " << tbn << " = get_tbn_matrix();\n";
code << gn.GetGlslOutputDeclaration(OUT_TANGENT_WS) << " = normalize(" << tbn << "[0]);\n";
return code.str();
}
4 changes: 2 additions & 2 deletions core/client/src/rendering/shader_graph/nodes/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ObjectNode::ObjectNode(const std::string_view &type) : Node {type}
std::string ObjectNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const
{
std::ostringstream code;
code << instance.GetOutputVarName(OUT_MODEL_MATRIX) << " = u_instance.M;\n";
code << instance.GetOutputVarName(OUT_COLOR) << " = u_instance.color;\n";
code << instance.GetGlslOutputDeclaration(OUT_MODEL_MATRIX) << " = u_instance.M;\n";
code << instance.GetGlslOutputDeclaration(OUT_COLOR) << " = u_instance.color;\n";
return code.str();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* 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/texture_coordinate.hpp"

using namespace pragma::rendering::shader_graph;

TextureCoordinateNode::TextureCoordinateNode(const std::string_view &type) : Node {type}
{
AddOutput(OUT_UV, pragma::shadergraph::SocketType::Vector);

AddModuleDependency("uv_data");
}

std::string TextureCoordinateNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const
{
std::ostringstream code;
code << instance.GetGlslOutputDeclaration(OUT_UV) << " = vec3(get_uv_coordinates(), 0.0);\n";
return code.str();
}
8 changes: 4 additions & 4 deletions core/client/src/rendering/shader_graph/nodes/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ TimeNode::TimeNode(const std::string_view &type) : Node {type}
std::string TimeNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const
{
std::ostringstream code;
code << instance.GetOutputVarName(OUT_TIME) << " = u_time.time;\n";
code << instance.GetOutputVarName(OUT_DELTA_TIME) << " = u_time.deltaTime;\n";
code << instance.GetOutputVarName(OUT_REAL_TIME) << " = u_time.realTime;\n";
code << instance.GetOutputVarName(OUT_DELTA_REAL_TIME) << " = u_time.deltaRealTime;\n";
code << instance.GetGlslOutputDeclaration(OUT_TIME) << " = u_time.time;\n";
code << instance.GetGlslOutputDeclaration(OUT_DELTA_TIME) << " = u_time.deltaTime;\n";
code << instance.GetGlslOutputDeclaration(OUT_REAL_TIME) << " = u_time.realTime;\n";
code << instance.GetGlslOutputDeclaration(OUT_DELTA_REAL_TIME) << " = u_time.deltaRealTime;\n";
return code.str();
}
96 changes: 96 additions & 0 deletions core/client/src/rendering/shader_graph/nodes/vector_transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* 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/vector_transform.hpp"

using namespace pragma::rendering::shader_graph;

VectorTransformNode::VectorTransformNode(const std::string_view &type) : Node {type}
{
AddSocketEnum<Type>(IN_TRANSFORM_TYPE, Type::Vector);
AddSocketEnum<Space>(IN_CONVERT_FROM, Space::World);
AddSocketEnum<Space>(IN_CONVERT_TO, Space::Object);
AddInput(IN_VECTOR, pragma::shadergraph::SocketType::Vector, Vector3 {0.f, 0.f, 0.f});
AddOutput(OUT_VECTOR, pragma::shadergraph::SocketType::Vector);

AddModuleDependency("camera");
AddModuleDependency("entity");
}

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

auto vector = gn.GetInputNameOrValue(IN_VECTOR);

auto type = *gn.GetConstantInputValue<Type>(IN_TRANSFORM_TYPE);
auto convertFrom = *gn.GetConstantInputValue<Space>(IN_CONVERT_FROM);
auto convertTo = *gn.GetConstantInputValue<Space>(IN_CONVERT_TO);

if(convertFrom == convertTo) {
code << gn.GetGlslOutputDeclaration(OUT_VECTOR) << " = " << vector << ";\n";
return code.str();
}

auto prefix = gn.GetBaseVarName() + "_";
std::string matName = prefix + "mat";
code << "mat4 " << matName << " = ";
switch(convertFrom) {
case Space::Object:
switch(convertTo) {
case Space::World:
code << "get_model_matrix()";
break;
case Space::Camera:
code << "get_view_matrix() *get_model_matrix()";
break;
}
break;
case Space::Camera:
switch(convertTo) {
case Space::World:
code << "inverse(get_view_matrix())";
break;
case Space::Object:
code << "inverse(get_model_matrix()) *inverse(get_view_matrix())";
break;
}
break;
case Space::World:
switch(convertTo) {
case Space::Object:
code << "inverse(get_model_matrix())";
break;
case Space::Camera:
code << "get_view_matrix()";
break;
}
break;
}
code << ";\n";

code << gn.GetGlslOutputDeclaration(OUT_VECTOR) << " = ";
switch(type) {
case Type::Vector:
// Vectors are transformed with the matrix, ignoring translation.
code << "(" << matName << " * vec4(" << vector << ", 0.0)).xyz";
break;

case Type::Normal:
// Normals require the inverse transpose of the matrix.
code << "(transpose(inverse(mat3(" << matName << "))) * " << vector << ")";
break;

case Type::Point:
// Points are fully transformed, including translation.
code << "(" << matName << " * vec4(" << vector << ", 1.0)).xyz";
break;
}
code << ";\n";
return code.str();
}

0 comments on commit aa9aa5a

Please sign in to comment.