CmShader.h 7.3 KB

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