2
0

BsD3D11HardwareBuffer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "BsD3D11HardwareBuffer.h"
  2. #include "BsD3D11Mappings.h"
  3. #include "BsD3D11Device.h"
  4. #include "BsException.h"
  5. #include "BsDebug.h"
  6. #include "BsProfilerCPU.h"
  7. namespace BansheeEngine
  8. {
  9. D3D11HardwareBuffer::D3D11HardwareBuffer(BufferType btype, GpuBufferUsage usage, UINT32 elementCount, UINT32 elementSize,
  10. D3D11Device& device, bool useSystemMem, bool streamOut, bool randomGpuWrite, bool useCounter)
  11. : HardwareBuffer(usage, useSystemMem), mD3DBuffer(nullptr), mpTempStagingBuffer(nullptr), mUseTempStagingBuffer(false),
  12. mBufferType(btype), mDevice(device), mElementCount(elementCount), mElementSize(elementSize), mRandomGpuWrite(randomGpuWrite),
  13. mUseCounter(useCounter)
  14. {
  15. assert((!streamOut || btype == BT_VERTEX) && "Stream out flag is only supported on vertex buffers");
  16. assert(!randomGpuWrite || (btype & BT_GROUP_GENERIC) != 0 && "randomGpuWrite flag can only be enabled with append/consume, indirect argument, structured or raw buffers");
  17. assert(btype != BT_APPENDCONSUME || randomGpuWrite && "Append/Consume buffer must be created with randomGpuWrite enabled.");
  18. assert(!useCounter || btype == BT_STRUCTURED && "Counter can only be used with a structured buffer.");
  19. assert(!useCounter || randomGpuWrite && "Counter can only be used with buffers that have randomGpuWrite enabled.");
  20. assert(!randomGpuWrite || !useSystemMem && "randomGpuWrite and useSystemMem cannot be used together.");
  21. assert(!(useSystemMem && streamOut) && "useSystemMem and streamOut cannot be used together.");
  22. mSizeInBytes = elementCount * elementSize;
  23. mDesc.ByteWidth = mSizeInBytes;
  24. mDesc.MiscFlags = 0;
  25. mDesc.StructureByteStride = 0;
  26. if (useSystemMem)
  27. {
  28. mDesc.Usage = D3D11_USAGE_STAGING;
  29. mDesc.BindFlags = 0;
  30. mDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ ;
  31. }
  32. else if(randomGpuWrite)
  33. {
  34. mDesc.Usage = D3D11_USAGE_DEFAULT;
  35. mDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
  36. mDesc.CPUAccessFlags = 0;
  37. }
  38. else
  39. {
  40. mDesc.Usage = D3D11Mappings::_getUsage(mUsage);
  41. mDesc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
  42. switch(btype)
  43. {
  44. case BT_VERTEX:
  45. mDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  46. if (streamOut)
  47. mDesc.BindFlags |= D3D11_BIND_STREAM_OUTPUT;
  48. break;
  49. case BT_INDEX:
  50. mDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  51. break;
  52. case BT_CONSTANT:
  53. mDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  54. break;
  55. case BT_STRUCTURED:
  56. case BT_APPENDCONSUME:
  57. mDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  58. mDesc.StructureByteStride = elementSize;
  59. mDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
  60. break;
  61. case BT_RAW:
  62. mDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  63. mDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS;
  64. break;
  65. case BT_INDIRECTARGUMENT:
  66. mDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  67. mDesc.MiscFlags = D3D11_RESOURCE_MISC_DRAWINDIRECT_ARGS;
  68. break;
  69. }
  70. }
  71. HRESULT hr = device.getD3D11Device()->CreateBuffer( &mDesc, nullptr, &mD3DBuffer );
  72. if (FAILED(hr) || mDevice.hasError())
  73. {
  74. String msg = device.getErrorDescription();
  75. BS_EXCEPT(RenderingAPIException, "Cannot create D3D11 buffer: " + msg);
  76. }
  77. }
  78. D3D11HardwareBuffer::~D3D11HardwareBuffer()
  79. {
  80. SAFE_RELEASE(mD3DBuffer);
  81. if(mpTempStagingBuffer != nullptr)
  82. bs_delete<PoolAlloc>(mpTempStagingBuffer);
  83. }
  84. void* D3D11HardwareBuffer::lockImpl(UINT32 offset,
  85. UINT32 length, GpuLockOptions options)
  86. {
  87. if (length > mSizeInBytes)
  88. BS_EXCEPT(RenderingAPIException, "Provided length " + toString(length) + " larger than the buffer " + toString(mSizeInBytes) + ".");
  89. // Use direct (and faster) Map/Unmap if dynamic write, or a staging read/write
  90. if((mDesc.Usage == D3D11_USAGE_DYNAMIC && options != GBL_READ_ONLY) || mDesc.Usage == D3D11_USAGE_STAGING)
  91. {
  92. D3D11_MAP mapType;
  93. switch(options)
  94. {
  95. case GBL_WRITE_ONLY_DISCARD:
  96. if (mUsage & GBU_DYNAMIC)
  97. {
  98. mapType = D3D11_MAP_WRITE_DISCARD;
  99. }
  100. else
  101. {
  102. // Map cannot be called with MAP_WRITE_DISCARD access,
  103. // because the Resource was not created as D3D11_USAGE_DYNAMIC.
  104. // D3D11_USAGE_DYNAMIC Resources must use either MAP_WRITE_DISCARD
  105. // or MAP_WRITE_NO_OVERWRITE with Map.
  106. mapType = D3D11_MAP_WRITE;
  107. LOGWRN("DISCARD lock is only available on dynamic buffers. Falling back to normal write.");
  108. }
  109. break;
  110. case GBL_WRITE_ONLY_NO_OVERWRITE:
  111. if(mBufferType == BT_INDEX || mBufferType == BT_VERTEX)
  112. mapType = D3D11_MAP_WRITE_NO_OVERWRITE;
  113. else
  114. {
  115. mapType = D3D11_MAP_WRITE;
  116. LOGWRN("NO_OVERWRITE lock is not available on this (" + toString(mBufferType) + ") buffer type. Falling back to normal write.");
  117. }
  118. break;
  119. case GBL_WRITE_ONLY:
  120. mapType = D3D11_MAP_WRITE;
  121. break;
  122. case GBL_READ_WRITE:
  123. if ((mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) != 0 &&
  124. (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) != 0)
  125. {
  126. mapType = D3D11_MAP_READ_WRITE;
  127. }
  128. else if(mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE)
  129. {
  130. mapType = D3D11_MAP_WRITE;
  131. }
  132. else
  133. {
  134. mapType = D3D11_MAP_READ;
  135. }
  136. break;
  137. case GBL_READ_ONLY:
  138. mapType = D3D11_MAP_READ;
  139. break;
  140. }
  141. if(D3D11Mappings::isMappingRead(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) == 0)
  142. BS_EXCEPT(RenderingAPIException, "Trying to read a buffer, but buffer wasn't created with a read access flag.");
  143. if(D3D11Mappings::isMappingWrite(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) == 0)
  144. BS_EXCEPT(RenderingAPIException, "Trying to write to a buffer, but buffer wasn't created with a write access flag.");
  145. void * pRet = NULL;
  146. D3D11_MAPPED_SUBRESOURCE mappedSubResource;
  147. mappedSubResource.pData = NULL;
  148. mDevice.clearErrors();
  149. gProfilerCPU().beginSample("Map");
  150. HRESULT hr = mDevice.getImmediateContext()->Map(mD3DBuffer, 0, mapType, 0, &mappedSubResource);
  151. if (FAILED(hr) || mDevice.hasError())
  152. {
  153. String msg = mDevice.getErrorDescription();
  154. BS_EXCEPT(RenderingAPIException, "Error calling Map: " + msg);
  155. }
  156. gProfilerCPU().endSample("Map");
  157. pRet = static_cast<void*>(static_cast<char*>(mappedSubResource.pData) + offset);
  158. return pRet;
  159. }
  160. else // Otherwise create a staging buffer to do all read/write operations on. Usually try to avoid this.
  161. {
  162. mUseTempStagingBuffer = true;
  163. if (!mpTempStagingBuffer)
  164. {
  165. // create another buffer instance but use system memory
  166. mpTempStagingBuffer = bs_new<D3D11HardwareBuffer, PoolAlloc>(mBufferType, mUsage, 1, mSizeInBytes, std::ref(mDevice), true);
  167. }
  168. // schedule a copy to the staging
  169. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  170. mpTempStagingBuffer->copyData(*this, 0, 0, mSizeInBytes, true);
  171. // register whether we'll need to upload on unlock
  172. mStagingUploadNeeded = (options != GBL_READ_ONLY);
  173. return mpTempStagingBuffer->lock(offset, length, options);
  174. }
  175. }
  176. void D3D11HardwareBuffer::unlockImpl(void)
  177. {
  178. if (mUseTempStagingBuffer)
  179. {
  180. mUseTempStagingBuffer = false;
  181. // ok, we locked the staging buffer
  182. mpTempStagingBuffer->unlock();
  183. // copy data if needed
  184. // this is async but driver should keep reference
  185. if (mStagingUploadNeeded)
  186. copyData(*mpTempStagingBuffer, 0, 0, mSizeInBytes, true);
  187. // delete
  188. // not that efficient, but we should not be locking often
  189. if(mpTempStagingBuffer != nullptr)
  190. {
  191. bs_delete<PoolAlloc>(mpTempStagingBuffer);
  192. mpTempStagingBuffer = nullptr;
  193. }
  194. }
  195. else
  196. {
  197. // unmap
  198. mDevice.getImmediateContext()->Unmap(mD3DBuffer, 0);
  199. }
  200. }
  201. void D3D11HardwareBuffer::copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  202. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer)
  203. {
  204. // If we're copying same-size buffers in their entirety...
  205. if (srcOffset == 0 && dstOffset == 0 &&
  206. length == mSizeInBytes && mSizeInBytes == srcBuffer.getSizeInBytes())
  207. {
  208. // schedule hardware buffer copy
  209. mDevice.getImmediateContext()->CopyResource(mD3DBuffer, static_cast<D3D11HardwareBuffer&>(srcBuffer).getD3DBuffer());
  210. if (mDevice.hasError())
  211. {
  212. String errorDescription = mDevice.getErrorDescription();
  213. BS_EXCEPT(RenderingAPIException, "Cannot copy D3D11 resource\nError Description:" + errorDescription);
  214. }
  215. }
  216. else
  217. {
  218. // copy subregion
  219. D3D11_BOX srcBox;
  220. srcBox.left = (UINT)srcOffset;
  221. srcBox.right = (UINT)srcOffset + length;
  222. srcBox.top = 0;
  223. srcBox.bottom = 1;
  224. srcBox.front = 0;
  225. srcBox.back = 1;
  226. mDevice.getImmediateContext()->CopySubresourceRegion(mD3DBuffer, 0, (UINT)dstOffset, 0, 0,
  227. static_cast<D3D11HardwareBuffer&>(srcBuffer).getD3DBuffer(), 0, &srcBox);
  228. if (mDevice.hasError())
  229. {
  230. String errorDescription = mDevice.getErrorDescription();
  231. BS_EXCEPT(RenderingAPIException, "Cannot copy D3D11 subresource region\nError Description:" + errorDescription);
  232. }
  233. }
  234. }
  235. void D3D11HardwareBuffer::readData(UINT32 offset, UINT32 length, void* pDest)
  236. {
  237. // There is no functional interface in D3D, just do via manual
  238. // lock, copy & unlock
  239. void* pSrc = this->lock(offset, length, GBL_READ_ONLY);
  240. memcpy(pDest, pSrc, length);
  241. this->unlock();
  242. }
  243. void D3D11HardwareBuffer::writeData(UINT32 offset, UINT32 length, const void* pSource, BufferWriteType writeFlags)
  244. {
  245. if(mDesc.Usage == D3D11_USAGE_DYNAMIC || mDesc.Usage == D3D11_USAGE_STAGING)
  246. {
  247. GpuLockOptions lockOption = GBL_WRITE_ONLY;
  248. if(writeFlags == BufferWriteType::Discard)
  249. lockOption = GBL_WRITE_ONLY_DISCARD;
  250. else if(writeFlags == BufferWriteType::NoOverwrite)
  251. lockOption = GBL_WRITE_ONLY_NO_OVERWRITE;
  252. void* pDst = this->lock(offset, length, lockOption);
  253. memcpy(pDst, pSource, length);
  254. this->unlock();
  255. }
  256. else if(mDesc.Usage == D3D11_USAGE_DEFAULT)
  257. {
  258. mDevice.getImmediateContext()->UpdateSubresource(mD3DBuffer, 0, nullptr, pSource, offset, length);
  259. }
  260. else
  261. {
  262. BS_EXCEPT(RenderingAPIException, "Trying to write into a buffer with unsupported usage: " + toString(mDesc.Usage));
  263. }
  264. }
  265. }