blob: 3ee71b85b1383e96afed343fa383c6d013d4bdea [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
joshualitt79f8fae2014-10-28 17:59:26 -070050 const GrProgramDesc& 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 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000063 * The GrDrawState's view matrix along with the aspects of the render target determine the
64 * matrix sent to GL. The size of the render target affects the GL matrix because we must
65 * convert from Skia device coords to GL's normalized coords. Also the origin of the render
66 * target may require us to perform a mirror-flip.
67 */
68 struct MatrixState {
69 SkMatrix fViewMatrix;
70 SkISize fRenderTargetSize;
71 GrSurfaceOrigin fRenderTargetOrigin;
72
73 MatrixState() { this->invalidate(); }
74 void invalidate() {
75 fViewMatrix = SkMatrix::InvalidMatrix();
bsalomon@google.com45a412e2013-02-13 16:13:13 +000076 fRenderTargetSize.fWidth = -1;
77 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000078 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
79 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000080
81 /**
82 * Gets a matrix that goes from local coords to Skia's device coordinates.
83 */
commit-bot@chromium.org215a6822013-09-05 18:28:42 +000084 template<int Size> void getGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000085 GrGLGetMatrix<Size>(destMatrix, fViewMatrix);
86 }
87
88 /**
89 * Gets a matrix that goes from local coordinates to GL normalized device coords.
90 */
91 template<int Size> void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) {
commit-bot@chromium.org215a6822013-09-05 18:28:42 +000092 SkMatrix combined;
93 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
94 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
95 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000096 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +000097 } else {
98 combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1,
99 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1,
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000100 0, 0, 1);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000101 }
commit-bot@chromium.orgb930cc32014-03-28 14:29:23 +0000102 combined.preConcat(fViewMatrix);
commit-bot@chromium.org215a6822013-09-05 18:28:42 +0000103 GrGLGetMatrix<Size>(destMatrix, combined);
104 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +0000105
106 /**
107 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
108 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
109 * applied as such:
110 * pos.x = dot(v.xy, pos.xz)
111 * pos.y = dot(v.zq, pos.yz)
112 */
113 void getRTAdjustmentVec(GrGLfloat* destVec) {
114 destVec[0] = 2.f / fRenderTargetSize.fWidth;
115 destVec[1] = -1.f;
116 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
117 destVec[2] = -2.f / fRenderTargetSize.fHeight;
118 destVec[3] = 1.f;
119 } else {
120 destVec[2] = 2.f / fRenderTargetSize.fHeight;
121 destVec[3] = -1.f;
122 }
123 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000124 };
125
126 /**
joshualittb0a8a372014-09-23 09:50:21 -0700127 * This function uploads uniforms and calls each GrGLProcessor's setData. It is called before a
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000128 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon861e1032014-12-16 07:33:49 -0800129 * GrGLGpu object to bind the textures required by the GrGLProcessors. The color and coverage
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000130 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +0000131 */
joshualittdafa4d02014-12-04 08:59:10 -0800132 void setData(const GrOptDrawState&);
bsalomon@google.com91961302011-05-09 18:39:58 +0000133
joshualitt47bb3822014-10-07 16:43:25 -0700134protected:
kkinnunen7510b222014-07-30 00:04:16 -0700135 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
joshualitt47bb3822014-10-07 16:43:25 -0700136 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000137
bsalomon861e1032014-12-16 07:33:49 -0800138 GrGLProgram(GrGLGpu*,
joshualitt79f8fae2014-10-28 17:59:26 -0700139 const GrProgramDesc&,
joshualitt47bb3822014-10-07 16:43:25 -0700140 const BuiltinUniformHandles&,
141 GrGLuint programID,
142 const UniformInfoArray&,
joshualitta5305a12014-10-10 17:47:00 -0700143 GrGLInstalledGeoProc* geometryProcessor,
egdanielc2304142014-12-11 13:15:13 -0800144 GrGLInstalledXferProc* xferProcessor,
joshualitta5305a12014-10-10 17:47:00 -0700145 GrGLInstalledFragProcs* fragmentProcessors);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000146
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000147 // Sets the texture units for samplers.
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000148 void initSamplerUniforms();
joshualitta5305a12014-10-10 17:47:00 -0700149 void initSamplers(GrGLInstalledProc*, int* texUnitIdx);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000150
joshualitt47bb3822014-10-07 16:43:25 -0700151 // A templated helper to loop over effects, set the transforms(via subclass) and bind textures
joshualitta5305a12014-10-10 17:47:00 -0700152 void setFragmentData(const GrOptDrawState&);
joshualitt290c09b2014-12-19 13:45:20 -0800153 virtual void setTransformData(const GrPendingFragmentStage&,
154 const SkMatrix& localMatrix,
155 GrGLInstalledFragProc*);
joshualitta5305a12014-10-10 17:47:00 -0700156 void bindTextures(const GrGLInstalledProc*, const GrProcessor&);
joshualitt47bb3822014-10-07 16:43:25 -0700157
158 /*
159 * Legacy NVPR needs a hook here to flush path tex gen settings.
160 * TODO when legacy nvpr is removed, remove this call.
161 */
162 virtual void didSetData(GrGpu::DrawType);
163
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000164 // Helper for setData() that sets the view matrix and loads the render target height uniform
joshualittdafa4d02014-12-04 08:59:10 -0800165 void setMatrixAndRenderTargetHeight(const GrOptDrawState&);
166 virtual void onSetMatrixAndRenderTargetHeight(const GrOptDrawState&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000167
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000168 // these reflect the current values of uniforms (GL uniform values travel with program)
joshualitt47bb3822014-10-07 16:43:25 -0700169 MatrixState fMatrixState;
170 GrColor fColor;
egdaniel37b4d862014-11-03 10:07:07 -0800171 uint8_t fCoverage;
joshualitt47bb3822014-10-07 16:43:25 -0700172 int fDstCopyTexUnit;
173 BuiltinUniformHandles fBuiltinUniformHandles;
174 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000175
joshualitt47bb3822014-10-07 16:43:25 -0700176 // the installed effects
joshualitta5305a12014-10-10 17:47:00 -0700177 SkAutoTDelete<GrGLInstalledGeoProc> fGeometryProcessor;
egdanielc2304142014-12-11 13:15:13 -0800178 SkAutoTDelete<GrGLInstalledXferProc> fXferProcessor;
joshualitta5305a12014-10-10 17:47:00 -0700179 SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000180
joshualitt79f8fae2014-10-28 17:59:26 -0700181 GrProgramDesc fDesc;
bsalomon861e1032014-12-16 07:33:49 -0800182 GrGLGpu* fGpu;
joshualitt47bb3822014-10-07 16:43:25 -0700183 GrGLProgramDataManager fProgramDataManager;
junov@google.comf93e7172011-03-31 21:26:24 +0000184
joshualitt47bb3822014-10-07 16:43:25 -0700185 friend class GrGLProgramBuilder;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000186
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000187 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000188};
189
joshualitt47bb3822014-10-07 16:43:25 -0700190/*
191 * Below are slight specializations of the program object for the different types of programs
192 * The default GrGL programs consist of at the very least a vertex and fragment shader.
193 * Legacy Nvpr only has a fragment shader, 1.3+ Nvpr ignores the vertex shader, but both require
194 * specialized methods for setting transform data. Both types of NVPR also require setting the
195 * projection matrix through a special function call
196 */
197class GrGLNvprProgramBase : public GrGLProgram {
198protected:
bsalomon861e1032014-12-16 07:33:49 -0800199 GrGLNvprProgramBase(GrGLGpu*,
joshualitt79f8fae2014-10-28 17:59:26 -0700200 const GrProgramDesc&,
joshualitt47bb3822014-10-07 16:43:25 -0700201 const BuiltinUniformHandles&,
202 GrGLuint programID,
203 const UniformInfoArray&,
joshualitt9b989322014-12-15 14:16:27 -0800204 GrGLInstalledGeoProc*,
egdanielc2304142014-12-11 13:15:13 -0800205 GrGLInstalledXferProc* xferProcessor,
joshualitta5305a12014-10-10 17:47:00 -0700206 GrGLInstalledFragProcs* fragmentProcessors);
joshualittdafa4d02014-12-04 08:59:10 -0800207 virtual void onSetMatrixAndRenderTargetHeight(const GrOptDrawState&);
joshualitt47bb3822014-10-07 16:43:25 -0700208
209 typedef GrGLProgram INHERITED;
210};
211
212class GrGLNvprProgram : public GrGLNvprProgramBase {
213public:
bsalomonae59b772014-11-19 08:23:49 -0800214 virtual bool hasVertexShader() const SK_OVERRIDE { return true; }
joshualitt47bb3822014-10-07 16:43:25 -0700215
216private:
217 typedef GrGLNvprProgramBuilder::SeparableVaryingInfo SeparableVaryingInfo;
218 typedef GrGLNvprProgramBuilder::SeparableVaryingInfoArray SeparableVaryingInfoArray;
bsalomon861e1032014-12-16 07:33:49 -0800219 GrGLNvprProgram(GrGLGpu*,
joshualitt79f8fae2014-10-28 17:59:26 -0700220 const GrProgramDesc&,
joshualitt47bb3822014-10-07 16:43:25 -0700221 const BuiltinUniformHandles&,
222 GrGLuint programID,
223 const UniformInfoArray&,
joshualitt9b989322014-12-15 14:16:27 -0800224 GrGLInstalledGeoProc*,
egdanielc2304142014-12-11 13:15:13 -0800225 GrGLInstalledXferProc* xferProcessor,
joshualitta5305a12014-10-10 17:47:00 -0700226 GrGLInstalledFragProcs* fragmentProcessors,
joshualitt47bb3822014-10-07 16:43:25 -0700227 const SeparableVaryingInfoArray& separableVaryings);
228 virtual void didSetData(GrGpu::DrawType) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800229 virtual void setTransformData(const GrPendingFragmentStage&,
joshualitt290c09b2014-12-19 13:45:20 -0800230 const SkMatrix& localMatrix,
bsalomonae59b772014-11-19 08:23:49 -0800231 GrGLInstalledFragProc*) SK_OVERRIDE;
joshualitt47bb3822014-10-07 16:43:25 -0700232
233 struct Varying {
234 GrGLint fLocation;
235 SkDEBUGCODE(
236 GrSLType fType;
237 );
238 };
239 SkTArray<Varying, true> fVaryings;
240
241 friend class GrGLNvprProgramBuilder;
242
243 typedef GrGLNvprProgramBase INHERITED;
244};
245
246class GrGLLegacyNvprProgram : public GrGLNvprProgramBase {
247public:
248 virtual bool hasVertexShader() const SK_OVERRIDE { return false; }
249
250private:
bsalomon861e1032014-12-16 07:33:49 -0800251 GrGLLegacyNvprProgram(GrGLGpu* gpu,
joshualitt79f8fae2014-10-28 17:59:26 -0700252 const GrProgramDesc& desc,
joshualitt47bb3822014-10-07 16:43:25 -0700253 const BuiltinUniformHandles&,
254 GrGLuint programID,
255 const UniformInfoArray&,
joshualitt9b989322014-12-15 14:16:27 -0800256 GrGLInstalledGeoProc*,
egdanielc2304142014-12-11 13:15:13 -0800257 GrGLInstalledXferProc* xp,
258 GrGLInstalledFragProcs* fps,
joshualitt47bb3822014-10-07 16:43:25 -0700259 int texCoordSetCnt);
260 virtual void didSetData(GrGpu::DrawType) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800261 virtual void setTransformData(const GrPendingFragmentStage&,
joshualitt290c09b2014-12-19 13:45:20 -0800262 const SkMatrix& localMatrix,
bsalomonae59b772014-11-19 08:23:49 -0800263 GrGLInstalledFragProc*) SK_OVERRIDE;
joshualitt47bb3822014-10-07 16:43:25 -0700264
265 int fTexCoordSetCnt;
266
267 friend class GrGLLegacyNvprProgramBuilder;
268
269 typedef GrGLNvprProgramBase INHERITED;
270};
271
junov@google.comf93e7172011-03-31 21:26:24 +0000272#endif