blob: fc441e5d76f9fe3e123fc5dad53ae078e4ffc9db [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
joshualitt30ba4362014-08-21 20:18:45 -070012#include "builders/GrGLProgramBuilder.h"
joshualitt47bb3822014-10-07 16:43:25 -070013#include "builders/GrGLNvprProgramBuilder.h"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000014#include "GrDrawState.h"
robertphillips@google.com6177e692013-02-28 20:16:25 +000015#include "GrGLContext.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000016#include "GrGLProgramDesc.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000017#include "GrGLSL.h"
bsalomon@google.com890e3b52012-06-01 19:01:37 +000018#include "GrGLTexture.h"
kkinnunen7510b222014-07-30 00:04:16 -070019#include "GrGLProgramDataManager.h"
junov@google.comf93e7172011-03-31 21:26:24 +000020
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000021#include "SkString.h"
Scroggo97c88c22011-05-11 14:05:25 +000022#include "SkXfermode.h"
23
joshualittb0a8a372014-09-23 09:50:21 -070024class GrGLProcessor;
joshualitt47bb3822014-10-07 16:43:25 -070025class GrGLInstalledProcessors;
joshualitt30ba4362014-08-21 20:18:45 -070026class GrGLProgramBuilder;
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
joshualitt30ba4362014-08-21 20:18:45 -070041 typedef GrGLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles;
kkinnunendddc18a2014-08-03 23:19:46 -070042
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000043 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000044
bsalomon@google.com34cccde2013-01-04 18:34:30 +000045 /**
46 * Call to abandon GL objects owned by this program.
47 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000048 void abandon();
49
bsalomon@google.com31ec7982013-03-27 18:14:57 +000050 const GrGLProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000051
bsalomon@google.com271cffc2011-05-20 14:13:56 +000052 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000053 * Gets the GL program ID for this program.
54 */
kkinnunendddc18a2014-08-03 23:19:46 -070055 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000056
joshualitt47bb3822014-10-07 16:43:25 -070057 /*
58 * The base class always has a vertex shader, only the NVPR variants may omit a vertex shader
59 */
60 virtual bool hasVertexShader() const { return true; }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000061
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000062 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000063 * 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 +000064 * and coverage attributes can be global state. This struct is read and updated by
65 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
jvanverth@google.com054ae992013-04-01 20:06:51 +000066 * redundantly.
bsalomon@google.com91207482013-02-12 21:45:24 +000067 */
68 struct SharedGLState {
69 GrColor fConstAttribColor;
jvanverth@google.com054ae992013-04-01 20:06:51 +000070 int fConstAttribColorIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000071 GrColor fConstAttribCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +000072 int fConstAttribCoverageIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000073
74 SharedGLState() { this->invalidate(); }
75 void invalidate() {
76 fConstAttribColor = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000077 fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000078 fConstAttribCoverage = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000079 fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000080 }
81 };
82
83 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000084 * The GrDrawState's view matrix along with the aspects of the render target determine the
85 * matrix sent to GL. The size of the render target affects the GL matrix because we must
86 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
87 * target may require us to perform a mirror-flip.
88 */
89 struct MatrixState {
90 SkMatrix fViewMatrix;
91 SkISize fRenderTargetSize;
92 GrSurfaceOrigin fRenderTargetOrigin;
93
94 MatrixState() { this->invalidate(); }
95 void invalidate() {
96 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +000097 fRenderTargetSize.fWidth = -1;
98 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000099 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
100 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000101
102 /**
103 * Gets a matrix that goes from local coords to Skia's device coordinates.
104 */
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000105 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000106 GrGLGetMatrix<Size>(destMatrix, fViewMatrix);
107 }
108
109 /**
110 * Gets a matrix that goes from local coordinates to GL normalized device coords.
111 */
112 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000113 SkMatrix combined;
114 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
115 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
116 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000117 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000118 } else {
119 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
120 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000121 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000122 }
commit-bot@chromium.orgb930cc32014-03-28 14:29:23 +0000123 combined.preConcat(fViewMatrix);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000124 GrGLGetMatrix<Size>(destMatrix, combined);
125 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000126
127 /**
128 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
129 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
130 * applied as such:
131 * pos.x = dot(v.xy, pos.xz)
132 * pos.y = dot(v.zq, pos.yz)
133 */
134 void getRTAdjustmentVec(GrGLfloat* destVec) {
135 destVec[0] = 2.f / fRenderTargetSize.fWidth;
136 destVec[1] = -1.f;
137 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
138 destVec[2] = -2.f / fRenderTargetSize.fHeight;
139 destVec[3] = 1.f;
140 } else {
141 destVec[2] = 2.f / fRenderTargetSize.fHeight;
142 destVec[3] = -1.f;
143 }
144 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000145 };
146
147 /**
joshualittb0a8a372014-09-23 09:50:21 -0700148 * This function uploads uniforms and calls each GrGLProcessor's setData. It is called before a
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000149 * draw occurs using the program after the program has already been bound. It also uses the
joshualittb0a8a372014-09-23 09:50:21 -0700150 * GrGpuGL object to bind the textures required by the GrGLProcessors. The color and coverage
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000151 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000152 */
egdaniel170f90b2014-09-16 12:54:40 -0700153 void setData(const GrOptDrawState&,
154 GrGpu::DrawType,
joshualitt89c7a2e2014-10-10 14:11:59 -0700155 const GrGeometryStage* geometryProcessor,
156 const GrFragmentStage* colorStages[],
157 const GrFragmentStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000158 const GrDeviceCoordTexture* dstCopy, // can be NULL
159 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000160
joshualitt47bb3822014-10-07 16:43:25 -0700161protected:
kkinnunen7510b222014-07-30 00:04:16 -0700162 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
joshualitt47bb3822014-10-07 16:43:25 -0700163 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000164
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000165 GrGLProgram(GrGpuGL*,
166 const GrGLProgramDesc&,
joshualitt47bb3822014-10-07 16:43:25 -0700167 const BuiltinUniformHandles&,
168 GrGLuint programID,
169 const UniformInfoArray&,
joshualitt89c7a2e2014-10-10 14:11:59 -0700170 GrGLInstalledProcessors* geometryProcessor,
171 GrGLInstalledProcessors* colorProcessors,
172 GrGLInstalledProcessors* coverageProcessors);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000173
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000174 // Sets the texture units for samplers.
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000175 void initSamplerUniforms();
joshualitt89c7a2e2014-10-10 14:11:59 -0700176 void initSamplers(GrGLInstalledProcessors* processors, int* texUnitIdx);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000177
178 // Helper for setData(). Makes GL calls to specify the initial color when there is not
179 // per-vertex colors.
egdaniel170f90b2014-09-16 12:54:40 -0700180 void setColor(const GrOptDrawState&, GrColor color, SharedGLState*);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000181
182 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
183 // per-vertex coverages.
egdaniel170f90b2014-09-16 12:54:40 -0700184 void setCoverage(const GrOptDrawState&, GrColor coverage, SharedGLState*);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000185
joshualitt47bb3822014-10-07 16:43:25 -0700186 // A templated helper to loop over effects, set the transforms(via subclass) and bind textures
joshualitt89c7a2e2014-10-10 14:11:59 -0700187 template <class ProcessorStage>
188 void setData(const ProcessorStage* effectStages[],
189 GrGLInstalledProcessors* installedProcessors) {
190 int numEffects = installedProcessors->fGLProcessors.count();
191 SkASSERT(numEffects == installedProcessors->fTransforms.count());
192 SkASSERT(numEffects == installedProcessors->fSamplers.count());
193 for (int e = 0; e < numEffects; ++e) {
194 const GrProcessor& effect = *effectStages[e]->getProcessor();
195 installedProcessors->fGLProcessors[e]->setData(fProgramDataManager, effect);
196 this->setTransformData(*effectStages[e], e, installedProcessors);
197 this->bindTextures(installedProcessors, effect, e);
198 }
199 }
200 virtual void setTransformData(const GrProcessorStage& effectStage,
201 int effectIdx,
202 GrGLInstalledProcessors* pe);
203 void bindTextures(const GrGLInstalledProcessors*, const GrProcessor&, int effectIdx);
joshualitt47bb3822014-10-07 16:43:25 -0700204
205 /*
206 * Legacy NVPR needs a hook here to flush path tex gen settings.
207 * TODO when legacy nvpr is removed, remove this call.
208 */
209 virtual void didSetData(GrGpu::DrawType);
210
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000211 // Helper for setData() that sets the view matrix and loads the render target height uniform
joshualitt47bb3822014-10-07 16:43:25 -0700212 void setMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDrawState&);
213 virtual void onSetMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDrawState&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000214
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000215 // these reflect the current values of uniforms (GL uniform values travel with program)
joshualitt47bb3822014-10-07 16:43:25 -0700216 MatrixState fMatrixState;
217 GrColor fColor;
218 GrColor fCoverage;
219 int fDstCopyTexUnit;
220 BuiltinUniformHandles fBuiltinUniformHandles;
221 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000222
joshualitt47bb3822014-10-07 16:43:25 -0700223 // the installed effects
joshualitt89c7a2e2014-10-10 14:11:59 -0700224 SkAutoTUnref<GrGLInstalledProcessors> fGeometryProcessor;
225 SkAutoTUnref<GrGLInstalledProcessors> fColorEffects;
226 SkAutoTUnref<GrGLInstalledProcessors> fCoverageEffects;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000227
joshualitt47bb3822014-10-07 16:43:25 -0700228 GrGLProgramDesc fDesc;
229 GrGpuGL* fGpu;
230 GrGLProgramDataManager fProgramDataManager;
junov@google.comf93e7172011-03-31 21:26:24 +0000231
joshualitt47bb3822014-10-07 16:43:25 -0700232 friend class GrGLProgramBuilder;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000233
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000234 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000235};
236
joshualitt47bb3822014-10-07 16:43:25 -0700237/*
238 * Below are slight specializations of the program object for the different types of programs
239 * The default GrGL programs consist of at the very least a vertex and fragment shader.
240 * Legacy Nvpr only has a fragment shader, 1.3+ Nvpr ignores the vertex shader, but both require
241 * specialized methods for setting transform data. Both types of NVPR also require setting the
242 * projection matrix through a special function call
243 */
244class GrGLNvprProgramBase : public GrGLProgram {
245protected:
246 GrGLNvprProgramBase(GrGpuGL*,
247 const GrGLProgramDesc&,
248 const BuiltinUniformHandles&,
249 GrGLuint programID,
250 const UniformInfoArray&,
joshualitt89c7a2e2014-10-10 14:11:59 -0700251 GrGLInstalledProcessors* colorProcessors,
252 GrGLInstalledProcessors* coverageProcessors);
joshualitt47bb3822014-10-07 16:43:25 -0700253 virtual void onSetMatrixAndRenderTargetHeight(GrGpu::DrawType, const GrOptDrawState&);
254
255 typedef GrGLProgram INHERITED;
256};
257
258class GrGLNvprProgram : public GrGLNvprProgramBase {
259public:
260 virtual bool hasVertexShader() const SK_OVERRIDE { return true; }
261
262private:
263 typedef GrGLNvprProgramBuilder::SeparableVaryingInfo SeparableVaryingInfo;
264 typedef GrGLNvprProgramBuilder::SeparableVaryingInfoArray SeparableVaryingInfoArray;
265 GrGLNvprProgram(GrGpuGL*,
266 const GrGLProgramDesc&,
267 const BuiltinUniformHandles&,
268 GrGLuint programID,
269 const UniformInfoArray&,
joshualitt89c7a2e2014-10-10 14:11:59 -0700270 GrGLInstalledProcessors* colorProcessors,
271 GrGLInstalledProcessors* coverageProcessors,
joshualitt47bb3822014-10-07 16:43:25 -0700272 const SeparableVaryingInfoArray& separableVaryings);
273 virtual void didSetData(GrGpu::DrawType) SK_OVERRIDE;
joshualitt89c7a2e2014-10-10 14:11:59 -0700274 virtual void setTransformData(const GrProcessorStage&,
275 int effectIdx,
276 GrGLInstalledProcessors*) SK_OVERRIDE;
joshualitt47bb3822014-10-07 16:43:25 -0700277
278 struct Varying {
279 GrGLint fLocation;
280 SkDEBUGCODE(
281 GrSLType fType;
282 );
283 };
284 SkTArray<Varying, true> fVaryings;
285
286 friend class GrGLNvprProgramBuilder;
287
288 typedef GrGLNvprProgramBase INHERITED;
289};
290
291class GrGLLegacyNvprProgram : public GrGLNvprProgramBase {
292public:
293 virtual bool hasVertexShader() const SK_OVERRIDE { return false; }
294
295private:
296 GrGLLegacyNvprProgram(GrGpuGL* gpu,
297 const GrGLProgramDesc& desc,
298 const BuiltinUniformHandles&,
299 GrGLuint programID,
300 const UniformInfoArray&,
joshualitt89c7a2e2014-10-10 14:11:59 -0700301 GrGLInstalledProcessors* colorProcessors,
302 GrGLInstalledProcessors* coverageProcessors,
joshualitt47bb3822014-10-07 16:43:25 -0700303 int texCoordSetCnt);
304 virtual void didSetData(GrGpu::DrawType) SK_OVERRIDE;
joshualitt89c7a2e2014-10-10 14:11:59 -0700305 virtual void setTransformData(const GrProcessorStage&,
306 int effectIdx,
307 GrGLInstalledProcessors*) SK_OVERRIDE;
joshualitt47bb3822014-10-07 16:43:25 -0700308
309 int fTexCoordSetCnt;
310
311 friend class GrGLLegacyNvprProgramBuilder;
312
313 typedef GrGLNvprProgramBase INHERITED;
314};
315
junov@google.comf93e7172011-03-31 21:26:24 +0000316#endif