BsGpuParams.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 bs
  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. class GpuPipelineParamInfoBase;
  36. /** Contains functionality common for both sim and core thread version of GpuParams. */
  37. class BS_CORE_EXPORT GpuParamsBase
  38. {
  39. public:
  40. virtual ~GpuParamsBase();
  41. // Note: Disallow copy/assign because it would require some care when copying (copy internal data shared_ptr and
  42. // all the internal buffers too). Trivial to implement but not needed at this time. Un-delete and implement if necessary.
  43. GpuParamsBase(const GpuParamsBase& other) = delete;
  44. GpuParamsBase& operator=(const GpuParamsBase& rhs) = delete;
  45. /** Returns a description of all stored parameters. */
  46. SPtr<GpuParamDesc> getParamDesc(GpuProgramType type) const;
  47. /** Gets the object that contains the processed information about all parameters. */
  48. SPtr<GpuPipelineParamInfoBase> getParamInfo() const { return mParamInfo; }
  49. /**
  50. * Returns the size of a data parameter with the specified name, in bytes. Returns 0 if such parameter doesn't exist.
  51. */
  52. UINT32 getDataParamSize(GpuProgramType type, const String& name) const;
  53. /** Checks if parameter with the specified name exists. */
  54. bool hasParam(GpuProgramType type, const String& name) const;
  55. /** Checks if texture parameter with the specified name exists. */
  56. bool hasTexture(GpuProgramType type, const String& name) const;
  57. /** Checks if load/store texture parameter with the specified name exists. */
  58. bool hasLoadStoreTexture(GpuProgramType type, const String& name) const;
  59. /** Checks if buffer parameter with the specified name exists. */
  60. bool hasBuffer(GpuProgramType type, const String& name) const;
  61. /** Checks if sampler state parameter with the specified name exists. */
  62. bool hasSamplerState(GpuProgramType type, const String& name) const;
  63. /** Checks if a parameter block with the specified name exists. */
  64. bool hasParamBlock(GpuProgramType type, const String& name) const;
  65. /** Gets a descriptor for a parameter block buffer with the specified name. */
  66. GpuParamBlockDesc* getParamBlockDesc(GpuProgramType type, const String& name) const;
  67. /** Marks the sim thread object as dirty, causing it to sync its contents with its core thread counterpart. */
  68. virtual void _markCoreDirty() { }
  69. /** @copydoc IResourceListener::markListenerResourcesDirty */
  70. virtual void _markResourcesDirty() { }
  71. protected:
  72. GpuParamsBase(const SPtr<GpuPipelineParamInfoBase>& paramInfo);
  73. /** Gets a descriptor for a data parameter with the specified name. */
  74. GpuParamDataDesc* getParamDesc(GpuProgramType type, const String& name) const;
  75. SPtr<GpuPipelineParamInfoBase> mParamInfo;
  76. };
  77. template<bool Core> struct TGpuParamsTypes { };
  78. template<> struct TGpuParamsTypes < false >
  79. {
  80. typedef GpuParams GpuParamsType;
  81. typedef HTexture TextureType;
  82. typedef SPtr<GpuBuffer> BufferType;
  83. typedef SPtr<SamplerState> SamplerType;
  84. typedef SPtr<GpuParamBlockBuffer> ParamsBufferType;
  85. };
  86. template<> struct TGpuParamsTypes < true >
  87. {
  88. typedef GpuParamsCore GpuParamsType;
  89. typedef SPtr<TextureCore> TextureType;
  90. typedef SPtr<GpuBufferCore> BufferType;
  91. typedef SPtr<SamplerStateCore> SamplerType;
  92. typedef SPtr<GpuParamBlockBufferCore> ParamsBufferType;
  93. };
  94. /** Templated version of GpuParams that contains functionality for both sim and core thread versions of stored data. */
  95. template <bool Core>
  96. class BS_CORE_EXPORT TGpuParams : public GpuParamsBase
  97. {
  98. public:
  99. typedef typename TGpuParamsTypes<Core>::GpuParamsType GpuParamsType;
  100. typedef typename TGpuParamsTypes<Core>::TextureType TextureType;
  101. typedef typename TGpuParamsTypes<Core>::BufferType BufferType;
  102. typedef typename TGpuParamsTypes<Core>::SamplerType SamplerType;
  103. typedef typename TGpuParamsTypes<Core>::ParamsBufferType ParamsBufferType;
  104. virtual ~TGpuParams();
  105. /**
  106. * Returns a handle for the parameter with the specified name. Handle may then be stored and used for quickly
  107. * setting or retrieving values to/from that parameter.
  108. *
  109. * Throws exception if parameter with that name and type doesn't exist.
  110. *
  111. * Parameter handles will be invalidated when their parent GpuParams object changes.
  112. */
  113. template<class T> void getParam(GpuProgramType type, const String& name, TGpuDataParam<T, Core>& output) const;
  114. /** @copydoc getParam */
  115. void getStructParam(GpuProgramType type, const String& name, TGpuParamStruct<Core>& output) const;
  116. /** @copydoc getParam */
  117. void getTextureParam(GpuProgramType type, const String& name, TGpuParamTexture<Core>& output) const;
  118. /** @copydoc getParam */
  119. void getLoadStoreTextureParam(GpuProgramType type, const String& name, TGpuParamLoadStoreTexture<Core>& output) const;
  120. /** @copydoc getParam */
  121. void getBufferParam(GpuProgramType type, const String& name, TGpuParamBuffer<Core>& output) const;
  122. /** @copydoc getParam */
  123. void getSamplerStateParam(GpuProgramType type, const String& name, TGpuParamSampState<Core>& output) const;
  124. /** Gets a parameter block buffer from the specified set/slot combination. */
  125. ParamsBufferType getParamBlockBuffer(UINT32 set, UINT32 slot) const;
  126. /** Gets a texture bound to the specified set/slot combination. */
  127. TextureType getTexture(UINT32 set, UINT32 slot) const;
  128. /** Gets a load/store texture bound to the specified set/slot combination. */
  129. TextureType getLoadStoreTexture(UINT32 set, UINT32 slot) const;
  130. /** Gets a buffer bound to the specified set/slot combination. */
  131. BufferType getBuffer(UINT32 set, UINT32 slot) const;
  132. /** Gets a sampler state bound to the specified set/slot combination. */
  133. SamplerType getSamplerState(UINT32 set, UINT32 slot) const;
  134. /** Gets information that determines which texture surfaces to bind as load/store parameters. */
  135. const TextureSurface& getLoadStoreSurface(UINT32 set, UINT32 slot) const;
  136. /**
  137. * Sets the parameter buffer with the specified name. Any following parameter reads or writes that are
  138. * referencing that buffer will use the new buffer.
  139. *
  140. * @note
  141. * This is useful if you want to share a parameter buffer among multiple GPU programs. You would only set the
  142. * values once and then share the buffer among all other GpuParams.
  143. * @note
  144. * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
  145. */
  146. void setParamBlockBuffer(GpuProgramType type, const String& name, const ParamsBufferType& paramBlockBuffer);
  147. /**
  148. * Binds a new parameter buffer to the specified slot. Any following parameter reads or writes that are referencing
  149. * that buffer slot will use the new buffer.
  150. *
  151. * @note
  152. * This is useful if you want to share a parameter buffer among multiple GPU programs. You would only set the
  153. * values once and then share the buffer among all other GpuParams.
  154. * @note
  155. * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
  156. */
  157. virtual void setParamBlockBuffer(UINT32 set, UINT32 slot, const ParamsBufferType& paramBlockBuffer);
  158. /** Sets a texture at the specified set/slot combination. */
  159. virtual void setTexture(UINT32 set, UINT32 slot, const TextureType& texture);
  160. /** Sets a load/store texture at the specified set/slot combination. */
  161. virtual void setLoadStoreTexture(UINT32 set, UINT32 slot, const TextureType& texture, const TextureSurface& surface);
  162. /** Sets a buffer at the specified set/slot combination. */
  163. virtual void setBuffer(UINT32 set, UINT32 slot, const BufferType& buffer);
  164. /** Sets a sampler state at the specified set/slot combination. */
  165. virtual void setSamplerState(UINT32 set, UINT32 slot, const SamplerType& sampler);
  166. /** Sets information that determines which texture surfaces to bind as load/store parameters. */
  167. virtual void setLoadStoreSurface(UINT32 set, UINT32 slot, const TextureSurface& surface);
  168. protected:
  169. TGpuParams(const SPtr<GpuPipelineParamInfoBase>& paramInfo);
  170. /** @copydoc CoreObject::getThisPtr */
  171. virtual SPtr<GpuParamsType> _getThisPtr() const = 0;
  172. ParamsBufferType* mParamBlockBuffers = nullptr;
  173. TextureType* mTextures = nullptr;
  174. TextureType* mLoadStoreTextures = nullptr;
  175. TextureSurface* mLoadStoreSurfaces = nullptr;
  176. BufferType* mBuffers = nullptr;
  177. SamplerType* mSamplerStates = nullptr;
  178. };
  179. /** @} */
  180. /** @addtogroup RenderAPI-Internal
  181. * @{
  182. */
  183. /**
  184. * Core thread version of GpuParams.
  185. *
  186. * @note Core thread only.
  187. */
  188. class BS_CORE_EXPORT GpuParamsCore : public CoreObjectCore, public TGpuParams<true>
  189. {
  190. public:
  191. virtual ~GpuParamsCore() { }
  192. /**
  193. * @copydoc GpuParams::create(const SPtr<GraphicsPipelineState>&)
  194. * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
  195. */
  196. static SPtr<GpuParamsCore> create(const SPtr<GraphicsPipelineStateCore>& pipelineState,
  197. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  198. /**
  199. * @copydoc GpuParams::create(const SPtr<ComputePipelineState>&)
  200. * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
  201. */
  202. static SPtr<GpuParamsCore> create(const SPtr<ComputePipelineStateCore>& pipelineState,
  203. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  204. /**
  205. * @copydoc GpuParams::create(const SPtr<GpuPipelineParamInfo>&)
  206. * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
  207. */
  208. static SPtr<GpuParamsCore> create(const SPtr<GpuPipelineParamInfoCore>& paramInfo,
  209. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  210. protected:
  211. friend class GpuParams;
  212. friend class HardwareBufferCoreManager;
  213. GpuParamsCore(const SPtr<GpuPipelineParamInfoCore>& paramInfo, GpuDeviceFlags deviceMask);
  214. /** @copydoc CoreObject::getThisPtr */
  215. SPtr<GpuParamsCore> _getThisPtr() const override;
  216. /** @copydoc CoreObjectCore::syncToCore */
  217. void syncToCore(const CoreSyncData& data) override;
  218. };
  219. /** @} */
  220. /** @addtogroup RenderAPI
  221. * @{
  222. */
  223. /**
  224. * Contains descriptions for all parameters in a set of programs (ones for each stage) and allows you to write and read
  225. * those parameters. All parameter values are stored internally on the CPU, and are only submitted to the GPU once the
  226. * parameters are bound to the pipeline.
  227. *
  228. * @note Sim thread only.
  229. */
  230. class BS_CORE_EXPORT GpuParams : public CoreObject, public TGpuParams<false>, public IResourceListener
  231. {
  232. public:
  233. ~GpuParams() { }
  234. /** Retrieves a core implementation of a mesh usable only from the core thread. */
  235. SPtr<GpuParamsCore> getCore() const;
  236. /**
  237. * Creates new GpuParams object that can serve for changing the GPU program parameters on the specified pipeline.
  238. *
  239. * @param[in] pipelineState Pipeline state for which this object can set parameters for.
  240. * @return New GpuParams object.
  241. */
  242. static SPtr<GpuParams> create(const SPtr<GraphicsPipelineState>& pipelineState);
  243. /** @copydoc GpuParams::create(const SPtr<GraphicsPipelineState>&) */
  244. static SPtr<GpuParams> create(const SPtr<ComputePipelineState>& pipelineState);
  245. /**
  246. * Creates a new set of GPU parameters using an object describing the parameters for a pipeline.
  247. *
  248. * @param[in] paramInfo Description of GPU parameters for a specific GPU pipeline state.
  249. */
  250. static SPtr<GpuParams> create(const SPtr<GpuPipelineParamInfo>& paramInfo);
  251. /** Contains a lookup table for sizes of all data parameters. Sizes are in bytes. */
  252. const static GpuDataParamInfos PARAM_SIZES;
  253. /** @name Internal
  254. * @{
  255. */
  256. /** @copydoc GpuParamsBase::_markCoreDirty */
  257. void _markCoreDirty() override;
  258. /** @copydoc IResourceListener::markListenerResourcesDirty */
  259. void _markResourcesDirty() override;
  260. /** @} */
  261. protected:
  262. friend class HardwareBufferManager;
  263. GpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo);
  264. /** @copydoc CoreObject::getThisPtr */
  265. SPtr<GpuParams> _getThisPtr() const override;
  266. /** @copydoc CoreObject::createCore */
  267. SPtr<CoreObjectCore> createCore() const override;
  268. /** @copydoc CoreObject::syncToCore */
  269. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  270. /** @copydoc IResourceListener::getListenerResources */
  271. void getListenerResources(Vector<HResource>& resources) override;
  272. /** @copydoc IResourceListener::notifyResourceLoaded */
  273. void notifyResourceLoaded(const HResource& resource) override { markCoreDirty(); }
  274. /** @copydoc IResourceListener::notifyResourceChanged */
  275. void notifyResourceChanged(const HResource& resource) override { markCoreDirty(); }
  276. };
  277. /** @} */
  278. }