Skip to content

Commit

Permalink
Add FMA feature to glsl backend
Browse files Browse the repository at this point in the history
- I think this is right. Just iterate all known expressions in all
  functions and entry points to locate any `fma` function call.
  Should not need to walk the statement DAG.
  • Loading branch information
parasyte committed Dec 7, 2021
1 parent beddc71 commit 5cec686
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/back/glsl/features.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{BackendResult, Error, Version, Writer};
use crate::{
Binding, Bytes, Handle, ImageClass, ImageDimension, Interpolation, Sampling, ScalarKind,
ShaderStage, StorageClass, StorageFormat, Type, TypeInner,
Binding, Bytes, Expression, Handle, ImageClass, ImageDimension, Interpolation, MathFunction,
Sampling, ScalarKind, ShaderStage, StorageClass, StorageFormat, Type, TypeInner,
};
use std::fmt::Write;

Expand Down Expand Up @@ -33,6 +33,8 @@ bitflags::bitflags! {
/// Arrays with a dynamic length
const DYNAMIC_ARRAY_SIZE = 1 << 16;
const MULTI_VIEW = 1 << 17;
/// Adds support for fused multiply-add
const FMA = 1 << 18;
}
}

Expand Down Expand Up @@ -98,6 +100,7 @@ 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);

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

if self.0.contains(Features::FMA) && version.is_es() {
// https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_gpu_shader5.txt
writeln!(out, "#extension GL_EXT_gpu_shader5 : require")?;
}

Ok(())
}
}
Expand Down Expand Up @@ -347,6 +355,25 @@ impl<'a, W> Writer<'a, W> {
}
}

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);
}

self.features.check_availability(self.options.version)
}

Expand Down
1 change: 1 addition & 0 deletions tests/out/glsl/functions.main.Compute.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#version 310 es
#extension GL_EXT_gpu_shader5 : require

precision highp float;
precision highp int;
Expand Down

0 comments on commit 5cec686

Please sign in to comment.