BsGpuProgram.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "CoreThread/BsCoreObject.h"
  6. #include "Reflection/BsIReflectable.h"
  7. namespace bs
  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. bool requiresAdjacency = false; /**< If true then adjacency information will be provided when rendering. */
  52. };
  53. /** Data describing a GpuProgram. */
  54. class BS_CORE_EXPORT GpuProgramProperties
  55. {
  56. public:
  57. GpuProgramProperties(const String& source, const String& entryPoint, GpuProgramType gptype);
  58. virtual ~GpuProgramProperties() { }
  59. /** Source used for creating this program. */
  60. const String& getSource() const { return mSource; }
  61. /** Type of GPU program (for example fragment, vertex). */
  62. GpuProgramType getType() const { return mType; }
  63. /** Name of the program entry method (for example "main"). */
  64. const String& getEntryPoint() const { return mEntryPoint; }
  65. protected:
  66. friend class GpuProgramRTTI;
  67. GpuProgramType mType;
  68. String mEntryPoint;
  69. String mSource;
  70. };
  71. /**
  72. * Contains a GPU program such as vertex or fragment program which gets compiled from the provided source code.
  73. *
  74. * @note Sim thread only.
  75. */
  76. class BS_CORE_EXPORT GpuProgram : public IReflectable, public CoreObject
  77. {
  78. public:
  79. virtual ~GpuProgram() { }
  80. /**
  81. * Returns true if the program was successfully compiled.
  82. *
  83. * @note Only valid after core thread has initialized the program.
  84. */
  85. bool isCompiled() const;
  86. /**
  87. * Returns an error message returned by the compiler, if the compilation failed.
  88. *
  89. * @note Only valid after core thread has initialized the program.
  90. */
  91. String getCompileErrorMessage() const;
  92. /**
  93. * Returns description of all parameters in this GPU program.
  94. *
  95. * @note Only valid after core thread has initialized the program.
  96. */
  97. SPtr<GpuParamDesc> getParamDesc() const;
  98. /** Retrieves a core implementation of a gpu program usable only from the core thread. */
  99. SPtr<ct::GpuProgram> getCore() const;
  100. /** Returns properties that contain information about the GPU program. */
  101. const GpuProgramProperties& getProperties() const { return mProperties; }
  102. /**
  103. * Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  104. * isCompiled() with return false, and you will be able to retrieve the error message via getCompileErrorMessage().
  105. *
  106. * @param[in] desc Description of the program to create.
  107. */
  108. static SPtr<GpuProgram> create(const GPU_PROGRAM_DESC& desc);
  109. protected:
  110. friend class GpuProgramManager;
  111. GpuProgram(const GPU_PROGRAM_DESC& desc);
  112. /** @copydoc CoreObject::createCore */
  113. SPtr<ct::CoreObject> createCore() const override;
  114. protected:
  115. bool mNeedsAdjacencyInfo;
  116. String mLanguage;
  117. GpuProgramProperties mProperties;
  118. /************************************************************************/
  119. /* SERIALIZATION */
  120. /************************************************************************/
  121. public:
  122. friend class GpuProgramRTTI;
  123. static RTTITypeBase* getRTTIStatic();
  124. RTTITypeBase* getRTTI() const override;
  125. };
  126. /** @} */
  127. namespace ct
  128. {
  129. /** @addtogroup RenderAPI-Internal
  130. * @{
  131. */
  132. /**
  133. * Core thread version of a bs::GpuProgram.
  134. *
  135. * @note Core thread only.
  136. */
  137. class BS_CORE_EXPORT GpuProgram : public CoreObject
  138. {
  139. public:
  140. virtual ~GpuProgram() { }
  141. /** Returns whether this program can be supported on the current renderer and hardware. */
  142. virtual bool isSupported() const;
  143. /** Returns true if program was successfully compiled. */
  144. virtual bool isCompiled() const { return mIsCompiled; }
  145. /** Returns an error message returned by the compiler, if the compilation failed. */
  146. virtual String getCompileErrorMessage() const { return mCompileError; }
  147. /**
  148. * Sets whether this geometry program requires adjacency information from the input primitives.
  149. *
  150. * @note Only relevant for geometry programs.
  151. */
  152. virtual void setAdjacencyInfoRequired(bool required) { mNeedsAdjacencyInfo = required; }
  153. /**
  154. * Returns whether this geometry program requires adjacency information from the input primitives.
  155. *
  156. * @note Only relevant for geometry programs.
  157. */
  158. virtual bool isAdjacencyInfoRequired() const { return mNeedsAdjacencyInfo; }
  159. /** @copydoc bs::GpuProgram::getParamDesc */
  160. SPtr<GpuParamDesc> getParamDesc() const { return mParametersDesc; }
  161. /** Returns GPU program input declaration. Only relevant for vertex programs. */
  162. SPtr<VertexDeclaration> getInputDeclaration() const { return mInputDeclaration; }
  163. /** Returns properties that contain information about the GPU program. */
  164. const GpuProgramProperties& getProperties() const { return mProperties; }
  165. /**
  166. * @copydoc bs::GpuProgram::create
  167. * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
  168. */
  169. static SPtr<GpuProgram> create(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask = GDF_DEFAULT);
  170. protected:
  171. GpuProgram(const GPU_PROGRAM_DESC& desc, GpuDeviceFlags deviceMask);
  172. /** Returns whether required capabilities for this program is supported. */
  173. bool isRequiredCapabilitiesSupported() const;
  174. bool mNeedsAdjacencyInfo;
  175. bool mIsCompiled;
  176. String mCompileError;
  177. SPtr<GpuParamDesc> mParametersDesc;
  178. SPtr<VertexDeclaration> mInputDeclaration;
  179. GpuProgramProperties mProperties;
  180. };
  181. /** @} */
  182. }
  183. }