@@ -55,6 +55,7 @@ use std::{
55
55
cmp:: Ordering ,
56
56
fmt,
57
57
fmt:: { Error as FmtError , Write } ,
58
+ mem,
58
59
} ;
59
60
use thiserror:: Error ;
60
61
@@ -64,7 +65,7 @@ mod features;
64
65
mod keywords;
65
66
66
67
/// 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 ] ;
68
69
/// List of supported `es` GLSL versions.
69
70
pub const SUPPORTED_ES_VERSIONS : & [ u16 ] = & [ 300 , 310 , 320 ] ;
70
71
@@ -163,6 +164,10 @@ impl Version {
163
164
}
164
165
}
165
166
167
+ fn supports_io_locations ( & self ) -> bool {
168
+ * self >= Version :: Desktop ( 330 ) || * self >= Version :: new_gles ( 300 )
169
+ }
170
+
166
171
/// Checks if the version supports all of the explicit layouts:
167
172
/// - `location=` qualifiers for bindings
168
173
/// - `binding=` qualifiers for resources
@@ -285,12 +290,25 @@ pub struct PipelineOptions {
285
290
pub multiview : Option < std:: num:: NonZeroU32 > ,
286
291
}
287
292
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
+
288
303
/// Reflection info for texture mappings and uniforms.
304
+ #[ derive( Debug ) ]
289
305
pub struct ReflectionInfo {
290
306
/// Mapping between texture names and variables/samplers.
291
307
pub texture_mapping : crate :: FastHashMap < String , TextureMapping > ,
292
308
/// Mapping between uniform variables and names.
293
309
pub uniforms : crate :: FastHashMap < Handle < crate :: GlobalVariable > , String > ,
310
+ /// Mapping between names and attribute locations.
311
+ pub varying : crate :: FastHashMap < String , VaryingLocation > ,
294
312
}
295
313
296
314
/// Mapping between a texture and its sampler, if it exists.
@@ -463,6 +481,8 @@ pub struct Writer<'a, W> {
463
481
need_bake_expressions : back:: NeedBakeExpressions ,
464
482
/// How many views to render to, if doing multiview rendering.
465
483
multiview : Option < std:: num:: NonZeroU32 > ,
484
+ /// Mapping of varying variables to their location. Needed for reflections.
485
+ varying : crate :: FastHashMap < String , VaryingLocation > ,
466
486
}
467
487
468
488
impl < ' a , W : Write > Writer < ' a , W > {
@@ -525,6 +545,7 @@ impl<'a, W: Write> Writer<'a, W> {
525
545
block_id : IdGenerator :: default ( ) ,
526
546
named_expressions : Default :: default ( ) ,
527
547
need_bake_expressions : Default :: default ( ) ,
548
+ varying : Default :: default ( ) ,
528
549
} ;
529
550
530
551
// Find all features required to print this module
@@ -1006,9 +1027,16 @@ impl<'a, W: Write> Writer<'a, W> {
1006
1027
Ic :: Storage { format, .. } => ( "image" , format. into ( ) , "" , "" ) ,
1007
1028
} ;
1008
1029
1030
+ let precision = if self . options . version . is_es ( ) {
1031
+ "highp "
1032
+ } else {
1033
+ ""
1034
+ } ;
1035
+
1009
1036
write ! (
1010
1037
self . out,
1011
- "highp {}{}{}{}{}{}" ,
1038
+ "{}{}{}{}{}{}{}" ,
1039
+ precision,
1012
1040
glsl_scalar( kind, 4 ) ?. prefix,
1013
1041
base,
1014
1042
glsl_dimension( dim) ,
@@ -1367,13 +1395,25 @@ impl<'a, W: Write> Writer<'a, W> {
1367
1395
} ;
1368
1396
1369
1397
// 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
1373
1408
} else {
1374
- write ! ( self . out, "layout(location = {location}) " ) ?;
1409
+ Some ( VaryingLocation {
1410
+ location,
1411
+ index : second_blend_source as u32 ,
1412
+ } )
1375
1413
}
1376
- }
1414
+ } else {
1415
+ None
1416
+ } ;
1377
1417
1378
1418
// Write the interpolation qualifier.
1379
1419
if let Some ( interp) = interpolation {
@@ -1417,6 +1457,10 @@ impl<'a, W: Write> Writer<'a, W> {
1417
1457
} ;
1418
1458
writeln ! ( self . out, " {vname};" ) ?;
1419
1459
1460
+ if let Some ( location) = io_location {
1461
+ self . varying . insert ( vname. to_string ( ) , location) ;
1462
+ }
1463
+
1420
1464
Ok ( ( ) )
1421
1465
}
1422
1466
@@ -4000,7 +4044,7 @@ impl<'a, W: Write> Writer<'a, W> {
4000
4044
}
4001
4045
4002
4046
/// 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 > {
4004
4048
use std:: collections:: hash_map:: Entry ;
4005
4049
let info = self . info . get_entry_point ( self . entry_point_idx as usize ) ;
4006
4050
let mut texture_mapping = crate :: FastHashMap :: default ( ) ;
@@ -4057,6 +4101,7 @@ impl<'a, W: Write> Writer<'a, W> {
4057
4101
Ok ( ReflectionInfo {
4058
4102
texture_mapping,
4059
4103
uniforms,
4104
+ varying : mem:: take ( & mut self . varying ) ,
4060
4105
} )
4061
4106
}
4062
4107
}
0 commit comments