Changeset 241181 in webkit


Ignore:
Timestamp:
Feb 7, 2019 6:01:39 PM (5 years ago)
Author:
Justin Fan
Message:

[Web GPU] GPUDevice::createTexture implementation prototype
https://bugs.webkit.org/show_bug.cgi?id=194409
<rdar://problem/47894312>

Reviewed by Myles C. Maxfield.

Source/WebCore:

Test: textures-textureviews.html updated to test new functionality.

Implement GPUDevice::createTexture():

  • Modules/webgpu/WebGPUDevice.cpp:

(WebCore::WebGPUDevice::createTexture const):

  • Modules/webgpu/WebGPUDevice.h:
  • Modules/webgpu/WebGPUDevice.idl:
  • Modules/webgpu/WebGPUTexture.cpp:

(WebCore::WebGPUTexture::create): Modified to return non-nullable to match direction of API.
(WebCore::WebGPUTexture::WebGPUTexture):

  • Modules/webgpu/WebGPUTexture.h:

Metal backend MTLTextureDescriptor and MTLTexture creation:

  • platform/graphics/gpu/GPUDevice.cpp:

(WebCore::GPUDevice::tryCreateTexture const):

  • platform/graphics/gpu/GPUDevice.h:
  • platform/graphics/gpu/GPUTexture.h:
  • platform/graphics/gpu/cocoa/GPUTextureMetal.mm:

(WebCore::mtlTextureTypeForGPUTextureDescriptor):
(WebCore::mtlTextureUsageForGPUTextureUsageFlags):
(WebCore::storageModeForPixelFormatAndSampleCount):
(WebCore::tryCreateMtlTextureDescriptor):
(WebCore::GPUTexture::tryCreate):
(WebCore::GPUTexture::createDefaultTextureView): Add ObjC try/catch guards.

Add GPUUtils.h/cpp for shared utility functions:

  • SourcesCocoa.txt:
  • WebCore.xcodeproj/project.pbxproj:
  • platform/graphics/gpu/GPUUtils.h: Added. Moved platformTextureFormatForGPUTextureFormat here.
  • platform/graphics/gpu/cocoa/GPUSwapChainMetal.mm:

(WebCore::GPUSwapChain::setFormat):
(WebCore::platformTextureFormatForGPUTextureFormat): Moved to GPUUtils.

  • platform/graphics/gpu/cocoa/GPUUtilsMetal.mm: Added.

(WebCore::platformTextureFormatForGPUTextureFormat): Moved here to be referenced by multiple files.

LayoutTests:

Update textures-textureviews.html to WPT format and to test creation of textures via the GPUDevice.

  • webgpu/textures-textureviews-expected.txt:
  • webgpu/textures-textureviews.html:
