BsGpuParams.h 13 KB

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