blob: a9a2da222e1e656a2d1dd19b0ced081555b2f4f3 [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;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000025class GrGLProgramEffects;
tomhudson@google.comf9ad8862012-05-11 20:38:48 +000026class GrGLShaderBuilder;
junov@google.comf93e7172011-03-31 21:26:24 +000027
28/**
29 * This class manages a GPU program and records per-program information.
30 * We can specify the attribute locations so that they are constant
31 * across our shaders. But the driver determines the uniform locations
32 * at link time. We don't need to remember the sampler uniform location
33 * because we will bind a texture slot to it and never change it
34 * Uniforms are program-local so we can't rely on fHWState to hold the
35 * previous uniform state after a program change.
36 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000037class GrGLProgram : public SkRefCnt {
junov@google.comf93e7172011-03-31 21:26:24 +000038public:
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000039 SK_DECLARE_INST_COUNT(GrGLProgram)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000040
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000041 static GrGLProgram* Create(GrGpuGL* gpu,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000042 const GrGLProgramDesc& desc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000043 const GrEffectStage* colorStages[],
44 const GrEffectStage* coverageStages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000045
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000046 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000047
bsalomon@google.com34cccde2013-01-04 18:34:30 +000048 /**
49 * Call to abandon GL objects owned by this program.
50 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000051 void abandon();
52
junov@google.comf93e7172011-03-31 21:26:24 +000053 /**
bsalomon@google.com2db3ded2013-05-22 14:34:04 +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.com31ec7982013-03-27 18:14:57 +000058 const GrGLProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000059
bsalomon@google.com271cffc2011-05-20 14:13:56 +000060 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000061 * Gets the GL program ID for this program.
62 */
63 GrGLuint programID() const { return fProgramID; }
64
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000065 bool hasVertexShader() const { return fHasVertexShader; }
66
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000067 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000068 * 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 +000069 * and coverage attributes can be global state. This struct is read and updated by
70 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
jvanverth@google.com054ae992013-04-01 20:06:51 +000071 * redundantly.
bsalomon@google.com91207482013-02-12 21:45:24 +000072 */
73 struct SharedGLState {
74 GrColor fConstAttribColor;
jvanverth@google.com054ae992013-04-01 20:06:51 +000075 int fConstAttribColorIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000076 GrColor fConstAttribCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +000077 int fConstAttribCoverageIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000078
79 SharedGLState() { this->invalidate(); }
80 void invalidate() {
81 fConstAttribColor = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000082 fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000083 fConstAttribCoverage = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000084 fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000085 }
86 };
87
88 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000089 * The GrDrawState's view matrix along with the aspects of the render target determine the
90 * matrix sent to GL. The size of the render target affects the GL matrix because we must
91 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
92 * target may require us to perform a mirror-flip.
93 */
94 struct MatrixState {
95 SkMatrix fViewMatrix;
96 SkISize fRenderTargetSize;
97 GrSurfaceOrigin fRenderTargetOrigin;
98
99 MatrixState() { this->invalidate(); }
100 void invalidate() {
101 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +0000102 fRenderTargetSize.fWidth = -1;
103 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000104 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
105 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000106
107 /**
108 * Gets a matrix that goes from local coords to Skia's device coordinates.
109 */
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000110 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000111 GrGLGetMatrix<Size>(destMatrix, fViewMatrix);
112 }
113
114 /**
115 * Gets a matrix that goes from local coordinates to GL normalized device coords.
116 */
117 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000118 SkMatrix combined;
119 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
120 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
121 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000122 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000123 } else {
124 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
125 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000126 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000127 }
commit-bot@chromium.orgb930cc32014-03-28 14:29:23 +0000128 combined.preConcat(fViewMatrix);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000129 GrGLGetMatrix<Size>(destMatrix, combined);
130 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000131
132 /**
133 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
134 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
135 * applied as such:
136 * pos.x = dot(v.xy, pos.xz)
137 * pos.y = dot(v.zq, pos.yz)
138 */
139 void getRTAdjustmentVec(GrGLfloat* destVec) {
140 destVec[0] = 2.f / fRenderTargetSize.fWidth;
141 destVec[1] = -1.f;
142 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
143 destVec[2] = -2.f / fRenderTargetSize.fHeight;
144 destVec[3] = 1.f;
145 } else {
146 destVec[2] = 2.f / fRenderTargetSize.fHeight;
147 destVec[3] = -1.f;
148 }
149 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000150 };
151
152 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000153 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
154 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000155 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
156 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000157 */
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000158 void setData(GrDrawState::BlendOptFlags,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000159 const GrEffectStage* colorStages[],
160 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000161 const GrDeviceCoordTexture* dstCopy, // can be NULL
162 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000163
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000164private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000165 typedef GrGLUniformManager::UniformHandle UniformHandle;
166
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000167 GrGLProgram(GrGpuGL* gpu,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000168 const GrGLProgramDesc& desc,
169 const GrEffectStage* colorStages[],
170 const GrEffectStage* coverageStages[]);
171
172 bool succeeded() const { return 0 != fProgramID; }
173
174 /**
175 * This is the heavy initialization routine for building a GLProgram. colorStages and
176 * coverageStages correspond to the output of GrGLProgramDesc::Build().
177 */
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000178 bool genProgram(GrGLShaderBuilder* builder,
179 const GrEffectStage* colorStages[],
180 const GrEffectStage* coverageStages[]);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000181
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000182 // Sets the texture units for samplers
183 void initSamplerUniforms();
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000184
185 // Helper for setData(). Makes GL calls to specify the initial color when there is not
186 // per-vertex colors.
187 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
188
189 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
190 // per-vertex coverages.
191 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
192
193 // Helper for setData() that sets the view matrix and loads the render target height uniform
194 void setMatrixAndRenderTargetHeight(const GrDrawState&);
195
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000196 // GL program ID
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000197 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000198
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000199 // these reflect the current values of uniforms (GL uniform values travel with program)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000200 MatrixState fMatrixState;
201 GrColor fColor;
202 GrColor fCoverage;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000203 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000204
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000205 SkAutoTDelete<GrGLProgramEffects> fColorEffects;
206 SkAutoTDelete<GrGLProgramEffects> fCoverageEffects;
junov@google.comf93e7172011-03-31 21:26:24 +0000207
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000208 GrGLProgramDesc fDesc;
209 GrGpuGL* fGpu;
junov@google.comf93e7172011-03-31 21:26:24 +0000210
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000211 GrGLUniformManager fUniformManager;
commit-bot@chromium.org03652612014-05-29 19:09:52 +0000212 GrGLShaderBuilder::UniformHandles fUniformHandles;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000213
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000214 bool fHasVertexShader;
215 int fNumTexCoordSets;
216
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000217 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000218};
219
220#endif