CmD3D11HardwareBuffer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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, UINT32 sizeBytes, HardwareBuffer::Usage usage,
  9. D3D11Device& device, bool useSystemMemory, bool streamOut)
  10. : HardwareBuffer(usage, useSystemMemory),
  11. mD3DBuffer(0),
  12. mpTempStagingBuffer(0),
  13. mUseTempStagingBuffer(false),
  14. mBufferType(btype),
  15. mDevice(device)
  16. {
  17. mSizeInBytes = sizeBytes;
  18. mDesc.ByteWidth = static_cast<UINT>(sizeBytes);
  19. mDesc.MiscFlags = 0;
  20. if (useSystemMemory)
  21. {
  22. mDesc.Usage = D3D11_USAGE_STAGING;
  23. //A D3D11_USAGE_STAGING Resource cannot be bound to any parts of the graphics pipeline, so therefore cannot have any BindFlags bits set.
  24. mDesc.BindFlags = 0;
  25. mDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ ;// A D3D11_USAGE_STAGING Resource must have at least one CPUAccessFlag bit set.
  26. }
  27. else
  28. {
  29. mDesc.Usage = D3D11Mappings::_getUsage(mUsage);
  30. mDesc.BindFlags = btype == VERTEX_BUFFER ? D3D11_BIND_VERTEX_BUFFER :
  31. btype == INDEX_BUFFER ? D3D11_BIND_INDEX_BUFFER :
  32. D3D11_BIND_CONSTANT_BUFFER;
  33. mDesc.CPUAccessFlags = D3D11Mappings::_getAccessFlags(mUsage);
  34. }
  35. // Check of stream out flag
  36. if (streamOut && btype != CONSTANT_BUFFER)
  37. {
  38. mDesc.BindFlags |= D3D11_BIND_STREAM_OUTPUT;
  39. }
  40. // TODO: we can explicitly initialise the buffer contents here if we like
  41. // not doing this since OGRE doesn't support this model yet
  42. HRESULT hr = device.getD3D11Device()->CreateBuffer( &mDesc, nullptr, &mD3DBuffer );
  43. if (FAILED(hr) || mDevice.hasError())
  44. {
  45. String msg = device.getErrorDescription();
  46. CM_EXCEPT(RenderingAPIException, "Cannot create D3D11 buffer: " + msg);
  47. }
  48. }
  49. D3D11HardwareBuffer::~D3D11HardwareBuffer()
  50. {
  51. SAFE_RELEASE(mD3DBuffer);
  52. SAFE_DELETE(mpTempStagingBuffer); // should never be nonzero unless destroyed while locked
  53. }
  54. void* D3D11HardwareBuffer::lockImpl(UINT32 offset,
  55. UINT32 length, LockOptions options)
  56. {
  57. if (length > mSizeInBytes)
  58. CM_EXCEPT(RenderingAPIException, "Provided length " + toString(length) + " larger than the buffer " + toString(mSizeInBytes) + ".");
  59. // Use direct (and faster) Map/Unmap if dynamic write, or a staging read/write
  60. if((mDesc.Usage == D3D11_USAGE_DYNAMIC && options != HardwareBuffer::HBL_READ_ONLY) || mDesc.Usage == D3D11_USAGE_STAGING)
  61. {
  62. D3D11_MAP mapType;
  63. switch(options)
  64. {
  65. case HBL_DISCARD:
  66. if (mUsage & HardwareBuffer::HBU_DYNAMIC)
  67. {
  68. // Map cannot be called with MAP_WRITE access,
  69. // because the Resource was created as D3D11_USAGE_DYNAMIC.
  70. // D3D11_USAGE_DYNAMIC Resources must use either MAP_WRITE_DISCARD
  71. // or MAP_WRITE_NO_OVERWRITE with Map.
  72. mapType = D3D11_MAP_WRITE_DISCARD;
  73. }
  74. else
  75. {
  76. // Map cannot be called with MAP_WRITE_DISCARD access,
  77. // because the Resource was not created as D3D11_USAGE_DYNAMIC.
  78. // D3D11_USAGE_DYNAMIC Resources must use either MAP_WRITE_DISCARD
  79. // or MAP_WRITE_NO_OVERWRITE with Map.
  80. mapType = D3D11_MAP_WRITE;
  81. LOGWRN("DISCARD lock is only available on dynamic buffers. Falling back to normal write.");
  82. }
  83. break;
  84. case HBL_NO_OVERWRITE:
  85. if(mBufferType == INDEX_BUFFER || mBufferType == VERTEX_BUFFER)
  86. mapType = D3D11_MAP_WRITE_NO_OVERWRITE;
  87. else
  88. {
  89. mapType = D3D11_MAP_WRITE;
  90. LOGWRN("NO_OVERWRITE lock is not available on this (" + toString(mBufferType) + ") buffer type. Falling back to normal write.");
  91. }
  92. break;
  93. case HBL_NORMAL:
  94. if ((mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) != 0 &&
  95. (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) != 0)
  96. {
  97. mapType = D3D11_MAP_READ_WRITE;
  98. }
  99. else if(mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE)
  100. {
  101. mapType = D3D11_MAP_WRITE;
  102. }
  103. else
  104. {
  105. mapType = D3D11_MAP_READ;
  106. }
  107. break;
  108. case HBL_READ_ONLY:
  109. mapType = D3D11_MAP_READ;
  110. break;
  111. }
  112. if(D3D11Mappings::isMappingRead(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) == 0)
  113. CM_EXCEPT(RenderingAPIException, "Trying to read a buffer, but buffer wasn't created with a read access flag.");
  114. if(D3D11Mappings::isMappingWrite(mapType) && (mDesc.CPUAccessFlags & D3D11_CPU_ACCESS_WRITE) == 0)
  115. CM_EXCEPT(RenderingAPIException, "Trying to write to a buffer, but buffer wasn't created with a write access flag.");
  116. void * pRet = NULL;
  117. D3D11_MAPPED_SUBRESOURCE mappedSubResource;
  118. mappedSubResource.pData = NULL;
  119. mDevice.clearErrors();
  120. HRESULT hr = mDevice.getImmediateContext()->Map(mD3DBuffer, 0, mapType, 0, &mappedSubResource);
  121. if (FAILED(hr) || mDevice.hasError())
  122. {
  123. String msg = mDevice.getErrorDescription();
  124. CM_EXCEPT(RenderingAPIException, "Error calling Map: " + msg);
  125. }
  126. pRet = static_cast<void*>(static_cast<char*>(mappedSubResource.pData) + offset);
  127. return pRet;
  128. }
  129. else // Otherwise create a staging buffer to do all read/write operations on. Usually try to avoid this.
  130. {
  131. mUseTempStagingBuffer = true;
  132. if (!mpTempStagingBuffer)
  133. {
  134. // create another buffer instance but use system memory
  135. mpTempStagingBuffer = new D3D11HardwareBuffer(mBufferType,
  136. mSizeInBytes, mUsage, mDevice, true, false);
  137. }
  138. // schedule a copy to the staging
  139. if (options != HBL_DISCARD)
  140. mpTempStagingBuffer->copyData(*this, 0, 0, mSizeInBytes, true);
  141. // register whether we'll need to upload on unlock
  142. mStagingUploadNeeded = (options != HBL_READ_ONLY);
  143. return mpTempStagingBuffer->lock(offset, length, options);
  144. }
  145. }
  146. void D3D11HardwareBuffer::unlockImpl(void)
  147. {
  148. if (mUseTempStagingBuffer)
  149. {
  150. mUseTempStagingBuffer = false;
  151. // ok, we locked the staging buffer
  152. mpTempStagingBuffer->unlock();
  153. // copy data if needed
  154. // this is async but driver should keep reference
  155. if (mStagingUploadNeeded)
  156. copyData(*mpTempStagingBuffer, 0, 0, mSizeInBytes, true);
  157. // delete
  158. // not that efficient, but we should not be locking often
  159. SAFE_DELETE(mpTempStagingBuffer);
  160. }
  161. else
  162. {
  163. // unmap
  164. mDevice.getImmediateContext()->Unmap(mD3DBuffer, 0);
  165. }
  166. }
  167. void D3D11HardwareBuffer::copyData(HardwareBuffer& srcBuffer, UINT32 srcOffset,
  168. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer)
  169. {
  170. // If we're copying same-size buffers in their entirety...
  171. if (srcOffset == 0 && dstOffset == 0 &&
  172. length == mSizeInBytes && mSizeInBytes == srcBuffer.getSizeInBytes())
  173. {
  174. // schedule hardware buffer copy
  175. mDevice.getImmediateContext()->CopyResource(mD3DBuffer, static_cast<D3D11HardwareBuffer&>(srcBuffer).getD3DBuffer());
  176. if (mDevice.hasError())
  177. {
  178. String errorDescription = mDevice.getErrorDescription();
  179. CM_EXCEPT(RenderingAPIException, "Cannot copy D3D11 resource\nError Description:" + errorDescription);
  180. }
  181. }
  182. else
  183. {
  184. // copy subregion
  185. D3D11_BOX srcBox;
  186. srcBox.left = (UINT)srcOffset;
  187. srcBox.right = (UINT)srcOffset + length;
  188. srcBox.top = 0;
  189. srcBox.bottom = 1;
  190. srcBox.front = 0;
  191. srcBox.back = 1;
  192. mDevice.getImmediateContext()->CopySubresourceRegion(mD3DBuffer, 0, (UINT)dstOffset, 0, 0,
  193. static_cast<D3D11HardwareBuffer&>(srcBuffer).getD3DBuffer(), 0, &srcBox);
  194. if (mDevice.hasError())
  195. {
  196. String errorDescription = mDevice.getErrorDescription();
  197. CM_EXCEPT(RenderingAPIException, "Cannot copy D3D11 subresource region\nError Description:" + errorDescription);
  198. }
  199. }
  200. }
  201. void D3D11HardwareBuffer::readData(UINT32 offset, UINT32 length, void* pDest)
  202. {
  203. // There is no functional interface in D3D, just do via manual
  204. // lock, copy & unlock
  205. void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY);
  206. memcpy(pDest, pSrc, length);
  207. this->unlock();
  208. }
  209. void D3D11HardwareBuffer::writeData(UINT32 offset, UINT32 length,
  210. const void* pSource, bool discardWholeBuffer)
  211. {
  212. if(mDesc.Usage == D3D11_USAGE_DYNAMIC || mDesc.Usage == D3D11_USAGE_STAGING)
  213. {
  214. void* pDst = this->lock(offset, length,
  215. discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL);
  216. memcpy(pDst, pSource, length);
  217. this->unlock();
  218. }
  219. else if(mDesc.Usage == D3D11_USAGE_DEFAULT)
  220. {
  221. mDevice.getImmediateContext()->UpdateSubresource(mD3DBuffer, 0, nullptr, pSource, offset, length);
  222. }
  223. else
  224. {
  225. CM_EXCEPT(RenderingAPIException, "Trying to write into a buffer with unsupported usage: " + toString(mDesc.Usage));
  226. }
  227. }
  228. }