Skip to content

Commit

Permalink
[WebGPU] Implement GPUExternalTexture
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=255883
<radar://108463707>

Reviewed by Myles C. Maxfield.

Implement GPUExternalTexture and pass it through to WebGPU.framework.

* Source/WebCore/Modules/WebGPU/GPUExternalTextureDescriptor.h:
(WebCore::GPUExternalTextureDescriptor::convertToBacking const):

* Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUConvertToBackingContext.h:
* Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.cpp:
(PAL::WebGPU::DeviceImpl::importExternalTexture):
(PAL::WebGPU::DeviceImpl::createBindGroupLayout):
(PAL::WebGPU::DeviceImpl::createBindGroup):
* Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDowncastConvertToBackingContext.cpp:
(PAL::WebGPU::DowncastConvertToBackingContext::convertToBacking):
* Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDowncastConvertToBackingContext.h:
* Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUExternalTextureImpl.cpp:
(PAL::WebGPU::ExternalTextureImpl::ExternalTextureImpl):
* Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUExternalTextureImpl.h:
* Source/WebCore/PAL/pal/graphics/WebGPU/WebGPUExternalTextureDescriptor.h:
* Source/WebGPU/WebGPU.xcodeproj/project.pbxproj:
* Source/WebGPU/WebGPU/APIConversions.h:
(WebGPU::fromAPI):
* Source/WebGPU/WebGPU/BindGroup.mm:
(WebGPU::Device::createBindGroup):
* Source/WebGPU/WebGPU/Device.h:
* Source/WebGPU/WebGPU/Device.mm:
(WebGPU::Device::Device):
(WebGPU::metalPixelFormatForCVPixelBuffer):
(WebGPU::Device::createTextureFromPixelBuffer const):
(wgpuDeviceCreateExternalTexture):
* Source/WebGPU/WebGPU/ExternalTexture.h: Added.
(WebGPU::ExternalTexture::create):
(WebGPU::ExternalTexture::createInvalid):
(WebGPU::ExternalTexture::pixelBuffer const):
* Source/WebGPU/WebGPU/ExternalTexture.mm: Added.
(WebGPU::Device::createExternalTexture):
(WebGPU::ExternalTexture::ExternalTexture):
* Source/WebGPU/WebGPU/WebGPUExt.h:
* Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.cpp:
(WebKit::RemoteRenderingBackend::createRemoteGPU):
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteAdapter.cpp:
(WebKit::RemoteAdapter::RemoteAdapter):
(WebKit::RemoteAdapter::requestDevice):
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteAdapter.h:
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteDevice.cpp:
(WebKit::RemoteDevice::RemoteDevice):
(WebKit::RemoteDevice::importExternalTexture):
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteDevice.h:
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteGPU.cpp:
(WebKit::RemoteGPU::RemoteGPU):
(WebKit::RemoteGPU::remoteRenderingBackend const):
(WebKit::RemoteGPU::requestAdapter):
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteGPU.h:
* Source/WebKit/GPUProcess/graphics/WebGPU/RemoteRenderPassEncoder.cpp:
(WebKit::RemoteRenderPassEncoder::setBindGroup):
* Source/WebKit/Shared/WebGPU/WebGPUExternalTextureDescriptor.cpp:
(WebKit::WebGPU::ConvertToBackingContext::convertToBacking):
(WebKit::WebGPU::ConvertFromBackingContext::convertFromBacking):
* Source/WebKit/Shared/WebGPU/WebGPUExternalTextureDescriptor.h:
* Source/WebKit/Shared/WebGPU/WebGPUExternalTextureDescriptor.serialization.in:
* Websites/webkit.org/demos/webgpu/external-textured-cube.html: Added.
* Websites/webkit.org/demos/webgpu/scripts/external-textured-cube.js: Added.
(async helloCube.frameUpdate):
(async helloCube):

Canonical link: https://commits.webkit.org/264108@main
  • Loading branch information
mwyrzykowski committed May 16, 2023
1 parent 4a2d770 commit cb91b74
Show file tree
Hide file tree
Showing 31 changed files with 798 additions and 36 deletions.
1 change: 1 addition & 0 deletions Source/WTF/wtf/PlatformHave.h
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,7 @@

