Skip to content

Commit

Permalink
Replace Base with clamp_to_edge
Browse files Browse the repository at this point in the history
  • Loading branch information
evahop committed Oct 16, 2023
1 parent c3190d5 commit a152303
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 69 deletions.
5 changes: 2 additions & 3 deletions src/back/dot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ fn write_function_expressions(
offset: _,
level,
depth_ref,
clamp_to_edge: _,
} => {
edges.insert("image", image);
edges.insert("sampler", sampler);
Expand All @@ -460,9 +461,7 @@ fn write_function_expressions(
edges.insert("array_index", expr);
}
match level {
crate::SampleLevel::Auto
| crate::SampleLevel::Zero
| crate::SampleLevel::Base => {}
crate::SampleLevel::Auto | crate::SampleLevel::Zero => {}
crate::SampleLevel::Exact(expr) => {
edges.insert("level", expr);
}
Expand Down
15 changes: 4 additions & 11 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,8 @@ impl<'a, W: Write> Writer<'a, W> {
// If we are going to write a `textureSampleBaseClampToEdge` next,
// precompute the half-texel before clamping the coordinates.
if let crate::Expression::ImageSample {
level: crate::SampleLevel::Base,
level: crate::SampleLevel::Zero,
clamp_to_edge: true,
image,
..
} = ctx.expressions[handle]
Expand Down Expand Up @@ -2485,6 +2486,7 @@ impl<'a, W: Write> Writer<'a, W> {
offset,
level,
depth_ref,
clamp_to_edge,
} => {
let dim = match *ctx.info[image].ty.inner_with(&self.module.types) {
TypeInner::Image { dim, .. } => dim,
Expand All @@ -2506,11 +2508,6 @@ impl<'a, W: Write> Writer<'a, W> {
)))
}
crate::SampleLevel::Auto => {}
crate::SampleLevel::Base => {
unreachable!(
"textureSampleBaseClampToEdge should not have passed validation"
)
}
}
}

