Skip to content

Commit

Permalink
Don't use layout qualifiers to allow for GLSL 140 support (#2575)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc authored Oct 24, 2023
1 parent 65f2216 commit d105909
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
61 changes: 53 additions & 8 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use std::{
cmp::Ordering,
fmt,
fmt::{Error as FmtError, Write},
mem,
};
use thiserror::Error;

Expand All @@ -64,7 +65,7 @@ mod features;
mod keywords;

/// List of supported `core` GLSL versions.
pub const SUPPORTED_CORE_VERSIONS: &[u16] = &[330, 400, 410, 420, 430, 440, 450];
pub const SUPPORTED_CORE_VERSIONS: &[u16] = &[140, 150, 330, 400, 410, 420, 430, 440, 450, 460];
/// List of supported `es` GLSL versions.
pub const SUPPORTED_ES_VERSIONS: &[u16] = &[300, 310, 320];

Expand Down Expand Up @@ -163,6 +164,10 @@ impl Version {
}
}

fn supports_io_locations(&self) -> bool {
*self >= Version::Desktop(330) || *self >= Version::new_gles(300)
}

/// Checks if the version supports all of the explicit layouts:
/// - `location=` qualifiers for bindings
/// - `binding=` qualifiers for resources
Expand Down Expand Up @@ -285,12 +290,25 @@ pub struct PipelineOptions {
pub multiview: Option<std::num::NonZeroU32>,
}

#[derive(Debug)]
pub struct VaryingLocation {
/// The location of the global.
/// This corresponds to `layout(location = ..)` in GLSL.
pub location: u32,
/// The index which can be used for dual source blending.
/// This corresponds to `layout(index = ..)` in GLSL.
pub index: u32,
}

/// Reflection info for texture mappings and uniforms.
#[derive(Debug)]
pub struct ReflectionInfo {
/// Mapping between texture names and variables/samplers.
pub texture_mapping: crate::FastHashMap<String, TextureMapping>,
/// Mapping between uniform variables and names.
pub uniforms: crate::FastHashMap<Handle<crate::GlobalVariable>, String>,
/// Mapping between names and attribute locations.
pub varying: crate::FastHashMap<String, VaryingLocation>,
}

/// Mapping between a texture and its sampler, if it exists.
Expand Down Expand Up @@ -463,6 +481,8 @@ pub struct Writer<'a, W> {
need_bake_expressions: back::NeedBakeExpressions,
/// How many views to render to, if doing multiview rendering.
multiview: Option<std::num::NonZeroU32>,
/// Mapping of varying variables to their location. Needed for reflections.
varying: crate::FastHashMap<String, VaryingLocation>,
}

