2017-10-11 23:29:05 +02:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
|
2017-10-18 09:50:30 +02:00
|
|
|
#include "spirv_code_buffer.h"
|
2017-10-11 23:29:05 +02:00
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
SpirvCodeBuffer:: SpirvCodeBuffer() { }
|
|
|
|
SpirvCodeBuffer::~SpirvCodeBuffer() { }
|
2017-10-11 23:29:05 +02:00
|
|
|
|
|
|
|
|
2019-04-04 02:46:41 +02:00
|
|
|
SpirvCodeBuffer::SpirvCodeBuffer(uint32_t size)
|
|
|
|
: m_ptr(size) {
|
|
|
|
m_code.resize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-30 17:22:36 +01:00
|
|
|
SpirvCodeBuffer::SpirvCodeBuffer(uint32_t size, const uint32_t* data)
|
|
|
|
: m_ptr(size) {
|
2017-11-20 15:35:29 +01:00
|
|
|
m_code.resize(size);
|
|
|
|
std::memcpy(m_code.data(), data, size * sizeof(uint32_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-23 18:17:16 +01:00
|
|
|
SpirvCodeBuffer::SpirvCodeBuffer(std::istream& stream) {
|
2017-10-15 17:56:06 +02:00
|
|
|
stream.ignore(std::numeric_limits<std::streamsize>::max());
|
|
|
|
std::streamsize length = stream.gcount();
|
|
|
|
stream.clear();
|
|
|
|
stream.seekg(0, std::ios_base::beg);
|
|
|
|
|
|
|
|
std::vector<char> buffer(length);
|
|
|
|
stream.read(buffer.data(), length);
|
|
|
|
buffer.resize(stream.gcount());
|
|
|
|
|
|
|
|
m_code.resize(buffer.size() / sizeof(uint32_t));
|
|
|
|
std::memcpy(reinterpret_cast<char*>(m_code.data()),
|
|
|
|
buffer.data(), m_code.size() * sizeof(uint32_t));
|
2017-12-30 17:22:36 +01:00
|
|
|
|
|
|
|
m_ptr = m_code.size();
|
2017-10-15 17:56:06 +02:00
|
|
|
}
|
2017-10-11 23:29:05 +02:00
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::append(const SpirvCodeBuffer& other) {
|
2017-10-16 19:52:54 +02:00
|
|
|
if (other.size() != 0) {
|
|
|
|
const size_t size = m_code.size();
|
|
|
|
m_code.resize(size + other.m_code.size());
|
|
|
|
|
|
|
|
uint32_t* dst = this->m_code.data();
|
|
|
|
const uint32_t* src = other.m_code.data();
|
|
|
|
|
2017-10-17 13:02:57 +02:00
|
|
|
std::memcpy(dst + size, src, other.size());
|
2017-12-30 17:22:36 +01:00
|
|
|
m_ptr += other.m_code.size();
|
2017-10-16 19:52:54 +02:00
|
|
|
}
|
2017-10-11 23:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::putWord(uint32_t word) {
|
2017-12-30 17:22:36 +01:00
|
|
|
m_code.insert(m_code.begin() + m_ptr, word);
|
|
|
|
m_ptr += 1;
|
2017-10-11 23:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::putIns(spv::Op opCode, uint16_t wordCount) {
|
2017-10-11 23:29:05 +02:00
|
|
|
this->putWord(
|
|
|
|
(static_cast<uint32_t>(opCode) << 0)
|
|
|
|
| (static_cast<uint32_t>(wordCount) << 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::putInt32(uint32_t word) {
|
2017-10-11 23:29:05 +02:00
|
|
|
this->putWord(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::putInt64(uint64_t value) {
|
2017-10-11 23:29:05 +02:00
|
|
|
this->putWord(value >> 0);
|
|
|
|
this->putWord(value >> 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::putFloat32(float value) {
|
2017-10-11 23:29:05 +02:00
|
|
|
uint32_t tmp;
|
|
|
|
static_assert(sizeof(tmp) == sizeof(value));
|
|
|
|
std::memcpy(&tmp, &value, sizeof(value));
|
|
|
|
this->putInt32(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::putFloat64(double value) {
|
2017-10-11 23:29:05 +02:00
|
|
|
uint64_t tmp;
|
|
|
|
static_assert(sizeof(tmp) == sizeof(value));
|
|
|
|
std::memcpy(&tmp, &value, sizeof(value));
|
|
|
|
this->putInt64(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::putStr(const char* str) {
|
2017-10-11 23:29:05 +02:00
|
|
|
uint32_t word = 0;
|
|
|
|
uint32_t nbit = 0;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; str[i] != '\0'; str++) {
|
|
|
|
word |= (static_cast<uint32_t>(str[i]) & 0xFF) << nbit;
|
|
|
|
|
|
|
|
if ((nbit += 8) == 32) {
|
|
|
|
this->putWord(word);
|
|
|
|
word = 0;
|
|
|
|
nbit = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit current word
|
|
|
|
this->putWord(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
void SpirvCodeBuffer::putHeader(uint32_t boundIds) {
|
2017-10-11 23:29:05 +02:00
|
|
|
this->putWord(spv::MagicNumber);
|
2018-11-07 11:57:04 +01:00
|
|
|
this->putWord(0x00010300); // v1.3
|
2017-10-11 23:29:05 +02:00
|
|
|
this->putWord(0); // Generator
|
|
|
|
this->putWord(boundIds);
|
|
|
|
this->putWord(0); // Schema
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 10:36:47 +02:00
|
|
|
uint32_t SpirvCodeBuffer::strLen(const char* str) {
|
2017-10-11 23:29:05 +02:00
|
|
|
// Null-termination plus padding
|
|
|
|
return (std::strlen(str) + 4) / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-23 18:17:16 +01:00
|
|
|
void SpirvCodeBuffer::store(std::ostream& stream) const {
|
2017-10-15 17:56:06 +02:00
|
|
|
stream.write(
|
|
|
|
reinterpret_cast<const char*>(m_code.data()),
|
|
|
|
sizeof(uint32_t) * m_code.size());
|
2017-10-11 23:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|