blob: b89653fb81f766456671ac3b6ba30a5e4f04071d [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
junov@google.comf93e7172011-03-31 21:26:24 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
junov@google.comf93e7172011-03-31 21:26:24 +000010#ifndef GrGLProgram_DEFINED
11#define GrGLProgram_DEFINED
12
13#include "GrGLInterface.h"
bsalomon@google.com91961302011-05-09 18:39:58 +000014#include "GrStringBuilder.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000015#include "GrGpu.h"
junov@google.comf93e7172011-03-31 21:26:24 +000016
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkXfermode.h"
18
junov@google.comf93e7172011-03-31 21:26:24 +000019class GrBinHashKeyBuilder;
junov@google.comd31cbc42011-05-17 17:01:17 +000020
21struct ShaderCodeSegments {
bsalomon@google.com271cffc2011-05-20 14:13:56 +000022 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
junov@google.comd31cbc42011-05-17 17:01:17 +000023 GrStringBuilder fVSUnis;
24 GrStringBuilder fVSAttrs;
25 GrStringBuilder fVaryings;
26 GrStringBuilder fFSUnis;
bsalomon@google.com271cffc2011-05-20 14:13:56 +000027 GrStringBuilder fFSOutputs;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +000028 GrStringBuilder fFSFunctions;
junov@google.comd31cbc42011-05-17 17:01:17 +000029 GrStringBuilder fVSCode;
30 GrStringBuilder fFSCode;
31};
junov@google.comf93e7172011-03-31 21:26:24 +000032
33/**
34 * This class manages a GPU program and records per-program information.
35 * We can specify the attribute locations so that they are constant
36 * across our shaders. But the driver determines the uniform locations
37 * at link time. We don't need to remember the sampler uniform location
38 * because we will bind a texture slot to it and never change it
39 * Uniforms are program-local so we can't rely on fHWState to hold the
40 * previous uniform state after a program change.
41 */
42class GrGLProgram {
43public:
44 class CachedData;
45
46 GrGLProgram();
47 ~GrGLProgram();
48
49 /**
junov@google.comf93e7172011-03-31 21:26:24 +000050 * This is the heavy initilization routine for building a GLProgram.
51 * The result of heavy init is not stored in datamembers of GrGLProgam,
52 * but in a separate cacheable container.
53 */
bsalomon@google.com0b77d682011-08-19 13:28:54 +000054 bool genProgram(const GrGLInterface* gl,
55 CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000056
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057 /**
58 * The shader may modify the blend coeffecients. Params are in/out
59 */
60 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
61
62 /**
63 * Attribute indices
64 */
bsalomon@google.com91961302011-05-09 18:39:58 +000065 static int PositionAttributeIdx() { return 0; }
66 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
67 static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; }
tomhudson@google.com0d831722011-06-02 15:37:14 +000068 static int ViewMatrixAttributeIdx() {
69 return 2 + GrDrawTarget::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000070 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000071 static int TextureMatrixAttributeIdx(int stage) {
72 return 5 + GrDrawTarget::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000073 }
74
junov@google.comf93e7172011-03-31 21:26:24 +000075private:
76
tomhudson@google.com0d831722011-06-02 15:37:14 +000077 // Parameters that affect code generation
78 // These structs should be kept compact; they are the input to an
79 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000080 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000081 ProgramDesc() {
82 // since we use this as part of a key we can't have any unitialized
83 // padding
84 memset(this, 0, sizeof(ProgramDesc));
85 }
86
tomhudson@google.com0d831722011-06-02 15:37:14 +000087 struct StageDesc {
88 enum OptFlagBits {
89 kNoPerspective_OptFlagBit = 1 << 0,
90 kIdentityMatrix_OptFlagBit = 1 << 1,
91 kCustomTextureDomain_OptFlagBit = 1 << 2,
92 kIsEnabled_OptFlagBit = 1 << 7
93 };
94 enum Modulation {
95 kColor_Modulation,
bsalomon@google.com1e257a52011-07-06 19:52:16 +000096 kAlpha_Modulation,
97
98 kModulationCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +000099 };
100 enum FetchMode {
101 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000102 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000103 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000104
105 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000106 };
107 enum CoordMapping {
108 kIdentity_CoordMapping,
109 kRadialGradient_CoordMapping,
110 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000111 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000112 // need different shader computation when quadratic
113 // eq describing the gradient degenerates to a linear eq.
114 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000115 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000116 };
junov@google.comf93e7172011-03-31 21:26:24 +0000117
tomhudson@google.com0d831722011-06-02 15:37:14 +0000118 uint8_t fOptFlags;
119 uint8_t fModulation; // casts to enum Modulation
120 uint8_t fFetchMode; // casts to enum FetchMode
121 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000122 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000123
124 inline bool isEnabled() const {
125 return fOptFlags & kIsEnabled_OptFlagBit;
126 }
127 inline void setEnabled(bool newValue) {
128 if (newValue) {
129 fOptFlags |= kIsEnabled_OptFlagBit;
130 } else {
131 fOptFlags &= ~kIsEnabled_OptFlagBit;
132 }
133 }
134 };
135
136 enum ColorType {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000137 kNone_ColorType = 0,
138 kAttribute_ColorType = 1,
139 kUniform_ColorType = 2,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000140 };
141 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000142 // used as a per-pixel blend coeffecient. This controls whether a
143 // secondary source is output and what value it holds.
144 enum DualSrcOutput {
145 kNone_DualSrcOutput,
146 kCoverage_DualSrcOutput,
147 kCoverageISA_DualSrcOutput,
148 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000149
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000151 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000152
tomhudson@google.com0d831722011-06-02 15:37:14 +0000153 // stripped of bits that don't affect prog generation
154 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000155
tomhudson@google.com0d831722011-06-02 15:37:14 +0000156 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000157
tomhudson@google.com0d831722011-06-02 15:37:14 +0000158 uint8_t fColorType; // casts to enum ColorType
159 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
160 int8_t fFirstCoverageStage;
161 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000162 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000163
tomhudson@google.com0d831722011-06-02 15:37:14 +0000164 int8_t fEdgeAANumEdges;
165 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000166
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000167 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000168
junov@google.comf93e7172011-03-31 21:26:24 +0000169 } fProgramDesc;
170
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000171 const ProgramDesc& getDesc() { return fProgramDesc; }
172
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000173 // for code readability
174 typedef ProgramDesc::StageDesc StageDesc;
175
junov@google.comf93e7172011-03-31 21:26:24 +0000176public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000177 enum {
178 kUnusedUniform = -1,
179 kSetAsAttribute = 1000,
180 };
181
junov@google.comf93e7172011-03-31 21:26:24 +0000182 struct StageUniLocations {
183 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000184 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000185 GrGLint fSamplerUni;
186 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000187 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000188 GrGLint fKernelUni;
189 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000190 void reset() {
191 fTextureMatrixUni = kUnusedUniform;
192 fNormalizedTexelSizeUni = kUnusedUniform;
193 fSamplerUni = kUnusedUniform;
194 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000195 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000196 fKernelUni = kUnusedUniform;
197 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000198 }
junov@google.comf93e7172011-03-31 21:26:24 +0000199 };
200
201 struct UniLocations {
202 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000203 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000204 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000205 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000206 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000207 void reset() {
208 fViewMatrixUni = kUnusedUniform;
209 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000210 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000211 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000212 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
213 fStages[s].reset();
214 }
215 }
junov@google.comf93e7172011-03-31 21:26:24 +0000216 };
217
218 class CachedData : public ::GrNoncopyable {
219 public:
220 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000221 }
222
223 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000224 }
225
226 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000227 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000228 }
229
junov@google.comf93e7172011-03-31 21:26:24 +0000230 public:
231
232 // IDs
233 GrGLuint fVShaderID;
234 GrGLuint fFShaderID;
235 GrGLuint fProgramID;
236 // shader uniform locations (-1 if shader doesn't use them)
237 UniLocations fUniLocations;
238
239 GrMatrix fViewMatrix;
240
241 // these reflect the current values of uniforms
242 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000243 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000244 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000245 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000246 // width and height used for normalized texel size
247 int fTextureWidth[GrDrawTarget::kNumStages];
248 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000249 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
250 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
251 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000252 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000253
254 private:
255 enum Constants {
256 kUniLocationPreAllocSize = 8
257 };
258
junov@google.comf93e7172011-03-31 21:26:24 +0000259 }; // CachedData
260
junov@google.comf7c00f62011-08-18 18:15:16 +0000261 enum Constants {
262 kProgramKeySize = sizeof(ProgramDesc)
263 };
264
265 // Provide an opaque ProgramDesc
266 const uint32_t* keyData() const{
267 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
268 }
269
junov@google.comf93e7172011-03-31 21:26:24 +0000270private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000271 enum {
272 kUseUniform = 2000
273 };
274
275 // should set all fields in locations var to kUseUniform if the
276 // corresponding uniform is required for the program.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000277 void genStageCode(const GrGLInterface* gl,
278 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000279 const ProgramDesc::StageDesc& desc,
280 const char* fsInColor, // NULL means no incoming color
281 const char* fsOutColor,
282 const char* vsInCoord,
283 ShaderCodeSegments* segments,
284 StageUniLocations* locations) const;
285
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000286 static bool CompileFSAndVS(const GrGLInterface* gl,
287 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000288 CachedData* programData);
289
junov@google.comf93e7172011-03-31 21:26:24 +0000290 // Compiles a GL shader, returns shader ID or 0 if failed
291 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000292 static GrGLuint CompileShader(const GrGLInterface* gl,
293 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000294 const char** strings,
295 int* stringLengths);
296
bsalomon@google.com91961302011-05-09 18:39:58 +0000297 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
298 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000299 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000300 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000301 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
302 bool bindColorOut,
303 bool bindDualSrcOut,
304 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000305
306 // Gets locations for all uniforms set to kUseUniform and initializes cache
307 // to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000308 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
309 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000310
junov@google.comf93e7172011-03-31 21:26:24 +0000311 friend class GrGpuGLShaders;
312};
313
314#endif