Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit d105909

Browse files
authored
Don't use layout qualifiers to allow for GLSL 140 support (#2575)
1 parent 65f2216 commit d105909

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

src/back/glsl/mod.rs

+53-8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use std::{
5555
cmp::Ordering,
5656
fmt,
5757
fmt::{Error as FmtError, Write},
58+
mem,
5859
};
5960
use thiserror::Error;
6061

@@ -64,7 +65,7 @@ mod features;
6465
mod keywords;
6566

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

@@ -163,6 +164,10 @@ impl Version {
163164
}
164165
}
165166

167+
fn supports_io_locations(&self) -> bool {
168+
*self >= Version::Desktop(330) || *self >= Version::new_gles(300)
169+
}
170+
166171
/// Checks if the version supports all of the explicit layouts:
167172
/// - `location=` qualifiers for bindings
168173
/// - `binding=` qualifiers for resources
@@ -285,12 +290,25 @@ pub struct PipelineOptions {
285290
pub multiview: Option<std::num::NonZeroU32>,
286291
}
287292

293+
#[derive(Debug)]
294+
pub struct VaryingLocation {
295+
/// The location of the global.
296+
/// This corresponds to `layout(location = ..)` in GLSL.
297+
pub location: u32,
298+
/// The index which can be used for dual source blending.
299+
/// This corresponds to `layout(index = ..)` in GLSL.
300+
pub index: u32,
301+
}
302+
288303
/// Reflection info for texture mappings and uniforms.
304+
#[derive(Debug)]
289305
pub struct ReflectionInfo {
290306
/// Mapping between texture names and variables/samplers.
291307
pub texture_mapping: crate::FastHashMap<String, TextureMapping>,
292308
/// Mapping between uniform variables and names.
293309
pub uniforms: crate::FastHashMap<Handle<crate::GlobalVariable>, String>,
310+
/// Mapping between names and attribute locations.
311+
pub varying: crate::FastHashMap<String, VaryingLocation>,
294312
}
295313

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

