blob: c397d741cbe4568aed6d83051dfe3c37199c47f7 [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"
bsalomon@google.com890e3b52012-06-01 19:01:37 +000015#include "GrGLTexture.h"
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000016//#include "GrGpu.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000018#include "SkString.h"
Scroggo97c88c22011-05-11 14:05:25 +000019#include "SkXfermode.h"
20
junov@google.comf93e7172011-03-31 21:26:24 +000021class GrBinHashKeyBuilder;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000022class GrGLProgramStage;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000023class GrGLShaderBuilder;
junov@google.comf93e7172011-03-31 21:26:24 +000024
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000025// optionally compile the experimental GS code. Set to GR_DEBUG
26// so that debug build bots will execute the code.
27#define GR_GL_EXPERIMENTAL_GS GR_DEBUG
28
junov@google.comf93e7172011-03-31 21:26:24 +000029/**
30 * This class manages a GPU program and records per-program information.
31 * We can specify the attribute locations so that they are constant
32 * across our shaders. But the driver determines the uniform locations
33 * at link time. We don't need to remember the sampler uniform location
34 * because we will bind a texture slot to it and never change it
35 * Uniforms are program-local so we can't rely on fHWState to hold the
36 * previous uniform state after a program change.
37 */
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000038class GrGLProgram : public GrRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000039public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000040 SK_DECLARE_INST_COUNT(GrGLProgram)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000041
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000042 struct Desc;
junov@google.comf93e7172011-03-31 21:26:24 +000043
44 GrGLProgram();
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000045 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000046
47 /**
junov@google.comf93e7172011-03-31 21:26:24 +000048 * This is the heavy initilization routine for building a GLProgram.
junov@google.comf93e7172011-03-31 21:26:24 +000049 */
bsalomon@google.com96399942012-02-13 14:39:16 +000050 bool genProgram(const GrGLContextInfo& gl,
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000051 const Desc& desc,
52 GrCustomStage** customStages);
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
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000059 const Desc& getDesc() { return fDesc; }
60
bsalomon@google.com271cffc2011-05-20 14:13:56 +000061 /**
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000062 * Attribute indices. These should not overlap. Matrices consume 3 slots.
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063 */
bsalomon@google.com91961302011-05-09 18:39:58 +000064 static int PositionAttributeIdx() { return 0; }
65 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
tomhudson@google.com93813632011-10-27 20:21:16 +000066 static int ColorAttributeIdx() { return 1 + GrDrawState::kMaxTexCoords; }
bsalomon@google.coma3108262011-10-10 14:08:47 +000067 static int CoverageAttributeIdx() {
tomhudson@google.com93813632011-10-27 20:21:16 +000068 return 2 + GrDrawState::kMaxTexCoords;
bsalomon@google.coma3108262011-10-10 14:08:47 +000069 }
tomhudson@google.com93813632011-10-27 20:21:16 +000070 static int EdgeAttributeIdx() { return 3 + GrDrawState::kMaxTexCoords; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000071
tomhudson@google.com0d831722011-06-02 15:37:14 +000072 static int ViewMatrixAttributeIdx() {
tomhudson@google.com93813632011-10-27 20:21:16 +000073 return 4 + GrDrawState::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000074 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000075 static int TextureMatrixAttributeIdx(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +000076 return 7 + GrDrawState::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000077 }
78
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000079 enum {
80 kUnusedUniform = -1,
81 };
junov@google.comf93e7172011-03-31 21:26:24 +000082
tomhudson@google.com0d831722011-06-02 15:37:14 +000083 // Parameters that affect code generation
84 // These structs should be kept compact; they are the input to an
85 // expensive hash key generator.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000086 struct Desc {
87 Desc() {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000088 // since we use this as part of a key we can't have any unitialized
89 // padding
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000090 memset(this, 0, sizeof(Desc));
91 }
92
93 // returns this as a uint32_t array to be used as a key in the program cache
94 const uint32_t* asKey() const {
95 return reinterpret_cast<const uint32_t*>(this);
bsalomon@google.com4be283f2011-04-19 21:15:09 +000096 }
97
bsalomon@google.coma91e9232012-02-23 15:39:54 +000098 enum OutputConfig {
bsalomon@google.comc4364992011-11-07 15:54:49 +000099 // PM-color OR color with no alpha channel
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000100 kPremultiplied_OutputConfig,
101 // nonPM-color with alpha channel. Round components up after
102 // dividing by alpha. Assumes output is 8 bits for r, g, and b
103 kUnpremultiplied_RoundUp_OutputConfig,
104 // nonPM-color with alpha channel. Round components down after
105 // dividing by alpha. Assumes output is 8 bits for r, g, and b
106 kUnpremultiplied_RoundDown_OutputConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000107
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000108 kOutputConfigCnt
bsalomon@google.comc4364992011-11-07 15:54:49 +0000109 };
110
tomhudson@google.com0d831722011-06-02 15:37:14 +0000111 struct StageDesc {
112 enum OptFlagBits {
113 kNoPerspective_OptFlagBit = 1 << 0,
114 kIdentityMatrix_OptFlagBit = 1 << 1,
115 kCustomTextureDomain_OptFlagBit = 1 << 2,
116 kIsEnabled_OptFlagBit = 1 << 7
117 };
bsalomon@google.comb505a122012-05-31 18:40:36 +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
bsalomon@google.comd2ae1fa2012-06-04 20:06:02 +0000153 with kSmearAlpha.
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 };
junov@google.comf93e7172011-03-31 21:26:24 +0000167
tomhudson@google.com0d831722011-06-02 15:37:14 +0000168 uint8_t fOptFlags;
bsalomon@google.com74b98712011-11-11 19:46:16 +0000169 uint8_t fInConfigFlags; // bitfield of InConfigFlags values
tomhudson@google.com0d831722011-06-02 15:37:14 +0000170
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000171 /** Non-zero if user-supplied code will write the stage's
172 contribution to the fragment shader. */
173 uint16_t fCustomStageKey;
174
bsalomon@google.com74b98712011-11-11 19:46:16 +0000175 GR_STATIC_ASSERT((InConfigFlags)(uint8_t)kInConfigBitMask ==
176 kInConfigBitMask);
177
tomhudson@google.com0d831722011-06-02 15:37:14 +0000178 inline bool isEnabled() const {
bsalomon@google.comc2c9b972011-10-03 13:17:22 +0000179 return SkToBool(fOptFlags & kIsEnabled_OptFlagBit);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000180 }
181 inline void setEnabled(bool newValue) {
182 if (newValue) {
183 fOptFlags |= kIsEnabled_OptFlagBit;
184 } else {
185 fOptFlags &= ~kIsEnabled_OptFlagBit;
186 }
187 }
188 };
189
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000190 // Specifies where the intitial color comes from before the stages are
191 // applied.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000192 enum ColorInput {
193 kSolidWhite_ColorInput,
194 kTransBlack_ColorInput,
195 kAttribute_ColorInput,
196 kUniform_ColorInput,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000197
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000198 kColorInputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000199 };
200 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000201 // used as a per-pixel blend coeffecient. This controls whether a
202 // secondary source is output and what value it holds.
203 enum DualSrcOutput {
204 kNone_DualSrcOutput,
205 kCoverage_DualSrcOutput,
206 kCoverageISA_DualSrcOutput,
207 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000208
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000209 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000210 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000211
tomhudson@google.com93813632011-10-27 20:21:16 +0000212 GrDrawState::VertexEdgeType fVertexEdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213
tomhudson@google.com0d831722011-06-02 15:37:14 +0000214 // stripped of bits that don't affect prog generation
215 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000216
tomhudson@google.com93813632011-10-27 20:21:16 +0000217 StageDesc fStages[GrDrawState::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000218
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000219 // To enable experimental geometry shader code (not for use in
220 // production)
221#if GR_GL_EXPERIMENTAL_GS
222 bool fExperimentalGS;
223#endif
224
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000225 uint8_t fColorInput; // casts to enum ColorInput
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000226 uint8_t fCoverageInput; // casts to enum CoverageInput
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000227 uint8_t fOutputConfig; // casts to enum OutputConfig
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000228 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
tomhudson@google.com0d831722011-06-02 15:37:14 +0000229 int8_t fFirstCoverageStage;
230 SkBool8 fEmitsPointSize;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000231 SkBool8 fColorMatrixEnabled;
junov@google.comf93e7172011-03-31 21:26:24 +0000232
tomhudson@google.com0d831722011-06-02 15:37:14 +0000233 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
bsalomon@google.com7ffe6812012-05-11 17:32:43 +0000234 int8_t fPadding[1];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000235 };
236 GR_STATIC_ASSERT(!(sizeof(Desc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000237
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000238 // for code readability
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000239 typedef Desc::StageDesc StageDesc;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000240
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000241private:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000242 void genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000243
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000244 // Determines which uniforms will need to be bound.
245 void genStageCode(const GrGLContextInfo& gl,
246 int stageNum,
247 const char* fsInColor, // NULL means no incoming color
248 const char* fsOutColor,
249 const char* vsInCoord,
250 GrGLShaderBuilder* builder);
251
252 void genGeometryShader(const GrGLContextInfo& gl, GrGLShaderBuilder* segments) const;
253
254 void genUniformCoverage(GrGLShaderBuilder* segments, SkString* inOutCoverage);
255
256 // generates code to compute coverage based on edge AA.
257 void genEdgeCoverage(const GrGLContextInfo& gl,
258 SkString* coverageVar,
259 GrGLShaderBuilder* builder) const;
260
261 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
262 bool bindOutputsAttribsAndLinkProgram(const GrGLContextInfo& gl,
263 SkString texCoordAttrNames[GrDrawState::kMaxTexCoords],
264 bool bindColorOut,
265 bool bindDualSrcOut);
266
267 // Binds uniforms; initializes cache to invalid values.
268 void getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
269 const GrGLShaderBuilder& builder);
270
271 bool compileShaders(const GrGLContextInfo& gl, const GrGLShaderBuilder& builder);
272
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000273 const char* adjustInColor(const SkString& inColor) const;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000274
junov@google.comf93e7172011-03-31 21:26:24 +0000275 struct StageUniLocations {
276 GrGLint fTextureMatrixUni;
277 GrGLint fSamplerUni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000278 GrGLint fTexDomUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000279 void reset() {
280 fTextureMatrixUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000281 fSamplerUni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000282 fTexDomUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000283 }
junov@google.comf93e7172011-03-31 21:26:24 +0000284 };
285
286 struct UniLocations {
287 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000288 GrGLint fColorUni;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000289 GrGLint fCoverageUni;
Scroggo97c88c22011-05-11 14:05:25 +0000290 GrGLint fColorFilterUni;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000291 GrGLint fColorMatrixUni;
292 GrGLint fColorMatrixVecUni;
tomhudson@google.com93813632011-10-27 20:21:16 +0000293 StageUniLocations fStages[GrDrawState::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000294 void reset() {
295 fViewMatrixUni = kUnusedUniform;
296 fColorUni = kUnusedUniform;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000297 fCoverageUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000298 fColorFilterUni = kUnusedUniform;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000299 fColorMatrixUni = kUnusedUniform;
300 fColorMatrixVecUni = kUnusedUniform;
tomhudson@google.com93813632011-10-27 20:21:16 +0000301 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000302 fStages[s].reset();
303 }
304 }
junov@google.comf93e7172011-03-31 21:26:24 +0000305 };
306
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000307 // IDs
308 GrGLuint fVShaderID;
309 GrGLuint fGShaderID;
310 GrGLuint fFShaderID;
311 GrGLuint fProgramID;
312 // shader uniform locations (-1 if shader doesn't use them)
313 UniLocations fUniLocations;
junov@google.comf93e7172011-03-31 21:26:24 +0000314
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000315 // The matrix sent to GL is determined by both the client's matrix and
316 // the size of the viewport.
317 GrMatrix fViewMatrix;
318 SkISize fViewportSize;
junov@google.comf93e7172011-03-31 21:26:24 +0000319
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000320 // these reflect the current values of uniforms
321 // (GL uniform values travel with program)
322 GrColor fColor;
323 GrColor fCoverage;
324 GrColor fColorFilterColor;
325 GrMatrix fTextureMatrices[GrDrawState::kNumStages];
326 GrRect fTextureDomain[GrDrawState::kNumStages];
327 // The texture domain and texture matrix sent to GL depend upon the
328 // orientation.
329 GrGLTexture::Orientation fTextureOrientation[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000330
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000331 GrGLProgramStage* fProgramStage[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000332
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000333 Desc fDesc;
junov@google.comf93e7172011-03-31 21:26:24 +0000334
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000335 friend class GrGpuGL; // TODO: remove this by adding getters and moving functionality.
junov@google.comf93e7172011-03-31 21:26:24 +0000336
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000337 typedef GrRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000338};
339
340#endif