Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(render): uploads sized a read of caller pixels with the destination row

yhh90305132769c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index aaf6e8285..6789017a9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -96,6 +96,7 @@ set(ESENGINE_HEADERS
src/esengine/renderer/draw/BlendMode.hpp
src/esengine/renderer/draw/CustomGeometry.hpp
src/esengine/renderer/rhi/GfxEnums.hpp
+ src/esengine/renderer/rhi/PixelUpload.hpp
src/esengine/renderer/rhi/GfxDevice.hpp
src/esengine/renderer/rhi/GLDevice.hpp
src/esengine/renderer/rhi/TransientBufferPool.hpp
diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cpp
index 12978c09f..d4d0c66cd 100644
--- a/src/esengine/renderer/rhi/GLDevice.cpp
+++ b/src/esengine/renderer/rhi/GLDevice.cpp
@@ -182,6 +182,18 @@ GLPixelFormatInfo toGLPixelFormat(GfxPixelFormat fmt) {
}
}
+/// GL reads each source row padded up to UNPACK_ALIGNMENT (4 by default) while
+/// engine uploads are tightly packed, so a row whose byte count is not 4-aligned
+/// has to drop it to 1 — otherwise GL reads past the end of the last row.
+struct TightRowScope {
+ bool tightened;
+ TightRowScope(GfxPixelFormat fmt, u32 width)
+ : tightened((static_cast<u64>(width) * gfxBytesPerPixel(fmt)) % 4u != 0) {
+ if (tightened) glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ }
+ ~TightRowScope() { if (tightened) glPixelStorei(GL_UNPACK_ALIGNMENT, 4); }
+};
+
GLenum toGLCompressedFormat(GfxCompressedFormat fmt) {
switch (fmt) {
case GfxCompressedFormat::ETC2_RGB8: return GL_COMPRESSED_RGB8_ETC2;
@@ -883,6 +895,7 @@ TextureHandle GLDevice::createTexture(const TextureDesc& desc, const void* pixel
auto gl = toGLPixelFormat(desc.format);
bindTextureForEdit(id);
+ const TightRowScope rows(desc.format, desc.width);
if (pixels && desc.flipY) glPixelStorei(GL_UNPACK_FLIP_Y_WEBGL, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(gl.internalFormat),
static_cast<GLsizei>(desc.width), static_cast<GLsizei>(desc.height),
@@ -919,7 +932,7 @@ TextureHandle GLDevice::createCompressedTexture(const TextureDesc& desc, GfxComp
const u32 blocksX = (lw + bi.blockWidth - 1) / bi.blockWidth;
const u32 blocksY = (lh + bi.blockHeight - 1) / bi.blockHeight;
const u32 levelBytes = blocksX * blocksY * bi.bytesPerBlock;
- if (ptr + levelBytes > end) break;
+ if (levelBytes > static_cast<usize>(end - ptr)) break; // truncated pyramid
glCompressedTexImage2D(GL_TEXTURE_2D, static_cast<GLint>(level), glFmt,
static_cast<GLsizei>(lw), static_cast<GLsizei>(lh),
0, static_cast<GLsizei>(levelBytes), ptr);
@@ -949,8 +962,10 @@ void GLDevice::deleteTexture(TextureHandle texture) {
void GLDevice::updateTexture(TextureHandle texture, i32 x, i32 y, u32 width, u32 height,
const void* pixels, bool flipY) {
auto it = texture_formats_.find(static_cast<u32>(texture));
- auto gl = toGLPixelFormat(it != texture_formats_.end() ? it->second : GfxPixelFormat::RGBA8);
+ const GfxPixelFormat fmt = it != texture_formats_.end() ? it->second : GfxPixelFormat::RGBA8;
+ auto gl = toGLPixelFormat(fmt);
bindTextureForEdit(static_cast<u32>(texture));
+ const TightRowScope rows(fmt, width);
if (flipY) glPixelStorei(GL_UNPACK_FLIP_Y_WEBGL, GL_TRUE);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y,
static_cast<GLsizei>(width), static_cast<GLsizei>(height),
diff --git a/src/esengine/renderer/rhi/GfxEnums.hpp b/src/esengine/renderer/rhi/GfxEnums.hpp
index 3cd250966..df241199f 100644
--- a/src/esengine/renderer/rhi/GfxEnums.hpp
+++ b/src/esengine/renderer/rhi/GfxEnums.hpp
@@ -175,6 +175,18 @@ enum class GfxPixelFormat : u8 {
Depth24Stencil8,
};
+/** @brief Bytes one pixel occupies in the CALLER's upload buffer — the single
+ * source both backends size a read of caller-owned pixels with. A
+ * backend's own texel size may differ (RGB8 widens to RGBA8 on WebGPU).
+ * Depth formats are attachment-only and never uploaded. */
+inline u32 gfxBytesPerPixel(GfxPixelFormat fmt) {
+ switch (fmt) {
+ case GfxPixelFormat::RGB8: return 3;
+ case GfxPixelFormat::RGBA16F: return 8;
+ default: return 4;
+ }
+}
+
// =============================================================================
// Compressed Texture Format (GPU-compressed internal formats)
// =============================================================================
diff --git a/src/esengine/renderer/rhi/PixelUpload.hpp b/src/esengine/renderer/rhi/PixelUpload.hpp
new file mode 100644
index 000000000..6b4951403
--- /dev/null
+++ b/src/esengine/renderer/rhi/PixelUpload.hpp
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file PixelUpload.hpp
+ * @brief Row arithmetic shared by the backends' texture uploads
+ * @details A backend's texel size need not match the layout the caller allocated
+ * (WebGPU has no 3-channel format, so RGB8 sources land in RGBA8
+ * textures). Sizing a read of caller-owned pixels with the DESTINATION
+ * row is an out-of-bounds read, so the conversion lives here, in one
+ * testable place, rather than inline in each backend.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the Apache License, Version 2.0.
+ */
+#pragma once
+
+#include "../../core/Types.hpp"
+
+#include <cstring>
+
+namespace esengine {
+
+/// Stage @p height rows for a texture upload: reads exactly `width * srcBpp` bytes
+/// per row from @p src, writes `width * dstBpp` per row into @p dst, bottom-up when
+/// @p reverseRows, widening 3-byte pixels to opaque RGBA when the two sizes differ.
+inline void stageTextureRows(u8* dst, const u8* src, u32 width, u32 height,
+ u32 srcBpp, u32 dstBpp, bool reverseRows) {
+ const usize srcRow = static_cast<usize>(width) * srcBpp;
+ const usize dstRow = static_cast<usize>(width) * dstBpp;
+ for (u32 row = 0; row < height; ++row) {
+ const u8* from = src + srcRow * (reverseRows ? (height - 1 - row) : row);
+ u8* to = dst + dstRow * row;
+ if (srcBpp == dstBpp) {
+ std::memcpy(to, from, srcRow);
+ continue;
+ }
+ for (u32 px = 0; px < width; ++px) {
+ std::memcpy(to + px * dstBpp, from + px * srcBpp, srcBpp);
+ to[px * dstBpp + 3] = 0xFF;
+ }
+ }
+}
+
+} // namespace esengine
diff --git a/src/esengine/renderer/rhi/Texture.cpp b/src/esengine/renderer/rhi/Texture.cpp
index 33756f3ee..c6b9c39c0 100644
--- a/src/esengine/renderer/rhi/Texture.cpp
+++ b/src/esengine/renderer/rhi/Texture.cpp
@@ -36,12 +36,7 @@ GfxPixelFormat toGfxPixelFormat(TextureFormat format) {
}
u32 bytesPerPixel(TextureFormat format) {
- switch (format) {
- case TextureFormat::RGBA16F: return 8u;
- case TextureFormat::RGBA8:
- case TextureFormat::SRGB8A8: return 4u;
- default: return 3u;
- }
+ return gfxBytesPerPixel(toGfxPixelFormat(format));
}
} // namespace
@@ -231,7 +226,8 @@ void Texture::updateSubRegion(u32 xoffset, u32 yoffset, u32 width, u32 height,
const void* data, u32 sizeBytes, bool flipY) {
// The sub-rect must lie fully inside the texture; otherwise the upload
// writes outside the allocated texture storage (GL error / undefined).
- if (xoffset + width > width_ || yoffset + height > height_) {
+ if (width > width_ || xoffset > width_ - width ||
+ height > height_ || yoffset > height_ - height) {
ES_LOG_ERROR("Texture::updateSubRegion: rect {}x{} at ({},{}) exceeds texture {}x{}; skipping",
width, height, xoffset, yoffset, width_, height_);
return;
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
index 1e3b58800..11cc5d192 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp
@@ -9,6 +9,7 @@
*/
#include "WebGPUDevice.hpp"
#include "WebGPUMappings.hpp"
+#include "../rhi/PixelUpload.hpp"
#include "../../core/Log.hpp"
#include <algorithm>
@@ -396,9 +397,8 @@ BufferHandle WebGPUDevice::createBuffer(const BufferDesc& desc, const void* init
WGPUBufferDescriptor bd{};
bd.usage = toWGPUBufferUsage(desc.usage);
- // WriteBuffer requires 4-byte multiples; keep the GL-side size contract by
- // rounding the allocation, never the caller's data.
- bd.size = (desc.size + 3u) & ~3u;
+ // Round the allocation, never the caller's data — see alignedWriteSize.
+ bd.size = webgpu::alignedWriteSize(desc.size);
WGPUBuffer buffer = wgpuDeviceCreateBuffer(device_, &bd);
if (!buffer) {
@@ -410,19 +410,19 @@ BufferHandle WebGPUDevice::createBuffer(const BufferDesc& desc, const void* init
buffers_[id] = BufferRec{buffer, desc.size, desc.usage};
if (initialData && desc.size > 0) {
- writeBufferFromStart(buffer, initialData, desc.size);
+ writeBufferPadded(buffer, 0, initialData, desc.size);
}
return BufferHandle{id};
}
-void WebGPUDevice::writeBufferFromStart(WGPUBuffer buffer, const void* data, u32 size) {
+void WebGPUDevice::writeBufferPadded(WGPUBuffer buffer, u32 offset, const void* data, u32 size) {
if (!webgpu::needsWriteStaging(size)) {
- wgpuQueueWriteBuffer(queue_, buffer, 0, data, size);
+ wgpuQueueWriteBuffer(queue_, buffer, offset, data, size);
return;
}
std::vector<u8> padded(webgpu::alignedWriteSize(size), 0);
std::memcpy(padded.data(), data, size);
- wgpuQueueWriteBuffer(queue_, buffer, 0, padded.data(), padded.size());
+ wgpuQueueWriteBuffer(queue_, buffer, offset, padded.data(), padded.size());
}
void WebGPUDevice::deleteBuffer(BufferHandle buffer) {
@@ -453,8 +453,8 @@ void WebGPUDevice::evictBindGroups(u64 id) {
void WebGPUDevice::updateBuffer(BufferHandle buffer, u32 offsetBytes, const void* data, u32 sizeBytes) {
auto it = buffers_.find(static_cast<u32>(buffer));
- if (it == buffers_.end() || !data || sizeBytes == 0) return;
- if (offsetBytes + sizeBytes > it->second.size) {
+ if (it == buffers_.end() || !it->second.buffer || !data || sizeBytes == 0) return;
+ if (!webgpu::writeFitsInBuffer(offsetBytes, sizeBytes, it->second.size)) {
ES_LOG_ERROR("WebGPUDevice::updateBuffer: range {}+{} exceeds buffer size {}",
offsetBytes, sizeBytes, it->second.size);
return;
@@ -464,20 +464,14 @@ void WebGPUDevice::updateBuffer(BufferHandle buffer, u32 offsetBytes, const void
ES_LOG_ERROR("WebGPUDevice::updateBuffer: offset {} is not 4-byte aligned", offsetBytes);
return;
}
- if (webgpu::needsWriteStaging(sizeBytes)) {
- // Only a write ending at the logical end may pad, into the slack the rounded
- // allocation already has. One ending mid-buffer would clobber the next field.
- if (offsetBytes + sizeBytes != it->second.size) {
- ES_LOG_ERROR("WebGPUDevice::updateBuffer: unaligned partial write {}+{} of {}",
- offsetBytes, sizeBytes, it->second.size);
- return;
- }
- std::vector<u8> padded(webgpu::alignedWriteSize(sizeBytes), 0);
- std::memcpy(padded.data(), data, sizeBytes);
- wgpuQueueWriteBuffer(queue_, it->second.buffer, offsetBytes, padded.data(), padded.size());
+ // Only a write ending at the logical end may pad, into the slack the rounded
+ // allocation has. One ending mid-buffer would clobber the next field.
+ if (webgpu::needsWriteStaging(sizeBytes) && offsetBytes + sizeBytes != it->second.size) {
+ ES_LOG_ERROR("WebGPUDevice::updateBuffer: unaligned partial write {}+{} of {}",
+ offsetBytes, sizeBytes, it->second.size);
return;
}
- wgpuQueueWriteBuffer(queue_, it->second.buffer, offsetBytes, data, sizeBytes);
+ writeBufferPadded(it->second.buffer, offsetBytes, data, sizeBytes);
}
void WebGPUDevice::resizeBuffer(BufferHandle buffer, u32 sizeBytes, const void* data) {
@@ -490,12 +484,16 @@ void WebGPUDevice::resizeBuffer(BufferHandle buffer, u32 sizeBytes, const void*
WGPUBufferDescriptor bd{};
bd.usage = toWGPUBufferUsage(it->second.usage);
- bd.size = (sizeBytes + 3u) & ~3u;
+ bd.size = webgpu::alignedWriteSize(sizeBytes);
it->second.buffer = wgpuDeviceCreateBuffer(device_, &bd);
it->second.size = sizeBytes;
+ if (!it->second.buffer) {
+ ES_LOG_ERROR("WebGPUDevice::resizeBuffer: creation failed ({} bytes)", sizeBytes);
+ return;
+ }
- if (data && sizeBytes > 0 && it->second.buffer) {
- writeBufferFromStart(it->second.buffer, data, sizeBytes);
+ if (data && sizeBytes > 0) {
+ writeBufferPadded(it->second.buffer, 0, data, sizeBytes);
}
}
@@ -587,7 +585,7 @@ TextureHandle WebGPUDevice::createTexture(const TextureDesc& desc, const void* p
const u32 id = next_id_++;
textures_[id] = TextureRec{texture, wgpuTextureCreateView(texture, nullptr),
- desc.width, desc.height, td.format,
+ desc.width, desc.height, td.format, desc.format,
packSamplerKey(desc.minFilter, desc.magFilter,
desc.wrapS, desc.wrapT)};
@@ -637,7 +635,7 @@ TextureHandle WebGPUDevice::createCompressedTexture(const TextureDesc& desc, Gfx
const u32 blocksX = (lw + bi.blockWidth - 1) / bi.blockWidth;
const u32 blocksY = (lh + bi.blockHeight - 1) / bi.blockHeight;
const u32 levelBytes = blocksX * blocksY * bi.bytesPerBlock;
- if (ptr + levelBytes > end) break; // truncated pyramid: stop cleanly
+ if (levelBytes > static_cast<usize>(end - ptr)) break; // truncated pyramid
WGPUTexelCopyTextureInfo dst{};
dst.texture = texture;
dst.mipLevel = level;
@@ -652,7 +650,7 @@ TextureHandle WebGPUDevice::createCompressedTexture(const TextureDesc& desc, Gfx
const u32 id = next_id_++;
textures_[id] = TextureRec{texture, wgpuTextureCreateView(texture, nullptr),
- desc.width, desc.height, wgpuFmt,
+ desc.width, desc.height, wgpuFmt, desc.format,
packSamplerKey(desc.minFilter, desc.magFilter,
desc.wrapS, desc.wrapT)};
return TextureHandle{id};
@@ -688,33 +686,32 @@ void WebGPUDevice::updateTexture(TextureHandle texture, i32 x, i32 y, u32 width,
dst.texture = it->second.texture;
dst.origin = WGPUOrigin3D{static_cast<u32>(x), static_cast<u32>(y), 0};
- const u32 bpp = it->second.format == WGPUTextureFormat_RGBA16Float ? 8u : 4u;
+ // Source rows are what the caller allocated; destination rows are the texel
+ // size WebGPU stores. They differ for RGB8, which has no WebGPU format and so
+ // lands in an RGBA8 texture — reading dstRow from the source is the over-read.
+ const u32 srcBpp = gfxBytesPerPixel(it->second.srcFormat);
+ const u32 dstBpp = wgpuBytesPerPixel(it->second.srcFormat);
+ const usize dstRow = static_cast<usize>(width) * dstBpp;
+
WGPUTexelCopyBufferLayout layout{};
- layout.bytesPerRow = width * bpp;
+ layout.bytesPerRow = static_cast<u32>(dstRow);
layout.rowsPerImage = height;
- // Decoded images arrive top-first while the engine samples v upward, so an
- // upload is flipped. GL does it with UNPACK_FLIP_Y_WEBGL; WebGPU has no such
- // pixel-store parameter, so the rows are reversed here — the same bytes GL
- // would have sent, which is what keeps the two backends pixel-identical.
- // Without this every texture on the WebGPU backend was upside down: invisible
- // on a symmetric sprite, and glaring on a tileset atlas, where row 0 sampled
- // the atlas's last row.
- const usize rowBytes = static_cast<usize>(width) * bpp;
- std::vector<u8> flipped;
+ // Decoded images arrive top-first while the engine samples v upward. GL flips
+ // with UNPACK_FLIP_Y_WEBGL; WebGPU has no pixel-store parameter, so rows are
+ // reversed here — the same bytes GL sends, which is what keeps the two equal.
+ const bool reverseRows = flipY && height > 1;
+ std::vector<u8> staged;
const void* src = pixels;
- if (flipY && height > 1) {
- flipped.resize(rowBytes * height);
- const u8* in = static_cast<const u8*>(pixels);
- for (u32 row = 0; row < height; ++row) {
- std::memcpy(flipped.data() + rowBytes * row,
- in + rowBytes * (height - 1 - row), rowBytes);
- }
- src = flipped.data();
+ if (reverseRows || srcBpp != dstBpp) {
+ staged.resize(dstRow * height);
+ stageTextureRows(staged.data(), static_cast<const u8*>(pixels),
+ width, height, srcBpp, dstBpp, reverseRows);
+ src = staged.data();
}
WGPUExtent3D extent{width, height, 1};
- wgpuQueueWriteTexture(queue_, &dst, src, rowBytes * height, &layout, &extent);
+ wgpuQueueWriteTexture(queue_, &dst, src, dstRow * height, &layout, &extent);
}
void WebGPUDevice::setTextureParams(TextureHandle texture, TextureFilter minFilter,
diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
index d7bfd970f..7d1c75a3d 100644
--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp
@@ -221,6 +221,10 @@ private:
u32 width = 0;
u32 height = 0;
WGPUTextureFormat format = WGPUTextureFormat_RGBA8Unorm;
+ /// The RHI format the texture was created with — the layout an upload reads
+ /// the caller's pixels in. WebGPU has no 3-channel format, so RGB8 sources
+ /// land in an RGBA8 texture and `format` alone cannot size the source rows.
+ GfxPixelFormat srcFormat = GfxPixelFormat::RGBA8;
u8 samplerKey = 0; ///< Packed filter/wrap params (sampler cache key).
};
struct ProgramRec {
@@ -388,10 +392,10 @@ private:
/// Drop every cached group referencing a dying resource id (see cache note).
void evictBindGroups(u64 id);
- /// Fill @p buffer from offset 0 with @p size bytes of @p data. WriteBuffer's size
- /// must be a 4-byte multiple; the caller's allocation is only @p size bytes, so a
- /// short tail is padded through a staging copy rather than over-read.
- void writeBufferFromStart(WGPUBuffer buffer, const void* data, u32 size);
+ /// The single WriteBuffer call site. Its size must be a 4-byte multiple and the
+ /// caller's allocation is only @p size bytes, so a short tail is staged through a
+ /// zero-padded copy rather than over-read. @p offset must already be aligned.
+ void writeBufferPadded(WGPUBuffer buffer, u32 offset, const void* data, u32 size);
// GPU timing — the WebGPU analog of GLDevice's GL_TIME_ELAPSED timer, so the
// profiler's gpuMs/gpuScopes populate on both backends. Active only when the
diff --git a/src/esengine/renderer/webgpu/WebGPUMappings.hpp b/src/esengine/renderer/webgpu/WebGPUMappings.hpp
index 92c1fb040..61f1c987d 100644
--- a/src/esengine/renderer/webgpu/WebGPUMappings.hpp
+++ b/src/esengine/renderer/webgpu/WebGPUMappings.hpp
@@ -408,4 +408,10 @@ inline u32 scanWGSLBindingMask(const char* source, u32 group) {
inline u32 alignedWriteSize(u32 size) { return (size + 3u) & ~3u; }
inline bool needsWriteStaging(u32 size) { return (size & 3u) != 0; }
+/// Whether a write lands inside a buffer. Subtraction, not addition: `offset + size`
+/// can wrap past UINT32_MAX and compare as in-range.
+inline bool writeFitsInBuffer(u32 offset, u32 size, u32 capacity) {
+ return size <= capacity && offset <= capacity - size;
+}
+
} // namespace esengine::webgpu
diff --git a/tests/renderer/test_texture_fb.cpp b/tests/renderer/test_texture_fb.cpp
index bc25ad12d..3d08a8811 100644
--- a/tests/renderer/test_texture_fb.cpp
+++ b/tests/renderer/test_texture_fb.cpp
@@ -7,6 +7,7 @@
#include "MockGfxDevice.hpp"
#include "esengine/renderer/rhi/Texture.hpp"
#include "esengine/renderer/rhi/Framebuffer.hpp"
+#include "esengine/renderer/rhi/PixelUpload.hpp"
#include <cstdio>
#include <vector>
@@ -128,6 +129,35 @@ int main() {
CHECK(d.deleteTextureCalls == 2, "destructor deletes both attachments via device");
}
+ // --- Upload row arithmetic: reads are sized by the SOURCE format ---
+ {
+ CHECK(gfxBytesPerPixel(GfxPixelFormat::RGB8) == 3, "RGB8 pixels are 3 source bytes");
+ CHECK(gfxBytesPerPixel(GfxPixelFormat::RGBA8) == 4, "RGBA8 pixels are 4 source bytes");
+ CHECK(gfxBytesPerPixel(GfxPixelFormat::SRGB8_ALPHA8) == 4, "sRGB8A8 pixels are 4 source bytes");
+ CHECK(gfxBytesPerPixel(GfxPixelFormat::RGBA16F) == 8, "RGBA16F pixels are 8 source bytes");
+
+ // Sized to the source EXACTLY: staging that reads a destination-sized row
+ // runs off the end here, which is what the sanitizer build is here to catch.
+ const u32 w = 3, h = 2;
+ std::vector<u8> rgb(static_cast<usize>(w) * h * 3);
+ for (usize i = 0; i < rgb.size(); ++i) rgb[i] = static_cast<u8>(i + 1);
+ std::vector<u8> out(static_cast<usize>(w) * h * 4, 0);
+
+ stageTextureRows(out.data(), rgb.data(), w, h, 3, 4, /*reverseRows=*/false);
+ CHECK(out[0] == 1 && out[1] == 2 && out[2] == 3 && out[3] == 0xFF,
+ "RGB8 widens to RGBA8 with opaque alpha");
+ CHECK(out[12] == 10 && out[15] == 0xFF, "row 1 starts at the destination stride");
+
+ stageTextureRows(out.data(), rgb.data(), w, h, 3, 4, /*reverseRows=*/true);
+ CHECK(out[0] == 10 && out[12] == 1, "reverseRows emits the last source row first");
+
+ std::vector<u8> rgba(static_cast<usize>(w) * h * 4);
+ for (usize i = 0; i < rgba.size(); ++i) rgba[i] = static_cast<u8>(i + 1);
+ std::vector<u8> same(rgba.size(), 0);
+ stageTextureRows(same.data(), rgba.data(), w, h, 4, 4, /*reverseRows=*/true);
+ CHECK(same[0] == 13 && same[12] == 1, "equal pixel sizes copy rows verbatim");
+ }
+
if (g_failures == 0) {
std::printf("\nALL TEXTURE/FRAMEBUFFER TESTS PASSED\n");
return 0;
diff --git a/tests/renderer/test_webgpu_device.cpp b/tests/renderer/test_webgpu_device.cpp
index 5f3517ad8..0b15f18ef 100644
--- a/tests/renderer/test_webgpu_device.cpp
+++ b/tests/renderer/test_webgpu_device.cpp
@@ -245,3 +245,15 @@ TEST_CASE("write sizes round the ALLOCATION, never the read out of the caller's
else CHECK(alignedWriteSize(size) > size);
}
}
+
+TEST_CASE("a write range is checked without letting offset + size wrap") {
+ CHECK(writeFitsInBuffer(0, 64, 64));
+ CHECK(writeFitsInBuffer(60, 4, 64));
+ CHECK(!writeFitsInBuffer(60, 8, 64));
+ CHECK(!writeFitsInBuffer(0, 65, 64));
+ CHECK(!writeFitsInBuffer(64, 1, 64));
+
+ // offset + size == 0x100000000, which truncates to 0 and would read as in-range.
+ CHECK(!writeFitsInBuffer(0xFFFFFFF0u, 16u, 64));
+ CHECK(!writeFitsInBuffer(16u, 0xFFFFFFF0u, 64));
+}