2017-10-11 23:28:06 +02:00
|
|
|
#include "dxvk_shader.h"
|
|
|
|
|
2019-04-06 10:08:12 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-10-11 23:28:06 +02:00
|
|
|
namespace dxvk {
|
|
|
|
|
2018-07-30 20:15:19 +02:00
|
|
|
DxvkShaderConstData::DxvkShaderConstData() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkShaderConstData::DxvkShaderConstData(
|
|
|
|
size_t dwordCount,
|
|
|
|
const uint32_t* dwordArray)
|
|
|
|
: m_size(dwordCount), m_data(new uint32_t[dwordCount]) {
|
|
|
|
for (size_t i = 0; i < dwordCount; i++)
|
|
|
|
m_data[i] = dwordArray[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkShaderConstData::DxvkShaderConstData(DxvkShaderConstData&& other)
|
|
|
|
: m_size(other.m_size), m_data(other.m_data) {
|
|
|
|
other.m_size = 0;
|
|
|
|
other.m_data = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkShaderConstData& DxvkShaderConstData::operator = (DxvkShaderConstData&& other) {
|
|
|
|
delete[] m_data;
|
|
|
|
this->m_size = other.m_size;
|
|
|
|
this->m_data = other.m_data;
|
|
|
|
other.m_size = 0;
|
|
|
|
other.m_data = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkShaderConstData::~DxvkShaderConstData() {
|
|
|
|
delete[] m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 20:47:58 +02:00
|
|
|
DxvkShaderModule::DxvkShaderModule()
|
2019-04-04 16:10:44 +02:00
|
|
|
: m_vkd(nullptr), m_stage() {
|
|
|
|
|
2019-04-03 20:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkShaderModule::DxvkShaderModule(DxvkShaderModule&& other)
|
2019-04-04 16:10:44 +02:00
|
|
|
: m_vkd(std::move(other.m_vkd)) {
|
|
|
|
this->m_stage = other.m_stage;
|
|
|
|
other.m_stage = VkPipelineShaderStageCreateInfo();
|
2019-04-03 20:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-07 09:38:31 +01:00
|
|
|
DxvkShaderModule::DxvkShaderModule(
|
2017-11-20 14:03:00 +01:00
|
|
|
const Rc<vk::DeviceFn>& vkd,
|
2018-05-03 20:26:55 +02:00
|
|
|
const Rc<DxvkShader>& shader,
|
|
|
|
const SpirvCodeBuffer& code)
|
2019-04-04 16:10:44 +02:00
|
|
|
: m_vkd(vkd), m_stage() {
|
|
|
|
m_stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
|
|
|
m_stage.pNext = nullptr;
|
|
|
|
m_stage.flags = 0;
|
|
|
|
m_stage.stage = shader->stage();
|
|
|
|
m_stage.module = VK_NULL_HANDLE;
|
|
|
|
m_stage.pName = "main";
|
|
|
|
m_stage.pSpecializationInfo = nullptr;
|
|
|
|
|
2017-11-20 14:03:00 +01:00
|
|
|
VkShaderModuleCreateInfo info;
|
|
|
|
info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
|
|
|
info.pNext = nullptr;
|
|
|
|
info.flags = 0;
|
|
|
|
info.codeSize = code.size();
|
|
|
|
info.pCode = code.data();
|
|
|
|
|
2019-04-04 16:10:44 +02:00
|
|
|
if (m_vkd->vkCreateShaderModule(m_vkd->device(), &info, nullptr, &m_stage.module) != VK_SUCCESS)
|
2017-11-20 14:03:00 +01:00
|
|
|
throw DxvkError("DxvkComputePipeline::DxvkComputePipeline: Failed to create shader module");
|
2017-10-11 23:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-07 09:38:31 +01:00
|
|
|
DxvkShaderModule::~DxvkShaderModule() {
|
2019-04-03 20:47:58 +02:00
|
|
|
if (m_vkd != nullptr) {
|
|
|
|
m_vkd->vkDestroyShaderModule(
|
2019-04-04 16:10:44 +02:00
|
|
|
m_vkd->device(), m_stage.module, nullptr);
|
2019-04-03 20:47:58 +02:00
|
|
|
}
|
2017-11-20 14:03:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 20:47:58 +02:00
|
|
|
DxvkShaderModule& DxvkShaderModule::operator = (DxvkShaderModule&& other) {
|
2019-04-04 16:10:44 +02:00
|
|
|
this->m_vkd = std::move(other.m_vkd);
|
|
|
|
this->m_stage = other.m_stage;
|
|
|
|
other.m_stage = VkPipelineShaderStageCreateInfo();
|
2019-04-03 20:47:58 +02:00
|
|
|
return *this;
|
2017-10-14 23:52:47 +02:00
|
|
|
}
|
2019-04-03 20:47:58 +02:00
|
|
|
|
|
|
|
|
2017-12-07 09:38:31 +01:00
|
|
|
DxvkShader::DxvkShader(
|
2017-12-08 18:14:05 +01:00
|
|
|
VkShaderStageFlagBits stage,
|
|
|
|
uint32_t slotCount,
|
|
|
|
const DxvkResourceSlot* slotInfos,
|
2018-01-12 14:25:26 +01:00
|
|
|
const DxvkInterfaceSlots& iface,
|
2019-04-04 02:51:16 +02:00
|
|
|
SpirvCodeBuffer code,
|
2018-06-23 20:19:46 +02:00
|
|
|
const DxvkShaderOptions& options,
|
2018-07-30 20:15:19 +02:00
|
|
|
DxvkShaderConstData&& constData)
|
|
|
|
: m_stage(stage), m_code(code), m_interface(iface),
|
2018-06-23 20:19:46 +02:00
|
|
|
m_options(options), m_constData(std::move(constData)) {
|
2018-05-03 20:07:42 +02:00
|
|
|
// Write back resource slot infos
|
2017-12-07 09:38:31 +01:00
|
|
|
for (uint32_t i = 0; i < slotCount; i++)
|
|
|
|
m_slots.push_back(slotInfos[i]);
|
2018-04-10 12:48:46 +02:00
|
|
|
|
|
|
|
// Gather the offsets where the binding IDs
|
|
|
|
// are stored so we can quickly remap them.
|
2018-11-16 20:00:28 +01:00
|
|
|
uint32_t o1VarId = 0;
|
|
|
|
|
2019-04-04 02:51:16 +02:00
|
|
|
for (auto ins : code) {
|
2018-11-16 20:00:28 +01:00
|
|
|
if (ins.opCode() == spv::OpDecorate) {
|
|
|
|
if (ins.arg(2) == spv::DecorationBinding
|
|
|
|
|| ins.arg(2) == spv::DecorationSpecId)
|
|
|
|
m_idOffsets.push_back(ins.offset() + 3);
|
|
|
|
|
|
|
|
if (ins.arg(2) == spv::DecorationLocation && ins.arg(3) == 1) {
|
|
|
|
m_o1LocOffset = ins.offset() + 3;
|
|
|
|
o1VarId = ins.arg(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ins.arg(2) == spv::DecorationIndex && ins.arg(1) == o1VarId)
|
|
|
|
m_o1IdxOffset = ins.offset() + 3;
|
|
|
|
}
|
2019-04-04 02:46:59 +02:00
|
|
|
|
|
|
|
if (ins.opCode() == spv::OpCapability)
|
|
|
|
m_capabilities.push_back(spv::Capability(ins.arg(1)));
|
2018-04-10 12:48:46 +02:00
|
|
|
}
|
2017-12-07 09:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DxvkShader::~DxvkShader() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-08 21:49:30 +02:00
|
|
|
bool DxvkShader::hasCapability(spv::Capability cap) {
|
2019-04-04 02:46:59 +02:00
|
|
|
auto entry = std::find(
|
|
|
|
m_capabilities.begin(),
|
|
|
|
m_capabilities.end(), cap);
|
2018-04-08 21:49:30 +02:00
|
|
|
|
2019-04-04 02:46:59 +02:00
|
|
|
return entry != m_capabilities.end();
|
2018-04-08 21:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-07 09:38:31 +01:00
|
|
|
void DxvkShader::defineResourceSlots(
|
|
|
|
DxvkDescriptorSlotMapping& mapping) const {
|
|
|
|
for (const auto& slot : m_slots)
|
2019-06-23 15:49:29 +02:00
|
|
|
mapping.defineSlot(m_stage, slot);
|
2019-05-07 20:27:26 +02:00
|
|
|
|
|
|
|
if (m_interface.pushConstSize) {
|
|
|
|
mapping.definePushConstRange(m_stage,
|
|
|
|
m_interface.pushConstOffset,
|
|
|
|
m_interface.pushConstSize);
|
|
|
|
}
|
2017-12-07 09:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 20:47:58 +02:00
|
|
|
DxvkShaderModule DxvkShader::createShaderModule(
|
2017-12-08 18:14:05 +01:00
|
|
|
const Rc<vk::DeviceFn>& vkd,
|
2018-11-16 20:00:28 +01:00
|
|
|
const DxvkDescriptorSlotMapping& mapping,
|
|
|
|
const DxvkShaderModuleCreateInfo& info) {
|
2019-04-04 02:51:16 +02:00
|
|
|
SpirvCodeBuffer spirvCode = m_code.decompress();
|
2018-11-16 20:00:28 +01:00
|
|
|
uint32_t* code = spirvCode.data();
|
2017-12-08 22:30:41 +01:00
|
|
|
|
2018-04-10 12:48:46 +02:00
|
|
|
// Remap resource binding IDs
|
2018-09-01 20:27:36 +02:00
|
|
|
for (uint32_t ofs : m_idOffsets) {
|
|
|
|
if (code[ofs] < MaxNumResourceSlots)
|
|
|
|
code[ofs] = mapping.getBindingId(code[ofs]);
|
|
|
|
}
|
2018-11-16 20:00:28 +01:00
|
|
|
|
|
|
|
// For dual-source blending we need to re-map
|
|
|
|
// location 1, index 0 to location 0, index 1
|
|
|
|
if (info.fsDualSrcBlend && m_o1IdxOffset && m_o1LocOffset)
|
|
|
|
std::swap(code[m_o1IdxOffset], code[m_o1LocOffset]);
|
2017-12-08 22:30:41 +01:00
|
|
|
|
2019-04-03 20:47:58 +02:00
|
|
|
return DxvkShaderModule(vkd, this, spirvCode);
|
2017-12-08 18:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-23 18:17:16 +01:00
|
|
|
void DxvkShader::dump(std::ostream& outputStream) const {
|
2019-04-04 02:51:16 +02:00
|
|
|
m_code.decompress().store(outputStream);
|
2017-12-07 09:38:31 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 23:28:06 +02:00
|
|
|
}
|