BsGpuParams.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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<Color>(const String& name, TGpuDataParam<Color>& output) const
  151. {
  152. auto iterFind = mParamDesc->params.find(name);
  153. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_FLOAT4)
  154. BS_EXCEPT(InvalidParametersException, "Cannot find color parameter with the name '" + name + "'");
  155. output = GpuParamColor(&iterFind->second, mInternalData);
  156. }
  157. /**
  158. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  159. */
  160. template<>
  161. void getParam<Matrix3>(const String& name, TGpuDataParam<Matrix3>& output) const
  162. {
  163. auto iterFind = mParamDesc->params.find(name);
  164. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_MATRIX_3X3)
  165. BS_EXCEPT(InvalidParametersException, "Cannot find matrix (3x3) parameter with the name '" + name + "'");
  166. output = GpuParamMat3(&iterFind->second, mInternalData);
  167. }
  168. /**
  169. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  170. */
  171. template<>
  172. void getParam<Matrix4>(const String& name, TGpuDataParam<Matrix4>& output) const
  173. {
  174. auto iterFind = mParamDesc->params.find(name);
  175. if (iterFind == mParamDesc->params.end() || iterFind->second.type != GPDT_MATRIX_4X4)
  176. BS_EXCEPT(InvalidParametersException, "Cannot find matrix (4x4) parameter with the name '" + name + "'");
  177. output = GpuParamMat4(&iterFind->second, mInternalData);
  178. }
  179. /**
  180. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  181. */
  182. void getStructParam(const String& name, GpuParamStruct& output) const;
  183. /**
  184. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  185. */
  186. void getTextureParam(const String& name, GpuParamTexture& output) const;
  187. /**
  188. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  189. */
  190. void getSamplerStateParam(const String& name, GpuParamSampState& output) const;
  191. /**
  192. * @brief Uploads all CPU stored parameter buffer data to the GPU buffers.
  193. *
  194. * @note Core thread only.
  195. */
  196. void updateHardwareBuffers();
  197. /**
  198. * @brief Gets a parameter block buffer from the specified slot.
  199. */
  200. GpuParamBlockBufferPtr getParamBlockBuffer(UINT32 slot) const;
  201. /**
  202. * @brief Gets a texture bound to the specified slot.
  203. */
  204. HTexture getTexture(UINT32 slot);
  205. /**
  206. * @brief Gets a sampler state bound to the specified slot.
  207. */
  208. HSamplerState getSamplerState(UINT32 slot);
  209. /**
  210. * @brief Updates all internal data from the provided copy. Copy must have the same
  211. * number of parameters as this object. Internal buffer contents will also be copied
  212. * and their sizes must match as well.
  213. */
  214. void _updateFromCopy(const GpuParamsPtr& copy);
  215. /**
  216. * @brief Returns an exact copy of this object. Internal parameter buffers will also be cloned.
  217. *
  218. * @param frameAlloc Optional frame allocator to allocate the returned data with. If not specified
  219. * allocation will be done using normal means.
  220. * @param onlyDirtyBlocks If true, only dirty param blocks will be cloned and the non-dirty ones will
  221. * be set to null.
  222. *
  223. * @note Internal method.
  224. */
  225. GpuParamsPtr _clone(FrameAlloc* frameAlloc = nullptr, bool onlyDirtyBlocks = false) const;
  226. /**
  227. * @brief Checks is the core dirty flag set. This is used by external systems
  228. * to know when internal data has changed and core proxy potentially needs to be updated.
  229. *
  230. * @note Internal method. Sim thread only.
  231. */
  232. bool _isCoreDirty() const;
  233. /**
  234. * @brief Marks the core dirty flag as clean.
  235. *
  236. * @note Internal method. Sim thread only.
  237. */
  238. void _markCoreClean();
  239. private:
  240. GpuParamDescPtr mParamDesc;
  241. std::shared_ptr<GpuParamsInternalData> mInternalData;
  242. /**
  243. * @brief Gets a descriptor for a data parameter with the specified name.
  244. */
  245. GpuParamDataDesc* getParamDesc(const String& name) const;
  246. /**
  247. * @brief Allocates and constructs internal buffers. Parameter counts must have been previously assigned.
  248. * If provided, internal data will be allocated using the frame allocator, otherwise using
  249. * the normal allocator.
  250. */
  251. void constructInternalBuffers(FrameAlloc* frameAlloc = nullptr);
  252. /**
  253. * @brief Marks the core data as dirty, signifying the core thread it should update it.
  254. */
  255. void markCoreDirty();
  256. };
  257. /**
  258. * @brief Structure used for storing GpuParams internal data.
  259. */
  260. struct GpuParamsInternalData
  261. {
  262. GpuParamsInternalData();
  263. UINT8* mData;
  264. UINT32 mNumParamBlocks;
  265. UINT32 mNumTextures;
  266. UINT32 mNumSamplerStates;
  267. GpuParamBlockPtr* mParamBlocks;
  268. GpuParamBlockBufferPtr* mParamBlockBuffers;
  269. HTexture* mTextures;
  270. HSamplerState* mSamplerStates;
  271. bool mTransposeMatrices;
  272. bool mIsDestroyed;
  273. UINT32 mCoreDirtyFlags;
  274. FrameAlloc* mFrameAlloc;
  275. };
  276. }