blob: 5b9deb9f9178e46b85fc012c59a792b2e57b0909 [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"
tomhudson@google.comd8f856c2012-05-10 12:13:36 +000013#include "GrDrawState.h"
robertphillips@google.com6177e692013-02-28 20:16:25 +000014#include "GrGLContext.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000015#include "GrGLProgramDesc.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"
kkinnunen7510b222014-07-30 00:04:16 -070018#include "GrGLProgramDataManager.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
bsalomon@google.comd698f772012-10-25 13:22:00 +000023class GrGLEffect;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000024class GrGLProgramEffects;
joshualitt30ba4362014-08-21 20:18:45 -070025class GrGLProgramBuilder;
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 */
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000036class GrGLProgram : public SkRefCnt {
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
joshualitt30ba4362014-08-21 20:18:45 -070040 typedef GrGLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles;
kkinnunendddc18a2014-08-03 23:19:46 -070041
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000042 static GrGLProgram* Create(GrGpuGL* gpu,
bsalomon@google.com31ec7982013-03-27 18:14:57 +000043 const GrGLProgramDesc& desc,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000044 const GrEffectStage* colorStages[],
45 const GrEffectStage* coverageStages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000046
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000047 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000048
bsalomon@google.com34cccde2013-01-04 18:34:30 +000049 /**
50 * Call to abandon GL objects owned by this program.
51 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000052 void abandon();
53
junov@google.comf93e7172011-03-31 21:26:24 +000054 /**
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000055 * The shader may modify the blend coefficients. Params are in/out.
junov@google.comf93e7172011-03-31 21:26:24 +000056 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000057 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
junov@google.comf93e7172011-03-31 21:26:24 +000058
bsalomon@google.com31ec7982013-03-27 18:14:57 +000059 const GrGLProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000060
bsalomon@google.com271cffc2011-05-20 14:13:56 +000061 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000062 * Gets the GL program ID for this program.
63 */
kkinnunendddc18a2014-08-03 23:19:46 -070064 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000065
kkinnunendddc18a2014-08-03 23:19:46 -070066 bool hasVertexShader() const { return fHasVertexShader; }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000067
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000068 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000069 * 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 +000070 * and coverage attributes can be global state. This struct is read and updated by
71 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
jvanverth@google.com054ae992013-04-01 20:06:51 +000072 * redundantly.
bsalomon@google.com91207482013-02-12 21:45:24 +000073 */
74 struct SharedGLState {
75 GrColor fConstAttribColor;
jvanverth@google.com054ae992013-04-01 20:06:51 +000076 int fConstAttribColorIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000077 GrColor fConstAttribCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +000078 int fConstAttribCoverageIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000079
80 SharedGLState() { this->invalidate(); }
81 void invalidate() {
82 fConstAttribColor = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000083 fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000084 fConstAttribCoverage = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000085 fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000086 }
87 };
88
89 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000090 * The GrDrawState's view matrix along with the aspects of the render target determine the
91 * matrix sent to GL. The size of the render target affects the GL matrix because we must
92 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
93 * target may require us to perform a mirror-flip.
94 */
95 struct MatrixState {
96 SkMatrix fViewMatrix;
97 SkISize fRenderTargetSize;
98 GrSurfaceOrigin fRenderTargetOrigin;
99
100 MatrixState() { this->invalidate(); }
101 void invalidate() {
102 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +0000103 fRenderTargetSize.fWidth = -1;
104 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000105 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
106 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000107
108 /**
109 * Gets a matrix that goes from local coords to Skia's device coordinates.
110 */
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000111 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000112 GrGLGetMatrix<Size>(destMatrix, fViewMatrix);
113 }
114
115 /**
116 * Gets a matrix that goes from local coordinates to GL normalized device coords.
117 */
118 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000119 SkMatrix combined;
120 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
121 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
122 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000123 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000124 } else {
125 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
126 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000127 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000128 }
commit-bot@chromium.orgb930cc32014-03-28 14:29:23 +0000129 combined.preConcat(fViewMatrix);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000130 GrGLGetMatrix<Size>(destMatrix, combined);
131 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000132
133 /**
134 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
135 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
136 * applied as such:
137 * pos.x = dot(v.xy, pos.xz)
138 * pos.y = dot(v.zq, pos.yz)
139 */
140 void getRTAdjustmentVec(GrGLfloat* destVec) {
141 destVec[0] = 2.f / fRenderTargetSize.fWidth;
142 destVec[1] = -1.f;
143 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
144 destVec[2] = -2.f / fRenderTargetSize.fHeight;
145 destVec[3] = 1.f;
146 } else {
147 destVec[2] = 2.f / fRenderTargetSize.fHeight;
148 destVec[3] = -1.f;
149 }
150 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000151 };
152
153 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000154 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
155 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000156 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
157 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000158 */
kkinnunenec56e452014-08-25 22:21:16 -0700159 void setData(GrGpu::DrawType,
160 GrDrawState::BlendOptFlags,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000161 const GrEffectStage* colorStages[],
162 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000163 const GrDeviceCoordTexture* dstCopy, // can be NULL
164 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000165
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000166private:
kkinnunen7510b222014-07-30 00:04:16 -0700167 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000168
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000169 GrGLProgram(GrGpuGL*,
170 const GrGLProgramDesc&,
joshualitt30ba4362014-08-21 20:18:45 -0700171 const GrGLProgramBuilder&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000172
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000173 // Sets the texture units for samplers.
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000174 void initSamplerUniforms();
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000175
176 // Helper for setData(). Makes GL calls to specify the initial color when there is not
177 // per-vertex colors.
178 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
179
180 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
181 // per-vertex coverages.
182 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
183
184 // Helper for setData() that sets the view matrix and loads the render target height uniform
kkinnunenec56e452014-08-25 22:21:16 -0700185 void setMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, const GrDrawState&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000186
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000187 // these reflect the current values of uniforms (GL uniform values travel with program)
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000188 MatrixState fMatrixState;
189 GrColor fColor;
190 GrColor fCoverage;
191 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000192
kkinnunendddc18a2014-08-03 23:19:46 -0700193 BuiltinUniformHandles fBuiltinUniformHandles;
194 SkAutoTUnref<GrGLProgramEffects> fColorEffects;
195 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects;
196 GrGLuint fProgramID;
197 bool fHasVertexShader;
198 int fTexCoordSetCnt;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000199
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000200 GrGLProgramDesc fDesc;
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000201 GrGpuGL* fGpu;
junov@google.comf93e7172011-03-31 21:26:24 +0000202
kkinnunendddc18a2014-08-03 23:19:46 -0700203 GrGLProgramDataManager fProgramDataManager;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000204
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000205 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000206};
207
208#endif