Skip to content

Commit

Permalink
Add support for push constants in GLSL (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix authored Jan 21, 2025
1 parent 909d9b6 commit a39f15f
Show file tree
Hide file tree
Showing 17 changed files with 610 additions and 417 deletions.
10 changes: 10 additions & 0 deletions bin/resources/Shader.glsl.binding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bindings": [
{
"set": 0,
"binding": 0,
"glsl_binding": 0
}
],
"push_constant_binding": 1
}
13 changes: 12 additions & 1 deletion bin/resources/Shader.nzsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ import GetColor as Color from Color;
import * from DataStruct;
import * from OutputStruct;

[layout(std140)]
struct PushConstants
{
color: vec4[f32]
}

external ExternalResources
{
constants: push_constant[PushConstants]
}

[entry(frag)]
fn main() -> Output
{
let data: Data;
data.color = Color();

let output: Output;
output.color = GetColorFromData(data);
output.color = GetColorFromData(data) * ExternalResources.constants.color;

return output;
}
13 changes: 7 additions & 6 deletions include/CNZSL/GlslWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern "C"
#endif

typedef struct nzslGlslWriter nzslGlslWriter;
typedef struct nzslGlslBindingMapping nzslGlslBindingMapping;
typedef struct nzslGlslWriterParameters nzslGlslWriterParameters;
typedef struct nzslGlslOutput nzslGlslOutput;

typedef struct
Expand All @@ -34,16 +34,17 @@ typedef struct
int allowDrawParametersUniformsFallback;
} nzslGlslWriterEnvironment;

CNZSL_API nzslGlslBindingMapping* nzslGlslBindingMappingCreate(void);
CNZSL_API void nzslGlslBindingMappingDestroy(nzslGlslBindingMapping* bindingMappingPtr);
CNZSL_API nzslGlslWriterParameters* nzslGlslWriterParametersCreate(void);
CNZSL_API void nzslGlslWriterParametersDestroy(nzslGlslWriterParameters* parameterPtr);

CNZSL_API void nzslGlslBindingMappingSetBinding(nzslGlslBindingMapping* bindingMappingPtr, uint32_t setIndex, uint32_t bindingIndex, unsigned int glBinding);
CNZSL_API void nzslGlslWriterParametersSetBindingMapping(nzslGlslWriterParameters* parameterPtr, uint32_t setIndex, uint32_t bindingIndex, unsigned int glBinding);
CNZSL_API void nzslGlslWriterParametersSetPushConstantBinding(nzslGlslWriterParameters* parameterPtr, unsigned int glBinding);
CNZSL_API void nzslGlslWriterParametersSetShaderStage(nzslGlslWriterParameters* parameterPtr, nzslShaderStageType stage);

CNZSL_API nzslGlslWriter* nzslGlslWriterCreate(void);
CNZSL_API void nzslGlslWriterDestroy(nzslGlslWriter* writerPtr);

CNZSL_API nzslGlslOutput* nzslGlslWriterGenerate(nzslGlslWriter* writerPtr, const nzslModule* modulePtr, const nzslGlslBindingMapping* bindingMapping, const nzslWriterStates* statesPtr);
CNZSL_API nzslGlslOutput* nzslGlslWriterGenerateStage(nzslShaderStageType stage, nzslGlslWriter* writerPtr, const nzslModule* modulePtr, const nzslGlslBindingMapping* bindingMapping, const nzslWriterStates* statesPtr);
CNZSL_API nzslGlslOutput* nzslGlslWriterGenerate(nzslGlslWriter* writerPtr, const nzslModule* modulePtr, const nzslGlslWriterParameters* parameterPtr, const nzslWriterStates* statesPtr);

/**
** Gets the last error message set by the last operation to this writer
Expand Down
12 changes: 9 additions & 3 deletions include/NZSL/GlslWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ namespace nzsl
class NZSL_API GlslWriter : public ShaderWriter, public Ast::ExpressionVisitorExcept, public Ast::StatementVisitorExcept
{
public:
using BindingMapping = std::unordered_map<std::uint64_t /* set | binding */, unsigned int /*glBinding*/>;
using ExtSupportCallback = std::function<bool(std::string_view name)>;
struct Environment;
struct Output;
struct Parameters;

