BsGpuParamsSet.h 5.9 KB

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