Shader/SpirvPrinter: Add settings

This commit is contained in:
Jérôme Leclercq 2020-08-24 16:49:14 +02:00
parent 77b66620c9
commit ba777ebbca
2 changed files with 146 additions and 123 deletions

View File

@ -16,16 +16,24 @@ namespace Nz
class NAZARA_SHADER_API SpirvPrinter
{
public:
struct Settings;
inline SpirvPrinter();
SpirvPrinter(const SpirvPrinter&) = default;
SpirvPrinter(SpirvPrinter&&) = default;
~SpirvPrinter() = default;
std::string Print(const UInt32* codepoints, std::size_t count);
std::string Print(const UInt32* codepoints, std::size_t count, const Settings& settings = Settings());
SpirvPrinter& operator=(const SpirvPrinter&) = default;
SpirvPrinter& operator=(SpirvPrinter&&) = default;
struct Settings
{
bool printHeader = true;
bool printParameters = true;
};
private:
void AppendInstruction();
std::string ReadString();

View File

@ -19,13 +19,18 @@ namespace Nz
std::size_t index = 0;
std::size_t count;
std::ostringstream stream;
const Settings& settings;
};
std::string SpirvPrinter::Print(const UInt32* codepoints, std::size_t count)
std::string SpirvPrinter::Print(const UInt32* codepoints, std::size_t count, const Settings& settings)
{
State state;
state.codepoints = codepoints;
state.count = count;
State state = {
codepoints,
0,
count,
{},
settings
};
m_currentState = &state;
CallOnExit resetOnExit([&] { m_currentState = nullptr; });
@ -34,6 +39,7 @@ namespace Nz
if (magicNumber != SpvMagicNumber)
throw std::runtime_error("invalid Spir-V: magic number didn't match");
if (m_currentState->settings.printHeader)
m_currentState->stream << "Spir-V module\n";
UInt32 versionNumber = ReadWord();
@ -43,17 +49,17 @@ namespace Nz
UInt8 majorVersion = ((versionNumber) >> 16) & 0xFF;
UInt8 minorVersion = ((versionNumber) >> 8) & 0xFF;
m_currentState->stream << "Version " + std::to_string(+majorVersion) << "." << std::to_string(+minorVersion) << "\n";
UInt32 generatorId = ReadWord();
m_currentState->stream << "Generator: " << std::to_string(generatorId) << "\n";
UInt32 bound = ReadWord();
m_currentState->stream << "Bound: " << std::to_string(bound) << "\n";
UInt32 schema = ReadWord();
if (m_currentState->settings.printHeader)
{
m_currentState->stream << "Version " + std::to_string(+majorVersion) << "." << std::to_string(+minorVersion) << "\n";
m_currentState->stream << "Generator: " << std::to_string(generatorId) << "\n";
m_currentState->stream << "Bound: " << std::to_string(bound) << "\n";
m_currentState->stream << "Schema: " << std::to_string(schema) << "\n";
}
while (m_currentState->index < m_currentState->count)
AppendInstruction();
@ -76,6 +82,8 @@ namespace Nz
m_currentState->stream << inst->name;
if (m_currentState->settings.printParameters)
{
std::size_t currentOperand = 0;
std::size_t instructionEnd = startIndex + wordCount;
while (m_currentState->index < instructionEnd)
@ -197,6 +205,13 @@ namespace Nz
if (currentOperand < inst->minOperandCount - 1)
currentOperand++;
}
}
else
{
m_currentState->index += wordCount - 1;
if (m_currentState->index > m_currentState->count)
throw std::runtime_error("unexpected end of stream");
}
m_currentState->stream << "\n";