Skip to content

u32::max / u32::min require OpCapability U8 #147

Open

Description

@Firestar99

u32::max / u32::min cannot be used as they require OpCapability U8. They should just work out of the box.

Workaround: use u32::clamp() instead.

It's the Ordering enum. it has #[repr(i8)] (with -1, 0, +1 values)

Maybe we should special case the Ordering enum to be a u32?

Activity

  1. added
    bug
    on Nov 18, 2024
  2. LegNeato commented on Nov 19, 2024

    @LegNeato
    Collaborator

    Wouldn't that mean it takes more space on devices that support OpCapability U8?

  3. Firestar99 commented on Nov 19, 2024

    @Firestar99
    MemberAuthor

    Used within a shader not much would change, the registers are (usually) 32bit anyway and you'd want to do the actual comparison directly afterwards anyway to reduce it to bools, which on many platforms have more compact storage. (eg. on AMD they represent bools by bits in a scalar register instead of using vector registers.)
    The bigger issue is when the Ordering enum is written to or read from a buffer, as their representation between CPU and GPU would now differ and any struct containing Ordering as a member would me misaligned.
    This would open up the discussion if we should automatically widen u8 and u16 to u32 for arbirary types that are used only within the shader, and whether to bitpack them automagically when loading / storing them to a buffer.
    But honestly pretty much all desktop devices support U8 and U16 loads and stores anyway...

  4. Firestar99 commented on Nov 30, 2024

    @Firestar99
    MemberAuthor

    What about a way simpler solution: Overwrite the method {u8, u16, u32, ..., f32}::max and min to just do what f32::clamp does, which does not use the Ordinal enum at all:

    pub fn clamp(mut self, min: f32, max: f32) -> f32 {
        assert!(min <= max, "min > max, or either was NaN. min = {min:?}, max = {max:?}");
        if self < min {
            self = min;
        }
        if self > max {
            self = max;
        }
        self
    }
  5. LegNeato commented on Nov 30, 2024

    @LegNeato
    Collaborator

    What do we lose with that? I assume nothing?

  6. Firestar99 commented on Nov 30, 2024

    @Firestar99
    MemberAuthor

    Exactly, it would just make those common methods work without breaking anything else. Now everything using Ordinal directly or implementing Ord / PartialOrd would still fail to compile without OpCapability U8. But I think that's a fine tradeoff, as comparing custom types is rarely done on GPUs anyway, whereas min and maxing int / floats is used everywhere.

  7. LegNeato commented on Nov 30, 2024

    @LegNeato
    Collaborator

    I assume the impl with Ordinal is there for a reason, so perhaps we could even switch back to that when OpCapability U8 is used.

  8. mikwielgus commented on Oct 9, 2026

    @mikwielgus
    Contributor

    Hello. In this issue, OpCapability U8 is mentioned. But I was unable to find anything about this on the Web. So I suppose you actually meant OpCapability Int8.

    This issue was opened in November 2024. But in December 2024, Vulkan 1.4 was released. This new version made Vulkan's shaderInt8 feature, which maps to OpCapability Int8 in SPIR-V, a mandatory requirement.

    So, I would like to suggest just adding an optional Vulkan 1.4 version gate that will just enable OpCapability Int8 for all generated SPIR-V bytecode. This will be a much easier solution than method override and the rabbit hole of integer promotion, and will make idiomatic Rust code (PartialOrd, u32::max, u32::min) work on an only increasing number of devices onwards.

    If you think this is a good idea, I can look into this. :)

  9. Firestar99 commented on Oct 12, 2026

    @Firestar99
    MemberAuthor

    For completeness sake: Vulkan 1.4 requires both Int8 and Int16, source.

    However, even though vulkan 1.4 drivers are required to offer those features, when you create your instance you must still enable them to tell the driver you want to use them. So us implicitly enabling them for the vk1.4 target requires end users to always enable the feature, even if they're not using them. And if you're on wgpu, you'll be big sad cause you don't get access to the VkInstanceCreateInfo to enable said features.

  10. mikwielgus commented on Oct 12, 2026

    @mikwielgus
    Contributor

    So us implicitly enabling them for the vk1.4 target requires end users to always enable the feature, even if they're not using them.

    I wasn't aware of this, that's a bummer.

    But then perhaps we could at least have something akin to the Rust's default features mechanism?

  11. Firestar99 commented on Oct 12, 2026

    @Firestar99
    MemberAuthor

    That's a decent amount of effort and pretty much everyone will disable it cause it makes getting rust-gpu up and running more complicated... don't see the value in adding that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions