BsGpuParams.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsGpuParam.h"
  4. #include "BsCoreObject.h"
  5. #include "BsIResourceListener.h"
  6. #include "BsDebug.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Stores information needed for binding a texture to the pipeline.
  11. */
  12. struct BoundTextureInfo
  13. {
  14. BoundTextureInfo()
  15. :isLoadStore(false)
  16. { }
  17. bool isLoadStore;
  18. TextureSurface surface;
  19. };
  20. /**
  21. * @brief Contains functionality common for both sim and core thread
  22. * version of GpuParams.
  23. */
  24. class BS_CORE_EXPORT GpuParamsBase
  25. {
  26. public:
  27. /**
  28. * @brief Creates new GpuParams object using the specified parameter descriptions.
  29. *
  30. * @param paramDesc Reference to parameter descriptions that will be used for
  31. * finding needed parameters.
  32. * @param transposeMatrices If true the stored matrices will be transposed before
  33. * submitted to the GPU (some APIs require different
  34. * matrix layout).
  35. *
  36. * @note You normally do not want to call this manually. Instead use GpuProgram::createParameters.
  37. */
  38. GpuParamsBase(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  39. virtual ~GpuParamsBase();
  40. // Note: Disallow copy/assign because it would require some care when copying (copy internal data shared_ptr and
  41. // all the internal buffers too). Trivial to implement but not needed at this time. Un-delete and implement if necessary.
  42. GpuParamsBase(const GpuParamsBase& other) = delete;
  43. GpuParamsBase& operator=(const GpuParamsBase& rhs) = delete;
  44. /**
  45. * @brief Returns a description of all stored parameters.
  46. */
  47. const GpuParamDesc& getParamDesc() const { return *mParamDesc; }
  48. /**
  49. * @brief Returns the size of a data parameter with the specified name, in bytes.
  50. * Returns 0 if such parameter doesn't exist.
  51. */
  52. UINT32 getDataParamSize(const String& name) const;
  53. /**
  54. * @brief Checks if parameter with the specified name exists.
  55. */
  56. bool hasParam(const String& name) const;
  57. /**
  58. * @brief Checks if texture parameter with the specified name exists.
  59. */
  60. bool hasTexture(const String& name) const;
  61. /**
  62. * @brief Checks if sampler state parameter with the specified name exists.
  63. */
  64. bool hasSamplerState(const String& name) const;
  65. /**
  66. * @brief Checks if a parameter block with the specified name exists.
  67. */
  68. bool hasParamBlock(const String& name) const;
  69. /**
  70. * @brief Checks is the texture at the specified slot to be bound as
  71. * random load/store texture instead of a normal sampled texture.
  72. */
  73. bool isLoadStoreTexture(UINT32 slot) const;
  74. /**
  75. * @brief Changes the type of the texture at the specified slot.
  76. */
  77. void setIsLoadStoreTexture(UINT32 slot, bool isLoadStore);
  78. /**
  79. * @brief Returns information that determines which texture surfaces to bind
  80. * as load/store parameters.
  81. */
  82. const TextureSurface& getLoadStoreSurface(UINT32 slot) const;
  83. /**
  84. * @brief Sets information that determines which texture surfaces to bind
  85. * as load/store parameters.
  86. */
  87. void setLoadStoreSurface(UINT32 slot, const TextureSurface& surface) const;
  88. /**
  89. * @brief Checks whether matrices should be transformed before
  90. * being written to the parameter buffer.
  91. */
  92. bool getTransposeMatrices() const { return mTransposeMatrices; }
  93. /**
  94. * @copydoc CoreObject::markCoreDirty
  95. *
  96. * @note Internal method.
  97. */
  98. virtual void _markCoreDirty() { }
  99. /**
  100. * @copydoc IResourceListener::markResourcesDirty
  101. *
  102. * @note Internal method.
  103. */
  104. virtual void _markResourcesDirty() { }
  105. protected:
  106. /**
  107. * @brief Gets a descriptor for a data parameter with the specified name.
  108. */
  109. GpuParamDataDesc* getParamDesc(const String& name) const;
  110. GpuParamDescPtr mParamDesc;
  111. UINT32 mNumParamBlocks;
  112. UINT32 mNumTextures;
  113. UINT32 mNumSamplerStates;
  114. BoundTextureInfo* mTextureInfo;
  115. bool mTransposeMatrices;
  116. };
  117. /**
  118. * @brief Templated version of GpuParams that contains functionality for both
  119. * sim and core thread versions of stored data.
  120. */
  121. template <bool Core>
  122. class BS_CORE_EXPORT TGpuParams : public GpuParamsBase
  123. {
  124. public:
  125. template<bool Core> struct TTypes { };
  126. template<> struct TTypes < false >
  127. {
  128. typedef GpuParams GpuParamsType;
  129. typedef HTexture TextureType;
  130. typedef SamplerStatePtr SamplerType;
  131. typedef SPtr<GpuParamBlockBuffer> ParamsBufferType;
  132. };
  133. template<> struct TTypes < true >
  134. {
  135. typedef GpuParamsCore GpuParamsType;
  136. typedef SPtr<TextureCore> TextureType;
  137. typedef SPtr<SamplerStateCore> SamplerType;
  138. typedef SPtr<GpuParamBlockBufferCore> ParamsBufferType;
  139. };
  140. typedef typename TTypes<Core>::GpuParamsType GpuParamsType;
  141. typedef typename TTypes<Core>::TextureType TextureType;
  142. typedef typename TTypes<Core>::SamplerType SamplerType;
  143. typedef typename TTypes<Core>::ParamsBufferType ParamsBufferType;
  144. /**
  145. * @copydoc GpuParamsBase::GpuParamsBase(const GpuParamDescPtr&, bool)
  146. */
  147. TGpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  148. virtual ~TGpuParams();
  149. /**
  150. * @brief Binds a new parameter buffer to the specified slot. Any following parameter reads or
  151. * writes that are referencing that buffer slot will use the new buffer.
  152. *
  153. * @note This is useful if you want to share a parameter buffer among multiple GPU programs.
  154. * You would only set the values once and then share the buffer among all other GpuParams.
  155. *
  156. * It is up to the caller to guarantee the provided buffer matches parameter block
  157. * descriptor for this slot.
  158. */
  159. void setParamBlockBuffer(UINT32 slot, const ParamsBufferType& paramBlockBuffer);
  160. /**
  161. * @brief Replaces the parameter buffer with the specified name. Any following parameter reads or
  162. * writes that are referencing that buffer will use the new buffer.
  163. *
  164. * @note This is useful if you want to share a parameter buffer among multiple GPU programs.
  165. * You would only set the values once and then share the buffer among all other GpuParams.
  166. *
  167. * It is up to the caller to guarantee the provided buffer matches parameter block
  168. * descriptor for this slot.
  169. */
  170. void setParamBlockBuffer(const String& name, const ParamsBufferType& paramBlockBuffer);
  171. /**
  172. * @brief Returns a handle for the parameter with the specified name.
  173. * Handle may then be stored and used for quickly setting or retrieving
  174. * values to/from that parameter.
  175. *
  176. * Throws exception if parameter with that name and type doesn't exist.
  177. *
  178. * Parameter handles will be invalidated when their parent GpuParams object changes.
  179. */
  180. template<class T> void getParam(const String& name, TGpuDataParam<T, Core>& output) const;
  181. /**
  182. * @copydoc getParam(const String&, TGpuDataParam<T, Core>&)
  183. */
  184. void getStructParam(const String& name, TGpuParamStruct<Core>& output) const;
  185. /**
  186. * @copydoc getParam(const String&, TGpuDataParam<T, Core>&)
  187. */
  188. void getTextureParam(const String& name, TGpuParamTexture<Core>& output) const;
  189. /**
  190. * @copydoc getParam(const String&, TGpuDataParam<T, Core>&)
  191. */
  192. void getLoadStoreTextureParam(const String& name, TGpuParamLoadStoreTexture<Core>& output) const;
  193. /**
  194. * @copydoc getParam(const String&, TGpuDataParam<T, Core>&)
  195. */
  196. void getSamplerStateParam(const String& name, TGpuParamSampState<Core>& output) const;
  197. /**
  198. * @brief Gets a parameter block buffer from the specified slot.
  199. */
  200. ParamsBufferType getParamBlockBuffer(UINT32 slot) const;
  201. /**
  202. * @brief Gets a texture bound to the specified slot.
  203. */
  204. TextureType getTexture(UINT32 slot);
  205. /**
  206. * @brief Gets a sampler state bound to the specified slot.
  207. */
  208. SamplerType getSamplerState(UINT32 slot);
  209. /**
  210. * @brief Sets a texture at the specified slot.
  211. */
  212. void setTexture(UINT32 slot, const TextureType& texture);
  213. /**
  214. * @brief Sets a sampler state at the specified slot.
  215. */
  216. void setSamplerState(UINT32 slot, const SamplerType& sampler);
  217. protected:
  218. /**
  219. * @copydoc CoreObject::getThisPtr
  220. */
  221. virtual SPtr<GpuParamsType> _getThisPtr() const = 0;
  222. ParamsBufferType* mParamBlockBuffers;
  223. TextureType* mTextures;
  224. SamplerType* mSamplerStates;
  225. };
  226. /**
  227. * @brief Core thread version of GpuParams.
  228. *
  229. * @see GpuParams
  230. *
  231. * @note Core thread only.
  232. */
  233. class BS_CORE_EXPORT GpuParamsCore : public CoreObjectCore, public TGpuParams<true>
  234. {
  235. public:
  236. ~GpuParamsCore() { }
  237. /**
  238. * @brief Uploads all CPU stored parameter buffer data to the GPU buffers.
  239. */
  240. void updateHardwareBuffers();
  241. /**
  242. * @copydoc GpuParamsBase::GpuParamsBase
  243. */
  244. static SPtr<GpuParamsCore> create(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  245. protected:
  246. friend class GpuParams;
  247. /**
  248. * @copydoc GpuParamsBase::GpuParamsBase
  249. */
  250. GpuParamsCore(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  251. /**
  252. * @copydoc CoreObject::getThisPtr
  253. */
  254. SPtr<GpuParamsCore> _getThisPtr() const override;
  255. /**
  256. * @copydoc CoreObjectCore::syncToCore
  257. */
  258. void syncToCore(const CoreSyncData& data) override;
  259. };
  260. /**
  261. * @brief Contains descriptions for all parameters in a GPU program and also
  262. * allows you to write and read those parameters. All parameter values
  263. * are stored internally on the CPU, and are only submitted to the GPU
  264. * once the parameters are bound to the pipeline.
  265. *
  266. * @see CoreThreadAccessor::setConstantBuffers
  267. *
  268. * @note Sim thread only.
  269. */
  270. class BS_CORE_EXPORT GpuParams : public CoreObject, public TGpuParams<false>, public IResourceListener
  271. {
  272. public:
  273. ~GpuParams() { }
  274. /**
  275. * @copydoc CoreObject::markCoreDirty
  276. *
  277. * @note Internal method.
  278. */
  279. void _markCoreDirty() override;
  280. /**
  281. * @copydoc IResourceListener::markResourcesDirty
  282. *
  283. * @note Internal method.
  284. */
  285. void _markResourcesDirty() override;
  286. /**
  287. * @brief Retrieves a core implementation of a mesh usable only from the
  288. * core thread.
  289. */
  290. SPtr<GpuParamsCore> getCore() const;
  291. /**
  292. * @copydoc GpuParamsBase::GpuParamsBase
  293. */
  294. static SPtr<GpuParams> create(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  295. protected:
  296. /**
  297. * @copydoc GpuParamsBase::GpuParamsBase
  298. */
  299. GpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  300. /**
  301. * @copydoc CoreObject::getThisPtr
  302. */
  303. SPtr<GpuParams> _getThisPtr() const override;
  304. /**
  305. * @copydoc CoreObject::createCore
  306. */
  307. SPtr<CoreObjectCore> createCore() const override;
  308. /**
  309. * @copydoc CoreObject::syncToCore
  310. */
  311. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  312. /**
  313. * @copydoc IResourceListener::getListenerResources
  314. */
  315. void getListenerResources(Vector<HResource>& resources) override;
  316. /**
  317. * @copydoc IResourceListener::notifyResourceLoaded
  318. */
  319. void notifyResourceLoaded(const HResource& resource) override { markCoreDirty(); }
  320. /**
  321. * @copydoc IResourceListener::notifyResourceDestroyed
  322. */
  323. void notifyResourceDestroyed(const HResource& resource) override { markCoreDirty(); }
  324. /**
  325. * @copydoc IResourceListener::notifyResourceChanged
  326. */
  327. void notifyResourceChanged(const HResource& resource) override { markCoreDirty(); }
  328. };
  329. }