CmD3D11HardwareBuffer.cpp 10 KB

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