BsGpuProgram.h 7.5 KB

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