2017-10-11 15:32:24 +02:00
|
|
|
#include <array>
|
|
|
|
|
2017-12-12 12:50:52 +01:00
|
|
|
#include "../dxgi/dxgi_adapter.h"
|
2017-10-11 09:51:48 +02:00
|
|
|
|
2018-12-03 21:18:40 +01:00
|
|
|
#include "../dxvk/dxvk_instance.h"
|
|
|
|
|
2017-10-11 09:51:48 +02:00
|
|
|
#include "d3d11_device.h"
|
2017-12-02 16:47:06 +01:00
|
|
|
#include "d3d11_enums.h"
|
2018-04-20 00:19:03 +02:00
|
|
|
#include "d3d11_interop.h"
|
2018-03-28 21:24:52 +02:00
|
|
|
#include "d3d11_present.h"
|
2017-10-11 09:51:48 +02:00
|
|
|
|
2017-12-06 23:36:11 +01:00
|
|
|
namespace dxvk {
|
|
|
|
Logger Logger::s_instance("d3d11.log");
|
|
|
|
}
|
|
|
|
|
2017-10-11 09:51:48 +02:00
|
|
|
extern "C" {
|
2017-10-11 15:32:24 +02:00
|
|
|
using namespace dxvk;
|
2017-10-11 09:51:48 +02:00
|
|
|
|
2018-05-07 14:06:52 +02:00
|
|
|
DLLEXPORT HRESULT __stdcall D3D11CoreCreateDevice(
|
|
|
|
IDXGIFactory* pFactory,
|
|
|
|
IDXGIAdapter* pAdapter,
|
2017-10-11 09:51:48 +02:00
|
|
|
UINT Flags,
|
2018-05-07 14:06:52 +02:00
|
|
|
const D3D_FEATURE_LEVEL* pFeatureLevels,
|
2017-10-11 09:51:48 +02:00
|
|
|
UINT FeatureLevels,
|
2018-05-07 14:06:52 +02:00
|
|
|
ID3D11Device** ppDevice) {
|
2018-12-03 21:18:40 +01:00
|
|
|
Rc<DxvkAdapter> dxvkAdapter;
|
|
|
|
Rc<DxvkInstance> dxvkInstance;
|
|
|
|
|
|
|
|
Com<IDXGIVkAdapter> dxgiVkAdapter;
|
2017-10-11 15:32:24 +02:00
|
|
|
|
2018-12-03 21:18:40 +01:00
|
|
|
// Try to find the corresponding Vulkan device for the DXGI adapter
|
|
|
|
if (SUCCEEDED(pAdapter->QueryInterface(__uuidof(IDXGIVkAdapter), reinterpret_cast<void**>(&dxgiVkAdapter)))) {
|
|
|
|
dxvkAdapter = dxgiVkAdapter->GetDXVKAdapter();
|
|
|
|
dxvkInstance = dxvkAdapter->instance();
|
|
|
|
} else {
|
|
|
|
Logger::warn("D3D11CoreCreateDevice: Adapter is not a DXVK adapter");
|
|
|
|
DXGI_ADAPTER_DESC desc;
|
|
|
|
pAdapter->GetDesc(&desc);
|
|
|
|
|
|
|
|
dxvkInstance = new DxvkInstance();
|
|
|
|
dxvkAdapter = dxvkInstance->findAdapterByLuid(&desc.AdapterLuid);
|
|
|
|
|
|
|
|
if (dxvkAdapter == nullptr)
|
|
|
|
dxvkAdapter = dxvkInstance->findAdapterByDeviceId(desc.VendorId, desc.DeviceId);
|
|
|
|
|
|
|
|
if (dxvkAdapter == nullptr)
|
|
|
|
dxvkAdapter = dxvkInstance->enumAdapters(0);
|
|
|
|
|
|
|
|
if (dxvkAdapter == nullptr)
|
|
|
|
return E_FAIL;
|
2017-10-11 15:32:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Feature levels to probe if the
|
|
|
|
// application does not specify any.
|
|
|
|
std::array<D3D_FEATURE_LEVEL, 6> defaultFeatureLevels = {
|
|
|
|
D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1,
|
|
|
|
D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3,
|
|
|
|
D3D_FEATURE_LEVEL_9_2, D3D_FEATURE_LEVEL_9_1,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (pFeatureLevels == nullptr || FeatureLevels == 0) {
|
|
|
|
pFeatureLevels = defaultFeatureLevels.data();
|
|
|
|
FeatureLevels = defaultFeatureLevels.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the highest feature level supported by the device.
|
|
|
|
// This works because the feature level array is ordered.
|
|
|
|
UINT flId;
|
2018-12-03 21:18:40 +01:00
|
|
|
|
2017-10-11 15:32:24 +02:00
|
|
|
for (flId = 0 ; flId < FeatureLevels; flId++) {
|
2018-05-07 14:06:52 +02:00
|
|
|
Logger::info(str::format("D3D11CoreCreateDevice: Probing ", pFeatureLevels[flId]));
|
2017-12-02 19:07:48 +01:00
|
|
|
|
2018-12-03 21:18:40 +01:00
|
|
|
if (D3D11Device::CheckFeatureLevelSupport(dxvkAdapter, pFeatureLevels[flId]))
|
2017-10-11 15:32:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flId == FeatureLevels) {
|
2018-05-07 14:06:52 +02:00
|
|
|
Logger::err("D3D11CoreCreateDevice: Requested feature level not supported");
|
2017-12-09 13:43:14 +01:00
|
|
|
return E_INVALIDARG;
|
2017-10-11 15:32:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create the device with the given parameters.
|
|
|
|
const D3D_FEATURE_LEVEL fl = pFeatureLevels[flId];
|
|
|
|
|
|
|
|
try {
|
2018-05-07 14:06:52 +02:00
|
|
|
Logger::info(str::format("D3D11CoreCreateDevice: Using feature level ", fl));
|
2018-12-03 19:26:29 +01:00
|
|
|
Com<D3D11DXGIDevice> device = new D3D11DXGIDevice(
|
2018-12-03 21:18:40 +01:00
|
|
|
pAdapter, dxvkAdapter.ptr(), fl, Flags);
|
2018-01-18 01:18:22 +01:00
|
|
|
|
2018-12-03 19:26:29 +01:00
|
|
|
return device->QueryInterface(
|
|
|
|
__uuidof(ID3D11Device),
|
|
|
|
reinterpret_cast<void**>(ppDevice));
|
2017-10-11 15:32:24 +02:00
|
|
|
} catch (const DxvkError& e) {
|
2018-05-07 14:06:52 +02:00
|
|
|
Logger::err("D3D11CoreCreateDevice: Failed to create D3D11 device");
|
2017-10-11 15:32:24 +02:00
|
|
|
return E_FAIL;
|
|
|
|
}
|
2017-10-11 09:51:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-07 14:06:52 +02:00
|
|
|
DLLEXPORT HRESULT __stdcall D3D11CreateDevice(
|
|
|
|
IDXGIAdapter* pAdapter,
|
|
|
|
D3D_DRIVER_TYPE DriverType,
|
|
|
|
HMODULE Software,
|
|
|
|
UINT Flags,
|
|
|
|
const D3D_FEATURE_LEVEL* pFeatureLevels,
|
|
|
|
UINT FeatureLevels,
|
|
|
|
UINT SDKVersion,
|
|
|
|
ID3D11Device** ppDevice,
|
|
|
|
D3D_FEATURE_LEVEL* pFeatureLevel,
|
|
|
|
ID3D11DeviceContext** ppImmediateContext) {
|
|
|
|
Com<IDXGIFactory> dxgiFactory = nullptr;
|
|
|
|
Com<IDXGIAdapter> dxgiAdapter = pAdapter;
|
|
|
|
|
|
|
|
if (dxgiAdapter == nullptr) {
|
|
|
|
// We'll treat everything as hardware, even if the
|
|
|
|
// Vulkan device is actually a software device.
|
|
|
|
if (DriverType != D3D_DRIVER_TYPE_HARDWARE)
|
|
|
|
Logger::warn("D3D11CreateDevice: Unsupported driver type");
|
|
|
|
|
|
|
|
// We'll use the first adapter returned by a DXGI factory
|
|
|
|
if (FAILED(CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&dxgiFactory)))) {
|
|
|
|
Logger::err("D3D11CreateDevice: Failed to create a DXGI factory");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(dxgiFactory->EnumAdapters(0, &dxgiAdapter))) {
|
|
|
|
Logger::err("D3D11CreateDevice: No default adapter available");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// We should be able to query the DXGI factory from the adapter
|
|
|
|
if (FAILED(dxgiAdapter->GetParent(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&dxgiFactory)))) {
|
|
|
|
Logger::err("D3D11CreateDevice: Failed to query DXGI factory from DXGI adapter");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In theory we could ignore these, but the Microsoft docs explicitly
|
|
|
|
// state that we need to return E_INVALIDARG in case the arguments are
|
|
|
|
// invalid. Both the driver type and software parameter can only be
|
|
|
|
// set if the adapter itself is unspecified.
|
|
|
|
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476082(v=vs.85).aspx
|
|
|
|
if (DriverType != D3D_DRIVER_TYPE_UNKNOWN || Software != nullptr)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the actual device
|
|
|
|
Com<ID3D11Device> device;
|
|
|
|
|
|
|
|
HRESULT hr = D3D11CoreCreateDevice(
|
|
|
|
dxgiFactory.ptr(), dxgiAdapter.ptr(),
|
|
|
|
Flags, pFeatureLevels, FeatureLevels,
|
|
|
|
&device);
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
// Write back whatever info the application requested
|
|
|
|
if (pFeatureLevel != nullptr)
|
|
|
|
*pFeatureLevel = device->GetFeatureLevel();
|
|
|
|
|
|
|
|
if (ppDevice != nullptr)
|
|
|
|
*ppDevice = device.ref();
|
|
|
|
|
|
|
|
if (ppImmediateContext != nullptr)
|
|
|
|
device->GetImmediateContext(ppImmediateContext);
|
|
|
|
|
|
|
|
// If we were unable to write back the device and the
|
|
|
|
// swap chain, the application has no way of working
|
|
|
|
// with the device so we should report S_FALSE here.
|
|
|
|
if (ppDevice == nullptr && ppImmediateContext == nullptr)
|
|
|
|
return S_FALSE;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-11 09:51:48 +02:00
|
|
|
DLLEXPORT HRESULT __stdcall D3D11CreateDeviceAndSwapChain(
|
2018-05-07 14:06:52 +02:00
|
|
|
IDXGIAdapter* pAdapter,
|
|
|
|
D3D_DRIVER_TYPE DriverType,
|
|
|
|
HMODULE Software,
|
|
|
|
UINT Flags,
|
|
|
|
const D3D_FEATURE_LEVEL* pFeatureLevels,
|
|
|
|
UINT FeatureLevels,
|
|
|
|
UINT SDKVersion,
|
|
|
|
const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
|
|
|
|
IDXGISwapChain** ppSwapChain,
|
|
|
|
ID3D11Device** ppDevice,
|
|
|
|
D3D_FEATURE_LEVEL* pFeatureLevel,
|
|
|
|
ID3D11DeviceContext** ppImmediateContext) {
|
2018-03-03 23:15:15 +01:00
|
|
|
Com<ID3D11Device> d3d11Device;
|
|
|
|
Com<ID3D11DeviceContext> d3d11Context;
|
|
|
|
|
2018-12-02 13:43:55 +01:00
|
|
|
if (ppSwapChain && !pSwapChainDesc)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2017-10-11 15:32:24 +02:00
|
|
|
// Try to create a device first.
|
|
|
|
HRESULT status = D3D11CreateDevice(pAdapter, DriverType,
|
|
|
|
Software, Flags, pFeatureLevels, FeatureLevels,
|
2018-03-03 23:15:15 +01:00
|
|
|
SDKVersion, &d3d11Device, pFeatureLevel, &d3d11Context);
|
2017-10-11 15:32:24 +02:00
|
|
|
|
|
|
|
if (FAILED(status))
|
|
|
|
return status;
|
|
|
|
|
|
|
|
// Again, the documentation does not exactly tell us what we
|
|
|
|
// need to do in case one of the arguments is a null pointer.
|
2018-12-02 13:43:55 +01:00
|
|
|
if (ppSwapChain) {
|
|
|
|
Com<IDXGIDevice> dxgiDevice = nullptr;
|
|
|
|
Com<IDXGIAdapter> dxgiAdapter = nullptr;
|
|
|
|
Com<IDXGIFactory> dxgiFactory = nullptr;
|
|
|
|
|
|
|
|
if (FAILED(d3d11Device->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&dxgiDevice)))) {
|
|
|
|
Logger::err("D3D11CreateDeviceAndSwapChain: Failed to query DXGI device");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(dxgiDevice->GetParent(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&dxgiAdapter)))) {
|
|
|
|
Logger::err("D3D11CreateDeviceAndSwapChain: Failed to query DXGI adapter");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(dxgiAdapter->GetParent(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&dxgiFactory)))) {
|
|
|
|
Logger::err("D3D11CreateDeviceAndSwapChain: Failed to query DXGI factory");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DXGI_SWAP_CHAIN_DESC desc = *pSwapChainDesc;
|
|
|
|
if (FAILED(dxgiFactory->CreateSwapChain(d3d11Device.ptr(), &desc, ppSwapChain))) {
|
|
|
|
Logger::err("D3D11CreateDeviceAndSwapChain: Failed to create swap chain");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
2018-03-03 23:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ppDevice != nullptr)
|
|
|
|
*ppDevice = d3d11Device.ref();
|
|
|
|
|
|
|
|
if (ppImmediateContext != nullptr)
|
|
|
|
*ppImmediateContext = d3d11Context.ref();
|
|
|
|
|
|
|
|
return S_OK;
|
2017-10-11 09:51:48 +02:00
|
|
|
}
|
2017-10-11 15:32:24 +02:00
|
|
|
|
2017-10-11 09:51:48 +02:00
|
|
|
}
|