Skip to content

Commit

Permalink
[WebGPU] Calling GPUTexture.destroy() on the result of GPUCanvasConte…
Browse files Browse the repository at this point in the history
…xt.getCurrentTexture() is problematic

https://bugs.webkit.org/show_bug.cgi?id=267098
<radar://120494910>

Reviewed by Tadeu Zagallo.

Calling GPUTexture.destroy() on the result of GPUCanvasContext.getCurrentTexture() was
problematic because we don't continuously make new IOSurface backed MTLTexture instances
for the canvas.

We don't need to, we just need to ensure that calling destroy makes the Texture act
like an invalid texture and that calling getCurrentTexture() returns a non-destroyed
texture.

* Source/WebGPU/WebGPU/PresentationContextIOSurface.mm:
(WebGPU::PresentationContextIOSurface::configure):
(WebGPU::PresentationContextIOSurface::getCurrentTexture):
* Source/WebGPU/WebGPU/Texture.h:
* Source/WebGPU/WebGPU/Texture.mm:
(WebGPU::Texture::recreateIfNeeded):
(WebGPU::Texture::makeCanvasBacking):
(WebGPU::Texture::destroy):

Canonical link: https://commits.webkit.org/272711@main
  • Loading branch information
mwyrzykowski committed Jan 5, 2024
1 parent cae26b3 commit b6b15a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Source/WebGPU/WebGPU/PresentationContextIOSurface.mm
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
texture.label = fromAPI(descriptor.label);
auto viewFormats = Vector<WGPUTextureFormat> { Texture::pixelFormat(descriptor.format) };
auto parentTexture = Texture::create(texture, wgpuTextureDescriptor, WTFMove(viewFormats), device);
parentTexture->makeCanvasBacking();
m_renderBuffers.append({ parentTexture, TextureView::create(texture, wgpuTextureViewDescriptor, { { width, height, 1 } }, parentTexture, device) });
}
ASSERT(m_ioSurfaces.count == m_renderBuffers.size());
Expand All @@ -143,7 +144,9 @@
Texture* PresentationContextIOSurface::getCurrentTexture()
{
ASSERT(m_ioSurfaces.count == m_renderBuffers.size());
return m_renderBuffers[m_currentIndex].texture.ptr();
auto& texture = m_renderBuffers[m_currentIndex].texture;
texture->recreateIfNeeded();
return texture.ptr();
}

TextureView* PresentationContextIOSurface::getCurrentTextureView()
Expand Down
3 changes: 3 additions & 0 deletions Source/WebGPU/WebGPU/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Texture : public WGPUTextureImpl, public RefCounted<Texture> {
static bool hasStorageBindingCapability(WGPUTextureFormat, const Device&, WGPUStorageTextureAccess = WGPUStorageTextureAccess_Undefined);
static bool supportsMultisampling(WGPUTextureFormat, const Device&);
static bool supportsBlending(WGPUTextureFormat, const Device&);
void recreateIfNeeded();
void makeCanvasBacking();

private:
Texture(id<MTLTexture>, const WGPUTextureDescriptor&, Vector<WGPUTextureFormat>&& viewFormats, Device&);
Expand Down Expand Up @@ -136,6 +138,7 @@ class Texture : public WGPUTextureImpl, public RefCounted<Texture> {
using ClearedToZeroContainer = HashMap<uint32_t, ClearedToZeroInnerContainer, DefaultHash<uint32_t>, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>>;
ClearedToZeroContainer m_clearedToZero;
bool m_destroyed { false };
bool m_canvasBacking { false };
};

} // namespace WebGPU
15 changes: 13 additions & 2 deletions Source/WebGPU/WebGPU/Texture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2723,11 +2723,22 @@ static MTLPixelFormat resolvedPixelFormat(MTLPixelFormat viewPixelFormat, MTLPix
return TextureView::create(texture, *descriptor, renderExtent, *this, m_device);
}

void Texture::recreateIfNeeded()
{
RELEASE_ASSERT(m_texture.iosurface && m_canvasBacking);
m_destroyed = false;
}

void Texture::makeCanvasBacking()
{
m_canvasBacking = true;
}

void Texture::destroy()
{
// https://gpuweb.github.io/gpuweb/#dom-gputexture-destroy

m_texture = nil;
if (!m_canvasBacking)
m_texture = nil;
m_destroyed = true;
}

Expand Down

0 comments on commit b6b15a9

Please sign in to comment.