Merge pull request #2104 from CookiePLMonster/d3d9ex

Move to Direct3D 9Ex
This commit is contained in:
Luke Usher 2020-12-22 11:11:11 +00:00 committed by GitHub
commit c89e1ba079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 33 deletions

View File

@ -84,7 +84,7 @@ using namespace std::literals::chrono_literals;
// Global(s) // Global(s)
HWND g_hEmuWindow = NULL; // rendering window HWND g_hEmuWindow = NULL; // rendering window
bool g_bClipCursor = false; // indicates that the mouse cursor should be confined inside the rendering window bool g_bClipCursor = false; // indicates that the mouse cursor should be confined inside the rendering window
IDirect3DDevice *g_pD3DDevice = nullptr; // Direct3D Device IDirect3DDevice9Ex *g_pD3DDevice = nullptr; // Direct3D Device
// Static Variable(s) // Static Variable(s)
static bool g_bSupportsFormatSurface[xbox::X_D3DFMT_LIN_R8G8B8A8 + 1]; // Does device support surface format? static bool g_bSupportsFormatSurface[xbox::X_D3DFMT_LIN_R8G8B8A8 + 1]; // Does device support surface format?
@ -112,7 +112,7 @@ static D3DSURFACE_DESC g_HostBackBufferDesc;
static Settings::s_video g_XBVideo; static Settings::s_video g_XBVideo;
// D3D based variables // D3D based variables
static IDirect3D *g_pDirect3D = nullptr; static IDirect3D9Ex *g_pDirect3D = nullptr;
D3DCAPS g_D3DCaps = {}; // Direct3D Caps D3DCAPS g_D3DCaps = {}; // Direct3D Caps
static IDirect3DIndexBuffer *g_pClosingLineLoopHostIndexBuffer = nullptr; static IDirect3DIndexBuffer *g_pClosingLineLoopHostIndexBuffer = nullptr;
static IDirect3DIndexBuffer *g_pQuadToTriangleHostIndexBuffer = nullptr; static IDirect3DIndexBuffer *g_pQuadToTriangleHostIndexBuffer = nullptr;
@ -296,7 +296,7 @@ g_EmuCDPD;
XB_MACRO(xbox::void_xt, WINAPI, D3DDevice_MultiplyTransform, (D3DTRANSFORMSTATETYPE, CONST D3DMATRIX*) ); \ XB_MACRO(xbox::void_xt, WINAPI, D3DDevice_MultiplyTransform, (D3DTRANSFORMSTATETYPE, CONST D3DMATRIX*) ); \
XB_MACRO(xbox::void_xt, WINAPI, D3D_DestroyResource, (xbox::X_D3DResource*) ); \ XB_MACRO(xbox::void_xt, WINAPI, D3D_DestroyResource, (xbox::X_D3DResource*) ); \
XB_MACRO(xbox::void_xt, WINAPI, D3D_DestroyResource__LTCG, (xbox::void_xt) ); \ XB_MACRO(xbox::void_xt, WINAPI, D3D_DestroyResource__LTCG, (xbox::void_xt) ); \
XB_MACRO(xbox::hresult_xt, WINAPI, Direct3D_CreateDevice, (xbox::uint_xt, D3DDEVTYPE, HWND, xbox::dword_xt, xbox::X_D3DPRESENT_PARAMETERS*, IDirect3DDevice**) ); \ XB_MACRO(xbox::hresult_xt, WINAPI, Direct3D_CreateDevice, (xbox::uint_xt, D3DDEVTYPE, HWND, xbox::dword_xt, xbox::X_D3DPRESENT_PARAMETERS*, xbox::X_D3DDevice**)); \
XB_MACRO(xbox::hresult_xt, WINAPI, Direct3D_CreateDevice_16__LTCG_eax_BehaviorFlags_ebx_ppReturnedDeviceInterface, (xbox::uint_xt, D3DDEVTYPE, HWND, xbox::X_D3DPRESENT_PARAMETERS*) ); \ XB_MACRO(xbox::hresult_xt, WINAPI, Direct3D_CreateDevice_16__LTCG_eax_BehaviorFlags_ebx_ppReturnedDeviceInterface, (xbox::uint_xt, D3DDEVTYPE, HWND, xbox::X_D3DPRESENT_PARAMETERS*) ); \
XB_MACRO(xbox::hresult_xt, WINAPI, Direct3D_CreateDevice_16__LTCG_eax_BehaviorFlags_ecx_ppReturnedDeviceInterface, (xbox::uint_xt, D3DDEVTYPE, HWND, xbox::X_D3DPRESENT_PARAMETERS*) ); \ XB_MACRO(xbox::hresult_xt, WINAPI, Direct3D_CreateDevice_16__LTCG_eax_BehaviorFlags_ecx_ppReturnedDeviceInterface, (xbox::uint_xt, D3DDEVTYPE, HWND, xbox::X_D3DPRESENT_PARAMETERS*) ); \
XB_MACRO(xbox::hresult_xt, WINAPI, Direct3D_CreateDevice_4, (xbox::X_D3DPRESENT_PARAMETERS*) ); \ XB_MACRO(xbox::hresult_xt, WINAPI, Direct3D_CreateDevice_4, (xbox::X_D3DPRESENT_PARAMETERS*) ); \
@ -1613,8 +1613,7 @@ void EmuD3DInit()
// create Direct3D8 and retrieve caps // create Direct3D8 and retrieve caps
{ {
// xbox Direct3DCreate8 returns "1" always, so we need our own ptr // xbox Direct3DCreate8 returns "1" always, so we need our own ptr
g_pDirect3D = Direct3DCreate(D3D_SDK_VERSION); if(FAILED(Direct3DCreate9Ex(D3D_SDK_VERSION, &g_pDirect3D)))
if(g_pDirect3D == nullptr)
CxbxKrnlCleanup("Could not initialize Direct3D8!"); CxbxKrnlCleanup("Could not initialize Direct3D8!");
g_pDirect3D->GetDeviceCaps(g_EmuCDPD.Adapter, g_EmuCDPD.DeviceType, &g_D3DCaps); g_pDirect3D->GetDeviceCaps(g_EmuCDPD.Adapter, g_EmuCDPD.DeviceType, &g_D3DCaps);
@ -2403,22 +2402,33 @@ static void CreateDefaultD3D9Device
// We never want auto-depth stencil on the host, Xbox D3D will handle this for us // We never want auto-depth stencil on the host, Xbox D3D will handle this for us
g_EmuCDPD.HostPresentationParameters.EnableAutoDepthStencil = FALSE; g_EmuCDPD.HostPresentationParameters.EnableAutoDepthStencil = FALSE;
D3DDISPLAYMODEEX displayMode { sizeof(displayMode) };
{
const auto& presentParameters = g_EmuCDPD.HostPresentationParameters;
displayMode.Width = presentParameters.BackBufferWidth;
displayMode.Height = presentParameters.BackBufferHeight;
displayMode.RefreshRate = presentParameters.FullScreen_RefreshRateInHz;
displayMode.Format = presentParameters.BackBufferFormat;
displayMode.ScanLineOrdering = D3DSCANLINEORDERING_PROGRESSIVE;
}
// IDirect3D9::CreateDevice must be called from the window message thread // IDirect3D9::CreateDevice must be called from the window message thread
// See https://docs.microsoft.com/en-us/windows/win32/direct3d9/multithreading-issues // See https://docs.microsoft.com/en-us/windows/win32/direct3d9/multithreading-issues
HRESULT hr; HRESULT hr;
RunOnWndMsgThread([&hr, BehaviorFlags] { RunOnWndMsgThread([&hr, BehaviorFlags, &displayMode] {
hr = g_pDirect3D->CreateDevice( hr = g_pDirect3D->CreateDeviceEx(
g_EmuCDPD.Adapter, g_EmuCDPD.Adapter,
g_EmuCDPD.DeviceType, g_EmuCDPD.DeviceType,
g_hEmuWindow, g_hEmuWindow,
BehaviorFlags, BehaviorFlags,
&g_EmuCDPD.HostPresentationParameters, &g_EmuCDPD.HostPresentationParameters,
g_EmuCDPD.HostPresentationParameters.Windowed ? nullptr : &displayMode,
&g_pD3DDevice); &g_pD3DDevice);
}); });
DEBUG_D3DRESULT(hr, "IDirect3D::CreateDevice"); DEBUG_D3DRESULT(hr, "IDirect3D::CreateDeviceEx");
if(FAILED(hr)) if(FAILED(hr))
CxbxKrnlCleanup("IDirect3D::CreateDevice failed"); CxbxKrnlCleanup("IDirect3D::CreateDeviceEx failed");
// Which texture formats does this device support? // Which texture formats does this device support?
DetermineSupportedD3DFormats(); DetermineSupportedD3DFormats();
@ -2664,7 +2674,7 @@ IDirect3DIndexBuffer* CxbxCreateIndexBuffer(unsigned IndexCount)
// "Managing Resources (Direct3D 9)" // "Managing Resources (Direct3D 9)"
// suggests "for resources which change with high frequency" [...] // suggests "for resources which change with high frequency" [...]
// "D3DPOOL_DEFAULT along with D3DUSAGE_DYNAMIC should be used." // "D3DPOOL_DEFAULT along with D3DUSAGE_DYNAMIC should be used."
const D3DPOOL D3DPool = D3DPOOL_DEFAULT; // Was D3DPOOL_MANAGED const D3DPOOL D3DPool = D3DPOOL_DEFAULT;
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb172625(v=vs.85).aspx // https://msdn.microsoft.com/en-us/library/windows/desktop/bb172625(v=vs.85).aspx
// "Buffers created with D3DPOOL_DEFAULT that do not specify D3DUSAGE_WRITEONLY may suffer a severe performance penalty." // "Buffers created with D3DPOOL_DEFAULT that do not specify D3DUSAGE_WRITEONLY may suffer a severe performance penalty."
const DWORD D3DUsage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; // Was D3DUSAGE_WRITEONLY const DWORD D3DUsage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; // Was D3DUSAGE_WRITEONLY
@ -3030,7 +3040,7 @@ static void Direct3D_CreateDevice_4
( (
xbox::dword_xt BehaviorFlags, xbox::dword_xt BehaviorFlags,
xbox::X_D3DPRESENT_PARAMETERS *pPresentationParameters, xbox::X_D3DPRESENT_PARAMETERS *pPresentationParameters,
IDirect3DDevice **ppReturnedDeviceInterface xbox::X_D3DDevice **ppReturnedDeviceInterface
) )
{ {
LOG_FUNC_BEGIN LOG_FUNC_BEGIN
@ -3049,7 +3059,7 @@ __declspec(naked) xbox::hresult_xt WINAPI xbox::EMUPATCH(Direct3D_CreateDevice_4
) )
{ {
DWORD BehaviorFlags; DWORD BehaviorFlags;
IDirect3DDevice **ppReturnedDeviceInterface; xbox::X_D3DDevice **ppReturnedDeviceInterface;
__asm { __asm {
push ebp push ebp
@ -3092,7 +3102,7 @@ static void Direct3D_CreateDevice_16__LTCG_eax_BehaviorFlags_ecx_ppReturnedDevic
HWND hFocusWindow, HWND hFocusWindow,
xbox::dword_xt BehaviorFlags, xbox::dword_xt BehaviorFlags,
xbox::X_D3DPRESENT_PARAMETERS *pPresentationParameters, xbox::X_D3DPRESENT_PARAMETERS *pPresentationParameters,
IDirect3DDevice **ppReturnedDeviceInterface xbox::X_D3DDevice **ppReturnedDeviceInterface
) )
{ {
LOG_FUNC_BEGIN LOG_FUNC_BEGIN
@ -3117,7 +3127,7 @@ __declspec(naked) xbox::hresult_xt WINAPI xbox::EMUPATCH(Direct3D_CreateDevice_1
) )
{ {
dword_xt BehaviorFlags; dword_xt BehaviorFlags;
IDirect3DDevice **ppReturnedDeviceInterface; xbox::X_D3DDevice **ppReturnedDeviceInterface;
__asm { __asm {
push ebp push ebp
@ -3163,7 +3173,7 @@ static void Direct3D_CreateDevice_16__LTCG_eax_BehaviorFlags_ebx_ppReturnedDevic
HWND hFocusWindow, HWND hFocusWindow,
xbox::dword_xt BehaviorFlags, xbox::dword_xt BehaviorFlags,
xbox::X_D3DPRESENT_PARAMETERS *pPresentationParameters, xbox::X_D3DPRESENT_PARAMETERS *pPresentationParameters,
IDirect3DDevice **ppReturnedDeviceInterface xbox::X_D3DDevice **ppReturnedDeviceInterface
) )
{ {
LOG_FUNC_BEGIN LOG_FUNC_BEGIN
@ -3188,7 +3198,7 @@ __declspec(naked) xbox::hresult_xt WINAPI xbox::EMUPATCH(Direct3D_CreateDevice_1
) )
{ {
dword_xt BehaviorFlags; dword_xt BehaviorFlags;
IDirect3DDevice **ppReturnedDeviceInterface; xbox::X_D3DDevice **ppReturnedDeviceInterface;
__asm { __asm {
push ebp push ebp
@ -3284,7 +3294,7 @@ xbox::hresult_xt WINAPI xbox::EMUPATCH(Direct3D_CreateDevice)
HWND hFocusWindow, HWND hFocusWindow,
dword_xt BehaviorFlags, dword_xt BehaviorFlags,
X_D3DPRESENT_PARAMETERS *pPresentationParameters, X_D3DPRESENT_PARAMETERS *pPresentationParameters,
IDirect3DDevice **ppReturnedDeviceInterface xbox::X_D3DDevice **ppReturnedDeviceInterface
) )
{ {
LOG_FUNC_BEGIN LOG_FUNC_BEGIN
@ -5405,7 +5415,7 @@ void CreateHostResource(xbox::X_D3DResource *pResource, DWORD D3DUsage, int iTex
case xbox::X_D3DRTYPE_CUBETEXTURE: { case xbox::X_D3DRTYPE_CUBETEXTURE: {
xbox::X_D3DPixelContainer *pPixelContainer = (xbox::X_D3DPixelContainer*)pResource; xbox::X_D3DPixelContainer *pPixelContainer = (xbox::X_D3DPixelContainer*)pResource;
xbox::X_D3DFORMAT X_Format = GetXboxPixelContainerFormat(pPixelContainer); xbox::X_D3DFORMAT X_Format = GetXboxPixelContainerFormat(pPixelContainer);
D3DPOOL D3DPool = D3DPOOL_DEFAULT; // Was: D3DPOOL_MANAGED TODO : Nuance D3DPOOL where/when needed D3DPOOL D3DPool = D3DPOOL_DEFAULT; // TODO : Nuance D3DPOOL where/when needed
if (EmuXBFormatIsDepthBuffer(X_Format)) { if (EmuXBFormatIsDepthBuffer(X_Format)) {
D3DUsage |= D3DUSAGE_DEPTHSTENCIL; D3DUsage |= D3DUSAGE_DEPTHSTENCIL;
@ -5692,7 +5702,7 @@ void CreateHostResource(xbox::X_D3DResource *pResource, DWORD D3DUsage, int iTex
} }
case xbox::X_D3DRTYPE_CUBETEXTURE: { case xbox::X_D3DRTYPE_CUBETEXTURE: {
EmuLog(LOG_LEVEL::DEBUG, "CreateCubeTexture(%d, %d, 0, %d, D3DPOOL_MANAGED)", dwWidth, EmuLog(LOG_LEVEL::DEBUG, "CreateCubeTexture(%d, %d, 0, %d, D3DPOOL_DEFAULT)", dwWidth,
dwMipMapLevels, PCFormat); dwMipMapLevels, PCFormat);
hRet = g_pD3DDevice->CreateCubeTexture(dwWidth, dwMipMapLevels, D3DUsage, hRet = g_pD3DDevice->CreateCubeTexture(dwWidth, dwMipMapLevels, D3DUsage,

View File

@ -72,7 +72,7 @@ extern void EmuD3DInit();
// cleanup direct3d // cleanup direct3d
extern void EmuD3DCleanup(); extern void EmuD3DCleanup();
extern IDirect3DDevice *g_pD3DDevice; extern IDirect3DDevice9Ex *g_pD3DDevice;
extern xbox::dword_xt g_Xbox_VertexShader_Handle; extern xbox::dword_xt g_Xbox_VertexShader_Handle;
@ -92,7 +92,7 @@ xbox::hresult_xt WINAPI EMUPATCH(Direct3D_CreateDevice)
HWND hFocusWindow, HWND hFocusWindow,
dword_xt BehaviorFlags, dword_xt BehaviorFlags,
X_D3DPRESENT_PARAMETERS *pPresentationParameters, X_D3DPRESENT_PARAMETERS *pPresentationParameters,
IDirect3DDevice **ppReturnedDeviceInterface X_D3DDevice **ppReturnedDeviceInterface
); );
xbox::hresult_xt WINAPI EMUPATCH(Direct3D_CreateDevice_16__LTCG_eax_BehaviorFlags_ebx_ppReturnedDeviceInterface) xbox::hresult_xt WINAPI EMUPATCH(Direct3D_CreateDevice_16__LTCG_eax_BehaviorFlags_ebx_ppReturnedDeviceInterface)

View File

@ -170,7 +170,7 @@ void VertexShaderSource::ReleaseShader(ShaderKey key)
} }
} }
void VertexShaderSource::ResetD3DDevice(IDirect3DDevice* newDevice) void VertexShaderSource::ResetD3DDevice(IDirect3DDevice9* newDevice)
{ {
EmuLog(LOG_LEVEL::DEBUG, "Resetting D3D device"); EmuLog(LOG_LEVEL::DEBUG, "Resetting D3D device");
this->pD3DDevice = newDevice; this->pD3DDevice = newDevice;

View File

@ -15,7 +15,7 @@ public:
IDirect3DVertexShader *GetShader(ShaderKey key); IDirect3DVertexShader *GetShader(ShaderKey key);
void ReleaseShader(ShaderKey key); void ReleaseShader(ShaderKey key);
void ResetD3DDevice(IDirect3DDevice* pD3DDevice); void ResetD3DDevice(IDirect3DDevice9* pD3DDevice);
// TODO // TODO
// WriteCacheToDisk // WriteCacheToDisk
@ -35,7 +35,7 @@ private:
// OptimizationLevel? // OptimizationLevel?
}; };
IDirect3DDevice* pD3DDevice; IDirect3DDevice9* pD3DDevice;
std::mutex cacheMutex; std::mutex cacheMutex;
std::map<ShaderKey, LazyVertexShader> cache; std::map<ShaderKey, LazyVertexShader> cache;

View File

@ -58,7 +58,6 @@
// Alias all host Direct3D 9 symbols to generic symbols // Alias all host Direct3D 9 symbols to generic symbols
#define DXGetErrorString DXGetErrorString9A #define DXGetErrorString DXGetErrorString9A
#define DXGetErrorDescription DXGetErrorDescription9A #define DXGetErrorDescription DXGetErrorDescription9A
#define Direct3DCreate Direct3DCreate9
#define D3DXAssembleShader D3DXAssembleShader #define D3DXAssembleShader D3DXAssembleShader
#define FullScreen_PresentationInterval PresentationInterval // a field in D3DPRESENT_PARAMETERS #define FullScreen_PresentationInterval PresentationInterval // a field in D3DPRESENT_PARAMETERS
#define D3DLockData void #define D3DLockData void
@ -68,8 +67,6 @@
#define D3DVERTEXELEMENT D3DVERTEXELEMENT9 #define D3DVERTEXELEMENT D3DVERTEXELEMENT9
#define D3DVIEWPORT D3DVIEWPORT9 #define D3DVIEWPORT D3DVIEWPORT9
#define IDirect3D IDirect3D9
#define IDirect3DDevice IDirect3DDevice9
#define IDirect3DStateBlock IDirect3DStateBlock9 // unused #define IDirect3DStateBlock IDirect3DStateBlock9 // unused
#define IDirect3DVertexDeclaration IDirect3DVertexDeclaration9 #define IDirect3DVertexDeclaration IDirect3DVertexDeclaration9
#define IDirect3DVertexShader IDirect3DVertexShader9 #define IDirect3DVertexShader IDirect3DVertexShader9
@ -417,6 +414,7 @@ typedef struct _X_D3DPIXELSHADERDEF // <- blueshogun 10/1/07
} }
X_D3DPIXELSHADERDEF; X_D3DPIXELSHADERDEF;
typedef void X_D3DDevice;
typedef struct _X_PixelShader typedef struct _X_PixelShader
{ {

View File

@ -47,7 +47,7 @@ static void RefreshDirect3DDevice();
static void RefreshRenderResolution(); static void RefreshRenderResolution();
/*! direct3d instance */ /*! direct3d instance */
static IDirect3D *g_pDirect3D = nullptr; static IDirect3D9Ex *g_pDirect3D = nullptr;
/*! video configuration */ /*! video configuration */
static Settings::s_video g_XBVideo; static Settings::s_video g_XBVideo;
/*! changes flag */ /*! changes flag */
@ -63,8 +63,6 @@ static HWND g_hVideoResolution = NULL;
/*! handle to scale factor window*/ /*! handle to scale factor window*/
static HWND g_hRenderResolution = NULL; static HWND g_hRenderResolution = NULL;
#pragma optimize("", off)
void ShowVideoConfig(HWND hwnd) void ShowVideoConfig(HWND hwnd)
{ {
/*! reset changes flag */ /*! reset changes flag */
@ -75,9 +73,7 @@ void ShowVideoConfig(HWND hwnd)
/*! initialize direct3d */ /*! initialize direct3d */
{ {
g_pDirect3D = Direct3DCreate(D3D_SDK_VERSION); if(FAILED(Direct3DCreate9Ex(D3D_SDK_VERSION, &g_pDirect3D))) { goto cleanup; }
if(g_pDirect3D == 0) { goto cleanup; }
g_dwAdapterCount = g_pDirect3D->GetAdapterCount(); g_dwAdapterCount = g_pDirect3D->GetAdapterCount();
} }