DX12GraphicsApi.cpp 46 KB

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