|
1 /* |
|
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" |
|
13 #include "GrAllocator.h" |
|
14 |
|
15 #include "SkTArray.h" |
|
16 |
|
17 class GrGpuGL; |
|
18 class SkMatrix; |
|
19 |
|
20 /** Manages a program's uniforms. |
|
21 */ |
|
22 class GrGLUniformManager { |
|
23 public: |
|
24 // Opaque handle to a uniform |
|
25 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) { |
|
40 SkASSERT(isValid()); |
|
41 } |
|
42 |
|
43 int toUniformIndex() const { SkASSERT(isValid()); return ~fValue; } |
|
44 |
|
45 int fValue; |
|
46 friend class GrGLUniformManager; // For accessing toUniformIndex(). |
|
47 }; |
|
48 |
|
49 GrGLUniformManager(GrGpuGL* gpu); |
|
50 |
|
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. 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 arrayCount, const GrGLfloat v[]) const; |
|
59 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; |
|
60 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
|
61 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; |
|
62 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
|
63 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; |
|
64 void set4fv(UniformHandle, 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 arrayCount, const GrGLfloat matrices[]) const; |
|
70 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; |
|
71 |
|
72 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform |
|
73 void setSkMatrix(UniformHandle, const SkMatrix&) const; |
|
74 |
|
75 struct BuilderUniform { |
|
76 GrGLShaderVar fVariable; |
|
77 uint32_t fVisibility; |
|
78 }; |
|
79 // 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; |
|
83 |
|
84 /** |
|
85 * Called by the GrGLShaderBuilder to know if the manager is using |
|
86 * BindUniformLocation. In that case getUniformLocations must be called |
|
87 * before the program is linked. |
|
88 */ |
|
89 bool isUsingBindUniform() const { return fUsingBindUniform; } |
|
90 |
|
91 /** |
|
92 * Called by the GrGLShaderBuilder to get GL locations for all uniforms. |
|
93 */ |
|
94 void getUniformLocations(GrGLuint programID, const BuilderUniformArray& uniforms); |
|
95 |
|
96 /** |
|
97 * Called by the GrGLShaderBuilder to access the array by the handle (index). |
|
98 */ |
|
99 const BuilderUniform& getBuilderUniform(const BuilderUniformArray&, GrGLUniformManager::UniformHandle) const; |
|
100 |
|
101 private: |
|
102 enum { |
|
103 kUnusedUniform = -1, |
|
104 }; |
|
105 |
|
106 struct Uniform { |
|
107 GrGLint fVSLocation; |
|
108 GrGLint fFSLocation; |
|
109 GrSLType fType; |
|
110 int fArrayCount; |
|
111 }; |
|
112 |
|
113 bool fUsingBindUniform; |
|
114 SkTArray<Uniform, true> fUniforms; |
|
115 GrGpuGL* fGpu; |
|
116 }; |
|
117 |
|
118 #endif |