blob: dc63aa82aa1b2b86de4b689a8464c4e3e99946cb [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 Phillipsd5c1f342019-10-14 09:50:05 -040047 bool snapVerticesToPixelCenters() const {
48 return fProgramInfo.pipeline().snapVerticesToPixelCenters();
49 }
50 // TODO: remove this usage of the descriptor's header
51 bool hasPointSize() const { return fDesc->hasPointSize(); }
Robert Phillips901aff02019-10-08 12:32:56 -040052
53 // TODO: stop passing in the renderTarget for just the sampleLocations
Chris Daltond7291ba2019-03-07 14:17:03 -070054 int effectiveSampleCnt() const {
Robert Phillips7de13332019-10-09 15:44:54 -040055 SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & fProgramInfo.requestedFeatures());
Chris Dalton8c4cafd2019-04-15 19:14:36 -060056 return fRenderTarget->renderTargetPriv().getSampleLocations().count();
Chris Daltond7291ba2019-03-07 14:17:03 -070057 }
Robert Phillips65a77752019-10-02 15:22:24 -040058 const SkTArray<SkPoint>& getSampleLocations() const {
59 return fRenderTarget->renderTargetPriv().getSampleLocations();
60 }
Robert Phillips901aff02019-10-08 12:32:56 -040061
62 const GrProgramDesc* desc() const { return fDesc; }
egdaniel7ea439b2015-12-03 09:20:44 -080063
cdalton5e58cee2016-02-11 12:49:47 -080064 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080065
Stephen Whited523a062019-06-19 13:12:46 -040066 const char* samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050067 return this->uniformHandler()->samplerVariable(handle);
68 }
69
70 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
Brian Salomon68ba1172019-06-05 11:15:08 -040071 if (this->caps()->shaderCaps()->textureSwizzleAppliedInShader()) {
72 return this->uniformHandler()->samplerSwizzle(handle);
73 }
74 return GrSwizzle::RGBA();
Brian Salomon101b8442016-11-18 11:58:54 -050075 }
egdaniel09aa1fc2016-04-20 07:09:46 -070076
Ethan Nicholascd700e92018-08-24 16:43:57 -040077 // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
Greg Daniele6ab9982018-08-22 13:56:32 +000078 // the name of the uniform inside of a stage.
Ethan Nicholascd700e92018-08-24 16:43:57 -040079 void addRTWidthUniform(const char* name);
80
81 // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
82 // without mangling the name of the uniform inside of a stage.
Greg Daniele6ab9982018-08-22 13:56:32 +000083 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080084
85 // Generates a name for a variable. The generated string will be name prefixed by the prefix
86 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
87 // explicitly asked not to.
88 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
89
egdaniel7ea439b2015-12-03 09:20:44 -080090 virtual GrGLSLUniformHandler* uniformHandler() = 0;
91 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080092 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
93
egdanielb80ec8b2016-02-09 09:54:43 -080094 // Used for backend customization of the output color and secondary color variables from the
95 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050096 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
97 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080098
egdaniel8dcdedc2015-11-11 06:27:20 -080099 // number of each input/output type in a single allocation block, used by many builders
100 static const int kVarsPerBlock;
101
Greg Daniel9a51a862018-11-30 10:18:14 -0500102 GrGLSLVertexBuilder fVS;
103 GrGLSLGeometryBuilder fGS;
104 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -0800105
egdaniel8dcdedc2015-11-11 06:27:20 -0800106 int fStageIndex;
107
Robert Phillips901aff02019-10-08 12:32:56 -0400108 const GrRenderTarget* fRenderTarget; // TODO: remove this
109 const GrProgramInfo& fProgramInfo;
egdaniel8dcdedc2015-11-11 06:27:20 -0800110
Robert Phillips901aff02019-10-08 12:32:56 -0400111 const GrProgramDesc* fDesc;
Greg Daniel9a51a862018-11-30 10:18:14 -0500112
113 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -0800114
Robert Phillips369e8b72017-08-01 16:13:04 -0400115 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
116 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400117 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
118 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -0800119
egdaniel7ea439b2015-12-03 09:20:44 -0800120protected:
Robert Phillips901aff02019-10-08 12:32:56 -0400121 explicit GrGLSLProgramBuilder(GrRenderTarget*, const GrProgramInfo&, const 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);
Robert Phillips66ae3042019-10-10 12:57:58 -0400166 SamplerHandle emitSampler(const GrTextureProxy*, const GrSamplerState&, const GrSwizzle&,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400167 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