Skip to content

Commit

Permalink
Add SHADER_FLOAT16 feature (#2646)
Browse files Browse the repository at this point in the history
* Add SHADER_FLOAT16 feature

* vk: check SHADER_FLOAT16 feature
  • Loading branch information
jinleili authored May 13, 2022
1 parent 144e943 commit ddf1903
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions deno_webgpu/02_idl_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"texture-compression-astc",
"timestamp-query",
"indirect-first-instance",
"shader-f16",
// extended from spec
"mappable-primary-buffers",
"texture-binding-array",
Expand Down
7 changes: 7 additions & 0 deletions deno_webgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ fn deserialize_features(features: &wgpu_types::Features) -> Vec<&'static str> {
if features.contains(wgpu_types::Features::INDIRECT_FIRST_INSTANCE) {
return_features.push("indirect-first-instance");
}
if features.contains(wgpu_types::Features::SHADER_FLOAT16) {
return_features.push("shader-f16")
}

// extended from spec
if features.contains(wgpu_types::Features::MAPPABLE_PRIMARY_BUFFERS) {
Expand Down Expand Up @@ -306,6 +309,10 @@ impl From<GpuRequiredFeatures> for wgpu_types::Features {
wgpu_types::Features::INDIRECT_FIRST_INSTANCE,
required_features.0.contains("indirect-first-instance"),
);
features.set(
wgpu_types::Features::SHADER_FLOAT16,
required_features.0.contains("shader-f16"),
);

// extended from spec
features.set(
Expand Down
1 change: 1 addition & 0 deletions deno_webgpu/webgpu.idl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ enum GPUFeatureName {
"texture-compression-astc",
"timestamp-query",
"indirect-first-instance",
"shader-f16",
};

[Exposed=(Window, DedicatedWorker), SecureContext]
Expand Down
3 changes: 2 additions & 1 deletion wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ impl super::PrivateCapabilities {
| F::PUSH_CONSTANTS
| F::POLYGON_MODE_LINE
| F::CLEAR_TEXTURE
| F::TEXTURE_FORMAT_16BIT_NORM;
| F::TEXTURE_FORMAT_16BIT_NORM
| F::SHADER_FLOAT16;

features.set(F::TEXTURE_COMPRESSION_ASTC_LDR, self.format_astc);
features.set(F::TEXTURE_COMPRESSION_ASTC_HDR, self.format_astc_hdr);
Expand Down
47 changes: 46 additions & 1 deletion wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub struct PhysicalDeviceFeatures {
depth_clip_enable: Option<vk::PhysicalDeviceDepthClipEnableFeaturesEXT>,
multiview: Option<vk::PhysicalDeviceMultiviewFeaturesKHR>,
astc_hdr: Option<vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT>,
shader_float16: Option<(
vk::PhysicalDeviceShaderFloat16Int8Features,
vk::PhysicalDevice16BitStorageFeatures,
)>,
}

// This is safe because the structs have `p_next: *mut c_void`, which we null out/never read.
Expand Down Expand Up @@ -62,6 +66,10 @@ impl PhysicalDeviceFeatures {
if let Some(ref mut feature) = self.astc_hdr {
info = info.push_next(feature);
}
if let Some((ref mut f16_i8_feature, ref mut _16bit_feature)) = self.shader_float16 {
info = info.push_next(f16_i8_feature);
info = info.push_next(_16bit_feature);
}
info
}

Expand Down Expand Up @@ -326,6 +334,19 @@ impl PhysicalDeviceFeatures {
} else {
None
},
shader_float16: if requested_features.contains(wgt::Features::SHADER_FLOAT16) {
Some((
vk::PhysicalDeviceShaderFloat16Int8Features::builder()
.shader_float16(true)
.build(),
vk::PhysicalDevice16BitStorageFeatures::builder()
.storage_buffer16_bit_access(true)
.uniform_and_storage_buffer16_bit_access(true)
.build(),
))
} else {
None
},
}
}

Expand Down Expand Up @@ -531,6 +552,15 @@ impl PhysicalDeviceFeatures {
);
}

if let Some((ref f16_i8, ref bit16)) = self.shader_float16 {
features.set(
F::SHADER_FLOAT16,
f16_i8.shader_float16 != 0
&& bit16.storage_buffer16_bit_access != 0
&& bit16.uniform_and_storage_buffer16_bit_access != 0,
);
}

(features, dl_flags)
}

Expand Down Expand Up @@ -642,7 +672,12 @@ impl PhysicalDeviceCapabilities {
extensions.push(vk::KhrPortabilitySubsetFn::name());

if requested_features.contains(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR) {
extensions.push(vk::ExtTextureCompressionAstcHdrFn::name())
extensions.push(vk::ExtTextureCompressionAstcHdrFn::name());
}

if requested_features.contains(wgt::Features::SHADER_FLOAT16) {
extensions.push(vk::KhrShaderFloat16Int8Fn::name());
extensions.push(vk::Khr16bitStorageFn::name());
}

extensions
Expand Down Expand Up @@ -883,6 +918,16 @@ impl super::InstanceShared {
.insert(vk::PhysicalDeviceTextureCompressionASTCHDRFeaturesEXT::default());
builder = builder.push_next(next);
}
if capabilities.supports_extension(vk::KhrShaderFloat16Int8Fn::name())
&& capabilities.supports_extension(vk::Khr16bitStorageFn::name())
{
let next = features.shader_float16.insert((
vk::PhysicalDeviceShaderFloat16Int8FeaturesKHR::default(),
vk::PhysicalDevice16BitStorageFeaturesKHR::default(),
));
builder = builder.push_next(&mut next.0);
builder = builder.push_next(&mut next.1);
}

let mut features2 = builder.build();
unsafe {
Expand Down
10 changes: 10 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ bitflags::bitflags! {
///
/// This is a web and native feature.
const PIPELINE_STATISTICS_QUERY = 1 << 4;
/// Allows shaders to acquire the FP16 ability
///
/// Note: this is not supported in naga yet,only through spir-v passthrough right now.
///
/// Supported Platforms:
/// - Vulkan
/// - Metal
///
/// This is a web and native feature.
const SHADER_FLOAT16 = 1 << 5;
/// Webgpu only allows the MAP_READ and MAP_WRITE buffer usage to be matched with
/// COPY_DST and COPY_SRC respectively. This removes this requirement.
///
Expand Down

0 comments on commit ddf1903

Please sign in to comment.