blob: ffe9f00347b0c6791677b72f422f914a55e541a9 [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 /**
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.com91961302011-05-09 18:39:58 +000054 bool genProgram(CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000055
bsalomon@google.com271cffc2011-05-20 14:13:56 +000056 /**
57 * The shader may modify the blend coeffecients. Params are in/out
58 */
59 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
60
61 /**
62 * Attribute indices
63 */
bsalomon@google.com91961302011-05-09 18:39:58 +000064 static int PositionAttributeIdx() { return 0; }
65 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
66 static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; }
tomhudson@google.com0d831722011-06-02 15:37:14 +000067 static int ViewMatrixAttributeIdx() {
68 return 2 + GrDrawTarget::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000069 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000070 static int TextureMatrixAttributeIdx(int stage) {
71 return 5 + GrDrawTarget::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000072 }
73
junov@google.comf93e7172011-03-31 21:26:24 +000074private:
75
tomhudson@google.com0d831722011-06-02 15:37:14 +000076 // Parameters that affect code generation
77 // These structs should be kept compact; they are the input to an
78 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000079 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000080 ProgramDesc() {
81 // since we use this as part of a key we can't have any unitialized
82 // padding
83 memset(this, 0, sizeof(ProgramDesc));
84 }
85
tomhudson@google.com0d831722011-06-02 15:37:14 +000086 struct StageDesc {
87 enum OptFlagBits {
88 kNoPerspective_OptFlagBit = 1 << 0,
89 kIdentityMatrix_OptFlagBit = 1 << 1,
90 kCustomTextureDomain_OptFlagBit = 1 << 2,
91 kIsEnabled_OptFlagBit = 1 << 7
92 };
93 enum Modulation {
94 kColor_Modulation,
bsalomon@google.com1e257a52011-07-06 19:52:16 +000095 kAlpha_Modulation,
96
97 kModulationCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +000098 };
99 enum FetchMode {
100 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000101 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000102 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000103
104 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000105 };
106 enum CoordMapping {
107 kIdentity_CoordMapping,
108 kRadialGradient_CoordMapping,
109 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000110 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000111 // need different shader computation when quadratic
112 // eq describing the gradient degenerates to a linear eq.
113 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000114 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000115 };
junov@google.comf93e7172011-03-31 21:26:24 +0000116
tomhudson@google.com0d831722011-06-02 15:37:14 +0000117 uint8_t fOptFlags;
118 uint8_t fModulation; // casts to enum Modulation
119 uint8_t fFetchMode; // casts to enum FetchMode
120 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000121 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000122
123 inline bool isEnabled() const {
124 return fOptFlags & kIsEnabled_OptFlagBit;
125 }
126 inline void setEnabled(bool newValue) {
127 if (newValue) {
128 fOptFlags |= kIsEnabled_OptFlagBit;
129 } else {
130 fOptFlags &= ~kIsEnabled_OptFlagBit;
131 }
132 }
133 };
134
135 enum ColorType {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000136 kNone_ColorType = 0,
137 kAttribute_ColorType = 1,
138 kUniform_ColorType = 2,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000139 };
140 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000141 // used as a per-pixel blend coeffecient. This controls whether a
142 // secondary source is output and what value it holds.
143 enum DualSrcOutput {
144 kNone_DualSrcOutput,
145 kCoverage_DualSrcOutput,
146 kCoverageISA_DualSrcOutput,
147 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000148
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000150 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000151
tomhudson@google.com0d831722011-06-02 15:37:14 +0000152 // stripped of bits that don't affect prog generation
153 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000154
tomhudson@google.com0d831722011-06-02 15:37:14 +0000155 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000156
tomhudson@google.com0d831722011-06-02 15:37:14 +0000157 uint8_t fColorType; // casts to enum ColorType
158 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
159 int8_t fFirstCoverageStage;
160 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000161 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000162
tomhudson@google.com0d831722011-06-02 15:37:14 +0000163 int8_t fEdgeAANumEdges;
164 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000165
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000166 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000167
junov@google.comf93e7172011-03-31 21:26:24 +0000168 } fProgramDesc;
169
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000170 const ProgramDesc& getDesc() { return fProgramDesc; }
171
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000172 // for code readability
173 typedef ProgramDesc::StageDesc StageDesc;
174
junov@google.comf93e7172011-03-31 21:26:24 +0000175public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000176 enum {
177 kUnusedUniform = -1,
178 kSetAsAttribute = 1000,
179 };
180
junov@google.comf93e7172011-03-31 21:26:24 +0000181 struct StageUniLocations {
182 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000183 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000184 GrGLint fSamplerUni;
185 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000186 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000187 GrGLint fKernelUni;
188 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000189 void reset() {
190 fTextureMatrixUni = kUnusedUniform;
191 fNormalizedTexelSizeUni = kUnusedUniform;
192 fSamplerUni = kUnusedUniform;
193 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000194 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000195 fKernelUni = kUnusedUniform;
196 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000197 }
junov@google.comf93e7172011-03-31 21:26:24 +0000198 };
199
200 struct UniLocations {
201 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000202 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000203 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000204 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000205 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000206 void reset() {
207 fViewMatrixUni = kUnusedUniform;
208 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000209 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000210 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000211 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
212 fStages[s].reset();
213 }
214 }
junov@google.comf93e7172011-03-31 21:26:24 +0000215 };
216
217 class CachedData : public ::GrNoncopyable {
218 public:
219 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000220 }
221
222 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000223 }
224
225 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000226 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000227 }
228
junov@google.comf93e7172011-03-31 21:26:24 +0000229 public:
230
231 // IDs
232 GrGLuint fVShaderID;
233 GrGLuint fFShaderID;
234 GrGLuint fProgramID;
235 // shader uniform locations (-1 if shader doesn't use them)
236 UniLocations fUniLocations;
237
238 GrMatrix fViewMatrix;
239
240 // these reflect the current values of uniforms
241 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000242 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000243 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000244 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000245 // width and height used for normalized texel size
246 int fTextureWidth[GrDrawTarget::kNumStages];
247 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000248 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
249 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
250 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000251 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000252
253 private:
254 enum Constants {
255 kUniLocationPreAllocSize = 8
256 };
257
junov@google.comf93e7172011-03-31 21:26:24 +0000258 }; // CachedData
259
junov@google.comf7c00f62011-08-18 18:15:16 +0000260 enum Constants {
261 kProgramKeySize = sizeof(ProgramDesc)
262 };
263
264 // Provide an opaque ProgramDesc
265 const uint32_t* keyData() const{
266 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
267 }
268
junov@google.comf93e7172011-03-31 21:26:24 +0000269private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000270 enum {
271 kUseUniform = 2000
272 };
273
274 // should set all fields in locations var to kUseUniform if the
275 // corresponding uniform is required for the program.
junov@google.comf93e7172011-03-31 21:26:24 +0000276 void genStageCode(int stageNum,
277 const ProgramDesc::StageDesc& desc,
278 const char* fsInColor, // NULL means no incoming color
279 const char* fsOutColor,
280 const char* vsInCoord,
281 ShaderCodeSegments* segments,
282 StageUniLocations* locations) const;
283
bsalomon@google.com91961302011-05-09 18:39:58 +0000284 static bool CompileFSAndVS(const ShaderCodeSegments& segments,
285 CachedData* programData);
286
junov@google.comf93e7172011-03-31 21:26:24 +0000287 // Compiles a GL shader, returns shader ID or 0 if failed
288 // params have same meaning as glShaderSource
289 static GrGLuint CompileShader(GrGLenum type, int stringCnt,
290 const char** strings,
291 int* stringLengths);
292
bsalomon@google.com91961302011-05-09 18:39:58 +0000293 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
294 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000295 bool bindOutputsAttribsAndLinkProgram(
296 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
297 bool bindColorOut,
298 bool bindDualSrcOut,
299 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000300
301 // Gets locations for all uniforms set to kUseUniform and initializes cache
302 // to invalid values.
303 void getUniformLocationsAndInitCache(CachedData* programData) const;
304
junov@google.comf93e7172011-03-31 21:26:24 +0000305 friend class GrGpuGLShaders;
306};
307
308#endif