2
0

BsGpuProgram.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. * Creates a new parameters object compatible with this program definition. You may populate the returned object
  89. * with actual parameter values and bind it to the pipeline to render an object using those values and this program.
  90. *
  91. * @note Only valid after core thread has initialized the program.
  92. */
  93. SPtr<GpuParams> createParameters();
  94. /**
  95. * Returns description of all parameters in this GPU program.
  96. *
  97. * @note Only valid after core thread has initialized the program.
  98. */
  99. SPtr<GpuParamDesc> getParamDesc() const;
  100. /** Retrieves a core implementation of a gpu program usable only from the core thread. */
  101. SPtr<GpuProgramCore> getCore() const;
  102. /** Returns properties that contain information about the GPU program. */
  103. const GpuProgramProperties& getProperties() const { return mProperties; }
  104. /**
  105. * Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  106. * isCompiled() with return false, and you will be able to retrieve the error message via getCompileErrorMessage().
  107. *
  108. * @param[in] source Source code to compile the program from.
  109. * @param[in] entryPoint Name of the entry point function, for example "main".
  110. * @param[in] language Language the source is written in, for example "hlsl" or "glsl".
  111. * @param[in] gptype Type of the program, for example vertex or fragment.
  112. * @param[in] profile Program profile specifying supported feature-set. Must match the type.
  113. * @param[in] requiresAdjacency If true then adjacency information will be provided when rendering using this
  114. * program.
  115. */
  116. static SPtr<GpuProgram> create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  117. GpuProgramProfile profile, bool requiresAdjacency = false);
  118. protected:
  119. friend class GpuProgramManager;
  120. GpuProgram(const String& source, const String& entryPoint, const String& language,
  121. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  122. /** @copydoc CoreObject::createCore */
  123. SPtr<CoreObjectCore> createCore() const override;
  124. protected:
  125. bool mNeedsAdjacencyInfo;
  126. String mLanguage;
  127. GpuProgramProperties mProperties;
  128. /************************************************************************/
  129. /* SERIALIZATION */
  130. /************************************************************************/
  131. public:
  132. friend class GpuProgramRTTI;
  133. static RTTITypeBase* getRTTIStatic();
  134. RTTITypeBase* getRTTI() const override;
  135. };
  136. /** @} */
  137. /** @addtogroup RenderAPI-Internal
  138. * @{
  139. */
  140. /**
  141. * Core thread version of a GpuProgram.
  142. *
  143. * @note Core thread only.
  144. */
  145. class BS_CORE_EXPORT GpuProgramCore : public CoreObjectCore
  146. {
  147. public:
  148. virtual ~GpuProgramCore() { }
  149. /** Returns whether this program can be supported on the current renderer and hardware. */
  150. virtual bool isSupported() const;
  151. /** Returns true if program was successfully compiled. */
  152. virtual bool isCompiled() const { return mIsCompiled; }
  153. /** Returns an error message returned by the compiler, if the compilation failed. */
  154. virtual String getCompileErrorMessage() const { return mCompileError; }
  155. /**
  156. * Sets whether this geometry program requires adjacency information from the input primitives.
  157. *
  158. * @note Only relevant for geometry programs.
  159. */
  160. virtual void setAdjacencyInfoRequired(bool required) { mNeedsAdjacencyInfo = required; }
  161. /**
  162. * Returns whether this geometry program requires adjacency information from the input primitives.
  163. *
  164. * @note Only relevant for geometry programs.
  165. */
  166. virtual bool isAdjacencyInfoRequired() const { return mNeedsAdjacencyInfo; }
  167. /** @copydoc GpuProgram::createParameters */
  168. virtual SPtr<GpuParamsCore> createParameters();
  169. /** @copydoc GpuProgram::getParamDesc */
  170. SPtr<GpuParamDesc> getParamDesc() const { return mParametersDesc; }
  171. /** Returns GPU program input declaration. Only relevant for vertex programs. */
  172. SPtr<VertexDeclarationCore> getInputDeclaration() const { return mInputDeclaration; }
  173. /** Returns properties that contain information about the GPU program. */
  174. const GpuProgramProperties& getProperties() const { return mProperties; }
  175. /** @copydoc GpuProgram::create */
  176. static SPtr<GpuProgramCore> create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  177. GpuProgramProfile profile, bool requiresAdjacency = false);
  178. protected:
  179. GpuProgramCore(const String& source, const String& entryPoint,
  180. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired = false);
  181. /** Returns whether required capabilities for this program is supported. */
  182. bool isRequiredCapabilitiesSupported() const;
  183. bool mNeedsAdjacencyInfo;
  184. bool mIsCompiled;
  185. String mCompileError;
  186. SPtr<GpuParamDesc> mParametersDesc;
  187. SPtr<VertexDeclarationCore> mInputDeclaration;
  188. GpuProgramProperties mProperties;
  189. };
  190. /** @} */
  191. }
  192. namespace std
  193. {
  194. /** Hash value generator for GpuProgramProfile. */
  195. template<>
  196. struct hash<BansheeEngine::GpuProgramProfile>
  197. {
  198. size_t operator()(const BansheeEngine::GpuProgramProfile& profile) const
  199. {
  200. size_t hash = 0;
  201. BansheeEngine::hash_combine(hash, (int)profile);
  202. return hash;
  203. }
  204. };
  205. }