blob: 189e876ae5a626761a5b65f3c5026f26d1f509b4 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
junov@google.comf93e7172011-03-31 21:26:24 +00009#ifndef GrGLProgram_DEFINED
10#define GrGLProgram_DEFINED
11
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrDrawState.h"
bsalomon@google.com96399942012-02-13 14:39:16 +000013#include "GrGLContextInfo.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000014#include "GrGLSL.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000015#include "GrStringBuilder.h"
16#include "GrGpu.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkXfermode.h"
19
junov@google.comf93e7172011-03-31 21:26:24 +000020class GrBinHashKeyBuilder;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000021class GrGLProgramStage;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000022class GrGLShaderBuilder;
junov@google.comf93e7172011-03-31 21:26:24 +000023
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000024// optionally compile the experimental GS code. Set to GR_DEBUG
25// so that debug build bots will execute the code.
26#define GR_GL_EXPERIMENTAL_GS GR_DEBUG
27
junov@google.comf93e7172011-03-31 21:26:24 +000028/**
29 * This class manages a GPU program and records per-program information.
30 * We can specify the attribute locations so that they are constant
31 * across our shaders. But the driver determines the uniform locations
32 * at link time. We don't need to remember the sampler uniform location
33 * because we will bind a texture slot to it and never change it
34 * Uniforms are program-local so we can't rely on fHWState to hold the
35 * previous uniform state after a program change.
36 */
37class GrGLProgram {
38public:
bsalomon@google.com4fa66942011-09-20 19:06:12 +000039
junov@google.comf93e7172011-03-31 21:26:24 +000040 class CachedData;
41
42 GrGLProgram();
43 ~GrGLProgram();
44
45 /**
junov@google.comf93e7172011-03-31 21:26:24 +000046 * This is the heavy initilization routine for building a GLProgram.
47 * The result of heavy init is not stored in datamembers of GrGLProgam,
48 * but in a separate cacheable container.
49 */
bsalomon@google.com96399942012-02-13 14:39:16 +000050 bool genProgram(const GrGLContextInfo& gl,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000051 GrCustomStage** customStages,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000052 CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000053
bsalomon@google.com271cffc2011-05-20 14:13:56 +000054 /**
55 * The shader may modify the blend coeffecients. Params are in/out
56 */
57 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
58
59 /**
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000060 * Attribute indices. These should not overlap. Matrices consume 3 slots.
bsalomon@google.com271cffc2011-05-20 14:13:56 +000061 */
bsalomon@google.com91961302011-05-09 18:39:58 +000062 static int PositionAttributeIdx() { return 0; }
63 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
tomhudson@google.com93813632011-10-27 20:21:16 +000064 static int ColorAttributeIdx() { return 1 + GrDrawState::kMaxTexCoords; }
bsalomon@google.coma3108262011-10-10 14:08:47 +000065 static int CoverageAttributeIdx() {
tomhudson@google.com93813632011-10-27 20:21:16 +000066 return 2 + GrDrawState::kMaxTexCoords;
bsalomon@google.coma3108262011-10-10 14:08:47 +000067 }
tomhudson@google.com93813632011-10-27 20:21:16 +000068 static int EdgeAttributeIdx() { return 3 + GrDrawState::kMaxTexCoords; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000069
tomhudson@google.com0d831722011-06-02 15:37:14 +000070 static int ViewMatrixAttributeIdx() {
tomhudson@google.com93813632011-10-27 20:21:16 +000071 return 4 + GrDrawState::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000072 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000073 static int TextureMatrixAttributeIdx(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +000074 return 7 + GrDrawState::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000075 }
76
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +000077public:
junov@google.comf93e7172011-03-31 21:26:24 +000078
tomhudson@google.com0d831722011-06-02 15:37:14 +000079 // Parameters that affect code generation
80 // These structs should be kept compact; they are the input to an
81 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000082 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000083 ProgramDesc() {
84 // since we use this as part of a key we can't have any unitialized
85 // padding
86 memset(this, 0, sizeof(ProgramDesc));
87 }
88
bsalomon@google.coma91e9232012-02-23 15:39:54 +000089 enum OutputConfig {
bsalomon@google.comc4364992011-11-07 15:54:49 +000090 // PM-color OR color with no alpha channel
bsalomon@google.coma91e9232012-02-23 15:39:54 +000091 kPremultiplied_OutputConfig,
92 // nonPM-color with alpha channel. Round components up after
93 // dividing by alpha. Assumes output is 8 bits for r, g, and b
94 kUnpremultiplied_RoundUp_OutputConfig,
95 // nonPM-color with alpha channel. Round components down after
96 // dividing by alpha. Assumes output is 8 bits for r, g, and b
97 kUnpremultiplied_RoundDown_OutputConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +000098
bsalomon@google.coma91e9232012-02-23 15:39:54 +000099 kOutputConfigCnt
bsalomon@google.comc4364992011-11-07 15:54:49 +0000100 };
101
tomhudson@google.com0d831722011-06-02 15:37:14 +0000102 struct StageDesc {
103 enum OptFlagBits {
104 kNoPerspective_OptFlagBit = 1 << 0,
105 kIdentityMatrix_OptFlagBit = 1 << 1,
106 kCustomTextureDomain_OptFlagBit = 1 << 2,
107 kIsEnabled_OptFlagBit = 1 << 7
108 };
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000109 // Convolution is obsolete; left in for testing only
tomhudson@google.com0d831722011-06-02 15:37:14 +0000110 enum FetchMode {
111 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000112 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000113 kConvolution_FetchMode,
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000114 kErode_FetchMode,
115 kDilate_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000116
117 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000118 };
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000119 /**
bsalomon@google.com74b98712011-11-11 19:46:16 +0000120 Flags set based on a src texture's pixel config. The operations
121 described are performed after reading a texel.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000122 */
bsalomon@google.com74b98712011-11-11 19:46:16 +0000123 enum InConfigFlags {
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000124 kNone_InConfigFlag = 0x00,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000125
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000126 /**
bsalomon@google.com74b98712011-11-11 19:46:16 +0000127 Swap the R and B channels. This is incompatible with
128 kSmearAlpha. It is prefereable to perform the swizzle outside
129 the shader using GL_ARB_texture_swizzle if possible rather
130 than setting this flag.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000131 */
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000132 kSwapRAndB_InConfigFlag = 0x01,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000133
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000134 /**
bsalomon@google.com74b98712011-11-11 19:46:16 +0000135 Smear alpha across all four channels. This is incompatible with
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000136 kSwapRAndB, kMulRGBByAlpha* and kSmearRed. It is prefereable
137 to perform the smear outside the shader using
138 GL_ARB_texture_swizzle if possible rather than setting this
139 flag.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000140 */
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000141 kSmearAlpha_InConfigFlag = 0x02,
142
143 /**
144 Smear the red channel across all four channels. This flag is
145 incompatible with kSwapRAndB, kMulRGBByAlpha*and kSmearAlpha.
146 It is preferable to use GL_ARB_texture_swizzle instead of this
147 flag.
148 */
149 kSmearRed_InConfigFlag = 0x04,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000150
151 /**
152 Multiply r,g,b by a after texture reads. This flag incompatible
153 with kSmearAlpha and may only be used with FetchMode kSingle.
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000154
155 It is assumed the src texture has 8bit color components. After
156 reading the texture one version rounds up to the next multiple
157 of 1/255.0 and the other rounds down. At most one of these
158 flags may be set.
bsalomon@google.com74b98712011-11-11 19:46:16 +0000159 */
robertphillips@google.com443e5a52012-04-30 20:01:21 +0000160 kMulRGBByAlpha_RoundUp_InConfigFlag = 0x08,
161 kMulRGBByAlpha_RoundDown_InConfigFlag = 0x10,
bsalomon@google.com74b98712011-11-11 19:46:16 +0000162
163 kDummyInConfigFlag,
164 kInConfigBitMask = (kDummyInConfigFlag-1) |
165 (kDummyInConfigFlag-2)
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000166 };
tomhudson@google.com0d831722011-06-02 15:37:14 +0000167 enum CoordMapping {
168 kIdentity_CoordMapping,
169 kRadialGradient_CoordMapping,
170 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000171 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000172 // need different shader computation when quadratic
173 // eq describing the gradient degenerates to a linear eq.
174 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000175 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000176 };
junov@google.comf93e7172011-03-31 21:26:24 +0000177
tomhudson@google.com0d831722011-06-02 15:37:14 +0000178 uint8_t fOptFlags;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000179 uint8_t fInConfigFlags; // bitfield of InConfigFlags values
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000180 uint8_t fFetchMode; // casts to enum FetchMode
tomhudson@google.com0d831722011-06-02 15:37:14 +0000181 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000182 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000183
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000184 /** Non-zero if user-supplied code will write the stage's
185 contribution to the fragment shader. */
186 uint16_t fCustomStageKey;
187
bsalomon@google.com74b98712011-11-11 19:46:16 +0000188 GR_STATIC_ASSERT((InConfigFlags)(uint8_t)kInConfigBitMask ==
189 kInConfigBitMask);
190
tomhudson@google.com0d831722011-06-02 15:37:14 +0000191 inline bool isEnabled() const {
bsalomon@google.comc2c9b972011-10-03 13:17:22 +0000192 return SkToBool(fOptFlags & kIsEnabled_OptFlagBit);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000193 }
194 inline void setEnabled(bool newValue) {
195 if (newValue) {
196 fOptFlags |= kIsEnabled_OptFlagBit;
197 } else {
198 fOptFlags &= ~kIsEnabled_OptFlagBit;
199 }
200 }
201 };
202
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000203 // Specifies where the intitial color comes from before the stages are
204 // applied.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000205 enum ColorInput {
206 kSolidWhite_ColorInput,
207 kTransBlack_ColorInput,
208 kAttribute_ColorInput,
209 kUniform_ColorInput,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000210
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000211 kColorInputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000212 };
213 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000214 // used as a per-pixel blend coeffecient. This controls whether a
215 // secondary source is output and what value it holds.
216 enum DualSrcOutput {
217 kNone_DualSrcOutput,
218 kCoverage_DualSrcOutput,
219 kCoverageISA_DualSrcOutput,
220 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000221
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000222 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000223 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000224
tomhudson@google.com93813632011-10-27 20:21:16 +0000225 GrDrawState::VertexEdgeType fVertexEdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000226
tomhudson@google.com0d831722011-06-02 15:37:14 +0000227 // stripped of bits that don't affect prog generation
228 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000229
tomhudson@google.com93813632011-10-27 20:21:16 +0000230 StageDesc fStages[GrDrawState::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000231
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000232 // To enable experimental geometry shader code (not for use in
233 // production)
234#if GR_GL_EXPERIMENTAL_GS
235 bool fExperimentalGS;
236#endif
237
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000238 uint8_t fColorInput; // casts to enum ColorInput
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000239 uint8_t fCoverageInput; // casts to enum CoverageInput
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000240 uint8_t fOutputConfig; // casts to enum OutputConfig
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000241 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
tomhudson@google.com0d831722011-06-02 15:37:14 +0000242 int8_t fFirstCoverageStage;
243 SkBool8 fEmitsPointSize;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000244 SkBool8 fColorMatrixEnabled;
junov@google.comf93e7172011-03-31 21:26:24 +0000245
tomhudson@google.com0d831722011-06-02 15:37:14 +0000246 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000247 int8_t fPadding[1];
junov@google.comf93e7172011-03-31 21:26:24 +0000248
junov@google.comf93e7172011-03-31 21:26:24 +0000249 } fProgramDesc;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000250 GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000251
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000252 // for code readability
253 typedef ProgramDesc::StageDesc StageDesc;
254
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000255private:
256
257 const ProgramDesc& getDesc() { return fProgramDesc; }
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000258 const char* adjustInColor(const GrStringBuilder& inColor) const;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000259
junov@google.comf93e7172011-03-31 21:26:24 +0000260public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000261 enum {
262 kUnusedUniform = -1,
bsalomon@google.com91961302011-05-09 18:39:58 +0000263 };
264
junov@google.comf93e7172011-03-31 21:26:24 +0000265 struct StageUniLocations {
266 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000267 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000268 GrGLint fSamplerUni;
269 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000270 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000271 GrGLint fKernelUni;
272 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000273 void reset() {
274 fTextureMatrixUni = kUnusedUniform;
275 fNormalizedTexelSizeUni = kUnusedUniform;
276 fSamplerUni = kUnusedUniform;
277 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000278 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000279 fKernelUni = kUnusedUniform;
280 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000281 }
junov@google.comf93e7172011-03-31 21:26:24 +0000282 };
283
284 struct UniLocations {
285 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000286 GrGLint fColorUni;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000287 GrGLint fCoverageUni;
Scroggo97c88c22011-05-11 14:05:25 +0000288 GrGLint fColorFilterUni;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000289 GrGLint fColorMatrixUni;
290 GrGLint fColorMatrixVecUni;
tomhudson@google.com93813632011-10-27 20:21:16 +0000291 StageUniLocations fStages[GrDrawState::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000292 void reset() {
293 fViewMatrixUni = kUnusedUniform;
294 fColorUni = kUnusedUniform;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000295 fCoverageUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000296 fColorFilterUni = kUnusedUniform;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000297 fColorMatrixUni = kUnusedUniform;
298 fColorMatrixVecUni = kUnusedUniform;
tomhudson@google.com93813632011-10-27 20:21:16 +0000299 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000300 fStages[s].reset();
301 }
302 }
junov@google.comf93e7172011-03-31 21:26:24 +0000303 };
304
305 class CachedData : public ::GrNoncopyable {
306 public:
307 CachedData() {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000308 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
309 fCustomStage[i] = NULL;
310 }
junov@google.comf93e7172011-03-31 21:26:24 +0000311 }
312
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000313 ~CachedData();
junov@google.comf93e7172011-03-31 21:26:24 +0000314
315 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000316 memcpy(this, &other, sizeof(*this));
tomhudson@google.comd8f856c2012-05-10 12:13:36 +0000317 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
318 other.fCustomStage[i] = NULL;
319 }
junov@google.comf93e7172011-03-31 21:26:24 +0000320 }
321
junov@google.comf93e7172011-03-31 21:26:24 +0000322 public:
323
324 // IDs
325 GrGLuint fVShaderID;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000326 GrGLuint fGShaderID;
junov@google.comf93e7172011-03-31 21:26:24 +0000327 GrGLuint fFShaderID;
328 GrGLuint fProgramID;
329 // shader uniform locations (-1 if shader doesn't use them)
330 UniLocations fUniLocations;
331
332 GrMatrix fViewMatrix;
333
334 // these reflect the current values of uniforms
335 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000336 GrColor fColor;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000337 GrColor fCoverage;
Scroggo97c88c22011-05-11 14:05:25 +0000338 GrColor fColorFilterColor;
tomhudson@google.com93813632011-10-27 20:21:16 +0000339 GrMatrix fTextureMatrices[GrDrawState::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000340 // width and height used for normalized texel size
tomhudson@google.com93813632011-10-27 20:21:16 +0000341 int fTextureWidth[GrDrawState::kNumStages];
342 int fTextureHeight[GrDrawState::kNumStages];
343 GrScalar fRadial2CenterX1[GrDrawState::kNumStages];
344 GrScalar fRadial2Radius0[GrDrawState::kNumStages];
345 bool fRadial2PosRoot[GrDrawState::kNumStages];
346 GrRect fTextureDomain[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000347
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000348 GrGLProgramStage* fCustomStage[GrDrawState::kNumStages];
349
junov@google.comf93e7172011-03-31 21:26:24 +0000350 private:
351 enum Constants {
352 kUniLocationPreAllocSize = 8
353 };
354
junov@google.comf93e7172011-03-31 21:26:24 +0000355 }; // CachedData
356
junov@google.comf7c00f62011-08-18 18:15:16 +0000357 enum Constants {
358 kProgramKeySize = sizeof(ProgramDesc)
359 };
360
361 // Provide an opaque ProgramDesc
362 const uint32_t* keyData() const{
363 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
364 }
365
junov@google.comf93e7172011-03-31 21:26:24 +0000366private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000367
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000368 // Determines which uniforms will need to be bound.
bsalomon@google.com96399942012-02-13 14:39:16 +0000369 void genStageCode(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000370 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000371 const ProgramDesc::StageDesc& desc,
372 const char* fsInColor, // NULL means no incoming color
373 const char* fsOutColor,
374 const char* vsInCoord,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000375 GrGLShaderBuilder* segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000376 StageUniLocations* locations,
377 GrGLProgramStage* override) const;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000378
bsalomon@google.com96399942012-02-13 14:39:16 +0000379 void genGeometryShader(const GrGLContextInfo& gl,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000380 GrGLShaderBuilder* segments) const;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000381
bsalomon@google.com66105672011-09-15 15:12:00 +0000382 // generates code to compute coverage based on edge AA.
bsalomon@google.com96399942012-02-13 14:39:16 +0000383 void genEdgeCoverage(const GrGLContextInfo& gl,
bsalomon@google.com66105672011-09-15 15:12:00 +0000384 GrVertexLayout layout,
385 CachedData* programData,
386 GrStringBuilder* coverageVar,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000387 GrGLShaderBuilder* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000388
bsalomon@google.com96399942012-02-13 14:39:16 +0000389 static bool CompileShaders(const GrGLContextInfo& gl,
tomhudson@google.comf9ad8862012-05-11 20:38:48 +0000390 const GrGLShaderBuilder& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000391 CachedData* programData);
392
junov@google.comf93e7172011-03-31 21:26:24 +0000393 // Compiles a GL shader, returns shader ID or 0 if failed
394 // params have same meaning as glShaderSource
bsalomon@google.com96399942012-02-13 14:39:16 +0000395 static GrGLuint CompileShader(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000396 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000397 const char** strings,
398 int* stringLengths);
399
bsalomon@google.com91961302011-05-09 18:39:58 +0000400 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
401 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000402 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com96399942012-02-13 14:39:16 +0000403 const GrGLContextInfo& gl,
tomhudson@google.com93813632011-10-27 20:21:16 +0000404 GrStringBuilder texCoordAttrNames[GrDrawState::kMaxTexCoords],
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000405 bool bindColorOut,
406 bool bindDualSrcOut,
407 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000408
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000409 // Binds uniforms; initializes cache to invalid values.
bsalomon@google.com96399942012-02-13 14:39:16 +0000410 void getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000411 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000412
bsalomon@google.com5739d2c2012-05-31 15:07:19 +0000413 friend class GrGpuGL;
junov@google.comf93e7172011-03-31 21:26:24 +0000414};
415
416#endif