BsGpuProgram.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /** Descriptor structure used for initialization of a GpuProgram. */
  45. struct GPU_PROGRAM_DESC
  46. {
  47. String source; /**< Source code to compile the program from. */
  48. String entryPoint; /**< Name of the entry point function, for example "main". */
  49. String language; /**< Language the source is written in, for example "hlsl" or "glsl". */
  50. GpuProgramType type = GPT_VERTEX_PROGRAM; /**< Type of the program, for example vertex or fragment. */
  51. GpuProgramProfile profile = GPP_NONE; /**< Program profile specifying supported feature-set. Must match the type. */
  52. bool requiresAdjacency = false; /**< If true then adjacency information will be provided when rendering. */
  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 the program 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. * Returns description of all parameters in this GPU program.
  99. *
  100. * @note Only valid after core thread has initialized the program.
  101. */
  102. SPtr<GpuParamDesc> getParamDesc() const;
  103. /** Retrieves a core implementation of a gpu program usable only from the core thread. */
  104. SPtr<GpuProgramCore> getCore() const;
  105. /** Returns properties that contain information about the GPU program. */
  106. const GpuProgramProperties& getProperties() const { return mProperties; }
  107. /**
  108. * Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  109. * isCompiled() with return false, and you will be able to retrieve the error message via getCompileErrorMessage().
  110. *
  111. * @param[in] desc Description of the program to create.
  112. */
  113. static SPtr<GpuProgram> create(const GPU_PROGRAM_DESC& desc);
  114. protected:
  115. friend class GpuProgramManager;
  116. GpuProgram(const GPU_PROGRAM_DESC& desc);
  117. /** @copydoc CoreObject::createCore */
  118. SPtr<CoreObjectCore> createCore() const override;
  119. protected:
  120. bool mNeedsAdjacencyInfo;
  121. String mLanguage;
  122. GpuProgramProperties mProperties;
  123. /************************************************************************/
  124. /* SERIALIZATION */
  125. /************************************************************************/
  126. public:
  127. friend class GpuProgramRTTI;
  128. static RTTITypeBase* getRTTIStatic();
  129. RTTITypeBase* getRTTI() const override;
  130. };
  131. /** @} */
  132. /** @addtogroup RenderAPI-Internal
  133. * @{
  134. */
  135. /**
  136. * Core thread version of a GpuProgram.
  137. *
  138. * @note Core thread only.
  139. */
  140. class BS_CORE_EXPORT GpuProgramCore : public CoreObjectCore
  141. {
  142. public:
  143. virtual ~GpuProgramCore() { }
  144. /** Returns whether this program can be supported on the current renderer and hardware. */
  145. virtual bool isSupported() const;
  146. /** Returns true if program was successfully compiled. */
  147. virtual bool isCompiled() const { return mIsCompiled; }
  148. /** Returns an error message returned by the compiler, if the compilation failed. */
  149. virtual String getCompileErrorMessage() const { return mCompileError; }
  150. /**
  151. * Sets whether this geometry program requires adjacency information from the input primitives.
  152. *
  153. * @note Only relevant for geometry programs.
  154. */
  155. virtual void setAdjacencyInfoRequired(bool required) { mNeedsAdjacencyInfo = required; }
  156. /**
  157. * Returns whether this geometry program requires adjacency information from the input primitives.
  158. *
  159. * @note Only relevant for geometry programs.
  160. */
  161. virtual bool isAdjacencyInfoRequired() const { return mNeedsAdjacencyInfo; }
  162. /** @copydoc GpuProgram::getParamDesc */
  163. SPtr<GpuParamDesc> getParamDesc() const { return mParametersDesc; }
  164. /** Returns GPU program input declaration. Only relevant for vertex programs. */
  165. SPtr<VertexDeclarationCore> getInputDeclaration() const { return mInputDeclaration; }
  166. /** Returns properties that contain information about the GPU program. */
  167. const GpuProgramProperties& getProperties() const { return mProperties; }
  168. /**
  169. * @copydoc GpuProgram::create
  170. * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
  171. */
  172. static SPtr<GpuProgramCore> create(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask = GDF_DEFAULT);
  173. protected:
  174. GpuProgramCore(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask);
  175. /** Returns whether required capabilities for this program is supported. */
  176. bool isRequiredCapabilitiesSupported() const;
  177. bool mNeedsAdjacencyInfo;
  178. bool mIsCompiled;
  179. String mCompileError;
  180. SPtr<GpuParamDesc> mParametersDesc;
  181. SPtr<VertexDeclarationCore> mInputDeclaration;
  182. GpuProgramProperties mProperties;
  183. };
  184. /** @} */
  185. }