BsGpuProgram.h 7.5 KB

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