BsGpuParamsSet.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "Material/BsMaterial.h"
  6. #include "Material/BsShader.h"
  7. #include "Material/BsPass.h"
  8. namespace bs
  9. {
  10. /** @addtogroup Implementation
  11. * @{
  12. */
  13. template<bool Core> struct TGpuParamsType { };
  14. template<> struct TGpuParamsType<false> { typedef GpuParams Type; };
  15. template<> struct TGpuParamsType<true> { typedef ct::GpuParams Type; };
  16. /** Contains a set of GpuParams used for a single technique within a Material. */
  17. template<bool Core>
  18. class BS_CORE_EXPORT TGpuParamsSet
  19. {
  20. typedef typename TGpuParamsType<Core>::Type GpuParamsType;
  21. typedef typename TMaterialParamsType<Core>::Type MaterialParamsType;
  22. typedef typename TGpuParamBlockBufferPtrType<Core>::Type ParamBlockPtrType;
  23. typedef typename TTechniqueType<Core>::Type TechniqueType;
  24. typedef typename TShaderType<Core>::Type ShaderType;
  25. typedef typename TPassType<Core>::Type PassType;
  26. typedef typename TGpuProgramType<Core>::Type GpuProgramPtrType;
  27. typedef typename TGpuParamBlockBufferType<Core>::Type ParamBlockType;
  28. typedef typename TGpuParamTextureType<Core>::Type TextureType;
  29. typedef typename TGpuBufferType<Core>::Type BufferType;
  30. typedef typename TGpuParamSamplerStateType<Core>::Type SamplerStateType;
  31. typedef typename TPassTypes<Core>::GraphicsPipelineStateType GraphicsPipelineStateType;
  32. typedef typename TPassTypes<Core>::ComputePipelineStateType ComputePipelineStateType;
  33. /** Binding location for a single GPU param block buffer. */
  34. struct BlockBinding
  35. {
  36. UINT32 set;
  37. UINT32 slot;
  38. };
  39. /** All bindings for GPU param block buffers, for a single pass. */
  40. struct PassBlockBindings
  41. {
  42. BlockBinding bindings[GPT_COUNT];
  43. };
  44. /** Information about a parameter block buffer. */
  45. struct BlockInfo
  46. {
  47. BlockInfo(const String& name, UINT32 set, UINT32 slot, const ParamBlockPtrType& buffer, bool shareable)
  48. : name(name), set(set), slot(slot), buffer(buffer), shareable(shareable), allowUpdate(true), isUsed(true)
  49. , passData(nullptr)
  50. { }
  51. String name;
  52. UINT32 set;
  53. UINT32 slot;
  54. ParamBlockPtrType buffer;
  55. bool shareable;
  56. bool allowUpdate;
  57. bool isUsed;
  58. PassBlockBindings* passData;
  59. };
  60. /** Information about how a data parameter maps from a material parameter into a parameter block buffer. */
  61. struct DataParamInfo
  62. {
  63. UINT32 paramIdx;
  64. UINT32 blockIdx;
  65. UINT32 offset;
  66. };
  67. /** Information about how an object parameter maps from a material parameter to a GPU stage slot. */
  68. struct ObjectParamInfo
  69. {
  70. UINT32 paramIdx;
  71. UINT32 slotIdx;
  72. UINT32 setIdx;
  73. };
  74. /** Information about all object parameters for a specific GPU programmable stage. */
  75. struct StageParamInfo
  76. {
  77. ObjectParamInfo* textures;
  78. UINT32 numTextures;
  79. ObjectParamInfo* loadStoreTextures;
  80. UINT32 numLoadStoreTextures;
  81. ObjectParamInfo* buffers;
  82. UINT32 numBuffers;
  83. ObjectParamInfo* samplerStates;
  84. UINT32 numSamplerStates;
  85. };
  86. /** Information about all object parameters for a specific pass. */
  87. struct PassParamInfo
  88. {
  89. StageParamInfo stages[GPT_COUNT];
  90. };
  91. public:
  92. TGpuParamsSet() {}
  93. TGpuParamsSet(const SPtr<TechniqueType>& technique, const ShaderType& shader,
  94. const SPtr<MaterialParamsType>& params);
  95. ~TGpuParamsSet();
  96. /**
  97. * Returns a set of GPU parameters for the specified pass.
  98. *
  99. * @param[in] passIdx Pass in which to look the GPU program for in.
  100. * @return GPU parameters object that can be used for setting parameters of all GPU programs
  101. * in a pass. Returns null if pass doesn't exist.
  102. */
  103. SPtr<GpuParamsType> getGpuParams(UINT32 passIdx = 0);
  104. /**
  105. * Searches for a parameter block buffer with the specified name, and returns an index you can use for accessing it.
  106. * Returns -1 if buffer was not found.
  107. */
  108. UINT32 getParamBlockBufferIndex(const String& name) const;
  109. /**
  110. * Assign a parameter block buffer with the specified index to all the relevant child GpuParams.
  111. *
  112. * @param[in] index Index of the buffer, as retrieved from getParamBlockBufferIndex().
  113. * @param[in] paramBlock Parameter block to assign.
  114. * @param[in] ignoreInUpdate If true the buffer will not be updated during the update() call. This is useful
  115. * if the caller wishes to manually update the buffer contents externally, to prevent
  116. * overwriting manually written data during update.
  117. *
  118. * @note
  119. * Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or
  120. * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters
  121. * in the buffers need only be set once and then reused multiple times.
  122. */
  123. void setParamBlockBuffer(UINT32 index, const ParamBlockPtrType& paramBlock, bool ignoreInUpdate = false);
  124. /**
  125. * Assign a parameter block buffer with the specified name to all the relevant child GpuParams.
  126. *
  127. * @param[in] name Name of the buffer to set.
  128. * @param[in] paramBlock Parameter block to assign.
  129. * @param[in] ignoreInUpdate If true the buffer will not be updated during the update() call. This is useful
  130. * if the caller wishes to manually update the buffer contents externally, to prevent
  131. * overwriting manually written data during update.
  132. *
  133. * @note
  134. * Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or
  135. * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters
  136. * in the buffers need only be set once and then reused multiple times.
  137. */
  138. void setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock, bool ignoreInUpdate = false);
  139. /** Returns the number of passes the set contains the parameters for. */
  140. UINT32 getNumPasses() const { return (UINT32)mPassParams.size(); }
  141. /**
  142. * Updates parameter data in this object from the provided material parameters object.
  143. *
  144. * @param[in] params Object containing the parameter data to update from. Layout of the object must match
  145. * the object used for creating this object (be created for the same shader).
  146. * @param[in] updateAll Normally the system will track dirty parameters since the last call to this method,
  147. * and only update the dirty ones. Set this to true if you want to force all parameters
  148. * to update, regardless of their dirty state.
  149. */
  150. void update(const SPtr<MaterialParamsType>& params, bool updateAll = false);
  151. static const UINT32 NUM_STAGES;
  152. private:
  153. template<bool Core2> friend class TMaterial;
  154. Vector<SPtr<GpuParamsType>> mPassParams;
  155. Vector<BlockInfo> mBlocks;
  156. Vector<DataParamInfo> mDataParamInfos;
  157. PassParamInfo* mPassParamInfos;
  158. UINT64 mParamVersion;
  159. UINT8* mData;
  160. };
  161. /** Sim thread version of TGpuParamsSet<Core>. */
  162. class BS_CORE_EXPORT GpuParamsSet : public TGpuParamsSet<false>
  163. {
  164. public:
  165. GpuParamsSet() { }
  166. GpuParamsSet(const SPtr<Technique>& technique, const HShader& shader,
  167. const SPtr<MaterialParams>& params)
  168. :TGpuParamsSet(technique, shader, params)
  169. { }
  170. };
  171. namespace ct
  172. {
  173. /** Core thread version of TGpuParamsSet<Core>. */
  174. class BS_CORE_EXPORT GpuParamsSet : public TGpuParamsSet<true>
  175. {
  176. public:
  177. GpuParamsSet() { }
  178. GpuParamsSet(const SPtr<Technique>& technique, const SPtr<Shader>& shader,
  179. const SPtr<MaterialParams>& params)
  180. :TGpuParamsSet(technique, shader, params)
  181. { }
  182. };
  183. }
  184. /** @} */
  185. }