blob: 7a4646d03f8847296460546db492bc837c3a943f [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
2 * Copyright 2014 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 GrGLProgramBuilder_DEFINED
9#define GrGLProgramBuilder_DEFINED
10
joshualitt30ba4362014-08-21 20:18:45 -070011#include "GrGLFragmentShaderBuilder.h"
12#include "GrGLGeometryShaderBuilder.h"
13#include "GrGLVertexShaderBuilder.h"
joshualitt47bb3822014-10-07 16:43:25 -070014#include "../GrGLProgramDataManager.h"
15#include "../GrGLUniformHandle.h"
joshualitta5305a12014-10-10 17:47:00 -070016#include "../GrGLGeometryProcessor.h"
bsalomon04ddf892014-11-19 12:36:22 -080017#include "../../GrOptDrawState.h"
bsalomonae59b772014-11-19 08:23:49 -080018#include "../../GrPendingFragmentStage.h"
joshualitt89c7a2e2014-10-10 14:11:59 -070019
joshualitt47bb3822014-10-07 16:43:25 -070020/*
21 * This is the base class for a series of interfaces. This base class *MUST* remain abstract with
22 * NO data members because it is used in multiple interface inheritance.
23 * Heirarchy:
24 * GrGLUniformBuilder
25 * / \
26 * GrGLFPBuilder GrGLGPBuilder
27 * \ /
28 * GrGLProgramBuilder(internal use only)
29 */
30class GrGLUniformBuilder {
joshualitt30ba4362014-08-21 20:18:45 -070031public:
32 enum ShaderVisibility {
33 kVertex_Visibility = 0x1,
34 kGeometry_Visibility = 0x2,
35 kFragment_Visibility = 0x4,
36 };
37
joshualitt47bb3822014-10-07 16:43:25 -070038 virtual ~GrGLUniformBuilder() {}
39
joshualitt30ba4362014-08-21 20:18:45 -070040 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
joshualitt47bb3822014-10-07 16:43:25 -070041
42 /** Add a uniform variable to the current program, that has visibility in one or more shaders.
43 visibility is a bitfield of ShaderVisibility values indicating from which shaders the
44 uniform should be accessible. At least one bit must be set. Geometry shader uniforms are not
45 supported at this time. The actual uniform name will be mangled. If outName is not NULL then
46 it will refer to the final uniform name after return. Use the addUniformArray variant to add
47 an array of uniforms. */
48 virtual UniformHandle addUniform(uint32_t visibility,
49 GrSLType type,
50 const char* name,
51 const char** outName = NULL) = 0;
52 virtual UniformHandle addUniformArray(uint32_t visibility,
53 GrSLType type,
54 const char* name,
55 int arrayCount,
56 const char** outName = NULL) = 0;
57
58 virtual const GrGLShaderVar& getUniformVariable(UniformHandle u) const = 0;
59
60 /**
61 * Shortcut for getUniformVariable(u).c_str()
62 */
63 virtual const char* getUniformCStr(UniformHandle u) const = 0;
64
65 virtual const GrGLContextInfo& ctxInfo() const = 0;
66
67 virtual GrGpuGL* gpu() const = 0;
68
69 /*
70 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
71 */
72};
73
joshualitt74077b92014-10-24 11:26:03 -070074// TODO move this into GrGLGPBuilder and move them both out of this file
75class GrGLVarying {
76public:
77 bool vsVarying() const { return kVertToFrag_Varying == fVarying ||
78 kVertToGeo_Varying == fVarying; }
79 bool fsVarying() const { return kVertToFrag_Varying == fVarying ||
80 kGeoToFrag_Varying == fVarying; }
81 const char* vsOut() const { return fVsOut; }
82 const char* gsIn() const { return fGsIn; }
83 const char* gsOut() const { return fGsOut; }
84 const char* fsIn() const { return fFsIn; }
85
86protected:
87 enum Varying {
88 kVertToFrag_Varying,
89 kVertToGeo_Varying,
90 kGeoToFrag_Varying,
91 };
92
93 GrGLVarying(GrSLType type, Varying varying)
94 : fVarying(varying), fType(type), fVsOut(NULL), fGsIn(NULL), fGsOut(NULL),
95 fFsIn(NULL) {}
96
97 Varying fVarying;
98
99private:
100 GrSLType fType;
101 const char* fVsOut;
102 const char* fGsIn;
103 const char* fGsOut;
104 const char* fFsIn;
105
106 friend class GrGLVertexBuilder;
107 friend class GrGLGeometryBuilder;
108 friend class GrGLFragmentShaderBuilder;
109};
110
111struct GrGLVertToFrag : public GrGLVarying {
112 GrGLVertToFrag(GrSLType type)
113 : GrGLVarying(type, kVertToFrag_Varying) {}
114};
115
116struct GrGLVertToGeo : public GrGLVarying {
117 GrGLVertToGeo(GrSLType type)
118 : GrGLVarying(type, kVertToGeo_Varying) {}
119};
120
121struct GrGLGeoToFrag : public GrGLVarying {
122 GrGLGeoToFrag(GrSLType type)
123 : GrGLVarying(type, kGeoToFrag_Varying) {}
124};
125
joshualitt47bb3822014-10-07 16:43:25 -0700126/* a specialization of the above for GPs. Lets the user add uniforms, varyings, and VS / FS code */
127class GrGLGPBuilder : public virtual GrGLUniformBuilder {
128public:
joshualitt74077b92014-10-24 11:26:03 -0700129 virtual void addVarying(const char* name,
130 GrGLVarying*,
joshualitt47bb3822014-10-07 16:43:25 -0700131 GrGLShaderVar::Precision fsPrecision=GrGLShaderVar::kDefault_Precision) = 0;
132
133 // TODO rename getFragmentBuilder
134 virtual GrGLGPFragmentBuilder* getFragmentShaderBuilder() = 0;
135 virtual GrGLVertexBuilder* getVertexShaderBuilder() = 0;
136
137 /*
138 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
139 */
140};
141
142/* a specializations for FPs. Lets the user add uniforms and FS code */
143class GrGLFPBuilder : public virtual GrGLUniformBuilder {
144public:
145 virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() = 0;
146
147 /*
148 * *NOTE* NO MEMBERS ALLOWED, MULTIPLE INHERITANCE
149 */
150};
151
joshualitta5305a12014-10-10 17:47:00 -0700152struct GrGLInstalledProc;
153struct GrGLInstalledGeoProc;
154struct GrGLInstalledFragProc;
155struct GrGLInstalledFragProcs;
156
joshualitt47bb3822014-10-07 16:43:25 -0700157/*
158 * Please note - no diamond problems because of virtual inheritance. Also, both base classes
159 * are pure virtual with no data members. This is the base class for program building.
160 * Subclasses are nearly identical but each has their own way of emitting transforms. State for
161 * each of the elements of the shader pipeline, ie vertex, fragment, geometry, etc, lives in those
162 * respective builders
163*/
164class GrGLProgramBuilder : public GrGLGPBuilder,
165 public GrGLFPBuilder {
166public:
167 /** Generates a shader program.
168 *
169 * The program implements what is specified in the stages given as input.
170 * After successful generation, the builder result objects are available
171 * to be used.
172 * @return true if generation was successful.
173 */
joshualitt79f8fae2014-10-28 17:59:26 -0700174 static GrGLProgram* CreateProgram(const GrOptDrawState&, GrGpu::DrawType, GrGpuGL*);
joshualitt47bb3822014-10-07 16:43:25 -0700175
176 virtual UniformHandle addUniform(uint32_t visibility,
177 GrSLType type,
178 const char* name,
179 const char** outName = NULL) SK_OVERRIDE {
180 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName);
181 }
182 virtual UniformHandle addUniformArray(uint32_t visibility,
183 GrSLType type,
184 const char* name,
185 int arrayCount,
186 const char** outName = NULL) SK_OVERRIDE;
187
188 virtual const GrGLShaderVar& getUniformVariable(UniformHandle u) const SK_OVERRIDE {
189 return fUniforms[u.toShaderBuilderIndex()].fVariable;
190 }
191
192 virtual const char* getUniformCStr(UniformHandle u) const SK_OVERRIDE {
193 return this->getUniformVariable(u).c_str();
194 }
195
196 virtual const GrGLContextInfo& ctxInfo() const SK_OVERRIDE;
197
198 virtual GrGpuGL* gpu() const SK_OVERRIDE { return fGpu; }
199
joshualitt15988992014-10-09 15:04:05 -0700200 virtual GrGLFPFragmentBuilder* getFragmentShaderBuilder() SK_OVERRIDE { return &fFS; }
joshualitt47bb3822014-10-07 16:43:25 -0700201 virtual GrGLVertexBuilder* getVertexShaderBuilder() SK_OVERRIDE { return &fVS; }
202
joshualitta5305a12014-10-10 17:47:00 -0700203 virtual void addVarying(
joshualitta5305a12014-10-10 17:47:00 -0700204 const char* name,
joshualitt74077b92014-10-24 11:26:03 -0700205 GrGLVarying*,
joshualitta5305a12014-10-10 17:47:00 -0700206 GrGLShaderVar::Precision fsPrecision=GrGLShaderVar::kDefault_Precision) SK_OVERRIDE;
joshualitt30ba4362014-08-21 20:18:45 -0700207
208 // Handles for program uniforms (other than per-effect uniforms)
209 struct BuiltinUniformHandles {
210 UniformHandle fViewMatrixUni;
211 UniformHandle fRTAdjustmentUni;
212 UniformHandle fColorUni;
213 UniformHandle fCoverageUni;
214
215 // We use the render target height to provide a y-down frag coord when specifying
216 // origin_upper_left is not supported.
217 UniformHandle fRTHeightUni;
218
219 // Uniforms for computing texture coords to do the dst-copy lookup
220 UniformHandle fDstCopyTopLeftUni;
221 UniformHandle fDstCopyScaleUni;
222 UniformHandle fDstCopySamplerUni;
223 };
224
joshualittdb0d3ca2014-10-07 12:42:26 -0700225protected:
joshualitta5305a12014-10-10 17:47:00 -0700226 typedef GrGLProgramDataManager::UniformInfo UniformInfo;
227 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
228
joshualitt79f8fae2014-10-28 17:59:26 -0700229 static GrGLProgramBuilder* CreateProgramBuilder(const GrOptDrawState&,
joshualitt47bb3822014-10-07 16:43:25 -0700230 GrGpu::DrawType,
231 bool hasGeometryProcessor,
232 GrGpuGL*);
233
joshualitt79f8fae2014-10-28 17:59:26 -0700234 GrGLProgramBuilder(GrGpuGL*, const GrOptDrawState&);
joshualitt30ba4362014-08-21 20:18:45 -0700235
egdaniel307796b2014-10-06 12:13:54 -0700236 const GrOptDrawState& optState() const { return fOptState; }
joshualitt79f8fae2014-10-28 17:59:26 -0700237 const GrProgramDesc& desc() const { return fDesc; }
238 const GrProgramDesc::KeyHeader& header() const { return fDesc.header(); }
joshualitt23e280d2014-09-18 12:26:38 -0700239
joshualitt30ba4362014-08-21 20:18:45 -0700240 // Generates a name for a variable. The generated string will be name prefixed by the prefix
241 // char (unless the prefix is '\0'). It also mangles the name to be stage-specific if we're
242 // generating stage code.
243 void nameVariable(SkString* out, char prefix, const char* name);
egdaniel37b4d862014-11-03 10:07:07 -0800244 void setupUniformColorAndCoverageIfNeeded(GrGLSLExpr4* inputColor, GrGLSLExpr1* inputCoverage);
joshualitt4973d9d2014-11-08 09:24:25 -0800245 void emitAndInstallProcs(GrGLSLExpr4* inputColor,
joshualitta5305a12014-10-10 17:47:00 -0700246 GrGLSLExpr4* inputCoverage);
247 void emitAndInstallFragProcs(int procOffset, int numProcs, GrGLSLExpr4* inOut);
248 template <class Proc>
249 void emitAndInstallProc(const Proc&,
250 int index,
joshualitta5305a12014-10-10 17:47:00 -0700251 const GrGLSLExpr4& input,
252 GrGLSLExpr4* output);
253
254 // these emit functions help to keep the createAndEmitProcessors template general
bsalomonae59b772014-11-19 08:23:49 -0800255 void emitAndInstallProc(const GrPendingFragmentStage&,
joshualitta5305a12014-10-10 17:47:00 -0700256 const char* outColor,
257 const char* inColor);
258 void emitAndInstallProc(const GrGeometryProcessor&,
joshualitt4973d9d2014-11-08 09:24:25 -0800259 const char* outCoverage,
260 const char* inCoverage);
joshualitt47bb3822014-10-07 16:43:25 -0700261 void verify(const GrGeometryProcessor&);
262 void verify(const GrFragmentProcessor&);
263 void emitSamplers(const GrProcessor&,
264 GrGLProcessor::TextureSamplerArray* outSamplers,
joshualitta5305a12014-10-10 17:47:00 -0700265 GrGLInstalledProc*);
joshualitt30ba4362014-08-21 20:18:45 -0700266
joshualitt47bb3822014-10-07 16:43:25 -0700267 // each specific program builder has a distinct transform and must override this function
bsalomonae59b772014-11-19 08:23:49 -0800268 virtual void emitTransforms(const GrPendingFragmentStage&,
joshualitt47bb3822014-10-07 16:43:25 -0700269 GrGLProcessor::TransformedCoordsArray* outCoords,
joshualitta5305a12014-10-10 17:47:00 -0700270 GrGLInstalledFragProc*);
joshualitt47bb3822014-10-07 16:43:25 -0700271 GrGLProgram* finalize();
272 void bindUniformLocations(GrGLuint programID);
273 bool checkLinkStatus(GrGLuint programID);
274 void resolveUniformLocations(GrGLuint programID);
joshualitt47bb3822014-10-07 16:43:25 -0700275 void cleanupProgram(GrGLuint programID, const SkTDArray<GrGLuint>& shaderIDs);
276 void cleanupShaders(const SkTDArray<GrGLuint>& shaderIDs);
joshualitt30ba4362014-08-21 20:18:45 -0700277
joshualitt47bb3822014-10-07 16:43:25 -0700278 // Subclasses create different programs
279 virtual GrGLProgram* createProgram(GrGLuint programID);
280
joshualitt30ba4362014-08-21 20:18:45 -0700281 void appendUniformDecls(ShaderVisibility, SkString*) const;
282
joshualitt47bb3822014-10-07 16:43:25 -0700283 // reset is called by program creator between each processor's emit code. It increments the
284 // stage offset for variable name mangling, and also ensures verfication variables in the
285 // fragment shader are cleared.
286 void reset() {
287 this->enterStage();
288 this->addStage();
289 fFS.reset();
290 }
291 void addStage() { fStageIndex++; }
292
293 // This simple class exits the stage and then restores the stage when it goes out of scope
294 class AutoStageRestore {
joshualitt30ba4362014-08-21 20:18:45 -0700295 public:
joshualitt47bb3822014-10-07 16:43:25 -0700296 AutoStageRestore(GrGLProgramBuilder* pb)
297 : fPB(pb), fOutOfStage(pb->fOutOfStage) { pb->exitStage(); }
298 ~AutoStageRestore() { fPB->fOutOfStage = fOutOfStage; }
joshualittb0a8a372014-09-23 09:50:21 -0700299 private:
joshualitt47bb3822014-10-07 16:43:25 -0700300 GrGLProgramBuilder* fPB;
301 bool fOutOfStage;
joshualittb0a8a372014-09-23 09:50:21 -0700302 };
joshualitt47bb3822014-10-07 16:43:25 -0700303 class AutoStageAdvance {
joshualittdb0d3ca2014-10-07 12:42:26 -0700304 public:
joshualitt47bb3822014-10-07 16:43:25 -0700305 AutoStageAdvance(GrGLProgramBuilder* pb) : fPB(pb) { fPB->reset(); }
306 ~AutoStageAdvance() { fPB->exitStage(); }
joshualittdb0d3ca2014-10-07 12:42:26 -0700307 private:
joshualitt47bb3822014-10-07 16:43:25 -0700308 GrGLProgramBuilder* fPB;
309 };
310 void exitStage() { fOutOfStage = true; }
311 void enterStage() { fOutOfStage = false; }
312 int stageIndex() const { return fStageIndex; }
313
joshualitt4973d9d2014-11-08 09:24:25 -0800314 struct TransformVarying {
315 TransformVarying(const GrGLVarying& v, const char* uniName, const char* sourceCoords)
316 : fV(v), fUniName(uniName), fSourceCoords(sourceCoords) {}
317 GrGLVarying fV;
318 SkString fUniName;
319 SkString fSourceCoords;
320 };
321
322 void addCoordVarying(const char* name, GrGLVarying* v, const char* uniName,
323 const char* sourceCoords) {
324 this->addVarying(name, v);
325 fCoordVaryings.push_back(TransformVarying(*v, uniName, sourceCoords));
326 }
327
328 const char* rtAdjustment() const { return "rtAdjustment"; }
329
joshualitt47bb3822014-10-07 16:43:25 -0700330 // number of each input/output type in a single allocation block, used by many builders
331 static const int kVarsPerBlock;
332
333 BuiltinUniformHandles fUniformHandles;
334 GrGLVertexBuilder fVS;
335 GrGLGeometryBuilder fGS;
336 GrGLFragmentShaderBuilder fFS;
337 bool fOutOfStage;
338 int fStageIndex;
339
joshualitta5305a12014-10-10 17:47:00 -0700340 GrGLInstalledGeoProc* fGeometryProcessor;
341 SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors;
joshualitt47bb3822014-10-07 16:43:25 -0700342
343 const GrOptDrawState& fOptState;
joshualitt79f8fae2014-10-28 17:59:26 -0700344 const GrProgramDesc& fDesc;
joshualitt47bb3822014-10-07 16:43:25 -0700345 GrGpuGL* fGpu;
346 UniformInfoArray fUniforms;
joshualitt4973d9d2014-11-08 09:24:25 -0800347 SkSTArray<16, TransformVarying, true> fCoordVaryings;
joshualitt47bb3822014-10-07 16:43:25 -0700348
349 friend class GrGLShaderBuilder;
350 friend class GrGLVertexBuilder;
351 friend class GrGLFragmentShaderBuilder;
352 friend class GrGLGeometryBuilder;
353};
354
355/**
joshualitta5305a12014-10-10 17:47:00 -0700356 * The below structs represent processors installed in programs. All processors can have texture
357 * samplers, but only frag processors have coord transforms, hence the need for different structs
joshualitt47bb3822014-10-07 16:43:25 -0700358 */
joshualitta5305a12014-10-10 17:47:00 -0700359struct GrGLInstalledProc {
360 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
joshualitt47bb3822014-10-07 16:43:25 -0700361
joshualitta5305a12014-10-10 17:47:00 -0700362 struct Sampler {
363 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {})
364 UniformHandle fUniform;
365 int fTextureUnit;
366 };
367 SkSTArray<4, Sampler, true> fSamplers;
368};
joshualitt47bb3822014-10-07 16:43:25 -0700369
joshualitta5305a12014-10-10 17:47:00 -0700370struct GrGLInstalledGeoProc : public GrGLInstalledProc {
371 SkAutoTDelete<GrGLGeometryProcessor> fGLProc;
372};
joshualitt47bb3822014-10-07 16:43:25 -0700373
joshualitta5305a12014-10-10 17:47:00 -0700374struct GrGLInstalledFragProc : public GrGLInstalledProc {
375 GrGLInstalledFragProc(bool useLocalCoords) : fGLProc(NULL), fLocalCoordAttrib(useLocalCoords) {}
joshualitt47bb3822014-10-07 16:43:25 -0700376 class ShaderVarHandle {
377 public:
378 bool isValid() const { return fHandle > -1; }
379 ShaderVarHandle() : fHandle(-1) {}
380 ShaderVarHandle(int value) : fHandle(value) { SkASSERT(this->isValid()); }
381 int handle() const { SkASSERT(this->isValid()); return fHandle; }
382 UniformHandle convertToUniformHandle() {
383 SkASSERT(this->isValid());
384 return GrGLProgramDataManager::UniformHandle::CreateFromUniformIndex(fHandle);
385 }
joshualittdb0d3ca2014-10-07 12:42:26 -0700386
joshualitt47bb3822014-10-07 16:43:25 -0700387 private:
388 int fHandle;
389 };
joshualittdb0d3ca2014-10-07 12:42:26 -0700390
joshualitt47bb3822014-10-07 16:43:25 -0700391 struct Transform {
392 Transform() : fType(kVoid_GrSLType) { fCurrentValue = SkMatrix::InvalidMatrix(); }
393 ShaderVarHandle fHandle;
394 SkMatrix fCurrentValue;
395 GrSLType fType;
396 };
joshualittdb0d3ca2014-10-07 12:42:26 -0700397
joshualitta5305a12014-10-10 17:47:00 -0700398 SkAutoTDelete<GrGLFragmentProcessor> fGLProc;
399 SkSTArray<2, Transform, true> fTransforms;
400 bool fLocalCoordAttrib;
401};
joshualittdb0d3ca2014-10-07 12:42:26 -0700402
joshualitta5305a12014-10-10 17:47:00 -0700403struct GrGLInstalledFragProcs : public SkRefCnt {
404 virtual ~GrGLInstalledFragProcs();
405 SkSTArray<8, GrGLInstalledFragProc*, true> fProcs;
joshualitt30ba4362014-08-21 20:18:45 -0700406};
407
joshualitt30ba4362014-08-21 20:18:45 -0700408#endif