BsGpuProgram.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Returns true if matrices need to be transposed before sent to the GPU
  113. * program as GPU program parameters.
  114. */
  115. virtual bool requiresMatrixTranspose() const { return false; }
  116. /**
  117. * @brief Creates a new parameters object compatible with this program definition. You
  118. * may populate the returned object with actual parameter values and bind it
  119. * to the pipeline to render an object using those values and this program.
  120. */
  121. virtual GpuParamsPtr createParameters();
  122. /**
  123. * @brief Returns description of all parameters in this GPU program.
  124. */
  125. GpuParamDescPtr getParamDesc() const { return mParametersDesc; }
  126. /**
  127. * @brief Language this shader was created from (e.g. HLSL, GLSL).
  128. */
  129. virtual const String& getLanguage() const;
  130. /**
  131. * @brief Creates a new GPU program using the provided source code. If compilation fails or program is not supported
  132. * "isCompiled" with return false, and you will be able to retrieve the error message via "getCompileErrorMessage".
  133. *
  134. * @param source Source code to compile the shader from.
  135. * @param entryPoint Name of the entry point function, e.g. "main".
  136. * @param language Language the source is written in, e.g. "hlsl" or "glsl".
  137. * @param gptype Type of the program, e.g. vertex or fragment.
  138. * @param profile Program profile specifying supported feature-set. Must match the type.
  139. * @param includes Optional includes to append to the source before compiling.
  140. * @param requiresAdjacency If true then adjacency information will be provided when rendering using this program.
  141. */
  142. static HGpuProgram create(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  143. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes = nullptr, bool requiresAdjacency = false);
  144. /**
  145. * @copydoc create
  146. *
  147. * @note Internal method. For normal use call "create".
  148. */
  149. static GpuProgramPtr _createPtr(const String& source, const String& entryPoint, const String& language, GpuProgramType gptype,
  150. GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes = nullptr, bool requiresAdjacency = false);
  151. protected:
  152. friend class GpuProgramManager;
  153. GpuProgram(const String& source, const String& entryPoint,
  154. GpuProgramType gptype, GpuProgramProfile profile, const Vector<HGpuProgInclude>* includes,
  155. bool isAdjacencyInfoRequired = false);
  156. /**
  157. * @brief Returns whether required capabilities for this program is supported.
  158. */
  159. bool isRequiredCapabilitiesSupported() const;
  160. /**
  161. * @copydoc Resource::calculateSize
  162. */
  163. size_t calculateSize() const { return 0; } // TODO
  164. protected:
  165. GpuProgramType mType;
  166. bool mNeedsAdjacencyInfo;
  167. String mEntryPoint;
  168. GpuProgramProfile mProfile;
  169. String mSource;
  170. bool mIsCompiled;
  171. String mCompileError;
  172. GpuParamDescPtr mParametersDesc;
  173. /************************************************************************/
  174. /* SERIALIZATION */
  175. /************************************************************************/
  176. public:
  177. friend class GpuProgramRTTI;
  178. static RTTITypeBase* getRTTIStatic();
  179. virtual RTTITypeBase* getRTTI() const;
  180. };
  181. }