Location:
trunk
Files:
16 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r241179 r241181  
     12019-02-07  Justin Fan  <justin_fan@apple.com>
     2
     3        [Web GPU] GPUDevice::createTexture implementation prototype
     4        https://bugs.webkit.org/show_bug.cgi?id=194409
     5        <rdar://problem/47894312>
     6
     7        Reviewed by Myles C. Maxfield.
     8
     9        Update textures-textureviews.html to WPT format and to test creation of textures via the GPUDevice.
     10
     11        * webgpu/textures-textureviews-expected.txt:
     12        * webgpu/textures-textureviews.html:
     13
    1142019-02-07  Shawn Roberts  <sroberts@apple.com>
    215
  • trunk/LayoutTests/webgpu/textures-textureviews-expected.txt

    r238382 r241181  
    1 PASS [object WebGPU] is defined.
    2 PASS Acquired next WebGPUTexture from WebGPURenderingContext.
    3 PASS Created default WebGPUTextureView from a WebGPUTexture
    4 All tests complete.
    5 PASS successfullyParsed is true
    61
    7 TEST COMPLETE
     2PASS Create texture view from swap chain.
     3PASS Create basic depth texture from device.
     4PASS Create basic 4x multisampled texture.
     5PASS Create basic 3D texture from device.
    86
  • trunk/LayoutTests/webgpu/textures-textureviews.html

    r238382 r241181  
    1 <!DOCTYPE html>
    2 <html>
    3 <script src="../resources/js-test-pre.js"></script>
    4 <script src="js/basic-webgpu-functions.js"></script>
     1<!DOCTYPE html><!-- webkit-test-runner [ experimental:WebGPUEnabled=true ] -->
     2<meta charset=utf-8>
     3<title>Basic GPUTexture Tests.</title>
     4<body>
     5<canvas width="400" height="400"></canvas>
     6<script src="js/webgpu-functions.js"></script>
     7<script src="../resources/testharness.js"></script>
     8<script src="../resources/testharnessreport.js"></script>
    59<script>
    6 if (window.testRunner)
    7     window.testRunner.dumpAsText();
     10const canvas = document.querySelector("canvas");   
     11let device, context;
    812
    9 function setUpNextTextureView() {
    10     let texture = context.getNextTexture();
    11     if (texture)
    12         testPassed("Acquired next WebGPUTexture from WebGPURenderingContext.");
    13     else {
    14         testFailed("Could not get next WebGPUTexture from WebGPURenderingContext!");
    15         return;
    16     }
     13let texSize = {
     14    width: canvas.width,
     15    height: canvas.height,
     16    depth: 1
     17};
    1718
    18     let textureView = texture.createDefaultTextureView();
    19     if (textureView)
    20         testPassed("Created default WebGPUTextureView from a WebGPUTexture");
    21     else {
    22         testFailed("Could not create default WebGPUTextureView!");
    23         return;
    24     }
    25 }
     19let texDescriptor = {
     20    size: texSize,
     21    arrayLayerCount: 1,
     22    mipLevelCount: 1,
     23    sampleCount: 1,
     24    dimension: "2d",
     25    format: "d32-float-s8-uint",
     26    usage: GPUTextureUsage.OUTPUT_ATTACHMENT
     27};
    2628
    27 // FIXME: Add tests for device.createTexture, WebGPUTextureDescriptor, and WebGPUTextureViewDescriptor.
     29promise_test(async () => {
     30    device = await getBasicDevice();
     31    context = createBasicContext(canvas, device);
    2832
    29 runWebGPUTests([setUpNextTextureView]);
     33    const texture = context.getNextTexture();
     34    assert_true(texture instanceof WebGPUTexture, "Successfully acquired next texture.");
    3035
    31 successfullyParsed = true;
     36    const textureView = texture.createDefaultTextureView();
     37    assert_true(textureView instanceof WebGPUTextureView, "Successfully created texture view from next texture.");
     38}, "Create texture view from swap chain.");
     39
     40promise_test(async () => {
     41    const depthTexture = device.createTexture(texDescriptor);
     42    assert_true(depthTexture instanceof WebGPUTexture, "Successfully created depth texture.");
     43}, "Create basic depth texture from device.");
     44
     45promise_test(async () => {
     46    texDescriptor.sampleCount = 4;
     47    texDescriptor.format = "r8g8b8a8-unorm";
     48    texDescriptor.usage = GPUTextureUsage.SAMPLED | GPUTextureUsage.TRANSFER_SRC
     49
     50    const multisampledTexture = device.createTexture(texDescriptor);
     51    assert_true(multisampledTexture instanceof WebGPUTexture, "Successfully created multisampled texture.");
     52}, "Create basic 4x multisampled texture.");
     53
     54promise_test(async () => {
     55    texDescriptor.size.depth = 3;
     56    texDescriptor.sampleCount = 1;
     57    texDescriptor.dimension = "3d";
     58
     59    const texture3d = device.createTexture(texDescriptor);
     60    assert_true(texture3d instanceof WebGPUTexture, "Successfully created basic 3D texture.");
     61}, "Create basic 3D texture from device.");
     62
     63// FIXME: Add tests for 1D textures, textureArrays, and WebGPUTextureViews.
    3264</script>
    33 <script src="../resources/js-test-post.js"></script>
     65</body>
    3466</html>
  • trunk/Source/WebCore/ChangeLog

    r241170 r241181  
     12019-02-07  Justin Fan  <justin_fan@apple.com>
     2
     3        [Web GPU] GPUDevice::createTexture implementation prototype
     4        https://bugs.webkit.org/show_bug.cgi?id=194409
     5        <rdar://problem/47894312>
     6
     7        Reviewed by Myles C. Maxfield.
     8
     9        Test: textures-textureviews.html updated to test new functionality.
     10
     11        Implement GPUDevice::createTexture():
     12        * Modules/webgpu/WebGPUDevice.cpp:
     13        (WebCore::WebGPUDevice::createTexture const):
     14        * Modules/webgpu/WebGPUDevice.h:
     15        * Modules/webgpu/WebGPUDevice.idl:
     16        * Modules/webgpu/WebGPUTexture.cpp:
     17        (WebCore::WebGPUTexture::create): Modified to return non-nullable to match direction of API.
     18        (WebCore::WebGPUTexture::WebGPUTexture):
     19        * Modules/webgpu/WebGPUTexture.h:
     20
     21        Metal backend MTLTextureDescriptor and MTLTexture creation:
     22        * platform/graphics/gpu/GPUDevice.cpp:
     23        (WebCore::GPUDevice::tryCreateTexture const):
     24        * platform/graphics/gpu/GPUDevice.h:
     25        * platform/graphics/gpu/GPUTexture.h:
     26        * platform/graphics/gpu/cocoa/GPUTextureMetal.mm:
     27        (WebCore::mtlTextureTypeForGPUTextureDescriptor):
     28        (WebCore::mtlTextureUsageForGPUTextureUsageFlags):
     29        (WebCore::storageModeForPixelFormatAndSampleCount):
     30        (WebCore::tryCreateMtlTextureDescriptor):
     31        (WebCore::GPUTexture::tryCreate):
     32        (WebCore::GPUTexture::createDefaultTextureView): Add ObjC try/catch guards.
     33
     34        Add GPUUtils.h/cpp for shared utility functions:
     35        * SourcesCocoa.txt:
     36        * WebCore.xcodeproj/project.pbxproj:
     37        * platform/graphics/gpu/GPUUtils.h: Added. Moved platformTextureFormatForGPUTextureFormat here.
     38        * platform/graphics/gpu/cocoa/GPUSwapChainMetal.mm:
     39        (WebCore::GPUSwapChain::setFormat):
     40        (WebCore::platformTextureFormatForGPUTextureFormat): Moved to GPUUtils.
     41        * platform/graphics/gpu/cocoa/GPUUtilsMetal.mm: Added.
     42        (WebCore::platformTextureFormatForGPUTextureFormat): Moved here to be referenced by multiple files.
     43
    1442019-02-07  Sihui Liu  <sihui_liu@apple.com>
    245
  • trunk/Source/WebCore/Modules/webgpu/WebGPUDevice.cpp

    r240807 r241181  
    3737#include "GPURenderPipelineDescriptor.h"
    3838#include "GPUShaderModuleDescriptor.h"
     39#include "GPUTextureDescriptor.h"
    3940#include "Logging.h"
    4041#include "WebGPUBindGroup.h"
     
    5354#include "WebGPUShaderModule.h"
    5455#include "WebGPUShaderModuleDescriptor.h"
     56#include "WebGPUTexture.h"
    5557#include <wtf/Variant.h>
    5658
     
    7779        return WebGPUBuffer::create(buffer.releaseNonNull());
    7880    return nullptr;
     81}
     82
     83Ref<WebGPUTexture> WebGPUDevice::createTexture(GPUTextureDescriptor&& descriptor) const
     84{
     85    auto texture = m_device->tryCreateTexture(WTFMove(descriptor));
     86    return WebGPUTexture::create(WTFMove(texture));
    7987}
    8088
  • trunk/Source/WebCore/Modules/webgpu/WebGPUDevice.h

    r239853 r241181  
    4848class WebGPURenderPipeline;
    4949class WebGPUShaderModule;
     50class WebGPUTexture;
    5051
     52struct GPUTextureDescriptor;
    5153struct WebGPUBindGroupDescriptor;
    5254struct WebGPUPipelineLayoutDescriptor;
     
    6264
    6365    RefPtr<WebGPUBuffer> createBuffer(WebGPUBufferDescriptor&&) const;
     66    Ref<WebGPUTexture> createTexture(GPUTextureDescriptor&&) const;
    6467
    6568    Ref<WebGPUBindGroupLayout> createBindGroupLayout(WebGPUBindGroupLayoutDescriptor&&) const;
  • trunk/Source/WebCore/Modules/webgpu/WebGPUDevice.idl

    r240748 r241181  
    3333
    3434    WebGPUBuffer createBuffer(WebGPUBufferDescriptor descriptor);
     35    WebGPUTexture createTexture(GPUTextureDescriptor descriptor);
    3536
    3637    WebGPUBindGroupLayout createBindGroupLayout(WebGPUBindGroupLayoutDescriptor descriptor);
  • trunk/Source/WebCore/Modules/webgpu/WebGPUTexture.cpp

    r239535 r241181  
    3333namespace WebCore {
    3434
    35 RefPtr<WebGPUTexture> WebGPUTexture::create(RefPtr<GPUTexture>&& texture)
     35Ref<WebGPUTexture> WebGPUTexture::create(RefPtr<GPUTexture>&& texture)
    3636{
    37     return texture ? adoptRef(new WebGPUTexture(texture.releaseNonNull())) : nullptr;
     37    return adoptRef(*new WebGPUTexture(WTFMove(texture)));
    3838}
    3939
    40 WebGPUTexture::WebGPUTexture(Ref<GPUTexture>&& texture)
     40WebGPUTexture::WebGPUTexture(RefPtr<GPUTexture>&& texture)
    4141    : m_texture(WTFMove(texture))
    4242{
  • trunk/Source/WebCore/Modules/webgpu/WebGPUTexture.h

    r238741 r241181  
    3939class WebGPUTexture : public RefCounted<WebGPUTexture> {
    4040public:
    41     static RefPtr<WebGPUTexture> create(RefPtr<GPUTexture>&&);
     41    static Ref<WebGPUTexture> create(RefPtr<GPUTexture>&&);
    4242
    4343    RefPtr<WebGPUTextureView> createDefaultTextureView();
    4444
    4545private:
    46     explicit WebGPUTexture(Ref<GPUTexture>&&);
     46    explicit WebGPUTexture(RefPtr<GPUTexture>&&);
    4747
    48     Ref<GPUTexture> m_texture;
     48    RefPtr<GPUTexture> m_texture;
    4949};
    5050
  • trunk/Source/WebCore/SourcesCocoa.txt

    r241105 r241181  
    332332platform/graphics/gpu/cocoa/GPUSwapChainMetal.mm
    333333platform/graphics/gpu/cocoa/GPUTextureMetal.mm
     334platform/graphics/gpu/cocoa/GPUUtilsMetal.mm
    334335platform/graphics/gpu/Texture.cpp
    335336platform/graphics/gpu/TilingData.cpp
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r241105 r241181  
    1406714067                D06C0D8D0CFD11460065F43F /* RemoveFormatCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RemoveFormatCommand.h; sourceTree = "<group>"; };
    1406814068                D06C0D8E0CFD11460065F43F /* RemoveFormatCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RemoveFormatCommand.cpp; sourceTree = "<group>"; };
     14069                D06EF552220BA26A0018724E /* GPUUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPUUtils.h; sourceTree = "<group>"; };
     14070                D06EF553220BA26A0018724E /* GPUUtilsMetal.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = GPUUtilsMetal.mm; sourceTree = "<group>"; };
    1406914071                D07DEAB70A36554A00CA30F8 /* InsertListCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = InsertListCommand.cpp; sourceTree = "<group>"; };
    1407014072                D07DEAB80A36554A00CA30F8 /* InsertListCommand.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = InsertListCommand.h; sourceTree = "<group>"; };
     
    1850418506                                312FF8C321A4C2F300EB199D /* GPUTextureFormat.h */,
    1850518507                                D026F485220A477200AC5F49 /* GPUTextureUsage.h */,
     18508                                D06EF552220BA26A0018724E /* GPUUtils.h */,
    1850618509                                D0D8649B21BA1C2D003C983C /* GPUVertexAttributeDescriptor.h */,
    1850718510                                D0D8649C21BA1CE8003C983C /* GPUVertexInputDescriptor.h */,
     
    2618926192                                D087CE3E21ACA94200BDE174 /* GPUSwapChainMetal.mm */,
    2619026193                                D087CE3F21ACA94200BDE174 /* GPUTextureMetal.mm */,
     26194                                D06EF553220BA26A0018724E /* GPUUtilsMetal.mm */,
    2619126195                        );
    2619226196                        path = cocoa;
  • trunk/Source/WebCore/platform/graphics/gpu/GPUDevice.cpp

    r240180 r241181  
    4040#include "GPUShaderModule.h"
    4141#include "GPUShaderModuleDescriptor.h"
     42#include "GPUTexture.h"
     43#include "GPUTextureDescriptor.h"
    4244
    4345namespace WebCore {
     
    4648{
    4749    return GPUBuffer::create(*this, WTFMove(descriptor));
     50}
     51
     52RefPtr<GPUTexture> GPUDevice::tryCreateTexture(GPUTextureDescriptor&& descriptor) const
     53{
     54    return GPUTexture::tryCreate(*this, WTFMove(descriptor));
    4855}
    4956
  • trunk/Source/WebCore/platform/graphics/gpu/GPUDevice.h

    r240807 r241181  
    4747class GPURenderPipeline;
    4848class GPUShaderModule;
     49class GPUTexture;
    4950
    5051struct GPUBindGroupLayoutDescriptor;
     
    5455struct GPURequestAdapterOptions;
    5556struct GPUShaderModuleDescriptor;
     57struct GPUTextureDescriptor;
    5658
    5759class GPUDevice : public RefCounted<GPUDevice> {
     
    6062
    6163    RefPtr<GPUBuffer> createBuffer(GPUBufferDescriptor&&) const;
     64    RefPtr<GPUTexture> tryCreateTexture(GPUTextureDescriptor&&) const;
    6265
    6366    RefPtr<GPUBindGroupLayout> tryCreateBindGroupLayout(GPUBindGroupLayoutDescriptor&&) const;
  • trunk/Source/WebCore/platform/graphics/gpu/GPUTexture.h

    r238629 r241181  
    3636namespace WebCore {
    3737
     38class GPUDevice;
     39
     40struct GPUTextureDescriptor;
     41
    3842using PlatformTexture = MTLTexture;
    3943using PlatformTextureSmartPtr = RetainPtr<MTLTexture>;
     
    4145class GPUTexture : public RefCounted<GPUTexture> {
    4246public:
     47    static RefPtr<GPUTexture> tryCreate(const GPUDevice&, GPUTextureDescriptor&&);
    4348    static Ref<GPUTexture> create(PlatformTextureSmartPtr&&);
    4449
  • trunk/Source/WebCore/platform/graphics/gpu/GPUUtils.h

    r241180 r241181  
    11/*
    2  * Copyright (C) 2018 Apple Inc. All rights reserved.
     2 * Copyright (C) 2019 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2828#if ENABLE(WEBGPU)
    2929
    30 #include "GPUTexture.h"
    31 
    32 #include <wtf/RefCounted.h>
    33 #include <wtf/RefPtr.h>
     30#include "GPUTextureFormat.h"
    3431
    3532namespace WebCore {
    3633
    37 class WebGPUTextureView;
    38 
    39 class WebGPUTexture : public RefCounted<WebGPUTexture> {
    40 public:
    41     static RefPtr<WebGPUTexture> create(RefPtr<GPUTexture>&&);
    42 
    43     RefPtr<WebGPUTextureView> createDefaultTextureView();
    44 
    45 private:
    46     explicit WebGPUTexture(Ref<GPUTexture>&&);
    47 
    48     Ref<GPUTexture> m_texture;
    49 };
     34PlatformTextureFormat platformTextureFormatForGPUTextureFormat(GPUTextureFormat);
    5035
    5136} // namespace WebCore
  • trunk/Source/WebCore/platform/graphics/gpu/cocoa/GPUSwapChainMetal.mm

    r241048 r241181  
    3232#import "GPUTexture.h"
    3333#import "GPUTextureFormat.h"
     34#import "GPUUtils.h"
    3435#import "Logging.h"
    3536#import "WebGPULayer.h"
    36 
    3737#import <Metal/Metal.h>
    3838#import <QuartzCore/QuartzCore.h>
     
    8181}
    8282
    83 static Optional<PlatformTextureFormat> platformTextureFormatForGPUTextureFormat(GPUTextureFormat format)
    84 {
    85     switch (format) {
    86     case GPUTextureFormat::R8g8b8a8Unorm:
    87         return MTLPixelFormatRGBA8Unorm;
    88     case GPUTextureFormat::R8g8b8a8Uint:
    89         return MTLPixelFormatRGBA8Uint;
    90     case GPUTextureFormat::B8g8r8a8Unorm:
    91         return MTLPixelFormatBGRA8Unorm;
    92     case GPUTextureFormat::D32FloatS8Uint:
    93         return MTLPixelFormatDepth32Float_Stencil8;
    94     default:
    95         LOG(WebGPU, "GPUSwapChain::setFormat(): Invalid texture format specified!");
    96         return WTF::nullopt;
    97     }
    98 }
    99 
    10083void GPUSwapChain::setFormat(GPUTextureFormat format)
    10184{
    102     auto result = platformTextureFormatForGPUTextureFormat(format);
    103     if (!result)
    104         return;
     85    auto mtlFormat = static_cast<MTLPixelFormat>(platformTextureFormatForGPUTextureFormat(format));
    10586
    106     auto mtlResult = static_cast<MTLPixelFormat>(result.value());
    107 
    108     switch (mtlResult) {
     87    switch (mtlFormat) {
    10988    case MTLPixelFormatBGRA8Unorm:
    11089    // FIXME: Add the other supported swap layer formats as they are added to GPU spec.
    11190    //  MTLPixelFormatBGRA8Unorm_sRGB, MTLPixelFormatRGBA16Float, MTLPixelFormatBGRA10_XR, and MTLPixelFormatBGRA10_XR_sRGB.
    112         [m_platformSwapLayer setPixelFormat:mtlResult];
     91        [m_platformSwapLayer setPixelFormat:mtlFormat];
    11392        return;
    11493    default:
  • trunk/Source/WebCore/platform/graphics/gpu/cocoa/GPUTextureMetal.mm

    r239094 r241181  
    2929#if ENABLE(WEBGPU)
    3030
     31#import "GPUDevice.h"
     32#import "GPUTextureDescriptor.h"
     33#import "GPUUtils.h"
    3134#import "Logging.h"
    32 
    3335#import <Metal/Metal.h>
    3436#import <wtf/BlockObjCExceptions.h>
     37#import <wtf/Optional.h>
    3538
    3639namespace WebCore {
     40
     41static MTLTextureType mtlTextureTypeForGPUTextureDescriptor(const GPUTextureDescriptor& descriptor)
     42{
     43    switch (descriptor.dimension) {
     44    case GPUTextureDimension::_1d:
     45        return (descriptor.arrayLayerCount == 1) ? MTLTextureType1D : MTLTextureType1DArray;
     46    case GPUTextureDimension::_2d: {
     47        if (descriptor.arrayLayerCount == 1)
     48            return (descriptor.sampleCount == 1) ? MTLTextureType2D : MTLTextureType2DMultisample;
     49
     50        return MTLTextureType2DArray;
     51    }
     52    case GPUTextureDimension::_3d:
     53        return MTLTextureType3D;
     54    }
     55}
     56
     57static Optional<MTLTextureUsage> mtlTextureUsageForGPUTextureUsageFlags(GPUTextureUsageFlags flags)
     58{
     59    MTLTextureUsage usage = MTLTextureUsageUnknown;
     60
     61    if (flags & GPUTextureUsage::Storage)
     62        usage |= MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead | MTLTextureUsagePixelFormatView;
     63
     64    if (flags & GPUTextureUsage::Sampled) {
     65        // SAMPLED is a read-only usage.
     66        if (flags & GPUTextureUsage::Storage)
     67            return WTF::nullopt;
     68
     69        usage |= MTLTextureUsageShaderRead | MTLTextureUsagePixelFormatView;
     70    }
     71
     72    if (flags & GPUTextureUsage::OutputAttachment)
     73        usage |= MTLTextureUsageRenderTarget;
     74
     75    return usage;
     76}
     77
     78static MTLStorageMode storageModeForPixelFormatAndSampleCount(MTLPixelFormat format, unsigned long samples)
     79{
     80    // Depth, Stencil, DepthStencil, and Multisample textures must be allocated with the MTLStorageModePrivate resource option.
     81    if (format == MTLPixelFormatDepth32Float_Stencil8 || samples > 1)
     82        return MTLStorageModePrivate;
     83
     84    return MTLStorageModeManaged;
     85}
     86
     87static RetainPtr<MTLTextureDescriptor> tryCreateMtlTextureDescriptor(const char* const functionName, const GPUTextureDescriptor&& descriptor)
     88{
     89#if LOG_DISABLED
     90    UNUSED_PARAM(functionName);
     91#endif
     92
     93    RetainPtr<MTLTextureDescriptor> mtlDescriptor;
     94
     95    BEGIN_BLOCK_OBJC_EXCEPTIONS;
     96
     97    mtlDescriptor = adoptNS([MTLTextureDescriptor new]);
     98
     99    END_BLOCK_OBJC_EXCEPTIONS;
     100
     101    if (!mtlDescriptor) {
     102        LOG(WebGPU, "%s: Unable to create new MTLTextureDescriptor!", functionName);
     103        return nullptr;
     104    }
     105
     106    // FIXME: Add more validation as constraints are added to spec.
     107    auto pixelFormat = static_cast<MTLPixelFormat>(platformTextureFormatForGPUTextureFormat(descriptor.format));
     108
     109    auto usage = mtlTextureUsageForGPUTextureUsageFlags(descriptor.usage);
     110    if (!usage) {
     111        LOG(WebGPU, "%s: Invalid GPUTextureUsageFlags!", functionName);
     112        return nullptr;
     113    }
     114
     115    auto storageMode = storageModeForPixelFormatAndSampleCount(pixelFormat, descriptor.sampleCount);
     116
     117    BEGIN_BLOCK_OBJC_EXCEPTIONS;
     118
     119    [mtlDescriptor setWidth:descriptor.size.width];
     120    [mtlDescriptor setHeight:descriptor.size.height];
     121    [mtlDescriptor setDepth:descriptor.size.depth];
     122    [mtlDescriptor setArrayLength:descriptor.arrayLayerCount];
     123    [mtlDescriptor setMipmapLevelCount:descriptor.mipLevelCount];
     124    [mtlDescriptor setSampleCount:descriptor.sampleCount];
     125    [mtlDescriptor setTextureType:mtlTextureTypeForGPUTextureDescriptor(descriptor)];
     126    [mtlDescriptor setPixelFormat:pixelFormat];
     127    [mtlDescriptor setUsage:*usage];
     128
     129    [mtlDescriptor setStorageMode:storageMode];
     130
     131    END_BLOCK_OBJC_EXCEPTIONS;
     132
     133    return mtlDescriptor;
     134}
     135
     136RefPtr<GPUTexture> GPUTexture::tryCreate(const GPUDevice& device, GPUTextureDescriptor&& descriptor)
     137{
     138    const char* const functionName = "GPUTexture::tryCreate()";
     139
     140    if (!device.platformDevice()) {
     141        LOG(WebGPU, "%s: Invalid GPUDevice!", functionName);
     142        return nullptr;
     143    }
     144
     145    auto mtlDescriptor = tryCreateMtlTextureDescriptor(functionName, WTFMove(descriptor));
     146    if (!mtlDescriptor)
     147        return nullptr;
     148
     149    RetainPtr<MTLTexture> mtlTexture;
     150
     151    BEGIN_BLOCK_OBJC_EXCEPTIONS;
     152
     153    mtlTexture = adoptNS([device.platformDevice() newTextureWithDescriptor:mtlDescriptor.get()]);
     154
     155    END_BLOCK_OBJC_EXCEPTIONS;
     156
     157    if (!mtlTexture) {
     158        LOG(WebGPU, "%s: Unable to create MTLTexture!", functionName);
     159        return nullptr;
     160    }
     161
     162    return adoptRef(new GPUTexture(WTFMove(mtlTexture)));
     163}
    37164
    38165Ref<GPUTexture> GPUTexture::create(PlatformTextureSmartPtr&& texture)
     
    50177    RetainPtr<MTLTexture> texture;
    51178
     179    BEGIN_BLOCK_OBJC_EXCEPTIONS;
     180
    52181    texture = adoptNS([m_platformTexture newTextureViewWithPixelFormat:m_platformTexture.get().pixelFormat]);
     182
     183    END_BLOCK_OBJC_EXCEPTIONS;
    53184
    54185    if (!texture) {
  • trunk/Source/WebCore/platform/graphics/gpu/cocoa/GPUUtilsMetal.mm

    r241180 r241181  
    11/*
    2  * Copyright (C) 2018 Apple Inc. All rights reserved.
     2 * Copyright (C) 2019 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2424 */
    2525
    26 #pragma once
     26#import "config.h"
     27#import "GPUUtils.h"
    2728
    2829#if ENABLE(WEBGPU)
    2930
    30 #include "GPUTexture.h"
    31 
    32 #include <wtf/RefCounted.h>
    33 #include <wtf/RefPtr.h>
     31#import <Metal/Metal.h>
    3432
    3533namespace WebCore {
    3634
    37 class WebGPUTextureView;
    38 
    39 class WebGPUTexture : public RefCounted<WebGPUTexture> {
    40 public:
    41     static RefPtr<WebGPUTexture> create(RefPtr<GPUTexture>&&);
    42 
    43     RefPtr<WebGPUTextureView> createDefaultTextureView();
    44 
    45 private:
    46     explicit WebGPUTexture(Ref<GPUTexture>&&);
    47 
    48     Ref<GPUTexture> m_texture;
    49 };
     35PlatformTextureFormat platformTextureFormatForGPUTextureFormat(GPUTextureFormat format)
     36{
     37    switch (format) {
     38    case GPUTextureFormat::R8g8b8a8Unorm:
     39        return MTLPixelFormatRGBA8Unorm;
     40    case GPUTextureFormat::R8g8b8a8Uint:
     41        return MTLPixelFormatRGBA8Uint;
     42    case GPUTextureFormat::B8g8r8a8Unorm:
     43        return MTLPixelFormatBGRA8Unorm;
     44    case GPUTextureFormat::D32FloatS8Uint:
     45        return MTLPixelFormatDepth32Float_Stencil8;
     46    }
     47}
    5048
    5149} // namespace WebCore
Note: See TracChangeset for help on using the changeset viewer.