DX12GraphicsApi.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. wchar_t buff[256] = {};
  225. swprintf_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. res = device->CreateCommittedResource(
  438. &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  439. D3D12_HEAP_FLAG_NONE,
  440. &CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT,
  441. this->backBufferSize.x,
  442. this->backBufferSize.y,
  443. 1,
  444. 0,
  445. 1,
  446. 0,
  447. D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
  448. D3D12_RESOURCE_STATE_DEPTH_WRITE,
  449. &optimizedClearValue,
  450. __uuidof(ID3D12Resource),
  451. (void**)&depthBuffer);
  452. if (FAILED(res))
  453. {
  454. factory->Release();
  455. std::cout << "ERROR: CreateCommittedResource returned " << res << "\n";
  456. WMessageBox(fenster->getFensterHandle(),
  457. new Text("Fehler"),
  458. new Text("CreateCommittedResource ist Fehlgeschlagen."),
  459. MB_ICONERROR);
  460. return;
  461. }
  462. D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {};
  463. dsvHeapDesc.NumDescriptors = 1;
  464. dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
  465. dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
  466. res = device->CreateDescriptorHeap(
  467. &dsvHeapDesc, __uuidof(ID3D12DescriptorHeap), (void**)&dsvHeap);
  468. if (FAILED(res))
  469. {
  470. factory->Release();
  471. std::cout << "ERROR: CreateDescriptorHeap returned " << res << "\n";
  472. WMessageBox(fenster->getFensterHandle(),
  473. new Text("Fehler"),
  474. new Text("CreateDescriptorHeap ist Fehlgeschlagen."),
  475. MB_ICONERROR);
  476. return;
  477. }
  478. D3D12_DEPTH_STENCIL_VIEW_DESC dsv = {};
  479. dsv.Format = DXGI_FORMAT_D32_FLOAT;
  480. dsv.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  481. dsv.Texture2D.MipSlice = 0;
  482. dsv.Flags = D3D12_DSV_FLAG_NONE;
  483. device->CreateDepthStencilView(
  484. depthBuffer, &dsv, dsvHeap->GetCPUDescriptorHandleForHeapStart());
  485. D3D12_DESCRIPTOR_HEAP_DESC sbheapDesc = {};
  486. sbheapDesc.NumDescriptors = 6;
  487. sbheapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
  488. sbheapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
  489. res = device->CreateDescriptorHeap(
  490. &sbheapDesc, __uuidof(ID3D12DescriptorHeap), (void**)&shaderBufferHeap);
  491. if (FAILED(res))
  492. {
  493. factory->Release();
  494. std::cout << "ERROR: CreateDescriptorHeap returned " << res << "\n";
  495. WMessageBox(fenster->getFensterHandle(),
  496. new Text("Fehler"),
  497. new Text("CreateDescriptorHeap ist Fehlgeschlagen."),
  498. MB_ICONERROR);
  499. return;
  500. }
  501. vertexShader
  502. = new DX12VertexShader(device, copyCommandQueue, directCommandQueue);
  503. vertexShader->setCompiledByteArray(
  504. (unsigned char*)DX12VertexShaderBytes, sizeof(DX12VertexShaderBytes));
  505. vertexShader->erstelleConstBuffer(sizeof(Mat4<float>) * 2, 0);
  506. vertexShader->erstelleConstBuffer(sizeof(Mat4<float>) * 128, 1);
  507. pixelShader
  508. = new DX12PixelShader(device, copyCommandQueue, directCommandQueue);
  509. pixelShader->setCompiledByteArray(
  510. (unsigned char*)DX12PixelShaderBytes, sizeof(DX12PixelShaderBytes));
  511. pixelShader->erstelleConstBuffer(sizeof(float) * 4, 2);
  512. pixelShader->erstelleConstBuffer(sizeof(float) * 3, 3);
  513. pixelShader->erstelleConstBuffer(sizeof(int) * 2, 4);
  514. D3D12_INPUT_ELEMENT_DESC inputLayout[] = {
  515. {"POSITION",
  516. 0, DXGI_FORMAT_R32G32B32_FLOAT,
  517. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  518. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  519. {"TEXCOORD",
  520. 0, DXGI_FORMAT_R32G32_FLOAT,
  521. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  522. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  523. {"NORMAL",
  524. 0, DXGI_FORMAT_R32G32B32_FLOAT,
  525. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  526. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  527. {"KNOCHEN_ID",
  528. 0, DXGI_FORMAT_R32_UINT,
  529. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  530. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
  531. {"KNOCHEN_ID",
  532. 0, DXGI_FORMAT_R32_UINT,
  533. 0, D3D12_APPEND_ALIGNED_ELEMENT,
  534. D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}
  535. };
  536. vertexShader->erstelleInputLayout(inputLayout, 5);
  537. D3D12_FEATURE_DATA_ROOT_SIGNATURE featureData = {};
  538. featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1;
  539. if (FAILED(device->CheckFeatureSupport(
  540. D3D12_FEATURE_ROOT_SIGNATURE, &featureData, sizeof(featureData))))
  541. featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_0;
  542. D3D12_ROOT_SIGNATURE_FLAGS rootSignatureFlags
  543. = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT
  544. | D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS
  545. | D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS
  546. | D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS;
  547. D3D12_DESCRIPTOR_RANGE1 range[2];
  548. range[0].NumDescriptors = 5;
  549. range[0].BaseShaderRegister = 0;
  550. range[0].RegisterSpace = 0;
  551. range[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
  552. range[0].OffsetInDescriptorsFromTableStart
  553. = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  554. range[0].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
  555. range[1].NumDescriptors = 1;
  556. range[1].BaseShaderRegister = 0;
  557. range[1].RegisterSpace = 0;
  558. range[1].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  559. range[1].OffsetInDescriptorsFromTableStart
  560. = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  561. range[1].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
  562. D3D12_ROOT_PARAMETER1 rootParameters[1];
  563. rootParameters[0].ParameterType
  564. = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  565. rootParameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
  566. rootParameters[0].DescriptorTable.NumDescriptorRanges = 2;
  567. rootParameters[0].DescriptorTable.pDescriptorRanges = range;
  568. D3D12_STATIC_SAMPLER_DESC sampler = {};
  569. sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
  570. sampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  571. sampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  572. sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  573. sampler.MipLODBias = 0;
  574. sampler.MaxAnisotropy = 0;
  575. sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
  576. sampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
  577. sampler.MinLOD = 0.0f;
  578. sampler.MaxLOD = D3D12_FLOAT32_MAX;
  579. sampler.ShaderRegister = 0;
  580. sampler.RegisterSpace = 0;
  581. sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  582. D3D12_VERSIONED_ROOT_SIGNATURE_DESC rootSignatureDescription;
  583. rootSignatureDescription.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
  584. rootSignatureDescription.Desc_1_1.NumParameters = 1;
  585. rootSignatureDescription.Desc_1_1.pParameters = rootParameters;
  586. rootSignatureDescription.Desc_1_1.NumStaticSamplers = 1;
  587. rootSignatureDescription.Desc_1_1.pStaticSamplers = &sampler;
  588. rootSignatureDescription.Desc_1_1.Flags = rootSignatureFlags;
  589. ID3DBlob* rootSignature;
  590. ID3DBlob* error;
  591. res = D3DX12SerializeVersionedRootSignature(&rootSignatureDescription,
  592. featureData.HighestVersion,
  593. &rootSignature,
  594. &error,
  595. d3d12svrsf,
  596. d3d12srsf);
  597. if (FAILED(res))
  598. {
  599. factory->Release();
  600. std::cout << "ERROR: D3DX12SerializeVersionedRootSignature returned "
  601. << res << "\n";
  602. WMessageBox(fenster->getFensterHandle(),
  603. new Text("Fehler"),
  604. new Text(
  605. "D3DX12SerializeVersionedRootSignature ist Fehlgeschlagen."),
  606. MB_ICONERROR);
  607. if (error) error->Release();
  608. return;
  609. }
  610. res = device->CreateRootSignature(0,
  611. rootSignature->GetBufferPointer(),
  612. rootSignature->GetBufferSize(),
  613. __uuidof(ID3D12RootSignature),
  614. (void**)&signature);
  615. if (FAILED(res))
  616. {
  617. factory->Release();
  618. std::cout << "ERROR: CreateRootSignature returned " << res << "\n";
  619. WMessageBox(fenster->getFensterHandle(),
  620. new Text("Fehler"),
  621. new Text("CreateRootSignature ist Fehlgeschlagen."),
  622. MB_ICONERROR);
  623. return;
  624. }
  625. rootSignature->Release();
  626. D3D12_RASTERIZER_DESC rdesc;
  627. rdesc.FillMode = D3D12_FILL_MODE_SOLID;
  628. rdesc.CullMode = D3D12_CULL_MODE_BACK;
  629. rdesc.FrontCounterClockwise = 0;
  630. rdesc.DepthBias = 0;
  631. rdesc.DepthBiasClamp = 0.f;
  632. rdesc.SlopeScaledDepthBias = 0.f;
  633. rdesc.DepthClipEnable = 1;
  634. rdesc.MultisampleEnable = 0;
  635. rdesc.AntialiasedLineEnable = 0;
  636. rdesc.ForcedSampleCount = 0;
  637. rdesc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
  638. D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
  639. psoDesc.InputLayout = {vertexShader->zInputLayout(),
  640. (unsigned)vertexShader->getInputLayoutSize()};
  641. psoDesc.pRootSignature = signature;
  642. psoDesc.VS = {vertexShader->getCompiledShader(),
  643. (unsigned __int64)vertexShader->getCompiledLength()};
  644. psoDesc.PS = {pixelShader->getCompiledShader(),
  645. (unsigned __int64)pixelShader->getCompiledLength()};
  646. psoDesc.RasterizerState = rdesc;
  647. psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
  648. psoDesc.DepthStencilState.DepthEnable = FALSE;
  649. psoDesc.DepthStencilState.StencilEnable = FALSE;
  650. psoDesc.SampleMask = UINT_MAX;
  651. psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
  652. psoDesc.NumRenderTargets = 1;
  653. psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
  654. psoDesc.SampleDesc.Count = 1;
  655. D3D12_CPU_DESCRIPTOR_HANDLE sbHeapHandle
  656. = shaderBufferHeap->GetCPUDescriptorHandleForHeapStart();
  657. unsigned int incr = device->GetDescriptorHandleIncrementSize(
  658. D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
  659. D3D12_CONSTANT_BUFFER_VIEW_DESC desc;
  660. vertexShader->getViewDesc(0, desc);
  661. device->CreateConstantBufferView(&desc, sbHeapHandle);
  662. res = device->GetDeviceRemovedReason();
  663. sbHeapHandle.ptr += incr;
  664. vertexShader->getViewDesc(1, desc);
  665. device->CreateConstantBufferView(&desc, sbHeapHandle);
  666. sbHeapHandle.ptr += incr;
  667. pixelShader->getViewDesc(2, desc);
  668. device->CreateConstantBufferView(&desc, sbHeapHandle);
  669. sbHeapHandle.ptr += incr;
  670. pixelShader->getViewDesc(3, desc);
  671. device->CreateConstantBufferView(&desc, sbHeapHandle);
  672. sbHeapHandle.ptr += incr;
  673. pixelShader->getViewDesc(4, desc);
  674. device->CreateConstantBufferView(&desc, sbHeapHandle);
  675. sbHeapHandle.ptr += incr;
  676. uiTextur->updateTextur();
  677. D3D12_SHADER_RESOURCE_VIEW_DESC vd;
  678. vd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  679. vd.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  680. vd.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  681. vd.Texture2D.MipLevels = 1;
  682. vd.Texture2D.MostDetailedMip = 0;
  683. vd.Texture2D.PlaneSlice = 0;
  684. vd.Texture2D.ResourceMinLODClamp = 0;
  685. device->CreateShaderResourceView(
  686. ((DX12Textur*)uiTextur)->getResource(), &vd, sbHeapHandle);
  687. directCommandQueue->execute();
  688. res = device->CreateGraphicsPipelineState(
  689. &psoDesc, __uuidof(ID3D12PipelineState), (void**)&pipeline);
  690. if (FAILED(res))
  691. {
  692. factory->Release();
  693. std::cout << "ERROR: CreatePipelineState returned " << res << "\n";
  694. WMessageBox(fenster->getFensterHandle(),
  695. new Text("Fehler"),
  696. new Text("CreatePipelineState ist Fehlgeschlagen."),
  697. MB_ICONERROR);
  698. return;
  699. }
  700. factory->Release();
  701. }
  702. void DirectX12::update()
  703. {
  704. if (!device || !swapChain || !directCommandQueue) return;
  705. directCommandQueue->flush();
  706. copyCommandQueue->flush();
  707. computeCommandQueue->flush();
  708. modelList->removeAll();
  709. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL("dxgi.dll", "dxgi.dll");
  710. if (!dxgiDLL)
  711. {
  712. WMessageBox(fenster->getFensterHandle(),
  713. new Text("Fehler"),
  714. new Text("dxgi.dll konnte nicht gefunden werden."),
  715. MB_ICONERROR);
  716. return;
  717. }
  718. CreateDXGIFactory2Function createFactory
  719. = (CreateDXGIFactory2Function)GetProcAddress(
  720. dxgiDLL, "CreateDXGIFactory2");
  721. if (!createFactory)
  722. {
  723. getDLLRegister()->releaseDLL("dxgi.dll");
  724. WMessageBox(fenster->getFensterHandle(),
  725. new Text("Fehler"),
  726. new Text("Der Einstiegspunkt CreateDXGIFactory2 fon DXGI konnte "
  727. "nicht gefunden werden."),
  728. MB_ICONERROR);
  729. return;
  730. }
  731. DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
  732. HRESULT res = swapChain->GetDesc(&swapChainDesc);
  733. if (FAILED(res))
  734. {
  735. getDLLRegister()->releaseDLL("dxgi.dll");
  736. std::cout << "ERROR: GetDesc returned " << res << "\n";
  737. WMessageBox(fenster->getFensterHandle(),
  738. new Text("Fehler"),
  739. new Text("GetDesc ist Fehlgeschlagen."),
  740. MB_ICONERROR);
  741. return;
  742. }
  743. IDXGIFactory4* factory;
  744. UINT createFactoryFlags = 0;
  745. #if defined(_DEBUG)
  746. createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  747. #endif
  748. res = createFactory(
  749. createFactoryFlags, __uuidof(IDXGIFactory4), (void**)&factory);
  750. if (FAILED(res))
  751. {
  752. getDLLRegister()->releaseDLL("dxgi.dll");
  753. std::cout << "ERROR: createFactory returned " << res << "\n";
  754. WMessageBox(fenster->getFensterHandle(),
  755. new Text("Fehler"),
  756. new Text("createFactory ist Fehlgeschlagen."),
  757. MB_ICONERROR);
  758. return;
  759. }
  760. for (int i = 0; i < 2; ++i)
  761. {
  762. backBuffer[i]->Release();
  763. backBuffer[i] = 0;
  764. }
  765. res = swapChain->ResizeBuffers(2,
  766. backBufferSize.x,
  767. backBufferSize.y,
  768. swapChainDesc.BufferDesc.Format,
  769. swapChainDesc.Flags);
  770. backBufferIndex = swapChain->GetCurrentBackBufferIndex();
  771. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize(
  772. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  773. D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle(
  774. rtvHeap->GetCPUDescriptorHandleForHeapStart());
  775. for (int i = 0; i < 2; i++)
  776. {
  777. ID3D12Resource* backBuffer;
  778. res = swapChain->GetBuffer(
  779. i, __uuidof(ID3D12Resource), (void**)&backBuffer);
  780. if (FAILED(res))
  781. {
  782. getDLLRegister()->releaseDLL("dxgi.dll");
  783. factory->Release();
  784. std::cout << "ERROR: GetBuffer returned " << res << "\n";
  785. WMessageBox(fenster->getFensterHandle(),
  786. new Text("Fehler"),
  787. new Text("GetBuffer ist Fehlgeschlagen."),
  788. MB_ICONERROR);
  789. return;
  790. }
  791. device->CreateRenderTargetView(backBuffer, nullptr, rtvHandle);
  792. this->backBuffer[i] = backBuffer;
  793. rtvHandle.ptr += rtvDescriptorSize;
  794. }
  795. getDLLRegister()->releaseDLL("dxgi.dll");
  796. delete viewPort;
  797. viewPort = new D3D12_VIEWPORT();
  798. viewPort->Width = (float)this->backBufferSize.x;
  799. viewPort->Height = (float)this->backBufferSize.y;
  800. viewPort->MinDepth = 0.0f;
  801. viewPort->MaxDepth = 1.0f;
  802. viewPort->TopLeftX = 0.0f;
  803. viewPort->TopLeftY = 0.0f;
  804. depthBuffer->Release();
  805. D3D12_CLEAR_VALUE optimizedClearValue = {};
  806. optimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;
  807. optimizedClearValue.DepthStencil = {1.0f, 0};
  808. res = device->CreateCommittedResource(
  809. &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  810. D3D12_HEAP_FLAG_NONE,
  811. &CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT,
  812. this->backBufferSize.x,
  813. this->backBufferSize.y,
  814. 1,
  815. 0,
  816. 1,
  817. 0,
  818. D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
  819. D3D12_RESOURCE_STATE_DEPTH_WRITE,
  820. 0,
  821. __uuidof(ID3D12Resource),
  822. (void**)&depthBuffer);
  823. if (FAILED(res))
  824. {
  825. factory->Release();
  826. std::cout << "ERROR: CreateCommittedResource returned " << res << "\n";
  827. WMessageBox(fenster->getFensterHandle(),
  828. new Text("Fehler"),
  829. new Text("CreateCommittedResource ist Fehlgeschlagen."),
  830. MB_ICONERROR);
  831. return;
  832. }
  833. D3D12_DEPTH_STENCIL_VIEW_DESC dsv = {};
  834. dsv.Format = DXGI_FORMAT_D32_FLOAT;
  835. dsv.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  836. dsv.Texture2D.MipSlice = 0;
  837. dsv.Flags = D3D12_DSV_FLAG_NONE;
  838. device->CreateDepthStencilView(
  839. depthBuffer, &dsv, dsvHeap->GetCPUDescriptorHandleForHeapStart());
  840. if (uiTextur) uiTextur->release();
  841. Bild* renderB = new Bild(1);
  842. renderB->setAlpha3D(1);
  843. renderB->neuBild(this->backBufferSize.x, this->backBufferSize.y, 0);
  844. uiTextur = createOrGetTextur("_f_Render_Bild", renderB);
  845. texturModel->setTextur(dynamic_cast<Textur*>(uiTextur->getThis()));
  846. factory->Release();
  847. }
  848. void DirectX12::beginFrame(bool fill2D, bool fill3D, int fillColor)
  849. {
  850. D3D12_RESOURCE_BARRIER barrier;
  851. ZeroMemory(&barrier, sizeof(barrier));
  852. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  853. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  854. barrier.Transition.pResource = this->backBuffer[backBufferIndex];
  855. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  856. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  857. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  858. directCommandQueue->getCommandList()->ResourceBarrier(1, &barrier);
  859. if (fill2D) uiTextur->zBild()->setFarbe(fillColor);
  860. if (fill3D)
  861. {
  862. float color[4];
  863. // Setup the color to clear the buffer.
  864. color[0] = ((fillColor >> 16) & 0xFF) / 255.f; // R
  865. color[1] = ((fillColor >> 8) & 0xFF) / 255.f; // G
  866. color[2] = (fillColor & 0xFF) / 255.f; // B
  867. color[3] = ((fillColor >> 24) & 0xFF) / 255.f; // A
  868. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize(
  869. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  870. D3D12_CPU_DESCRIPTOR_HANDLE rtv
  871. = rtvHeap->GetCPUDescriptorHandleForHeapStart();
  872. rtv.ptr += rtvDescriptorSize * backBufferIndex;
  873. directCommandQueue->getCommandList()->OMSetRenderTargets(1, &rtv, 0, 0);
  874. directCommandQueue->getCommandList()->ClearRenderTargetView(
  875. rtv, color, 0, 0);
  876. }
  877. int lc[] = {0, 0};
  878. pixelShader->füllConstBuffer((char*)lc, 4, sizeof(int) * 2);
  879. }
  880. void DirectX12::renderObject(Model3D* zObj)
  881. {
  882. Mat4<float> trans = Mat4<float>::identity();
  883. zObj->zModelData()->zDXVertexBuffer()->copieren();
  884. zObj->zModelData()->zDXIndexBuffer()->copieren();
  885. int anz = zObj->errechneMatrizen(trans, matrixBuffer);
  886. if (vertexShader)
  887. vertexShader->füllConstBuffer(
  888. (char*)matrixBuffer, 1, sizeof(Mat4<float>) * anz);
  889. float matirialBuffer[3]; // light factors (phong model)
  890. matirialBuffer[0] = zObj->getAmbientFactor();
  891. matirialBuffer[1] = zObj->getDiffusFactor();
  892. matirialBuffer[2] = zObj->getSpecularFactor();
  893. if (pixelShader)
  894. pixelShader->füllConstBuffer(
  895. (char*)matirialBuffer, 3, sizeof(float) * 3);
  896. unsigned int offset = 0;
  897. unsigned int es
  898. = (unsigned)zObj->zModelData()->zDXVertexBuffer()->getElementLength();
  899. Model3DTextur* zTextur = zObj->zTextur();
  900. int ind = 0;
  901. zObj->beforeRender(this, vertexShader, pixelShader);
  902. for (auto i = zObj->zModelData()->getPolygons(); i; i++)
  903. {
  904. if (zObj->needRenderPolygon(ind))
  905. {
  906. Textur* t = zTextur->zPolygonTextur(ind);
  907. // if( t &&t->brauchtUpdate() )
  908. // t->updateTextur();
  909. DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
  910. if (zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 2)
  911. f = DXGI_FORMAT_R16_UINT;
  912. if (zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 1)
  913. f = DXGI_FORMAT_R8_UINT;
  914. indexBufferView->Format = f;
  915. if (t)
  916. {
  917. /*ID3D11ShaderResourceView *v[ 3 ];
  918. v[ 0 ] = *(DX11Textur *)t;
  919. v[ 1 ] = *diffuseLights;
  920. v[ 2 ] = *pointLights;
  921. d3d11Context->PSSetShaderResources( 0, 3, v );
  922. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0
  923. );*/
  924. directCommandQueue->getCommandList()->SetPipelineState(
  925. pipeline);
  926. directCommandQueue->getCommandList()->SetGraphicsRootSignature(
  927. signature);
  928. directCommandQueue->getCommandList()->SetDescriptorHeaps(
  929. 1, &shaderBufferHeap);
  930. directCommandQueue->getCommandList()
  931. ->SetGraphicsRootDescriptorTable(0,
  932. shaderBufferHeap->GetGPUDescriptorHandleForHeapStart());
  933. directCommandQueue->getCommandList()->RSSetViewports(
  934. 1, viewPort);
  935. directCommandQueue->getCommandList()->RSSetScissorRects(
  936. 1, allowedRenderArea);
  937. auto rtvDescriptorSize
  938. = device->GetDescriptorHandleIncrementSize(
  939. D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
  940. D3D12_CPU_DESCRIPTOR_HANDLE rtv
  941. = rtvHeap->GetCPUDescriptorHandleForHeapStart();
  942. rtv.ptr += rtvDescriptorSize * backBufferIndex;
  943. directCommandQueue->getCommandList()->OMSetRenderTargets(
  944. 1, &rtv, 0, 0);
  945. indexBufferView->SizeInBytes
  946. = zObj->zModelData()->zDXIndexBuffer()->getElementAnzahl()
  947. * zObj->zModelData()->zDXIndexBuffer()->getElementLength();
  948. indexBufferView->BufferLocation
  949. = ((DX12Buffer*)zObj->zModelData()->zDXIndexBuffer())
  950. ->zBuffer()
  951. ->GetGPUVirtualAddress();
  952. directCommandQueue->getCommandList()->IASetIndexBuffer(
  953. indexBufferView);
  954. vertexBufferView->SizeInBytes
  955. = zObj->zModelData()->zDXVertexBuffer()->getElementAnzahl()
  956. * zObj->zModelData()->zDXVertexBuffer()->getElementLength();
  957. vertexBufferView->BufferLocation
  958. = ((DX12Buffer*)zObj->zModelData()->zDXVertexBuffer())
  959. ->zBuffer()
  960. ->GetGPUVirtualAddress();
  961. directCommandQueue->getCommandList()->IASetVertexBuffers(
  962. 0, 1, vertexBufferView);
  963. directCommandQueue->getCommandList()->IASetPrimitiveTopology(
  964. D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  965. directCommandQueue->getCommandList()->DrawIndexedInstanced(
  966. zObj->zModelData()->zDXIndexBuffer()->getElementAnzahl(),
  967. 1,
  968. 0,
  969. 0,
  970. 0);
  971. }
  972. else
  973. {
  974. /*d3d11Context->RSSetState( meshRS );
  975. ID3D11ShaderResourceView *v[ 3 ];
  976. v[ 0 ] = *(DX11Textur *)defaultTextur;
  977. v[ 1 ] = *diffuseLights;
  978. v[ 2 ] = *pointLights;
  979. d3d11Context->PSSetShaderResources( 0, 3, v );
  980. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0
  981. ); d3d11Context->RSSetState( texturRS );*/
  982. // directCommandQueue->getCommandList()->DrawIndexedInstanced(
  983. // indexBuffer->getElementAnzahl(), 1, 0, 0, 0 );
  984. }
  985. }
  986. ind++;
  987. }
  988. zObj->afterRender(this, pixelShader, vertexShader);
  989. }
  990. // Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet
  991. // werden muss
  992. // pos: Der Mittelpunkt der Kugel
  993. // radius: Der Radius der Kugel
  994. // dist: Einen Zeiger auf einen float, in dem das quadrat des Abstands zur
  995. // Kammeraposition gespeichert wird, falls diese Funktion true zurückgiebt und
  996. // der Zeiger nicht 0 ist
  997. bool DirectX12::isInFrustrum(
  998. const Vec3<float>& pos, float radius, float* dist) const
  999. {
  1000. for (int i = 0; i < 6; i++)
  1001. {
  1002. if (frustrum[i] * pos + radius < 0) return 0;
  1003. }
  1004. if (dist) *dist = kamPos.abstand(pos);
  1005. return 1;
  1006. }
  1007. void DirectX12::renderKamera(Kam3D* zKamera)
  1008. {
  1009. directCommandQueue->getCommandList()->RSSetViewports(
  1010. 1, (D3D12_VIEWPORT*)zKamera->zViewPort());
  1011. Mat4<float> tmp = zKamera->getProjectionMatrix() * zKamera->getViewMatrix();
  1012. frustrum[0].x = tmp.elements[3][0] + tmp.elements[0][0];
  1013. frustrum[0].y = tmp.elements[3][1] + tmp.elements[0][1];
  1014. frustrum[0].z = tmp.elements[3][2] + tmp.elements[0][2];
  1015. frustrum[0].w = tmp.elements[3][3] + tmp.elements[0][3];
  1016. frustrum[1].x = tmp.elements[3][0] - tmp.elements[0][0];
  1017. frustrum[1].y = tmp.elements[3][1] - tmp.elements[0][1];
  1018. frustrum[1].z = tmp.elements[3][2] - tmp.elements[0][2];
  1019. frustrum[1].w = tmp.elements[3][3] - tmp.elements[0][3];
  1020. frustrum[2].x = tmp.elements[3][0] - tmp.elements[1][0];
  1021. frustrum[2].y = tmp.elements[3][1] - tmp.elements[1][1];
  1022. frustrum[2].z = tmp.elements[3][2] - tmp.elements[1][2];
  1023. frustrum[2].w = tmp.elements[3][3] - tmp.elements[1][3];
  1024. frustrum[3].x = tmp.elements[3][0] + tmp.elements[1][0];
  1025. frustrum[3].y = tmp.elements[3][1] + tmp.elements[1][1];
  1026. frustrum[3].z = tmp.elements[3][2] + tmp.elements[1][2];
  1027. frustrum[3].w = tmp.elements[3][3] + tmp.elements[1][3];
  1028. frustrum[4].x = tmp.elements[2][0];
  1029. frustrum[4].y = tmp.elements[2][1];
  1030. frustrum[4].z = tmp.elements[2][2];
  1031. frustrum[4].w = tmp.elements[2][3];
  1032. frustrum[5].x = tmp.elements[3][0] - tmp.elements[2][0];
  1033. frustrum[5].y = tmp.elements[3][1] - tmp.elements[2][1];
  1034. frustrum[5].z = tmp.elements[3][2] - tmp.elements[2][2];
  1035. frustrum[5].w = tmp.elements[3][3] - tmp.elements[2][3];
  1036. for (int i = 0; i < 6; i++)
  1037. frustrum[i].normalize();
  1038. viewAndProj[0] = zKamera->getViewMatrix();
  1039. viewAndProj[1] = zKamera->getProjectionMatrix();
  1040. kamPos = zKamera->getWorldPosition();
  1041. if (vertexShader)
  1042. vertexShader->füllConstBuffer(
  1043. (char*)viewAndProj, 0, sizeof(Mat4<float>) * 2);
  1044. if (pixelShader)
  1045. pixelShader->füllConstBuffer((char*)&kamPos, 2, sizeof(float) * 3);
  1046. Welt3D* w = zKamera->zWelt();
  1047. w->forAll([this](Model3D* obj) {
  1048. if (isInFrustrum(obj->getPos(), obj->getRadius())) renderObject(obj);
  1049. });
  1050. }
  1051. void DirectX12::presentFrame()
  1052. {
  1053. directCommandQueue->getCommandList()->RSSetViewports(1, viewPort);
  1054. viewAndProj[0] = Mat4<float>::identity();
  1055. viewAndProj[1] = Mat4<float>::identity();
  1056. if (vertexShader)
  1057. vertexShader->füllConstBuffer(
  1058. (char*)viewAndProj, 0, sizeof(Mat4<float>) * 2);
  1059. uiTextur->updateTextur();
  1060. if (fenster && !IsIconic(fenster->getFensterHandle()))
  1061. renderObject(texturModel);
  1062. D3D12_RESOURCE_BARRIER barrier;
  1063. ZeroMemory(&barrier, sizeof(barrier));
  1064. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  1065. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  1066. barrier.Transition.pResource = this->backBuffer[backBufferIndex];
  1067. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  1068. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  1069. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  1070. directCommandQueue->getCommandList()->ResourceBarrier(1, &barrier);
  1071. copyCommandQueue->execute();
  1072. directCommandQueue->execute();
  1073. swapChain->Present(0, 0);
  1074. backBufferIndex = swapChain->GetCurrentBackBufferIndex();
  1075. }
  1076. Textur* DirectX12::createOrGetTextur(const char* name, Bild* b)
  1077. {
  1078. if (!device)
  1079. {
  1080. if (b) b->release();
  1081. return 0;
  1082. }
  1083. if (texturRegister->hatTextur(name))
  1084. {
  1085. Textur* ret = texturRegister->getTextur(name);
  1086. if (b) ret->setBildZ(b);
  1087. return ret;
  1088. }
  1089. Textur* ret = new DX12Textur(device, copyCommandQueue, directCommandQueue);
  1090. if (b) ret->setBildZ(b);
  1091. texturRegister->addTextur(dynamic_cast<Textur*>(ret->getThis()), name);
  1092. ret->updateTextur();
  1093. copyCommandQueue->execute();
  1094. directCommandQueue->execute();
  1095. return ret;
  1096. }
  1097. Bild* DirectX12::zUIRenderBild() const
  1098. {
  1099. return uiTextur ? uiTextur->zBild() : 0;
  1100. }
  1101. bool DirectX12::isAvailable()
  1102. {
  1103. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL("dxgi.dll", "dxgi.dll");
  1104. if (!dxgiDLL) return 0;
  1105. HINSTANCE d3d12DLL = getDLLRegister()->ladeDLL("d3d12.dll", "d3d12.dll");
  1106. if (!d3d12DLL)
  1107. {
  1108. getDLLRegister()->releaseDLL("dxgi.dll");
  1109. return 0;
  1110. }
  1111. CreateDXGIFactory2Function createFactory
  1112. = (CreateDXGIFactory2Function)GetProcAddress(
  1113. dxgiDLL, "CreateDXGIFactory2");
  1114. if (!createFactory)
  1115. {
  1116. getDLLRegister()->releaseDLL("dxgi.dll");
  1117. getDLLRegister()->releaseDLL("d3d12.dll");
  1118. return 0;
  1119. }
  1120. D3D12CreateDeviceFunction createDevice
  1121. = (D3D12CreateDeviceFunction)GetProcAddress(
  1122. d3d12DLL, "D3D12CreateDevice");
  1123. if (!createDevice)
  1124. {
  1125. getDLLRegister()->releaseDLL("dxgi.dll");
  1126. getDLLRegister()->releaseDLL("d3d12.dll");
  1127. return 0;
  1128. }
  1129. #ifdef _DEBUG
  1130. D3D12GetDebugInterfaceFunction getDebugInterface
  1131. = (D3D12GetDebugInterfaceFunction)GetProcAddress(
  1132. d3d12DLL, "D3D12GetDebugInterface");
  1133. ID3D12Debug* debug = 0;
  1134. getDebugInterface(__uuidof(ID3D12Debug), (void**)&debug);
  1135. debug->EnableDebugLayer();
  1136. #endif
  1137. IDXGIFactory4* factory;
  1138. UINT createFactoryFlags = 0;
  1139. #ifdef _DEBUG
  1140. createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  1141. #endif
  1142. HRESULT res = createFactory(
  1143. createFactoryFlags, __uuidof(IDXGIFactory4), (void**)&factory);
  1144. if (FAILED(res))
  1145. {
  1146. getDLLRegister()->releaseDLL("dxgi.dll");
  1147. getDLLRegister()->releaseDLL("d3d12.dll");
  1148. return 0;
  1149. }
  1150. int index = 0;
  1151. do
  1152. {
  1153. IDXGIAdapter1* current;
  1154. res = factory->EnumAdapters1(index++, &current);
  1155. if (res == S_OK)
  1156. {
  1157. DXGI_ADAPTER_DESC1 dxgiAdapterDesc1;
  1158. current->GetDesc1(&dxgiAdapterDesc1);
  1159. ID3D12Device5* device = 0;
  1160. if ((dxgiAdapterDesc1.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) == 0
  1161. && SUCCEEDED(createDevice(current,
  1162. D3D_FEATURE_LEVEL_12_1,
  1163. __uuidof(ID3D12Device5),
  1164. (void**)&device)))
  1165. {
  1166. device->Release();
  1167. current->Release();
  1168. factory->Release();
  1169. #ifdef _DEBUG
  1170. debug->Release();
  1171. #endif
  1172. getDLLRegister()->releaseDLL("dxgi.dll");
  1173. getDLLRegister()->releaseDLL("d3d12.dll");
  1174. return 1;
  1175. }
  1176. current->Release();
  1177. }
  1178. } while (res != DXGI_ERROR_NOT_FOUND);
  1179. factory->Release();
  1180. getDLLRegister()->releaseDLL("dxgi.dll");
  1181. getDLLRegister()->releaseDLL("d3d12.dll");
  1182. return 0;
  1183. }
  1184. DXBuffer* DirectX12::createIndexBuffer()
  1185. {
  1186. return new DX12IndexBuffer(
  1187. sizeof(int), device, copyCommandQueue, directCommandQueue);
  1188. }
  1189. DXBuffer* DirectX12::createVertexBuffer()
  1190. {
  1191. return new DX12VertexBuffer(
  1192. sizeof(Vertex3D), device, copyCommandQueue, directCommandQueue);
  1193. }