Loading...
Searching...
No Matches
BufferToGeometryCommon.hpp
1#pragma once
2
3#include "GeometryToBufferStrategies.hpp"
4
5#include <ossia/dataflow/geometry_port.hpp>
6
7#include <Threedim/Debug.hpp>
8
9#include <cstring>
10
11namespace Threedim
12{
13
14// Matches QRhiVertexInputAttribute::Format
15enum AttributeFormat
16{
17 Float4,
18 Float3,
19 Float2,
20 Float,
21 UNormByte4,
22 UNormByte2,
23 UNormByte,
24 UInt4,
25 UInt2,
26 UInt,
27 SInt4,
28 SInt2,
29 SInt,
30 Half4,
31 Half3,
32 Half2,
33 Half,
34 UShort4,
35 UShort2,
36 UShort,
37 SShort4,
38 SShort2,
39 SShort,
40};
41
42enum PrimitiveTopology
43{
44 Triangles,
45 TriangleStrip,
46 TriangleFan,
47 Lines,
48 LineStrip,
49 Points
50};
51
52enum CullMode
53{
54 None,
55 Front,
56 Back
57};
58
59enum FrontFace
60{
61 CounterClockwise,
62 Clockwise
63};
64
65enum IndexFormat
66{
67 UInt16,
68 UInt32
69};
70
71namespace
72{
73
74[[nodiscard]] constexpr halp::attribute_format toHalpFormat(AttributeFormat fmt) noexcept
75{
76 // A real switch, NOT a static_cast: the local enum mirrors
77 // QRhiVertexInputAttribute::Format, which has no 3-component
78 // int/short entries, while halp::attribute_format interleaves
79 // uint3/sint3/ushort3/sshort3 into the sequence. The two enums agree
80 // only for values 0..7 (Float4..UInt4); from UInt2 upward a cast
81 // lands every format on the wrong halp entry (UInt2 -> uint3,
82 // SInt4 -> uint1, UShort4 -> half2, ...).
83 switch(fmt)
84 {
85 case AttributeFormat::Float4: return halp::attribute_format::float4;
86 case AttributeFormat::Float3: return halp::attribute_format::float3;
87 case AttributeFormat::Float2: return halp::attribute_format::float2;
88 case AttributeFormat::Float: return halp::attribute_format::float1;
89 case AttributeFormat::UNormByte4: return halp::attribute_format::unormbyte4;
90 case AttributeFormat::UNormByte2: return halp::attribute_format::unormbyte2;
91 case AttributeFormat::UNormByte: return halp::attribute_format::unormbyte1;
92 case AttributeFormat::UInt4: return halp::attribute_format::uint4;
93 case AttributeFormat::UInt2: return halp::attribute_format::uint2;
94 case AttributeFormat::UInt: return halp::attribute_format::uint1;
95 case AttributeFormat::SInt4: return halp::attribute_format::sint4;
96 case AttributeFormat::SInt2: return halp::attribute_format::sint2;
97 case AttributeFormat::SInt: return halp::attribute_format::sint1;
98 case AttributeFormat::Half4: return halp::attribute_format::half4;
99 case AttributeFormat::Half3: return halp::attribute_format::half3;
100 case AttributeFormat::Half2: return halp::attribute_format::half2;
101 case AttributeFormat::Half: return halp::attribute_format::half1;
102 case AttributeFormat::UShort4: return halp::attribute_format::ushort4;
103 case AttributeFormat::UShort2: return halp::attribute_format::ushort2;
104 case AttributeFormat::UShort: return halp::attribute_format::ushort1;
105 case AttributeFormat::SShort4: return halp::attribute_format::sshort4;
106 case AttributeFormat::SShort2: return halp::attribute_format::sshort2;
107 case AttributeFormat::SShort: return halp::attribute_format::sshort1;
108 }
109 return halp::attribute_format::float4;
110}
111
112[[nodiscard]] constexpr int32_t attributeFormatSize(AttributeFormat fmt) noexcept
113{
114 return Threedim::attributeFormatSize(toHalpFormat(fmt));
115}
116
117[[nodiscard]] constexpr halp::primitive_topology
118toHalpTopology(PrimitiveTopology t) noexcept
119{
120 switch(t)
121 {
122 case PrimitiveTopology::Triangles:
123 return halp::primitive_topology::triangles;
124 case PrimitiveTopology::TriangleStrip:
125 return halp::primitive_topology::triangle_strip;
126 case PrimitiveTopology::TriangleFan:
127 return halp::primitive_topology::triangle_fan;
128 case PrimitiveTopology::Lines:
129 return halp::primitive_topology::lines;
130 case PrimitiveTopology::LineStrip:
131 return halp::primitive_topology::line_strip;
132 case PrimitiveTopology::Points:
133 return halp::primitive_topology::points;
134 }
135 return halp::primitive_topology::triangles;
136}
137
138[[nodiscard]] constexpr halp::cull_mode toHalpCullMode(CullMode c) noexcept
139{
140 switch(c)
141 {
142 case CullMode::None:
143 return halp::cull_mode::none;
144 case CullMode::Front:
145 return halp::cull_mode::front;
146 case CullMode::Back:
147 return halp::cull_mode::back;
148 }
149 return halp::cull_mode::none;
150}
151
152[[nodiscard]] constexpr halp::front_face toHalpFrontFace(FrontFace f) noexcept
153{
154 switch(f)
155 {
156 case FrontFace::CounterClockwise:
157 return halp::front_face::counter_clockwise;
158 case FrontFace::Clockwise:
159 return halp::front_face::clockwise;
160 }
161 return halp::front_face::counter_clockwise;
162}
163
164[[nodiscard]] constexpr halp::index_format toHalpIndexFormat(IndexFormat f) noexcept
165{
166 switch(f)
167 {
168 case IndexFormat::UInt16:
169 return halp::index_format::uint16;
170 case IndexFormat::UInt32:
171 return halp::index_format::uint32;
172 }
173 return halp::index_format::uint32;
174}
175
176[[nodiscard]] constexpr halp::binding_classification
177toHalpClassification(bool instanced) noexcept
178{
179 return instanced ? halp::binding_classification::per_instance
180 : halp::binding_classification::per_vertex;
181}
182
183// Resolve a user-provided semantic name to ossia::attribute_semantic.
184// Uses case-insensitive name_to_semantic; if unrecognized, returns custom.
185[[nodiscard]] inline int resolveSemanticFromName(const std::string& name) noexcept
186{
187 if(name.empty())
188 return static_cast<int>(ossia::attribute_semantic::custom);
189
190 auto sem = ossia::name_to_semantic(name);
191 return static_cast<int>(sem);
192}
193
194} // anonymous namespace
195
196}