DX11GraphicsApi.cpp 40 KB

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