DX12GraphicsApi.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. #include <d3d11.h>
  2. #include <d3d12.h>
  3. #include <dxgi1_6.h>
  4. #include <dxgidebug.h>
  5. #include "Bild.h"
  6. #include "d3dx12.h"
  7. #include "DLLRegister.h"
  8. #include "DX12Buffer.h"
  9. #include "DX12CommandQueue.h"
  10. #include "DX12PixelShader.h"
  11. #include "DX12Shader.h"
  12. #include "DX12Textur.h"
  13. #include "DX12VertexShader.h"
  14. #include "Fenster.h"
  15. #include "Globals.h"
  16. #include "GraphicsApi.h"
  17. #include "Kam3D.h"
  18. #include "Model3D.h"
  19. #include "Model3DList.h"
  20. #include "Shader.h"
  21. #include "TexturList.h"
  22. #include "TexturModel.h"
  23. #include "Welt3D.h"
  24. using namespace Framework;
  25. DirectX12::DirectX12()
  26. : GraphicsApi(DIRECTX12),
  27. debug(0),
  28. device(0),
  29. infoQueue(0),
  30. directCommandQueue(0),
  31. copyCommandQueue(0),
  32. computeCommandQueue(0),
  33. swapChain(0),
  34. rtvHeap(0),
  35. dsvHeap(0),
  36. shaderBufferHeap(0),
  37. depthBuffer(0),
  38. backBufferIndex(0),
  39. tearing(0),
  40. viewPort(0),
  41. allowedRenderArea(0),
  42. vertexBufferView(0),
  43. indexBufferView(0),
  44. signature(0),
  45. pipeline(0),
  46. texturModel(0),
  47. uiTextur(0),
  48. texturRegister(new TexturList()),
  49. vertexShader(0),
  50. pixelShader(0)
  51. {
  52. for (int i = 0; i < 2; i++)
  53. backBuffer[i] = 0;
  54. }
  55. DirectX12::~DirectX12()
  56. {
  57. if (directCommandQueue)
  58. {
  59. directCommandQueue->flush();
  60. directCommandQueue->release();
  61. }
  62. if (copyCommandQueue)
  63. {
  64. copyCommandQueue->flush();
  65. copyCommandQueue->release();
  66. }
  67. if (computeCommandQueue)
  68. {
  69. computeCommandQueue->flush();
  70. computeCommandQueue->release();
  71. }
  72. if (depthBuffer) depthBuffer->Release();
  73. if (dsvHeap) dsvHeap->Release();
  74. if (shaderBufferHeap) shaderBufferHeap->Release();
  75. if (vertexShader) vertexShader->release();
  76. if (pixelShader) pixelShader->release();
  77. texturRegister->release();
  78. if (uiTextur) uiTextur->release();
  79. if (texturModel) texturModel->release();
  80. if (pipeline) pipeline->Release();
  81. if (signature) signature->Release();
  82. delete indexBufferView;
  83. delete vertexBufferView;
  84. delete allowedRenderArea;
  85. delete viewPort;
  86. for (int i = 0; i < 2; i++)
  87. {
  88. if (backBuffer[i]) backBuffer[i]->Release();
  89. }
  90. if (rtvHeap) rtvHeap->Release();
  91. if (swapChain) swapChain->Release();
  92. if (infoQueue) infoQueue->Release();
  93. if (device)
  94. {
  95. device->Release();
  96. getDLLRegister()->releaseDLL("dxgi.dll");
  97. getDLLRegister()->releaseDLL("d3d12.dll");
  98. }
  99. if (debug) debug->Release();
  100. }
  101. typedef HRESULT(__stdcall* CreateDXGIFactory2Function)(UINT, REFIID, void**);
  102. typedef HRESULT(__stdcall* D3D12CreateDeviceFunction)(
  103. IDXGIAdapter*, D3D_FEATURE_LEVEL, REFIID, void**);
  104. typedef HRESULT(__stdcall* D3D12GetDebugInterfaceFunction)(REFIID, void**);
  105. typedef HRESULT(__stdcall* DXGIGetDebugInterface1Function)(
  106. UINT Flags, REFIID riid, _COM_Outptr_ void** pDebug);
  107. void DirectX12::initialize(
  108. WFenster* fenster, Vec2<int> backBufferSize, bool fullScreen)
  109. {
  110. if (device)
  111. return GraphicsApi::initialize(fenster, backBufferSize, fullScreen);
  112. GraphicsApi::initialize(fenster, backBufferSize, fullScreen);
  113. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL("dxgi.dll", "dxgi.dll");
  114. if (!dxgiDLL)
  115. {
  116. WMessageBox(fenster->getFensterHandle(),
  117. new Text("Fehler"),
  118. new Text("dxgi.dll konnte nicht gefunden werden."),
  119. MB_ICONERROR);
  120. return;
  121. }
  122. HINSTANCE d3d12DLL = getDLLRegister()->ladeDLL("d3d12.dll", "d3d12.dll");
  123. if (!d3d12DLL)
  124. {
  125. getDLLRegister()->releaseDLL("dxgi.dll");
  126. WMessageBox(fenster->getFensterHandle(),
  127. new Text("Fehler"),
  128. new Text("DirectX 12 konnte nicht gefunden werden."),
  129. MB_ICONERROR);
  130. return;
  131. }
  132. CreateDXGIFactory2Function createFactory
  133. = (CreateDXGIFactory2Function)GetProcAddress(
  134. dxgiDLL, "CreateDXGIFactory2");
  135. if (!createFactory)
  136. {
  137. getDLLRegister()->releaseDLL("dxgi.dll");
  138. getDLLRegister()->releaseDLL("d3d12.dll");
  139. WMessageBox(fenster->getFensterHandle(),
  140. new Text("Fehler"),
  141. new Text("Der Einstiegspunkt CreateDXGIFactory2 fon DXGI konnte "
  142. "nicht gefunden werden."),
  143. MB_ICONERROR);
  144. return;
  145. }
  146. D3D12CreateDeviceFunction createDevice
  147. = (D3D12CreateDeviceFunction)GetProcAddress(
  148. d3d12DLL, "D3D12CreateDevice");
  149. if (!createDevice)
  150. {
  151. getDLLRegister()->releaseDLL("dxgi.dll");
  152. getDLLRegister()->releaseDLL("d3d12.dll");
  153. WMessageBox(fenster->getFensterHandle(),
  154. new Text("Fehler"),
  155. new Text("Der Einstiegspunkt D3D12CreateDevice fon DirectX 12 "
  156. "konnte nicht gefunden werden."),
  157. MB_ICONERROR);
  158. return;
  159. }
  160. PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE d3d12svrsf
  161. = (PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE)GetProcAddress(
  162. d3d12DLL, "D3D12SerializeVersionedRootSignature");
  163. PFN_D3D12_SERIALIZE_ROOT_SIGNATURE d3d12srsf
  164. = (PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)GetProcAddress(
  165. d3d12DLL, "D3D12SerializeRootSignature");
  166. bool debugDXGI = 0;
  167. #ifdef _DEBUG
  168. IDXGIInfoQueue* dxgiInfoQueue;
  169. if (debugDX)
  170. {
  171. D3D12GetDebugInterfaceFunction getDebugInterface
  172. = (D3D12GetDebugInterfaceFunction)GetProcAddress(
  173. d3d12DLL, "D3D12GetDebugInterface");
  174. if (SUCCEEDED(getDebugInterface(__uuidof(ID3D12Debug), (void**)&debug)))
  175. debug->EnableDebugLayer();
  176. DXGIGetDebugInterface1Function dxgiDebugInterface
  177. = (DXGIGetDebugInterface1Function)GetProcAddress(
  178. dxgiDLL, "DXGIGetDebugInterface1");
  179. if (SUCCEEDED(dxgiDebugInterface(
  180. 0, __uuidof(IDXGIInfoQueue), (void**)&dxgiInfoQueue)))
  181. debugDXGI = 1;
  182. }
  183. #endif
  184. IDXGIFactory6* factory;
  185. UINT createFactoryFlags = 0;
  186. #if defined(_DEBUG)
  187. if (debugDX && debugDXGI) createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  188. #endif
  189. HRESULT res = createFactory(
  190. createFactoryFlags, __uuidof(IDXGIFactory6), (void**)&factory);
  191. if (FAILED(res))
  192. {
  193. getDLLRegister()->releaseDLL("dxgi.dll");
  194. getDLLRegister()->releaseDLL("d3d12.dll");
  195. Logging::error() << "ERROR: createFactory returned " << res << "\n";
  196. WMessageBox(fenster->getFensterHandle(),
  197. new Text("Fehler"),
  198. new Text("createFactory ist Fehlgeschlagen."),
  199. MB_ICONERROR);
  200. return;
  201. }
  202. IDXGIAdapter1* adapter = 0;
  203. for (UINT adapterID = 0; DXGI_ERROR_NOT_FOUND
  204. != factory->EnumAdapterByGpuPreference(adapterID,
  205. DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
  206. __uuidof(IDXGIAdapter1),
  207. (void**)&adapter);
  208. ++adapterID)
  209. {
  210. DXGI_ADAPTER_DESC1 desc;
  211. adapter->GetDesc1(&desc);
  212. if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
  213. {
  214. // Don't select the Basic Render Driver adapter.
  215. continue;
  216. }
  217. // Check to see if the adapter supports Direct3D 12, but don't create
  218. // the actual device yet.
  219. if (SUCCEEDED(createDevice(adapter,
  220. D3D_FEATURE_LEVEL_12_1,
  221. _uuidof(ID3D12Device),
  222. nullptr)))
  223. {
  224. char buff[256] = {};
  225. printf_s(buff,
  226. L"Direct3D Adapter (%u): VID:%04X, PID:%04X - %ls\n",
  227. adapterID,
  228. desc.VendorId,
  229. desc.DeviceId,
  230. desc.Description);
  231. Logging::info() << buff;
  232. break;
  233. }
  234. }
  235. if (!adapter)
  236. {
  237. if (FAILED(factory->EnumWarpAdapter(
  238. _uuidof(IDXGIAdapter1), (void**)&adapter)))
  239. Logging::error()
  240. << "ERROR: WARP12 not available. Enable the 'Graphics "
  241. "Tools' optional feature";
  242. }
  243. if (!adapter)
  244. {
  245. factory->Release();
  246. getDLLRegister()->releaseDLL("dxgi.dll");
  247. getDLLRegister()->releaseDLL("d3d12.dll");
  248. WMessageBox(fenster->getFensterHandle(),
  249. new Text("Fehler"),
  250. new Text("Es wurde keine passende Grafigkarte gefunden."),
  251. MB_ICONERROR);
  252. return;
  253. }
  254. res = createDevice(adapter,
  255. D3D_FEATURE_LEVEL_12_1,
  256. __uuidof(ID3D12Device5),
  257. (void**)&device);
  258. adapter->Release();
  259. if (FAILED(res))
  260. {
  261. factory->Release();
  262. getDLLRegister()->releaseDLL("dxgi.dll");
  263. getDLLRegister()->releaseDLL("d3d12.dll");
  264. Logging::error() << "ERROR: createDevice returned " << res << "\n";
  265. WMessageBox(fenster->getFensterHandle(),
  266. new Text("Fehler"),
  267. new Text("createDevice ist Fehlgeschlagen."),
  268. MB_ICONERROR);
  269. return;
  270. }
  271. D3D12_FEATURE_DATA_D3D12_OPTIONS5 featureSupportData = {};
  272. device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5,
  273. &featureSupportData,
  274. sizeof(featureSupportData));
  275. if (featureSupportData.RaytracingTier < D3D12_RAYTRACING_TIER_1_0)
  276. {
  277. device->Release();
  278. factory->Release();
  279. getDLLRegister()->releaseDLL("dxgi.dll");
  280. getDLLRegister()->releaseDLL("d3d12.dll");
  281. Logging::error() << "ERROR: Raytracing is not available\n";
  282. WMessageBox(fenster->getFensterHandle(),
  283. new Text("Fehler"),
  284. new Text("Raytracing ist nicht verfügbar. DirectX12 kann nicht "
  285. "verwendet werden."),
  286. MB_ICONERROR);
  287. return;
  288. }
  289. res = device->QueryInterface(__uuidof(ID3D12InfoQueue), (void**)&infoQueue);
  290. if (SUCCEEDED(res))
  291. {
  292. if (debugDX)
  293. {
  294. infoQueue->SetBreakOnSeverity(
  295. D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
  296. infoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
  297. infoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
  298. }
  299. // Suppress individual messages by their ID
  300. D3D12_MESSAGE_ID DenyIds[] = {
  301. // D3D12_MESSAGE_ID_CLEARRENDERTARGETVIEW_MISMATCHINGCLEARVALUE, //
  302. // I'm really not sure how to avoid this message.
  303. D3D12_MESSAGE_ID_MAP_INVALID_NULLRANGE, // This warning occurs when
  304. // using capture frame while
  305. // graphics debugging.
  306. D3D12_MESSAGE_ID_UNMAP_INVALID_NULLRANGE, // This warning occurs
  307. // when using capture
  308. // frame while graphics
  309. // debugging.
  310. };
  311. D3D12_INFO_QUEUE_FILTER NewFilter = {};
  312. NewFilter.DenyList.NumSeverities = 0; //_countof( Severities );
  313. NewFilter.DenyList.pSeverityList = 0; // Severities;
  314. NewFilter.DenyList.NumIDs = _countof(DenyIds);
  315. NewFilter.DenyList.pIDList = DenyIds;
  316. infoQueue->PushStorageFilter(&NewFilter);
  317. }
  318. directCommandQueue = new DX12DirectCommandQueue(device);
  319. copyCommandQueue = new DX12CopyCommandQueue(device);
  320. computeCommandQueue = new DX12ComputeCommandQueue(device);
  321. IDXGIFactory5* fac5 = 0;
  322. factory->QueryInterface(__uuidof(IDXGIFactory5), (void**)&fac5);
  323. if (fac5)
  324. {
  325. res = fac5->CheckFeatureSupport(
  326. DXGI_FEATURE_PRESENT_ALLOW_TEARING, &tearing, sizeof(tearing));
  327. if (FAILED(res)) tearing = 0;
  328. fac5->Release();
  329. }
  330. DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
  331. swapChainDesc.Width = backBufferSize.x;
  332. swapChainDesc.Height = backBufferSize.y;
  333. swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  334. swapChainDesc.Stereo = FALSE;
  335. swapChainDesc.SampleDesc = {1, 0};
  336. swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  337. swapChainDesc.BufferCount = 2;
  338. swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
  339. swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
  340. swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
  341. swapChainDesc.Flags = tearing ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
  342. IDXGISwapChain1* tmpSwapChain;
  343. res = factory->CreateSwapChainForHwnd(directCommandQueue->getQueue(),
  344. fenster->getFensterHandle(),
  345. &swapChainDesc,
  346. 0,
  347. 0,
  348. &tmpSwapChain);
  349. if (FAILED(res))
  350. {
  351. factory->Release();
  352. Logging::error() << "ERROR: CreateSwapChainForHwnd returned " << res
  353. << "\n";
  354. WMessageBox(fenster->getFensterHandle(),
  355. new Text("Fehler"),
  356. new Text("CreateSwapChainForHwnd ist Fehlgeschlagen."),
  357. MB_ICONERROR);
  358. return;
  359. }
  360. res = tmpSwapChain->QueryInterface(
  361. __uuidof(IDXGISwapChain4), (void**)&swapChain);
  362. tmpSwapChain->Release();
  363. if (FAILED(res))
  364. {
  365. factory->Release();
  366. Logging::error() << "ERROR: QueryInterface returned " << res << "\n";
  367. WMessageBox(fenster->getFensterHandle(),
  368. new Text("Fehler"),
  369. new Text("QueryInterface ist Fehlgeschlagen."),
  370. MB_ICONERROR);
  371. return;
  372. }
  373. factory->MakeWindowAssociation(
  374. fenster->getFensterHandle(), DXGI_MWA_NO_ALT_ENTER);
  375. D3D12_DESCRIPTOR_HEAP_DESC rtvhdesc = {};
  376. rtvhdesc.NumDescriptors = 2; // back buffer count
  377. rtvhdesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
  378. res = device->CreateDescriptorHeap(
  379. &rtvhdesc, __uuidof(ID3D12DescriptorHeap), (void**)&rtvHeap);
  380. if (FAILED(res))
  381. {
  382. factory->Release();
  383. Logging::error() << "ERROR: CreateDescriptorHeap returned " << res
  384. << "\n";
  385. WMessageBox(fenster->getFensterHandle(),
  386. new Text("Fehler"),
  387. new Text("CreateDescriptorHeap ist Fehlgeschlagen."),
  388. MB_ICONERROR);
  389. return;
  390. }
  391. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize(
  392. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  393. D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle(
  394. rtvHeap->GetCPUDescriptorHandleForHeapStart());
  395. for (int i = 0; i < 2; i++)
  396. {
  397. ID3D12Resource* backBuffer;
  398. res = swapChain->GetBuffer(
  399. i, __uuidof(ID3D12Resource), (void**)&backBuffer);
  400. if (FAILED(res))
  401. {
  402. factory->Release();
  403. Logging::error() << "ERROR: GetBuffer returned " << res << "\n";
  404. WMessageBox(fenster->getFensterHandle(),
  405. new Text("Fehler"),
  406. new Text("GetBuffer ist Fehlgeschlagen."),
  407. MB_ICONERROR);
  408. return;
  409. }
  410. device->CreateRenderTargetView(backBuffer, nullptr, rtvHandle);
  411. this->backBuffer[i] = backBuffer;
  412. rtvHandle.ptr += rtvDescriptorSize;
  413. }
  414. viewPort = new D3D12_VIEWPORT();
  415. viewPort->Width = (float)this->backBufferSize.x;
  416. viewPort->Height = (float)this->backBufferSize.y;
  417. viewPort->MinDepth = 0.0f;
  418. viewPort->MaxDepth = 1.0f;
  419. viewPort->TopLeftX = 0.0f;
  420. viewPort->TopLeftY = 0.0f;
  421. allowedRenderArea = new D3D12_RECT();
  422. allowedRenderArea->left = 0;
  423. allowedRenderArea->top = 0;
  424. allowedRenderArea->right = LONG_MAX;
  425. allowedRenderArea->bottom = LONG_MAX;
  426. texturModel = new TexturModel(this);
  427. Bild* renderB = new Bild(1);
  428. renderB->setAlpha3D(1);
  429. renderB->neuBild(this->backBufferSize.x, this->backBufferSize.y, 0);
  430. uiTextur = createOrGetTextur("_f_Render_Bild", renderB);
  431. texturModel->setSize(Vec2<float>(2.f, 2.f));
  432. texturModel->setTextur(dynamic_cast<Textur*>(uiTextur->getThis()));
  433. vertexBufferView = new D3D12_VERTEX_BUFFER_VIEW();
  434. vertexBufferView->StrideInBytes = sizeof(Vertex3D);
  435. indexBufferView = new D3D12_INDEX_BUFFER_VIEW();
  436. indexBufferView->Format = DXGI_FORMAT_R32_UINT;
  437. D3D12_CLEAR_VALUE optimizedClearValue = {};
  438. optimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;
  439. optimizedClearValue.DepthStencil = {1.0f, 0};
  440. CD3DX12_HEAP_PROPERTIES heapProp
  441. = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
  442. CD3DX12_RESOURCE_DESC heapDesc
  443. = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT,
  444. this->backBufferSize.x,
  445. this->backBufferSize.y,
  446. 1,
  447. 0,
  448. 1,
  449. 0,
  450. D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
  451. res = device->CreateCommittedResource(&heapProp,
  452. D3D12_HEAP_FLAG_NONE,
  453. &heapDesc,
  454. D3D12_RESOURCE_STATE_DEPTH_WRITE,
  455. &optimizedClearValue,
  456. __uuidof(ID3D12Resource),
  457. (void**)&depthBuffer);
  458. if (FAILED(res))
  459. {
  460. factory->Release();
  461. Logging::error() << "ERROR: CreateCommittedResource returned " << res
  462. << "\n";
  463. WMessageBox(fenster->getFensterHandle(),
  464. new Text("Fehler"),
  465. new Text("CreateCommittedResource ist Fehlgeschlagen."),
  466. MB_ICONERROR);
  467. return;
  468. }
  469. D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {};
  470. dsvHeapDesc.NumDescriptors = 1;
  471. dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
  472. dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
  473. res = device->CreateDescriptorHeap(
  474. &dsvHeapDesc, __uuidof(ID3D12DescriptorHeap), (void**)&dsvHeap);
  475. if (FAILED(res))
  476. {
  477. factory->Release();
  478. Logging::error() << "ERROR: CreateDescriptorHeap returned " << res
  479. << "\n";
  480. WMessageBox(fenster->getFensterHandle(),
  481. new Text("Fehler"),
  482. new Text("CreateDescriptorHeap ist Fehlgeschlagen."),
  483. MB_ICONERROR);
  484. return;
  485. }
  486. D3D12_DEPTH_STENCIL_VIEW_DESC dsv = {};
  487. dsv.Format = DXGI_FORMAT_D32_FLOAT;
  488. dsv.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  489. dsv.Texture2D.MipSlice = 0;
  490. dsv.Flags = D3D12_DSV_FLAG_NONE;
  491. device->CreateDepthStencilView(
  492. depthBuffer, &dsv, dsvHeap->GetCPUDescriptorHandleForHeapStart());
  493. D3D12_DESCRIPTOR_HEAP_DESC sbheapDesc = {};
  494. sbheapDesc.NumDescriptors = 6;
  495. sbheapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  496. sbheapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  497. res = device->CreateDescriptorHeap(
  498. &sbheapDesc, __uuidof(ID3D12DescriptorHeap), (void**)&shaderBufferHeap);
  499. if (FAILED(res))
  500. {
  501. factory->Release();
  502. Logging::error() << "ERROR: CreateDescriptorHeap returned " << res
  503. << "\n";
  504. WMessageBox(fenster->getFensterHandle(),
  505. new Text("Fehler"),
  506. new Text("CreateDescriptorHeap ist Fehlgeschlagen."),
  507. MB_ICONERROR);
  508. return;
  509. }
  510. vertexShader
  511. = new DX12VertexShader(device, copyCommandQueue, directCommandQueue);
  512. vertexShader->setCompiledByteArray(
  513. (unsigned char*)DX12VertexShaderBytes, sizeof(DX12VertexShaderBytes));
  514. vertexShader->erstelleConstBuffer(sizeof(Mat4<float>) * 2, 0);
  515. vertexShader->erstelleConstBuffer(sizeof(Mat4<float>) * 128, 1);
  516. pixelShader
  517. = new DX12PixelShader(device, copyCommandQueue, directCommandQueue);
  518. pixelShader->setCompiledByteArray(
  519. (unsigned char*)DX12PixelShaderBytes, sizeof(DX12PixelShaderBytes));
  520. pixelShader->erstelleConstBuffer(sizeof(float) * 4, 2);
  521. pixelShader->erstelleConstBuffer(sizeof(float) * 3, 3);
  522. pixelShader->erstelleConstBuffer(sizeof(int) * 2, 4);
  523. D3D12_INPUT_ELEMENT_DESC inputLayout[] = {
  524. {"POSITION",
  525. 0, DXGI_FORMAT_R32G32B32_FLOAT,
  526. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  527. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  528. {"TEXCOORD",
  529. 0, DXGI_FORMAT_R32G32_FLOAT,
  530. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  531. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  532. {"NORMAL",
  533. 0, DXGI_FORMAT_R32G32B32_FLOAT,
  534. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  535. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  536. {"KNOCHEN_ID",
  537. 0, DXGI_FORMAT_R32_UINT,
  538. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  539. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  540. {"KNOCHEN_ID",
  541. 0, DXGI_FORMAT_R32_UINT,
  542. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  543. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}
  544. };
  545. vertexShader->erstelleInputLayout(inputLayout, 5);
  546. D3D12_FEATURE_DATA_ROOT_SIGNATURE featureData = {};
  547. featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1;
  548. if (FAILED(device->CheckFeatureSupport(
  549. D3D12_FEATURE_ROOT_SIGNATURE, &featureData, sizeof(featureData))))
  550. featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_0;
  551. D3D12_ROOT_SIGNATURE_FLAGS rootSignatureFlags
  552. = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT
  553. | D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS
  554. | D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS
  555. | D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS;
  556. D3D12_DESCRIPTOR_RANGE1 range[2];
  557. range[0].NumDescriptors = 5;
  558. range[0].BaseShaderRegister = 0;
  559. range[0].RegisterSpace = 0;
  560. range[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
  561. range[0].OffsetInDescriptorsFromTableStart
  562. = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  563. range[0].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
  564. range[1].NumDescriptors = 1;
  565. range[1].BaseShaderRegister = 0;
  566. range[1].RegisterSpace = 0;
  567. range[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  568. range[1].OffsetInDescriptorsFromTableStart
  569. = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  570. range[1].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
  571. D3D12_ROOT_PARAMETER1 rootParameters[1];
  572. rootParameters[0].ParameterType
  573. = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  574. rootParameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
  575. rootParameters[0].DescriptorTable.NumDescriptorRanges = 2;
  576. rootParameters[0].DescriptorTable.pDescriptorRanges = range;
  577. D3D12_STATIC_SAMPLER_DESC sampler = {};
  578. sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
  579. sampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  580. sampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  581. sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  582. sampler.MipLODBias = 0;
  583. sampler.MaxAnisotropy = 0;
  584. sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
  585. sampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
  586. sampler.MinLOD = 0.0f;
  587. sampler.MaxLOD = D3D12_FLOAT32_MAX;
  588. sampler.ShaderRegister = 0;
  589. sampler.RegisterSpace = 0;
  590. sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  591. D3D12_VERSIONED_ROOT_SIGNATURE_DESC rootSignatureDescription;
  592. rootSignatureDescription.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
  593. rootSignatureDescription.Desc_1_1.NumParameters = 1;
  594. rootSignatureDescription.Desc_1_1.pParameters = rootParameters;
  595. rootSignatureDescription.Desc_1_1.NumStaticSamplers = 1;
  596. rootSignatureDescription.Desc_1_1.pStaticSamplers = &sampler;
  597. rootSignatureDescription.Desc_1_1.Flags = rootSignatureFlags;
  598. ID3DBlob* rootSignature;
  599. ID3DBlob* error;
  600. res = D3DX12SerializeVersionedRootSignature(&rootSignatureDescription,
  601. featureData.HighestVersion,
  602. &rootSignature,
  603. &error,
  604. d3d12svrsf,
  605. d3d12srsf);
  606. if (FAILED(res))
  607. {
  608. factory->Release();
  609. Logging::error()
  610. << "ERROR: D3DX12SerializeVersionedRootSignature returned " << res
  611. << "\n";
  612. WMessageBox(fenster->getFensterHandle(),
  613. new Text("Fehler"),
  614. new Text(
  615. "D3DX12SerializeVersionedRootSignature ist Fehlgeschlagen."),
  616. MB_ICONERROR);
  617. if (error) error->Release();
  618. return;
  619. }
  620. res = device->CreateRootSignature(0,
  621. rootSignature->GetBufferPointer(),
  622. rootSignature->GetBufferSize(),
  623. __uuidof(ID3D12RootSignature),
  624. (void**)&signature);
  625. if (FAILED(res))
  626. {
  627. factory->Release();
  628. Logging::error() << "ERROR: CreateRootSignature returned " << res
  629. << "\n";
  630. WMessageBox(fenster->getFensterHandle(),
  631. new Text("Fehler"),
  632. new Text("CreateRootSignature ist Fehlgeschlagen."),
  633. MB_ICONERROR);
  634. return;
  635. }
  636. rootSignature->Release();
  637. D3D12_RASTERIZER_DESC rdesc;
  638. rdesc.FillMode = D3D12_FILL_MODE_SOLID;
  639. rdesc.CullMode = D3D12_CULL_MODE_BACK;
  640. rdesc.FrontCounterClockwise = 0;
  641. rdesc.DepthBias = 0;
  642. rdesc.DepthBiasClamp = 0.f;
  643. rdesc.SlopeScaledDepthBias = 0.f;
  644. rdesc.DepthClipEnable = 1;
  645. rdesc.MultisampleEnable = 0;
  646. rdesc.AntialiasedLineEnable = 0;
  647. rdesc.ForcedSampleCount = 0;
  648. rdesc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
  649. D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
  650. psoDesc.InputLayout = {vertexShader->zInputLayout(),
  651. (unsigned)vertexShader->getInputLayoutSize()};
  652. psoDesc.pRootSignature = signature;
  653. psoDesc.VS = {vertexShader->getCompiledShader(),
  654. (unsigned __int64)vertexShader->getCompiledLength()};
  655. psoDesc.PS = {pixelShader->getCompiledShader(),
  656. (unsigned __int64)pixelShader->getCompiledLength()};
  657. psoDesc.RasterizerState = rdesc;
  658. psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
  659. psoDesc.DepthStencilState.DepthEnable = FALSE;
  660. psoDesc.DepthStencilState.StencilEnable = FALSE;
  661. psoDesc.SampleMask = UINT_MAX;
  662. psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
  663. psoDesc.NumRenderTargets = 1;
  664. psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
  665. psoDesc.SampleDesc.Count = 1;
  666. D3D12_CPU_DESCRIPTOR_HANDLE sbHeapHandle
  667. = shaderBufferHeap->GetCPUDescriptorHandleForHeapStart();
  668. unsigned int incr = device->GetDescriptorHandleIncrementSize(
  669. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
  670. D3D12_CONSTANT_BUFFER_VIEW_DESC desc;
  671. vertexShader->getViewDesc(0, desc);
  672. device->CreateConstantBufferView(&desc, sbHeapHandle);
  673. res = device->GetDeviceRemovedReason();
  674. sbHeapHandle.ptr += incr;
  675. vertexShader->getViewDesc(1, desc);
  676. device->CreateConstantBufferView(&desc, sbHeapHandle);
  677. sbHeapHandle.ptr += incr;
  678. pixelShader->getViewDesc(2, desc);
  679. device->CreateConstantBufferView(&desc, sbHeapHandle);
  680. sbHeapHandle.ptr += incr;
  681. pixelShader->getViewDesc(3, desc);
  682. device->CreateConstantBufferView(&desc, sbHeapHandle);
  683. sbHeapHandle.ptr += incr;
  684. pixelShader->getViewDesc(4, desc);
  685. device->CreateConstantBufferView(&desc, sbHeapHandle);
  686. sbHeapHandle.ptr += incr;
  687. uiTextur->updateTextur();
  688. D3D12_SHADER_RESOURCE_VIEW_DESC vd;
  689. vd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  690. vd.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  691. vd.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  692. vd.Texture2D.MipLevels = 1;
  693. vd.Texture2D.MostDetailedMip = 0;
  694. vd.Texture2D.PlaneSlice = 0;
  695. vd.Texture2D.ResourceMinLODClamp = 0;
  696. device->CreateShaderResourceView(
  697. ((DX12Textur*)uiTextur)->getResource(), &vd, sbHeapHandle);
  698. directCommandQueue->execute();
  699. res = device->CreateGraphicsPipelineState(
  700. &psoDesc, __uuidof(ID3D12PipelineState), (void**)&pipeline);
  701. if (FAILED(res))
  702. {
  703. factory->Release();
  704. Logging::error() << "ERROR: CreatePipelineState returned " << res
  705. << "\n";
  706. WMessageBox(fenster->getFensterHandle(),
  707. new Text("Fehler"),
  708. new Text("CreatePipelineState ist Fehlgeschlagen."),
  709. MB_ICONERROR);
  710. return;
  711. }
  712. factory->Release();
  713. }
  714. void DirectX12::update()
  715. {
  716. if (!device || !swapChain || !directCommandQueue) return;
  717. directCommandQueue->flush();
  718. copyCommandQueue->flush();
  719. computeCommandQueue->flush();
  720. modelList->removeAll();
  721. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL("dxgi.dll", "dxgi.dll");
  722. if (!dxgiDLL)
  723. {
  724. WMessageBox(fenster->getFensterHandle(),
  725. new Text("Fehler"),
  726. new Text("dxgi.dll konnte nicht gefunden werden."),
  727. MB_ICONERROR);
  728. return;
  729. }
  730. CreateDXGIFactory2Function createFactory
  731. = (CreateDXGIFactory2Function)GetProcAddress(
  732. dxgiDLL, "CreateDXGIFactory2");
  733. if (!createFactory)
  734. {
  735. getDLLRegister()->releaseDLL("dxgi.dll");
  736. WMessageBox(fenster->getFensterHandle(),
  737. new Text("Fehler"),
  738. new Text("Der Einstiegspunkt CreateDXGIFactory2 fon DXGI konnte "
  739. "nicht gefunden werden."),
  740. MB_ICONERROR);
  741. return;
  742. }
  743. DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
  744. HRESULT res = swapChain->GetDesc(&swapChainDesc);
  745. if (FAILED(res))
  746. {
  747. getDLLRegister()->releaseDLL("dxgi.dll");
  748. Logging::error() << "ERROR: GetDesc returned " << res << "\n";
  749. WMessageBox(fenster->getFensterHandle(),
  750. new Text("Fehler"),
  751. new Text("GetDesc ist Fehlgeschlagen."),
  752. MB_ICONERROR);
  753. return;
  754. }
  755. IDXGIFactory4* factory;
  756. UINT createFactoryFlags = 0;
  757. #if defined(_DEBUG)
  758. createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  759. #endif
  760. res = createFactory(
  761. createFactoryFlags, __uuidof(IDXGIFactory4), (void**)&factory);
  762. if (FAILED(res))
  763. {
  764. getDLLRegister()->releaseDLL("dxgi.dll");
  765. Logging::error() << "ERROR: createFactory returned " << res << "\n";
  766. WMessageBox(fenster->getFensterHandle(),
  767. new Text("Fehler"),
  768. new Text("createFactory ist Fehlgeschlagen."),
  769. MB_ICONERROR);
  770. return;
  771. }
  772. for (int i = 0; i < 2; ++i)
  773. {
  774. backBuffer[i]->Release();
  775. backBuffer[i] = 0;
  776. }
  777. res = swapChain->ResizeBuffers(2,
  778. backBufferSize.x,
  779. backBufferSize.y,
  780. swapChainDesc.BufferDesc.Format,
  781. swapChainDesc.Flags);
  782. backBufferIndex = swapChain->GetCurrentBackBufferIndex();
  783. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize(
  784. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  785. D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle(
  786. rtvHeap->GetCPUDescriptorHandleForHeapStart());
  787. for (int i = 0; i < 2; i++)
  788. {
  789. ID3D12Resource* backBuffer;
  790. res = swapChain->GetBuffer(
  791. i, __uuidof(ID3D12Resource), (void**)&backBuffer);
  792. if (FAILED(res))
  793. {
  794. getDLLRegister()->releaseDLL("dxgi.dll");
  795. factory->Release();
  796. Logging::error() << "ERROR: GetBuffer returned " << res << "\n";
  797. WMessageBox(fenster->getFensterHandle(),
  798. new Text("Fehler"),
  799. new Text("GetBuffer ist Fehlgeschlagen."),
  800. MB_ICONERROR);
  801. return;
  802. }
  803. device->CreateRenderTargetView(backBuffer, nullptr, rtvHandle);
  804. this->backBuffer[i] = backBuffer;
  805. rtvHandle.ptr += rtvDescriptorSize;
  806. }
  807. getDLLRegister()->releaseDLL("dxgi.dll");
  808. delete viewPort;
  809. viewPort = new D3D12_VIEWPORT();
  810. viewPort->Width = (float)this->backBufferSize.x;
  811. viewPort->Height = (float)this->backBufferSize.y;
  812. viewPort->MinDepth = 0.0f;
  813. viewPort->MaxDepth = 1.0f;
  814. viewPort->TopLeftX = 0.0f;
  815. viewPort->TopLeftY = 0.0f;
  816. depthBuffer->Release();
  817. D3D12_CLEAR_VALUE optimizedClearValue = {};
  818. optimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;
  819. optimizedClearValue.DepthStencil = {1.0f, 0};
  820. CD3DX12_HEAP_PROPERTIES heapProps
  821. = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
  822. CD3DX12_RESOURCE_DESC heapDesc
  823. = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT,
  824. this->backBufferSize.x,
  825. this->backBufferSize.y,
  826. 1,
  827. 0,
  828. 1,
  829. 0,
  830. D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
  831. res = device->CreateCommittedResource(&heapProps,
  832. D3D12_HEAP_FLAG_NONE,
  833. &heapDesc,
  834. D3D12_RESOURCE_STATE_DEPTH_WRITE,
  835. 0,
  836. __uuidof(ID3D12Resource),
  837. (void**)&depthBuffer);
  838. if (FAILED(res))
  839. {
  840. factory->Release();
  841. Logging::error() << "ERROR: CreateCommittedResource returned " << res
  842. << "\n";
  843. WMessageBox(fenster->getFensterHandle(),
  844. new Text("Fehler"),
  845. new Text("CreateCommittedResource ist Fehlgeschlagen."),
  846. MB_ICONERROR);
  847. return;
  848. }
  849. D3D12_DEPTH_STENCIL_VIEW_DESC dsv = {};
  850. dsv.Format = DXGI_FORMAT_D32_FLOAT;
  851. dsv.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  852. dsv.Texture2D.MipSlice = 0;
  853. dsv.Flags = D3D12_DSV_FLAG_NONE;
  854. device->CreateDepthStencilView(
  855. depthBuffer, &dsv, dsvHeap->GetCPUDescriptorHandleForHeapStart());
  856. if (uiTextur) uiTextur->release();
  857. Bild* renderB = new Bild(1);
  858. renderB->setAlpha3D(1);
  859. renderB->neuBild(this->backBufferSize.x, this->backBufferSize.y, 0);
  860. uiTextur = createOrGetTextur("_f_Render_Bild", renderB);
  861. texturModel->setTextur(dynamic_cast<Textur*>(uiTextur->getThis()));
  862. factory->Release();
  863. }
  864. void DirectX12::beginFrame(bool fill2D, bool fill3D, int fillColor)
  865. {
  866. D3D12_RESOURCE_BARRIER barrier;
  867. ZeroMemory(&barrier, sizeof(barrier));
  868. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  869. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  870. barrier.Transition.pResource = this->backBuffer[backBufferIndex];
  871. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  872. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  873. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  874. directCommandQueue->getCommandList()->ResourceBarrier(1, &barrier);
  875. if (fill2D) uiTextur->zBild()->setFarbe(fillColor);
  876. if (fill3D)
  877. {
  878. float color[4];
  879. // Setup the color to clear the buffer.
  880. color[0] = ((fillColor >> 16) & 0xFF) / 255.f; // R
  881. color[1] = ((fillColor >> 8) & 0xFF) / 255.f; // G
  882. color[2] = (fillColor & 0xFF) / 255.f; // B
  883. color[3] = ((fillColor >> 24) & 0xFF) / 255.f; // A
  884. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize(
  885. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  886. D3D12_CPU_DESCRIPTOR_HANDLE rtv
  887. = rtvHeap->GetCPUDescriptorHandleForHeapStart();
  888. rtv.ptr += rtvDescriptorSize * backBufferIndex;
  889. directCommandQueue->getCommandList()->OMSetRenderTargets(1, &rtv, 0, 0);
  890. directCommandQueue->getCommandList()->ClearRenderTargetView(
  891. rtv, color, 0, 0);
  892. }
  893. int lc[] = {0, 0};
  894. pixelShader->füllConstBuffer((char*)lc, 4, sizeof(int) * 2);
  895. }
  896. void DirectX12::renderObject(Model3D* zObj)
  897. {
  898. Mat4<float> trans = Mat4<float>::identity();
  899. zObj->zModelData()->zDXVertexBuffer()->copieren();
  900. zObj->zModelData()->zDXIndexBuffer()->copieren();
  901. int anz = zObj->errechneMatrizen(trans, matrixBuffer);
  902. if (vertexShader)
  903. vertexShader->füllConstBuffer(
  904. (char*)matrixBuffer, 1, sizeof(Mat4<float>) * anz);
  905. float matirialBuffer[3]; // light factors (phong model)
  906. matirialBuffer[0] = zObj->getAmbientFactor();
  907. matirialBuffer[1] = zObj->getDiffusFactor();
  908. matirialBuffer[2] = zObj->getSpecularFactor();
  909. if (pixelShader)
  910. pixelShader->füllConstBuffer(
  911. (char*)matirialBuffer, 3, sizeof(float) * 3);
  912. unsigned int offset = 0;
  913. unsigned int es
  914. = (unsigned)zObj->zModelData()->zDXVertexBuffer()->getElementLength();
  915. Model3DTextur* zTextur = zObj->zTextur();
  916. int ind = 0;
  917. zObj->beforeRender(this, vertexShader, pixelShader);
  918. for (auto i = zObj->zModelData()->getPolygons(); i; i++)
  919. {
  920. if (zObj->needRenderPolygon(ind))
  921. {
  922. Textur* t = zTextur->zPolygonTextur(ind);
  923. // if( t &&t->brauchtUpdate() )
  924. // t->updateTextur();
  925. DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
  926. if (zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 2)
  927. f = DXGI_FORMAT_R16_UINT;
  928. if (zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 1)
  929. f = DXGI_FORMAT_R8_UINT;
  930. indexBufferView->Format = f;
  931. if (t)
  932. {
  933. /*ID3D11ShaderResourceView *v[ 3 ];
  934. v[ 0 ] = *(DX11Textur *)t;
  935. v[ 1 ] = *diffuseLights;
  936. v[ 2 ] = *pointLights;
  937. d3d11Context->PSSetShaderResources( 0, 3, v );
  938. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0
  939. );*/
  940. directCommandQueue->getCommandList()->SetPipelineState(
  941. pipeline);
  942. directCommandQueue->getCommandList()->SetGraphicsRootSignature(
  943. signature);
  944. directCommandQueue->getCommandList()->SetDescriptorHeaps(
  945. 1, &shaderBufferHeap);
  946. directCommandQueue->getCommandList()
  947. ->SetGraphicsRootDescriptorTable(0,
  948. shaderBufferHeap->GetGPUDescriptorHandleForHeapStart());
  949. directCommandQueue->getCommandList()->RSSetViewports(
  950. 1, viewPort);
  951. directCommandQueue->getCommandList()->RSSetScissorRects(
  952. 1, allowedRenderArea);
  953. auto rtvDescriptorSize
  954. = device->GetDescriptorHandleIncrementSize(
  955. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  956. D3D12_CPU_DESCRIPTOR_HANDLE rtv
  957. = rtvHeap->GetCPUDescriptorHandleForHeapStart();
  958. rtv.ptr += rtvDescriptorSize * backBufferIndex;
  959. directCommandQueue->getCommandList()->OMSetRenderTargets(
  960. 1, &rtv, 0, 0);
  961. indexBufferView->SizeInBytes
  962. = zObj->zModelData()->zDXIndexBuffer()->getElementAnzahl()
  963. * zObj->zModelData()->zDXIndexBuffer()->getElementLength();
  964. indexBufferView->BufferLocation
  965. = ((DX12Buffer*)zObj->zModelData()->zDXIndexBuffer())
  966. ->zBuffer()
  967. ->GetGPUVirtualAddress();
  968. directCommandQueue->getCommandList()->IASetIndexBuffer(
  969. indexBufferView);
  970. vertexBufferView->SizeInBytes
  971. = zObj->zModelData()->zDXVertexBuffer()->getElementAnzahl()
  972. * zObj->zModelData()->zDXVertexBuffer()->getElementLength();
  973. vertexBufferView->BufferLocation
  974. = ((DX12Buffer*)zObj->zModelData()->zDXVertexBuffer())
  975. ->zBuffer()
  976. ->GetGPUVirtualAddress();
  977. directCommandQueue->getCommandList()->IASetVertexBuffers(
  978. 0, 1, vertexBufferView);
  979. directCommandQueue->getCommandList()->IASetPrimitiveTopology(
  980. D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  981. directCommandQueue->getCommandList()->DrawIndexedInstanced(
  982. zObj->zModelData()->zDXIndexBuffer()->getElementAnzahl(),
  983. 1,
  984. 0,
  985. 0,
  986. 0);
  987. }
  988. else
  989. {
  990. /*d3d11Context->RSSetState( meshRS );
  991. ID3D11ShaderResourceView *v[ 3 ];
  992. v[ 0 ] = *(DX11Textur *)defaultTextur;
  993. v[ 1 ] = *diffuseLights;
  994. v[ 2 ] = *pointLights;
  995. d3d11Context->PSSetShaderResources( 0, 3, v );
  996. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0
  997. ); d3d11Context->RSSetState( texturRS );*/
  998. // directCommandQueue->getCommandList()->DrawIndexedInstanced(
  999. // indexBuffer->getElementAnzahl(), 1, 0, 0, 0 );
  1000. }
  1001. }
  1002. ind++;
  1003. }
  1004. zObj->afterRender(this, pixelShader, vertexShader);
  1005. }
  1006. // Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet
  1007. // werden muss
  1008. // pos: Der Mittelpunkt der Kugel
  1009. // radius: Der Radius der Kugel
  1010. // dist: Einen Zeiger auf einen float, in dem das quadrat des Abstands zur
  1011. // Kammeraposition gespeichert wird, falls diese Funktion true zurückgiebt und
  1012. // der Zeiger nicht 0 ist
  1013. bool DirectX12::isInFrustrum(
  1014. const Vec3<float>& pos, float radius, float* dist) const
  1015. {
  1016. for (int i = 0; i < 6; i++)
  1017. {
  1018. if (frustrum[i] * pos + radius < 0) return 0;
  1019. }
  1020. if (dist) *dist = kamPos.abstand(pos);
  1021. return 1;
  1022. }
  1023. void DirectX12::renderKamera(Kam3D* zKamera)
  1024. {
  1025. directCommandQueue->getCommandList()->RSSetViewports(
  1026. 1, (D3D12_VIEWPORT*)zKamera->zViewPort());
  1027. Mat4<float> tmp = zKamera->getProjectionMatrix() * zKamera->getViewMatrix();
  1028. frustrum[0].x = tmp.elements[3][0] + tmp.elements[0][0];
  1029. frustrum[0].y = tmp.elements[3][1] + tmp.elements[0][1];
  1030. frustrum[0].z = tmp.elements[3][2] + tmp.elements[0][2];
  1031. frustrum[0].w = tmp.elements[3][3] + tmp.elements[0][3];
  1032. frustrum[1].x = tmp.elements[3][0] - tmp.elements[0][0];
  1033. frustrum[1].y = tmp.elements[3][1] - tmp.elements[0][1];
  1034. frustrum[1].z = tmp.elements[3][2] - tmp.elements[0][2];
  1035. frustrum[1].w = tmp.elements[3][3] - tmp.elements[0][3];
  1036. frustrum[2].x = tmp.elements[3][0] - tmp.elements[1][0];
  1037. frustrum[2].y = tmp.elements[3][1] - tmp.elements[1][1];
  1038. frustrum[2].z = tmp.elements[3][2] - tmp.elements[1][2];
  1039. frustrum[2].w = tmp.elements[3][3] - tmp.elements[1][3];
  1040. frustrum[3].x = tmp.elements[3][0] + tmp.elements[1][0];
  1041. frustrum[3].y = tmp.elements[3][1] + tmp.elements[1][1];
  1042. frustrum[3].z = tmp.elements[3][2] + tmp.elements[1][2];
  1043. frustrum[3].w = tmp.elements[3][3] + tmp.elements[1][3];
  1044. frustrum[4].x = tmp.elements[2][0];
  1045. frustrum[4].y = tmp.elements[2][1];
  1046. frustrum[4].z = tmp.elements[2][2];
  1047. frustrum[4].w = tmp.elements[2][3];
  1048. frustrum[5].x = tmp.elements[3][0] - tmp.elements[2][0];
  1049. frustrum[5].y = tmp.elements[3][1] - tmp.elements[2][1];
  1050. frustrum[5].z = tmp.elements[3][2] - tmp.elements[2][2];
  1051. frustrum[5].w = tmp.elements[3][3] - tmp.elements[2][3];
  1052. for (int i = 0; i < 6; i++)
  1053. frustrum[i].normalize();
  1054. viewAndProj[0] = zKamera->getViewMatrix();
  1055. viewAndProj[1] = zKamera->getProjectionMatrix();
  1056. kamPos = zKamera->getWorldPosition();
  1057. if (vertexShader)
  1058. vertexShader->füllConstBuffer(
  1059. (char*)viewAndProj, 0, sizeof(Mat4<float>) * 2);
  1060. if (pixelShader)
  1061. pixelShader->füllConstBuffer((char*)&kamPos, 2, sizeof(float) * 3);
  1062. Welt3D* w = zKamera->zWelt();
  1063. w->render([this](Model3D* obj) {
  1064. if (isInFrustrum(obj->getPos(), obj->getRadius())) renderObject(obj);
  1065. });
  1066. }
  1067. void DirectX12::presentFrame()
  1068. {
  1069. directCommandQueue->getCommandList()->RSSetViewports(1, viewPort);
  1070. viewAndProj[0] = Mat4<float>::identity();
  1071. viewAndProj[1] = Mat4<float>::identity();
  1072. if (vertexShader)
  1073. vertexShader->füllConstBuffer(
  1074. (char*)viewAndProj, 0, sizeof(Mat4<float>) * 2);
  1075. uiTextur->updateTextur();
  1076. if (fenster && !IsIconic(fenster->getFensterHandle()))
  1077. renderObject(texturModel);
  1078. D3D12_RESOURCE_BARRIER barrier;
  1079. ZeroMemory(&barrier, sizeof(barrier));
  1080. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  1081. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  1082. barrier.Transition.pResource = this->backBuffer[backBufferIndex];
  1083. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  1084. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  1085. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  1086. directCommandQueue->getCommandList()->ResourceBarrier(1, &barrier);
  1087. copyCommandQueue->execute();
  1088. directCommandQueue->execute();
  1089. swapChain->Present(0, 0);
  1090. backBufferIndex = swapChain->GetCurrentBackBufferIndex();
  1091. }
  1092. Textur* DirectX12::createOrGetTextur(const char* name, Bild* b)
  1093. {
  1094. if (!device)
  1095. {
  1096. if (b) b->release();
  1097. return 0;
  1098. }
  1099. if (texturRegister->hatTextur(name))
  1100. {
  1101. Textur* ret = texturRegister->getTextur(name);
  1102. if (b) ret->setBildZ(b);
  1103. return ret;
  1104. }
  1105. Textur* ret = new DX12Textur(device, copyCommandQueue, directCommandQueue);
  1106. if (b) ret->setBildZ(b);
  1107. texturRegister->addTextur(dynamic_cast<Textur*>(ret->getThis()), name);
  1108. ret->updateTextur();
  1109. copyCommandQueue->execute();
  1110. directCommandQueue->execute();
  1111. return ret;
  1112. }
  1113. Bild* DirectX12::zUIRenderBild() const
  1114. {
  1115. return uiTextur ? uiTextur->zBild() : 0;
  1116. }
  1117. bool DirectX12::isAvailable()
  1118. {
  1119. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL("dxgi.dll", "dxgi.dll");
  1120. if (!dxgiDLL) return 0;
  1121. HINSTANCE d3d12DLL = getDLLRegister()->ladeDLL("d3d12.dll", "d3d12.dll");
  1122. if (!d3d12DLL)
  1123. {
  1124. getDLLRegister()->releaseDLL("dxgi.dll");
  1125. return 0;
  1126. }
  1127. CreateDXGIFactory2Function createFactory
  1128. = (CreateDXGIFactory2Function)GetProcAddress(
  1129. dxgiDLL, "CreateDXGIFactory2");
  1130. if (!createFactory)
  1131. {
  1132. getDLLRegister()->releaseDLL("dxgi.dll");
  1133. getDLLRegister()->releaseDLL("d3d12.dll");
  1134. return 0;
  1135. }
  1136. D3D12CreateDeviceFunction createDevice
  1137. = (D3D12CreateDeviceFunction)GetProcAddress(
  1138. d3d12DLL, "D3D12CreateDevice");
  1139. if (!createDevice)
  1140. {
  1141. getDLLRegister()->releaseDLL("dxgi.dll");
  1142. getDLLRegister()->releaseDLL("d3d12.dll");
  1143. return 0;
  1144. }
  1145. #ifdef _DEBUG
  1146. D3D12GetDebugInterfaceFunction getDebugInterface
  1147. = (D3D12GetDebugInterfaceFunction)GetProcAddress(
  1148. d3d12DLL, "D3D12GetDebugInterface");
  1149. ID3D12Debug* debug = 0;
  1150. getDebugInterface(__uuidof(ID3D12Debug), (void**)&debug);
  1151. debug->EnableDebugLayer();
  1152. #endif
  1153. IDXGIFactory4* factory;
  1154. UINT createFactoryFlags = 0;
  1155. #ifdef _DEBUG
  1156. createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  1157. #endif
  1158. HRESULT res = createFactory(
  1159. createFactoryFlags, __uuidof(IDXGIFactory4), (void**)&factory);
  1160. if (FAILED(res))
  1161. {
  1162. getDLLRegister()->releaseDLL("dxgi.dll");
  1163. getDLLRegister()->releaseDLL("d3d12.dll");
  1164. return 0;
  1165. }
  1166. int index = 0;
  1167. do
  1168. {
  1169. IDXGIAdapter1* current;
  1170. res = factory->EnumAdapters1(index++, &current);
  1171. if (res == S_OK)
  1172. {
  1173. DXGI_ADAPTER_DESC1 dxgiAdapterDesc1;
  1174. current->GetDesc1(&dxgiAdapterDesc1);
  1175. ID3D12Device5* device = 0;
  1176. if ((dxgiAdapterDesc1.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) == 0
  1177. && SUCCEEDED(createDevice(current,
  1178. D3D_FEATURE_LEVEL_12_1,
  1179. __uuidof(ID3D12Device5),
  1180. (void**)&device)))
  1181. {
  1182. device->Release();
  1183. current->Release();
  1184. factory->Release();
  1185. #ifdef _DEBUG
  1186. debug->Release();
  1187. #endif
  1188. getDLLRegister()->releaseDLL("dxgi.dll");
  1189. getDLLRegister()->releaseDLL("d3d12.dll");
  1190. return 1;
  1191. }
  1192. current->Release();
  1193. }
  1194. } while (res != DXGI_ERROR_NOT_FOUND);
  1195. factory->Release();
  1196. getDLLRegister()->releaseDLL("dxgi.dll");
  1197. getDLLRegister()->releaseDLL("d3d12.dll");
  1198. return 0;
  1199. }
  1200. DXBuffer* DirectX12::createIndexBuffer()
  1201. {
  1202. return new DX12IndexBuffer(
  1203. sizeof(int), device, copyCommandQueue, directCommandQueue);
  1204. }
  1205. DXBuffer* DirectX12::createVertexBuffer()
  1206. {
  1207. return new DX12VertexBuffer(
  1208. sizeof(Vertex3D), device, copyCommandQueue, directCommandQueue);
  1209. }
  1210. DXBuffer* DirectX12::createStructuredBuffer(int eSize)
  1211. {
  1212. throw "Sorry, support for structured buffers will come eventualy"; // TODO:
  1213. // support
  1214. // structured
  1215. // buffers
  1216. }