DX12Buffer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #pragma once
  2. #include "DX12Buffer.h"
  3. #include "d3dx12.h"
  4. #include "DX12CommandQueue.h"
  5. using namespace Framework;
  6. // Konstruktor
  7. // eSize: Die Länge eines Elementes in Bytes
  8. DX12Buffer::DX12Buffer(
  9. int eSize, ID3D12Device* device, ID3D12GraphicsCommandList* list, int flags)
  10. : DXBuffer(eSize),
  11. buffer(0),
  12. intermediate(0),
  13. device(device),
  14. list(list)
  15. {
  16. description = new D3D12_RESOURCE_DESC();
  17. ZeroMemory(description, sizeof(D3D12_RESOURCE_DESC));
  18. description->Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  19. description->Height = 1;
  20. description->DepthOrArraySize = 1;
  21. description->MipLevels = 1;
  22. description->Format = DXGI_FORMAT_UNKNOWN;
  23. description->SampleDesc.Count = 1;
  24. description->Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  25. description->Flags = (D3D12_RESOURCE_FLAGS)flags;
  26. }
  27. // Destruktor
  28. DX12Buffer::~DX12Buffer()
  29. {
  30. if (intermediate) intermediate->Release();
  31. if (buffer) buffer->Release();
  32. delete description;
  33. }
  34. // Kopiert die Daten in den Buffer, fals sie sich verändert haben
  35. void DX12Buffer::copieren(int byteCount)
  36. {
  37. if (!len) return;
  38. if (byteCount < 0) byteCount = len;
  39. if (description->Width < len)
  40. {
  41. if (intermediate) intermediate->Release();
  42. if (buffer) buffer->Release();
  43. buffer = 0;
  44. description->Width = len;
  45. }
  46. if (!buffer)
  47. {
  48. D3D12_HEAP_PROPERTIES hprop;
  49. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  50. hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  51. hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  52. hprop.CreationNodeMask = 1;
  53. hprop.VisibleNodeMask = 1;
  54. device->CreateCommittedResource(&hprop,
  55. D3D12_HEAP_FLAG_NONE,
  56. description,
  57. D3D12_RESOURCE_STATE_GENERIC_READ,
  58. 0,
  59. __uuidof(ID3D12Resource),
  60. (void**)&buffer);
  61. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  62. device->CreateCommittedResource(&hprop,
  63. D3D12_HEAP_FLAG_NONE,
  64. description,
  65. D3D12_RESOURCE_STATE_GENERIC_READ,
  66. 0,
  67. __uuidof(ID3D12Resource),
  68. (void**)&intermediate);
  69. if (data) changed = 1;
  70. }
  71. if (changed && data)
  72. {
  73. BYTE* pData;
  74. D3D12_RANGE r;
  75. r.Begin = 0;
  76. r.End = 0;
  77. buffer->Map(0, &r, (void**)&pData);
  78. memcpy(pData, data, byteCount);
  79. r.End = byteCount;
  80. buffer->Unmap(0, &r);
  81. // list->CopyBufferRegion( buffer, 0, intermediate, 0, len );
  82. changed = 0;
  83. }
  84. }
  85. // Gibt den Buffer zurück
  86. ID3D12Resource* DX12Buffer::zBuffer() const
  87. {
  88. return buffer;
  89. }
  90. DX12IndexBuffer::DX12IndexBuffer(int eSize,
  91. ID3D12Device* device,
  92. DX12CopyCommandQueue* copy,
  93. DX12DirectCommandQueue* direct)
  94. : DX12Buffer(
  95. eSize, device, copy->getCommandList(), D3D12_RESOURCE_FLAG_NONE),
  96. copy(copy),
  97. direct(direct)
  98. {
  99. ibs = 0;
  100. }
  101. DX12IndexBuffer::~DX12IndexBuffer() {}
  102. // Kopiert die Daten in den Buffer, fals sie sich verändert haben
  103. void DX12IndexBuffer::copieren(int byteCount)
  104. {
  105. /*if( ibs )
  106. {
  107. D3D12_RESOURCE_BARRIER barrier;
  108. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  109. barrier.Transition.pResource = buffer;
  110. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_INDEX_BUFFER;
  111. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  112. barrier.Transition.Subresource = 0;
  113. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  114. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  115. direct->execute();
  116. ibs = 0;
  117. }*/
  118. DX12Buffer::copieren(byteCount);
  119. // copy->execute();
  120. /*D3D12_RESOURCE_BARRIER barrier;
  121. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  122. barrier.Transition.pResource = buffer;
  123. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  124. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_INDEX_BUFFER;
  125. barrier.Transition.Subresource = 0;
  126. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  127. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  128. direct->execute();
  129. ibs = 1;*/
  130. }
  131. DX12VertexBuffer::DX12VertexBuffer(int eSize,
  132. ID3D12Device* device,
  133. DX12CopyCommandQueue* copy,
  134. DX12DirectCommandQueue* direct)
  135. : DX12Buffer(
  136. eSize, device, copy->getCommandList(), D3D12_RESOURCE_FLAG_NONE),
  137. copy(copy),
  138. direct(direct)
  139. {
  140. vbs = 0;
  141. }
  142. DX12VertexBuffer::~DX12VertexBuffer() {}
  143. // Kopiert die Daten in den Buffer, fals sie sich verändert haben
  144. void DX12VertexBuffer::copieren(int byteCount)
  145. {
  146. /*if( vbs )
  147. {
  148. D3D12_RESOURCE_BARRIER barrier;
  149. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  150. barrier.Transition.pResource = buffer;
  151. barrier.Transition.StateBefore =
  152. D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
  153. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  154. barrier.Transition.Subresource = 0;
  155. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  156. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  157. direct->execute();
  158. vbs = 0;
  159. }*/
  160. DX12Buffer::copieren(byteCount);
  161. // copy->execute();
  162. /*D3D12_RESOURCE_BARRIER barrier;
  163. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  164. barrier.Transition.pResource = buffer;
  165. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  166. barrier.Transition.StateAfter =
  167. D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
  168. barrier.Transition.Subresource = 0;
  169. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  170. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  171. direct->execute();
  172. vbs = 1;*/
  173. }