Skip to content

Commit 7df0aa6

Browse files
committed
move the burden of evaluating override-expressions to users of naga's API
1 parent 7bed9e8 commit 7df0aa6

File tree

20 files changed

+175
-168
lines changed

20 files changed

+175
-168
lines changed

naga-cli/src/bin/naga.rs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -597,17 +597,18 @@ fn write_output(
597597
let mut options = params.msl.clone();
598598
options.bounds_check_policies = params.bounds_check_policies;
599599

600+
let info = info.as_ref().ok_or(CliError(
601+
"Generating metal output requires validation to \
602+
succeed, and it failed in a previous step",
603+
))?;
604+
605+
let (module, info) =
606+
naga::back::pipeline_constants::process_overrides(module, info, &params.overrides)
607+
.unwrap_pretty();
608+
600609
let pipeline_options = msl::PipelineOptions::default();
601-
let (msl, _) = msl::write_string(
602-
module,
603-
info.as_ref().ok_or(CliError(
604-
"Generating metal output requires validation to \
605-
succeed, and it failed in a previous step",
606-
))?,
607-
&options,
608-
&pipeline_options,
609-
)
610-
.unwrap_pretty();
610+
let (msl, _) =
611+
msl::write_string(&module, &info, &options, &pipeline_options).unwrap_pretty();
611612
fs::write(output_path, msl)?;
612613
}
613614
"spv" => {
@@ -624,23 +625,23 @@ fn write_output(
624625
pipeline_options_owned = spv::PipelineOptions {
625626
entry_point: name.clone(),
626627
shader_stage: module.entry_points[ep_index].stage,
627-
constants: naga::back::PipelineConstants::default(),
628628
};
629629
Some(&pipeline_options_owned)
630630
}
631631
None => None,
632632
};
633633

