DX11GraphicsApi.cpp 35 KB

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