#if HAVE(WEBGPU_IMPLEMENTATION) && ((PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 160000))
#define HAVE_METAL_BUFFER_BINDING_REFLECTION 1
#define HAVE_COREVIDEO_METAL_SUPPORT COREVIDEO_SUPPORTS_METAL
#endif

#if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000)
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/Modules/WebGPU/GPUExternalTextureDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class HTMLVideoElement;
struct GPUExternalTextureDescriptor : public GPUObjectDescriptorBase {
PAL::WebGPU::ExternalTextureDescriptor convertToBacking() const
{
auto playerIdentifier = source->playerIdentifier();
return {
{ label },
// FIXME: Handle the video element.
playerIdentifier.has_value() ? playerIdentifier->toUInt64() : 0,
WebCore::convertToBacking(colorSpace),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "WebGPUShaderStage.h"
#include "WebGPUTextureUsage.h"
#include <WebGPU/WebGPU.h>
#include <WebGPU/WebGPUExt.h>
#include <cstdint>
#include <wtf/RefCounted.h>

Expand All @@ -62,6 +63,7 @@ class ComputePipeline;
enum class CullMode : uint8_t;
class Device;
enum class ErrorFilter : uint8_t;
class ExternalTexture;
enum class FeatureName : uint8_t;
enum class FilterMode : uint8_t;
enum class FrontFace : uint8_t;
Expand Down Expand Up @@ -154,6 +156,7 @@ class ConvertToBackingContext : public RefCounted<ConvertToBackingContext> {
virtual WGPUComputePassEncoder convertToBacking(const ComputePassEncoder&) = 0;
virtual WGPUComputePipeline convertToBacking(const ComputePipeline&) = 0;
virtual WGPUDevice convertToBacking(const Device&) = 0;
virtual WGPUExternalTexture convertToBacking(const ExternalTexture&) = 0;
virtual WGPUInstance convertToBacking(const GPU&) = 0;
virtual WGPUPipelineLayout convertToBacking(const PipelineLayout&) = 0;
virtual WGPUQuerySet convertToBacking(const QuerySet&) = 0;
Expand Down
41 changes: 35 additions & 6 deletions Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "WebGPUComputePipelineImpl.h"
#include "WebGPUConvertToBackingContext.h"
#include "WebGPUExtent3D.h"
#include "WebGPUExternalTextureDescriptor.h"
#include "WebGPUExternalTextureImpl.h"
#include "WebGPUOutOfMemoryError.h"
#include "WebGPUPipelineLayoutDescriptor.h"
Expand All @@ -59,6 +60,7 @@
#include "WebGPUTextureImpl.h"
#include "WebGPUTextureViewImpl.h"
#include "WebGPUValidationError.h"
#include <CoreGraphics/CGColorSpace.h>
#include <WebGPU/WebGPUExt.h>
#include <wtf/BlockPtr.h>

Expand Down Expand Up @@ -146,9 +148,27 @@ Ref<Sampler> DeviceImpl::createSampler(const SamplerDescriptor& descriptor)
return SamplerImpl::create(wgpuDeviceCreateSampler(m_backing, &backingDescriptor), m_convertToBackingContext);
}

Ref<ExternalTexture> DeviceImpl::importExternalTexture(const ExternalTextureDescriptor&)
static WGPUColorSpace convertToWGPUColorSpace(const PredefinedColorSpace& colorSpace)
{
return ExternalTextureImpl::create(m_convertToBackingContext);
switch (colorSpace) {
case PredefinedColorSpace::SRGB:
return WGPUColorSpace::SRGB;
case PredefinedColorSpace::DisplayP3:
return WGPUColorSpace::DisplayP3;
}
}

Ref<ExternalTexture> DeviceImpl::importExternalTexture(const ExternalTextureDescriptor& descriptor)
{
auto label = descriptor.label.utf8();

WGPUExternalTextureDescriptor backingDescriptor {
.nextInChain = nullptr,
label.data(),
.pixelBuffer = descriptor.pixelBuffer,
.colorSpace = convertToWGPUColorSpace(descriptor.colorSpace),
};
return ExternalTextureImpl::create(wgpuDeviceImportExternalTexture(backing(), &backingDescriptor), descriptor, m_convertToBackingContext);
}

Ref<BindGroupLayout> DeviceImpl::createBindGroupLayout(const BindGroupLayoutDescriptor& descriptor)
Expand All @@ -169,8 +189,8 @@ Ref<BindGroupLayout> DeviceImpl::createBindGroupLayout(const BindGroupLayoutDesc
entry.sampler ? m_convertToBackingContext->convertToBacking(entry.sampler->type) : WGPUSamplerBindingType_Undefined,
}, {
nullptr,
entry.texture ? m_convertToBackingContext->convertToBacking(entry.texture->sampleType) : WGPUTextureSampleType_Undefined,
entry.texture ? m_convertToBackingContext->convertToBacking(entry.texture->viewDimension) : WGPUTextureViewDimension_Undefined,
entry.texture ? m_convertToBackingContext->convertToBacking(entry.texture->sampleType) : (entry.externalTexture ? WGPUTextureSampleType_Float : WGPUTextureSampleType_Undefined),
entry.texture ? m_convertToBackingContext->convertToBacking(entry.texture->viewDimension) : (entry.externalTexture ? WGPUTextureViewDimension_2D : WGPUTextureViewDimension_Undefined),
entry.texture ? entry.texture->multisampled : false,
}, {
nullptr,
Expand Down Expand Up @@ -213,9 +233,18 @@ Ref<BindGroup> DeviceImpl::createBindGroup(const BindGroupDescriptor& descriptor
{
auto label = descriptor.label.utf8();

auto backingEntries = descriptor.entries.map([this] (const auto& bindGroupEntry) {
Vector<WGPUBindGroupExternalTextureEntry> chainedEntries;
auto backingEntries = descriptor.entries.map([this, &chainedEntries] (const auto& bindGroupEntry) {
auto externalTexture = std::holds_alternative<std::reference_wrapper<ExternalTexture>>(bindGroupEntry.resource) ? m_convertToBackingContext->convertToBacking(std::get<std::reference_wrapper<ExternalTexture>>(bindGroupEntry.resource).get()) : nullptr;
chainedEntries.append(WGPUBindGroupExternalTextureEntry {
{
.next = nullptr,
.sType = static_cast<WGPUSType>(WGPUSTypeExtended_BindGroupEntryExternalTexture)
},
.externalTexture = externalTexture,
});
return WGPUBindGroupEntry {
nullptr,
externalTexture ? reinterpret_cast<WGPUChainedStruct*>(&chainedEntries.last()) : nullptr,
bindGroupEntry.binding,
std::holds_alternative<BufferBinding>(bindGroupEntry.resource) ? m_convertToBackingContext->convertToBacking(std::get<BufferBinding>(bindGroupEntry.resource).buffer) : nullptr,
std::holds_alternative<BufferBinding>(bindGroupEntry.resource) ? std::get<BufferBinding>(bindGroupEntry.resource).offset : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ WGPUDevice DowncastConvertToBackingContext::convertToBacking(const Device& devic
return static_cast<const DeviceImpl&>(device).backing();
}

WGPUExternalTexture DowncastConvertToBackingContext::convertToBacking(const ExternalTexture& externalTexture)
{
return static_cast<const ExternalTextureImpl&>(externalTexture).backing();
}

WGPUInstance DowncastConvertToBackingContext::convertToBacking(const GPU& gpu)
{
return static_cast<const GPUImpl&>(gpu).backing();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DowncastConvertToBackingContext final : public ConvertToBackingContext {
WGPUComputePassEncoder convertToBacking(const ComputePassEncoder&) final;
WGPUComputePipeline convertToBacking(const ComputePipeline&) final;
WGPUDevice convertToBacking(const Device&) final;
WGPUExternalTexture convertToBacking(const ExternalTexture&) final;
WGPUInstance convertToBacking(const GPU&) final;
WGPUPipelineLayout convertToBacking(const PipelineLayout&) final;
WGPUSurface convertToBacking(const PresentationContext&) final;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
#if HAVE(WEBGPU_IMPLEMENTATION)

#include "WebGPUConvertToBackingContext.h"
#include "WebGPUExternalTextureDescriptor.h"
#include <WebGPU/WebGPUExt.h>

namespace PAL::WebGPU {

ExternalTextureImpl::ExternalTextureImpl(ConvertToBackingContext& convertToBackingContext)
ExternalTextureImpl::ExternalTextureImpl(WGPUExternalTexture externalTexture, const ExternalTextureDescriptor& descriptor, ConvertToBackingContext& convertToBackingContext)
: m_convertToBackingContext(convertToBackingContext)
, m_backing(externalTexture)
, m_colorSpace(descriptor.colorSpace)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,32 @@
#if HAVE(WEBGPU_IMPLEMENTATION)

#include "WebGPUExternalTexture.h"
#include "WebGPUExternalTextureDescriptor.h"
#include "WebGPUPredefinedColorSpace.h"
#include <WebGPU/WebGPU.h>
#include <WebGPU/WebGPUExt.h>

namespace PAL::WebGPU {

class ConvertToBackingContext;
struct ExternalTextureDescriptor;

class ExternalTextureImpl final : public ExternalTexture {
WTF_MAKE_FAST_ALLOCATED;
public:
static Ref<ExternalTextureImpl> create(ConvertToBackingContext& convertToBackingContext)
static Ref<ExternalTextureImpl> create(WGPUExternalTexture externalTexture, const ExternalTextureDescriptor& descriptor, ConvertToBackingContext& convertToBackingContext)
{
return adoptRef(*new ExternalTextureImpl(convertToBackingContext));
return adoptRef(*new ExternalTextureImpl(externalTexture, descriptor, convertToBackingContext));
}

virtual ~ExternalTextureImpl();

WGPUExternalTexture backing() const { return m_backing; };

private:
friend class DowncastConvertToBackingContext;

ExternalTextureImpl(ConvertToBackingContext&);
ExternalTextureImpl(WGPUExternalTexture, const ExternalTextureDescriptor&, ConvertToBackingContext&);

ExternalTextureImpl(const ExternalTextureImpl&) = delete;
ExternalTextureImpl(ExternalTextureImpl&&) = delete;
Expand All @@ -57,6 +63,9 @@ class ExternalTextureImpl final : public ExternalTexture {
void setLabelInternal(const String&) final;

Ref<ConvertToBackingContext> m_convertToBackingContext;

WGPUExternalTexture m_backing;
PredefinedColorSpace m_colorSpace;
};

} // namespace PAL::WebGPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
#include "WebGPUObjectDescriptorBase.h"
#include "WebGPUPredefinedColorSpace.h"

typedef struct __CVBuffer* CVPixelBufferRef;

namespace PAL::WebGPU {

struct ExternalTextureDescriptor : public ObjectDescriptorBase {
uint64_t mediaIdentifier { 0 };
PredefinedColorSpace colorSpace { PredefinedColorSpace::SRGB };
CVPixelBufferRef pixelBuffer { nullptr };
};

} // namespace PAL::WebGPU
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace PAL::WebGPU {

enum class PredefinedColorSpace : uint8_t {
SRGB,
DisplayP3,
};

} // namespace PAL::WebGPU
12 changes: 12 additions & 0 deletions Source/WebGPU/WebGPU.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
0D30F93729F1F94A0055D9F1 /* ExternalTexture.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0D30F93629F1F94A0055D9F1 /* ExternalTexture.mm */; };
0D30F93929F1FAC50055D9F1 /* ExternalTexture.h in Headers */ = {isa = PBXBuildFile; fileRef = 0D30F93829F1FAC50055D9F1 /* ExternalTexture.h */; };
0D30F93B29F1FBE40055D9F1 /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0D30F93A29F1FBE40055D9F1 /* CoreVideo.framework */; };
1C0F41EE280940650005886D /* HardwareCapabilities.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1C0F41EC280940650005886D /* HardwareCapabilities.mm */; };
1C2CEDEE271E8A7300EDC16F /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1C2CEDED271E8A7300EDC16F /* Metal.framework */; };
1C582FF927E04131009B40F0 /* CommandsMixin.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1C582FF727E04131009B40F0 /* CommandsMixin.mm */; };
Expand Down Expand Up @@ -205,6 +208,9 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0D30F93629F1F94A0055D9F1 /* ExternalTexture.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ExternalTexture.mm; sourceTree = "<group>"; };
0D30F93829F1FAC50055D9F1 /* ExternalTexture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExternalTexture.h; sourceTree = "<group>"; };
0D30F93A29F1FBE40055D9F1 /* CoreVideo.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreVideo.framework; path = System/Library/Frameworks/CoreVideo.framework; sourceTree = SDKROOT; };
0D4D2E80294A89CF0000A1AB /* BindableResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BindableResource.h; sourceTree = "<group>"; };
1C0F41EC280940650005886D /* HardwareCapabilities.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = HardwareCapabilities.mm; sourceTree = "<group>"; };
1C0F41ED280940650005886D /* HardwareCapabilities.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HardwareCapabilities.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -400,6 +406,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
0D30F93B29F1FBE40055D9F1 /* CoreVideo.framework in Frameworks */,
664C92FD286A66090008D143 /* IOSurface.framework in Frameworks */,
1CBAB0922718CCA0006080BB /* JavaScriptCore.framework in Frameworks */,
1CEBD8262716CACC00A5254D /* libwgsl.a in Frameworks */,
Expand Down Expand Up @@ -475,6 +482,8 @@
1C5ACA9B273A426D0095F8D5 /* Device.h */,
1C5ACAA3273A426D0095F8D5 /* Device.mm */,
1CEBD80B2716C37900A5254D /* ExportMacros.h */,
0D30F93829F1FAC50055D9F1 /* ExternalTexture.h */,
0D30F93629F1F94A0055D9F1 /* ExternalTexture.mm */,
1C0F41ED280940650005886D /* HardwareCapabilities.h */,
1C0F41EC280940650005886D /* HardwareCapabilities.mm */,
1C5ACAA0273A426D0095F8D5 /* Instance.h */,
Expand Down Expand Up @@ -581,6 +590,7 @@
isa = PBXGroup;
children = (
1CEBD82B2716CAFB00A5254D /* CoreFoundation.framework */,
0D30F93A29F1FBE40055D9F1 /* CoreVideo.framework */,
1CEBD82D2716CB1600A5254D /* Foundation.framework */,
664C92FC286A66090008D143 /* IOSurface.framework */,
1CBAB0912718CCA0006080BB /* JavaScriptCore.framework */,
Expand Down Expand Up @@ -692,6 +702,7 @@
buildActionMask = 2147483647;
files = (
1CEBD7E72716AFBA00A5254D /* WebGPU.h in Headers */,
0D30F93929F1FAC50055D9F1 /* ExternalTexture.h in Headers */,
973F784729C8A78200166C66 /* Pipeline.h in Headers */,
1C5ACAD3273A4C860095F8D5 /* WebGPUExt.h in Headers */,
);
Expand Down Expand Up @@ -933,6 +944,7 @@
1C5ACAC6273A426D0095F8D5 /* ComputePassEncoder.mm in Sources */,
1C5ACAC0273A426D0095F8D5 /* ComputePipeline.mm in Sources */,
1C5ACAC1273A426D0095F8D5 /* Device.mm in Sources */,
0D30F93729F1F94A0055D9F1 /* ExternalTexture.mm in Sources */,
1C0F41EE280940650005886D /* HardwareCapabilities.mm in Sources */,
1C5ACA94273A41C20095F8D5 /* Instance.mm in Sources */,
973F784829C8A78200166C66 /* Pipeline.mm in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions Source/WebGPU/WebGPU/APIConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import "ComputePassEncoder.h"
#import "ComputePipeline.h"
#import "Device.h"
#import "ExternalTexture.h"
#import "Instance.h"
#import "PipelineLayout.h"
#import "PresentationContext.h"
Expand Down Expand Up @@ -99,6 +100,11 @@ inline Device& fromAPI(WGPUDevice device)
return static_cast<Device&>(*device);
}

inline ExternalTexture& fromAPI(WGPUExternalTexture texture)
{
return static_cast<ExternalTexture&>(*texture);
}

inline Instance& fromAPI(WGPUInstance instance)
{
return static_cast<Instance&>(*instance);
Expand Down
Loading

0 comments on commit cb91b74

Please sign in to comment.