blob: 889219d876bdcf314fa8df0cb0073ef3a2b3ab7c [file] [log] [blame]
tomhudson@google.comf9ad8862012-05-11 20:38:48 +00001/*
2 * Copyright 2012 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 GrGLShaderBuilder_DEFINED
9#define GrGLShaderBuilder_DEFINED
10
11#include "GrAllocator.h"
12#include "gl/GrGLShaderVar.h"
13#include "gl/GrGLSL.h"
14
15typedef GrTAllocator<GrGLShaderVar> VarArray;
16
17/**
18 Containts all the incremental state of a shader as it is being built,
19 as well as helpers to manipulate that state.
20 TODO: migrate CompileShaders() here?
21*/
22
23class GrGLShaderBuilder {
24
25public:
26
27 GrGLShaderBuilder();
28
29 void appendVarying(GrSLType type,
30 const char* name,
31 const char** vsOutName = NULL,
32 const char** fsInName = NULL);
33
34 void appendVarying(GrSLType type,
35 const char* name,
36 int stageNum,
37 const char** vsOutName = NULL,
38 const char** fsInName = NULL);
39
tomhudson@google.com52598142012-05-24 17:44:30 +000040 void computeSwizzle(uint32_t configFlags);
41 void computeModulate(const char* fsInColor);
42
43 void emitTextureSetup();
44
45 /** texture2D(samplerName, coordName), with projection
46 if necessary; if coordName is not specified,
47 uses fSampleCoords. */
48 void emitTextureLookup(const char* samplerName,
49 const char* coordName = NULL);
50
51 /** sets outColor to results of texture lookup, with
52 swizzle, and/or modulate as necessary */
53 void emitDefaultFetch(const char* outColor,
54 const char* samplerName);
55
56 // TODO: needs a better name
57 enum SamplerMode {
58 kDefault_SamplerMode,
59 kProj_SamplerMode,
60 kExplicitDivide_SamplerMode // must do an explicit divide
61 };
62
63 // TODO: computing this requires information about fetch mode
64 // && coord mapping, as well as StageDesc::fOptFlags - proably
65 // need to set up default value and have some custom stages
66 // override as necessary?
67 void setSamplerMode(SamplerMode samplerMode) {
68 fSamplerMode = samplerMode;
69 }
70
71
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000072 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
73 VarArray fVSUnis;
74 VarArray fVSAttrs;
75 VarArray fVSOutputs;
76 VarArray fGSInputs;
77 VarArray fGSOutputs;
78 VarArray fFSInputs;
79 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
80 VarArray fFSUnis;
81 VarArray fFSOutputs;
82 GrStringBuilder fFSFunctions;
83 GrStringBuilder fVSCode;
84 GrStringBuilder fGSCode;
85 GrStringBuilder fFSCode;
tomhudson@google.com040c41a2012-05-18 14:57:40 +000086 bool fUsesGS;
87
tomhudson@google.com52598142012-05-24 17:44:30 +000088 /// Per-stage settings - only valid while we're inside
89 /// GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +000090 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000091
tomhudson@google.com52598142012-05-24 17:44:30 +000092 int fVaryingDims;
tomhudson@google.com9c639a42012-05-14 19:58:06 +000093 static const int fCoordDims = 2;
94
tomhudson@google.com52598142012-05-24 17:44:30 +000095protected:
96
97 SamplerMode fSamplerMode;
98
99public:
100
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000101 /// True if fSampleCoords is an expression; false if it's a bare
102 /// variable name
tomhudson@google.com52598142012-05-24 17:44:30 +0000103 bool fComplexCoord;
104 GrStringBuilder fSampleCoords;
105
106 GrStringBuilder fSwizzle;
107 GrStringBuilder fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000108
109 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000110
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000111};
112
113#endif