Open
Conversation
bddfc29 to
306fdb0
Compare
Add i16/u16 scalar types to naga's IR, validation, WGSL frontend, and all backend code generators (SPIR-V, MSL, HLSL, GLSL, WGSL). Gated behind a new SHADER_INT16 capability and `enable wgpu_int16;` WGSL extension.
- Vulkan: require storageBuffer16BitAccess for SHADER_I16 (fixes gfx-rs#7468), request VK_KHR_16bit_storage when SHADER_I16 is enabled, add Int16 to available SPIR-V capabilities - Metal: unconditionally report SHADER_I16 (MSL natively supports short/ushort) - DX12: gate SHADER_I16 on SM 6.2 + Native16BitShaderOpsSupported, pass -enable-16bit-types to DXC when SHADER_I16 is enabled - wgpu-naga-bridge: map Features::SHADER_I16 to Capabilities::SHADER_INT16 - wgpu-types: update SHADER_I16 feature docs
- Snapshot test (int16.wgsl): comprehensive coverage of types, operators, builtins, conversions, bitcast, subgroup ops across all backends - Error tests: enable extension gating, capability gating, atomic rejection, subgroup bitwise ops rejection - Add -enable-16bit-types to DXC validation in xtask (SM 6.2+ only) - Changelog entries for the feature and Vulkan bug fix
| val_u16_4: vec4<u16>, | ||
|
|
||
| // i16 | ||
| val_i16: i16, |
Collaborator
There was a problem hiding this comment.
Does std140 even support i16? I was under the impression that you couldn't use these in uniforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Disclaimer
Entirely LLM generated. My process was to point the LLM at the SPIRV, MSL, and HLSL specs, as well as existing issues/PRs/code related to u64, f16, etc. I also had it write a crap ton of tests covering all the places I could think of that could be affected.
Connections
Closes #4424 (16-bit integer support in naga)
Fixes #7468 (Vulkan:
SHADER_I16does not enablestorage_buffer16_bit_access)Partially addresses #7109 (i16 division/modulo overflow is now guarded in HLSL/MSL/SPIR-V backends)
Description
Implements
i16/u1616-bit integer type support in WGSL shaders, gated behind the existingFeatures::SHADER_I16feature flag and a newenable wgpu_int16;WGSL enable extension.What's included
Naga (shader compiler):
Scalar::I16/U16,Literal::I16/U16), validation (SHADER_INT16capability), and WGSL parsing (i16,u16,vec2<i16>, etc.) withenable wgpu_int16;gatingOpTypeInt 16+SPV_KHR_16bit_storage), MSL (short/ushort), HLSL (int16_t/uint16_t), GLSL (int16_t/uint16_t), WGSL round-tripabs,min,max,clamp,sign, etc.) viaScalarSetadditionsi16::MIN / -1) in SPIR-V, MSL, and HLSL backendsTryFrom<Literal> for u32)put_bitcasted_expressionto addstatic_casttruncation for sub-32-bit types, preventing invalidas_typecasts due to C++ integer promotion (ushort + ushortpromotes toint, makingas_type<short>(int)a size mismatch)i16literals into the 32-bit constant word sospirv-ascan round-trip the disassemblymetal::selectcalls within div/mod/abs helpers (e.g.short(1)instead of bare1) to avoid overload ambiguity with sub-32-bit typesasuint()/asint()for 16-bit bitcasts (those intrinsics only support 32-bit types)enable wgpu_int16;when the module uses i16/u16 typesAtomic validation fix (pre-existing):
Ti::Atomicvalidation fromSint | Uint, width: _to explicitwidth: 4andwidth: 8arms, soatomic<u16>is correctly rejected instead of silently accepted. This was a pre-existing bug that also affectedatomic<u8>if anyone ever tried it via SPIR-V input.HAL backends:
SHADER_I16reporting now requiresstorageBuffer16BitAccess+uniformAndStorageBuffer16BitAccess(matchingSHADER_F16behavior).VK_KHR_16bit_storageextension now requested forSHADER_I16(not justSHADER_F16).Int16added to available SPIR-V capabilities.SHADER_I16unconditionally reported (MSL natively supportsshort/ushort).SHADER_I16gated on SM 6.2 +Native16BitShaderOpsSupported(same condition asSHADER_F16).-enable-16bit-typesnow passed to DXC at runtime whenSHADER_I16is enabled (not justSHADER_F16).wgpu-naga-bridge:
Features::SHADER_I16toCapabilities::SHADER_INT16.wgpu-types:
SHADER_I16feature docs to reflect naga support and platform availability.What's NOT included (intentional)
VertexFormat::Uint16still maps toScalar::U32in shader validation, consistent withFloat16mapping toScalar::F32. The hardware expands 16-bit vertex data to 32-bit before the shader sees it, per WebGPU spec.R16Uint/R16Sintstorage textures still produce 32-bit component types in shaders, consistent with the WebGPU spec.int(4-byte) padding and divides gap sizes by 4, which can't represent sub-4-byte gaps. This is a pre-existing limitation shared withf16— HLSL's implicit padding handles it correctly in practice, and f16 has been shipping with this behavior and passing CTS.42ssyntax). Values are constructed viau16(42)/i16(42).subgroupAnd/subgroupOr/subgroupXorare rejected for 16-bit types at the naga validation level because HLSL'sWaveActiveBitAnd/Or/Xordon't support them.var<immediate>with i16/u16 types is rejected, matching f16 behavior. Vulkan requiresStoragePushConstant16for 16-bit push constants, which wgpu does not currently enable.atomic<u16>/atomic<i16>are rejected — 16-bit atomics aren't supported by hardware.SHADER_I16not reported as a device feature. The naga GLSL backend can generate the code (int16_t/uint16_t), but the wgpu-hal GL/GLES adapter doesn't expose the feature since GLSL 16-bit integer support requires rare extensions.Testing
naga/tests/in/wgsl/int16.wgsl): Comprehensive coverage of types (scalars, vectors, structs, arrays), storage (uniform/storage/workgroup buffers), operators (arithmetic, bitwise, shifts, negation, comparisons), builtins (abs/min/max/clamp/sign),select, type conversions, bitcast, array indexing with u16, vector arithmetic, and subgroup operations. Output verified for SPIR-V, HLSL, MSL, and WGSL backends.spirv-valandspirv-asround-trip.naga/tests/naga/wgsl_errors.rs):int16_capability_and_enable: Tests thatu16/i16requireenable wgpu_int16;(parse error) andSHADER_INT16capability (validation error). Covers zero-value expressions, constructor calls, andvardeclarations.int16_in_atomic: Tests thatatomic<u16>andatomic<i16>are rejected withInvalidAtomicWidth.-enable-16bit-typesadded for SM 6.2+ profiles only.Squash or Rebase?
Squash.
Checklist
cargo fmt.taplo format.cargo clippy --tests. If applicable, add:--target wasm32-unknown-unknowncargo xtask testto run tests.CHANGELOG.mdentry.