BsGpuParamBlockBuffer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 CPU. Essentially a CPU
  51. * copy of the GPU buffer.
  52. *
  53. * @note Sim thread only.
  54. */
  55. GpuParamBlockPtr getParamBlock() const { return mParamBlock; }
  56. // TODO UNDOCUMENTED
  57. // TODO - Keeping a core param block along with a sim param block is redundant in some cases,
  58. // as it might not be used (e.g. if gpu param block buffer is only accessed from core thread)
  59. GpuParamBlockPtr getCoreParamBlock() const { return mCoreParamBlock; }
  60. void setCoreParamBlock(const GpuParamBlockPtr& paramBlock) { mCoreParamBlock = paramBlock; }
  61. protected:
  62. GpuParamBlockUsage mUsage;
  63. UINT32 mSize;
  64. GpuParamBlockPtr mParamBlock;
  65. GpuParamBlockPtr mCoreParamBlock;
  66. };
  67. /**
  68. * @brief Implementation of a GpuParamBlock buffer that doesn't use a GPU buffer
  69. * for storage. Used with APIs that do not support GPU parameter buffers.
  70. */
  71. class BS_CORE_EXPORT GenericGpuParamBlockBuffer : public GpuParamBlockBuffer
  72. {
  73. public:
  74. GenericGpuParamBlockBuffer();
  75. /**
  76. * @copydoc GpuParamBlockBuffer::writeData
  77. */
  78. void writeData(const UINT8* data);
  79. /**
  80. * @copydoc GpuParamBlockBuffer::readData.
  81. */
  82. void readData(UINT8* data) const;
  83. protected:
  84. UINT8* mData;
  85. /**
  86. * @copydoc CoreObject::initialize_internal.
  87. */
  88. virtual void initialize_internal();
  89. /**
  90. * @copydoc CoreObject::destroy_internal.
  91. */
  92. virtual void destroy_internal();
  93. };
  94. }