8
8
#include < limits>
9
9
#include < type_traits>
10
10
11
+ #include " flutter/fml/logging.h"
11
12
#include " impeller/scene/importer/conversions.h"
12
13
#include " impeller/scene/importer/scene_flatbuffers.h"
13
14
@@ -17,29 +18,6 @@ namespace importer {
17
18
18
19
VerticesBuilder::VerticesBuilder () = default ;
19
20
20
- std::map<VerticesBuilder::Attribute, VerticesBuilder::AttributeProperties>
21
- VerticesBuilder::kAttributes = {
22
- {VerticesBuilder::Attribute::kPosition ,
23
- {.offset_bytes = offsetof (Vertex, position),
24
- .size_bytes = sizeof (Vertex::position),
25
- .component_count = 3 }},
26
- {VerticesBuilder::Attribute::kNormal ,
27
- {.offset_bytes = offsetof (Vertex, normal ),
28
- .size_bytes = sizeof (Vertex::normal ),
29
- .component_count = 3 }},
30
- {VerticesBuilder::Attribute::kTangent ,
31
- {.offset_bytes = offsetof (Vertex, tangent),
32
- .size_bytes = sizeof (Vertex::tangent),
33
- .component_count = 4 }},
34
- {VerticesBuilder::Attribute::kTextureCoords ,
35
- {.offset_bytes = offsetof (Vertex, texture_coords),
36
- .size_bytes = sizeof (Vertex::texture_coords),
37
- .component_count = 2 }},
38
- {VerticesBuilder::Attribute::kColor ,
39
- {.offset_bytes = offsetof (Vertex, color),
40
- .size_bytes = sizeof (Vertex::color),
41
- .component_count = 4 }}};
42
-
43
21
void VerticesBuilder::WriteFBVertices (std::vector<fb::Vertex>& vertices) const {
44
22
vertices.resize (0 );
45
23
for (auto & v : vertices_) {
@@ -49,64 +27,119 @@ void VerticesBuilder::WriteFBVertices(std::vector<fb::Vertex>& vertices) const {
49
27
}
50
28
}
51
29
52
- // / @brief Reads a contiguous sequence of numeric components from `source` and
53
- // / writes them to `destination` as 32bit floats. Signed SourceTypes
54
- // / convert to a range of -1 to 1, and unsigned SourceTypes convert to a
55
- // / range of 0 to 1.
30
+ // / @brief Reads a numeric component from `source` and returns a 32bit float.
31
+ // / Signed SourceTypes convert to a range of -1 to 1, and unsigned
32
+ // / SourceTypes convert to a range of 0 to 1.
56
33
template <typename SourceType>
57
- static void WriteComponentsAsScalars (void * destination,
58
- const void * source,
59
- size_t component_count) {
34
+ static Scalar ToNormalizedScalar (const void * source, size_t index) {
60
35
constexpr SourceType divisor = std::is_integral_v<SourceType>
61
36
? std::numeric_limits<SourceType>::max ()
62
37
: 1 ;
63
- for (size_t i = 0 ; i < component_count; i++) {
64
- const SourceType* s = reinterpret_cast <const SourceType*>(source) + i;
65
- Scalar v = static_cast <Scalar>(*s) / static_cast <Scalar>(divisor);
66
- Scalar* dest = reinterpret_cast <Scalar*>(destination) + i;
67
- *dest = v;
38
+ const SourceType* s = reinterpret_cast <const SourceType*>(source) + index ;
39
+ return static_cast <Scalar>(*s) / static_cast <Scalar>(divisor);
40
+ }
41
+
42
+ // / @brief A ComponentWriter which simply converts all of an attribute's
43
+ // / components to normalized scalar form.
44
+ static void PassthroughAttributeWriter (
45
+ Scalar* destination,
46
+ const void * source,
47
+ const VerticesBuilder::ComponentProperties& component,
48
+ const VerticesBuilder::AttributeProperties& attribute) {
49
+ FML_DCHECK (attribute.size_bytes ==
50
+ attribute.component_count * sizeof (Scalar));
51
+ for (size_t component_i = 0 ; component_i < attribute.component_count ;
52
+ component_i++) {
53
+ *(destination + component_i) = component.convert_proc (source, component_i);
68
54
}
69
55
}
70
56
71
- static std::map<
72
- VerticesBuilder::ComponentType,
73
- std::function<
74
- void (void * destination, const void * source, size_t component_count)>>
75
- kAttributeWriters = {
57
+ // / @brief A ComponentWriter which converts a Vector3 position from
58
+ // / right-handed GLTF space to left-handed Impeller space.
59
+ static void PositionAttributeWriter (
60
+ Scalar* destination,
61
+ const void * source,
62
+ const VerticesBuilder::ComponentProperties& component,
63
+ const VerticesBuilder::AttributeProperties& attribute) {
64
+ FML_DCHECK (attribute.component_count == 3 );
65
+ *(destination + 0 ) = component.convert_proc (source, 0 );
66
+ *(destination + 1 ) = component.convert_proc (source, 1 );
67
+ *(destination + 2 ) = -component.convert_proc (source, 2 );
68
+ }
69
+
70
+ std::map<VerticesBuilder::AttributeType, VerticesBuilder::AttributeProperties>
71
+ VerticesBuilder::kAttributeTypes = {
72
+ {VerticesBuilder::AttributeType::kPosition ,
73
+ {.offset_bytes = offsetof (Vertex, position),
74
+ .size_bytes = sizeof (Vertex::position),
75
+ .component_count = 3 ,
76
+ .write_proc = PositionAttributeWriter}},
77
+ {VerticesBuilder::AttributeType::kNormal ,
78
+ {.offset_bytes = offsetof (Vertex, normal ),
79
+ .size_bytes = sizeof (Vertex::normal ),
80
+ .component_count = 3 ,
81
+ .write_proc = PassthroughAttributeWriter}},
82
+ {VerticesBuilder::AttributeType::kTangent ,
83
+ {.offset_bytes = offsetof (Vertex, tangent),
84
+ .size_bytes = sizeof (Vertex::tangent),
85
+ .component_count = 4 ,
86
+ .write_proc = PassthroughAttributeWriter}},
87
+ {VerticesBuilder::AttributeType::kTextureCoords ,
88
+ {.offset_bytes = offsetof (Vertex, texture_coords),
89
+ .size_bytes = sizeof (Vertex::texture_coords),
90
+ .component_count = 2 ,
91
+ .write_proc = PassthroughAttributeWriter}},
92
+ {VerticesBuilder::AttributeType::kColor ,
93
+ {.offset_bytes = offsetof (Vertex, color),
94
+ .size_bytes = sizeof (Vertex::color),
95
+ .component_count = 4 ,
96
+ .write_proc = PassthroughAttributeWriter}}};
97
+
98
+ static std::map<VerticesBuilder::ComponentType,
99
+ VerticesBuilder::ComponentProperties>
100
+ kComponentTypes = {
76
101
{VerticesBuilder::ComponentType::kSignedByte ,
77
- WriteComponentsAsScalars<int8_t >},
102
+ {.size_bytes = sizeof (int8_t ),
103
+ .convert_proc = ToNormalizedScalar<int8_t >}},
78
104
{VerticesBuilder::ComponentType::kUnsignedByte ,
79
- WriteComponentsAsScalars<uint8_t >},
105
+ {.size_bytes = sizeof (int8_t ),
106
+ .convert_proc = ToNormalizedScalar<uint8_t >}},
80
107
{VerticesBuilder::ComponentType::kSignedShort ,
81
- WriteComponentsAsScalars<int16_t >},
108
+ {.size_bytes = sizeof (int16_t ),
109
+ .convert_proc = ToNormalizedScalar<int16_t >}},
82
110
{VerticesBuilder::ComponentType::kUnsignedShort ,
83
- WriteComponentsAsScalars<uint16_t >},
111
+ {.size_bytes = sizeof (int16_t ),
112
+ .convert_proc = ToNormalizedScalar<uint16_t >}},
84
113
{VerticesBuilder::ComponentType::kSignedInt ,
85
- WriteComponentsAsScalars<int32_t >},
114
+ {.size_bytes = sizeof (int32_t ),
115
+ .convert_proc = ToNormalizedScalar<int32_t >}},
86
116
{VerticesBuilder::ComponentType::kUnsignedInt ,
87
- WriteComponentsAsScalars<uint32_t >},
117
+ {.size_bytes = sizeof (int32_t ),
118
+ .convert_proc = ToNormalizedScalar<uint32_t >}},
88
119
{VerticesBuilder::ComponentType::kFloat ,
89
- WriteComponentsAsScalars<float >},
120
+ {.size_bytes = sizeof (float ),
121
+ .convert_proc = ToNormalizedScalar<float >}},
90
122
};
91
123
92
- void VerticesBuilder::SetAttributeFromBuffer (Attribute attribute,
124
+ void VerticesBuilder::SetAttributeFromBuffer (AttributeType attribute,
93
125
ComponentType component_type,
94
126
const void * buffer_start,
95
- size_t stride_bytes ,
96
- size_t count ) {
97
- if (count > vertices_.size ()) {
98
- vertices_.resize (count , Vertex ());
127
+ size_t attribute_stride_bytes ,
128
+ size_t attribute_count ) {
129
+ if (attribute_count > vertices_.size ()) {
130
+ vertices_.resize (attribute_count , Vertex ());
99
131
}
100
132
101
- const auto & properties = kAttributes [attribute ];
102
- const auto & writer = kAttributeWriters [component_type ];
103
- for (size_t i = 0 ; i < count ; i++) {
104
- const char * source =
105
- reinterpret_cast < const char *>(buffer_start) + stride_bytes * i;
106
- char * destination =
107
- reinterpret_cast < char *>(&vertices_. data ()[i]) + properties .offset_bytes ;
133
+ const ComponentProperties& component_props = kComponentTypes [component_type ];
134
+ const AttributeProperties& attribute_props = kAttributeTypes [attribute ];
135
+ for (size_t i = 0 ; i < attribute_count ; i++) {
136
+ const uint8_t * source = reinterpret_cast < const uint8_t *>(buffer_start) +
137
+ attribute_stride_bytes * i;
138
+ uint8_t * destination = reinterpret_cast < uint8_t *>(&vertices_. data ()[i]) +
139
+ attribute_props .offset_bytes ;
108
140
109
- writer (destination, source, properties.component_count );
141
+ attribute_props.write_proc (reinterpret_cast <Scalar*>(destination), source,
142
+ component_props, attribute_props);
110
143
}
111
144
}
112
145
0 commit comments