BsGpuProgram.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 GPU program input declaration. Only relevant for vertex programs.
  135. */
  136. SPtr<VertexDeclarationCore> getInputDeclaration() const { return mInputDeclaration; }
  137. /**
  138. * @brief Returns properties that contain information about the GPU program.
  139. */
  140. const GpuProgramProperties& getProperties() const { return mProperties; }
  141. /**
  142. * @copydoc GpuProgram::create
  143. */
  144. static SPtr<GpuProgramCore> create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  145. GpuProgramProfile profile, bool requiresAdjacency = false);
  146. protected:
  147. GpuProgramCore(const String& source, const String& entryPoint,
  148. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  149. /**
  150. * @brief Returns whether required capabilities for this program is supported.
  151. */
  152. bool isRequiredCapabilitiesSupported() const;
  153. bool mNeedsAdjacencyInfo;
  154. bool mIsCompiled;
  155. String mCompileError;
  156. GpuParamDescPtr mParametersDesc;
  157. SPtr<VertexDeclarationCore> mInputDeclaration;
  158. GpuProgramProperties mProperties;
  159. };
  160. /**
  161. * @brief Contains a GPU program such as vertex or fragment program which gets
  162. * compiled from the provided source code.
  163. *
  164. * @note Sim thread only.
  165. */
  166. class BS_CORE_EXPORT GpuProgram : public IReflectable, public CoreObject
  167. {
  168. public:
  169. virtual ~GpuProgram() { }
  170. /**
  171. * @brief Returns true if shader was successfully compiled.
  172. *
  173. * @note Only valid after core thread has initialized the program.
  174. */
  175. bool isCompiled() const;
  176. /**
  177. * @brief Returns an error message returned by the compiler, if the compilation failed.
  178. *
  179. * @note Only valid after core thread has initialized the program.
  180. */
  181. String getCompileErrorMessage() const;
  182. /**
  183. * @brief Creates a new parameters object compatible with this program definition. You
  184. * may populate the returned object with actual parameter values and bind it
  185. * to the pipeline to render an object using those values and this program.
  186. *
  187. * @note Only valid after core thread has initialized the program.
  188. */
  189. GpuParamsPtr createParameters();
  190. /**
  191. * @brief Returns description of all parameters in this GPU program.
  192. *
  193. * @note Only valid after core thread has initialized the program.
  194. */
  195. GpuParamDescPtr getParamDesc() const;
  196. /**
  197. * @brief Retrieves a core implementation of a gpu program usable only from the
  198. * core thread.
  199. */
  200. SPtr<GpuProgramCore> getCore() const;
  201. /**
  202. * @brief Returns properties that contain information about the GPU program.
  203. */
  204. const GpuProgramProperties& getProperties() const { return mProperties; }
  205. /**
  206. * @brief Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  207. * "isCompiled" with return false, and you will be able to retrieve the error message via "getCompileErrorMessage".
  208. *
  209. * @param source Source code to compile the shader from.
  210. * @param entryPoint Name of the entry point function, e.g. "main".
  211. * @param language Language the source is written in, e.g. "hlsl" or "glsl".
  212. * @param gptype Type of the program, e.g. vertex or fragment.
  213. * @param profile Program profile specifying supported feature-set. Must match the type.
  214. * @param requiresAdjacency If true then adjacency information will be provided when rendering using this program.
  215. */
  216. static GpuProgramPtr create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  217. GpuProgramProfile profile, bool requiresAdjacency = false);
  218. protected:
  219. friend class GpuProgramManager;
  220. GpuProgram(const String& source, const String& entryPoint, const String& language,
  221. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  222. /**
  223. * @copydoc CoreObject::createCore
  224. */
  225. SPtr<CoreObjectCore> createCore() const;
  226. /**
  227. * @copydoc Resource::calculateSize
  228. */
  229. size_t calculateSize() const { return 0; } // TODO
  230. protected:
  231. bool mNeedsAdjacencyInfo;
  232. String mLanguage;
  233. GpuProgramProperties mProperties;
  234. /************************************************************************/
  235. /* SERIALIZATION */
  236. /************************************************************************/
  237. public:
  238. friend class GpuProgramRTTI;
  239. static RTTITypeBase* getRTTIStatic();
  240. virtual RTTITypeBase* getRTTI() const;
  241. };
  242. }