CmD3D11HardwareBuffer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_READ_WRITE:
  129. if ((mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) != 0 &&
  130. (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) != 0)
  131. {
  132. mapType = D3D11_MAP_READ_WRITE;
  133. }
  134. else if(mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE)
  135. {
  136. mapType = D3D11_MAP_WRITE;
  137. }
  138. else
  139. {
  140. mapType = D3D11_MAP_READ;
  141. }
  142. break;
  143. case GBL_READ_ONLY:
  144. mapType = D3D11_MAP_READ;
  145. break;
  146. }
  147. if(D3D11Mappings::isMappingRead(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) == 0)
  148. CM_EXCEPT(RenderingAPIException, "Trying to read a buffer, but buffer wasn't created with a read access flag.");
  149. if(D3D11Mappings::isMappingWrite(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) == 0)
  150. CM_EXCEPT(RenderingAPIException, "Trying to write to a buffer, but buffer wasn't created with a write access flag.");
  151. void * pRet = NULL;
  152. D3D11_MAPPED_SUBRESOURCE mappedSubResource;
  153. mappedSubResource.pData = NULL;
  154. mDevice.clearErrors();
  155. HRESULT hr = mDevice.getImmediateContext()->Map(mD3DBuffer, 0, mapType, 0, &mappedSubResource);
  156. if (FAILED(hr) || mDevice.hasError())
  157. {
  158. String msg = mDevice.getErrorDescription();
  159. CM_EXCEPT(RenderingAPIException, "Error calling Map: " + msg);
  160. }
  161. pRet = static_cast<void*>(static_cast<char*>(mappedSubResource.pData) + offset);
  162. return pRet;
  163. }
  164. else // Otherwise create a staging buffer to do all read/write operations on. Usually try to avoid this.
  165. {
  166. mUseTempStagingBuffer = true;
  167. if (!mpTempStagingBuffer)
  168. {
  169. // create another buffer instance but use system memory
  170. mpTempStagingBuffer = new D3D11HardwareBuffer(mBufferType, mUsage, 1, mSizeInBytes, mDevice, true);
  171. }
  172. // schedule a copy to the staging
  173. if (options != GBL_WRITE_ONLY_DISCARD)
  174. mpTempStagingBuffer->copyData(*this, 0, 0, mSizeInBytes, true);
  175. // register whether we'll need to upload on unlock
  176. mStagingUploadNeeded = (options != GBL_READ_ONLY);
  177. return mpTempStagingBuffer->lock(offset, length, options);
  178. }
  179. }
  180. void D3D11HardwareBuffer::unlockImpl(void)
  181. {
  182. if (mUseTempStagingBuffer)
  183. {
  184. mUseTempStagingBuffer = false;
  185. // ok, we locked the staging buffer
  186. mpTempStagingBuffer->unlock();
  187. // copy data if needed
  188. // this is async but driver should keep reference
  189. if (mStagingUploadNeeded)
  190. copyData(*mpTempStagingBuffer, 0, 0, mSizeInBytes, true);
  191. // delete
  192. // not that efficient, but we should not be locking often
  193. SAFE_DELETE(mpTempStagingBuffer);
  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. CM_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. CM_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,
  244. const void* pSource, bool discardWholeBuffer)
  245. {
  246. if(mDesc.Usage == D3D11_USAGE_DYNAMIC || mDesc.Usage == D3D11_USAGE_STAGING)
  247. {
  248. void* pDst = this->lock(offset, length,
  249. discardWholeBuffer ? GBL_WRITE_ONLY_DISCARD : GBL_READ_WRITE);
  250. memcpy(pDst, pSource, length);
  251. this->unlock();
  252. }
  253. else if(mDesc.Usage == D3D11_USAGE_DEFAULT)
  254. {
  255. mDevice.getImmediateContext()->UpdateSubresource(mD3DBuffer, 0, nullptr, pSource, offset, length);
  256. }
  257. else
  258. {
  259. CM_EXCEPT(RenderingAPIException, "Trying to write into a buffer with unsupported usage: " + toString(mDesc.Usage));
  260. }
  261. }
  262. }