Bug 237861 - [WebGPU] Repeated calls to wgpuDeviceGetQueue() are supposed to return the same pointer
Summary: [WebGPU] Repeated calls to wgpuDeviceGetQueue() are supposed to return the sa...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Myles C. Maxfield
URL:
Keywords: InRadar
Depends on:
Blocks: 237583 237864
  Show dependency treegraph
 
Reported: 2022-03-14 18:00 PDT by Myles C. Maxfield
Modified: 2022-03-15 23:10 PDT (History)
4 users (show)

See Also:


Attachments
Patch (5.77 KB, patch)
2022-03-14 18:02 PDT, Myles C. Maxfield
kkinnunen: review+
ews-feeder: commit-queue-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Myles C. Maxfield 2022-03-14 18:00:28 PDT
[WebGPU] Repeated calls to wgpuDeviceGetQueue() are supposed to return the same pointer
Comment 1 Myles C. Maxfield 2022-03-14 18:02:08 PDT
Created attachment 454644 [details]
Patch
Comment 2 Kimmo Kinnunen 2022-03-15 03:22:43 PDT
Comment on attachment 454644 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=454644&action=review

> Source/WebGPU/WebGPU/Adapter.mm:123
>      // See the comment in Device::setLabel() about why we're not setting the label here.

I think you can remove this comment.
For all intents and purposes, passing label to the device constructor counts as initialising, e.g setting, the label for the device.

> Source/WebGPU/WebGPU/Device.mm:56
> +    if (deviceLabel && strlen(deviceLabel))

nit: we calculate the length of the label for the purpose of "isempty"
Comment 3 Kimmo Kinnunen 2022-03-15 06:14:17 PDT
Comment on attachment 454644 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=454644&action=review

> Source/WebGPU/WebGPU/Adapter.mm:171
> +            callback(status, new WGPUDeviceImpl { device.releaseNonNull(), { queue } }, message);

btw: Is there a reason for this pattern where we create new wrapper objects every time we communicate with another layer in the sw stack?

I think it's employed here (invoking the wgpu client callback) as well as internally ?
Comment 4 Myles C. Maxfield 2022-03-15 14:03:26 PDT
Comment on attachment 454644 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=454644&action=review

>> Source/WebGPU/WebGPU/Adapter.mm:123
>>      // See the comment in Device::setLabel() about why we're not setting the label here.
> 
> I think you can remove this comment.
> For all intents and purposes, passing label to the device constructor counts as initialising, e.g setting, the label for the device.

Good point.

>> Source/WebGPU/WebGPU/Adapter.mm:171
>> +            callback(status, new WGPUDeviceImpl { device.releaseNonNull(), { queue } }, message);
> 
> btw: Is there a reason for this pattern where we create new wrapper objects every time we communicate with another layer in the sw stack?
> 
> I think it's employed here (invoking the wgpu client callback) as well as internally ?

There's a (bad) reason. Ideas for improvements are welcome.

The reason is that the shared WebGPU.h header has:

typedef struct WGPUDeviceImpl* WGPUDevice;

However, it would be convenient if we could have our own names like this:

namespace WebGPU {
class Device {
    ...
    id<MTLDevice> m_device;
    ...
}
}

I thought of 3 possible solutions:

1. Use reinterpret_cast<>() to freely convert between WGPUDeviceImpl* and WebGPU::Device*. This breaks C++'s type system, and is technically UB (I think) so I was hoping to avoid this option.

2. Don't have nice names, and don't use "namespace WebGPU":

struct WGPUDeviceImpl {
    ...
    id<MTLDevice> m_device;
    ...
}

This is unfortunate because it foregoes C++'s built-in namespace support in favor of prefixes, and isn't the pattern we use for the rest of WebKit

3. Do a double-pointer-indirection:

struct WGPUDeviceImpl {
    Ref<WebGPU::Device> device;
};

This isn't great for performance (though it's probably negligible) and has a somewhat bad smell about having two types which mean the same thing.

For the initial implementation, I chose option 3, under the assumption that it would be easy/mechanical to change later if a better option presents itself.
Comment 5 Myles C. Maxfield 2022-03-15 14:30:25 PDT
Committed r291315 (248454@trunk): <https://commits.webkit.org/248454@trunk>
Comment 6 Myles C. Maxfield 2022-03-15 14:30:48 PDT
Comment on attachment 454644 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=454644&action=review

>>> Source/WebGPU/WebGPU/Adapter.mm:171
>>> +            callback(status, new WGPUDeviceImpl { device.releaseNonNull(), { queue } }, message);
>> 
>> btw: Is there a reason for this pattern where we create new wrapper objects every time we communicate with another layer in the sw stack?
>> 
>> I think it's employed here (invoking the wgpu client callback) as well as internally ?
> 
> There's a (bad) reason. Ideas for improvements are welcome.
> 
> The reason is that the shared WebGPU.h header has:
> 
> typedef struct WGPUDeviceImpl* WGPUDevice;
> 
> However, it would be convenient if we could have our own names like this:
> 
> namespace WebGPU {
> class Device {
>     ...
>     id<MTLDevice> m_device;
>     ...
> }
> }
> 
> I thought of 3 possible solutions:
> 
> 1. Use reinterpret_cast<>() to freely convert between WGPUDeviceImpl* and WebGPU::Device*. This breaks C++'s type system, and is technically UB (I think) so I was hoping to avoid this option.
> 
> 2. Don't have nice names, and don't use "namespace WebGPU":
> 
> struct WGPUDeviceImpl {
>     ...
>     id<MTLDevice> m_device;
>     ...
> }
> 
> This is unfortunate because it foregoes C++'s built-in namespace support in favor of prefixes, and isn't the pattern we use for the rest of WebKit
> 
> 3. Do a double-pointer-indirection:
> 
> struct WGPUDeviceImpl {
>     Ref<WebGPU::Device> device;
> };
> 
> This isn't great for performance (though it's probably negligible) and has a somewhat bad smell about having two types which mean the same thing.
> 
> For the initial implementation, I chose option 3, under the assumption that it would be easy/mechanical to change later if a better option presents itself.

