Hash :
3cf7a604
Author :
Date :
2025-05-17T19:39:08
WebGPU: Add extensions for importing external textures Add EGL_ANGLE_device_webgpu which exposes the adapter and device used by ANGLE internally. Add EGL_ANGLE_webgpu_texture_client_buffer which allows importing external WGPUTexture handles if they share the same device as ANGLE (queried from EGL_ANGLE_device_webgpu). Bug: angleproject:418022112 Change-Id: I0683d36b84a0f8e0e9b68a5ec0d3aa8b7a95152c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6553063 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
//
// Copyright 2024 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ImageWgpu.cpp:
// Implements the class methods for ImageWgpu.
//
#include "libANGLE/renderer/wgpu/ImageWgpu.h"
#include "libANGLE/Display.h"
#include "libANGLE/renderer/wgpu/DisplayWgpu.h"
#include "libANGLE/renderer/wgpu/RenderbufferWgpu.h"
#include "libANGLE/renderer/wgpu/TextureWgpu.h"
#include "common/debug.h"
namespace rx
{
WebGPUTextureImageSiblingWgpu::WebGPUTextureImageSiblingWgpu(EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
: mBuffer(buffer), mAttribs(attribs)
{}
WebGPUTextureImageSiblingWgpu::~WebGPUTextureImageSiblingWgpu() {}
egl::Error WebGPUTextureImageSiblingWgpu::initialize(const egl::Display *display)
{
return angle::ResultToEGL(initializeImpl(display));
}
angle::Result WebGPUTextureImageSiblingWgpu::initializeImpl(const egl::Display *display)
{
DisplayWgpu *displayWgpu = webgpu::GetImpl(display);
const DawnProcTable *wgpu = displayWgpu->getProcs();
webgpu::TextureHandle externalTexture =
webgpu::TextureHandle::Acquire(wgpu, reinterpret_cast<WGPUTexture>(mBuffer));
ASSERT(externalTexture != nullptr);
// Explicitly add-ref to hold on to the external reference
wgpu->textureAddRef(externalTexture.get());
const webgpu::Format *webgpuFormat = displayWgpu->getFormatForImportedTexture(
mAttribs, wgpu->textureGetFormat(externalTexture.get()));
ASSERT(webgpuFormat != nullptr);
ANGLE_TRY(mImage.initExternal(wgpu, webgpuFormat->getIntendedFormatID(),
webgpuFormat->getActualImageFormatID(), externalTexture));
return angle::Result::Continue;
}
void WebGPUTextureImageSiblingWgpu::onDestroy(const egl::Display *display)
{
mImage.resetImage();
}
gl::Format WebGPUTextureImageSiblingWgpu::getFormat() const
{
const angle::Format &angleFormat = angle::Format::Get(mImage.getIntendedFormatID());
return gl::Format(angleFormat.glInternalFormat);
}
bool WebGPUTextureImageSiblingWgpu::isRenderable(const gl::Context *context) const
{
constexpr WGPUTextureUsage kRenderableRequiredUsages =
WGPUTextureUsage_CopySrc | WGPUTextureUsage_RenderAttachment;
return (mImage.getUsage() & kRenderableRequiredUsages) == kRenderableRequiredUsages;
}
bool WebGPUTextureImageSiblingWgpu::isTexturable(const gl::Context *context) const
{
constexpr WGPUTextureUsage kTexturableRequiredUsages =
WGPUTextureUsage_CopyDst | WGPUTextureUsage_TextureBinding;
return (mImage.getUsage() & kTexturableRequiredUsages) == kTexturableRequiredUsages;
}
bool WebGPUTextureImageSiblingWgpu::isYUV() const
{
const angle::Format &actualFormat = angle::Format::Get(mImage.getActualFormatID());
return actualFormat.isYUV;
}
bool WebGPUTextureImageSiblingWgpu::hasFrontBufferUsage() const
{
return false;
}
bool WebGPUTextureImageSiblingWgpu::isCubeMap() const
{
return false;
}
bool WebGPUTextureImageSiblingWgpu::hasProtectedContent() const
{
return false;
}
gl::Extents WebGPUTextureImageSiblingWgpu::getSize() const
{
return wgpu_gl::GetExtents(mImage.getSize());
}
size_t WebGPUTextureImageSiblingWgpu::getSamples() const
{
// GL treats a sample count of 1 as multisampled. When the WebGPU texture has a sample count of
// 1, force it to 0.
uint32_t samples = mImage.getSamples();
return samples > 1 ? samples : 0;
}
uint32_t WebGPUTextureImageSiblingWgpu::getLevelCount() const
{
return mImage.getLevelCount();
}
webgpu::ImageHelper *WebGPUTextureImageSiblingWgpu::getImage() const
{
return const_cast<webgpu::ImageHelper *>(&mImage);
}
ImageWgpu::ImageWgpu(const egl::ImageState &state, const gl::Context *context)
: ImageImpl(state), mContext(context)
{}
ImageWgpu::~ImageWgpu() {}
egl::Error ImageWgpu::initialize(const egl::Display *display)
{
if (egl::IsTextureTarget(mState.target))
{
if (mState.imageIndex.getLevelIndex() != 0)
{
UNIMPLEMENTED();
return egl::Error(EGL_BAD_ACCESS,
"Creation of EGLImages from non-zero mip levels is unimplemented.");
}
if (mState.imageIndex.getType() != gl::TextureType::_2D)
{
UNIMPLEMENTED();
return egl::Error(EGL_BAD_ACCESS,
"Creation of EGLImages from non-2D textures is unimplemented.");
}
TextureWgpu *textureWgpu = webgpu::GetImpl(GetAs<gl::Texture>(mState.source));
ASSERT(mContext != nullptr);
angle::Result initResult = textureWgpu->ensureImageInitialized(mContext);
if (initResult != angle::Result::Continue)
{
return egl::Error(EGL_BAD_ACCESS, "Failed to initialize source texture.");
}
mImage = textureWgpu->getImage();
}
else if (egl::IsRenderbufferTarget(mState.target))
{
RenderbufferWgpu *renderbufferWgpu =
webgpu::GetImpl(GetAs<gl::Renderbuffer>(mState.source));
mImage = renderbufferWgpu->getImage();
ASSERT(mContext != nullptr);
}
else if (egl::IsExternalImageTarget(mState.target))
{
const ExternalImageSiblingWgpu *externalImageSibling =
webgpu::GetImpl(GetAs<egl::ExternalImageSibling>(mState.source));
mImage = externalImageSibling->getImage();
ASSERT(mContext == nullptr);
}
else
{
UNREACHABLE();
return egl::Error(EGL_BAD_ACCESS);
}
ASSERT(mImage->isInitialized());
mOwnsImage = false;
return egl::NoError();
}
angle::Result ImageWgpu::orphan(const gl::Context *context, egl::ImageSibling *sibling)
{
if (sibling == mState.source)
{
if (egl::IsTextureTarget(mState.target))
{
TextureWgpu *textureWgpu = webgpu::GetImpl(GetAs<gl::Texture>(mState.source));
ASSERT(mImage == textureWgpu->getImage());
textureWgpu->releaseOwnershipOfImage(context);
}
else if (egl::IsRenderbufferTarget(mState.target))
{
RenderbufferWgpu *renderbufferWgpu =
webgpu::GetImpl(GetAs<gl::Renderbuffer>(mState.source));
ASSERT(mImage == renderbufferWgpu->getImage());
renderbufferWgpu->releaseOwnershipOfImage(context);
}
else
{
UNREACHABLE();
return angle::Result::Stop;
}
mOwnsImage = true;
}
return angle::Result::Continue;
}
} // namespace rx