SpivWriter WIP
We have debug label, annotations, types and constants. The big part missing is instructions
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <Nazara/Renderer/ShaderVarVisitor.hpp>
|
||||
#include <Nazara/Renderer/ShaderVisitor.hpp>
|
||||
#include <Nazara/Renderer/ShaderWriter.hpp>
|
||||
#include <Nazara/Utility/FieldOffsets.hpp>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
@@ -41,33 +42,49 @@ namespace Nz
|
||||
|
||||
private:
|
||||
struct Opcode;
|
||||
struct Raw;
|
||||
struct WordCount;
|
||||
|
||||
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);
|
||||
struct Section
|
||||
{
|
||||
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);
|
||||
inline std::size_t Append(UInt32 value);
|
||||
std::size_t Append(const Opcode& opcode, const WordCount& wordCount);
|
||||
std::size_t Append(const Raw& raw);
|
||||
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);
|
||||
|
||||
inline unsigned int CountWord(const char* str);
|
||||
inline unsigned int CountWord(const std::string_view& str);
|
||||
inline unsigned int CountWord(const std::string& str);
|
||||
unsigned int CountWord(const Raw& raw);
|
||||
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);
|
||||
|
||||
inline std::size_t GetOutputOffset() const;
|
||||
|
||||
std::vector<UInt32> data;
|
||||
};
|
||||
|
||||
UInt32 AllocateResultId();
|
||||
|
||||
void AppendConstants();
|
||||
void AppendHeader();
|
||||
void AppendStructType(std::size_t structIndex, UInt32 resultId);
|
||||
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);
|
||||
UInt32 GetConstantId(const ShaderNodes::Constant::Variant& value) const;
|
||||
UInt32 GetTypeId(const ShaderExpressionType& type) const;
|
||||
|
||||
std::size_t GetOutputOffset() const;
|
||||
void PushResultId(UInt32 value);
|
||||
UInt32 PopResultId();
|
||||
|
||||
UInt32 ProcessType(ShaderExpressionType type);
|
||||
UInt32 RegisterType(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;
|
||||
@@ -82,7 +99,7 @@ namespace Nz
|
||||
void Visit(const ShaderNodes::StatementBlock& node) override;
|
||||
void Visit(const ShaderNodes::SwizzleOp& node) override;
|
||||
|
||||
static void MergeBlocks(std::vector<UInt32>& output, const std::vector<UInt32>& from);
|
||||
static void MergeBlocks(std::vector<UInt32>& output, const Section& from);
|
||||
|
||||
struct Context
|
||||
{
|
||||
|
||||
@@ -9,12 +9,12 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline std::size_t SpirvWriter::Append(const char* str)
|
||||
inline std::size_t SpirvWriter::Section::Append(const char* str)
|
||||
{
|
||||
return Append(std::string_view(str));
|
||||
}
|
||||
|
||||
inline std::size_t SpirvWriter::Append(const std::string_view& str)
|
||||
inline std::size_t SpirvWriter::Section::Append(const std::string_view& str)
|
||||
{
|
||||
std::size_t offset = GetOutputOffset();
|
||||
|
||||
@@ -35,12 +35,20 @@ namespace Nz
|
||||
return offset;
|
||||
}
|
||||
|
||||
inline std::size_t SpirvWriter::Append(const std::string& str)
|
||||
inline std::size_t SpirvWriter::Section::Append(const std::string& str)
|
||||
{
|
||||
return Append(std::string_view(str));
|
||||
}
|
||||
|
||||
inline std::size_t SpirvWriter::Append(std::initializer_list<UInt32> codepoints)
|
||||
inline std::size_t SpirvWriter::Section::Append(UInt32 value)
|
||||
{
|
||||
std::size_t offset = GetOutputOffset();
|
||||
data.push_back(value);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
inline std::size_t SpirvWriter::Section::Append(std::initializer_list<UInt32> codepoints)
|
||||
{
|
||||
std::size_t offset = GetOutputOffset();
|
||||
|
||||
@@ -51,10 +59,10 @@ namespace Nz
|
||||
}
|
||||
|
||||
template<typename ...Args>
|
||||
inline std::size_t SpirvWriter::Append(Opcode opcode, const Args&... args)
|
||||
std::size_t SpirvWriter::Section::Append(Opcode opcode, const Args&... args)
|
||||
{
|
||||
unsigned int wordCount = 1 + (CountWord(args) + ... + 0);
|
||||
std::size_t offset = Append(opcode, wordCount);
|
||||
std::size_t offset = Append(opcode, WordCount{ wordCount });
|
||||
if constexpr (sizeof...(args) > 0)
|
||||
(Append(args), ...);
|
||||
|
||||
@@ -62,37 +70,42 @@ namespace Nz
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline std::size_t SpirvWriter::Append(T value)
|
||||
std::size_t SpirvWriter::Section::Append(T value)
|
||||
{
|
||||
return Append(static_cast<UInt32>(value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline unsigned int SpirvWriter::CountWord(const T& value)
|
||||
unsigned int SpirvWriter::Section::CountWord(const T& value)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename ...Args>
|
||||
unsigned int SpirvWriter::CountWord(const T1& value, const T2& value2, const Args&... rest)
|
||||
unsigned int SpirvWriter::Section::CountWord(const T1& value, const T2& value2, const Args&... rest)
|
||||
{
|
||||
return CountWord(value) + CountWord(value2) + (CountWord(rest) + ...);
|
||||
}
|
||||
|
||||
inline unsigned int SpirvWriter::CountWord(const char* str)
|
||||
inline unsigned int SpirvWriter::Section::CountWord(const char* str)
|
||||
{
|
||||
return CountWord(std::string_view(str));
|
||||
}
|
||||
|
||||
inline unsigned int Nz::SpirvWriter::CountWord(const std::string& str)
|
||||
inline unsigned int Nz::SpirvWriter::Section::CountWord(const std::string& str)
|
||||
{
|
||||
return CountWord(std::string_view(str));
|
||||
}
|
||||
|
||||
inline unsigned int SpirvWriter::CountWord(const std::string_view& str)
|
||||
inline unsigned int SpirvWriter::Section::CountWord(const std::string_view& str)
|
||||
{
|
||||
return (static_cast<unsigned int>(str.size() + 1) + sizeof(UInt32) - 1) / sizeof(UInt32); //< + 1 for null character
|
||||
}
|
||||
|
||||
std::size_t SpirvWriter::Section::GetOutputOffset() const
|
||||
{
|
||||
return data.size();
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Renderer/DebugOff.hpp>
|
||||
|
||||
Reference in New Issue
Block a user