blob: d4d2099a415cc047ec6c7e2f3768d97de418798b [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"
Robert Phillips646e4292017-06-13 12:44:56 -040013#include "GrProgramDesc.h"
robertphillips28a838e2016-06-23 14:07:00 -070014#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080015#include "glsl/GrGLSLFragmentShaderBuilder.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"
Chris Daltonc17bf322017-10-24 10:59:03 -060019#include "glsl/GrGLSLVertexGeoBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080020#include "glsl/GrGLSLXferProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080021
Brian Salomon99938a82016-11-21 13:41:08 -050022class GrShaderVar;
egdaniel0eafe792015-11-20 14:01:22 -080023class GrGLSLVaryingHandler;
Ethan Nicholas2983f402017-05-08 09:36:08 -040024class SkString;
Brian Salomon94efbf52016-11-29 13:43:05 -050025class GrShaderCaps;
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:
Brian Salomonf9f45122016-11-29 11:59:17 -050031 using UniformHandle = GrGLSLUniformHandler::UniformHandle;
32 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
Greg Danielbc5d4d72017-05-05 10:28:42 -040033 using TexelBufferHandle = GrGLSLUniformHandler::TexelBufferHandle;
egdaniel7ea439b2015-12-03 09:20:44 -080034
35 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080036
egdanielfa896322016-01-13 12:19:30 -080037 virtual const GrCaps* caps() const = 0;
Brian Salomon94efbf52016-11-29 13:43:05 -050038 const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
egdaniela2e3e0f2015-11-19 07:23:45 -080039
egdaniel0e1853c2016-03-17 11:35:45 -070040 const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
41 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050042 GrProgramDesc* desc() { return fDesc; }
43 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080044
cdalton5e58cee2016-02-11 12:49:47 -080045 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080046
Brian Salomon99938a82016-11-21 13:41:08 -050047 const GrShaderVar& samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050048 return this->uniformHandler()->samplerVariable(handle);
49 }
50
51 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
52 return this->uniformHandler()->samplerSwizzle(handle);
53 }
egdaniel09aa1fc2016-04-20 07:09:46 -070054
Greg Danielbc5d4d72017-05-05 10:28:42 -040055 const GrShaderVar& texelBufferVariable(TexelBufferHandle handle) const {
56 return this->uniformHandler()->texelBufferVariable(handle);
57 }
58
egdaniel8dcdedc2015-11-11 06:27:20 -080059 // Handles for program uniforms (other than per-effect uniforms)
60 struct BuiltinUniformHandles {
61 UniformHandle fRTAdjustmentUni;
62
63 // We use the render target height to provide a y-down frag coord when specifying
64 // origin_upper_left is not supported.
65 UniformHandle fRTHeightUni;
66 };
67
egdaniel7ea439b2015-12-03 09:20:44 -080068 // 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.
Ethan Nicholas941e7e22016-12-12 15:33:30 -050070 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080071
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
egdaniel7ea439b2015-12-03 09:20:44 -080077 virtual GrGLSLUniformHandler* uniformHandler() = 0;
78 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080079 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
80
egdanielb80ec8b2016-02-09 09:54:43 -080081 // 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 Salomon99938a82016-11-21 13:41:08 -050083 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
84 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080085
egdaniel8dcdedc2015-11-11 06:27:20 -080086 // number of each input/output type in a single allocation block, used by many builders
87 static const int kVarsPerBlock;
88
egdaniel0eafe792015-11-20 14:01:22 -080089 GrGLSLVertexBuilder fVS;
90 GrGLSLGeometryBuilder fGS;
egdaniel2d721d32015-11-11 13:06:05 -080091 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080092
egdaniel8dcdedc2015-11-11 06:27:20 -080093 int fStageIndex;
94
egdaniel0e1853c2016-03-17 11:35:45 -070095 const GrPipeline& fPipeline;
96 const GrPrimitiveProcessor& fPrimProc;
Ethan Nicholas38657112017-02-09 17:01:22 -050097 GrProgramDesc* fDesc;
egdaniel8dcdedc2015-11-11 06:27:20 -080098
egdaniel7ea439b2015-12-03 09:20:44 -080099 BuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -0800100
Robert Phillips369e8b72017-08-01 16:13:04 -0400101 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
102 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
egdanielfa896322016-01-13 12:19:30 -0800103 GrGLSLFragProcs fFragmentProcessors;
104
egdaniel7ea439b2015-12-03 09:20:44 -0800105protected:
egdaniel0e1853c2016-03-17 11:35:45 -0700106 explicit GrGLSLProgramBuilder(const GrPipeline&,
107 const GrPrimitiveProcessor&,
Ethan Nicholas38657112017-02-09 17:01:22 -0500108 GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -0800109
cdalton9c3f1432016-03-11 10:07:37 -0800110 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
111
Ethan Nicholas2983f402017-05-08 09:36:08 -0400112 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800113
114 void cleanupFragmentProcessors();
115
egdaniel9f1d4152016-02-10 09:50:38 -0800116 void finalizeShaders();
117
Brian Salomondc092132018-04-04 10:14:16 -0400118 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
119
egdanielfa896322016-01-13 12:19:30 -0800120private:
121 // reset is called by program creator between each processor's emit code. It increments the
122 // stage offset for variable name mangling, and also ensures verfication variables in the
123 // fragment shader are cleared.
124 void reset() {
125 this->addStage();
cdalton87332102016-02-26 12:22:02 -0800126 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800127 }
128 void addStage() { fStageIndex++; }
129
130 class AutoStageAdvance {
131 public:
132 AutoStageAdvance(GrGLSLProgramBuilder* pb)
133 : fPB(pb) {
134 fPB->reset();
135 // Each output to the fragment processor gets its own code section
136 fPB->fFS.nextStage();
137 }
138 ~AutoStageAdvance() {}
139 private:
140 GrGLSLProgramBuilder* fPB;
141 };
142
143 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400144 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800145
146 void emitAndInstallPrimProc(const GrPrimitiveProcessor&,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400147 SkString* outputColor,
148 SkString* outputCoverage);
149 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
150 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
151 int index,
152 int transformedCoordVarsIdx,
153 const SkString& input,
154 SkString output);
155 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Brian Salomon559f5562017-11-15 14:28:33 -0500156 void emitSamplers(const GrResourceIOProcessor& processor,
157 SkTArray<SamplerHandle>* outTexSamplerHandles,
158 SkTArray<TexelBufferHandle>* outTexelBufferHandles);
Brian Salomon18dfa982017-04-03 16:57:43 -0400159 SamplerHandle emitSampler(GrSLType samplerType, GrPixelConfig, const char* name,
160 GrShaderFlags visibility);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400161 TexelBufferHandle emitTexelBuffer(GrPixelConfig, const char* name, GrShaderFlags visibility);
egdanielfa896322016-01-13 12:19:30 -0800162 void emitFSOutputSwizzle(bool hasSecondaryOutput);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400163 void updateSamplerCounts(GrShaderFlags visibility);
cdalton9c3f1432016-03-11 10:07:37 -0800164 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800165
cdalton87332102016-02-26 12:22:02 -0800166#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800167 void verify(const GrPrimitiveProcessor&);
168 void verify(const GrXferProcessor&);
169 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800170#endif
egdanielfa896322016-01-13 12:19:30 -0800171
Greg Danielbc5d4d72017-05-05 10:28:42 -0400172 // These are used to check that we don't excede the allowable number of resources in a shader.
173 // The sampler counts include both normal texure samplers as well as texel buffers.
bsalomona624bf32016-09-20 09:12:47 -0700174 int fNumVertexSamplers;
175 int fNumGeometrySamplers;
176 int fNumFragmentSamplers;
177 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800178};
179
180#endif