blob: bf308f3fbeb12b9dac0fa6ebd19be716d2ff85b8 [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/**
bsalomon@google.comeb715c82012-07-11 15:03:31 +000018 Contains all the incremental state of a shader as it is being built,as well as helpers to
19 manipulate that state.
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000020 TODO: migrate CompileShaders() here?
21*/
22
23class GrGLShaderBuilder {
24
25public:
26
bsalomon@google.comeb715c82012-07-11 15:03:31 +000027 enum ShaderType {
28 kVertex_ShaderType = 0x1,
29 kGeometry_ShaderType = 0x2,
30 kFragment_ShaderType = 0x4,
31 };
32
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000033 GrGLShaderBuilder();
34
tomhudson@google.com52598142012-05-24 17:44:30 +000035 void computeSwizzle(uint32_t configFlags);
36 void computeModulate(const char* fsInColor);
37
tomhudson@google.com5440f062012-06-01 15:55:50 +000038 // TODO: needs a better name
39 enum SamplerMode {
40 kDefault_SamplerMode,
41 kProj_SamplerMode,
42 kExplicitDivide_SamplerMode // must do an explicit divide
43 };
44
bsalomon@google.comeb715c82012-07-11 15:03:31 +000045 /** Determines whether we should use texture2D() or texture2Dproj(), and if an explicit divide
46 is required for the sample coordinates, creates the new variable and emits the code to
47 initialize it. */
tomhudson@google.com5440f062012-06-01 15:55:50 +000048 void setupTextureAccess(SamplerMode samplerMode, int stageNum);
tomhudson@google.com52598142012-05-24 17:44:30 +000049
bsalomon@google.comeb715c82012-07-11 15:03:31 +000050 /** texture2D(samplerName, coordName), with projection if necessary; if coordName is not
51 specified, uses fSampleCoords. */
tomhudson@google.com52598142012-05-24 17:44:30 +000052 void emitTextureLookup(const char* samplerName,
53 const char* coordName = NULL);
54
bsalomon@google.comeb715c82012-07-11 15:03:31 +000055 /** sets outColor to results of texture lookup, with swizzle, and/or modulate as necessary */
tomhudson@google.com52598142012-05-24 17:44:30 +000056 void emitDefaultFetch(const char* outColor,
57 const char* samplerName);
58
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000059
bsalomon@google.comeb715c82012-07-11 15:03:31 +000060 /** Add a uniform variable to the current program, that has visibilty in one or more shaders.
61 If stageNum is specified, it is appended to the name to guarantee uniqueness; if count is
62 specified, the uniform is an array. visibility is a bitfield of ShaderType values indicating
63 from which shaders the uniform should be accessible. At least one bit must be set. Geometry
64 shader uniforms are not supported at this time.
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000065 */
bsalomon@google.comeb715c82012-07-11 15:03:31 +000066 const GrGLShaderVar& addUniform(uint32_t visibility,
67 GrSLType type,
68 const char* name,
69 int stageNum = -1,
70 int count = GrGLShaderVar::kNonArray);
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000071
bsalomon@google.comeb715c82012-07-11 15:03:31 +000072 /** Add a varying variable to the current program to pass values between vertex and fragment
73 shaders. If the last two parameters are non-NULL, they are filled in with the name
74 generated. */
tomhudson@google.com23cb2292012-05-30 18:26:03 +000075 void addVarying(GrSLType type,
76 const char* name,
77 const char** vsOutName = NULL,
78 const char** fsInName = NULL);
79
bsalomon@google.comeb715c82012-07-11 15:03:31 +000080 /** Add a varying variable to the current program to pass values between vertex and fragment
81 shaders; stageNum is appended to the name to guarantee uniqueness. If the last two
82 parameters are non-NULL, they are filled in with the name generated. */
tomhudson@google.com23cb2292012-05-30 18:26:03 +000083 void addVarying(GrSLType type,
84 const char* name,
85 int stageNum,
86 const char** vsOutName = NULL,
87 const char** fsInName = NULL);
bsalomon@google.comeb715c82012-07-11 15:03:31 +000088
89 // TODO: Everything below here private.
90
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000091 SkString fHeader; // VS+FS, GLSL version, etc
92 VarArray fVSUnis;
93 VarArray fVSAttrs;
94 VarArray fVSOutputs;
95 VarArray fGSInputs;
96 VarArray fGSOutputs;
97 VarArray fFSInputs;
98 SkString fGSHeader; // layout qualifiers specific to GS
99 VarArray fFSUnis;
100 VarArray fFSOutputs;
101 SkString fFSFunctions;
102 SkString fVSCode;
103 SkString fGSCode;
104 SkString fFSCode;
105 bool fUsesGS;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000106
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000107 /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000108 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000109
tomhudson@google.com52598142012-05-24 17:44:30 +0000110 int fVaryingDims;
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000111 static const int fCoordDims = 2;
112
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000113 /// True if fSampleCoords is an expression; false if it's a bare
114 /// variable name
tomhudson@google.com52598142012-05-24 17:44:30 +0000115 bool fComplexCoord;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000116 SkString fSampleCoords;
tomhudson@google.com52598142012-05-24 17:44:30 +0000117
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000118 SkString fSwizzle;
119 SkString fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000120
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000121 SkString fTexFunc;
tomhudson@google.com5440f062012-06-01 15:55:50 +0000122
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000123 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000124
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000125};
126
127#endif