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 "src/gpu/glsl/GrGLSLProgramDataManager.h"
12
13#include "include/private/GrTypesPriv.h"
14#include "include/private/SkTArray.h"
15#include "src/core/SkAutoMalloc.h"
16
17/**
18 * Subclass of GrGLSLProgramDataManager used to store uniforms for a program in a CPU buffer that
19 * can be uploaded to a UBO. This currently assumes uniform layouts that are compatible with
20 * Vulkan, Dawn, and D3D12. It could be used more broadly if this aspect was made configurable.
21 */
22class GrUniformDataManager : public GrGLSLProgramDataManager {
23public:
24 GrUniformDataManager(uint32_t uniformCount, uint32_t uniformSize);
25
26 void set1i(UniformHandle, int32_t) const override;
27 void set1iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
28 void set1f(UniformHandle, float v0) const override;
29 void set1fv(UniformHandle, int arrayCount, const float v[]) const override;
30 void set2i(UniformHandle, int32_t, int32_t) const override;
31 void set2iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
32 void set2f(UniformHandle, float, float) const override;
33 void set2fv(UniformHandle, int arrayCount, const float v[]) const override;
34 void set3i(UniformHandle, int32_t, int32_t, int32_t) const override;
35 void set3iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
36 void set3f(UniformHandle, float, float, float) const override;
37 void set3fv(UniformHandle, int arrayCount, const float v[]) const override;
38 void set4i(UniformHandle, int32_t, int32_t, int32_t, int32_t) const override;
39 void set4iv(UniformHandle, int arrayCount, const int32_t v[]) const override;
40 void set4f(UniformHandle, float, float, float, float) const override;
41 void set4fv(UniformHandle, int arrayCount, const float v[]) const override;
42 // matrices are column-major, the first two upload a single matrix, the latter two upload
43 // arrayCount matrices into a uniform array.
44 void setMatrix2f(UniformHandle, const float matrix[]) const override;
45 void setMatrix3f(UniformHandle, const float matrix[]) const override;
46 void setMatrix4f(UniformHandle, const float matrix[]) const override;
47 void setMatrix2fv(UniformHandle, int arrayCount, const float matrices[]) const override;
48 void setMatrix3fv(UniformHandle, int arrayCount, const float matrices[]) const override;
49 void setMatrix4fv(UniformHandle, int arrayCount, const float matrices[]) const override;
50
51 // for nvpr only
52 void setPathFragmentInputTransform(VaryingHandle u, int components,
53 const SkMatrix& matrix) const override {
54 SK_ABORT("Only supported in NVPR, which is only available in GL");
55 }
56
57 // For the uniform data to be dirty so that we will reupload on the next use.
58 void markDirty() { fUniformsDirty = true; }
59
60protected:
61 struct Uniform {
62 uint32_t fOffset;
63 SkDEBUGCODE(
64 GrSLType fType;
65 int fArrayCount;
66 );
67 };
68
69 template<int N> inline void setMatrices(UniformHandle, int arrayCount,
70 const float matrices[]) const;
71
72 void* getBufferPtrAndMarkDirty(const Uniform& uni) const;
73
74 uint32_t fUniformSize;
75
76 SkTArray<Uniform, true> fUniforms;
77
78 mutable SkAutoMalloc fUniformData;
79 mutable bool fUniformsDirty;
80};
81
82#endif
83