blob: 8a98a75caea0eff1a62d52cbc214f16497dd62ac [file] [log] [blame]
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +00001/*
2 * Copyright 2012 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
kkinnunen7510b222014-07-30 00:04:16 -07008#ifndef GrGLProgramDataManager_DEFINED
9#define GrGLProgramDataManager_DEFINED
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000010
11#include "gl/GrGLShaderVar.h"
12#include "gl/GrGLSL.h"
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000013#include "GrAllocator.h"
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000014
15#include "SkTArray.h"
16
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000017class GrGpuGL;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000018class SkMatrix;
kkinnunendddc18a2014-08-03 23:19:46 -070019class GrGLProgram;
joshualitt30ba4362014-08-21 20:18:45 -070020class GrGLProgramBuilder;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000021
kkinnunen7510b222014-07-30 00:04:16 -070022/** Manages the resources used by a shader program.
23 * The resources are objects the program uses to communicate with the
24 * application code.
25 */
26class GrGLProgramDataManager : public SkRefCnt {
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000027public:
28 // Opaque handle to a uniform
kkinnunenec56e452014-08-25 22:21:16 -070029 class ShaderResourceHandle {
30 public:
31 bool isValid() const { return -1 != fValue; }
32 ShaderResourceHandle()
33 : fValue(-1) {
34 }
35 protected:
36 ShaderResourceHandle(int value)
37 : fValue(value) {
38 SkASSERT(isValid());
39 }
40 int fValue;
41 };
42
43 class UniformHandle : public ShaderResourceHandle {
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000044 public:
kkinnunendddc18a2014-08-03 23:19:46 -070045 /** Creates a reference to an unifrom of a GrGLShaderBuilder.
46 * The ref can be used to set the uniform with corresponding the GrGLProgramDataManager.*/
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000047 static UniformHandle CreateFromUniformIndex(int i);
kkinnunenec56e452014-08-25 22:21:16 -070048 UniformHandle() { }
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000049 bool operator==(const UniformHandle& other) const { return other.fValue == fValue; }
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000050 private:
kkinnunenec56e452014-08-25 22:21:16 -070051 UniformHandle(int value) : ShaderResourceHandle(value) { }
kkinnunendddc18a2014-08-03 23:19:46 -070052 int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; }
53 int toShaderBuilderIndex() const { return toProgramDataIndex(); }
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000054
kkinnunendddc18a2014-08-03 23:19:46 -070055 friend class GrGLProgramDataManager; // For accessing toProgramDataIndex().
joshualitt30ba4362014-08-21 20:18:45 -070056 friend class GrGLProgramBuilder; // For accessing toShaderBuilderIndex().
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000057 };
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000058
kkinnunenec56e452014-08-25 22:21:16 -070059 class VaryingHandle : public ShaderResourceHandle {
60 public:
61 /** Creates a reference to a varying in separable varyings of a GrGLShaderBuilder.
62 * The ref can be used to set the varying with the corresponding GrGLProgramDataManager.*/
63 static VaryingHandle CreateFromSeparableVaryingIndex(int i) {
64 return VaryingHandle(i);
65 }
66 VaryingHandle() { }
67 bool operator==(const VaryingHandle& other) const { return other.fValue == fValue; }
68 private:
69 VaryingHandle(int value) : ShaderResourceHandle(value) { }
70 int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; }
71 friend class GrGLProgramDataManager; // For accessing toProgramDataIndex().
72 };
73
joshualitt30ba4362014-08-21 20:18:45 -070074 GrGLProgramDataManager(GrGpuGL*, GrGLProgram*, const GrGLProgramBuilder&);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000075
76 /** Functions for uploading uniform values. The varities ending in v can be used to upload to an
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000077 * array of uniforms. arrayCount must be <= the array count of the uniform.
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000078 */
79 void setSampler(UniformHandle, GrGLint texUnit) const;
80 void set1f(UniformHandle, GrGLfloat v0) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000081 void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000082 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000083 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000084 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000085 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000086 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000087 void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000088 // matrices are column-major, the first three upload a single matrix, the latter three upload
89 // arrayCount matrices into a uniform array.
90 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const;
91 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000092 void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
93 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000094
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000095 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform
96 void setSkMatrix(UniformHandle, const SkMatrix&) const;
97
kkinnunenec56e452014-08-25 22:21:16 -070098 void setProgramPathFragmentInputTransform(VaryingHandle i,
99 unsigned components,
100 const SkMatrix& matrix) const;
101
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000102private:
103 enum {
104 kUnusedUniform = -1,
105 };
106
107 struct Uniform {
108 GrGLint fVSLocation;
109 GrGLint fFSLocation;
kkinnunendddc18a2014-08-03 23:19:46 -0700110 SkDEBUGCODE(
111 GrSLType fType;
112 int fArrayCount;
113 );
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000114 };
kkinnunenec56e452014-08-25 22:21:16 -0700115 struct Varying {
116 GrGLint fLocation;
117 SkDEBUGCODE(
118 GrSLType fType;
119 );
120 };
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000121
122 SkTArray<Uniform, true> fUniforms;
kkinnunenec56e452014-08-25 22:21:16 -0700123 SkTArray<Varying, true> fVaryings;
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000124 GrGpuGL* fGpu;
kkinnunenec56e452014-08-25 22:21:16 -0700125 GrGLProgram* fProgram;
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000126
127 typedef SkRefCnt INHERITED;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000128};
129
130#endif