DX11GraphicsApi.cpp 38 KB

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