CmGpuProgram.h 6.0 KB

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