DX12GraphicsApi.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. };
  421. vertexShader->erstelleInputLayout( inputLayout, 4 );
  422. D3D12_FEATURE_DATA_ROOT_SIGNATURE featureData = {};
  423. featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_1;
  424. if( FAILED( device->CheckFeatureSupport( D3D12_FEATURE_ROOT_SIGNATURE, &featureData, sizeof( featureData ) ) ) )
  425. featureData.HighestVersion = D3D_ROOT_SIGNATURE_VERSION_1_0;
  426. D3D12_ROOT_SIGNATURE_FLAGS rootSignatureFlags =
  427. D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT |
  428. D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |
  429. D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
  430. D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS;
  431. D3D12_DESCRIPTOR_RANGE1 range[ 2 ];
  432. range[ 0 ].NumDescriptors = 5;
  433. range[ 0 ].BaseShaderRegister = 0;
  434. range[ 0 ].RegisterSpace = 0;
  435. range[ 0 ].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
  436. range[ 0 ].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  437. range[ 0 ].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
  438. range[ 1 ].NumDescriptors = 1;
  439. range[ 1 ].BaseShaderRegister = 0;
  440. range[ 1 ].RegisterSpace = 0;
  441. range[ 1 ].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
  442. range[ 1 ].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
  443. range[ 1 ].Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
  444. D3D12_ROOT_PARAMETER1 rootParameters[ 1 ];
  445. rootParameters[ 0 ].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  446. rootParameters[ 0 ].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
  447. rootParameters[ 0 ].DescriptorTable.NumDescriptorRanges = 2;
  448. rootParameters[ 0 ].DescriptorTable.pDescriptorRanges = range;
  449. D3D12_STATIC_SAMPLER_DESC sampler = {};
  450. sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
  451. sampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  452. sampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  453. sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
  454. sampler.MipLODBias = 0;
  455. sampler.MaxAnisotropy = 0;
  456. sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
  457. sampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
  458. sampler.MinLOD = 0.0f;
  459. sampler.MaxLOD = D3D12_FLOAT32_MAX;
  460. sampler.ShaderRegister = 0;
  461. sampler.RegisterSpace = 0;
  462. sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
  463. D3D12_VERSIONED_ROOT_SIGNATURE_DESC rootSignatureDescription;
  464. rootSignatureDescription.Version = D3D_ROOT_SIGNATURE_VERSION_1_1;
  465. rootSignatureDescription.Desc_1_1.NumParameters = 1;
  466. rootSignatureDescription.Desc_1_1.pParameters = rootParameters;
  467. rootSignatureDescription.Desc_1_1.NumStaticSamplers = 1;
  468. rootSignatureDescription.Desc_1_1.pStaticSamplers = &sampler;
  469. rootSignatureDescription.Desc_1_1.Flags = rootSignatureFlags;
  470. ID3DBlob* rootSignature;
  471. ID3DBlob* error;
  472. res = D3DX12SerializeVersionedRootSignature( &rootSignatureDescription, featureData.HighestVersion, &rootSignature, &error, d3d12svrsf, d3d12srsf );
  473. if( FAILED( res ) )
  474. {
  475. factory->Release();
  476. std::cout << "ERROR: D3DX12SerializeVersionedRootSignature returned " << res << "\n";
  477. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "D3DX12SerializeVersionedRootSignature ist Fehlgeschlagen." ), MB_ICONERROR );
  478. if( error )
  479. error->Release();
  480. return;
  481. }
  482. res = device->CreateRootSignature( 0, rootSignature->GetBufferPointer(), rootSignature->GetBufferSize(), __uuidof(ID3D12RootSignature), (void**)&signature );
  483. if( FAILED( res ) )
  484. {
  485. factory->Release();
  486. std::cout << "ERROR: CreateRootSignature returned " << res << "\n";
  487. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "CreateRootSignature ist Fehlgeschlagen." ), MB_ICONERROR );
  488. return;
  489. }
  490. rootSignature->Release();
  491. D3D12_RASTERIZER_DESC rdesc;
  492. rdesc.FillMode = D3D12_FILL_MODE_SOLID;
  493. rdesc.CullMode = D3D12_CULL_MODE_BACK;
  494. rdesc.FrontCounterClockwise = 0;
  495. rdesc.DepthBias = 0;
  496. rdesc.DepthBiasClamp = 0.f;
  497. rdesc.SlopeScaledDepthBias = 0.f;
  498. rdesc.DepthClipEnable = 1;
  499. rdesc.MultisampleEnable = 0;
  500. rdesc.AntialiasedLineEnable = 0;
  501. rdesc.ForcedSampleCount = 0;
  502. rdesc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
  503. D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
  504. psoDesc.InputLayout = { vertexShader->zInputLayout(), (unsigned)vertexShader->getInputLayoutSize() };
  505. psoDesc.pRootSignature = signature;
  506. psoDesc.VS = { vertexShader->getCompiledShader(), (unsigned __int64)vertexShader->getCompiledLength() };
  507. psoDesc.PS = { pixelShader->getCompiledShader(), (unsigned __int64)pixelShader->getCompiledLength() };
  508. psoDesc.RasterizerState = rdesc;
  509. psoDesc.BlendState = CD3DX12_BLEND_DESC( D3D12_DEFAULT );
  510. psoDesc.DepthStencilState.DepthEnable = FALSE;
  511. psoDesc.DepthStencilState.StencilEnable = FALSE;
  512. psoDesc.SampleMask = UINT_MAX;
  513. psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
  514. psoDesc.NumRenderTargets = 1;
  515. psoDesc.RTVFormats[ 0 ] = DXGI_FORMAT_R8G8B8A8_UNORM;
  516. psoDesc.SampleDesc.Count = 1;
  517. D3D12_CPU_DESCRIPTOR_HANDLE sbHeapHandle = shaderBufferHeap->GetCPUDescriptorHandleForHeapStart();
  518. unsigned int incr = device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV );
  519. D3D12_CONSTANT_BUFFER_VIEW_DESC desc;
  520. vertexShader->getViewDesc( 0, desc );
  521. device->CreateConstantBufferView( &desc, sbHeapHandle );
  522. res = device->GetDeviceRemovedReason();
  523. sbHeapHandle.ptr += incr;
  524. vertexShader->getViewDesc( 1, desc );
  525. device->CreateConstantBufferView( &desc, sbHeapHandle );
  526. sbHeapHandle.ptr += incr;
  527. pixelShader->getViewDesc( 2, desc );
  528. device->CreateConstantBufferView( &desc, sbHeapHandle );
  529. sbHeapHandle.ptr += incr;
  530. pixelShader->getViewDesc( 3, desc );
  531. device->CreateConstantBufferView( &desc, sbHeapHandle );
  532. sbHeapHandle.ptr += incr;
  533. pixelShader->getViewDesc( 4, desc );
  534. device->CreateConstantBufferView( &desc, sbHeapHandle );
  535. sbHeapHandle.ptr += incr;
  536. uiTextur->updateTextur();
  537. D3D12_SHADER_RESOURCE_VIEW_DESC vd;
  538. vd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  539. vd.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
  540. vd.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
  541. vd.Texture2D.MipLevels = 1;
  542. vd.Texture2D.MostDetailedMip = 0;
  543. vd.Texture2D.PlaneSlice = 0;
  544. vd.Texture2D.ResourceMinLODClamp = 0;
  545. device->CreateShaderResourceView( ((DX12Textur*)uiTextur)->getResource(), &vd, sbHeapHandle );
  546. directCommandQueue->execute();
  547. res = device->CreateGraphicsPipelineState( &psoDesc, __uuidof(ID3D12PipelineState), (void**)&pipeline );
  548. if( FAILED( res ) )
  549. {
  550. factory->Release();
  551. std::cout << "ERROR: CreatePipelineState returned " << res << "\n";
  552. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "CreatePipelineState ist Fehlgeschlagen." ), MB_ICONERROR );
  553. return;
  554. }
  555. factory->Release();
  556. }
  557. void DirectX12::update()
  558. {
  559. if( !device || !swapChain || !directCommandQueue )
  560. return;
  561. directCommandQueue->flush();
  562. copyCommandQueue->flush();
  563. computeCommandQueue->flush();
  564. modelList->removeAll();
  565. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL( "dxgi.dll", "dxgi.dll" );
  566. if( !dxgiDLL )
  567. {
  568. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "dxgi.dll konnte nicht gefunden werden." ), MB_ICONERROR );
  569. return;
  570. }
  571. CreateDXGIFactory2Function createFactory = (CreateDXGIFactory2Function)GetProcAddress( dxgiDLL, "CreateDXGIFactory2" );
  572. if( !createFactory )
  573. {
  574. getDLLRegister()->releaseDLL( "dxgi.dll" );
  575. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "Der Einstiegspunkt CreateDXGIFactory2 fon DXGI konnte nicht gefunden werden." ), MB_ICONERROR );
  576. return;
  577. }
  578. DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
  579. HRESULT res = swapChain->GetDesc( &swapChainDesc );
  580. if( FAILED( res ) )
  581. {
  582. getDLLRegister()->releaseDLL( "dxgi.dll" );
  583. std::cout << "ERROR: GetDesc returned " << res << "\n";
  584. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "GetDesc ist Fehlgeschlagen." ), MB_ICONERROR );
  585. return;
  586. }
  587. IDXGIFactory4* factory;
  588. UINT createFactoryFlags = 0;
  589. #if defined(_DEBUG)
  590. createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  591. #endif
  592. res = createFactory( createFactoryFlags, __uuidof(IDXGIFactory4), (void**)&factory );
  593. if( FAILED( res ) )
  594. {
  595. getDLLRegister()->releaseDLL( "dxgi.dll" );
  596. std::cout << "ERROR: createFactory returned " << res << "\n";
  597. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "createFactory ist Fehlgeschlagen." ), MB_ICONERROR );
  598. return;
  599. }
  600. for( int i = 0; i < 2; ++i )
  601. {
  602. backBuffer[ i ]->Release();
  603. backBuffer[ i ] = 0;
  604. }
  605. res = swapChain->ResizeBuffers( 2, backBufferSize.x, backBufferSize.y,
  606. swapChainDesc.BufferDesc.Format, swapChainDesc.Flags );
  607. backBufferIndex = swapChain->GetCurrentBackBufferIndex();
  608. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_RTV );
  609. D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle( rtvHeap->GetCPUDescriptorHandleForHeapStart() );
  610. for( int i = 0; i < 2; i++ )
  611. {
  612. ID3D12Resource* backBuffer;
  613. res = swapChain->GetBuffer( i, __uuidof(ID3D12Resource), (void**)&backBuffer );
  614. if( FAILED( res ) )
  615. {
  616. getDLLRegister()->releaseDLL( "dxgi.dll" );
  617. factory->Release();
  618. std::cout << "ERROR: GetBuffer returned " << res << "\n";
  619. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "GetBuffer ist Fehlgeschlagen." ), MB_ICONERROR );
  620. return;
  621. }
  622. device->CreateRenderTargetView( backBuffer, nullptr, rtvHandle );
  623. this->backBuffer[ i ] = backBuffer;
  624. rtvHandle.ptr += rtvDescriptorSize;
  625. }
  626. getDLLRegister()->releaseDLL( "dxgi.dll" );
  627. delete viewPort;
  628. viewPort = new D3D12_VIEWPORT();
  629. viewPort->Width = (float)this->backBufferSize.x;
  630. viewPort->Height = (float)this->backBufferSize.y;
  631. viewPort->MinDepth = 0.0f;
  632. viewPort->MaxDepth = 1.0f;
  633. viewPort->TopLeftX = 0.0f;
  634. viewPort->TopLeftY = 0.0f;
  635. depthBuffer->Release();
  636. D3D12_CLEAR_VALUE optimizedClearValue = {};
  637. optimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;
  638. optimizedClearValue.DepthStencil = { 1.0f, 0 };
  639. res = device->CreateCommittedResource(
  640. &CD3DX12_HEAP_PROPERTIES( D3D12_HEAP_TYPE_DEFAULT ),
  641. D3D12_HEAP_FLAG_NONE,
  642. &CD3DX12_RESOURCE_DESC::Tex2D( DXGI_FORMAT_D32_FLOAT, this->backBufferSize.x, this->backBufferSize.y,
  643. 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL ),
  644. D3D12_RESOURCE_STATE_DEPTH_WRITE,
  645. 0,
  646. __uuidof(ID3D12Resource),
  647. (void**)&depthBuffer
  648. );
  649. if( FAILED( res ) )
  650. {
  651. factory->Release();
  652. std::cout << "ERROR: CreateCommittedResource returned " << res << "\n";
  653. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "CreateCommittedResource ist Fehlgeschlagen." ), MB_ICONERROR );
  654. return;
  655. }
  656. D3D12_DEPTH_STENCIL_VIEW_DESC dsv = {};
  657. dsv.Format = DXGI_FORMAT_D32_FLOAT;
  658. dsv.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
  659. dsv.Texture2D.MipSlice = 0;
  660. dsv.Flags = D3D12_DSV_FLAG_NONE;
  661. device->CreateDepthStencilView( depthBuffer, &dsv,
  662. dsvHeap->GetCPUDescriptorHandleForHeapStart() );
  663. if( uiTextur )
  664. uiTextur->release();
  665. Bild* renderB = new Bild( 1 );
  666. renderB->setAlpha3D( 1 );
  667. renderB->neuBild( this->backBufferSize.x, this->backBufferSize.y, 0 );
  668. uiTextur = createOrGetTextur( "_f_Render_Bild", renderB );
  669. texturModel->setTextur( dynamic_cast<Textur*>(uiTextur->getThis()) );
  670. factory->Release();
  671. }
  672. void DirectX12::beginFrame( bool fill2D, bool fill3D, int fillColor )
  673. {
  674. D3D12_RESOURCE_BARRIER barrier;
  675. ZeroMemory( &barrier, sizeof( barrier ) );
  676. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  677. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  678. barrier.Transition.pResource = this->backBuffer[ backBufferIndex ];
  679. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  680. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  681. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  682. directCommandQueue->getCommandList()->ResourceBarrier( 1, &barrier );
  683. if( fill2D )
  684. uiTextur->zBild()->setFarbe( fillColor );
  685. if( fill3D )
  686. {
  687. float color[ 4 ];
  688. // Setup the color to clear the buffer.
  689. color[ 0 ] = ((fillColor >> 16) & 0xFF) / 255.f; // R
  690. color[ 1 ] = ((fillColor >> 8) & 0xFF) / 255.f; // G
  691. color[ 2 ] = (fillColor & 0xFF) / 255.f; // B
  692. color[ 3 ] = ((fillColor >> 24) & 0xFF) / 255.f; // A
  693. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_RTV );
  694. D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtvHeap->GetCPUDescriptorHandleForHeapStart();
  695. rtv.ptr += rtvDescriptorSize * backBufferIndex;
  696. directCommandQueue->getCommandList()->OMSetRenderTargets( 1, &rtv, 0, 0 );
  697. directCommandQueue->getCommandList()->ClearRenderTargetView( rtv, color, 0, 0 );
  698. }
  699. int lc[] = { 0, 0 };
  700. pixelShader->füllConstBuffer( (char*)lc, 4, sizeof( int ) * 2 );
  701. }
  702. void DirectX12::renderObject( Model3D* zObj )
  703. {
  704. Mat4< float > trans = Mat4< float >::identity();
  705. zObj->zModelData()->zDXVertexBuffer()->copieren();
  706. zObj->zModelData()->zDXIndexBuffer()->copieren();
  707. int anz = zObj->errechneMatrizen( trans, matrixBuffer );
  708. if( vertexShader )
  709. vertexShader->füllConstBuffer( (char*)matrixBuffer, 1, sizeof( Mat4< float > ) * anz );
  710. float matirialBuffer[ 3 ]; // light factors (phong model)
  711. matirialBuffer[ 0 ] = zObj->getAmbientFactor();
  712. matirialBuffer[ 1 ] = zObj->getDiffusFactor();
  713. matirialBuffer[ 2 ] = zObj->getSpecularFactor();
  714. if( pixelShader )
  715. pixelShader->füllConstBuffer( (char*)matirialBuffer, 3, sizeof( float ) * 3 );
  716. unsigned int offset = 0;
  717. unsigned int es = (unsigned)zObj->zModelData()->zDXVertexBuffer()->getElementLength();
  718. Model3DTextur* zTextur = zObj->zTextur();
  719. int ind = 0;
  720. for( auto i = zObj->zModelData()->getPolygons(); i; i++ )
  721. {
  722. if( zObj->needRenderPolygon( ind ) )
  723. {
  724. Textur* t = zTextur->zPolygonTextur( ind );
  725. //if( t &&t->brauchtUpdate() )
  726. // t->updateTextur();
  727. DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
  728. if( zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 2 )
  729. f = DXGI_FORMAT_R16_UINT;
  730. if( zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 1 )
  731. f = DXGI_FORMAT_R8_UINT;
  732. indexBufferView->Format = f;
  733. if( t )
  734. {
  735. /*ID3D11ShaderResourceView *v[ 3 ];
  736. v[ 0 ] = *(DX11Textur *)t;
  737. v[ 1 ] = *diffuseLights;
  738. v[ 2 ] = *pointLights;
  739. d3d11Context->PSSetShaderResources( 0, 3, v );
  740. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );*/
  741. directCommandQueue->getCommandList()->SetPipelineState( pipeline );
  742. directCommandQueue->getCommandList()->SetGraphicsRootSignature( signature );
  743. directCommandQueue->getCommandList()->SetDescriptorHeaps( 1, &shaderBufferHeap );
  744. directCommandQueue->getCommandList()->SetGraphicsRootDescriptorTable( 0, shaderBufferHeap->GetGPUDescriptorHandleForHeapStart() );
  745. directCommandQueue->getCommandList()->RSSetViewports( 1, viewPort );
  746. directCommandQueue->getCommandList()->RSSetScissorRects( 1, allowedRenderArea );
  747. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_RTV );
  748. D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtvHeap->GetCPUDescriptorHandleForHeapStart();
  749. rtv.ptr += rtvDescriptorSize * backBufferIndex;
  750. directCommandQueue->getCommandList()->OMSetRenderTargets( 1, &rtv, 0, 0 );
  751. indexBufferView->SizeInBytes = zObj->zModelData()->zDXIndexBuffer()->getElementAnzahl() * zObj->zModelData()->zDXIndexBuffer()->getElementLength();
  752. indexBufferView->BufferLocation = ((DX12Buffer*)zObj->zModelData()->zDXIndexBuffer())->zBuffer()->GetGPUVirtualAddress();
  753. directCommandQueue->getCommandList()->IASetIndexBuffer( indexBufferView );
  754. vertexBufferView->SizeInBytes = zObj->zModelData()->zDXVertexBuffer()->getElementAnzahl() * zObj->zModelData()->zDXVertexBuffer()->getElementLength();
  755. vertexBufferView->BufferLocation = ((DX12Buffer*)zObj->zModelData()->zDXVertexBuffer())->zBuffer()->GetGPUVirtualAddress();
  756. directCommandQueue->getCommandList()->IASetVertexBuffers( 0, 1, vertexBufferView );
  757. directCommandQueue->getCommandList()->IASetPrimitiveTopology( D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  758. directCommandQueue->getCommandList()->DrawIndexedInstanced( zObj->zModelData()->zDXIndexBuffer()->getElementAnzahl(), 1, 0, 0, 0 );
  759. }
  760. else
  761. {
  762. /*d3d11Context->RSSetState( meshRS );
  763. ID3D11ShaderResourceView *v[ 3 ];
  764. v[ 0 ] = *(DX11Textur *)defaultTextur;
  765. v[ 1 ] = *diffuseLights;
  766. v[ 2 ] = *pointLights;
  767. d3d11Context->PSSetShaderResources( 0, 3, v );
  768. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );
  769. d3d11Context->RSSetState( texturRS );*/
  770. //directCommandQueue->getCommandList()->DrawIndexedInstanced( indexBuffer->getElementAnzahl(), 1, 0, 0, 0 );
  771. }
  772. }
  773. ind++;
  774. }
  775. }
  776. // Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet werden muss
  777. // pos: Der Mittelpunkt der Kugel
  778. // radius: Der Radius der Kugel
  779. // 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
  780. bool DirectX12::isInFrustrum( const Vec3< float >& pos, float radius, float* dist ) const
  781. {
  782. for( int i = 0; i < 6; i++ )
  783. {
  784. if( frustrum[ i ] * pos + radius < 0 )
  785. return 0;
  786. }
  787. if( dist )
  788. *dist = kamPos.abstand( pos );
  789. return 1;
  790. }
  791. void DirectX12::renderKamera( Kam3D* zKamera )
  792. {
  793. directCommandQueue->getCommandList()->RSSetViewports( 1, (D3D12_VIEWPORT*)zKamera->zViewPort() );
  794. Mat4< float > tmp = zKamera->getProjectionMatrix() * zKamera->getViewMatrix();
  795. frustrum[ 0 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 0 ][ 0 ];
  796. frustrum[ 0 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 0 ][ 1 ];
  797. frustrum[ 0 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 0 ][ 2 ];
  798. frustrum[ 0 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 0 ][ 3 ];
  799. frustrum[ 1 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 0 ][ 0 ];
  800. frustrum[ 1 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 0 ][ 1 ];
  801. frustrum[ 1 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 0 ][ 2 ];
  802. frustrum[ 1 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 0 ][ 3 ];
  803. frustrum[ 2 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 1 ][ 0 ];
  804. frustrum[ 2 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 1 ][ 1 ];
  805. frustrum[ 2 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 1 ][ 2 ];
  806. frustrum[ 2 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 1 ][ 3 ];
  807. frustrum[ 3 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 1 ][ 0 ];
  808. frustrum[ 3 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 1 ][ 1 ];
  809. frustrum[ 3 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 1 ][ 2 ];
  810. frustrum[ 3 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 1 ][ 3 ];
  811. frustrum[ 4 ].x = tmp.elements[ 2 ][ 0 ];
  812. frustrum[ 4 ].y = tmp.elements[ 2 ][ 1 ];
  813. frustrum[ 4 ].z = tmp.elements[ 2 ][ 2 ];
  814. frustrum[ 4 ].w = tmp.elements[ 2 ][ 3 ];
  815. frustrum[ 5 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 2 ][ 0 ];
  816. frustrum[ 5 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 2 ][ 1 ];
  817. frustrum[ 5 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 2 ][ 2 ];
  818. frustrum[ 5 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 2 ][ 3 ];
  819. for( int i = 0; i < 6; i++ )
  820. frustrum[ i ].normalize();
  821. viewAndProj[ 0 ] = zKamera->getViewMatrix();
  822. viewAndProj[ 1 ] = zKamera->getProjectionMatrix();
  823. kamPos = zKamera->getWorldPosition();
  824. if( vertexShader )
  825. vertexShader->füllConstBuffer( (char*)viewAndProj, 0, sizeof( Mat4< float > ) * 2 );
  826. if( pixelShader )
  827. pixelShader->füllConstBuffer( (char*)&kamPos, 2, sizeof( float ) * 3 );
  828. Welt3D* w = zKamera->zWelt();
  829. w->forAll( [this]( Model3D* obj ) {
  830. if( isInFrustrum( obj->getPos(), obj->getRadius() ) )
  831. renderObject( obj );
  832. } );
  833. }
  834. void DirectX12::presentFrame()
  835. {
  836. directCommandQueue->getCommandList()->RSSetViewports( 1, viewPort );
  837. viewAndProj[ 0 ] = Mat4<float>::identity();
  838. viewAndProj[ 1 ] = Mat4<float>::identity();
  839. if( vertexShader )
  840. vertexShader->füllConstBuffer( (char*)viewAndProj, 0, sizeof( Mat4< float > ) * 2 );
  841. uiTextur->updateTextur();
  842. if( fenster && !IsIconic( fenster->getFensterHandle() ) )
  843. renderObject( texturModel );
  844. D3D12_RESOURCE_BARRIER barrier;
  845. ZeroMemory( &barrier, sizeof( barrier ) );
  846. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  847. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  848. barrier.Transition.pResource = this->backBuffer[ backBufferIndex ];
  849. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  850. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  851. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  852. directCommandQueue->getCommandList()->ResourceBarrier( 1, &barrier );
  853. copyCommandQueue->execute();
  854. directCommandQueue->execute();
  855. swapChain->Present( 0, 0 );
  856. backBufferIndex = swapChain->GetCurrentBackBufferIndex();
  857. }
  858. Textur* DirectX12::createOrGetTextur( const char* name, Bild* b )
  859. {
  860. if( !device )
  861. {
  862. if( b )
  863. b->release();
  864. return 0;
  865. }
  866. if( texturRegister->hatTextur( name ) )
  867. {
  868. Textur* ret = texturRegister->getTextur( name );
  869. if( b )
  870. ret->setBildZ( b );
  871. return ret;
  872. }
  873. Textur* ret = new DX12Textur( device, copyCommandQueue, directCommandQueue );
  874. if( b )
  875. ret->setBildZ( b );
  876. texturRegister->addTextur( dynamic_cast<Textur*>(ret->getThis()), name );
  877. ret->updateTextur();
  878. copyCommandQueue->execute();
  879. directCommandQueue->execute();
  880. return ret;
  881. }
  882. Bild* DirectX12::zUIRenderBild() const
  883. {
  884. return uiTextur ? uiTextur->zBild() : 0;
  885. }
  886. bool DirectX12::isAvailable()
  887. {
  888. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL( "dxgi.dll", "dxgi.dll" );
  889. if( !dxgiDLL )
  890. return 0;
  891. HINSTANCE d3d12DLL = getDLLRegister()->ladeDLL( "d3d12.dll", "d3d12.dll" );
  892. if( !d3d12DLL )
  893. {
  894. getDLLRegister()->releaseDLL( "dxgi.dll" );
  895. return 0;
  896. }
  897. CreateDXGIFactory2Function createFactory = (CreateDXGIFactory2Function)GetProcAddress( dxgiDLL, "CreateDXGIFactory2" );
  898. if( !createFactory )
  899. {
  900. getDLLRegister()->releaseDLL( "dxgi.dll" );
  901. getDLLRegister()->releaseDLL( "d3d12.dll" );
  902. return 0;
  903. }
  904. D3D12CreateDeviceFunction createDevice = (D3D12CreateDeviceFunction)GetProcAddress( d3d12DLL, "D3D12CreateDevice" );
  905. if( !createDevice )
  906. {
  907. getDLLRegister()->releaseDLL( "dxgi.dll" );
  908. getDLLRegister()->releaseDLL( "d3d12.dll" );
  909. return 0;
  910. }
  911. #ifdef _DEBUG
  912. D3D12GetDebugInterfaceFunction getDebugInterface = (D3D12GetDebugInterfaceFunction)GetProcAddress( d3d12DLL, "D3D12GetDebugInterface" );
  913. ID3D12Debug* debug = 0;
  914. getDebugInterface( __uuidof(ID3D12Debug), (void**)&debug );
  915. debug->EnableDebugLayer();
  916. #endif
  917. IDXGIFactory4* factory;
  918. UINT createFactoryFlags = 0;
  919. #ifdef _DEBUG
  920. createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  921. #endif
  922. HRESULT res = createFactory( createFactoryFlags, __uuidof(IDXGIFactory4), (void**)&factory );
  923. if( FAILED( res ) )
  924. {
  925. getDLLRegister()->releaseDLL( "dxgi.dll" );
  926. getDLLRegister()->releaseDLL( "d3d12.dll" );
  927. return 0;
  928. }
  929. int index = 0;
  930. do
  931. {
  932. IDXGIAdapter1* current;
  933. res = factory->EnumAdapters1( index++, &current );
  934. if( res == S_OK )
  935. {
  936. DXGI_ADAPTER_DESC1 dxgiAdapterDesc1;
  937. current->GetDesc1( &dxgiAdapterDesc1 );
  938. ID3D12Device5* device = 0;
  939. if( (dxgiAdapterDesc1.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) == 0 &&
  940. SUCCEEDED( createDevice( current, D3D_FEATURE_LEVEL_12_1, __uuidof(ID3D12Device5), (void**)&device ) ) )
  941. {
  942. device->Release();
  943. current->Release();
  944. factory->Release();
  945. #ifdef _DEBUG
  946. debug->Release();
  947. #endif
  948. getDLLRegister()->releaseDLL( "dxgi.dll" );
  949. getDLLRegister()->releaseDLL( "d3d12.dll" );
  950. return 1;
  951. }
  952. current->Release();
  953. }
  954. } while( res != DXGI_ERROR_NOT_FOUND );
  955. factory->Release();
  956. getDLLRegister()->releaseDLL( "dxgi.dll" );
  957. getDLLRegister()->releaseDLL( "d3d12.dll" );
  958. return 0;
  959. }
  960. DXBuffer* DirectX12::createIndexBuffer()
  961. {
  962. return new DX12IndexBuffer( sizeof( int ), device, copyCommandQueue, directCommandQueue );
  963. }
  964. DXBuffer* DirectX12::createVertexBuffer()
  965. {
  966. return new DX12VertexBuffer( sizeof( Vertex3D ), device, copyCommandQueue, directCommandQueue );
  967. }