blob: 7d0ce74abe0f7567ebf34d0c9dcb7889cd934706 [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;
Greg Danielbc5d4d72017-05-05 10:28:42 -040031 using TexelBufferHandle = GrGLSLUniformHandler::TexelBufferHandle;
egdaniel7ea439b2015-12-03 09:20:44 -080032
33 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080034
egdanielfa896322016-01-13 12:19:30 -080035 virtual const GrCaps* caps() const = 0;
Brian Salomon94efbf52016-11-29 13:43:05 -050036 const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
egdaniela2e3e0f2015-11-19 07:23:45 -080037
egdaniel0e1853c2016-03-17 11:35:45 -070038 const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
39 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050040 GrProgramDesc* desc() { return fDesc; }
41 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080042
cdalton5e58cee2016-02-11 12:49:47 -080043 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080044
Brian Salomon99938a82016-11-21 13:41:08 -050045 const GrShaderVar& samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050046 return this->uniformHandler()->samplerVariable(handle);
47 }
48
49 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
50 return this->uniformHandler()->samplerSwizzle(handle);
51 }
egdaniel09aa1fc2016-04-20 07:09:46 -070052
Greg Danielbc5d4d72017-05-05 10:28:42 -040053 const GrShaderVar& texelBufferVariable(TexelBufferHandle handle) const {
54 return this->uniformHandler()->texelBufferVariable(handle);
55 }
56
egdaniel7ea439b2015-12-03 09:20:44 -080057 // Used to add a uniform for the RenderTarget height (used for frag position) without mangling
58 // the name of the uniform inside of a stage.
Ethan Nicholas941e7e22016-12-12 15:33:30 -050059 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080060
61 // Generates a name for a variable. The generated string will be name prefixed by the prefix
62 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
63 // explicitly asked not to.
64 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
65
egdaniel7ea439b2015-12-03 09:20:44 -080066 virtual GrGLSLUniformHandler* uniformHandler() = 0;
67 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080068 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
69
egdanielb80ec8b2016-02-09 09:54:43 -080070 // Used for backend customization of the output color and secondary color variables from the
71 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050072 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
73 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080074
egdaniel8dcdedc2015-11-11 06:27:20 -080075 // number of each input/output type in a single allocation block, used by many builders
76 static const int kVarsPerBlock;
77
egdaniel0eafe792015-11-20 14:01:22 -080078 GrGLSLVertexBuilder fVS;
79 GrGLSLGeometryBuilder fGS;
egdaniel2d721d32015-11-11 13:06:05 -080080 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080081
egdaniel8dcdedc2015-11-11 06:27:20 -080082 int fStageIndex;
83
egdaniel0e1853c2016-03-17 11:35:45 -070084 const GrPipeline& fPipeline;
85 const GrPrimitiveProcessor& fPrimProc;
Ethan Nicholas38657112017-02-09 17:01:22 -050086 GrProgramDesc* fDesc;
egdaniel8dcdedc2015-11-11 06:27:20 -080087
Brian Salomon1471df92018-06-08 10:49:00 -040088 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -080089
Robert Phillips369e8b72017-08-01 16:13:04 -040090 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
91 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -040092 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
93 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -080094
egdaniel7ea439b2015-12-03 09:20:44 -080095protected:
egdaniel0e1853c2016-03-17 11:35:45 -070096 explicit GrGLSLProgramBuilder(const GrPipeline&,
97 const GrPrimitiveProcessor&,
Ethan Nicholas38657112017-02-09 17:01:22 -050098 GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -080099
cdalton9c3f1432016-03-11 10:07:37 -0800100 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
101
Ethan Nicholas2983f402017-05-08 09:36:08 -0400102 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800103
egdaniel9f1d4152016-02-10 09:50:38 -0800104 void finalizeShaders();
105
Brian Salomondc092132018-04-04 10:14:16 -0400106 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
107
egdanielfa896322016-01-13 12:19:30 -0800108private:
109 // reset is called by program creator between each processor's emit code. It increments the
110 // stage offset for variable name mangling, and also ensures verfication variables in the
111 // fragment shader are cleared.
112 void reset() {
113 this->addStage();
cdalton87332102016-02-26 12:22:02 -0800114 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800115 }
116 void addStage() { fStageIndex++; }
117
118 class AutoStageAdvance {
119 public:
120 AutoStageAdvance(GrGLSLProgramBuilder* pb)
121 : fPB(pb) {
122 fPB->reset();
123 // Each output to the fragment processor gets its own code section
124 fPB->fFS.nextStage();
125 }
126 ~AutoStageAdvance() {}
127 private:
128 GrGLSLProgramBuilder* fPB;
129 };
130
131 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400132 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800133
134 void emitAndInstallPrimProc(const GrPrimitiveProcessor&,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400135 SkString* outputColor,
136 SkString* outputCoverage);
137 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
138 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
139 int index,
140 int transformedCoordVarsIdx,
141 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400142 SkString output,
143 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400144 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Brian Salomon559f5562017-11-15 14:28:33 -0500145 void emitSamplers(const GrResourceIOProcessor& processor,
146 SkTArray<SamplerHandle>* outTexSamplerHandles,
147 SkTArray<TexelBufferHandle>* outTexelBufferHandles);
Brian Salomon18dfa982017-04-03 16:57:43 -0400148 SamplerHandle emitSampler(GrSLType samplerType, GrPixelConfig, const char* name,
149 GrShaderFlags visibility);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400150 TexelBufferHandle emitTexelBuffer(GrPixelConfig, const char* name, GrShaderFlags visibility);
egdanielfa896322016-01-13 12:19:30 -0800151 void emitFSOutputSwizzle(bool hasSecondaryOutput);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400152 void updateSamplerCounts(GrShaderFlags visibility);
cdalton9c3f1432016-03-11 10:07:37 -0800153 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800154
cdalton87332102016-02-26 12:22:02 -0800155#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800156 void verify(const GrPrimitiveProcessor&);
157 void verify(const GrXferProcessor&);
158 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800159#endif
egdanielfa896322016-01-13 12:19:30 -0800160
Greg Danielbc5d4d72017-05-05 10:28:42 -0400161 // These are used to check that we don't excede the allowable number of resources in a shader.
162 // The sampler counts include both normal texure samplers as well as texel buffers.
bsalomona624bf32016-09-20 09:12:47 -0700163 int fNumVertexSamplers;
164 int fNumGeometrySamplers;
165 int fNumFragmentSamplers;
166 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800167};
168
169#endif