2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGLProgramDataManager_DEFINED
9 #define GrGLProgramDataManager_DEFINED
11 #include "gl/GrGLShaderVar.h"
12 #include "gl/GrGLSL.h"
13 #include "GrAllocator.h"
20 class GrGLProgramBuilder;
22 /** Manages the resources used by a shader program.
23 * The resources are objects the program uses to communicate with the
26 class GrGLProgramDataManager : public SkRefCnt {
28 // Opaque handle to a uniform
29 class ShaderResourceHandle {
31 bool isValid() const { return -1 != fValue; }
32 ShaderResourceHandle()
36 ShaderResourceHandle(int value)
43 class UniformHandle : public ShaderResourceHandle {
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);
49 bool operator==(const UniformHandle& other) const { return other.fValue == fValue; }
51 UniformHandle(int value) : ShaderResourceHandle(value) { }
52 int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; }
53 int toShaderBuilderIndex() const { return toProgramDataIndex(); }
55 friend class GrGLProgramDataManager; // For accessing toProgramDataIndex().
56 friend class GrGLProgramBuilder; // For accessing toShaderBuilderIndex().
59 class VaryingHandle : public ShaderResourceHandle {
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);
67 bool operator==(const VaryingHandle& other) const { return other.fValue == fValue; }
69 VaryingHandle(int value) : ShaderResourceHandle(value) { }
70 int toProgramDataIndex() const { SkASSERT(isValid()); return fValue; }
71 friend class GrGLProgramDataManager; // For accessing toProgramDataIndex().
74 GrGLProgramDataManager(GrGpuGL*, GrGLProgram*, const GrGLProgramBuilder&);
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.
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;
95 // convenience method for uploading a SkMatrix to a 3x3 matrix uniform
96 void setSkMatrix(UniformHandle, const SkMatrix&) const;
98 void setProgramPathFragmentInputTransform(VaryingHandle i,
100 const SkMatrix& matrix) const;
122 SkTArray<Uniform, true> fUniforms;
123 SkTArray<Varying, true> fVaryings;
125 GrGLProgram* fProgram;
127 typedef SkRefCnt INHERITED;