blob: c186ba1fe3fd01dc63699de57d209c06e2f4f1a7 [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.comaeb21602011-08-30 18:13:44 +000069 static int EdgeAttributeIdx() { return 2 + GrDrawTarget::kMaxTexCoords; }
70
tomhudson@google.com0d831722011-06-02 15:37:14 +000071 static int ViewMatrixAttributeIdx() {
72 return 2 + GrDrawTarget::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000073 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000074 static int TextureMatrixAttributeIdx(int stage) {
75 return 5 + GrDrawTarget::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000076 }
77
junov@google.comf93e7172011-03-31 21:26:24 +000078private:
79
tomhudson@google.com0d831722011-06-02 15:37:14 +000080 // Parameters that affect code generation
81 // These structs should be kept compact; they are the input to an
82 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000083 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000084 ProgramDesc() {
85 // since we use this as part of a key we can't have any unitialized
86 // padding
87 memset(this, 0, sizeof(ProgramDesc));
88 }
89
tomhudson@google.com0d831722011-06-02 15:37:14 +000090 struct StageDesc {
91 enum OptFlagBits {
92 kNoPerspective_OptFlagBit = 1 << 0,
93 kIdentityMatrix_OptFlagBit = 1 << 1,
94 kCustomTextureDomain_OptFlagBit = 1 << 2,
95 kIsEnabled_OptFlagBit = 1 << 7
96 };
97 enum Modulation {
98 kColor_Modulation,
bsalomon@google.com1e257a52011-07-06 19:52:16 +000099 kAlpha_Modulation,
100
101 kModulationCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000102 };
103 enum FetchMode {
104 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000105 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000106 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000107
108 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000109 };
110 enum CoordMapping {
111 kIdentity_CoordMapping,
112 kRadialGradient_CoordMapping,
113 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000114 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000115 // need different shader computation when quadratic
116 // eq describing the gradient degenerates to a linear eq.
117 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000118 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000119 };
junov@google.comf93e7172011-03-31 21:26:24 +0000120
tomhudson@google.com0d831722011-06-02 15:37:14 +0000121 uint8_t fOptFlags;
122 uint8_t fModulation; // casts to enum Modulation
123 uint8_t fFetchMode; // casts to enum FetchMode
124 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000125 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000126
127 inline bool isEnabled() const {
128 return fOptFlags & kIsEnabled_OptFlagBit;
129 }
130 inline void setEnabled(bool newValue) {
131 if (newValue) {
132 fOptFlags |= kIsEnabled_OptFlagBit;
133 } else {
134 fOptFlags &= ~kIsEnabled_OptFlagBit;
135 }
136 }
137 };
138
139 enum ColorType {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000140 kNone_ColorType = 0,
141 kAttribute_ColorType = 1,
142 kUniform_ColorType = 2,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000143 };
144 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000145 // used as a per-pixel blend coeffecient. This controls whether a
146 // secondary source is output and what value it holds.
147 enum DualSrcOutput {
148 kNone_DualSrcOutput,
149 kCoverage_DualSrcOutput,
150 kCoverageISA_DualSrcOutput,
151 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000152
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000153 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000154 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000155
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000156 GrDrawTarget::VertexEdgeType fVertexEdgeType;
157
tomhudson@google.com0d831722011-06-02 15:37:14 +0000158 // stripped of bits that don't affect prog generation
159 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000160
tomhudson@google.com0d831722011-06-02 15:37:14 +0000161 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000162
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000163 // To enable experimental geometry shader code (not for use in
164 // production)
165#if GR_GL_EXPERIMENTAL_GS
166 bool fExperimentalGS;
167#endif
168
tomhudson@google.com0d831722011-06-02 15:37:14 +0000169 uint8_t fColorType; // casts to enum ColorType
170 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
171 int8_t fFirstCoverageStage;
172 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000173 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000174
tomhudson@google.com0d831722011-06-02 15:37:14 +0000175 int8_t fEdgeAANumEdges;
176 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000177
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000178 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000179
junov@google.comf93e7172011-03-31 21:26:24 +0000180 } fProgramDesc;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000181 GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000182
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000183 const ProgramDesc& getDesc() { return fProgramDesc; }
184
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000185 // for code readability
186 typedef ProgramDesc::StageDesc StageDesc;
187
junov@google.comf93e7172011-03-31 21:26:24 +0000188public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000189 enum {
190 kUnusedUniform = -1,
191 kSetAsAttribute = 1000,
192 };
193
junov@google.comf93e7172011-03-31 21:26:24 +0000194 struct StageUniLocations {
195 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000196 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000197 GrGLint fSamplerUni;
198 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000199 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000200 GrGLint fKernelUni;
201 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000202 void reset() {
203 fTextureMatrixUni = kUnusedUniform;
204 fNormalizedTexelSizeUni = kUnusedUniform;
205 fSamplerUni = kUnusedUniform;
206 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000207 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000208 fKernelUni = kUnusedUniform;
209 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000210 }
junov@google.comf93e7172011-03-31 21:26:24 +0000211 };
212
213 struct UniLocations {
214 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000215 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000216 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000217 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000218 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000219 void reset() {
220 fViewMatrixUni = kUnusedUniform;
221 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000222 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000223 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000224 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
225 fStages[s].reset();
226 }
227 }
junov@google.comf93e7172011-03-31 21:26:24 +0000228 };
229
230 class CachedData : public ::GrNoncopyable {
231 public:
232 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000233 }
234
235 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000236 }
237
238 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000239 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000240 }
241
junov@google.comf93e7172011-03-31 21:26:24 +0000242 public:
243
244 // IDs
245 GrGLuint fVShaderID;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000246 GrGLuint fGShaderID;
junov@google.comf93e7172011-03-31 21:26:24 +0000247 GrGLuint fFShaderID;
248 GrGLuint fProgramID;
249 // shader uniform locations (-1 if shader doesn't use them)
250 UniLocations fUniLocations;
251
252 GrMatrix fViewMatrix;
253
254 // these reflect the current values of uniforms
255 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000256 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000257 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000258 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000259 // width and height used for normalized texel size
260 int fTextureWidth[GrDrawTarget::kNumStages];
261 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000262 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
263 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
264 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000265 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000266
267 private:
268 enum Constants {
269 kUniLocationPreAllocSize = 8
270 };
271
junov@google.comf93e7172011-03-31 21:26:24 +0000272 }; // CachedData
273
junov@google.comf7c00f62011-08-18 18:15:16 +0000274 enum Constants {
275 kProgramKeySize = sizeof(ProgramDesc)
276 };
277
278 // Provide an opaque ProgramDesc
279 const uint32_t* keyData() const{
280 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
281 }
282
junov@google.comf93e7172011-03-31 21:26:24 +0000283private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000284 enum {
285 kUseUniform = 2000
286 };
287
288 // should set all fields in locations var to kUseUniform if the
289 // corresponding uniform is required for the program.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000290 void genStageCode(const GrGLInterface* gl,
291 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000292 const ProgramDesc::StageDesc& desc,
293 const char* fsInColor, // NULL means no incoming color
294 const char* fsOutColor,
295 const char* vsInCoord,
296 ShaderCodeSegments* segments,
297 StageUniLocations* locations) const;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000298
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000299 void genGeometryShader(const GrGLInterface* gl,
300 GLSLVersion glslVersion,
301 ShaderCodeSegments* segments) const;
302
bsalomon@google.com66105672011-09-15 15:12:00 +0000303 // generates code to compute coverage based on edge AA.
304 void genEdgeCoverage(const GrGLInterface* gl,
305 GrVertexLayout layout,
306 CachedData* programData,
307 GrStringBuilder* coverageVar,
308 ShaderCodeSegments* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000309
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000310 static bool CompileShaders(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000311 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000312 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000313 CachedData* programData);
314
junov@google.comf93e7172011-03-31 21:26:24 +0000315 // Compiles a GL shader, returns shader ID or 0 if failed
316 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000317 static GrGLuint CompileShader(const GrGLInterface* gl,
318 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000319 const char** strings,
320 int* stringLengths);
321
bsalomon@google.com91961302011-05-09 18:39:58 +0000322 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
323 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000324 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000325 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000326 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
327 bool bindColorOut,
328 bool bindDualSrcOut,
329 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000330
331 // Gets locations for all uniforms set to kUseUniform and initializes cache
332 // to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000333 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
334 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000335
junov@google.comf93e7172011-03-31 21:26:24 +0000336 friend class GrGpuGLShaders;
337};
338
339#endif