BsD3D11GpuBufferView.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsD3D11Prerequisites.h"
  5. namespace bs { namespace ct
  6. {
  7. /** @addtogroup D3D11
  8. * @{
  9. */
  10. /**
  11. * Descriptor structure used for initializing a GpuBufferView.
  12. *
  13. * @see GpuBuffer
  14. */
  15. struct BS_CORE_EXPORT GPU_BUFFER_VIEW_DESC
  16. {
  17. UINT32 firstElement;
  18. UINT32 elementWidth;
  19. UINT32 numElements;
  20. bool useCounter;
  21. GpuViewUsage usage;
  22. GpuBufferFormat format;
  23. };
  24. /**
  25. * Represents a specific view of a GpuBuffer. Different views all of the same buffer be used in different situations
  26. * (for example for reading from a shader, or for a unordered read/write operation).
  27. */
  28. class GpuBufferView
  29. {
  30. public:
  31. class HashFunction
  32. {
  33. public:
  34. size_t operator()(const GPU_BUFFER_VIEW_DESC& key) const;
  35. };
  36. class EqualFunction
  37. {
  38. public:
  39. bool operator()(const GPU_BUFFER_VIEW_DESC& a, const GPU_BUFFER_VIEW_DESC& b) const;
  40. };
  41. GpuBufferView();
  42. virtual ~GpuBufferView();
  43. /**
  44. * Initializes a new buffer view for the specified buffer. Descriptor structure defines which portion of the buffer,
  45. * and how will its contents be represented by the view.
  46. */
  47. void initialize(const SPtr<D3D11GpuBuffer>& buffer, GPU_BUFFER_VIEW_DESC& desc);
  48. /** Returns a descriptor structure used for creating the view. */
  49. const GPU_BUFFER_VIEW_DESC& getDesc() const { return mDesc; }
  50. /** Returns the buffer this view was created for. */
  51. SPtr<D3D11GpuBuffer> getBuffer() const { return mBuffer; }
  52. /** Returns index of first element in the buffer that this view provides access to. */
  53. UINT32 getFirstElement() const { return mDesc.firstElement; }
  54. /** Returns width of an element in the buffer, in bytes. */
  55. UINT32 getElementWidth() const { return mDesc.elementWidth; }
  56. /** Returns the total number of elements this buffer provides access to. */
  57. UINT32 getNumElements() const { return mDesc.numElements; }
  58. /** Returns true if this view allows a GPU program to use counters on the bound buffer. */
  59. bool getUseCounter() const { return mDesc.useCounter; }
  60. /** Returns view usage which determines where in the pipeline can the view be bound. */
  61. GpuViewUsage getUsage() const { return mDesc.usage; }
  62. /** Returns the DX11 shader resource view object for the buffer. */
  63. ID3D11ShaderResourceView* getSRV() const { return mSRV; }
  64. /** Returns the DX11 unordered access view object for the buffer. */
  65. ID3D11UnorderedAccessView* getUAV() const { return mUAV; }
  66. private:
  67. /**
  68. * Creates a DX11 shader resource view that allows a buffer to be bound to a shader for reading (the most common
  69. * option).
  70. *
  71. * @param[in] buffer Buffer to create the view for.
  72. * @param[in] firstElement Index of the first element the view manages. Interpretation of this value depends on
  73. * exact buffer type. It may be byte offset for raw buffers, or number of elements for
  74. * structured buffers.
  75. * @param[in] elementWidth Width of a single element in the buffer. Size of the structure in structured buffers
  76. * and ignored for raw buffers as they always operate on single byte basis.
  77. * @param[in] numElements Number of elements the view manages, starting after the "firstElement". This means
  78. * number of bytes for raw buffers or number of structures for structured buffers.
  79. * @return Constructed DX11 shader resource view object.
  80. */
  81. ID3D11ShaderResourceView* createSRV(D3D11GpuBuffer* buffer, UINT32 firstElement, UINT32 elementWidth, UINT32 numElements);
  82. /**
  83. * Creates a DX11 unordered access view that allows a buffer to be bound to a shader for random reading or writing.
  84. *
  85. * @param[in] buffer Buffer to create the view for.
  86. * @param[in] firstElement Index of the first element the view manages. Interpretation of this value depends on
  87. * exact buffer type. It may be byte offset for raw buffers, or number of elements for
  88. * structured buffers.
  89. * @param[in] numElements Number of elements the view manages, starting after the @p firstElement. This means
  90. * number of bytes for raw buffers or number of structures for structured buffers.
  91. * @return Constructed DX11 unordered access view object.
  92. */
  93. ID3D11UnorderedAccessView* createUAV(D3D11GpuBuffer* buffer, UINT32 firstElement, UINT32 numElements, bool useCounter);
  94. ID3D11ShaderResourceView* mSRV;
  95. ID3D11UnorderedAccessView* mUAV;
  96. GPU_BUFFER_VIEW_DESC mDesc;
  97. SPtr<D3D11GpuBuffer> mBuffer;
  98. };
  99. /** @} */
  100. }}