blob: 073cfc6785e08436ba4496f009bd10e35290f330 [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; }
38 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050039 GrProgramDesc* desc() { return fDesc; }
40 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080041
cdalton5e58cee2016-02-11 12:49:47 -080042 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080043
Brian Salomon99938a82016-11-21 13:41:08 -050044 const GrShaderVar& samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050045 return this->uniformHandler()->samplerVariable(handle);
46 }
47
48 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
49 return this->uniformHandler()->samplerSwizzle(handle);
50 }
egdaniel09aa1fc2016-04-20 07:09:46 -070051
Ethan Nicholascd700e92018-08-24 16:43:57 -040052 // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
Greg Daniele6ab9982018-08-22 13:56:32 +000053 // the name of the uniform inside of a stage.
Ethan Nicholascd700e92018-08-24 16:43:57 -040054 void addRTWidthUniform(const char* name);
55
56 // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
57 // without mangling the name of the uniform inside of a stage.
Greg Daniele6ab9982018-08-22 13:56:32 +000058 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080059
60 // Generates a name for a variable. The generated string will be name prefixed by the prefix
61 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
62 // explicitly asked not to.
63 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
64
egdaniel7ea439b2015-12-03 09:20:44 -080065 virtual GrGLSLUniformHandler* uniformHandler() = 0;
66 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080067 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
68
egdanielb80ec8b2016-02-09 09:54:43 -080069 // Used for backend customization of the output color and secondary color variables from the
70 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050071 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
72 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080073
egdaniel8dcdedc2015-11-11 06:27:20 -080074 // number of each input/output type in a single allocation block, used by many builders
75 static const int kVarsPerBlock;
76
egdaniel0eafe792015-11-20 14:01:22 -080077 GrGLSLVertexBuilder fVS;
78 GrGLSLGeometryBuilder fGS;
egdaniel2d721d32015-11-11 13:06:05 -080079 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080080
egdaniel8dcdedc2015-11-11 06:27:20 -080081 int fStageIndex;
82
egdaniel0e1853c2016-03-17 11:35:45 -070083 const GrPipeline& fPipeline;
84 const GrPrimitiveProcessor& fPrimProc;
Ethan Nicholas38657112017-02-09 17:01:22 -050085 GrProgramDesc* fDesc;
egdaniel8dcdedc2015-11-11 06:27:20 -080086
Brian Salomon1471df92018-06-08 10:49:00 -040087 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -080088
Robert Phillips369e8b72017-08-01 16:13:04 -040089 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
90 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -040091 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
92 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -080093
egdaniel7ea439b2015-12-03 09:20:44 -080094protected:
Brian Salomonff168d92018-06-23 15:17:27 -040095 explicit GrGLSLProgramBuilder(const GrPrimitiveProcessor&, const GrPipeline&, GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -080096
cdalton9c3f1432016-03-11 10:07:37 -080097 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
98
Ethan Nicholas2983f402017-05-08 09:36:08 -040099 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800100
egdaniel9f1d4152016-02-10 09:50:38 -0800101 void finalizeShaders();
102
Brian Salomondc092132018-04-04 10:14:16 -0400103 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
104
egdanielfa896322016-01-13 12:19:30 -0800105private:
106 // reset is called by program creator between each processor's emit code. It increments the
107 // stage offset for variable name mangling, and also ensures verfication variables in the
108 // fragment shader are cleared.
109 void reset() {
110 this->addStage();
cdalton87332102016-02-26 12:22:02 -0800111 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800112 }
113 void addStage() { fStageIndex++; }
114
115 class AutoStageAdvance {
116 public:
117 AutoStageAdvance(GrGLSLProgramBuilder* pb)
118 : fPB(pb) {
119 fPB->reset();
120 // Each output to the fragment processor gets its own code section
121 fPB->fFS.nextStage();
122 }
123 ~AutoStageAdvance() {}
124 private:
125 GrGLSLProgramBuilder* fPB;
126 };
127
128 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400129 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800130
131 void emitAndInstallPrimProc(const GrPrimitiveProcessor&,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400132 SkString* outputColor,
133 SkString* outputCoverage);
134 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
135 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
136 int index,
137 int transformedCoordVarsIdx,
138 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400139 SkString output,
140 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400141 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Brian Salomon60dd8c72018-07-30 10:24:13 -0400142 SamplerHandle emitSampler(GrTextureType, GrPixelConfig, const char* name,
Brian Salomon18dfa982017-04-03 16:57:43 -0400143 GrShaderFlags visibility);
egdanielfa896322016-01-13 12:19:30 -0800144 void emitFSOutputSwizzle(bool hasSecondaryOutput);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400145 void updateSamplerCounts(GrShaderFlags visibility);
cdalton9c3f1432016-03-11 10:07:37 -0800146 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800147
cdalton87332102016-02-26 12:22:02 -0800148#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800149 void verify(const GrPrimitiveProcessor&);
150 void verify(const GrXferProcessor&);
151 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800152#endif
egdanielfa896322016-01-13 12:19:30 -0800153
Greg Danielbc5d4d72017-05-05 10:28:42 -0400154 // These are used to check that we don't excede the allowable number of resources in a shader.
bsalomona624bf32016-09-20 09:12:47 -0700155 int fNumVertexSamplers;
156 int fNumGeometrySamplers;
157 int fNumFragmentSamplers;
158 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800159};
160
161#endif