DX12GraphicsApi.cpp 42 KB

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