blob: d385e6070e49ab80a215a163ffdc01c335b15b27 [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;
junov@google.comf93e7172011-03-31 21:26:24 +000025
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000026// optionally compile the experimental GS code. Set to GR_DEBUG
27// so that debug build bots will execute the code.
28#define GR_GL_EXPERIMENTAL_GS GR_DEBUG
29
junov@google.comf93e7172011-03-31 21:26:24 +000030/**
31 * This class manages a GPU program and records per-program information.
32 * We can specify the attribute locations so that they are constant
33 * across our shaders. But the driver determines the uniform locations
34 * at link time. We don't need to remember the sampler uniform location
35 * because we will bind a texture slot to it and never change it
36 * Uniforms are program-local so we can't rely on fHWState to hold the
37 * previous uniform state after a program change.
38 */
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000039class GrGLProgram : public GrRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000040public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000041 SK_DECLARE_INST_COUNT(GrGLProgram)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000042
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000043 struct Desc;
junov@google.comf93e7172011-03-31 21:26:24 +000044
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000045 static GrGLProgram* Create(const GrGLContextInfo& gl,
46 const Desc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000047 const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000048
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000049 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000050
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000051 /** Call to abandon GL objects owned by this program */
52 void abandon();
53
junov@google.comf93e7172011-03-31 21:26:24 +000054 /**
bsalomon@google.com4285acc2012-10-22 14:11:24 +000055 * The shader may modify the blend coefficients. Params are in/out
junov@google.comf93e7172011-03-31 21:26:24 +000056 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000057 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
junov@google.comf93e7172011-03-31 21:26:24 +000058
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000059 const Desc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000060
bsalomon@google.com271cffc2011-05-20 14:13:56 +000061 /**
bsalomon@google.com28f6ab42012-10-23 14:21:11 +000062 * Attribute indices. These should not overlap.
bsalomon@google.com271cffc2011-05-20 14:13:56 +000063 */
bsalomon@google.com91961302011-05-09 18:39:58 +000064 static int PositionAttributeIdx() { return 0; }
bsalomon@google.com28f6ab42012-10-23 14:21:11 +000065 static int ColorAttributeIdx() { return 1; }
66 static int CoverageAttributeIdx() { return 2; }
67 static int EdgeAttributeIdx() { return 3; }
68 static int TexCoordAttributeIdx(int tcIdx) { return 4 + tcIdx; }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000069
bsalomon@google.com4285acc2012-10-22 14:11:24 +000070 /**
bsalomon@google.coma469c282012-10-24 18:28:34 +000071 * This function uploads uniforms and calls each GrEffect's setData. It is called before a draw
72 * occurs using the program after the program has already been bound.
bsalomon@google.com4285acc2012-10-22 14:11:24 +000073 */
bsalomon@google.com706f6682012-10-23 14:53:55 +000074 void setData(const GrDrawState& drawState);
bsalomon@google.com91961302011-05-09 18:39:58 +000075
tomhudson@google.com0d831722011-06-02 15:37:14 +000076 // Parameters that affect code generation
bsalomon@google.comdbe49f72012-11-05 16:36:02 +000077 // This structs should be kept compact; it is input to an 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
bsalomon@google.com4285acc2012-10-22 14:11:24 +000090 // Specifies where the initial color comes from before the stages are applied.
bsalomon@google.com85b505b2011-11-07 14:56:51 +000091 enum ColorInput {
92 kSolidWhite_ColorInput,
93 kTransBlack_ColorInput,
94 kAttribute_ColorInput,
95 kUniform_ColorInput,
bsalomon@google.com86c1f712011-10-12 14:54:26 +000096
bsalomon@google.com85b505b2011-11-07 14:56:51 +000097 kColorInputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +000098 };
99 // Dual-src blending makes use of a secondary output color that can be
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000100 // used as a per-pixel blend coefficient. This controls whether a
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000101 // secondary source is output and what value it holds.
102 enum DualSrcOutput {
103 kNone_DualSrcOutput,
104 kCoverage_DualSrcOutput,
105 kCoverageISA_DualSrcOutput,
106 kCoverageISC_DualSrcOutput,
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000107
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000108 kDualSrcOutputCnt
tomhudson@google.com0d831722011-06-02 15:37:14 +0000109 };
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110
tomhudson@google.com93813632011-10-27 20:21:16 +0000111 GrDrawState::VertexEdgeType fVertexEdgeType;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000112
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000113 // stripped of bits that don't affect program generation
tomhudson@google.com0d831722011-06-02 15:37:14 +0000114 GrVertexLayout fVertexLayout;
junov@google.comf93e7172011-03-31 21:26:24 +0000115
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000116 /** Non-zero if this stage has an effect */
117 GrGLEffect::EffectKey fEffectKeys[GrDrawState::kNumStages];
Scroggo97c88c22011-05-11 14:05:25 +0000118
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000119 // To enable experimental geometry shader code (not for use in
120 // production)
121#if GR_GL_EXPERIMENTAL_GS
122 bool fExperimentalGS;
123#endif
124
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000125 uint8_t fColorInput; // casts to enum ColorInput
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000126 uint8_t fCoverageInput; // casts to enum CoverageInput
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000127 uint8_t fDualSrcOutput; // casts to enum DualSrcOutput
tomhudson@google.com0d831722011-06-02 15:37:14 +0000128 int8_t fFirstCoverageStage;
129 SkBool8 fEmitsPointSize;
junov@google.comf93e7172011-03-31 21:26:24 +0000130
tomhudson@google.com0d831722011-06-02 15:37:14 +0000131 uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000132 };
133 GR_STATIC_ASSERT(!(sizeof(Desc) % 4));
junov@google.comf93e7172011-03-31 21:26:24 +0000134
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000135private:
bsalomon@google.comf06df1b2012-09-06 20:22:31 +0000136 struct StageUniforms;
137
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000138 GrGLProgram(const GrGLContextInfo& gl,
139 const Desc& desc,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000140 const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000141
142 bool succeeded() const { return 0 != fProgramID; }
143
144 /**
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000145 * This is the heavy initialization routine for building a GLProgram.
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000146 */
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000147 bool genProgram(const GrEffectStage* stages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000148
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000149 void genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000150
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000151 static GrGLEffect* GenStageCode(const GrEffectStage& stage,
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000152 GrGLEffect::EffectKey key,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +0000153 StageUniforms* stageUniforms, // TODO: Eliminate this
154 const char* fsInColor, // NULL means no incoming color
155 const char* fsOutColor,
156 const char* vsInCoord,
157 GrGLShaderBuilder* builder);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000158
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000159 void genGeometryShader(GrGLShaderBuilder* segments) const;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000160
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000161 typedef GrGLUniformManager::UniformHandle UniformHandle;
162
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000163 void genUniformCoverage(GrGLShaderBuilder* segments, SkString* inOutCoverage);
164
bsalomon@google.comd4726202012-08-03 14:34:46 +0000165 // generates code to compute coverage based on edge AA. Returns true if edge coverage was
166 // inserted in which case coverageVar will be updated to refer to a scalar. Otherwise,
167 // coverageVar is set to an empty string.
168 bool genEdgeCoverage(SkString* coverageVar, GrGLShaderBuilder* builder) const;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000169
170 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
bsalomon@google.com17504f52012-10-30 12:34:25 +0000171 bool bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder,
172 SkString texCoordAttrNames[GrDrawState::kMaxTexCoords],
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000173 bool bindColorOut,
174 bool bindDualSrcOut);
175
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000176 // Sets the texture units for samplers
177 void initSamplerUniforms();
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000178
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000179 bool compileShaders(const GrGLShaderBuilder& builder);
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000180
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000181 const char* adjustInColor(const SkString& inColor) const;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000182
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000183 struct StageUniforms {
bsalomon@google.com0982d352012-07-31 15:33:25 +0000184 SkTArray<UniformHandle, true> fSamplerUniforms;
junov@google.comf93e7172011-03-31 21:26:24 +0000185 };
186
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000187 struct Uniforms {
188 UniformHandle fViewMatrixUni;
189 UniformHandle fColorUni;
190 UniformHandle fCoverageUni;
191 UniformHandle fColorFilterUni;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000192 // We use the render target height to provide a y-down frag coord when specifying
193 // origin_upper_left is not supported.
194 UniformHandle fRTHeight;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000195 StageUniforms fStages[GrDrawState::kNumStages];
196 Uniforms() {
197 fViewMatrixUni = GrGLUniformManager::kInvalidUniformHandle;
198 fColorUni = GrGLUniformManager::kInvalidUniformHandle;
199 fCoverageUni = GrGLUniformManager::kInvalidUniformHandle;
200 fColorFilterUni = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000201 fRTHeight = GrGLUniformManager::kInvalidUniformHandle;
bsalomon@google.com91961302011-05-09 18:39:58 +0000202 }
junov@google.comf93e7172011-03-31 21:26:24 +0000203 };
204
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000205 // IDs
206 GrGLuint fVShaderID;
207 GrGLuint fGShaderID;
208 GrGLuint fFShaderID;
209 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000210
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000211 // The matrix sent to GL is determined by both the client's matrix and
212 // the size of the viewport.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000213 SkMatrix fViewMatrix;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000214 SkISize fViewportSize;
junov@google.comf93e7172011-03-31 21:26:24 +0000215
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000216 // these reflect the current values of uniforms
217 // (GL uniform values travel with program)
218 GrColor fColor;
219 GrColor fCoverage;
220 GrColor fColorFilterColor;
bsalomon@google.com706f6682012-10-23 14:53:55 +0000221 int fRTHeight;
junov@google.comf93e7172011-03-31 21:26:24 +0000222
bsalomon@google.com46fba0d2012-10-25 21:42:05 +0000223 GrGLEffect* fEffects[GrDrawState::kNumStages];
junov@google.comf93e7172011-03-31 21:26:24 +0000224
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000225 Desc fDesc;
bsalomon@google.comecb60aa2012-07-18 13:20:29 +0000226 const GrGLContextInfo& fContextInfo;
junov@google.comf93e7172011-03-31 21:26:24 +0000227
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000228 GrGLUniformManager fUniformManager;
229 Uniforms fUniforms;
230
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000231 friend class GrGpuGL; // TODO: remove this by adding getters and moving functionality.
junov@google.comf93e7172011-03-31 21:26:24 +0000232
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000233 typedef GrRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000234};
235
236#endif