Skip to content

Commit

Permalink
Change: Make Buffer data std::byte instead of std::uint8_t
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Jun 19, 2024
1 parent d062687 commit 00ca410
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
5 changes: 3 additions & 2 deletions examples/gl_viewer/gl_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ bool loadImage(Viewer* viewer, fastgltf::Image& image) {
},
[&](fastgltf::sources::Array& vector) {
int width, height, nrChannels;
unsigned char *data = stbi_load_from_memory(vector.bytes.data(), static_cast<int>(vector.bytes.size()), &width, &height, &nrChannels, 4);
unsigned char *data = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(vector.bytes.data()), static_cast<int>(vector.bytes.size()), &width, &height, &nrChannels, 4);
glTextureStorage2D(texture, getLevelCount(width, height), GL_RGBA8, width, height);
glTextureSubImage2D(texture, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
Expand All @@ -545,7 +545,8 @@ bool loadImage(Viewer* viewer, fastgltf::Image& image) {
[](auto& arg) {},
[&](fastgltf::sources::Array& vector) {
int width, height, nrChannels;
unsigned char* data = stbi_load_from_memory(vector.bytes.data() + bufferView.byteOffset, static_cast<int>(bufferView.byteLength), &width, &height, &nrChannels, 4);
unsigned char* data = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(vector.bytes.data() + bufferView.byteOffset),
static_cast<int>(bufferView.byteLength), &width, &height, &nrChannels, 4);
glTextureStorage2D(texture, getLevelCount(width, height), GL_RGBA8, width, height);
glTextureSubImage2D(texture, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
Expand Down
4 changes: 2 additions & 2 deletions include/fastgltf/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1520,13 +1520,13 @@ namespace fastgltf {
};

FASTGLTF_EXPORT struct Array {
StaticVector<std::uint8_t> bytes;
StaticVector<std::byte> bytes;
MimeType mimeType = MimeType::None;
};

/** @note This type is not used by the fastgltf parser and is only used for exporting. Use sources::Array instead when importing intead. */
FASTGLTF_EXPORT struct Vector {
std::vector<std::uint8_t> bytes;
std::vector<std::byte> bytes;
MimeType mimeType = MimeType::None;
};

Expand Down
36 changes: 18 additions & 18 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,19 +725,19 @@ fg::Expected<fg::DataSource> fg::Parser::decodeDataUri(URIView& uri) const noexc
}
}

// Decode the base64 data into a traditional vector
auto padding = base64::getPadding(encodedData);
fg::StaticVector<std::uint8_t> uriData(base64::getOutputSize(encodedData.size(), padding));
if (config.decodeCallback != nullptr) {
config.decodeCallback(encodedData, uriData.data(), padding, uriData.size(), config.userPointer);
} else {
base64::decode_inplace(encodedData, uriData.data(), padding);
}
// Decode the base64 data into a traditional vector
auto padding = base64::getPadding(encodedData);
fg::StaticVector<std::byte> uriData(base64::getOutputSize(encodedData.size(), padding));
if (config.decodeCallback != nullptr) {
config.decodeCallback(encodedData, reinterpret_cast<std::uint8_t*>(uriData.data()), padding, uriData.size(), config.userPointer);
} else {
base64::decode_inplace(encodedData, reinterpret_cast<std::uint8_t*>(uriData.data()), padding);
}

sources::Array source {
sources::Array source {
std::move(uriData),
getMimeTypeFromString(mime),
};
};
return { std::move(source) };
}

Expand Down Expand Up @@ -856,19 +856,19 @@ namespace fastgltf {
}
}

std::pair<StaticVector<std::uint8_t>, ComponentType> writeIndices(PrimitiveType type, std::size_t indexCount, std::size_t primitiveCount) {
std::pair<StaticVector<std::byte>, ComponentType> writeIndices(PrimitiveType type, std::size_t indexCount, std::size_t primitiveCount) {
if (indexCount < 255) {
StaticVector<std::uint8_t> generatedIndices(indexCount);
span<std::uint8_t> indices(generatedIndices.data(), generatedIndices.size());
StaticVector<std::byte> generatedIndices(indexCount * sizeof(std::uint8_t));
span<std::uint8_t> indices(reinterpret_cast<std::uint8_t*>(generatedIndices.data()), generatedIndices.size() / sizeof(std::uint8_t));
writeIndices(type, indices, primitiveCount);
return std::make_pair(generatedIndices, ComponentType::UnsignedByte);
} else if (indexCount < 65535) {
StaticVector<std::uint8_t> generatedIndices(indexCount * sizeof(std::uint16_t));
StaticVector<std::byte> generatedIndices(indexCount * sizeof(std::uint16_t));
span<std::uint16_t> indices(reinterpret_cast<std::uint16_t*>(generatedIndices.data()), generatedIndices.size() / sizeof(std::uint16_t));
writeIndices(type, indices, primitiveCount);
return std::make_pair(generatedIndices, ComponentType::UnsignedShort);
} else {
StaticVector<std::uint8_t> generatedIndices(indexCount * sizeof(std::uint32_t));
StaticVector<std::byte> generatedIndices(indexCount * sizeof(std::uint32_t));
span<std::uint32_t> indices(reinterpret_cast<std::uint32_t*>(generatedIndices.data()), generatedIndices.size() / sizeof(std::uint32_t));
writeIndices(type, indices, primitiveCount);
return std::make_pair(generatedIndices, ComponentType::UnsignedInt);
Expand Down Expand Up @@ -3891,12 +3891,12 @@ fg::Expected<fg::Asset> fg::Parser::loadGltfBinary(GltfDataGetter& data, fs::pat
glbBuffer = sources::CustomBuffer{info.customId, MimeType::None};
}
} else {
StaticVector<std::uint8_t> binaryData(binaryChunk.chunkLength);
StaticVector<std::byte> binaryData(binaryChunk.chunkLength);
data.read(binaryData.data(), binaryChunk.chunkLength);

sources::Array vectorData = {
std::move(binaryData),
MimeType::GltfBuffer,
std::move(binaryData),
MimeType::GltfBuffer,
};
glbBuffer = std::move(vectorData);
}
Expand Down
2 changes: 1 addition & 1 deletion src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ fg::Expected<fg::DataSource> fg::Parser::loadFileFromUri(URIView& uri) const noe
}
}

StaticVector<std::uint8_t> data(static_cast<std::size_t>(length));
StaticVector<std::byte> data(static_cast<std::size_t>(length));
file.read(reinterpret_cast<char*>(data.data()), length);
sources::Array arraySource {
std::move(data),
Expand Down

0 comments on commit 00ca410

Please sign in to comment.