blob: 0457e7105872d0359589abbd8a24f67ea08ef76d [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"
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;
Ethan Nicholas2983f402017-05-08 09:36:08 -040025class SkString;
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;
Greg Danielbc5d4d72017-05-05 10:28:42 -040034 using TexelBufferHandle = GrGLSLUniformHandler::TexelBufferHandle;
Brian Salomonf9f45122016-11-29 11:59:17 -050035 using ImageStorageHandle = GrGLSLUniformHandler::ImageStorageHandle;
egdaniel7ea439b2015-12-03 09:20:44 -080036
37 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080038
egdanielfa896322016-01-13 12:19:30 -080039 virtual const GrCaps* caps() const = 0;
Brian Salomon94efbf52016-11-29 13:43:05 -050040 const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
egdaniela2e3e0f2015-11-19 07:23:45 -080041
egdaniel0e1853c2016-03-17 11:35:45 -070042 const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
43 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050044 GrProgramDesc* desc() { return fDesc; }
45 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080046
cdalton5e58cee2016-02-11 12:49:47 -080047 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080048
Brian Salomon99938a82016-11-21 13:41:08 -050049 const GrShaderVar& samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050050 return this->uniformHandler()->samplerVariable(handle);
51 }
52
53 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
54 return this->uniformHandler()->samplerSwizzle(handle);
55 }
egdaniel09aa1fc2016-04-20 07:09:46 -070056
Greg Danielbc5d4d72017-05-05 10:28:42 -040057 const GrShaderVar& texelBufferVariable(TexelBufferHandle handle) const {
58 return this->uniformHandler()->texelBufferVariable(handle);
59 }
60
Brian Salomonf9f45122016-11-29 11:59:17 -050061 const GrShaderVar& imageStorageVariable(ImageStorageHandle handle) const {
62 return this->uniformHandler()->imageStorageVariable(handle);
63 }
64
egdaniel8dcdedc2015-11-11 06:27:20 -080065 // Handles for program uniforms (other than per-effect uniforms)
66 struct BuiltinUniformHandles {
67 UniformHandle fRTAdjustmentUni;
68
69 // We use the render target height to provide a y-down frag coord when specifying
70 // origin_upper_left is not supported.
71 UniformHandle fRTHeightUni;
72 };
73
egdaniel7ea439b2015-12-03 09:20:44 -080074 // Used to add a uniform for the RenderTarget height (used for frag position) without mangling
75 // the name of the uniform inside of a stage.
Ethan Nicholas941e7e22016-12-12 15:33:30 -050076 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080077
78 // Generates a name for a variable. The generated string will be name prefixed by the prefix
79 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
80 // explicitly asked not to.
81 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
82
egdaniel7ea439b2015-12-03 09:20:44 -080083 virtual GrGLSLUniformHandler* uniformHandler() = 0;
84 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080085 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
86
egdanielb80ec8b2016-02-09 09:54:43 -080087 // Used for backend customization of the output color and secondary color variables from the
88 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050089 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
90 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080091
egdaniel8dcdedc2015-11-11 06:27:20 -080092 // number of each input/output type in a single allocation block, used by many builders
93 static const int kVarsPerBlock;
94
egdaniel0eafe792015-11-20 14:01:22 -080095 GrGLSLVertexBuilder fVS;
96 GrGLSLGeometryBuilder fGS;
egdaniel2d721d32015-11-11 13:06:05 -080097 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080098
egdaniel8dcdedc2015-11-11 06:27:20 -080099 int fStageIndex;
100
egdaniel0e1853c2016-03-17 11:35:45 -0700101 const GrPipeline& fPipeline;
102 const GrPrimitiveProcessor& fPrimProc;
Ethan Nicholas38657112017-02-09 17:01:22 -0500103 GrProgramDesc* fDesc;
egdaniel8dcdedc2015-11-11 06:27:20 -0800104
egdaniel7ea439b2015-12-03 09:20:44 -0800105 BuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -0800106
Robert Phillips369e8b72017-08-01 16:13:04 -0400107 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
108 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
egdanielfa896322016-01-13 12:19:30 -0800109 GrGLSLFragProcs fFragmentProcessors;
110
egdaniel7ea439b2015-12-03 09:20:44 -0800111protected:
egdaniel0e1853c2016-03-17 11:35:45 -0700112 explicit GrGLSLProgramBuilder(const GrPipeline&,
113 const GrPrimitiveProcessor&,
Ethan Nicholas38657112017-02-09 17:01:22 -0500114 GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -0800115
cdalton9c3f1432016-03-11 10:07:37 -0800116 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
117
Ethan Nicholas2983f402017-05-08 09:36:08 -0400118 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800119
120 void cleanupFragmentProcessors();
121
egdaniel9f1d4152016-02-10 09:50:38 -0800122 void finalizeShaders();
123
egdanielfa896322016-01-13 12:19:30 -0800124private:
125 // reset is called by program creator between each processor's emit code. It increments the
126 // stage offset for variable name mangling, and also ensures verfication variables in the
127 // fragment shader are cleared.
128 void reset() {
129 this->addStage();
cdalton87332102016-02-26 12:22:02 -0800130 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800131 }
132 void addStage() { fStageIndex++; }
133
134 class AutoStageAdvance {
135 public:
136 AutoStageAdvance(GrGLSLProgramBuilder* pb)
137 : fPB(pb) {
138 fPB->reset();
139 // Each output to the fragment processor gets its own code section
140 fPB->fFS.nextStage();
141 }
142 ~AutoStageAdvance() {}
143 private:
144 GrGLSLProgramBuilder* fPB;
145 };
146
147 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400148 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800149
150 void emitAndInstallPrimProc(const GrPrimitiveProcessor&,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400151 SkString* outputColor,
152 SkString* outputCoverage);
153 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
154 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
155 int index,
156 int transformedCoordVarsIdx,
157 const SkString& input,
158 SkString output);
159 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Brian Salomonab015ef2017-04-04 10:15:51 -0400160 void emitSamplersAndImageStorages(const GrResourceIOProcessor& processor,
Brian Salomonf9f45122016-11-29 11:59:17 -0500161 SkTArray<SamplerHandle>* outTexSamplerHandles,
Greg Danielbc5d4d72017-05-05 10:28:42 -0400162 SkTArray<TexelBufferHandle>* outTexelBufferHandles,
Brian Salomonf9f45122016-11-29 11:59:17 -0500163 SkTArray<ImageStorageHandle>* outImageStorageHandles);
Brian Salomon18dfa982017-04-03 16:57:43 -0400164 SamplerHandle emitSampler(GrSLType samplerType, GrPixelConfig, const char* name,
165 GrShaderFlags visibility);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400166 TexelBufferHandle emitTexelBuffer(GrPixelConfig, const char* name, GrShaderFlags visibility);
Brian Salomonab015ef2017-04-04 10:15:51 -0400167 ImageStorageHandle emitImageStorage(const GrResourceIOProcessor::ImageStorageAccess&,
168 const char* name);
egdanielfa896322016-01-13 12:19:30 -0800169 void emitFSOutputSwizzle(bool hasSecondaryOutput);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400170 void updateSamplerCounts(GrShaderFlags visibility);
cdalton9c3f1432016-03-11 10:07:37 -0800171 bool checkSamplerCounts();
Brian Salomonf9f45122016-11-29 11:59:17 -0500172 bool checkImageStorageCounts();
egdanielfa896322016-01-13 12:19:30 -0800173
cdalton87332102016-02-26 12:22:02 -0800174#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800175 void verify(const GrPrimitiveProcessor&);
176 void verify(const GrXferProcessor&);
177 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800178#endif
egdanielfa896322016-01-13 12:19:30 -0800179
Greg Danielbc5d4d72017-05-05 10:28:42 -0400180 // These are used to check that we don't excede the allowable number of resources in a shader.
181 // The sampler counts include both normal texure samplers as well as texel buffers.
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