Skip to content

Commit

Permalink
glsl-out: Fix feature search in expressions
Browse files Browse the repository at this point in the history
The fma wasn't being searched in case it wasn't supported which is the
opposite of what we want since if it's used we want to error if it isn't
supported.

It was also searching in all entry points instead of only in the current
one.

All samples queries need the ARB_shader_texture_image_samples extension
so we need to check if any samples queries are made and if so request
the extension.
  • Loading branch information
JCapucho committed May 3, 2022
1 parent a06b604 commit 92f94a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
53 changes: 34 additions & 19 deletions src/back/glsl/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ bitflags::bitflags! {
const MULTI_VIEW = 1 << 17;
/// Fused multiply-add.
const FMA = 1 << 18;
/// Texture samples query
const TEXTURE_SAMPLES = 1 << 19;
}
}

Expand Down Expand Up @@ -101,7 +103,10 @@ impl FeaturesManager {
check_feature!(SAMPLE_VARIABLES, 400, 300);
check_feature!(DYNAMIC_ARRAY_SIZE, 430, 310);
check_feature!(MULTI_VIEW, 140, 310);
check_feature!(FMA, 400, 310);
// Only available on glsl core, this means that opengl es can't query the number
// of samples in a image and neither do bound checks on the sample argument
// of texelFecth
check_feature!(TEXTURE_SAMPLES, 150);

// Return an error if there are missing features
if missing.is_empty() {
Expand Down Expand Up @@ -210,6 +215,14 @@ impl FeaturesManager {
writeln!(out, "#extension GL_EXT_gpu_shader5 : require")?;
}

if self.0.contains(Features::TEXTURE_SAMPLES) {
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shader_texture_image_samples.txt
writeln!(
out,
"#extension GL_ARB_shader_texture_image_samples : require"
)?;
}

Ok(())
}
}
Expand Down Expand Up @@ -363,24 +376,26 @@ impl<'a, W> Writer<'a, W> {
}
}

if self.options.version.supports_fma_function() {
let has_fma = self
.module
.functions
.iter()
.flat_map(|(_, f)| f.expressions.iter())
.chain(
self.module
.entry_points
.iter()
.flat_map(|e| e.function.expressions.iter()),
)
.any(|(_, e)| match *e {
Expression::Math { fun, .. } if fun == MathFunction::Fma => true,
_ => false,
});
if has_fma {
self.features.request(Features::FMA);
// Loop trough all expressions in both functions and entry points
// to check for needed features
for (_, expr) in self
.module
.functions
.iter()
.flat_map(|(_, f)| f.expressions.iter())
.chain(self.entry_point.function.expressions.iter())
{
match *expr {
// Check for fused multiply add use
Expression::Math { fun, .. } if fun == MathFunction::Fma => {
self.features.request(Features::FMA)
}
// Check for samples query
Expression::ImageQuery {
query: crate::ImageQuery::NumSamples,
..
} => self.features.request(Features::TEXTURE_SAMPLES),
_ => {}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,6 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
crate::ImageQuery::NumSamples => {
// assumes ARB_shader_texture_image_samples
let fun_name = match class {
ImageClass::Sampled { .. } | ImageClass::Depth { .. } => {
"textureSamples"
Expand Down
1 change: 1 addition & 0 deletions tests/out/glsl/functions-webgl.main.Vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#version 300 es
#extension GL_EXT_gpu_shader5 : require

precision highp float;
precision highp int;
Expand Down

0 comments on commit 92f94a7

Please sign in to comment.