468488
impl<'a, W: Write> Writer<'a, W> {
@@ -525,6 +545,7 @@ impl<'a, W: Write> Writer<'a, W> {
525545
block_id: IdGenerator::default(),
526546
named_expressions: Default::default(),
527547
need_bake_expressions: Default::default(),
548+
varying: Default::default(),
528549
};
529550

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

1030+
let precision = if self.options.version.is_es() {
1031+
"highp "
1032+
} else {
1033+
""
1034+
};
1035+
10091036
write!(
10101037
self.out,
1011-
"highp {}{}{}{}{}{}",
1038+
"{}{}{}{}{}{}{}",
1039+
precision,
10121040
glsl_scalar(kind, 4)?.prefix,
10131041
base,
10141042
glsl_dimension(dim),
@@ -1367,13 +1395,25 @@ impl<'a, W: Write> Writer<'a, W> {
13671395
};
13681396

13691397
// Write the I/O locations, if allowed
1370-
if self.options.version.supports_explicit_locations() || !emit_interpolation_and_auxiliary {
1371-
if second_blend_source {
1372-
write!(self.out, "layout(location = {location}, index = 1) ")?;
1398+
let io_location = if self.options.version.supports_explicit_locations()
1399+
|| !emit_interpolation_and_auxiliary
1400+
{
1401+
if self.options.version.supports_io_locations() {
1402+
if second_blend_source {
1403+
write!(self.out, "layout(location = {location}, index = 1) ")?;
1404+
} else {
1405+
write!(self.out, "layout(location = {location}) ")?;
1406+
}
1407+
None
13731408
} else {
1374-
write!(self.out, "layout(location = {location}) ")?;
1409+
Some(VaryingLocation {
1410+
location,
1411+
index: second_blend_source as u32,
1412+
})
13751413
}
1376-
}
1414+
} else {
1415+
None
1416+
};
13771417

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

1460+
if let Some(location) = io_location {
1461+
self.varying.insert(vname.to_string(), location);
1462+
}
1463+
14201464
Ok(())
14211465
}
14221466

@@ -4000,7 +4044,7 @@ impl<'a, W: Write> Writer<'a, W> {
40004044
}
40014045

40024046
/// Helper method used to produce the reflection info that's returned to the user
4003-
fn collect_reflection_info(&self) -> Result<ReflectionInfo, Error> {
4047+
fn collect_reflection_info(&mut self) -> Result<ReflectionInfo, Error> {
40044048
use std::collections::hash_map::Entry;
40054049
let info = self.info.get_entry_point(self.entry_point_idx as usize);
40064050
let mut texture_mapping = crate::FastHashMap::default();
@@ -4057,6 +4101,7 @@ impl<'a, W: Write> Writer<'a, W> {
40574101
Ok(ReflectionInfo {
40584102
texture_mapping,
40594103
uniforms,
4104+
varying: mem::take(&mut self.varying),
40604105
})
40614106
}
40624107
}

tests/out/glsl/bounds-check-image-restrict.fragment_shader.Fragment.glsl

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
#version 430 core
22
#extension GL_ARB_shader_texture_image_samples : require
3-
uniform highp sampler1D _group_0_binding_0_fs;
3+
uniform sampler1D _group_0_binding_0_fs;
44

5-
uniform highp sampler2D _group_0_binding_1_fs;
5+
uniform sampler2D _group_0_binding_1_fs;
66

7-
uniform highp sampler2DArray _group_0_binding_2_fs;
7+
uniform sampler2DArray _group_0_binding_2_fs;
88

9-
uniform highp sampler3D _group_0_binding_3_fs;
9+
uniform sampler3D _group_0_binding_3_fs;
1010

11-
uniform highp sampler2DMS _group_0_binding_4_fs;
11+
uniform sampler2DMS _group_0_binding_4_fs;
1212

13-
layout(rgba8) writeonly uniform highp image1D _group_0_binding_8_fs;
13+
layout(rgba8) writeonly uniform image1D _group_0_binding_8_fs;
1414

15-
layout(rgba8) writeonly uniform highp image2D _group_0_binding_9_fs;
15+
layout(rgba8) writeonly uniform image2D _group_0_binding_9_fs;
1616

17-
layout(rgba8) writeonly uniform highp image2DArray _group_0_binding_10_fs;
17+
layout(rgba8) writeonly uniform image2DArray _group_0_binding_10_fs;
1818

19-
layout(rgba8) writeonly uniform highp image3D _group_0_binding_11_fs;
19+
layout(rgba8) writeonly uniform image3D _group_0_binding_11_fs;
2020

2121
layout(location = 0) out vec4 _fs2p_location0;
2222

tests/out/glsl/bounds-check-image-rzsw.fragment_shader.Fragment.glsl

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
#version 430 core
22
#extension GL_ARB_shader_texture_image_samples : require
3-
uniform highp sampler1D _group_0_binding_0_fs;
3+
uniform sampler1D _group_0_binding_0_fs;
44

5-
uniform highp sampler2D _group_0_binding_1_fs;
5+
uniform sampler2D _group_0_binding_1_fs;
66

7-
uniform highp sampler2DArray _group_0_binding_2_fs;
7+
uniform sampler2DArray _group_0_binding_2_fs;
88

9-
uniform highp sampler3D _group_0_binding_3_fs;
9+
uniform sampler3D _group_0_binding_3_fs;
1010

11-
uniform highp sampler2DMS _group_0_binding_4_fs;
11+
uniform sampler2DMS _group_0_binding_4_fs;
1212

13-
layout(rgba8) writeonly uniform highp image1D _group_0_binding_8_fs;
13+
layout(rgba8) writeonly uniform image1D _group_0_binding_8_fs;
1414

15-
layout(rgba8) writeonly uniform highp image2D _group_0_binding_9_fs;
15+
layout(rgba8) writeonly uniform image2D _group_0_binding_9_fs;
1616

17-
layout(rgba8) writeonly uniform highp image2DArray _group_0_binding_10_fs;
17+
layout(rgba8) writeonly uniform image2DArray _group_0_binding_10_fs;
1818

19-
layout(rgba8) writeonly uniform highp image3D _group_0_binding_11_fs;
19+
layout(rgba8) writeonly uniform image3D _group_0_binding_11_fs;
2020

2121
layout(location = 0) out vec4 _fs2p_location0;
2222

0 commit comments

Comments
 (0)