blob: 3074110f60657741db172379a9015f042fa477cd [file] [log] [blame]
egdaniel8dcdedc2015-11-11 06:27:20 -08001/*
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 Salomon94efbf52016-11-29 13:43:05 -050011#include "GrCaps.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080012#include "GrGeometryProcessor.h"
13#include "GrGpu.h"
robertphillips28a838e2016-06-23 14:07:00 -070014#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080015#include "glsl/GrGLSLFragmentShaderBuilder.h"
16#include "glsl/GrGLSLGeometryShaderBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080017#include "glsl/GrGLSLPrimitiveProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080018#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080019#include "glsl/GrGLSLUniformHandler.h"
egdaniel2d721d32015-11-11 13:06:05 -080020#include "glsl/GrGLSLVertexShaderBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080021#include "glsl/GrGLSLXferProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080022
Brian Salomon99938a82016-11-21 13:41:08 -050023class GrShaderVar;
egdaniel0eafe792015-11-20 14:01:22 -080024class GrGLSLVaryingHandler;
Brian Salomon99938a82016-11-21 13:41:08 -050025class GrGLSLExpr4;
Brian Salomon94efbf52016-11-29 13:43:05 -050026class GrShaderCaps;
egdaniel8dcdedc2015-11-11 06:27:20 -080027
egdanielfa896322016-01-13 12:19:30 -080028typedef SkSTArray<8, GrGLSLFragmentProcessor*, true> GrGLSLFragProcs;
29
egdaniel7ea439b2015-12-03 09:20:44 -080030class GrGLSLProgramBuilder {
egdaniel8dcdedc2015-11-11 06:27:20 -080031public:
Brian Salomonf9f45122016-11-29 11:59:17 -050032 using UniformHandle = GrGLSLUniformHandler::UniformHandle;
33 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
34 using ImageStorageHandle = GrGLSLUniformHandler::ImageStorageHandle;
egdaniel7ea439b2015-12-03 09:20:44 -080035
36 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080037
egdanielfa896322016-01-13 12:19:30 -080038 virtual const GrCaps* caps() const = 0;
Brian Salomon94efbf52016-11-29 13:43:05 -050039 const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
egdaniela2e3e0f2015-11-19 07:23:45 -080040
egdaniel0e1853c2016-03-17 11:35:45 -070041 const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
42 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050043 GrProgramDesc* desc() { return fDesc; }
44 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080045
cdalton5e58cee2016-02-11 12:49:47 -080046 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080047
Brian Salomon99938a82016-11-21 13:41:08 -050048 const GrShaderVar& samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050049 return this->uniformHandler()->samplerVariable(handle);
50 }
51
52 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
53 return this->uniformHandler()->samplerSwizzle(handle);
54 }
egdaniel09aa1fc2016-04-20 07:09:46 -070055
Brian Salomonf9f45122016-11-29 11:59:17 -050056 const GrShaderVar& imageStorageVariable(ImageStorageHandle handle) const {
57 return this->uniformHandler()->imageStorageVariable(handle);
58 }
59
egdaniel8dcdedc2015-11-11 06:27:20 -080060 // 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
egdaniel7ea439b2015-12-03 09:20:44 -080069 // 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);
egdaniel8dcdedc2015-11-11 06:27:20 -080071 const char* rtAdjustment() const { return "rtAdjustment"; }
halcanary9d524f22016-03-29 09:03:52 -070072
egdaniel7ea439b2015-12-03 09:20:44 -080073 // 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 Nicholas941e7e22016-12-12 15:33:30 -050075 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080076
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
egdaniel7ea439b2015-12-03 09:20:44 -080082 virtual GrGLSLUniformHandler* uniformHandler() = 0;
83 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080084 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
85
egdanielb80ec8b2016-02-09 09:54:43 -080086 // 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 Salomon99938a82016-11-21 13:41:08 -050088 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
89 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080090
egdaniel8dcdedc2015-11-11 06:27:20 -080091 // number of each input/output type in a single allocation block, used by many builders
92 static const int kVarsPerBlock;
93
egdaniel0eafe792015-11-20 14:01:22 -080094 GrGLSLVertexBuilder fVS;
95 GrGLSLGeometryBuilder fGS;
egdaniel2d721d32015-11-11 13:06:05 -080096 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080097
egdaniel8dcdedc2015-11-11 06:27:20 -080098 int fStageIndex;
99
egdaniel0e1853c2016-03-17 11:35:45 -0700100 const GrPipeline& fPipeline;
101 const GrPrimitiveProcessor& fPrimProc;
Ethan Nicholas38657112017-02-09 17:01:22 -0500102 GrProgramDesc* fDesc;
egdaniel8dcdedc2015-11-11 06:27:20 -0800103
egdaniel7ea439b2015-12-03 09:20:44 -0800104 BuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -0800105
egdanielfa896322016-01-13 12:19:30 -0800106 GrGLSLPrimitiveProcessor* fGeometryProcessor;
107 GrGLSLXferProcessor* fXferProcessor;
108 GrGLSLFragProcs fFragmentProcessors;
109
egdaniel7ea439b2015-12-03 09:20:44 -0800110protected:
egdaniel0e1853c2016-03-17 11:35:45 -0700111 explicit GrGLSLProgramBuilder(const GrPipeline&,
112 const GrPrimitiveProcessor&,
Ethan Nicholas38657112017-02-09 17:01:22 -0500113 GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -0800114
cdalton9c3f1432016-03-11 10:07:37 -0800115 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
116
117 bool emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage);
egdanielfa896322016-01-13 12:19:30 -0800118
119 void cleanupFragmentProcessors();
120
egdaniel9f1d4152016-02-10 09:50:38 -0800121 void finalizeShaders();
122
egdanielfa896322016-01-13 12:19:30 -0800123private:
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();
cdalton87332102016-02-26 12:22:02 -0800129 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800130 }
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);
bsalomona624bf32016-09-20 09:12:47 -0700153 void emitAndInstallFragProcs(GrGLSLExpr4* colorInOut, GrGLSLExpr4* coverageInOut);
egdanielfa896322016-01-13 12:19:30 -0800154 void emitAndInstallFragProc(const GrFragmentProcessor&,
155 int index,
bsalomona624bf32016-09-20 09:12:47 -0700156 int transformedCoordVarsIdx,
egdanielfa896322016-01-13 12:19:30 -0800157 const GrGLSLExpr4& input,
158 GrGLSLExpr4* output);
159 void emitAndInstallXferProc(const GrXferProcessor&,
160 const GrGLSLExpr4& colorIn,
161 const GrGLSLExpr4& coverageIn,
ethannicholas22793252016-01-30 09:59:10 -0800162 GrPixelLocalStorageState plsState);
Brian Salomonf9f45122016-11-29 11:59:17 -0500163 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);
egdanielfa896322016-01-13 12:19:30 -0800172 void emitFSOutputSwizzle(bool hasSecondaryOutput);
cdalton9c3f1432016-03-11 10:07:37 -0800173 bool checkSamplerCounts();
Brian Salomonf9f45122016-11-29 11:59:17 -0500174 bool checkImageStorageCounts();
egdanielfa896322016-01-13 12:19:30 -0800175
cdalton87332102016-02-26 12:22:02 -0800176#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800177 void verify(const GrPrimitiveProcessor&);
178 void verify(const GrXferProcessor&);
179 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800180#endif
egdanielfa896322016-01-13 12:19:30 -0800181
bsalomona624bf32016-09-20 09:12:47 -0700182 int fNumVertexSamplers;
183 int fNumGeometrySamplers;
184 int fNumFragmentSamplers;
Brian Salomonf9f45122016-11-29 11:59:17 -0500185 int fNumVertexImageStorages;
186 int fNumGeometryImageStorages;
187 int fNumFragmentImageStorages;
bsalomona624bf32016-09-20 09:12:47 -0700188 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800189};
190
191#endif