Skip to content

Commit

Permalink
Enable WebGPU Canvas STORAGE_BINDING on Win
Browse files Browse the repository at this point in the history
This allows the usage for kAllowedMailboxTextureUsages and
SharedImageBackingD3D.
This also extends F16 support for WGPUAdapterType_CPU.

Bug: 1241369
Bug: 1266549
Change-Id: I1d4a2c7c95bf2357b70ff1d9f557fa6d8c8bb829
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3534517
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jie A Chen <jie.a.chen@intel.com>
Cr-Commit-Position: refs/heads/main@{#983725}
  • Loading branch information
jchen10 authored and Chromium LUCI CQ committed Mar 22, 2022
1 parent 51a84f7 commit af76ea8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
24 changes: 20 additions & 4 deletions gpu/command_buffer/service/shared_image_backing_d3d.cc
Expand Up @@ -580,12 +580,22 @@ bool SharedImageBackingD3D::ProduceLegacyMailbox(
return true;
}

uint32_t SharedImageBackingD3D::GetAllowedDawnUsages() const {
WGPUTextureUsageFlags SharedImageBackingD3D::GetAllowedDawnUsages(
const WGPUTextureFormat wgpu_format) const {
// TODO(crbug.com/2709243): Figure out other SI flags, if any.
DCHECK(usage() & gpu::SHARED_IMAGE_USAGE_WEBGPU);
return static_cast<uint32_t>(
const WGPUTextureUsageFlags kBasicUsage =
WGPUTextureUsage_CopySrc | WGPUTextureUsage_CopyDst |
WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment);
WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment;
switch (wgpu_format) {
case WGPUTextureFormat_BGRA8Unorm:
return kBasicUsage;
case WGPUTextureFormat_RGBA8Unorm:
case WGPUTextureFormat_RGBA16Float:
return kBasicUsage | WGPUTextureUsage_StorageBinding;
default:
return WGPUTextureUsage_None;
}
}

std::unique_ptr<SharedImageRepresentationDawn>
Expand All @@ -607,10 +617,16 @@ SharedImageBackingD3D::ProduceDawn(SharedImageManager* manager,
DLOG(ERROR) << "Unsupported viz format found: " << viz_resource_format;
return nullptr;
}
const WGPUTextureUsageFlags usage = GetAllowedDawnUsages(wgpu_format);
if (usage == WGPUTextureUsage_None) {
DLOG(ERROR) << "WGPUTextureUsage is unknown for viz format: "
<< viz_resource_format;
return nullptr;
}

WGPUTextureDescriptor texture_descriptor = {};
texture_descriptor.format = wgpu_format;
texture_descriptor.usage = GetAllowedDawnUsages();
texture_descriptor.usage = static_cast<uint32_t>(usage);
texture_descriptor.dimension = WGPUTextureDimension_2D;
texture_descriptor.size = {static_cast<uint32_t>(size().width()),
static_cast<uint32_t>(size().height()), 1};
Expand Down
3 changes: 2 additions & 1 deletion gpu/command_buffer/service/shared_image_backing_d3d.h
Expand Up @@ -168,7 +168,8 @@ class GPU_GLES2_EXPORT SharedImageBackingD3D
Microsoft::WRL::ComPtr<IDXGISwapChain1> swap_chain = nullptr,
bool is_back_buffer = false);

uint32_t GetAllowedDawnUsages() const;
WGPUTextureUsageFlags GetAllowedDawnUsages(
const WGPUTextureFormat wgpu_format) const;

gl::GLImage* GetGLImage() const;

Expand Down
Expand Up @@ -330,6 +330,11 @@ SharedImageBackingFactoryD3D::CreateSharedImage(
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
// WebGPU can use RGBA_8888 and RGBA_16 for STORAGE_BINDING.
if ((usage & gpu::SHARED_IMAGE_USAGE_WEBGPU) &&
(format == viz::RGBA_8888 || format == viz::RGBA_F16)) {
desc.BindFlags |= D3D11_BIND_UNORDERED_ACCESS;
}
desc.CPUAccessFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE |
D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
Expand Down
13 changes: 11 additions & 2 deletions gpu/command_buffer/service/webgpu_decoder_impl.cc
Expand Up @@ -66,10 +66,10 @@ constexpr size_t kDawnReturnCmdsOffset =

static_assert(kDawnReturnCmdsOffset < kMaxWireBufferSize, "");

// TODO(crbug.com/1266549): Support Storage usage
static constexpr uint32_t kAllowedWritableMailboxTextureUsages =
static_cast<uint32_t>(WGPUTextureUsage_CopyDst |
WGPUTextureUsage_RenderAttachment);
WGPUTextureUsage_RenderAttachment |
WGPUTextureUsage_StorageBinding);

static constexpr uint32_t kAllowedReadableMailboxTextureUsages =
static_cast<uint32_t>(WGPUTextureUsage_CopySrc |
Expand Down Expand Up @@ -530,6 +530,7 @@ class WebGPUDecoderImpl final : public WebGPUDecoder {
case viz::ResourceFormat::RGBA_8888:
#endif // !BUILDFLAG(IS_MAC)
case viz::ResourceFormat::BGRA_8888:
case viz::ResourceFormat::RGBA_F16:
break;
default:
return nullptr;
Expand Down Expand Up @@ -1637,6 +1638,14 @@ WebGPUDecoderImpl::AssociateMailboxDawn(const Mailbox& mailbox,
return nullptr;
}

#if !BUILDFLAG(IS_WIN)
if (usage & WGPUTextureUsage_StorageBinding) {
DLOG(ERROR) << "AssociateMailbox: WGPUTextureUsage_StorageBinding is NOT "
"supported yet.";
return nullptr;
}
#endif

if (flags & WEBGPU_MAILBOX_DISCARD) {
// Set contents to uncleared.
shared_image->SetClearedRect(gfx::Rect());
Expand Down
13 changes: 11 additions & 2 deletions gpu/command_buffer/tests/webgpu_mailbox_unittest.cc
Expand Up @@ -60,6 +60,9 @@ std::ostream& operator<<(std::ostream& os,
case viz::ResourceFormat::BGRA_8888:
os << "BGRA_8888";
break;
case viz::ResourceFormat::RGBA_F16:
os << "RGBA_F16";
break;
default:
NOTREACHED();
}
Expand Down Expand Up @@ -93,7 +96,7 @@ class WebGPUMailboxTest
#if !BUILDFLAG(IS_MAC)
viz::ResourceFormat::RGBA_8888,
#endif // !BUILDFLAG(IS_MAC)
viz::ResourceFormat::BGRA_8888,
viz::ResourceFormat::BGRA_8888, viz::ResourceFormat::RGBA_F16,
}) {
WebGPUMailboxTestParams o = options;
o.format = format;
Expand Down Expand Up @@ -403,7 +406,10 @@ TEST_P(WebGPUMailboxTest, WriteToMailboxThenReadFromIt) {
LOG(ERROR) << "Test skipped because WebGPUSharedImage isn't supported";
return;
}

if (GetParam().format == viz::ResourceFormat::RGBA_F16) {
LOG(ERROR) << "Test skipped because RGBA_F16 isn't supported.";
return;
}
// Create the shared image
SharedImageInterface* sii = GetSharedImageInterface();
Mailbox mailbox = sii->CreateSharedImage(
Expand Down Expand Up @@ -701,6 +707,9 @@ TEST_P(WebGPUMailboxTest, ErrorWhenUsingTextureAfterDissociate) {
case viz::ResourceFormat::BGRA_8888:
dst_desc.format = wgpu::TextureFormat::BGRA8Unorm;
break;
case viz::ResourceFormat::RGBA_F16:
dst_desc.format = wgpu::TextureFormat::RGBA16Float;
break;
default:
NOTREACHED();
}
Expand Down

0 comments on commit af76ea8

Please sign in to comment.