inline GlslWriter();
GlslWriter(const GlslWriter&) = delete;
GlslWriter(GlslWriter&&) = delete;
~GlslWriter() = default;

inline Output Generate(const Ast::Module& module, const BindingMapping& bindingMapping = {}, const States& states = {});
Output Generate(std::optional<ShaderStageType> shaderStage, const Ast::Module& module, const BindingMapping& bindingMapping = {}, const States& states = {});
Output Generate(const Ast::Module& module, const Parameters& parameters = {}, const States& states = {});

void SetEnv(Environment environment);

Expand All @@ -49,6 +48,13 @@ namespace nzsl
bool allowDrawParametersUniformsFallback = false;
};

struct Parameters
{
std::optional<unsigned int> pushConstantBinding;
std::optional<ShaderStageType> shaderStage;
std::unordered_map<std::uint64_t /* set | binding */, unsigned int /*glBinding*/> bindingMapping;
};

struct Output
{
std::string code;
Expand Down
6 changes: 0 additions & 6 deletions include/NZSL/GlslWriter.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@
// This file is part of the "Nazara Shading Language" project
// For conditions of distribution and use, see copyright notice in Config.hpp


namespace nzsl
{
inline GlslWriter::GlslWriter() :
m_currentState(nullptr)
{
}

inline auto GlslWriter::Generate(const Ast::Module& shader, const BindingMapping& bindingMapping, const States& states) -> Output
{
return Generate(std::nullopt, shader, bindingMapping, states);
}
}
65 changes: 25 additions & 40 deletions src/CNZSL/GlslWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp

#include <CNZSL/GlslWriter.h>
#include <CNZSL/Structs/GlslBindingMapping.hpp>
#include <CNZSL/Structs/GlslWriterParameters.hpp>
#include <CNZSL/Structs/GlslOutput.hpp>
#include <CNZSL/Structs/GlslWriter.hpp>
#include <CNZSL/Structs/Module.hpp>
Expand All @@ -14,76 +14,61 @@

extern "C"
{
CNZSL_API nzslGlslBindingMapping* nzslGlslBindingMappingCreate(void)
CNZSL_API nzslGlslWriterParameters* nzslGlslWriterParametersCreate(void)
{
return new nzslGlslBindingMapping;
return new nzslGlslWriterParameters;
}

CNZSL_API void nzslGlslBindingMappingDestroy(nzslGlslBindingMapping* bindingMappingPtr)
CNZSL_API void nzslGlslWriterParametersDestroy(nzslGlslWriterParameters* parameterPtr)
{
delete bindingMappingPtr;
delete parameterPtr;
}

CNZSL_API void nzslGlslBindingMappingSetBinding(nzslGlslBindingMapping* bindingMappingPtr, uint32_t setIndex, uint32_t bindingIndex, unsigned int glBinding)
CNZSL_API void nzslGlslWriterParametersSetBindingMapping(nzslGlslWriterParameters* parameterPtr, uint32_t setIndex, uint32_t bindingIndex, unsigned int glBinding)
{
uint64_t setBinding = setIndex;
setBinding <<= 32;
setBinding |= bindingIndex;

bindingMappingPtr->mappings[setBinding] = glBinding;
parameterPtr->parameters.bindingMapping[setBinding] = glBinding;
}

CNZSL_API nzslGlslWriter* nzslGlslWriterCreate(void)
CNZSL_API void nzslGlslWriterParametersSetPushConstantBinding(nzslGlslWriterParameters* parameterPtr, unsigned int glBinding)
{
return new nzslGlslWriter;
parameterPtr->parameters.pushConstantBinding = glBinding;
}

CNZSL_API void nzslGlslWriterDestroy(nzslGlslWriter* writerPtr)
CNZSL_API void nzslGlslWriterParametersSetShaderStage(nzslGlslWriterParameters* parameterPtr, nzslShaderStageType stage)
{
delete writerPtr;
constexpr std::array s_shaderStages = {
nzsl::ShaderStageType::Compute, // NZSL_STAGE_COMPUTE
nzsl::ShaderStageType::Fragment, // NZSL_STAGE_FRAGMENT
nzsl::ShaderStageType::Vertex // NZSL_STAGE_VERTEX
};

parameterPtr->parameters.shaderStage = s_shaderStages[stage];
}

CNZSL_API nzslGlslOutput* nzslGlslWriterGenerate(nzslGlslWriter* writerPtr, const nzslModule* modulePtr, const nzslGlslBindingMapping* bindingMapping, const nzslWriterStates* statesPtr)
CNZSL_API nzslGlslWriter* nzslGlslWriterCreate(void)
{
try
{
nzsl::GlslWriter::States states;
if (statesPtr)
states = static_cast<const nzsl::GlslWriter::States&>(*statesPtr);

std::unique_ptr<nzslGlslOutput> output = std::make_unique<nzslGlslOutput>();
static_cast<nzsl::GlslWriter::Output&>(*output) = writerPtr->writer.Generate(*modulePtr->module, bindingMapping->mappings, states);

return output.release();
}
catch (std::exception& e)
{
writerPtr->lastError = fmt::format("nzslGlslWriterGenerate failed: {}", e.what());
return nullptr;
}
catch (...)
{
writerPtr->lastError = "nzslGlslWriterGenerate failed with unknown error";
return nullptr;
}
return new nzslGlslWriter;
}

CNZSL_API nzslGlslOutput* nzslGlslWriterGenerateStage(nzslShaderStageType stage, nzslGlslWriter* writerPtr, const nzslModule* modulePtr, const nzslGlslBindingMapping* bindingMapping, const nzslWriterStates* statesPtr)
CNZSL_API void nzslGlslWriterDestroy(nzslGlslWriter* writerPtr)
{
constexpr std::array s_shaderStages = {
nzsl::ShaderStageType::Compute, // NZSL_STAGE_COMPUTE
nzsl::ShaderStageType::Fragment, // NZSL_STAGE_FRAGMENT
nzsl::ShaderStageType::Vertex // NZSL_STAGE_VERTEX
};
delete writerPtr;
}

CNZSL_API nzslGlslOutput* nzslGlslWriterGenerate(nzslGlslWriter* writerPtr, const nzslModule* modulePtr, const nzslGlslWriterParameters* parameters, const nzslWriterStates* statesPtr)
{
try
{
nzsl::GlslWriter::States states;
if (statesPtr)
states = static_cast<const nzsl::GlslWriter::States&>(*statesPtr);

std::unique_ptr<nzslGlslOutput> output = std::make_unique<nzslGlslOutput>();
static_cast<nzsl::GlslWriter::Output&>(*output) = writerPtr->writer.Generate(s_shaderStages[stage], *modulePtr->module, bindingMapping->mappings, states);
static_cast<nzsl::GlslWriter::Output&>(*output) = writerPtr->writer.Generate(*modulePtr->module, parameters->parameters, states);

return output.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

#pragma once

#ifndef CNZSL_STRUCTS_GLSLBINDINGMAPPING_HPP
#define CNZSL_STRUCTS_GLSLBINDINGMAPPING_HPP
#ifndef CNZSL_STRUCTS_GLSLWRITERPARAMETERS_HPP
#define CNZSL_STRUCTS_GLSLWRITERPARAMETERS_HPP

#include <NZSL/GlslWriter.hpp>

struct nzslGlslBindingMapping
struct nzslGlslWriterParameters
{
nzsl::GlslWriter::BindingMapping mappings;
nzsl::GlslWriter::Parameters parameters;
};

#endif // CNZSL_STRUCTS_GLSLBINDINGMAPPING_HPP
#endif // CNZSL_STRUCTS_GLSLWRITERPARAMETERS_HPP
Loading

0 comments on commit a39f15f

Please sign in to comment.