BsGpuParamBlockBuffer.h 3.7 KB

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