xref: /aosp_15_r20/external/skia/src/gpu/graphite/Attribute.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_Attribute_DEFINED
9 #define skgpu_graphite_Attribute_DEFINED
10 
11 #include "include/private/base/SkAlign.h"
12 #include "src/core/SkSLTypeShared.h"
13 #include "src/gpu/graphite/DrawTypes.h"
14 
15 namespace skgpu::graphite {
16 
17  /** Describes a vertex or instance attribute. */
18 class Attribute {
19 public:
20     constexpr Attribute() = default;
Attribute(const char * name,VertexAttribType cpuType,SkSLType gpuType)21     constexpr Attribute(const char* name,
22                         VertexAttribType cpuType,
23                         SkSLType gpuType)
24             : fName(name), fCPUType(cpuType), fGPUType(gpuType) {
25         SkASSERT(name && gpuType != SkSLType::kVoid);
26     }
27     constexpr Attribute(const Attribute&) = default;
28 
29     Attribute& operator=(const Attribute&) = default;
30 
isInitialized()31     constexpr bool isInitialized() const { return fGPUType != SkSLType::kVoid; }
32 
name()33     constexpr const char*      name()    const { return fName; }
cpuType()34     constexpr VertexAttribType cpuType() const { return fCPUType; }
gpuType()35     constexpr SkSLType         gpuType() const { return fGPUType; }
36 
size()37     constexpr size_t size()       const { return VertexAttribTypeSize(fCPUType); }
sizeAlign4()38     constexpr size_t sizeAlign4() const { return SkAlign4(this->size()); }
39 
40 private:
41     const char* fName = nullptr;
42     VertexAttribType fCPUType = VertexAttribType::kFloat;
43     SkSLType fGPUType = SkSLType::kVoid;
44 };
45 
46 enum class Interpolation {
47     // The default perspective-correct interpolation for floating point types.
48     kPerspective,
49     // Screen-space linear interpolation for floating point types.
50     kLinear,
51     // No guarantee on what the provoking vertex is, should be used when all vertices have the same
52     // value so that is irrelevant.
53     //
54     // The only supported interpolation option for integer types.
55     kFlat
56 };
57 
58 /**Describes an interpolated value passed between a vertex and fragment shader. */
59 class Varying {
60 public:
61     constexpr Varying() = default;
62     constexpr Varying(const char* name,
63                       SkSLType gpuType,
64                       Interpolation interpolation = Interpolation::kPerspective)
fName(name)65             : fName(name)
66             , fGPUType(gpuType)
67             , fInterpolation(SkSLTypeIsIntegralType(gpuType) ? Interpolation::kFlat
68                                                              : interpolation) {
69         SkASSERT(name && gpuType != SkSLType::kVoid);
70         SkASSERT(SkSLTypeVecLength(gpuType) >= 1); // Only scalar/vector types allowed as varyings.
71         // Allow kPerspective for integer types since that's the default arg and will be replaced
72         // with kFlat; but explicitly requesting kLinear for integer types is not allowed.
73         SkASSERT(SkSLTypeIsFloatType(gpuType) || interpolation != Interpolation::kLinear);
74     }
75 
76     constexpr Varying(const Varying&) = default;
77 
78     Varying& operator=(const Varying&) = default;
79 
isInitialized()80     constexpr bool isInitialized() const { return fGPUType != SkSLType::kVoid; }
81 
name()82     constexpr const char*   name()          const { return fName; }
gpuType()83     constexpr SkSLType      gpuType()       const { return fGPUType; }
interpolation()84     constexpr Interpolation interpolation() const { return fInterpolation; }
85 
86 private:
87     const char* fName = nullptr;
88     SkSLType fGPUType = SkSLType::kVoid;
89     Interpolation fInterpolation = Interpolation::kPerspective;
90 };
91 
92 } // namespace skgpu::graphite
93 
94 #endif // skgpu_graphite_Attribute_DEFINED
95