blob: 279890f5d4a8858b7b103c95b77f04410a98bac5 [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"
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
26/**
27 * This class manages a GPU program and records per-program information.
28 * We can specify the attribute locations so that they are constant
29 * across our shaders. But the driver determines the uniform locations
30 * at link time. We don't need to remember the sampler uniform location
31 * because we will bind a texture slot to it and never change it
32 * Uniforms are program-local so we can't rely on fHWState to hold the
33 * previous uniform state after a program change.
34 */
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000035class GrGLProgram : public GrRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000036public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000037 SK_DECLARE_INST_COUNT(GrGLProgram)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000038
robertphillips@google.com6177e692013-02-28 20:16:25 +000039 static GrGLProgram* Create(const GrGLContext& gl,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000040 const GrGLProgramDesc& desc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000041 const GrEffectStage* colorStages[],
42 const GrEffectStage* coverageStages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000043
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000044 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000045
bsalomon@google.com34cccde2013-01-04 18:34:30 +000046 /**
47 * Call to abandon GL objects owned by this program.
48 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000049 void abandon();
50
junov@google.comf93e7172011-03-31 21:26:24 +000051 /**
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000052 * The shader may modify the blend coefficients. Params are in/out.
junov@google.comf93e7172011-03-31 21:26:24 +000053 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000054 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
junov@google.comf93e7172011-03-31 21:26:24 +000055
bsalomon@google.com31ec7982013-03-27 18:14:57 +000056 const GrGLProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000057
bsalomon@google.com271cffc2011-05-20 14:13:56 +000058 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000059 * Gets the GL program ID for this program.
60 */
61 GrGLuint programID() const { return fProgramID; }
62
63 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000064 * 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 +000065 * and coverage attributes can be global state. This struct is read and updated by
66 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
jvanverth@google.com054ae992013-04-01 20:06:51 +000067 * redundantly.
bsalomon@google.com91207482013-02-12 21:45:24 +000068 */
69 struct SharedGLState {
70 GrColor fConstAttribColor;
jvanverth@google.com054ae992013-04-01 20:06:51 +000071 int fConstAttribColorIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000072 GrColor fConstAttribCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +000073 int fConstAttribCoverageIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000074
75 SharedGLState() { this->invalidate(); }
76 void invalidate() {
77 fConstAttribColor = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000078 fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000079 fConstAttribCoverage = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000080 fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000081 }
82 };
83
84 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000085 * The GrDrawState's view matrix along with the aspects of the render target determine the
86 * matrix sent to GL. The size of the render target affects the GL matrix because we must
87 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
88 * target may require us to perform a mirror-flip.
89 */
90 struct MatrixState {
91 SkMatrix fViewMatrix;
92 SkISize fRenderTargetSize;
93 GrSurfaceOrigin fRenderTargetOrigin;
94
95 MatrixState() { this->invalidate(); }
96 void invalidate() {
97 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +000098 fRenderTargetSize.fWidth = -1;
99 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000100 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
101 }
102 };
103
104 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000105 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
106 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000107 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
108 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000109 */
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000110 void setData(GrGpuGL*,
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000111 GrDrawState::BlendOptFlags,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000112 const GrEffectStage* colorStages[],
113 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000114 const GrDeviceCoordTexture* dstCopy, // can be NULL
115 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000116
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000117private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000118 typedef GrGLUniformManager::UniformHandle UniformHandle;
119
bsalomon@google.com804e9942013-06-06 18:04:38 +0000120 // handles for uniforms (aside from per-effect samplers)
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000121 struct UniformHandles {
122 UniformHandle fViewMatrixUni;
123 UniformHandle fColorUni;
124 UniformHandle fCoverageUni;
125 UniformHandle fColorFilterUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000126
bsalomon@google.com706f6682012-10-23 14:53:55 +0000127 // We use the render target height to provide a y-down frag coord when specifying
128 // origin_upper_left is not supported.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000129 UniformHandle fRTHeightUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000130
131 // Uniforms for computing texture coords to do the dst-copy lookup
132 UniformHandle fDstCopyTopLeftUni;
133 UniformHandle fDstCopyScaleUni;
134 UniformHandle fDstCopySamplerUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000135 };
136
bsalomon@google.com804e9942013-06-06 18:04:38 +0000137 typedef SkSTArray<4, UniformHandle, true> SamplerUniSArray;
138 typedef SkSTArray<4, int, true> TextureUnitSArray;
139
140 struct EffectAndSamplers {
141 EffectAndSamplers() : fGLEffect(NULL) {}
142 ~EffectAndSamplers() { delete fGLEffect; }
143 GrGLEffect* fGLEffect;
144 SamplerUniSArray fSamplerUnis; // sampler uni handles for effect's GrTextureAccess
145 TextureUnitSArray fTextureUnits; // texture unit used for each entry of fSamplerUnis
146 };
147
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000148 GrGLProgram(const GrGLContext& gl,
149 const GrGLProgramDesc& desc,
150 const GrEffectStage* colorStages[],
151 const GrEffectStage* coverageStages[]);
152
153 bool succeeded() const { return 0 != fProgramID; }
154
155 /**
156 * This is the heavy initialization routine for building a GLProgram. colorStages and
157 * coverageStages correspond to the output of GrGLProgramDesc::Build().
158 */
159 bool genProgram(const GrEffectStage* colorStages[], const GrEffectStage* coverageStages[]);
160
161 GrSLConstantVec genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
162
163 GrSLConstantVec genInputCoverage(GrGLShaderBuilder* builder, SkString* inCoverage);
164
165 void genGeometryShader(GrGLShaderBuilder* segments) const;
166
167 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
168 bool bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder,
169 bool bindColorOut,
170 bool bindDualSrcOut);
171
172 // Sets the texture units for samplers
173 void initSamplerUniforms();
174 void initEffectSamplerUniforms(EffectAndSamplers* effect, int* texUnitIdx);
175
176 bool compileShaders(const GrGLShaderBuilder& builder);
177
178 const char* adjustInColor(const SkString& inColor) const;
179
180 // Helper for setData().
181 void setEffectData(GrGpuGL* gpu, const GrEffectStage& stage, const EffectAndSamplers& effect);
182
183 // Helper for setData(). Makes GL calls to specify the initial color when there is not
184 // per-vertex colors.
185 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
186
187 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
188 // per-vertex coverages.
189 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
190
191 // Helper for setData() that sets the view matrix and loads the render target height uniform
192 void setMatrixAndRenderTargetHeight(const GrDrawState&);
193
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000194 // GL IDs
195 GrGLuint fVShaderID;
196 GrGLuint fGShaderID;
197 GrGLuint fFShaderID;
198 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000199
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000200 // these reflect the current values of uniforms (GL uniform values travel with program)
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000201 MatrixState fMatrixState;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000202 GrColor fColor;
203 GrColor fCoverage;
204 GrColor fColorFilterColor;
bsalomon@google.com804e9942013-06-06 18:04:38 +0000205 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000206
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000207 SkTArray<EffectAndSamplers> fColorEffects;
208 SkTArray<EffectAndSamplers> fCoverageEffects;
junov@google.comf93e7172011-03-31 21:26:24 +0000209
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000210 GrGLProgramDesc fDesc;
robertphillips@google.com6177e692013-02-28 20:16:25 +0000211 const GrGLContext& fContext;
junov@google.comf93e7172011-03-31 21:26:24 +0000212
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000213 GrGLUniformManager fUniformManager;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000214 UniformHandles fUniformHandles;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000215
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000216 typedef GrRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000217};
218
219#endif