Debian Codegraph @

src/gpu/graphite/Attribute.h

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