blob: 31f7f2e15ba5a280ee244a5128c77aa6e0383375 [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
bsalomon861e1032014-12-16 07:33:49 -080017class GrGLGpu;
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
joshualitt47bb3822014-10-07 16:43:25 -070059 struct UniformInfo {
60 GrGLShaderVar fVariable;
61 uint32_t fVisibility;
62 GrGLint fLocation;
kkinnunenec56e452014-08-25 22:21:16 -070063 };
64
joshualitt47bb3822014-10-07 16:43:25 -070065 // This uses an allocator rather than array so that the GrGLShaderVars don't move in memory
66 // after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their
67 // name strings. Otherwise, we'd have to hand out copies.
68 typedef GrTAllocator<UniformInfo> UniformInfoArray;
69
bsalomon861e1032014-12-16 07:33:49 -080070 GrGLProgramDataManager(GrGLGpu*, const UniformInfoArray&);
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000071
72 /** 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 +000073 * array of uniforms. arrayCount must be <= the array count of the uniform.
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000074 */
75 void setSampler(UniformHandle, GrGLint texUnit) const;
76 void set1f(UniformHandle, GrGLfloat v0) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000077 void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000078 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000079 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000080 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000081 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000082 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000083 void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000084 // matrices are column-major, the first three upload a single matrix, the latter three upload
85 // arrayCount matrices into a uniform array.
86 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const;
87 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const;
commit-bot@chromium.orgd3baf202013-11-07 22:06:08 +000088 void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
89 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000090
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000091 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform
92 void setSkMatrix(UniformHandle, const SkMatrix&) const;
93
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000094private:
95 enum {
96 kUnusedUniform = -1,
97 };
98
99 struct Uniform {
100 GrGLint fVSLocation;
101 GrGLint fFSLocation;
kkinnunendddc18a2014-08-03 23:19:46 -0700102 SkDEBUGCODE(
103 GrSLType fType;
104 int fArrayCount;
105 );
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000106 };
107
108 SkTArray<Uniform, true> fUniforms;
bsalomon861e1032014-12-16 07:33:49 -0800109 GrGLGpu* fGpu;
commit-bot@chromium.org6eac42e2014-05-29 21:29:51 +0000110
111 typedef SkRefCnt INHERITED;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000112};
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000113#endif