blob: 8e2ae74aab991696ebc56ca34d00bd0cc97fc4b5 [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.org215a6822013-09-05 18:28:42 +0000106 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
107 SkMatrix combined;
108 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
109 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
110 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
111 0, 0, SkMatrix::I()[8]);
112 } else {
113 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
114 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
115 0, 0, SkMatrix::I()[8]);
116 }
117 combined.setConcat(combined, fViewMatrix);
118 GrGLGetMatrix<Size>(destMatrix, combined);
119 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000120 };
121
122 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000123 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
124 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000125 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
126 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000127 */
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000128 void setData(GrDrawState::BlendOptFlags,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000129 const GrEffectStage* colorStages[],
130 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000131 const GrDeviceCoordTexture* dstCopy, // can be NULL
132 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000133
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000134private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000135 typedef GrGLUniformManager::UniformHandle UniformHandle;
136
bsalomon@google.com804e9942013-06-06 18:04:38 +0000137 // handles for uniforms (aside from per-effect samplers)
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000138 struct UniformHandles {
139 UniformHandle fViewMatrixUni;
140 UniformHandle fColorUni;
141 UniformHandle fCoverageUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000142
bsalomon@google.com706f6682012-10-23 14:53:55 +0000143 // We use the render target height to provide a y-down frag coord when specifying
144 // origin_upper_left is not supported.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000145 UniformHandle fRTHeightUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000146
147 // Uniforms for computing texture coords to do the dst-copy lookup
148 UniformHandle fDstCopyTopLeftUni;
149 UniformHandle fDstCopyScaleUni;
150 UniformHandle fDstCopySamplerUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000151 };
152
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000153 GrGLProgram(GrGpuGL* gpu,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000154 const GrGLProgramDesc& desc,
155 const GrEffectStage* colorStages[],
156 const GrEffectStage* coverageStages[]);
157
158 bool succeeded() const { return 0 != fProgramID; }
159
160 /**
161 * This is the heavy initialization routine for building a GLProgram. colorStages and
162 * coverageStages correspond to the output of GrGLProgramDesc::Build().
163 */
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000164 bool genProgram(GrGLShaderBuilder* builder,
165 const GrEffectStage* colorStages[],
166 const GrEffectStage* coverageStages[]);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000167
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000168 // Sets the texture units for samplers
169 void initSamplerUniforms();
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000170
171 // Helper for setData(). Makes GL calls to specify the initial color when there is not
172 // per-vertex colors.
173 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
174
175 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
176 // per-vertex coverages.
177 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
178
179 // Helper for setData() that sets the view matrix and loads the render target height uniform
180 void setMatrixAndRenderTargetHeight(const GrDrawState&);
181
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000182 // GL program ID
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000183 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000184
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000185 // these reflect the current values of uniforms (GL uniform values travel with program)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000186 MatrixState fMatrixState;
187 GrColor fColor;
188 GrColor fCoverage;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000189 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000190
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000191 SkAutoTDelete<GrGLProgramEffects> fColorEffects;
192 SkAutoTDelete<GrGLProgramEffects> fCoverageEffects;
junov@google.comf93e7172011-03-31 21:26:24 +0000193
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000194 GrGLProgramDesc fDesc;
195 GrGpuGL* fGpu;
junov@google.comf93e7172011-03-31 21:26:24 +0000196
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000197 GrGLUniformManager fUniformManager;
198 UniformHandles fUniformHandles;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000199
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000200 bool fHasVertexShader;
201 int fNumTexCoordSets;
202
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000203 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000204};
205
206#endif