-
Notifications
You must be signed in to change notification settings - Fork 962
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[spv/msl/hlsl-out] support pipeline constant value replacements
- Loading branch information
Showing
23 changed files
with
368 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,118 @@ | ||
use super::PipelineConstants; | ||
use crate::{Arena, Constant, Expression, Literal, Module, Scalar, Span, TypeInner}; | ||
use std::borrow::Cow; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug, Clone)] | ||
pub enum PipelineConstantError { | ||
#[error("Missing value for pipeline-overridable constant with identifier string: '{0}'")] | ||
MissingValue(String), | ||
#[error("Source f64 value needs to be finite (NaNs and Inifinites are not allowed) for number destinations")] | ||
SrcNeedsToBeFinite, | ||
#[error("Source f64 value doesn't fit in destination")] | ||
DstRangeTooSmall, | ||
} | ||
|
||
pub(super) fn process_overrides<'a>( | ||
module: &'a Module, | ||
pipeline_constants: &PipelineConstants, | ||
) -> Result<Cow<'a, Module>, PipelineConstantError> { | ||
if module.overrides.is_empty() { | ||
return Ok(Cow::Borrowed(module)); | ||
} | ||
|
||
let mut module = module.clone(); | ||
let overrides = std::mem::replace(&mut module.overrides, Arena::new()); | ||
|
||
for (_handle, override_, span) in overrides.drain() { | ||
let key = if let Some(id) = override_.id { | ||
Cow::Owned(id.to_string()) | ||
} else if let Some(ref name) = override_.name { | ||
Cow::Borrowed(name) | ||
} else { | ||
unreachable!(); | ||
}; | ||
let init = if let Some(value) = pipeline_constants.get::<str>(&key) { | ||
let literal = match module.types[override_.ty].inner { | ||
TypeInner::Scalar(scalar) => map_value_to_literal(*value, scalar)?, | ||
_ => unreachable!(), | ||
}; | ||
module | ||
.const_expressions | ||
.append(Expression::Literal(literal), Span::UNDEFINED) | ||
} else if let Some(init) = override_.init { | ||
init | ||
} else { | ||
return Err(PipelineConstantError::MissingValue(key.to_string())); | ||
}; | ||
let constant = Constant { | ||
name: override_.name, | ||
ty: override_.ty, | ||
init, | ||
}; | ||
module.constants.append(constant, span); | ||
} | ||
|
||
Ok(Cow::Owned(module)) | ||
} | ||
|
||
fn map_value_to_literal(value: f64, scalar: Scalar) -> Result<Literal, PipelineConstantError> { | ||
// note that in rust 0.0 == -0.0 | ||
match scalar { | ||
Scalar::BOOL => { | ||
// https://webidl.spec.whatwg.org/#js-boolean | ||
let value = value != 0.0 && !value.is_nan(); | ||
Ok(Literal::Bool(value)) | ||
} | ||
Scalar::I32 => { | ||
// https://webidl.spec.whatwg.org/#js-long | ||
if value.is_finite() { | ||
let value = value.abs().floor() * value.signum(); | ||
if value < f64::from(i32::MIN) || value > f64::from(i32::MAX) { | ||
Err(PipelineConstantError::DstRangeTooSmall) | ||
} else { | ||
let value = value as i32; | ||
Ok(Literal::I32(value)) | ||
} | ||
} else { | ||
Err(PipelineConstantError::SrcNeedsToBeFinite) | ||
} | ||
} | ||
Scalar::U32 => { | ||
// https://webidl.spec.whatwg.org/#js-unsigned-long | ||
if value.is_finite() { | ||
let value = value.abs().floor() * value.signum(); | ||
if value < f64::from(u32::MIN) || value > f64::from(u32::MAX) { | ||
Err(PipelineConstantError::DstRangeTooSmall) | ||
} else { | ||
let value = value as u32; | ||
Ok(Literal::U32(value)) | ||
} | ||
} else { | ||
Err(PipelineConstantError::SrcNeedsToBeFinite) | ||
} | ||
} | ||
Scalar::F32 => { | ||
// https://webidl.spec.whatwg.org/#js-float | ||
if value.is_finite() { | ||
let value = value as f32; | ||
if value.is_finite() { | ||
Ok(Literal::F32(value)) | ||
} else { | ||
Err(PipelineConstantError::DstRangeTooSmall) | ||
} | ||
} else { | ||
Err(PipelineConstantError::SrcNeedsToBeFinite) | ||
} | ||
} | ||
Scalar::F64 => { | ||
// https://webidl.spec.whatwg.org/#js-double | ||
if value.is_finite() { | ||
Ok(Literal::F64(value)) | ||
} else { | ||
Err(PipelineConstantError::SrcNeedsToBeFinite) | ||
} | ||
} | ||
_ => unreachable!(), | ||
} | ||
} |
This file contains 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 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 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 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 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,11 @@ | ||
( | ||
spv: ( | ||
version: (1, 0), | ||
separate_entry_points: true, | ||
), | ||
pipeline_constants: { | ||
"0": NaN, | ||
"1300": 1.1, | ||
"depth": 2.3, | ||
} | ||
) |
This file contains 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 |
---|---|---|
|
@@ -12,3 +12,6 @@ | |
// overridable constant. | ||
|
||
override inferred_f32 = 2.718; | ||
|
||
@compute @workgroup_size(1) | ||
fn main() {} |
This file contains 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 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,12 @@ | ||
static const bool has_point_light = false; | ||
static const float specular_param = 2.3; | ||
static const float gain = 1.1; | ||
static const float width = 0.0; | ||
static const float depth = 2.3; | ||
static const float inferred_f32_ = 2.718; | ||
|
||
[numthreads(1, 1, 1)] | ||
void main() | ||
{ | ||
return; | ||
} |
This file contains 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,12 @@ | ||
( | ||
vertex:[ | ||
], | ||
fragment:[ | ||
], | ||
compute:[ | ||
( | ||
entry_point:"main", | ||
target_profile:"cs_5_1", | ||
), | ||
], | ||
) |
This file contains 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
Oops, something went wrong.