Expand All @@ -2537,7 +2534,6 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
crate::SampleLevel::Gradient { .. } => "textureGrad",
crate::SampleLevel::Base => "textureLod",
};
let offset_name = match offset {
Some(_) => "Offset",
Expand Down Expand Up @@ -2570,7 +2566,7 @@ impl<'a, W: Write> Writer<'a, W> {
let tex_1d_hack = dim == crate::ImageDimension::D1 && self.options.version.is_es();
let is_vec = tex_1d_hack || coord_dim != 1;

if level == crate::SampleLevel::Base {
if clamp_to_edge {
// clamp the coordinates to [ half_texel, 1 - half_texel ]
write!(self.out, "clamp(")?;
self.write_expr(coordinate, ctx)?;
Expand Down Expand Up @@ -2653,9 +2649,6 @@ impl<'a, W: Write> Writer<'a, W> {
self.write_expr(y, ctx)?;
}
}
crate::SampleLevel::Base => {
write!(self.out, ", 0.0")?;
}
}

if let Some(constant) = offset {
Expand Down
15 changes: 9 additions & 6 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,8 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
// If we are going to write a `textureSampleBaseClampToEdge` next,
// precompute the half-texel before clamping the coordinates.
if let crate::Expression::ImageSample {
level: crate::SampleLevel::Base,
level: crate::SampleLevel::Zero,
clamp_to_edge: true,
image,
..
} = func_ctx.expressions[handle]
Expand Down Expand Up @@ -2394,6 +2395,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
offset,
level,
depth_ref,
clamp_to_edge,
} => {
use crate::SampleLevel as Sl;
const COMPONENTS: [&str; 4] = ["", "Green", "Blue", "Alpha"];
Expand All @@ -2407,9 +2409,10 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
None => "",
};
let level_str = match level {
Sl::Zero if clamp_to_edge => "Level",
Sl::Zero if gather.is_none() => "LevelZero",
Sl::Auto | Sl::Zero => "",
Sl::Exact(_) | Sl::Base => "Level",
Sl::Exact(_) => "Level",
Sl::Bias(_) => "Bias",
Sl::Gradient { .. } => "Grad",
};
Expand All @@ -2418,7 +2421,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
write!(self.out, ".{base_str}{cmp_str}{component_str}{level_str}(")?;
self.write_expr(module, sampler, func_ctx)?;
write!(self.out, ", ")?;
if level == Sl::Base {
if clamp_to_edge {
// clamp the coordinates to [ half_texel, 1 - half_texel ]
write!(self.out, "clamp(")?;
self.write_expr(module, coordinate, func_ctx)?;
Expand All @@ -2445,6 +2448,9 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
}

match level {
Sl::Zero if clamp_to_edge => {
write!(self.out, ", 0.0")?;
}
Sl::Auto | Sl::Zero => {}
Sl::Exact(expr) => {
write!(self.out, ", ")?;
Expand All @@ -2460,9 +2466,6 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
write!(self.out, ", ")?;
self.write_expr(module, y, func_ctx)?;
}
Sl::Base => {
write!(self.out, ", 0.0")?;
}
}

if let Some(offset) = offset {
Expand Down
15 changes: 9 additions & 6 deletions src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,11 +743,15 @@ impl<W: Write> Writer<W> {
&mut self,
image: Handle<crate::Expression>,
level: crate::SampleLevel,
clamp_to_edge: bool,
context: &ExpressionContext,
) -> BackendResult {
let has_levels = context.image_needs_lod(image);
match level {
crate::SampleLevel::Auto => {}
crate::SampleLevel::Zero if clamp_to_edge => {
write!(self.out, ", {NAMESPACE}::level(0.0)")?;
}
crate::SampleLevel::Zero => {
//TODO: do we support Zero on `Sampled` image classes?
}
Expand All @@ -771,9 +775,6 @@ impl<W: Write> Writer<W> {
self.put_expression(y, context, true)?;
write!(self.out, ")")?;
}
crate::SampleLevel::Base => {
write!(self.out, ", {NAMESPACE}::level(0.0)")?;
}
}
Ok(())
}
Expand Down Expand Up @@ -1467,6 +1468,7 @@ impl<W: Write> Writer<W> {
offset,
level,
depth_ref,
clamp_to_edge,
} => {
let main_op = match gather {
Some(_) => "gather",
Expand All @@ -1481,7 +1483,7 @@ impl<W: Write> Writer<W> {
self.put_expression(sampler, context, true)?;
write!(self.out, ", ")?;

if level == crate::SampleLevel::Base {
if clamp_to_edge {
// clamp the coordinates to [ half_texel, 1 - half_texel ]
write!(self.out, "{NAMESPACE}::clamp(")?;
self.put_expression(coordinate, context, true)?;
Expand All @@ -1505,7 +1507,7 @@ impl<W: Write> Writer<W> {
self.put_expression(dref, context, true)?;
}

self.put_image_sample_level(image, level, context)?;
self.put_image_sample_level(image, level, clamp_to_edge, context)?;

if let Some(offset) = offset {
write!(self.out, ", ")?;
Expand Down Expand Up @@ -2671,7 +2673,8 @@ impl<W: Write> Writer<W> {
// If we are going to write a `textureSampleBaseClampToEdge` next,
// precompute the half-texel before clamping the coordinates.
if let crate::Expression::ImageSample {
level: crate::SampleLevel::Base,
level: crate::SampleLevel::Zero,
clamp_to_edge: true,
image,
..
} = context.expression.function.expressions[handle]
Expand Down
2 changes: 2 additions & 0 deletions src/back/spv/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,7 @@ impl<'w> BlockContext<'w> {
offset,
level,
depth_ref,
clamp_to_edge,
} => self.write_image_sample(
result_type_id,
image,
Expand All @@ -1322,6 +1323,7 @@ impl<'w> BlockContext<'w> {
offset,
level,
depth_ref,
clamp_to_edge,
block,
)?,
crate::Expression::Select {
Expand Down
5 changes: 3 additions & 2 deletions src/back/spv/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ impl<'w> BlockContext<'w> {
offset: Option<Handle<crate::Expression>>,
level: crate::SampleLevel,
depth_ref: Option<Handle<crate::Expression>>,
clamp_to_edge: bool,
block: &mut Block,
) -> Result<Word, Error> {
use super::instructions::SampleLod;
Expand Down Expand Up @@ -947,7 +948,7 @@ impl<'w> BlockContext<'w> {
self.get_type_id(LookupType::Local(LocalType::SampledImage { image_type_id }));

let sampler_id = self.get_handle_id(sampler);
let coordinates_id = if level == crate::SampleLevel::Base {
let coordinates_id = if clamp_to_edge {
self.write_clamped_image_coordinates(image_id, coordinate, block)?
} else {
self.write_image_coordinates(coordinate, array_index, block)?
Expand Down Expand Up @@ -983,7 +984,7 @@ impl<'w> BlockContext<'w> {
}
inst
}
(crate::SampleLevel::Zero | crate::SampleLevel::Base, None) => {
(crate::SampleLevel::Zero, None) => {
let mut inst = Instruction::image_sample(
sample_result_type_id,
id,
Expand Down
7 changes: 5 additions & 2 deletions src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,7 @@ impl<W: Write> Writer<W> {
offset,
level,
depth_ref,
clamp_to_edge,
} => {
use crate::SampleLevel as Sl;

Expand All @@ -1254,10 +1255,10 @@ impl<W: Write> Writer<W> {
};
let suffix_level = match level {
Sl::Auto => "",
Sl::Zero if clamp_to_edge => "BaseClampToEdge",
Sl::Zero | Sl::Exact(_) => "Level",
Sl::Bias(_) => "Bias",
Sl::Gradient { .. } => "Grad",
Sl::Base => "BaseClampToEdge",
};

write!(self.out, "textureSample{suffix_cmp}{suffix_level}(")?;
Expand All @@ -1278,7 +1279,8 @@ impl<W: Write> Writer<W> {
}

match level {
Sl::Auto | Sl::Base => {}
Sl::Auto => {}
Sl::Zero if clamp_to_edge => {}
Sl::Zero => {
// Level 0 is implied for depth comparison
if depth_ref.is_none() {
Expand Down Expand Up @@ -1318,6 +1320,7 @@ impl<W: Write> Writer<W> {
offset,
level: _,
depth_ref,
clamp_to_edge: _,
} => {
let suffix_cmp = match depth_ref {
Some(_) => "Compare",
Expand Down
6 changes: 4 additions & 2 deletions src/compact/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl<'tracer> ExpressionTracer<'tracer> {
offset,
ref level,
depth_ref,
clamp_to_edge: _,
} => {
work_list.push(image);
work_list.push(sampler);
Expand All @@ -96,7 +97,7 @@ impl<'tracer> ExpressionTracer<'tracer> {
}
use crate::SampleLevel as Sl;
match *level {
Sl::Auto | Sl::Zero | Sl::Base => {}
Sl::Auto | Sl::Zero => {}
Sl::Exact(expr) | Sl::Bias(expr) => work_list.push(expr),
Sl::Gradient { x, y } => work_list.extend([x, y]),
}
Expand Down Expand Up @@ -266,6 +267,7 @@ impl ModuleMap {
ref mut offset,
ref mut level,
ref mut depth_ref,
clamp_to_edge: _,
} => {
adjust(image);
adjust(sampler);
Expand Down Expand Up @@ -366,7 +368,7 @@ impl ModuleMap {

use crate::SampleLevel as Sl;
match *level {
Sl::Auto | Sl::Zero | Sl::Base => {}
Sl::Auto | Sl::Zero => {}
Sl::Exact(ref mut expr) => adjust(expr),
Sl::Bias(ref mut expr) => adjust(expr),
Sl::Gradient {
Expand Down
1 change: 1 addition & 0 deletions src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,7 @@ fn texture_call(
offset,
level,
depth_ref: comps.depth_ref,
clamp_to_edge: false,
},
meta,
)?)
Expand Down
1 change: 1 addition & 0 deletions src/front/spv/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
offset,
level,
depth_ref,
clamp_to_edge: false,
};
self.lookup_expression.insert(
result_id,
Expand Down
8 changes: 7 additions & 1 deletion src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,8 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
.then(|| self.expression(args.next()?, ctx.reborrow()))
.transpose()?;

let mut clamp_to_edge = false;

let (level, depth_ref) = match fun {
Texture::Gather => (crate::SampleLevel::Zero, None),
Texture::GatherCompare => {
Expand Down Expand Up @@ -2428,7 +2430,10 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
let level = self.expression(args.next()?, ctx.reborrow())?;
(crate::SampleLevel::Exact(level), None)
}
Texture::SampleBaseClampToEdge => (crate::SampleLevel::Base, None),
Texture::SampleBaseClampToEdge => {
clamp_to_edge = true;
(crate::SampleLevel::Zero, None)
}
};

let offset = args
Expand All @@ -2448,6 +2453,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
offset,
level,
depth_ref,
clamp_to_edge,
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,6 @@ pub enum SampleLevel {
x: Handle<Expression>,
y: Handle<Expression>,
},
Base,
}

/// Type of an image query.
Expand Down Expand Up @@ -1415,6 +1414,7 @@ pub enum Expression {
offset: Option<Handle<Expression>>,
level: SampleLevel,
depth_ref: Option<Handle<Expression>>,
clamp_to_edge: bool,
},

/// Load a texel from an image.
Expand Down
2 changes: 1 addition & 1 deletion src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ impl crate::SampleLevel {
pub const fn implicit_derivatives(&self) -> bool {
match *self {
Self::Auto | Self::Bias(_) => true,
Self::Zero | Self::Exact(_) | Self::Gradient { .. } | Self::Base => false,
Self::Zero | Self::Exact(_) | Self::Gradient { .. } => false,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/valid/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ impl FunctionInfo {
offset: _,
level,
depth_ref,
clamp_to_edge: _,
} => {
let image_storage = GlobalOrArgument::from_expression(expression_arena, image)?;
let sampler_storage = GlobalOrArgument::from_expression(expression_arena, sampler)?;
Expand All @@ -619,7 +620,7 @@ impl FunctionInfo {
// "nur" == "Non-Uniform Result"
let array_nur = array_index.and_then(|h| self.add_ref(h));
let level_nur = match level {
Sl::Auto | Sl::Zero | Sl::Base => None,
Sl::Auto | Sl::Zero => None,
Sl::Exact(h) | Sl::Bias(h) => self.add_ref(h),
Sl::Gradient { x, y } => self.add_ref(x).or(self.add_ref(y)),
};
Expand Down
Loading

0 comments on commit a152303

Please sign in to comment.