From 6d4c2c4df36f87d574f9f624beb1d82998521a5e Mon Sep 17 00:00:00 2001 From: Evan Mark Hopkins Date: Mon, 25 Sep 2023 06:19:52 -0400 Subject: [PATCH] [glsl-out] Polyfill frexp --- src/back/glsl/mod.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index 54d5f341a2..8a8dea664c 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -189,6 +189,10 @@ impl Version { *self >= Version::Desktop(400) || *self >= Version::new_gles(310) } + fn supports_frexp_function(&self) -> bool { + *self >= Version::Desktop(400) || *self >= Version::new_gles(310) + } + fn supports_derivative_control(&self) -> bool { *self >= Version::Desktop(450) } @@ -667,15 +671,27 @@ impl<'a, W: Write> Writer<'a, W> { let struct_name = &self.names[&NameKey::Type(*struct_ty)]; writeln!(self.out)?; - writeln!( - self.out, - "{} {defined_func_name}({arg_type_name} arg) {{ + if !self.options.version.supports_frexp_function() + && matches!(type_key, &crate::PredeclaredType::FrexpResult { .. }) + { + writeln!( + self.out, + "{struct_name} {defined_func_name}({arg_type_name} arg) {{ + {other_type_name} other = arg == {arg_type_name}(0) ? {other_type_name}(0) : {other_type_name}({arg_type_name}(1) + log2(arg)); + {arg_type_name} fract = arg * exp2({arg_type_name}(-other)); + return {struct_name}(fract, other); +}}", + )?; + } else { + writeln!( + self.out, + "{struct_name} {defined_func_name}({arg_type_name} arg) {{ {other_type_name} other; {arg_type_name} fract = {called_func_name}(arg, other); - return {}(fract, other); + return {struct_name}(fract, other); }}", - struct_name, struct_name - )?; + )?; + } } &crate::PredeclaredType::AtomicCompareExchangeWeakResult { .. } => {} }