blob: e39e9bf076ca60fb3197859e65eed6c0f5f8c5e9 [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"
robertphillips@google.com6177e692013-02-28 20:16:25 +000013#include "GrGLContext.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000014#include "GrGLProgramDesc.h"
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +000015#include "GrGLShaderBuilder.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000016#include "GrGLSL.h"
bsalomon@google.com890e3b52012-06-01 19:01:37 +000017#include "GrGLTexture.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000018#include "GrGLUniformManager.h"
junov@google.comf93e7172011-03-31 21:26:24 +000019
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000020#include "SkString.h"
Scroggo97c88c22011-05-11 14:05:25 +000021#include "SkXfermode.h"
22
junov@google.comf93e7172011-03-31 21:26:24 +000023class GrBinHashKeyBuilder;
bsalomon@google.comd698f772012-10-25 13:22:00 +000024class GrGLEffect;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000025class GrGLShaderBuilder;
junov@google.comf93e7172011-03-31 21:26:24 +000026
27/**
28 * This class manages a GPU program and records per-program information.
29 * We can specify the attribute locations so that they are constant
30 * across our shaders. But the driver determines the uniform locations
31 * at link time. We don't need to remember the sampler uniform location
32 * because we will bind a texture slot to it and never change it
33 * Uniforms are program-local so we can't rely on fHWState to hold the
34 * previous uniform state after a program change.
35 */
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000036class GrGLProgram : public GrRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000037public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000038 SK_DECLARE_INST_COUNT(GrGLProgram)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000039
robertphillips@google.com6177e692013-02-28 20:16:25 +000040 static GrGLProgram* Create(const GrGLContext& gl,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000041 const GrGLProgramDesc& desc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000042 const GrEffectStage* colorStages[],
43 const GrEffectStage* coverageStages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000044
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000045 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000046
bsalomon@google.com34cccde2013-01-04 18:34:30 +000047 /**
48 * Call to abandon GL objects owned by this program.
49 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000050 void abandon();
51
junov@google.comf93e7172011-03-31 21:26:24 +000052 /**
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000053 * The shader may modify the blend coefficients. Params are in/out.
junov@google.comf93e7172011-03-31 21:26:24 +000054 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000055 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
junov@google.comf93e7172011-03-31 21:26:24 +000056
bsalomon@google.com31ec7982013-03-27 18:14:57 +000057 const GrGLProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000058
bsalomon@google.com271cffc2011-05-20 14:13:56 +000059 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000060 * Gets the GL program ID for this program.
61 */
62 GrGLuint programID() const { return fProgramID; }
63
64 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000065 * Some GL state that is relevant to programs is not stored per-program. In particular color
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +000066 * and coverage attributes can be global state. This struct is read and updated by
67 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
jvanverth@google.com054ae992013-04-01 20:06:51 +000068 * redundantly.
bsalomon@google.com91207482013-02-12 21:45:24 +000069 */
70 struct SharedGLState {
71 GrColor fConstAttribColor;
jvanverth@google.com054ae992013-04-01 20:06:51 +000072 int fConstAttribColorIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000073 GrColor fConstAttribCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +000074 int fConstAttribCoverageIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000075
76 SharedGLState() { this->invalidate(); }
77 void invalidate() {
78 fConstAttribColor = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000079 fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000080 fConstAttribCoverage = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000081 fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000082 }
83 };
84
85 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000086 * The GrDrawState's view matrix along with the aspects of the render target determine the
87 * matrix sent to GL. The size of the render target affects the GL matrix because we must
88 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
89 * target may require us to perform a mirror-flip.
90 */
91 struct MatrixState {
92 SkMatrix fViewMatrix;
93 SkISize fRenderTargetSize;
94 GrSurfaceOrigin fRenderTargetOrigin;
95
96 MatrixState() { this->invalidate(); }
97 void invalidate() {
98 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +000099 fRenderTargetSize.fWidth = -1;
100 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000101 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
102 }
103 };
104
105 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000106 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
107 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000108 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
109 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000110 */
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000111 void setData(GrGpuGL*,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000112 GrDrawState::BlendOptFlags,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000113 const GrEffectStage* colorStages[],
114 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000115 const GrDeviceCoordTexture* dstCopy, // can be NULL
116 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000117
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000118private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000119 typedef GrGLUniformManager::UniformHandle UniformHandle;
120
bsalomon@google.com804e9942013-06-06 18:04:38 +0000121 // handles for uniforms (aside from per-effect samplers)
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000122 struct UniformHandles {
123 UniformHandle fViewMatrixUni;
124 UniformHandle fColorUni;
125 UniformHandle fCoverageUni;
126 UniformHandle fColorFilterUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000127
bsalomon@google.com706f6682012-10-23 14:53:55 +0000128 // We use the render target height to provide a y-down frag coord when specifying
129 // origin_upper_left is not supported.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000130 UniformHandle fRTHeightUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000131
132 // Uniforms for computing texture coords to do the dst-copy lookup
133 UniformHandle fDstCopyTopLeftUni;
134 UniformHandle fDstCopyScaleUni;
135 UniformHandle fDstCopySamplerUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000136 };
137
bsalomon@google.com804e9942013-06-06 18:04:38 +0000138 typedef SkSTArray<4, UniformHandle, true> SamplerUniSArray;
139 typedef SkSTArray<4, int, true> TextureUnitSArray;
140
141 struct EffectAndSamplers {
142 EffectAndSamplers() : fGLEffect(NULL) {}
143 ~EffectAndSamplers() { delete fGLEffect; }
144 GrGLEffect* fGLEffect;
145 SamplerUniSArray fSamplerUnis; // sampler uni handles for effect's GrTextureAccess
146 TextureUnitSArray fTextureUnits; // texture unit used for each entry of fSamplerUnis
147 };
148
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000149 GrGLProgram(const GrGLContext& gl,
150 const GrGLProgramDesc& desc,
151 const GrEffectStage* colorStages[],
152 const GrEffectStage* coverageStages[]);
153
154 bool succeeded() const { return 0 != fProgramID; }
155
156 /**
157 * This is the heavy initialization routine for building a GLProgram. colorStages and
158 * coverageStages correspond to the output of GrGLProgramDesc::Build().
159 */
160 bool genProgram(const GrEffectStage* colorStages[], const GrEffectStage* coverageStages[]);
161
162 GrSLConstantVec genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
163
164 GrSLConstantVec genInputCoverage(GrGLShaderBuilder* builder, SkString* inCoverage);
165
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000166 void genGeometryShader(GrGLShaderBuilder::VertexBuilder* vertexBuilder) const;
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000167
168 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
169 bool bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder,
170 bool bindColorOut,
171 bool bindDualSrcOut);
172
173 // Sets the texture units for samplers
174 void initSamplerUniforms();
175 void initEffectSamplerUniforms(EffectAndSamplers* effect, int* texUnitIdx);
176
177 bool compileShaders(const GrGLShaderBuilder& builder);
178
179 const char* adjustInColor(const SkString& inColor) const;
180
181 // Helper for setData().
182 void setEffectData(GrGpuGL* gpu, const GrEffectStage& stage, const EffectAndSamplers& effect);
183
184 // Helper for setData(). Makes GL calls to specify the initial color when there is not
185 // per-vertex colors.
186 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
187
188 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
189 // per-vertex coverages.
190 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
191
192 // Helper for setData() that sets the view matrix and loads the render target height uniform
193 void setMatrixAndRenderTargetHeight(const GrDrawState&);
194
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000195 // GL IDs
196 GrGLuint fVShaderID;
197 GrGLuint fGShaderID;
198 GrGLuint fFShaderID;
199 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000200
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000201 // these reflect the current values of uniforms (GL uniform values travel with program)
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000202 MatrixState fMatrixState;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000203 GrColor fColor;
204 GrColor fCoverage;
205 GrColor fColorFilterColor;
bsalomon@google.com804e9942013-06-06 18:04:38 +0000206 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000207
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000208 SkTArray<EffectAndSamplers> fColorEffects;
209 SkTArray<EffectAndSamplers> fCoverageEffects;
junov@google.comf93e7172011-03-31 21:26:24 +0000210
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000211 GrGLProgramDesc fDesc;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000212 const GrGLContext& fContext;
junov@google.comf93e7172011-03-31 21:26:24 +0000213
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000214 GrGLUniformManager fUniformManager;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000215 UniformHandles fUniformHandles;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000216
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000217 typedef GrRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000218};
219
220#endif