-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[naga spv-out] Add f16 io polyfill #7884
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
Open
cryvosh
wants to merge
3
commits into
gfx-rs:trunk
Choose a base branch
from
cryvosh:fix_shader16_vulkan
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/*! | ||
This module provides functionality polyfills `f16` input/output variables when the | ||
`StorageInputOutput16` capability is not available or disabled. | ||
|
||
It works by: | ||
|
||
1. Declaring `f16` I/O variables as `f32` in SPIR-V | ||
2. Converting between `f16` and `f32` at runtime using `OpFConvert` | ||
3. Maintaining mappings to track which variables need conversion | ||
*/ | ||
|
||
use crate::back::spv::{Instruction, LocalType, NumericType, Word}; | ||
use alloc::vec::Vec; | ||
|
||
/// Manages `f16` I/O polyfill state and operations. | ||
#[derive(Default)] | ||
pub(in crate::back::spv) struct F16IoPolyfill { | ||
use_native: bool, | ||
variable_map: crate::FastHashMap<Word, (Word, Word)>, | ||
} | ||
|
||
impl F16IoPolyfill { | ||
pub fn new(use_storage_input_output_16: bool) -> Self { | ||
Self { | ||
use_native: use_storage_input_output_16, | ||
variable_map: crate::FastHashMap::default(), | ||
} | ||
} | ||
|
||
pub fn needs_polyfill(&self, ty_inner: &crate::TypeInner) -> bool { | ||
use crate::{ScalarKind as Sk, TypeInner}; | ||
|
||
!self.use_native | ||
&& match *ty_inner { | ||
TypeInner::Scalar(ref s) if s.kind == Sk::Float && s.width == 2 => true, | ||
TypeInner::Vector { scalar, .. } | ||
if scalar.kind == Sk::Float && scalar.width == 2 => | ||
{ | ||
true | ||
} | ||
_ => false, | ||
} | ||
} | ||
|
||
pub fn register_variable(&mut self, variable_id: Word, f32_type_id: Word, f16_type_id: Word) { | ||
self.variable_map | ||
.insert(variable_id, (f32_type_id, f16_type_id)); | ||
} | ||
|
||
pub fn get_polyfill_info(&self, variable_id: Word) -> Option<(Word, Word)> { | ||
self.variable_map.get(&variable_id).copied() | ||
} | ||
|
||
pub fn emit_f16_to_f32_conversion( | ||
f16_value_id: Word, | ||
f32_type_id: Word, | ||
converted_id: Word, | ||
body: &mut Vec<Instruction>, | ||
) { | ||
body.push(Instruction::unary( | ||
spirv::Op::FConvert, | ||
f32_type_id, | ||
converted_id, | ||
f16_value_id, | ||
)); | ||
} | ||
|
||
pub fn emit_f32_to_f16_conversion( | ||
f32_value_id: Word, | ||
f16_type_id: Word, | ||
converted_id: Word, | ||
body: &mut Vec<Instruction>, | ||
) { | ||
body.push(Instruction::unary( | ||
spirv::Op::FConvert, | ||
f16_type_id, | ||
converted_id, | ||
f32_value_id, | ||
)); | ||
} | ||
|
||
pub fn create_polyfill_type(ty_inner: &crate::TypeInner) -> Option<LocalType> { | ||
use crate::{ScalarKind as Sk, TypeInner}; | ||
|
||
match *ty_inner { | ||
TypeInner::Scalar(ref s) if s.kind == Sk::Float && s.width == 2 => { | ||
Some(LocalType::Numeric(NumericType::Scalar(crate::Scalar::F32))) | ||
} | ||
TypeInner::Vector { size, scalar } if scalar.kind == Sk::Float && scalar.width == 2 => { | ||
Some(LocalType::Numeric(NumericType::Vector { | ||
size, | ||
scalar: crate::Scalar::F32, | ||
})) | ||
} | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
impl crate::back::spv::recyclable::Recyclable for F16IoPolyfill { | ||
fn recycle(mut self) -> Self { | ||
self.variable_map = self.variable_map.recycle(); | ||
self | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ impl Writer { | |
bounds_check_policies: options.bounds_check_policies, | ||
zero_initialize_workgroup_memory: options.zero_initialize_workgroup_memory, | ||
force_loop_bounding: options.force_loop_bounding, | ||
use_storage_input_output_16: options.use_storage_input_output_16, | ||
void_type, | ||
lookup_type: crate::FastHashMap::default(), | ||
lookup_function: crate::FastHashMap::default(), | ||
|
@@ -92,6 +93,9 @@ impl Writer { | |
temp_list: Vec::new(), | ||
ray_get_committed_intersection_function: None, | ||
ray_get_candidate_intersection_function: None, | ||
io_f16_polyfills: super::f16_polyfill::F16IoPolyfill::new( | ||
options.use_storage_input_output_16, | ||
), | ||
}) | ||
} | ||
|
||
|
@@ -125,6 +129,7 @@ impl Writer { | |
bounds_check_policies: self.bounds_check_policies, | ||
zero_initialize_workgroup_memory: self.zero_initialize_workgroup_memory, | ||
force_loop_bounding: self.force_loop_bounding, | ||
use_storage_input_output_16: self.use_storage_input_output_16, | ||
capabilities_available: take(&mut self.capabilities_available), | ||
binding_map: take(&mut self.binding_map), | ||
|
||
|
@@ -151,6 +156,7 @@ impl Writer { | |
temp_list: take(&mut self.temp_list).recycle(), | ||
ray_get_candidate_intersection_function: None, | ||
ray_get_committed_intersection_function: None, | ||
io_f16_polyfills: take(&mut self.io_f16_polyfills).recycle(), | ||
}; | ||
|
||
*self = fresh; | ||
|
@@ -726,10 +732,28 @@ impl Writer { | |
binding, | ||
)?; | ||
iface.varying_ids.push(varying_id); | ||
let id = self.id_gen.next(); | ||
prelude | ||
.body | ||
.push(Instruction::load(argument_type_id, id, varying_id, None)); | ||
let mut id = self.id_gen.next(); | ||
|
||
if let Some((f32_ty, _)) = self.io_f16_polyfills.get_polyfill_info(varying_id) { | ||
prelude | ||
.body | ||
.push(Instruction::load(f32_ty, id, varying_id, None)); | ||
let converted = self.id_gen.next(); | ||
super::f16_polyfill::F16IoPolyfill::emit_f32_to_f16_conversion( | ||
id, | ||
argument_type_id, | ||
converted, | ||
&mut prelude.body, | ||
); | ||
id = converted; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: You have an |
||
} else { | ||
prelude.body.push(Instruction::load( | ||
argument_type_id, | ||
id, | ||
varying_id, | ||
None, | ||
)); | ||
} | ||
|
||
if binding == &crate::Binding::BuiltIn(crate::BuiltIn::LocalInvocationId) { | ||
local_invocation_id = Some(id); | ||
|
@@ -755,10 +779,26 @@ impl Writer { | |
)?; | ||
iface.varying_ids.push(varying_id); | ||
let id = self.id_gen.next(); | ||
prelude | ||
.body | ||
.push(Instruction::load(type_id, id, varying_id, None)); | ||
constituent_ids.push(id); | ||
if let Some((f32_ty, _)) = | ||
self.io_f16_polyfills.get_polyfill_info(varying_id) | ||
{ | ||
prelude | ||
.body | ||
.push(Instruction::load(f32_ty, id, varying_id, None)); | ||
let converted = self.id_gen.next(); | ||
super::f16_polyfill::F16IoPolyfill::emit_f32_to_f16_conversion( | ||
id, | ||
type_id, | ||
converted, | ||
&mut prelude.body, | ||
); | ||
constituent_ids.push(converted); | ||
} else { | ||
prelude | ||
.body | ||
.push(Instruction::load(type_id, id, varying_id, None)); | ||
constituent_ids.push(id); | ||
} | ||
|
||
if binding == &crate::Binding::BuiltIn(crate::BuiltIn::GlobalInvocationId) { | ||
local_invocation_id = Some(id); | ||
|
@@ -1220,8 +1260,10 @@ impl Writer { | |
.insert(spirv::Capability::StorageBuffer16BitAccess); | ||
self.capabilities_used | ||
.insert(spirv::Capability::UniformAndStorageBuffer16BitAccess); | ||
self.capabilities_used | ||
.insert(spirv::Capability::StorageInputOutput16); | ||
if self.use_storage_input_output_16 { | ||
self.capabilities_used | ||
.insert(spirv::Capability::StorageInputOutput16); | ||
} | ||
} | ||
Instruction::type_float(id, bits) | ||
} | ||
|
@@ -1904,8 +1946,28 @@ impl Writer { | |
ty: Handle<crate::Type>, | ||
binding: &crate::Binding, | ||
) -> Result<Word, Error> { | ||
use crate::TypeInner; | ||
|
||
let id = self.id_gen.next(); | ||
let pointer_type_id = self.get_handle_pointer_type_id(ty, class); | ||
let ty_inner = &ir_module.types[ty].inner; | ||
let needs_polyfill = self.needs_f16_polyfill(ty_inner); | ||
|
||
let pointer_type_id = if needs_polyfill { | ||
let f32_value_local = | ||
super::f16_polyfill::F16IoPolyfill::create_polyfill_type(ty_inner) | ||
.expect("needs_polyfill returned true but create_polyfill_type returned None"); | ||
|
||
let f32_type_id = self.get_localtype_id(f32_value_local); | ||
let ptr_id = self.get_pointer_type_id(f32_type_id, class); | ||
let f16_type_id = self.get_handle_type_id(ty); | ||
self.io_f16_polyfills | ||
.register_variable(id, f32_type_id, f16_type_id); | ||
|
||
ptr_id | ||
} else { | ||
self.get_handle_pointer_type_id(ty, class) | ||
}; | ||
|
||
Instruction::variable(pointer_type_id, id, class, None) | ||
.to_words(&mut self.logical_layout.declarations); | ||
|
||
|
@@ -2088,8 +2150,9 @@ impl Writer { | |
// > shader, must be decorated Flat | ||
if class == spirv::StorageClass::Input && stage == crate::ShaderStage::Fragment { | ||
let is_flat = match ir_module.types[ty].inner { | ||
crate::TypeInner::Scalar(scalar) | ||
| crate::TypeInner::Vector { scalar, .. } => match scalar.kind { | ||
TypeInner::Scalar(scalar) | TypeInner::Vector { scalar, .. } => match scalar | ||
.kind | ||
{ | ||
Sk::Uint | Sk::Sint | Sk::Bool => true, | ||
Sk::Float => false, | ||
Sk::AbstractInt | Sk::AbstractFloat => { | ||
|
@@ -2584,6 +2647,10 @@ impl Writer { | |
self.decorate(id, spirv::Decoration::NonUniform, &[]); | ||
Ok(()) | ||
} | ||
|
||
pub(super) fn needs_f16_polyfill(&self, ty_inner: &crate::TypeInner) -> bool { | ||
self.io_f16_polyfills.needs_polyfill(ty_inner) | ||
} | ||
} | ||
|
||
#[test] | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
targets = "SPIRV" | ||
god_mode = true | ||
|
||
[spv] | ||
debug = true | ||
version = [1, 1] | ||
use_storage_input_output_16 = true | ||
capabilities = ["Float16"] | ||
|
||
[bounds_check_policies] | ||
index = "ReadZeroSkipWrite" | ||
buffer = "ReadZeroSkipWrite" | ||
image = "ReadZeroSkipWrite" |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: I noticed that
f16_type_id
is never used when recalled viaget_polyfill_info
. Is this a bug, or just some data that can be eliminated?