GrGLSLProgramBuilder.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2015 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 GrGLSLProgramBuilder_DEFINED
  8. #define GrGLSLProgramBuilder_DEFINED
  9. #include "include/gpu/GrRenderTarget.h"
  10. #include "src/gpu/GrCaps.h"
  11. #include "src/gpu/GrGeometryProcessor.h"
  12. #include "src/gpu/GrProgramDesc.h"
  13. #include "src/gpu/GrRenderTargetPriv.h"
  14. #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
  15. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  16. #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
  17. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  18. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  19. #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
  20. #include "src/gpu/glsl/GrGLSLXferProcessor.h"
  21. class GrShaderVar;
  22. class GrGLSLVaryingHandler;
  23. class SkString;
  24. class GrShaderCaps;
  25. class GrGLSLProgramBuilder {
  26. public:
  27. using UniformHandle = GrGLSLUniformHandler::UniformHandle;
  28. using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
  29. virtual ~GrGLSLProgramBuilder() {}
  30. virtual const GrCaps* caps() const = 0;
  31. const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
  32. const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
  33. const GrTextureProxy* const* primProcProxies() const { return fPrimProcProxies; }
  34. const GrRenderTarget* renderTarget() const { return fRenderTarget; }
  35. GrPixelConfig config() const { return fRenderTarget->config(); }
  36. int effectiveSampleCnt() const {
  37. SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & header().processorFeatures());
  38. return fRenderTarget->renderTargetPriv().getSampleLocations().count();
  39. }
  40. GrSurfaceOrigin origin() const { return fOrigin; }
  41. const GrPipeline& pipeline() const { return fPipeline; }
  42. GrProgramDesc* desc() { return fDesc; }
  43. const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
  44. void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
  45. const char* samplerVariable(SamplerHandle handle) const {
  46. return this->uniformHandler()->samplerVariable(handle);
  47. }
  48. GrSwizzle samplerSwizzle(SamplerHandle handle) const {
  49. if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
  50. return this->uniformHandler()->samplerSwizzle(handle);
  51. }
  52. return GrSwizzle::RGBA();
  53. }
  54. // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
  55. // the name of the uniform inside of a stage.
  56. void addRTWidthUniform(const char* name);
  57. // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
  58. // without mangling the name of the uniform inside of a stage.
  59. void addRTHeightUniform(const char* name);
  60. // Generates a name for a variable. The generated string will be name prefixed by the prefix
  61. // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
  62. // explicitly asked not to.
  63. void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
  64. virtual GrGLSLUniformHandler* uniformHandler() = 0;
  65. virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
  66. virtual GrGLSLVaryingHandler* varyingHandler() = 0;
  67. // Used for backend customization of the output color and secondary color variables from the
  68. // fragment processor. Only used if the outputs are explicitly declared in the shaders
  69. virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
  70. virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
  71. // number of each input/output type in a single allocation block, used by many builders
  72. static const int kVarsPerBlock;
  73. GrGLSLVertexBuilder fVS;
  74. GrGLSLGeometryBuilder fGS;
  75. GrGLSLFragmentShaderBuilder fFS;
  76. int fStageIndex;
  77. const GrRenderTarget* fRenderTarget;
  78. const GrSurfaceOrigin fOrigin;
  79. const GrPipeline& fPipeline;
  80. const GrPrimitiveProcessor& fPrimProc;
  81. const GrTextureProxy* const* fPrimProcProxies;
  82. GrProgramDesc* fDesc;
  83. GrGLSLBuiltinUniformHandles fUniformHandles;
  84. std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
  85. std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
  86. std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
  87. int fFragmentProcessorCnt;
  88. protected:
  89. explicit GrGLSLProgramBuilder(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
  90. const GrPrimitiveProcessor&,
  91. const GrTextureProxy* const primProcProxies[],
  92. const GrPipeline&,
  93. GrProgramDesc*);
  94. void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
  95. bool emitAndInstallProcs();
  96. void finalizeShaders();
  97. bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
  98. private:
  99. // reset is called by program creator between each processor's emit code. It increments the
  100. // stage offset for variable name mangling, and also ensures verfication variables in the
  101. // fragment shader are cleared.
  102. void reset() {
  103. this->addStage();
  104. SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();)
  105. }
  106. void addStage() { fStageIndex++; }
  107. class AutoStageAdvance {
  108. public:
  109. AutoStageAdvance(GrGLSLProgramBuilder* pb)
  110. : fPB(pb) {
  111. fPB->reset();
  112. // Each output to the fragment processor gets its own code section
  113. fPB->fFS.nextStage();
  114. }
  115. ~AutoStageAdvance() {}
  116. private:
  117. GrGLSLProgramBuilder* fPB;
  118. };
  119. // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
  120. void nameExpression(SkString*, const char* baseName);
  121. void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage);
  122. void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
  123. SkString emitAndInstallFragProc(const GrFragmentProcessor&,
  124. int index,
  125. int transformedCoordVarsIdx,
  126. const SkString& input,
  127. SkString output,
  128. SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
  129. void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
  130. SamplerHandle emitSampler(const GrTexture*, const GrSamplerState&, const GrSwizzle&,
  131. const char* name);
  132. bool checkSamplerCounts();
  133. #ifdef SK_DEBUG
  134. void verify(const GrPrimitiveProcessor&);
  135. void verify(const GrFragmentProcessor&);
  136. void verify(const GrXferProcessor&);
  137. #endif
  138. // These are used to check that we don't excede the allowable number of resources in a shader.
  139. int fNumFragmentSamplers;
  140. SkSTArray<4, GrShaderVar> fTransformedCoordVars;
  141. };
  142. #endif