Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions 3rd_party/kiero2/kiero_d3d10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ kiero::Error kiero::locate<kiero::Implementation_D3D10>(void* in, void* out)
sc_desc.OutputWindow = window.hwnd;
sc_desc.Windowed = TRUE;
sc_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sc_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;

IDXGISwapChain* swapchain;
ID3D10Device* device;
Expand Down Expand Up @@ -115,17 +116,13 @@ kiero::Error kiero::locate<kiero::Implementation_D3D10>(void* in, void* out)

D3D10Output* output = (D3D10Output*)out;

for (auto vtable = *(void***)swapchain; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->swapchain_methods.push_back(ptr);
}
void** swapchain_vtable = *(void***)swapchain;
void** device_vtable = *(void***)device;

for (auto vtable = *(void***)device; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->device_methods.push_back(ptr);
}
// COM vtables are not null-terminated. Keep these counts aligned with the
// original kiero table sizes used before the kiero2 migration.
output->swapchain_methods.assign(swapchain_vtable, swapchain_vtable + 18);
output->device_methods.assign(device_vtable, device_vtable + 98);

return Error_Nil;
}
153 changes: 86 additions & 67 deletions 3rd_party/kiero2/kiero_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,12 @@ kiero::Error kiero::locate<kiero::Implementation_D3D11>(void* in, void* out)
{
KIERO_UNUSED(in);

auto dxgi_dll = GetModuleHandleA("dxgi.dll");
if (!dxgi_dll) {
KIERO_DBG_MSG("dxgi.dll not loaded");
return Error_ModuleNotFound;
}

auto d3d11_dll = GetModuleHandleA("d3d11.dll");
if (!d3d11_dll) {
KIERO_DBG_MSG("d3d11.dll not loaded");
return Error_ModuleNotFound;
}

auto CreateDXGIFactory = (
(CreateDXGIFactory_t)
GetProcAddress(dxgi_dll, "CreateDXGIFactory")
);
if (!CreateDXGIFactory) {
KIERO_DBG_MSG("CreateDXGIFactory not found");
return Error_MethodNotFound;
}

IDXGIFactory* factory;
auto hresult = CreateDXGIFactory(
__uuidof(IDXGIFactory),
(void**)&factory
);
if (hresult != S_OK) {
KIERO_DBG_MSG("CreateDXGIFactory failed (%d)", hresult);
return Error_D3D11_CreateDXGIFactoryFailed;
}
KIERO_DEFER([&]() { factory->Release(); });

IDXGIAdapter* adapter;
hresult = factory->EnumAdapters(0, &adapter);
if (hresult != S_OK) {
KIERO_DBG_MSG("EnumAdapters failed (%d)", hresult);
return Error_D3D11_EnumAdaptersFailed;
}
KIERO_DEFER([&]() { adapter->Release(); });

auto D3D11CreateDeviceAndSwapChain =(
(D3D11CreateDeviceAndSwapChain_t)
GetProcAddress(d3d11_dll, "D3D11CreateDeviceAndSwapChain")
Expand All @@ -76,6 +42,38 @@ kiero::Error kiero::locate<kiero::Implementation_D3D11>(void* in, void* out)
return Error_MethodNotFound;
}

auto dxgi_dll = GetModuleHandleA("dxgi.dll");
IDXGIFactory* factory = nullptr;
IDXGIAdapter* adapter = nullptr;
if (dxgi_dll) {
auto CreateDXGIFactory = (
(CreateDXGIFactory_t)
GetProcAddress(dxgi_dll, "CreateDXGIFactory")
);
if (CreateDXGIFactory) {
HRESULT factory_result = CreateDXGIFactory(
__uuidof(IDXGIFactory),
(void**)&factory
);
if (factory_result == S_OK && factory) {
auto adapter_result = factory->EnumAdapters(0, &adapter);
if (adapter_result != S_OK) {
KIERO_DBG_MSG("EnumAdapters failed (%d), will try default hardware adapter", adapter_result);
}
} else {
KIERO_DBG_MSG("CreateDXGIFactory failed (%d), will try default hardware adapter", factory_result);
}
} else {
KIERO_DBG_MSG("CreateDXGIFactory not found, will try default hardware adapter");
}
} else {
KIERO_DBG_MSG("dxgi.dll not loaded, will try default hardware adapter");
}
KIERO_DEFER([&]() {
if (adapter) adapter->Release();
if (factory) factory->Release();
});

kiero::_::DummyWin32Window window;
kiero::_::create_dummy_win32_window(&window);
KIERO_DEFER([&]() { kiero::_::destroy_dummy_win32_window(&window); });
Expand All @@ -100,27 +98,56 @@ kiero::Error kiero::locate<kiero::Implementation_D3D11>(void* in, void* out)
D3D_FEATURE_LEVEL_10_0,
};

