blob: 1dd1e3e41f62b38f61b6872647f5a90c5fcdaa50 [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
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +000092public:
junov@google.comf93e7172011-03-31 21:26:24 +000093
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.com22c5dea2011-07-07 14:38:03 +0000202 // for code readability
203 typedef ProgramDesc::StageDesc StageDesc;
204
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000205private:
206
207 const ProgramDesc& getDesc() { return fProgramDesc; }
208
junov@google.comf93e7172011-03-31 21:26:24 +0000209public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000210 enum {
211 kUnusedUniform = -1,
212 kSetAsAttribute = 1000,
213 };
214
junov@google.comf93e7172011-03-31 21:26:24 +0000215 struct StageUniLocations {
216 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000217 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000218 GrGLint fSamplerUni;
219 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000220 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000221 GrGLint fKernelUni;
222 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000223 void reset() {
224 fTextureMatrixUni = kUnusedUniform;
225 fNormalizedTexelSizeUni = kUnusedUniform;
226 fSamplerUni = kUnusedUniform;
227 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000228 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000229 fKernelUni = kUnusedUniform;
230 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000231 }
junov@google.comf93e7172011-03-31 21:26:24 +0000232 };
233
234 struct UniLocations {
235 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000236 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000237 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000238 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000239 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000240 void reset() {
241 fViewMatrixUni = kUnusedUniform;
242 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000243 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000244 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000245 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
246 fStages[s].reset();
247 }
248 }
junov@google.comf93e7172011-03-31 21:26:24 +0000249 };
250
251 class CachedData : public ::GrNoncopyable {
252 public:
253 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000254 }
255
256 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000257 }
258
259 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000260 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000261 }
262
junov@google.comf93e7172011-03-31 21:26:24 +0000263 public:
264
265 // IDs
266 GrGLuint fVShaderID;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000267 GrGLuint fGShaderID;
junov@google.comf93e7172011-03-31 21:26:24 +0000268 GrGLuint fFShaderID;
269 GrGLuint fProgramID;
270 // shader uniform locations (-1 if shader doesn't use them)
271 UniLocations fUniLocations;
272
273 GrMatrix fViewMatrix;
274
275 // these reflect the current values of uniforms
276 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000277 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000278 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000279 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000280 // width and height used for normalized texel size
281 int fTextureWidth[GrDrawTarget::kNumStages];
282 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000283 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
284 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
285 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000286 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000287
288 private:
289 enum Constants {
290 kUniLocationPreAllocSize = 8
291 };
292
junov@google.comf93e7172011-03-31 21:26:24 +0000293 }; // CachedData
294
junov@google.comf7c00f62011-08-18 18:15:16 +0000295 enum Constants {
296 kProgramKeySize = sizeof(ProgramDesc)
297 };
298
299 // Provide an opaque ProgramDesc
300 const uint32_t* keyData() const{
301 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
302 }
303
junov@google.comf93e7172011-03-31 21:26:24 +0000304private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000305
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000306 // Determines which uniforms will need to be bound.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000307 void genStageCode(const GrGLInterface* gl,
308 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000309 const ProgramDesc::StageDesc& desc,
310 const char* fsInColor, // NULL means no incoming color
311 const char* fsOutColor,
312 const char* vsInCoord,
313 ShaderCodeSegments* segments,
314 StageUniLocations* locations) const;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000315
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000316 void genGeometryShader(const GrGLInterface* gl,
317 GLSLVersion glslVersion,
318 ShaderCodeSegments* segments) const;
319
bsalomon@google.com66105672011-09-15 15:12:00 +0000320 // generates code to compute coverage based on edge AA.
321 void genEdgeCoverage(const GrGLInterface* gl,
322 GrVertexLayout layout,
323 CachedData* programData,
324 GrStringBuilder* coverageVar,
325 ShaderCodeSegments* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000326
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000327 static bool CompileShaders(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000328 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000329 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000330 CachedData* programData);
331
junov@google.comf93e7172011-03-31 21:26:24 +0000332 // Compiles a GL shader, returns shader ID or 0 if failed
333 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000334 static GrGLuint CompileShader(const GrGLInterface* gl,
335 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000336 const char** strings,
337 int* stringLengths);
338
bsalomon@google.com91961302011-05-09 18:39:58 +0000339 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
340 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000341 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000342 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000343 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
344 bool bindColorOut,
345 bool bindDualSrcOut,
346 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000347
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000348 // Binds uniforms; initializes cache to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000349 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
350 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000351
junov@google.comf93e7172011-03-31 21:26:24 +0000352 friend class GrGpuGLShaders;
353};
354
355#endif