2 Bullet Continuous Collision Detection and Physics Library
\r
3 Copyright (c) 2010 Advanced Micro Devices
\r
5 This software is provided 'as-is', without any express or implied warranty.
\r
6 In no event will the authors be held liable for any damages arising from the use of this software.
\r
7 Permission is granted to anyone to use this software for any purpose,
\r
8 including commercial applications, and to alter it and redistribute it freely,
\r
9 subject to the following restrictions:
\r
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
\r
13 3. This notice may not be removed or altered from any source distribution.
\r
18 #ifndef BT_DIRECT_COMPUTE_SUPPORT_HPP
\r
19 #define BT_DIRECT_COMPUTE_SUPPORT_HPP
\r
22 #include <windows.h>
\r
26 #include <d3dcompiler.h>
\r
28 #ifndef SAFE_RELEASE
\r
29 #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
\r
32 namespace BTAcceleratedSoftBody
\r
36 * Class to provide basic DX11 support to an application, wrapping the device creation functionality and necessary pointers.
\r
38 class DX11SupportHelper
\r
42 ID3D11Device* m_pd3dDevice;
\r
43 ID3D11DeviceContext* m_pd3dImmediateContext;
\r
44 typedef HRESULT (WINAPI * LPD3D11CREATEDEVICE)( IDXGIAdapter*, D3D_DRIVER_TYPE, HMODULE, UINT32, D3D_FEATURE_LEVEL*, UINT, UINT32, ID3D11Device**, D3D_FEATURE_LEVEL*, ID3D11DeviceContext** );
\r
45 HMODULE m_s_hModD3D11;
\r
46 LPD3D11CREATEDEVICE m_s_DynamicD3D11CreateDevice;
\r
49 bool Dynamic_EnsureD3D11APIs( void )
\r
51 // If both modules are non-NULL, this function has already been called. Note
\r
52 // that this doesn't guarantee that all ProcAddresses were found.
\r
53 if( m_s_hModD3D11 != NULL )
\r
56 // This may fail if Direct3D 11 isn't installed
\r
57 m_s_hModD3D11 = LoadLibraryA( "d3d11.dll" );
\r
58 if( m_s_hModD3D11 != NULL )
\r
60 m_s_DynamicD3D11CreateDevice = ( LPD3D11CREATEDEVICE )GetProcAddress( m_s_hModD3D11, "D3D11CreateDevice" );
\r
63 return ( m_s_hModD3D11 != NULL );
\r
66 // Helper to call D3D11CreateDevice from d3d11.dll
\r
67 HRESULT WINAPI Dynamic_D3D11CreateDevice( IDXGIAdapter* pAdapter,
\r
68 D3D_DRIVER_TYPE DriverType,
\r
71 D3D_FEATURE_LEVEL* pFeatureLevels,
\r
74 ID3D11Device** ppDevice,
\r
75 D3D_FEATURE_LEVEL* pFeatureLevel,
\r
76 ID3D11DeviceContext** ppImmediateContext )
\r
78 if( Dynamic_EnsureD3D11APIs() && m_s_DynamicD3D11CreateDevice != NULL )
\r
80 return m_s_DynamicD3D11CreateDevice(
\r
90 ppImmediateContext );
\r
94 MessageBoxA( 0, "Could not locate resources need by d3d11."\
\r
95 " Please install the latest DirectX SDK.", "Error", MB_ICONEXCLAMATION );
\r
101 DX11SupportHelper()
\r
104 m_s_DynamicD3D11CreateDevice = 0;
\r
106 m_pd3dImmediateContext = 0;
\r
109 virtual ~DX11SupportHelper()
\r
111 SAFE_RELEASE( m_pd3dDevice );
\r
112 SAFE_RELEASE( m_pd3dImmediateContext );
\r
115 ID3D11Device* getDevice()
\r
117 return m_pd3dDevice;
\r
120 ID3D11DeviceContext* getContext()
\r
122 return m_pd3dImmediateContext;
\r
126 * Do a simplistic initialisation of the first DirectCompute device in the system.
\r
127 * Clearly the user can do their own version for more flexibility.
\r
129 bool InitComputeShaderDevice()
\r
133 UINT createDeviceFlags = 0;
\r
135 createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
\r
138 D3D_FEATURE_LEVEL fl[] = {
\r
139 D3D_FEATURE_LEVEL_11_0,
\r
140 D3D_FEATURE_LEVEL_10_1,
\r
141 D3D_FEATURE_LEVEL_10_0
\r
144 // Create a hardware Direct3D 11 device
\r
145 hr = Dynamic_D3D11CreateDevice( NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags,
\r
146 fl, _countof(fl), D3D11_SDK_VERSION, &m_pd3dDevice, NULL, &m_pd3dImmediateContext );
\r
148 // Check if the hardware device supports Compute Shader 4.0
\r
149 D3D11_FEATURE_DATA_D3D10_X_HARDWARE_OPTIONS hwopts;
\r
150 m_pd3dDevice->CheckFeatureSupport(D3D11_FEATURE_D3D10_X_HARDWARE_OPTIONS, &hwopts, sizeof(hwopts));
\r
151 if( !hwopts.ComputeShaders_Plus_RawAndStructuredBuffers_Via_Shader_4_x ) {
\r
152 SAFE_RELEASE( m_pd3dImmediateContext );
\r
153 SAFE_RELEASE( m_pd3dDevice );
\r
155 int result = MessageBoxA(0, "This program needs to use the Direct3D 11 reference device. This device implements the entire Direct3D 11 feature set, but runs very slowly. Do you wish to continue?", "Compute Shader Sort", MB_ICONINFORMATION | MB_YESNO);
\r
156 if( result == IDNO )
\r
157 return false;//E_FAIL;
\r
159 // Create a reference device if hardware is not available
\r
160 hr = Dynamic_D3D11CreateDevice( NULL, D3D_DRIVER_TYPE_REFERENCE, NULL, createDeviceFlags,
\r
161 fl, _countof(fl), D3D11_SDK_VERSION, &m_pd3dDevice, NULL, &m_pd3dImmediateContext );
\r
165 printf("Using Direct3D 11 Reference Device\n");
\r
168 bool returnVal = (hr==S_OK);
\r
173 } // InitComputeShaderDevice
\r
175 }; // DX11SupportHelper
\r
177 } // namespace BTAcceleratedSoftBody
\r
180 #endif // #ifndef BT_DIRECT_COMPUTE_SUPPORT_HPP