BsGpuProgram.h 7.9 KB

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