blob: 197da5d61c4f8c0378e4cf293c904312c81cbbfa [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 /**
64 * Attribute indices
65 */
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() {
75 return 2 + 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) {
78 return 5 + 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
142 enum ColorType {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000143 kNone_ColorType = 0,
144 kAttribute_ColorType = 1,
145 kUniform_ColorType = 2,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000146 };
147 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000148 // used as a per-pixel blend coeffecient. This controls whether a
149 // secondary source is output and what value it holds.
150 enum DualSrcOutput {
151 kNone_DualSrcOutput,
152 kCoverage_DualSrcOutput,
153 kCoverageISA_DualSrcOutput,
154 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000155
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000156 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000157 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000158
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000159 GrDrawTarget::VertexEdgeType fVertexEdgeType;
160
tomhudson@google.com0d831722011-06-02 15:37:14 +0000161 // stripped of bits that don't affect prog generation
162 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000163
tomhudson@google.com0d831722011-06-02 15:37:14 +0000164 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000165
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000166 // To enable experimental geometry shader code (not for use in
167 // production)
168#if GR_GL_EXPERIMENTAL_GS
169 bool fExperimentalGS;
170#endif
171
tomhudson@google.com0d831722011-06-02 15:37:14 +0000172 uint8_t fColorType; // casts to enum ColorType
173 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
174 int8_t fFirstCoverageStage;
175 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000176 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000177
tomhudson@google.com0d831722011-06-02 15:37:14 +0000178 int8_t fEdgeAANumEdges;
179 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000180
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000181 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000182
junov@google.comf93e7172011-03-31 21:26:24 +0000183 } fProgramDesc;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000184 GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000185
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000186 const ProgramDesc& getDesc() { return fProgramDesc; }
187
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000188 // for code readability
189 typedef ProgramDesc::StageDesc StageDesc;
190
junov@google.comf93e7172011-03-31 21:26:24 +0000191public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000192 enum {
193 kUnusedUniform = -1,
194 kSetAsAttribute = 1000,
195 };
196
junov@google.comf93e7172011-03-31 21:26:24 +0000197 struct StageUniLocations {
198 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000199 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000200 GrGLint fSamplerUni;
201 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000202 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000203 GrGLint fKernelUni;
204 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000205 void reset() {
206 fTextureMatrixUni = kUnusedUniform;
207 fNormalizedTexelSizeUni = kUnusedUniform;
208 fSamplerUni = kUnusedUniform;
209 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000210 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000211 fKernelUni = kUnusedUniform;
212 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000213 }
junov@google.comf93e7172011-03-31 21:26:24 +0000214 };
215
216 struct UniLocations {
217 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000218 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000219 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000220 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000221 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000222 void reset() {
223 fViewMatrixUni = kUnusedUniform;
224 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000225 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000226 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000227 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
228 fStages[s].reset();
229 }
230 }
junov@google.comf93e7172011-03-31 21:26:24 +0000231 };
232
233 class CachedData : public ::GrNoncopyable {
234 public:
235 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000236 }
237
238 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000239 }
240
241 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000242 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000243 }
244
junov@google.comf93e7172011-03-31 21:26:24 +0000245 public:
246
247 // IDs
248 GrGLuint fVShaderID;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000249 GrGLuint fGShaderID;
junov@google.comf93e7172011-03-31 21:26:24 +0000250 GrGLuint fFShaderID;
251 GrGLuint fProgramID;
252 // shader uniform locations (-1 if shader doesn't use them)
253 UniLocations fUniLocations;
254
255 GrMatrix fViewMatrix;
256
257 // these reflect the current values of uniforms
258 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000259 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000260 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000261 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000262 // width and height used for normalized texel size
263 int fTextureWidth[GrDrawTarget::kNumStages];
264 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000265 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
266 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
267 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000268 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000269
270 private:
271 enum Constants {
272 kUniLocationPreAllocSize = 8
273 };
274
junov@google.comf93e7172011-03-31 21:26:24 +0000275 }; // CachedData
276
junov@google.comf7c00f62011-08-18 18:15:16 +0000277 enum Constants {
278 kProgramKeySize = sizeof(ProgramDesc)
279 };
280
281 // Provide an opaque ProgramDesc
282 const uint32_t* keyData() const{
283 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
284 }
285
junov@google.comf93e7172011-03-31 21:26:24 +0000286private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000287 enum {
288 kUseUniform = 2000
289 };
290
291 // should set all fields in locations var to kUseUniform if the
292 // corresponding uniform is required for the program.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000293 void genStageCode(const GrGLInterface* gl,
294 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000295 const ProgramDesc::StageDesc& desc,
296 const char* fsInColor, // NULL means no incoming color
297 const char* fsOutColor,
298 const char* vsInCoord,
299 ShaderCodeSegments* segments,
300 StageUniLocations* locations) const;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000301
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000302 void genGeometryShader(const GrGLInterface* gl,
303 GLSLVersion glslVersion,
304 ShaderCodeSegments* segments) const;
305
bsalomon@google.com66105672011-09-15 15:12:00 +0000306 // generates code to compute coverage based on edge AA.
307 void genEdgeCoverage(const GrGLInterface* gl,
308 GrVertexLayout layout,
309 CachedData* programData,
310 GrStringBuilder* coverageVar,
311 ShaderCodeSegments* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000312
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000313 static bool CompileShaders(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000314 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000315 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000316 CachedData* programData);
317
junov@google.comf93e7172011-03-31 21:26:24 +0000318 // Compiles a GL shader, returns shader ID or 0 if failed
319 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000320 static GrGLuint CompileShader(const GrGLInterface* gl,
321 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000322 const char** strings,
323 int* stringLengths);
324
bsalomon@google.com91961302011-05-09 18:39:58 +0000325 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
326 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000327 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000328 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000329 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
330 bool bindColorOut,
331 bool bindDualSrcOut,
332 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000333
334 // Gets locations for all uniforms set to kUseUniform and initializes cache
335 // to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000336 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
337 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000338
junov@google.comf93e7172011-03-31 21:26:24 +0000339 friend class GrGpuGLShaders;
340};
341
342#endif