blob: 5884a90f85500036858c7f4e1cefe1ff9282a971 [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
bsalomon@google.comad5e9372012-07-11 18:11:27 +000015class GrGLContextInfo;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000016/**
bsalomon@google.comeb715c82012-07-11 15:03:31 +000017 Contains all the incremental state of a shader as it is being built,as well as helpers to
18 manipulate that state.
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000019 TODO: migrate CompileShaders() here?
20*/
21
22class GrGLShaderBuilder {
23
24public:
bsalomon@google.comad5e9372012-07-11 18:11:27 +000025 typedef GrTAllocator<GrGLShaderVar> VarArray;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000026
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
bsalomon@google.comad5e9372012-07-11 18:11:27 +000033 GrGLShaderBuilder(const GrGLContextInfo&);
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000034
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
bsalomon@google.comeb715c82012-07-11 15:03:31 +000059 /** Add a uniform variable to the current program, that has visibilty in one or more shaders.
60 If stageNum is specified, it is appended to the name to guarantee uniqueness; if count is
61 specified, the uniform is an array. visibility is a bitfield of ShaderType values indicating
62 from which shaders the uniform should be accessible. At least one bit must be set. Geometry
63 shader uniforms are not supported at this time.
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000064 */
bsalomon@google.comeb715c82012-07-11 15:03:31 +000065 const GrGLShaderVar& addUniform(uint32_t visibility,
66 GrSLType type,
67 const char* name,
68 int stageNum = -1,
69 int count = GrGLShaderVar::kNonArray);
tomhudson@google.com242ed6f2012-05-30 17:38:57 +000070
bsalomon@google.comeb715c82012-07-11 15:03:31 +000071 /** Add a varying variable to the current program to pass values between vertex and fragment
72 shaders. If the last two parameters are non-NULL, they are filled in with the name
73 generated. */
tomhudson@google.com23cb2292012-05-30 18:26:03 +000074 void addVarying(GrSLType type,
75 const char* name,
76 const char** vsOutName = NULL,
77 const char** fsInName = NULL);
78
bsalomon@google.comeb715c82012-07-11 15:03:31 +000079 /** Add a varying variable to the current program to pass values between vertex and fragment
80 shaders; stageNum is appended to the name to guarantee uniqueness. If the last two
81 parameters are non-NULL, they are filled in with the name generated. */
tomhudson@google.com23cb2292012-05-30 18:26:03 +000082 void addVarying(GrSLType type,
83 const char* name,
84 int stageNum,
85 const char** vsOutName = NULL,
86 const char** fsInName = NULL);
bsalomon@google.comeb715c82012-07-11 15:03:31 +000087
bsalomon@google.comad5e9372012-07-11 18:11:27 +000088 /** Called after building is complete to get the final shader string. */
89 void getShader(ShaderType, SkString*) const;
90
bsalomon@google.comeb715c82012-07-11 15:03:31 +000091 // TODO: Everything below here private.
92
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000093 SkString fHeader; // VS+FS, GLSL version, etc
94 VarArray fVSUnis;
95 VarArray fVSAttrs;
96 VarArray fVSOutputs;
97 VarArray fGSInputs;
98 VarArray fGSOutputs;
99 VarArray fFSInputs;
100 SkString fGSHeader; // layout qualifiers specific to GS
101 VarArray fFSUnis;
102 VarArray fFSOutputs;
103 SkString fFSFunctions;
104 SkString fVSCode;
105 SkString fGSCode;
106 SkString fFSCode;
107 bool fUsesGS;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000108
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000109 /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode().
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000110 //@{
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000111
tomhudson@google.com52598142012-05-24 17:44:30 +0000112 int fVaryingDims;
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000113 static const int fCoordDims = 2;
114
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000115 /// True if fSampleCoords is an expression; false if it's a bare
116 /// variable name
tomhudson@google.com52598142012-05-24 17:44:30 +0000117 bool fComplexCoord;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000118 SkString fSampleCoords;
tomhudson@google.com52598142012-05-24 17:44:30 +0000119
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000120 SkString fSwizzle;
121 SkString fModulate;
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000122
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000123 SkString fTexFunc;
tomhudson@google.com5440f062012-06-01 15:55:50 +0000124
tomhudson@google.com040c41a2012-05-18 14:57:40 +0000125 //@}
tomhudson@google.com9c639a42012-05-14 19:58:06 +0000126
bsalomon@google.comad5e9372012-07-11 18:11:27 +0000127private:
128 const GrGLContextInfo& fContext;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000129};
130
131#endif