blob: dec6d115805be642305a3ec432504666fd208fc8 [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 int effectiveSampleCnt() const {
42 SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & header().processorFeatures());
Chris Dalton8c4cafd2019-04-15 19:14:36 -060043 return fRenderTarget->renderTargetPriv().getSampleLocations().count();
Chris Daltond7291ba2019-03-07 14:17:03 -070044 }
Robert Phillips65a77752019-10-02 15:22:24 -040045 const SkTArray<SkPoint>& getSampleLocations() const {
46 return fRenderTarget->renderTargetPriv().getSampleLocations();
47 }
48 int numSamples() const { return fNumSamples; }
Robert Phillipsd0fe8752019-01-31 14:13:59 -050049 GrSurfaceOrigin origin() const { return fOrigin; }
egdaniel0e1853c2016-03-17 11:35:45 -070050 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050051 GrProgramDesc* desc() { return fDesc; }
52 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080053
cdalton5e58cee2016-02-11 12:49:47 -080054 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080055
Stephen Whited523a062019-06-19 13:12:46 -040056 const char* samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050057 return this->uniformHandler()->samplerVariable(handle);
58 }
59
60 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
Brian Salomon68ba1172019-06-05 11:15:08 -040061 if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
62 return this->uniformHandler()->samplerSwizzle(handle);
63 }
64 return GrSwizzle::RGBA();
Brian Salomon101b8442016-11-18 11:58:54 -050065 }
egdaniel09aa1fc2016-04-20 07:09:46 -070066
Ethan Nicholascd700e92018-08-24 16:43:57 -040067 // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
Greg Daniele6ab9982018-08-22 13:56:32 +000068 // the name of the uniform inside of a stage.
Ethan Nicholascd700e92018-08-24 16:43:57 -040069 void addRTWidthUniform(const char* name);
70
71 // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
72 // without mangling the name of the uniform inside of a stage.
Greg Daniele6ab9982018-08-22 13:56:32 +000073 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080074
75 // Generates a name for a variable. The generated string will be name prefixed by the prefix
76 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
77 // explicitly asked not to.
78 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
79
egdaniel7ea439b2015-12-03 09:20:44 -080080 virtual GrGLSLUniformHandler* uniformHandler() = 0;
81 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080082 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
83
egdanielb80ec8b2016-02-09 09:54:43 -080084 // Used for backend customization of the output color and secondary color variables from the
85 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050086 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
87 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080088
egdaniel8dcdedc2015-11-11 06:27:20 -080089 // number of each input/output type in a single allocation block, used by many builders
90 static const int kVarsPerBlock;
91
Greg Daniel9a51a862018-11-30 10:18:14 -050092 GrGLSLVertexBuilder fVS;
93 GrGLSLGeometryBuilder fGS;
94 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080095
egdaniel8dcdedc2015-11-11 06:27:20 -080096 int fStageIndex;
97
Chris Daltond7291ba2019-03-07 14:17:03 -070098 const GrRenderTarget* fRenderTarget;
Robert Phillips65a77752019-10-02 15:22:24 -040099 const int fNumSamples;
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500100 const GrSurfaceOrigin fOrigin;
Greg Daniel9a51a862018-11-30 10:18:14 -0500101 const GrPipeline& fPipeline;
102 const GrPrimitiveProcessor& fPrimProc;
103 const GrTextureProxy* const* fPrimProcProxies;
egdaniel8dcdedc2015-11-11 06:27:20 -0800104
Greg Daniel9a51a862018-11-30 10:18:14 -0500105 GrProgramDesc* fDesc;
106
107 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -0800108
Robert Phillips369e8b72017-08-01 16:13:04 -0400109 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
110 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400111 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
112 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -0800113
egdaniel7ea439b2015-12-03 09:20:44 -0800114protected:
Robert Phillips65a77752019-10-02 15:22:24 -0400115 explicit GrGLSLProgramBuilder(GrRenderTarget* renderTarget,
116 int numSamples,
117 GrSurfaceOrigin origin,
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500118 const GrPrimitiveProcessor&,
Greg Daniel9a51a862018-11-30 10:18:14 -0500119 const GrTextureProxy* const primProcProxies[],
120 const GrPipeline&,
121 GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -0800122
cdalton9c3f1432016-03-11 10:07:37 -0800123 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
124
Ethan Nicholas2983f402017-05-08 09:36:08 -0400125 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800126
egdaniel9f1d4152016-02-10 09:50:38 -0800127 void finalizeShaders();
128
Brian Salomondc092132018-04-04 10:14:16 -0400129 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
130
egdanielfa896322016-01-13 12:19:30 -0800131private:
132 // reset is called by program creator between each processor's emit code. It increments the
133 // stage offset for variable name mangling, and also ensures verfication variables in the
134 // fragment shader are cleared.
135 void reset() {
136 this->addStage();
Chris Daltond7291ba2019-03-07 14:17:03 -0700137 SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();)
egdanielfa896322016-01-13 12:19:30 -0800138 }
139 void addStage() { fStageIndex++; }
140
141 class AutoStageAdvance {
142 public:
143 AutoStageAdvance(GrGLSLProgramBuilder* pb)
144 : fPB(pb) {
145 fPB->reset();
146 // Each output to the fragment processor gets its own code section
147 fPB->fFS.nextStage();
148 }
149 ~AutoStageAdvance() {}
150 private:
151 GrGLSLProgramBuilder* fPB;
152 };
153
154 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400155 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800156
Greg Daniel9a51a862018-11-30 10:18:14 -0500157 void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400158 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
159 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
160 int index,
161 int transformedCoordVarsIdx,
162 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400163 SkString output,
164 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400165 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Greg Daniel2c3398d2019-06-19 11:58:01 -0400166 SamplerHandle emitSampler(const GrTexture*, const GrSamplerState&, const GrSwizzle&,
167 const char* name);
cdalton9c3f1432016-03-11 10:07:37 -0800168 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800169
cdalton87332102016-02-26 12:22:02 -0800170#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800171 void verify(const GrPrimitiveProcessor&);
egdanielfa896322016-01-13 12:19:30 -0800172 void verify(const GrFragmentProcessor&);
Chris Daltond7291ba2019-03-07 14:17:03 -0700173 void verify(const GrXferProcessor&);
cdalton87332102016-02-26 12:22:02 -0800174#endif
egdanielfa896322016-01-13 12:19:30 -0800175
Greg Danielbc5d4d72017-05-05 10:28:42 -0400176 // 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 -0400177 int fNumFragmentSamplers;
178 SkSTArray<4, GrGLSLPrimitiveProcessor::TransformVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800179};
180
181#endif