Skip to content
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

Associate DXGI function parameters and struct fields with the corresponding enum type #1757

Merged
merged 5 commits into from
May 6, 2024

Conversation

MarijnS95
Copy link
Contributor

@MarijnS95 MarijnS95 commented Dec 8, 2023

These are more convenient and descriptive to use than plain UINTs.

@MarijnS95
Copy link
Contributor Author

MarijnS95 commented Dec 8, 2023

Before merging this, maybe we should do a bigger pass over this namespace and check some more UINT APIs, though I haven't come across many obvious ones just yet (in an existing codebase).

@MarijnS95 MarijnS95 changed the title Annotate two DXGI getters with the right enum type Annotate DXGI function parameters and struct fields with the right enum type Dec 8, 2023
@MarijnS95
Copy link
Contributor Author

MarijnS95 commented Dec 8, 2023

This is basically a followup of #1583 where I should have checked for more types (even ones on the same struct of a different version...). Found a few more, in the Dxgi namespace by searching for *flags: u32 and *mut u32 in windows-rs. Only the following "types" are loose constants instead of associated enums, and cannot be used to better type the function signatures.

https://learn.microsoft.com/en-us/windows/win32/direct3ddxgi/dxgi-present
https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgisurface-map
https://learn.microsoft.com/en-us/windows/win32/direct3ddxgi/dxgi-enum-modes


Curiously in the linked PR we had to annotate these via [AssociatedEnum("…")]. Same here for struct fields and UINTs passed by value, but not for * UINTs passed as pointer... Is that expected?

@riverar
Copy link
Collaborator

riverar commented Jan 6, 2024

I think these are the remaining items that need conversion?

IDXGIOutput3::CheckOverlaySupport::pFlags=DXGI_OVERLAY_SUPPORT_FLAG
IDXGIOutput4::CheckOverlayColorSpaceSupport::pFlags=DXGI_OVERLAY_COLOR_SPACE_SUPPORT_FLAG
IDXGIOutput6::CheckHardwareCompositionSupport::pFlags=DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAGS

@MarijnS95
Copy link
Contributor Author

@riverar don't forget about:

IDXGISwapChain3::CheckColorSpaceSupport::pColorSpaceSupport=DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG

Those four are the only pointer params in this PR.

@riverar
Copy link
Collaborator

riverar commented Jan 6, 2024

Right, just looking for you to fix up these last four?

-IDXGIOutput3::CheckOverlaySupport::pFlags=DXGI_OVERLAY_SUPPORT_FLAG
+IDXGIOutput3::CheckOverlaySupport::pFlags=[AssociatedEnum(DXGI_OVERLAY_SUPPORT_FLAG)]

