BsGpuProgram.h 6.3 KB

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