DX11GraphicsApi.cpp 35 KB

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