blob: 9f5e3a369d2f2670a31c6ce1cd45f7119133e3cd [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrGeometryProcessor.h"
13#include "src/gpu/GrProgramDesc.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040014#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrRenderTargetPriv.h"
16#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
17#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
18#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
19#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
20#include "src/gpu/glsl/GrGLSLUniformHandler.h"
21#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
22#include "src/gpu/glsl/GrGLSLXferProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080023
Brian Salomon99938a82016-11-21 13:41:08 -050024class GrShaderVar;
egdaniel0eafe792015-11-20 14:01:22 -080025class GrGLSLVaryingHandler;
Ethan Nicholas2983f402017-05-08 09:36:08 -040026class SkString;
Brian Salomon94efbf52016-11-29 13:43:05 -050027class GrShaderCaps;
egdaniel8dcdedc2015-11-11 06:27:20 -080028
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;
egdaniel7ea439b2015-12-03 09:20:44 -080033
34 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080035
egdanielfa896322016-01-13 12:19:30 -080036 virtual const GrCaps* caps() const = 0;
Brian Salomon94efbf52016-11-29 13:43:05 -050037 const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
egdaniela2e3e0f2015-11-19 07:23:45 -080038
egdaniel0e1853c2016-03-17 11:35:45 -070039 const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
Greg Daniel9a51a862018-11-30 10:18:14 -050040 const GrTextureProxy* const* primProcProxies() const { return fPrimProcProxies; }
Chris Daltond7291ba2019-03-07 14:17:03 -070041 const GrRenderTarget* renderTarget() const { return fRenderTarget; }
42 GrPixelConfig config() const { return fRenderTarget->config(); }
43 int effectiveSampleCnt() const {
44 SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & header().processorFeatures());
Chris Dalton8c4cafd2019-04-15 19:14:36 -060045 return fRenderTarget->renderTargetPriv().getSampleLocations().count();
Chris Daltond7291ba2019-03-07 14:17:03 -070046 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -050047 GrSurfaceOrigin origin() const { return fOrigin; }
egdaniel0e1853c2016-03-17 11:35:45 -070048 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050049 GrProgramDesc* desc() { return fDesc; }
50 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080051
cdalton5e58cee2016-02-11 12:49:47 -080052 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080053
Stephen Whited523a062019-06-19 13:12:46 -040054 const char* samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050055 return this->uniformHandler()->samplerVariable(handle);
56 }
57
58 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
Brian Salomon68ba1172019-06-05 11:15:08 -040059 if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
60 return this->uniformHandler()->samplerSwizzle(handle);
61 }
62 return GrSwizzle::RGBA();
Brian Salomon101b8442016-11-18 11:58:54 -050063 }
egdaniel09aa1fc2016-04-20 07:09:46 -070064
Ethan Nicholascd700e92018-08-24 16:43:57 -040065 // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
Greg Daniele6ab9982018-08-22 13:56:32 +000066 // the name of the uniform inside of a stage.
Ethan Nicholascd700e92018-08-24 16:43:57 -040067 void addRTWidthUniform(const char* name);
68
69 // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
70 // without mangling the name of the uniform inside of a stage.
Greg Daniele6ab9982018-08-22 13:56:32 +000071 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080072
73 // Generates a name for a variable. The generated string will be name prefixed by the prefix
74 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
75 // explicitly asked not to.
76 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
77
egdaniel7ea439b2015-12-03 09:20:44 -080078 virtual GrGLSLUniformHandler* uniformHandler() = 0;
79 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080080 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
81
egdanielb80ec8b2016-02-09 09:54:43 -080082 // Used for backend customization of the output color and secondary color variables from the
83 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050084 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
85 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080086
egdaniel8dcdedc2015-11-11 06:27:20 -080087 // number of each input/output type in a single allocation block, used by many builders
88 static const int kVarsPerBlock;
89
Greg Daniel9a51a862018-11-30 10:18:14 -050090 GrGLSLVertexBuilder fVS;
91 GrGLSLGeometryBuilder fGS;
92 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080093
egdaniel8dcdedc2015-11-11 06:27:20 -080094 int fStageIndex;
95
Chris Daltond7291ba2019-03-07 14:17:03 -070096 const GrRenderTarget* fRenderTarget;
Robert Phillipsd0fe8752019-01-31 14:13:59 -050097 const GrSurfaceOrigin fOrigin;
Greg Daniel9a51a862018-11-30 10:18:14 -050098 const GrPipeline& fPipeline;
99 const GrPrimitiveProcessor& fPrimProc;
100 const GrTextureProxy* const* fPrimProcProxies;
egdaniel8dcdedc2015-11-11 06:27:20 -0800101
Greg Daniel9a51a862018-11-30 10:18:14 -0500102 GrProgramDesc* fDesc;
103
104 GrGLSLBuiltinUniformHandles 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;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400108 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
109 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -0800110
egdaniel7ea439b2015-12-03 09:20:44 -0800111protected:
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500112 explicit GrGLSLProgramBuilder(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
113 const GrPrimitiveProcessor&,
Greg Daniel9a51a862018-11-30 10:18:14 -0500114 const GrTextureProxy* const primProcProxies[],
115 const GrPipeline&,
116 GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -0800117
cdalton9c3f1432016-03-11 10:07:37 -0800118 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
119
Ethan Nicholas2983f402017-05-08 09:36:08 -0400120 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800121
egdaniel9f1d4152016-02-10 09:50:38 -0800122 void finalizeShaders();
123
Brian Salomondc092132018-04-04 10:14:16 -0400124 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
125
egdanielfa896322016-01-13 12:19:30 -0800126private:
127 // reset is called by program creator between each processor's emit code. It increments the
128 // stage offset for variable name mangling, and also ensures verfication variables in the
129 // fragment shader are cleared.
130 void reset() {
131 this->addStage();
Chris Daltond7291ba2019-03-07 14:17:03 -0700132 SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();)
egdanielfa896322016-01-13 12:19:30 -0800133 }
134 void addStage() { fStageIndex++; }
135
136 class AutoStageAdvance {
137 public:
138 AutoStageAdvance(GrGLSLProgramBuilder* pb)
139 : fPB(pb) {
140 fPB->reset();
141 // Each output to the fragment processor gets its own code section
142 fPB->fFS.nextStage();
143 }
144 ~AutoStageAdvance() {}
145 private:
146 GrGLSLProgramBuilder* fPB;
147 };
148
149 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400150 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800151
Greg Daniel9a51a862018-11-30 10:18:14 -0500152 void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400153 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
154 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
155 int index,
156 int transformedCoordVarsIdx,
157 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400158 SkString output,
159 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400160 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400161 SamplerHandle emitSampler(const GrTexture*, const GrSamplerState&, const GrSwizzle&,
162 const char* name);
cdalton9c3f1432016-03-11 10:07:37 -0800163 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800164
cdalton87332102016-02-26 12:22:02 -0800165#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800166 void verify(const GrPrimitiveProcessor&);
egdanielfa896322016-01-13 12:19:30 -0800167 void verify(const GrFragmentProcessor&);
Chris Daltond7291ba2019-03-07 14:17:03 -0700168 void verify(const GrXferProcessor&);
cdalton87332102016-02-26 12:22:02 -0800169#endif
egdanielfa896322016-01-13 12:19:30 -0800170
Greg Danielbc5d4d72017-05-05 10:28:42 -0400171 // These are used to check that we don't excede the allowable number of resources in a shader.
Ethan Nicholasd4efe682019-08-29 16:10:13 -0400172 int fNumFragmentSamplers;
173 SkSTArray<4, GrGLSLPrimitiveProcessor::TransformVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800174};
175
176#endif