blob: 8d263b8e008028df13247dd87a75d119e862c3c5 [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
Greg Daniele6ab9982018-08-22 13:56:32 +000052 // Used to add a uniform for the RenderTarget height (used for frag position) without mangling
53 // the name of the uniform inside of a stage.
54 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080055
56 // Generates a name for a variable. The generated string will be name prefixed by the prefix
57 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
58 // explicitly asked not to.
59 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
60
egdaniel7ea439b2015-12-03 09:20:44 -080061 virtual GrGLSLUniformHandler* uniformHandler() = 0;
62 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080063 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
64
egdanielb80ec8b2016-02-09 09:54:43 -080065 // Used for backend customization of the output color and secondary color variables from the
66 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050067 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
68 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080069
egdaniel8dcdedc2015-11-11 06:27:20 -080070 // number of each input/output type in a single allocation block, used by many builders
71 static const int kVarsPerBlock;
72
egdaniel0eafe792015-11-20 14:01:22 -080073 GrGLSLVertexBuilder fVS;
74 GrGLSLGeometryBuilder fGS;
egdaniel2d721d32015-11-11 13:06:05 -080075 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080076
egdaniel8dcdedc2015-11-11 06:27:20 -080077 int fStageIndex;
78
egdaniel0e1853c2016-03-17 11:35:45 -070079 const GrPipeline& fPipeline;
80 const GrPrimitiveProcessor& fPrimProc;
Ethan Nicholas38657112017-02-09 17:01:22 -050081 GrProgramDesc* fDesc;
egdaniel8dcdedc2015-11-11 06:27:20 -080082
Brian Salomon1471df92018-06-08 10:49:00 -040083 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -080084
Robert Phillips369e8b72017-08-01 16:13:04 -040085 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
86 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -040087 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
88 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -080089
egdaniel7ea439b2015-12-03 09:20:44 -080090protected:
Brian Salomonff168d92018-06-23 15:17:27 -040091 explicit GrGLSLProgramBuilder(const GrPrimitiveProcessor&, const GrPipeline&, GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -080092
cdalton9c3f1432016-03-11 10:07:37 -080093 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
94
Ethan Nicholas2983f402017-05-08 09:36:08 -040095 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -080096
egdaniel9f1d4152016-02-10 09:50:38 -080097 void finalizeShaders();
98
Brian Salomondc092132018-04-04 10:14:16 -040099 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
100
egdanielfa896322016-01-13 12:19:30 -0800101private:
102 // reset is called by program creator between each processor's emit code. It increments the
103 // stage offset for variable name mangling, and also ensures verfication variables in the
104 // fragment shader are cleared.
105 void reset() {
106 this->addStage();
cdalton87332102016-02-26 12:22:02 -0800107 SkDEBUGCODE(fFS.resetVerification();)
egdanielfa896322016-01-13 12:19:30 -0800108 }
109 void addStage() { fStageIndex++; }
110
111 class AutoStageAdvance {
112 public:
113 AutoStageAdvance(GrGLSLProgramBuilder* pb)
114 : fPB(pb) {
115 fPB->reset();
116 // Each output to the fragment processor gets its own code section
117 fPB->fFS.nextStage();
118 }
119 ~AutoStageAdvance() {}
120 private:
121 GrGLSLProgramBuilder* fPB;
122 };
123
124 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400125 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800126
127 void emitAndInstallPrimProc(const GrPrimitiveProcessor&,
Ethan Nicholas2983f402017-05-08 09:36:08 -0400128 SkString* outputColor,
129 SkString* outputCoverage);
130 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
131 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
132 int index,
133 int transformedCoordVarsIdx,
134 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400135 SkString output,
136 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400137 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Brian Salomon60dd8c72018-07-30 10:24:13 -0400138 SamplerHandle emitSampler(GrTextureType, GrPixelConfig, const char* name,
Brian Salomon18dfa982017-04-03 16:57:43 -0400139 GrShaderFlags visibility);
egdanielfa896322016-01-13 12:19:30 -0800140 void emitFSOutputSwizzle(bool hasSecondaryOutput);
Greg Danielbc5d4d72017-05-05 10:28:42 -0400141 void updateSamplerCounts(GrShaderFlags visibility);
cdalton9c3f1432016-03-11 10:07:37 -0800142 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800143
cdalton87332102016-02-26 12:22:02 -0800144#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800145 void verify(const GrPrimitiveProcessor&);
146 void verify(const GrXferProcessor&);
147 void verify(const GrFragmentProcessor&);
cdalton87332102016-02-26 12:22:02 -0800148#endif
egdanielfa896322016-01-13 12:19:30 -0800149
Greg Danielbc5d4d72017-05-05 10:28:42 -0400150 // These are used to check that we don't excede the allowable number of resources in a shader.
bsalomona624bf32016-09-20 09:12:47 -0700151 int fNumVertexSamplers;
152 int fNumGeometrySamplers;
153 int fNumFragmentSamplers;
154 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800155};
156
157#endif