From 1e8e82b42ecc4228eecfd7d5399c3fe1c8b990b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 21 Jun 2026 21:05:14 -0700 Subject: [PATCH] WebGPU: Fixed texture conversion pitch, and mip-gen array views. (#3776) --- src/renderer_webgpu.cpp | 18 ++++++++++++------ src/renderer_webgpu.h | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/renderer_webgpu.cpp b/src/renderer_webgpu.cpp index 5fc832412..517765a37 100644 --- a/src/renderer_webgpu.cpp +++ b/src/renderer_webgpu.cpp @@ -4127,7 +4127,7 @@ WGPU_IMPORT rectPitch = (_rect.m_width / blockInfo.blockWidth) * blockInfo.blockSize; } - const uint32_t bytesPerRow = UINT16_MAX == _pitch ? rectPitch : _pitch; + uint32_t bytesPerRow = UINT16_MAX == _pitch ? rectPitch : _pitch; const uint32_t slicePitch = rectPitch*_rect.m_height; const bool convert = m_textureFormat != m_requestedFormat; @@ -4137,10 +4137,10 @@ WGPU_IMPORT if (convert) { + bytesPerRow = rectPitch; temp = (uint8_t*)bx::alloc(g_allocator, slicePitch); - bimg::imageDecodeToBgra8(g_allocator, temp, srcData, _rect.m_width, _rect.m_height, bytesPerRow, bimg::TextureFormat::Enum(m_requestedFormat) ); + bimg::imageDecodeToBgra8(g_allocator, temp, srcData, _rect.m_width, _rect.m_height, rectPitch, bimg::TextureFormat::Enum(m_requestedFormat) ); srcData = temp; - } const uint32_t width = bx::min(bx::max(1u, bx::alignUp(m_width >> _mip, blockInfo.blockWidth ) ), _rect.m_width); @@ -4226,7 +4226,7 @@ WGPU_IMPORT return sampler; } - WGPUTextureView TextureWGPU::getTextureView(uint8_t _baseMipLevel, uint8_t _mipLevelCount, bool _storage, uint16_t _baseArrayLayer, uint16_t _arrayLayerCount) const + WGPUTextureView TextureWGPU::getTextureView(uint8_t _baseMipLevel, uint8_t _mipLevelCount, bool _storage, uint16_t _baseArrayLayer, uint16_t _arrayLayerCount, bool _force2DArray) const { bx::HashMurmur3 murmur; murmur.begin(); @@ -4236,6 +4236,7 @@ WGPU_IMPORT murmur.add(_storage); murmur.add(_baseArrayLayer); murmur.add(_arrayLayerCount); + murmur.add(_force2DArray); const uint32_t hash = murmur.end(); WGPUTextureView textureView = s_renderWGPU->m_textureViewStateCache.find(hash); @@ -4256,6 +4257,11 @@ WGPU_IMPORT } } + if (_force2DArray) + { + tvd = WGPUTextureViewDimension_2DArray; + } + WGPUTextureViewDescriptor textureViewDesc = { .nextInChain = NULL, @@ -5397,7 +5403,7 @@ m_resolution.formatColor = TextureFormat::BGRA8; WGPUTextureView view; if (ii < numMips) { - view = _texture.getTextureView(uint8_t(topMip + 1 + ii), 1, true, true); + view = _texture.getTextureView(uint8_t(topMip + 1 + ii), 1, true, 0, UINT16_MAX, true); } else { @@ -5429,7 +5435,7 @@ m_resolution.formatColor = TextureFormat::BGRA8; .offset = 0, .size = 0, .sampler = NULL, - .textureView = _texture.getTextureView(uint8_t(topMip), 1, false, true), + .textureView = _texture.getTextureView(uint8_t(topMip), 1, false, 0, UINT16_MAX, true), }; const uint32_t samplerFlags = 0 diff --git a/src/renderer_webgpu.h b/src/renderer_webgpu.h index 9cc196e04..ece1d09bc 100644 --- a/src/renderer_webgpu.h +++ b/src/renderer_webgpu.h @@ -638,7 +638,7 @@ namespace wgpu { void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem); WGPUSampler getSamplerState(uint32_t _samplerFlags) const; - WGPUTextureView getTextureView(uint8_t _baseMipLevel, uint8_t _mipLevelCount, bool _storage, uint16_t _baseArrayLayer = 0, uint16_t _arrayLayerCount = UINT16_MAX) const; + WGPUTextureView getTextureView(uint8_t _baseMipLevel, uint8_t _mipLevelCount, bool _storage, uint16_t _baseArrayLayer = 0, uint16_t _arrayLayerCount = UINT16_MAX, bool _force2DArray = false) const; WGPUTexture m_texture; WGPUTexture m_textureResolve;