blob: 026f68b3ddc3dc34c08482e552fd9638cb8f49c7 [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
21struct ShaderCodeSegments {
bsalomon@google.com271cffc2011-05-20 14:13:56 +000022 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
junov@google.comd31cbc42011-05-17 17:01:17 +000023 GrStringBuilder fVSUnis;
24 GrStringBuilder fVSAttrs;
25 GrStringBuilder fVaryings;
26 GrStringBuilder fFSUnis;
bsalomon@google.com271cffc2011-05-20 14:13:56 +000027 GrStringBuilder fFSOutputs;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +000028 GrStringBuilder fFSFunctions;
junov@google.comd31cbc42011-05-17 17:01:17 +000029 GrStringBuilder fVSCode;
30 GrStringBuilder fFSCode;
31};
junov@google.comf93e7172011-03-31 21:26:24 +000032
33/**
34 * This class manages a GPU program and records per-program information.
35 * We can specify the attribute locations so that they are constant
36 * across our shaders. But the driver determines the uniform locations
37 * at link time. We don't need to remember the sampler uniform location
38 * because we will bind a texture slot to it and never change it
39 * Uniforms are program-local so we can't rely on fHWState to hold the
40 * previous uniform state after a program change.
41 */
42class GrGLProgram {
43public:
44 class CachedData;
45
46 GrGLProgram();
47 ~GrGLProgram();
48
49 /**
50 * Streams data that can uniquely identifies the generated
51 * gpu program into a key, for cache indexing purposes.
52 *
53 * @param key The key object to receive the key data
54 */
55 void buildKey(GrBinHashKeyBuilder& key) const;
56
57 /**
58 * This is the heavy initilization routine for building a GLProgram.
59 * The result of heavy init is not stored in datamembers of GrGLProgam,
60 * but in a separate cacheable container.
61 */
bsalomon@google.com91961302011-05-09 18:39:58 +000062 bool genProgram(CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000063
bsalomon@google.com271cffc2011-05-20 14:13:56 +000064 /**
65 * The shader may modify the blend coeffecients. Params are in/out
66 */
67 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
68
69 /**
70 * Attribute indices
71 */
bsalomon@google.com91961302011-05-09 18:39:58 +000072 static int PositionAttributeIdx() { return 0; }
73 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
74 static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; }
tomhudson@google.com0d831722011-06-02 15:37:14 +000075 static int ViewMatrixAttributeIdx() {
76 return 2 + GrDrawTarget::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000077 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000078 static int TextureMatrixAttributeIdx(int stage) {
79 return 5 + GrDrawTarget::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000080 }
81
junov@google.comf93e7172011-03-31 21:26:24 +000082private:
83
tomhudson@google.com0d831722011-06-02 15:37:14 +000084 // Parameters that affect code generation
85 // These structs should be kept compact; they are the input to an
86 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000087 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000088 ProgramDesc() {
89 // since we use this as part of a key we can't have any unitialized
90 // padding
91 memset(this, 0, sizeof(ProgramDesc));
92 }
93
tomhudson@google.com0d831722011-06-02 15:37:14 +000094 struct StageDesc {
95 enum OptFlagBits {
96 kNoPerspective_OptFlagBit = 1 << 0,
97 kIdentityMatrix_OptFlagBit = 1 << 1,
98 kCustomTextureDomain_OptFlagBit = 1 << 2,
99 kIsEnabled_OptFlagBit = 1 << 7
100 };
101 enum Modulation {
102 kColor_Modulation,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000103 kAlpha_Modulation,
104
105 kModulationCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000106 };
107 enum FetchMode {
108 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000109 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000110 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000111
112 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000113 };
114 enum CoordMapping {
115 kIdentity_CoordMapping,
116 kRadialGradient_CoordMapping,
117 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000118 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000119 // need different shader computation when quadratic
120 // eq describing the gradient degenerates to a linear eq.
121 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000122 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000123 };
junov@google.comf93e7172011-03-31 21:26:24 +0000124
tomhudson@google.com0d831722011-06-02 15:37:14 +0000125 uint8_t fOptFlags;
126 uint8_t fModulation; // casts to enum Modulation
127 uint8_t fFetchMode; // casts to enum FetchMode
128 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000129 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000130
131 inline bool isEnabled() const {
132 return fOptFlags & kIsEnabled_OptFlagBit;
133 }
134 inline void setEnabled(bool newValue) {
135 if (newValue) {
136 fOptFlags |= kIsEnabled_OptFlagBit;
137 } else {
138 fOptFlags &= ~kIsEnabled_OptFlagBit;
139 }
140 }
141 };
142
143 enum ColorType {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000144 kNone_ColorType = 0,
145 kAttribute_ColorType = 1,
146 kUniform_ColorType = 2,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000147 };
148 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149 // used as a per-pixel blend coeffecient. This controls whether a
150 // secondary source is output and what value it holds.
151 enum DualSrcOutput {
152 kNone_DualSrcOutput,
153 kCoverage_DualSrcOutput,
154 kCoverageISA_DualSrcOutput,
155 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000156
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000157 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000158 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000159
tomhudson@google.com0d831722011-06-02 15:37:14 +0000160 // stripped of bits that don't affect prog generation
161 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000162
tomhudson@google.com0d831722011-06-02 15:37:14 +0000163 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000164
tomhudson@google.com0d831722011-06-02 15:37:14 +0000165 uint8_t fColorType; // casts to enum ColorType
166 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
167 int8_t fFirstCoverageStage;
168 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000169 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000170
tomhudson@google.com0d831722011-06-02 15:37:14 +0000171 int8_t fEdgeAANumEdges;
172 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000173
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000174 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000175
junov@google.comf93e7172011-03-31 21:26:24 +0000176 } fProgramDesc;
177
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000178 const ProgramDesc& getDesc() { return fProgramDesc; }
179
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000180 // for code readability
181 typedef ProgramDesc::StageDesc StageDesc;
182
junov@google.comf93e7172011-03-31 21:26:24 +0000183public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000184 enum {
185 kUnusedUniform = -1,
186 kSetAsAttribute = 1000,
187 };
188
junov@google.comf93e7172011-03-31 21:26:24 +0000189 struct StageUniLocations {
190 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000191 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000192 GrGLint fSamplerUni;
193 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000194 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000195 GrGLint fKernelUni;
196 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000197 void reset() {
198 fTextureMatrixUni = kUnusedUniform;
199 fNormalizedTexelSizeUni = kUnusedUniform;
200 fSamplerUni = kUnusedUniform;
201 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000202 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000203 fKernelUni = kUnusedUniform;
204 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000205 }
junov@google.comf93e7172011-03-31 21:26:24 +0000206 };
207
208 struct UniLocations {
209 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000210 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000211 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000212 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000213 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000214 void reset() {
215 fViewMatrixUni = kUnusedUniform;
216 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000217 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000218 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000219 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
220 fStages[s].reset();
221 }
222 }
junov@google.comf93e7172011-03-31 21:26:24 +0000223 };
224
225 class CachedData : public ::GrNoncopyable {
226 public:
227 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000228 }
229
230 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000231 }
232
233 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000234 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000235 }
236
junov@google.comf93e7172011-03-31 21:26:24 +0000237 public:
238
239 // IDs
240 GrGLuint fVShaderID;
241 GrGLuint fFShaderID;
242 GrGLuint fProgramID;
243 // shader uniform locations (-1 if shader doesn't use them)
244 UniLocations fUniLocations;
245
246 GrMatrix fViewMatrix;
247
248 // these reflect the current values of uniforms
249 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000250 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000251 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000252 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000253 // width and height used for normalized texel size
254 int fTextureWidth[GrDrawTarget::kNumStages];
255 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000256 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
257 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
258 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000259 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000260
261 private:
262 enum Constants {
263 kUniLocationPreAllocSize = 8
264 };
265
junov@google.comf93e7172011-03-31 21:26:24 +0000266 }; // CachedData
267
junov@google.comf93e7172011-03-31 21:26:24 +0000268private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000269 enum {
270 kUseUniform = 2000
271 };
272
273 // should set all fields in locations var to kUseUniform if the
274 // corresponding uniform is required for the program.
junov@google.comf93e7172011-03-31 21:26:24 +0000275 void genStageCode(int stageNum,
276 const ProgramDesc::StageDesc& desc,
277 const char* fsInColor, // NULL means no incoming color
278 const char* fsOutColor,
279 const char* vsInCoord,
280 ShaderCodeSegments* segments,
281 StageUniLocations* locations) const;
282
bsalomon@google.com91961302011-05-09 18:39:58 +0000283 static bool CompileFSAndVS(const ShaderCodeSegments& segments,
284 CachedData* programData);
285
junov@google.comf93e7172011-03-31 21:26:24 +0000286 // Compiles a GL shader, returns shader ID or 0 if failed
287 // params have same meaning as glShaderSource
288 static GrGLuint CompileShader(GrGLenum type, int stringCnt,
289 const char** strings,
290 int* stringLengths);
291
bsalomon@google.com91961302011-05-09 18:39:58 +0000292 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
293 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000294 bool bindOutputsAttribsAndLinkProgram(
295 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
296 bool bindColorOut,
297 bool bindDualSrcOut,
298 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000299
300 // Gets locations for all uniforms set to kUseUniform and initializes cache
301 // to invalid values.
302 void getUniformLocationsAndInitCache(CachedData* programData) const;
303
junov@google.comf93e7172011-03-31 21:26:24 +0000304 friend class GrGpuGLShaders;
305};
306
307#endif