BsGpuParamsSet.h 6.1 KB

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