BsGpuProgram.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsCoreObject.h"
  6. #include "BsIReflectable.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup RenderAPI
  10. * @{
  11. */
  12. /** GPU program profiles representing supported feature sets. */
  13. enum GpuProgramProfile
  14. {
  15. GPP_NONE, /**< No profile. */
  16. GPP_FS_1_1, /**< Fragment program 1.1 profile. */
  17. GPP_FS_1_2, /**< Fragment program 1.2 profile. */
  18. GPP_FS_1_3, /**< Fragment program 1.3 profile. */
  19. GPP_FS_1_4, /**< Fragment program 1.4 profile. */
  20. GPP_FS_2_0, /**< Fragment program 2.0 profile. */
  21. GPP_FS_2_x, /**< Fragment program 2.x profile. */
  22. GPP_FS_2_a, /**< Fragment program 2.a profile. */
  23. GPP_FS_2_b, /**< Fragment program 2.b profile. */
  24. GPP_FS_3_0, /**< Fragment program 3.0 profile. */
  25. GPP_FS_3_x, /**< Fragment program 3.x profile. */
  26. GPP_FS_4_0, /**< Fragment program 4.0 profile. */
  27. GPP_FS_4_1, /**< Fragment program 4.1 profile. */
  28. GPP_FS_5_0, /**< Fragment program 5.0 profile. */
  29. GPP_VS_1_1, /**< Vertex program 1.1 profile. */
  30. GPP_VS_2_0, /**< Vertex program 2.0 profile. */
  31. GPP_VS_2_x, /**< Vertex program 2.x profile. */
  32. GPP_VS_2_a, /**< Vertex program 2.a profile. */
  33. GPP_VS_3_0, /**< Vertex program 3.0 profile. */
  34. GPP_VS_4_0, /**< Vertex program 4.0 profile. */
  35. GPP_VS_4_1, /**< Vertex program 4.1 profile. */
  36. GPP_VS_5_0, /**< Vertex program 5.0 profile. */
  37. GPP_GS_4_0, /**< Geometry program 4.0 profile. */
  38. GPP_GS_4_1, /**< Geometry program 4.1 profile. */
  39. GPP_GS_5_0, /**< Geometry program 5.0 profile. */
  40. GPP_HS_5_0, /**< Hull program 5.0 profile. */
  41. GPP_DS_5_0, /**< Domain program 5.0 profile. */
  42. GPP_CS_5_0 /**< Compute program 5.0 profile. */
  43. };
  44. /** Data describing a GpuProgram. */
  45. class BS_CORE_EXPORT GpuProgramProperties
  46. {
  47. public:
  48. GpuProgramProperties(const String& source, const String& entryPoint,
  49. GpuProgramType gptype, GpuProgramProfile profile);
  50. virtual ~GpuProgramProperties() { }
  51. /** Source used for creating this program. */
  52. const String& getSource() const { return mSource; }
  53. /** Type of GPU program (for example fragment, vertex). */
  54. GpuProgramType getType() const { return mType; }
  55. /** Profile of the GPU program (for example VS_4_0, VS_5_0). */
  56. GpuProgramProfile getProfile() const { return mProfile; }
  57. /** Name of the program entry method (for example "main"). */
  58. const String& getEntryPoint() const { return mEntryPoint; }
  59. protected:
  60. friend class GpuProgramRTTI;
  61. GpuProgramType mType;
  62. String mEntryPoint;
  63. GpuProgramProfile mProfile;
  64. String mSource;
  65. };
  66. /**
  67. * Contains a GPU program such as vertex or fragment program which gets compiled from the provided source code.
  68. *
  69. * @note Sim thread only.
  70. */
  71. class BS_CORE_EXPORT GpuProgram : public IReflectable, public CoreObject
  72. {
  73. public:
  74. virtual ~GpuProgram() { }
  75. /**
  76. * Returns true if the program was successfully compiled.
  77. *
  78. * @note Only valid after core thread has initialized the program.
  79. */
  80. bool isCompiled() const;
  81. /**
  82. * Returns an error message returned by the compiler, if the compilation failed.
  83. *
  84. * @note Only valid after core thread has initialized the program.
  85. */
  86. String getCompileErrorMessage() const;
  87. /**
  88. * Returns description of all parameters in this GPU program.
  89. *
  90. * @note Only valid after core thread has initialized the program.
  91. */
  92. SPtr<GpuParamDesc> getParamDesc() const;
  93. /** Retrieves a core implementation of a gpu program usable only from the core thread. */
  94. SPtr<GpuProgramCore> getCore() const;
  95. /** Returns properties that contain information about the GPU program. */
  96. const GpuProgramProperties& getProperties() const { return mProperties; }
  97. /**
  98. * Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  99. * isCompiled() with return false, and you will be able to retrieve the error message via getCompileErrorMessage().
  100. *
  101. * @param[in] source Source code to compile the program from.
  102. * @param[in] entryPoint Name of the entry point function, for example "main".
  103. * @param[in] language Language the source is written in, for example "hlsl" or "glsl".
  104. * @param[in] gptype Type of the program, for example vertex or fragment.
  105. * @param[in] profile Program profile specifying supported feature-set. Must match the type.
  106. * @param[in] requiresAdjacency If true then adjacency information will be provided when rendering using this
  107. * program.
  108. */
  109. static SPtr<GpuProgram> create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  110. GpuProgramProfile profile, bool requiresAdjacency = false);
  111. protected:
  112. friend class GpuProgramManager;
  113. GpuProgram(const String& source, const String& entryPoint, const String& language,
  114. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  115. /** @copydoc CoreObject::createCore */
  116. SPtr<CoreObjectCore> createCore() const override;
  117. protected:
  118. bool mNeedsAdjacencyInfo;
  119. String mLanguage;
  120. GpuProgramProperties mProperties;
  121. /************************************************************************/
  122. /* SERIALIZATION */
  123. /************************************************************************/
  124. public:
  125. friend class GpuProgramRTTI;
  126. static RTTITypeBase* getRTTIStatic();
  127. RTTITypeBase* getRTTI() const override;
  128. };
  129. /** @} */
  130. /** @addtogroup RenderAPI-Internal
  131. * @{
  132. */
  133. /**
  134. * Core thread version of a GpuProgram.
  135. *
  136. * @note Core thread only.
  137. */
  138. class BS_CORE_EXPORT GpuProgramCore : public CoreObjectCore
  139. {
  140. public:
  141. virtual ~GpuProgramCore() { }
  142. /** Returns whether this program can be supported on the current renderer and hardware. */
  143. virtual bool isSupported() const;
  144. /** Returns true if program was successfully compiled. */
  145. virtual bool isCompiled() const { return mIsCompiled; }
  146. /** Returns an error message returned by the compiler, if the compilation failed. */
  147. virtual String getCompileErrorMessage() const { return mCompileError; }
  148. /**
  149. * Sets whether this geometry program requires adjacency information from the input primitives.
  150. *
  151. * @note Only relevant for geometry programs.
  152. */
  153. virtual void setAdjacencyInfoRequired(bool required) { mNeedsAdjacencyInfo = required; }
  154. /**
  155. * Returns whether this geometry program requires adjacency information from the input primitives.
  156. *
  157. * @note Only relevant for geometry programs.
  158. */
  159. virtual bool isAdjacencyInfoRequired() const { return mNeedsAdjacencyInfo; }
  160. /** @copydoc GpuProgram::getParamDesc */
  161. SPtr<GpuParamDesc> getParamDesc() const { return mParametersDesc; }
  162. /** Returns GPU program input declaration. Only relevant for vertex programs. */
  163. SPtr<VertexDeclarationCore> getInputDeclaration() const { return mInputDeclaration; }
  164. /** Returns properties that contain information about the GPU program. */
  165. const GpuProgramProperties& getProperties() const { return mProperties; }
  166. /** @copydoc GpuProgram::create */
  167. static SPtr<GpuProgramCore> create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  168. GpuProgramProfile profile, bool requiresAdjacency = false);
  169. protected:
  170. GpuProgramCore(const String& source, const String& entryPoint,
  171. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  172. /** Returns whether required capabilities for this program is supported. */
  173. bool isRequiredCapabilitiesSupported() const;
  174. bool mNeedsAdjacencyInfo;
  175. bool mIsCompiled;
  176. String mCompileError;
  177. SPtr<GpuParamDesc> mParametersDesc;
  178. SPtr<VertexDeclarationCore> mInputDeclaration;
  179. GpuProgramProperties mProperties;
  180. };
  181. /** @} */
  182. }
  183. namespace std
  184. {
  185. /** Hash value generator for GpuProgramProfile. */
  186. template<>
  187. struct hash<BansheeEngine::GpuProgramProfile>
  188. {
  189. size_t operator()(const BansheeEngine::GpuProgramProfile& profile) const
  190. {
  191. size_t hash = 0;
  192. BansheeEngine::hash_combine(hash, (int)profile);
  193. return hash;
  194. }
  195. };
  196. }