2017-10-11 23:29:05 +02:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "dxvk_spirv_code_buffer.h"
|
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
DxvkSpirvCodeBuffer:: DxvkSpirvCodeBuffer() { }
|
|
|
|
DxvkSpirvCodeBuffer::~DxvkSpirvCodeBuffer() { }
|
2017-10-11 23:29:05 +02:00
|
|
|
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
DxvkSpirvCodeBuffer::DxvkSpirvCodeBuffer(
|
2017-10-11 23:29:05 +02:00
|
|
|
std::basic_istream<uint32_t>& stream)
|
|
|
|
: m_code(
|
|
|
|
std::istreambuf_iterator<uint32_t>(stream),
|
|
|
|
std::istreambuf_iterator<uint32_t>()) { }
|
|
|
|
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::append(const DxvkSpirvCodeBuffer& other) {
|
2017-10-11 23:29:05 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
std::memcpy(dst + size, src, sizeof(uint32_t) * size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::putWord(uint32_t word) {
|
2017-10-11 23:29:05 +02:00
|
|
|
m_code.push_back(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::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-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::putInt32(uint32_t word) {
|
2017-10-11 23:29:05 +02:00
|
|
|
this->putWord(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::putInt64(uint64_t value) {
|
2017-10-11 23:29:05 +02:00
|
|
|
this->putWord(value >> 0);
|
|
|
|
this->putWord(value >> 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::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-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::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-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::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-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::putHeader(uint32_t boundIds) {
|
2017-10-11 23:29:05 +02:00
|
|
|
this->putWord(spv::MagicNumber);
|
|
|
|
this->putWord(spv::Version);
|
|
|
|
this->putWord(0); // Generator
|
|
|
|
this->putWord(boundIds);
|
|
|
|
this->putWord(0); // Schema
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
uint32_t DxvkSpirvCodeBuffer::strLen(const char* str) {
|
2017-10-11 23:29:05 +02:00
|
|
|
// Null-termination plus padding
|
|
|
|
return (std::strlen(str) + 4) / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-14 23:52:47 +02:00
|
|
|
void DxvkSpirvCodeBuffer::store(std::basic_ostream<uint32_t>& stream) const {
|
2017-10-11 23:29:05 +02:00
|
|
|
stream.write(m_code.data(), m_code.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|