BsD3D11GpuBuffer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "BsD3D11GpuBuffer.h"
  2. #include "BsD3D11GpuBufferView.h"
  3. #include "BsD3D11RenderSystem.h"
  4. #include "BsD3D11HardwareBuffer.h"
  5. #include "BsD3D11Device.h"
  6. #include "BsD3D11Mappings.h"
  7. #include "BsRenderStats.h"
  8. #include "BsException.h"
  9. namespace BansheeEngine
  10. {
  11. D3D11GpuBuffer::D3D11GpuBuffer(UINT32 elementCount, UINT32 elementSize, GpuBufferType type, GpuBufferUsage usage, bool randomGpuWrite, bool useCounter)
  12. : GpuBuffer(elementCount, elementSize, type, usage, randomGpuWrite, useCounter), mBuffer(nullptr)
  13. { }
  14. D3D11GpuBuffer::~D3D11GpuBuffer()
  15. { }
  16. void D3D11GpuBuffer::initialize_internal()
  17. {
  18. D3D11HardwareBuffer::BufferType bufferType;
  19. D3D11RenderSystem* d3d11rs = static_cast<D3D11RenderSystem*>(D3D11RenderSystem::instancePtr());
  20. switch(mType)
  21. {
  22. case GBT_STRUCTURED:
  23. bufferType = D3D11HardwareBuffer::BT_STRUCTURED;
  24. break;
  25. case GBT_RAW:
  26. bufferType = D3D11HardwareBuffer::BT_RAW;
  27. break;
  28. case GBT_INDIRECTARGUMENT:
  29. bufferType = D3D11HardwareBuffer::BT_INDIRECTARGUMENT;
  30. break;
  31. case GBT_APPENDCONSUME:
  32. bufferType = D3D11HardwareBuffer::BT_APPENDCONSUME;
  33. break;
  34. default:
  35. BS_EXCEPT(InvalidParametersException, "Unsupported buffer type " + toString(mType));
  36. }
  37. mBuffer = bs_new<D3D11HardwareBuffer, PoolAlloc>(bufferType, mUsage, mElementCount, mElementSize,
  38. d3d11rs->getPrimaryDevice(), false, false, mRandomGpuWrite, mUseCounter);
  39. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_GpuBuffer);
  40. GpuBuffer::initialize_internal();
  41. }
  42. void D3D11GpuBuffer::destroy_internal()
  43. {
  44. bs_delete<PoolAlloc>(mBuffer);
  45. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_GpuBuffer);
  46. GpuBuffer::destroy_internal();
  47. }
  48. void* D3D11GpuBuffer::lock(UINT32 offset, UINT32 length, GpuLockOptions options)
  49. {
  50. #if BS_PROFILING_ENABLED
  51. if (options == GBL_READ_ONLY || options == GBL_READ_WRITE)
  52. {
  53. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_GpuBuffer);
  54. }
  55. if (options == GBL_READ_WRITE || options == GBL_WRITE_ONLY || options == GBL_WRITE_ONLY_DISCARD || options == GBL_WRITE_ONLY_NO_OVERWRITE)
  56. {
  57. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_GpuBuffer);
  58. }
  59. #endif
  60. return mBuffer->lock(offset, length, options);
  61. }
  62. void D3D11GpuBuffer::unlock()
  63. {
  64. mBuffer->unlock();
  65. }
  66. void D3D11GpuBuffer::readData(UINT32 offset, UINT32 length, void* pDest)
  67. {
  68. BS_INC_RENDER_STAT_CAT(ResRead, RenderStatObject_GpuBuffer);
  69. mBuffer->readData(offset, length, pDest);
  70. }
  71. void D3D11GpuBuffer::writeData(UINT32 offset, UINT32 length, const void* pSource, BufferWriteType writeFlags)
  72. {
  73. BS_INC_RENDER_STAT_CAT(ResWrite, RenderStatObject_GpuBuffer);
  74. mBuffer->writeData(offset, length, pSource, writeFlags);
  75. }
  76. void D3D11GpuBuffer::copyData(GpuBuffer& srcBuffer, UINT32 srcOffset,
  77. UINT32 dstOffset, UINT32 length, bool discardWholeBuffer)
  78. {
  79. D3D11GpuBuffer* d3d11SrcBuffer = static_cast<D3D11GpuBuffer*>(&srcBuffer);
  80. mBuffer->copyData(*d3d11SrcBuffer->mBuffer, srcOffset, dstOffset, length, discardWholeBuffer);
  81. }
  82. ID3D11Buffer* D3D11GpuBuffer::getDX11Buffer() const
  83. {
  84. return mBuffer->getD3DBuffer();
  85. }
  86. GpuBufferView* D3D11GpuBuffer::createView()
  87. {
  88. return bs_new<D3D11GpuBufferView, PoolAlloc>();
  89. }
  90. void D3D11GpuBuffer::destroyView(GpuBufferView* view)
  91. {
  92. if(view != nullptr)
  93. bs_delete<PoolAlloc>(view);
  94. }
  95. }