634-
let spv = spv::write_vec(
635-
module,
636-
info.as_ref().ok_or(CliError(
637-
"Generating SPIR-V output requires validation to \
638-
succeed, and it failed in a previous step",
639-
))?,
640-
&params.spv_out,
641-
pipeline_options,
642-
)
643-
.unwrap_pretty();
634+
let info = info.as_ref().ok_or(CliError(
635+
"Generating SPIR-V output requires validation to \
636+
succeed, and it failed in a previous step",
637+
))?;
638+
639+
let (module, info) =
640+
naga::back::pipeline_constants::process_overrides(module, info, &params.overrides)
641+
.unwrap_pretty();
642+
643+
let spv =
644+
spv::write_vec(&module, &info, &params.spv_out, pipeline_options).unwrap_pretty();
644645
let bytes = spv
645646
.iter()
646647
.fold(Vec::with_capacity(spv.len() * 4), |mut v, w| {
@@ -665,17 +666,22 @@ fn write_output(
665666
_ => unreachable!(),
666667
},
667668
multiview: None,
668-
constants: naga::back::PipelineConstants::default(),
669669
};
670670

671+
let info = info.as_ref().ok_or(CliError(
672+
"Generating glsl output requires validation to \
673+
succeed, and it failed in a previous step",
674+
))?;
675+
676+
let (module, info) =
677+
naga::back::pipeline_constants::process_overrides(module, info, &params.overrides)
678+
.unwrap_pretty();
679+
671680
let mut buffer = String::new();
672681
let mut writer = glsl::Writer::new(
673682
&mut buffer,
674-
module,
675-
info.as_ref().ok_or(CliError(
676-
"Generating glsl output requires validation to \
677-
succeed, and it failed in a previous step",
678-
))?,
683+
&module,
684+
&info,
679685
&params.glsl,
680686
&pipeline_options,
681687
params.bounds_check_policies,
@@ -692,20 +698,19 @@ fn write_output(
692698
}
693699
"hlsl" => {
694700
use naga::back::hlsl;
701+
702+
let info = info.as_ref().ok_or(CliError(
703+
"Generating hlsl output requires validation to \
704+
succeed, and it failed in a previous step",
705+
))?;
706+
707+
let (module, info) =
708+
naga::back::pipeline_constants::process_overrides(module, info, &params.overrides)
709+
.unwrap_pretty();
710+
695711
let mut buffer = String::new();
696712
let mut writer = hlsl::Writer::new(&mut buffer, &params.hlsl);
697-
writer
698-
.write(
699-
module,
700-
info.as_ref().ok_or(CliError(
701-
"Generating hlsl output requires validation to \
702-
succeed, and it failed in a previous step",
703-
))?,
704-
&hlsl::PipelineOptions {
705-
constants: params.overrides.clone(),
706-
},
707-
)
708-
.unwrap_pretty();
713+
writer.write(&module, &info).unwrap_pretty();
709714
fs::write(output_path, buffer)?;
710715
}
711716
"wgsl" => {

naga/benches/criterion.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ fn backends(c: &mut Criterion) {
193193
let pipeline_options = naga::back::spv::PipelineOptions {
194194
shader_stage: ep.stage,
195195
entry_point: ep.name.clone(),
196-
constants: naga::back::PipelineConstants::default(),
197196
};
198197
writer
199198
.write(module, info, Some(&pipeline_options), &None, &mut data)
@@ -224,11 +223,10 @@ fn backends(c: &mut Criterion) {
224223
group.bench_function("hlsl", |b| {
225224
b.iter(|| {
226225
let options = naga::back::hlsl::Options::default();
227-
let pipeline_options = naga::back::hlsl::PipelineOptions::default();
228226
let mut string = String::new();
229227
for &(ref module, ref info) in inputs.iter() {
230228
let mut writer = naga::back::hlsl::Writer::new(&mut string, &options);
231-
let _ = writer.write(module, info, &pipeline_options); // may fail on unimplemented things
229+
let _ = writer.write(module, info); // may fail on unimplemented things
232230
string.clear();
233231
}
234232
});
@@ -250,7 +248,6 @@ fn backends(c: &mut Criterion) {
250248
shader_stage: ep.stage,
251249
entry_point: ep.name.clone(),
252250
multiview: None,
253-
constants: naga::back::PipelineConstants::default(),
254251
};
255252

256253
// might be `Err` if missing features

naga/src/back/glsl/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ pub struct PipelineOptions {
294294
pub entry_point: String,
295295
/// How many views to render to, if doing multiview rendering.
296296
pub multiview: Option<std::num::NonZeroU32>,
297-
/// Pipeline constants.
298-
pub constants: back::PipelineConstants,
299297
}
300298

301299
#[derive(Debug)]
@@ -499,6 +497,8 @@ pub enum Error {
499497
ImageMultipleSamplers,
500498
#[error("{0}")]
501499
Custom(String),
500+
#[error("overrides should not be present at this stage")]
501+
Override,
502502
}
503503

504504
/// Binary operation with a different logic on the GLSL side.
@@ -568,9 +568,7 @@ impl<'a, W: Write> Writer<'a, W> {
568568
policies: proc::BoundsCheckPolicies,
569569
) -> Result<Self, Error> {
570570
if !module.overrides.is_empty() {
571-
return Err(Error::Custom(
572-
"Pipeline constants are not yet supported for this back-end".to_string(),
573-
));
571+
return Err(Error::Override);
574572
}
575573

576574
// Check if the requested version is supported
@@ -2544,7 +2542,7 @@ impl<'a, W: Write> Writer<'a, W> {
25442542
|writer, expr| writer.write_expr(expr, ctx),
25452543
)?;
25462544
}
2547-
Expression::Override(_) => return Err(Error::Custom("overrides are WIP".into())),
2545+
Expression::Override(_) => return Err(Error::Override),
25482546
// `Access` is applied to arrays, vectors and matrices and is written as indexing
25492547
Expression::Access { base, index } => {
25502548
self.write_expr(base, ctx)?;

naga/src/back/hlsl/mod.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,6 @@ pub struct Options {
195195
pub zero_initialize_workgroup_memory: bool,
196196
}
197197

198-
#[derive(Clone, Debug, Default)]
199-
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
200-
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
201-
pub struct PipelineOptions {
202-
/// Pipeline constants.
203-
pub constants: back::PipelineConstants,
204-
}
205-
206198
impl Default for Options {
207199
fn default() -> Self {
208200
Options {
@@ -255,8 +247,8 @@ pub enum Error {
255247
Unimplemented(String), // TODO: Error used only during development
256248
#[error("{0}")]
257249
Custom(String),
258-
#[error(transparent)]
259-
PipelineConstant(#[from] Box<back::pipeline_constants::PipelineConstantError>),
250+
#[error("overrides should not be present at this stage")]
251+
Override,
260252
}
261253

262254
#[derive(Default)]

naga/src/back/hlsl/writer.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{
22
help::{WrappedArrayLength, WrappedConstructor, WrappedImageQuery, WrappedStructMatrixAccess},
33
storage::StoreValue,
4-
BackendResult, Error, Options, PipelineOptions,
4+
BackendResult, Error, Options,
55
};
66
use crate::{
77
back,
@@ -167,16 +167,10 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
167167
&mut self,
168168
module: &Module,
169169
module_info: &valid::ModuleInfo,
170-
pipeline_options: &PipelineOptions,
171170
) -> Result<super::ReflectionInfo, Error> {
172-
let (module, module_info) = back::pipeline_constants::process_overrides(
173-
module,
174-
module_info,
175-
&pipeline_options.constants,
176-
)
177-
.map_err(Box::new)?;
178-
let module = module.as_ref();
179-
let module_info = module_info.as_ref();
171+
if !module.overrides.is_empty() {
172+
return Err(Error::Override);
173+
}
180174

181175
self.reset(module);
182176

@@ -2150,9 +2144,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
21502144
|writer, expr| writer.write_expr(module, expr, func_ctx),
21512145
)?;
21522146
}
2153-
Expression::Override(_) => {
2154-
return Err(Error::Unimplemented("overrides are WIP".into()))
2155-
}
2147+
Expression::Override(_) => return Err(Error::Override),
21562148
// All of the multiplication can be expressed as `mul`,
21572149
// except vector * vector, which needs to use the "*" operator.
21582150
Expression::Binary {

naga/src/back/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub mod wgsl;
2222
feature = "spv-out",
2323
feature = "glsl-out"
2424
))]
25-
mod pipeline_constants;
25+
pub mod pipeline_constants;
2626

2727
/// Names of vector components.
2828
pub const COMPONENTS: &[char] = &['x', 'y', 'z', 'w'];

naga/src/back/msl/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ pub enum Error {
143143
UnsupportedArrayOfType(Handle<crate::Type>),
144144
#[error("ray tracing is not supported prior to MSL 2.3")]
145145
UnsupportedRayTracing,
146-
#[error(transparent)]
147-
PipelineConstant(#[from] Box<crate::back::pipeline_constants::PipelineConstantError>),
146+
#[error("overrides should not be present at this stage")]
147+
Override,
148148
}
149149

150150
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
@@ -234,8 +234,6 @@ pub struct PipelineOptions {
234234
///
235235
/// Enable this for vertex shaders with point primitive topologies.
236236
pub allow_and_force_point_size: bool,
237-
/// Pipeline constants.
238-
pub constants: crate::back::PipelineConstants,
239237
}
240238

241239
impl Options {

naga/src/back/msl/writer.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,9 +1431,7 @@ impl<W: Write> Writer<W> {
14311431
|writer, context, expr| writer.put_expression(expr, context, true),
14321432
)?;
14331433
}
1434-
crate::Expression::Override(_) => {
1435-
return Err(Error::FeatureNotImplemented("overrides are WIP".into()))
1436-
}
1434+
crate::Expression::Override(_) => return Err(Error::Override),
14371435
crate::Expression::Access { base, .. }
14381436
| crate::Expression::AccessIndex { base, .. } => {
14391437
// This is an acceptable place to generate a `ReadZeroSkipWrite` check.
@@ -3223,11 +3221,9 @@ impl<W: Write> Writer<W> {
32233221
options: &Options,
32243222
pipeline_options: &PipelineOptions,
32253223
) -> Result<TranslationInfo, Error> {
3226-
let (module, info) =
3227-
back::pipeline_constants::process_overrides(module, info, &pipeline_options.constants)
3228-
.map_err(Box::new)?;
3229-
let module = module.as_ref();
3230-
let info = info.as_ref();
3224+
if !module.overrides.is_empty() {
3225+
return Err(Error::Override);
3226+
}
32313227

32323228
self.names.clear();
32333229
self.namer.reset(

naga/src/back/pipeline_constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub enum PipelineConstantError {
3636
/// fully-evaluated expressions.
3737
///
3838
/// [`global_expressions`]: Module::global_expressions
39-
pub(super) fn process_overrides<'a>(
39+
pub fn process_overrides<'a>(
4040
module: &'a Module,
4141
module_info: &'a ModuleInfo,
4242
pipeline_constants: &PipelineConstants,

naga/src/back/spv/block.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ impl<'w> BlockContext<'w> {
239239
let init = self.ir_module.constants[handle].init;
240240
self.writer.constant_ids[init.index()]
241241
}
242-
crate::Expression::Override(_) => {
243-
return Err(Error::FeatureNotImplemented("overrides are WIP"))
244-
}
242+
crate::Expression::Override(_) => return Err(Error::Override),
245243
crate::Expression::ZeroValue(_) => self.writer.get_constant_null(result_type_id),
246244
crate::Expression::Compose { ty, ref components } => {
247245
self.temp_list.clear();

0 commit comments

Comments
 (0)