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