blob: 8b870d4410c7bfe5510bc5a265f4e8be75b77d77 [file] [log] [blame]
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +00001/*
2 * Copyright 2013 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.
6 */
7
8#ifndef GrGLProgramEffects_DEFINED
9#define GrGLProgramEffects_DEFINED
10
11#include "GrBackendEffectFactory.h"
kkinnunen7510b222014-07-30 00:04:16 -070012#include "GrGLProgramDataManager.h"
kkinnunenec56e452014-08-25 22:21:16 -070013#include "GrGpu.h"
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000014#include "GrTexture.h"
15#include "GrTextureAccess.h"
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000016
bsalomonf99f8842014-07-07 11:54:23 -070017class GrEffect;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000018class GrEffectStage;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000019class GrGLVertexProgramEffectsBuilder;
joshualitt30ba4362014-08-21 20:18:45 -070020class GrGLProgramBuilder;
21class GrGLFullProgramBuilder;
22class GrGLFragmentOnlyProgramBuilder;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000023
24/**
25 * This class encapsulates an array of GrGLEffects and their supporting data (coord transforms
26 * and textures). It is built with GrGLProgramEffectsBuilder, then used to manage the necessary GL
27 * state and shader uniforms.
28 */
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +000029class GrGLProgramEffects : public SkRefCnt {
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000030public:
kkinnunen7510b222014-07-30 00:04:16 -070031 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
kkinnunenec56e452014-08-25 22:21:16 -070032 typedef GrGLProgramDataManager::VaryingHandle VaryingHandle;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000033
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000034 virtual ~GrGLProgramEffects();
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000035
36 /**
37 * Assigns a texture unit to each sampler. It starts on *texUnitIdx and writes the next
38 * available unit to *texUnitIdx when it returns.
39 */
kkinnunen7510b222014-07-30 00:04:16 -070040 void initSamplers(const GrGLProgramDataManager&, int* texUnitIdx);
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000041
42 /**
43 * Calls setData() on each effect, and sets their transformation matrices and texture bindings.
44 */
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000045 virtual void setData(GrGpuGL*,
kkinnunenec56e452014-08-25 22:21:16 -070046 GrGpu::DrawType,
kkinnunen7510b222014-07-30 00:04:16 -070047 const GrGLProgramDataManager&,
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000048 const GrEffectStage* effectStages[]) = 0;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000049
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000050protected:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000051 GrGLProgramEffects(int reserveCount)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000052 : fGLEffects(reserveCount)
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000053 , fSamplers(reserveCount) {
54 }
55
56 /**
57 * Helper for setData(). Binds all the textures for an effect.
58 */
joshualitt49586be2014-09-16 08:21:41 -070059 void bindTextures(GrGpuGL*, const GrEffect&, int effectIdx);
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000060
61 struct Sampler {
62 SkDEBUGCODE(Sampler() : fTextureUnit(-1) {})
63 UniformHandle fUniform;
64 int fTextureUnit;
65 };
66
joshualitt23e280d2014-09-18 12:26:38 -070067 /*
68 * Helpers for shader builders to build up program effects objects alongside shader code
69 */
70 void addEffect(GrGLEffect* effect) { fGLEffects.push_back(effect); }
71 SkTArray<Sampler, true>& addSamplers() { return fSamplers.push_back(); }
72
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000073 SkTArray<GrGLEffect*> fGLEffects;
74 SkTArray<SkSTArray<4, Sampler, true> > fSamplers;
commit-bot@chromium.orga05fa062014-05-30 18:55:03 +000075
76private:
joshualitt23e280d2014-09-18 12:26:38 -070077 friend class GrGLProgramBuilder;
78 friend class GrGLFullProgramBuilder;
79 friend class GrGLFragmentOnlyShaderBuilder;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000080
joshualitt23e280d2014-09-18 12:26:38 -070081 typedef SkRefCnt INHERITED;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +000082};
83
84////////////////////////////////////////////////////////////////////////////////
85
86/**
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000087 * This is a GrGLProgramEffects implementation that does coord transforms with the vertex shader.
88 */
89class GrGLVertexProgramEffects : public GrGLProgramEffects {
90public:
91 virtual void setData(GrGpuGL*,
kkinnunenec56e452014-08-25 22:21:16 -070092 GrGpu::DrawType,
kkinnunen7510b222014-07-30 00:04:16 -070093 const GrGLProgramDataManager&,
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000094 const GrEffectStage* effectStages[]) SK_OVERRIDE;
95
96private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000097 GrGLVertexProgramEffects(int reserveCount, bool explicitLocalCoords)
98 : INHERITED(reserveCount)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +000099 , fTransforms(reserveCount)
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000100 , fHasExplicitLocalCoords(explicitLocalCoords) {
101 }
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000102
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000103 struct Transform {
104 Transform() { fCurrentValue = SkMatrix::InvalidMatrix(); }
105 UniformHandle fHandle;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000106 SkMatrix fCurrentValue;
107 };
108
kkinnunenec56e452014-08-25 22:21:16 -0700109 struct PathTransform {
110 PathTransform() { fCurrentValue = SkMatrix::InvalidMatrix(); }
111 VaryingHandle fHandle;
112 SkMatrix fCurrentValue;
113 GrSLType fType;
114 };
115
joshualitt23e280d2014-09-18 12:26:38 -0700116 /*
117 * These functions are used by the builders to build up program effects along side the shader
118 * code itself
119 */
120 SkSTArray<2, Transform, true>& addTransforms() { return fTransforms.push_back(); }
121 SkTArray<PathTransform, true>& addPathTransforms() { return fPathTransforms.push_back(); }
122
123 /**
124 * Helper for setData(). Sets all the transform matrices for an effect.
125 */
126 void setTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&, const GrEffectStage&,
127 int effectIdx);
128 void setPathTransformData(GrGpuGL* gpu, const GrGLProgramDataManager&, const GrEffectStage&,
129 int effectIdx);
130
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000131 SkTArray<SkSTArray<2, Transform, true> > fTransforms;
kkinnunenec56e452014-08-25 22:21:16 -0700132 SkTArray<SkTArray<PathTransform, true> > fPathTransforms;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000133 bool fHasExplicitLocalCoords;
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000134
joshualitt23e280d2014-09-18 12:26:38 -0700135 friend class GrGLFullProgramBuilder;
joshualitt30ba4362014-08-21 20:18:45 -0700136
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000137 typedef GrGLProgramEffects INHERITED;
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000138};
139
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000140////////////////////////////////////////////////////////////////////////////////
141
142/**
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000143 * This is a GrGLProgramEffects implementation that does coord transforms with
144 * the the NV_path_rendering PathTexGen functionality.
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000145 */
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000146class GrGLPathTexGenProgramEffects : public GrGLProgramEffects {
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000147public:
148 virtual void setData(GrGpuGL*,
kkinnunenec56e452014-08-25 22:21:16 -0700149 GrGpu::DrawType,
kkinnunen7510b222014-07-30 00:04:16 -0700150 const GrGLProgramDataManager&,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000151 const GrEffectStage* effectStages[]) SK_OVERRIDE;
152
153private:
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000154 GrGLPathTexGenProgramEffects(int reserveCount)
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000155 : INHERITED(reserveCount)
156 , fTransforms(reserveCount) {
157 }
158
159 /**
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000160 * Helper for setData(). Sets the PathTexGen state for each transform in an effect.
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000161 */
joshualitt49586be2014-09-16 08:21:41 -0700162 void setPathTexGenState(GrGpuGL*, const GrEffectStage&, int effectIdx);
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000163
164 struct Transforms {
joshualitt23e280d2014-09-18 12:26:38 -0700165 Transforms(int texCoordIndex)
166 : fTexCoordIndex(texCoordIndex) {}
167 int fTexCoordIndex;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000168 };
169
joshualitt23e280d2014-09-18 12:26:38 -0700170 /*
171 * Helper for fragment only shader builder to build up the program effects alongside the shader
172 */
173 void addTransforms(int coordIndex) {
174 fTransforms.push_back(Transforms(coordIndex));
175 }
176
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000177 SkTArray<Transforms> fTransforms;
178
joshualitt23e280d2014-09-18 12:26:38 -0700179 friend class GrGLFragmentOnlyProgramBuilder;
180
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000181 typedef GrGLProgramEffects INHERITED;
182};
183
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000184#endif