blob: 727614455f682d4801c30464509707ed1a980a13 [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
11#include "GrGeometryProcessor.h"
12#include "GrGpu.h"
robertphillips28a838e2016-06-23 14:07:00 -070013#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
15#include "glsl/GrGLSLGeometryShaderBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080016#include "glsl/GrGLSLPrimitiveProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080017#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080018#include "glsl/GrGLSLUniformHandler.h"
egdaniel2d721d32015-11-11 13:06:05 -080019#include "glsl/GrGLSLVertexShaderBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080020#include "glsl/GrGLSLXferProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080021
22class GrGLSLCaps;
23class GrGLSLShaderVar;
egdaniel0eafe792015-11-20 14:01:22 -080024class GrGLSLVaryingHandler;
egdaniel8dcdedc2015-11-11 06:27:20 -080025
egdanielfa896322016-01-13 12:19:30 -080026typedef SkSTArray<8, GrGLSLFragmentProcessor*, true> GrGLSLFragProcs;
27
egdaniel7ea439b2015-12-03 09:20:44 -080028class GrGLSLProgramBuilder {
egdaniel8dcdedc2015-11-11 06:27:20 -080029public:
egdaniel7ea439b2015-12-03 09:20:44 -080030 typedef GrGLSLUniformHandler::UniformHandle UniformHandle;
31
32 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080033
egdanielfa896322016-01-13 12:19:30 -080034 virtual const GrCaps* caps() const = 0;
egdaniela2e3e0f2015-11-19 07:23:45 -080035 virtual const GrGLSLCaps* glslCaps() const = 0;
36
egdaniel0e1853c2016-03-17 11:35:45 -070037 const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
38 const GrPipeline& pipeline() const { return fPipeline; }
39 const GrProgramDesc& desc() const { return fDesc; }
40 const GrProgramDesc::KeyHeader& header() const { return fDesc.header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080041
cdalton5e58cee2016-02-11 12:49:47 -080042 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080043
egdaniel09aa1fc2016-04-20 07:09:46 -070044 typedef GrGLSLUniformHandler::SamplerHandle SamplerHandle;
45
Brian Salomon101b8442016-11-18 11:58:54 -050046 const GrGLSLShaderVar& samplerVariable(SamplerHandle handle) const {
47 return this->uniformHandler()->samplerVariable(handle);
48 }
49
50 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
51 return this->uniformHandler()->samplerSwizzle(handle);
52 }
egdaniel09aa1fc2016-04-20 07:09:46 -070053
egdaniel8dcdedc2015-11-11 06:27:20 -080054 // Handles for program uniforms (other than per-effect uniforms)
55 struct BuiltinUniformHandles {
56 UniformHandle fRTAdjustmentUni;
57
58 // We use the render target height to provide a y-down frag coord when specifying
59 // origin_upper_left is not supported.
60 UniformHandle fRTHeightUni;
61 };
62
egdaniel7ea439b2015-12-03 09:20:44 -080063 // Used to add a uniform in the vertex shader for transforming into normalized device space.
64 void addRTAdjustmentUniform(GrSLPrecision precision, const char* name, const char** outName);
egdaniel8dcdedc2015-11-11 06:27:20 -080065 const char* rtAdjustment() const { return "rtAdjustment"; }
halcanary9d524f22016-03-29 09:03:52 -070066
egdaniel7ea439b2015-12-03 09:20:44 -080067 // Used to add a uniform for the RenderTarget height (used for frag position) without mangling
68 // the name of the uniform inside of a stage.
69 void addRTHeightUniform(const char* name, const char** outName);
egdaniel8dcdedc2015-11-11 06:27:20 -080070
71 // Generates a name for a variable. The generated string will be name prefixed by the prefix
72 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
73 // explicitly asked not to.
74 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
75
egdaniel7ea439b2015-12-03 09:20:44 -080076 virtual GrGLSLUniformHandler* uniformHandler() = 0;
77 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080078 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
79
egdanielb80ec8b2016-02-09 09:54:43 -080080 // Used for backend customization of the output color and secondary color variables from the
81 // fragment processor. Only used if the outputs are explicitly declared in the shaders
82 virtual void finalizeFragmentOutputColor(GrGLSLShaderVar& outputColor) {}
83 virtual void finalizeFragmentSecondaryColor(GrGLSLShaderVar& outputColor) {}
84
egdaniel8dcdedc2015-11-11 06:27:20 -080085 // number of each input/output type in a single allocation block, used by many builders
86 static const int kVarsPerBlock;
87
egdaniel0eafe792015-11-20 14:01:22 -080088 GrGLSLVertexBuilder fVS;
89 GrGLSLGeometryBuilder fGS;
egdaniel2d721d32015-11-11 13:06:05 -080090 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080091
egdaniel8dcdedc2015-11-11 06:27:20 -080092 int fStageIndex;
93
egdaniel0e1853c2016-03-17 11:35:45 -070094 const GrPipeline& fPipeline;
95 const GrPrimitiveProcessor& fPrimProc;
96 const GrProgramDesc& fDesc;
egdaniel8dcdedc2015-11-11 06:27:20 -080097
egdaniel7ea439b2015-12-03 09:20:44 -080098 BuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -080099
egdanielfa896322016-01-13 12:19:30 -0800100 GrGLSLPrimitiveProcessor* fGeometryProcessor;
101 GrGLSLXferProcessor* fXferProcessor;
102 GrGLSLFragProcs fFragmentProcessors;
103
egdaniel7ea439b2015-12-03 09:20:44 -0800104protected:
egdaniel0e1853c2016-03-17 11:35:45 -0700105 explicit GrGLSLProgramBuilder(const GrPipeline&,
106 const GrPrimitiveProcessor&,
107 const GrProgramDesc&);
egdanielfa896322016-01-13 12:19:30 -0800108
cdalton9c3f1432016-03-11 10:07:37 -0800109 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
110
111 bool emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage);
egdanielfa896322016-01-13 12:19:30 -0800112
113 void cleanupFragmentProcessors();
114
egdaniel9f1d4152016-02-10 09:50:38 -0800115 void finalizeShaders();
116
egdanielfa896322016-01-13 12:19:30 -0800117private:
118 // reset is called by program creator between each processor's emit code. It increments the
119 // stage offset for variable name mangling, and also ensures verfication variables in the
120 // fragment shader are cleared.
121 void reset() {
122 this->addStage();
cdalton87332102016-02-26 12:22:02 -0800123 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800124 }
125 void addStage() { fStageIndex++; }
126
127 class AutoStageAdvance {
128 public:
129 AutoStageAdvance(GrGLSLProgramBuilder* pb)
130 : fPB(pb) {
131 fPB->reset();
132 // Each output to the fragment processor gets its own code section
133 fPB->fFS.nextStage();
134 }
135 ~AutoStageAdvance() {}
136 private:
137 GrGLSLProgramBuilder* fPB;
138 };
139
140 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
141 // If GrGLSLExpr4 has a valid name then it will use that instead
142 void nameExpression(GrGLSLExpr4*, const char* baseName);
143
144 void emitAndInstallPrimProc(const GrPrimitiveProcessor&,
145 GrGLSLExpr4* outputColor,
146 GrGLSLExpr4* outputCoverage);
bsalomona624bf32016-09-20 09:12:47 -0700147 void emitAndInstallFragProcs(GrGLSLExpr4* colorInOut, GrGLSLExpr4* coverageInOut);
egdanielfa896322016-01-13 12:19:30 -0800148 void emitAndInstallFragProc(const GrFragmentProcessor&,
149 int index,
bsalomona624bf32016-09-20 09:12:47 -0700150 int transformedCoordVarsIdx,
egdanielfa896322016-01-13 12:19:30 -0800151 const GrGLSLExpr4& input,
152 GrGLSLExpr4* output);
153 void emitAndInstallXferProc(const GrXferProcessor&,
154 const GrGLSLExpr4& colorIn,
155 const GrGLSLExpr4& coverageIn,
ethannicholas22793252016-01-30 09:59:10 -0800156 bool ignoresCoverage,
157 GrPixelLocalStorageState plsState);
egdaniel09aa1fc2016-04-20 07:09:46 -0700158
cdalton74b8d322016-04-11 14:47:28 -0700159 void emitSamplers(const GrProcessor& processor,
egdaniel09aa1fc2016-04-20 07:09:46 -0700160 SkTArray<SamplerHandle>* outTexSamplers,
161 SkTArray<SamplerHandle>* outBufferSamplers);
cdalton74b8d322016-04-11 14:47:28 -0700162 void emitSampler(GrSLType samplerType,
163 GrPixelConfig,
164 const char* name,
165 GrShaderFlags visibility,
egdaniel09aa1fc2016-04-20 07:09:46 -0700166 SkTArray<SamplerHandle>* outSamplers);
egdanielfa896322016-01-13 12:19:30 -0800167 void emitFSOutputSwizzle(bool hasSecondaryOutput);
cdalton9c3f1432016-03-11 10:07:37 -0800168 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800169
cdalton87332102016-02-26 12:22:02 -0800170#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800171 void verify(const GrPrimitiveProcessor&);
172 void verify(const GrXferProcessor&);
173 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800174#endif
egdanielfa896322016-01-13 12:19:30 -0800175
bsalomona624bf32016-09-20 09:12:47 -0700176 int fNumVertexSamplers;
177 int fNumGeometrySamplers;
178 int fNumFragmentSamplers;
179 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800180};
181
182#endif