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 "glsl/GrGLSLProgramDataManager.h"
12 
13 #include "GrAllocator.h"
14 #include "gl/GrGLSampler.h"
15 #include "gl/GrGLTypes.h"
16 #include "glsl/GrGLSLShaderVar.h"
17 
18 #include "SkTArray.h"
19 
20 class GrGLGpu;
21 class SkMatrix;
22 class GrGLProgram;
23 
24 /** Manages the resources used by a shader program.
25  * The resources are objects the program uses to communicate with the
26  * application code.
27  */
28 class GrGLProgramDataManager : public GrGLSLProgramDataManager {
29 public:
30     struct UniformInfo {
31         GrGLSLShaderVar fVariable;
32         uint32_t        fVisibility;
33         GrGLint         fLocation;
34     };
35 
36     struct VaryingInfo {
37         GrGLSLShaderVar fVariable;
38         GrGLint         fLocation;
39     };
40 
41     // This uses an allocator rather than array so that the GrGLSLShaderVars don't move in memory
42     // after they are inserted. Users of GrGLShaderBuilder get refs to the vars and ptrs to their
43     // name strings. Otherwise, we'd have to hand out copies.
44     typedef GrTAllocator<UniformInfo> UniformInfoArray;
45     typedef GrTAllocator<VaryingInfo> VaryingInfoArray;
46 
47     GrGLProgramDataManager(GrGLGpu*, GrGLuint programID, const UniformInfoArray&,
48                            const VaryingInfoArray&);
49 
50 
51     void setSamplers(const SkTArray<GrGLSampler>& samplers) const;
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 set1i(UniformHandle, int32_t) const override;
57     void set1iv(UniformHandle, int arrayCount, const int v[]) const override;
58     void set1f(UniformHandle, float v0) const override;
59     void set1fv(UniformHandle, int arrayCount, const float v[]) const override;
60     void set2f(UniformHandle, float, float) const override;
61     void set2fv(UniformHandle, int arrayCount, const float v[]) const override;
62     void set3f(UniformHandle, float, float, float) const override;
63     void set3fv(UniformHandle, int arrayCount, const float v[]) const override;
64     void set4f(UniformHandle, float, float, float, float) const override;
65     void set4fv(UniformHandle, int arrayCount, const float v[]) const override;
66     // matrices are column-major, the first three upload a single matrix, the latter three upload
67     // arrayCount matrices into a uniform array.
68     void setMatrix2f(UniformHandle, const float matrix[]) const override;
69     void setMatrix3f(UniformHandle, const float matrix[]) const override;
70     void setMatrix4f(UniformHandle, const float matrix[]) const override;
71     void setMatrix2fv(UniformHandle, int arrayCount, const float matrices[]) const override;
72     void setMatrix3fv(UniformHandle, int arrayCount, const float matrices[]) const override;
73     void setMatrix4fv(UniformHandle, int arrayCount, const float matrices[]) const override;
74 
75     // for nvpr only
76     void setPathFragmentInputTransform(VaryingHandle u, int components,
77                                        const SkMatrix& matrix) const override;
78 
79 private:
80     enum {
81         kUnusedUniform = -1,
82     };
83 
84     struct Uniform {
85         GrGLint     fVSLocation;
86         GrGLint     fFSLocation;
87         SkDEBUGCODE(
88             GrSLType    fType;
89             int         fArrayCount;
90         );
91     };
92 
93     enum {
94         kUnusedPathProcVarying = -1,
95     };
96     struct PathProcVarying {
97         GrGLint     fLocation;
98         SkDEBUGCODE(
99             GrSLType    fType;
100             int         fArrayCount;
101         );
102     };
103 
104     SkDEBUGCODE(void printUnused(const Uniform&) const;)
105 
106     template<int N> inline void setMatrices(UniformHandle, int arrayCount,
107                                             const float matrices[]) const;
108 
109     SkTArray<Uniform, true> fUniforms;
110     SkTArray<PathProcVarying, true> fPathProcVaryings;
111     GrGLGpu* fGpu;
112     GrGLuint fProgramID;
113 
114     typedef GrGLSLProgramDataManager INHERITED;
115 };
116 
117 #endif
118