BsGpuParamsSet.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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)
  42. { }
  43. String name;
  44. ParamBlockPtrType buffer;
  45. bool shareable;
  46. bool allowUpdate;
  47. };
  48. /** Information about how a data parameter maps from a material parameter into a parameter block buffer. */
  49. struct DataParamInfo
  50. {
  51. UINT32 paramIdx;
  52. UINT32 blockIdx;
  53. UINT32 offset;
  54. };
  55. /** Information about how an object parameter maps from a material parameter to a GPU stage slot. */
  56. struct ObjectParamInfo
  57. {
  58. UINT32 paramIdx;
  59. UINT32 slotIdx;
  60. };
  61. /** Information about all object parameters for a specific GPU programmable stage. */
  62. struct StageParamInfo
  63. {
  64. ObjectParamInfo* textures;
  65. UINT32 numTextures;
  66. ObjectParamInfo* loadStoreTextures;
  67. UINT32 numLoadStoreTextures;
  68. ObjectParamInfo* buffers;
  69. UINT32 numBuffers;
  70. ObjectParamInfo* samplerStates;
  71. UINT32 numSamplerStates;
  72. };
  73. /** Information about all object parameters for a specific pass. */
  74. struct PassParamInfo
  75. {
  76. StageParamInfo stages[6];
  77. };
  78. public:
  79. TGpuParamsSet() {}
  80. TGpuParamsSet(const SPtr<TechniqueType>& technique, const ShaderType& shader, UINT32 techniqueIdx,
  81. const SPtr<MaterialParamsType>& params);
  82. ~TGpuParamsSet();
  83. /**
  84. * Returns a GPU parameters for a specific shader stage and pass.
  85. *
  86. * @param[in] stageidx Sequential index of the shader stage to retrieve the parameters for.
  87. * @param[in] passIdx Pass for which to retrieve the parameters for.
  88. * @return GPU parameters object that can be used for setting parameters of a GPU program
  89. * individually.
  90. *
  91. * @note Useful when needing to iterate over all sets of GPU parameters.
  92. */
  93. GpuParamsType getParamByIdx(UINT32 stageidx, UINT32 passIdx = 0)
  94. {
  95. GpuParamsType* paramArray[] = { &mPassParams[passIdx].vertex, &mPassParams[passIdx].fragment,
  96. &mPassParams[passIdx].geometry, &mPassParams[passIdx].hull, &mPassParams[passIdx].domain,
  97. &mPassParams[passIdx].compute };
  98. return *paramArray[stageidx];
  99. }
  100. /**
  101. * Sets GPU parameters for a specific shader stage and pass.
  102. *
  103. * @param[in] stageidx Sequential index of the shader stage to set the parameters for.
  104. * @param[in] params GPU parameters object to assign.
  105. * @param[in] passIdx Pass for which to set the parameters for.
  106. *
  107. * @note Useful when needing to iterate over all sets of GPU parameters.
  108. */
  109. void setParamByIdx(UINT32 stageidx, const GpuParamsType& params, UINT32 passIdx = 0)
  110. {
  111. GpuParamsType* paramArray[] = { &mPassParams[passIdx].vertex, &mPassParams[passIdx].fragment,
  112. &mPassParams[passIdx].geometry, &mPassParams[passIdx].hull, &mPassParams[passIdx].domain,
  113. &mPassParams[passIdx].compute };
  114. (*paramArray[stageidx]) = params;
  115. }
  116. /**
  117. * Returns a set of GPU parameters for an individual GPU program of the specified pass.
  118. *
  119. * @param[in] type Type of the program to retrieve parameters for.
  120. * @param[in] passIdx Pass in which to look the GPU program for in.
  121. * @return GPU parameters object that can be used for setting parameters of a GPU program
  122. * individually. Returns null if program or pass doesn't exist.
  123. */
  124. GpuParamsType getGpuParams(GpuProgramType type, UINT32 passIdx = 0);
  125. /**
  126. * Assign a parameter block buffer with the specified name to all the relevant child GpuParams.
  127. *
  128. * @param[in] name Name of the buffer to set.
  129. * @param[in] paramBlock Parameter block to assign.
  130. * @param[in] ignoreInUpdate If true the buffer will not be updated during the update() call. This is useful
  131. * if the caller wishes to manually update the buffer contents externally, to prevent
  132. * overwriting manually written data during update.
  133. *
  134. * @note
  135. * Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or
  136. * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters
  137. * in the buffers need only be set once and then reused multiple times.
  138. */
  139. void setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock, bool ignoreInUpdate = false);
  140. /** Returns the number of passes the set contains the parameters for. */
  141. UINT32 getNumPasses() const { return (UINT32)mPassParams.size(); }
  142. /**
  143. * Updates internal GPU params for all passes and stages from the provided material parameters object.
  144. *
  145. * @param[in] params Object containing the parameter data to update from. Layout of the object must match the
  146. * object used for creating this object (be created for the same shader).
  147. * @param[in] updateAll By default the system will only update parameters marked as dirty in @p params. If this
  148. * is set to true, all parameters will be updated instead.
  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. UINT32 mTechniqueIdx;
  155. Vector<PassParams> mPassParams;
  156. Vector<BlockInfo> mBlocks;
  157. Vector<DataParamInfo> mDataParamInfos;
  158. PassParamInfo* mPassParamInfos;
  159. };
  160. /** Sim thread version of TGpuParamsSet<Core>. */
  161. class BS_CORE_EXPORT GpuParamsSet : public TGpuParamsSet<false>
  162. {
  163. public:
  164. GpuParamsSet() { }
  165. GpuParamsSet(const SPtr<Technique>& technique, const HShader& shader, UINT32 techniqueIdx,
  166. const SPtr<MaterialParams>& params)
  167. :TGpuParamsSet(technique, shader, techniqueIdx, params)
  168. { }
  169. };
  170. /** Core thread version of TGpuParamsSet<Core>. */
  171. class BS_CORE_EXPORT GpuParamsSetCore : public TGpuParamsSet<true>
  172. {
  173. public:
  174. GpuParamsSetCore() { }
  175. GpuParamsSetCore(const SPtr<TechniqueCore>& technique, const SPtr<ShaderCore>& shader, UINT32 techniqueIdx,
  176. const SPtr<MaterialParamsCore>& params)
  177. :TGpuParamsSet(technique, shader, techniqueIdx, params)
  178. { }
  179. };
  180. /** @} */
  181. }