IDXGISwapChain* swapchain;
ID3D11Device* device;
ID3D11DeviceContext* context;
IDXGISwapChain* swapchain = nullptr;
ID3D11Device* device = nullptr;
ID3D11DeviceContext* context = nullptr;
D3D_FEATURE_LEVEL feature_level;

hresult = D3D11CreateDeviceAndSwapChain(
adapter,
D3D_DRIVER_TYPE_UNKNOWN,
nullptr,
0,
feature_levels,
ARRAYSIZE(feature_levels),
D3D11_SDK_VERSION,
&sc_desc,
&swapchain,
&device,
&feature_level,
&context
);
auto release_created_objects = [&]() {
if (swapchain) {
swapchain->Release();
swapchain = nullptr;
}
if (device) {
device->Release();
device = nullptr;
}
if (context) {
context->Release();
context = nullptr;
}
};

auto create_dummy_swapchain = [&](IDXGIAdapter* selected_adapter, D3D_DRIVER_TYPE driver_type) {
release_created_objects();
return D3D11CreateDeviceAndSwapChain(
selected_adapter,
driver_type,
nullptr,
0,
feature_levels,
ARRAYSIZE(feature_levels),
D3D11_SDK_VERSION,
&sc_desc,
&swapchain,
&device,
&feature_level,
&context
);
};