-IDXGIOutput4::CheckOverlayColorSpaceSupport::pFlags=DXGI_OVERLAY_COLOR_SPACE_SUPPORT_FLAG
+IDXGIOutput4::CheckOverlayColorSpaceSupport::pFlags=[AssociatedEnum(DXGI_OVERLAY_COLOR_SPACE_SUPPORT_FLAG)

-IDXGIOutput6::CheckHardwareCompositionSupport::pFlags=DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAGS
+IDXGIOutput6::CheckHardwareCompositionSupport::pFlags=[AssociatedEnum(DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAGS)

-IDXGISwapChain3::CheckColorSpaceSupport::pColorSpaceSupport=DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG
+IDXGISwapChain3::CheckColorSpaceSupport::pColorSpaceSupport=[AssociatedEnum(DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG)

Edit: Just saw the last one hiding in there. Added above.

@MarijnS95
Copy link
Contributor Author

@riverar so that means there's a definitive yes on annotating pointer types?

@riverar
Copy link
Collaborator

riverar commented Jan 6, 2024

Yup, lets do it 🚀

@MarijnS95 MarijnS95 changed the title Annotate DXGI function parameters and struct fields with the right enum type Associate DXGI function parameters and struct fields with the corresponding enum type Jan 7, 2024
@MarijnS95
Copy link
Contributor Author

@riverar done! Would you please make sure that I've covered every remaining untyped UINT enum in the DXGI bindings?

Perhaps we should seek for the same patterns in D3D12?

@MarijnS95
Copy link
Contributor Author

#1757 (comment) if you run into any of these: maybe we can turn the constants into enums?

@MarijnS95
Copy link
Contributor Author

@riverar I'll rebase this when you or @mikebattista have reviewed it and are ready to merge it, to prevent constant force-pushes. Looking forward to the heads-up, thanks!

@MarijnS95
Copy link
Contributor Author

Looks like it will also be advantageous to mark some of these types, like DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG with [Flags] attribute :)

@MarijnS95
Copy link
Contributor Author

MarijnS95 commented Jan 17, 2024

Only the following "types" are loose constants instead of associated enums, and cannot be used to better type the function signatures.

https://learn.microsoft.com/en-us/windows/win32/direct3ddxgi/dxgi-present
https://learn.microsoft.com/en-us/windows/win32/api/dxgi/nf-dxgi-idxgisurface-map
https://learn.microsoft.com/en-us/windows/win32/direct3ddxgi/dxgi-enum-modes

Since I ran into enums.json recently, I figured I should just introduce them.

Again, please help me check that I've caught mostly everything!

Documentation annotations are missing though, should those be included as well?

@MarijnS95 MarijnS95 force-pushed the dxgi branch 2 times, most recently from ea4f255 to dc191a0 Compare January 17, 2024 15:54
generation/WinSDK/enums.json Show resolved Hide resolved
scripts/ChangesSinceLastRelease.txt Outdated Show resolved Hide resolved
generation/WinSDK/enums.json Outdated Show resolved Hide resolved
generation/WinSDK/enums.json Outdated Show resolved Hide resolved
generation/WinSDK/enums.json Show resolved Hide resolved
generation/WinSDK/enums.json Show resolved Hide resolved
generation/WinSDK/enums.json Outdated Show resolved Hide resolved
generation/WinSDK/enums.json Show resolved Hide resolved
generation/WinSDK/enums.json Show resolved Hide resolved
generation/WinSDK/enums.json Outdated Show resolved Hide resolved
generation/WinSDK/enums.json Outdated Show resolved Hide resolved
sources/MetadataUtils/ConstantsScraper.cs Show resolved Hide resolved
riverar
riverar previously approved these changes May 3, 2024
@MarijnS95
Copy link
Contributor Author

Thanks for the review @riverar! @mikebattista let me know when this is ready to be merged, so that I can solve the conflicts with ChangesSinceLastRelease.txt.

mikebattista
mikebattista previously approved these changes May 6, 2024
@mikebattista mikebattista dismissed stale reviews from riverar and themself via 372c4b8 May 6, 2024 15:54
@mikebattista mikebattista merged commit 2e09958 into microsoft:main May 6, 2024
1 check passed
@mikebattista
Copy link
Collaborator

Merged. Thanks!

@MarijnS95 MarijnS95 deleted the dxgi branch May 6, 2024 21:40
@MarijnS95 MarijnS95 changed the title Associate DXGI function parameters and struct fields with the corresponding enum type Associate DXGI function parameters and struct fields with the corresponding enum type May 16, 2024
--with-attribute
DXGI_MULTIPLANE_OVERLAY_YCbCr_FLAGS=Flags
DXGI_OFFER_RESOURCE_FLAGS=Flags
DXGI_OUTDUPL_FLAG=Flags
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This flag has one DXGI_OUTDUPL_COMPOSITED_UI_CAPTURE_ONLY = 1 constant, but I couldn't find in what API it is supposed to be used. Only the constructor for output duplication has as Flags field that "should always be zero":

https://learn.microsoft.com/en-us/windows/win32/api/dxgi1_5/nf-dxgi1_5-idxgioutput5-duplicateoutput1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a question for the DX repo. Suspect this is was meant to stay internal. But a real world example from OS components verifies your claim: pObject->DuplicateOutput1(device, DXGI_OUTDUPL_COMPOSITED_UI_CAPTURE_ONLY, ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, in that case the documentation should be updated too.

What's the desired way to assign the enum, emitter.settings.rsp or listing it in enums.json?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we prefer enums.json these days!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riverar that might mean we could move more of the --with-attribute here - with explicit reassignments in emitter.settings.rsp - and consolidate them into enums.json?

Looks like there's also --enumMakeFlags in emitter.settings.rsp doing a similar thing 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But a real world example from OS components verifies your claim: pObject->DuplicateOutput1(device, DXGI_OUTDUPL_COMPOSITED_UI_CAPTURE_ONLY, ...)

@riverar where'd you find this example?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bcastdvruserservice.dll!Windows::Media::Capture::Internal::DDACapture::AcquireOutputDuplication (26217.5000), reverse engineered 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posted the question at microsoft/DirectX-Headers#128, let's see.

MarijnS95 added a commit to MarijnS95/win32metadata that referenced this pull request May 17, 2024
Even though my previous contribution in microsoft#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.
MarijnS95 added a commit to MarijnS95/win32metadata that referenced this pull request Jul 3, 2024
Even though my previous contribution in microsoft#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.
MarijnS95 added a commit to MarijnS95/win32metadata that referenced this pull request Aug 29, 2024
Even though my previous contribution in microsoft#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.
MarijnS95 added a commit to MarijnS95/win32metadata that referenced this pull request Aug 29, 2024
Even though my previous contribution in microsoft#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants