blob: dae2b6c902df0dad56bd91809765047c9381326e [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"
Chris Daltond7291ba2019-03-07 14:17:03 -070014#include "GrRenderTarget.h"
15#include "GrRenderTargetPriv.h"
robertphillips28a838e2016-06-23 14:07:00 -070016#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080017#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080018#include "glsl/GrGLSLPrimitiveProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080019#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080020#include "glsl/GrGLSLUniformHandler.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060021#include "glsl/GrGLSLVertexGeoBuilder.h"
egdanielfa896322016-01-13 12:19:30 -080022#include "glsl/GrGLSLXferProcessor.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080023
Brian Salomon99938a82016-11-21 13:41:08 -050024class GrShaderVar;
egdaniel0eafe792015-11-20 14:01:22 -080025class GrGLSLVaryingHandler;
Ethan Nicholas2983f402017-05-08 09:36:08 -040026class SkString;
Brian Salomon94efbf52016-11-29 13:43:05 -050027class GrShaderCaps;
egdaniel8dcdedc2015-11-11 06:27:20 -080028
egdaniel7ea439b2015-12-03 09:20:44 -080029class GrGLSLProgramBuilder {
egdaniel8dcdedc2015-11-11 06:27:20 -080030public:
Brian Salomonf9f45122016-11-29 11:59:17 -050031 using UniformHandle = GrGLSLUniformHandler::UniformHandle;
32 using SamplerHandle = GrGLSLUniformHandler::SamplerHandle;
egdaniel7ea439b2015-12-03 09:20:44 -080033
34 virtual ~GrGLSLProgramBuilder() {}
egdaniel8dcdedc2015-11-11 06:27:20 -080035
egdanielfa896322016-01-13 12:19:30 -080036 virtual const GrCaps* caps() const = 0;
Brian Salomon94efbf52016-11-29 13:43:05 -050037 const GrShaderCaps* shaderCaps() const { return this->caps()->shaderCaps(); }
egdaniela2e3e0f2015-11-19 07:23:45 -080038
egdaniel0e1853c2016-03-17 11:35:45 -070039 const GrPrimitiveProcessor& primitiveProcessor() const { return fPrimProc; }
Greg Daniel9a51a862018-11-30 10:18:14 -050040 const GrTextureProxy* const* primProcProxies() const { return fPrimProcProxies; }
Chris Daltond7291ba2019-03-07 14:17:03 -070041 const GrRenderTarget* renderTarget() const { return fRenderTarget; }
42 GrPixelConfig config() const { return fRenderTarget->config(); }
43 int effectiveSampleCnt() const {
44 SkASSERT(GrProcessor::CustomFeatures::kSampleLocations & header().processorFeatures());
Chris Dalton8c4cafd2019-04-15 19:14:36 -060045 return fRenderTarget->renderTargetPriv().getSampleLocations().count();
Chris Daltond7291ba2019-03-07 14:17:03 -070046 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -050047 GrSurfaceOrigin origin() const { return fOrigin; }
egdaniel0e1853c2016-03-17 11:35:45 -070048 const GrPipeline& pipeline() const { return fPipeline; }
Ethan Nicholas38657112017-02-09 17:01:22 -050049 GrProgramDesc* desc() { return fDesc; }
50 const GrProgramDesc::KeyHeader& header() const { return fDesc->header(); }
egdaniel7ea439b2015-12-03 09:20:44 -080051
cdalton5e58cee2016-02-11 12:49:47 -080052 void appendUniformDecls(GrShaderFlags visibility, SkString*) const;
egdaniel7ea439b2015-12-03 09:20:44 -080053
Brian Salomon99938a82016-11-21 13:41:08 -050054 const GrShaderVar& samplerVariable(SamplerHandle handle) const {
Brian Salomon101b8442016-11-18 11:58:54 -050055 return this->uniformHandler()->samplerVariable(handle);
56 }
57
58 GrSwizzle samplerSwizzle(SamplerHandle handle) const {
59 return this->uniformHandler()->samplerSwizzle(handle);
60 }
egdaniel09aa1fc2016-04-20 07:09:46 -070061
Ethan Nicholascd700e92018-08-24 16:43:57 -040062 // Used to add a uniform for the RenderTarget width (used for sk_Width) without mangling
Greg Daniele6ab9982018-08-22 13:56:32 +000063 // the name of the uniform inside of a stage.
Ethan Nicholascd700e92018-08-24 16:43:57 -040064 void addRTWidthUniform(const char* name);
65
66 // Used to add a uniform for the RenderTarget height (used for sk_Height and frag position)
67 // without mangling the name of the uniform inside of a stage.
Greg Daniele6ab9982018-08-22 13:56:32 +000068 void addRTHeightUniform(const char* name);
egdaniel8dcdedc2015-11-11 06:27:20 -080069
70 // Generates a name for a variable. The generated string will be name prefixed by the prefix
71 // char (unless the prefix is '\0'). It also will mangle the name to be stage-specific unless
72 // explicitly asked not to.
73 void nameVariable(SkString* out, char prefix, const char* name, bool mangle = true);
74
egdaniel7ea439b2015-12-03 09:20:44 -080075 virtual GrGLSLUniformHandler* uniformHandler() = 0;
76 virtual const GrGLSLUniformHandler* uniformHandler() const = 0;
egdaniel0eafe792015-11-20 14:01:22 -080077 virtual GrGLSLVaryingHandler* varyingHandler() = 0;
78
egdanielb80ec8b2016-02-09 09:54:43 -080079 // Used for backend customization of the output color and secondary color variables from the
80 // fragment processor. Only used if the outputs are explicitly declared in the shaders
Brian Salomon99938a82016-11-21 13:41:08 -050081 virtual void finalizeFragmentOutputColor(GrShaderVar& outputColor) {}
82 virtual void finalizeFragmentSecondaryColor(GrShaderVar& outputColor) {}
egdanielb80ec8b2016-02-09 09:54:43 -080083
egdaniel8dcdedc2015-11-11 06:27:20 -080084 // number of each input/output type in a single allocation block, used by many builders
85 static const int kVarsPerBlock;
86
Greg Daniel9a51a862018-11-30 10:18:14 -050087 GrGLSLVertexBuilder fVS;
88 GrGLSLGeometryBuilder fGS;
89 GrGLSLFragmentShaderBuilder fFS;
egdaniel0eafe792015-11-20 14:01:22 -080090
egdaniel8dcdedc2015-11-11 06:27:20 -080091 int fStageIndex;
92
Chris Daltond7291ba2019-03-07 14:17:03 -070093 const GrRenderTarget* fRenderTarget;
Robert Phillipsd0fe8752019-01-31 14:13:59 -050094 const GrSurfaceOrigin fOrigin;
Greg Daniel9a51a862018-11-30 10:18:14 -050095 const GrPipeline& fPipeline;
96 const GrPrimitiveProcessor& fPrimProc;
97 const GrTextureProxy* const* fPrimProcProxies;
egdaniel8dcdedc2015-11-11 06:27:20 -080098
Greg Daniel9a51a862018-11-30 10:18:14 -050099 GrProgramDesc* fDesc;
100
101 GrGLSLBuiltinUniformHandles fUniformHandles;
egdaniel8dcdedc2015-11-11 06:27:20 -0800102
Robert Phillips369e8b72017-08-01 16:13:04 -0400103 std::unique_ptr<GrGLSLPrimitiveProcessor> fGeometryProcessor;
104 std::unique_ptr<GrGLSLXferProcessor> fXferProcessor;
Brian Salomon4d3f5172018-06-07 14:42:52 -0400105 std::unique_ptr<std::unique_ptr<GrGLSLFragmentProcessor>[]> fFragmentProcessors;
106 int fFragmentProcessorCnt;
egdanielfa896322016-01-13 12:19:30 -0800107
egdaniel7ea439b2015-12-03 09:20:44 -0800108protected:
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500109 explicit GrGLSLProgramBuilder(GrRenderTarget* renderTarget, GrSurfaceOrigin origin,
110 const GrPrimitiveProcessor&,
Greg Daniel9a51a862018-11-30 10:18:14 -0500111 const GrTextureProxy* const primProcProxies[],
112 const GrPipeline&,
113 GrProgramDesc*);
egdanielfa896322016-01-13 12:19:30 -0800114
cdalton9c3f1432016-03-11 10:07:37 -0800115 void addFeature(GrShaderFlags shaders, uint32_t featureBit, const char* extensionName);
116
Ethan Nicholas2983f402017-05-08 09:36:08 -0400117 bool emitAndInstallProcs();
egdanielfa896322016-01-13 12:19:30 -0800118
egdaniel9f1d4152016-02-10 09:50:38 -0800119 void finalizeShaders();
120
Brian Salomondc092132018-04-04 10:14:16 -0400121 bool fragColorIsInOut() const { return fFS.primaryColorOutputIsInOut(); }
122
egdanielfa896322016-01-13 12:19:30 -0800123private:
124 // reset is called by program creator between each processor's emit code. It increments the
125 // stage offset for variable name mangling, and also ensures verfication variables in the
126 // fragment shader are cleared.
127 void reset() {
128 this->addStage();
Chris Daltond7291ba2019-03-07 14:17:03 -0700129 SkDEBUGCODE(fFS.debugOnly_resetPerStageVerification();)
egdanielfa896322016-01-13 12:19:30 -0800130 }
131 void addStage() { fStageIndex++; }
132
133 class AutoStageAdvance {
134 public:
135 AutoStageAdvance(GrGLSLProgramBuilder* pb)
136 : fPB(pb) {
137 fPB->reset();
138 // Each output to the fragment processor gets its own code section
139 fPB->fFS.nextStage();
140 }
141 ~AutoStageAdvance() {}
142 private:
143 GrGLSLProgramBuilder* fPB;
144 };
145
146 // Generates a possibly mangled name for a stage variable and writes it to the fragment shader.
Ethan Nicholas2983f402017-05-08 09:36:08 -0400147 void nameExpression(SkString*, const char* baseName);
egdanielfa896322016-01-13 12:19:30 -0800148
Greg Daniel9a51a862018-11-30 10:18:14 -0500149 void emitAndInstallPrimProc(SkString* outputColor, SkString* outputCoverage);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400150 void emitAndInstallFragProcs(SkString* colorInOut, SkString* coverageInOut);
151 SkString emitAndInstallFragProc(const GrFragmentProcessor&,
152 int index,
153 int transformedCoordVarsIdx,
154 const SkString& input,
Brian Salomon4d3f5172018-06-07 14:42:52 -0400155 SkString output,
156 SkTArray<std::unique_ptr<GrGLSLFragmentProcessor>>*);
Ethan Nicholas2983f402017-05-08 09:36:08 -0400157 void emitAndInstallXferProc(const SkString& colorIn, const SkString& coverageIn);
Greg Daniel9a51a862018-11-30 10:18:14 -0500158 SamplerHandle emitSampler(const GrTexture*, const GrSamplerState&, const char* name);
cdalton9c3f1432016-03-11 10:07:37 -0800159 bool checkSamplerCounts();
egdanielfa896322016-01-13 12:19:30 -0800160
cdalton87332102016-02-26 12:22:02 -0800161#ifdef SK_DEBUG
egdanielfa896322016-01-13 12:19:30 -0800162 void verify(const GrPrimitiveProcessor&);
egdanielfa896322016-01-13 12:19:30 -0800163 void verify(const GrFragmentProcessor&);
Chris Daltond7291ba2019-03-07 14:17:03 -0700164 void verify(const GrXferProcessor&);
cdalton87332102016-02-26 12:22:02 -0800165#endif
egdanielfa896322016-01-13 12:19:30 -0800166
Greg Danielbc5d4d72017-05-05 10:28:42 -0400167 // These are used to check that we don't excede the allowable number of resources in a shader.
bsalomona624bf32016-09-20 09:12:47 -0700168 int fNumFragmentSamplers;
169 SkSTArray<4, GrShaderVar> fTransformedCoordVars;
egdaniel8dcdedc2015-11-11 06:27:20 -0800170};
171
172#endif