6
6
7
7
#include < cstring>
8
8
#include < limits>
9
+ #include < type_traits>
9
10
10
11
#include " impeller/scene/importer/conversions.h"
11
12
#include " impeller/scene/importer/scene_flatbuffers.h"
@@ -48,13 +49,20 @@ void VerticesBuilder::WriteFBVertices(std::vector<fb::Vertex>& vertices) const {
48
49
}
49
50
}
50
51
51
- template <typename SourceType, size_t Divisor>
52
- void WriteComponentsAsScalars (void * destination,
53
- const void * source,
54
- size_t component_count) {
52
+ // / @brief Reads a contiguous sequence of numeric components from `source` and
53
+ // / writes them to `destination` buffer as 32bit floats. Signed
54
+ // / SourceTypes convert to a range of -1 to 1, and unsigned SourceTypes
55
+ // / convert to a range of 0 to 1.
56
+ template <typename SourceType>
57
+ static void WriteComponentsAsScalars (void * destination,
58
+ const void * source,
59
+ size_t component_count) {
60
+ constexpr size_t divisor = std::is_integral_v<SourceType>
61
+ ? std::numeric_limits<SourceType>::max ()
62
+ : 1 ;
55
63
for (size_t i = 0 ; i < component_count; i++) {
56
64
const SourceType* s = reinterpret_cast <const SourceType*>(source) + i;
57
- Scalar v = static_cast <Scalar>(*s) / static_cast <Scalar>(Divisor );
65
+ Scalar v = static_cast <Scalar>(*s) / static_cast <Scalar>(divisor );
58
66
Scalar* dest = reinterpret_cast <Scalar*>(destination) + i;
59
67
*dest = v;
60
68
}
@@ -66,18 +74,19 @@ static std::map<
66
74
void (void * destination, const void * source, size_t component_count)>>
67
75
kAttributeWriters = {
68
76
{VerticesBuilder::ComponentType::kSignedByte ,
69
- WriteComponentsAsScalars<int8_t , std::numeric_limits< int8_t >:: max () >},
77
+ WriteComponentsAsScalars<int8_t >},
70
78
{VerticesBuilder::ComponentType::kUnsignedByte ,
71
- WriteComponentsAsScalars<uint8_t , std::numeric_limits< uint8_t >:: max () >},
79
+ WriteComponentsAsScalars<uint8_t >},
72
80
{VerticesBuilder::ComponentType::kSignedShort ,
73
- WriteComponentsAsScalars<int16_t , std::numeric_limits< int16_t >:: max () >},
81
+ WriteComponentsAsScalars<int16_t >},
74
82
{VerticesBuilder::ComponentType::kUnsignedShort ,
75
- WriteComponentsAsScalars<uint16_t , std::numeric_limits< uint16_t >:: max () >},
83
+ WriteComponentsAsScalars<uint16_t >},
76
84
{VerticesBuilder::ComponentType::kSignedInt ,
77
- WriteComponentsAsScalars<int32_t , std::numeric_limits< int32_t >:: max () >},
85
+ WriteComponentsAsScalars<int32_t >},
78
86
{VerticesBuilder::ComponentType::kUnsignedInt ,
79
- WriteComponentsAsScalars<uint32_t , std::numeric_limits<uint32_t >::max ()>},
80
- {VerticesBuilder::ComponentType::kFloat , WriteComponentsAsScalars<float , 1 >},
87
+ WriteComponentsAsScalars<uint32_t >},
88
+ {VerticesBuilder::ComponentType::kFloat ,
89
+ WriteComponentsAsScalars<float >},
81
90
};
82
91
83
92
void VerticesBuilder::SetAttributeFromBuffer (Attribute attribute,
0 commit comments