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