BsGpuProgram.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsDrawOps.h"
  4. #include "BsResource.h"
  5. #include "BsGpuParamDesc.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Types of programs that may run on GPU.
  10. */
  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. /**
  21. * @brief GPU program profiles representing supported
  22. * feature sets.
  23. */
  24. enum GpuProgramProfile
  25. {
  26. GPP_NONE,
  27. GPP_PS_1_1,
  28. GPP_PS_1_2,
  29. GPP_PS_1_3,
  30. GPP_PS_1_4,
  31. GPP_PS_2_0,
  32. GPP_PS_2_x,
  33. GPP_PS_2_a,
  34. GPP_PS_2_b,
  35. GPP_PS_3_0,
  36. GPP_PS_3_x,
  37. GPP_PS_4_0,
  38. GPP_PS_4_1,
  39. GPP_PS_5_0,
  40. GPP_VS_1_1,
  41. GPP_VS_2_0,
  42. GPP_VS_2_x,
  43. GPP_VS_2_a,
  44. GPP_VS_3_0,
  45. GPP_VS_4_0,
  46. GPP_VS_4_1,
  47. GPP_VS_5_0,
  48. GPP_GS_4_0,
  49. GPP_GS_4_1,
  50. GPP_GS_5_0,
  51. GPP_HS_5_0,
  52. GPP_DS_5_0,
  53. GPP_CS_5_0
  54. };
  55. /**
  56. * @brief Contains a GPU program such as vertex or fragment program which gets
  57. * compiled from the provided source code.
  58. *
  59. * @note Core thread only.
  60. */
  61. class BS_CORE_EXPORT GpuProgram : public Resource
  62. {
  63. public:
  64. virtual ~GpuProgram();
  65. /**
  66. * @brief Source used for creating this program.
  67. */
  68. virtual const String& getSource() const { return mSource; }
  69. /**
  70. * @brief Type of GPU program (e.g. fragment, vertex)
  71. */
  72. virtual GpuProgramType getType() const { return mType; }
  73. /**
  74. * @brief Profile of the GPU program (e.g. VS_4_0, VS_5_0)
  75. */
  76. virtual GpuProgramProfile getProfile() const { return mProfile; }
  77. /**
  78. * @brief Name of the program entry method (e.g. "main")
  79. */
  80. virtual const String& getEntryPoint() const { return mEntryPoint; }
  81. /**
  82. * @brief Returns whether this program can be supported on the current renderer and hardware.
  83. */
  84. virtual bool isSupported() const;
  85. /**
  86. * @brief Returns true if shader was successfully compiled.
  87. *
  88. * @note Thread safe. Only valid after core thread has initialized the program.
  89. */
  90. virtual bool isCompiled() const { return mIsCompiled; }
  91. /**
  92. * @brief Returns an error message returned by the compiler, if the compilation failed.
  93. *
  94. * @note Thread safe. Only valid after core thread has initialized the program.
  95. */
  96. virtual String getCompileErrorMessage() const { return mCompileError; }
  97. /**
  98. * @brief Sets whether this geometry program requires adjacency information
  99. * from the input primitives.
  100. *
  101. * @note Only relevant for geometry programs.
  102. */
  103. virtual void setAdjacencyInfoRequired(bool required) { mNeedsAdjacencyInfo = required; }
  104. /**
  105. * @brief Returns whether this geometry program requires adjacency information
  106. * from the input primitives.
  107. *
  108. * @note Only relevant for geometry programs.
  109. */
  110. virtual bool isAdjacencyInfoRequired() const { return mNeedsAdjacencyInfo; }
  111. /**
  112. * @brief Creates a new parameters object compatible with this program definition. You
  113. * may populate the returned object with actual parameter values and bind it
  114. * to the pipeline to render an object using those values and this program.
  115. */
  116. virtual GpuParamsPtr createParameters();
  117. /**
  118. * @brief Returns description of all parameters in this GPU program.
  119. */
  120. const GpuParamDesc& getParamDesc() const { return mParametersDesc; }
  121. /**
  122. * @brief Language this shader was created from (e.g. HLSL, GLSL).
  123. */
  124. virtual const String& getLanguage() const;
  125. /**
  126. * @brief Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  127. * "isCompiled" with return false, and you will be able to retrieve the error message via "getCompileErrorMessage".
  128. *
  129. * @param source Source code to compile the shader from.
  130. * @param entryPoint Name of the entry point function, e.g. "main".
  131. * @param language Language the source is written in, e.g. "hlsl" or "glsl".
  132. * @param gptype Type of the program, e.g. vertex or fragment.
  133. * @param profile Program profile specifying supported feature-set. Must match the type.
  134. * @param includes Optional includes to append to the source before compiling.
  135. * @param requiresAdjacency If true then adjacency information will be provided when rendering using this program.
  136. */
  137. static HGpuProgram create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  138. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes = nullptr, bool requiresAdjacency = false);
  139. /**
  140. * @copydoc create
  141. *
  142. * @note Internal method. For normal use call "create".
  143. */
  144. static GpuProgramPtr _createPtr(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  145. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes = nullptr, bool requiresAdjacency = false);
  146. protected:
  147. friend class GpuProgramManager;
  148. GpuProgram(const String& source, const String& entryPoint,
  149. GpuProgramType gptype, GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes,
  150. bool isAdjacencyInfoRequired = false);
  151. /**
  152. * @brief Returns whether required capabilities for this program is supported.
  153. */
  154. bool isRequiredCapabilitiesSupported() const;
  155. /**
  156. * @copydoc Resource::calculateSize
  157. */
  158. size_t calculateSize() const { return 0; } // TODO
  159. protected:
  160. GpuProgramType mType;
  161. bool mNeedsAdjacencyInfo;
  162. String mEntryPoint;
  163. GpuProgramProfile mProfile;
  164. String mSource;
  165. bool mIsCompiled;
  166. String mCompileError;
  167. GpuParamDesc mParametersDesc;
  168. /************************************************************************/
  169. /* SERIALIZATION */
  170. /************************************************************************/
  171. public:
  172. friend class GpuProgramRTTI;
  173. static RTTITypeBase* getRTTIStatic();
  174. virtual RTTITypeBase* getRTTI() const;
  175. };
  176. }