WIP
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/ShaderExpressionType.hpp>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
@@ -27,7 +28,7 @@ namespace Nz
|
||||
|
||||
using VariablePtr = std::shared_ptr<Variable>;
|
||||
|
||||
struct NAZARA_RENDERER_API Variable
|
||||
struct NAZARA_RENDERER_API Variable : std::enable_shared_from_this<Variable>
|
||||
{
|
||||
virtual ~Variable();
|
||||
|
||||
|
||||
@@ -13,13 +13,13 @@
|
||||
#include <Nazara/Renderer/ShaderVarVisitor.hpp>
|
||||
#include <Nazara/Renderer/ShaderVisitor.hpp>
|
||||
#include <Nazara/Renderer/ShaderWriter.hpp>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_RENDERER_API SpirvWriter : public ShaderVarVisitor, public ShaderVisitor
|
||||
class NAZARA_RENDERER_API SpirvWriter : public ShaderVisitor
|
||||
{
|
||||
public:
|
||||
struct Environment;
|
||||
@@ -35,48 +35,54 @@ namespace Nz
|
||||
|
||||
struct Environment
|
||||
{
|
||||
UInt32 spvMajorVersion = 1;
|
||||
UInt32 spvMinorVersion = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
struct Opcode;
|
||||
|
||||
inline void Append(const char* str);
|
||||
void Append(const std::string_view& str);
|
||||
void Append(const Opcode& opcode, unsigned int wordCount);
|
||||
void Append(UInt32 codepoint);
|
||||
void Append(std::initializer_list<UInt32> codepoints);
|
||||
template<typename... Args> void Append(Opcode opcode, const Args&... args);
|
||||
template<typename T> void Append(T value);
|
||||
inline std::size_t Append(const char* str);
|
||||
inline std::size_t Append(const std::string_view& str);
|
||||
inline std::size_t Append(const std::string& str);
|
||||
std::size_t Append(UInt32 value);
|
||||
std::size_t Append(const Opcode& opcode, unsigned int wordCount);
|
||||
inline std::size_t Append(std::initializer_list<UInt32> codepoints);
|
||||
template<typename... Args> std::size_t Append(Opcode opcode, const Args&... args);
|
||||
template<typename T> std::size_t Append(T value);
|
||||
|
||||
UInt32 AllocateResultId();
|
||||
|
||||
void AppendHeader();
|
||||
void AppendTypes();
|
||||
|
||||
inline unsigned int CountWord(const char* str);
|
||||
unsigned int CountWord(const std::string_view& str);
|
||||
inline unsigned int CountWord(const std::string& str);
|
||||
template<typename T> unsigned int CountWord(const T& value);
|
||||
template<typename T1, typename T2, typename... Args> unsigned int CountWord(const T1& value, const T2& value2, const Args&... rest);
|
||||
|
||||
using ShaderVarVisitor::Visit;
|
||||
std::size_t GetOutputOffset() const;
|
||||
|
||||
UInt32 ProcessType(ShaderExpressionType type);
|
||||
|
||||
using ShaderVisitor::Visit;
|
||||
void Visit(const ShaderNodes::ExpressionPtr& expr, bool encloseIfRequired = false);
|
||||
void Visit(const ShaderNodes::AccessMember& node) override;
|
||||
void Visit(const ShaderNodes::AssignOp& node) override;
|
||||
void Visit(const ShaderNodes::Branch& node) override;
|
||||
void Visit(const ShaderNodes::BinaryOp& node) override;
|
||||
void Visit(const ShaderNodes::BuiltinVariable& var) override;
|
||||
void Visit(const ShaderNodes::Cast& node) override;
|
||||
void Visit(const ShaderNodes::Constant& node) override;
|
||||
void Visit(const ShaderNodes::DeclareVariable& node) override;
|
||||
void Visit(const ShaderNodes::ExpressionStatement& node) override;
|
||||
void Visit(const ShaderNodes::Identifier& node) override;
|
||||
void Visit(const ShaderNodes::InputVariable& var) override;
|
||||
void Visit(const ShaderNodes::IntrinsicCall& node) override;
|
||||
void Visit(const ShaderNodes::LocalVariable& var) override;
|
||||
void Visit(const ShaderNodes::ParameterVariable& var) override;
|
||||
void Visit(const ShaderNodes::OutputVariable& var) override;
|
||||
void Visit(const ShaderNodes::Sample2D& node) override;
|
||||
void Visit(const ShaderNodes::StatementBlock& node) override;
|
||||
void Visit(const ShaderNodes::SwizzleOp& node) override;
|
||||
void Visit(const ShaderNodes::UniformVariable& var) override;
|
||||
|
||||
static void MergeBlocks(std::vector<UInt32>& output, const std::vector<UInt32>& from);
|
||||
|
||||
struct Context
|
||||
{
|
||||
@@ -84,10 +90,7 @@ namespace Nz
|
||||
const ShaderAst::Function* currentFunction = nullptr;
|
||||
};
|
||||
|
||||
struct State
|
||||
{
|
||||
std::vector<UInt32> output;
|
||||
};
|
||||
struct State;
|
||||
|
||||
Context m_context;
|
||||
Environment m_environment;
|
||||
|
||||
@@ -9,25 +9,62 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline void SpirvWriter::Append(const char* str)
|
||||
inline std::size_t SpirvWriter::Append(const char* str)
|
||||
{
|
||||
return Append(std::string_view(str));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void SpirvWriter::Append(T value)
|
||||
inline std::size_t SpirvWriter::Append(const std::string_view& str)
|
||||
{
|
||||
assert(m_currentState);
|
||||
m_currentState->output.push_back(static_cast<UInt32>(value));
|
||||
std::size_t offset = GetOutputOffset();
|
||||
|
||||
std::size_t size4 = CountWord(str);
|
||||
for (std::size_t i = 0; i < size4; ++i)
|
||||
{
|
||||
UInt32 codepoint = 0;
|
||||
for (std::size_t j = 0; j < 4; ++j)
|
||||
{
|
||||
std::size_t pos = i * 4 + j;
|
||||
if (pos < str.size())
|
||||
codepoint |= UInt32(str[pos]) << (j * 8);
|
||||
}
|
||||
|
||||
Append(codepoint);
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
inline std::size_t SpirvWriter::Append(const std::string& str)
|
||||
{
|
||||
return Append(std::string_view(str));
|
||||
}
|
||||
|
||||
inline std::size_t SpirvWriter::Append(std::initializer_list<UInt32> codepoints)
|
||||
{
|
||||
std::size_t offset = GetOutputOffset();
|
||||
|
||||
for (UInt32 cp : codepoints)
|
||||
Append(cp);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
template<typename ...Args>
|
||||
inline void SpirvWriter::Append(Opcode opcode, const Args&... args)
|
||||
inline std::size_t SpirvWriter::Append(Opcode opcode, const Args&... args)
|
||||
{
|
||||
unsigned int wordCount = 1 + (CountWord(args) + ... + 0);
|
||||
Append(opcode, wordCount);
|
||||
std::size_t offset = Append(opcode, wordCount);
|
||||
if constexpr (sizeof...(args) > 0)
|
||||
(Append(args), ...);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline std::size_t SpirvWriter::Append(T value)
|
||||
{
|
||||
return Append(static_cast<UInt32>(value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -47,6 +84,11 @@ namespace Nz
|
||||
return CountWord(std::string_view(str));
|
||||
}
|
||||
|
||||
inline unsigned int Nz::SpirvWriter::CountWord(const std::string& str)
|
||||
{
|
||||
return CountWord(std::string_view(str));
|
||||
}
|
||||
|
||||
inline unsigned int SpirvWriter::CountWord(const std::string_view& str)
|
||||
{
|
||||
return (static_cast<unsigned int>(str.size() + 1) + sizeof(UInt32) - 1) / sizeof(UInt32); //< + 1 for null character
|
||||
|
||||
Reference in New Issue
Block a user