Skip to content

Commit

Permalink
glsl-out: Fix feature search in expressions
Browse files Browse the repository at this point in the history
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 5, 2022
1 parent 1869c68 commit cf32c2b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
55 changes: 35 additions & 20 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 @@ -205,11 +210,19 @@ impl FeaturesManager {
writeln!(out, "#extension GL_EXT_multiview : require")?;
}

if self.0.contains(Features::FMA) && version.is_es() {
if self.0.contains(Features::FMA) && version >= Version::Embedded(310) {
// https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_gpu_shader5.txt
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

0 comments on commit cf32c2b

Please sign in to comment.