Shader/SpirvWriter: Split section to another class + improve variadic parameters support
This commit is contained in:
73
include/Nazara/Shader/SpirvSection.hpp
Normal file
73
include/Nazara/Shader/SpirvSection.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Shader generator"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SPIRVSECTION_HPP
|
||||
#define NAZARA_SPIRVSECTION_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Shader/ShaderEnums.hpp>
|
||||
#include <Nazara/Shader/SpirvData.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_SHADER_API SpirvSection
|
||||
{
|
||||
public:
|
||||
struct OpSize;
|
||||
struct Raw;
|
||||
|
||||
SpirvSection() = default;
|
||||
SpirvSection(const SpirvSection& cache) = default;
|
||||
SpirvSection(SpirvSection&& cache) = default;
|
||||
~SpirvSection() = default;
|
||||
|
||||
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);
|
||||
inline std::size_t Append(SpirvOp opcode, const OpSize& 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(SpirvOp opcode, const Args&... args);
|
||||
template<typename F> std::size_t AppendVariadic(SpirvOp opcode, F&& callback);
|
||||
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);
|
||||
inline 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 const std::vector<UInt32>& GetBytecode() const;
|
||||
inline std::size_t GetOutputOffset() const;
|
||||
|
||||
SpirvSection& operator=(const SpirvSection& cache) = delete;
|
||||
SpirvSection& operator=(SpirvSection&& cache) = default;
|
||||
|
||||
struct OpSize
|
||||
{
|
||||
unsigned int wc;
|
||||
};
|
||||
|
||||
struct Raw
|
||||
{
|
||||
const void* ptr;
|
||||
std::size_t size;
|
||||
};
|
||||
|
||||
static inline UInt32 BuildOpcode(SpirvOp opcode, unsigned int wordCount);
|
||||
|
||||
private:
|
||||
std::vector<UInt32> m_bytecode;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/SpirvSection.inl>
|
||||
|
||||
#endif
|
||||
146
include/Nazara/Shader/SpirvSection.inl
Normal file
146
include/Nazara/Shader/SpirvSection.inl
Normal file
@@ -0,0 +1,146 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Shader generator"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Shader/SpirvSection.hpp>
|
||||
#include <Nazara/Shader/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline std::size_t SpirvSection::Append(const char* str)
|
||||
{
|
||||
return Append(std::string_view(str));
|
||||
}
|
||||
|
||||
inline std::size_t SpirvSection::Append(const std::string_view& str)
|
||||
{
|
||||
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 SpirvSection::Append(const std::string& str)
|
||||
{
|
||||
return Append(std::string_view(str));
|
||||
}
|
||||
|
||||
inline std::size_t SpirvSection::Append(UInt32 value)
|
||||
{
|
||||
std::size_t offset = GetOutputOffset();
|
||||
m_bytecode.push_back(value);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
inline std::size_t SpirvSection::Append(SpirvOp opcode, const OpSize& wordCount)
|
||||
{
|
||||
return Append(BuildOpcode(opcode, wordCount.wc));
|
||||
}
|
||||
|
||||
inline std::size_t SpirvSection::Append(std::initializer_list<UInt32> codepoints)
|
||||
{
|
||||
std::size_t offset = GetOutputOffset();
|
||||
|
||||
for (UInt32 cp : codepoints)
|
||||
Append(cp);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
template<typename ...Args>
|
||||
std::size_t SpirvSection::Append(SpirvOp opcode, const Args&... args)
|
||||
{
|
||||
unsigned int wordCount = 1 + (CountWord(args) + ... + 0);
|
||||
std::size_t offset = Append(opcode, OpSize{ wordCount });
|
||||
if constexpr (sizeof...(args) > 0)
|
||||
(Append(args), ...);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
template<typename F> std::size_t SpirvSection::AppendVariadic(SpirvOp opcode, F&& callback)
|
||||
{
|
||||
std::size_t offset = Append(0); //< Will be filled later
|
||||
|
||||
unsigned int wordCount = 1;
|
||||
auto appendFunctor = [&](const auto& value)
|
||||
{
|
||||
wordCount += CountWord(value);
|
||||
Append(value);
|
||||
};
|
||||
callback(appendFunctor);
|
||||
|
||||
m_bytecode[offset] = BuildOpcode(opcode, wordCount);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::size_t SpirvSection::Append(T value)
|
||||
{
|
||||
return Append(static_cast<UInt32>(value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
unsigned int SpirvSection::CountWord(const T& value)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename ...Args>
|
||||
unsigned int SpirvSection::CountWord(const T1& value, const T2& value2, const Args&... rest)
|
||||
{
|
||||
return CountWord(value) + CountWord(value2) + (CountWord(rest) + ...);
|
||||
}
|
||||
|
||||
inline unsigned int SpirvSection::CountWord(const char* str)
|
||||
{
|
||||
return CountWord(std::string_view(str));
|
||||
}
|
||||
|
||||
inline unsigned int Nz::SpirvSection::CountWord(const std::string& str)
|
||||
{
|
||||
return CountWord(std::string_view(str));
|
||||
}
|
||||
|
||||
inline unsigned int SpirvSection::CountWord(const Raw& raw)
|
||||
{
|
||||
return static_cast<unsigned int>((raw.size + sizeof(UInt32) - 1) / sizeof(UInt32));
|
||||
}
|
||||
|
||||
inline unsigned int SpirvSection::CountWord(const std::string_view& str)
|
||||
{
|
||||
return (static_cast<unsigned int>(str.size() + 1) + sizeof(UInt32) - 1) / sizeof(UInt32); //< + 1 for null character
|
||||
}
|
||||
|
||||
inline const std::vector<UInt32>& SpirvSection::GetBytecode() const
|
||||
{
|
||||
return m_bytecode;
|
||||
}
|
||||
|
||||
inline std::size_t SpirvSection::GetOutputOffset() const
|
||||
{
|
||||
return m_bytecode.size();
|
||||
}
|
||||
|
||||
inline UInt32 SpirvSection::BuildOpcode(SpirvOp opcode, unsigned int wordCount)
|
||||
{
|
||||
return UInt32(opcode) | UInt32(wordCount) << 16;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Shader/DebugOff.hpp>
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class SpirvSection;
|
||||
|
||||
class NAZARA_SHADER_API SpirvWriter : public ShaderAstVisitor, public ShaderVarVisitor
|
||||
{
|
||||
public:
|
||||
@@ -42,33 +44,6 @@ namespace Nz
|
||||
|
||||
private:
|
||||
struct ExtVar;
|
||||
struct Opcode;
|
||||
struct Raw;
|
||||
struct WordCount;
|
||||
|
||||
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();
|
||||
|
||||
@@ -111,7 +86,7 @@ namespace Nz
|
||||
void Visit(ShaderNodes::ParameterVariable& var) override;
|
||||
void Visit(ShaderNodes::UniformVariable& var) override;
|
||||
|
||||
static void MergeBlocks(std::vector<UInt32>& output, const Section& from);
|
||||
static void MergeBlocks(std::vector<UInt32>& output, const SpirvSection& from);
|
||||
|
||||
struct Context
|
||||
{
|
||||
|
||||
@@ -3,109 +3,10 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Shader/SpirvWriter.hpp>
|
||||
#include <cassert>
|
||||
#include <type_traits>
|
||||
#include <Nazara/Shader/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline std::size_t SpirvWriter::Section::Append(const char* str)
|
||||
{
|
||||
return Append(std::string_view(str));
|
||||
}
|
||||
|
||||
inline std::size_t SpirvWriter::Section::Append(const std::string_view& str)
|
||||
{
|
||||
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::Section::Append(const std::string& str)
|
||||
{
|
||||
return Append(std::string_view(str));
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
for (UInt32 cp : codepoints)
|
||||
Append(cp);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
template<typename ...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{ wordCount });
|
||||
if constexpr (sizeof...(args) > 0)
|
||||
(Append(args), ...);
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::size_t SpirvWriter::Section::Append(T value)
|
||||
{
|
||||
return Append(static_cast<UInt32>(value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
unsigned int SpirvWriter::Section::CountWord(const T& value)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename ...Args>
|
||||
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::Section::CountWord(const char* str)
|
||||
{
|
||||
return CountWord(std::string_view(str));
|
||||
}
|
||||
|
||||
inline unsigned int Nz::SpirvWriter::Section::CountWord(const std::string& str)
|
||||
{
|
||||
return CountWord(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/Shader/DebugOff.hpp>
|
||||
|
||||
Reference in New Issue
Block a user