DX12GraphicsApi.cpp 44 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. };
  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. Model3DIterator* iterator = w->getIterator();
  830. while( iterator->hasNext() )
  831. {
  832. Model3D* obj = iterator->getNext();
  833. if( isInFrustrum( obj->getPos(), obj->getRadius() ) )
  834. renderObject( obj );
  835. }
  836. iterator->release();
  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. }