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