DX11GraphicsApi.cpp 39 KB

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