From 06170a25f7f0fb0fb4eecfd41310a4c9a2f57b44 Mon Sep 17 00:00:00 2001 From: Powei Feng Date: Fri, 6 Jun 2025 11:20:05 -0700 Subject: [PATCH] Fix PBD size check in Texture (#8826) Bug found when running ./samples/heightfield and press add pixel buffer padding. --- filament/src/details/Texture.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/filament/src/details/Texture.cpp b/filament/src/details/Texture.cpp index 49d4945bd2..2f4bbc2772 100644 --- a/filament/src/details/Texture.cpp +++ b/filament/src/details/Texture.cpp @@ -432,10 +432,19 @@ void FTexture::setImage(FEngine& engine, size_t const level, size_t const stride = p.stride ? p.stride : width; size_t const bpp = PBD::computeDataSize(p.format, p.type, 1, 1, 1); size_t const bpr = PBD::computeDataSize(p.format, p.type, stride, 1, p.alignment); - size_t const bpl = bpr * height; // TODO: PBD should have a "layer stride" + size_t const pbdWidth = stride; + // TODO: PBD should have a "layer stride" (using "depth" as a substitute). + size_t const pbdDepth = depth; + size_t const pbdHeight = p.size / bpr / pbdDepth; + assert_invariant( + p.size % bpr == 0 && + (p.size / bpr) % pbdDepth == 0); + // TODO: PBD should have a p.depth (# layers to skip) - FILAMENT_CHECK_PRECONDITION(bpp * p.left + bpr * p.top + bpl * (0 + depth) <= p.size) - << "buffer overflow: (size=" << size_t(p.size) << ", stride=" << size_t(p.stride) + FILAMENT_CHECK_PRECONDITION( + bpp * (pbdWidth - p.left) * (pbdHeight - p.top) * (pbdDepth - 0) >= + bpp * (width - xoffset) * (height - yoffset) * (depth - zoffset)) + << "buffer overflow: (size=" << size_t(p.size) << ", stride=" << size_t(p.stride) << ", left=" << unsigned(p.left) << ", top=" << unsigned(p.top) << ") smaller than specified region " "{{"