blob: eefab049865070d92ea57cb88ccdfb536fea3a18 [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
8#ifndef GrGLUniformManager_DEFINED
9#define GrGLUniformManager_DEFINED
10
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;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000019
20/** Manages a program's uniforms.
21*/
22class GrGLUniformManager {
23public:
24 // Opaque handle to a uniform
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000025 class UniformHandle {
26 public:
27 static UniformHandle CreateFromUniformIndex(int i);
28
29 bool isValid() const { return 0 != fValue; }
30
31 bool operator==(const UniformHandle& other) const { return other.fValue == fValue; }
32
33 UniformHandle()
34 : fValue(0) {
35 }
36
37 private:
38 UniformHandle(int value)
39 : fValue(~value) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000040 SkASSERT(isValid());
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000041 }
42
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000043 int toUniformIndex() const { SkASSERT(isValid()); return ~fValue; }
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000044
45 int fValue;
46 friend class GrGLUniformManager; // For accessing toUniformIndex().
47 };
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000048
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000049 GrGLUniformManager(GrGpuGL* gpu) : fGpu(gpu) {}
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000050
51 UniformHandle appendUniform(GrSLType type, int arrayCount = GrGLShaderVar::kNonArray);
52
53 /** Functions for uploading uniform values. The varities ending in v can be used to upload to an
54 * array of uniforms. offset + arrayCount must be <= the array count of the uniform.
55 */
56 void setSampler(UniformHandle, GrGLint texUnit) const;
57 void set1f(UniformHandle, GrGLfloat v0) const;
58 void set1fv(UniformHandle, int offset, int arrayCount, const GrGLfloat v[]) const;
59 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const;
60 void set2fv(UniformHandle, int offset, int arrayCount, const GrGLfloat v[]) const;
61 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const;
62 void set3fv(UniformHandle, int offset, int arrayCount, const GrGLfloat v[]) const;
63 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const;
64 void set4fv(UniformHandle, int offset, int arrayCount, const GrGLfloat v[]) const;
65 // matrices are column-major, the first three upload a single matrix, the latter three upload
66 // arrayCount matrices into a uniform array.
67 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const;
68 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const;
69 void setMatrix3fv(UniformHandle, int offset, int arrayCount, const GrGLfloat matrices[]) const;
70 void setMatrix4fv(UniformHandle, int offset, int arrayCount, const GrGLfloat matrices[]) const;
71
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000072 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform
73 void setSkMatrix(UniformHandle, const SkMatrix&) const;
74
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000075 struct BuilderUniform {
76 GrGLShaderVar fVariable;
77 uint32_t fVisibility;
78 };
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000079 // This uses an allocator rather than array so that the GrGLShaderVars don't move in memory
80 // after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their
81 // name strings. Otherwise, we'd have to hand out copies.
82 typedef GrTAllocator<BuilderUniform> BuilderUniformArray;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000083
84 /**
85 * Called by the GrGLShaderBuilder to get GL locations for all uniforms.
86 */
87 void getUniformLocations(GrGLuint programID, const BuilderUniformArray& uniforms);
88
commit-bot@chromium.org7425c122013-08-14 18:14:19 +000089 /**
90 * Called by the GrGLShaderBuilder to access the array by the handle (index).
91 */
92 const BuilderUniform& getBuilderUniform(const BuilderUniformArray&, GrGLUniformManager::UniformHandle) 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;
102 GrSLType fType;
103 int fArrayCount;
104 };
105
106 SkTArray<Uniform, true> fUniforms;
commit-bot@chromium.org9188a152013-09-05 18:28:24 +0000107 GrGpuGL* fGpu;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +0000108};
109
110#endif