egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 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 | |
| 8 | #ifndef GrGLSLProgramBuilder_DEFINED |
| 9 | #define GrGLSLProgramBuilder_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrCaps.h" |
| 12 | #include "src/gpu/GrGeometryProcessor.h" |
| 13 | #include "src/gpu/GrProgramDesc.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrRenderTarget.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrRenderTargetPriv.h" |
| 16 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 18 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
| 19 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 20 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 21 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
| 22 | #include "src/gpu/glsl/GrGLSLXferProcessor.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 23 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 24 | class GrShaderVar; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 25 | class GrGLSLVaryingHandler; |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 26 | class SkString; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 27 | class GrShaderCaps; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 28 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 29 | class GrGLSLProgramBuilder { |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 30 | public: |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 31 | using UniformHandle = GrGLSLUniformHandler::UniformHandle; |
| 32 | using SamplerHandle = GrGLSLUniformHandler::SamplerHandle; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 33 | |
| 34 | virtual ~GrGLSLProgramBuilder() {} |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 35 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 36 | virtual const GrCaps* caps() const = 0; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 37 | const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); } |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 38 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 39 | const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; } |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 40 | const GrTextureProxy* const* primProcProxies() const { return fPrimProcProxies; } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 41 | const GrRenderTarget* renderTarget() const { return fRenderTarget; } |
| 42 | GrPixelConfig config() const { return fRenderTarget->config(); } |
| 43 | int effectiveSampleCnt() const { |
| 44 | SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & header().processorFeatures()); |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 45 | return fRenderTarget->renderTargetPriv().getSampleLocations().count(); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 46 | } |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 47 | GrSurfaceOrigin origin() const { return fOrigin; } |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 48 | const GrPipeline& pipeline() const { return fPipeline; } |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 49 | GrProgramDesc* desc() { return fDesc; } |
| 50 | const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); } |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 51 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 52 | void appendUniformDecls(GrShaderFlags visibility, SkString*) const; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 53 | |
Stephen White | d523a06 | 2019-06-19 13:12:46 -0400 | [diff] [blame] | 54 | const char* samplerVariable(SamplerHandle handle) const { |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 55 | return this->uniformHandler()->samplerVariable(handle); |
| 56 | } |
| 57 | |
| 58 | GrSwizzle samplerSwizzle(SamplerHandle handle) const { |
Brian Salomon | 68ba117 | 2019-06-05 11:15:08 -0400 | [diff] [blame] | 59 | if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) { |
| 60 | return this->uniformHandler()->samplerSwizzle(handle); |
| 61 | } |
| 62 | return GrSwizzle::RGBA(); |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 63 | } |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 64 | |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 65 | // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 66 | // the name of the uniform inside of a stage. |
Ethan Nicholas | cd700e9 | 2018-08-24 16:43:57 -0400 | [diff] [blame] | 67 | void addRTWidthUniform(const char* name); |
| 68 | |
| 69 | // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position) |
| 70 | // without mangling the name of the uniform inside of a stage. |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 71 | void addRTHeightUniform(const char* name); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 72 | |
| 73 | // Generates a name for a variable. The generated string will be name prefixed by the prefix |
| 74 | // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless |
| 75 | // explicitly asked not to. |
| 76 | void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true); |
| 77 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 78 | virtual GrGLSLUniformHandler* uniformHandler() = 0; |
| 79 | virtual const GrGLSLUniformHandler* uniformHandler() const = 0; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 80 | virtual GrGLSLVaryingHandler* varyingHandler() = 0; |
| 81 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 82 | // Used for backend customization of the output color and secondary color variables from the |
| 83 | // fragment processor. Only used if the outputs are explicitly declared in the shaders |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 84 | virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {} |
| 85 | virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {} |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 86 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 87 | // number of each input/output type in a single allocation block, used by many builders |
| 88 | static const int kVarsPerBlock; |
| 89 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 90 | GrGLSLVertexBuilder fVS; |
| 91 | GrGLSLGeometryBuilder fGS; |
| 92 | GrGLSLFragmentShaderBuilder fFS; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 93 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 94 | int fStageIndex; |
| 95 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 96 | const GrRenderTarget* fRenderTarget; |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 97 | const GrSurfaceOrigin fOrigin; |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 98 | const GrPipeline& fPipeline; |
| 99 | const GrPrimitiveProcessor& fPrimProc; |
| 100 | const GrTextureProxy* const* fPrimProcProxies; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 101 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 102 | GrProgramDesc* fDesc; |
| 103 | |
| 104 | GrGLSLBuiltinUniformHandles fUniformHandles; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 105 | |
Robert Phillips | 369e8b7 | 2017-08-01 16:13:04 -0400 | [diff] [blame] | 106 | std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor; |
| 107 | std::unique_ptr<GrGLSLXferProcessor> fXferProcessor; |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 108 | std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors; |
| 109 | int fFragmentProcessorCnt; |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 110 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 111 | protected: |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 112 | explicit GrGLSLProgramBuilder(GrRenderTarget* renderTarget, GrSurfaceOrigin origin, |
| 113 | const GrPrimitiveProcessor&, |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 114 | const GrTextureProxy* const primProcProxies[], |
| 115 | const GrPipeline&, |
| 116 | GrProgramDesc*); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 117 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 118 | void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName); |
| 119 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 120 | bool emitAndInstallProcs(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 121 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 122 | void finalizeShaders(); |
| 123 | |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 124 | bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); } |
| 125 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 126 | private: |
| 127 | // reset is called by program creator between each processor's emit code. It increments the |
| 128 | // stage offset for variable name mangling, and also ensures verfication variables in the |
| 129 | // fragment shader are cleared. |
| 130 | void reset() { |
| 131 | this->addStage(); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 132 | SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 133 | } |
| 134 | void addStage() { fStageIndex++; } |
| 135 | |
| 136 | class AutoStageAdvance { |
| 137 | public: |
| 138 | AutoStageAdvance(GrGLSLProgramBuilder* pb) |
| 139 | : fPB(pb) { |
| 140 | fPB->reset(); |
| 141 | // Each output to the fragment processor gets its own code section |
| 142 | fPB->fFS.nextStage(); |
| 143 | } |
| 144 | ~AutoStageAdvance() {} |
| 145 | private: |
| 146 | GrGLSLProgramBuilder* fPB; |
| 147 | }; |
| 148 | |
| 149 | // Generates a possibly mangled name for a stage variable and writes it to the fragment shader. |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 150 | void nameExpression(SkString*, const char* baseName); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 151 | |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 152 | void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage); |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 153 | void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut); |
| 154 | SkString emitAndInstallFragProc(const GrFragmentProcessor&, |
| 155 | int index, |
| 156 | int transformedCoordVarsIdx, |
| 157 | const SkString& input, |
Brian Salomon | 4d3f517 | 2018-06-07 14:42:52 -0400 | [diff] [blame] | 158 | SkString output, |
| 159 | SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*); |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 160 | void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn); |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 161 | SamplerHandle emitSampler(const GrTexture*, const GrSamplerState&, const GrSwizzle&, |
| 162 | const char* name); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 163 | bool checkSamplerCounts(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 164 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 165 | #ifdef SK_DEBUG |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 166 | void verify(const GrPrimitiveProcessor&); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 167 | void verify(const GrFragmentProcessor&); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 168 | void verify(const GrXferProcessor&); |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 169 | #endif |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 170 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame] | 171 | // These are used to check that we don't excede the allowable number of resources in a shader. |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 172 | int fNumFragmentSamplers; |
| 173 | SkSTArray<4, GrGLSLPrimitiveProcessor::TransformVar> fTransformedCoordVars; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | #endif |