DXBuffer.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "DXBuffer.h"
  2. #include <iostream>
  3. #ifdef WIN32
  4. #include "DXCommandQueue.h"
  5. #include <d3d11.h>
  6. #include <d3d12.h>
  7. #include <d3dx12.h>
  8. #endif
  9. using namespace Framework;
  10. // Inhalt der DXBuffer Klasse
  11. // Konstruktor
  12. // bind: Der verwendungszweck des Buffers. Beispiel: D3D11_BIND_INDEX_BUFFER, D3D11_BIND_VERTEX_BUFFER.
  13. // eLän: Länge eines einzelnen Elements in Bytes
  14. DXBuffer::DXBuffer( int eLen )
  15. : ReferenceCounter()
  16. {
  17. data = 0;
  18. changed = 0;
  19. len = 0;
  20. elLen = eLen;
  21. }
  22. // Destruktor
  23. DXBuffer::~DXBuffer()
  24. {}
  25. // Setzt den geändert fläg, so das beim nächsten auruf von 'kopieren' die daten neu kopiert werden
  26. void DXBuffer::setChanged()
  27. {
  28. changed = 1;
  29. }
  30. // Ändert die länge des Buffers beim nächsten aufruf von 'kopieren'
  31. // län: Die Länge in Bytes
  32. void DXBuffer::setLength( int len )
  33. {
  34. this->len = len;
  35. }
  36. // Legt fest, was beim nächsten aufruf von 'kopieren' kopiert wird
  37. // data: Ein zeiger auf die Daten
  38. void DXBuffer::setData( void *data )
  39. {
  40. this->data = data;
  41. changed = 1;
  42. }
  43. // Gibt die Länge eines Elementes in bytes zurück
  44. int DXBuffer::getElementLength() const
  45. {
  46. return elLen;
  47. }
  48. // Gibt die Anzahl der Elemente im Buffer zurück
  49. int DXBuffer::getElementAnzahl() const
  50. {
  51. return len / elLen;
  52. }
  53. #ifdef WIN32
  54. // Inhalt der DX11Buffer Klasse
  55. // Konstruktor
  56. // eSize: Die Länge eines Elementes in Bytes
  57. DX11Buffer::DX11Buffer( int eSize, ID3D11Device * device, ID3D11DeviceContext * context, int bindFlags )
  58. : DXBuffer( eSize )
  59. {
  60. buffer = 0;
  61. description = new D3D11_BUFFER_DESC();
  62. memset( description, 0, sizeof( description ) );
  63. description->Usage = D3D11_USAGE_DYNAMIC;
  64. description->CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  65. description->BindFlags = bindFlags;
  66. this->device = device;
  67. this->context = context;
  68. }
  69. // Destruktor
  70. DX11Buffer::~DX11Buffer()
  71. {
  72. if( buffer )
  73. buffer->Release();
  74. delete description;
  75. }
  76. // Kopiert die Daten in den Buffer, fals sie sich verändert haben
  77. // zRObj: Das Objekt, mit dem die Grafikkarte angesprochen wird
  78. void DX11Buffer::copieren( int byteCount )
  79. {
  80. if( !len )
  81. return;
  82. if( byteCount < 0 )
  83. byteCount = len;
  84. if( description->ByteWidth < (unsigned)len )
  85. {
  86. if( buffer )
  87. buffer->Release();
  88. buffer = 0;
  89. }
  90. if( !buffer )
  91. {
  92. description->ByteWidth = len;
  93. device->CreateBuffer( description, 0, &buffer );
  94. if( data )
  95. changed = 1;
  96. }
  97. if( changed )
  98. {
  99. D3D11_MAPPED_SUBRESOURCE map;
  100. if( ( description->Usage | D3D11_USAGE_DYNAMIC ) == description->Usage )
  101. context->Map( buffer, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &map );
  102. else
  103. context->Map( buffer, 0, D3D11_MAP::D3D11_MAP_WRITE, 0, &map );
  104. memcpy( map.pData, data, byteCount );
  105. context->Unmap( buffer, 0 );
  106. changed = 0;
  107. }
  108. }
  109. // Gibt den Buffer zurück
  110. ID3D11Buffer * DX11Buffer::zBuffer() const
  111. {
  112. return buffer;
  113. }
  114. // Inhalt der DXStructuredBuffer Klasse
  115. // Konstruktor
  116. // eSize: Die Länge eines Elementes in Bytes
  117. DX11StructuredBuffer::DX11StructuredBuffer( int eSize, ID3D11Device * device, ID3D11DeviceContext * context )
  118. : DX11Buffer( eSize, device, context, D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE )
  119. {
  120. description->MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
  121. description->StructureByteStride = eSize;
  122. description->Usage = D3D11_USAGE_DEFAULT;
  123. view = 0;
  124. }
  125. // Destruktor
  126. DX11StructuredBuffer::~DX11StructuredBuffer()
  127. {
  128. if( view )
  129. view->Release();
  130. }
  131. // Kopiert die Daten in den Buffer, fals sie sich verändert haben
  132. // zRObj: Das Objekt, mit dem die Grafikkarte angesprochen wird
  133. void DX11StructuredBuffer::copieren( int byteCount )
  134. {
  135. ID3D11Buffer *old = buffer;
  136. DX11Buffer::copieren( byteCount );
  137. if( buffer != old )
  138. {
  139. if( view )
  140. view->Release();
  141. D3D11_SHADER_RESOURCE_VIEW_DESC desc = {};
  142. desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX;
  143. desc.BufferEx.FirstElement = 0;
  144. desc.Format = DXGI_FORMAT_UNKNOWN;
  145. desc.BufferEx.NumElements = description->ByteWidth / description->StructureByteStride;
  146. device->CreateShaderResourceView( buffer, &desc, &view );
  147. }
  148. }
  149. // Gibt die verwendtete Shader Resource View zurück
  150. DX11StructuredBuffer::operator ID3D11ShaderResourceView *( ) const
  151. {
  152. return view;
  153. }
  154. // Konstruktor
  155. // eSize: Die Länge eines Elementes in Bytes
  156. DX12Buffer::DX12Buffer( int eSize, ID3D12Device2 * device, ID3D12GraphicsCommandList2 * list, int flags )
  157. : DXBuffer( eSize ),
  158. buffer( 0 ),
  159. intermediate( 0 ),
  160. device( device ),
  161. list( list )
  162. {
  163. description = new D3D12_RESOURCE_DESC();
  164. ZeroMemory( description, sizeof( D3D12_RESOURCE_DESC ) );
  165. description->Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
  166. description->Height = 1;
  167. description->DepthOrArraySize = 1;
  168. description->MipLevels = 1;
  169. description->Format = DXGI_FORMAT_UNKNOWN;
  170. description->SampleDesc.Count = 1;
  171. description->Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
  172. description->Flags = (D3D12_RESOURCE_FLAGS)flags;
  173. }
  174. // Destruktor
  175. DX12Buffer::~DX12Buffer()
  176. {
  177. if( intermediate )
  178. intermediate->Release();
  179. if( buffer )
  180. buffer->Release();
  181. delete description;
  182. }
  183. // Kopiert die Daten in den Buffer, fals sie sich verändert haben
  184. void DX12Buffer::copieren( int byteCount )
  185. {
  186. if( !len )
  187. return;
  188. if( byteCount < 0 )
  189. byteCount = len;
  190. if( description->Width < len )
  191. {
  192. if( intermediate )
  193. intermediate->Release();
  194. if( buffer )
  195. buffer->Release();
  196. buffer = 0;
  197. description->Width = len;
  198. }
  199. if( !buffer )
  200. {
  201. D3D12_HEAP_PROPERTIES hprop;
  202. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  203. hprop.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  204. hprop.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  205. hprop.CreationNodeMask = 1;
  206. hprop.VisibleNodeMask = 1;
  207. device->CreateCommittedResource( &hprop, D3D12_HEAP_FLAG_NONE, description, D3D12_RESOURCE_STATE_GENERIC_READ, 0, __uuidof( ID3D12Resource ), (void **)& buffer );
  208. hprop.Type = D3D12_HEAP_TYPE_UPLOAD;
  209. device->CreateCommittedResource( &hprop, D3D12_HEAP_FLAG_NONE, description, D3D12_RESOURCE_STATE_GENERIC_READ, 0, __uuidof( ID3D12Resource ), (void **)& intermediate );
  210. if( data )
  211. changed = 1;
  212. }
  213. if( changed && data )
  214. {
  215. BYTE *pData;
  216. D3D12_RANGE r;
  217. r.Begin = 0;
  218. r.End = 0;
  219. buffer->Map( 0, &r, (void **)&pData );
  220. memcpy( pData, data, byteCount );
  221. r.End = byteCount;
  222. buffer->Unmap( 0, &r );
  223. //list->CopyBufferRegion( buffer, 0, intermediate, 0, len );
  224. changed = 0;
  225. }
  226. }
  227. // Gibt den Buffer zurück
  228. ID3D12Resource *DX12Buffer::zBuffer() const
  229. {
  230. return buffer;
  231. }
  232. DX12IndexBuffer::DX12IndexBuffer( int eSize, ID3D12Device2 *device, DX12CopyCommandQueue *copy, DX12DirectCommandQueue *direct )
  233. : DX12Buffer( eSize, device, copy->getCommandList(), D3D12_RESOURCE_FLAG_NONE ),
  234. copy( copy ),
  235. direct( direct )
  236. {
  237. ibs = 0;
  238. }
  239. DX12IndexBuffer::~DX12IndexBuffer()
  240. {}
  241. // Kopiert die Daten in den Buffer, fals sie sich verändert haben
  242. void DX12IndexBuffer::copieren( int byteCount )
  243. {
  244. /*if( ibs )
  245. {
  246. D3D12_RESOURCE_BARRIER barrier;
  247. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  248. barrier.Transition.pResource = buffer;
  249. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_INDEX_BUFFER;
  250. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  251. barrier.Transition.Subresource = 0;
  252. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  253. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  254. direct->execute();
  255. ibs = 0;
  256. }*/
  257. DX12Buffer::copieren( byteCount );
  258. //copy->execute();
  259. /*D3D12_RESOURCE_BARRIER barrier;
  260. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  261. barrier.Transition.pResource = buffer;
  262. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  263. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_INDEX_BUFFER;
  264. barrier.Transition.Subresource = 0;
  265. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  266. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  267. direct->execute();
  268. ibs = 1;*/
  269. }
  270. DX12VertexBuffer::DX12VertexBuffer( int eSize, ID3D12Device2 *device, DX12CopyCommandQueue *copy, DX12DirectCommandQueue *direct )
  271. : DX12Buffer( eSize, device, copy->getCommandList(), D3D12_RESOURCE_FLAG_NONE ),
  272. copy( copy ),
  273. direct( direct )
  274. {
  275. vbs = 0;
  276. }
  277. DX12VertexBuffer::~DX12VertexBuffer()
  278. {}
  279. // Kopiert die Daten in den Buffer, fals sie sich verändert haben
  280. void DX12VertexBuffer::copieren( int byteCount )
  281. {
  282. /*if( vbs )
  283. {
  284. D3D12_RESOURCE_BARRIER barrier;
  285. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  286. barrier.Transition.pResource = buffer;
  287. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
  288. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
  289. barrier.Transition.Subresource = 0;
  290. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  291. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  292. direct->execute();
  293. vbs = 0;
  294. }*/
  295. DX12Buffer::copieren( byteCount );
  296. //copy->execute();
  297. /*D3D12_RESOURCE_BARRIER barrier;
  298. barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  299. barrier.Transition.pResource = buffer;
  300. barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
  301. barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
  302. barrier.Transition.Subresource = 0;
  303. barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
  304. direct->getCommandList()->ResourceBarrier( 1, &barrier );
  305. direct->execute();
  306. vbs = 1;*/
  307. }
  308. #endif