DXCommandQueue.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "DXCommandQueue.h"
  2. #include "Text.h"
  3. #include "Fenster.h"
  4. #include <iostream>
  5. #include <d3d12.h>
  6. using namespace Framework;
  7. DX12CommandQueue::DX12CommandQueue( int typ, ID3D12Device2 *device )
  8. : device( device ),
  9. event( CreateEvent( 0, 0, 0, 0 ) ),
  10. fenceValue( 0 ),
  11. ref( 1 )
  12. {
  13. D3D12_COMMAND_QUEUE_DESC desc = {};
  14. desc.Type = (D3D12_COMMAND_LIST_TYPE)typ;
  15. desc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
  16. desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  17. desc.NodeMask = 0;
  18. HRESULT res = device->CreateCommandQueue( &desc, __uuidof( ID3D12CommandQueue ), (void **)& queue );
  19. if( FAILED( res ) )
  20. {
  21. std::cout << "ERROR: CreateCommandQueue returned " << res << "\n";
  22. WMessageBox( 0, new Text( "Fehler" ), new Text( "CreateCommandQueue ist Fehlgeschlagen." ), MB_ICONERROR );
  23. }
  24. res = device->CreateCommandAllocator( (D3D12_COMMAND_LIST_TYPE)typ, __uuidof( ID3D12CommandAllocator ), (void **)& allocator );
  25. if( FAILED( res ) )
  26. {
  27. std::cout << "ERROR: CreateCommandAllocator returned " << res << "\n";
  28. WMessageBox( 0, new Text( "Fehler" ), new Text( "CreateCommandAllocator ist Fehlgeschlagen." ), MB_ICONERROR );
  29. }
  30. res = device->CreateCommandList( 0, (D3D12_COMMAND_LIST_TYPE)typ, allocator, nullptr, __uuidof( ID3D12GraphicsCommandList ), (void **)& commandList );
  31. if( FAILED( res ) )
  32. {
  33. std::cout << "ERROR: CreateCommandList returned " << res << "\n";
  34. WMessageBox( 0, new Text( "Fehler" ), new Text( "CreateCommandList ist Fehlgeschlagen." ), MB_ICONERROR );
  35. }
  36. res = device->CreateFence( 0, D3D12_FENCE_FLAG_NONE, __uuidof( ID3D12Fence ), (void **)& fence );
  37. if( FAILED( res ) )
  38. {
  39. std::cout << "ERROR: CreateFence returned " << res << "\n";
  40. WMessageBox( 0, new Text( "Fehler" ), new Text( "CreateFence ist Fehlgeschlagen." ), MB_ICONERROR );
  41. }
  42. }
  43. DX12CommandQueue::~DX12CommandQueue()
  44. {
  45. flush();
  46. if( commandList )
  47. commandList->Release();
  48. if( allocator )
  49. allocator->Release();
  50. if( queue )
  51. queue->Release();
  52. }
  53. unsigned __int64 DX12CommandQueue::addSignalFromGPU()
  54. {
  55. queue->Signal( fence, ++fenceValue );
  56. return fenceValue;
  57. }
  58. void DX12CommandQueue::whaitForGPUSignal( unsigned __int64 value )
  59. {
  60. if( fence->GetCompletedValue() < value )
  61. {
  62. fence->SetEventOnCompletion( value, event );
  63. WaitForSingleObject( event, INFINITE );
  64. }
  65. }
  66. void DX12CommandQueue::whaitForGPUSignal()
  67. {
  68. if( fence->GetCompletedValue() < fenceValue )
  69. {
  70. fence->SetEventOnCompletion( fenceValue, event );
  71. WaitForSingleObject( event, INFINITE );
  72. }
  73. }
  74. void DX12CommandQueue::flush()
  75. {
  76. whaitForGPUSignal( addSignalFromGPU() );
  77. }
  78. ID3D12CommandAllocator *DX12CommandQueue::getAllocator() const
  79. {
  80. return allocator;
  81. }
  82. ID3D12GraphicsCommandList2 *DX12CommandQueue::getCommandList() const
  83. {
  84. return commandList;
  85. }
  86. ID3D12CommandQueue *DX12CommandQueue::getQueue() const
  87. {
  88. return queue;
  89. }
  90. void DX12CommandQueue::execute()
  91. {
  92. commandList->Close();
  93. queue->ExecuteCommandLists( 1, (ID3D12CommandList * *)& commandList );
  94. flush();
  95. allocator->Reset();
  96. commandList->Reset( allocator, nullptr );
  97. }
  98. DX12CommandQueue *DX12CommandQueue::getThis()
  99. {
  100. ref++;
  101. return this;
  102. }
  103. DX12CommandQueue *DX12CommandQueue::release()
  104. {
  105. if( !--ref )
  106. delete this;
  107. return 0;
  108. }
  109. DX12DirectCommandQueue::DX12DirectCommandQueue( ID3D12Device2 *device )
  110. : DX12CommandQueue( D3D12_COMMAND_LIST_TYPE_DIRECT, device )
  111. {}
  112. DX12CopyCommandQueue::DX12CopyCommandQueue( ID3D12Device2 * device )
  113. : DX12CommandQueue( D3D12_COMMAND_LIST_TYPE_COPY, device )
  114. {}
  115. DX12ComputeCommandQueue::DX12ComputeCommandQueue( ID3D12Device2 * device )
  116. : DX12CommandQueue( D3D12_COMMAND_LIST_TYPE_COMPUTE, device )
  117. {}