BsGpuParams.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/BsMatrixNxM.h"
  9. namespace bs
  10. {
  11. /** @addtogroup Implementation
  12. * @{
  13. */
  14. /** Helper structure whose specializations convert an engine data type into a GPU program data parameter type. */
  15. template<class T> struct TGpuDataParamInfo { };
  16. template<> struct TGpuDataParamInfo < float > { enum { TypeId = GPDT_FLOAT1 }; };
  17. template<> struct TGpuDataParamInfo < Vector2 > { enum { TypeId = GPDT_FLOAT2 }; };
  18. template<> struct TGpuDataParamInfo < Vector3 > { enum { TypeId = GPDT_FLOAT3 }; };
  19. template<> struct TGpuDataParamInfo < Vector4 > { enum { TypeId = GPDT_FLOAT4 }; };
  20. template<> struct TGpuDataParamInfo < int > { enum { TypeId = GPDT_INT1 }; };
  21. template<> struct TGpuDataParamInfo < Vector2I > { enum { TypeId = GPDT_INT2 }; };
  22. template<> struct TGpuDataParamInfo < Vector3I > { enum { TypeId = GPDT_INT3 }; };
  23. template<> struct TGpuDataParamInfo < Vector4I > { enum { TypeId = GPDT_INT4 }; };
  24. template<> struct TGpuDataParamInfo < Matrix2 > { enum { TypeId = GPDT_MATRIX_2X2 }; };
  25. template<> struct TGpuDataParamInfo < Matrix2x3 > { enum { TypeId = GPDT_MATRIX_2X3 }; };
  26. template<> struct TGpuDataParamInfo < Matrix2x4 > { enum { TypeId = GPDT_MATRIX_2X3 }; };
  27. template<> struct TGpuDataParamInfo < Matrix3 > { enum { TypeId = GPDT_MATRIX_3X3 }; };
  28. template<> struct TGpuDataParamInfo < Matrix3x2 > { enum { TypeId = GPDT_MATRIX_3X2 }; };
  29. template<> struct TGpuDataParamInfo < Matrix3x4 > { enum { TypeId = GPDT_MATRIX_3X4 }; };
  30. template<> struct TGpuDataParamInfo < Matrix4 > { enum { TypeId = GPDT_MATRIX_4X4 }; };
  31. template<> struct TGpuDataParamInfo < Matrix4x2 > { enum { TypeId = GPDT_MATRIX_4X2 }; };
  32. template<> struct TGpuDataParamInfo < Matrix4x3 > { enum { TypeId = GPDT_MATRIX_4X3 }; };
  33. template<> struct TGpuDataParamInfo < Color > { enum { TypeId = GPDT_COLOR }; };
  34. class GpuPipelineParamInfoBase;
  35. /** Contains functionality common for both sim and core thread version of GpuParams. */
  36. class BS_CORE_EXPORT GpuParamsBase
  37. {
  38. public:
  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. /** Returns a description of all stored parameters. */
  45. SPtr<GpuParamDesc> getParamDesc(GpuProgramType type) const;
  46. /** Gets the object that contains the processed information about all parameters. */
  47. SPtr<GpuPipelineParamInfoBase> getParamInfo() const { return mParamInfo; }
  48. /**
  49. * Returns the size of a data parameter with the specified name, in bytes. Returns 0 if such parameter doesn't exist.
  50. */
  51. UINT32 getDataParamSize(GpuProgramType type, const String& name) const;
  52. /** Checks if parameter with the specified name exists. */
  53. bool hasParam(GpuProgramType type, const String& name) const;
  54. /** Checks if texture parameter with the specified name exists. */
  55. bool hasTexture(GpuProgramType type, const String& name) const;
  56. /** Checks if load/store texture parameter with the specified name exists. */
  57. bool hasLoadStoreTexture(GpuProgramType type, const String& name) const;
  58. /** Checks if buffer parameter with the specified name exists. */
  59. bool hasBuffer(GpuProgramType type, const String& name) const;
  60. /** Checks if sampler state parameter with the specified name exists. */
  61. bool hasSamplerState(GpuProgramType type, const String& name) const;
  62. /** Checks if a parameter block with the specified name exists. */
  63. bool hasParamBlock(GpuProgramType type, const String& name) const;
  64. /** Gets a descriptor for a parameter block buffer with the specified name. */
  65. GpuParamBlockDesc* getParamBlockDesc(GpuProgramType type, const String& name) const;
  66. /** Marks the sim thread object as dirty, causing it to sync its contents with its core thread counterpart. */
  67. virtual void _markCoreDirty() { }
  68. /** @copydoc IResourceListener::markListenerResourcesDirty */
  69. virtual void _markResourcesDirty() { }
  70. protected:
  71. GpuParamsBase(const SPtr<GpuPipelineParamInfoBase>& paramInfo);
  72. /** Gets a descriptor for a data parameter with the specified name. */
  73. GpuParamDataDesc* getParamDesc(GpuProgramType type, const String& name) const;
  74. SPtr<GpuPipelineParamInfoBase> mParamInfo;
  75. };
  76. template<bool Core> struct TGpuParamsTypes { };
  77. template<> struct TGpuParamsTypes < false >
  78. {
  79. typedef GpuParams GpuParamsType;
  80. typedef HTexture TextureType;
  81. typedef SPtr<GpuBuffer> BufferType;
  82. typedef SPtr<SamplerState> SamplerType;
  83. typedef SPtr<GpuParamBlockBuffer> ParamsBufferType;
  84. };
  85. template<> struct TGpuParamsTypes < true >
  86. {
  87. typedef ct::GpuParams GpuParamsType;
  88. typedef SPtr<ct::Texture> TextureType;
  89. typedef SPtr<ct::GpuBuffer> BufferType;
  90. typedef SPtr<ct::SamplerState> SamplerType;
  91. typedef SPtr<ct::GpuParamBlockBuffer> ParamsBufferType;
  92. };
  93. /** Templated version of GpuParams that contains functionality for both sim and core thread versions of stored data. */
  94. template <bool Core>
  95. class BS_CORE_EXPORT TGpuParams : public GpuParamsBase
  96. {
  97. public:
  98. typedef typename TGpuParamsTypes<Core>::GpuParamsType GpuParamsType;
  99. typedef typename TGpuParamsTypes<Core>::TextureType TextureType;
  100. typedef typename TGpuParamsTypes<Core>::BufferType BufferType;
  101. typedef typename TGpuParamsTypes<Core>::SamplerType SamplerType;
  102. typedef typename TGpuParamsTypes<Core>::ParamsBufferType ParamsBufferType;
  103. virtual ~TGpuParams();
  104. /**
  105. * Returns a handle for the parameter with the specified name. Handle may then be stored and used for quickly
  106. * setting or retrieving values to/from that parameter.
  107. *
  108. * Throws exception if parameter with that name and type doesn't exist.
  109. *
  110. * Parameter handles will be invalidated when their parent GpuParams object changes.
  111. */
  112. template<class T> void getParam(GpuProgramType type, const String& name, TGpuDataParam<T, Core>& output) const;
  113. /** @copydoc getParam */
  114. void getStructParam(GpuProgramType type, const String& name, TGpuParamStruct<Core>& output) const;
  115. /** @copydoc getParam */
  116. void getTextureParam(GpuProgramType type, const String& name, TGpuParamTexture<Core>& output) const;
  117. /** @copydoc getParam */
  118. void getLoadStoreTextureParam(GpuProgramType type, const String& name, TGpuParamLoadStoreTexture<Core>& output) const;
  119. /** @copydoc getParam */
  120. void getBufferParam(GpuProgramType type, const String& name, TGpuParamBuffer<Core>& output) const;
  121. /** @copydoc getParam */
  122. void getSamplerStateParam(GpuProgramType type, const String& name, TGpuParamSampState<Core>& output) const;
  123. /** Gets a parameter block buffer from the specified set/slot combination. */
  124. ParamsBufferType getParamBlockBuffer(UINT32 set, UINT32 slot) const;
  125. /** Gets a texture bound to the specified set/slot combination. */
  126. TextureType getTexture(UINT32 set, UINT32 slot) const;
  127. /** Gets a load/store texture bound to the specified set/slot combination. */
  128. TextureType getLoadStoreTexture(UINT32 set, UINT32 slot) const;
  129. /** Gets a buffer bound to the specified set/slot combination. */
  130. BufferType getBuffer(UINT32 set, UINT32 slot) const;
  131. /** Gets a sampler state bound to the specified set/slot combination. */
  132. SamplerType getSamplerState(UINT32 set, UINT32 slot) const;
  133. /** Gets information that determines which texture surfaces to bind as a sampled texture parameter. */
  134. const TextureSurface& getTextureSurface(UINT32 set, UINT32 slot) const;
  135. /** Gets information that determines which texture surfaces to bind as a load/store parameter. */
  136. const TextureSurface& getLoadStoreSurface(UINT32 set, UINT32 slot) const;
  137. /**
  138. * Assigns the provided parameter block buffer to a buffer with the specified name, for the specified GPU program
  139. * stage. Any following parameter reads or writes that are referencing that buffer will use the new buffer.
  140. *
  141. * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
  142. */
  143. void setParamBlockBuffer(GpuProgramType type, const String& name, const ParamsBufferType& paramBlockBuffer);
  144. /**
  145. * Assigns the provided parameter block buffer to a buffer with the specified name, for any stages that reference
  146. * the buffer. Any following parameter reads or writes that are referencing that buffer will use the new buffer.
  147. *
  148. * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
  149. * It is up to the caller that all stages using this buffer name refer to the same buffer type.
  150. */
  151. void setParamBlockBuffer(const String& name, const ParamsBufferType& paramBlockBuffer);
  152. /**
  153. * Sets the parameter buffer with the specified set/slot combination.Any following parameter reads or writes that are
  154. * referencing that buffer will use the new buffer. Set/slot information for a specific buffer can be extracted
  155. * from GPUProgram's GpuParamDesc structure.
  156. *
  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. }