BsD3D11HardwareBuffer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D11HardwareBuffer.h"
  4. #include "BsD3D11Mappings.h"
  5. #include "BsD3D11Device.h"
  6. #include "BsException.h"
  7. #include "BsDebug.h"
  8. namespace bs { namespace ct
  9. {
  10. D3D11HardwareBuffer::D3D11HardwareBuffer(BufferType btype, GpuBufferUsage usage, UINT32 elementCount, UINT32 elementSize,
  11. D3D11Device& device, bool useSystemMem, bool streamOut, bool randomGpuWrite, bool useCounter)
  12. : HardwareBuffer(elementCount * elementSize), mD3DBuffer(nullptr), mpTempStagingBuffer(nullptr)
  13. , mUseTempStagingBuffer(false), mBufferType(btype), mDevice(device), mElementCount(elementCount)
  14. , mElementSize(elementSize), mUsage(usage), mRandomGpuWrite(randomGpuWrite), mUseCounter(useCounter)
  15. {
  16. assert((!streamOut || btype == BT_VERTEX) && "Stream out flag is only supported on vertex buffers.");
  17. assert(!randomGpuWrite || (btype & BT_GROUP_GENERIC) != 0 && "randomGpuWrite flag can only be enabled with standard, append/consume, indirect argument, structured or raw buffers.");
  18. assert(btype != BT_APPENDCONSUME || randomGpuWrite && "Append/Consume buffer must be created with randomGpuWrite enabled.");
  19. assert(!useCounter || btype == BT_STRUCTURED && "Counter can only be used with a structured buffer.");
  20. assert(!useCounter || randomGpuWrite && "Counter can only be used with buffers that have randomGpuWrite enabled.");
  21. assert(!randomGpuWrite || !useSystemMem && "randomGpuWrite and useSystemMem cannot be used together.");
  22. assert(!(useSystemMem && streamOut) && "useSystemMem and streamOut cannot be used together.");
  23. mDesc.ByteWidth = getSize();
  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(usage);
  41. mDesc.CPUAccessFlags = D3D11Mappings::getAccessFlags(usage);
  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(mpTempStagingBuffer);
  83. }
  84. void* D3D11HardwareBuffer::map(UINT32 offset, UINT32 length, GpuLockOptions options, UINT32 deviceIdx, UINT32 queueIdx)
  85. {
  86. if (length > mSize)
  87. BS_EXCEPT(RenderingAPIException, "Provided length " + toString(length) + " larger than the buffer " + toString(mSize) + ".");
  88. // Use direct (and faster) Map/Unmap if dynamic write, or a staging read/write
  89. if((mDesc.Usage == D3D11_USAGE_DYNAMIC && options != GBL_READ_ONLY) || mDesc.Usage == D3D11_USAGE_STAGING)
  90. {
  91. D3D11_MAP mapType;
  92. switch(options)
  93. {
  94. case GBL_WRITE_ONLY_DISCARD:
  95. if (mUsage & GBU_DYNAMIC)
  96. {
  97. mapType = D3D11_MAP_WRITE_DISCARD;
  98. }
  99. else
  100. {
  101. // Map cannot be called with MAP_WRITE_DISCARD access, because the Resource was not created as
  102. // D3D11_USAGE_DYNAMIC. D3D11_USAGE_DYNAMIC Resources must use either MAP_WRITE_DISCARD
  103. // or MAP_WRITE_NO_OVERWRITE with Map.
  104. mapType = D3D11_MAP_WRITE;
  105. }
  106. break;
  107. case GBL_WRITE_ONLY_NO_OVERWRITE:
  108. if(mBufferType == BT_INDEX || mBufferType == BT_VERTEX)
  109. mapType = D3D11_MAP_WRITE_NO_OVERWRITE;
  110. else
  111. {
  112. // Note supported on anything but index/vertex buffers in DX11 (this restriction was dropped in 11.1)
  113. mapType = D3D11_MAP_WRITE;
  114. }
  115. break;
  116. case GBL_WRITE_ONLY:
  117. mapType = D3D11_MAP_WRITE;
  118. break;
  119. case GBL_READ_WRITE:
  120. if ((mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) != 0 &&
  121. (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) != 0)
  122. {
  123. mapType = D3D11_MAP_READ_WRITE;
  124. }
  125. else if(mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE)
  126. {
  127. mapType = D3D11_MAP_WRITE;
  128. }
  129. else
  130. {
  131. mapType = D3D11_MAP_READ;
  132. }
  133. break;
  134. case GBL_READ_ONLY:
  135. mapType = D3D11_MAP_READ;
  136. break;
  137. }
  138. if(D3D11Mappings::isMappingRead(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) == 0)
  139. LOGERR("Trying to read a buffer, but buffer wasn't created with a read access flag.");
  140. if(D3D11Mappings::isMappingWrite(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) == 0)
  141. LOGERR("Trying to write to a buffer, but buffer wasn't created with a write access flag.");
  142. void * pRet = NULL;
  143. D3D11_MAPPED_SUBRESOURCE mappedSubResource;
  144. mappedSubResource.pData = NULL;
  145. mDevice.clearErrors();
  146. HRESULT hr = mDevice.getImmediateContext()->Map(mD3DBuffer, 0, mapType, 0, &mappedSubResource);
  147. if (FAILED(hr) || mDevice.hasError())
  148. {
  149. String msg = mDevice.getErrorDescription();
  150. BS_EXCEPT(RenderingAPIException, "Error calling Map: " + msg);
  151. }
  152. pRet = static_cast<void*>(static_cast<char*>(mappedSubResource.pData) + offset);
  153. return pRet;
  154. }
  155. else // Otherwise create a staging buffer to do all read/write operations on. Usually try to avoid this.
  156. {
  157. mUseTempStagingBuffer = true;
  158. if (!mpTempStagingBuffer)
  159. {
  160. // Create another buffer instance but use system memory
  161. mpTempStagingBuffer = bs_new<D3D11HardwareBuffer>(mBufferType, mUsage, 1, mSize, std::ref(mDevice), true);
  162. }
  163. // Schedule a copy to the staging
  164. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  165. mpTempStagingBuffer->copyData(*this, 0, 0, mSize, true);
  166. // Register whether we'll need to upload on unlock
  167. mStagingUploadNeeded = (options != GBL_READ_ONLY);
  168. return mpTempStagingBuffer->lock(offset, length, options);
  169. }
  170. }
  171. void D3D11HardwareBuffer::unmap()
  172. {
  173. if (mUseTempStagingBuffer)
  174. {
  175. mUseTempStagingBuffer = false;
  176. mpTempStagingBuffer->unlock();
  177. if (mStagingUploadNeeded)
  178. copyData(*mpTempStagingBuffer, 0, 0, mSize, true);
  179. if(mpTempStagingBuffer != nullptr)
  180. {
  181. bs_delete(mpTempStagingBuffer);
  182. mpTempStagingBuffer = nullptr;
  183. }
  184. }
  185. else
  186. {
  187. mDevice.getImmediateContext()->Unmap(mD3DBuffer, 0);
  188. }
  189. }
  190. void D3D11HardwareBuffer::copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  191. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer, UINT32 queueIdx)
  192. {
  193. // If we're copying same-size buffers in their entirety
  194. if (srcOffset == 0 && dstOffset == 0 &&
  195. length == mSize && mSize == srcBuffer.getSize())
  196. {
  197. mDevice.getImmediateContext()->CopyResource(mD3DBuffer, static_cast<D3D11HardwareBuffer&>(srcBuffer).getD3DBuffer());
  198. if (mDevice.hasError())
  199. {
  200. String errorDescription = mDevice.getErrorDescription();
  201. BS_EXCEPT(RenderingAPIException, "Cannot copy D3D11 resource\nError Description:" + errorDescription);
  202. }
  203. }
  204. else
  205. {
  206. // Copy subregion
  207. D3D11_BOX srcBox;
  208. srcBox.left = (UINT)srcOffset;
  209. srcBox.right = (UINT)srcOffset + length;
  210. srcBox.top = 0;
  211. srcBox.bottom = 1;
  212. srcBox.front = 0;
  213. srcBox.back = 1;
  214. mDevice.getImmediateContext()->CopySubresourceRegion(mD3DBuffer, 0, (UINT)dstOffset, 0, 0,
  215. static_cast<D3D11HardwareBuffer&>(srcBuffer).getD3DBuffer(), 0, &srcBox);
  216. if (mDevice.hasError())
  217. {
  218. String errorDescription = mDevice.getErrorDescription();
  219. BS_EXCEPT(RenderingAPIException, "Cannot copy D3D11 subresource region\nError Description:" + errorDescription);
  220. }
  221. }
  222. }
  223. void D3D11HardwareBuffer::readData(UINT32 offset, UINT32 length, void* dest, UINT32 deviceIdx, UINT32 queueIdx)
  224. {
  225. // There is no functional interface in D3D, just do via manual lock, copy & unlock
  226. void* pSrc = this->lock(offset, length, GBL_READ_ONLY);
  227. memcpy(dest, pSrc, length);
  228. this->unlock();
  229. }
  230. void D3D11HardwareBuffer::writeData(UINT32 offset, UINT32 length, const void* pSource, BufferWriteType writeFlags,
  231. UINT32 queueIdx)
  232. {
  233. if(mDesc.Usage == D3D11_USAGE_DYNAMIC || mDesc.Usage == D3D11_USAGE_STAGING)
  234. {
  235. GpuLockOptions lockOption = GBL_WRITE_ONLY;
  236. if(writeFlags == BWT_DISCARD)
  237. lockOption = GBL_WRITE_ONLY_DISCARD;
  238. else if(writeFlags == BTW_NO_OVERWRITE)
  239. lockOption = GBL_WRITE_ONLY_NO_OVERWRITE;
  240. void* pDst = this->lock(offset, length, lockOption);
  241. memcpy(pDst, pSource, length);
  242. this->unlock();
  243. }
  244. else if(mDesc.Usage == D3D11_USAGE_DEFAULT)
  245. {
  246. if (mBufferType == BT_CONSTANT)
  247. {
  248. assert(offset == 0);
  249. // Constant buffer cannot be updated partially using UpdateSubresource
  250. mDevice.getImmediateContext()->UpdateSubresource(mD3DBuffer, 0, nullptr, pSource, 0, 0);
  251. }
  252. else
  253. {
  254. D3D11_BOX dstBox;
  255. dstBox.left = (UINT)offset;
  256. dstBox.right = (UINT)offset + length;
  257. dstBox.top = 0;
  258. dstBox.bottom = 1;
  259. dstBox.front = 0;
  260. dstBox.back = 1;
  261. mDevice.getImmediateContext()->UpdateSubresource(mD3DBuffer, 0, &dstBox, pSource, 0, 0);
  262. }
  263. }
  264. else
  265. {
  266. LOGERR("Trying to write into a buffer with unsupported usage: " + toString(mDesc.Usage));
  267. }
  268. }
  269. }}