-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add INIT macros with default values #422
Add INIT macros with default values #422
Conversation
I spotted one subtlety that the proposed approach does not handle properly: Fields of Let's take a simplified example: struct WGPUBindGroupLayoutEntry {
// [...]
WGPUBufferBindingLayout buffer;
WGPUTextureBindingLayout texture;
}
struct WGPUBufferBindingLayout {
// [...]
WGPUBufferBindingType type;
}
struct WGPUBufferBindingLayout {
// [...]
WGPUTextureSampleType sampleType;
} This means that when initializing the I see 2 options:
|
Another limitation spotted: |
Wow, thank you so much!! I have been working through all of the resolved changes and many tentative changes but I had been procrastinating on this one because it would be a lot of work :) Will start reviewing soon. |
I think this would be great.
I have been planning to use Option B. We actually started doing this already in Dawn: https://dawn-review.googlesource.com/c/dawn/+/214854, here for aside: In C++ it occurs to me we could have the default values in
Hmm, I hope we don't run into problems with the INIT design. When would we need to initialize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not review thoroughly yet but it is looking quite good! Just a few small notes for things I saw so far.
BTW I filed #423 as a possible (noncritical) followup.
- Remove TRUE/FALSE internal macros - Remove duplicate definition of COMMA macro - Set default callback mode to WaitAnyOnly - Set default optional bool to Undefined - Set default vertex step mode to NotUsed
Ok! I've applied it for all occurences of "NotUsed" in enum values, assuming it encompasses all such cases (namely vertex step mode and the 3 binding layouts).
I think that's fine actually, I could use
Added them! NB: The default value is given for all member, including when it is not explicitly specified in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I hope we don't run into problems with the INIT design.
I think that's fine actually, I could use
_wgpu_MAKE_INIT_STRUCT
inline
Ah, that's true!
State default values in generated docstring: Shall we?
I think this would be great.
Added them! NB: The default value is given for all member, including when it is not explicitly specified in
webgpu.yml
due to the zero-value being the expected default. I think it is good to make things explicit but we could also leave them unspecified and globally say that unspecified means 0.
Will review but that seems good to me.
Adding test revealed a lot of small things (which I should have tested indeed). I notably took the questionable liberty of adding (back?) an Undefined entry to the Another note: I realize the generation of default values in |
PR webgpu-native#425 added /wd4189, but it does not seem to be enough. I was getting the following error when adding initializer tests: main.inl(122): warning C4101: 'x': unreferenced local variable
Hm, I don't feel strongly either way. Alternative would be to zero-init those fields with
Honestly I have no idea. If it doesn't cause problems for generation right now, it's fine and we'll just deal with it later. |
The issue occurs in some other cases as well actually, because we generate INIT macros for structs that are only used to return info (e.g., about the adapter) for which it really doesn't make sense to add an However, as you can see above CI complains about using Since I had to add this, I'll roll back the (re)introduction of
It does not cause any issue for now |
Mmh now C++ complains about C-style cast... I'll try with a |
Now It is smart enough to realize that 0 is not a valid value, better to default to the first non-null entry of the enum in the end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow I submitted some of the comments before others. Here's the rest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through all the differences between this and Dawn and added comments for things that should change in this PR. Here are all the other differences that don't need to change in this PR:
- Places where Dawn is behind or the definition order is different
- Spelling of default values differs (but value is the same so it doesn't matter except for the C vs C++ differences you were already investigating):
0.0f
vs0.f
- In many (but not all) places it uses
{}
instead of a named value - In some places it uses a named value instead of
{}
- It doesn't use
_wgpu_MAKE_INIT_STRUCT
when initializing.chain
, it just uses a bare{NULL _wgpu_COMMA WGPUSType_RenderPassMaxDrawCount}
- (I was wrong about this before) the new
CallbackInfo.mode
fields init to{}
instead ofWaitAnyOnly
(some outdated duplicates areWaitAnyOnly
) - It incorrectly comments
/*.next=*/
as/*.nextInChain*/
- It incorrectly uses
false
instead of0
(since it doesn't includestdbool.h
) - It incorrectly uses
nullptr
instead ofNULL
(in some places where it doesn't use{}
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh and 0xFFFFFFFF
vs 4294967295
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for going through all this!
I think the key remaining difference now is that we cannot use {}
, as far as I can tell (as mentionned above, it is not standard C) so the generator now just goes and figure the very first enum entry and use it as default. This also means we need to decide on a default value for CallbackInfo.mode
: WaitAnyOnly
was fine with me but if we rather want to force the user to choose one, we need to reintroduce an Undefined
value to initialize to (otherwise C++ complains)
edit I realize { 0 }
is actually doing the same thing in C and C++, and is probably what we should use in lieu of {}
(rather than looking for the first enum entry).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pulled your latest code and I'm experimenting. I see that 0
or {0}
hits -Wassign-enum
in clang (from -Weverything
). Trying to determine if that is just a "maybe you made a typo" warning or a "you're doing UB" warning (I've researched this before but didn't remember). This answer https://stackoverflow.com/a/33814842 says basically that the underlying type will be a signed or unsigned integer type of arbitrary bit length large enough to hold all of the values, which means this is safe because 0 is always representable.
(WGPUCompilationMessageType)0
works in C but of course hits -Wold-style-cast
in C++. We could conditionally choose between that for C and WGPUCompilationMessageType(0)
for C++ (assuming MSVC is OK with these).
#if defined(__cplusplus)
# define _wgpu_ENUM_ZERO_INIT(type) type(0)
#else
# define _wgpu_ENUM_ZERO_INIT(type) (type)0
#endif
Or we could just give names to all the enums' zero values, which IMO seems more reasonable. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it out, that seems to work fine: https://github.com/kainino0x/webgpu-headers/actions/runs/11945415786
So perhaps we can land the PR with that and we'll follow up on it in #427.
This reverts commit d543b8e.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I addressed things using _wgpu_ENUM_ZERO_INIT
, finally reviewed the Go code, and did some minor nits to the documentation. Everything looks great now, if there was anything else you wanted to change feel free to open more PR(s) - I'm going to land this now!
Motivated by #158, and inspired by how Dawn does it, this PR introduces a
WGPU_FOO_INIT
macro to initialize a structWGPUFoo
with default values.WGPUChainedStruct
andWGPUChainedStructOut