microsoft / win32metadata

Tooling to generate metadata for Win32 APIs in the Windows SDK.
Other
1.32k stars 113 forks source link

dxgi: Override enum (flags) ABI type to UINT when used in structures #1911

Open MarijnS95 opened 3 months ago

MarijnS95 commented 3 months ago

CC @riverar

Even though my previous contribution in #1757 added AssociatedEnum annotations to both function parameters and structure fields for some enum types, language bindings like windows-rs can only generate ABI conversions for parameters (and return types) inside functions when there's a mismatch (untyped enums are assumed to be INT, whereas structures and functions usually use UINT instead of the enum name). There is no place for this in repr(C) structs (in Rust), requiring us to get more creative to use the original UINT ABI while still replacing the field type with that of the relevant self-descriptive enum type.

Instead, move these (also rather disconnected) annotations and references on those enums and their respective fields to enums.json, where we can set the AssociatedEnum attribute at once in a more coherent way while above all allowing us to override the enum/flags ABI type to match how it's used by the APIs in function parameters and struct fields.

Also, DXGI_OUTDUPL_FLAG is now used as the type for IDXGIOutput5::DupliucateOutput1::Flags. The documentation says that its value should always be 0, but there's an example out there [TODO: link needed] that shows the use of DXGI_OUTDUPL_COMPOSITED_UI_CAPTURE_ONLY as arugment. This should be reported to be fixed in the documentation.

TODO

This is my attempt at solving https://github.com/microsoft/win32metadata/pull/1757#discussion_r1605484460 and https://github.com/microsoft/windows-rs/pull/3044#issuecomment-2118447109, but as is obvious from the changes since the last release it is not working yet. Unlike other enums that I added to enums.json with for example autoPopulate, none of these configs appear to be working. Any obvious thing that I'm doing wrong?

Then:

MarijnS95 commented 3 months ago

@riverar since you pointed this out to me in https://github.com/microsoft/windows-rs/pull/3044#issuecomment-2118385620, can you or @mikebattista help me with this?

As can be seen in the changelog in this PR, nothing is changing, presumably because the scraper already "has" an enum (whereas enums.json seems to mainly be used to scrape existing constants into enums).

Fortunately there seems to be a addUsesTo to "extend" an existing enum, but it doesn't seem to allow us to change the type:

EMITWINMD : error : Failed to emit DXGI_SWAP_CHAIN_DESC in win32metadata\generation\WinSDK\obj\generated\obj\crossarch\common\Dxgi.modified.cs: DXGI_SWAP_CHAIN_DESC.Flags was remapped to enum Windows.Win32.Graphics.Dxgi.DXGI_SWAP_CHAIN_FLAG (type int, size 4) but the original field was of type UINT (size 4). Either don't use an enum or make sure the enum is of the same size and sign. [generation\WinSDK\Windows.Win32.proj]