2013-04-17 21:29:41 -06:00
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
2010-06-13 13:50:06 -06:00
2011-01-25 09:43:08 -07:00
# include "StringUtil.h"
# include "VideoConfig.h"
2010-06-13 13:50:06 -06:00
# include "D3DBase.h"
# include "D3DTexture.h"
2011-01-25 09:43:08 -07:00
# include "GfxState.h"
2010-06-13 13:50:06 -06:00
2011-01-29 13:16:51 -07:00
namespace DX11
{
2011-02-26 16:41:02 -07:00
HINSTANCE hD3DCompilerDll = NULL ;
D3DREFLECT PD3DReflect = NULL ;
2013-10-19 03:27:57 -06:00
pD3DCompile PD3DCompile = NULL ;
2011-02-26 16:41:02 -07:00
int d3dcompiler_dll_ref = 0 ;
2010-11-21 08:34:04 -07:00
CREATEDXGIFACTORY PCreateDXGIFactory = NULL ;
HINSTANCE hDXGIDll = NULL ;
2010-12-29 06:07:00 -07:00
int dxgi_dll_ref = 0 ;
2010-11-21 08:34:04 -07:00
typedef HRESULT ( WINAPI * D3D11CREATEDEVICEANDSWAPCHAIN ) ( IDXGIAdapter * , D3D_DRIVER_TYPE , HMODULE , UINT , CONST D3D_FEATURE_LEVEL * , UINT , UINT , CONST DXGI_SWAP_CHAIN_DESC * , IDXGISwapChain * * , ID3D11Device * * , D3D_FEATURE_LEVEL * , ID3D11DeviceContext * * ) ;
2010-11-23 12:58:02 -07:00
D3D11CREATEDEVICE PD3D11CreateDevice = NULL ;
2010-11-21 08:34:04 -07:00
D3D11CREATEDEVICEANDSWAPCHAIN PD3D11CreateDeviceAndSwapChain = NULL ;
2011-06-11 13:37:21 -06:00
HINSTANCE hD3DDll = NULL ;
2010-12-29 06:07:00 -07:00
int d3d_dll_ref = 0 ;
2010-11-21 08:34:04 -07:00
2010-06-13 13:50:06 -06:00
namespace D3D
{
2011-06-11 13:37:21 -06:00
ID3D11Device * device = NULL ;
ID3D11DeviceContext * context = NULL ;
IDXGISwapChain * swapchain = NULL ;
2010-06-13 13:50:06 -06:00
D3D_FEATURE_LEVEL featlevel ;
2011-06-11 13:37:21 -06:00
D3DTexture2D * backbuf = NULL ;
2010-06-13 13:50:06 -06:00
HWND hWnd ;
2011-06-11 13:37:21 -06:00
std : : vector < DXGI_SAMPLE_DESC > aa_modes ; // supported AA modes of the current adapter
2010-11-23 12:58:02 -07:00
2010-06-18 19:02:43 -06:00
bool bgra_textures_supported ;
2010-06-14 16:38:47 -06:00
# define NUM_SUPPORTED_FEATURE_LEVELS 3
const D3D_FEATURE_LEVEL supported_feature_levels [ NUM_SUPPORTED_FEATURE_LEVELS ] = {
D3D_FEATURE_LEVEL_11_0 ,
D3D_FEATURE_LEVEL_10_1 ,
D3D_FEATURE_LEVEL_10_0
} ;
2010-06-13 13:50:06 -06:00
unsigned int xres , yres ;
bool bFrameInProgress = false ;
2010-11-23 12:58:02 -07:00
HRESULT LoadDXGI ( )
2010-06-13 13:50:06 -06:00
{
2010-12-29 06:07:00 -07:00
if ( dxgi_dll_ref + + > 0 ) return S_OK ;
2010-11-23 12:58:02 -07:00
if ( hDXGIDll ) return S_OK ;
2010-11-21 08:34:04 -07:00
hDXGIDll = LoadLibraryA ( " dxgi.dll " ) ;
if ( ! hDXGIDll )
{
MessageBoxA ( NULL , " Failed to load dxgi.dll " , " Critical error " , MB_OK | MB_ICONERROR ) ;
2010-12-29 06:07:00 -07:00
- - dxgi_dll_ref ;
2010-11-21 08:34:04 -07:00
return E_FAIL ;
}
PCreateDXGIFactory = ( CREATEDXGIFACTORY ) GetProcAddress ( hDXGIDll , " CreateDXGIFactory " ) ;
if ( PCreateDXGIFactory = = NULL ) MessageBoxA ( NULL , " GetProcAddress failed for CreateDXGIFactory! " , " Critical error " , MB_OK | MB_ICONERROR ) ;
2010-06-13 13:50:06 -06:00
2010-11-21 08:34:04 -07:00
return S_OK ;
}
2010-11-23 12:58:02 -07:00
HRESULT LoadD3D ( )
2010-11-21 08:34:04 -07:00
{
2011-06-11 13:37:21 -06:00
if ( d3d_dll_ref + + > 0 ) return S_OK ;
2010-12-29 06:07:00 -07:00
2011-06-11 13:37:21 -06:00
if ( hD3DDll ) return S_OK ;
hD3DDll = LoadLibraryA ( " d3d11.dll " ) ;
if ( ! hD3DDll )
2010-11-23 12:58:02 -07:00
{
MessageBoxA ( NULL , " Failed to load d3d11.dll " , " Critical error " , MB_OK | MB_ICONERROR ) ;
2010-12-29 06:07:00 -07:00
- - d3d_dll_ref ;
2010-11-23 12:58:02 -07:00
return E_FAIL ;
}
2011-06-11 13:37:21 -06:00
PD3D11CreateDevice = ( D3D11CREATEDEVICE ) GetProcAddress ( hD3DDll , " D3D11CreateDevice " ) ;
if ( PD3D11CreateDevice = = NULL ) MessageBoxA ( NULL , " GetProcAddress failed for D3D11CreateDevice! " , " Critical error " , MB_OK | MB_ICONERROR ) ;
2010-11-23 12:58:02 -07:00
2011-06-11 13:37:21 -06:00
PD3D11CreateDeviceAndSwapChain = ( D3D11CREATEDEVICEANDSWAPCHAIN ) GetProcAddress ( hD3DDll , " D3D11CreateDeviceAndSwapChain " ) ;
if ( PD3D11CreateDeviceAndSwapChain = = NULL ) MessageBoxA ( NULL , " GetProcAddress failed for D3D11CreateDeviceAndSwapChain! " , " Critical error " , MB_OK | MB_ICONERROR ) ;
2010-11-23 12:58:02 -07:00
return S_OK ;
2010-11-21 08:34:04 -07:00
}
2010-06-13 13:50:06 -06:00
2011-02-26 16:41:02 -07:00
HRESULT LoadD3DCompiler ( )
{
if ( d3dcompiler_dll_ref + + > 0 ) return S_OK ;
if ( hD3DCompilerDll ) return S_OK ;
// try to load D3DCompiler first to check whether we have proper runtime support
// try to use the dll the backend was compiled against first - don't bother about debug runtimes
hD3DCompilerDll = LoadLibraryA ( D3DCOMPILER_DLL_A ) ;
if ( ! hD3DCompilerDll )
{
// if that fails, use the dll which should be available in every SDK which officially supports DX11.
hD3DCompilerDll = LoadLibraryA ( " D3DCompiler_42.dll " ) ;
if ( ! hD3DCompilerDll )
{
MessageBoxA ( NULL , " Failed to load D3DCompiler_42.dll, update your DX11 runtime, please " , " Critical error " , MB_OK | MB_ICONERROR ) ;
return E_FAIL ;
}
else
{
NOTICE_LOG ( VIDEO , " Successfully loaded D3DCompiler_42.dll. If you're having trouble, try updating your DX runtime first. " ) ;
}
}
PD3DReflect = ( D3DREFLECT ) GetProcAddress ( hD3DCompilerDll , " D3DReflect " ) ;
if ( PD3DReflect = = NULL ) MessageBoxA ( NULL , " GetProcAddress failed for D3DReflect! " , " Critical error " , MB_OK | MB_ICONERROR ) ;
2013-10-19 03:27:57 -06:00
PD3DCompile = ( pD3DCompile ) GetProcAddress ( hD3DCompilerDll , " D3DCompile " ) ;
if ( PD3DCompile = = NULL ) MessageBoxA ( NULL , " GetProcAddress failed for D3DCompile! " , " Critical error " , MB_OK | MB_ICONERROR ) ;
2011-02-26 16:41:02 -07:00
return S_OK ;
}
2010-11-23 12:58:02 -07:00
void UnloadDXGI ( )
{
2010-12-29 06:07:00 -07:00
if ( ! dxgi_dll_ref ) return ;
if ( - - dxgi_dll_ref ! = 0 ) return ;
2010-11-23 12:58:02 -07:00
if ( hDXGIDll ) FreeLibrary ( hDXGIDll ) ;
hDXGIDll = NULL ;
PCreateDXGIFactory = NULL ;
}
void UnloadD3D ( )
{
2011-06-11 13:37:21 -06:00
if ( ! d3d_dll_ref ) return ;
if ( - - d3d_dll_ref ! = 0 ) return ;
2010-12-29 06:07:00 -07:00
2011-06-11 13:37:21 -06:00
if ( hD3DDll ) FreeLibrary ( hD3DDll ) ;
hD3DDll = NULL ;
2010-11-23 12:58:02 -07:00
PD3D11CreateDevice = NULL ;
PD3D11CreateDeviceAndSwapChain = NULL ;
}
2011-02-26 16:41:02 -07:00
void UnloadD3DCompiler ( )
{
if ( ! d3dcompiler_dll_ref ) return ;
if ( - - d3dcompiler_dll_ref ! = 0 ) return ;
if ( hD3DCompilerDll ) FreeLibrary ( hD3DCompilerDll ) ;
hD3DCompilerDll = NULL ;
PD3DReflect = NULL ;
}
2011-03-30 01:17:23 -06:00
std : : vector < DXGI_SAMPLE_DESC > EnumAAModes ( IDXGIAdapter * adapter )
2010-11-23 12:58:02 -07:00
{
2011-06-11 13:37:21 -06:00
std : : vector < DXGI_SAMPLE_DESC > aa_modes ;
2010-11-28 08:23:51 -07:00
// NOTE: D3D 10.0 doesn't support multisampled resources which are bound as depth buffers AND shader resources.
// Thus, we can't have MSAA with 10.0 level hardware.
2011-06-11 13:37:21 -06:00
ID3D11Device * device ;
ID3D11DeviceContext * context ;
2010-11-23 12:58:02 -07:00
D3D_FEATURE_LEVEL feat_level ;
2011-06-11 13:37:21 -06:00
HRESULT hr = PD3D11CreateDevice ( adapter , D3D_DRIVER_TYPE_UNKNOWN , NULL , D3D11_CREATE_DEVICE_SINGLETHREADED , supported_feature_levels , NUM_SUPPORTED_FEATURE_LEVELS , D3D11_SDK_VERSION , & device , & feat_level , & context ) ;
2010-11-28 08:23:51 -07:00
if ( FAILED ( hr ) | | feat_level = = D3D_FEATURE_LEVEL_10_0 )
{
2010-11-28 10:25:19 -07:00
DXGI_SAMPLE_DESC desc ;
desc . Count = 1 ;
desc . Quality = 0 ;
aa_modes . push_back ( desc ) ;
2011-06-11 13:37:21 -06:00
SAFE_RELEASE ( context ) ;
SAFE_RELEASE ( device ) ;
2010-11-28 08:23:51 -07:00
}
2011-03-30 01:17:23 -06:00
else
2010-11-21 08:34:04 -07:00
{
2011-06-11 13:37:21 -06:00
for ( int samples = 0 ; samples < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT ; + + samples )
2011-03-30 01:17:23 -06:00
{
UINT quality_levels = 0 ;
device - > CheckMultisampleQualityLevels ( DXGI_FORMAT_R8G8B8A8_UNORM , samples , & quality_levels ) ;
2011-06-11 13:37:21 -06:00
if ( quality_levels > 0 ) {
2011-03-30 01:17:23 -06:00
DXGI_SAMPLE_DESC desc ;
desc . Count = samples ;
2011-06-11 13:37:21 -06:00
for ( desc . Quality = 0 ; desc . Quality < quality_levels ; + + desc . Quality )
2011-03-30 01:17:23 -06:00
aa_modes . push_back ( desc ) ;
}
2010-11-23 12:58:02 -07:00
}
2011-06-11 13:37:21 -06:00
context - > Release ( ) ;
device - > Release ( ) ;
2010-11-21 08:34:04 -07:00
}
2011-03-30 01:17:23 -06:00
return aa_modes ;
2010-11-23 12:58:02 -07:00
}
2013-07-22 06:38:09 -06:00
D3D_FEATURE_LEVEL GetFeatureLevel ( IDXGIAdapter * adapter )
{
D3D_FEATURE_LEVEL feat_level = D3D_FEATURE_LEVEL_9_1 ;
PD3D11CreateDevice ( adapter , D3D_DRIVER_TYPE_UNKNOWN , NULL , D3D11_CREATE_DEVICE_SINGLETHREADED , supported_feature_levels , NUM_SUPPORTED_FEATURE_LEVELS , D3D11_SDK_VERSION , NULL , & feat_level , NULL ) ;
return feat_level ;
}
2010-11-23 12:58:02 -07:00
DXGI_SAMPLE_DESC GetAAMode ( int index )
{
2011-06-11 13:37:21 -06:00
return aa_modes [ index ] ;
2010-11-21 08:34:04 -07:00
}
HRESULT Create ( HWND wnd )
{
hWnd = wnd ;
HRESULT hr ;
RECT client ;
GetClientRect ( hWnd , & client ) ;
xres = client . right - client . left ;
yres = client . bottom - client . top ;
2010-11-23 12:58:02 -07:00
hr = LoadDXGI ( ) ;
if ( SUCCEEDED ( hr ) ) hr = LoadD3D ( ) ;
2011-02-26 16:41:02 -07:00
if ( SUCCEEDED ( hr ) ) hr = LoadD3DCompiler ( ) ;
2010-11-23 12:58:02 -07:00
if ( FAILED ( hr ) )
{
UnloadDXGI ( ) ;
UnloadD3D ( ) ;
2011-02-26 16:41:02 -07:00
UnloadD3DCompiler ( ) ;
2010-11-23 12:58:02 -07:00
return hr ;
}
2010-11-21 08:34:04 -07:00
2010-06-14 13:20:41 -06:00
IDXGIFactory * factory ;
2011-06-11 13:37:21 -06:00
IDXGIAdapter * adapter ;
IDXGIOutput * output ;
2010-11-21 08:34:04 -07:00
hr = PCreateDXGIFactory ( __uuidof ( IDXGIFactory ) , ( void * * ) & factory ) ;
2011-06-11 13:37:21 -06:00
if ( FAILED ( hr ) ) MessageBox ( wnd , _T ( " Failed to create IDXGIFactory object " ) , _T ( " Dolphin Direct3D 11 backend " ) , MB_OK | MB_ICONERROR ) ;
2010-06-14 13:20:41 -06:00
hr = factory - > EnumAdapters ( g_ActiveConfig . iAdapter , & adapter ) ;
if ( FAILED ( hr ) )
{
// try using the first one
hr = factory - > EnumAdapters ( 0 , & adapter ) ;
2011-06-11 13:37:21 -06:00
if ( FAILED ( hr ) ) MessageBox ( wnd , _T ( " Failed to enumerate adapters " ) , _T ( " Dolphin Direct3D 11 backend " ) , MB_OK | MB_ICONERROR ) ;
2010-06-14 13:20:41 -06:00
}
// TODO: Make this configurable
hr = adapter - > EnumOutputs ( 0 , & output ) ;
if ( FAILED ( hr ) )
{
// try using the first one
hr = adapter - > EnumOutputs ( 0 , & output ) ;
2011-06-11 13:37:21 -06:00
if ( FAILED ( hr ) ) MessageBox ( wnd , _T ( " Failed to enumerate outputs " ) , _T ( " Dolphin Direct3D 11 backend " ) , MB_OK | MB_ICONERROR ) ;
2010-06-14 13:20:41 -06:00
}
2010-11-23 12:58:02 -07:00
// get supported AA modes
2011-06-11 13:37:21 -06:00
aa_modes = EnumAAModes ( adapter ) ;
if ( g_Config . iMultisampleMode > = ( int ) aa_modes . size ( ) )
2010-11-28 08:23:51 -07:00
{
g_Config . iMultisampleMode = 0 ;
UpdateActiveConfig ( ) ;
}
2010-11-23 12:58:02 -07:00
2010-06-14 13:20:41 -06:00
DXGI_SWAP_CHAIN_DESC swap_chain_desc ;
memset ( & swap_chain_desc , 0 , sizeof ( swap_chain_desc ) ) ;
swap_chain_desc . BufferCount = 1 ;
swap_chain_desc . BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT ;
swap_chain_desc . OutputWindow = wnd ;
swap_chain_desc . SampleDesc . Count = 1 ;
swap_chain_desc . SampleDesc . Quality = 0 ;
swap_chain_desc . Windowed = TRUE ;
DXGI_MODE_DESC mode_desc ;
memset ( & mode_desc , 0 , sizeof ( mode_desc ) ) ;
mode_desc . Width = xres ;
mode_desc . Height = yres ;
mode_desc . Format = DXGI_FORMAT_R8G8B8A8_UNORM ;
mode_desc . Scaling = DXGI_MODE_SCALING_UNSPECIFIED ;
hr = output - > FindClosestMatchingMode ( & mode_desc , & swap_chain_desc . BufferDesc , NULL ) ;
2011-06-11 13:37:21 -06:00
if ( FAILED ( hr ) ) MessageBox ( wnd , _T ( " Failed to find a supported video mode " ) , _T ( " Dolphin Direct3D 11 backend " ) , MB_OK | MB_ICONERROR ) ;
2010-06-14 15:31:23 -06:00
// forcing buffer resolution to xres and yres.. TODO: The new video mode might not actually be supported!
swap_chain_desc . BufferDesc . Width = xres ;
swap_chain_desc . BufferDesc . Height = yres ;
2010-06-13 13:50:06 -06:00
2013-08-15 08:07:52 -06:00
# if defined(_DEBUG) || defined(DEBUGFAST)
2013-08-15 08:19:14 -06:00
// Creating debug devices can sometimes fail if the user doesn't have the correct
// version of the DirectX SDK. If it does, simply fallback to a non-debug device.
{
hr = PD3D11CreateDeviceAndSwapChain ( adapter , D3D_DRIVER_TYPE_UNKNOWN , NULL ,
D3D11_CREATE_DEVICE_SINGLETHREADED | D3D11_CREATE_DEVICE_DEBUG ,
supported_feature_levels , NUM_SUPPORTED_FEATURE_LEVELS ,
D3D11_SDK_VERSION , & swap_chain_desc , & swapchain , & device ,
& featlevel , & context ) ;
}
if ( FAILED ( hr ) )
2013-08-15 08:07:52 -06:00
# endif
2013-08-15 08:19:14 -06:00
{
hr = PD3D11CreateDeviceAndSwapChain ( adapter , D3D_DRIVER_TYPE_UNKNOWN , NULL ,
D3D11_CREATE_DEVICE_SINGLETHREADED ,
supported_feature_levels , NUM_SUPPORTED_FEATURE_LEVELS ,
D3D11_SDK_VERSION , & swap_chain_desc , & swapchain , & device ,
& featlevel , & context ) ;
}
if ( FAILED ( hr ) )
2010-06-13 13:50:06 -06:00
{
2011-06-11 13:37:21 -06:00
MessageBox ( wnd , _T ( " Failed to initialize Direct3D. \n Make sure your video card supports at least D3D 10.0 " ) , _T ( " Dolphin Direct3D 11 backend " ) , MB_OK | MB_ICONERROR ) ;
SAFE_RELEASE ( device ) ;
SAFE_RELEASE ( context ) ;
SAFE_RELEASE ( swapchain ) ;
2010-06-13 13:50:06 -06:00
return E_FAIL ;
}
2011-06-11 13:37:21 -06:00
SetDebugObjectName ( ( ID3D11DeviceChild * ) context , " device context " ) ;
2010-07-17 05:42:28 -06:00
SAFE_RELEASE ( factory ) ;
SAFE_RELEASE ( output ) ;
SAFE_RELEASE ( adapter ) ;
2010-06-13 13:50:06 -06:00
2011-06-11 13:37:21 -06:00
ID3D11Texture2D * buf ;
hr = swapchain - > GetBuffer ( 0 , IID_ID3D11Texture2D , ( void * * ) & buf ) ;
if ( FAILED ( hr ) )
2010-06-13 13:50:06 -06:00
{
2011-02-13 19:18:03 -07:00
MessageBox ( wnd , _T ( " Failed to get swapchain buffer " ) , _T ( " Dolphin Direct3D 11 backend " ) , MB_OK | MB_ICONERROR ) ;
2011-06-11 13:37:21 -06:00
SAFE_RELEASE ( device ) ;
SAFE_RELEASE ( context ) ;
SAFE_RELEASE ( swapchain ) ;
2010-06-13 13:50:06 -06:00
return E_FAIL ;
}
2011-06-11 13:37:21 -06:00
backbuf = new D3DTexture2D ( buf , D3D11_BIND_RENDER_TARGET ) ;
SAFE_RELEASE ( buf ) ;
2010-06-18 12:40:58 -06:00
CHECK ( backbuf ! = NULL , " Create back buffer texture " ) ;
2011-06-11 13:37:21 -06:00
SetDebugObjectName ( ( ID3D11DeviceChild * ) backbuf - > GetTex ( ) , " backbuffer texture " ) ;
SetDebugObjectName ( ( ID3D11DeviceChild * ) backbuf - > GetRTV ( ) , " backbuffer render target view " ) ;
2010-06-13 13:50:06 -06:00
context - > OMSetRenderTargets ( 1 , & backbuf - > GetRTV ( ) , NULL ) ;
2010-06-18 19:02:43 -06:00
// BGRA textures are easier to deal with in TextureCache, but might not be supported by the hardware
UINT format_support ;
device - > CheckFormatSupport ( DXGI_FORMAT_B8G8R8A8_UNORM , & format_support ) ;
bgra_textures_supported = ( format_support & D3D11_FORMAT_SUPPORT_TEXTURE2D ) ! = 0 ;
2011-06-11 13:37:21 -06:00
stateman = new StateManager ;
2010-06-13 13:50:06 -06:00
return S_OK ;
}
void Close ( )
{
// release all bound resources
2011-06-11 13:37:21 -06:00
context - > ClearState ( ) ;
SAFE_RELEASE ( backbuf ) ;
SAFE_RELEASE ( swapchain ) ;
SAFE_DELETE ( stateman ) ;
context - > Flush ( ) ; // immediately destroy device objects
SAFE_RELEASE ( context ) ;
ULONG references = device - > Release ( ) ;
if ( references )
{
ERROR_LOG ( VIDEO , " Unreleased references: %i. " , references ) ;
}
else
{
NOTICE_LOG ( VIDEO , " Successfully released all device references! " ) ;
}
device = NULL ;
2010-11-21 08:34:04 -07:00
// unload DLLs
2010-11-23 12:58:02 -07:00
UnloadD3D ( ) ;
2010-11-21 08:34:04 -07:00
UnloadDXGI ( ) ;
2010-06-13 13:50:06 -06:00
}
2010-11-27 04:11:05 -07:00
const char * VertexShaderVersionString ( )
{
if ( featlevel = = D3D_FEATURE_LEVEL_11_0 ) return " vs_5_0 " ;
else if ( featlevel = = D3D_FEATURE_LEVEL_10_1 ) return " vs_4_1 " ;
else /*if(featlevel == D3D_FEATURE_LEVEL_10_0)*/ return " vs_4_0 " ;
}
2011-03-14 03:38:29 -06:00
const char * GeometryShaderVersionString ( )
{
if ( featlevel = = D3D_FEATURE_LEVEL_11_0 ) return " gs_5_0 " ;
else if ( featlevel = = D3D_FEATURE_LEVEL_10_1 ) return " gs_4_1 " ;
else /*if(featlevel == D3D_FEATURE_LEVEL_10_0)*/ return " gs_4_0 " ;
}
2010-11-27 04:11:05 -07:00
const char * PixelShaderVersionString ( )
{
if ( featlevel = = D3D_FEATURE_LEVEL_11_0 ) return " ps_5_0 " ;
else if ( featlevel = = D3D_FEATURE_LEVEL_10_1 ) return " ps_4_1 " ;
else /*if(featlevel == D3D_FEATURE_LEVEL_10_0)*/ return " ps_4_0 " ;
}
2010-06-13 13:50:06 -06:00
2011-06-11 13:37:21 -06:00
D3DTexture2D * & GetBackBuffer ( ) { return backbuf ; }
2010-06-13 13:50:06 -06:00
unsigned int GetBackBufferWidth ( ) { return xres ; }
unsigned int GetBackBufferHeight ( ) { return yres ; }
2010-06-18 19:02:43 -06:00
bool BGRATexturesSupported ( ) { return bgra_textures_supported ; }
2010-07-11 10:26:46 -06:00
// Returns the maximum width/height of a texture. This value only depends upon the feature level in DX11
unsigned int GetMaxTextureSize ( )
{
switch ( featlevel )
{
case D3D_FEATURE_LEVEL_11_0 :
2010-11-27 04:11:05 -07:00
return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION ;
2010-07-11 10:26:46 -06:00
case D3D_FEATURE_LEVEL_10_1 :
case D3D_FEATURE_LEVEL_10_0 :
2010-11-27 04:11:05 -07:00
return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION ;
2010-07-11 10:26:46 -06:00
case D3D_FEATURE_LEVEL_9_3 :
return 4096 ;
case D3D_FEATURE_LEVEL_9_2 :
case D3D_FEATURE_LEVEL_9_1 :
return 2048 ;
2010-07-17 05:42:28 -06:00
default :
return 0 ;
2010-07-11 10:26:46 -06:00
}
}
2010-06-13 13:50:06 -06:00
void Reset ( )
{
2010-06-17 04:42:57 -06:00
// release all back buffer references
2011-06-11 13:37:21 -06:00
SAFE_RELEASE ( backbuf ) ;
2010-06-17 04:42:57 -06:00
// resize swapchain buffers
RECT client ;
GetClientRect ( hWnd , & client ) ;
xres = client . right - client . left ;
yres = client . bottom - client . top ;
2011-06-11 13:37:21 -06:00
D3D : : swapchain - > ResizeBuffers ( 1 , xres , yres , DXGI_FORMAT_R8G8B8A8_UNORM , 0 ) ;
2010-06-17 04:42:57 -06:00
// recreate back buffer texture
2011-06-11 13:37:21 -06:00
ID3D11Texture2D * buf ;
HRESULT hr = swapchain - > GetBuffer ( 0 , IID_ID3D11Texture2D , ( void * * ) & buf ) ;
if ( FAILED ( hr ) )
2010-06-17 04:42:57 -06:00
{
2011-02-13 19:18:03 -07:00
MessageBox ( hWnd , _T ( " Failed to get swapchain buffer " ) , _T ( " Dolphin Direct3D 11 backend " ) , MB_OK | MB_ICONERROR ) ;
2011-06-11 13:37:21 -06:00
SAFE_RELEASE ( device ) ;
SAFE_RELEASE ( context ) ;
SAFE_RELEASE ( swapchain ) ;
2010-06-17 04:42:57 -06:00
return ;
}
2011-06-11 13:37:21 -06:00
backbuf = new D3DTexture2D ( buf , D3D11_BIND_RENDER_TARGET ) ;
SAFE_RELEASE ( buf ) ;
2010-06-18 12:40:58 -06:00
CHECK ( backbuf ! = NULL , " Create back buffer texture " ) ;
2011-06-11 13:37:21 -06:00
SetDebugObjectName ( ( ID3D11DeviceChild * ) backbuf - > GetTex ( ) , " backbuffer texture " ) ;
SetDebugObjectName ( ( ID3D11DeviceChild * ) backbuf - > GetRTV ( ) , " backbuffer render target view " ) ;
2010-06-13 13:50:06 -06:00
}
bool BeginFrame ( )
{
if ( bFrameInProgress )
{
PanicAlert ( " BeginFrame called although a frame is already in progress " ) ;
return false ;
}
bFrameInProgress = true ;
2011-06-11 13:37:21 -06:00
return ( device ! = NULL ) ;
2010-06-13 13:50:06 -06:00
}
void EndFrame ( )
{
if ( ! bFrameInProgress )
{
PanicAlert ( " EndFrame called although no frame is in progress " ) ;
return ;
}
bFrameInProgress = false ;
}
void Present ( )
{
// TODO: Is 1 the correct value for vsyncing?
2013-03-18 18:41:45 -06:00
swapchain - > Present ( ( UINT ) g_ActiveConfig . IsVSync ( ) , 0 ) ;
2010-06-13 13:50:06 -06:00
}
2011-01-29 13:16:51 -07:00
} // namespace D3D
2011-02-13 19:18:03 -07:00
} // namespace DX11