blob: c8bcbf479ee6acb035bc4265c84f22a7d3099613 [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
65 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000066 * 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 +000067 * and coverage attributes can be global state. This struct is read and updated by
68 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
jvanverth@google.com054ae992013-04-01 20:06:51 +000069 * redundantly.
bsalomon@google.com91207482013-02-12 21:45:24 +000070 */
71 struct SharedGLState {
72 GrColor fConstAttribColor;
jvanverth@google.com054ae992013-04-01 20:06:51 +000073 int fConstAttribColorIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000074 GrColor fConstAttribCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +000075 int fConstAttribCoverageIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000076
77 SharedGLState() { this->invalidate(); }
78 void invalidate() {
79 fConstAttribColor = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000080 fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000081 fConstAttribCoverage = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000082 fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000083 }
84 };
85
86 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000087 * The GrDrawState's view matrix along with the aspects of the render target determine the
88 * matrix sent to GL. The size of the render target affects the GL matrix because we must
89 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
90 * target may require us to perform a mirror-flip.
91 */
92 struct MatrixState {
93 SkMatrix fViewMatrix;
94 SkISize fRenderTargetSize;
95 GrSurfaceOrigin fRenderTargetOrigin;
96
97 MatrixState() { this->invalidate(); }
98 void invalidate() {
99 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +0000100 fRenderTargetSize.fWidth = -1;
101 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000102 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
103 }
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000104 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
105 SkMatrix combined;
106 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
107 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
108 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
109 0, 0, SkMatrix::I()[8]);
110 } else {
111 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
112 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
113 0, 0, SkMatrix::I()[8]);
114 }
115 combined.setConcat(combined, fViewMatrix);
116 GrGLGetMatrix<Size>(destMatrix, combined);
117 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000118 };
119
120 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000121 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
122 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000123 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
124 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000125 */
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000126 void setData(GrDrawState::BlendOptFlags,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000127 const GrEffectStage* colorStages[],
128 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000129 const GrDeviceCoordTexture* dstCopy, // can be NULL
130 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000131
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000132private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000133 typedef GrGLUniformManager::UniformHandle UniformHandle;
134
bsalomon@google.com804e9942013-06-06 18:04:38 +0000135 // handles for uniforms (aside from per-effect samplers)
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000136 struct UniformHandles {
137 UniformHandle fViewMatrixUni;
138 UniformHandle fColorUni;
139 UniformHandle fCoverageUni;
140 UniformHandle fColorFilterUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000141
bsalomon@google.com706f6682012-10-23 14:53:55 +0000142 // We use the render target height to provide a y-down frag coord when specifying
143 // origin_upper_left is not supported.
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000144 UniformHandle fRTHeightUni;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000145
146 // Uniforms for computing texture coords to do the dst-copy lookup
147 UniformHandle fDstCopyTopLeftUni;
148 UniformHandle fDstCopyScaleUni;
149 UniformHandle fDstCopySamplerUni;
junov@google.comf93e7172011-03-31 21:26:24 +0000150 };
151
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000152 GrGLProgram(GrGpuGL* gpu,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000153 const GrGLProgramDesc& desc,
154 const GrEffectStage* colorStages[],
155 const GrEffectStage* coverageStages[]);
156
157 bool succeeded() const { return 0 != fProgramID; }
158
159 /**
160 * This is the heavy initialization routine for building a GLProgram. colorStages and
161 * coverageStages correspond to the output of GrGLProgramDesc::Build().
162 */
163 bool genProgram(const GrEffectStage* colorStages[], const GrEffectStage* coverageStages[]);
164
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000165 // Sets the texture units for samplers
166 void initSamplerUniforms();
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000167
168 // Helper for setData(). Makes GL calls to specify the initial color when there is not
169 // per-vertex colors.
170 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
171
172 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
173 // per-vertex coverages.
174 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
175
176 // Helper for setData() that sets the view matrix and loads the render target height uniform
177 void setMatrixAndRenderTargetHeight(const GrDrawState&);
178
commit-bot@chromium.org410552a2013-09-30 15:30:27 +0000179 // GL program ID
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000180 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000181
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000182 // these reflect the current values of uniforms (GL uniform values travel with program)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000183 MatrixState fMatrixState;
184 GrColor fColor;
185 GrColor fCoverage;
186 GrColor fColorFilterColor;
187 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000188
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000189 SkAutoTDelete<GrGLProgramEffects> fColorEffects;
190 SkAutoTDelete<GrGLProgramEffects> fCoverageEffects;
junov@google.comf93e7172011-03-31 21:26:24 +0000191
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000192 GrGLProgramDesc fDesc;
193 GrGpuGL* fGpu;
junov@google.comf93e7172011-03-31 21:26:24 +0000194
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000195 GrGLUniformManager fUniformManager;
196 UniformHandles fUniformHandles;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000197
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000198 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000199};
200
201#endif