Skip to content

Commit

Permalink
D3D11: Port to DXVK
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Jun 2, 2023
1 parent 73810f3 commit 242c098
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
66 changes: 64 additions & 2 deletions src/FNA3D_Driver_D3D11.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
#include "FNA3D_Driver_D3D11_shaders.h"

#include <SDL.h>
#ifdef FNA3D_DXVK_NATIVE
#define DXVK_SDL2_IMPL
#include "dxvk_sdl2.h"
#else
#include <SDL_syswm.h>
#endif /* FNA3D_DXVK_NATIVE */

/* D3D11 Libraries */

Expand Down Expand Up @@ -4845,6 +4850,9 @@ static uint8_t D3D11_PrepareWindowAttributes(uint32_t *flags)
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
void* dxgi_dll;
void* factory; /* IDXGIFactory1 or IDXGIFactory2 */
IDXGIAdapter1 *adapter;
HRESULT res;

#ifdef FNA3D_DXVK_NATIVE
Expand Down Expand Up @@ -4882,8 +4890,32 @@ static uint8_t D3D11_PrepareWindowAttributes(uint32_t *flags)
return 0;
}

res = D3D11_PLATFORM_CreateDXGIFactory(
&dxgi_dll,
&factory
);
if (FAILED(res))
{
D3D11_PLATFORM_UnloadDXGI(dxgi_dll);
SDL_UnloadObject(module);
return 0;
}
#ifdef FNA3D_DXVK_NATIVE
res = dxvkInitWSI(factory); /* Defined by dxvk_sdl2.h */
if (FAILED(res))
{
IUnknown_Release((IUnknown*) factory);
D3D11_PLATFORM_UnloadDXGI(dxgi_dll);
SDL_UnloadObject(module);
}
#endif /* FNA3D_DXVK_NATIVE */
D3D11_PLATFORM_GetDefaultAdapter(
factory,
&adapter
);

res = D3D11CreateDeviceFunc(
NULL,
(IDXGIAdapter*) adapter,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
Expand All @@ -4899,7 +4931,7 @@ static uint8_t D3D11_PrepareWindowAttributes(uint32_t *flags)
{
FNA3D_LogWarn("Creating device with feature level 11_1 failed. Lowering feature level.", res);
res = D3D11CreateDeviceFunc(
NULL,
(IDXGIAdapter*) adapter,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
Expand All @@ -4912,6 +4944,8 @@ static uint8_t D3D11_PrepareWindowAttributes(uint32_t *flags)
);
}

IUnknown_Release((IUnknown*) factory);
D3D11_PLATFORM_UnloadDXGI(dxgi_dll);
D3D11_PLATFORM_UnloadD3D11(module);

if (FAILED(res))
Expand All @@ -4922,6 +4956,10 @@ static uint8_t D3D11_PrepareWindowAttributes(uint32_t *flags)

/* No window flags required */
SDL_SetHint(SDL_HINT_VIDEO_EXTERNAL_CONTEXT, "1");
#ifdef FNA3D_DXVK_NATIVE
/* ... unless this is DXVK */
*flags = SDL_WINDOW_VULKAN;
#endif /* FNA3D_DXVK_NATIVE */
return 1;
}

Expand Down Expand Up @@ -5120,6 +5158,10 @@ static FNA3D_Device* D3D11_CreateDevice(
&renderer->factory
);
ERROR_CHECK_RETURN("Could not create DXGIFactory", NULL)
#ifdef FNA3D_DXVK_NATIVE
res = dxvkInitWSI(renderer->factory); /* Defined by dxvk_sdl2.h */
ERROR_CHECK_RETURN("Could not initialize DXVK WSI", NULL)
#endif /* FNA3D_DXVK_NATIVE */
D3D11_PLATFORM_GetDefaultAdapter(
renderer->factory,
&renderer->adapter
Expand Down Expand Up @@ -5552,6 +5594,21 @@ static void ResolveSwapChainModeDescription(
DXGI_MODE_DESC* modeDescription,
DXGI_MODE_DESC* swapChainDescription
) {
#ifdef FNA3D_DXVK_NATIVE
/* FIXME: We could do SDL_GetWindowDisplayIndex here, but eh */
IDXGIOutput *output;
IDXGIAdapter_EnumOutputs(
adapter,
0,
&output
);
IDXGIOutput_FindClosestMatchingMode(
output,
modeDescription,
swapChainDescription,
device
);
#else
HMONITOR monitor;
int iAdapter = 0, iOutput;
IDXGIAdapter1* pAdapter;
Expand Down Expand Up @@ -5579,6 +5636,7 @@ static void ResolveSwapChainModeDescription(
}
IDXGIAdapter1_Release(pAdapter);
}
#endif /* FNA3D_DXVK_NATIVE */
}

static void D3D11_PLATFORM_CreateSwapChain(
Expand All @@ -5594,10 +5652,14 @@ static void D3D11_PLATFORM_CreateSwapChain(
HWND dxgiHandle;
HRESULT res;

#ifdef FNA3D_DXVK_NATIVE
dxgiHandle = (HWND) windowHandle;
#else
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
SDL_GetWindowWMInfo((SDL_Window*) windowHandle, &info);
dxgiHandle = info.info.win.window;
#endif /* FNA3D_DXVK_NATIVE */

/* Initialize swapchain buffer descriptor */
swapchainBufferDesc.Width = 0;
Expand Down
12 changes: 12 additions & 0 deletions src/dxgi_dxvk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef DXGI_DXVK_H
#define DXGI_DXVK_H

#include <dxgi.h>

static const IID D3D_IID_IDxvkWsi = {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}};

typedef struct IDxvkWsi IDxvkWsi;

/* TODO: vtable for IDxvkWsi */

#endif /* DXGI_DXVK_H */
33 changes: 33 additions & 0 deletions src/dxvk_sdl2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef DXVK_SDL2_H
#define DXVK_SDL2_H

#include <windows.h>

extern HRESULT dxvkInitWSI(void* factory);

#endif /* DXVK_SDL2_H */

#ifdef DXVK_SDL2_IMPL

#include <dxgi_dxvk.h>

/* TODO: Define static functions for DXGI vtable */

HRESULT dxvkInitWSI(void* factory)
{
IDxvkWsi *wsi;
HRESULT res = IDXGIFactory1_QueryInterface(
(IDXGIFactory1*) factory,
&D3D_IID_IDxvkWsi,
(void**) &wsi
);
if (FAILED(res))
{
return res;
}

/* TODO: wsi->SetWSI(vtable) */
return E_NOTIMPL;
}

#endif /* DXVK_SDL2_IMPL */

0 comments on commit 242c098

Please sign in to comment.