DX12GraphicsApi.cpp 39 KB

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