BsGpuParamsSet.h 7.6 KB

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