BsGpuParamBlockBuffer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsCoreObject.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Represents a GPU parameter block buffer. Parameter block buffers
  8. * are bound to GPU programs which then fetch parameters from those buffers.
  9. *
  10. * Writing or reading from this buffer will translate directly to API calls
  11. * that update the GPU.
  12. *
  13. * @note Core thread only.
  14. */
  15. class BS_CORE_EXPORT GpuParamBlockBuffer : public CoreObject
  16. {
  17. public:
  18. GpuParamBlockBuffer();
  19. virtual ~GpuParamBlockBuffer();
  20. /**
  21. * @brief Initializes a buffer with the specified size in bytes and usage.
  22. * Specify dynamic usage if you plan on modifying the buffer often,
  23. * otherwise specify static usage.
  24. *
  25. * @see CoreObject::initialize
  26. *
  27. * @note Must be called right after construction.
  28. */
  29. void initialize(UINT32 size, GpuParamBlockUsage usage);
  30. /**
  31. * @brief Writes all of the specified data to the buffer.
  32. * Data size must be the same size as the buffer;
  33. */
  34. virtual void writeData(const UINT8* data) = 0;
  35. /**
  36. * @brief Copies data from the internal buffer to a pre-allocated array.
  37. * Be aware this generally isn't a very fast operation as reading
  38. * from the GPU will most definitely involve a CPU-GPU sync point.
  39. *
  40. * @param [in,out] data Array where the data will be written to. Must be of
  41. * "getSize()" bytes.
  42. */
  43. virtual void readData(UINT8* data) const = 0;
  44. /**
  45. * @brief Returns the size of the buffer in bytes.
  46. */
  47. UINT32 getSize() const { return mSize; }
  48. /**
  49. * @brief Returns a parameter block buffer which is used for caching
  50. * the parameter information on the sim thread. Essentially a
  51. * sim thread copy of the GPU buffer.
  52. *
  53. * @note Sim thread only.
  54. */
  55. GpuParamBlockPtr getParamBlock() const { return mParamBlock; }
  56. /**
  57. * @brief Returns a parameter block buffer which is used for caching
  58. * the parameter information on the core thread. Essentially a CPU
  59. * core thread copy of the GPU buffer.
  60. *
  61. * @note Core thread only.
  62. */
  63. // TODO - Keeping a core param block along with a sim param block is redundant in some cases,
  64. // as it might not be used (e.g. if gpu param block buffer is only accessed from core thread)
  65. GpuParamBlockPtr getCoreParamBlock() const { return mCoreParamBlock; }
  66. /**
  67. * @brief Sets a reference to a core thread CPU buffer that may be used for buffering
  68. * parameter data on the core thread, before actually writing it to the buffer.
  69. */
  70. void setCoreParamBlock(const GpuParamBlockPtr& paramBlock) { mCoreParamBlock = paramBlock; }
  71. protected:
  72. GpuParamBlockUsage mUsage;
  73. UINT32 mSize;
  74. GpuParamBlockPtr mParamBlock;
  75. GpuParamBlockPtr mCoreParamBlock;
  76. };
  77. /**
  78. * @brief Implementation of a GpuParamBlock buffer that doesn't use a GPU buffer
  79. * for storage. Used with APIs that do not support GPU parameter buffers.
  80. */
  81. class BS_CORE_EXPORT GenericGpuParamBlockBuffer : public GpuParamBlockBuffer
  82. {
  83. public:
  84. GenericGpuParamBlockBuffer();
  85. /**
  86. * @copydoc GpuParamBlockBuffer::writeData
  87. */
  88. void writeData(const UINT8* data);
  89. /**
  90. * @copydoc GpuParamBlockBuffer::readData.
  91. */
  92. void readData(UINT8* data) const;
  93. protected:
  94. UINT8* mData;
  95. /**
  96. * @copydoc CoreObject::initialize_internal.
  97. */
  98. virtual void initialize_internal();
  99. /**
  100. * @copydoc CoreObject::destroy_internal.
  101. */
  102. virtual void destroy_internal();
  103. };
  104. }