BsGpuParams.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "RenderAPI/BsGpuParam.h"
  6. #include "CoreThread/BsCoreObject.h"
  7. #include "Resources/BsIResourceListener.h"
  8. #include "Math/BsVectorNI.h"
  9. #include "Math/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 ct::GpuParams GpuParamsType;
  89. typedef SPtr<ct::Texture> TextureType;
  90. typedef SPtr<ct::GpuBuffer> BufferType;
  91. typedef SPtr<ct::SamplerState> SamplerType;
  92. typedef SPtr<ct::GpuParamBlockBuffer> 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 a sampled texture parameter. */
  135. const TextureSurface& getTextureSurface(UINT32 set, UINT32 slot) const;
  136. /** Gets information that determines which texture surfaces to bind as a load/store parameter. */
  137. const TextureSurface& getLoadStoreSurface(UINT32 set, UINT32 slot) const;
  138. /**
  139. * Sets the parameter buffer with the specified name. Any following parameter reads or writes that are
  140. * referencing that buffer will use the new buffer.
  141. *
  142. * @note
  143. * This is useful if you want to share a parameter buffer among multiple GPU programs. You would only set the
  144. * values once and then share the buffer among all other GpuParams.
  145. * @note
  146. * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
  147. */
  148. void setParamBlockBuffer(GpuProgramType type, const String& name, const ParamsBufferType& paramBlockBuffer);
  149. /**
  150. * Binds a new parameter buffer to the specified slot. Any following parameter reads or writes that are referencing
  151. * that buffer slot will use the new buffer.
  152. *
  153. * @note
  154. * This is useful if you want to share a parameter buffer among multiple GPU programs. You would only set the
  155. * values once and then share the buffer among all other GpuParams.
  156. * @note
  157. * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
  158. */
  159. virtual void setParamBlockBuffer(UINT32 set, UINT32 slot, const ParamsBufferType& paramBlockBuffer);
  160. /** Sets a texture at the specified set/slot combination. */
  161. virtual void setTexture(UINT32 set, UINT32 slot, const TextureType& texture,
  162. const TextureSurface& surface = TextureSurface::COMPLETE);
  163. /** Sets a load/store texture at the specified set/slot combination. */
  164. virtual void setLoadStoreTexture(UINT32 set, UINT32 slot, const TextureType& texture, const TextureSurface& surface);
  165. /** Sets a buffer at the specified set/slot combination. */
  166. virtual void setBuffer(UINT32 set, UINT32 slot, const BufferType& buffer);
  167. /** Sets a sampler state at the specified set/slot combination. */
  168. virtual void setSamplerState(UINT32 set, UINT32 slot, const SamplerType& sampler);
  169. /** Assigns a data value to the parameter with the specified name. */
  170. template<class T> void setParam(GpuProgramType type, const String& name, const T& value)
  171. { TGpuDataParam<T, Core> param; getParam(type, name, param); param.set(value); }
  172. /** Assigns a texture to the parameter with the specified name. */
  173. void setTexture(GpuProgramType type, const String& name, const TextureType& texture,
  174. const TextureSurface& surface = TextureSurface::COMPLETE)
  175. { TGpuParamTexture<Core> param; getTextureParam(type, name, param); param.set(texture, surface); }
  176. /** Assigns a load/store texture to the parameter with the specified name. */
  177. void setLoadStoreTexture(GpuProgramType type, const String& name, const TextureType& texture, const TextureSurface& surface)
  178. { TGpuParamLoadStoreTexture<Core> param; getLoadStoreTextureParam(type, name, param); param.set(texture, surface); }
  179. /** Assigns a buffer to the parameter with the specified name. */
  180. void setBuffer(GpuProgramType type, const String& name, const BufferType& buffer)
  181. { TGpuParamBuffer<Core> param; getBufferParam(type, name, param); param.set(buffer); }
  182. /** Assigns a sampler state to the parameter with the specified name. */
  183. void setSamplerState(GpuProgramType type, const String& name, const SamplerType& sampler)
  184. { TGpuParamSampState<Core> param; getSamplerStateParam(type, name, param); param.set(sampler); }
  185. protected:
  186. TGpuParams(const SPtr<GpuPipelineParamInfoBase>& paramInfo);
  187. /** @copydoc CoreObject::getThisPtr */
  188. virtual SPtr<GpuParamsType> _getThisPtr() const = 0;
  189. /** Data for a single bound texture. */
  190. struct TextureData
  191. {
  192. TextureType texture;
  193. TextureSurface surface;
  194. };
  195. ParamsBufferType* mParamBlockBuffers = nullptr;
  196. TextureData* mSampledTextureData = nullptr;
  197. TextureData* mLoadStoreTextureData = nullptr;
  198. BufferType* mBuffers = nullptr;
  199. SamplerType* mSamplerStates = nullptr;
  200. };
  201. /** @} */
  202. /** @addtogroup RenderAPI
  203. * @{
  204. */
  205. /**
  206. * Contains descriptions for all parameters in a set of programs (ones for each stage) and allows you to write and read
  207. * those parameters. All parameter values are stored internally on the CPU, and are only submitted to the GPU once the
  208. * parameters are bound to the pipeline.
  209. *
  210. * @note Sim thread only.
  211. */
  212. class BS_CORE_EXPORT GpuParams : public CoreObject, public TGpuParams<false>, public IResourceListener
  213. {
  214. public:
  215. ~GpuParams() { }
  216. /** Retrieves a core implementation of a mesh usable only from the core thread. */
  217. SPtr<ct::GpuParams> getCore() const;
  218. /**
  219. * Creates new GpuParams object that can serve for changing the GPU program parameters on the specified pipeline.
  220. *
  221. * @param[in] pipelineState Pipeline state for which this object can set parameters for.
  222. * @return New GpuParams object.
  223. */
  224. static SPtr<GpuParams> create(const SPtr<GraphicsPipelineState>& pipelineState);
  225. /** @copydoc GpuParams::create(const SPtr<GraphicsPipelineState>&) */
  226. static SPtr<GpuParams> create(const SPtr<ComputePipelineState>& pipelineState);
  227. /**
  228. * Creates a new set of GPU parameters using an object describing the parameters for a pipeline.
  229. *
  230. * @param[in] paramInfo Description of GPU parameters for a specific GPU pipeline state.
  231. */
  232. static SPtr<GpuParams> create(const SPtr<GpuPipelineParamInfo>& paramInfo);
  233. /** Contains a lookup table for sizes of all data parameters. Sizes are in bytes. */
  234. const static GpuDataParamInfos PARAM_SIZES;
  235. /** @name Internal
  236. * @{
  237. */
  238. /** @copydoc GpuParamsBase::_markCoreDirty */
  239. void _markCoreDirty() override;
  240. /** @copydoc IResourceListener::markListenerResourcesDirty */
  241. void _markResourcesDirty() override;
  242. /** @} */
  243. protected:
  244. friend class HardwareBufferManager;
  245. GpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo);
  246. /** @copydoc CoreObject::getThisPtr */
  247. SPtr<GpuParams> _getThisPtr() const override;
  248. /** @copydoc CoreObject::createCore */
  249. SPtr<ct::CoreObject> createCore() const override;
  250. /** @copydoc CoreObject::syncToCore */
  251. CoreSyncData syncToCore(FrameAlloc* allocator) override;
  252. /** @copydoc IResourceListener::getListenerResources */
  253. void getListenerResources(Vector<HResource>& resources) override;
  254. /** @copydoc IResourceListener::notifyResourceLoaded */
  255. void notifyResourceLoaded(const HResource& resource) override { markCoreDirty(); }
  256. /** @copydoc IResourceListener::notifyResourceChanged */
  257. void notifyResourceChanged(const HResource& resource) override { markCoreDirty(); }
  258. };
  259. /** @} */
  260. namespace ct
  261. {
  262. /** @addtogroup RenderAPI-Internal
  263. * @{
  264. */
  265. /**
  266. * Core thread version of bs::GpuParams.
  267. *
  268. * @note Core thread only.
  269. */
  270. class BS_CORE_EXPORT GpuParams : public CoreObject, public TGpuParams<true>
  271. {
  272. public:
  273. virtual ~GpuParams() { }
  274. /**
  275. * @copydoc bs::GpuParams::create(const SPtr<GraphicsPipelineState>&)
  276. * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
  277. */
  278. static SPtr<GpuParams> create(const SPtr<GraphicsPipelineState>& pipelineState,
  279. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  280. /**
  281. * @copydoc bs::GpuParams::create(const SPtr<ComputePipelineState>&)
  282. * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
  283. */
  284. static SPtr<GpuParams> create(const SPtr<ComputePipelineState>& pipelineState,
  285. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  286. /**
  287. * @copydoc bs::GpuParams::create(const SPtr<GpuPipelineParamInfo>&)
  288. * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
  289. */
  290. static SPtr<GpuParams> create(const SPtr<GpuPipelineParamInfo>& paramInfo,
  291. GpuDeviceFlags deviceMask = GDF_DEFAULT);
  292. protected:
  293. friend class bs::GpuParams;
  294. friend class HardwareBufferManager;
  295. GpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo, GpuDeviceFlags deviceMask);
  296. /** @copydoc CoreObject::getThisPtr */
  297. SPtr<GpuParams> _getThisPtr() const override;
  298. /** @copydoc CoreObject::syncToCore */
  299. void syncToCore(const CoreSyncData& data) override;
  300. };
  301. /** @} */
  302. }
  303. }