HRESULT hresult = adapter ? create_dummy_swapchain(adapter, D3D_DRIVER_TYPE_UNKNOWN) : E_FAIL;
if (FAILED(hresult)) {
KIERO_DBG_MSG("D3D11 adapter swapchain failed (%d), trying default hardware", hresult);
hresult = create_dummy_swapchain(nullptr, D3D_DRIVER_TYPE_HARDWARE);
}
if (FAILED(hresult)) {
KIERO_DBG_MSG("D3D11 hardware swapchain failed (%d), trying WARP", hresult);
hresult = create_dummy_swapchain(nullptr, D3D_DRIVER_TYPE_WARP);
}
if (hresult != S_OK) {
KIERO_DBG_MSG("D3D11CreateDeviceAndSwapChain failed (%d)", hresult);
release_created_objects();
return Error_D3D11_CreateDeviceAndSwapChainFailed;
}
KIERO_DEFER([&]() {
Expand All @@ -131,23 +158,15 @@ kiero::Error kiero::locate<kiero::Implementation_D3D11>(void* in, void* out)

D3D11Output* output = (D3D11Output*)out;

for (auto vtable = *(void***)swapchain; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->swapchain_methods.push_back(ptr);
}
void** swapchain_vtable = *(void***)swapchain;
void** device_vtable = *(void***)device;
void** context_vtable = *(void***)context;

for (auto vtable = *(void***)device; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->device_methods.push_back(ptr);
}

for (auto vtable = *(void***)context; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->context_methods.push_back(ptr);
}
// COM vtables are not null-terminated. Keep these counts aligned with the
// original kiero table sizes used before the kiero2 migration.
output->swapchain_methods.assign(swapchain_vtable, swapchain_vtable + 18);
output->device_methods.assign(device_vtable, device_vtable + 43);
output->context_methods.assign(context_vtable, context_vtable + 144);

return Error_Nil;
}
42 changes: 13 additions & 29 deletions 3rd_party/kiero2/kiero_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,35 +156,19 @@ kiero::Error kiero::locate<kiero::Implementation_D3D12>(void* in, void* out)

D3D12Output* output = (D3D12Output*)out;

for (auto vtable = *(void***)device; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->device_methods.push_back(ptr);
}

for (auto vtable = *(void***)command_queue; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->command_queue_methods.push_back(ptr);
}

for (auto vtable = *(void***)command_allocator; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->command_allocator_methods.push_back(ptr);
}

for (auto vtable = *(void***)command_list; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->command_list_methods.push_back(ptr);
}

for (auto vtable = *(void***)swapchain; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->swapchain_methods.push_back(ptr);
}
void** device_vtable = *(void***)device;
void** command_queue_vtable = *(void***)command_queue;
void** command_allocator_vtable = *(void***)command_allocator;
void** command_list_vtable = *(void***)command_list;
void** swapchain_vtable = *(void***)swapchain;

// COM vtables are not null-terminated. Keep these counts aligned with the
// original kiero table sizes used before the kiero2 migration.
output->device_methods.assign(device_vtable, device_vtable + 44);
output->command_queue_methods.assign(command_queue_vtable, command_queue_vtable + 19);
output->command_allocator_methods.assign(command_allocator_vtable, command_allocator_vtable + 9);
output->command_list_methods.assign(command_list_vtable, command_list_vtable + 60);
output->swapchain_methods.assign(swapchain_vtable, swapchain_vtable + 18);

return Error_Nil;
}
34 changes: 26 additions & 8 deletions 3rd_party/kiero2/kiero_d3d9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,35 @@ kiero::Error kiero::locate<kiero::Implementation_D3D9>(void* in, void* out)
kiero::_::create_dummy_win32_window(&window);
KIERO_DEFER([&]() { kiero::_::destroy_dummy_win32_window(&window); });

D3DDISPLAYMODE display_mode = {};
auto hresult = d3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode);
if (hresult != S_OK) {
KIERO_DBG_MSG("GetAdapterDisplayMode failed (%d)", hresult);
return Error_D3D9_CreateDeviceFailed;
}

D3DPRESENT_PARAMETERS present_parameters = {};
present_parameters.Windowed = TRUE;
present_parameters.BackBufferWidth = 0;
present_parameters.BackBufferHeight = 0;
present_parameters.BackBufferFormat = display_mode.Format;
present_parameters.BackBufferCount = 0;
present_parameters.MultiSampleType = D3DMULTISAMPLE_NONE;
present_parameters.MultiSampleQuality = 0;
present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
present_parameters.hDeviceWindow = window.hwnd;
present_parameters.Windowed = TRUE;
present_parameters.EnableAutoDepthStencil = FALSE;
present_parameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
present_parameters.Flags = 0;
present_parameters.FullScreen_RefreshRateInHz = 0;
present_parameters.PresentationInterval = 0;

IDirect3DDevice9* device;
auto hresult = d3d9->CreateDevice(
hresult = d3d9->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
window.hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_DISABLE_DRIVER_MANAGEMENT,
&present_parameters,
&device
);
Expand All @@ -58,11 +76,11 @@ kiero::Error kiero::locate<kiero::Implementation_D3D9>(void* in, void* out)

D3D9Output* output = (D3D9Output*)out;

for (auto vtable = *(void***)device; vtable; vtable++) {
auto ptr = *vtable;
if (!ptr) break;
output->device_methods.push_back(ptr);
}
void** device_vtable = *(void***)device;

// COM vtables are not null-terminated. Keep this count aligned with the
// original kiero table size used before the kiero2 migration.
output->device_methods.assign(device_vtable, device_vtable + 119);

return Error_Nil;
}
Loading
Loading