blob: 22678cb6a38343804e82e733ce71df313cb4de84 [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
robertphillips@google.com6177e692013-02-28 20:16:25 +000012#include "GrGLContext.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000013#include "GrGLProgramDesc.h"
bsalomon@google.com890e3b52012-06-01 19:01:37 +000014#include "GrGLTexture.h"
kkinnunen7510b222014-07-30 00:04:16 -070015#include "GrGLProgramDataManager.h"
egdaniel018fb622015-10-28 07:26:40 -070016#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080017#include "glsl/GrGLSLUniformHandler.h"
junov@google.comf93e7172011-03-31 21:26:24 +000018
bsalomon@google.comf0a104e2012-07-10 17:51:07 +000019#include "SkString.h"
Scroggo97c88c22011-05-11 14:05:25 +000020#include "SkXfermode.h"
21
joshualittd8dd47b2015-09-11 11:45:01 -070022#include "builders/GrGLProgramBuilder.h"
23
joshualitt47bb3822014-10-07 16:43:25 -070024class GrGLInstalledProcessors;
joshualitt30ba4362014-08-21 20:18:45 -070025class GrGLProgramBuilder;
egdaniel8dd688b2015-01-22 10:16:09 -080026class GrPipeline;
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:
egdaniel7ea439b2015-12-03 09:20:44 -080039 typedef GrGLSLProgramBuilder::BuiltinUniformHandles BuiltinUniformHandles;
kkinnunendddc18a2014-08-03 23:19:46 -070040
joshualittd8dd47b2015-09-11 11:45:01 -070041 ~GrGLProgram();
junov@google.comf93e7172011-03-31 21:26:24 +000042
bsalomon@google.com34cccde2013-01-04 18:34:30 +000043 /**
44 * Call to abandon GL objects owned by this program.
45 */
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000046 void abandon();
47
joshualitt79f8fae2014-10-28 17:59:26 -070048 const GrProgramDesc& getDesc() { return fDesc; }
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000049
bsalomon@google.com271cffc2011-05-20 14:13:56 +000050 /**
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000051 * Gets the GL program ID for this program.
52 */
kkinnunendddc18a2014-08-03 23:19:46 -070053 GrGLuint programID() const { return fProgramID; }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000054
55 /**
joshualittee2af952014-12-30 09:04:15 -080056 * We use the RT's size and origin to adjust from Skia device space to OpenGL normalized device
57 * space and to make device space positions have the correct origin for processors that require
58 * them.
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000059 */
joshualittee2af952014-12-30 09:04:15 -080060 struct RenderTargetState {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000061 SkISize fRenderTargetSize;
62 GrSurfaceOrigin fRenderTargetOrigin;
63
joshualittee2af952014-12-30 09:04:15 -080064 RenderTargetState() { this->invalidate(); }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000065 void invalidate() {
bsalomon@google.com45a412e2013-02-13 16:13:13 +000066 fRenderTargetSize.fWidth = -1;
67 fRenderTargetSize.fHeight = -1;
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000068 fRenderTargetOrigin = (GrSurfaceOrigin) -1;
69 }
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000070
71 /**
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000072 * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device
73 * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is
74 * applied as such:
75 * pos.x = dot(v.xy, pos.xz)
robertphillipsef4ba3d2015-09-17 11:21:06 -070076 * pos.y = dot(v.zw, pos.yz)
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000077 */
egdaniel018fb622015-10-28 07:26:40 -070078 void getRTAdjustmentVec(float* destVec) {
commit-bot@chromium.org47c66dd2014-05-29 01:12:10 +000079 destVec[0] = 2.f / fRenderTargetSize.fWidth;
80 destVec[1] = -1.f;
81 if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) {
82 destVec[2] = -2.f / fRenderTargetSize.fHeight;
83 destVec[3] = 1.f;
84 } else {
85 destVec[2] = 2.f / fRenderTargetSize.fHeight;
86 destVec[3] = -1.f;
87 }
88 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +000089 };
90
91 /**
egdaniel7dc4bd02015-10-29 07:57:01 -070092 * This function uploads uniforms, calls each GrGL*Processor's setData, and retrieves the
cdalton42717652015-06-18 11:54:30 -070093 * textures that need to be bound on each unit. It is the caller's responsibility to ensure
94 * the program is bound before calling, and to bind the outgoing textures to their respective
95 * units upon return. (Each index in the array corresponds to its matching GL texture unit.)
bsalomon@google.com4285acc2012-10-22 14:11:24 +000096 */
joshualitt465283c2015-09-11 08:19:35 -070097 void setData(const GrPrimitiveProcessor&, const GrPipeline&,
cdalton42717652015-06-18 11:54:30 -070098 SkTArray<const GrTextureAccess*>* textureBindings);
bsalomon@google.com91961302011-05-09 18:39:58 +000099
joshualitt47bb3822014-10-07 16:43:25 -0700100protected:
egdaniel018fb622015-10-28 07:26:40 -0700101 typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
joshualitt47bb3822014-10-07 16:43:25 -0700102 typedef GrGLProgramDataManager::UniformInfoArray UniformInfoArray;
egdaniel0eafe792015-11-20 14:01:22 -0800103 typedef GrGLProgramDataManager::VaryingInfoArray VaryingInfoArray;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000104
bsalomon861e1032014-12-16 07:33:49 -0800105 GrGLProgram(GrGLGpu*,
joshualitt79f8fae2014-10-28 17:59:26 -0700106 const GrProgramDesc&,
joshualitt47bb3822014-10-07 16:43:25 -0700107 const BuiltinUniformHandles&,
108 GrGLuint programID,
109 const UniformInfoArray&,
egdaniel0eafe792015-11-20 14:01:22 -0800110 const VaryingInfoArray&, // used for NVPR only currently
egdanielfa896322016-01-13 12:19:30 -0800111 GrGLSLPrimitiveProcessor* geometryProcessor,
112 GrGLSLXferProcessor* xferProcessor,
113 const GrGLSLFragProcs& fragmentProcessors,
cdalton42717652015-06-18 11:54:30 -0700114 SkTArray<UniformHandle>* passSamplerUniforms);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000115
joshualitt47bb3822014-10-07 16:43:25 -0700116 // A templated helper to loop over effects, set the transforms(via subclass) and bind textures
cdalton42717652015-06-18 11:54:30 -0700117 void setFragmentData(const GrPrimitiveProcessor&, const GrPipeline&,
118 SkTArray<const GrTextureAccess*>* textureBindings);
joshualittd8dd47b2015-09-11 11:45:01 -0700119 void setTransformData(const GrPrimitiveProcessor&,
120 const GrFragmentProcessor&,
egdanielfa896322016-01-13 12:19:30 -0800121 int index);
joshualitt47bb3822014-10-07 16:43:25 -0700122
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000123 // Helper for setData() that sets the view matrix and loads the render target height uniform
egdaniel8dd688b2015-01-22 10:16:09 -0800124 void setRenderTargetState(const GrPrimitiveProcessor&, const GrPipeline&);
bsalomon@google.com2c84aa32013-06-06 20:28:57 +0000125
bsalomon@google.com34cccde2013-01-04 18:34:30 +0000126 // these reflect the current values of uniforms (GL uniform values travel with program)
joshualittee2af952014-12-30 09:04:15 -0800127 RenderTargetState fRenderTargetState;
joshualitt47bb3822014-10-07 16:43:25 -0700128 BuiltinUniformHandles fBuiltinUniformHandles;
129 GrGLuint fProgramID;
junov@google.comf93e7172011-03-31 21:26:24 +0000130
joshualitt47bb3822014-10-07 16:43:25 -0700131 // the installed effects
egdanielfa896322016-01-13 12:19:30 -0800132 SkAutoTDelete<GrGLSLPrimitiveProcessor> fGeometryProcessor;
133 SkAutoTDelete<GrGLSLXferProcessor> fXferProcessor;
134 GrGLSLFragProcs fFragmentProcessors;
skia.committer@gmail.com9681eeb2014-05-30 03:06:10 +0000135
joshualitt79f8fae2014-10-28 17:59:26 -0700136 GrProgramDesc fDesc;
bsalomon861e1032014-12-16 07:33:49 -0800137 GrGLGpu* fGpu;
joshualitt47bb3822014-10-07 16:43:25 -0700138 GrGLProgramDataManager fProgramDataManager;
cdalton42717652015-06-18 11:54:30 -0700139 SkTArray<UniformHandle> fSamplerUniforms;
junov@google.comf93e7172011-03-31 21:26:24 +0000140
joshualitt47bb3822014-10-07 16:43:25 -0700141 friend class GrGLProgramBuilder;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000142
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000143 typedef SkRefCnt INHERITED;
junov@google.comf93e7172011-03-31 21:26:24 +0000144};
145
junov@google.comf93e7172011-03-31 21:26:24 +0000146#endif