GrShaderCaps.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrShaderCaps_DEFINED
  8. #define GrShaderCaps_DEFINED
  9. #include "include/core/SkRefCnt.h"
  10. #include "include/private/GrTypesPriv.h"
  11. #include "src/gpu/GrSwizzle.h"
  12. #include "src/gpu/glsl/GrGLSL.h"
  13. namespace SkSL {
  14. class ShaderCapsFactory;
  15. }
  16. struct GrContextOptions;
  17. class SkJSONWriter;
  18. class GrShaderCaps : public SkRefCnt {
  19. public:
  20. /**
  21. * Indicates how GLSL must interact with advanced blend equations. The KHR extension requires
  22. * special layout qualifiers in the fragment shader.
  23. */
  24. enum AdvBlendEqInteraction {
  25. kNotSupported_AdvBlendEqInteraction, //<! No _blend_equation_advanced extension
  26. kAutomatic_AdvBlendEqInteraction, //<! No interaction required
  27. kGeneralEnable_AdvBlendEqInteraction, //<! layout(blend_support_all_equations) out
  28. kSpecificEnables_AdvBlendEqInteraction, //<! Specific layout qualifiers per equation
  29. kLast_AdvBlendEqInteraction = kSpecificEnables_AdvBlendEqInteraction
  30. };
  31. GrShaderCaps(const GrContextOptions&);
  32. void dumpJSON(SkJSONWriter*) const;
  33. bool supportsDistanceFieldText() const { return fShaderDerivativeSupport; }
  34. bool shaderDerivativeSupport() const { return fShaderDerivativeSupport; }
  35. bool geometryShaderSupport() const { return fGeometryShaderSupport; }
  36. bool gsInvocationsSupport() const { return fGSInvocationsSupport; }
  37. bool pathRenderingSupport() const { return fPathRenderingSupport; }
  38. bool dstReadInShaderSupport() const { return fDstReadInShaderSupport; }
  39. bool dualSourceBlendingSupport() const { return fDualSourceBlendingSupport; }
  40. bool integerSupport() const { return fIntegerSupport; }
  41. /**
  42. * Some helper functions for encapsulating various extensions to read FB Buffer on openglES
  43. *
  44. * TODO(joshualitt) On desktop opengl 4.2+ we can achieve something similar to this effect
  45. */
  46. bool fbFetchSupport() const { return fFBFetchSupport; }
  47. bool fbFetchNeedsCustomOutput() const { return fFBFetchNeedsCustomOutput; }
  48. const char* versionDeclString() const { return fVersionDeclString; }
  49. const char* fbFetchColorName() const { return fFBFetchColorName; }
  50. const char* fbFetchExtensionString() const { return fFBFetchExtensionString; }
  51. bool flatInterpolationSupport() const { return fFlatInterpolationSupport; }
  52. bool preferFlatInterpolation() const { return fPreferFlatInterpolation; }
  53. bool noperspectiveInterpolationSupport() const { return fNoPerspectiveInterpolationSupport; }
  54. // Can we use sample variables everywhere?
  55. bool sampleVariablesSupport() const { return fSampleVariablesSupport; }
  56. // Can we use sample variables when rendering to stencil? (This is a workaround for platforms
  57. // where sample variables are broken in general, but seem to work when rendering to stencil.)
  58. bool sampleVariablesStencilSupport() const { return fSampleVariablesStencilSupport; }
  59. bool externalTextureSupport() const { return fExternalTextureSupport; }
  60. bool vertexIDSupport() const { return fVertexIDSupport; }
  61. // frexp, ldexp, etc.
  62. bool fpManipulationSupport() const { return fFPManipulationSupport; }
  63. bool floatIs32Bits() const { return fFloatIs32Bits; }
  64. bool halfIs32Bits() const { return fHalfIs32Bits; }
  65. bool hasLowFragmentPrecision() const { return fHasLowFragmentPrecision; }
  66. // SkSL only.
  67. bool builtinFMASupport() const { return fBuiltinFMASupport; }
  68. AdvBlendEqInteraction advBlendEqInteraction() const { return fAdvBlendEqInteraction; }
  69. bool mustEnableAdvBlendEqs() const {
  70. return fAdvBlendEqInteraction >= kGeneralEnable_AdvBlendEqInteraction;
  71. }
  72. bool mustEnableSpecificAdvBlendEqs() const {
  73. return fAdvBlendEqInteraction == kSpecificEnables_AdvBlendEqInteraction;
  74. }
  75. bool mustDeclareFragmentShaderOutput() const { return fGLSLGeneration > k110_GrGLSLGeneration; }
  76. bool usesPrecisionModifiers() const { return fUsesPrecisionModifiers; }
  77. // Returns whether we can use the glsl function any() in our shader code.
  78. bool canUseAnyFunctionInShader() const { return fCanUseAnyFunctionInShader; }
  79. bool canUseMinAndAbsTogether() const { return fCanUseMinAndAbsTogether; }
  80. bool canUseFractForNegativeValues() const { return fCanUseFractForNegativeValues; }
  81. bool mustForceNegatedAtanParamToFloat() const { return fMustForceNegatedAtanParamToFloat; }
  82. // Returns whether a device incorrectly implements atan(y,x) as atan(y/x)
  83. bool atan2ImplementedAsAtanYOverX() const { return fAtan2ImplementedAsAtanYOverX; }
  84. // If this returns true some operation (could be a no op) must be called between floor and abs
  85. // to make sure the driver compiler doesn't inline them together which can cause a driver bug in
  86. // the shader.
  87. bool mustDoOpBetweenFloorAndAbs() const { return fMustDoOpBetweenFloorAndAbs; }
  88. // If false, SkSL uses a workaround so that sk_FragCoord doesn't actually query gl_FragCoord
  89. bool canUseFragCoord() const { return fCanUseFragCoord; }
  90. // If true, short ints can't represent every integer in the 16-bit two's complement range as
  91. // required by the spec. SKSL will always emit full ints.
  92. bool incompleteShortIntPrecision() const { return fIncompleteShortIntPrecision; }
  93. // If true, then conditions in for loops need "&& true" to work around driver bugs.
  94. bool addAndTrueToLoopCondition() const { return fAddAndTrueToLoopCondition; }
  95. // If true, then expressions such as "x && y" or "x || y" are rewritten as
  96. // ternary to work around driver bugs.
  97. bool unfoldShortCircuitAsTernary() const { return fUnfoldShortCircuitAsTernary; }
  98. bool emulateAbsIntFunction() const { return fEmulateAbsIntFunction; }
  99. bool rewriteDoWhileLoops() const { return fRewriteDoWhileLoops; }
  100. bool removePowWithConstantExponent() const { return fRemovePowWithConstantExponent; }
  101. bool requiresLocalOutputColorForFBFetch() const { return fRequiresLocalOutputColorForFBFetch; }
  102. bool mustObfuscateUniformColor() const { return fMustObfuscateUniformColor; }
  103. // The D3D shader compiler, when targeting PS 3.0 (ie within ANGLE) fails to compile certain
  104. // constructs. See detailed comments in GrGLCaps.cpp.
  105. bool mustGuardDivisionEvenAfterExplicitZeroCheck() const {
  106. return fMustGuardDivisionEvenAfterExplicitZeroCheck;
  107. }
  108. // On Nexus 6, the GL context can get lost if a shader does not write a value to gl_FragColor.
  109. // https://bugs.chromium.org/p/chromium/issues/detail?id=445377
  110. bool mustWriteToFragColor() const { return fMustWriteToFragColor; }
  111. // Returns the string of an extension that must be enabled in the shader to support
  112. // derivatives. If nullptr is returned then no extension needs to be enabled. Before calling
  113. // this function, the caller should check that shaderDerivativeSupport exists.
  114. const char* shaderDerivativeExtensionString() const {
  115. SkASSERT(this->shaderDerivativeSupport());
  116. return fShaderDerivativeExtensionString;
  117. }
  118. // Returns the string of an extension that must be enabled in the shader to support geometry
  119. // shaders. If nullptr is returned then no extension needs to be enabled. Before calling this
  120. // function, the caller must verify that geometryShaderSupport exists.
  121. const char* geometryShaderExtensionString() const {
  122. SkASSERT(this->geometryShaderSupport());
  123. return fGeometryShaderExtensionString;
  124. }
  125. // Returns the string of an extension that must be enabled in the shader to support
  126. // geometry shader invocations. If nullptr is returned then no extension needs to be enabled.
  127. // Before calling this function, the caller must verify that gsInvocationsSupport exists.
  128. const char* gsInvocationsExtensionString() const {
  129. SkASSERT(this->gsInvocationsSupport());
  130. return fGSInvocationsExtensionString;
  131. }
  132. // Returns the string of an extension that will do all necessary coord transfomations needed
  133. // when reading the fragment position. If such an extension does not exisits, this function
  134. // returns a nullptr, and all transforms of the frag position must be done manually in the
  135. // shader.
  136. const char* fragCoordConventionsExtensionString() const {
  137. return fFragCoordConventionsExtensionString;
  138. }
  139. // This returns the name of an extension that must be enabled in the shader, if such a thing is
  140. // required in order to use a secondary output in the shader. This returns a nullptr if no such
  141. // extension is required. However, the return value of this function does not say whether dual
  142. // source blending is supported.
  143. const char* secondaryOutputExtensionString() const { return fSecondaryOutputExtensionString; }
  144. // This returns the name of an extension that must be enabled in the shader to support external
  145. // textures. In some cases, two extensions must be enabled - the second extension is returned
  146. // by secondExternalTextureExtensionString(). If that function returns nullptr, then only one
  147. // extension is required.
  148. const char* externalTextureExtensionString() const {
  149. SkASSERT(this->externalTextureSupport());
  150. return fExternalTextureExtensionString;
  151. }
  152. const char* secondExternalTextureExtensionString() const {
  153. SkASSERT(this->externalTextureSupport());
  154. return fSecondExternalTextureExtensionString;
  155. }
  156. const char* noperspectiveInterpolationExtensionString() const {
  157. SkASSERT(this->noperspectiveInterpolationSupport());
  158. return fNoPerspectiveInterpolationExtensionString;
  159. }
  160. const char* sampleVariablesExtensionString() const {
  161. SkASSERT(this->sampleVariablesSupport() || this->sampleVariablesStencilSupport());
  162. return fSampleVariablesExtensionString;
  163. }
  164. int maxFragmentSamplers() const { return fMaxFragmentSamplers; }
  165. bool textureSwizzleAppliedInShader() const { return fTextureSwizzleAppliedInShader; }
  166. GrGLSLGeneration generation() const { return fGLSLGeneration; }
  167. private:
  168. void applyOptionsOverrides(const GrContextOptions& options);
  169. GrGLSLGeneration fGLSLGeneration;
  170. bool fShaderDerivativeSupport : 1;
  171. bool fGeometryShaderSupport : 1;
  172. bool fGSInvocationsSupport : 1;
  173. bool fPathRenderingSupport : 1;
  174. bool fDstReadInShaderSupport : 1;
  175. bool fDualSourceBlendingSupport : 1;
  176. bool fIntegerSupport : 1;
  177. bool fFBFetchSupport : 1;
  178. bool fFBFetchNeedsCustomOutput : 1;
  179. bool fUsesPrecisionModifiers : 1;
  180. bool fFlatInterpolationSupport : 1;
  181. bool fPreferFlatInterpolation : 1;
  182. bool fNoPerspectiveInterpolationSupport : 1;
  183. bool fSampleVariablesSupport : 1;
  184. bool fSampleVariablesStencilSupport : 1;
  185. bool fExternalTextureSupport : 1;
  186. bool fVertexIDSupport : 1;
  187. bool fFPManipulationSupport : 1;
  188. bool fFloatIs32Bits : 1;
  189. bool fHalfIs32Bits : 1;
  190. bool fHasLowFragmentPrecision : 1;
  191. bool fTextureSwizzleAppliedInShader : 1;
  192. // Used by SkSL to know when to generate polyfills.
  193. bool fBuiltinFMASupport : 1;
  194. // Used for specific driver bug work arounds
  195. bool fCanUseAnyFunctionInShader : 1;
  196. bool fCanUseMinAndAbsTogether : 1;
  197. bool fCanUseFractForNegativeValues : 1;
  198. bool fMustForceNegatedAtanParamToFloat : 1;
  199. bool fAtan2ImplementedAsAtanYOverX : 1;
  200. bool fMustDoOpBetweenFloorAndAbs : 1;
  201. bool fRequiresLocalOutputColorForFBFetch : 1;
  202. bool fMustObfuscateUniformColor : 1;
  203. bool fMustGuardDivisionEvenAfterExplicitZeroCheck : 1;
  204. bool fCanUseFragCoord : 1;
  205. bool fIncompleteShortIntPrecision : 1;
  206. bool fAddAndTrueToLoopCondition : 1;
  207. bool fUnfoldShortCircuitAsTernary : 1;
  208. bool fEmulateAbsIntFunction : 1;
  209. bool fRewriteDoWhileLoops : 1;
  210. bool fRemovePowWithConstantExponent : 1;
  211. bool fMustWriteToFragColor : 1;
  212. const char* fVersionDeclString;
  213. const char* fShaderDerivativeExtensionString;
  214. const char* fGeometryShaderExtensionString;
  215. const char* fGSInvocationsExtensionString;
  216. const char* fFragCoordConventionsExtensionString;
  217. const char* fSecondaryOutputExtensionString;
  218. const char* fExternalTextureExtensionString;
  219. const char* fSecondExternalTextureExtensionString;
  220. const char* fNoPerspectiveInterpolationExtensionString;
  221. const char* fSampleVariablesExtensionString;
  222. const char* fFBFetchColorName;
  223. const char* fFBFetchExtensionString;
  224. int fMaxFragmentSamplers;
  225. AdvBlendEqInteraction fAdvBlendEqInteraction;
  226. friend class GrCaps; // For initialization.
  227. friend class GrGLCaps;
  228. friend class GrMockCaps;
  229. friend class GrMtlCaps;
  230. friend class GrVkCaps;
  231. friend class SkSL::ShaderCapsFactory;
  232. };
  233. #endif