DX12Buffer.cpp 5.5 KB

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