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