blob: e0ae4ebea544319dffc840b7fc12bf651774009f [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
bsalomon@google.com4fa66942011-09-20 19:06:12 +000021struct ShaderCodeSegments;
junov@google.comf93e7172011-03-31 21:26:24 +000022
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000023// optionally compile the experimental GS code. Set to GR_DEBUG
24// so that debug build bots will execute the code.
25#define GR_GL_EXPERIMENTAL_GS GR_DEBUG
26
junov@google.comf93e7172011-03-31 21:26:24 +000027/**
28 * This class manages a GPU program and records per-program information.
29 * We can specify the attribute locations so that they are constant
30 * across our shaders. But the driver determines the uniform locations
31 * at link time. We don't need to remember the sampler uniform location
32 * because we will bind a texture slot to it and never change it
33 * Uniforms are program-local so we can't rely on fHWState to hold the
34 * previous uniform state after a program change.
35 */
36class GrGLProgram {
37public:
bsalomon@google.com373a6632011-10-19 20:43:20 +000038 // Limited set of GLSL versions we build shaders for. Caller should round
39 // down the GLSL version to one of these enums.
bsalomon@google.com4fa66942011-09-20 19:06:12 +000040 enum GLSLVersion {
bsalomon@google.com373a6632011-10-19 20:43:20 +000041 /**
42 * Desktop GLSL 1.10 and ES2 shading lang (based on desktop GLSL 1.20)
43 */
44 k110_GLSLVersion,
45 /**
46 * Desktop GLSL 1.30
47 */
48 k130_GLSLVersion,
49 /**
50 * Dekstop GLSL 1.50
51 */
52 k150_GLSLVersion,
bsalomon@google.com4fa66942011-09-20 19:06:12 +000053 };
54
junov@google.comf93e7172011-03-31 21:26:24 +000055 class CachedData;
56
57 GrGLProgram();
58 ~GrGLProgram();
59
60 /**
junov@google.comf93e7172011-03-31 21:26:24 +000061 * This is the heavy initilization routine for building a GLProgram.
62 * The result of heavy init is not stored in datamembers of GrGLProgam,
63 * but in a separate cacheable container.
64 */
bsalomon@google.com0b77d682011-08-19 13:28:54 +000065 bool genProgram(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +000066 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000067 CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000068
bsalomon@google.com271cffc2011-05-20 14:13:56 +000069 /**
70 * The shader may modify the blend coeffecients. Params are in/out
71 */
72 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
73
74 /**
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000075 * Attribute indices. These should not overlap. Matrices consume 3 slots.
bsalomon@google.com271cffc2011-05-20 14:13:56 +000076 */
bsalomon@google.com91961302011-05-09 18:39:58 +000077 static int PositionAttributeIdx() { return 0; }
78 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
79 static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; }
bsalomon@google.coma3108262011-10-10 14:08:47 +000080 static int CoverageAttributeIdx() {
81 return 2 + GrDrawTarget::kMaxTexCoords;
82 }
83 static int EdgeAttributeIdx() { return 3 + GrDrawTarget::kMaxTexCoords; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000084
tomhudson@google.com0d831722011-06-02 15:37:14 +000085 static int ViewMatrixAttributeIdx() {
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000086 return 4 + GrDrawTarget::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000087 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000088 static int TextureMatrixAttributeIdx(int stage) {
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000089 return 7 + GrDrawTarget::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000090 }
91
junov@google.comf93e7172011-03-31 21:26:24 +000092private:
93
tomhudson@google.com0d831722011-06-02 15:37:14 +000094 // Parameters that affect code generation
95 // These structs should be kept compact; they are the input to an
96 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000097 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000098 ProgramDesc() {
99 // since we use this as part of a key we can't have any unitialized
100 // padding
101 memset(this, 0, sizeof(ProgramDesc));
102 }
103
tomhudson@google.com0d831722011-06-02 15:37:14 +0000104 struct StageDesc {
105 enum OptFlagBits {
106 kNoPerspective_OptFlagBit = 1 << 0,
107 kIdentityMatrix_OptFlagBit = 1 << 1,
108 kCustomTextureDomain_OptFlagBit = 1 << 2,
109 kIsEnabled_OptFlagBit = 1 << 7
110 };
111 enum Modulation {
112 kColor_Modulation,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000113 kAlpha_Modulation,
114
115 kModulationCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000116 };
117 enum FetchMode {
118 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000119 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000120 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000121
122 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000123 };
124 enum CoordMapping {
125 kIdentity_CoordMapping,
126 kRadialGradient_CoordMapping,
127 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000128 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000129 // need different shader computation when quadratic
130 // eq describing the gradient degenerates to a linear eq.
131 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000132 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000133 };
junov@google.comf93e7172011-03-31 21:26:24 +0000134
tomhudson@google.com0d831722011-06-02 15:37:14 +0000135 uint8_t fOptFlags;
136 uint8_t fModulation; // casts to enum Modulation
137 uint8_t fFetchMode; // casts to enum FetchMode
138 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000139 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000140
141 inline bool isEnabled() const {
bsalomon@google.comc2c9b972011-10-03 13:17:22 +0000142 return SkToBool(fOptFlags & kIsEnabled_OptFlagBit);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000143 }
144 inline void setEnabled(bool newValue) {
145 if (newValue) {
146 fOptFlags |= kIsEnabled_OptFlagBit;
147 } else {
148 fOptFlags &= ~kIsEnabled_OptFlagBit;
149 }
150 }
151 };
152
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000153 // Specifies where the intitial color comes from before the stages are
154 // applied.
tomhudson@google.com0d831722011-06-02 15:37:14 +0000155 enum ColorType {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000156 kSolidWhite_ColorType,
157 kTransBlack_ColorType,
158 kAttribute_ColorType,
159 kUniform_ColorType,
160
161 kColorTypeCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000162 };
163 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000164 // used as a per-pixel blend coeffecient. This controls whether a
165 // secondary source is output and what value it holds.
166 enum DualSrcOutput {
167 kNone_DualSrcOutput,
168 kCoverage_DualSrcOutput,
169 kCoverageISA_DualSrcOutput,
170 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000171
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000172 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000173 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000174
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000175 GrDrawTarget::VertexEdgeType fVertexEdgeType;
176
tomhudson@google.com0d831722011-06-02 15:37:14 +0000177 // stripped of bits that don't affect prog generation
178 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000179
tomhudson@google.com0d831722011-06-02 15:37:14 +0000180 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000181
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000182 // To enable experimental geometry shader code (not for use in
183 // production)
184#if GR_GL_EXPERIMENTAL_GS
185 bool fExperimentalGS;
186#endif
187
tomhudson@google.com0d831722011-06-02 15:37:14 +0000188 uint8_t fColorType; // casts to enum ColorType
189 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
190 int8_t fFirstCoverageStage;
191 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000192 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000193
tomhudson@google.com0d831722011-06-02 15:37:14 +0000194 int8_t fEdgeAANumEdges;
195 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000196
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000197 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000198
junov@google.comf93e7172011-03-31 21:26:24 +0000199 } fProgramDesc;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000200 GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000201
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000202 const ProgramDesc& getDesc() { return fProgramDesc; }
203
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000204 // for code readability
205 typedef ProgramDesc::StageDesc StageDesc;
206
junov@google.comf93e7172011-03-31 21:26:24 +0000207public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000208 enum {
209 kUnusedUniform = -1,
210 kSetAsAttribute = 1000,
211 };
212
junov@google.comf93e7172011-03-31 21:26:24 +0000213 struct StageUniLocations {
214 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000215 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000216 GrGLint fSamplerUni;
217 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000218 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000219 GrGLint fKernelUni;
220 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000221 void reset() {
222 fTextureMatrixUni = kUnusedUniform;
223 fNormalizedTexelSizeUni = kUnusedUniform;
224 fSamplerUni = kUnusedUniform;
225 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000226 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000227 fKernelUni = kUnusedUniform;
228 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000229 }
junov@google.comf93e7172011-03-31 21:26:24 +0000230 };
231
232 struct UniLocations {
233 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000234 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000235 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000236 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000237 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000238 void reset() {
239 fViewMatrixUni = kUnusedUniform;
240 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000241 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000242 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000243 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
244 fStages[s].reset();
245 }
246 }
junov@google.comf93e7172011-03-31 21:26:24 +0000247 };
248
249 class CachedData : public ::GrNoncopyable {
250 public:
251 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000252 }
253
254 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000255 }
256
257 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000258 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000259 }
260
junov@google.comf93e7172011-03-31 21:26:24 +0000261 public:
262
263 // IDs
264 GrGLuint fVShaderID;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000265 GrGLuint fGShaderID;
junov@google.comf93e7172011-03-31 21:26:24 +0000266 GrGLuint fFShaderID;
267 GrGLuint fProgramID;
268 // shader uniform locations (-1 if shader doesn't use them)
269 UniLocations fUniLocations;
270
271 GrMatrix fViewMatrix;
272
273 // these reflect the current values of uniforms
274 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000275 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000276 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000277 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000278 // width and height used for normalized texel size
279 int fTextureWidth[GrDrawTarget::kNumStages];
280 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000281 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
282 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
283 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000284 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000285
286 private:
287 enum Constants {
288 kUniLocationPreAllocSize = 8
289 };
290
junov@google.comf93e7172011-03-31 21:26:24 +0000291 }; // CachedData
292
junov@google.comf7c00f62011-08-18 18:15:16 +0000293 enum Constants {
294 kProgramKeySize = sizeof(ProgramDesc)
295 };
296
297 // Provide an opaque ProgramDesc
298 const uint32_t* keyData() const{
299 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
300 }
301
junov@google.comf93e7172011-03-31 21:26:24 +0000302private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000303 enum {
304 kUseUniform = 2000
305 };
306
307 // should set all fields in locations var to kUseUniform if the
308 // corresponding uniform is required for the program.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000309 void genStageCode(const GrGLInterface* gl,
310 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000311 const ProgramDesc::StageDesc& desc,
312 const char* fsInColor, // NULL means no incoming color
313 const char* fsOutColor,
314 const char* vsInCoord,
315 ShaderCodeSegments* segments,
316 StageUniLocations* locations) const;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000317
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000318 void genGeometryShader(const GrGLInterface* gl,
319 GLSLVersion glslVersion,
320 ShaderCodeSegments* segments) const;
321
bsalomon@google.com66105672011-09-15 15:12:00 +0000322 // generates code to compute coverage based on edge AA.
323 void genEdgeCoverage(const GrGLInterface* gl,
324 GrVertexLayout layout,
325 CachedData* programData,
326 GrStringBuilder* coverageVar,
327 ShaderCodeSegments* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000328
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000329 static bool CompileShaders(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000330 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000331 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000332 CachedData* programData);
333
junov@google.comf93e7172011-03-31 21:26:24 +0000334 // Compiles a GL shader, returns shader ID or 0 if failed
335 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000336 static GrGLuint CompileShader(const GrGLInterface* gl,
337 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000338 const char** strings,
339 int* stringLengths);
340
bsalomon@google.com91961302011-05-09 18:39:58 +0000341 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
342 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000343 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000344 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000345 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
346 bool bindColorOut,
347 bool bindDualSrcOut,
348 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000349
350 // Gets locations for all uniforms set to kUseUniform and initializes cache
351 // to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000352 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
353 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000354
tomhudson@google.com5960d002011-10-19 20:21:48 +0000355 const char* genRadialVS(int stageNum, ShaderCodeSegments* segments,
356 StageUniLocations* locations,
357 const char** radial2VaryingVSName,
358 const char** radial2VaryingFSName,
359 const char* varyingVSName,
360 int varyingDims, int coordDims) const;
361
362 void gen2x2FS(int stageNum,
363 ShaderCodeSegments* segments,
364 StageUniLocations* locations,
365 GrStringBuilder* sampleCoords,
366 const char* samplerName,
367 const char* texelSizeName,
368 const char* smear,
369 const char* fsOutColor,
370 GrStringBuilder& texFunc,
371 GrStringBuilder& modulate,
372 bool complexCoord,
373 int coordDims) const;
374
375 void genConvolutionVS(int stageNum,
376 const ProgramDesc::StageDesc& desc,
377 ShaderCodeSegments* segments,
378 StageUniLocations* locations,
379 const char** kernelName,
380 const char** imageIncrementName,
381 const char* varyingVSName) const;
382
383
384 void genConvolutionFS(int stageNum,
385 const ProgramDesc::StageDesc& desc,
386 ShaderCodeSegments* segments,
387 const char* samplerName,
388 const char* kernelName,
389 const char* smear,
390 const char* imageIncrementName,
391 const char* fsOutColor,
392 GrStringBuilder& sampleCoords,
393 GrStringBuilder& texFunc,
394 GrStringBuilder& modulate) const;
395
junov@google.comf93e7172011-03-31 21:26:24 +0000396 friend class GrGpuGLShaders;
397};
398
399#endif