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-01-12 00:06:54 +01:00
|
|
|
const std::string dumpPath = env::getEnvVar(L"DXVK_SHADER_DUMP_PATH");
|
2017-12-06 18:54:01 +01:00
|
|
|
|
|
|
|
if (dumpPath.size() != 0) {
|
2018-09-18 10:21:18 +02:00
|
|
|
reader.store(std::ofstream(str::format(dumpPath, "/", name, ".dxbc"),
|
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
|
|
|
|
&& module.programInfo().type() != DxbcProgramType::GeometryShader;
|
|
|
|
|
|
|
|
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(
|
2018-09-18 10:21:18 +02:00
|
|
|
str::format(dumpPath, "/", name, ".spv"),
|
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
|
|
|
|
if (m_shader->shaderConstants().data() != nullptr) {
|
|
|
|
DxvkBufferCreateInfo info;
|
|
|
|
info.size = m_shader->shaderConstants().sizeInBytes();
|
|
|
|
info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
|
|
|
|
info.stages = util::pipelineStages(m_shader->stage())
|
|
|
|
| VK_PIPELINE_STAGE_HOST_BIT;
|
|
|
|
info.access = VK_ACCESS_UNIFORM_READ_BIT
|
|
|
|
| VK_ACCESS_HOST_WRITE_BIT;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
std::memcpy(m_buffer->mapPtr(0),
|
|
|
|
m_shader->shaderConstants().data(),
|
|
|
|
m_shader->shaderConstants().sizeInBytes());
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
2018-07-30 19:34:48 +02:00
|
|
|
D3D11CommonShader D3D11ShaderModuleSet::GetShaderModule(
|
2018-07-30 20:15:19 +02:00
|
|
|
D3D11Device* pDevice,
|
2018-10-25 11:28:02 +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-10-25 11:28:02 +02:00
|
|
|
size_t BytecodeLength) {
|
|
|
|
// Use the shader's unique key for the lookup
|
2018-04-06 17:54:02 +02:00
|
|
|
{ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
2018-10-25 11:28:02 +02:00
|
|
|
auto entry = m_modules.find(*pShaderKey);
|
2018-04-06 17:54:02 +02:00
|
|
|
if (entry != m_modules.end())
|
|
|
|
return entry->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-10-25 11:28:02 +02:00
|
|
|
D3D11CommonShader module(pDevice, pShaderKey,
|
2018-07-30 20:15:19 +02:00
|
|
|
pDxbcModuleInfo, pShaderBytecode, BytecodeLength);
|
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.
|
|
|
|
{ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
2018-10-25 11:28:02 +02:00
|
|
|
auto status = m_modules.insert({ *pShaderKey, module });
|
2018-04-06 17:54:02 +02:00
|
|
|
if (!status.second)
|
|
|
|
return status.first->second;
|
2017-12-06 18:54:01 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 17:54:02 +02:00
|
|
|
return module;
|
2017-12-06 18:54:01 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 14:16:14 +01:00
|
|
|
}
|