xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrUniformDataManager.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 Google LLC
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 GrUniformDataManager_DEFINED
9 #define GrUniformDataManager_DEFINED
10 
11 #include "include/private/base/SkDebug.h"
12 #include "include/private/base/SkTArray.h"
13 #include "src/base/SkAutoMalloc.h"
14 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
15 
16 #include <cstdint>
17 
18 enum class SkSLType : char;
19 
20 /**
21  * Subclass of GrGLSLProgramDataManager used to store uniforms for a program in a CPU buffer that
22  * can be uploaded to a UBO. This currently assumes uniform layouts that are compatible with
23  * Vulkan, Dawn, and D3D12. It could be used more broadly if this aspect was made configurable.
24  */
25 class GrUniformDataManager : public GrGLSLProgramDataManager {
26 public:
27     GrUniformDataManager(uint32_t uniformCount, uint32_t uniformSize);
28 
29     void set1i(UniformHandle, int32_t) const override;
30     void set1iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
31     void set1f(UniformHandle, float v0) const override;
32     void set1fv(UniformHandle, int arrayCount, const float v[]) const override;
33     void set2i(UniformHandle, int32_t, int32_t) const override;
34     void set2iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
35     void set2f(UniformHandle, float, float) const override;
36     void set2fv(UniformHandle, int arrayCount, const float v[]) const override;
37     void set3i(UniformHandle, int32_t, int32_t, int32_t) const override;
38     void set3iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
39     void set3f(UniformHandle, float, float, float) const override;
40     void set3fv(UniformHandle, int arrayCount, const float v[]) const override;
41     void set4i(UniformHandle, int32_t, int32_t, int32_t, int32_t) const override;
42     void set4iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
43     void set4f(UniformHandle, float, float, float, float) const override;
44     void set4fv(UniformHandle, int arrayCount, const float v[]) const override;
45     // Matrices are column-major. The following three calls upload a single matrix into a uniform.
46     void setMatrix2f(UniformHandle, const float matrix[]) const override;
47     void setMatrix3f(UniformHandle, const float matrix[]) const override;
48     void setMatrix4f(UniformHandle, const float matrix[]) const override;
49     // These three calls upload arrayCount matrices into a uniform array.
50     void setMatrix2fv(UniformHandle, int arrayCount, const float matrices[]) const override;
51     void setMatrix3fv(UniformHandle, int arrayCount, const float matrices[]) const override;
52     void setMatrix4fv(UniformHandle, int arrayCount, const float matrices[]) const override;
53 
54     // For the uniform data to be dirty so that we will reupload on the next use.
markDirty()55     void markDirty() { fUniformsDirty = true; }
56 
57 protected:
58     struct Uniform {
59         uint32_t fOffset : 24;
60         SkSLType fType   : 8;
61         SkDEBUGCODE(
62             int  fArrayCount;
63         )
64     };
65 
66     int copyUniforms(void* dest, const void* src, int numUniforms, SkSLType uniformType) const;
67 
68     template <int N, SkSLType kFullType, SkSLType kHalfType>
69     inline void set(UniformHandle u, const void* v) const;
70     template <int N, SkSLType kFullType, SkSLType kHalfType>
71     inline void setv(UniformHandle u, int arrayCount, const void* v) const;
72     template <int N, SkSLType FullType, SkSLType HalfType>
73     inline void setMatrices(UniformHandle, int arrayCount, const float matrices[]) const;
74 
75     void* getBufferPtrAndMarkDirty(const Uniform& uni) const;
76 
77     uint32_t fUniformSize;
78     bool fWrite16BitUniforms = false;
79 
80     skia_private::TArray<Uniform, true> fUniforms;
81 
82     mutable SkAutoMalloc fUniformData;
83     mutable bool         fUniformsDirty = false;
84 };
85 
86 #endif
87