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

feat: Add 32-bit floating-point atomics (SHADER_FLOAT32_ATOMIC) #6234

Open
wants to merge 17 commits into
base: trunk
Choose a base branch
from

Conversation

AsherJingkongChen
Copy link
Contributor

@AsherJingkongChen AsherJingkongChen commented Sep 7, 2024

Connections

Resolves #1020

Description

The wgpu feature SHADER_FLOAT32_ATOMIC allows 32-bit floating point atomic operations on native backends:

The implemented or supported (natively) atomic operations by Metal and Vulkan are listed in the table (✅ is true, ❌ is false):

Function Implemented? Supported? Implemented? Supported?
f32 Metal Metal Vulkan Vulkan
atomicLoad
atomicStore
atomicAdd
atomicSub
atomicMax
atomicMin
atomicAnd
atomicOr
atomicXor
atomicExchange
atomicCompareExchangeWeak

There are some floating-point atomic operations not implemented on some backends in this PR:

  • Operations on pointers not in the storage address space: Metal 3 supports the atomic_float for device memory only. (Page 247)
  • atomicAnd: This bitwise operation is designed for integer types, not floating-point ones.
  • atomicOr: This bitwise operation is designed for integer types, not floating-point ones.
  • atomicXor: This bitwise operation is designed for integer types, not floating-point ones.
  • atomicMax: Metal does not support atomic_fetch_max_explicit (Page 250) on floating-point types.
  • atomicMin: Metal does not support atomic_fetch_min_explicit (Page 250) on floating-point types.
  • atomicCompareExchangeWeak: Vulkan only support OpAtomicCompareExchange on integer types.

There are some floating-point atomic operations implemented on all backends in this PR:

  • Operations in the storage address space:
    • atomicLoad
    • atomicStore
    • atomicAdd
    • atomicSub
    • atomicExchange

There are some hacks in this PR implementation:

  • atomicSub: Vulkan does not have subtraction for floating-point types (no OpAtomicFSub). We can simulate it by adding the negated value. They are mathematically equivalent since A - v == A + (-v).

Testing

  • A naga snapshot atomicOps-float32 is added.
  • A shader test numeric_builtins::float32_atomic is added.
  • Relevant tests are passed on Apple M2 Pro (Metal 3.1+, Vulkan 1.2):
    • cargo xtask test snapshots float32_atomic
      
  • Only 2 irrelevant tests failed on Apple M2 Pro (Vulkan 1.2):
    • cargo xtask test vertex_formats
      

Checklist

  • Run cargo fmt.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
      • The crate winit does not compile on my device.
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.
  • Run cargo xtask validate to validate shaders.

* Current supported platforms: Metal
* Platforms to support in the future: Vulkan

Related issues or PRs:

* gfx-rs#1020
@AsherJingkongChen

This comment was marked as outdated.

@AsherJingkongChen AsherJingkongChen changed the title feat: Add 32-bit floating-point atomics feat: Add 32-bit floating-point atomics (SHADER_FLT32_ATOMIC) Oct 9, 2024
@AsherJingkongChen
Copy link
Contributor Author

AsherJingkongChen commented Oct 10, 2024

Optional: Add fallback implementation for floating-point atomics

Description

Currently, the addition operation works on some recent chips like M1 Pro, NVIDIA RTX 4060, etc.
Without native supports, we can use more available operation, integer atomics, to simulate floating-point atomics.

By using atomic CAS loop, we can ensure the operation is atomic, but it is slower.

@AsherJingkongChen AsherJingkongChen marked this pull request as draft October 10, 2024 12:23
@AsherJingkongChen AsherJingkongChen changed the title feat: Add 32-bit floating-point atomics (SHADER_FLT32_ATOMIC) feat: Add 32-bit floating-point atomics Oct 10, 2024
* Make branches tidy
* Also revise old codes
* Ensure the implementations are supported by Metal and Vulkan backends
@AsherJingkongChen AsherJingkongChen deleted the pr/shader-flt32-atomic branch October 13, 2024 02:17
@AsherJingkongChen AsherJingkongChen restored the pr/shader-flt32-atomic branch October 13, 2024 02:18
@AsherJingkongChen AsherJingkongChen changed the title feat: Add 32-bit floating-point atomics feat: Add 32-bit floating-point atomics (SHADER_FLOAT32_ATOMIC) Oct 13, 2024
@AsherJingkongChen AsherJingkongChen marked this pull request as ready for review October 13, 2024 03:06
@AsherJingkongChen

This comment was marked as outdated.

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.

Support floating-point atomics
1 participant