blob: 83a437f84f7bfc4a2c1d5edb4c7264fa57761270 [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;
Brian Salomonf9f45122016-11-29 11:59:17 -050034 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
Greg Danielbc5d4d72017-05-05 10:28:42 -040056 const GrShaderVar& texelBufferVariable(TexelBufferHandle handle) const {
57 return this->uniformHandler()->texelBufferVariable(handle);
58 }
59
Brian Salomonf9f45122016-11-29 11:59:17 -050060 const GrShaderVar& imageStorageVariable(ImageStorageHandle handle) const {
61 return this->uniformHandler()->imageStorageVariable(handle);
62 }
63
egdaniel8dcdedc2015-11-11 06:27:20 -080064 // Handles for program uniforms (other than per-effect uniforms)
65 struct BuiltinUniformHandles {
66 UniformHandle fRTAdjustmentUni;
67
68 // We use the render target height to provide a y-down frag coord when specifying
69 // origin_upper_left is not supported.
70 UniformHandle fRTHeightUni;
71 };
72
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
Robert Phillips369e8b72017-08-01 16:13:04 -0400106 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
107 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
egdanielfa896322016-01-13 12:19:30 -0800108 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
Ethan Nicholas2983f402017-05-08 09:36:08 -0400117 bool emitAndInstallProcs();
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.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400147 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800148
149 void emitAndInstallPrimProc(const GrPrimitiveProcessor&,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400150 SkString* outputColor,
151 SkString* outputCoverage);
152 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
153 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
154 int index,
155 int transformedCoordVarsIdx,
156 const SkString& input,
157 SkString output);
158 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Brian Salomonab015ef2017-04-04 10:15:51 -0400159 void emitSamplersAndImageStorages(const GrResourceIOProcessor& processor,
Brian Salomonf9f45122016-11-29 11:59:17 -0500160 SkTArray<SamplerHandle>* outTexSamplerHandles,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400161 SkTArray<TexelBufferHandle>* outTexelBufferHandles,
Brian Salomonf9f45122016-11-29 11:59:17 -0500162 SkTArray<ImageStorageHandle>* outImageStorageHandles);
Brian Salomon18dfa982017-04-03 16:57:43 -0400163 SamplerHandle emitSampler(GrSLType samplerType, GrPixelConfig, const char* name,
164 GrShaderFlags visibility);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400165 TexelBufferHandle emitTexelBuffer(GrPixelConfig, const char* name, GrShaderFlags visibility);
Brian Salomonab015ef2017-04-04 10:15:51 -0400166 ImageStorageHandle emitImageStorage(const GrResourceIOProcessor::ImageStorageAccess&,
167 const char* name);
egdanielfa896322016-01-13 12:19:30 -0800168 void emitFSOutputSwizzle(bool hasSecondaryOutput);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400169 void updateSamplerCounts(GrShaderFlags visibility);
cdalton9c3f1432016-03-11 10:07:37 -0800170 bool checkSamplerCounts();
Brian Salomonf9f45122016-11-29 11:59:17 -0500171 bool checkImageStorageCounts();
egdanielfa896322016-01-13 12:19:30 -0800172
cdalton87332102016-02-26 12:22:02 -0800173#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800174 void verify(const GrPrimitiveProcessor&);
175 void verify(const GrXferProcessor&);
176 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800177#endif
egdanielfa896322016-01-13 12:19:30 -0800178
Greg Danielbc5d4d72017-05-05 10:28:42 -0400179 // These are used to check that we don't excede the allowable number of resources in a shader.
180 // The sampler counts include both normal texure samplers as well as texel buffers.
bsalomona624bf32016-09-20 09:12:47 -0700181 int fNumVertexSamplers;
182 int fNumGeometrySamplers;
183 int fNumFragmentSamplers;
Brian Salomonf9f45122016-11-29 11:59:17 -0500184 int fNumVertexImageStorages;
185 int fNumGeometryImageStorages;
186 int fNumFragmentImageStorages;
bsalomona624bf32016-09-20 09:12:47 -0700187 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800188};
189
190#endif