blob: 8adcd6278df36f6efd3d1a2e370b9d800db98e94 [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.com96399942012-02-13 14:39:16 +000013#include "GrGLContextInfo.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000014#include "GrGLSL.h"
bsalomon@google.com890e3b52012-06-01 19:01:37 +000015#include "GrGLTexture.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000016#include "GrGLUniformManager.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000018#include "SkString.h"
Scroggo97c88c22011-05-11 14:05:25 +000019#include "SkXfermode.h"
20
junov@google.comf93e7172011-03-31 21:26:24 +000021class GrBinHashKeyBuilder;
bsalomon@google.comd698f772012-10-25 13:22:00 +000022class GrGLEffect;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000023class GrGLShaderBuilder;
junov@google.comf93e7172011-03-31 21:26:24 +000024
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000025// optionally compile the experimental GS code. Set to GR_DEBUG
26// so that debug build bots will execute the code.
27#define GR_GL_EXPERIMENTAL_GS GR_DEBUG
28
junov@google.comf93e7172011-03-31 21:26:24 +000029/**
30 * This class manages a GPU program and records per-program information.
31 * We can specify the attribute locations so that they are constant
32 * across our shaders. But the driver determines the uniform locations
33 * at link time. We don't need to remember the sampler uniform location
34 * because we will bind a texture slot to it and never change it
35 * Uniforms are program-local so we can't rely on fHWState to hold the
36 * previous uniform state after a program change.
37 */
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000038class GrGLProgram : public GrRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000039public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000040 SK_DECLARE_INST_COUNT(GrGLProgram)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000041
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000042 struct Desc;
junov@google.comf93e7172011-03-31 21:26:24 +000043
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000044 static GrGLProgram* Create(const GrGLContextInfo& gl,
45 const Desc& desc,
bsalomon@google.comf271cc72012-10-24 19:35:13 +000046 const GrEffect** effects);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000047
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000048 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000049
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000050 /** Call to abandon GL objects owned by this program */
51 void abandon();
52
junov@google.comf93e7172011-03-31 21:26:24 +000053 /**
bsalomon@google.com4285acc2012-10-22 14:11:24 +000054 * The shader may modify the blend coefficients. Params are in/out
junov@google.comf93e7172011-03-31 21:26:24 +000055 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000056 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
junov@google.comf93e7172011-03-31 21:26:24 +000057
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000058 const Desc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000059
bsalomon@google.com271cffc2011-05-20 14:13:56 +000060 /**
bsalomon@google.com28f6ab42012-10-23 14:21:11 +000061 * Attribute indices. These should not overlap.
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062 */
bsalomon@google.com91961302011-05-09 18:39:58 +000063 static int PositionAttributeIdx() { return 0; }
bsalomon@google.com28f6ab42012-10-23 14:21:11 +000064 static int ColorAttributeIdx() { return 1; }
65 static int CoverageAttributeIdx() { return 2; }
66 static int EdgeAttributeIdx() { return 3; }
67 static int TexCoordAttributeIdx(int tcIdx) { return 4 + tcIdx; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000068
bsalomon@google.com4285acc2012-10-22 14:11:24 +000069 /**
bsalomon@google.coma469c282012-10-24 18:28:34 +000070 * This function uploads uniforms and calls each GrEffect's setData. It is called before a draw
71 * occurs using the program after the program has already been bound.
bsalomon@google.com4285acc2012-10-22 14:11:24 +000072 */
bsalomon@google.com706f6682012-10-23 14:53:55 +000073 void setData(const GrDrawState& drawState);
bsalomon@google.com91961302011-05-09 18:39:58 +000074
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.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000078 struct Desc {
79 Desc() {
bsalomon@google.com4285acc2012-10-22 14:11:24 +000080 // since we use this as part of a key we can't have any uninitialized
bsalomon@google.com4be283f2011-04-19 21:15:09 +000081 // padding
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000082 memset(this, 0, sizeof(Desc));
83 }
84
85 // returns this as a uint32_t array to be used as a key in the program cache
86 const uint32_t* asKey() const {
87 return reinterpret_cast<const uint32_t*>(this);
bsalomon@google.com4be283f2011-04-19 21:15:09 +000088 }
89
tomhudson@google.com0d831722011-06-02 15:37:14 +000090 struct StageDesc {
91 enum OptFlagBits {
92 kNoPerspective_OptFlagBit = 1 << 0,
93 kIdentityMatrix_OptFlagBit = 1 << 1,
tomhudson@google.com0d831722011-06-02 15:37:14 +000094 kIsEnabled_OptFlagBit = 1 << 7
95 };
bsalomon@google.comb505a122012-05-31 18:40:36 +000096
tomhudson@google.com0d831722011-06-02 15:37:14 +000097 uint8_t fOptFlags;
tomhudson@google.com0d831722011-06-02 15:37:14 +000098
bsalomon@google.com46fba0d2012-10-25 21:42:05 +000099 /** Non-zero if this stage has an effect */
100 GrBackendEffectFactory::EffectKey fEffectKey;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000101
tomhudson@google.com0d831722011-06-02 15:37:14 +0000102 inline bool isEnabled() const {
bsalomon@google.comc2c9b972011-10-03 13:17:22 +0000103 return SkToBool(fOptFlags & kIsEnabled_OptFlagBit);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000104 }
105 inline void setEnabled(bool newValue) {
106 if (newValue) {
107 fOptFlags |= kIsEnabled_OptFlagBit;
108 } else {
109 fOptFlags &= ~kIsEnabled_OptFlagBit;
110 }
111 }
112 };
113
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000114 // Specifies where the initial color comes from before the stages are applied.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000115 enum ColorInput {
116 kSolidWhite_ColorInput,
117 kTransBlack_ColorInput,
118 kAttribute_ColorInput,
119 kUniform_ColorInput,
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000120
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000121 kColorInputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000122 };
123 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000124 // used as a per-pixel blend coefficient. This controls whether a
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000125 // secondary source is output and what value it holds.
126 enum DualSrcOutput {
127 kNone_DualSrcOutput,
128 kCoverage_DualSrcOutput,
129 kCoverageISA_DualSrcOutput,
130 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000131
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000132 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000133 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000134
tomhudson@google.com93813632011-10-27 20:21:16 +0000135 GrDrawState::VertexEdgeType fVertexEdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000136
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000137 // stripped of bits that don't affect program generation
tomhudson@google.com0d831722011-06-02 15:37:14 +0000138 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000139
tomhudson@google.com93813632011-10-27 20:21:16 +0000140 StageDesc fStages[GrDrawState::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000141
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000142 // To enable experimental geometry shader code (not for use in
143 // production)
144#if GR_GL_EXPERIMENTAL_GS
145 bool fExperimentalGS;
146#endif
147
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000148 uint8_t fColorInput; // casts to enum ColorInput
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000149 uint8_t fCoverageInput; // casts to enum CoverageInput
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000150 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
tomhudson@google.com0d831722011-06-02 15:37:14 +0000151 int8_t fFirstCoverageStage;
152 SkBool8 fEmitsPointSize;
junov@google.comf93e7172011-03-31 21:26:24 +0000153
tomhudson@google.com0d831722011-06-02 15:37:14 +0000154 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000155 };
156 GR_STATIC_ASSERT(!(sizeof(Desc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000157
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000158 // for code readability
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000159 typedef Desc::StageDesc StageDesc;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +0000160
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000161private:
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000162 struct StageUniforms;
163
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000164 GrGLProgram(const GrGLContextInfo& gl,
165 const Desc& desc,
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000166 const GrEffect** effects);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000167
168 bool succeeded() const { return 0 != fProgramID; }
169
170 /**
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000171 * This is the heavy initialization routine for building a GLProgram.
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000172 */
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000173 bool genProgram(const GrEffect** effects);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000174
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000175 void genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000176
bsalomon@google.comd698f772012-10-25 13:22:00 +0000177 static GrGLEffect* GenStageCode(const GrEffect* effect,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000178 const StageDesc& desc, // TODO: Eliminate this
179 StageUniforms* stageUniforms, // TODO: Eliminate this
180 const char* fsInColor, // NULL means no incoming color
181 const char* fsOutColor,
182 const char* vsInCoord,
183 GrGLShaderBuilder* builder);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000184
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000185 void genGeometryShader(GrGLShaderBuilder* segments) const;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000186
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000187 typedef GrGLUniformManager::UniformHandle UniformHandle;
188
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000189 void genUniformCoverage(GrGLShaderBuilder* segments, SkString* inOutCoverage);
190
bsalomon@google.comd4726202012-08-03 14:34:46 +0000191 // generates code to compute coverage based on edge AA. Returns true if edge coverage was
192 // inserted in which case coverageVar will be updated to refer to a scalar. Otherwise,
193 // coverageVar is set to an empty string.
194 bool genEdgeCoverage(SkString* coverageVar, GrGLShaderBuilder* builder) const;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000195
196 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000197 bool bindOutputsAttribsAndLinkProgram(SkString texCoordAttrNames[GrDrawState::kMaxTexCoords],
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000198 bool bindColorOut,
199 bool bindDualSrcOut);
200
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000201 // Sets the texture units for samplers
202 void initSamplerUniforms();
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000203
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000204 bool compileShaders(const GrGLShaderBuilder& builder);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000205
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000206 const char* adjustInColor(const SkString& inColor) const;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000207
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000208 struct StageUniforms {
209 UniformHandle fTextureMatrixUni;
bsalomon@google.com0982d352012-07-31 15:33:25 +0000210 SkTArray<UniformHandle, true> fSamplerUniforms;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000211 StageUniforms() {
212 fTextureMatrixUni = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com91961302011-05-09 18:39:58 +0000213 }
junov@google.comf93e7172011-03-31 21:26:24 +0000214 };
215
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000216 struct Uniforms {
217 UniformHandle fViewMatrixUni;
218 UniformHandle fColorUni;
219 UniformHandle fCoverageUni;
220 UniformHandle fColorFilterUni;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000221 // We use the render target height to provide a y-down frag coord when specifying
222 // origin_upper_left is not supported.
223 UniformHandle fRTHeight;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000224 StageUniforms fStages[GrDrawState::kNumStages];
225 Uniforms() {
226 fViewMatrixUni = GrGLUniformManager::kInvalidUniformHandle;
227 fColorUni = GrGLUniformManager::kInvalidUniformHandle;
228 fCoverageUni = GrGLUniformManager::kInvalidUniformHandle;
229 fColorFilterUni = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000230 fRTHeight = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com91961302011-05-09 18:39:58 +0000231 }
junov@google.comf93e7172011-03-31 21:26:24 +0000232 };
233
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000234 // IDs
235 GrGLuint fVShaderID;
236 GrGLuint fGShaderID;
237 GrGLuint fFShaderID;
238 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000239
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000240 // The matrix sent to GL is determined by both the client's matrix and
241 // the size of the viewport.
242 GrMatrix fViewMatrix;
243 SkISize fViewportSize;
junov@google.comf93e7172011-03-31 21:26:24 +0000244
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000245 // these reflect the current values of uniforms
246 // (GL uniform values travel with program)
247 GrColor fColor;
248 GrColor fCoverage;
249 GrColor fColorFilterColor;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000250 int fRTHeight;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000251 /// When it is sent to GL, the texture matrix will be flipped if the texture orientation
252 /// (below) requires.
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000253 GrMatrix fTextureMatrices[GrDrawState::kNumStages];
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000254 GrGLTexture::Orientation fTextureOrientation[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000255
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000256 GrGLEffect* fEffects[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000257
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000258 Desc fDesc;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000259 const GrGLContextInfo& fContextInfo;
junov@google.comf93e7172011-03-31 21:26:24 +0000260
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000261 GrGLUniformManager fUniformManager;
262 Uniforms fUniforms;
263
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000264 friend class GrGpuGL; // TODO: remove this by adding getters and moving functionality.
junov@google.comf93e7172011-03-31 21:26:24 +0000265
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000266 typedef GrRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000267};
268
269#endif