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" |
| 13 | #include "GrGpu.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" |
| 16 | #include "glsl/GrGLSLGeometryShaderBuilder.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLPrimitiveProcessor.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 18 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 20 | #include "glsl/GrGLSLVertexShaderBuilder.h" |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 21 | #include "glsl/GrGLSLXferProcessor.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 22 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 23 | class GrShaderVar; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 24 | class GrGLSLVaryingHandler; |
Ethan Nicholas | 421fe44 | 2017-05-04 13:10:59 +0000 | [diff] [blame] | 25 | class GrGLSLExpr4; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 26 | class GrShaderCaps; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 27 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 28 | typedef SkSTArray<8, GrGLSLFragmentProcessor*, true> GrGLSLFragProcs; |
| 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; |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame^] | 34 | using TexelBufferHandle = GrGLSLUniformHandler::TexelBufferHandle; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 35 | using ImageStorageHandle = GrGLSLUniformHandler::ImageStorageHandle; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 36 | |
| 37 | virtual ~GrGLSLProgramBuilder() {} |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 38 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 39 | virtual const GrCaps* caps() const = 0; |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 40 | const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); } |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 41 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 42 | const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; } |
| 43 | const GrPipeline& pipeline() const { return fPipeline; } |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 44 | GrProgramDesc* desc() { return fDesc; } |
| 45 | const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); } |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 46 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 47 | void appendUniformDecls(GrShaderFlags visibility, SkString*) const; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 48 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 49 | const GrShaderVar& samplerVariable(SamplerHandle handle) const { |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 50 | return this->uniformHandler()->samplerVariable(handle); |
| 51 | } |
| 52 | |
| 53 | GrSwizzle samplerSwizzle(SamplerHandle handle) const { |
| 54 | return this->uniformHandler()->samplerSwizzle(handle); |
| 55 | } |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 56 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame^] | 57 | const GrShaderVar& texelBufferVariable(TexelBufferHandle handle) const { |
| 58 | return this->uniformHandler()->texelBufferVariable(handle); |
| 59 | } |
| 60 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 61 | const GrShaderVar& imageStorageVariable(ImageStorageHandle handle) const { |
| 62 | return this->uniformHandler()->imageStorageVariable(handle); |
| 63 | } |
| 64 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 65 | // Handles for program uniforms (other than per-effect uniforms) |
| 66 | struct BuiltinUniformHandles { |
| 67 | UniformHandle fRTAdjustmentUni; |
| 68 | |
| 69 | // We use the render target height to provide a y-down frag coord when specifying |
| 70 | // origin_upper_left is not supported. |
| 71 | UniformHandle fRTHeightUni; |
| 72 | }; |
| 73 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 74 | // Used to add a uniform for the RenderTarget height (used for frag position) without mangling |
| 75 | // the name of the uniform inside of a stage. |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 76 | void addRTHeightUniform(const char* name); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 77 | |
| 78 | // Generates a name for a variable. The generated string will be name prefixed by the prefix |
| 79 | // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless |
| 80 | // explicitly asked not to. |
| 81 | void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true); |
| 82 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 83 | virtual GrGLSLUniformHandler* uniformHandler() = 0; |
| 84 | virtual const GrGLSLUniformHandler* uniformHandler() const = 0; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 85 | virtual GrGLSLVaryingHandler* varyingHandler() = 0; |
| 86 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 87 | // Used for backend customization of the output color and secondary color variables from the |
| 88 | // 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] | 89 | virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {} |
| 90 | virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {} |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 91 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 92 | // number of each input/output type in a single allocation block, used by many builders |
| 93 | static const int kVarsPerBlock; |
| 94 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 95 | GrGLSLVertexBuilder fVS; |
| 96 | GrGLSLGeometryBuilder fGS; |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 97 | GrGLSLFragmentShaderBuilder fFS; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 98 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 99 | int fStageIndex; |
| 100 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 101 | const GrPipeline& fPipeline; |
| 102 | const GrPrimitiveProcessor& fPrimProc; |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 103 | GrProgramDesc* fDesc; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 104 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 105 | BuiltinUniformHandles fUniformHandles; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 106 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 107 | GrGLSLPrimitiveProcessor* fGeometryProcessor; |
| 108 | GrGLSLXferProcessor* fXferProcessor; |
| 109 | GrGLSLFragProcs fFragmentProcessors; |
| 110 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 111 | protected: |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 112 | explicit GrGLSLProgramBuilder(const GrPipeline&, |
| 113 | const GrPrimitiveProcessor&, |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 114 | GrProgramDesc*); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 115 | |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 116 | void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName); |
| 117 | |
Ethan Nicholas | 421fe44 | 2017-05-04 13:10:59 +0000 | [diff] [blame] | 118 | bool emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 119 | |
| 120 | void cleanupFragmentProcessors(); |
| 121 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 122 | void finalizeShaders(); |
| 123 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 124 | private: |
| 125 | // reset is called by program creator between each processor's emit code. It increments the |
| 126 | // stage offset for variable name mangling, and also ensures verfication variables in the |
| 127 | // fragment shader are cleared. |
| 128 | void reset() { |
| 129 | this->addStage(); |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 130 | SkDEBUGCODE(fFS.resetVerification();) |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 131 | } |
| 132 | void addStage() { fStageIndex++; } |
| 133 | |
| 134 | class AutoStageAdvance { |
| 135 | public: |
| 136 | AutoStageAdvance(GrGLSLProgramBuilder* pb) |
| 137 | : fPB(pb) { |
| 138 | fPB->reset(); |
| 139 | // Each output to the fragment processor gets its own code section |
| 140 | fPB->fFS.nextStage(); |
| 141 | } |
| 142 | ~AutoStageAdvance() {} |
| 143 | private: |
| 144 | GrGLSLProgramBuilder* fPB; |
| 145 | }; |
| 146 | |
| 147 | // Generates a possibly mangled name for a stage variable and writes it to the fragment shader. |
Ethan Nicholas | 421fe44 | 2017-05-04 13:10:59 +0000 | [diff] [blame] | 148 | // If GrGLSLExpr4 has a valid name then it will use that instead |
| 149 | void nameExpression(GrGLSLExpr4*, const char* baseName); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 150 | |
| 151 | void emitAndInstallPrimProc(const GrPrimitiveProcessor&, |
Ethan Nicholas | 421fe44 | 2017-05-04 13:10:59 +0000 | [diff] [blame] | 152 | GrGLSLExpr4* outputColor, |
| 153 | GrGLSLExpr4* outputCoverage); |
| 154 | void emitAndInstallFragProcs(GrGLSLExpr4* colorInOut, GrGLSLExpr4* coverageInOut); |
| 155 | void emitAndInstallFragProc(const GrFragmentProcessor&, |
| 156 | int index, |
| 157 | int transformedCoordVarsIdx, |
| 158 | const GrGLSLExpr4& input, |
| 159 | GrGLSLExpr4* output); |
| 160 | void emitAndInstallXferProc(const GrGLSLExpr4& colorIn, const GrGLSLExpr4& coverageIn); |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 161 | void emitSamplersAndImageStorages(const GrResourceIOProcessor& processor, |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 162 | SkTArray<SamplerHandle>* outTexSamplerHandles, |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame^] | 163 | SkTArray<TexelBufferHandle>* outTexelBufferHandles, |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 164 | SkTArray<ImageStorageHandle>* outImageStorageHandles); |
Brian Salomon | 18dfa98 | 2017-04-03 16:57:43 -0400 | [diff] [blame] | 165 | SamplerHandle emitSampler(GrSLType samplerType, GrPixelConfig, const char* name, |
| 166 | GrShaderFlags visibility); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame^] | 167 | TexelBufferHandle emitTexelBuffer(GrPixelConfig, const char* name, GrShaderFlags visibility); |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 168 | ImageStorageHandle emitImageStorage(const GrResourceIOProcessor::ImageStorageAccess&, |
| 169 | const char* name); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 170 | void emitFSOutputSwizzle(bool hasSecondaryOutput); |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame^] | 171 | void updateSamplerCounts(GrShaderFlags visibility); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 172 | bool checkSamplerCounts(); |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 173 | bool checkImageStorageCounts(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 174 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 175 | #ifdef SK_DEBUG |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 176 | void verify(const GrPrimitiveProcessor&); |
| 177 | void verify(const GrXferProcessor&); |
| 178 | void verify(const GrFragmentProcessor&); |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 179 | #endif |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 180 | |
Greg Daniel | bc5d4d7 | 2017-05-05 10:28:42 -0400 | [diff] [blame^] | 181 | // These are used to check that we don't excede the allowable number of resources in a shader. |
| 182 | // The sampler counts include both normal texure samplers as well as texel buffers. |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 183 | int fNumVertexSamplers; |
| 184 | int fNumGeometrySamplers; |
| 185 | int fNumFragmentSamplers; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 186 | int fNumVertexImageStorages; |
| 187 | int fNumGeometryImageStorages; |
| 188 | int fNumFragmentImageStorages; |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 189 | SkSTArray<4, GrShaderVar> fTransformedCoordVars; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | #endif |