DX12CommandQueue.cpp 3.7 KB

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