blob: 0f89e07b0d734fbc452dac1ac7cefc54e3364168 [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,
joshualittbd769d02014-09-04 08:56:46 -070044 const GrEffectStage* geometryProcessor,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000045 const GrEffectStage* colorStages[],
46 const GrEffectStage* coverageStages[]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000047
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000048 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000049
bsalomon@google.com34cccde2013-01-04 18:34:30 +000050 /**
51 * Call to abandon GL objects owned by this program.
52 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000053 void abandon();
54
junov@google.comf93e7172011-03-31 21:26:24 +000055 /**
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000056 * The shader may modify the blend coefficients. Params are in/out.
junov@google.comf93e7172011-03-31 21:26:24 +000057 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000058 void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const;
junov@google.comf93e7172011-03-31 21:26:24 +000059
bsalomon@google.com31ec7982013-03-27 18:14:57 +000060 const GrGLProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000061
bsalomon@google.com271cffc2011-05-20 14:13:56 +000062 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000063 * Gets the GL program ID for this program.
64 */
kkinnunendddc18a2014-08-03 23:19:46 -070065 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000066
kkinnunendddc18a2014-08-03 23:19:46 -070067 bool hasVertexShader() const { return fHasVertexShader; }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000068
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000069 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000070 * 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 +000071 * and coverage attributes can be global state. This struct is read and updated by
72 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
jvanverth@google.com054ae992013-04-01 20:06:51 +000073 * redundantly.
bsalomon@google.com91207482013-02-12 21:45:24 +000074 */
75 struct SharedGLState {
76 GrColor fConstAttribColor;
jvanverth@google.com054ae992013-04-01 20:06:51 +000077 int fConstAttribColorIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000078 GrColor fConstAttribCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +000079 int fConstAttribCoverageIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000080
81 SharedGLState() { this->invalidate(); }
82 void invalidate() {
83 fConstAttribColor = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000084 fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000085 fConstAttribCoverage = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000086 fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000087 }
88 };
89
90 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000091 * The GrDrawState's view matrix along with the aspects of the render target determine the
92 * matrix sent to GL. The size of the render target affects the GL matrix because we must
93 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
94 * target may require us to perform a mirror-flip.
95 */
96 struct MatrixState {
97 SkMatrix fViewMatrix;
98 SkISize fRenderTargetSize;
99 GrSurfaceOrigin fRenderTargetOrigin;
100
101 MatrixState() { this->invalidate(); }
102 void invalidate() {
103 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +0000104 fRenderTargetSize.fWidth = -1;
105 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000106 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
107 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000108
109 /**
110 * Gets a matrix that goes from local coords to Skia's device coordinates.
111 */
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000112 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000113 GrGLGetMatrix<Size>(destMatrix, fViewMatrix);
114 }
115
116 /**
117 * Gets a matrix that goes from local coordinates to GL normalized device coords.
118 */
119 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000120 SkMatrix combined;
121 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
122 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
123 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000124 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000125 } else {
126 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
127 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000128 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000129 }
commit-bot@chromium.orgb930cc32014-03-28 14:29:23 +0000130 combined.preConcat(fViewMatrix);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000131 GrGLGetMatrix<Size>(destMatrix, combined);
132 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000133
134 /**
135 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
136 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
137 * applied as such:
138 * pos.x = dot(v.xy, pos.xz)
139 * pos.y = dot(v.zq, pos.yz)
140 */
141 void getRTAdjustmentVec(GrGLfloat* destVec) {
142 destVec[0] = 2.f / fRenderTargetSize.fWidth;
143 destVec[1] = -1.f;
144 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
145 destVec[2] = -2.f / fRenderTargetSize.fHeight;
146 destVec[3] = 1.f;
147 } else {
148 destVec[2] = 2.f / fRenderTargetSize.fHeight;
149 destVec[3] = -1.f;
150 }
151 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000152 };
153
154 /**
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000155 * This function uploads uniforms and calls each GrGLEffect's setData. It is called before a
156 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000157 * GrGpuGL object to bind the textures required by the GrGLEffects. The color and coverage
158 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000159 */
kkinnunenec56e452014-08-25 22:21:16 -0700160 void setData(GrGpu::DrawType,
161 GrDrawState::BlendOptFlags,
joshualittbd769d02014-09-04 08:56:46 -0700162 const GrEffectStage* geometryProcessor,
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000163 const GrEffectStage* colorStages[],
164 const GrEffectStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000165 const GrDeviceCoordTexture* dstCopy, // can be NULL
166 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000167
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000168private:
kkinnunen7510b222014-07-30 00:04:16 -0700169 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000170
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000171 GrGLProgram(GrGpuGL*,
172 const GrGLProgramDesc&,
joshualitt30ba4362014-08-21 20:18:45 -0700173 const GrGLProgramBuilder&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000174
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000175 // Sets the texture units for samplers.
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000176 void initSamplerUniforms();
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.
180 void setColor(const GrDrawState&, GrColor color, SharedGLState*);
181
182 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
183 // per-vertex coverages.
184 void setCoverage(const GrDrawState&, GrColor coverage, SharedGLState*);
185
186 // Helper for setData() that sets the view matrix and loads the render target height uniform
kkinnunenec56e452014-08-25 22:21:16 -0700187 void setMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, const GrDrawState&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000188
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000189 // these reflect the current values of uniforms (GL uniform values travel with program)
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000190 MatrixState fMatrixState;
191 GrColor fColor;
192 GrColor fCoverage;
193 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000194
kkinnunendddc18a2014-08-03 23:19:46 -0700195 BuiltinUniformHandles fBuiltinUniformHandles;
joshualittbd769d02014-09-04 08:56:46 -0700196 SkAutoTUnref<GrGLProgramEffects> fGeometryProcessor;
kkinnunendddc18a2014-08-03 23:19:46 -0700197 SkAutoTUnref<GrGLProgramEffects> fColorEffects;
198 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects;
199 GrGLuint fProgramID;
200 bool fHasVertexShader;
201 int fTexCoordSetCnt;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000202
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000203 GrGLProgramDesc fDesc;
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000204 GrGpuGL* fGpu;
junov@google.comf93e7172011-03-31 21:26:24 +0000205
kkinnunendddc18a2014-08-03 23:19:46 -0700206 GrGLProgramDataManager fProgramDataManager;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000207
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000208 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000209};
210
211#endif