blob: d470b69ebd426219efda20cd40aac2cb13a43d46 [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"
Robert Phillips901aff02019-10-08 12:32:56 -040014#include "src/gpu/GrProgramInfo.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040015#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrRenderTargetPriv.h"
17#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
18#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
19#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
20#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
21#include "src/gpu/glsl/GrGLSLUniformHandler.h"
22#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
23#include "src/gpu/glsl/GrGLSLXferProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080024
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 int numSamples() const { return fProgramInfo.numSamples(); }
41 GrSurfaceOrigin origin() const { return fProgramInfo.origin(); }
42 const GrPipeline& pipeline() const { return fProgramInfo.pipeline(); }
43 const GrPrimitiveProcessor& primitiveProcessor() const { return fProgramInfo.primProc(); }
Robert Phillips7de13332019-10-09 15:44:54 -040044 GrProcessor::CustomFeatures processorFeatures() const {
45 return fProgramInfo.requestedFeatures();
46 }
Robert Phillips901aff02019-10-08 12:32:56 -040047
48 // TODO: stop passing in the renderTarget for just the sampleLocations
Chris Daltond7291ba2019-03-07 14:17:03 -070049 int effectiveSampleCnt() const {
Robert Phillips7de13332019-10-09 15:44:54 -040050 SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & fProgramInfo.requestedFeatures());
Chris Dalton8c4cafd2019-04-15 19:14:36 -060051 return fRenderTarget->renderTargetPriv().getSampleLocations().count();
Chris Daltond7291ba2019-03-07 14:17:03 -070052 }
Robert Phillips65a77752019-10-02 15:22:24 -040053 const SkTArray<SkPoint>& getSampleLocations() const {
54 return fRenderTarget->renderTargetPriv().getSampleLocations();
55 }
Robert Phillips901aff02019-10-08 12:32:56 -040056
57 const GrProgramDesc* desc() const { return fDesc; }
Ethan Nicholas38657112017-02-09 17:01:22 -050058 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080059
cdalton5e58cee2016-02-11 12:49:47 -080060 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080061
Stephen Whited523a062019-06-19 13:12:46 -040062 const char* samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050063 return this->uniformHandler()->samplerVariable(handle);
64 }
65
66 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
Brian Salomon68ba1172019-06-05 11:15:08 -040067 if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
68 return this->uniformHandler()->samplerSwizzle(handle);
69 }
70 return GrSwizzle::RGBA();
Brian Salomon101b8442016-11-18 11:58:54 -050071 }
egdaniel09aa1fc2016-04-20 07:09:46 -070072
Ethan Nicholascd700e92018-08-24 16:43:57 -040073 // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
Greg Daniele6ab9982018-08-22 13:56:32 +000074 // the name of the uniform inside of a stage.
Ethan Nicholascd700e92018-08-24 16:43:57 -040075 void addRTWidthUniform(const char* name);
76
77 // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
78 // without mangling the name of the uniform inside of a stage.
Greg Daniele6ab9982018-08-22 13:56:32 +000079 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080080
81 // Generates a name for a variable. The generated string will be name prefixed by the prefix
82 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
83 // explicitly asked not to.
84 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
85
egdaniel7ea439b2015-12-03 09:20:44 -080086 virtual GrGLSLUniformHandler* uniformHandler() = 0;
87 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080088 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
89
egdanielb80ec8b2016-02-09 09:54:43 -080090 // Used for backend customization of the output color and secondary color variables from the
91 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050092 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
93 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080094
egdaniel8dcdedc2015-11-11 06:27:20 -080095 // number of each input/output type in a single allocation block, used by many builders
96 static const int kVarsPerBlock;
97
Greg Daniel9a51a862018-11-30 10:18:14 -050098 GrGLSLVertexBuilder fVS;
99 GrGLSLGeometryBuilder fGS;
100 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -0800101
egdaniel8dcdedc2015-11-11 06:27:20 -0800102 int fStageIndex;
103
Robert Phillips901aff02019-10-08 12:32:56 -0400104 const GrRenderTarget* fRenderTarget; // TODO: remove this
105 const GrProgramInfo& fProgramInfo;
egdaniel8dcdedc2015-11-11 06:27:20 -0800106
Robert Phillips901aff02019-10-08 12:32:56 -0400107 const GrProgramDesc* fDesc;
Greg Daniel9a51a862018-11-30 10:18:14 -0500108
109 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -0800110
Robert Phillips369e8b72017-08-01 16:13:04 -0400111 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
112 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400113 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
114 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -0800115
egdaniel7ea439b2015-12-03 09:20:44 -0800116protected:
Robert Phillips901aff02019-10-08 12:32:56 -0400117 explicit GrGLSLProgramBuilder(GrRenderTarget*, const GrProgramInfo&, const GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -0800118
cdalton9c3f1432016-03-11 10:07:37 -0800119 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
120
Ethan Nicholas2983f402017-05-08 09:36:08 -0400121 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800122
egdaniel9f1d4152016-02-10 09:50:38 -0800123 void finalizeShaders();
124
Brian Salomondc092132018-04-04 10:14:16 -0400125 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
126
egdanielfa896322016-01-13 12:19:30 -0800127private:
128 // reset is called by program creator between each processor's emit code. It increments the
129 // stage offset for variable name mangling, and also ensures verfication variables in the
130 // fragment shader are cleared.
131 void reset() {
132 this->addStage();
Chris Daltond7291ba2019-03-07 14:17:03 -0700133 SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();)
egdanielfa896322016-01-13 12:19:30 -0800134 }
135 void addStage() { fStageIndex++; }
136
137 class AutoStageAdvance {
138 public:
139 AutoStageAdvance(GrGLSLProgramBuilder* pb)
140 : fPB(pb) {
141 fPB->reset();
142 // Each output to the fragment processor gets its own code section
143 fPB->fFS.nextStage();
144 }
145 ~AutoStageAdvance() {}
146 private:
147 GrGLSLProgramBuilder* fPB;
148 };
149
150 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400151 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800152
Greg Daniel9a51a862018-11-30 10:18:14 -0500153 void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400154 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
155 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
156 int index,
157 int transformedCoordVarsIdx,
158 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400159 SkString output,
160 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400161 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Robert Phillips66ae3042019-10-10 12:57:58 -0400162 SamplerHandle emitSampler(const GrTextureProxy*, const GrSamplerState&, const GrSwizzle&,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400163 const char* name);
cdalton9c3f1432016-03-11 10:07:37 -0800164 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800165
cdalton87332102016-02-26 12:22:02 -0800166#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800167 void verify(const GrPrimitiveProcessor&);
egdanielfa896322016-01-13 12:19:30 -0800168 void verify(const GrFragmentProcessor&);
Chris Daltond7291ba2019-03-07 14:17:03 -0700169 void verify(const GrXferProcessor&);
cdalton87332102016-02-26 12:22:02 -0800170#endif
egdanielfa896322016-01-13 12:19:30 -0800171
Greg Danielbc5d4d72017-05-05 10:28:42 -0400172 // 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 -0400173 int fNumFragmentSamplers;
174 SkSTArray<4, GrGLSLPrimitiveProcessor::TransformVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800175};
176
177#endif