| 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 GrGLProgramDataManager_DEFINED | 8 #ifndef GrGLProgramDataManager_DEFINED |
| 9 #define GrGLProgramDataManager_DEFINED | 9 #define GrGLProgramDataManager_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 class GrGLProgram; |
| 20 class GrGLShaderBuilder; |
| 19 | 21 |
| 20 /** Manages the resources used by a shader program. | 22 /** Manages the resources used by a shader program. |
| 21 * The resources are objects the program uses to communicate with the | 23 * The resources are objects the program uses to communicate with the |
| 22 * application code. | 24 * application code. |
| 23 */ | 25 */ |
| 24 class GrGLProgramDataManager : public SkRefCnt { | 26 class GrGLProgramDataManager : public SkRefCnt { |
| 25 public: | 27 public: |
| 26 // Opaque handle to a uniform | 28 // Opaque handle to a uniform |
| 27 class UniformHandle { | 29 class UniformHandle { |
| 28 public: | 30 public: |
| 31 /** Creates a reference to an unifrom of a GrGLShaderBuilder. |
| 32 * The ref can be used to set the uniform with corresponding the GrGLPro
gramDataManager.*/ |
| 29 static UniformHandle CreateFromUniformIndex(int i); | 33 static UniformHandle CreateFromUniformIndex(int i); |
| 30 | 34 |
| 31 bool isValid() const { return 0 != fValue; } | 35 bool isValid() const { return -1 != fValue; } |
| 32 | 36 |
| 33 bool operator==(const UniformHandle& other) const { return other.fValue
== fValue; } | 37 bool operator==(const UniformHandle& other) const { return other.fValue
== fValue; } |
| 34 | 38 |
| 35 UniformHandle() | 39 UniformHandle() |
| 36 : fValue(0) { | 40 : fValue(-1) { |
| 37 } | 41 } |
| 38 | 42 |
| 39 private: | 43 private: |
| 40 UniformHandle(int value) | 44 UniformHandle(int value) |
| 41 : fValue(~value) { | 45 : fValue(value) { |
| 42 SkASSERT(isValid()); | 46 SkASSERT(isValid()); |
| 43 } | 47 } |
| 44 | 48 |
| 45 int toUniformIndex() const { SkASSERT(isValid()); return ~fValue; } | 49 int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; } |
| 50 int toShaderBuilderIndex() const { return toProgramDataIndex(); } |
| 46 | 51 |
| 47 int fValue; | 52 int fValue; |
| 48 friend class GrGLProgramDataManager; // For accessing toUniformIndex(). | 53 friend class GrGLProgramDataManager; // For accessing toProgramDataIndex
(). |
| 54 friend class GrGLShaderBuilder; // For accessing toShaderBuilderIndex(). |
| 49 }; | 55 }; |
| 50 | 56 |
| 51 GrGLProgramDataManager(GrGpuGL* gpu); | 57 GrGLProgramDataManager(GrGpuGL*, GrGLProgram*, const GrGLShaderBuilder&); |
| 52 | |
| 53 UniformHandle appendUniform(GrSLType type, int arrayCount = GrGLShaderVar::k
NonArray); | |
| 54 | 58 |
| 55 /** Functions for uploading uniform values. The varities ending in v can be
used to upload to an | 59 /** Functions for uploading uniform values. The varities ending in v can be
used to upload to an |
| 56 * array of uniforms. arrayCount must be <= the array count of the uniform. | 60 * array of uniforms. arrayCount must be <= the array count of the uniform. |
| 57 */ | 61 */ |
| 58 void setSampler(UniformHandle, GrGLint texUnit) const; | 62 void setSampler(UniformHandle, GrGLint texUnit) const; |
| 59 void set1f(UniformHandle, GrGLfloat v0) const; | 63 void set1f(UniformHandle, GrGLfloat v0) const; |
| 60 void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; | 64 void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
| 61 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; | 65 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; |
| 62 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; | 66 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
| 63 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; | 67 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; |
| 64 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; | 68 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
| 65 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; | 69 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; |
| 66 void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; | 70 void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; |
| 67 // matrices are column-major, the first three upload a single matrix, the la
tter three upload | 71 // matrices are column-major, the first three upload a single matrix, the la
tter three upload |
| 68 // arrayCount matrices into a uniform array. | 72 // arrayCount matrices into a uniform array. |
| 69 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; | 73 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; |
| 70 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; | 74 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; |
| 71 void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[])
const; | 75 void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[])
const; |
| 72 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[])
const; | 76 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[])
const; |
| 73 | 77 |
| 74 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform | 78 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform |
| 75 void setSkMatrix(UniformHandle, const SkMatrix&) const; | 79 void setSkMatrix(UniformHandle, const SkMatrix&) const; |
| 76 | 80 |
| 77 struct BuilderUniform { | |
| 78 GrGLShaderVar fVariable; | |
| 79 uint32_t fVisibility; | |
| 80 }; | |
| 81 // This uses an allocator rather than array so that the GrGLShaderVars don't
move in memory | |
| 82 // after they are inserted. Users of GrGLShaderBuilder get refs to the vars
and ptrs to their | |
| 83 // name strings. Otherwise, we'd have to hand out copies. | |
| 84 typedef GrTAllocator<BuilderUniform> BuilderUniformArray; | |
| 85 | |
| 86 /** | |
| 87 * Called by the GrGLShaderBuilder to know if the manager is using | |
| 88 * BindUniformLocation. In that case getUniformLocations must be called | |
| 89 * before the program is linked. | |
| 90 */ | |
| 91 bool isUsingBindUniform() const { return fUsingBindUniform; } | |
| 92 | |
| 93 /** | |
| 94 * Called by the GrGLShaderBuilder to get GL locations for all uniforms. | |
| 95 */ | |
| 96 void getUniformLocations(GrGLuint programID, const BuilderUniformArray& unif
orms); | |
| 97 | |
| 98 /** | |
| 99 * Called by the GrGLShaderBuilder to access the array by the handle (index)
. | |
| 100 */ | |
| 101 const BuilderUniform& getBuilderUniform(const BuilderUniformArray&, GrGLProg
ramDataManager::UniformHandle) const; | |
| 102 | |
| 103 private: | 81 private: |
| 104 enum { | 82 enum { |
| 105 kUnusedUniform = -1, | 83 kUnusedUniform = -1, |
| 106 }; | 84 }; |
| 107 | 85 |
| 108 struct Uniform { | 86 struct Uniform { |
| 109 GrGLint fVSLocation; | 87 GrGLint fVSLocation; |
| 110 GrGLint fFSLocation; | 88 GrGLint fFSLocation; |
| 111 GrSLType fType; | 89 SkDEBUGCODE( |
| 112 int fArrayCount; | 90 GrSLType fType; |
| 91 int fArrayCount; |
| 92 ); |
| 113 }; | 93 }; |
| 114 | 94 |
| 115 bool fUsingBindUniform; | |
| 116 SkTArray<Uniform, true> fUniforms; | 95 SkTArray<Uniform, true> fUniforms; |
| 117 GrGpuGL* fGpu; | 96 GrGpuGL* fGpu; |
| 118 | 97 |
| 119 typedef SkRefCnt INHERITED; | 98 typedef SkRefCnt INHERITED; |
| 120 }; | 99 }; |
| 121 | 100 |
| 122 #endif | 101 #endif |
| OLD | NEW |