blob: 90e515f425a0d2ca68324750a7b314e849b988b3 [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.com1fac5ca2011-09-20 17:57:44 +000021struct ShaderCodeSegments {
22 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
23 GrStringBuilder fVSUnis;
24 GrStringBuilder fVSAttrs;
25 GrStringBuilder fVaryings;
26 GrStringBuilder fFSUnis;
27 GrStringBuilder fFSOutputs;
28 GrStringBuilder fFSFunctions;
29 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.com0b77d682011-08-19 13:28:54 +000054 bool genProgram(const GrGLInterface* gl,
55 CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000056
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057 /**
58 * The shader may modify the blend coeffecients. Params are in/out
59 */
60 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
61
62 /**
63 * Attribute indices
64 */
bsalomon@google.com91961302011-05-09 18:39:58 +000065 static int PositionAttributeIdx() { return 0; }
66 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
67 static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000068 static int EdgeAttributeIdx() { return 2 + GrDrawTarget::kMaxTexCoords; }
69
tomhudson@google.com0d831722011-06-02 15:37:14 +000070 static int ViewMatrixAttributeIdx() {
71 return 2 + GrDrawTarget::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000072 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000073 static int TextureMatrixAttributeIdx(int stage) {
74 return 5 + GrDrawTarget::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000075 }
76
junov@google.comf93e7172011-03-31 21:26:24 +000077private:
78
tomhudson@google.com0d831722011-06-02 15:37:14 +000079 // Parameters that affect code generation
80 // These structs should be kept compact; they are the input to an
81 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000082 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000083 ProgramDesc() {
84 // since we use this as part of a key we can't have any unitialized
85 // padding
86 memset(this, 0, sizeof(ProgramDesc));
87 }
88
tomhudson@google.com0d831722011-06-02 15:37:14 +000089 struct StageDesc {
90 enum OptFlagBits {
91 kNoPerspective_OptFlagBit = 1 << 0,
92 kIdentityMatrix_OptFlagBit = 1 << 1,
93 kCustomTextureDomain_OptFlagBit = 1 << 2,
94 kIsEnabled_OptFlagBit = 1 << 7
95 };
96 enum Modulation {
97 kColor_Modulation,
bsalomon@google.com1e257a52011-07-06 19:52:16 +000098 kAlpha_Modulation,
99
100 kModulationCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000101 };
102 enum FetchMode {
103 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000104 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000105 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000106
107 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000108 };
109 enum CoordMapping {
110 kIdentity_CoordMapping,
111 kRadialGradient_CoordMapping,
112 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000113 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000114 // need different shader computation when quadratic
115 // eq describing the gradient degenerates to a linear eq.
116 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000117 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000118 };
junov@google.comf93e7172011-03-31 21:26:24 +0000119
tomhudson@google.com0d831722011-06-02 15:37:14 +0000120 uint8_t fOptFlags;
121 uint8_t fModulation; // casts to enum Modulation
122 uint8_t fFetchMode; // casts to enum FetchMode
123 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000124 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000125
126 inline bool isEnabled() const {
127 return fOptFlags & kIsEnabled_OptFlagBit;
128 }
129 inline void setEnabled(bool newValue) {
130 if (newValue) {
131 fOptFlags |= kIsEnabled_OptFlagBit;
132 } else {
133 fOptFlags &= ~kIsEnabled_OptFlagBit;
134 }
135 }
136 };
137
138 enum ColorType {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000139 kNone_ColorType = 0,
140 kAttribute_ColorType = 1,
141 kUniform_ColorType = 2,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000142 };
143 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000144 // used as a per-pixel blend coeffecient. This controls whether a
145 // secondary source is output and what value it holds.
146 enum DualSrcOutput {
147 kNone_DualSrcOutput,
148 kCoverage_DualSrcOutput,
149 kCoverageISA_DualSrcOutput,
150 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000151
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000152 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000153 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000154
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000155 GrDrawTarget::VertexEdgeType fVertexEdgeType;
156
tomhudson@google.com0d831722011-06-02 15:37:14 +0000157 // stripped of bits that don't affect prog generation
158 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000159
tomhudson@google.com0d831722011-06-02 15:37:14 +0000160 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000161
tomhudson@google.com0d831722011-06-02 15:37:14 +0000162 uint8_t fColorType; // casts to enum ColorType
163 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
164 int8_t fFirstCoverageStage;
165 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000166 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000167
tomhudson@google.com0d831722011-06-02 15:37:14 +0000168 int8_t fEdgeAANumEdges;
169 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000170
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000171 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000172
junov@google.comf93e7172011-03-31 21:26:24 +0000173 } fProgramDesc;
174
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000175 const ProgramDesc& getDesc() { return fProgramDesc; }
176
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000177 // for code readability
178 typedef ProgramDesc::StageDesc StageDesc;
179
junov@google.comf93e7172011-03-31 21:26:24 +0000180public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000181 enum {
182 kUnusedUniform = -1,
183 kSetAsAttribute = 1000,
184 };
185
junov@google.comf93e7172011-03-31 21:26:24 +0000186 struct StageUniLocations {
187 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000188 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000189 GrGLint fSamplerUni;
190 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000191 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000192 GrGLint fKernelUni;
193 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000194 void reset() {
195 fTextureMatrixUni = kUnusedUniform;
196 fNormalizedTexelSizeUni = kUnusedUniform;
197 fSamplerUni = kUnusedUniform;
198 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000199 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000200 fKernelUni = kUnusedUniform;
201 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000202 }
junov@google.comf93e7172011-03-31 21:26:24 +0000203 };
204
205 struct UniLocations {
206 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000207 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000208 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000209 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000210 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000211 void reset() {
212 fViewMatrixUni = kUnusedUniform;
213 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000214 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000215 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000216 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
217 fStages[s].reset();
218 }
219 }
junov@google.comf93e7172011-03-31 21:26:24 +0000220 };
221
222 class CachedData : public ::GrNoncopyable {
223 public:
224 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000225 }
226
227 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000228 }
229
230 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000231 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000232 }
233
junov@google.comf93e7172011-03-31 21:26:24 +0000234 public:
235
236 // IDs
237 GrGLuint fVShaderID;
238 GrGLuint fFShaderID;
239 GrGLuint fProgramID;
240 // shader uniform locations (-1 if shader doesn't use them)
241 UniLocations fUniLocations;
242
243 GrMatrix fViewMatrix;
244
245 // these reflect the current values of uniforms
246 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000247 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000248 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000249 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000250 // width and height used for normalized texel size
251 int fTextureWidth[GrDrawTarget::kNumStages];
252 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000253 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
254 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
255 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000256 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000257
258 private:
259 enum Constants {
260 kUniLocationPreAllocSize = 8
261 };
262
junov@google.comf93e7172011-03-31 21:26:24 +0000263 }; // CachedData
264
junov@google.comf7c00f62011-08-18 18:15:16 +0000265 enum Constants {
266 kProgramKeySize = sizeof(ProgramDesc)
267 };
268
269 // Provide an opaque ProgramDesc
270 const uint32_t* keyData() const{
271 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
272 }
273
junov@google.comf93e7172011-03-31 21:26:24 +0000274private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000275 enum {
276 kUseUniform = 2000
277 };
278
279 // should set all fields in locations var to kUseUniform if the
280 // corresponding uniform is required for the program.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000281 void genStageCode(const GrGLInterface* gl,
282 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000283 const ProgramDesc::StageDesc& desc,
284 const char* fsInColor, // NULL means no incoming color
285 const char* fsOutColor,
286 const char* vsInCoord,
287 ShaderCodeSegments* segments,
288 StageUniLocations* locations) const;
bsalomon@google.com1fac5ca2011-09-20 17:57:44 +0000289
bsalomon@google.com66105672011-09-15 15:12:00 +0000290 // generates code to compute coverage based on edge AA.
291 void genEdgeCoverage(const GrGLInterface* gl,
292 GrVertexLayout layout,
293 CachedData* programData,
294 GrStringBuilder* coverageVar,
295 ShaderCodeSegments* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000296
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000297 static bool CompileFSAndVS(const GrGLInterface* gl,
298 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000299 CachedData* programData);
300
junov@google.comf93e7172011-03-31 21:26:24 +0000301 // Compiles a GL shader, returns shader ID or 0 if failed
302 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000303 static GrGLuint CompileShader(const GrGLInterface* gl,
304 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000305 const char** strings,
306 int* stringLengths);
307
bsalomon@google.com91961302011-05-09 18:39:58 +0000308 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
309 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000310 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000311 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000312 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
313 bool bindColorOut,
314 bool bindDualSrcOut,
315 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000316
317 // Gets locations for all uniforms set to kUseUniform and initializes cache
318 // to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000319 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
320 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000321
junov@google.comf93e7172011-03-31 21:26:24 +0000322 friend class GrGpuGLShaders;
323};
324
325#endif