DX11GraphicsApi.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. #include "GraphicsApi.h"
  2. #include "TexturModel.h"
  3. #include "TexturList.h"
  4. #include "Bild.h"
  5. #include "Fenster.h"
  6. #include "Shader.h"
  7. #include "DXBuffer.h"
  8. #include "Textur.h"
  9. #include "Globals.h"
  10. #include "DLLRegister.h"
  11. #include "UIVertexShader.h"
  12. #include "UIPixelShader.h"
  13. #include "Kam3D.h"
  14. #include "Welt3D.h"
  15. #include <d3d11.h>
  16. #include <dxgi1_5.h>
  17. using namespace Framework;
  18. DirectX11::DirectX11()
  19. : GraphicsApi( DIRECTX11 ),
  20. d3d11Device( 0 ),
  21. d3d11Context( 0 ),
  22. d3d11SpawChain( 0 ),
  23. uiTextur( 0 ),
  24. vertexShader( 0 ),
  25. pixelShader( 0 ),
  26. sampleState( 0 ),
  27. rtview( 0 ),
  28. dsView( 0 ),
  29. depthStencilBuffer( 0 ),
  30. depthStencilState( 0 ),
  31. depthDisabledStencilState( 0 ),
  32. blendStateAlphaBlend( 0 ),
  33. vp( 0 ),
  34. texturModel( new TexturModel() ),
  35. texturRegister( new TexturList() ),
  36. texturRS( 0 ),
  37. meshRS( 0 ),
  38. defaultTextur( 0 ),
  39. diffuseLights( 0 ),
  40. pointLights( 0 ),
  41. vertexBuffer( 0 ),
  42. indexBuffer( 0 )
  43. {}
  44. DirectX11::~DirectX11()
  45. {
  46. if( vertexBuffer )
  47. vertexBuffer->release();
  48. if( indexBuffer )
  49. indexBuffer->release();
  50. if( diffuseLights )
  51. diffuseLights->release();
  52. if( pointLights )
  53. pointLights->release();
  54. if( defaultTextur )
  55. defaultTextur->release();
  56. if( texturRS )
  57. texturRS->Release();
  58. if( meshRS )
  59. meshRS->Release();
  60. texturModel->release();
  61. texturRegister->release();
  62. if( blendStateAlphaBlend )
  63. {
  64. blendStateAlphaBlend->Release();
  65. blendStateAlphaBlend = NULL;
  66. }
  67. if( uiTextur )
  68. {
  69. uiTextur->release();
  70. uiTextur = NULL;
  71. }
  72. if( sampleState )
  73. {
  74. sampleState->Release();
  75. sampleState = NULL;
  76. }
  77. if( pixelShader )
  78. {
  79. pixelShader->release();
  80. pixelShader = NULL;
  81. }
  82. if( vertexShader )
  83. {
  84. vertexShader->release();
  85. vertexShader = NULL;
  86. }
  87. if( depthDisabledStencilState )
  88. {
  89. depthDisabledStencilState->Release();
  90. depthDisabledStencilState = NULL;
  91. }
  92. delete vp;
  93. vp = 0;
  94. if( dsView )
  95. {
  96. dsView->Release();
  97. dsView = NULL;
  98. }
  99. if( depthStencilState )
  100. {
  101. depthStencilState->Release();
  102. depthStencilState = NULL;
  103. }
  104. if( depthStencilBuffer )
  105. {
  106. depthStencilBuffer->Release();
  107. depthStencilBuffer = NULL;
  108. }
  109. if( rtview )
  110. {
  111. rtview->Release();
  112. rtview = NULL;
  113. }
  114. if( d3d11SpawChain )
  115. {
  116. d3d11SpawChain->Release();
  117. d3d11SpawChain = NULL;
  118. }
  119. if( d3d11Device )
  120. {
  121. d3d11Device->Release();
  122. d3d11Device = NULL;
  123. }
  124. if( d3d11Context )
  125. {
  126. d3d11Context->Release();
  127. d3d11Context = NULL;
  128. getDLLRegister()->releaseDLL( "d3d11.dll" );
  129. }
  130. }
  131. typedef HRESULT( __stdcall *D3D11CreateDeviceAndSwapChainFunction )( IDXGIAdapter *, D3D_DRIVER_TYPE,
  132. HMODULE, UINT, const D3D_FEATURE_LEVEL *,
  133. UINT, UINT, const DXGI_SWAP_CHAIN_DESC *,
  134. IDXGISwapChain **, ID3D11Device **,
  135. D3D_FEATURE_LEVEL *, ID3D11DeviceContext ** );
  136. void DirectX11::initialize( WFenster *fenster, Vec2<int> backBufferSize, bool fullScreen )
  137. {
  138. if( d3d11Device )
  139. return GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  140. GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  141. //--------------------------------------------------------------------
  142. // Create Device
  143. HINSTANCE dll = getDLLRegister()->ladeDLL( "d3d11.dll", "d3d11.dll" );
  144. if( !dll )
  145. {
  146. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht gefunden werden." ), MB_ICONERROR );
  147. return;
  148. }
  149. D3D11CreateDeviceAndSwapChainFunction createDeviceAndSwapChain = (D3D11CreateDeviceAndSwapChainFunction)GetProcAddress( dll, "D3D11CreateDeviceAndSwapChain" );
  150. if( !createDeviceAndSwapChain )
  151. {
  152. getDLLRegister()->releaseDLL( "d3d11.dll" );
  153. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "Der Einstiegspunkt D3D11CreateDeviceAndSwapChain fon DirectX 11 konnte nicht gefunden werden." ), MB_ICONERROR );
  154. return;
  155. }
  156. // create a struct to hold information about the swap chain
  157. DXGI_SWAP_CHAIN_DESC scd;
  158. // clear out the struct for use
  159. ZeroMemory( &scd, sizeof( DXGI_SWAP_CHAIN_DESC ) );
  160. // fill the swap chain description struct
  161. scd.BufferCount = 1; // one back buffer
  162. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
  163. scd.OutputWindow = fenster ? fenster->getFensterHandle() : 0; // the window to be used
  164. scd.SampleDesc.Count = 1;
  165. // Set the scan line ordering and scaling to unspecified.
  166. scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  167. scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  168. scd.Windowed = !fullScreen;
  169. scd.BufferDesc.Width = this->backBufferSize.x;
  170. scd.BufferDesc.Height = this->backBufferSize.y; // windowed/full-screen mode
  171. scd.BufferDesc.RefreshRate.Denominator = 1;
  172. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
  173. // Discard the back buffer contents after presenting.
  174. scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  175. D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
  176. D3D_FEATURE_LEVEL support = D3D_FEATURE_LEVEL_11_0;
  177. // create a device, device context and swap chain using the information in the scd struct
  178. UINT flag = 0;
  179. #ifdef _DEBUG
  180. flag |= D3D11_CREATE_DEVICE_DEBUG;
  181. #endif
  182. HRESULT result = createDeviceAndSwapChain( NULL,
  183. D3D_DRIVER_TYPE_HARDWARE,
  184. NULL,
  185. flag,
  186. &featureLevel,
  187. 1,
  188. D3D11_SDK_VERSION,
  189. &scd,
  190. &d3d11SpawChain,
  191. &d3d11Device,
  192. &support,
  193. &d3d11Context );
  194. if( result != S_OK )
  195. {
  196. getDLLRegister()->releaseDLL( "d3d11.dll" );
  197. std::cout << "ERROR: D3D11CreateDeviceAndSwapChain returned " << result << "\n";
  198. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  199. return;
  200. }
  201. ID3D11Texture2D *backBufferPtr;
  202. // Get the pointer to the back buffer.
  203. result = d3d11SpawChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID *)& backBufferPtr );
  204. if( result != S_OK )
  205. {
  206. std::cout << "ERROR: d3d11SpawChain->GetBuffer returned " << result << "\n";
  207. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  208. return;
  209. }
  210. // Create the render target view with the back buffer pointer.
  211. result = d3d11Device->CreateRenderTargetView( backBufferPtr, NULL, &rtview );
  212. if( result != S_OK )
  213. {
  214. std::cout << "ERROR: d3d11Device->CreateRenderTargetView returned " << result << "\n";
  215. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  216. return;
  217. }
  218. // Release pointer to the back buffer as we no longer need it.
  219. backBufferPtr->Release();
  220. // Initialize the description of the depth buffer.
  221. D3D11_TEXTURE2D_DESC depthBufferDesc;
  222. ZeroMemory( &depthBufferDesc, sizeof( depthBufferDesc ) );
  223. // Set up the description of the depth buffer.
  224. depthBufferDesc.Width = this->backBufferSize.x;
  225. depthBufferDesc.Height = this->backBufferSize.y;
  226. depthBufferDesc.MipLevels = 1;
  227. depthBufferDesc.ArraySize = 1;
  228. depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  229. depthBufferDesc.SampleDesc.Count = 1;
  230. depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  231. depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  232. // Create the texture for the depth buffer using the filled out description.
  233. result = d3d11Device->CreateTexture2D( &depthBufferDesc, NULL, &depthStencilBuffer );
  234. if( result != S_OK )
  235. {
  236. std::cout << "ERROR: d3d11Device->CreateTexture2D returned " << result << "\n";
  237. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  238. return;
  239. }
  240. // Initialize the description of the stencil state.
  241. D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
  242. ZeroMemory( &depthStencilDesc, sizeof( depthStencilDesc ) );
  243. // Set up the description of the stencil state.
  244. depthStencilDesc.DepthEnable = true;
  245. depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  246. depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
  247. depthStencilDesc.StencilEnable = true;
  248. depthStencilDesc.StencilReadMask = 0xFF;
  249. depthStencilDesc.StencilWriteMask = 0xFF;
  250. // Stencil operations if pixel is front-facing.
  251. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  252. depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  253. depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  254. depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  255. // Stencil operations if pixel is back-facing.
  256. depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  257. depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  258. depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  259. depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  260. // Create the depth stencil state.
  261. result = d3d11Device->CreateDepthStencilState( &depthStencilDesc, &depthStencilState );
  262. if( result != S_OK )
  263. {
  264. std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n";
  265. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  266. return;
  267. }
  268. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  269. // Initialize the depth stencil view.
  270. D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  271. ZeroMemory( &depthStencilViewDesc, sizeof( depthStencilViewDesc ) );
  272. // Set up the depth stencil view description.
  273. depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  274. depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  275. // Create the depth stencil view.
  276. result = d3d11Device->CreateDepthStencilView( depthStencilBuffer, &depthStencilViewDesc, &dsView );
  277. if( result != S_OK )
  278. {
  279. std::cout << "ERROR: d3d11Device->CreateDepthStencilView returned " << result << "\n";
  280. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  281. return;
  282. }
  283. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  284. vp = new D3D11_VIEWPORT();
  285. memset( vp, 0, sizeof( D3D11_VIEWPORT ) );
  286. vp->Width = (float)this->backBufferSize.x;
  287. vp->Height = (float)this->backBufferSize.y;
  288. vp->MinDepth = 0.0f;
  289. vp->MaxDepth = 1.0f;
  290. vp->TopLeftX = 0.0f;
  291. vp->TopLeftY = 0.0f;
  292. d3d11Context->RSSetViewports( 1, vp );
  293. D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
  294. // Clear the second depth stencil state before setting the parameters.
  295. ZeroMemory( &depthDisabledStencilDesc, sizeof( depthDisabledStencilDesc ) );
  296. // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. The only difference is
  297. // that DepthEnable is set to false, all other parameters are the same as the other depth stencil state.
  298. depthDisabledStencilDesc.DepthEnable = false;
  299. depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  300. depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
  301. depthDisabledStencilDesc.StencilEnable = true;
  302. depthDisabledStencilDesc.StencilReadMask = 0xFF;
  303. depthDisabledStencilDesc.StencilWriteMask = 0xFF;
  304. depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  305. depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  306. depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  307. depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  308. depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  309. depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  310. depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  311. depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  312. // Create the state using the device.
  313. result = d3d11Device->CreateDepthStencilState( &depthDisabledStencilDesc, &depthDisabledStencilState );
  314. if( result != S_OK )
  315. {
  316. std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n";
  317. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  318. return;
  319. }
  320. //-------------------------------------------------
  321. // Shaders
  322. vertexShader = new DX11VertexShader( d3d11Device, d3d11Context );
  323. vertexShader->setCompiledByteArray( (unsigned char *)UIVertexShader, sizeof( UIVertexShader ) );
  324. pixelShader = new DX11PixelShader( d3d11Device, d3d11Context );
  325. pixelShader->setCompiledByteArray( (unsigned char *)UIPixelShader, sizeof( UIPixelShader ) );
  326. D3D11_INPUT_ELEMENT_DESC polygonLayout[ 4 ];
  327. // Create the vertex input layout description.
  328. // This setup needs to match the VertexType stucture in the ModelClass and in the shader.
  329. polygonLayout[ 0 ].SemanticName = "POSITION";
  330. polygonLayout[ 0 ].SemanticIndex = 0;
  331. polygonLayout[ 0 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  332. polygonLayout[ 0 ].InputSlot = 0;
  333. polygonLayout[ 0 ].AlignedByteOffset = 0;
  334. polygonLayout[ 0 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  335. polygonLayout[ 0 ].InstanceDataStepRate = 0;
  336. polygonLayout[ 1 ].SemanticName = "TEXCOORD";
  337. polygonLayout[ 1 ].SemanticIndex = 0;
  338. polygonLayout[ 1 ].Format = DXGI_FORMAT_R32G32_FLOAT;
  339. polygonLayout[ 1 ].InputSlot = 0;
  340. polygonLayout[ 1 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  341. polygonLayout[ 1 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  342. polygonLayout[ 1 ].InstanceDataStepRate = 0;
  343. polygonLayout[ 2 ].SemanticName = "NORMAL";
  344. polygonLayout[ 2 ].SemanticIndex = 0;
  345. polygonLayout[ 2 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  346. polygonLayout[ 2 ].InputSlot = 0;
  347. polygonLayout[ 2 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  348. polygonLayout[ 2 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  349. polygonLayout[ 2 ].InstanceDataStepRate = 0;
  350. polygonLayout[ 3 ].SemanticName = "KNOCHEN_ID";
  351. polygonLayout[ 3 ].SemanticIndex = 0;
  352. polygonLayout[ 3 ].Format = DXGI_FORMAT_R32_UINT;
  353. polygonLayout[ 3 ].InputSlot = 0;
  354. polygonLayout[ 3 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  355. polygonLayout[ 3 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  356. polygonLayout[ 3 ].InstanceDataStepRate = 0;
  357. vertexShader->erstelleInputLayout( polygonLayout, 4 );
  358. vertexShader->erstelleConstBuffer( sizeof( Mat4< float > ) * MAX_KNOCHEN_ANZ, 0 ); // matrizen für skelett annimationen
  359. vertexShader->erstelleConstBuffer( sizeof( Mat4< float > ) * 2, 1 ); // View and Projection Matrix
  360. pixelShader->erstelleConstBuffer( sizeof( float ) * 3, 0 ); // Kamera Position
  361. pixelShader->erstelleConstBuffer( sizeof( float ) * 3, 1 ); // materialkonstanten nach phong model
  362. pixelShader->erstelleConstBuffer( sizeof( int ) * 2, 2 ); // materialkonstanten nach phong model
  363. // TODO: Remove Following Test Code
  364. int lc[] = { 1, 6 };
  365. pixelShader->füllConstBuffer( (char *)lc, 2, sizeof( int ) * 2 );
  366. // Create a texture sampler state description.
  367. D3D11_SAMPLER_DESC samplerDesc;
  368. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  369. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  370. samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  371. samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  372. samplerDesc.MipLODBias = 0.0f;
  373. samplerDesc.MaxAnisotropy = 1;
  374. samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  375. samplerDesc.BorderColor[ 0 ] = 0;
  376. samplerDesc.BorderColor[ 1 ] = 0;
  377. samplerDesc.BorderColor[ 2 ] = 0;
  378. samplerDesc.BorderColor[ 3 ] = 0;
  379. samplerDesc.MinLOD = 0;
  380. samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
  381. // Create the texture sampler state.
  382. result = d3d11Device->CreateSamplerState( &samplerDesc, &sampleState );
  383. if( result != S_OK )
  384. {
  385. std::cout << "ERROR: d3d11Device->CreateSamplerState returned " << result << "\n";
  386. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  387. return;
  388. }
  389. //---------------------------------------------------------------
  390. // Framework Backbuffer Texture
  391. Bild *renderB = new Bild( 1 );
  392. renderB->setAlpha3D( 1 );
  393. renderB->neuBild( this->backBufferSize.x, this->backBufferSize.y, 0 );
  394. uiTextur = createOrGetTextur( "_f_Render_Bild", renderB );
  395. texturModel->setSize( this->backBufferSize );
  396. texturModel->setTextur( uiTextur->getThis() );
  397. D3D11_BLEND_DESC blendState;
  398. ZeroMemory( &blendState, sizeof( D3D11_BLEND_DESC ) );
  399. blendState.AlphaToCoverageEnable = false;
  400. blendState.IndependentBlendEnable = false;
  401. blendState.RenderTarget[ 0 ].BlendEnable = true;
  402. blendState.RenderTarget[ 0 ].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  403. blendState.RenderTarget[ 0 ].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  404. blendState.RenderTarget[ 0 ].BlendOp = D3D11_BLEND_OP_ADD;
  405. blendState.RenderTarget[ 0 ].SrcBlendAlpha = D3D11_BLEND_ZERO;
  406. blendState.RenderTarget[ 0 ].DestBlendAlpha = D3D11_BLEND_ONE;
  407. blendState.RenderTarget[ 0 ].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  408. blendState.RenderTarget[ 0 ].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  409. d3d11Device->CreateBlendState( &blendState, &blendStateAlphaBlend );
  410. d3d11Context->OMSetBlendState( blendStateAlphaBlend, 0, 0xFFFFFFFF );
  411. // Setup Render Objekt
  412. vertexShader->benutzeShader();
  413. d3d11Context->PSSetSamplers( 0, 1, &sampleState );
  414. pixelShader->benutzeShader();
  415. D3D11_RASTERIZER_DESC rasterDesc;
  416. ZeroMemory( &rasterDesc, sizeof( rasterDesc ) );
  417. rasterDesc.AntialiasedLineEnable = false;
  418. rasterDesc.CullMode = D3D11_CULL_BACK;
  419. rasterDesc.DepthBiasClamp = 0.0f;
  420. rasterDesc.DepthClipEnable = true;
  421. rasterDesc.FillMode = D3D11_FILL_SOLID;
  422. rasterDesc.FrontCounterClockwise = false;
  423. rasterDesc.MultisampleEnable = false;
  424. rasterDesc.ScissorEnable = false;
  425. rasterDesc.SlopeScaledDepthBias = 0.0f;
  426. d3d11Device->CreateRasterizerState( &rasterDesc, &texturRS );
  427. ZeroMemory( &rasterDesc, sizeof( rasterDesc ) );
  428. rasterDesc.AntialiasedLineEnable = false;
  429. rasterDesc.CullMode = D3D11_CULL_BACK;
  430. rasterDesc.DepthBiasClamp = 0.0f;
  431. rasterDesc.DepthClipEnable = true;
  432. rasterDesc.FillMode = D3D11_FILL_WIREFRAME;
  433. rasterDesc.FrontCounterClockwise = false;
  434. rasterDesc.MultisampleEnable = false;
  435. rasterDesc.ScissorEnable = false;
  436. rasterDesc.SlopeScaledDepthBias = 0.0f;
  437. d3d11Device->CreateRasterizerState( &rasterDesc, &meshRS );
  438. d3d11Context->RSSetState( texturRS );
  439. Bild *b = new Bild();
  440. b->neuBild( 10, 10, 0xFFFFFFFF );
  441. defaultTextur = createOrGetTextur( "_default_textur", b );
  442. vertexBuffer = new DX11Buffer( sizeof( Vertex3D ), d3d11Device, d3d11Context, D3D11_BIND_VERTEX_BUFFER );
  443. indexBuffer = new DX11Buffer( sizeof( int ), d3d11Device, d3d11Context, D3D11_BIND_INDEX_BUFFER );
  444. DiffuseLight dl[ 1 ];
  445. dl[ 0 ].direction = Vec3< float >( -0.5f, -0.5f, -0.5f ).normalize();
  446. dl[ 0 ].color = Vec3<float>( 1.f, 0.f, 0.f );
  447. diffuseLights = new DX11StructuredBuffer( sizeof( DiffuseLight ), d3d11Device, d3d11Context );
  448. diffuseLights->setData( dl );
  449. diffuseLights->setLength( sizeof( dl ) );
  450. diffuseLights->copieren();
  451. PointLight pl[ 6 ];
  452. pl[ 0 ].position = Vec3< float >( 0, 130, 0 );
  453. pl[ 0 ].color = Vec3< float >( 1.f, 1.f, 0.f );
  454. pl[ 0 ].radius = 100;
  455. pl[ 1 ].position = Vec3< float >( 150, 130, 0 );
  456. pl[ 1 ].color = Vec3< float >( 0.f, 1.f, 0.f );
  457. pl[ 1 ].radius = 100;
  458. pl[ 2 ].position = Vec3< float >( 150, 130, 150 );
  459. pl[ 2 ].color = Vec3< float >( 0.f, 0.f, 1.f );
  460. pl[ 2 ].radius = 100;
  461. pl[ 3 ].position = Vec3< float >( -150, 130, 0 );
  462. pl[ 3 ].color = Vec3< float >( 1.f, 0.f, 1.f );
  463. pl[ 3 ].radius = 100;
  464. pl[ 4 ].position = Vec3< float >( 0, 130, 150 );
  465. pl[ 4 ].color = Vec3< float >( 0.f, 1.f, 1.f );
  466. pl[ 4 ].radius = 100;
  467. pl[ 5 ].position = Vec3< float >( -150, 130, 150 );
  468. pl[ 5 ].color = Vec3< float >( 1.f, 0.f, 0.f );
  469. pl[ 5 ].radius = 100;
  470. pointLights = new DX11StructuredBuffer( sizeof( PointLight ), d3d11Device, d3d11Context );
  471. pointLights->setData( pl );
  472. pointLights->setLength( sizeof( pl ) * 6 );
  473. pointLights->copieren();
  474. }
  475. void DirectX11::update()
  476. {
  477. if( pointLights )
  478. pointLights = (DX11StructuredBuffer *)pointLights->release();
  479. if( diffuseLights )
  480. diffuseLights = (DX11StructuredBuffer *)diffuseLights->release();
  481. if( vertexBuffer )
  482. vertexBuffer = (DX11Buffer *)vertexBuffer->release();
  483. if( indexBuffer )
  484. indexBuffer = (DX11Buffer *)indexBuffer->release();
  485. if( texturRS )
  486. {
  487. texturRS->Release();
  488. texturRS = NULL;
  489. }
  490. if( meshRS )
  491. {
  492. meshRS->Release();
  493. meshRS = NULL;
  494. }
  495. texturRegister->leeren();
  496. if( defaultTextur )
  497. defaultTextur = defaultTextur->release();
  498. if( blendStateAlphaBlend )
  499. {
  500. blendStateAlphaBlend->Release();
  501. blendStateAlphaBlend = NULL;
  502. }
  503. if( uiTextur )
  504. {
  505. uiTextur->release();
  506. uiTextur = NULL;
  507. }
  508. if( sampleState )
  509. {
  510. sampleState->Release();
  511. sampleState = NULL;
  512. }
  513. if( pixelShader )
  514. {
  515. pixelShader->release();
  516. pixelShader = NULL;
  517. }
  518. if( vertexShader )
  519. {
  520. vertexShader->release();
  521. vertexShader = NULL;
  522. }
  523. if( depthDisabledStencilState )
  524. {
  525. depthDisabledStencilState->Release();
  526. depthDisabledStencilState = NULL;
  527. }
  528. delete vp;
  529. vp = 0;
  530. if( dsView )
  531. {
  532. dsView->Release();
  533. dsView = NULL;
  534. }
  535. if( depthStencilState )
  536. {
  537. depthStencilState->Release();
  538. depthStencilState = NULL;
  539. }
  540. if( depthStencilBuffer )
  541. {
  542. depthStencilBuffer->Release();
  543. depthStencilBuffer = NULL;
  544. }
  545. if( rtview )
  546. {
  547. rtview->Release();
  548. rtview = NULL;
  549. }
  550. if( d3d11SpawChain )
  551. {
  552. d3d11SpawChain->Release();
  553. d3d11SpawChain = NULL;
  554. }
  555. if( d3d11Device )
  556. {
  557. d3d11Device->Release();
  558. d3d11Device = NULL;
  559. }
  560. if( d3d11Context )
  561. {
  562. d3d11Context->Release();
  563. d3d11Context = NULL;
  564. }
  565. initialize( fenster->getThis(), backBufferSize, fullScreen );
  566. }
  567. void DirectX11::beginFrame( bool fill2D, bool fill3D, int fillColor )
  568. {
  569. if( fill2D )
  570. uiTextur->zBild()->setFarbe( fillColor );
  571. if( fill3D )
  572. {
  573. float color[ 4 ];
  574. // Setup the color to clear the buffer.
  575. color[ 0 ] = ( ( fillColor >> 16 ) & 0xFF ) / 255.f; // R
  576. color[ 1 ] = ( ( fillColor >> 8 ) & 0xFF ) / 255.f; // G
  577. color[ 2 ] = ( fillColor & 0xFF ) / 255.f; // B
  578. color[ 3 ] = ( ( fillColor >> 24 ) & 0xFF ) / 255.f; // A
  579. d3d11Context->ClearRenderTargetView( rtview, color );
  580. // Clear the depth buffer.
  581. d3d11Context->ClearDepthStencilView( dsView, D3D11_CLEAR_DEPTH, 1, 0 );
  582. }
  583. // Bind the render target view and depth stencil buffer to the output render pipeline.
  584. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  585. // Set the depth stencil state.
  586. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  587. }
  588. void DirectX11::renderObject( Model3D * zObj )
  589. {
  590. vertexBuffer->setData( (void *)zObj->zVertexBuffer() );
  591. vertexBuffer->setLength( sizeof( Vertex3D ) * zObj->getVertexAnzahl() );
  592. vertexBuffer->copieren();
  593. Mat4< float > trans = Mat4< float >::identity();
  594. int anz = zObj->errechneMatrizen( trans, matrixBuffer );
  595. if( vertexShader )
  596. vertexShader->füllConstBuffer( (char *)matrixBuffer, 0, sizeof( Mat4< float > ) * anz );
  597. float matirialBuffer[ 3 ]; // light factors (phong model)
  598. matirialBuffer[ 0 ] = zObj->getAmbientFactor();
  599. matirialBuffer[ 1 ] = zObj->getDiffusFactor();
  600. matirialBuffer[ 2 ] = zObj->getSpecularFactor();
  601. if( pixelShader )
  602. pixelShader->füllConstBuffer( (char *)matirialBuffer, 1, sizeof( float ) * 3 );
  603. unsigned int offset = 0;
  604. unsigned int es = (unsigned)vertexBuffer->getElementLength();
  605. ID3D11Buffer *vBuffer = vertexBuffer->zBuffer();
  606. d3d11Context->IASetVertexBuffers( 0, 1, &vBuffer, &es, &offset );
  607. Model3DTextur *zTextur = zObj->zTextur();
  608. int ind = 0;
  609. for( auto i = zObj->zModelData()->getPolygons(); i; i++ )
  610. {
  611. indexBuffer->setData( i->indexList );
  612. indexBuffer->setLength( sizeof( int ) * i->indexAnz );
  613. indexBuffer->copieren();
  614. Textur *t = zTextur->zPolygonTextur( ind );
  615. if( t && t->brauchtUpdate() )
  616. t->updateTextur();
  617. DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
  618. if( indexBuffer->getElementLength() == 2 )
  619. f = DXGI_FORMAT_R16_UINT;
  620. if( indexBuffer->getElementLength() == 1 )
  621. f = DXGI_FORMAT_R8_UINT;
  622. d3d11Context->IASetIndexBuffer( indexBuffer->zBuffer(), f, 0 );
  623. d3d11Context->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  624. if( t )
  625. {
  626. ID3D11ShaderResourceView *v[ 3 ];
  627. v[ 0 ] = *(DX11Textur *)t;
  628. v[ 1 ] = *diffuseLights;
  629. v[ 2 ] = *pointLights;
  630. d3d11Context->PSSetShaderResources( 0, 3, v );
  631. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );
  632. }
  633. else
  634. {
  635. d3d11Context->RSSetState( meshRS );
  636. ID3D11ShaderResourceView *v[ 3 ];
  637. v[ 0 ] = *(DX11Textur *)defaultTextur;
  638. v[ 1 ] = *diffuseLights;
  639. v[ 2 ] = *pointLights;
  640. d3d11Context->PSSetShaderResources( 0, 3, v );
  641. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );
  642. d3d11Context->RSSetState( texturRS );
  643. }
  644. ind++;
  645. }
  646. }
  647. // Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet werden muss
  648. // pos: Der Mittelpunkt der Kugel
  649. // radius: Der Radius der Kugel
  650. // dist: Einen Zeiger auf einen float, in dem das quadrat des Abstands zur Kammeraposition gespeichert wird, falls diese Funktion true zurückgiebt und der Zeiger nicht 0 ist
  651. bool DirectX11::isInFrustrum( const Vec3< float > & pos, float radius, float *dist ) const
  652. {
  653. for( int i = 0; i < 6; i++ )
  654. {
  655. if( frustrum[ i ] * pos + radius < 0 )
  656. return 0;
  657. }
  658. if( dist )
  659. *dist = kamPos.abstand( pos );
  660. return 1;
  661. }
  662. void DirectX11::renderKamera( Kam3D * zKamera )
  663. {
  664. d3d11Context->RSSetViewports( 1, (D3D11_VIEWPORT *)zKamera->zViewPort() );
  665. Mat4< float > tmp = zKamera->getProjectionMatrix() * zKamera->getViewMatrix();
  666. frustrum[ 0 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 0 ][ 0 ];
  667. frustrum[ 0 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 0 ][ 1 ];
  668. frustrum[ 0 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 0 ][ 2 ];
  669. frustrum[ 0 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 0 ][ 3 ];
  670. frustrum[ 1 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 0 ][ 0 ];
  671. frustrum[ 1 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 0 ][ 1 ];
  672. frustrum[ 1 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 0 ][ 2 ];
  673. frustrum[ 1 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 0 ][ 3 ];
  674. frustrum[ 2 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 1 ][ 0 ];
  675. frustrum[ 2 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 1 ][ 1 ];
  676. frustrum[ 2 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 1 ][ 2 ];
  677. frustrum[ 2 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 1 ][ 3 ];
  678. frustrum[ 3 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 1 ][ 0 ];
  679. frustrum[ 3 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 1 ][ 1 ];
  680. frustrum[ 3 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 1 ][ 2 ];
  681. frustrum[ 3 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 1 ][ 3 ];
  682. frustrum[ 4 ].x = tmp.elements[ 2 ][ 0 ];
  683. frustrum[ 4 ].y = tmp.elements[ 2 ][ 1 ];
  684. frustrum[ 4 ].z = tmp.elements[ 2 ][ 2 ];
  685. frustrum[ 4 ].w = tmp.elements[ 2 ][ 3 ];
  686. frustrum[ 5 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 2 ][ 0 ];
  687. frustrum[ 5 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 2 ][ 1 ];
  688. frustrum[ 5 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 2 ][ 2 ];
  689. frustrum[ 5 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 2 ][ 3 ];
  690. for( int i = 0; i < 6; i++ )
  691. frustrum[ i ].normalize();
  692. viewAndProj[ 0 ] = zKamera->getViewMatrix();
  693. viewAndProj[ 1 ] = zKamera->getProjectionMatrix();
  694. kamPos = zKamera->getWorldPosition();
  695. if( vertexShader )
  696. vertexShader->füllConstBuffer( (char *)viewAndProj, 1, sizeof( Mat4< float > ) * 2 );
  697. if( pixelShader )
  698. pixelShader->füllConstBuffer( (char *)& kamPos, 0, sizeof( float ) * 3 );
  699. Welt3D * w = zKamera->zWelt();
  700. w->lock();
  701. int alphaAnzahl = 0;
  702. int maxDist = 0;
  703. int minDist = 0x7FFFFFFF;
  704. for( auto obj = w->getMembers(); obj; obj++ )
  705. {
  706. float dist;
  707. if( isInFrustrum( obj->getPos(), obj->getRadius(), &dist ) )
  708. {
  709. if( (int)dist > maxDist )
  710. maxDist = (int)dist;
  711. if( minDist < (int)dist )
  712. minDist = (int)dist;
  713. if( obj->hatAlpha() )
  714. alphaAnzahl++;
  715. else
  716. renderObject( obj._ );
  717. }
  718. }
  719. maxDist++;
  720. if( alphaAnzahl )
  721. {
  722. int size = maxDist - minDist;
  723. int *index = new int[ size ];
  724. Model3D **sorted = new Model3D * [ size * alphaAnzahl ];
  725. for( auto obj = w->getMembers(); obj; obj++ )
  726. {
  727. float dist;
  728. if( isInFrustrum( obj->getPos(), obj->getRadius(), &dist ) )
  729. {
  730. if( obj->hatAlpha() )
  731. {
  732. int pos = (int)dist - minDist;
  733. sorted[ pos * alphaAnzahl + index[ pos ]++ ] = obj._;
  734. }
  735. }
  736. }
  737. for( int i = 0; i < size; i++ )
  738. {
  739. for( int j = 0; j < index[ i ]; j++ )
  740. {
  741. renderObject( sorted[ i * alphaAnzahl + j ] );
  742. }
  743. }
  744. delete[] index;
  745. delete[] sorted;
  746. }
  747. w->unlock();
  748. }
  749. void DirectX11::presentFrame()
  750. {
  751. // Set the depth stencil state.
  752. d3d11Context->OMSetDepthStencilState( depthDisabledStencilState, 1 );
  753. uiTextur->updateTextur();
  754. d3d11Context->RSSetViewports( 1, vp );
  755. float screenAspect = (float)backBufferSize.x / (float)backBufferSize.y;
  756. Mat4< float > view = view.translation( Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f ) );
  757. viewAndProj[ 0 ] = view;
  758. viewAndProj[ 1 ] = view.projektion( (float)PI / 4.0f, screenAspect, 0.1f, 10000.f );
  759. kamPos = Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f );
  760. if( vertexShader )
  761. vertexShader->füllConstBuffer( (char *)viewAndProj, 1, sizeof( Mat4< float > ) * 2 );
  762. if( pixelShader )
  763. pixelShader->füllConstBuffer( (char *)& kamPos, 0, sizeof( float ) * 3 );
  764. if( fenster && !IsIconic( fenster->getFensterHandle() ) )
  765. renderObject( texturModel );
  766. HRESULT result = d3d11SpawChain->Present( 0, 0 );
  767. if( !SUCCEEDED( result ) )
  768. {
  769. update();
  770. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  771. }
  772. }
  773. Bild *DirectX11::zUIRenderBild() const
  774. {
  775. return uiTextur->zBild();
  776. }
  777. Textur *DirectX11::createOrGetTextur( const char *name, Bild * b )
  778. {
  779. if( !d3d11Device )
  780. {
  781. if( b )
  782. b->release();
  783. return 0;
  784. }
  785. if( texturRegister->hatTextur( name ) )
  786. {
  787. Textur *ret = texturRegister->getTextur( name );
  788. if( b )
  789. ret->setBildZ( b );
  790. return ret;
  791. }
  792. Textur *ret = new DX11Textur( d3d11Device, d3d11Context );
  793. if( b )
  794. ret->setBildZ( b );
  795. texturRegister->addTextur( ret->getThis(), name );
  796. return ret;
  797. }
  798. typedef HRESULT( __stdcall * CreateDXGIFactory2Function )( UINT, REFIID, void ** );
  799. typedef HRESULT( __stdcall * D3D11CreateDeviceFunction )( IDXGIAdapter *, D3D_DRIVER_TYPE, HMODULE, UINT, D3D_FEATURE_LEVEL *,
  800. UINT, UINT, ID3D11Device * *, D3D_FEATURE_LEVEL *, ID3D11DeviceContext * * );
  801. bool DirectX11::isAvailable()
  802. {
  803. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL( "dxgi.dll", "dxgi.dll" );
  804. if( !dxgiDLL )
  805. return 0;
  806. HINSTANCE d3d11DLL = getDLLRegister()->ladeDLL( "d3d11.dll", "d3d11.dll" );
  807. if( !d3d11DLL )
  808. {
  809. getDLLRegister()->releaseDLL( "dxgi.dll" );
  810. return 0;
  811. }
  812. CreateDXGIFactory2Function createFactory = (CreateDXGIFactory2Function)GetProcAddress( dxgiDLL, "CreateDXGIFactory2" );
  813. if( !createFactory )
  814. {
  815. getDLLRegister()->releaseDLL( "dxgi.dll" );
  816. getDLLRegister()->releaseDLL( "d3d11.dll" );
  817. return 0;
  818. }
  819. D3D11CreateDeviceFunction createDevice = (D3D11CreateDeviceFunction)GetProcAddress( d3d11DLL, "D3D11CreateDevice" );
  820. if( !createDevice )
  821. {
  822. getDLLRegister()->releaseDLL( "dxgi.dll" );
  823. getDLLRegister()->releaseDLL( "d3d11.dll" );
  824. return 0;
  825. }
  826. IDXGIFactory4 *factory;
  827. UINT createFactoryFlags = 0;
  828. #if defined(_DEBUG)
  829. createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  830. #endif
  831. HRESULT res = createFactory( createFactoryFlags, __uuidof( IDXGIFactory4 ), (void **)& factory );
  832. if( FAILED( res ) )
  833. {
  834. getDLLRegister()->releaseDLL( "dxgi.dll" );
  835. getDLLRegister()->releaseDLL( "d3d11.dll" );
  836. return 0;
  837. }
  838. int index = 0;
  839. UINT flag = 0;
  840. #ifdef _DEBUG
  841. flag |= D3D11_CREATE_DEVICE_DEBUG;
  842. #endif
  843. do
  844. {
  845. IDXGIAdapter1 *current;
  846. res = factory->EnumAdapters1( index++, &current );
  847. if( res == S_OK )
  848. {
  849. DXGI_ADAPTER_DESC1 dxgiAdapterDesc1;
  850. current->GetDesc1( &dxgiAdapterDesc1 );
  851. ID3D11Device *device = 0;
  852. ID3D11DeviceContext *context = 0;
  853. D3D_FEATURE_LEVEL level = D3D_FEATURE_LEVEL_11_0;
  854. if( ( dxgiAdapterDesc1.Flags & DXGI_ADAPTER_FLAG_SOFTWARE ) == 0 &&
  855. SUCCEEDED( createDevice( current, D3D_DRIVER_TYPE_UNKNOWN, 0, flag, &level, 1, D3D11_SDK_VERSION, &device, 0, &context ) ) )
  856. {
  857. context->Release();
  858. device->Release();
  859. current->Release();
  860. factory->Release();
  861. getDLLRegister()->releaseDLL( "dxgi.dll" );
  862. getDLLRegister()->releaseDLL( "d3d11.dll" );
  863. return 1;
  864. }
  865. current->Release();
  866. }
  867. } while( res != DXGI_ERROR_NOT_FOUND );
  868. factory->Release();
  869. getDLLRegister()->releaseDLL( "dxgi.dll" );
  870. getDLLRegister()->releaseDLL( "d3d11.dll" );
  871. return 0;
  872. }