blob: 783ebf6df3214d43084c865eda353970cd2ed6e3 [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.com4fa66942011-09-20 19:06:12 +000038 enum GLSLVersion {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000039 k120_GLSLVersion, // Desktop GLSL 1.20 and ES2 shading lang
40 k130_GLSLVersion, // Desktop GLSL 1.30
41 k150_GLSLVersion // Dekstop GLSL 1.50
bsalomon@google.com4fa66942011-09-20 19:06:12 +000042 };
43
junov@google.comf93e7172011-03-31 21:26:24 +000044 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,
bsalomon@google.com4fa66942011-09-20 19:06:12 +000055 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000056 CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000057
bsalomon@google.com271cffc2011-05-20 14:13:56 +000058 /**
59 * The shader may modify the blend coeffecients. Params are in/out
60 */
61 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
62
63 /**
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000064 * Attribute indices. These should not overlap. Matrices consume 3 slots.
bsalomon@google.com271cffc2011-05-20 14:13:56 +000065 */
bsalomon@google.com91961302011-05-09 18:39:58 +000066 static int PositionAttributeIdx() { return 0; }
67 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
68 static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; }
bsalomon@google.coma3108262011-10-10 14:08:47 +000069 static int CoverageAttributeIdx() {
70 return 2 + GrDrawTarget::kMaxTexCoords;
71 }
72 static int EdgeAttributeIdx() { return 3 + GrDrawTarget::kMaxTexCoords; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000073
tomhudson@google.com0d831722011-06-02 15:37:14 +000074 static int ViewMatrixAttributeIdx() {
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000075 return 4 + GrDrawTarget::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000076 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000077 static int TextureMatrixAttributeIdx(int stage) {
bsalomon@google.comb5b5eaf2011-10-19 13:25:46 +000078 return 7 + GrDrawTarget::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000079 }
80
junov@google.comf93e7172011-03-31 21:26:24 +000081private:
82
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.
junov@google.comf93e7172011-03-31 21:26:24 +000086 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000087 ProgramDesc() {
88 // since we use this as part of a key we can't have any unitialized
89 // padding
90 memset(this, 0, sizeof(ProgramDesc));
91 }
92
tomhudson@google.com0d831722011-06-02 15:37:14 +000093 struct StageDesc {
94 enum OptFlagBits {
95 kNoPerspective_OptFlagBit = 1 << 0,
96 kIdentityMatrix_OptFlagBit = 1 << 1,
97 kCustomTextureDomain_OptFlagBit = 1 << 2,
98 kIsEnabled_OptFlagBit = 1 << 7
99 };
100 enum Modulation {
101 kColor_Modulation,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000102 kAlpha_Modulation,
103
104 kModulationCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000105 };
106 enum FetchMode {
107 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000108 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000109 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000110
111 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000112 };
113 enum CoordMapping {
114 kIdentity_CoordMapping,
115 kRadialGradient_CoordMapping,
116 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000117 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000118 // need different shader computation when quadratic
119 // eq describing the gradient degenerates to a linear eq.
120 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000121 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000122 };
junov@google.comf93e7172011-03-31 21:26:24 +0000123
tomhudson@google.com0d831722011-06-02 15:37:14 +0000124 uint8_t fOptFlags;
125 uint8_t fModulation; // casts to enum Modulation
126 uint8_t fFetchMode; // casts to enum FetchMode
127 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000128 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000129
130 inline bool isEnabled() const {
bsalomon@google.comc2c9b972011-10-03 13:17:22 +0000131 return SkToBool(fOptFlags & kIsEnabled_OptFlagBit);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000132 }
133 inline void setEnabled(bool newValue) {
134 if (newValue) {
135 fOptFlags |= kIsEnabled_OptFlagBit;
136 } else {
137 fOptFlags &= ~kIsEnabled_OptFlagBit;
138 }
139 }
140 };
141
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000142 // Specifies where the intitial color comes from before the stages are
143 // applied.
tomhudson@google.com0d831722011-06-02 15:37:14 +0000144 enum ColorType {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000145 kSolidWhite_ColorType,
146 kTransBlack_ColorType,
147 kAttribute_ColorType,
148 kUniform_ColorType,
149
150 kColorTypeCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000151 };
152 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000153 // used as a per-pixel blend coeffecient. This controls whether a
154 // secondary source is output and what value it holds.
155 enum DualSrcOutput {
156 kNone_DualSrcOutput,
157 kCoverage_DualSrcOutput,
158 kCoverageISA_DualSrcOutput,
159 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000160
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000161 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000162 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000163
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000164 GrDrawTarget::VertexEdgeType fVertexEdgeType;
165
tomhudson@google.com0d831722011-06-02 15:37:14 +0000166 // stripped of bits that don't affect prog generation
167 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000168
tomhudson@google.com0d831722011-06-02 15:37:14 +0000169 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000170
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000171 // To enable experimental geometry shader code (not for use in
172 // production)
173#if GR_GL_EXPERIMENTAL_GS
174 bool fExperimentalGS;
175#endif
176
tomhudson@google.com0d831722011-06-02 15:37:14 +0000177 uint8_t fColorType; // casts to enum ColorType
178 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
179 int8_t fFirstCoverageStage;
180 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000181 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000182
tomhudson@google.com0d831722011-06-02 15:37:14 +0000183 int8_t fEdgeAANumEdges;
184 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000185
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000186 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000187
junov@google.comf93e7172011-03-31 21:26:24 +0000188 } fProgramDesc;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000189 GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000190
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000191 const ProgramDesc& getDesc() { return fProgramDesc; }
192
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000193 // for code readability
194 typedef ProgramDesc::StageDesc StageDesc;
195
junov@google.comf93e7172011-03-31 21:26:24 +0000196public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000197 enum {
198 kUnusedUniform = -1,
199 kSetAsAttribute = 1000,
200 };
201
junov@google.comf93e7172011-03-31 21:26:24 +0000202 struct StageUniLocations {
203 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000204 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000205 GrGLint fSamplerUni;
206 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000207 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000208 GrGLint fKernelUni;
209 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000210 void reset() {
211 fTextureMatrixUni = kUnusedUniform;
212 fNormalizedTexelSizeUni = kUnusedUniform;
213 fSamplerUni = kUnusedUniform;
214 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000215 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000216 fKernelUni = kUnusedUniform;
217 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000218 }
junov@google.comf93e7172011-03-31 21:26:24 +0000219 };
220
221 struct UniLocations {
222 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000223 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000224 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000225 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000226 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000227 void reset() {
228 fViewMatrixUni = kUnusedUniform;
229 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000230 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000231 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000232 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
233 fStages[s].reset();
234 }
235 }
junov@google.comf93e7172011-03-31 21:26:24 +0000236 };
237
238 class CachedData : public ::GrNoncopyable {
239 public:
240 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000241 }
242
243 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000244 }
245
246 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000247 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000248 }
249
junov@google.comf93e7172011-03-31 21:26:24 +0000250 public:
251
252 // IDs
253 GrGLuint fVShaderID;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000254 GrGLuint fGShaderID;
junov@google.comf93e7172011-03-31 21:26:24 +0000255 GrGLuint fFShaderID;
256 GrGLuint fProgramID;
257 // shader uniform locations (-1 if shader doesn't use them)
258 UniLocations fUniLocations;
259
260 GrMatrix fViewMatrix;
261
262 // these reflect the current values of uniforms
263 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000264 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000265 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000266 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000267 // width and height used for normalized texel size
268 int fTextureWidth[GrDrawTarget::kNumStages];
269 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000270 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
271 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
272 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000273 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000274
275 private:
276 enum Constants {
277 kUniLocationPreAllocSize = 8
278 };
279
junov@google.comf93e7172011-03-31 21:26:24 +0000280 }; // CachedData
281
junov@google.comf7c00f62011-08-18 18:15:16 +0000282 enum Constants {
283 kProgramKeySize = sizeof(ProgramDesc)
284 };
285
286 // Provide an opaque ProgramDesc
287 const uint32_t* keyData() const{
288 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
289 }
290
junov@google.comf93e7172011-03-31 21:26:24 +0000291private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000292 enum {
293 kUseUniform = 2000
294 };
295
296 // should set all fields in locations var to kUseUniform if the
297 // corresponding uniform is required for the program.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000298 void genStageCode(const GrGLInterface* gl,
299 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000300 const ProgramDesc::StageDesc& desc,
301 const char* fsInColor, // NULL means no incoming color
302 const char* fsOutColor,
303 const char* vsInCoord,
304 ShaderCodeSegments* segments,
305 StageUniLocations* locations) const;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000306
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000307 void genGeometryShader(const GrGLInterface* gl,
308 GLSLVersion glslVersion,
309 ShaderCodeSegments* segments) const;
310
bsalomon@google.com66105672011-09-15 15:12:00 +0000311 // generates code to compute coverage based on edge AA.
312 void genEdgeCoverage(const GrGLInterface* gl,
313 GrVertexLayout layout,
314 CachedData* programData,
315 GrStringBuilder* coverageVar,
316 ShaderCodeSegments* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000317
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000318 static bool CompileShaders(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000319 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000320 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000321 CachedData* programData);
322
junov@google.comf93e7172011-03-31 21:26:24 +0000323 // Compiles a GL shader, returns shader ID or 0 if failed
324 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000325 static GrGLuint CompileShader(const GrGLInterface* gl,
326 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000327 const char** strings,
328 int* stringLengths);
329
bsalomon@google.com91961302011-05-09 18:39:58 +0000330 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
331 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000332 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000333 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000334 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
335 bool bindColorOut,
336 bool bindDualSrcOut,
337 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000338
339 // Gets locations for all uniforms set to kUseUniform and initializes cache
340 // to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000341 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
342 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000343
junov@google.comf93e7172011-03-31 21:26:24 +0000344 friend class GrGpuGLShaders;
345};
346
347#endif