blob: e0f8fbc1b4fa4658e4e1dc839a5cf5fa124a7c15 [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
tomhudson@google.com93813632011-10-27 20:21:16 +000013#include "GrDrawState.h"
junov@google.comf93e7172011-03-31 21:26:24 +000014#include "GrGLInterface.h"
bsalomon@google.com91961302011-05-09 18:39:58 +000015#include "GrStringBuilder.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000016#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;
junov@google.comd31cbc42011-05-17 17:01:17 +000021
bsalomon@google.com4fa66942011-09-20 19:06:12 +000022struct ShaderCodeSegments;
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.com373a6632011-10-19 20:43:20 +000039 // Limited set of GLSL versions we build shaders for. Caller should round
40 // down the GLSL version to one of these enums.
bsalomon@google.com4fa66942011-09-20 19:06:12 +000041 enum GLSLVersion {
bsalomon@google.com373a6632011-10-19 20:43:20 +000042 /**
43 * Desktop GLSL 1.10 and ES2 shading lang (based on desktop GLSL 1.20)
44 */
45 k110_GLSLVersion,
46 /**
47 * Desktop GLSL 1.30
48 */
49 k130_GLSLVersion,
50 /**
51 * Dekstop GLSL 1.50
52 */
53 k150_GLSLVersion,
bsalomon@google.com4fa66942011-09-20 19:06:12 +000054 };
55
junov@google.comf93e7172011-03-31 21:26:24 +000056 class CachedData;
57
58 GrGLProgram();
59 ~GrGLProgram();
60
61 /**
junov@google.comf93e7172011-03-31 21:26:24 +000062 * This is the heavy initilization routine for building a GLProgram.
63 * The result of heavy init is not stored in datamembers of GrGLProgam,
64 * but in a separate cacheable container.
65 */
bsalomon@google.com0b77d682011-08-19 13:28:54 +000066 bool genProgram(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +000067 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000068 CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000069
bsalomon@google.com271cffc2011-05-20 14:13:56 +000070 /**
71 * The shader may modify the blend coeffecients. Params are in/out
72 */
73 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
74
75 /**
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000076 * Attribute indices. These should not overlap. Matrices consume 3 slots.
bsalomon@google.com271cffc2011-05-20 14:13:56 +000077 */
bsalomon@google.com91961302011-05-09 18:39:58 +000078 static int PositionAttributeIdx() { return 0; }
79 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
tomhudson@google.com93813632011-10-27 20:21:16 +000080 static int ColorAttributeIdx() { return 1 + GrDrawState::kMaxTexCoords; }
bsalomon@google.coma3108262011-10-10 14:08:47 +000081 static int CoverageAttributeIdx() {
tomhudson@google.com93813632011-10-27 20:21:16 +000082 return 2 + GrDrawState::kMaxTexCoords;
bsalomon@google.coma3108262011-10-10 14:08:47 +000083 }
tomhudson@google.com93813632011-10-27 20:21:16 +000084 static int EdgeAttributeIdx() { return 3 + GrDrawState::kMaxTexCoords; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000085
tomhudson@google.com0d831722011-06-02 15:37:14 +000086 static int ViewMatrixAttributeIdx() {
tomhudson@google.com93813632011-10-27 20:21:16 +000087 return 4 + GrDrawState::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000088 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000089 static int TextureMatrixAttributeIdx(int stage) {
tomhudson@google.com93813632011-10-27 20:21:16 +000090 return 7 + GrDrawState::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000091 }
92
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +000093public:
junov@google.comf93e7172011-03-31 21:26:24 +000094
tomhudson@google.com0d831722011-06-02 15:37:14 +000095 // Parameters that affect code generation
96 // These structs should be kept compact; they are the input to an
97 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000098 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000099 ProgramDesc() {
100 // since we use this as part of a key we can't have any unitialized
101 // padding
102 memset(this, 0, sizeof(ProgramDesc));
103 }
104
bsalomon@google.comc4364992011-11-07 15:54:49 +0000105 enum OutputPM {
106 // PM-color OR color with no alpha channel
107 kYes_OutputPM,
108 // nonPM-color with alpha channel
109 kNo_OutputPM,
110
111 kOutputPMCnt
112 };
113
tomhudson@google.com0d831722011-06-02 15:37:14 +0000114 struct StageDesc {
115 enum OptFlagBits {
116 kNoPerspective_OptFlagBit = 1 << 0,
117 kIdentityMatrix_OptFlagBit = 1 << 1,
118 kCustomTextureDomain_OptFlagBit = 1 << 2,
119 kIsEnabled_OptFlagBit = 1 << 7
120 };
tomhudson@google.com0d831722011-06-02 15:37:14 +0000121 enum FetchMode {
122 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000123 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000124 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000125
126 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000127 };
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000128 /**
129 Describes how to swizzle the texture's components. If swizzling
130 can be applied outside of the shader (GL_ARB_texture_swizzle) that
131 is preferrable to using this enum. Changing the enum value used
132 causes another program to be generated.
133 */
134 enum Swizzle {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000135 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000136 No swizzling applied to the inputs
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000137 */
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000138 kNone_Swizzle,
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000139 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000140 Swap the R and B channels
141 */
142 kSwapRAndB_Swizzle,
143 /**
144 Smear alpha across all four channels.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000145 */
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000146 kAlphaSmear_Swizzle,
147 kSwizzleCnt
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000148 };
tomhudson@google.com0d831722011-06-02 15:37:14 +0000149 enum CoordMapping {
150 kIdentity_CoordMapping,
151 kRadialGradient_CoordMapping,
152 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000153 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000154 // need different shader computation when quadratic
155 // eq describing the gradient degenerates to a linear eq.
156 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000157 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000158 };
junov@google.comf93e7172011-03-31 21:26:24 +0000159
tomhudson@google.com0d831722011-06-02 15:37:14 +0000160 uint8_t fOptFlags;
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000161 uint8_t fSwizzle; // casts to enum Swizzle
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000162 uint8_t fFetchMode; // casts to enum FetchMode
tomhudson@google.com0d831722011-06-02 15:37:14 +0000163 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000164 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000165
166 inline bool isEnabled() const {
bsalomon@google.comc2c9b972011-10-03 13:17:22 +0000167 return SkToBool(fOptFlags & kIsEnabled_OptFlagBit);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000168 }
169 inline void setEnabled(bool newValue) {
170 if (newValue) {
171 fOptFlags |= kIsEnabled_OptFlagBit;
172 } else {
173 fOptFlags &= ~kIsEnabled_OptFlagBit;
174 }
175 }
176 };
177
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000178 // Specifies where the intitial color comes from before the stages are
179 // applied.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000180 enum ColorInput {
181 kSolidWhite_ColorInput,
182 kTransBlack_ColorInput,
183 kAttribute_ColorInput,
184 kUniform_ColorInput,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000185
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000186 kColorInputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000187 };
188 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000189 // used as a per-pixel blend coeffecient. This controls whether a
190 // secondary source is output and what value it holds.
191 enum DualSrcOutput {
192 kNone_DualSrcOutput,
193 kCoverage_DualSrcOutput,
194 kCoverageISA_DualSrcOutput,
195 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000196
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000197 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000198 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000199
tomhudson@google.com93813632011-10-27 20:21:16 +0000200 GrDrawState::VertexEdgeType fVertexEdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000201
tomhudson@google.com0d831722011-06-02 15:37:14 +0000202 // stripped of bits that don't affect prog generation
203 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000204
tomhudson@google.com93813632011-10-27 20:21:16 +0000205 StageDesc fStages[GrDrawState::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000206
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000207 // To enable experimental geometry shader code (not for use in
208 // production)
209#if GR_GL_EXPERIMENTAL_GS
210 bool fExperimentalGS;
211#endif
212
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000213 uint8_t fColorInput; // casts to enum ColorInput
bsalomon@google.comc4364992011-11-07 15:54:49 +0000214 uint8_t fOutputPM; // cases to enum OutputPM
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000215 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
tomhudson@google.com0d831722011-06-02 15:37:14 +0000216 int8_t fFirstCoverageStage;
217 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000218 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000219
tomhudson@google.com0d831722011-06-02 15:37:14 +0000220 int8_t fEdgeAANumEdges;
221 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000222
junov@google.comf93e7172011-03-31 21:26:24 +0000223 } fProgramDesc;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000224 GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000225
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000226 // for code readability
227 typedef ProgramDesc::StageDesc StageDesc;
228
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000229private:
230
231 const ProgramDesc& getDesc() { return fProgramDesc; }
232
junov@google.comf93e7172011-03-31 21:26:24 +0000233public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000234 enum {
235 kUnusedUniform = -1,
236 kSetAsAttribute = 1000,
237 };
238
junov@google.comf93e7172011-03-31 21:26:24 +0000239 struct StageUniLocations {
240 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000241 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000242 GrGLint fSamplerUni;
243 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000244 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000245 GrGLint fKernelUni;
246 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000247 void reset() {
248 fTextureMatrixUni = kUnusedUniform;
249 fNormalizedTexelSizeUni = kUnusedUniform;
250 fSamplerUni = kUnusedUniform;
251 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000252 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000253 fKernelUni = kUnusedUniform;
254 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000255 }
junov@google.comf93e7172011-03-31 21:26:24 +0000256 };
257
258 struct UniLocations {
259 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000260 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000261 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000262 GrGLint fColorFilterUni;
tomhudson@google.com93813632011-10-27 20:21:16 +0000263 StageUniLocations fStages[GrDrawState::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000264 void reset() {
265 fViewMatrixUni = kUnusedUniform;
266 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000267 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000268 fColorFilterUni = kUnusedUniform;
tomhudson@google.com93813632011-10-27 20:21:16 +0000269 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000270 fStages[s].reset();
271 }
272 }
junov@google.comf93e7172011-03-31 21:26:24 +0000273 };
274
275 class CachedData : public ::GrNoncopyable {
276 public:
277 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000278 }
279
280 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000281 }
282
283 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000284 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000285 }
286
junov@google.comf93e7172011-03-31 21:26:24 +0000287 public:
288
289 // IDs
290 GrGLuint fVShaderID;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000291 GrGLuint fGShaderID;
junov@google.comf93e7172011-03-31 21:26:24 +0000292 GrGLuint fFShaderID;
293 GrGLuint fProgramID;
294 // shader uniform locations (-1 if shader doesn't use them)
295 UniLocations fUniLocations;
296
297 GrMatrix fViewMatrix;
298
299 // these reflect the current values of uniforms
300 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000301 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000302 GrColor fColorFilterColor;
tomhudson@google.com93813632011-10-27 20:21:16 +0000303 GrMatrix fTextureMatrices[GrDrawState::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000304 // width and height used for normalized texel size
tomhudson@google.com93813632011-10-27 20:21:16 +0000305 int fTextureWidth[GrDrawState::kNumStages];
306 int fTextureHeight[GrDrawState::kNumStages];
307 GrScalar fRadial2CenterX1[GrDrawState::kNumStages];
308 GrScalar fRadial2Radius0[GrDrawState::kNumStages];
309 bool fRadial2PosRoot[GrDrawState::kNumStages];
310 GrRect fTextureDomain[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000311
312 private:
313 enum Constants {
314 kUniLocationPreAllocSize = 8
315 };
316
junov@google.comf93e7172011-03-31 21:26:24 +0000317 }; // CachedData
318
junov@google.comf7c00f62011-08-18 18:15:16 +0000319 enum Constants {
320 kProgramKeySize = sizeof(ProgramDesc)
321 };
322
323 // Provide an opaque ProgramDesc
324 const uint32_t* keyData() const{
325 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
326 }
327
junov@google.comf93e7172011-03-31 21:26:24 +0000328private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000329
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000330 // Determines which uniforms will need to be bound.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000331 void genStageCode(const GrGLInterface* gl,
332 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000333 const ProgramDesc::StageDesc& desc,
334 const char* fsInColor, // NULL means no incoming color
335 const char* fsOutColor,
336 const char* vsInCoord,
337 ShaderCodeSegments* segments,
338 StageUniLocations* locations) const;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000339
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000340 void genGeometryShader(const GrGLInterface* gl,
341 GLSLVersion glslVersion,
342 ShaderCodeSegments* segments) const;
343
bsalomon@google.com66105672011-09-15 15:12:00 +0000344 // generates code to compute coverage based on edge AA.
345 void genEdgeCoverage(const GrGLInterface* gl,
346 GrVertexLayout layout,
347 CachedData* programData,
348 GrStringBuilder* coverageVar,
349 ShaderCodeSegments* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000350
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000351 static bool CompileShaders(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000352 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000353 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000354 CachedData* programData);
355
junov@google.comf93e7172011-03-31 21:26:24 +0000356 // Compiles a GL shader, returns shader ID or 0 if failed
357 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000358 static GrGLuint CompileShader(const GrGLInterface* gl,
359 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000360 const char** strings,
361 int* stringLengths);
362
bsalomon@google.com91961302011-05-09 18:39:58 +0000363 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
364 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000365 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000366 const GrGLInterface* gl,
tomhudson@google.com93813632011-10-27 20:21:16 +0000367 GrStringBuilder texCoordAttrNames[GrDrawState::kMaxTexCoords],
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000368 bool bindColorOut,
369 bool bindDualSrcOut,
370 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000371
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000372 // Binds uniforms; initializes cache to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000373 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
374 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000375
junov@google.comf93e7172011-03-31 21:26:24 +0000376 friend class GrGpuGLShaders;
377};
378
379#endif