blob: cce6c2a4d32882489d2d3bcf9c8bcc1882daa6f9 [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;
joshualitt47bb3822014-10-07 16:43:25 -070024class GrGLInstalledProcessors;
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
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000042 virtual ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000043
bsalomon@google.com34cccde2013-01-04 18:34:30 +000044 /**
45 * Call to abandon GL objects owned by this program.
46 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000047 void abandon();
48
joshualitt79f8fae2014-10-28 17:59:26 -070049 const GrProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000050
bsalomon@google.com271cffc2011-05-20 14:13:56 +000051 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000052 * Gets the GL program ID for this program.
53 */
kkinnunendddc18a2014-08-03 23:19:46 -070054 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000055
56 /**
joshualittee2af952014-12-30 09:04:15 -080057 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
58 * space and to make device space positions have the correct origin for processors that require
59 * them.
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000060 */
joshualittee2af952014-12-30 09:04:15 -080061 struct RenderTargetState {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000062 SkISize fRenderTargetSize;
63 GrSurfaceOrigin fRenderTargetOrigin;
64
joshualittee2af952014-12-30 09:04:15 -080065 RenderTargetState() { this->invalidate(); }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000066 void invalidate() {
bsalomon@google.com45a412e2013-02-13 16:13:13 +000067 fRenderTargetSize.fWidth = -1;
68 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000069 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
70 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000071
72 /**
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000073 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
74 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
75 * applied as such:
76 * pos.x = dot(v.xy, pos.xz)
77 * pos.y = dot(v.zq, pos.yz)
78 */
79 void getRTAdjustmentVec(GrGLfloat* destVec) {
80 destVec[0] = 2.f / fRenderTargetSize.fWidth;
81 destVec[1] = -1.f;
82 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
83 destVec[2] = -2.f / fRenderTargetSize.fHeight;
84 destVec[3] = 1.f;
85 } else {
86 destVec[2] = 2.f / fRenderTargetSize.fHeight;
87 destVec[3] = -1.f;
88 }
89 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000090 };
91
92 /**
joshualittb0a8a372014-09-23 09:50:21 -070093 * This function uploads uniforms and calls each GrGLProcessor's setData. It is called before a
bsalomon@google.com34cccde2013-01-04 18:34:30 +000094 * draw occurs using the program after the program has already been bound. It also uses the
bsalomon861e1032014-12-16 07:33:49 -080095 * GrGLGpu object to bind the textures required by the GrGLProcessors. The color and coverage
bsalomon@google.com2c84aa32013-06-06 20:28:57 +000096 * stages come from GrGLProgramDesc::Build().
bsalomon@google.com4285acc2012-10-22 14:11:24 +000097 */
joshualitt873ad0e2015-01-20 09:08:51 -080098 void setData(const GrPrimitiveProcessor&, const GrOptDrawState&, const GrBatchTracker&);
bsalomon@google.com91961302011-05-09 18:39:58 +000099
joshualitt47bb3822014-10-07 16:43:25 -0700100protected:
kkinnunen7510b222014-07-30 00:04:16 -0700101 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
joshualitt47bb3822014-10-07 16:43:25 -0700102 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000103
bsalomon861e1032014-12-16 07:33:49 -0800104 GrGLProgram(GrGLGpu*,
joshualitt79f8fae2014-10-28 17:59:26 -0700105 const GrProgramDesc&,
joshualitt47bb3822014-10-07 16:43:25 -0700106 const BuiltinUniformHandles&,
107 GrGLuint programID,
108 const UniformInfoArray&,
joshualitta5305a12014-10-10 17:47:00 -0700109 GrGLInstalledGeoProc* geometryProcessor,
egdanielc2304142014-12-11 13:15:13 -0800110 GrGLInstalledXferProc* xferProcessor,
joshualitta5305a12014-10-10 17:47:00 -0700111 GrGLInstalledFragProcs* fragmentProcessors);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000112
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +0000113 // Sets the texture units for samplers.
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000114 void initSamplerUniforms();
joshualittabb52a12015-01-13 15:02:10 -0800115 template <class Proc>
116 void initSamplers(Proc*, int* texUnitIdx);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000117
joshualitt47bb3822014-10-07 16:43:25 -0700118 // A templated helper to loop over effects, set the transforms(via subclass) and bind textures
joshualitt873ad0e2015-01-20 09:08:51 -0800119 void setFragmentData(const GrPrimitiveProcessor&, const GrOptDrawState&);
120 virtual void setTransformData(const GrPrimitiveProcessor&,
joshualittabb52a12015-01-13 15:02:10 -0800121 const GrPendingFragmentStage&,
122 int index,
joshualitt290c09b2014-12-19 13:45:20 -0800123 GrGLInstalledFragProc*);
joshualittabb52a12015-01-13 15:02:10 -0800124 template <class Proc>
125 void bindTextures(const Proc*, const GrProcessor&);
joshualitt47bb3822014-10-07 16:43:25 -0700126
127 /*
128 * Legacy NVPR needs a hook here to flush path tex gen settings.
129 * TODO when legacy nvpr is removed, remove this call.
130 */
joshualittb77e4b62015-01-21 10:16:17 -0800131 virtual void didSetData() {}
joshualitt47bb3822014-10-07 16:43:25 -0700132
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000133 // Helper for setData() that sets the view matrix and loads the render target height uniform
joshualitt873ad0e2015-01-20 09:08:51 -0800134 void setRenderTargetState(const GrPrimitiveProcessor&, const GrOptDrawState&);
135 virtual void onSetRenderTargetState(const GrPrimitiveProcessor&, const GrOptDrawState&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000136
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000137 // these reflect the current values of uniforms (GL uniform values travel with program)
joshualittee2af952014-12-30 09:04:15 -0800138 RenderTargetState fRenderTargetState;
joshualitt47bb3822014-10-07 16:43:25 -0700139 GrColor fColor;
egdaniel37b4d862014-11-03 10:07:07 -0800140 uint8_t fCoverage;
joshualitt47bb3822014-10-07 16:43:25 -0700141 int fDstCopyTexUnit;
142 BuiltinUniformHandles fBuiltinUniformHandles;
143 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000144
joshualitt47bb3822014-10-07 16:43:25 -0700145 // the installed effects
joshualitta5305a12014-10-10 17:47:00 -0700146 SkAutoTDelete<GrGLInstalledGeoProc> fGeometryProcessor;
egdanielc2304142014-12-11 13:15:13 -0800147 SkAutoTDelete<GrGLInstalledXferProc> fXferProcessor;
joshualitta5305a12014-10-10 17:47:00 -0700148 SkAutoTUnref<GrGLInstalledFragProcs> fFragmentProcessors;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000149
joshualitt79f8fae2014-10-28 17:59:26 -0700150 GrProgramDesc fDesc;
bsalomon861e1032014-12-16 07:33:49 -0800151 GrGLGpu* fGpu;
joshualitt47bb3822014-10-07 16:43:25 -0700152 GrGLProgramDataManager fProgramDataManager;
junov@google.comf93e7172011-03-31 21:26:24 +0000153
joshualitt47bb3822014-10-07 16:43:25 -0700154 friend class GrGLProgramBuilder;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000155
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000156 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000157};
158
joshualitt47bb3822014-10-07 16:43:25 -0700159/*
160 * Below are slight specializations of the program object for the different types of programs
161 * The default GrGL programs consist of at the very least a vertex and fragment shader.
162 * Legacy Nvpr only has a fragment shader, 1.3+ Nvpr ignores the vertex shader, but both require
163 * specialized methods for setting transform data. Both types of NVPR also require setting the
164 * projection matrix through a special function call
165 */
joshualittabb52a12015-01-13 15:02:10 -0800166class GrGLNvprProgram : public GrGLProgram {
joshualitt47bb3822014-10-07 16:43:25 -0700167protected:
bsalomon861e1032014-12-16 07:33:49 -0800168 GrGLNvprProgram(GrGLGpu*,
joshualitt79f8fae2014-10-28 17:59:26 -0700169 const GrProgramDesc&,
joshualitt47bb3822014-10-07 16:43:25 -0700170 const BuiltinUniformHandles&,
171 GrGLuint programID,
172 const UniformInfoArray&,
joshualitt9b989322014-12-15 14:16:27 -0800173 GrGLInstalledGeoProc*,
egdanielc2304142014-12-11 13:15:13 -0800174 GrGLInstalledXferProc* xferProcessor,
joshualittabb52a12015-01-13 15:02:10 -0800175 GrGLInstalledFragProcs* fragmentProcessors);
joshualitt47bb3822014-10-07 16:43:25 -0700176
joshualittabb52a12015-01-13 15:02:10 -0800177private:
joshualittb77e4b62015-01-21 10:16:17 -0800178 void didSetData() SK_OVERRIDE;
joshualitt873ad0e2015-01-20 09:08:51 -0800179 virtual void setTransformData(const GrPrimitiveProcessor&,
joshualittabb52a12015-01-13 15:02:10 -0800180 const GrPendingFragmentStage&,
181 int index,
182 GrGLInstalledFragProc*) SK_OVERRIDE;
joshualitt873ad0e2015-01-20 09:08:51 -0800183 virtual void onSetRenderTargetState(const GrPrimitiveProcessor&, const GrOptDrawState&);
joshualitt47bb3822014-10-07 16:43:25 -0700184
185 friend class GrGLNvprProgramBuilder;
186
joshualittabb52a12015-01-13 15:02:10 -0800187 typedef GrGLProgram INHERITED;
joshualitt47bb3822014-10-07 16:43:25 -0700188};
189
junov@google.comf93e7172011-03-31 21:26:24 +0000190#endif