blob: 6b07f307d638c1b61ab8e39f66db34d58db06861 [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
23/**
24 * This class manages a GPU program and records per-program information.
25 * We can specify the attribute locations so that they are constant
26 * across our shaders. But the driver determines the uniform locations
27 * at link time. We don't need to remember the sampler uniform location
28 * because we will bind a texture slot to it and never change it
29 * Uniforms are program-local so we can't rely on fHWState to hold the
30 * previous uniform state after a program change.
31 */
32class GrGLProgram {
33public:
bsalomon@google.com4fa66942011-09-20 19:06:12 +000034 enum GLSLVersion {
35 k120_GLSLVersion, // Desktop GLSL 1.2 and ES2 shading lang
36 k130_GLSLVersion // Desktop GLSL 1.3
37 };
38
junov@google.comf93e7172011-03-31 21:26:24 +000039 class CachedData;
40
41 GrGLProgram();
42 ~GrGLProgram();
43
44 /**
junov@google.comf93e7172011-03-31 21:26:24 +000045 * This is the heavy initilization routine for building a GLProgram.
46 * The result of heavy init is not stored in datamembers of GrGLProgam,
47 * but in a separate cacheable container.
48 */
bsalomon@google.com0b77d682011-08-19 13:28:54 +000049 bool genProgram(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +000050 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +000051 CachedData* programData) const;
junov@google.comf93e7172011-03-31 21:26:24 +000052
bsalomon@google.com271cffc2011-05-20 14:13:56 +000053 /**
54 * The shader may modify the blend coeffecients. Params are in/out
55 */
56 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
57
58 /**
59 * Attribute indices
60 */
bsalomon@google.com91961302011-05-09 18:39:58 +000061 static int PositionAttributeIdx() { return 0; }
62 static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; }
63 static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000064 static int EdgeAttributeIdx() { return 2 + GrDrawTarget::kMaxTexCoords; }
65
tomhudson@google.com0d831722011-06-02 15:37:14 +000066 static int ViewMatrixAttributeIdx() {
67 return 2 + GrDrawTarget::kMaxTexCoords;
bsalomon@google.com91961302011-05-09 18:39:58 +000068 }
tomhudson@google.com0d831722011-06-02 15:37:14 +000069 static int TextureMatrixAttributeIdx(int stage) {
70 return 5 + GrDrawTarget::kMaxTexCoords + 3 * stage;
bsalomon@google.com91961302011-05-09 18:39:58 +000071 }
72
junov@google.comf93e7172011-03-31 21:26:24 +000073private:
74
tomhudson@google.com0d831722011-06-02 15:37:14 +000075 // Parameters that affect code generation
76 // These structs should be kept compact; they are the input to an
77 // expensive hash key generator.
junov@google.comf93e7172011-03-31 21:26:24 +000078 struct ProgramDesc {
bsalomon@google.com4be283f2011-04-19 21:15:09 +000079 ProgramDesc() {
80 // since we use this as part of a key we can't have any unitialized
81 // padding
82 memset(this, 0, sizeof(ProgramDesc));
83 }
84
tomhudson@google.com0d831722011-06-02 15:37:14 +000085 struct StageDesc {
86 enum OptFlagBits {
87 kNoPerspective_OptFlagBit = 1 << 0,
88 kIdentityMatrix_OptFlagBit = 1 << 1,
89 kCustomTextureDomain_OptFlagBit = 1 << 2,
90 kIsEnabled_OptFlagBit = 1 << 7
91 };
92 enum Modulation {
93 kColor_Modulation,
bsalomon@google.com1e257a52011-07-06 19:52:16 +000094 kAlpha_Modulation,
95
96 kModulationCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +000097 };
98 enum FetchMode {
99 kSingle_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000100 k2x2_FetchMode,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000101 kConvolution_FetchMode,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000102
103 kFetchModeCnt,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000104 };
105 enum CoordMapping {
106 kIdentity_CoordMapping,
107 kRadialGradient_CoordMapping,
108 kSweepGradient_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000109 kRadial2Gradient_CoordMapping,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000110 // need different shader computation when quadratic
111 // eq describing the gradient degenerates to a linear eq.
112 kRadial2GradientDegenerate_CoordMapping,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000113 kCoordMappingCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000114 };
junov@google.comf93e7172011-03-31 21:26:24 +0000115
tomhudson@google.com0d831722011-06-02 15:37:14 +0000116 uint8_t fOptFlags;
117 uint8_t fModulation; // casts to enum Modulation
118 uint8_t fFetchMode; // casts to enum FetchMode
119 uint8_t fCoordMapping; // casts to enum CoordMapping
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000120 uint8_t fKernelWidth;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000121
122 inline bool isEnabled() const {
123 return fOptFlags & kIsEnabled_OptFlagBit;
124 }
125 inline void setEnabled(bool newValue) {
126 if (newValue) {
127 fOptFlags |= kIsEnabled_OptFlagBit;
128 } else {
129 fOptFlags &= ~kIsEnabled_OptFlagBit;
130 }
131 }
132 };
133
134 enum ColorType {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000135 kNone_ColorType = 0,
136 kAttribute_ColorType = 1,
137 kUniform_ColorType = 2,
tomhudson@google.com0d831722011-06-02 15:37:14 +0000138 };
139 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000140 // used as a per-pixel blend coeffecient. This controls whether a
141 // secondary source is output and what value it holds.
142 enum DualSrcOutput {
143 kNone_DualSrcOutput,
144 kCoverage_DualSrcOutput,
145 kCoverageISA_DualSrcOutput,
146 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000147
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000148 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000149 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000151 GrDrawTarget::VertexEdgeType fVertexEdgeType;
152
tomhudson@google.com0d831722011-06-02 15:37:14 +0000153 // stripped of bits that don't affect prog generation
154 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000155
tomhudson@google.com0d831722011-06-02 15:37:14 +0000156 StageDesc fStages[GrDrawTarget::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000157
tomhudson@google.com0d831722011-06-02 15:37:14 +0000158 uint8_t fColorType; // casts to enum ColorType
159 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
160 int8_t fFirstCoverageStage;
161 SkBool8 fEmitsPointSize;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000162 SkBool8 fEdgeAAConcave;
junov@google.comf93e7172011-03-31 21:26:24 +0000163
tomhudson@google.com0d831722011-06-02 15:37:14 +0000164 int8_t fEdgeAANumEdges;
165 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
junov@google.comf93e7172011-03-31 21:26:24 +0000166
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000167 uint8_t fPadTo32bLengthMultiple [1];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000168
junov@google.comf93e7172011-03-31 21:26:24 +0000169 } fProgramDesc;
170
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000171 const ProgramDesc& getDesc() { return fProgramDesc; }
172
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000173 // for code readability
174 typedef ProgramDesc::StageDesc StageDesc;
175
junov@google.comf93e7172011-03-31 21:26:24 +0000176public:
bsalomon@google.com91961302011-05-09 18:39:58 +0000177 enum {
178 kUnusedUniform = -1,
179 kSetAsAttribute = 1000,
180 };
181
junov@google.comf93e7172011-03-31 21:26:24 +0000182 struct StageUniLocations {
183 GrGLint fTextureMatrixUni;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000184 GrGLint fNormalizedTexelSizeUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000185 GrGLint fSamplerUni;
186 GrGLint fRadial2Uni;
junov@google.com6acc9b32011-05-16 18:32:07 +0000187 GrGLint fTexDomUni;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000188 GrGLint fKernelUni;
189 GrGLint fImageIncrementUni;
bsalomon@google.com91961302011-05-09 18:39:58 +0000190 void reset() {
191 fTextureMatrixUni = kUnusedUniform;
192 fNormalizedTexelSizeUni = kUnusedUniform;
193 fSamplerUni = kUnusedUniform;
194 fRadial2Uni = kUnusedUniform;
junov@google.com6acc9b32011-05-16 18:32:07 +0000195 fTexDomUni = kUnusedUniform;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000196 fKernelUni = kUnusedUniform;
197 fImageIncrementUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000198 }
junov@google.comf93e7172011-03-31 21:26:24 +0000199 };
200
201 struct UniLocations {
202 GrGLint fViewMatrixUni;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000203 GrGLint fColorUni;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000204 GrGLint fEdgesUni;
Scroggo97c88c22011-05-11 14:05:25 +0000205 GrGLint fColorFilterUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000206 StageUniLocations fStages[GrDrawTarget::kNumStages];
bsalomon@google.com91961302011-05-09 18:39:58 +0000207 void reset() {
208 fViewMatrixUni = kUnusedUniform;
209 fColorUni = kUnusedUniform;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000210 fEdgesUni = kUnusedUniform;
Scroggo97c88c22011-05-11 14:05:25 +0000211 fColorFilterUni = kUnusedUniform;
bsalomon@google.com91961302011-05-09 18:39:58 +0000212 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
213 fStages[s].reset();
214 }
215 }
junov@google.comf93e7172011-03-31 21:26:24 +0000216 };
217
218 class CachedData : public ::GrNoncopyable {
219 public:
220 CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000221 }
222
223 ~CachedData() {
junov@google.comf93e7172011-03-31 21:26:24 +0000224 }
225
226 void copyAndTakeOwnership(CachedData& other) {
bsalomon@google.com2d9ddf92011-05-11 16:52:59 +0000227 memcpy(this, &other, sizeof(*this));
junov@google.comf93e7172011-03-31 21:26:24 +0000228 }
229
junov@google.comf93e7172011-03-31 21:26:24 +0000230 public:
231
232 // IDs
233 GrGLuint fVShaderID;
234 GrGLuint fFShaderID;
235 GrGLuint fProgramID;
236 // shader uniform locations (-1 if shader doesn't use them)
237 UniLocations fUniLocations;
238
239 GrMatrix fViewMatrix;
240
241 // these reflect the current values of uniforms
242 // (GL uniform values travel with program)
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000243 GrColor fColor;
Scroggo97c88c22011-05-11 14:05:25 +0000244 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000245 GrMatrix fTextureMatrices[GrDrawTarget::kNumStages];
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000246 // width and height used for normalized texel size
247 int fTextureWidth[GrDrawTarget::kNumStages];
248 int fTextureHeight[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000249 GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages];
250 GrScalar fRadial2Radius0[GrDrawTarget::kNumStages];
251 bool fRadial2PosRoot[GrDrawTarget::kNumStages];
junov@google.com2f839402011-05-24 15:13:01 +0000252 GrRect fTextureDomain[GrDrawTarget::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000253
254 private:
255 enum Constants {
256 kUniLocationPreAllocSize = 8
257 };
258
junov@google.comf93e7172011-03-31 21:26:24 +0000259 }; // CachedData
260
junov@google.comf7c00f62011-08-18 18:15:16 +0000261 enum Constants {
262 kProgramKeySize = sizeof(ProgramDesc)
263 };
264
265 // Provide an opaque ProgramDesc
266 const uint32_t* keyData() const{
267 return reinterpret_cast<const uint32_t*>(&fProgramDesc);
268 }
269
junov@google.comf93e7172011-03-31 21:26:24 +0000270private:
bsalomon@google.com91961302011-05-09 18:39:58 +0000271 enum {
272 kUseUniform = 2000
273 };
274
275 // should set all fields in locations var to kUseUniform if the
276 // corresponding uniform is required for the program.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000277 void genStageCode(const GrGLInterface* gl,
278 int stageNum,
junov@google.comf93e7172011-03-31 21:26:24 +0000279 const ProgramDesc::StageDesc& desc,
280 const char* fsInColor, // NULL means no incoming color
281 const char* fsOutColor,
282 const char* vsInCoord,
283 ShaderCodeSegments* segments,
284 StageUniLocations* locations) const;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000285
bsalomon@google.com66105672011-09-15 15:12:00 +0000286 // generates code to compute coverage based on edge AA.
287 void genEdgeCoverage(const GrGLInterface* gl,
288 GrVertexLayout layout,
289 CachedData* programData,
290 GrStringBuilder* coverageVar,
291 ShaderCodeSegments* segments) const;
junov@google.comf93e7172011-03-31 21:26:24 +0000292
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000293 static bool CompileFSAndVS(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000294 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000295 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000296 CachedData* programData);
297
junov@google.comf93e7172011-03-31 21:26:24 +0000298 // Compiles a GL shader, returns shader ID or 0 if failed
299 // params have same meaning as glShaderSource
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000300 static GrGLuint CompileShader(const GrGLInterface* gl,
301 GrGLenum type, int stringCnt,
junov@google.comf93e7172011-03-31 21:26:24 +0000302 const char** strings,
303 int* stringLengths);
304
bsalomon@google.com91961302011-05-09 18:39:58 +0000305 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and
306 // links the program
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000307 bool bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000308 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000309 GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords],
310 bool bindColorOut,
311 bool bindDualSrcOut,
312 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000313
314 // Gets locations for all uniforms set to kUseUniform and initializes cache
315 // to invalid values.
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000316 void getUniformLocationsAndInitCache(const GrGLInterface* gl,
317 CachedData* programData) const;
bsalomon@google.com91961302011-05-09 18:39:58 +0000318
junov@google.comf93e7172011-03-31 21:26:24 +0000319 friend class GrGpuGLShaders;
320};
321
322#endif