BsGpuProgram.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsDrawOps.h"
  4. #include "BsCoreObject.h"
  5. #include "BsIReflectable.h"
  6. #include "BsGpuParamDesc.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Types of programs that may run on GPU.
  11. */
  12. enum GpuProgramType
  13. {
  14. GPT_VERTEX_PROGRAM,
  15. GPT_FRAGMENT_PROGRAM,
  16. GPT_GEOMETRY_PROGRAM,
  17. GPT_DOMAIN_PROGRAM,
  18. GPT_HULL_PROGRAM,
  19. GPT_COMPUTE_PROGRAM
  20. };
  21. /**
  22. * @brief GPU program profiles representing supported
  23. * feature sets.
  24. */
  25. enum GpuProgramProfile
  26. {
  27. GPP_NONE,
  28. GPP_FS_1_1,
  29. GPP_FS_1_2,
  30. GPP_FS_1_3,
  31. GPP_FS_1_4,
  32. GPP_FS_2_0,
  33. GPP_FS_2_x,
  34. GPP_FS_2_a,
  35. GPP_FS_2_b,
  36. GPP_FS_3_0,
  37. GPP_FS_3_x,
  38. GPP_FS_4_0,
  39. GPP_FS_4_1,
  40. GPP_FS_5_0,
  41. GPP_VS_1_1,
  42. GPP_VS_2_0,
  43. GPP_VS_2_x,
  44. GPP_VS_2_a,
  45. GPP_VS_3_0,
  46. GPP_VS_4_0,
  47. GPP_VS_4_1,
  48. GPP_VS_5_0,
  49. GPP_GS_4_0,
  50. GPP_GS_4_1,
  51. GPP_GS_5_0,
  52. GPP_HS_5_0,
  53. GPP_DS_5_0,
  54. GPP_CS_5_0
  55. };
  56. /**
  57. * @brief Data describing a GpuProgram.
  58. */
  59. class BS_CORE_EXPORT GpuProgramProperties
  60. {
  61. public:
  62. GpuProgramProperties(const String& source, const String& entryPoint,
  63. GpuProgramType gptype, GpuProgramProfile profile);
  64. virtual ~GpuProgramProperties() { }
  65. /**
  66. * @brief Source used for creating this program.
  67. */
  68. const String& getSource() const { return mSource; }
  69. /**
  70. * @brief Type of GPU program (e.g. fragment, vertex)
  71. */
  72. GpuProgramType getType() const { return mType; }
  73. /**
  74. * @brief Profile of the GPU program (e.g. VS_4_0, VS_5_0)
  75. */
  76. GpuProgramProfile getProfile() const { return mProfile; }
  77. /**
  78. * @brief Name of the program entry method (e.g. "main")
  79. */
  80. const String& getEntryPoint() const { return mEntryPoint; }
  81. protected:
  82. friend class GpuProgramRTTI;
  83. GpuProgramType mType;
  84. String mEntryPoint;
  85. GpuProgramProfile mProfile;
  86. String mSource;
  87. };
  88. /**
  89. * @brief Core thread version of a GpuProgram.
  90. *
  91. * @see GpuProgram
  92. *
  93. * @note Core thread only.
  94. */
  95. class BS_CORE_EXPORT GpuProgramCore : public CoreObjectCore
  96. {
  97. public:
  98. virtual ~GpuProgramCore() { }
  99. /**
  100. * @brief Returns whether this program can be supported on the current renderer and hardware.
  101. */
  102. virtual bool isSupported() const;
  103. /**
  104. * @brief Returns true if shader was successfully compiled.
  105. */
  106. virtual bool isCompiled() const { return mIsCompiled; }
  107. /**
  108. * @brief Returns an error message returned by the compiler, if the compilation failed.
  109. */
  110. virtual String getCompileErrorMessage() const { return mCompileError; }
  111. /**
  112. * @brief Sets whether this geometry program requires adjacency information
  113. * from the input primitives.
  114. *
  115. * @note Only relevant for geometry programs.
  116. */
  117. virtual void setAdjacencyInfoRequired(bool required) { mNeedsAdjacencyInfo = required; }
  118. /**
  119. * @brief Returns whether this geometry program requires adjacency information
  120. * from the input primitives.
  121. *
  122. * @note Only relevant for geometry programs.
  123. */
  124. virtual bool isAdjacencyInfoRequired() const { return mNeedsAdjacencyInfo; }
  125. /**
  126. * @copydoc GpuProgram::createParameters
  127. */
  128. virtual SPtr<GpuParamsCore> createParameters();
  129. /**
  130. * @copydoc GpuProgram::getParamDesc
  131. */
  132. GpuParamDescPtr getParamDesc() const { return mParametersDesc; }
  133. /**
  134. * @brief Returns properties that contain information about the GPU program.
  135. */
  136. const GpuProgramProperties& getProperties() const { return mProperties; }
  137. /**
  138. * @copydoc GpuProgram::create
  139. */
  140. static SPtr<GpuProgramCore> create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  141. GpuProgramProfile profile, bool requiresAdjacency = false);
  142. protected:
  143. GpuProgramCore(const String& source, const String& entryPoint,
  144. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  145. /**
  146. * @brief Returns whether required capabilities for this program is supported.
  147. */
  148. bool isRequiredCapabilitiesSupported() const;
  149. bool mNeedsAdjacencyInfo;
  150. bool mIsCompiled;
  151. String mCompileError;
  152. GpuParamDescPtr mParametersDesc;
  153. GpuProgramProperties mProperties;
  154. };
  155. /**
  156. * @brief Contains a GPU program such as vertex or fragment program which gets
  157. * compiled from the provided source code.
  158. *
  159. * @note Sim thread only.
  160. */
  161. class BS_CORE_EXPORT GpuProgram : public IReflectable, public CoreObject
  162. {
  163. public:
  164. virtual ~GpuProgram() { }
  165. /**
  166. * @brief Returns true if shader was successfully compiled.
  167. *
  168. * @note Only valid after core thread has initialized the program.
  169. */
  170. bool isCompiled() const;
  171. /**
  172. * @brief Returns an error message returned by the compiler, if the compilation failed.
  173. *
  174. * @note Only valid after core thread has initialized the program.
  175. */
  176. String getCompileErrorMessage() const;
  177. /**
  178. * @brief Creates a new parameters object compatible with this program definition. You
  179. * may populate the returned object with actual parameter values and bind it
  180. * to the pipeline to render an object using those values and this program.
  181. *
  182. * @note Only valid after core thread has initialized the program.
  183. */
  184. GpuParamsPtr createParameters();
  185. /**
  186. * @brief Returns description of all parameters in this GPU program.
  187. *
  188. * @note Only valid after core thread has initialized the program.
  189. */
  190. GpuParamDescPtr getParamDesc() const;
  191. /**
  192. * @brief Retrieves a core implementation of a gpu program usable only from the
  193. * core thread.
  194. */
  195. SPtr<GpuProgramCore> getCore() const;
  196. /**
  197. * @brief Returns properties that contain information about the GPU program.
  198. */
  199. const GpuProgramProperties& getProperties() const { return mProperties; }
  200. /**
  201. * @brief Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  202. * "isCompiled" with return false, and you will be able to retrieve the error message via "getCompileErrorMessage".
  203. *
  204. * @param source Source code to compile the shader from.
  205. * @param entryPoint Name of the entry point function, e.g. "main".
  206. * @param language Language the source is written in, e.g. "hlsl" or "glsl".
  207. * @param gptype Type of the program, e.g. vertex or fragment.
  208. * @param profile Program profile specifying supported feature-set. Must match the type.
  209. * @param requiresAdjacency If true then adjacency information will be provided when rendering using this program.
  210. */
  211. static GpuProgramPtr create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  212. GpuProgramProfile profile, bool requiresAdjacency = false);
  213. protected:
  214. friend class GpuProgramManager;
  215. GpuProgram(const String& source, const String& entryPoint, const String& language,
  216. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  217. /**
  218. * @copydoc CoreObject::createCore
  219. */
  220. SPtr<CoreObjectCore> createCore() const;
  221. /**
  222. * @copydoc Resource::calculateSize
  223. */
  224. size_t calculateSize() const { return 0; } // TODO
  225. protected:
  226. bool mNeedsAdjacencyInfo;
  227. String mLanguage;
  228. GpuProgramProperties mProperties;
  229. /************************************************************************/
  230. /* SERIALIZATION */
  231. /************************************************************************/
  232. public:
  233. friend class GpuProgramRTTI;
  234. static RTTITypeBase* getRTTIStatic();
  235. virtual RTTITypeBase* getRTTI() const;
  236. };
  237. }