CmD3D11HardwareBuffer.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "CmD3D11HardwareBuffer.h"
  2. #include "CmD3D11Mappings.h"
  3. #include "CmD3D11Device.h"
  4. #include "CmException.h"
  5. #include "CmDebug.h"
  6. namespace CamelotEngine
  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. SAFE_DELETE(mpTempStagingBuffer); // should never be nonzero unless destroyed while locked
  88. }
  89. void* D3D11HardwareBuffer::lockImpl(UINT32 offset,
  90. UINT32 length, GpuLockOptions options)
  91. {
  92. if (length > mSizeInBytes)
  93. CM_EXCEPT(RenderingAPIException, "Provided length " + toString(length) + " larger than the buffer " + toString(mSizeInBytes) + ".");
  94. // Use direct (and faster) Map/Unmap if dynamic write, or a staging read/write
  95. if((mDesc.Usage == D3D11_USAGE_DYNAMIC && options != GBL_READ_ONLY) || mDesc.Usage == D3D11_USAGE_STAGING)
  96. {
  97. D3D11_MAP mapType;
  98. switch(options)
  99. {
  100. case GBL_WRITE_ONLY_DISCARD:
  101. if (mUsage & GBU_DYNAMIC)
  102. {
  103. // Map cannot be called with MAP_WRITE access,
  104. // because the Resource was created as D3D11_USAGE_DYNAMIC.
  105. // D3D11_USAGE_DYNAMIC Resources must use either MAP_WRITE_DISCARD
  106. // or MAP_WRITE_NO_OVERWRITE with Map.
  107. mapType = D3D11_MAP_WRITE_DISCARD;
  108. }
  109. else
  110. {
  111. // Map cannot be called with MAP_WRITE_DISCARD access,
  112. // because the Resource was not created as D3D11_USAGE_DYNAMIC.
  113. // D3D11_USAGE_DYNAMIC Resources must use either MAP_WRITE_DISCARD
  114. // or MAP_WRITE_NO_OVERWRITE with Map.
  115. mapType = D3D11_MAP_WRITE;
  116. LOGWRN("DISCARD lock is only available on dynamic buffers. Falling back to normal write.");
  117. }
  118. break;
  119. case GBL_WRITE_ONLY_NO_OVERWRITE:
  120. if(mBufferType == BT_INDEX || mBufferType == BT_VERTEX)
  121. mapType = D3D11_MAP_WRITE_NO_OVERWRITE;
  122. else
  123. {
  124. mapType = D3D11_MAP_WRITE;
  125. LOGWRN("NO_OVERWRITE lock is not available on this (" + toString(mBufferType) + ") buffer type. Falling back to normal write.");
  126. }
  127. break;
  128. case GBL_WRITE_ONLY:
  129. mapType = D3D11_MAP_WRITE;
  130. break;
  131. case GBL_READ_WRITE:
  132. if ((mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) != 0 &&
  133. (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) != 0)
  134. {
  135. mapType = D3D11_MAP_READ_WRITE;
  136. }
  137. else if(mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE)
  138. {
  139. mapType = D3D11_MAP_WRITE;
  140. }
  141. else
  142. {
  143. mapType = D3D11_MAP_READ;
  144. }
  145. break;
  146. case GBL_READ_ONLY:
  147. mapType = D3D11_MAP_READ;
  148. break;
  149. }
  150. if(D3D11Mappings::isMappingRead(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) == 0)
  151. CM_EXCEPT(RenderingAPIException, "Trying to read a buffer, but buffer wasn't created with a read access flag.");
  152. if(D3D11Mappings::isMappingWrite(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) == 0)
  153. CM_EXCEPT(RenderingAPIException, "Trying to write to a buffer, but buffer wasn't created with a write access flag.");
  154. void * pRet = NULL;
  155. D3D11_MAPPED_SUBRESOURCE mappedSubResource;
  156. mappedSubResource.pData = NULL;
  157. mDevice.clearErrors();
  158. HRESULT hr = mDevice.getImmediateContext()->Map(mD3DBuffer, 0, mapType, 0, &mappedSubResource);
  159. if (FAILED(hr) || mDevice.hasError())
  160. {
  161. String msg = mDevice.getErrorDescription();
  162. CM_EXCEPT(RenderingAPIException, "Error calling Map: " + msg);
  163. }
  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 = new D3D11HardwareBuffer(mBufferType, mUsage, 1, mSizeInBytes, mDevice, true);
  174. }
  175. // schedule a copy to the staging
  176. if (options != GBL_WRITE_ONLY_DISCARD)
  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. SAFE_DELETE(mpTempStagingBuffer);
  197. }
  198. else
  199. {
  200. // unmap
  201. mDevice.getImmediateContext()->Unmap(mD3DBuffer, 0);
  202. }
  203. }
  204. void D3D11HardwareBuffer::copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  205. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer)
  206. {
  207. // If we're copying same-size buffers in their entirety...
  208. if (srcOffset == 0 && dstOffset == 0 &&
  209. length == mSizeInBytes && mSizeInBytes == srcBuffer.getSizeInBytes())
  210. {
  211. // schedule hardware buffer copy
  212. mDevice.getImmediateContext()->CopyResource(mD3DBuffer, static_cast<D3D11HardwareBuffer&>(srcBuffer).getD3DBuffer());
  213. if (mDevice.hasError())
  214. {
  215. String errorDescription = mDevice.getErrorDescription();
  216. CM_EXCEPT(RenderingAPIException, "Cannot copy D3D11 resource\nError Description:" + errorDescription);
  217. }
  218. }
  219. else
  220. {
  221. // copy subregion
  222. D3D11_BOX srcBox;
  223. srcBox.left = (UINT)srcOffset;
  224. srcBox.right = (UINT)srcOffset + length;
  225. srcBox.top = 0;
  226. srcBox.bottom = 1;
  227. srcBox.front = 0;
  228. srcBox.back = 1;
  229. mDevice.getImmediateContext()->CopySubresourceRegion(mD3DBuffer, 0, (UINT)dstOffset, 0, 0,
  230. static_cast<D3D11HardwareBuffer&>(srcBuffer).getD3DBuffer(), 0, &srcBox);
  231. if (mDevice.hasError())
  232. {
  233. String errorDescription = mDevice.getErrorDescription();
  234. CM_EXCEPT(RenderingAPIException, "Cannot copy D3D11 subresource region\nError Description:" + errorDescription);
  235. }
  236. }
  237. }
  238. void D3D11HardwareBuffer::readData(UINT32 offset, UINT32 length, void* pDest)
  239. {
  240. // There is no functional interface in D3D, just do via manual
  241. // lock, copy & unlock
  242. void* pSrc = this->lock(offset, length, GBL_READ_ONLY);
  243. memcpy(pDest, pSrc, length);
  244. this->unlock();
  245. }
  246. void D3D11HardwareBuffer::writeData(UINT32 offset, UINT32 length,
  247. const void* pSource, bool discardWholeBuffer)
  248. {
  249. if(mDesc.Usage == D3D11_USAGE_DYNAMIC || mDesc.Usage == D3D11_USAGE_STAGING)
  250. {
  251. void* pDst = this->lock(offset, length,
  252. discardWholeBuffer ? GBL_WRITE_ONLY_DISCARD : GBL_WRITE_ONLY);
  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. CM_EXCEPT(RenderingAPIException, "Trying to write into a buffer with unsupported usage: " + toString(mDesc.Usage));
  263. }
  264. }
  265. }