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 GrGLProgramDataManager_DEFINED 9 #define GrGLProgramDataManager_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 class GrGLProgram; 20 class GrGLProgramBuilder; 21 22 /** Manages the resources used by a shader program. 23 * The resources are objects the program uses to communicate with the 24 * application code. 25 */ 26 class GrGLProgramDataManager : public SkRefCnt { 27 public: 28 // Opaque handle to a uniform 29 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 { 44 public: 45 /** Creates a reference to an unifrom of a GrGLShaderBuilder. 46 * The ref can be used to set the uniform with corresponding the GrGLProgramDataManager.*/ 47 static UniformHandle CreateFromUniformIndex(int i); 48 UniformHandle() { } 49 bool operator==(const UniformHandle& other) const { return other.fValue == fValue; } 50 private: 51 UniformHandle(int value) : ShaderResourceHandle(value) { } 52 int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; } 53 int toShaderBuilderIndex() const { return toProgramDataIndex(); } 54 55 friend class GrGLProgramDataManager; // For accessing toProgramDataIndex(). 56 friend class GrGLProgramBuilder; // For accessing toShaderBuilderIndex(). 57 }; 58 59 class VaryingHandle : public ShaderResourceHandle { 60 public: 61 /** Creates a reference to a varying in separable varyings of a GrGLShaderBuilder. 62 * The ref can be used to set the varying with the corresponding GrGLProgramDataManager.*/ 63 static VaryingHandle CreateFromSeparableVaryingIndex(int i) { 64 return VaryingHandle(i); 65 } 66 VaryingHandle() { } 67 bool operator==(const VaryingHandle& other) const { return other.fValue == fValue; } 68 private: 69 VaryingHandle(int value) : ShaderResourceHandle(value) { } 70 int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; } 71 friend class GrGLProgramDataManager; // For accessing toProgramDataIndex(). 72 }; 73 74 GrGLProgramDataManager(GrGpuGL*, GrGLProgram*, const GrGLProgramBuilder&); 75 76 /** Functions for uploading uniform values. The varities ending in v can be used to upload to an 77 * array of uniforms. arrayCount must be <= the array count of the uniform. 78 */ 79 void setSampler(UniformHandle, GrGLint texUnit) const; 80 void set1f(UniformHandle, GrGLfloat v0) const; 81 void set1fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; 82 void set2f(UniformHandle, GrGLfloat, GrGLfloat) const; 83 void set2fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; 84 void set3f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat) const; 85 void set3fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; 86 void set4f(UniformHandle, GrGLfloat, GrGLfloat, GrGLfloat, GrGLfloat) const; 87 void set4fv(UniformHandle, int arrayCount, const GrGLfloat v[]) const; 88 // matrices are column-major, the first three upload a single matrix, the latter three upload 89 // arrayCount matrices into a uniform array. 90 void setMatrix3f(UniformHandle, const GrGLfloat matrix[]) const; 91 void setMatrix4f(UniformHandle, const GrGLfloat matrix[]) const; 92 void setMatrix3fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; 93 void setMatrix4fv(UniformHandle, int arrayCount, const GrGLfloat matrices[]) const; 94 95 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform 96 void setSkMatrix(UniformHandle, const SkMatrix&) const; 97 98 void setProgramPathFragmentInputTransform(VaryingHandle i, 99 unsigned components, 100 const SkMatrix& matrix) const; 101 102 private: 103 enum { 104 kUnusedUniform = -1, 105 }; 106 107 struct Uniform { 108 GrGLint fVSLocation; 109 GrGLint fFSLocation; 110 SkDEBUGCODE( 111 GrSLType fType; 112 int fArrayCount; 113 ); 114 }; 115 struct Varying { 116 GrGLint fLocation; 117 SkDEBUGCODE( 118 GrSLType fType; 119 ); 120 }; 121 122 SkTArray<Uniform, true> fUniforms; 123 SkTArray<Varying, true> fVaryings; 124 GrGpuGL* fGpu; 125 GrGLProgram* fProgram; 126 127 typedef SkRefCnt INHERITED; 128 }; 129 130 #endif 131