blob: 27c84372c1aeaaddab852f5122f9d39a3be8b611 [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"
cdalton3f6f76f2016-04-11 12:18:09 -070019#include "glsl/GrGLSLSampler.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
23class GrGLSLCaps;
24class GrGLSLShaderVar;
egdaniel0eafe792015-11-20 14:01:22 -080025class GrGLSLVaryingHandler;
egdaniel8dcdedc2015-11-11 06:27:20 -080026
egdanielfa896322016-01-13 12:19:30 -080027typedef SkSTArray<8, GrGLSLFragmentProcessor*, true> GrGLSLFragProcs;
28
egdaniel7ea439b2015-12-03 09:20:44 -080029class GrGLSLProgramBuilder {
egdaniel8dcdedc2015-11-11 06:27:20 -080030public:
egdaniel7ea439b2015-12-03 09:20:44 -080031 typedef GrGLSLUniformHandler::UniformHandle UniformHandle;
32
33 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080034
egdanielfa896322016-01-13 12:19:30 -080035 virtual const GrCaps* caps() const = 0;
egdaniela2e3e0f2015-11-19 07:23:45 -080036 virtual const GrGLSLCaps* glslCaps() const = 0;
37
egdaniel0e1853c2016-03-17 11:35:45 -070038 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(); }
egdaniel7ea439b2015-12-03 09:20:44 -080042
cdalton5e58cee2016-02-11 12:49:47 -080043 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080044
egdaniel09aa1fc2016-04-20 07:09:46 -070045 typedef GrGLSLUniformHandler::SamplerHandle SamplerHandle;
46
47 const GrGLSLSampler& getSampler(SamplerHandle handle) const;
48
egdaniel8dcdedc2015-11-11 06:27:20 -080049 // 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
egdaniel7ea439b2015-12-03 09:20:44 -080058 // 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);
egdaniel8dcdedc2015-11-11 06:27:20 -080060 const char* rtAdjustment() const { return "rtAdjustment"; }
halcanary9d524f22016-03-29 09:03:52 -070061
egdaniel7ea439b2015-12-03 09:20:44 -080062 // 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);
egdaniel8dcdedc2015-11-11 06:27:20 -080065
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
egdaniel7ea439b2015-12-03 09:20:44 -080071 virtual GrGLSLUniformHandler* uniformHandler() = 0;
72 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080073 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
74
egdanielb80ec8b2016-02-09 09:54:43 -080075 // 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
egdaniel8dcdedc2015-11-11 06:27:20 -080080 // number of each input/output type in a single allocation block, used by many builders
81 static const int kVarsPerBlock;
82
egdaniel0eafe792015-11-20 14:01:22 -080083 GrGLSLVertexBuilder fVS;
84 GrGLSLGeometryBuilder fGS;
egdaniel2d721d32015-11-11 13:06:05 -080085 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080086
egdaniel8dcdedc2015-11-11 06:27:20 -080087 int fStageIndex;
88
egdaniel0e1853c2016-03-17 11:35:45 -070089 const GrPipeline& fPipeline;
90 const GrPrimitiveProcessor& fPrimProc;
91 const GrProgramDesc& fDesc;
egdaniel8dcdedc2015-11-11 06:27:20 -080092
egdaniel7ea439b2015-12-03 09:20:44 -080093 BuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -080094
egdanielfa896322016-01-13 12:19:30 -080095 GrGLSLPrimitiveProcessor* fGeometryProcessor;
96 GrGLSLXferProcessor* fXferProcessor;
97 GrGLSLFragProcs fFragmentProcessors;
98
egdaniel7ea439b2015-12-03 09:20:44 -080099protected:
egdaniel0e1853c2016-03-17 11:35:45 -0700100 explicit GrGLSLProgramBuilder(const GrPipeline&,
101 const GrPrimitiveProcessor&,
102 const GrProgramDesc&);
egdanielfa896322016-01-13 12:19:30 -0800103
cdalton9c3f1432016-03-11 10:07:37 -0800104 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
105
106 bool emitAndInstallProcs(GrGLSLExpr4* inputColor, GrGLSLExpr4* inputCoverage);
egdanielfa896322016-01-13 12:19:30 -0800107
108 void cleanupFragmentProcessors();
109
egdaniel9f1d4152016-02-10 09:50:38 -0800110 void finalizeShaders();
111
egdanielfa896322016-01-13 12:19:30 -0800112private:
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();
cdalton87332102016-02-26 12:22:02 -0800118 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800119 }
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);
142 void emitAndInstallFragProcs(int procOffset, int numProcs, GrGLSLExpr4* inOut);
143 void emitAndInstallFragProc(const GrFragmentProcessor&,
144 int index,
145 const GrGLSLExpr4& input,
146 GrGLSLExpr4* output);
147 void emitAndInstallXferProc(const GrXferProcessor&,
148 const GrGLSLExpr4& colorIn,
149 const GrGLSLExpr4& coverageIn,
ethannicholas22793252016-01-30 09:59:10 -0800150 bool ignoresCoverage,
151 GrPixelLocalStorageState plsState);
egdaniel09aa1fc2016-04-20 07:09:46 -0700152
cdalton74b8d322016-04-11 14:47:28 -0700153 void emitSamplers(const GrProcessor& processor,
egdaniel09aa1fc2016-04-20 07:09:46 -0700154 SkTArray<SamplerHandle>* outTexSamplers,
155 SkTArray<SamplerHandle>* outBufferSamplers);
cdalton74b8d322016-04-11 14:47:28 -0700156 void emitSampler(GrSLType samplerType,
157 GrPixelConfig,
158 const char* name,
159 GrShaderFlags visibility,
egdaniel09aa1fc2016-04-20 07:09:46 -0700160 SkTArray<SamplerHandle>* outSamplers);
egdanielfa896322016-01-13 12:19:30 -0800161 void emitFSOutputSwizzle(bool hasSecondaryOutput);
cdalton9c3f1432016-03-11 10:07:37 -0800162 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800163
cdalton87332102016-02-26 12:22:02 -0800164#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800165 void verify(const GrPrimitiveProcessor&);
166 void verify(const GrXferProcessor&);
167 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800168#endif
egdanielfa896322016-01-13 12:19:30 -0800169
cdalton9c3f1432016-03-11 10:07:37 -0800170 GrGLSLPrimitiveProcessor::TransformsIn fCoordTransforms;
171 GrGLSLPrimitiveProcessor::TransformsOut fOutCoords;
172 int fNumVertexSamplers;
173 int fNumGeometrySamplers;
174 int fNumFragmentSamplers;
egdaniel8dcdedc2015-11-11 06:27:20 -0800175};
176
177#endif