Debian Codegraph @

Source/Core/VideoCommon/NativeVertexFormat.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <cstring>
7 #include <functional> // for hash
8
9 #include "Common/CommonTypes.h"
10 #include "VideoCommon/CPMemory.h"
11
12 // m_components
13 enum : u32
14 {
15 VB_HAS_POSMTXIDX = (1 << 1),
16 VB_HAS_TEXMTXIDX0 = (1 << 2),
17 VB_HAS_TEXMTXIDX1 = (1 << 3),
18 VB_HAS_TEXMTXIDX2 = (1 << 4),
19 VB_HAS_TEXMTXIDX3 = (1 << 5),
20 VB_HAS_TEXMTXIDX4 = (1 << 6),
21 VB_HAS_TEXMTXIDX5 = (1 << 7),
22 VB_HAS_TEXMTXIDX6 = (1 << 8),
23 VB_HAS_TEXMTXIDX7 = (1 << 9),
24 VB_HAS_TEXMTXIDXALL = (0xff << 2),
25
26 // VB_HAS_POS=0, // Implied, it always has pos! don't bother testing
27 VB_HAS_NORMAL = (1 << 10),
28 VB_HAS_TANGENT = (1 << 11),
29 VB_HAS_BINORMAL = (1 << 12),
30
31 VB_COL_SHIFT = 13,
32 VB_HAS_COL0 = (1 << 13),
33 VB_HAS_COL1 = (1 << 14),
34
35 VB_HAS_UV0 = (1 << 15),
36 VB_HAS_UV1 = (1 << 16),
37 VB_HAS_UV2 = (1 << 17),
38 VB_HAS_UV3 = (1 << 18),
39 VB_HAS_UV4 = (1 << 19),
40 VB_HAS_UV5 = (1 << 20),
41 VB_HAS_UV6 = (1 << 21),
42 VB_HAS_UV7 = (1 << 22),
43 VB_HAS_UVALL = (0xff << 15),
44 VB_HAS_UVTEXMTXSHIFT = 13,
45 };
46
47 struct AttributeFormat
48 {
49 ComponentFormat type;
50 int components;
51 int offset;
52 bool enable;
53 bool integer;
54 };
55
56 struct PortableVertexDeclaration
57 {
58 int stride;
59
60 AttributeFormat position;
61 std::array<AttributeFormat, 3> normals;
62 std::array<AttributeFormat, 2> colors;
63 std::array<AttributeFormat, 8> texcoords;
64 AttributeFormat posmtx;
65
66 // Make sure we initialize padding to 0 since padding is included in the == memcmp
67 PortableVertexDeclaration() { memset(this, 0, sizeof(*this)); }
68
69 inline bool operator<(const PortableVertexDeclaration& b) const
70 {
71 return memcmp(this, &b, sizeof(PortableVertexDeclaration)) < 0;
72 }
73 inline bool operator==(const PortableVertexDeclaration& b) const
74 {
75 return memcmp(this, &b, sizeof(PortableVertexDeclaration)) == 0;
76 }
77 };
78
79 static_assert(std::is_trivially_copyable_v<PortableVertexDeclaration>,
80 "Make sure we can memset-initialize");
81
82 template <>
83 struct std::hash<PortableVertexDeclaration>
84 {
85 // Implementation from Wikipedia.
86 template <typename T>
87 static u32 Fletcher32(const T& data)
88 {
89 static_assert(sizeof(T) % sizeof(u16) == 0);
90
91 auto buf = reinterpret_cast<const u16*>(&data);
92 size_t len = sizeof(T) / sizeof(u16);
93 u32 sum1 = 0xffff, sum2 = 0xffff;
94
95 while (len)
96 {
97 size_t tlen = len > 360 ? 360 : len;
98 len -= tlen;
99
100 do
101 {
102 sum1 += *buf++;
103 sum2 += sum1;
104 } while (--tlen);
105
106 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
107 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
108 }
109
110 // Second reduction step to reduce sums to 16 bits
111 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
112 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
113 return (sum2 << 16 | sum1);
114 }
115 size_t operator()(const PortableVertexDeclaration& decl) const noexcept
116 {
117 return Fletcher32(decl);
118 }
119 };
120
121 // The implementation of this class is specific for GL/DX, so NativeVertexFormat.cpp
122 // is in the respective backend, not here in VideoCommon.
123
124 // Note that this class can't just invent arbitrary vertex formats out of its input -
125 // all the data loading code must always be made compatible.
126 class NativeVertexFormat
127 {
128 public:
129 NativeVertexFormat(const PortableVertexDeclaration& vtx_decl) : m_decl(vtx_decl) {}
130 virtual ~NativeVertexFormat() {}
131
132 NativeVertexFormat(const NativeVertexFormat&) = delete;
133 NativeVertexFormat& operator=(const NativeVertexFormat&) = delete;
134 NativeVertexFormat(NativeVertexFormat&&) = default;
135 NativeVertexFormat& operator=(NativeVertexFormat&&) = default;
136
137 u32 GetVertexStride() const { return m_decl.stride; }
138 const PortableVertexDeclaration& GetVertexDeclaration() const { return m_decl; }
139
140 protected:
141 PortableVertexDeclaration m_decl;
142 };