2
0

BsGpuBuffer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "CoreThread/BsCoreObject.h"
  6. #include "RenderAPI/BsHardwareBuffer.h"
  7. namespace bs
  8. {
  9. /** @addtogroup RenderAPI
  10. * @{
  11. */
  12. /** Descriptor structure used for initialization of a GpuBuffer. */
  13. struct GPU_BUFFER_DESC
  14. {
  15. /** Number of elements in the buffer. */
  16. UINT32 elementCount;
  17. /**
  18. * Size of each individual element in the buffer, in bytes. Only needed if using non-standard buffer. If using
  19. * standard buffers element size is calculated from format and this must be zero.
  20. */
  21. UINT32 elementSize;
  22. /** Type of the buffer. Determines how is buffer seen by the GPU program and in what ways can it be used. */
  23. GpuBufferType type;
  24. /** Format if the data in the buffer. Only relevant for standard buffers, must be BF_UNKNOWN otherwise. */
  25. GpuBufferFormat format;
  26. /** Usage that tells the hardware how will be buffer be used. */
  27. GpuBufferUsage usage = GBU_STATIC;
  28. /** When true allows the GPU to write to the resource. Must be enabled if buffer type is GBT_APPENDCONSUME. */
  29. bool randomGpuWrite = false;
  30. /**
  31. * When true binds a counter that can be used from a GPU program on the buffer. Can only be used in combination
  32. * with GBT_STRUCTURED and randomGpuWrite must be enabled.
  33. */
  34. bool useCounter = false;
  35. };
  36. /**
  37. * Information about a GpuBuffer. Allows core and non-core versions of GpuBuffer to share the same structure for
  38. * properties.
  39. */
  40. class BS_CORE_EXPORT GpuBufferProperties
  41. {
  42. public:
  43. GpuBufferProperties(const GPU_BUFFER_DESC& desc);
  44. /**
  45. * Returns the type of the GPU buffer. Type determines which kind of views (if any) can be created for the buffer,
  46. * and how is data read or modified in it.
  47. */
  48. GpuBufferType getType() const { return mDesc.type; }
  49. /** Returns format used by the buffer. Only relevant for standard buffers. */
  50. GpuBufferFormat getFormat() const { return mDesc.format; }
  51. /** Returns buffer usage which determines how are planning on updating the buffer contents. */
  52. GpuBufferUsage getUsage() const { return mDesc.usage; }
  53. /** Return whether the buffer supports random reads and writes within the GPU programs. */
  54. bool getRandomGpuWrite() const { return mDesc.randomGpuWrite; }
  55. /** Returns whether the buffer supports counter use within GPU programs. */
  56. bool getUseCounter() const { return mDesc.useCounter; }
  57. /** Returns number of elements in the buffer. */
  58. UINT32 getElementCount() const { return mDesc.elementCount; }
  59. /** Returns size of a single element in the buffer in bytes. */
  60. UINT32 getElementSize() const { return mDesc.elementSize; }
  61. protected:
  62. friend class GpuBuffer;
  63. GPU_BUFFER_DESC mDesc;
  64. };
  65. /**
  66. * Handles a generic GPU buffer that you may use for storing any kind of data you wish to be accessible to the GPU.
  67. * These buffers may be bounds to GPU program binding slots and accessed from a GPU program, or may be used by fixed
  68. * pipeline in some way.
  69. *
  70. * Buffer types:
  71. * - Raw buffers containing a block of bytes that are up to the GPU program to interpret.
  72. * - Structured buffer containing an array of structures compliant to a certain layout. Similar to raw buffer but
  73. * easier to interpret the data.
  74. * - Random read/write buffers that allow you to write to random parts of the buffer from within the GPU program, and
  75. * then read it later. These can only be bound to pixel and compute stages.
  76. * - Append/Consume buffers also allow you to write to them, but in a stack-like fashion, usually where one set of
  77. * programs produces data while other set consumes it from the same buffer. Append/Consume buffers are structured
  78. * by default.
  79. *
  80. * @note Sim thread only.
  81. */
  82. class BS_CORE_EXPORT GpuBuffer : public CoreObject
  83. {
  84. public:
  85. virtual ~GpuBuffer() { }
  86. /** Returns properties describing the buffer. */
  87. const GpuBufferProperties& getProperties() const { return mProperties; }
  88. /** Retrieves a core implementation of a GPU buffer usable only from the core thread. */
  89. SPtr<ct::GpuBuffer> getCore() const;
  90. /** Returns the size of a single element in the buffer, of the provided format, in bytes. */
  91. static UINT32 getFormatSize(GpuBufferFormat format);
  92. /** @copydoc HardwareBufferManager::createGpuBuffer */
  93. static SPtr<GpuBuffer> create(const GPU_BUFFER_DESC& desc);
  94. protected:
  95. friend class HardwareBufferManager;
  96. GpuBuffer(const GPU_BUFFER_DESC& desc);
  97. /** @copydoc CoreObject::createCore */
  98. SPtr<ct::CoreObject> createCore() const override;
  99. GpuBufferProperties mProperties;
  100. };
  101. /** @} */
  102. namespace ct
  103. {
  104. /** @addtogroup RenderAPI-Internal
  105. * @{
  106. */
  107. /**
  108. * Core thread version of a bs::GpuBuffer.
  109. *
  110. * @note Core thread only.
  111. */
  112. class BS_CORE_EXPORT GpuBuffer : public CoreObject, public HardwareBuffer
  113. {
  114. public:
  115. virtual ~GpuBuffer();
  116. /** Returns properties describing the buffer. */
  117. const GpuBufferProperties& getProperties() const { return mProperties; }
  118. /** @copydoc HardwareBufferManager::createGpuBuffer */
  119. static SPtr<GpuBuffer> create(const GPU_BUFFER_DESC& desc, GpuDeviceFlags deviceMask = GDF_DEFAULT);
  120. protected:
  121. GpuBuffer(const GPU_BUFFER_DESC& desc, UINT32 deviceMask);
  122. GpuBufferProperties mProperties;
  123. };
  124. /** @} */
  125. }
  126. }