blob: 4a16b0cf4cec044539fb0bd5f12940fc7d1df8ed [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
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000040 static GrGLProgram* Create(GrGpuGL* gpu,
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 }
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000103 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
104 SkMatrix combined;
105 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
106 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
107 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
108 0, 0, SkMatrix::I()[8]);
109 } else {
110 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
111 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
112 0, 0, SkMatrix::I()[8]);
113 }
114 combined.setConcat(combined, fViewMatrix);
115 GrGLGetMatrix<Size>(destMatrix, combined);
116 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000117 };
118
119 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000120 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
121 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000122 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
123 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000124 */
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000125 void setData(GrDrawState::BlendOptFlags,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000126 const GrEffectStage* colorStages[],
127 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000128 const GrDeviceCoordTexture* dstCopy, // can be NULL
129 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000130
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000131private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000132 typedef GrGLUniformManager::UniformHandle UniformHandle;
133
bsalomon@google.com804e9942013-06-06 18:04:38 +0000134 // handles for uniforms (aside from per-effect samplers)
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000135 struct UniformHandles {
136 UniformHandle fViewMatrixUni;
137 UniformHandle fColorUni;
138 UniformHandle fCoverageUni;
139 UniformHandle fColorFilterUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000140
bsalomon@google.com706f6682012-10-23 14:53:55 +0000141 // We use the render target height to provide a y-down frag coord when specifying
142 // origin_upper_left is not supported.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000143 UniformHandle fRTHeightUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000144
145 // Uniforms for computing texture coords to do the dst-copy lookup
146 UniformHandle fDstCopyTopLeftUni;
147 UniformHandle fDstCopyScaleUni;
148 UniformHandle fDstCopySamplerUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000149 };
150
bsalomon@google.com804e9942013-06-06 18:04:38 +0000151 typedef SkSTArray<4, UniformHandle, true> SamplerUniSArray;
152 typedef SkSTArray<4, int, true> TextureUnitSArray;
153
154 struct EffectAndSamplers {
155 EffectAndSamplers() : fGLEffect(NULL) {}
156 ~EffectAndSamplers() { delete fGLEffect; }
157 GrGLEffect* fGLEffect;
158 SamplerUniSArray fSamplerUnis; // sampler uni handles for effect's GrTextureAccess
159 TextureUnitSArray fTextureUnits; // texture unit used for each entry of fSamplerUnis
160 };
161
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000162 GrGLProgram(GrGpuGL* gpu,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000163 const GrGLProgramDesc& desc,
164 const GrEffectStage* colorStages[],
165 const GrEffectStage* coverageStages[]);
166
167 bool succeeded() const { return 0 != fProgramID; }
168
169 /**
170 * This is the heavy initialization routine for building a GLProgram. colorStages and
171 * coverageStages correspond to the output of GrGLProgramDesc::Build().
172 */
173 bool genProgram(const GrEffectStage* colorStages[], const GrEffectStage* coverageStages[]);
174
175 GrSLConstantVec genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
176
177 GrSLConstantVec genInputCoverage(GrGLShaderBuilder* builder, SkString* inCoverage);
178
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000179 void genGeometryShader(GrGLShaderBuilder::VertexBuilder* vertexBuilder) const;
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000180
181 // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program
182 bool bindOutputsAttribsAndLinkProgram(const GrGLShaderBuilder& builder,
183 bool bindColorOut,
184 bool bindDualSrcOut);
185
186 // Sets the texture units for samplers
187 void initSamplerUniforms();
188 void initEffectSamplerUniforms(EffectAndSamplers* effect, int* texUnitIdx);
189
190 bool compileShaders(const GrGLShaderBuilder& builder);
191
192 const char* adjustInColor(const SkString& inColor) const;
193
194 // Helper for setData().
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000195 void setEffectData(const GrEffectStage& stage, const EffectAndSamplers& effect);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000196
197 // Helper for setData(). Makes GL calls to specify the initial color when there is not
198 // per-vertex colors.
199 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
200
201 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
202 // per-vertex coverages.
203 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
204
205 // Helper for setData() that sets the view matrix and loads the render target height uniform
206 void setMatrixAndRenderTargetHeight(const GrDrawState&);
207
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000208 // GL IDs
209 GrGLuint fVShaderID;
210 GrGLuint fGShaderID;
211 GrGLuint fFShaderID;
212 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000213
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000214 // these reflect the current values of uniforms (GL uniform values travel with program)
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000215 MatrixState fMatrixState;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000216 GrColor fColor;
217 GrColor fCoverage;
218 GrColor fColorFilterColor;
bsalomon@google.com804e9942013-06-06 18:04:38 +0000219 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000220
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000221 SkTArray<EffectAndSamplers> fColorEffects;
222 SkTArray<EffectAndSamplers> fCoverageEffects;
junov@google.comf93e7172011-03-31 21:26:24 +0000223
bsalomon@google.com31ec7982013-03-27 18:14:57 +0000224 GrGLProgramDesc fDesc;
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000225 GrGpuGL* fGpu;
junov@google.comf93e7172011-03-31 21:26:24 +0000226
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000227 GrGLUniformManager fUniformManager;
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000228 UniformHandles fUniformHandles;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000229
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000230 typedef GrRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000231};
232
233#endif