CmGpuParams.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmGpuParam.h"
  4. #include "CmBindableGpuParams.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Contains descriptions for all parameters in a GPU program and also
  9. * allows you to write and read those parameters. All parameter values
  10. * are stored internally on the CPU, and are only submitted to the GPU
  11. * once the parameters are bound to the pipeline.
  12. *
  13. * @see CoreThreadAccessor::bindGpuParams
  14. *
  15. * @note Sim thread only.
  16. */
  17. class BS_CORE_EXPORT GpuParams
  18. {
  19. public:
  20. /**
  21. * @brief Creates new GpuParams object using the specified parameter descriptions.
  22. *
  23. * @param transposeMatrices If true the stored matrices will be transposed before
  24. * submitted to the GPU (some APIs require different
  25. * matrix layout).
  26. *
  27. * @note You normally do not want to call this manually. Instead use GpuProgram::createParameters.
  28. */
  29. GpuParams(GpuParamDesc& paramDesc, bool transposeMatrices);
  30. ~GpuParams();
  31. /**
  32. * @brief Binds a new parameter buffer to the specified slot. Any following parameter reads or
  33. * writes that are referencing that buffer slot will use the new buffer.
  34. *
  35. * @note This is useful if you want to share a parameter buffer among multiple GPU programs.
  36. * You would only set the values once and then share the buffer among all other GpuParams.
  37. *
  38. * It is up to the caller to guarantee the provided buffer matches parameter block
  39. * descriptor for this slot.
  40. */
  41. void setParamBlockBuffer(UINT32 slot, const GpuParamBlockBufferPtr& paramBlockBuffer);
  42. /**
  43. * @brief Replaces the parameter buffer with the specified name. Any following parameter reads or
  44. * writes that are referencing that buffer will use the new buffer.
  45. *
  46. * @note This is useful if you want to share a parameter buffer among multiple GPU programs.
  47. * You would only set the values once and then share the buffer among all other GpuParams.
  48. *
  49. * It is up to the caller to guarantee the provided buffer matches parameter block
  50. * descriptor for this slot.
  51. */
  52. void setParamBlockBuffer(const String& name, const GpuParamBlockBufferPtr& paramBlockBuffer);
  53. /**
  54. * @brief Returns a description of all stored parameters.
  55. */
  56. const GpuParamDesc& getParamDesc() const { return mParamDesc; }
  57. /**
  58. * @brief Returns the size of a data parameter with the specified name, in bytes.
  59. * Returns 0 if such parameter doesn't exist.
  60. */
  61. UINT32 getDataParamSize(const String& name) const;
  62. /**
  63. * @brief Checks if parameter with the specified name exists.
  64. */
  65. bool hasParam(const String& name) const;
  66. /**
  67. * @brief Checks if texture parameter with the specified name exists.
  68. */
  69. bool hasTexture(const String& name) const;
  70. /**
  71. * @brief Checks if sampler state parameter with the specified name exists.
  72. */
  73. bool hasSamplerState(const String& name) const;
  74. /**
  75. * @brief Checks if a parameter block with the specified name exists.
  76. */
  77. bool hasParamBlock(const String& name) const;
  78. /**
  79. * @brief Returns a handle for the parameter with the specified name.
  80. * Handle may then be stored and used for quickly setting or retrieving
  81. * values to/from that parameter.
  82. *
  83. * Throws exception if parameter with that name and type doesn't exist.
  84. *
  85. * Parameter handles will be invalidated when their parent GpuParams object changes.
  86. */
  87. template<class T> void getParam(const String& name, GpuDataParamBase<T>& output) const
  88. {
  89. CM_EXCEPT(InvalidParametersException, "Unsupported parameter type");
  90. }
  91. /**
  92. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  93. */
  94. template<>
  95. void getParam<float>(const String& name, GpuDataParamBase<float>& output) const
  96. {
  97. auto iterFind = mFloatParams.find(name);
  98. if(iterFind == mFloatParams.end())
  99. CM_EXCEPT(InvalidParametersException, "Cannot find float parameter with the name '" + name + "'");
  100. output = iterFind->second;
  101. }
  102. /**
  103. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  104. */
  105. template<>
  106. void getParam<Vector2>(const String& name, GpuDataParamBase<Vector2>& output) const
  107. {
  108. auto iterFind = mVec2Params.find(name);
  109. if(iterFind == mVec2Params.end())
  110. CM_EXCEPT(InvalidParametersException, "Cannot find vector(2) parameter with the name '" + name + "'");
  111. output = iterFind->second;
  112. }
  113. /**
  114. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  115. */
  116. template<>
  117. void getParam<Vector3>(const String& name, GpuDataParamBase<Vector3>& output) const
  118. {
  119. auto iterFind = mVec3Params.find(name);
  120. if(iterFind == mVec3Params.end())
  121. CM_EXCEPT(InvalidParametersException, "Cannot find vector(3) parameter with the name '" + name + "'");
  122. output = iterFind->second;
  123. }
  124. /**
  125. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  126. */
  127. template<>
  128. void getParam<Vector4>(const String& name, GpuDataParamBase<Vector4>& output) const
  129. {
  130. auto iterFind = mVec4Params.find(name);
  131. if(iterFind == mVec4Params.end())
  132. CM_EXCEPT(InvalidParametersException, "Cannot find vector(4) parameter with the name '" + name + "'");
  133. output = iterFind->second;
  134. }
  135. /**
  136. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  137. */
  138. template<>
  139. void getParam<Matrix3>(const String& name, GpuDataParamBase<Matrix3>& output) const
  140. {
  141. auto iterFind = mMat3Params.find(name);
  142. if(iterFind == mMat3Params.end())
  143. CM_EXCEPT(InvalidParametersException, "Cannot find matrix(3x3) parameter with the name '" + name + "'");
  144. output = iterFind->second;
  145. }
  146. /**
  147. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  148. */
  149. template<>
  150. void getParam<Matrix4>(const String& name, GpuDataParamBase<Matrix4>& output) const
  151. {
  152. auto iterFind = mMat4Params.find(name);
  153. if(iterFind == mMat4Params.end())
  154. CM_EXCEPT(InvalidParametersException, "Cannot find matrix(4x4) parameter with the name '" + name + "'");
  155. output = iterFind->second;
  156. }
  157. /**
  158. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  159. */
  160. void getStructParam(const String& name, GpuParamStruct& output) const;
  161. /**
  162. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  163. */
  164. void getTextureParam(const String& name, GpuParamTexture& output) const;
  165. /**
  166. * @copydoc getParam(const String&, GpuDataParamBase<T>&)
  167. */
  168. void getSamplerStateParam(const String& name, GpuParamSampState& output) const;
  169. private:
  170. friend class BindableGpuParams;
  171. GpuParamDesc& mParamDesc;
  172. bool mTransposeMatrices;
  173. /**
  174. * @brief Gets a descriptor for a data parameter with the specified name.
  175. */
  176. GpuParamDataDesc* getParamDesc(const String& name) const;
  177. UINT8* mData;
  178. UINT32 mNumParamBlocks;
  179. UINT32 mNumTextures;
  180. UINT32 mNumSamplerStates;
  181. GpuParamBlock** mParamBlocks;
  182. GpuParamBlockBufferPtr* mParamBlockBuffers;
  183. HTexture* mTextures;
  184. HSamplerState* mSamplerStates;
  185. mutable Map<String, GpuParamFloat> mFloatParams;
  186. mutable Map<String, GpuParamVec2> mVec2Params;
  187. mutable Map<String, GpuParamVec3> mVec3Params;
  188. mutable Map<String, GpuParamVec4> mVec4Params;
  189. mutable Map<String, GpuParamMat3> mMat3Params;
  190. mutable Map<String, GpuParamMat4> mMat4Params;
  191. mutable Map<String, GpuParamStruct> mStructParams;
  192. mutable Map<String, GpuParamTexture> mTextureParams;
  193. mutable Map<String, GpuParamSampState> mSampStateParams;
  194. };
  195. }