impl<'a, W: Write> Writer<'a, W> {
Expand Down Expand Up @@ -525,6 +545,7 @@ impl<'a, W: Write> Writer<'a, W> {
block_id: IdGenerator::default(),
named_expressions: Default::default(),
need_bake_expressions: Default::default(),
varying: Default::default(),
};

// Find all features required to print this module
Expand Down Expand Up @@ -1006,9 +1027,16 @@ impl<'a, W: Write> Writer<'a, W> {
Ic::Storage { format, .. } => ("image", format.into(), "", ""),
};

let precision = if self.options.version.is_es() {
"highp "
} else {
""
};

write!(
self.out,
"highp {}{}{}{}{}{}",
"{}{}{}{}{}{}{}",
precision,
glsl_scalar(kind, 4)?.prefix,
base,
glsl_dimension(dim),
Expand Down Expand Up @@ -1367,13 +1395,25 @@ impl<'a, W: Write> Writer<'a, W> {
};

// Write the I/O locations, if allowed
if self.options.version.supports_explicit_locations() || !emit_interpolation_and_auxiliary {
if second_blend_source {
write!(self.out, "layout(location = {location}, index = 1) ")?;
let io_location = if self.options.version.supports_explicit_locations()
|| !emit_interpolation_and_auxiliary
{
if self.options.version.supports_io_locations() {
if second_blend_source {
write!(self.out, "layout(location = {location}, index = 1) ")?;
} else {
write!(self.out, "layout(location = {location}) ")?;
}
None
} else {
write!(self.out, "layout(location = {location}) ")?;
Some(VaryingLocation {
location,
index: second_blend_source as u32,
})
}
}
} else {
None
};

// Write the interpolation qualifier.
if let Some(interp) = interpolation {
Expand Down Expand Up @@ -1417,6 +1457,10 @@ impl<'a, W: Write> Writer<'a, W> {
};
writeln!(self.out, " {vname};")?;

if let Some(location) = io_location {
self.varying.insert(vname.to_string(), location);
}

Ok(())
}

Expand Down Expand Up @@ -4000,7 +4044,7 @@ impl<'a, W: Write> Writer<'a, W> {
}

/// Helper method used to produce the reflection info that's returned to the user
fn collect_reflection_info(&self) -> Result<ReflectionInfo, Error> {
fn collect_reflection_info(&mut self) -> Result<ReflectionInfo, Error> {
use std::collections::hash_map::Entry;
let info = self.info.get_entry_point(self.entry_point_idx as usize);
let mut texture_mapping = crate::FastHashMap::default();
Expand Down Expand Up @@ -4057,6 +4101,7 @@ impl<'a, W: Write> Writer<'a, W> {
Ok(ReflectionInfo {
texture_mapping,
uniforms,
varying: mem::take(&mut self.varying),
})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#version 430 core
#extension GL_ARB_shader_texture_image_samples : require
uniform highp sampler1D _group_0_binding_0_fs;
uniform sampler1D _group_0_binding_0_fs;

uniform highp sampler2D _group_0_binding_1_fs;
uniform sampler2D _group_0_binding_1_fs;

uniform highp sampler2DArray _group_0_binding_2_fs;
uniform sampler2DArray _group_0_binding_2_fs;

uniform highp sampler3D _group_0_binding_3_fs;
uniform sampler3D _group_0_binding_3_fs;

uniform highp sampler2DMS _group_0_binding_4_fs;
uniform sampler2DMS _group_0_binding_4_fs;

layout(rgba8) writeonly uniform highp image1D _group_0_binding_8_fs;
layout(rgba8) writeonly uniform image1D _group_0_binding_8_fs;

layout(rgba8) writeonly uniform highp image2D _group_0_binding_9_fs;
layout(rgba8) writeonly uniform image2D _group_0_binding_9_fs;

layout(rgba8) writeonly uniform highp image2DArray _group_0_binding_10_fs;
layout(rgba8) writeonly uniform image2DArray _group_0_binding_10_fs;

layout(rgba8) writeonly uniform highp image3D _group_0_binding_11_fs;
layout(rgba8) writeonly uniform image3D _group_0_binding_11_fs;

layout(location = 0) out vec4 _fs2p_location0;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#version 430 core
#extension GL_ARB_shader_texture_image_samples : require
uniform highp sampler1D _group_0_binding_0_fs;
uniform sampler1D _group_0_binding_0_fs;

uniform highp sampler2D _group_0_binding_1_fs;
uniform sampler2D _group_0_binding_1_fs;

uniform highp sampler2DArray _group_0_binding_2_fs;
uniform sampler2DArray _group_0_binding_2_fs;

uniform highp sampler3D _group_0_binding_3_fs;
uniform sampler3D _group_0_binding_3_fs;

uniform highp sampler2DMS _group_0_binding_4_fs;
uniform sampler2DMS _group_0_binding_4_fs;

layout(rgba8) writeonly uniform highp image1D _group_0_binding_8_fs;
layout(rgba8) writeonly uniform image1D _group_0_binding_8_fs;

layout(rgba8) writeonly uniform highp image2D _group_0_binding_9_fs;
layout(rgba8) writeonly uniform image2D _group_0_binding_9_fs;

layout(rgba8) writeonly uniform highp image2DArray _group_0_binding_10_fs;
layout(rgba8) writeonly uniform image2DArray _group_0_binding_10_fs;

layout(rgba8) writeonly uniform highp image3D _group_0_binding_11_fs;
layout(rgba8) writeonly uniform image3D _group_0_binding_11_fs;

layout(location = 0) out vec4 _fs2p_location0;

Expand Down

0 comments on commit d105909

Please sign in to comment.