2017-12-07 10:12:48 +01:00
|
|
|
#include "d3d11_device.h"
|
2017-12-06 14:16:14 +01:00
|
|
|
#include "d3d11_shader.h"
|
|
|
|
|
|
|
|
namespace dxvk {
|
|
|
|
|
2018-07-30 19:34:48 +02:00
|
|
|
D3D11CommonShader:: D3D11CommonShader() { }
|
|
|
|
D3D11CommonShader::~D3D11CommonShader() { }
|
2017-12-06 18:54:01 +01:00
|
|
|
|
|
|
|
|
2018-07-30 19:34:48 +02:00
|
|
|
D3D11CommonShader::D3D11CommonShader(
|
2018-07-30 20:15:19 +02:00
|
|
|
D3D11Device* pDevice,
|
2018-09-18 10:17:32 +02:00
|
|
|
const DxvkShaderKey* pShaderKey,
|
2018-06-23 17:14:35 +02:00
|
|
|
const DxbcModuleInfo* pDxbcModuleInfo,
|
2018-04-06 17:54:02 +02:00
|
|
|
const void* pShaderBytecode,
|
2018-09-18 10:21:18 +02:00
|
|
|
size_t BytecodeLength) {
|
|
|
|
const std::string name = pShaderKey->toString();
|
|
|
|
Logger::debug(str::format("Compiling shader ", name));
|
2018-04-06 17:54:02 +02:00
|
|
|
|
2017-12-06 18:54:01 +01:00
|
|
|
DxbcReader reader(
|
|
|
|
reinterpret_cast<const char*>(pShaderBytecode),
|
|
|
|
BytecodeLength);
|
|
|
|
|
|
|
|
DxbcModule module(reader);
|
|
|
|
|
|
|
|
// If requested by the user, dump both the raw DXBC
|
|
|
|
// shader and the compiled SPIR-V module to a file.
|
2018-11-15 23:12:09 +01:00
|
|
|
const std::string dumpPath = env::getEnvVar("DXVK_SHADER_DUMP_PATH");
|
2017-12-06 18:54:01 +01:00
|
|
|
|
|
|
|
if (dumpPath.size() != 0) {
|
2020-09-10 05:02:53 +02:00
|
|
|
reader.store(std::ofstream(str::tows(str::format(dumpPath, "/", name, ".dxbc").c_str()).c_str(),
|
2017-12-06 18:54:01 +01:00
|
|
|
std::ios_base::binary | std::ios_base::trunc));
|
2017-12-18 16:41:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 22:46:10 +02:00
|
|
|
// Decide whether we need to create a pass-through
|
|
|
|
// geometry shader for vertex shader stream output
|
|
|
|
bool passthroughShader = pDxbcModuleInfo->xfb != nullptr
|
2021-01-08 01:55:38 +01:00
|
|
|
&& (module.programInfo().type() == DxbcProgramType::VertexShader
|
|
|
|
|| module.programInfo().type() == DxbcProgramType::DomainShader);
|
|
|
|
|
|
|
|
if (module.programInfo().shaderStage() != pShaderKey->type() && !passthroughShader)
|
|
|
|
throw DxvkError("Mismatching shader type.");
|
2018-07-25 22:46:10 +02:00
|
|
|
|
|
|
|
m_shader = passthroughShader
|
|
|
|
? module.compilePassthroughShader(*pDxbcModuleInfo, name)
|
|
|
|
: module.compile (*pDxbcModuleInfo, name);
|
2018-09-18 10:21:18 +02:00
|
|
|
m_shader->setShaderKey(*pShaderKey);
|
2018-02-07 16:44:30 +01:00
|
|
|
|
2017-12-18 16:41:05 +01:00
|
|
|
if (dumpPath.size() != 0) {
|
2018-03-23 18:17:16 +01:00
|
|
|
std::ofstream dumpStream(
|
2020-09-10 05:02:53 +02:00
|
|
|
str::tows(str::format(dumpPath, "/", name, ".spv").c_str()).c_str(),
|
2018-03-23 18:17:16 +01:00
|
|
|
std::ios_base::binary | std::ios_base::trunc);
|
|
|
|
|
|
|
|
m_shader->dump(dumpStream);
|
2017-12-06 18:54:01 +01:00
|
|
|
}
|
2017-12-10 12:21:33 +01:00
|
|
|
|
2018-07-30 20:24:53 +02:00
|
|
|
// Create shader constant buffer if necessary
|
2022-04-09 13:51:36 +02:00
|
|
|
const DxvkShaderCreateInfo& shaderInfo = m_shader->info();
|
|
|
|
|
|
|
|
if (shaderInfo.uniformSize) {
|
2018-07-30 20:24:53 +02:00
|
|
|
DxvkBufferCreateInfo info;
|
2022-04-09 13:51:36 +02:00
|
|
|
info.size = shaderInfo.uniformSize;
|
2018-07-30 20:24:53 +02:00
|
|
|
info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
2022-04-09 13:51:36 +02:00
|
|
|
info.stages = util::pipelineStages(shaderInfo.stage);
|
2019-10-24 16:57:06 +02:00
|
|
|
info.access = VK_ACCESS_UNIFORM_READ_BIT;
|
2018-07-30 20:24:53 +02:00
|
|
|
|
|
|
|
VkMemoryPropertyFlags memFlags
|
|
|
|
= VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
|
|
|
|
| VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
|
|
|
| VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
|
|
|
|
|
|
|
m_buffer = pDevice->GetDXVKDevice()->createBuffer(info, memFlags);
|
2022-04-09 13:51:36 +02:00
|
|
|
std::memcpy(m_buffer->mapPtr(0), shaderInfo.uniformData, shaderInfo.uniformSize);
|
2018-07-30 20:24:53 +02:00
|
|
|
}
|
2018-09-21 23:24:01 +02:00
|
|
|
|
|
|
|
pDevice->GetDXVKDevice()->registerShader(m_shader);
|
2017-12-06 18:54:01 +01:00
|
|
|
}
|
2018-07-30 19:34:48 +02:00
|
|
|
|
2017-12-06 18:54:01 +01:00
|
|
|
|
2018-04-06 17:54:02 +02:00
|
|
|
D3D11ShaderModuleSet:: D3D11ShaderModuleSet() { }
|
|
|
|
D3D11ShaderModuleSet::~D3D11ShaderModuleSet() { }
|
2017-12-06 18:54:01 +01:00
|
|
|
|
|
|
|
|
2019-10-21 12:09:53 +02:00
|
|
|
HRESULT D3D11ShaderModuleSet::GetShaderModule(
|
|
|
|
D3D11Device* pDevice,
|
|
|
|
const DxvkShaderKey* pShaderKey,
|
|
|
|
const DxbcModuleInfo* pDxbcModuleInfo,
|
|
|
|
const void* pShaderBytecode,
|
|
|
|
size_t BytecodeLength,
|
|
|
|
D3D11CommonShader* pShader) {
|
2018-10-25 11:28:02 +02:00
|
|
|
// Use the shader's unique key for the lookup
|
2021-06-28 19:19:29 +02:00
|
|
|
{ std::unique_lock<dxvk::mutex> lock(m_mutex);
|
2018-04-06 17:54:02 +02:00
|
|
|
|
2018-10-25 11:28:02 +02:00
|
|
|
auto entry = m_modules.find(*pShaderKey);
|
2019-10-21 12:09:53 +02:00
|
|
|
if (entry != m_modules.end()) {
|
|
|
|
*pShader = entry->second;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2018-04-06 17:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This shader has not been compiled yet, so we have to create a
|
|
|
|
// new module. This takes a while, so we won't lock the structure.
|
2019-10-21 12:09:53 +02:00
|
|
|
D3D11CommonShader module;
|
|
|
|
|
|
|
|
try {
|
|
|
|
module = D3D11CommonShader(pDevice, pShaderKey,
|
|
|
|
pDxbcModuleInfo, pShaderBytecode, BytecodeLength);
|
|
|
|
} catch (const DxvkError& e) {
|
|
|
|
Logger::err(e.message());
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
2017-12-06 18:54:01 +01:00
|
|
|
|
2018-04-06 17:54:02 +02:00
|
|
|
// Insert the new module into the lookup table. If another thread
|
|
|
|
// has compiled the same shader in the meantime, we should return
|
|
|
|
// that object instead and discard the newly created module.
|
2021-06-28 19:19:29 +02:00
|
|
|
{ std::unique_lock<dxvk::mutex> lock(m_mutex);
|
2018-04-06 17:54:02 +02:00
|
|
|
|
2018-10-25 11:28:02 +02:00
|
|
|
auto status = m_modules.insert({ *pShaderKey, module });
|
2019-10-21 12:09:53 +02:00
|
|
|
if (!status.second) {
|
|
|
|
*pShader = status.first->second;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2017-12-06 18:54:01 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:09:53 +02:00
|
|
|
*pShader = std::move(module);
|
|
|
|
return S_OK;
|
2017-12-06 18:54:01 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 14:16:14 +01:00
|
|
|
}
|