CmD3D11HardwareBuffer.cpp 10 KB

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