blob: 4fbee102b2f8d91fc9ef8276465a37801ad8f73d [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
Brian Salomon94efbf52016-11-29 13:43:05 -050011#include "GrCaps.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080012#include "GrGeometryProcessor.h"
Robert Phillips646e4292017-06-13 12:44:56 -040013#include "GrProgramDesc.h"
robertphillips28a838e2016-06-23 14:07:00 -070014#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080015#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080016#include "glsl/GrGLSLPrimitiveProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080017#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080018#include "glsl/GrGLSLUniformHandler.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060019#include "glsl/GrGLSLVertexGeoBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080020#include "glsl/GrGLSLXferProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080021
Brian Salomon99938a82016-11-21 13:41:08 -050022class GrShaderVar;
egdaniel0eafe792015-11-20 14:01:22 -080023class GrGLSLVaryingHandler;
Ethan Nicholas2983f402017-05-08 09:36:08 -040024class SkString;
Brian Salomon94efbf52016-11-29 13:43:05 -050025class GrShaderCaps;
egdaniel8dcdedc2015-11-11 06:27:20 -080026
egdaniel7ea439b2015-12-03 09:20:44 -080027class GrGLSLProgramBuilder {
egdaniel8dcdedc2015-11-11 06:27:20 -080028public:
Brian Salomonf9f45122016-11-29 11:59:17 -050029 using UniformHandle = GrGLSLUniformHandler::UniformHandle;
30 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
egdaniel7ea439b2015-12-03 09:20:44 -080031
32 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080033
egdanielfa896322016-01-13 12:19:30 -080034 virtual const GrCaps* caps() const = 0;
Brian Salomon94efbf52016-11-29 13:43:05 -050035 const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
egdaniela2e3e0f2015-11-19 07:23:45 -080036
egdaniel0e1853c2016-03-17 11:35:45 -070037 const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
Greg Daniel9a51a862018-11-30 10:18:14 -050038 const GrTextureProxy* const* primProcProxies() const { return fPrimProcProxies; }
Robert Phillipsd0fe8752019-01-31 14:13:59 -050039 GrPixelConfig config() const { return fConfig; }
40 int numColorSamples() const { return fNumColorSamples; }
41 GrSurfaceOrigin origin() const { return fOrigin; }
egdaniel0e1853c2016-03-17 11:35:45 -070042 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050043 GrProgramDesc* desc() { return fDesc; }
44 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080045
cdalton5e58cee2016-02-11 12:49:47 -080046 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080047
Brian Salomon99938a82016-11-21 13:41:08 -050048 const GrShaderVar& samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050049 return this->uniformHandler()->samplerVariable(handle);
50 }
51
52 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
53 return this->uniformHandler()->samplerSwizzle(handle);
54 }
egdaniel09aa1fc2016-04-20 07:09:46 -070055
Ethan Nicholascd700e92018-08-24 16:43:57 -040056 // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
Greg Daniele6ab9982018-08-22 13:56:32 +000057 // the name of the uniform inside of a stage.
Ethan Nicholascd700e92018-08-24 16:43:57 -040058 void addRTWidthUniform(const char* name);
59
60 // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
61 // without mangling the name of the uniform inside of a stage.
Greg Daniele6ab9982018-08-22 13:56:32 +000062 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080063
64 // Generates a name for a variable. The generated string will be name prefixed by the prefix
65 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
66 // explicitly asked not to.
67 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
68
egdaniel7ea439b2015-12-03 09:20:44 -080069 virtual GrGLSLUniformHandler* uniformHandler() = 0;
70 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080071 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
72
egdanielb80ec8b2016-02-09 09:54:43 -080073 // Used for backend customization of the output color and secondary color variables from the
74 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050075 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
76 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080077
egdaniel8dcdedc2015-11-11 06:27:20 -080078 // number of each input/output type in a single allocation block, used by many builders
79 static const int kVarsPerBlock;
80
Greg Daniel9a51a862018-11-30 10:18:14 -050081 GrGLSLVertexBuilder fVS;
82 GrGLSLGeometryBuilder fGS;
83 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080084
egdaniel8dcdedc2015-11-11 06:27:20 -080085 int fStageIndex;
86
Robert Phillipsd0fe8752019-01-31 14:13:59 -050087 const GrPixelConfig fConfig;
88 const int fNumColorSamples;
89 const GrSurfaceOrigin fOrigin;
Greg Daniel9a51a862018-11-30 10:18:14 -050090 const GrPipeline& fPipeline;
91 const GrPrimitiveProcessor& fPrimProc;
92 const GrTextureProxy* const* fPrimProcProxies;
egdaniel8dcdedc2015-11-11 06:27:20 -080093
Greg Daniel9a51a862018-11-30 10:18:14 -050094 GrProgramDesc* fDesc;
95
96 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -080097
Robert Phillips369e8b72017-08-01 16:13:04 -040098 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
99 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400100 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
101 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -0800102
egdaniel7ea439b2015-12-03 09:20:44 -0800103protected:
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500104 explicit GrGLSLProgramBuilder(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
105 const GrPrimitiveProcessor&,
Greg Daniel9a51a862018-11-30 10:18:14 -0500106 const GrTextureProxy* const primProcProxies[],
107 const GrPipeline&,
108 GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -0800109
cdalton9c3f1432016-03-11 10:07:37 -0800110 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
111
Ethan Nicholas2983f402017-05-08 09:36:08 -0400112 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800113
egdaniel9f1d4152016-02-10 09:50:38 -0800114 void finalizeShaders();
115
Brian Salomondc092132018-04-04 10:14:16 -0400116 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
117
egdanielfa896322016-01-13 12:19:30 -0800118private:
119 // reset is called by program creator between each processor's emit code. It increments the
120 // stage offset for variable name mangling, and also ensures verfication variables in the
121 // fragment shader are cleared.
122 void reset() {
123 this->addStage();
cdalton87332102016-02-26 12:22:02 -0800124 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800125 }
126 void addStage() { fStageIndex++; }
127
128 class AutoStageAdvance {
129 public:
130 AutoStageAdvance(GrGLSLProgramBuilder* pb)
131 : fPB(pb) {
132 fPB->reset();
133 // Each output to the fragment processor gets its own code section
134 fPB->fFS.nextStage();
135 }
136 ~AutoStageAdvance() {}
137 private:
138 GrGLSLProgramBuilder* fPB;
139 };
140
141 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400142 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800143
Greg Daniel9a51a862018-11-30 10:18:14 -0500144 void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400145 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
146 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
147 int index,
148 int transformedCoordVarsIdx,
149 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400150 SkString output,
151 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400152 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Greg Daniel9a51a862018-11-30 10:18:14 -0500153 SamplerHandle emitSampler(const GrTexture*, const GrSamplerState&, const char* name);
egdanielfa896322016-01-13 12:19:30 -0800154 void emitFSOutputSwizzle(bool hasSecondaryOutput);
cdalton9c3f1432016-03-11 10:07:37 -0800155 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800156
cdalton87332102016-02-26 12:22:02 -0800157#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800158 void verify(const GrPrimitiveProcessor&);
159 void verify(const GrXferProcessor&);
160 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800161#endif
egdanielfa896322016-01-13 12:19:30 -0800162
Greg Danielbc5d4d72017-05-05 10:28:42 -0400163 // These are used to check that we don't excede the allowable number of resources in a shader.
bsalomona624bf32016-09-20 09:12:47 -0700164 int fNumFragmentSamplers;
165 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800166};
167
168#endif