[D3D12] Load D3D dynamically

This commit is contained in:
Triang3l 2018-10-23 19:21:01 +03:00
parent b81bb4d68c
commit 43f34d804a
14 changed files with 154 additions and 64 deletions

View File

@ -180,9 +180,6 @@ filter("platforms:Windows")
"comctl32",
"shcore",
"shlwapi",
"d3d12",
"d3dcompiler",
"dxgi",
"dxguid",
})

View File

@ -367,7 +367,7 @@ ID3D12RootSignature* D3D12CommandProcessor::GetRootSignature(
}
ID3D12RootSignature* root_signature = ui::d3d12::util::CreateRootSignature(
GetD3D12Context()->GetD3D12Provider()->GetDevice(), desc);
GetD3D12Context()->GetD3D12Provider(), desc);
if (root_signature == nullptr) {
XELOGE(
"Failed to create a root signature with %u pixel textures, %u pixel "

View File

@ -41,8 +41,8 @@ X_STATUS D3D12GraphicsSystem::Setup(cpu::Processor* processor,
kernel::KernelState* kernel_state,
ui::Window* target_window) {
provider_ = xe::ui::d3d12::D3D12Provider::Create(target_window);
auto device =
static_cast<xe::ui::d3d12::D3D12Provider*>(provider())->GetDevice();
auto d3d12_provider = static_cast<xe::ui::d3d12::D3D12Provider*>(provider());
auto device = d3d12_provider->GetDevice();
auto result = GraphicsSystem::Setup(processor, kernel_state, target_window);
if (result != X_STATUS_SUCCESS) {
@ -93,7 +93,7 @@ X_STATUS D3D12GraphicsSystem::Setup(cpu::Processor* processor,
stretch_root_desc.Flags =
D3D12_ROOT_SIGNATURE_FLAG_DENY_VERTEX_SHADER_ROOT_ACCESS;
stretch_root_signature_ =
ui::d3d12::util::CreateRootSignature(device, stretch_root_desc);
ui::d3d12::util::CreateRootSignature(d3d12_provider, stretch_root_desc);
if (stretch_root_signature_ == nullptr) {
XELOGE("Failed to create the front buffer stretch root signature");
return X_STATUS_UNSUCCESSFUL;
@ -120,7 +120,7 @@ X_STATUS D3D12GraphicsSystem::Setup(cpu::Processor* processor,
stretch_root_desc.NumParameters = 3;
stretch_root_desc.pParameters = stretch_root_parameters;
stretch_gamma_root_signature_ =
ui::d3d12::util::CreateRootSignature(device, stretch_root_desc);
ui::d3d12::util::CreateRootSignature(d3d12_provider, stretch_root_desc);
if (stretch_gamma_root_signature_ == nullptr) {
XELOGE(
"Failed to create the gamma-correcting front buffer stretch root "

View File

@ -60,16 +60,16 @@ void D3D12Shader::SetTexturesAndSamplers(
}
}
bool D3D12Shader::DisassembleDXBC() {
bool D3D12Shader::DisassembleDXBC(const ui::d3d12::D3D12Provider* provider) {
if (!host_disassembly_.empty()) {
return true;
}
ID3DBlob* blob;
if (FAILED(D3DDisassemble(translated_binary().data(),
translated_binary().size(),
D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING |
D3D_DISASM_ENABLE_INSTRUCTION_OFFSET,
nullptr, &blob))) {
if (FAILED(provider->Disassemble(translated_binary().data(),
translated_binary().size(),
D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING |
D3D_DISASM_ENABLE_INSTRUCTION_OFFSET,
nullptr, &blob))) {
return false;
}
host_disassembly_ = reinterpret_cast<const char*>(blob->GetBufferPointer());

View File

@ -14,6 +14,7 @@
#include "xenia/gpu/dxbc_shader_translator.h"
#include "xenia/gpu/shader.h"
#include "xenia/ui/d3d12/d3d12_provider.h"
namespace xe {
namespace gpu {
@ -30,7 +31,7 @@ class D3D12Shader : public Shader {
const DxbcShaderTranslator::SamplerBinding* sampler_bindings,
uint32_t sampler_binding_count);
bool DisassembleDXBC();
bool DisassembleDXBC(const ui::d3d12::D3D12Provider* provider);
static constexpr uint32_t kMaxTextureSRVIndexBits =
DxbcShaderTranslator::kMaxTextureSRVIndexBits;

View File

@ -224,7 +224,8 @@ bool PipelineCache::TranslateShader(D3D12Shader* shader,
// Disassemble the shader for dumping.
if (FLAGS_d3d12_dxbc_disasm) {
if (!shader->DisassembleDXBC()) {
auto provider = command_processor_->GetD3D12Context()->GetD3D12Provider();
if (!shader->DisassembleDXBC(provider)) {
XELOGE("Failed to disassemble DXBC shader %.16" PRIX64,
shader->ucode_data_hash());
}

View File

@ -76,8 +76,8 @@ RenderTargetCache::RenderTargetCache(D3D12CommandProcessor* command_processor,
RenderTargetCache::~RenderTargetCache() { Shutdown(); }
bool RenderTargetCache::Initialize() {
auto device =
command_processor_->GetD3D12Context()->GetD3D12Provider()->GetDevice();
auto provider = command_processor_->GetD3D12Context()->GetD3D12Provider();
auto device = provider->GetDevice();
// Create the buffer for reinterpreting EDRAM contents.
D3D12_RESOURCE_DESC edram_buffer_desc;
@ -132,7 +132,7 @@ bool RenderTargetCache::Initialize() {
load_store_root_desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
edram_load_store_root_signature_ =
ui::d3d12::util::CreateRootSignature(device, load_store_root_desc);
ui::d3d12::util::CreateRootSignature(provider, load_store_root_desc);
if (edram_load_store_root_signature_ == nullptr) {
XELOGE("Failed to create the EDRAM load/store root signature");
Shutdown();
@ -142,7 +142,7 @@ bool RenderTargetCache::Initialize() {
load_store_root_parameters[1].DescriptorTable.NumDescriptorRanges = 1;
++load_store_root_parameters[1].DescriptorTable.pDescriptorRanges;
edram_clear_root_signature_ =
ui::d3d12::util::CreateRootSignature(device, load_store_root_desc);
ui::d3d12::util::CreateRootSignature(provider, load_store_root_desc);
if (edram_clear_root_signature_ == nullptr) {
XELOGE("Failed to create the EDRAM buffer clear root signature");
Shutdown();
@ -265,7 +265,7 @@ bool RenderTargetCache::Initialize() {
resolve_root_desc.Flags =
D3D12_ROOT_SIGNATURE_FLAG_DENY_VERTEX_SHADER_ROOT_ACCESS;
resolve_root_signature_ =
ui::d3d12::util::CreateRootSignature(device, resolve_root_desc);
ui::d3d12::util::CreateRootSignature(provider, resolve_root_desc);
if (resolve_root_signature_ == nullptr) {
XELOGE("Failed to create the converting resolve root signature");
Shutdown();

View File

@ -369,8 +369,8 @@ TextureCache::TextureCache(D3D12CommandProcessor* command_processor,
TextureCache::~TextureCache() { Shutdown(); }
bool TextureCache::Initialize() {
auto device =
command_processor_->GetD3D12Context()->GetD3D12Provider()->GetDevice();
auto provider = command_processor_->GetD3D12Context()->GetD3D12Provider();
auto device = provider->GetDevice();
// Create the loading root signature.
D3D12_ROOT_PARAMETER root_parameters[2];
@ -402,7 +402,7 @@ bool TextureCache::Initialize() {
root_signature_desc.pStaticSamplers = nullptr;
root_signature_desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
load_root_signature_ =
ui::d3d12::util::CreateRootSignature(device, root_signature_desc);
ui::d3d12::util::CreateRootSignature(provider, root_signature_desc);
if (load_root_signature_ == nullptr) {
XELOGE("Failed to create the texture loading root signature");
Shutdown();
@ -416,7 +416,7 @@ bool TextureCache::Initialize() {
root_parameters[0].Constants.Num32BitValues =
sizeof(ResolveTileConstants) / sizeof(uint32_t);
resolve_tile_root_signature_ =
ui::d3d12::util::CreateRootSignature(device, root_signature_desc);
ui::d3d12::util::CreateRootSignature(provider, root_signature_desc);
if (resolve_tile_root_signature_ == nullptr) {
XELOGE("Failed to create the texture tiling root signature");
Shutdown();

View File

@ -124,13 +124,21 @@ int shader_compiler_main(const std::vector<std::wstring>& args) {
#if XE_PLATFORM_WIN32
ID3DBlob* dxbc_disasm_blob = nullptr;
if (FLAGS_shader_output_type == "dxbc") {
// Diassemble DXBC.
if (SUCCEEDED(D3DDisassemble(source_data, source_data_size,
D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING |
D3D_DISASM_ENABLE_INSTRUCTION_OFFSET,
nullptr, &dxbc_disasm_blob))) {
source_data = dxbc_disasm_blob->GetBufferPointer();
source_data_size = dxbc_disasm_blob->GetBufferSize();
HMODULE d3d_compiler = LoadLibrary(L"D3DCompiler_47.dll");
if (d3d_compiler != nullptr) {
pD3DDisassemble d3d_disassemble =
pD3DDisassemble(GetProcAddress(d3d_compiler, "D3DDisassemble"));
if (d3d_disassemble != nullptr) {
// Disassemble DXBC.
if (SUCCEEDED(d3d_disassemble(source_data, source_data_size,
D3D_DISASM_ENABLE_INSTRUCTION_NUMBERING |
D3D_DISASM_ENABLE_INSTRUCTION_OFFSET,
nullptr, &dxbc_disasm_blob))) {
source_data = dxbc_disasm_blob->GetBufferPointer();
source_data_size = dxbc_disasm_blob->GetBufferSize();
}
}
FreeLibrary(d3d_compiler);
}
}
#endif // XE_PLATFORM_WIN32

View File

@ -171,7 +171,7 @@ bool D3D12ImmediateDrawer::Initialize() {
root_signature_desc.pStaticSamplers = nullptr;
root_signature_desc.Flags =
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
root_signature_ = util::CreateRootSignature(device, root_signature_desc);
root_signature_ = util::CreateRootSignature(provider, root_signature_desc);
if (root_signature_ == nullptr) {
XELOGE("Failed to create the immediate drawer root signature");
Shutdown();

View File

@ -25,15 +25,18 @@ namespace d3d12 {
std::unique_ptr<D3D12Provider> D3D12Provider::Create(Window* main_window) {
std::unique_ptr<D3D12Provider> provider(new D3D12Provider(main_window));
if (!provider->Initialize()) {
xe::FatalError(
"Unable to initialize Direct3D 12 graphics subsystem.\n"
"\n"
"Ensure that you have the latest drivers for your GPU and it supports "
"Direct3D 12 feature level 11_0.\n"
"\n"
"See http://xenia.jp/faq/ for more information and a list of supported "
"GPUs.");
InitializationResult result = provider->Initialize();
if (result != InitializationResult::kSucceeded) {
if (result != InitializationResult::kLibraryLoadFailed) {
xe::FatalError(
"Unable to initialize Direct3D 12 graphics subsystem.\n"
"\n"
"Ensure that you have the latest drivers for your GPU and it "
"supports Direct3D 12 feature level 11_0.\n"
"\n"
"See http://xenia.jp/faq/ for more information and a list of "
"supported GPUs.");
}
return nullptr;
}
return provider;
@ -55,14 +58,57 @@ D3D12Provider::~D3D12Provider() {
if (dxgi_factory_ != nullptr) {
dxgi_factory_->Release();
}
if (library_d3dcompiler_ != nullptr) {
FreeLibrary(library_d3dcompiler_);
}
if (library_d3d12_ != nullptr) {
FreeLibrary(library_d3d12_);
}
if (library_dxgi_ != nullptr) {
FreeLibrary(library_dxgi_);
}
}
bool D3D12Provider::Initialize() {
D3D12Provider::InitializationResult D3D12Provider::Initialize() {
// Load the libraries.
library_dxgi_ = LoadLibrary(L"dxgi.dll");
library_d3d12_ = LoadLibrary(L"D3D12.dll");
library_d3dcompiler_ = LoadLibrary(L"D3DCompiler_47.dll");
if (library_dxgi_ == nullptr || library_d3d12_ == nullptr ||
library_d3dcompiler_ == nullptr) {
return InitializationResult::kLibraryLoadFailed;
}
bool libraries_loaded = true;
libraries_loaded &=
(pfn_create_dxgi_factory2_ = PFNCreateDXGIFactory2(
GetProcAddress(library_dxgi_, "CreateDXGIFactory2"))) != nullptr;
libraries_loaded &=
(pfn_dxgi_get_debug_interface1_ = PFNDXGIGetDebugInterface1(
GetProcAddress(library_dxgi_, "DXGIGetDebugInterface1"))) != nullptr;
libraries_loaded &=
(pfn_d3d12_get_debug_interface_ = PFN_D3D12_GET_DEBUG_INTERFACE(
GetProcAddress(library_d3d12_, "D3D12GetDebugInterface"))) !=
nullptr;
libraries_loaded &=
(pfn_d3d12_create_device_ = PFN_D3D12_CREATE_DEVICE(
GetProcAddress(library_d3d12_, "D3D12CreateDevice"))) != nullptr;
libraries_loaded &=
(pfn_d3d12_serialize_root_signature_ = PFN_D3D12_SERIALIZE_ROOT_SIGNATURE(
GetProcAddress(library_d3d12_, "D3D12SerializeRootSignature"))) !=
nullptr;
libraries_loaded &= (pfn_d3d_disassemble_ = pD3DDisassemble(GetProcAddress(
library_d3dcompiler_, "D3DDisassemble"))) != nullptr;
if (!libraries_loaded) {
return InitializationResult::kLibraryLoadFailed;
}
// Enable the debug layer.
bool debug = FLAGS_d3d12_debug;
if (debug) {
ID3D12Debug* debug_interface;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug_interface)))) {
if (SUCCEEDED(
pfn_d3d12_get_debug_interface_(IID_PPV_ARGS(&debug_interface)))) {
debug_interface->EnableDebugLayer();
debug_interface->Release();
} else {
@ -73,10 +119,10 @@ bool D3D12Provider::Initialize() {
// Create the DXGI factory.
IDXGIFactory2* dxgi_factory;
if (FAILED(CreateDXGIFactory2(debug ? DXGI_CREATE_FACTORY_DEBUG : 0,
IID_PPV_ARGS(&dxgi_factory)))) {
if (FAILED(pfn_create_dxgi_factory2_(debug ? DXGI_CREATE_FACTORY_DEBUG : 0,
IID_PPV_ARGS(&dxgi_factory)))) {
XELOGE("Failed to create a DXGI factory");
return false;
return InitializationResult::kDeviceInitializationFailed;
}
// Choose the adapter and create a device with required features.
@ -86,8 +132,8 @@ bool D3D12Provider::Initialize() {
while (dxgi_factory->EnumAdapters1(adapter_index, &adapter) == S_OK) {
DXGI_ADAPTER_DESC1 adapter_desc;
if (SUCCEEDED(adapter->GetDesc1(&adapter_desc))) {
if (SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0,
_uuidof(ID3D12Device), nullptr))) {
if (SUCCEEDED(pfn_d3d12_create_device_(adapter, D3D_FEATURE_LEVEL_11_0,
_uuidof(ID3D12Device), nullptr))) {
if (FLAGS_d3d12_adapter_index >= 0) {
if (adapter_index == FLAGS_d3d12_adapter_index) {
break;
@ -110,15 +156,15 @@ bool D3D12Provider::Initialize() {
if (adapter == nullptr) {
XELOGE("Failed to get an adapter supporting Direct3D feature level 11_0.");
dxgi_factory->Release();
return false;
return InitializationResult::kDeviceInitializationFailed;
}
ID3D12Device* device;
if (FAILED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0,
IID_PPV_ARGS(&device)))) {
if (FAILED(pfn_d3d12_create_device_(adapter, D3D_FEATURE_LEVEL_11_0,
IID_PPV_ARGS(&device)))) {
XELOGE("Failed to create a Direct3D 12 feature level 11_0 device.");
adapter->Release();
dxgi_factory->Release();
return false;
return InitializationResult::kDeviceInitializationFailed;
}
adapter->Release();
@ -172,11 +218,11 @@ bool D3D12Provider::Initialize() {
tiled_resources_tier_, programmable_sample_positions_tier_,
rasterizer_ordered_views_supported_ ? "" : "un");
// Get the graphics analysis interface, will silently fail if PIX not
// Get the graphics analysis interface, will silently fail if PIX is not
// attached.
DXGIGetDebugInterface1(0, IID_PPV_ARGS(&graphics_analysis_));
pfn_dxgi_get_debug_interface1_(0, IID_PPV_ARGS(&graphics_analysis_));
return true;
return InitializationResult::kSucceeded;
}
std::unique_ptr<GraphicsContext> D3D12Provider::CreateContext(

View File

@ -70,10 +70,47 @@ class D3D12Provider : public GraphicsProvider {
return programmable_sample_positions_tier_;
}
// Proxies for Direct3D 12 functions since they are loaded dynamically.
inline HRESULT SerializeRootSignature(const D3D12_ROOT_SIGNATURE_DESC* desc,
D3D_ROOT_SIGNATURE_VERSION version,
ID3DBlob** blob_out,
ID3DBlob** error_blob_out) const {
return pfn_d3d12_serialize_root_signature_(desc, version, blob_out,
error_blob_out);
}
inline HRESULT Disassemble(const void* src_data, size_t src_data_size,
UINT flags, const char* comments,
ID3DBlob** disassembly_out) const {
return pfn_d3d_disassemble_(src_data, src_data_size, flags, comments,
disassembly_out);
}
private:
explicit D3D12Provider(Window* main_window);
bool Initialize();
enum InitializationResult : uint32_t {
kSucceeded,
kDeviceInitializationFailed,
kLibraryLoadFailed,
};
InitializationResult Initialize();
HMODULE library_dxgi_ = nullptr;
HMODULE library_d3d12_ = nullptr;
HMODULE library_d3dcompiler_ = nullptr;
typedef HRESULT(WINAPI* PFNCreateDXGIFactory2)(UINT Flags, REFIID riid,
_COM_Outptr_ void** ppFactory);
typedef HRESULT(WINAPI* PFNDXGIGetDebugInterface1)(
UINT Flags, REFIID riid, _COM_Outptr_ void** pDebug);
PFNCreateDXGIFactory2 pfn_create_dxgi_factory2_;
PFNDXGIGetDebugInterface1 pfn_dxgi_get_debug_interface1_;
PFN_D3D12_GET_DEBUG_INTERFACE pfn_d3d12_get_debug_interface_;
PFN_D3D12_CREATE_DEVICE pfn_d3d12_create_device_;
PFN_D3D12_SERIALIZE_ROOT_SIGNATURE pfn_d3d12_serialize_root_signature_;
pD3DDisassemble pfn_d3d_disassemble_;
IDXGIFactory2* dxgi_factory_ = nullptr;
IDXGraphicsAnalysis* graphics_analysis_ = nullptr;

View File

@ -21,11 +21,11 @@ const D3D12_HEAP_PROPERTIES kHeapPropertiesDefault = {D3D12_HEAP_TYPE_DEFAULT};
const D3D12_HEAP_PROPERTIES kHeapPropertiesUpload = {D3D12_HEAP_TYPE_UPLOAD};
ID3D12RootSignature* CreateRootSignature(
ID3D12Device* device, const D3D12_ROOT_SIGNATURE_DESC& desc) {
D3D12Provider* provider, const D3D12_ROOT_SIGNATURE_DESC& desc) {
ID3DBlob* blob;
ID3DBlob* error_blob = nullptr;
if (FAILED(D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1,
&blob, &error_blob))) {
if (FAILED(provider->SerializeRootSignature(
&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error_blob))) {
XELOGE("Failed to serialize a root signature");
if (error_blob != nullptr) {
XELOGE("%s",
@ -38,9 +38,9 @@ ID3D12RootSignature* CreateRootSignature(
error_blob->Release();
}
ID3D12RootSignature* root_signature = nullptr;
device->CreateRootSignature(0, blob->GetBufferPointer(),
blob->GetBufferSize(),
IID_PPV_ARGS(&root_signature));
provider->GetDevice()->CreateRootSignature(0, blob->GetBufferPointer(),
blob->GetBufferSize(),
IID_PPV_ARGS(&root_signature));
blob->Release();
return root_signature;
}

View File

@ -30,7 +30,7 @@ inline bool ReleaseAndNull(T& object) {
return false;
};
ID3D12RootSignature* CreateRootSignature(ID3D12Device* device,
ID3D12RootSignature* CreateRootSignature(D3D12Provider* provider,
const D3D12_ROOT_SIGNATURE_DESC& desc);
ID3D12PipelineState* CreateComputePipeline(ID3D12Device* device,