Skip to content

Commit

Permalink
feat(shader_graph): image textures are now in global descriptor set; …
Browse files Browse the repository at this point in the history
…add alpha mode
  • Loading branch information
Silverlan committed Dec 24, 2024
1 parent f5c3468 commit 646b6d2
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 36 deletions.
10 changes: 6 additions & 4 deletions core/client/include/pragma/rendering/shader_graph/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ namespace prosper {
namespace pragma {
class CSceneComponent;
class CRasterizationRendererComponent;
class ShaderGraph;
};

class CModelSubMesh;
namespace pragma::rendering {
class ShaderProcessor;
class DLLCLIENT ShaderGraphModule {
public:
ShaderGraphModule(prosper::Shader &shader) : m_shader(shader) {}
ShaderGraphModule(ShaderGraph &shader) : m_shader(shader) {}
virtual ~ShaderGraphModule() {}
virtual void InitializeShaderResources() {}
virtual void InitializeGfxPipelineDescriptorSets() = 0;
virtual void GetShaderPreprocessorDefinitions(std::unordered_map<std::string, std::string> &outDefinitions, std::string &outPrefixCode) {}
virtual void UpdateRenderFlags(CModelSubMesh &mesh, ShaderGameWorld::SceneFlags &inOutFlags) {}
Expand All @@ -37,16 +39,16 @@ namespace pragma::rendering {
virtual void RecordBindMaterial(rendering::ShaderProcessor &shaderProcessor, CMaterial &mat) const {}
void SetNodes(std::vector<pragma::shadergraph::GraphNode *> &&nodes) { m_nodes = std::move(nodes); }
protected:
prosper::Shader &m_shader;
ShaderGraph &m_shader;
std::vector<pragma::shadergraph::GraphNode *> m_nodes;
};

class DLLCLIENT ShaderGraphModuleManager {
public:
using Factory = std::function<std::unique_ptr<ShaderGraphModule>(prosper::Shader &shader)>;
using Factory = std::function<std::unique_ptr<ShaderGraphModule>(ShaderGraph &shader)>;
ShaderGraphModuleManager() {}
void RegisterFactory(const std::string &name, const Factory &factory);
std::unique_ptr<ShaderGraphModule> CreateModule(const std::string &name, prosper::Shader &shader, std::vector<pragma::shadergraph::GraphNode *> &&nodes) const;
std::unique_ptr<ShaderGraphModule> CreateModule(const std::string &name, ShaderGraph &shader, std::vector<pragma::shadergraph::GraphNode *> &&nodes) const;
const std::unordered_map<std::string, Factory> &GetFactories() const { return m_factories; }
private:
std::unordered_map<std::string, Factory> m_factories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace pragma::rendering::shader_graph {

Count
};
ImageTextureModule(prosper::Shader &shader);
ImageTextureModule(ShaderGraph &shader);
virtual ~ImageTextureModule() override;
virtual void InitializeGfxPipelineDescriptorSets() override;
virtual void RecordBindScene(rendering::ShaderProcessor &shaderProcessor, const pragma::CSceneComponent &scene, const pragma::CRasterizationRendererComponent &renderer, ShaderGameWorld::SceneFlags &inOutSceneFlags) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ namespace pragma::rendering {
namespace pragma::rendering::shader_graph {
class DLLCLIENT InputDataModule : public pragma::rendering::ShaderGraphModule {
public:
InputDataModule(prosper::Shader &shader);
InputDataModule(ShaderGraph &shader);
virtual ~InputDataModule() override;
virtual void InitializeShaderResources() override;
virtual void InitializeGfxPipelineDescriptorSets() override;
virtual void GetShaderPreprocessorDefinitions(std::unordered_map<std::string, std::string> &outDefinitions, std::string &outPrefixCode) override;
virtual void RecordBindScene(rendering::ShaderProcessor &shaderProcessor, const pragma::CSceneComponent &scene, const pragma::CRasterizationRendererComponent &renderer, ShaderGameWorld::SceneFlags &inOutSceneFlags) const override;
private:
prosper::DescriptorSetInfo m_globalInputDataDsInfo;

std::unique_ptr<pragma::shadergraph::Graph> m_resolvedGraph;

std::shared_ptr<prosper::IDescriptorSetGroup> m_globalInputDsg;
std::vector<const pragma::shadergraph::GraphNode *> m_imageTextureNodes;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace pragma::rendering::shader_graph {

Count
};
PbrModule(prosper::Shader &shader);
PbrModule(ShaderGraph &shader);
virtual ~PbrModule() override;
virtual void InitializeGfxPipelineDescriptorSets() override;
virtual void RecordBindScene(rendering::ShaderProcessor &shaderProcessor, const pragma::CSceneComponent &scene, const pragma::CRasterizationRendererComponent &renderer, ShaderGameWorld::SceneFlags &inOutSceneFlags) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace pragma::rendering::shader_graph {
static constexpr const char *OUT_ALPHA = "alpha";

ImageTextureNode(const std::string_view &type);
std::string GetTextureVariableName(const pragma::shadergraph::GraphNode &gn) const;

virtual std::string DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const override;
virtual std::string DoEvaluateResourceDeclarations(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace pragma::rendering::shader_graph {
static constexpr const char *IN_COLOR = "color";
static constexpr const char *IN_ALPHA = "alpha";
static constexpr const char *IN_BLOOM_COLOR = "bloomColor";
static constexpr const char *CONST_ALPHA_MODE = "alphaMode";
// TODO: Only allow one of these!
SceneOutputNode(const std::string_view &type);

Expand Down
2 changes: 1 addition & 1 deletion core/client/src/rendering/shader_graph/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using namespace pragma::rendering;

void ShaderGraphModuleManager::RegisterFactory(const std::string &name, const Factory &factory) { m_factories[name] = factory; }
std::unique_ptr<ShaderGraphModule> ShaderGraphModuleManager::CreateModule(const std::string &name, prosper::Shader &shader, std::vector<pragma::shadergraph::GraphNode *> &&nodes) const
std::unique_ptr<ShaderGraphModule> ShaderGraphModuleManager::CreateModule(const std::string &name, ShaderGraph &shader, std::vector<pragma::shadergraph::GraphNode *> &&nodes) const
{
auto it = m_factories.find(name);
if(it == m_factories.end())
Expand Down
14 changes: 7 additions & 7 deletions core/client/src/rendering/shader_graph/modules/image_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace pragma::rendering::shader_graph;
extern DLLCLIENT CEngine *c_engine;
extern DLLCLIENT ClientState *client;
#pragma optimize("", off)
ImageTextureModule::ImageTextureModule(prosper::Shader &shader) : pragma::rendering::ShaderGraphModule {shader}
ImageTextureModule::ImageTextureModule(ShaderGraph &shader) : pragma::rendering::ShaderGraphModule {shader}
{
//Global settings +textures?
/* prosper::PrDescriptorSetBindingFlags::Cubemap */
Expand All @@ -31,7 +31,7 @@ ImageTextureModule::~ImageTextureModule() {}
void ImageTextureModule::InitializeGfxPipelineDescriptorSets()
{
// TODO: Move this somewhere else
std::vector<prosper::DescriptorSetInfo::Binding> bindings;
/*std::vector<prosper::DescriptorSetInfo::Binding> bindings;
bindings.reserve(m_nodes.size());
for(auto *node : m_nodes) {
auto texName = node->GetBaseVarName() + "_tex";
Expand All @@ -42,13 +42,13 @@ void ImageTextureModule::InitializeGfxPipelineDescriptorSets()
m_descSetInfo = {
"TEST",
bindings,
};
};*/

//

m_shader.AddDescriptorSetGroup(m_descSetInfo);
//m_shader.AddDescriptorSetGroup(m_descSetInfo);

auto &context = c_engine->GetRenderContext();
/*auto &context = c_engine->GetRenderContext();
auto dsg = context.CreateDescriptorSetGroup(m_descSetInfo);
auto &ds = *dsg->GetDescriptorSet(0);
auto &texManager = static_cast<msys::CMaterialManager &>(client->GetMaterialManager()).GetTextureManager();
Expand All @@ -65,9 +65,9 @@ void ImageTextureModule::InitializeGfxPipelineDescriptorSets()
ds.SetBindingTexture(*prosperTex, bindingIdx++);
}
m_dsg = dsg;
m_dsg = dsg;*/
}
void ImageTextureModule::RecordBindScene(rendering::ShaderProcessor &shaderProcessor, const pragma::CSceneComponent &scene, const pragma::CRasterizationRendererComponent &renderer, ShaderGameWorld::SceneFlags &inOutSceneFlags) const
{
shaderProcessor.GetCommandBuffer().RecordBindDescriptorSets(prosper::PipelineBindPoint::Graphics, shaderProcessor.GetCurrentPipelineLayout(), m_descSetInfo.setIndex, *m_dsg->GetDescriptorSet());
//shaderProcessor.GetCommandBuffer().RecordBindDescriptorSets(prosper::PipelineBindPoint::Graphics, shaderProcessor.GetCurrentPipelineLayout(), m_descSetInfo.setIndex, *m_dsg->GetDescriptorSet());
}
67 changes: 59 additions & 8 deletions core/client/src/rendering/shader_graph/modules/input_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,56 @@

#include "stdafx_client.h"
#include "pragma/rendering/shader_graph/modules/input_data.hpp"
#include "pragma/rendering/shader_graph/nodes/image_texture.hpp"
#include "pragma/rendering/render_processor.hpp"
#include "pragma/rendering/shader_material/shader_material.hpp"
#include "pragma/rendering/global_shader_input_manager.hpp"
#include "pragma/rendering/shader_graph/manager.hpp"
#include "pragma/rendering/shaders/world/c_shader_graph.hpp"
#include <pragma/util/global_string_table.hpp>
#include <cmaterial_manager2.hpp>
#include <texturemanager/texture_manager2.hpp>
#include <texturemanager/texture.h>
#include <buffers/prosper_buffer.hpp>
#include <buffers/prosper_buffer_create_info.hpp>
#include <prosper_command_buffer.hpp>

using namespace pragma::rendering::shader_graph;

extern DLLCLIENT CEngine *c_engine;
extern DLLCLIENT ClientState *client;
extern DLLCLIENT CGame *c_game;

InputDataModule::InputDataModule(prosper::Shader &shader) : pragma::rendering::ShaderGraphModule {shader}
InputDataModule::InputDataModule(ShaderGraph &shader) : pragma::rendering::ShaderGraphModule {shader} {}
InputDataModule::~InputDataModule() {}

void InputDataModule::InitializeShaderResources()
{
m_globalInputDataDsInfo = {
"SHADER_GRAPH",
{prosper::DescriptorSetInfo::Binding {"GLOBAL_INPUT_DATA", prosper::DescriptorType::UniformBuffer, prosper::ShaderStageFlags::FragmentBit | prosper::ShaderStageFlags::VertexBit | prosper::ShaderStageFlags::GeometryBit}},
};
std::vector<prosper::DescriptorSetInfo::Binding> bindings;

auto *graph = m_shader.GetGraph();
if(!graph)
return;
bindings.push_back({"GLOBAL_INPUT_DATA", prosper::DescriptorType::UniformBuffer, prosper::ShaderStageFlags::FragmentBit | prosper::ShaderStageFlags::VertexBit | prosper::ShaderStageFlags::GeometryBit});

// TODO
m_resolvedGraph = std::make_unique<pragma::shadergraph::Graph>(*graph);
m_resolvedGraph->Resolve();
for(auto &graphNode : m_resolvedGraph->GetNodes()) {
auto *node = dynamic_cast<const ImageTextureNode *>(&graphNode->node);
if(!node)
continue;
auto texVarName = node->GetTextureVariableName(*graphNode);
ustring::to_upper(texVarName);
bindings.push_back({pragma::register_global_string(texVarName), prosper::DescriptorType::CombinedImageSampler, prosper::ShaderStageFlags::FragmentBit});
if(m_imageTextureNodes.size() == m_imageTextureNodes.capacity())
m_imageTextureNodes.reserve(m_imageTextureNodes.size() * 2 + 10);
m_imageTextureNodes.push_back(graphNode.get());
}

m_globalInputDataDsInfo = {"SHADER_GRAPH", bindings};
ShaderGraphModule::InitializeShaderResources();
}
InputDataModule::~InputDataModule() {}

void InputDataModule::GetShaderPreprocessorDefinitions(std::unordered_map<std::string, std::string> &outDefinitions, std::string &outPrefixCode)
{
Expand All @@ -42,6 +71,15 @@ void InputDataModule::GetShaderPreprocessorDefinitions(std::unordered_map<std::s
code << "\t" << pragma::shadergraph::to_glsl_type(prop.parameter.type) << " " << prop.parameter.name << ";\n";
}
code << "} u_globalInputData;\n";

for(auto *graphNode : m_imageTextureNodes) {
auto *node = dynamic_cast<const ImageTextureNode *>(&graphNode->node);
auto texVarName = node->GetTextureVariableName(*graphNode);
auto texVarNameUpper = texVarName;
ustring::to_upper(texVarNameUpper);
code << "layout(LAYOUT_ID(SHADER_GRAPH, " << texVarNameUpper << ")) uniform sampler2D " << texVarName << ";\n";
}

outPrefixCode += code.str();
}

Expand All @@ -61,14 +99,27 @@ void InputDataModule::InitializeGfxPipelineDescriptorSets()

// TODO: One per shader
m_globalInputDsg = context.CreateDescriptorSetGroup(m_globalInputDataDsInfo);
auto &dummyTex = context.GetDummyTexture();
auto &dummyCubemapTex = context.GetDummyCubemapTexture();
auto &ds = *m_globalInputDsg->GetDescriptorSet(0);
constexpr uint32_t BINDING_IDX = 0;
auto buf = inputDataManager.GetBuffer();
if(!buf)
buf = c_engine->GetRenderContext().GetDummyBuffer();
ds.SetBindingUniformBuffer(*buf, BINDING_IDX);

// Image texture nodes
auto &texManager = static_cast<msys::CMaterialManager &>(client->GetMaterialManager()).GetTextureManager();
uint32_t bindingIdx = 1; // Binding 0 is global input data
for(auto *node : m_imageTextureNodes) {
std::string fileName;
node->GetInputValue<std::string>(pragma::rendering::shader_graph::ImageTextureNode::IN_FILENAME, fileName);
auto tex = texManager.LoadAsset(fileName);
std::shared_ptr<prosper::Texture> prosperTex;
if(tex)
prosperTex = tex->GetVkTexture();
if(!prosperTex)
prosperTex = context.GetDummyTexture();
ds.SetBindingTexture(*prosperTex, bindingIdx++);
}
}
void InputDataModule::RecordBindScene(rendering::ShaderProcessor &shaderProcessor, const pragma::CSceneComponent &scene, const pragma::CRasterizationRendererComponent &renderer, ShaderGameWorld::SceneFlags &inOutSceneFlags) const
{
Expand Down
4 changes: 3 additions & 1 deletion core/client/src/rendering/shader_graph/modules/pbr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "stdafx_client.h"
#include "pragma/rendering/shaders/world/c_shader_graph.hpp"
#include "pragma/rendering/shader_graph/modules/pbr.hpp"
#include "pragma/rendering/render_processor.hpp"
#include "pragma/entities/environment/c_env_reflection_probe.hpp"
Expand All @@ -15,9 +16,10 @@
using namespace pragma::rendering::shader_graph;

extern DLLCLIENT CEngine *c_engine;
#pragma optimize("", off)
std::shared_ptr<prosper::IDescriptorSetGroup> PbrModule::g_defaultPbrDsg = {};
size_t PbrModule::g_instanceCount = 0;
PbrModule::PbrModule(prosper::Shader &shader) : pragma::rendering::ShaderGraphModule {shader}
PbrModule::PbrModule(ShaderGraph &shader) : pragma::rendering::ShaderGraphModule {shader}
{
m_pbrDescSetInfo = {
"PBR",
Expand Down
18 changes: 11 additions & 7 deletions core/client/src/rendering/shader_graph/nodes/image_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ ImageTextureNode::ImageTextureNode(const std::string_view &type) : Node {type}
AddModuleDependency("image_texture");
}

std::string ImageTextureNode::GetTextureVariableName(const pragma::shadergraph::GraphNode &gn) const
{
auto prefix = gn.GetBaseVarName() + "_";
return prefix + "tex";
}

std::string ImageTextureNode::DoEvaluateResourceDeclarations(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &gn) const
{
std::ostringstream code;
auto prefix = gn.GetBaseVarName() + "_";
std::string texName = prefix + "tex";
auto upperTexName = texName;
ustring::to_upper(upperTexName);
code << "layout(LAYOUT_ID(TEST, " << upperTexName << ")) uniform sampler2D " << texName << ";\n";
//auto texName = GetTextureVariableName(gn);
//auto upperTexName = texName;
//ustring::to_upper(upperTexName);
//code << "layout(LAYOUT_ID(TEST, " << upperTexName << ")) uniform sampler2D " << texName << ";\n";
return code.str();
}

Expand All @@ -42,8 +47,7 @@ std::string ImageTextureNode::DoEvaluate(const pragma::shadergraph::Graph &graph
uv = "vec3(get_vertex_uv(), 0.0)";

auto prefix = gn.GetBaseVarName() + "_";
std::string texName = prefix + "tex";
code << "vec4 " << prefix << "texCol = texture(" << texName << ", " << uv << ".xy);\n";
code << "vec4 " << prefix << "texCol = texture(" << GetTextureVariableName(gn) << ", " << uv << ".xy);\n";

code << gn.GetGlslOutputDeclaration(OUT_COLOR) << " = ";
code << prefix << "texCol.rgb;\n";
Expand Down
17 changes: 13 additions & 4 deletions core/client/src/rendering/shader_graph/nodes/scene_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ SceneOutputNode::SceneOutputNode(const std::string_view &type) : Node {type}
AddInput(IN_COLOR, pragma::shadergraph::DataType::Color, Vector3 {1.f, 1.f, 1.f});
AddInput(IN_ALPHA, pragma::shadergraph::DataType::Float, 1.f);
AddInput(IN_BLOOM_COLOR, pragma::shadergraph::DataType::Color, Vector3 {0.f, 0.f, 0.f});

AddSocketEnum<AlphaMode>(CONST_ALPHA_MODE, AlphaMode::Opaque);
}

std::string SceneOutputNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &instance) const
std::string SceneOutputNode::DoEvaluate(const pragma::shadergraph::Graph &graph, const pragma::shadergraph::GraphNode &gn) const
{
std::ostringstream code;
code << "fs_color = vec4(" << GetInputNameOrValue(instance, IN_COLOR) << ", " << GetInputNameOrValue(instance, IN_ALPHA) << ");\n";
code << "fs_brightColor = vec4(" << GetInputNameOrValue(instance, IN_BLOOM_COLOR) << ", 1.0);\n";
code << "fs_color = vec4(" << GetInputNameOrValue(gn, IN_COLOR) << ", " << GetInputNameOrValue(gn, IN_ALPHA) << ");\n";
code << "fs_brightColor = vec4(" << GetInputNameOrValue(gn, IN_BLOOM_COLOR) << ", 1.0);\n";

auto alphaMode = *gn.GetConstantInputValue<AlphaMode>(CONST_ALPHA_MODE);
if(alphaMode == AlphaMode::Mask) {
// TODO: Use alpha cutoff value
code << "if(fs_color.a < 0.5)\n";
code << "\tdiscard;\n";
}

return code.str();
//
}
31 changes: 30 additions & 1 deletion core/client/src/rendering/shaders/world/c_shader_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "pragma/rendering/shader_graph/module.hpp"
#include "pragma/rendering/shader_graph/nodes/shader_material.hpp"
#include "pragma/rendering/shader_graph/nodes/input_parameter.hpp"
#include "pragma/rendering/shader_graph/nodes/scene_output.hpp"
#include "pragma/model/vk_mesh.h"
#include "pragma/model/c_modelmesh.h"
#include <shader/prosper_pipeline_create_info.hpp>
Expand Down Expand Up @@ -115,6 +116,9 @@ void ShaderGraph::InitializeShaderResources()
}
}

for(auto &mod : m_modules)
mod->InitializeShaderResources();

ShaderGameWorldLightingPass::InitializeShaderResources();
}

Expand All @@ -138,7 +142,32 @@ void ShaderGraph::InitializeMaterialData(const CMaterial &mat, const rendering::

std::shared_ptr<prosper::IDescriptorSetGroup> ShaderGraph::InitializeMaterialDescriptorSet(CMaterial &mat, const prosper::DescriptorSetInfo &descSetInfo) { return ShaderGameWorldLightingPass::InitializeMaterialDescriptorSet(mat, descSetInfo); }
void ShaderGraph::OnPipelinesInitialized() { ShaderGameWorldLightingPass::OnPipelinesInitialized(); }
void ShaderGraph::InitializeGfxPipeline(prosper::GraphicsPipelineCreateInfo &pipelineInfo, uint32_t pipelineIdx) { ShaderGameWorldLightingPass::InitializeGfxPipeline(pipelineInfo, pipelineIdx); }
void ShaderGraph::InitializeGfxPipeline(prosper::GraphicsPipelineCreateInfo &pipelineInfo, uint32_t pipelineIdx)
{
ShaderGameWorldLightingPass::InitializeGfxPipeline(pipelineInfo, pipelineIdx);

auto *graph = GetGraph();
if(graph) {
for(auto &node : graph->GetNodes()) {
auto *outputNode = dynamic_cast<const pragma::rendering::shader_graph::SceneOutputNode *>(&node->node);
if(!outputNode)
continue;
AlphaMode alphaMode;
if(node->GetInputValue(pragma::rendering::shader_graph::SceneOutputNode::CONST_ALPHA_MODE, alphaMode)) {
switch(alphaMode) {
case AlphaMode::Blend:
SetGenericAlphaColorBlendAttachmentProperties(pipelineInfo);
break;
case AlphaMode::Mask:
SetGenericAlphaColorBlendAttachmentProperties(pipelineInfo);
pipelineInfo.ToggleDepthWrites(true);
break;
}
}
break;
}
}
}

void ShaderGraph::RecordBindScene(rendering::ShaderProcessor &shaderProcessor, const pragma::CSceneComponent &scene, const pragma::CRasterizationRendererComponent &renderer, prosper::IDescriptorSet &dsScene, prosper::IDescriptorSet &dsRenderer, prosper::IDescriptorSet &dsRenderSettings,
prosper::IDescriptorSet &dsShadows, const Vector4 &drawOrigin, ShaderGameWorld::SceneFlags &inOutSceneFlags) const
Expand Down

0 comments on commit 646b6d2

Please sign in to comment.