Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / DX11ClothDemo / btDirectComputeSupport.h
1 /*\r
2 Bullet Continuous Collision Detection and Physics Library\r
3 Copyright (c) 2010 Advanced Micro Devices\r
4 \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
10 \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
14 */\r
15 \r
16 \r
17 \r
18 #ifndef BT_DIRECT_COMPUTE_SUPPORT_HPP\r
19 #define BT_DIRECT_COMPUTE_SUPPORT_HPP\r
20 \r
21 // DX11 support\r
22 #include <windows.h>\r
23 #include <crtdbg.h>\r
24 #include <d3d11.h>\r
25 #include <d3dx11.h>\r
26 #include <d3dcompiler.h>\r
27 \r
28 #ifndef SAFE_RELEASE\r
29 #define SAFE_RELEASE(p)      { if(p) { (p)->Release(); (p)=NULL; } }\r
30 #endif\r
31 \r
32 namespace BTAcceleratedSoftBody\r
33 {\r
34 \r
35         /**\r
36          * Class to provide basic DX11 support to an application, wrapping the device creation functionality and necessary pointers.\r
37          */\r
38         class DX11SupportHelper\r
39         {\r
40         private:\r
41 \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
47 \r
48 \r
49                 bool Dynamic_EnsureD3D11APIs( void )\r
50                 {\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
54                                 return true;\r
55 \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
59                         {\r
60                                 m_s_DynamicD3D11CreateDevice = ( LPD3D11CREATEDEVICE )GetProcAddress( m_s_hModD3D11, "D3D11CreateDevice" );\r
61                         }\r
62                 \r
63                         return ( m_s_hModD3D11 != NULL );\r
64                 }\r
65 \r
66                 // Helper to call D3D11CreateDevice from d3d11.dll\r
67                 HRESULT WINAPI Dynamic_D3D11CreateDevice( IDXGIAdapter* pAdapter,\r
68                                                                                                   D3D_DRIVER_TYPE DriverType,\r
69                                                                                                   HMODULE Software,\r
70                                                                                                   UINT32 Flags,\r
71                                                                                                   D3D_FEATURE_LEVEL* pFeatureLevels,\r
72                                                                                                   UINT FeatureLevels,\r
73                                                                                                   UINT32 SDKVersion,\r
74                                                                                                   ID3D11Device** ppDevice,\r
75                                                                                                   D3D_FEATURE_LEVEL* pFeatureLevel,\r
76                                                                                                   ID3D11DeviceContext** ppImmediateContext )\r
77                 {\r
78                         if( Dynamic_EnsureD3D11APIs() && m_s_DynamicD3D11CreateDevice != NULL )\r
79                         {\r
80                                 return m_s_DynamicD3D11CreateDevice( \r
81                                                 pAdapter, \r
82                                                 DriverType, \r
83                                                 Software, \r
84                                                 Flags, \r
85                                                 pFeatureLevels, \r
86                                                 FeatureLevels,\r
87                                                 SDKVersion, \r
88                                                 ppDevice, \r
89                                                 pFeatureLevel, \r
90                                                 ppImmediateContext );\r
91                         }\r
92                         else\r
93                         {\r
94                                 MessageBoxA( 0, "Could not locate resources need by d3d11."\\r
95                                         "  Please install the latest DirectX SDK.", "Error", MB_ICONEXCLAMATION );\r
96                                 return E_FAIL;\r
97                         }\r
98                 }\r
99 \r
100         public:\r
101                 DX11SupportHelper()\r
102                 {\r
103                         m_s_hModD3D11 = 0;\r
104                         m_s_DynamicD3D11CreateDevice = 0;\r
105                         m_pd3dDevice = 0;\r
106                         m_pd3dImmediateContext = 0;\r
107                 }\r
108 \r
109                 virtual ~DX11SupportHelper()\r
110                 {\r
111                         SAFE_RELEASE( m_pd3dDevice );\r
112                         SAFE_RELEASE( m_pd3dImmediateContext );\r
113                 }\r
114 \r
115                 ID3D11Device* getDevice()\r
116                 {\r
117                         return m_pd3dDevice;\r
118                 }\r
119 \r
120                 ID3D11DeviceContext* getContext()\r
121                 {\r
122                         return m_pd3dImmediateContext;\r
123                 }               \r
124                 \r
125                 /**\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
128                  */\r
129                 bool InitComputeShaderDevice()\r
130                 {\r
131                         HRESULT hr = S_OK;\r
132 \r
133                         UINT createDeviceFlags = 0;\r
134                 #ifdef _DEBUG\r
135                         createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;\r
136                 #endif\r
137 \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
142                         };\r
143 \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
147 \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
154 \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
158                         \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
162                                 if( FAILED( hr ) )\r
163                                         return (hr==S_OK);\r
164 \r
165                                 printf("Using Direct3D 11 Reference Device\n");\r
166                         }\r
167 \r
168                         bool returnVal = (hr==S_OK);\r
169 \r
170 \r
171                         return returnVal;\r
172 \r
173                 } // InitComputeShaderDevice\r
174 \r
175         }; // DX11SupportHelper\r
176 \r
177 } // namespace BTAcceleratedSoftBody\r
178 \r
179 \r
180 #endif // #ifndef BT_DIRECT_COMPUTE_SUPPORT_HPP