xref: /aosp_15_r20/external/skia/src/gpu/ganesh/gl/GrGLProgramDataManager.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/gpu/ganesh/gl/GrGLTypes.h"
12 #include "include/private/base/SkDebug.h"
13 #include "include/private/base/SkTArray.h"
14 #include "src/base/SkTBlockList.h"
15 #include "src/gpu/ganesh/GrShaderVar.h"
16 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
17 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
18 
19 #include <cstdint>
20 
21 class GrGLGpu;
22 enum class SkSLType : char;
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 GLUniformInfo : public GrGLSLUniformHandler::UniformInfo {
31         GrGLint fLocation;
32     };
33 
34     struct VaryingInfo {
35         GrShaderVar fVariable;
36         GrGLint     fLocation;
37     };
38 
39     // This uses a SkTBlockList rather than SkTArray/std::vector so that the GrShaderVars
40     // don't move in memory after they are inserted. Users of GrGLShaderBuilder get refs to the vars
41     // and ptrs to their name strings. Otherwise, we'd have to hand out copies.
42     typedef SkTBlockList<GLUniformInfo> UniformInfoArray;
43     typedef SkTBlockList<VaryingInfo>   VaryingInfoArray;
44 
45     GrGLProgramDataManager(GrGLGpu*, const UniformInfoArray&);
46 
47     void setSamplerUniforms(const UniformInfoArray& samplers, int startUnit) const;
48 
49     /** Functions for uploading uniform values. The varities ending in v can be used to upload to an
50     *  array of uniforms. arrayCount must be <= the array count of the uniform.
51     */
52     void set1i(UniformHandle, int32_t) const override;
53     void set1iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
54     void set1f(UniformHandle, float v0) const override;
55     void set1fv(UniformHandle, int arrayCount, const float v[]) const override;
56     void set2i(UniformHandle, int32_t, int32_t) const override;
57     void set2iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
58     void set2f(UniformHandle, float, float) const override;
59     void set2fv(UniformHandle, int arrayCount, const float v[]) const override;
60     void set3i(UniformHandle, int32_t, int32_t, int32_t) const override;
61     void set3iv(UniformHandle, int arrayCount, const int32_t 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 set4i(UniformHandle, int32_t, int32_t, int32_t, int32_t) const override;
65     void set4iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
66     void set4f(UniformHandle, float, float, float, float) const override;
67     void set4fv(UniformHandle, int arrayCount, const float v[]) const override;
68     // matrices are column-major, the first three upload a single matrix, the latter three upload
69     // arrayCount matrices into a uniform array.
70     void setMatrix2f(UniformHandle, const float matrix[]) const override;
71     void setMatrix3f(UniformHandle, const float matrix[]) const override;
72     void setMatrix4f(UniformHandle, const float matrix[]) const override;
73     void setMatrix2fv(UniformHandle, int arrayCount, const float matrices[]) const override;
74     void setMatrix3fv(UniformHandle, int arrayCount, const float matrices[]) const override;
75     void setMatrix4fv(UniformHandle, int arrayCount, const float matrices[]) const override;
76 
77 private:
78     struct Uniform {
79         GrGLint     fLocation;
80 #ifdef SK_DEBUG
81         SkSLType    fType;
82         int         fArrayCount;
83 #endif
84     };
85 
86     template<int N> inline void setMatrices(UniformHandle, int arrayCount,
87                                             const float matrices[]) const;
88 
89     skia_private::TArray<Uniform, true> fUniforms;
90     GrGLGpu* fGpu;
91 
92     using INHERITED = GrGLSLProgramDataManager;
93 };
94 
95 #endif
96