BsGpuParams.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGpuParam.h"
  4. namespace BansheeEngine
  5. {
  6. struct GpuParamsInternalData;
  7. /**
  8. * @brief Contains descriptions for all parameters in a GPU program and also
  9. * allows you to write and read those parameters. All parameter values
  10. * are stored internally on the CPU, and are only submitted to the GPU
  11. * once the parameters are bound to the pipeline.
  12. *
  13. * @see CoreThreadAccessor::bindGpuParams
  14. *
  15. * @note Sim thread only.
  16. */
  17. class BS_CORE_EXPORT GpuParams
  18. {
  19. struct PrivatelyConstruct {};
  20. public:
  21. /**
  22. * @brief Creates new GpuParams object using the specified parameter descriptions.
  23. *
  24. * @param paramDesc Reference to parameter descriptions that will be used for
  25. * finding needed parameters.
  26. * @param transposeMatrices If true the stored matrices will be transposed before
  27. * submitted to the GPU (some APIs require different
  28. * matrix layout).
  29. *
  30. * @note You normally do not want to call this manually. Instead use GpuProgram::createParameters.
  31. */
  32. GpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  33. /**
  34. * @brief Private constructor for internal use. Performs no initialization.
  35. */
  36. GpuParams(const GpuParamDescPtr& paramDesc, PrivatelyConstruct& dummy);
  37. ~GpuParams();
  38. // Note: Disallow copy/assign because it would require some care when copying (copy internal data shared_ptr and
  39. // all the internal buffers too). Trivial to implement but not needed at this time. Un-delete and implement if necessary.
  40. GpuParams(const GpuParams& other) = delete;
  41. GpuParams& operator=(const GpuParams& rhs) = delete;
  42. /**
  43. * @brief Binds a new parameter buffer to the specified slot. Any following parameter reads or
  44. * writes that are referencing that buffer slot will use the new buffer.
  45. *
  46. * @note This is useful if you want to share a parameter buffer among multiple GPU programs.
  47. * You would only set the values once and then share the buffer among all other GpuParams.
  48. *
  49. * It is up to the caller to guarantee the provided buffer matches parameter block
  50. * descriptor for this slot.
  51. */
  52. void setParamBlockBuffer(UINT32 slot, const GpuParamBlockBufferPtr& paramBlockBuffer);
  53. /**
  54. * @brief Replaces the parameter buffer with the specified name. Any following parameter reads or
  55. * writes that are referencing that buffer will use the new buffer.
  56. *
  57. * @note This is useful if you want to share a parameter buffer among multiple GPU programs.
  58. * You would only set the values once and then share the buffer among all other GpuParams.
  59. *
  60. * It is up to the caller to guarantee the provided buffer matches parameter block
  61. * descriptor for this slot.
  62. */
  63. void setParamBlockBuffer(const String& name, const GpuParamBlockBufferPtr& paramBlockBuffer);
  64. /**
  65. * @brief Returns a description of all stored parameters.
  66. */
  67. const GpuParamDesc& getParamDesc() const { return *mParamDesc; }
  68. /**
  69. * @brief Returns the size of a data parameter with the specified name, in bytes.
  70. * Returns 0 if such parameter doesn't exist.
  71. */
  72. UINT32 getDataParamSize(const String& name) const;
  73. /**
  74. * @brief Checks if parameter with the specified name exists.
  75. */
  76. bool hasParam(const String& name) const;
  77. /**
  78. * @brief Checks if texture parameter with the specified name exists.
  79. */
  80. bool hasTexture(const String& name) const;
  81. /**
  82. * @brief Checks if sampler state parameter with the specified name exists.
  83. */
  84. bool hasSamplerState(const String& name) const;
  85. /**
  86. * @brief Checks if a parameter block with the specified name exists.
  87. */
  88. bool hasParamBlock(const String& name) const;
  89. /**
  90. * @brief Returns a handle for the parameter with the specified name.
  91. * Handle may then be stored and used for quickly setting or retrieving
  92. * values to/from that parameter.
  93. *
  94. * Throws exception if parameter with that name and type doesn't exist.
  95. *
  96. * Parameter handles will be invalidated when their parent GpuParams object changes.
  97. */
  98. template<class T> void getParam(const String& name, TGpuDataParam<T>& output) const
  99. {
  100. BS_EXCEPT(InvalidParametersException, "Unsupported parameter type");
  101. }
  102. /**
  103. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  104. */
  105. template<>
  106. void getParam<float>(const String& name, TGpuDataParam<float>& output) const
  107. {
  108. auto iterFind = mParamDesc->params.find(name);
  109. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_FLOAT1)
  110. BS_EXCEPT(InvalidParametersException, "Cannot find float parameter with the name '" + name + "'");
  111. output = GpuParamFloat(&iterFind->second, mInternalData);
  112. }
  113. /**
  114. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  115. */
  116. template<>
  117. void getParam<Vector2>(const String& name, TGpuDataParam<Vector2>& output) const
  118. {
  119. auto iterFind = mParamDesc->params.find(name);
  120. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_FLOAT2)
  121. BS_EXCEPT(InvalidParametersException, "Cannot find vector (2) parameter with the name '" + name + "'");
  122. output = GpuParamVec2(&iterFind->second, mInternalData);
  123. }
  124. /**
  125. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  126. */
  127. template<>
  128. void getParam<Vector3>(const String& name, TGpuDataParam<Vector3>& output) const
  129. {
  130. auto iterFind = mParamDesc->params.find(name);
  131. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_FLOAT3)
  132. BS_EXCEPT(InvalidParametersException, "Cannot find vector (3) parameter with the name '" + name + "'");
  133. output = GpuParamVec3(&iterFind->second, mInternalData);
  134. }
  135. /**
  136. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  137. */
  138. template<>
  139. void getParam<Vector4>(const String& name, TGpuDataParam<Vector4>& output) const
  140. {
  141. auto iterFind = mParamDesc->params.find(name);
  142. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_FLOAT4)
  143. BS_EXCEPT(InvalidParametersException, "Cannot find vector (4) parameter with the name '" + name + "'");
  144. output = GpuParamVec4(&iterFind->second, mInternalData);
  145. }
  146. /**
  147. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  148. */
  149. template<>
  150. void getParam<Matrix3>(const String& name, TGpuDataParam<Matrix3>& output) const
  151. {
  152. auto iterFind = mParamDesc->params.find(name);
  153. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_MATRIX_3X3)
  154. BS_EXCEPT(InvalidParametersException, "Cannot find matrix (3x3) parameter with the name '" + name + "'");
  155. output = GpuParamMat3(&iterFind->second, mInternalData);
  156. }
  157. /**
  158. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  159. */
  160. template<>
  161. void getParam<Matrix4>(const String& name, TGpuDataParam<Matrix4>& output) const
  162. {
  163. auto iterFind = mParamDesc->params.find(name);
  164. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_MATRIX_4X4)
  165. BS_EXCEPT(InvalidParametersException, "Cannot find matrix (4x4) parameter with the name '" + name + "'");
  166. output = GpuParamMat4(&iterFind->second, mInternalData);
  167. }
  168. /**
  169. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  170. */
  171. void getStructParam(const String& name, GpuParamStruct& output) const;
  172. /**
  173. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  174. */
  175. void getTextureParam(const String& name, GpuParamTexture& output) const;
  176. /**
  177. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  178. */
  179. void getSamplerStateParam(const String& name, GpuParamSampState& output) const;
  180. /**
  181. * @brief Uploads all CPU stored parameter buffer data to the GPU buffers.
  182. *
  183. * @note Core thread only.
  184. */
  185. void updateHardwareBuffers();
  186. /**
  187. * @brief Gets a parameter block buffer from the specified slot.
  188. */
  189. GpuParamBlockBufferPtr getParamBlockBuffer(UINT32 slot) const;
  190. /**
  191. * @brief Gets a texture bound to the specified slot.
  192. */
  193. HTexture getTexture(UINT32 slot);
  194. /**
  195. * @brief Gets a sampler state bound to the specified slot.
  196. */
  197. HSamplerState getSamplerState(UINT32 slot);
  198. /**
  199. * @brief Returns an exact copy of this object. Internal parameter buffers will also be cloned,
  200. * but should only be used on the core thread.
  201. *
  202. * @note Optional frame allocator to allocate the returned data with. If not specified
  203. * allocation will be done using normal means.
  204. * Internal method.
  205. */
  206. GpuParamsPtr _cloneForCore(FrameAlloc* frameAlloc = nullptr) const;
  207. /**
  208. * @brief Checks is the core dirty flag set. This is used by external systems
  209. * to know when internal data has changed and core proxy potentially needs to be updated.
  210. *
  211. * @note Internal method. Sim thread only.
  212. */
  213. bool _isCoreDirty() const;
  214. /**
  215. * @brief Marks the core dirty flag as clean.
  216. *
  217. * @note Internal method. Sim thread only.
  218. */
  219. void _markCoreClean();
  220. private:
  221. GpuParamDescPtr mParamDesc;
  222. std::shared_ptr<GpuParamsInternalData> mInternalData;
  223. /**
  224. * @brief Gets a descriptor for a data parameter with the specified name.
  225. */
  226. GpuParamDataDesc* getParamDesc(const String& name) const;
  227. /**
  228. * @brief Allocates and constructs internal buffers. Parameter counts must have been previously assigned.
  229. * If provided, internal data will be allocated using the frame allocator, otherwise using
  230. * the normal allocator.
  231. */
  232. void constructInternalBuffers(FrameAlloc* frameAlloc = nullptr);
  233. /**
  234. * @brief Marks the core data as dirty, signifying the core thread it should update it.
  235. */
  236. void markCoreDirty();
  237. };
  238. /**
  239. * @brief Structure used for storing GpuParams internal data.
  240. */
  241. struct GpuParamsInternalData
  242. {
  243. GpuParamsInternalData();
  244. UINT8* mData;
  245. UINT32 mNumParamBlocks;
  246. UINT32 mNumTextures;
  247. UINT32 mNumSamplerStates;
  248. GpuParamBlockPtr* mParamBlocks;
  249. GpuParamBlockBufferPtr* mParamBlockBuffers;
  250. HTexture* mTextures;
  251. HSamplerState* mSamplerStates;
  252. bool mTransposeMatrices;
  253. bool mIsDestroyed;
  254. UINT32 mCoreDirtyFlags;
  255. FrameAlloc* mFrameAlloc;
  256. };
  257. }