2017-10-17 13:02:57 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <spirv/spirv.hpp>
|
2017-11-16 01:30:17 +01:00
|
|
|
#include <spirv/GLSL.std.450.hpp>
|
2017-10-17 13:02:57 +02:00
|
|
|
|
2017-10-18 09:50:30 +02:00
|
|
|
#include "spirv_include.h"
|
2017-10-17 13:02:57 +02:00
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief SPIR-V instruction
|
|
|
|
*
|
|
|
|
* Helps parsing a single instruction, providing
|
|
|
|
* access to the op code, instruction length and
|
|
|
|
* instruction arguments.
|
|
|
|
*/
|
2017-10-18 10:36:47 +02:00
|
|
|
class SpirvInstruction {
|
2017-10-17 13:02:57 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
SpirvInstruction() { }
|
2017-12-08 22:30:41 +01:00
|
|
|
SpirvInstruction(uint32_t* code, uint32_t size)
|
2017-10-17 13:02:57 +02:00
|
|
|
: m_code(code), m_size(size) { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief SPIR-V Op code
|
|
|
|
* \returns The op code
|
|
|
|
*/
|
|
|
|
spv::Op opCode() const {
|
|
|
|
return static_cast<spv::Op>(
|
2017-12-07 09:38:31 +01:00
|
|
|
this->arg(0) & spv::OpCodeMask);
|
2017-10-17 13:02:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Instruction length
|
|
|
|
* \returns Number of DWORDs
|
|
|
|
*/
|
|
|
|
uint32_t length() const {
|
2017-12-07 09:38:31 +01:00
|
|
|
return this->arg(0) >> spv::WordCountShift;
|
2017-10-17 13:02:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Argument value
|
|
|
|
*
|
|
|
|
* Retrieves an argument DWORD. Note that some instructions
|
|
|
|
* take 64-bit arguments which require more than one DWORD.
|
|
|
|
* Arguments start at index 1. Calling this method with an
|
|
|
|
* argument ID of 0 will return the opcode token.
|
|
|
|
* \param [in] id Argument index, starting at 1
|
|
|
|
* \returns The argument value
|
|
|
|
*/
|
|
|
|
uint32_t arg(uint32_t id) const {
|
|
|
|
return id < m_size ? m_code[id] : 0;
|
|
|
|
}
|
|
|
|
|
2017-12-08 22:30:41 +01:00
|
|
|
/**
|
|
|
|
* \brief Changes the value of an argument
|
|
|
|
*
|
|
|
|
* \param [in] id Argument index, starting at 1
|
|
|
|
* \param [in] word New argument word
|
|
|
|
*/
|
|
|
|
void setArg(uint32_t id, uint32_t word) const {
|
|
|
|
if (id < m_size)
|
|
|
|
m_code[id] = word;
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:02:57 +02:00
|
|
|
private:
|
|
|
|
|
2017-12-08 22:30:41 +01:00
|
|
|
uint32_t* m_code = nullptr;
|
|
|
|
uint32_t m_size = 0;
|
2017-10-17 13:02:57 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief SPIR-V instruction
|
|
|
|
*
|
|
|
|
* Convenient iterator that can be used
|
|
|
|
* to process raw SPIR-V shader code.
|
|
|
|
*/
|
2017-10-18 10:36:47 +02:00
|
|
|
class SpirvInstructionIterator {
|
2017-10-17 13:02:57 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
SpirvInstructionIterator() { }
|
2017-12-08 22:30:41 +01:00
|
|
|
SpirvInstructionIterator(uint32_t* code, uint32_t size)
|
2017-10-17 13:02:57 +02:00
|
|
|
: m_code(size != 0 ? code : nullptr), m_size(size) {
|
|
|
|
if ((size >= 5) && (m_code[0] == spv::MagicNumber))
|
|
|
|
this->advance(5);
|
|
|
|
}
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
SpirvInstructionIterator& operator ++ () {
|
|
|
|
this->advance(SpirvInstruction(m_code, m_size).length());
|
2017-10-17 13:02:57 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
SpirvInstruction operator * () const {
|
|
|
|
return SpirvInstruction(m_code, m_size);
|
2017-10-17 13:02:57 +02:00
|
|
|
}
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
bool operator == (const SpirvInstructionIterator& other) const {
|
2017-10-17 13:02:57 +02:00
|
|
|
return this->m_code == other.m_code
|
|
|
|
&& this->m_size == other.m_size;
|
|
|
|
}
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
bool operator != (const SpirvInstructionIterator& other) const {
|
2017-10-17 13:02:57 +02:00
|
|
|
return this->m_code != other.m_code
|
|
|
|
&& this->m_size != other.m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2017-12-08 22:30:41 +01:00
|
|
|
uint32_t* m_code = nullptr;
|
|
|
|
uint32_t m_size = 0;
|
2017-10-17 13:02:57 +02:00
|
|
|
|
|
|
|
void advance(uint32_t n) {
|
2017-10-21 17:58:58 +02:00
|
|
|
if (n < m_size) {
|
2017-10-17 13:02:57 +02:00
|
|
|
m_code += n;
|
|
|
|
m_size -= n;
|
|
|
|
} else {
|
|
|
|
m_code = nullptr;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|