Skip to content

Vulkan struct chaining #706

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

Merged
merged 74 commits into from
Dec 3, 2021

Conversation

thargy
Copy link
Contributor

@thargy thargy commented Nov 30, 2021

Summary of the PR

Full implementation of Vulkan Struct chaining Proposals 1, 2 and 3.

(Note, this replaces the following PRs: #676, #680, #683, #693)

Related issues, Discord discussions, or proposals

Further Comments

Proposals 1-3 were approved in a Working Group meeting held at 19:00 UTC on 29 November 2021. Proposal 4, is on hold awaiting a more compelling use case.

TODOs (Completed)

  • Consider IList
    • Rejected due to Insert and RemoveAt being unnecessarily complex additions, may add in future, but unlikely due to explosion of method overloads that would be required, or reflection to create types dynamically.
  • Check IExtendsChain<out TChain> constraint on TChain, currently IChainable, should it be IChainStart?
    • Fixed, the looser IChainable in only required on *Any methods, this interface should have the tighter constraint.
    • The looser constraint allows for checking any IChainable type extends any other, this is useful when you are dealing with 'loosely constrained' types - as happens with the managed Chain.
  • Fix Proposal 3 reference to ManagedCache
  • Rename ManagedChain to Chain
  • Rename ManagedChain.Append to Add
    • Already implemented as Add, just updated the proposal.
  • Replace use of Marshal (e.g. Marshal.StructureToPtr)
    • Also improved struct constraints to unmanaged to make it safe to blit the structures straight into memory - which should improve speed even further.
    • Removed unnecessary MemoryCopy operations.
  • Add chain modifiers to base class of ManagedChain (Add, Duplicate, Truncate)
    • Split out non-auto-generated code in Chain to Chain.cs for easier maintenance.
    • Added AddAny, Duplicate and TruncateAny instance methods to Chain to allow for easier manipulation of runtime chains.
    • Used *Any overloads after agreement in Discord. This is to indicate their alignment with the 'less-constrained' *Any extension methods they use.
    • Added new MaximumCount property to Chain to indicate the currently supported maximum (16).
    • Currently, instance methods call the static extension methods to perform the building. Previously the logic was moved into the statics to reduce the code, however, we may consider moving it back to instance methods (and have the extension methods call the instance methods again). For now, it was much quicker to implement this way.
    • Moved Add, Dupliacate and Truncate methods back into instance.
  • Add formal test project, Silk.Net.Vulkan.Tests and include chain tests.
  • Add Chain.Clear(bool includeHead) method to conveniently clear out a chain.
  • Add implicit conversion of Chain classes to nint, void*, BaseInStructure* and TChain*.
  • Added IEquatable<Chain> to Chain.
  • Add documentation (Probagbly to the website)
    • Added to documentation/structure-chaining based on Discord chat.
  • Remove completed proposals
  • Add Chain.LoadAny(TChain) method that is implemented directly and does not create a StringBuilder.

Lead Reviewer: @Perksey (BuildTools), @HurricanKai (Vulkan)

thargy added 30 commits November 4, 2021 18:08
* Created class library with 3 chainable structs
* Added `IChainable` interface hierarchy
* Added chaining methods `Chain`, `CreateNext`, `SetNext` and `End`
* Added tests
* Added tests to check that invalid structs don't compile
* Minimal additional code required per struct
  - just add an interface for each allowed `PNext` struct; and,
  - add a short static method `Chain` (technically optional), and a short instance method `SetNext`.
Retargeted project to ensure that it only targets the same includes,
etc. as `Silk.Next.Core` and `Silk.Net.Vulkan`
* Added `IStructureType` for any struct that has a
  `StructureType SType` field.  This is usually added automatically
  by one of the other interfaces; and allows the structure type to
  be automatically corrected and retrieved using the
  `StructureType IStructuredType.StructureType()` method.
* Added `IChainStart` to indicate a type that can form the start of
  a chain.
* `IChainable<...>` was replaced with `IExtendsChain<T>` which matches
  the Vulkan specification more readily.
* The chaining methods now all accept, and return, a reference to an
  `IChainStart`, making it easier to scan a chain for existing items,
  and making use of the auto-generated `IExtendsChain<T>` to validate
  types at compile time.
* `SetNext`, by default, will now replace existing chain entries if
  they match the supplied `SType`.  This behaviour can be overriden with
  the optional `alwaysAdd` parameter.
* `CreateNext` is now called `AddNext` to indicate it always adds to the
  end of a Chain.cs
* `TryAddNext` added to only create a new structure if it is not already
  there.
* `IndexOf` added to return the index of a structure in a chain.
* Updated Readme.md.
Updated `Changes Required` section.
* ManagedChain holds structures in unmanaged memory, preventing
  their movement, and it can be safely held on the heap itself.
* It allows for easy modification and manipulation of the chain.
`ManagedChain` now supports 2-16 chain items.
Can now easily append an item to an existing `ManagedChain` to
efficiently create a new `ManagedChain`.
`ManagedChain` now has a constructor that will load an unmanaged chain
allowing it to be safely stored on the heap.
Also improved loading to detect when the unmanaged chain is too long.
Also added the new blank proposals.
Proposal - Vulkan Struct Chaining - #1 StructureType correction.md
* Restored targets for PrototypeStructChaining.csproj to match
  `Silk.Net.Vulkan` targets.
* Upped the generated `ManageChain<T...` to 16.
* IChainable now exposes `Chain* PNext` property.
* Added `ManagedChain<TChain>` as smallest chain (just the head)
* All `IntPtr`s are replaced with `nint`s.
* Pointer offsets are calculated once per chain type and stored in
  statics, meaning that a chain contains a single `nint` field
  internally, making it incredibly lightweight.  Getting the sizes once
  may also improve the performance, and has simplified the code.
* Various fixes to ensure tail always has `PNext` set to `null` (0).
* Added `Truncate` instance methods.
* Added the `Extensions`, `Extenders`, `ClrTypes` and `StructureTypes`
  dictionaries to `Chain`.
* Added the `ClrType`, `StructureType`, `IsChainStart`, `IsChainable`,
  `CanExtend` and `CanBeExtendedBy` extension methods to
  `ChainExtensions`.
* Finished Proposal dotnet#4 on Chaining Metadata extension.
* Sets all chain elements (optionally excluding the head) to their
  default value.
* Implicit conversion added to convert to `nint`, `void*` and
  `BaseInStructure*` to convert to a pointer to the head of the chain,
  for all `Chain` classes, including the abstract base `Chain`.
* Implicit conversion added to convert to `TChain*` for the concrete
  `Chain<TChain...>` classes.
* Added missing license header to interfaces
Fixed namespace for `Silk.NET.Vulkan.Test` tests.
Copy link
Member

@Perksey Perksey left a comment

Choose a reason for hiding this comment

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

Generated code looks fine, just a few nitpicks here and there (mostly on style, but some carried over from the original repo)

* Also added operator overloads.
@Perksey
Copy link
Member

Perksey commented Dec 1, 2021

Also can you please do a full BuildTools run before continuing (this may pull in unrelated changes, but that's fine as I'll know not to look in the folders that aren't Vulkan or OpenXR) primarily so I can grasp the implications of this on OpenXR.

Finally feel free to add a blog post in website/blog:

Thanks.

* Removed implemented proposals
* Renamed Metadata proposal (as this will remain on hold and isn't in
  the scope of this PR)
* Added Chaining UML class diagram
* Updated TOCs
* Documented Managed chaining
* Added correct summary comments to `Chain` equality operator overloads.
@thargy
Copy link
Contributor Author

thargy commented Dec 1, 2021

Also can you please do a full BuildTools run before continuing (this may pull in unrelated changes, but that's fine as I'll know not to look in the folders that aren't Vulkan or OpenXR) primarily so I can grasp the implications of this on OpenXR.

As I mentioned in the Working Group, I'm not confident doing this and I still have quite a few projects not building on my dev machine due to dependencies. I also have to have quite a lot of projects unloaded for the solution to be workable. @HurricanKai suggested he'd be happy to do a BuildTools run for me, and I'm happy to look at and address any issues that arise.

Finally feel free to add a blog post in website/blog:

I plan on finishing the documentation and PR today/tomorrow to give it a chance of making the next release. Once it gets merged I'll happily write a blog post. Hopefully, you will find the documentation pretty extensive.

@Perksey
Copy link
Member

Perksey commented Dec 1, 2021

Ah no worries, I'll run BuildTools full now.

thargy and others added 11 commits December 1, 2021 20:30
Co-authored-by: Dylan Perks <11160611+Perksey@users.noreply.github.com>
Co-authored-by: Dylan Perks <11160611+Perksey@users.noreply.github.com>
Co-authored-by: Dylan Perks <11160611+Perksey@users.noreply.github.com>
…pproved' into feature/vulkan-struct-chaining/approved
Added finalizer and `GC.SuppressFinalize(this)` when disposing
explicitly.
`BuildTools` deletes all `.gen.cs` files which can cause collisions.
* Removed the Unmanaged Chaining proposal as implemented.
* No longer allocates a `StringBuilder` when no `out string errors` is
  required.
* Added CS0661 suppression to `Chain` base class, as `GetHashCode()` is
  correctly overriden in concrete descendents.
@thargy thargy requested a review from Perksey December 2, 2021 00:21
Copy link
Member

@HurricanKai HurricanKai left a comment

Choose a reason for hiding this comment

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

It's very difficult to review this, due to GitHub almost crashing, but I've looked at the Chaining implementation and it lgtm.
@Perksey I think the BuildTools stuff looks good as well, but that's your expertise

@Perksey Perksey merged commit 0a5aede into dotnet:main Dec 3, 2021
@thargy thargy deleted the feature/vulkan-struct-chaining/approved branch December 3, 2021 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants