BsGpuParams.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsGpuParam.h"
  6. #include "BsCoreObject.h"
  7. #include "BsIResourceListener.h"
  8. #include "BsVectorNI.h"
  9. #include "BsMatrixNxM.h"
  10. namespace BansheeEngine
  11. {
  12. /** @addtogroup Implementation
  13. * @{
  14. */
  15. /** @cond INTERNAL */
  16. /** Stores information needed for binding a texture to the pipeline. */
  17. struct BoundTextureInfo
  18. {
  19. BoundTextureInfo()
  20. :isLoadStore(false)
  21. { }
  22. bool isLoadStore;
  23. TextureSurface surface;
  24. };
  25. /** Helper structure whose specializations convert an engine data type into a GPU program data parameter type. */
  26. template<class T> struct TGpuDataParamInfo { };
  27. template<> struct TGpuDataParamInfo < float > { enum { TypeId = GPDT_FLOAT1 }; };
  28. template<> struct TGpuDataParamInfo < Vector2 > { enum { TypeId = GPDT_FLOAT2 }; };
  29. template<> struct TGpuDataParamInfo < Vector3 > { enum { TypeId = GPDT_FLOAT3 }; };
  30. template<> struct TGpuDataParamInfo < Vector4 > { enum { TypeId = GPDT_FLOAT4 }; };
  31. template<> struct TGpuDataParamInfo < int > { enum { TypeId = GPDT_INT1 }; };
  32. template<> struct TGpuDataParamInfo < Vector2I > { enum { TypeId = GPDT_INT2 }; };
  33. template<> struct TGpuDataParamInfo < Vector3I > { enum { TypeId = GPDT_INT3 }; };
  34. template<> struct TGpuDataParamInfo < Vector4I > { enum { TypeId = GPDT_INT4 }; };
  35. template<> struct TGpuDataParamInfo < Matrix2 > { enum { TypeId = GPDT_MATRIX_2X2 }; };
  36. template<> struct TGpuDataParamInfo < Matrix2x3 > { enum { TypeId = GPDT_MATRIX_2X3 }; };
  37. template<> struct TGpuDataParamInfo < Matrix2x4 > { enum { TypeId = GPDT_MATRIX_2X3 }; };
  38. template<> struct TGpuDataParamInfo < Matrix3 > { enum { TypeId = GPDT_MATRIX_3X3 }; };
  39. template<> struct TGpuDataParamInfo < Matrix3x2 > { enum { TypeId = GPDT_MATRIX_3X2 }; };
  40. template<> struct TGpuDataParamInfo < Matrix3x4 > { enum { TypeId = GPDT_MATRIX_3X4 }; };
  41. template<> struct TGpuDataParamInfo < Matrix4 > { enum { TypeId = GPDT_MATRIX_4X4 }; };
  42. template<> struct TGpuDataParamInfo < Matrix4x2 > { enum { TypeId = GPDT_MATRIX_4X2 }; };
  43. template<> struct TGpuDataParamInfo < Matrix4x3 > { enum { TypeId = GPDT_MATRIX_4X3 }; };
  44. template<> struct TGpuDataParamInfo < Color > { enum { TypeId = GPDT_COLOR }; };
  45. /** @endcond */
  46. /** Contains functionality common for both sim and core thread version of GpuParams. */
  47. class BS_CORE_EXPORT GpuParamsBase
  48. {
  49. public:
  50. /**
  51. * Creates new GpuParams object using the specified parameter descriptions.
  52. *
  53. * @param[in] paramDesc Reference to parameter descriptions that will be used for finding needed
  54. * parameters.
  55. * @param[in] transposeMatrices If true the stored matrices will be transposed before submitted to the GPU
  56. * (some APIs require different matrix layout).
  57. *
  58. * @note You normally do not want to call this manually. Instead use GpuProgram::createParameters.
  59. */
  60. GpuParamsBase(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  61. virtual ~GpuParamsBase();
  62. // Note: Disallow copy/assign because it would require some care when copying (copy internal data shared_ptr and
  63. // all the internal buffers too). Trivial to implement but not needed at this time. Un-delete and implement if necessary.
  64. GpuParamsBase(const GpuParamsBase& other) = delete;
  65. GpuParamsBase& operator=(const GpuParamsBase& rhs) = delete;
  66. /** Returns a description of all stored parameters. */
  67. const GpuParamDesc& getParamDesc() const { return *mParamDesc; }
  68. /**
  69. * Returns the size of a data parameter with the specified name, in bytes. Returns 0 if such parameter doesn't exist.
  70. */
  71. UINT32 getDataParamSize(const String& name) const;
  72. /** Checks if parameter with the specified name exists. */
  73. bool hasParam(const String& name) const;
  74. /** Checks if texture parameter with the specified name exists. */
  75. bool hasTexture(const String& name) const;
  76. /** Checks if sampler state parameter with the specified name exists. */
  77. bool hasSamplerState(const String& name) const;
  78. /** Checks if a parameter block with the specified name exists. */
  79. bool hasParamBlock(const String& name) const;
  80. /**
  81. * Checks is the texture at the specified slot to be bound as random load/store texture instead of a normal sampled
  82. * texture.
  83. */
  84. bool isLoadStoreTexture(UINT32 slot) const;
  85. /** Changes the type of the texture at the specified slot. */
  86. void setIsLoadStoreTexture(UINT32 slot, bool isLoadStore);
  87. /** Returns information that determines which texture surfaces to bind as load/store parameters. */
  88. const TextureSurface& getLoadStoreSurface(UINT32 slot) const;
  89. /** Sets information that determines which texture surfaces to bind as load/store parameters. */
  90. void setLoadStoreSurface(UINT32 slot, const TextureSurface& surface) const;
  91. /** Checks whether matrices should be transformed before being written to the parameter buffer. */
  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. /** Gets a descriptor for a data parameter with the specified name. */
  107. GpuParamDataDesc* getParamDesc(const String& name) const;
  108. GpuParamDescPtr mParamDesc;
  109. UINT32 mNumParamBlocks;
  110. UINT32 mNumTextures;
  111. UINT32 mNumSamplerStates;
  112. BoundTextureInfo* mTextureInfo;
  113. bool mTransposeMatrices;
  114. };
  115. /** Templated version of GpuParams that contains functionality for both sim and core thread versions of stored data. */
  116. template <bool Core>
  117. class BS_CORE_EXPORT TGpuParams : public GpuParamsBase
  118. {
  119. public:
  120. template<bool Core> struct TTypes { };
  121. template<> struct TTypes < false >
  122. {
  123. typedef GpuParams GpuParamsType;
  124. typedef HTexture TextureType;
  125. typedef SamplerStatePtr SamplerType;
  126. typedef SPtr<GpuParamBlockBuffer> ParamsBufferType;
  127. };
  128. template<> struct TTypes < true >
  129. {
  130. typedef GpuParamsCore GpuParamsType;
  131. typedef SPtr<TextureCore> TextureType;
  132. typedef SPtr<SamplerStateCore> SamplerType;
  133. typedef SPtr<GpuParamBlockBufferCore> ParamsBufferType;
  134. };
  135. typedef typename TTypes<Core>::GpuParamsType GpuParamsType;
  136. typedef typename TTypes<Core>::TextureType TextureType;
  137. typedef typename TTypes<Core>::SamplerType SamplerType;
  138. typedef typename TTypes<Core>::ParamsBufferType ParamsBufferType;
  139. /** @copydoc GpuParamsBase::GpuParamsBase(const GpuParamDescPtr&, bool) */
  140. TGpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  141. virtual ~TGpuParams();
  142. /**
  143. * Binds a new parameter buffer to the specified slot. Any following parameter reads or writes that are referencing
  144. * that buffer slot will use the new buffer.
  145. *
  146. * @note
  147. * This is useful if you want to share a parameter buffer among multiple GPU programs. You would only set the
  148. * values once and then share the buffer among all other GpuParams.
  149. * @note
  150. * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
  151. */
  152. void setParamBlockBuffer(UINT32 slot, const ParamsBufferType& paramBlockBuffer);
  153. /**
  154. * Replaces the parameter buffer with the specified name. Any following parameter reads or writes that are
  155. * referencing that buffer will use the new buffer.
  156. *
  157. * @note
  158. * This is useful if you want to share a parameter buffer among multiple GPU programs. You would only set the
  159. * values once and then share the buffer among all other GpuParams.
  160. * @note
  161. * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
  162. */
  163. void setParamBlockBuffer(const String& name, const ParamsBufferType& paramBlockBuffer);
  164. /**
  165. * Returns a handle for the parameter with the specified name. Handle may then be stored and used for quickly
  166. * setting or retrieving values to/from that parameter.
  167. *
  168. * Throws exception if parameter with that name and type doesn't exist.
  169. *
  170. * Parameter handles will be invalidated when their parent GpuParams object changes.
  171. */
  172. template<class T> void getParam(const String& name, TGpuDataParam<T, Core>& output) const;
  173. /** @copydoc getParam(const String&, TGpuDataParam<T, Core>&) */
  174. void getStructParam(const String& name, TGpuParamStruct<Core>& output) const;
  175. /**
  176. * @copydoc getParam(const String&, TGpuDataParam<T, Core>&)
  177. */
  178. void getTextureParam(const String& name, TGpuParamTexture<Core>& output) const;
  179. /** @copydoc getParam(const String&, TGpuDataParam<T, Core>&) */
  180. void getLoadStoreTextureParam(const String& name, TGpuParamLoadStoreTexture<Core>& output) const;
  181. /** @copydoc getParam(const String&, TGpuDataParam<T, Core>&) */
  182. void getSamplerStateParam(const String& name, TGpuParamSampState<Core>& output) const;
  183. /** Gets a parameter block buffer from the specified slot. */
  184. ParamsBufferType getParamBlockBuffer(UINT32 slot) const;
  185. /** Gets a texture bound to the specified slot. */
  186. TextureType getTexture(UINT32 slot);
  187. /** Gets a sampler state bound to the specified slot. */
  188. SamplerType getSamplerState(UINT32 slot);
  189. /** Sets a texture at the specified slot. */
  190. void setTexture(UINT32 slot, const TextureType& texture);
  191. /** Sets a sampler state at the specified slot. */
  192. void setSamplerState(UINT32 slot, const SamplerType& sampler);
  193. protected:
  194. /** @copydoc CoreObject::getThisPtr */
  195. virtual SPtr<GpuParamsType> _getThisPtr() const = 0;
  196. ParamsBufferType* mParamBlockBuffers;
  197. TextureType* mTextures;
  198. SamplerType* mSamplerStates;
  199. };
  200. /** @} */
  201. /** @addtogroup RenderAPI
  202. * @{
  203. */
  204. /** @cond INTERNAL */
  205. /**
  206. * @brief Core thread version of GpuParams.
  207. *
  208. * @note Core thread only.
  209. */
  210. class BS_CORE_EXPORT GpuParamsCore : public CoreObjectCore, public TGpuParams<true>
  211. {
  212. public:
  213. ~GpuParamsCore() { }
  214. /** Uploads all CPU stored parameter buffer data to the GPU buffers. */
  215. void updateHardwareBuffers();
  216. /** @copydoc GpuParamsBase::GpuParamsBase */
  217. static SPtr<GpuParamsCore> create(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  218. protected:
  219. friend class GpuParams;
  220. /** @copydoc GpuParamsBase::GpuParamsBase */
  221. GpuParamsCore(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  222. /** @copydoc CoreObject::getThisPtr */
  223. SPtr<GpuParamsCore> _getThisPtr() const override;
  224. /** @copydoc CoreObjectCore::syncToCore */
  225. void syncToCore(const CoreSyncData& data) override;
  226. };
  227. /** @endcond */
  228. /**
  229. * Contains descriptions for all parameters in a GPU program and also allows you to write and read those parameters.
  230. * All parameter values are stored internally on the CPU, and are only submitted to the GPU once the parameters are
  231. * bound to the pipeline.
  232. *
  233. * @note Sim thread only.
  234. */
  235. class BS_CORE_EXPORT GpuParams : public CoreObject, public TGpuParams<false>, public IResourceListener
  236. {
  237. public:
  238. ~GpuParams() { }
  239. /**
  240. * @copydoc CoreObject::markCoreDirty
  241. *
  242. * @note Internal method.
  243. */
  244. void _markCoreDirty() override;
  245. /**
  246. * @copydoc IResourceListener::markResourcesDirty
  247. *
  248. * @note Internal method.
  249. */
  250. void _markResourcesDirty() override;
  251. /** Retrieves a core implementation of a mesh usable only from the core thread. */
  252. SPtr<GpuParamsCore> getCore() const;
  253. /** @copydoc GpuParamsBase::GpuParamsBase */
  254. static SPtr<GpuParams> create(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  255. /** Contains a lookup table for sizes of all data parameters. Sizes are in bytes. */
  256. const static GpuDataParamInfos PARAM_SIZES;
  257. protected:
  258. /** @copydoc GpuParamsBase::GpuParamsBase */
  259. GpuParams(const GpuParamDescPtr& paramDesc, bool transposeMatrices);
  260. /** @copydoc CoreObject::getThisPtr */
  261. SPtr<GpuParams> _getThisPtr() const override;
  262. /** @copydoc CoreObject::createCore */
  263. SPtr<CoreObjectCore> createCore() const override;
  264. /** @copydoc CoreObject::syncToCore */
  265. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  266. /** @copydoc IResourceListener::getListenerResources */
  267. void getListenerResources(Vector<HResource>& resources) override;
  268. /** @copydoc IResourceListener::notifyResourceLoaded */
  269. void notifyResourceLoaded(const HResource& resource) override { markCoreDirty(); }
  270. /** @copydoc IResourceListener::notifyResourceChanged */
  271. void notifyResourceChanged(const HResource& resource) override { markCoreDirty(); }
  272. };
  273. /** @} */
  274. }