Support both 3 and 4 channel images and use a safer way to assign IDs to images.
PiperOrigin-RevId: 841727607 Change-Id: I319db4291ecee27f4ceb7b7e952c6f68e7f8b96d
This commit is contained in:
committed by
Copybara-Service
parent
96e376451c
commit
e238b65843
@@ -96,10 +96,16 @@ void GuiView::ResetRenderable() {
|
|||||||
|
|
||||||
uintptr_t GuiView::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
|
uintptr_t GuiView::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
|
||||||
int width, int height, int bpp) {
|
int width, int height, int bpp) {
|
||||||
if (bpp != 4) {
|
if (bpp != 4 && bpp != 3) {
|
||||||
mju_error("Unsupported image bpp. Got %d, wanted 4", bpp);
|
mju_error("Unsupported image bpp. Got %d, wanted 3 or 4", bpp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto internal_format =
|
||||||
|
bpp == 4 ? filament::Texture::InternalFormat::RGBA8
|
||||||
|
: filament::Texture::InternalFormat::RGB8;
|
||||||
|
const auto texture_format = bpp == 4 ? filament::Texture::Format::RGBA
|
||||||
|
: filament::Texture::Format::RGB;
|
||||||
|
|
||||||
filament::Engine* engine = object_mgr_->GetEngine();
|
filament::Engine* engine = object_mgr_->GetEngine();
|
||||||
|
|
||||||
filament::Texture* texture = nullptr;
|
filament::Texture* texture = nullptr;
|
||||||
@@ -108,10 +114,10 @@ uintptr_t GuiView::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
|
|||||||
.width(width)
|
.width(width)
|
||||||
.height(height)
|
.height(height)
|
||||||
.levels(1)
|
.levels(1)
|
||||||
.format(filament::Texture::InternalFormat::RGBA8)
|
.format(internal_format)
|
||||||
.sampler(filament::Texture::Sampler::SAMPLER_2D)
|
.sampler(filament::Texture::Sampler::SAMPLER_2D)
|
||||||
.build(*engine);
|
.build(*engine);
|
||||||
tex_id = reinterpret_cast<uintptr_t>(texture);
|
tex_id = textures_.size() + 1;
|
||||||
textures_[tex_id] = texture;
|
textures_[tex_id] = texture;
|
||||||
} else {
|
} else {
|
||||||
auto iter = textures_.find(tex_id);
|
auto iter = textures_.find(tex_id);
|
||||||
@@ -130,9 +136,9 @@ uintptr_t GuiView::UploadImage(uintptr_t tex_id, const uint8_t* pixels,
|
|||||||
auto* ptr = reinterpret_cast<std::byte*>(user);
|
auto* ptr = reinterpret_cast<std::byte*>(user);
|
||||||
delete[] ptr;
|
delete[] ptr;
|
||||||
};
|
};
|
||||||
filament::Texture::PixelBufferDescriptor pb(
|
filament::Texture::PixelBufferDescriptor pb(bytes, num_bytes, texture_format,
|
||||||
bytes, num_bytes, filament::Texture::Format::RGBA,
|
filament::Texture::Type::UBYTE,
|
||||||
filament::Texture::Type::UBYTE, callback);
|
callback);
|
||||||
texture->setImage(*engine, 0, std::move(pb));
|
texture->setImage(*engine, 0, std::move(pb));
|
||||||
return tex_id;
|
return tex_id;
|
||||||
}
|
}
|
||||||
@@ -152,7 +158,7 @@ void GuiView::CreateTexture(ImTextureData* data) {
|
|||||||
.sampler(filament::Texture::Sampler::SAMPLER_2D)
|
.sampler(filament::Texture::Sampler::SAMPLER_2D)
|
||||||
.build(*engine);
|
.build(*engine);
|
||||||
|
|
||||||
const uintptr_t tex_id = reinterpret_cast<uintptr_t>(texture);
|
const uintptr_t tex_id = textures_.size() + 1;
|
||||||
textures_[tex_id] = texture;
|
textures_[tex_id] = texture;
|
||||||
data->SetTexID((ImTextureID)tex_id);
|
data->SetTexID((ImTextureID)tex_id);
|
||||||
UpdateTexture(data);
|
UpdateTexture(data);
|
||||||
|
|||||||
Reference in New Issue
Block a user