DX12CommandQueue.cpp 3.8 KB

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