You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Init macros for designated initializers in C++ #579
On bkaradzic/bgfx#3544 there is an example of the following code that doesn't compile in C++ but should be possible to make work if the INIT macros used designated initializers. Maybe we should consider doing this (where designated initializers are availble, so C++17?)
C++ issue, I used named parameters, but WGPU_*_INIT is using struct / unnamed...
I don't understand what the issue is (unless it's the typo that uses WGPU_STORAGE_TEXTURE_BINDING_LAYOUT_INIT instead of WGPU_TEXTURE_BINDING_LAYOUT_INIT). It should be totally valid to put non-designated initializers inside designated initializers. I think after macro expansion it looks like this:
There's further discussion on the issue hinting that the real issue might just be that WGPU_SAMPLER_BINDING_LAYOUT_INIT sets Undefined (not BindingNotUsed). The reason for this is to use it like this:
WGPUBindGroupLayoutEntry entry = WGPU_BIND_GROUP_LAYOUT_ENTRY_INIT; // All set to BindingNotUsed
entry.texture = WGPU_TEXTURE_BINDING_LAYOUT_INIT; // I'm using the texture field
entry.texture.multisampled = true; // But I also need to modify it a bit
There's no way in C to do like:
WGPUBindGroupEntryentry= {
... WGPU_BIND_GROUP_LAYOUT_ENTRY_INIT, // Use the defaults
.texture= { // But override .texture
... WGPU_TEXTURE_BINDING_LAYOUT_INIT, // Use the defaults
.multisampled= true, // But override .multisampled
},
};
TBH the problem is that this is a C header and using it is a pain from any language. C++ should use C++ bindings and there should be stable supported C++ bindings even if they're very basic. Then you can do things like we already do in Dawn:
// Everything not mentioned here gets set to the default defined in the C++ struct definition
wgpu::BindGroupLayoutEntry entry = {
.texture = { .multisampled = true }, // This field is used so it defaults to Undefined (not BindingNotUsed)// Other fields default to BindingNotUsed
};
Technically we could put inline defaults in the header if using it from C++, but (1) that's a breaking change and (2) it's weird for app code to do something different in C than in C++.
TBH the problem is that this is a C header and using it is a pain from any language. C++ should use C++ bindings and there should be stable supported C++ bindings even if they're very basic. Then you can do things like we already do in Dawn:
No. Even thought I'm using C++, I prefer to use C headers, because C++ headers introduce stylistic choices that are not in alignment with my stylistic choices. At least C is neutral.
If any of these structs contained WGPUStringView label it would fail validation later. WGPU_STRING_VIEW_INIT is { NULL, SIZE_MAX } and validation treats it as not used. Where { NULL, 0 } is considered used but empty string.
Activity
kainino0x commentedon Jan 21, 2026
I don't understand what the issue is (unless it's the typo that uses
WGPU_STORAGE_TEXTURE_BINDING_LAYOUT_INITinstead ofWGPU_TEXTURE_BINDING_LAYOUT_INIT). It should be totally valid to put non-designated initializers inside designated initializers. I think after macro expansion it looks like this:Which seems to be totally valid.
There's further discussion on the issue hinting that the real issue might just be that
WGPU_SAMPLER_BINDING_LAYOUT_INITsetsUndefined(notBindingNotUsed). The reason for this is to use it like this:There's no way in C to do like:
TBH the problem is that this is a C header and using it is a pain from any language. C++ should use C++ bindings and there should be stable supported C++ bindings even if they're very basic. Then you can do things like we already do in Dawn:
Technically we could put inline defaults in the header if using it from C++, but (1) that's a breaking change and (2) it's weird for app code to do something different in C than in C++.
bkaradzic commentedon Jan 22, 2026
initBufferBinding3below doesn't work because it uses named fields, andWGPU_*_INITmacros have named fields commented out.gcc -std=c++20 -I<webgpu.h> test.cpp
No. Even thought I'm using C++, I prefer to use C headers, because C++ headers introduce stylistic choices that are not in alignment with my stylistic choices. At least C is neutral.
bkaradzic commentedon Jan 22, 2026
For example this zero initialization is not consistent, and can't be used for all structs.
If any of these structs contained
WGPUStringViewlabel it would fail validation later.WGPU_STRING_VIEW_INITis{ NULL, SIZE_MAX }and validation treats it as not used. Where{ NULL, 0 }is considered used but empty string.