CmShader.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmResource.h"
  4. #include "CmCommonEnums.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Describes a single data (int, Vector2, etc.) shader parameter.
  9. *
  10. * @see Shader::addParameter.
  11. */
  12. struct CM_EXPORT SHADER_DATA_PARAM_DESC
  13. {
  14. String name;
  15. String gpuVariableName;
  16. GpuParamDataType type;
  17. UINT32 arraySize;
  18. bool hidden;
  19. UINT32 elementSize;
  20. };
  21. /**
  22. * @brief Describes a single object (texture, sampler state, etc.) shader parameter.
  23. *
  24. * @see Shader::addParameter.
  25. */
  26. struct CM_EXPORT SHADER_OBJECT_PARAM_DESC
  27. {
  28. String name;
  29. String gpuVariableName;
  30. GpuParamObjectType type;
  31. bool hidden;
  32. };
  33. /**
  34. * @brief Describes a shader parameter block.
  35. */
  36. struct CM_EXPORT SHADER_PARAM_BLOCK_DESC
  37. {
  38. String name;
  39. bool shared;
  40. GpuParamBlockUsage usage;
  41. };
  42. /**
  43. * @brief Shader represents a collection of techniques. They are used in Materials,
  44. * which can be considered as instances of a Shader. Multiple materials
  45. * may share the same shader but provide different parameters to it.
  46. *
  47. * Shader will always choose the first supported technique based on the current render
  48. * system, render manager and other properties. So make sure to add most important techniques
  49. * first so you make sure they are used if they are supported.
  50. */
  51. class CM_EXPORT Shader : public Resource
  52. {
  53. public:
  54. /**
  55. * @brief Adds a new technique that supports the provided render system
  56. * and renderer to the shader. It's up to the caller to populate the
  57. * returned object with valid data.
  58. */
  59. TechniquePtr addTechnique(const String& renderSystem, const String& renderer);
  60. /**
  61. * @brief Removes a technique at the specified index.
  62. */
  63. void removeTechnique(UINT32 idx);
  64. /**
  65. * @brief Removes the specified technique.
  66. */
  67. void removeTechnique(TechniquePtr technique);
  68. /**
  69. * @brief Returns the total number of techniques in this shader.
  70. */
  71. UINT32 getNumTechniques() const { return (UINT32)mTechniques.size(); }
  72. /**
  73. * @brief Gets the best supported technique based on current render and other systems.
  74. * Returns null if not a single technique is supported.
  75. */
  76. TechniquePtr getBestTechnique() const;
  77. /**
  78. * @brief Registers a new data (int, Vector2, etc.) parameter you that you may then use
  79. * via Material by providing the parameter name. All parameters internally map to
  80. * variables defined in GPU programs.
  81. *
  82. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  83. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  84. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  85. * @param arraySize (optional) If the parameter is an array, the number of elements in the array. Size of 1 means its not an array.
  86. * @param elementSize (optional) Size of an individual element in the array, in bytes. You only need to set this if you are setting variable
  87. * length parameters, like structs.
  88. * @param hidden (optional) Property that is not directly used by the material system, but can be useful if you need to mark certain parameters
  89. * as hidden to some system. (e.g. hiding internal engine-managed parameters from the user in the Editor)
  90. */
  91. void addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type, UINT32 arraySize = 1, UINT32 elementSize = 0, bool hidden = false);
  92. /**
  93. * @brief Registers a new object (texture, sampler state, etc.) parameter you that you may then use
  94. * via Material by providing the parameter name. All parameters internally map to variables defined in GPU programs.
  95. *
  96. * @param name The name of the parameter. Name must be unique between all data and object parameters.
  97. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  98. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  99. * @param hidden (optional) Property that is not directly used by the material system, but can be useful if you need to mark certain parameters
  100. * as hidden to some system. (e.g. hiding internal engine-managed parameters from the user in the Editor)
  101. */
  102. void addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, bool hidden = false);
  103. /**
  104. * @brief Unregister a parameter with the specified name.
  105. */
  106. void removeParameter(const String& name);
  107. /**
  108. * @brief Changes parameters of a parameter block with the specified name.
  109. */
  110. void setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage);
  111. /**
  112. * @brief Returns type of the parameter with the specified name. Throws exception if
  113. * the parameter doesn't exist.
  114. */
  115. GpuParamType getParamType(const String& name) const;
  116. /**
  117. * @brief Returns description for a data parameter with the specified name. Throws exception if
  118. * the parameter doesn't exist.
  119. */
  120. const SHADER_DATA_PARAM_DESC& getDataParamDesc(const String& name) const;
  121. /**
  122. * @brief Returns description for an object parameter with the specified name. Throws exception if
  123. * the parameter doesn't exist.
  124. */
  125. const SHADER_OBJECT_PARAM_DESC& getObjectParamDesc(const String& name) const;
  126. /**
  127. * @brief Checks if the parameter with the specified name exists, and is a data parameter.
  128. */
  129. bool hasDataParam(const String& name) const;
  130. /**
  131. * @brief Checks if the parameter with the specified name exists, and is an object parameter.
  132. */
  133. bool hasObjectParam(const String& name) const;
  134. /**
  135. * @brief Returns a map of all data parameters in the shader.
  136. *
  137. * @note Internal method.
  138. */
  139. const Map<String, SHADER_DATA_PARAM_DESC>& _getDataParams() const { return mDataParams; }
  140. /**
  141. * @brief Returns a map of all object parameters in the shader.
  142. *
  143. * @note Internal method.
  144. */
  145. const Map<String, SHADER_OBJECT_PARAM_DESC>& _getObjectParams() const { return mObjectParams; }
  146. /**
  147. * @brief Returns a map of all parameter blocks.
  148. *
  149. * @note Internal method.
  150. */
  151. const Map<String, SHADER_PARAM_BLOCK_DESC>& _getParamBlocks() const { return mParamBlocks; }
  152. static bool isSampler(GpuParamObjectType type);
  153. static bool isTexture(GpuParamObjectType type);
  154. static bool isBuffer(GpuParamObjectType type);
  155. /**
  156. * @brief Returns an empty shader object with the specified name. Caller must register
  157. * techniques with the shader before using it in a Material.
  158. */
  159. static ShaderPtr create(const String& name);
  160. private:
  161. String mName;
  162. Vector<TechniquePtr> mTechniques;
  163. Map<String, SHADER_DATA_PARAM_DESC> mDataParams;
  164. Map<String, SHADER_OBJECT_PARAM_DESC> mObjectParams;
  165. Map<String, SHADER_PARAM_BLOCK_DESC> mParamBlocks;
  166. Shader(const String& name);
  167. /************************************************************************/
  168. /* RTTI */
  169. /************************************************************************/
  170. public:
  171. friend class ShaderRTTI;
  172. static RTTITypeBase* getRTTIStatic();
  173. virtual RTTITypeBase* getRTTI() const;
  174. };
  175. }