blob: de28bbdba1381617e403809bc0ca7e7d7c64b4b5 [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"
Robert Phillips901aff02019-10-08 12:32:56 -040013#include "src/gpu/GrProgramInfo.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
Robert Phillips03e4c952019-11-26 16:20:22 -050024class GrProgramDesc;
Brian Salomon99938a82016-11-21 13:41:08 -050025class GrShaderVar;
egdaniel0eafe792015-11-20 14:01:22 -080026class GrGLSLVaryingHandler;
Ethan Nicholas2983f402017-05-08 09:36:08 -040027class SkString;
Brian Salomon94efbf52016-11-29 13:43:05 -050028class GrShaderCaps;
egdaniel8dcdedc2015-11-11 06:27:20 -080029
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;
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
Robert Phillips901aff02019-10-08 12:32:56 -040040 GrSurfaceOrigin origin() const { return fProgramInfo.origin(); }
41 const GrPipeline& pipeline() const { return fProgramInfo.pipeline(); }
42 const GrPrimitiveProcessor& primitiveProcessor() const { return fProgramInfo.primProc(); }
Robert Phillips7de13332019-10-09 15:44:54 -040043 GrProcessor::CustomFeatures processorFeatures() const {
44 return fProgramInfo.requestedFeatures();
45 }
Robert Phillipsd5c1f342019-10-14 09:50:05 -040046 bool snapVerticesToPixelCenters() const {
47 return fProgramInfo.pipeline().snapVerticesToPixelCenters();
48 }
Robert Phillips373bda62019-11-12 13:30:05 -050049 bool hasPointSize() const { return fProgramInfo.primitiveType() == GrPrimitiveType::kPoints; }
Robert Phillips901aff02019-10-08 12:32:56 -040050
51 // TODO: stop passing in the renderTarget for just the sampleLocations
Chris Daltond7291ba2019-03-07 14:17:03 -070052 int effectiveSampleCnt() const {
Robert Phillips7de13332019-10-09 15:44:54 -040053 SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & fProgramInfo.requestedFeatures());
Chris Dalton8c4cafd2019-04-15 19:14:36 -060054 return fRenderTarget->renderTargetPriv().getSampleLocations().count();
Chris Daltond7291ba2019-03-07 14:17:03 -070055 }
Robert Phillips65a77752019-10-02 15:22:24 -040056 const SkTArray<SkPoint>& getSampleLocations() const {
57 return fRenderTarget->renderTargetPriv().getSampleLocations();
58 }
Robert Phillips901aff02019-10-08 12:32:56 -040059
Stephen Whiteb1857852020-02-07 15:33:23 +000060 const GrProgramDesc& desc() const { return fDesc; }
egdaniel7ea439b2015-12-03 09:20:44 -080061
cdalton5e58cee2016-02-11 12:49:47 -080062 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080063
Stephen Whited523a062019-06-19 13:12:46 -040064 const char* samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050065 return this->uniformHandler()->samplerVariable(handle);
66 }
67
68 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
Brian Salomon68ba1172019-06-05 11:15:08 -040069 if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
70 return this->uniformHandler()->samplerSwizzle(handle);
71 }
72 return GrSwizzle::RGBA();
Brian Salomon101b8442016-11-18 11:58:54 -050073 }
egdaniel09aa1fc2016-04-20 07:09:46 -070074
Ethan Nicholascd700e92018-08-24 16:43:57 -040075 // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
Greg Daniele6ab9982018-08-22 13:56:32 +000076 // the name of the uniform inside of a stage.
Ethan Nicholascd700e92018-08-24 16:43:57 -040077 void addRTWidthUniform(const char* name);
78
79 // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
80 // without mangling the name of the uniform inside of a stage.
Greg Daniele6ab9982018-08-22 13:56:32 +000081 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080082
83 // Generates a name for a variable. The generated string will be name prefixed by the prefix
84 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
85 // explicitly asked not to.
86 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
87
egdaniel7ea439b2015-12-03 09:20:44 -080088 virtual GrGLSLUniformHandler* uniformHandler() = 0;
89 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080090 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
91
egdanielb80ec8b2016-02-09 09:54:43 -080092 // Used for backend customization of the output color and secondary color variables from the
93 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050094 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
95 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080096
egdaniel8dcdedc2015-11-11 06:27:20 -080097 // number of each input/output type in a single allocation block, used by many builders
98 static const int kVarsPerBlock;
99
Greg Daniel9a51a862018-11-30 10:18:14 -0500100 GrGLSLVertexBuilder fVS;
101 GrGLSLGeometryBuilder fGS;
102 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -0800103
egdaniel8dcdedc2015-11-11 06:27:20 -0800104 int fStageIndex;
105
Robert Phillips901aff02019-10-08 12:32:56 -0400106 const GrRenderTarget* fRenderTarget; // TODO: remove this
Stephen Whiteb1857852020-02-07 15:33:23 +0000107 const GrProgramDesc& fDesc;
Robert Phillips901aff02019-10-08 12:32:56 -0400108 const GrProgramInfo& fProgramInfo;
egdaniel8dcdedc2015-11-11 06:27:20 -0800109
Greg Daniel9a51a862018-11-30 10:18:14 -0500110 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -0800111
Robert Phillips369e8b72017-08-01 16:13:04 -0400112 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
113 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400114 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
115 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -0800116
egdaniel7ea439b2015-12-03 09:20:44 -0800117protected:
Stephen Whiteb1857852020-02-07 15:33:23 +0000118 explicit GrGLSLProgramBuilder(GrRenderTarget*, const GrProgramDesc&, const GrProgramInfo&);
egdanielfa896322016-01-13 12:19:30 -0800119
cdalton9c3f1432016-03-11 10:07:37 -0800120 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
121
Ethan Nicholas2983f402017-05-08 09:36:08 -0400122 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800123
egdaniel9f1d4152016-02-10 09:50:38 -0800124 void finalizeShaders();
125
Brian Salomondc092132018-04-04 10:14:16 -0400126 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
127
egdanielfa896322016-01-13 12:19:30 -0800128private:
129 // reset is called by program creator between each processor's emit code. It increments the
130 // stage offset for variable name mangling, and also ensures verfication variables in the
131 // fragment shader are cleared.
132 void reset() {
133 this->addStage();
Chris Daltond7291ba2019-03-07 14:17:03 -0700134 SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();)
egdanielfa896322016-01-13 12:19:30 -0800135 }
136 void addStage() { fStageIndex++; }
137
138 class AutoStageAdvance {
139 public:
140 AutoStageAdvance(GrGLSLProgramBuilder* pb)
141 : fPB(pb) {
142 fPB->reset();
143 // Each output to the fragment processor gets its own code section
144 fPB->fFS.nextStage();
145 }
146 ~AutoStageAdvance() {}
147 private:
148 GrGLSLProgramBuilder* fPB;
149 };
150
151 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400152 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800153
Greg Daniel9a51a862018-11-30 10:18:14 -0500154 void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400155 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
156 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
157 int index,
158 int transformedCoordVarsIdx,
159 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400160 SkString output,
161 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400162 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Brian Salomonccb61422020-01-09 10:46:36 -0500163 SamplerHandle emitSampler(const GrSurfaceProxy*, GrSamplerState, const GrSwizzle&,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400164 const char* name);
cdalton9c3f1432016-03-11 10:07:37 -0800165 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800166
cdalton87332102016-02-26 12:22:02 -0800167#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800168 void verify(const GrPrimitiveProcessor&);
egdanielfa896322016-01-13 12:19:30 -0800169 void verify(const GrFragmentProcessor&);
Chris Daltond7291ba2019-03-07 14:17:03 -0700170 void verify(const GrXferProcessor&);
cdalton87332102016-02-26 12:22:02 -0800171#endif
egdanielfa896322016-01-13 12:19:30 -0800172
Greg Danielbc5d4d72017-05-05 10:28:42 -0400173 // 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 -0400174 int fNumFragmentSamplers;
175 SkSTArray<4, GrGLSLPrimitiveProcessor::TransformVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800176};
177
178#endif