I filed https://bugs.webkit.org/show_bug.cgi?id=237921 about it.
Comment 7 Radar WebKit Bug Importer 2022-03-15 14:31:16 PDT
<rdar://problem/90330891>
Comment 8 Dan Glastonbury 2022-03-15 23:09:59 PDT
Comment on attachment 454644 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=454644&action=review

>>>> Source/WebGPU/WebGPU/Adapter.mm:171
>>>> +            callback(status, new WGPUDeviceImpl { device.releaseNonNull(), { queue } }, message);
>>> 
>>> btw: Is there a reason for this pattern where we create new wrapper objects every time we communicate with another layer in the sw stack?
>>> 
>>> I think it's employed here (invoking the wgpu client callback) as well as internally ?
>> 
>> There's a (bad) reason. Ideas for improvements are welcome.
>> 
>> The reason is that the shared WebGPU.h header has:
>> 
>> typedef struct WGPUDeviceImpl* WGPUDevice;
>> 
>> However, it would be convenient if we could have our own names like this:
>> 
>> namespace WebGPU {
>> class Device {
>>     ...
>>     id<MTLDevice> m_device;
>>     ...
>> }
>> }
>> 
>> I thought of 3 possible solutions:
>> 
>> 1. Use reinterpret_cast<>() to freely convert between WGPUDeviceImpl* and WebGPU::Device*. This breaks C++'s type system, and is technically UB (I think) so I was hoping to avoid this option.
>> 
>> 2. Don't have nice names, and don't use "namespace WebGPU":
>> 
>> struct WGPUDeviceImpl {
>>     ...
>>     id<MTLDevice> m_device;
>>     ...
>> }
>> 
>> This is unfortunate because it foregoes C++'s built-in namespace support in favor of prefixes, and isn't the pattern we use for the rest of WebKit
>> 
>> 3. Do a double-pointer-indirection:
>> 
>> struct WGPUDeviceImpl {
>>     Ref<WebGPU::Device> device;
>> };
>> 
>> This isn't great for performance (though it's probably negligible) and has a somewhat bad smell about having two types which mean the same thing.
>> 
>> For the initial implementation, I chose option 3, under the assumption that it would be easy/mechanical to change later if a better option presents itself.
> 
> I filed https://bugs.webkit.org/show_bug.cgi?id=237921 about it.

Can you forward decl the namespaced type? eg:

namespace WebGPU { class Device; }
typedef WebGPU::Device* WGPUDevice;
Comment 9 Dan Glastonbury 2022-03-15 23:10:01 PDT
Comment on attachment 454644 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=454644&action=review

>>>> Source/WebGPU/WebGPU/Adapter.mm:171
>>>> +            callback(status, new WGPUDeviceImpl { device.releaseNonNull(), { queue } }, message);
>>> 
>>> btw: Is there a reason for this pattern where we create new wrapper objects every time we communicate with another layer in the sw stack?
>>> 
>>> I think it's employed here (invoking the wgpu client callback) as well as internally ?
>> 
>> There's a (bad) reason. Ideas for improvements are welcome.
>> 
>> The reason is that the shared WebGPU.h header has:
>> 
>> typedef struct WGPUDeviceImpl* WGPUDevice;
>> 
>> However, it would be convenient if we could have our own names like this:
>> 
>> namespace WebGPU {
>> class Device {
>>     ...
>>     id<MTLDevice> m_device;
>>     ...
>> }
>> }
>> 
>> I thought of 3 possible solutions:
>> 
>> 1. Use reinterpret_cast<>() to freely convert between WGPUDeviceImpl* and WebGPU::Device*. This breaks C++'s type system, and is technically UB (I think) so I was hoping to avoid this option.
>> 
>> 2. Don't have nice names, and don't use "namespace WebGPU":
>> 
>> struct WGPUDeviceImpl {
>>     ...
>>     id<MTLDevice> m_device;
>>     ...
>> }
>> 
>> This is unfortunate because it foregoes C++'s built-in namespace support in favor of prefixes, and isn't the pattern we use for the rest of WebKit
>> 
>> 3. Do a double-pointer-indirection:
>> 
>> struct WGPUDeviceImpl {
>>     Ref<WebGPU::Device> device;
>> };
>> 
>> This isn't great for performance (though it's probably negligible) and has a somewhat bad smell about having two types which mean the same thing.
>> 
>> For the initial implementation, I chose option 3, under the assumption that it would be easy/mechanical to change later if a better option presents itself.
> 
> I filed https://bugs.webkit.org/show_bug.cgi?id=237921 about it.

Can you forward decl the namespaced type? eg:

namespace WebGPU { class Device; }
typedef WebGPU::Device* WGPUDevice;