Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrGLUniformManager_DEFINED | 8 #ifndef GrGLUniformManager_DEFINED |
| 9 #define GrGLUniformManager_DEFINED | 9 #define GrGLUniformManager_DEFINED |
| 10 | 10 |
| 11 #include "gl/GrGLShaderVar.h" | 11 #include "gl/GrGLShaderVar.h" |
| 12 #include "gl/GrGLSL.h" | 12 #include "gl/GrGLSL.h" |
| 13 #include "GrAllocator.h" | 13 #include "GrAllocator.h" |
| 14 | 14 |
| 15 #include "SkTArray.h" | 15 #include "SkTArray.h" |
| 16 | 16 |
| 17 class GrGpuGL; | 17 class GrGpuGL; |
| 18 class SkMatrix; | 18 class SkMatrix; |
| 19 | 19 |
| 20 /** Manages a program's uniforms. | 20 /** Manages a program's uniforms and other shader resources. |
|
Kimmo Kinnunen
2014/07/01 13:01:21
I think this change sums up well the problem of th
| |
| 21 */ | 21 */ |
| 22 class GrGLUniformManager : public SkRefCnt { | 22 class GrGLUniformManager : public SkRefCnt { |
| 23 public: | 23 public: |
| 24 // Opaque handle to a uniform | 24 // Opaque handle to a uniform |
| 25 class UniformHandle { | 25 class ShaderResourceHandle { |
| 26 public: | 26 public: |
| 27 static UniformHandle CreateFromUniformIndex(int i); | |
| 28 | |
| 29 bool isValid() const { return 0 != fValue; } | 27 bool isValid() const { return 0 != fValue; } |
| 30 | 28 ShaderResourceHandle() |
| 31 bool operator==(const UniformHandle& other) const { return other.fValue == fValue; } | |
| 32 | |
| 33 UniformHandle() | |
| 34 : fValue(0) { | 29 : fValue(0) { |
| 35 } | 30 } |
| 36 | 31 protected: |
| 37 private: | 32 ShaderResourceHandle(int value) |
| 38 UniformHandle(int value) | |
| 39 : fValue(~value) { | 33 : fValue(~value) { |
| 40 SkASSERT(isValid()); | 34 SkASSERT(isValid()); |
| 41 } | 35 } |
| 36 int fValue; | |
| 37 }; | |
| 42 | 38 |
| 39 class UniformHandle : public ShaderResourceHandle { | |
| 40 public: | |
| 41 static UniformHandle CreateFromUniformIndex(int i); | |
| 42 UniformHandle() { } | |
| 43 bool operator==(const UniformHandle& other) const { return other.fValue == fValue; } | |
| 44 private: | |
| 45 UniformHandle(int value) : ShaderResourceHandle(value) { } | |
| 43 int toUniformIndex() const { SkASSERT(isValid()); return ~fValue; } | 46 int toUniformIndex() const { SkASSERT(isValid()); return ~fValue; } |
| 47 friend class GrGLUniformManager; // For accessing toUniformIndex(). | |
| 48 }; | |
| 44 | 49 |
| 45 int fValue; | 50 class FragmentInputHandle : public ShaderResourceHandle { |
| 46 friend class GrGLUniformManager; // For accessing toUniformIndex(). | 51 public: |
| 52 static FragmentInputHandle CreateFromFragmentInputIndex(int i) { | |
| 53 return FragmentInputHandle(i); | |
| 54 } | |
| 55 FragmentInputHandle() { } | |
| 56 bool operator==(const FragmentInputHandle& other) const { return other.f Value == fValue; } | |
| 57 private: | |
| 58 FragmentInputHandle(int value) : ShaderResourceHandle(value) { } | |
| 59 int toLocationIndex() const { SkASSERT(isValid()); return ~fValue; } | |
| 60 friend class GrGLUniformManager; // For accessing toLocationIndex(). | |
| 47 }; | 61 }; |
| 48 | 62 |
| 49 GrGLUniformManager(GrGpuGL* gpu); | 63 GrGLUniformManager(GrGpuGL* gpu); |
| 50 | 64 |
| 51 UniformHandle appendUniform(GrSLType type, int arrayCount = GrGLShaderVar::k NonArray); | 65 UniformHandle appendUniform(GrSLType type, int arrayCount = GrGLShaderVar::k NonArray); |
| 52 | 66 |
| 53 /** Functions for uploading uniform values. The varities ending in v can be used to upload to an | 67 /** 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. | 68 * array of uniforms. arrayCount must be <= the array count of the uniform. |
| 55 */ | 69 */ |
| 56 void setSampler(UniformHandle, GrGLint texUnit) const; | 70 void setSampler(UniformHandle, GrGLint texUnit) const; |
| 57 void set1f(UniformHandle, GrGLfloat v0) const; | 71 void set1f(UniformHandle, GrGLfloat v0) const; |
| 58 void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; | 72 void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
| 59 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; | 73 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; |
| 60 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; | 74 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
| 61 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; | 75 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; |
| 62 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; | 76 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
| 63 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; | 77 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; |
| 64 void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; | 78 void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
| 65 // matrices are column-major, the first three upload a single matrix, the la tter three upload | 79 // matrices are column-major, the first three upload a single matrix, the la tter three upload |
| 66 // arrayCount matrices into a uniform array. | 80 // arrayCount matrices into a uniform array. |
| 67 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; | 81 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; |
| 68 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; | 82 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; |
| 69 void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; | 83 void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; |
| 70 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; | 84 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; |
| 71 | 85 |
| 72 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform | 86 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform |
| 73 void setSkMatrix(UniformHandle, const SkMatrix&) const; | 87 void setSkMatrix(UniformHandle, const SkMatrix&) const; |
| 74 | 88 |
| 89 FragmentInputHandle appendFragmentInput(); | |
| 90 void setFragmentInput(FragmentInputHandle i, size_t components, const SkMatr ix& matrix) const; | |
| 91 | |
| 75 struct BuilderUniform { | 92 struct BuilderUniform { |
| 76 GrGLShaderVar fVariable; | 93 GrGLShaderVar fVariable; |
| 77 uint32_t fVisibility; | 94 uint32_t fVisibility; |
| 78 }; | 95 }; |
| 79 // This uses an allocator rather than array so that the GrGLShaderVars don't move in memory | 96 // 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 | 97 // 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. | 98 // name strings. Otherwise, we'd have to hand out copies. |
| 82 typedef GrTAllocator<BuilderUniform> BuilderUniformArray; | 99 typedef GrTAllocator<BuilderUniform> BuilderUniformArray; |
| 83 | 100 |
| 84 /** | 101 typedef GrTAllocator<GrGLShaderVar> BuilderFragmentInputArray; |
| 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 | 102 |
| 91 /** | 103 /** |
| 92 * Called by the GrGLShaderBuilder to get GL locations for all uniforms. | 104 * Prepares the resolution of program resource locations. |
| 105 * This is to be called before program is linked. | |
| 93 */ | 106 */ |
| 94 void getUniformLocations(GrGLuint programID, const BuilderUniformArray& unif orms); | 107 void bindProgramResourceLocations(GrGLuint programID, const BuilderUniformAr ray& uniforms); |
| 108 | |
| 109 /** | |
| 110 * Resolves the program resource locations. | |
| 111 * This is to be called after program is linked. | |
| 112 */ | |
| 113 void resolveProgramResourceLocations(GrGLuint programID, const BuilderUnifor mArray& uniforms, const BuilderFragmentInputArray& fragmentInputs); | |
| 95 | 114 |
| 96 /** | 115 /** |
| 97 * Called by the GrGLShaderBuilder to access the array by the handle (index) . | 116 * Called by the GrGLShaderBuilder to access the array by the handle (index) . |
| 98 */ | 117 */ |
| 99 const BuilderUniform& getBuilderUniform(const BuilderUniformArray&, GrGLUnif ormManager::UniformHandle) const; | 118 const BuilderUniform& getBuilderUniform(const BuilderUniformArray&, GrGLUnif ormManager::UniformHandle) const; |
| 100 | 119 |
| 101 private: | 120 private: |
| 102 enum { | 121 enum { |
| 103 kUnusedUniform = -1, | 122 kUnusedUniform = -1, |
| 104 }; | 123 }; |
| 105 | 124 |
| 106 struct Uniform { | 125 struct Uniform { |
| 107 GrGLint fVSLocation; | 126 GrGLint fVSLocation; |
| 108 GrGLint fFSLocation; | 127 GrGLint fFSLocation; |
| 109 GrSLType fType; | 128 GrSLType fType; |
| 110 int fArrayCount; | 129 int fArrayCount; |
| 111 }; | 130 }; |
| 131 struct FragmentInput { | |
| 132 GrGLint fLocation; | |
| 133 }; | |
| 112 | 134 |
| 113 bool fUsingBindUniform; | 135 bool fUsingBindUniform; |
| 114 SkTArray<Uniform, true> fUniforms; | 136 SkTArray<Uniform, true> fUniforms; |
| 137 SkTArray<FragmentInput, true> fFragmentInputs; | |
| 115 GrGpuGL* fGpu; | 138 GrGpuGL* fGpu; |
| 116 | 139 |
| 117 typedef SkRefCnt INHERITED; | 140 typedef SkRefCnt INHERITED; |
| 118 }; | 141 }; |
| 119 | 142 |
| 120 #endif | 143 #endif |
| OLD | NEW |