BsGpuProgram.h 7.7 KB

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