xref: /aosp_15_r20/external/skia/src/gpu/graphite/Uniform.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 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 skgpu_graphite_Uniform_DEFINED
9 #define skgpu_graphite_Uniform_DEFINED
10 
11 #include "src/core/SkSLTypeShared.h"
12 
13 namespace skgpu::graphite {
14 
15 // TODO: can SkRuntimeEffect::Uniform be absorbed into this class!?
16 
17 /**
18  * Describes a uniform. Uniforms consist of:
19  *  name:       The constant string name to use in the generated SkSL (with mangling)
20  *  type:       The type of the uniform
21  *  count:      Number of elements of 'type' in the array or kNonArray if not an array.
22  */
23 class Uniform {
24 public:
25     static constexpr int kNonArray = 0;
26 
27     constexpr Uniform(const char* name, SkSLType type, int count = kNonArray)
Uniform(name,type,count,false)28             : Uniform(name, type, count, /*isPaintColor=*/false) {}
29 
30     /*
31      * The paint color uniform is treated special and will only be added to the uniform block
32      * once. Its name will not be mangled.
33      */
PaintColor()34     static constexpr Uniform PaintColor() {
35         return Uniform("paintColor", SkSLType::kFloat4,
36                        Uniform::kNonArray, /*isPaintColor=*/true);
37     }
38 
39     constexpr Uniform(const Uniform&) = default;
40     Uniform& operator=(const Uniform&) = default;
41 
name()42     constexpr const char* name()  const { return fName; }
type()43     constexpr SkSLType    type()  const { return static_cast<SkSLType>(fType);  }
count()44     constexpr int         count() const { return static_cast<int>(fCount); }
45 
isPaintColor()46     constexpr bool isPaintColor() const { return static_cast<bool>(fIsPaintColor); }
47 
48 private:
Uniform(const char * name,SkSLType type,int count,bool isPaintColor)49     constexpr Uniform(const char* name, SkSLType type, int count, bool isPaintColor)
50             : fName(name)
51             , fType(static_cast<unsigned>(type))
52             , fIsPaintColor(static_cast<unsigned>(isPaintColor))
53             , fCount(static_cast<unsigned>(count)) {
54         SkASSERT(SkSLTypeCanBeUniformValue(type));
55         SkASSERT(count >= 0);
56     }
57 
58     const char* fName;
59 
60     // Uniform definitions for all encountered SkRuntimeEffects are stored permanently in the
61     // ShaderCodeDictionary as part of the stable ShaderSnippet and code ID assigned to the
62     // effect, including de-duplicating equivalent or re-created SkRuntimeEffects with the same
63     // SkSL. To help keep this memory overhead as low as possible, we pack the Uniform fields
64     // as tightly as possible.
65     uint32_t    fType         : 6;
66     uint32_t    fIsPaintColor : 1;
67     uint32_t    fCount        : 25;
68 
69     static_assert(kSkSLTypeCount <= (1 << 6));
70 };
71 
72 } // namespace skgpu::graphite
73 
74 #endif // skgpu_graphite_Uniform_DEFINED
75