blob: 5d02ba2a9cef85d359e783177fd600ab9ee91293 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
junov@google.comf93e7172011-03-31 21:26:24 +00009#ifndef GrGLProgram_DEFINED
10#define GrGLProgram_DEFINED
11
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000012#include "GrDrawState.h"
bsalomon@google.comdbe49f72012-11-05 16:36:02 +000013#include "GrGLEffect.h"
bsalomon@google.com96399942012-02-13 14:39:16 +000014#include "GrGLContextInfo.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000015#include "GrGLSL.h"
bsalomon@google.com890e3b52012-06-01 19:01:37 +000016#include "GrGLTexture.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000017#include "GrGLUniformManager.h"
junov@google.comf93e7172011-03-31 21:26:24 +000018
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000019#include "SkString.h"
Scroggo97c88c22011-05-11 14:05:25 +000020#include "SkXfermode.h"
21
junov@google.comf93e7172011-03-31 21:26:24 +000022class GrBinHashKeyBuilder;
bsalomon@google.comd698f772012-10-25 13:22:00 +000023class GrGLEffect;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000024class GrGLShaderBuilder;
bsalomon@google.com91207482013-02-12 21:45:24 +000025class SkMWCRandom;
junov@google.comf93e7172011-03-31 21:26:24 +000026
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000027// optionally compile the experimental GS code. Set to GR_DEBUG
28// so that debug build bots will execute the code.
29#define GR_GL_EXPERIMENTAL_GS GR_DEBUG
30
junov@google.comf93e7172011-03-31 21:26:24 +000031/**
32 * This class manages a GPU program and records per-program information.
33 * We can specify the attribute locations so that they are constant
34 * across our shaders. But the driver determines the uniform locations
35 * at link time. We don't need to remember the sampler uniform location
36 * because we will bind a texture slot to it and never change it
37 * Uniforms are program-local so we can't rely on fHWState to hold the
38 * previous uniform state after a program change.
39 */
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000040class GrGLProgram : public GrRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000041public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000042 SK_DECLARE_INST_COUNT(GrGLProgram)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000043
bsalomon@google.com91207482013-02-12 21:45:24 +000044 class Desc;
45
46 /**
47 * Builds a program descriptor from a GrDrawState. Whether the primitive type is points, the
48 * output of GrDrawState::getBlendOpts, and the caps of the GrGpuGL are also inputs.
49 */
50 static void BuildDesc(const GrDrawState&,
51 bool isPoints,
52 GrDrawState::BlendOptFlags,
53 GrBlendCoeff srcCoeff,
54 GrBlendCoeff dstCoeff,
55 const GrGpuGL* gpu,
56 Desc* outDesc);
junov@google.comf93e7172011-03-31 21:26:24 +000057
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000058 static GrGLProgram* Create(const GrGLContextInfo& gl,
59 const Desc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000060 const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000061
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000062 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000063
bsalomon@google.com34cccde2013-01-04 18:34:30 +000064 /**
65 * Call to abandon GL objects owned by this program.
66 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000067 void abandon();
68
junov@google.comf93e7172011-03-31 21:26:24 +000069 /**
bsalomon@google.com4285acc2012-10-22 14:11:24 +000070 * The shader may modify the blend coefficients. Params are in/out
junov@google.comf93e7172011-03-31 21:26:24 +000071 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000072 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
junov@google.comf93e7172011-03-31 21:26:24 +000073
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000074 const Desc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000075
bsalomon@google.com271cffc2011-05-20 14:13:56 +000076 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000077 * Gets the GL program ID for this program.
78 */
79 GrGLuint programID() const { return fProgramID; }
80
81 /**
bsalomon@google.com28f6ab42012-10-23 14:21:11 +000082 * Attribute indices. These should not overlap.
bsalomon@google.com271cffc2011-05-20 14:13:56 +000083 */
bsalomon@google.com91961302011-05-09 18:39:58 +000084 static int PositionAttributeIdx() { return 0; }
bsalomon@google.com28f6ab42012-10-23 14:21:11 +000085 static int ColorAttributeIdx() { return 1; }
86 static int CoverageAttributeIdx() { return 2; }
87 static int EdgeAttributeIdx() { return 3; }
88 static int TexCoordAttributeIdx(int tcIdx) { return 4 + tcIdx; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000089
bsalomon@google.com4285acc2012-10-22 14:11:24 +000090 /**
bsalomon@google.com91207482013-02-12 21:45:24 +000091 * Some GL state that is relevant to programs is not stored per-program. In particular vertex
92 * attributes are global state. This struct is read and updated by GrGLProgram::setData to
93 * allow us to avoid setting this state redundantly.
94 */
95 struct SharedGLState {
96 GrColor fConstAttribColor;
97 GrColor fConstAttribCoverage;
98
99 SharedGLState() { this->invalidate(); }
100 void invalidate() {
101 fConstAttribColor = GrColor_ILLEGAL;
102 fConstAttribCoverage = GrColor_ILLEGAL;
103 }
104 };
105
106 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000107 * The GrDrawState's view matrix along with the aspects of the render target determine the
108 * matrix sent to GL. The size of the render target affects the GL matrix because we must
109 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
110 * target may require us to perform a mirror-flip.
111 */
112 struct MatrixState {
113 SkMatrix fViewMatrix;
114 SkISize fRenderTargetSize;
115 GrSurfaceOrigin fRenderTargetOrigin;
116
117 MatrixState() { this->invalidate(); }
118 void invalidate() {
119 fViewMatrix = SkMatrix::InvalidMatrix();
120 fRenderTargetSize.fWidth = -1; // just make the first value compared illegal.
121 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
122 }
123 };
124
125 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000126 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
127 * draw occurs using the program after the program has already been bound. It also uses the
128 * GrGpuGL object to bind the textures required by the GrGLEffects.
bsalomon@google.com91207482013-02-12 21:45:24 +0000129 *
130 * The color and coverage params override the GrDrawState's getColor() and getCoverage() values.
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000131 */
bsalomon@google.com91207482013-02-12 21:45:24 +0000132 void setData(GrGpuGL*, GrColor color, GrColor coverage, SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000133
tomhudson@google.com0d831722011-06-02 15:37:14 +0000134 // Parameters that affect code generation
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000135 // This structs should be kept compact; it is input to an expensive hash key generator.
bsalomon@google.com91207482013-02-12 21:45:24 +0000136 class Desc {
137 public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000138 Desc() {
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000139 // since we use this as part of a key we can't have any uninitialized
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000140 // padding
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000141 memset(this, 0, sizeof(Desc));
142 }
143
144 // returns this as a uint32_t array to be used as a key in the program cache
145 const uint32_t* asKey() const {
146 return reinterpret_cast<const uint32_t*>(this);
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000147 }
148
bsalomon@google.com91207482013-02-12 21:45:24 +0000149 // For unit testing.
150 void setRandom(SkMWCRandom*,
151 const GrGpuGL* gpu,
152 const GrEffectStage stages[GrDrawState::kNumStages]);
153
154 private:
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000155 // Specifies where the initial color comes from before the stages are applied.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000156 enum ColorInput {
157 kSolidWhite_ColorInput,
158 kTransBlack_ColorInput,
159 kAttribute_ColorInput,
160 kUniform_ColorInput,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000161
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000162 kColorInputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000163 };
164 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000165 // used as a per-pixel blend coefficient. This controls whether a
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000166 // secondary source is output and what value it holds.
167 enum DualSrcOutput {
168 kNone_DualSrcOutput,
169 kCoverage_DualSrcOutput,
170 kCoverageISA_DualSrcOutput,
171 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000172
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000173 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000174 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000175
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000176 // TODO: remove these two members when edge-aa can be rewritten as a GrEffect.
tomhudson@google.com93813632011-10-27 20:21:16 +0000177 GrDrawState::VertexEdgeType fVertexEdgeType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000178 // should the FS discard if the edge-aa coverage is zero (to avoid stencil manipulation)
179 bool fDiscardIfOutsideEdge;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000180
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000181 // stripped of bits that don't affect program generation
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000182 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000183
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000184 /** Non-zero if this stage has an effect */
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000185 GrGLEffect::EffectKey fEffectKeys[GrDrawState::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000186
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000187 // To enable experimental geometry shader code (not for use in
188 // production)
189#if GR_GL_EXPERIMENTAL_GS
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000190 bool fExperimentalGS;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000191#endif
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000192 uint8_t fColorInput; // casts to enum ColorInput
193 uint8_t fCoverageInput; // casts to enum ColorInput
194 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
195 int8_t fFirstCoverageStage;
196 SkBool8 fEmitsPointSize;
197 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
bsalomon@google.com91207482013-02-12 21:45:24 +0000198
199 friend class GrGLProgram;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000200 };
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000201
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000202private:
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000203 GrGLProgram(const GrGLContextInfo& gl,
204 const Desc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000205 const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000206
207 bool succeeded() const { return 0 != fProgramID; }
208
209 /**
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000210 * This is the heavy initialization routine for building a GLProgram.
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000211 */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000212 bool genProgram(const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000213
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000214 void genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000215
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000216 void genGeometryShader(GrGLShaderBuilder* segments) const;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000217
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000218 typedef GrGLUniformManager::UniformHandle UniformHandle;
219
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000220 void genUniformCoverage(GrGLShaderBuilder* segments, SkString* inOutCoverage);
221
bsalomon@google.comd4726202012-08-03 14:34:46 +0000222 // generates code to compute coverage based on edge AA. Returns true if edge coverage was
223 // inserted in which case coverageVar will be updated to refer to a scalar. Otherwise,
224 // coverageVar is set to an empty string.
225 bool genEdgeCoverage(SkString* coverageVar, GrGLShaderBuilder* builder) const;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000226
227 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
bsalomon@google.com17504f52012-10-30 12:34:25 +0000228 bool bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder,
229 SkString texCoordAttrNames[GrDrawState::kMaxTexCoords],
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000230 bool bindColorOut,
231 bool bindDualSrcOut);
232
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000233 // Sets the texture units for samplers
234 void initSamplerUniforms();
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000235
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000236 bool compileShaders(const GrGLShaderBuilder& builder);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000237
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000238 const char* adjustInColor(const SkString& inColor) const;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000239
bsalomon@google.com91207482013-02-12 21:45:24 +0000240 // Helper for setData(). Makes GL calls to specify the initial color when there is not
241 // per-vertex colors.
242 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
243
244 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
245 // per-vertex coverages.
246 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
247
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000248 // Helper for setData() that sets the view matrix and loads the render target height uniform
249 void setMatrixAndRenderTargetHeight(const GrDrawState&);
250
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000251 typedef SkSTArray<4, UniformHandle, true> SamplerUniSArray;
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000252
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000253 struct UniformHandles {
254 UniformHandle fViewMatrixUni;
255 UniformHandle fColorUni;
256 UniformHandle fCoverageUni;
257 UniformHandle fColorFilterUni;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000258 // We use the render target height to provide a y-down frag coord when specifying
259 // origin_upper_left is not supported.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000260 UniformHandle fRTHeightUni;
261 // An array of sampler uniform handles for each effect.
262 SamplerUniSArray fSamplerUnis[GrDrawState::kNumStages];
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +0000263
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000264 UniformHandles() {
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000265 fViewMatrixUni = GrGLUniformManager::kInvalidUniformHandle;
266 fColorUni = GrGLUniformManager::kInvalidUniformHandle;
267 fCoverageUni = GrGLUniformManager::kInvalidUniformHandle;
268 fColorFilterUni = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000269 fRTHeightUni = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com91961302011-05-09 18:39:58 +0000270 }
junov@google.comf93e7172011-03-31 21:26:24 +0000271 };
272
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000273 // GL IDs
274 GrGLuint fVShaderID;
275 GrGLuint fGShaderID;
276 GrGLuint fFShaderID;
277 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000278
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000279 // these reflect the current values of uniforms (GL uniform values travel with program)
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000280 MatrixState fMatrixState;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000281 GrColor fColor;
282 GrColor fCoverage;
283 GrColor fColorFilterColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000284
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000285 GrGLEffect* fEffects[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000286
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000287 Desc fDesc;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000288 const GrGLContextInfo& fContextInfo;
junov@google.comf93e7172011-03-31 21:26:24 +0000289
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000290 GrGLUniformManager fUniformManager;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000291 UniformHandles fUniformHandles;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000292
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000293 typedef GrRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000294};
295
296#endif