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" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLVertexShaderBuilder.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 | |
| 22 | class GrGLSLCaps; |
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; |
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 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame^] | 47 | const GrShaderVar& samplerVariable(SamplerHandle handle) const { |
Brian Salomon | 101b844 | 2016-11-18 11:58:54 -0500 | [diff] [blame] | 48 | return this->uniformHandler()->samplerVariable(handle); |
| 49 | } |
| 50 | |
| 51 | GrSwizzle samplerSwizzle(SamplerHandle handle) const { |
| 52 | return this->uniformHandler()->samplerSwizzle(handle); |
| 53 | } |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 54 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 55 | // Handles for program uniforms (other than per-effect uniforms) |
| 56 | struct BuiltinUniformHandles { |
| 57 | UniformHandle fRTAdjustmentUni; |
| 58 | |
| 59 | // We use the render target height to provide a y-down frag coord when specifying |
| 60 | // origin_upper_left is not supported. |
| 61 | UniformHandle fRTHeightUni; |
| 62 | }; |
| 63 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 64 | // Used to add a uniform in the vertex shader for transforming into normalized device space. |
| 65 | void addRTAdjustmentUniform(GrSLPrecision precision, const char* name, const char** outName); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 66 | const char* rtAdjustment() const { return "rtAdjustment"; } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 67 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 68 | // Used to add a uniform for the RenderTarget height (used for frag position) without mangling |
| 69 | // the name of the uniform inside of a stage. |
| 70 | void addRTHeightUniform(const char* name, const char** outName); |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 71 | |
| 72 | // Generates a name for a variable. The generated string will be name prefixed by the prefix |
| 73 | // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless |
| 74 | // explicitly asked not to. |
| 75 | void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true); |
| 76 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 77 | virtual GrGLSLUniformHandler* uniformHandler() = 0; |
| 78 | virtual const GrGLSLUniformHandler* uniformHandler() const = 0; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 79 | virtual GrGLSLVaryingHandler* varyingHandler() = 0; |
| 80 | |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 81 | // Used for backend customization of the output color and secondary color variables from the |
| 82 | // 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^] | 83 | virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {} |
| 84 | virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {} |
egdaniel | b80ec8b | 2016-02-09 09:54:43 -0800 | [diff] [blame] | 85 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 86 | // number of each input/output type in a single allocation block, used by many builders |
| 87 | static const int kVarsPerBlock; |
| 88 | |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 89 | GrGLSLVertexBuilder fVS; |
| 90 | GrGLSLGeometryBuilder fGS; |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 91 | GrGLSLFragmentShaderBuilder fFS; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 92 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 93 | int fStageIndex; |
| 94 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 95 | const GrPipeline& fPipeline; |
| 96 | const GrPrimitiveProcessor& fPrimProc; |
| 97 | const GrProgramDesc& fDesc; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 98 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 99 | BuiltinUniformHandles fUniformHandles; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 100 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 101 | GrGLSLPrimitiveProcessor* fGeometryProcessor; |
| 102 | GrGLSLXferProcessor* fXferProcessor; |
| 103 | GrGLSLFragProcs fFragmentProcessors; |
| 104 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 105 | protected: |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 106 | explicit GrGLSLProgramBuilder(const GrPipeline&, |
| 107 | const GrPrimitiveProcessor&, |
| 108 | const 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 | |
| 112 | bool emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 113 | |
| 114 | void cleanupFragmentProcessors(); |
| 115 | |
egdaniel | 9f1d415 | 2016-02-10 09:50:38 -0800 | [diff] [blame] | 116 | void finalizeShaders(); |
| 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. |
| 142 | // If GrGLSLExpr4 has a valid name then it will use that instead |
| 143 | void nameExpression(GrGLSLExpr4*, const char* baseName); |
| 144 | |
| 145 | void emitAndInstallPrimProc(const GrPrimitiveProcessor&, |
| 146 | GrGLSLExpr4* outputColor, |
| 147 | GrGLSLExpr4* outputCoverage); |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 148 | void emitAndInstallFragProcs(GrGLSLExpr4* colorInOut, GrGLSLExpr4* coverageInOut); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 149 | void emitAndInstallFragProc(const GrFragmentProcessor&, |
| 150 | int index, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 151 | int transformedCoordVarsIdx, |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 152 | const GrGLSLExpr4& input, |
| 153 | GrGLSLExpr4* output); |
| 154 | void emitAndInstallXferProc(const GrXferProcessor&, |
| 155 | const GrGLSLExpr4& colorIn, |
| 156 | const GrGLSLExpr4& coverageIn, |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 157 | bool ignoresCoverage, |
| 158 | GrPixelLocalStorageState plsState); |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 159 | |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 160 | void emitSamplers(const GrProcessor& processor, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 161 | SkTArray<SamplerHandle>* outTexSamplers, |
| 162 | SkTArray<SamplerHandle>* outBufferSamplers); |
cdalton | 74b8d32 | 2016-04-11 14:47:28 -0700 | [diff] [blame] | 163 | void emitSampler(GrSLType samplerType, |
| 164 | GrPixelConfig, |
| 165 | const char* name, |
| 166 | GrShaderFlags visibility, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 167 | SkTArray<SamplerHandle>* outSamplers); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 168 | void emitFSOutputSwizzle(bool hasSecondaryOutput); |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 169 | bool checkSamplerCounts(); |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 170 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 171 | #ifdef SK_DEBUG |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 172 | void verify(const GrPrimitiveProcessor&); |
| 173 | void verify(const GrXferProcessor&); |
| 174 | void verify(const GrFragmentProcessor&); |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 175 | #endif |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 176 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 177 | int fNumVertexSamplers; |
| 178 | int fNumGeometrySamplers; |
| 179 | int fNumFragmentSamplers; |
| 180 | SkSTArray<4, GrShaderVar> fTransformedCoordVars; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | #endif |