From 5d1642af1f737093bf89f0e5bae4607dac1ed8f9 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Tue, 10 Apr 2018 12:48:24 +0200 Subject: [PATCH] [spirv] Track instruction offset in instruction iterator --- src/spirv/spirv_code_buffer.h | 10 +++--- src/spirv/spirv_instruction.h | 63 ++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/spirv/spirv_code_buffer.h b/src/spirv/spirv_code_buffer.h index dc3425380..b1d248284 100644 --- a/src/spirv/spirv_code_buffer.h +++ b/src/spirv/spirv_code_buffer.h @@ -34,9 +34,8 @@ namespace dxvk { * \brief Code data * \returns Code data */ - const uint32_t* data() const { - return m_code.data(); - } + const uint32_t* data() const { return m_code.data(); } + uint32_t* data() { return m_code.data(); } /** * \brief Code size, in bytes @@ -54,7 +53,8 @@ namespace dxvk { * \returns Instruction iterator */ SpirvInstructionIterator begin() { - return SpirvInstructionIterator(m_code.data(), m_code.size()); + return SpirvInstructionIterator( + m_code.data(), 0, m_code.size()); } /** @@ -64,7 +64,7 @@ namespace dxvk { * \returns Instruction iterator */ SpirvInstructionIterator end() { - return SpirvInstructionIterator(nullptr, 0); + return SpirvInstructionIterator(nullptr, 0, 0); } /** diff --git a/src/spirv/spirv_instruction.h b/src/spirv/spirv_instruction.h index 6ed9504cc..0751c1bc9 100644 --- a/src/spirv/spirv_instruction.h +++ b/src/spirv/spirv_instruction.h @@ -19,8 +19,8 @@ namespace dxvk { public: SpirvInstruction() { } - SpirvInstruction(uint32_t* code, uint32_t size) - : m_code(code), m_size(size) { } + SpirvInstruction(uint32_t* code, uint32_t offset, uint32_t length) + : m_code(code), m_offset(offset), m_length(length) { } /** * \brief SPIR-V Op code @@ -39,6 +39,14 @@ namespace dxvk { return this->arg(0) >> spv::WordCountShift; } + /** + * \brief Instruction offset + * \returns Offset in DWORDs + */ + uint32_t offset() const { + return m_offset; + } + /** * \brief Argument value * @@ -50,7 +58,8 @@ namespace dxvk { * \returns The argument value */ uint32_t arg(uint32_t id) const { - return id < m_size ? m_code[id] : 0; + const uint32_t index = m_offset + id; + return index < m_length ? m_code[index] : 0; } /** @@ -60,20 +69,21 @@ namespace dxvk { * \param [in] word New argument word */ void setArg(uint32_t id, uint32_t word) const { - if (id < m_size) - m_code[id] = word; + if (m_offset + id < m_length) + m_code[m_offset + id] = word; } private: - uint32_t* m_code = nullptr; - uint32_t m_size = 0; + uint32_t* m_code = nullptr; + uint32_t m_offset = 0; + uint32_t m_length = 0; }; /** - * \brief SPIR-V instruction + * \brief SPIR-V instruction iterator * * Convenient iterator that can be used * to process raw SPIR-V shader code. @@ -83,43 +93,48 @@ namespace dxvk { public: SpirvInstructionIterator() { } - SpirvInstructionIterator(uint32_t* code, uint32_t size) - : m_code(size != 0 ? code : nullptr), m_size(size) { - if ((size >= 5) && (m_code[0] == spv::MagicNumber)) + SpirvInstructionIterator(uint32_t* code, uint32_t offset, uint32_t length) + : m_code (length != 0 ? code : nullptr), + m_offset(length != 0 ? offset : 0), + m_length(length) { + if ((length >= 5) && (m_code[0] == spv::MagicNumber)) this->advance(5); } SpirvInstructionIterator& operator ++ () { - this->advance(SpirvInstruction(m_code, m_size).length()); + this->advance(SpirvInstruction(m_code, m_offset, m_length).length()); return *this; } SpirvInstruction operator * () const { - return SpirvInstruction(m_code, m_size); + return SpirvInstruction(m_code, m_offset, m_length); } bool operator == (const SpirvInstructionIterator& other) const { - return this->m_code == other.m_code - && this->m_size == other.m_size; + return this->m_code == other.m_code + && this->m_offset == other.m_offset + && this->m_length == other.m_length; } bool operator != (const SpirvInstructionIterator& other) const { - return this->m_code != other.m_code - && this->m_size != other.m_size; + return this->m_code != other.m_code + || this->m_offset != other.m_offset + || this->m_length != other.m_length; } private: - uint32_t* m_code = nullptr; - uint32_t m_size = 0; + uint32_t* m_code = nullptr; + uint32_t m_offset = 0; + uint32_t m_length = 0; void advance(uint32_t n) { - if (n < m_size) { - m_code += n; - m_size -= n; + if (m_offset + n < m_length) { + m_offset += n; } else { - m_code = nullptr; - m_size = 0; + m_code = nullptr; + m_offset = 0; + m_length = 0; } }