DX12GraphicsApi.cpp 42 KB

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