blob: ce7e6b0987fa985e30386d73b95370c93b6685bc [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
joshualittb0a8a372014-09-23 09:50:21 -070023class GrGLProcessor;
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,
joshualittb0a8a372014-09-23 09:50:21 -070044 const GrGeometryStage* geometryProcessor,
45 const GrFragmentStage* colorStages[],
46 const GrFragmentStage* 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
bsalomon@google.com31ec7982013-03-27 18:14:57 +000055 const GrGLProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000056
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000058 * Gets the GL program ID for this program.
59 */
kkinnunendddc18a2014-08-03 23:19:46 -070060 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000061
kkinnunendddc18a2014-08-03 23:19:46 -070062 bool hasVertexShader() const { return fHasVertexShader; }
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000063
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000064 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +000065 * 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 +000066 * and coverage attributes can be global state. This struct is read and updated by
67 * GrGLProgram::setColor and GrGLProgram::setCoverage to allow us to avoid setting this state
jvanverth@google.com054ae992013-04-01 20:06:51 +000068 * redundantly.
bsalomon@google.com91207482013-02-12 21:45:24 +000069 */
70 struct SharedGLState {
71 GrColor fConstAttribColor;
jvanverth@google.com054ae992013-04-01 20:06:51 +000072 int fConstAttribColorIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000073 GrColor fConstAttribCoverage;
jvanverth@google.com054ae992013-04-01 20:06:51 +000074 int fConstAttribCoverageIndex;
bsalomon@google.com91207482013-02-12 21:45:24 +000075
76 SharedGLState() { this->invalidate(); }
77 void invalidate() {
78 fConstAttribColor = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000079 fConstAttribColorIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000080 fConstAttribCoverage = GrColor_ILLEGAL;
jvanverth@google.com054ae992013-04-01 20:06:51 +000081 fConstAttribCoverageIndex = -1;
bsalomon@google.com91207482013-02-12 21:45:24 +000082 }
83 };
84
85 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000086 * The GrDrawState's view matrix along with the aspects of the render target determine the
87 * matrix sent to GL. The size of the render target affects the GL matrix because we must
88 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
89 * target may require us to perform a mirror-flip.
90 */
91 struct MatrixState {
92 SkMatrix fViewMatrix;
93 SkISize fRenderTargetSize;
94 GrSurfaceOrigin fRenderTargetOrigin;
95
96 MatrixState() { this->invalidate(); }
97 void invalidate() {
98 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +000099 fRenderTargetSize.fWidth = -1;
100 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000101 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
102 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000103
104 /**
105 * Gets a matrix that goes from local coords to Skia's device coordinates.
106 */
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000107 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000108 GrGLGetMatrix<Size>(destMatrix, fViewMatrix);
109 }
110
111 /**
112 * Gets a matrix that goes from local coordinates to GL normalized device coords.
113 */
114 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000115 SkMatrix combined;
116 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
117 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
118 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000119 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000120 } else {
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 }
commit-bot@chromium.orgb930cc32014-03-28 14:29:23 +0000125 combined.preConcat(fViewMatrix);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000126 GrGLGetMatrix<Size>(destMatrix, combined);
127 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000128
129 /**
130 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
131 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
132 * applied as such:
133 * pos.x = dot(v.xy, pos.xz)
134 * pos.y = dot(v.zq, pos.yz)
135 */
136 void getRTAdjustmentVec(GrGLfloat* destVec) {
137 destVec[0] = 2.f / fRenderTargetSize.fWidth;
138 destVec[1] = -1.f;
139 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
140 destVec[2] = -2.f / fRenderTargetSize.fHeight;
141 destVec[3] = 1.f;
142 } else {
143 destVec[2] = 2.f / fRenderTargetSize.fHeight;
144 destVec[3] = -1.f;
145 }
146 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000147 };
148
149 /**
joshualittb0a8a372014-09-23 09:50:21 -0700150 * This function uploads uniforms and calls each GrGLProcessor's setData. It is called before a
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000151 * draw occurs using the program after the program has already been bound. It also uses the
joshualittb0a8a372014-09-23 09:50:21 -0700152 * GrGpuGL object to bind the textures required by the GrGLProcessors. The color and coverage
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000153 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000154 */
egdaniel170f90b2014-09-16 12:54:40 -0700155 void setData(const GrOptDrawState&,
156 GrGpu::DrawType,
joshualittb0a8a372014-09-23 09:50:21 -0700157 const GrGeometryStage* geometryProcessor,
158 const GrFragmentStage* colorStages[],
159 const GrFragmentStage* coverageStages[],
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000160 const GrDeviceCoordTexture* dstCopy, // can be NULL
161 SharedGLState*);
bsalomon@google.com91961302011-05-09 18:39:58 +0000162
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000163private:
kkinnunen7510b222014-07-30 00:04:16 -0700164 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000165
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000166 GrGLProgram(GrGpuGL*,
167 const GrGLProgramDesc&,
joshualitt30ba4362014-08-21 20:18:45 -0700168 const GrGLProgramBuilder&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000169
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000170 // Sets the texture units for samplers.
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000171 void initSamplerUniforms();
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000172
173 // Helper for setData(). Makes GL calls to specify the initial color when there is not
174 // per-vertex colors.
egdaniel170f90b2014-09-16 12:54:40 -0700175 void setColor(const GrOptDrawState&, GrColor color, SharedGLState*);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000176
177 // Helper for setData(). Makes GL calls to specify the initial coverage when there is not
178 // per-vertex coverages.
egdaniel170f90b2014-09-16 12:54:40 -0700179 void setCoverage(const GrOptDrawState&, GrColor coverage, SharedGLState*);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000180
181 // Helper for setData() that sets the view matrix and loads the render target height uniform
egdaniel170f90b2014-09-16 12:54:40 -0700182 void setMatrixAndRenderTargetHeight(GrGpu::DrawType drawType, const GrOptDrawState&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000183
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000184 // these reflect the current values of uniforms (GL uniform values travel with program)
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000185 MatrixState fMatrixState;
186 GrColor fColor;
187 GrColor fCoverage;
188 int fDstCopyTexUnit;
junov@google.comf93e7172011-03-31 21:26:24 +0000189
kkinnunendddc18a2014-08-03 23:19:46 -0700190 BuiltinUniformHandles fBuiltinUniformHandles;
joshualittbd769d02014-09-04 08:56:46 -0700191 SkAutoTUnref<GrGLProgramEffects> fGeometryProcessor;
kkinnunendddc18a2014-08-03 23:19:46 -0700192 SkAutoTUnref<GrGLProgramEffects> fColorEffects;
193 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects;
194 GrGLuint fProgramID;
195 bool fHasVertexShader;
196 int fTexCoordSetCnt;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000197
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000198 GrGLProgramDesc fDesc;
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000199 GrGpuGL* fGpu;
junov@google.comf93e7172011-03-31 21:26:24 +0000200
kkinnunendddc18a2014-08-03 23:19:46 -0700201 GrGLProgramDataManager fProgramDataManager;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000202
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