DX12GraphicsApi.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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( *CreateDXGIFactory2Function )( UINT, REFIID, void ** );
  119. typedef HRESULT( *D3D12CreateDeviceFunction )( IDXGIAdapter *, D3D_FEATURE_LEVEL,
  120. REFIID, void ** );
  121. typedef HRESULT( *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. D3D12GetDebugInterfaceFunction getDebugInterface = (D3D12GetDebugInterfaceFunction)GetProcAddress( d3d12DLL, "D3D12GetDebugInterface" );
  159. getDebugInterface( __uuidof( ID3D12Debug ), (void **)& debug );
  160. #ifdef _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( 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->setSize( this->backBufferSize );
  647. texturModel->setTextur( uiTextur->getThis() );
  648. factory->Release();
  649. }
  650. void DirectX12::beginFrame( bool fill2D, bool fill3D, int fillColor )
  651. {
  652. D3D12_RESOURCE_BARRIER barrier;
  653. ZeroMemory( &barrier, sizeof( barrier ) );
  654. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  655. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  656. barrier.Transition.pResource = this->backBuffer[ backBufferIndex ];
  657. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
  658. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
  659. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  660. directCommandQueue->getCommandList()->ResourceBarrier( 1, &barrier );
  661. if( fill2D )
  662. uiTextur->zBild()->setFarbe( fillColor );
  663. if( fill3D )
  664. {
  665. float color[ 4 ];
  666. // Setup the color to clear the buffer.
  667. color[ 0 ] = ( ( fillColor >> 16 ) & 0xFF ) / 255.f; // R
  668. color[ 1 ] = ( ( fillColor >> 8 ) & 0xFF ) / 255.f; // G
  669. color[ 2 ] = ( fillColor & 0xFF ) / 255.f; // B
  670. color[ 3 ] = ( ( fillColor >> 24 ) & 0xFF ) / 255.f; // A
  671. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_RTV );
  672. D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtvHeap->GetCPUDescriptorHandleForHeapStart();
  673. rtv.ptr += rtvDescriptorSize * backBufferIndex;
  674. directCommandQueue->getCommandList()->OMSetRenderTargets( 1, &rtv, 0, 0 );
  675. directCommandQueue->getCommandList()->ClearRenderTargetView( rtv, color, 0, 0 );
  676. }
  677. int lc[] = { 0, 0 };
  678. pixelShader->füllConstBuffer( (char *)lc, 4, sizeof( int ) * 2 );
  679. }
  680. void DirectX12::renderObject( Model3D * zObj )
  681. {
  682. vertexBuffer->setData( (void *)texturModel->zVertexBuffer() );
  683. vertexBuffer->setLength( sizeof( Vertex3D ) * texturModel->getVertexAnzahl() );
  684. vertexBuffer->copieren();
  685. Mat4< float > trans = Mat4< float >::identity();
  686. int anz = zObj->errechneMatrizen( trans, matrixBuffer );
  687. if( vertexShader )
  688. vertexShader->füllConstBuffer( (char *)matrixBuffer, 1, sizeof( Mat4< float > ) * anz );
  689. float matirialBuffer[ 3 ]; // light factors (phong model)
  690. matirialBuffer[ 0 ] = zObj->getAmbientFactor();
  691. matirialBuffer[ 1 ] = zObj->getDiffusFactor();
  692. matirialBuffer[ 2 ] = zObj->getSpecularFactor();
  693. if( pixelShader )
  694. pixelShader->füllConstBuffer( (char *)matirialBuffer, 3, sizeof( float ) * 3 );
  695. unsigned int offset = 0;
  696. unsigned int es = (unsigned)vertexBuffer->getElementLength();
  697. Model3DTextur * zTextur = zObj->zTextur();
  698. int ind = 0;
  699. for( auto i = zObj->zModelData()->getPolygons(); i; i++ )
  700. {
  701. indexBuffer->setData( i->indexList );
  702. indexBuffer->setLength( sizeof( int ) * i->indexAnz );
  703. indexBuffer->copieren();
  704. Textur *t = zTextur->zPolygonTextur( ind );
  705. //if( t &&t->brauchtUpdate() )
  706. // t->updateTextur();
  707. DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
  708. if( indexBuffer->getElementLength() == 2 )
  709. f = DXGI_FORMAT_R16_UINT;
  710. if( indexBuffer->getElementLength() == 1 )
  711. f = DXGI_FORMAT_R8_UINT;
  712. indexBufferView->Format = f;
  713. if( t )
  714. {
  715. /*ID3D11ShaderResourceView *v[ 3 ];
  716. v[ 0 ] = *(DX11Textur *)t;
  717. v[ 1 ] = *diffuseLights;
  718. v[ 2 ] = *pointLights;
  719. d3d11Context->PSSetShaderResources( 0, 3, v );
  720. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );*/
  721. directCommandQueue->getCommandList()->SetPipelineState( pipeline );
  722. directCommandQueue->getCommandList()->SetGraphicsRootSignature( signature );
  723. directCommandQueue->getCommandList()->SetDescriptorHeaps( 1, &shaderBufferHeap );
  724. directCommandQueue->getCommandList()->SetGraphicsRootDescriptorTable( 0, shaderBufferHeap->GetGPUDescriptorHandleForHeapStart() );
  725. directCommandQueue->getCommandList()->RSSetViewports( 1, viewPort );
  726. directCommandQueue->getCommandList()->RSSetScissorRects( 1, allowedRenderArea );
  727. auto rtvDescriptorSize = device->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE_RTV );
  728. D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtvHeap->GetCPUDescriptorHandleForHeapStart();
  729. rtv.ptr += rtvDescriptorSize * backBufferIndex;
  730. directCommandQueue->getCommandList()->OMSetRenderTargets( 1, &rtv, 0, 0 );
  731. indexBufferView->SizeInBytes = indexBuffer->getElementAnzahl() * indexBuffer->getElementLength();
  732. indexBufferView->BufferLocation = indexBuffer->zBuffer()->GetGPUVirtualAddress();
  733. directCommandQueue->getCommandList()->IASetIndexBuffer( indexBufferView );
  734. vertexBufferView->SizeInBytes = vertexBuffer->getElementAnzahl() * vertexBuffer->getElementLength();
  735. vertexBufferView->BufferLocation = vertexBuffer->zBuffer()->GetGPUVirtualAddress();
  736. directCommandQueue->getCommandList()->IASetVertexBuffers( 0, 1, vertexBufferView );
  737. directCommandQueue->getCommandList()->IASetPrimitiveTopology( D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  738. directCommandQueue->getCommandList()->DrawIndexedInstanced( indexBuffer->getElementAnzahl(), 1, 0, 0, 0 );
  739. }
  740. else
  741. {
  742. /*d3d11Context->RSSetState( meshRS );
  743. ID3D11ShaderResourceView *v[ 3 ];
  744. v[ 0 ] = *(DX11Textur *)defaultTextur;
  745. v[ 1 ] = *diffuseLights;
  746. v[ 2 ] = *pointLights;
  747. d3d11Context->PSSetShaderResources( 0, 3, v );
  748. d3d11Context->DrawIndexed( indexBuffer->getElementAnzahl(), 0, 0 );
  749. d3d11Context->RSSetState( texturRS );*/
  750. //directCommandQueue->getCommandList()->DrawIndexedInstanced( indexBuffer->getElementAnzahl(), 1, 0, 0, 0 );
  751. }
  752. ind++;
  753. }
  754. }
  755. // Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet werden muss
  756. // pos: Der Mittelpunkt der Kugel
  757. // radius: Der Radius der Kugel
  758. // 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
  759. bool DirectX12::isInFrustrum( const Vec3< float > & pos, float radius, float *dist ) const
  760. {
  761. for( int i = 0; i < 6; i++ )
  762. {
  763. if( frustrum[ i ] * pos + radius < 0 )
  764. return 0;
  765. }
  766. if( dist )
  767. * dist = kamPos.abstand( pos );
  768. return 1;
  769. }
  770. void DirectX12::renderKamera( Kam3D * zKamera )
  771. {/*
  772. directCommandQueue->getCommandList()->RSSetViewports( 1, (D3D12_VIEWPORT *)zKamera->zViewPort() );
  773. Mat4< float > tmp = zKamera->getProjectionMatrix() * zKamera->getViewMatrix();
  774. frustrum[ 0 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 0 ][ 0 ];
  775. frustrum[ 0 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 0 ][ 1 ];
  776. frustrum[ 0 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 0 ][ 2 ];
  777. frustrum[ 0 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 0 ][ 3 ];
  778. frustrum[ 1 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 0 ][ 0 ];
  779. frustrum[ 1 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 0 ][ 1 ];
  780. frustrum[ 1 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 0 ][ 2 ];
  781. frustrum[ 1 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 0 ][ 3 ];
  782. frustrum[ 2 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 1 ][ 0 ];
  783. frustrum[ 2 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 1 ][ 1 ];
  784. frustrum[ 2 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 1 ][ 2 ];
  785. frustrum[ 2 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 1 ][ 3 ];
  786. frustrum[ 3 ].x = tmp.elements[ 3 ][ 0 ] + tmp.elements[ 1 ][ 0 ];
  787. frustrum[ 3 ].y = tmp.elements[ 3 ][ 1 ] + tmp.elements[ 1 ][ 1 ];
  788. frustrum[ 3 ].z = tmp.elements[ 3 ][ 2 ] + tmp.elements[ 1 ][ 2 ];
  789. frustrum[ 3 ].w = tmp.elements[ 3 ][ 3 ] + tmp.elements[ 1 ][ 3 ];
  790. frustrum[ 4 ].x = tmp.elements[ 2 ][ 0 ];
  791. frustrum[ 4 ].y = tmp.elements[ 2 ][ 1 ];
  792. frustrum[ 4 ].z = tmp.elements[ 2 ][ 2 ];
  793. frustrum[ 4 ].w = tmp.elements[ 2 ][ 3 ];
  794. frustrum[ 5 ].x = tmp.elements[ 3 ][ 0 ] - tmp.elements[ 2 ][ 0 ];
  795. frustrum[ 5 ].y = tmp.elements[ 3 ][ 1 ] - tmp.elements[ 2 ][ 1 ];
  796. frustrum[ 5 ].z = tmp.elements[ 3 ][ 2 ] - tmp.elements[ 2 ][ 2 ];
  797. frustrum[ 5 ].w = tmp.elements[ 3 ][ 3 ] - tmp.elements[ 2 ][ 3 ];
  798. for( int i = 0; i < 6; i++ )
  799. frustrum[ i ].normalize();
  800. viewAndProj[ 0 ] = zKamera->getViewMatrix();
  801. viewAndProj[ 1 ] = zKamera->getProjectionMatrix();
  802. kamPos = zKamera->getWorldPosition();
  803. if( vertexShader )
  804. vertexShader->füllConstBuffer( (char *)viewAndProj, 0, sizeof( Mat4< float > ) * 2 );
  805. if( pixelShader )
  806. pixelShader->füllConstBuffer( (char *)& kamPos, 2, sizeof( float ) * 3 );
  807. Welt3D * w = zKamera->zWelt();
  808. w->lock();
  809. for( auto obj = w->getMembers(); obj; obj++ )
  810. {
  811. float dist;
  812. if( isInFrustrum( obj->getPos(), obj->getRadius(), &dist ) )
  813. renderObject( obj._ );
  814. }
  815. w->unlock();*/
  816. }
  817. void DirectX12::presentFrame()
  818. {
  819. directCommandQueue->getCommandList()->RSSetViewports( 1, viewPort );
  820. viewAndProj[ 0 ] = Mat4<float>::identity();
  821. viewAndProj[ 1 ] = Mat4<float>::identity();
  822. if( vertexShader )
  823. vertexShader->füllConstBuffer( (char *)viewAndProj, 0, sizeof( Mat4< float > ) * 2 );
  824. uiTextur->updateTextur();
  825. if( fenster && !IsIconic( fenster->getFensterHandle() ) )
  826. renderObject( texturModel );
  827. D3D12_RESOURCE_BARRIER barrier;
  828. ZeroMemory( &barrier, sizeof( barrier ) );
  829. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  830. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  831. barrier.Transition.pResource = this->backBuffer[ backBufferIndex ];
  832. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
  833. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
  834. barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
  835. directCommandQueue->getCommandList()->ResourceBarrier( 1, &barrier );
  836. copyCommandQueue->execute();
  837. directCommandQueue->execute();
  838. swapChain->Present( 0, 0 );
  839. backBufferIndex = swapChain->GetCurrentBackBufferIndex();
  840. }
  841. Textur * DirectX12::createOrGetTextur( const char *name, Bild * b )
  842. {
  843. if( !device )
  844. {
  845. if( b )
  846. b->release();
  847. return 0;
  848. }
  849. if( texturRegister->hatTextur( name ) )
  850. {
  851. Textur *ret = texturRegister->getTextur( name );
  852. if( b )
  853. ret->setBildZ( b );
  854. return ret;
  855. }
  856. Textur *ret = new DX12Textur( device, copyCommandQueue, directCommandQueue );
  857. if( b )
  858. ret->setBildZ( b );
  859. texturRegister->addTextur( ret->getThis(), name );
  860. ret->updateTextur();
  861. copyCommandQueue->execute();
  862. directCommandQueue->execute();
  863. return ret;
  864. }
  865. Bild *DirectX12::zUIRenderBild() const
  866. {
  867. return uiTextur ? uiTextur->zBild() : 0;
  868. }
  869. bool DirectX12::isAvailable()
  870. {
  871. HINSTANCE dxgiDLL = getDLLRegister()->ladeDLL( "dxgi.dll", "dxgi.dll" );
  872. if( !dxgiDLL )
  873. return 0;
  874. HINSTANCE d3d12DLL = getDLLRegister()->ladeDLL( "d3d12.dll", "d3d12.dll" );
  875. if( !d3d12DLL )
  876. {
  877. getDLLRegister()->releaseDLL( "dxgi.dll" );
  878. return 0;
  879. }
  880. CreateDXGIFactory2Function createFactory = (CreateDXGIFactory2Function)GetProcAddress( dxgiDLL, "CreateDXGIFactory2" );
  881. if( !createFactory )
  882. {
  883. getDLLRegister()->releaseDLL( "dxgi.dll" );
  884. getDLLRegister()->releaseDLL( "d3d12.dll" );
  885. return 0;
  886. }
  887. D3D12CreateDeviceFunction createDevice = (D3D12CreateDeviceFunction)GetProcAddress( d3d12DLL, "D3D12CreateDevice" );
  888. if( !createDevice )
  889. {
  890. getDLLRegister()->releaseDLL( "dxgi.dll" );
  891. getDLLRegister()->releaseDLL( "d3d12.dll" );
  892. return 0;
  893. }
  894. D3D12GetDebugInterfaceFunction getDebugInterface = (D3D12GetDebugInterfaceFunction)GetProcAddress( d3d12DLL, "D3D12GetDebugInterface" );
  895. ID3D12Debug *debug = 0;
  896. getDebugInterface( __uuidof( ID3D12Debug ), (void **)&debug );
  897. #ifdef _DEBUG
  898. debug->EnableDebugLayer();
  899. #endif
  900. IDXGIFactory4 *factory;
  901. UINT createFactoryFlags = 0;
  902. #if defined(_DEBUG)
  903. createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
  904. #endif
  905. HRESULT res = createFactory( createFactoryFlags, __uuidof( IDXGIFactory4 ), (void **)& factory );
  906. if( FAILED( res ) )
  907. {
  908. getDLLRegister()->releaseDLL( "dxgi.dll" );
  909. getDLLRegister()->releaseDLL( "d3d12.dll" );
  910. return 0;
  911. }
  912. int index = 0;
  913. do
  914. {
  915. IDXGIAdapter1 *current;
  916. res = factory->EnumAdapters1( index++, &current );
  917. if( res == S_OK )
  918. {
  919. DXGI_ADAPTER_DESC1 dxgiAdapterDesc1;
  920. current->GetDesc1( &dxgiAdapterDesc1 );
  921. ID3D12Device2 *device = 0;
  922. if( ( dxgiAdapterDesc1.Flags &DXGI_ADAPTER_FLAG_SOFTWARE ) == 0 &&
  923. SUCCEEDED( createDevice( current, D3D_FEATURE_LEVEL_12_1, __uuidof( ID3D12Device2 ), (void**)&device ) ) )
  924. {
  925. device->Release();
  926. current->Release();
  927. factory->Release();
  928. debug->Release();
  929. getDLLRegister()->releaseDLL( "dxgi.dll" );
  930. getDLLRegister()->releaseDLL( "d3d12.dll" );
  931. return 1;
  932. }
  933. current->Release();
  934. }
  935. } while( res != DXGI_ERROR_NOT_FOUND );
  936. factory->Release();
  937. getDLLRegister()->releaseDLL( "dxgi.dll" );
  938. getDLLRegister()->releaseDLL( "d3d12.dll" );
  939. return 0;
  940. }