| 1 |
use core::fmt::Write; |
| 2 |
|
| 3 |
use super::{BackendResult, Error, Version, Writer}; |
| 4 |
use crate::{ |
| 5 |
back::glsl::{Options, WriterFlags}, |
| 6 |
AddressSpace, Binding, Expression, Handle, ImageClass, ImageDimension, Interpolation, |
| 7 |
SampleLevel, Sampling, Scalar, ScalarKind, ShaderStage, StorageFormat, Type, TypeInner, |
| 8 |
}; |
| 9 |
|
| 10 |
bitflags::bitflags! { |
| 11 |
/// Structure used to encode additions to GLSL that aren't supported by all versions. |
| 12 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 13 |
pub struct Features: u32 { |
| 14 |
/// Buffer address space support. |
| 15 |
const BUFFER_STORAGE = 1; |
| 16 |
const ARRAY_OF_ARRAYS = 1 << 1; |
| 17 |
/// 8 byte floats. |
| 18 |
const DOUBLE_TYPE = 1 << 2; |
| 19 |
/// More image formats. |
| 20 |
const FULL_IMAGE_FORMATS = 1 << 3; |
| 21 |
const MULTISAMPLED_TEXTURES = 1 << 4; |
| 22 |
const MULTISAMPLED_TEXTURE_ARRAYS = 1 << 5; |
| 23 |
const CUBE_TEXTURES_ARRAY = 1 << 6; |
| 24 |
const COMPUTE_SHADER = 1 << 7; |
| 25 |
/// Image load and early depth tests. |
| 26 |
const IMAGE_LOAD_STORE = 1 << 8; |
| 27 |
const CONSERVATIVE_DEPTH = 1 << 9; |
| 28 |
/// Interpolation and auxiliary qualifiers. |
| 29 |
/// |
| 30 |
/// Perspective, Flat, and Centroid are available in all GLSL versions we support. |
| 31 |
const NOPERSPECTIVE_QUALIFIER = 1 << 11; |
| 32 |
const SAMPLE_QUALIFIER = 1 << 12; |
| 33 |
const CLIP_DISTANCE = 1 << 13; |
| 34 |
const CULL_DISTANCE = 1 << 14; |
| 35 |
/// Sample ID. |
| 36 |
const SAMPLE_VARIABLES = 1 << 15; |
| 37 |
/// Arrays with a dynamic length. |
| 38 |
const DYNAMIC_ARRAY_SIZE = 1 << 16; |
| 39 |
const MULTI_VIEW = 1 << 17; |
| 40 |
/// Texture samples query |
| 41 |
const TEXTURE_SAMPLES = 1 << 18; |
| 42 |
/// Texture levels query |
| 43 |
const TEXTURE_LEVELS = 1 << 19; |
| 44 |
/// Image size query |
| 45 |
const IMAGE_SIZE = 1 << 20; |
| 46 |
/// Dual source blending |
| 47 |
const DUAL_SOURCE_BLENDING = 1 << 21; |
| 48 |
/// Instance index |
| 49 |
/// |
| 50 |
/// We can always support this, either through the language or a polyfill |
| 51 |
const INSTANCE_INDEX = 1 << 22; |
| 52 |
/// Sample specific LODs of cube / array shadow textures |
| 53 |
const TEXTURE_SHADOW_LOD = 1 << 23; |
| 54 |
/// Subgroup operations |
| 55 |
const SUBGROUP_OPERATIONS = 1 << 24; |
| 56 |
/// Image atomics |
| 57 |
const TEXTURE_ATOMICS = 1 << 25; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/// Helper structure used to store the required [`Features`] needed to output a |
| 62 |
/// [`Module`](crate::Module) |
| 63 |
/// |
| 64 |
/// Provides helper methods to check for availability and writing required extensions |
| 65 |
pub struct FeaturesManager(Features); |
| 66 |
|
| 67 |
impl FeaturesManager { |
| 68 |
/// Creates a new [`FeaturesManager`] instance |
| 69 |
pub const fn new() -> Self { |
| 70 |
Self(Features::empty()) |
| 71 |
} |
| 72 |
|
| 73 |
/// Adds to the list of required [`Features`] |
| 74 |
pub fn request(&mut self, features: Features) { |
| 75 |
self.0 |= features |
| 76 |
} |
| 77 |
|
| 78 |
/// Checks if the list of features [`Features`] contains the specified [`Features`] |
| 79 |
pub fn contains(&mut self, features: Features) -> bool { |
| 80 |
self.0.contains(features) |
| 81 |
} |
| 82 |
|
| 83 |
/// Checks that all required [`Features`] are available for the specified |
| 84 |
/// [`Version`] otherwise returns an [`Error::MissingFeatures`]. |
| 85 |
pub fn check_availability(&self, version: Version) -> BackendResult { |
| 86 |
// Will store all the features that are unavailable |
| 87 |
let mut missing = Features::empty(); |
| 88 |
|
| 89 |
// Helper macro to check for feature availability |
| 90 |
macro_rules! check_feature { |
| 91 |
// Used when only core glsl supports the feature |
| 92 |
($feature:ident, $core:literal) => { |
| 93 |
if self.0.contains(Features::$feature) |
| 94 |
&& (version < Version::Desktop($core) || version.is_es()) |
| 95 |
{ |
| 96 |
missing |= Features::$feature; |
| 97 |
} |
| 98 |
}; |
| 99 |
// Used when both core and es support the feature |
| 100 |
($feature:ident, $core:literal, $es:literal) => { |
| 101 |
if self.0.contains(Features::$feature) |
| 102 |
&& (version < Version::Desktop($core) || version < Version::new_gles($es)) |
| 103 |
{ |
| 104 |
missing |= Features::$feature; |
| 105 |
} |
| 106 |
}; |
| 107 |
} |
| 108 |
|
| 109 |
check_feature!(COMPUTE_SHADER, 420, 310); |
| 110 |
check_feature!(BUFFER_STORAGE, 400, 310); |
| 111 |
check_feature!(DOUBLE_TYPE, 150); |
| 112 |
check_feature!(CUBE_TEXTURES_ARRAY, 130, 310); |
| 113 |
check_feature!(MULTISAMPLED_TEXTURES, 150, 300); |
| 114 |
check_feature!(MULTISAMPLED_TEXTURE_ARRAYS, 150, 310); |
| 115 |
check_feature!(ARRAY_OF_ARRAYS, 120, 310); |
| 116 |
check_feature!(IMAGE_LOAD_STORE, 130, 310); |
| 117 |
check_feature!(CONSERVATIVE_DEPTH, 130, 300); |
| 118 |
check_feature!(NOPERSPECTIVE_QUALIFIER, 130); |
| 119 |
check_feature!(SAMPLE_QUALIFIER, 400, 320); |
| 120 |
check_feature!(CLIP_DISTANCE, 130, 300 /* with extension */); |
| 121 |
check_feature!(CULL_DISTANCE, 450, 300 /* with extension */); |
| 122 |
check_feature!(SAMPLE_VARIABLES, 400, 300); |
| 123 |
check_feature!(DYNAMIC_ARRAY_SIZE, 430, 310); |
| 124 |
check_feature!(DUAL_SOURCE_BLENDING, 330, 300 /* with extension */); |
| 125 |
check_feature!(SUBGROUP_OPERATIONS, 430, 310); |
| 126 |
check_feature!(TEXTURE_ATOMICS, 420, 310); |
| 127 |
match version { |
| 128 |
Version::Embedded { is_webgl: true, .. } => check_feature!(MULTI_VIEW, 140, 300), |
| 129 |
_ => check_feature!(MULTI_VIEW, 140, 310), |
| 130 |
}; |
| 131 |
// Only available on glsl core, this means that opengl es can't query the number |
| 132 |
// of samples nor levels in a image and neither do bound checks on the sample nor |
| 133 |
// the level argument of texelFecth |
| 134 |
check_feature!(TEXTURE_SAMPLES, 150); |
| 135 |
check_feature!(TEXTURE_LEVELS, 130); |
| 136 |
check_feature!(IMAGE_SIZE, 430, 310); |
| 137 |
check_feature!(TEXTURE_SHADOW_LOD, 200, 300); |
| 138 |
|
| 139 |
// Return an error if there are missing features |
| 140 |
if missing.is_empty() { |
| 141 |
Ok(()) |
| 142 |
} else { |
| 143 |
Err(Error::MissingFeatures(missing)) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/// Helper method used to write all needed extensions |
| 148 |
/// |
| 149 |
/// # Notes |
| 150 |
/// This won't check for feature availability so it might output extensions that aren't even |
| 151 |
/// supported.[`check_availability`](Self::check_availability) will check feature availability |
| 152 |
pub fn write(&self, options: &Options, mut out: impl Write) -> BackendResult { |
| 153 |
if self.0.contains(Features::COMPUTE_SHADER) && !options.version.is_es() { |
| 154 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_compute_shader.txt |
| 155 |
writeln!(out, "#extension GL_ARB_compute_shader : require")?; |
| 156 |
} |
| 157 |
|
| 158 |
if self.0.contains(Features::BUFFER_STORAGE) && !options.version.is_es() { |
| 159 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shader_storage_buffer_object.txt |
| 160 |
writeln!( |
| 161 |
out, |
| 162 |
"#extension GL_ARB_shader_storage_buffer_object : require" |
| 163 |
)?; |
| 164 |
} |
| 165 |
|
| 166 |
if self.0.contains(Features::DOUBLE_TYPE) && options.version < Version::Desktop(400) { |
| 167 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_gpu_shader_fp64.txt |
| 168 |
writeln!(out, "#extension GL_ARB_gpu_shader_fp64 : require")?; |
| 169 |
} |
| 170 |
|
| 171 |
if self.0.contains(Features::CUBE_TEXTURES_ARRAY) { |
| 172 |
if options.version.is_es() { |
| 173 |
// https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_texture_cube_map_array.txt |
| 174 |
writeln!(out, "#extension GL_EXT_texture_cube_map_array : require")?; |
| 175 |
} else if options.version < Version::Desktop(400) { |
| 176 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_cube_map_array.txt |
| 177 |
writeln!(out, "#extension GL_ARB_texture_cube_map_array : require")?; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if self.0.contains(Features::MULTISAMPLED_TEXTURE_ARRAYS) && options.version.is_es() { |
| 182 |
// https://www.khronos.org/registry/OpenGL/extensions/OES/OES_texture_storage_multisample_2d_array.txt |
| 183 |
writeln!( |
| 184 |
out, |
| 185 |
"#extension GL_OES_texture_storage_multisample_2d_array : require" |
| 186 |
)?; |
| 187 |
} |
| 188 |
|
| 189 |
if self.0.contains(Features::ARRAY_OF_ARRAYS) && options.version < Version::Desktop(430) { |
| 190 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_arrays_of_arrays.txt |
| 191 |
writeln!(out, "#extension ARB_arrays_of_arrays : require")?; |
| 192 |
} |
| 193 |
|
| 194 |
if self.0.contains(Features::IMAGE_LOAD_STORE) { |
| 195 |
if self.0.contains(Features::FULL_IMAGE_FORMATS) && options.version.is_es() { |
| 196 |
// https://www.khronos.org/registry/OpenGL/extensions/NV/NV_image_formats.txt |
| 197 |
writeln!(out, "#extension GL_NV_image_formats : require")?; |
| 198 |
} |
| 199 |
|
| 200 |
if options.version < Version::Desktop(420) { |
| 201 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shader_image_load_store.txt |
| 202 |
writeln!(out, "#extension GL_ARB_shader_image_load_store : require")?; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if self.0.contains(Features::CONSERVATIVE_DEPTH) { |
| 207 |
if options.version.is_es() { |
| 208 |
// https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_conservative_depth.txt |
| 209 |
writeln!(out, "#extension GL_EXT_conservative_depth : require")?; |
| 210 |
} |
| 211 |
|
| 212 |
if options.version < Version::Desktop(420) { |
| 213 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_conservative_depth.txt |
| 214 |
writeln!(out, "#extension GL_ARB_conservative_depth : require")?; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if (self.0.contains(Features::CLIP_DISTANCE) || self.0.contains(Features::CULL_DISTANCE)) |
| 219 |
&& options.version.is_es() |
| 220 |
{ |
| 221 |
// https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_clip_cull_distance.txt |
| 222 |
writeln!(out, "#extension GL_EXT_clip_cull_distance : require")?; |
| 223 |
} |
| 224 |
|
| 225 |
if self.0.contains(Features::SAMPLE_VARIABLES) && options.version.is_es() { |
| 226 |
// https://www.khronos.org/registry/OpenGL/extensions/OES/OES_sample_variables.txt |
| 227 |
writeln!(out, "#extension GL_OES_sample_variables : require")?; |
| 228 |
} |
| 229 |
|
| 230 |
if self.0.contains(Features::MULTI_VIEW) { |
| 231 |
if let Version::Embedded { is_webgl: true, .. } = options.version { |
| 232 |
// https://www.khronos.org/registry/OpenGL/extensions/OVR/OVR_multiview2.txt |
| 233 |
writeln!(out, "#extension GL_OVR_multiview2 : require")?; |
| 234 |
} else { |
| 235 |
// https://github.com/KhronosGroup/GLSL/blob/master/extensions/ext/GL_EXT_multiview.txt |
| 236 |
writeln!(out, "#extension GL_EXT_multiview : require")?; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if self.0.contains(Features::TEXTURE_SAMPLES) { |
| 241 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_shader_texture_image_samples.txt |
| 242 |
writeln!( |
| 243 |
out, |
| 244 |
"#extension GL_ARB_shader_texture_image_samples : require" |
| 245 |
)?; |
| 246 |
} |
| 247 |
|
| 248 |
if self.0.contains(Features::TEXTURE_LEVELS) && options.version < Version::Desktop(430) { |
| 249 |
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_query_levels.txt |
| 250 |
writeln!(out, "#extension GL_ARB_texture_query_levels : require")?; |
| 251 |
} |
| 252 |
if self.0.contains(Features::DUAL_SOURCE_BLENDING) && options.version.is_es() { |
| 253 |
// https://registry.khronos.org/OpenGL/extensions/EXT/EXT_blend_func_extended.txt |
| 254 |
writeln!(out, "#extension GL_EXT_blend_func_extended : require")?; |
| 255 |
} |
| 256 |
|
| 257 |
if self.0.contains(Features::INSTANCE_INDEX) { |
| 258 |
if options.writer_flags.contains(WriterFlags::DRAW_PARAMETERS) { |
| 259 |
// https://registry.khronos.org/OpenGL/extensions/ARB/ARB_shader_draw_parameters.txt |
| 260 |
writeln!(out, "#extension GL_ARB_shader_draw_parameters : require")?; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
if self.0.contains(Features::TEXTURE_SHADOW_LOD) { |
| 265 |
// https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_shadow_lod.txt |
| 266 |
writeln!(out, "#extension GL_EXT_texture_shadow_lod : require")?; |
| 267 |
} |
| 268 |
|
| 269 |
if self.0.contains(Features::SUBGROUP_OPERATIONS) { |
| 270 |
// https://registry.khronos.org/OpenGL/extensions/KHR/KHR_shader_subgroup.txt |
| 271 |
writeln!(out, "#extension GL_KHR_shader_subgroup_basic : require")?; |
| 272 |
writeln!(out, "#extension GL_KHR_shader_subgroup_vote : require")?; |
| 273 |
writeln!( |
| 274 |
out, |
| 275 |
"#extension GL_KHR_shader_subgroup_arithmetic : require" |
| 276 |
)?; |
| 277 |
writeln!(out, "#extension GL_KHR_shader_subgroup_ballot : require")?; |
| 278 |
writeln!(out, "#extension GL_KHR_shader_subgroup_shuffle : require")?; |
| 279 |
writeln!( |
| 280 |
out, |
| 281 |
"#extension GL_KHR_shader_subgroup_shuffle_relative : require" |
| 282 |
)?; |
| 283 |
} |
| 284 |
|
| 285 |
if self.0.contains(Features::TEXTURE_ATOMICS) { |
| 286 |
// https://www.khronos.org/registry/OpenGL/extensions/OES/OES_shader_image_atomic.txt |
| 287 |
writeln!(out, "#extension GL_OES_shader_image_atomic : require")?; |
| 288 |
} |
| 289 |
|
| 290 |
Ok(()) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
impl<W> Writer<'_, W> { |
| 295 |
/// Helper method that searches the module for all the needed [`Features`] |
| 296 |
/// |
| 297 |
/// # Errors |
| 298 |
/// If the version doesn't support any of the needed [`Features`] a |
| 299 |
/// [`Error::MissingFeatures`] will be returned |
| 300 |
pub(super) fn collect_required_features(&mut self) -> BackendResult { |
| 301 |
let ep_info = self.info.get_entry_point(self.entry_point_idx as usize); |
| 302 |
|
| 303 |
if let Some(depth_test) = self.entry_point.early_depth_test { |
| 304 |
// If IMAGE_LOAD_STORE is supported for this version of GLSL |
| 305 |
if self.options.version.supports_early_depth_test() { |
| 306 |
self.features.request(Features::IMAGE_LOAD_STORE); |
| 307 |
} |
| 308 |
|
| 309 |
if depth_test.conservative.is_some() { |
| 310 |
self.features.request(Features::CONSERVATIVE_DEPTH); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
for arg in self.entry_point.function.arguments.iter() { |
| 315 |
self.varying_required_features(arg.binding.as_ref(), arg.ty); |
| 316 |
} |
| 317 |
if let Some(ref result) = self.entry_point.function.result { |
| 318 |
self.varying_required_features(result.binding.as_ref(), result.ty); |
| 319 |
} |
| 320 |
|
| 321 |
if let ShaderStage::Compute = self.entry_point.stage { |
| 322 |
self.features.request(Features::COMPUTE_SHADER) |
| 323 |
} |
| 324 |
|
| 325 |
if self.multiview.is_some() { |
| 326 |
self.features.request(Features::MULTI_VIEW); |
| 327 |
} |
| 328 |
|
| 329 |
for (ty_handle, ty) in self.module.types.iter() { |
| 330 |
match ty.inner { |
| 331 |
TypeInner::Scalar(scalar) |
| 332 |
| TypeInner::Vector { scalar, .. } |
| 333 |
| TypeInner::Matrix { scalar, .. } => self.scalar_required_features(scalar), |
| 334 |
TypeInner::Array { base, size, .. } => { |
| 335 |
if let TypeInner::Array { .. } = self.module.types[base].inner { |
| 336 |
self.features.request(Features::ARRAY_OF_ARRAYS) |
| 337 |
} |
| 338 |
|
| 339 |
// If the array is dynamically sized |
| 340 |
if size == crate::ArraySize::Dynamic { |
| 341 |
let mut is_used = false; |
| 342 |
|
| 343 |
// Check if this type is used in a global that is needed by the current entrypoint |
| 344 |
for (global_handle, global) in self.module.global_variables.iter() { |
| 345 |
// Skip unused globals |
| 346 |
if ep_info[global_handle].is_empty() { |
| 347 |
continue; |
| 348 |
} |
| 349 |
|
| 350 |
// If this array is the type of a global, then this array is used |
| 351 |
if global.ty == ty_handle { |
| 352 |
is_used = true; |
| 353 |
break; |
| 354 |
} |
| 355 |
|
| 356 |
// If the type of this global is a struct |
| 357 |
if let TypeInner::Struct { ref members, .. } = |
| 358 |
self.module.types[global.ty].inner |
| 359 |
{ |
| 360 |
// Check the last element of the struct to see if it's type uses |
| 361 |
// this array |
| 362 |
if let Some(last) = members.last() { |
| 363 |
if last.ty == ty_handle { |
| 364 |
is_used = true; |
| 365 |
break; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
// If this dynamically size array is used, we need dynamic array size support |
| 372 |
if is_used { |
| 373 |
self.features.request(Features::DYNAMIC_ARRAY_SIZE); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
TypeInner::Image { |
| 378 |
dim, |
| 379 |
arrayed, |
| 380 |
class, |
| 381 |
} => { |
| 382 |
if arrayed && dim == ImageDimension::Cube { |
| 383 |
self.features.request(Features::CUBE_TEXTURES_ARRAY) |
| 384 |
} |
| 385 |
|
| 386 |
match class { |
| 387 |
ImageClass::Sampled { multi: true, .. } |
| 388 |
| ImageClass::Depth { multi: true } => { |
| 389 |
self.features.request(Features::MULTISAMPLED_TEXTURES); |
| 390 |
if arrayed { |
| 391 |
self.features.request(Features::MULTISAMPLED_TEXTURE_ARRAYS); |
| 392 |
} |
| 393 |
} |
| 394 |
ImageClass::Storage { format, .. } => match format { |
| 395 |
StorageFormat::R8Unorm |
| 396 |
| StorageFormat::R8Snorm |
| 397 |
| StorageFormat::R8Uint |
| 398 |
| StorageFormat::R8Sint |
| 399 |
| StorageFormat::R16Uint |
| 400 |
| StorageFormat::R16Sint |
| 401 |
| StorageFormat::R16Float |
| 402 |
| StorageFormat::Rg8Unorm |
| 403 |
| StorageFormat::Rg8Snorm |
| 404 |
| StorageFormat::Rg8Uint |
| 405 |
| StorageFormat::Rg8Sint |
| 406 |
| StorageFormat::Rg16Uint |
| 407 |
| StorageFormat::Rg16Sint |
| 408 |
| StorageFormat::Rg16Float |
| 409 |
| StorageFormat::Rgb10a2Uint |
| 410 |
| StorageFormat::Rgb10a2Unorm |
| 411 |
| StorageFormat::Rg11b10Ufloat |
| 412 |
| StorageFormat::R64Uint |
| 413 |
| StorageFormat::Rg32Uint |
| 414 |
| StorageFormat::Rg32Sint |
| 415 |
| StorageFormat::Rg32Float => { |
| 416 |
self.features.request(Features::FULL_IMAGE_FORMATS) |
| 417 |
} |
| 418 |
_ => {} |
| 419 |
}, |
| 420 |
ImageClass::Sampled { multi: false, .. } |
| 421 |
| ImageClass::Depth { multi: false } => {} |
| 422 |
} |
| 423 |
} |
| 424 |
_ => {} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
let mut push_constant_used = false; |
| 429 |
|
| 430 |
for (handle, global) in self.module.global_variables.iter() { |
| 431 |
if ep_info[handle].is_empty() { |
| 432 |
continue; |
| 433 |
} |
| 434 |
match global.space { |
| 435 |
AddressSpace::WorkGroup => self.features.request(Features::COMPUTE_SHADER), |
| 436 |
AddressSpace::Storage { .. } => self.features.request(Features::BUFFER_STORAGE), |
| 437 |
AddressSpace::PushConstant => { |
| 438 |
if push_constant_used { |
| 439 |
return Err(Error::MultiplePushConstants); |
| 440 |
} |
| 441 |
push_constant_used = true; |
| 442 |
} |
| 443 |
_ => {} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
// We will need to pass some of the members to a closure, so we need |
| 448 |
// to separate them otherwise the borrow checker will complain, this |
| 449 |
// shouldn't be needed in rust 2021 |
| 450 |
let &mut Self { |
| 451 |
module, |
| 452 |
info, |
| 453 |
ref mut features, |
| 454 |
entry_point, |
| 455 |
entry_point_idx, |
| 456 |
ref policies, |
| 457 |
.. |
| 458 |
} = self; |
| 459 |
|
| 460 |
// Loop through all expressions in both functions and the entry point |
| 461 |
// to check for needed features |
| 462 |
for (expressions, info) in module |
| 463 |
.functions |
| 464 |
.iter() |
| 465 |
.map(|(h, f)| (&f.expressions, &info[h])) |
| 466 |
.chain(core::iter::once(( |
| 467 |
&entry_point.function.expressions, |
| 468 |
info.get_entry_point(entry_point_idx as usize), |
| 469 |
))) |
| 470 |
{ |
| 471 |
for (_, expr) in expressions.iter() { |
| 472 |
match *expr { |
| 473 |
// Check for queries that need aditonal features |
| 474 |
Expression::ImageQuery { |
| 475 |
image, |
| 476 |
query, |
| 477 |
.. |
| 478 |
} => match query { |
| 479 |
// Storage images use `imageSize` which is only available |
| 480 |
// in glsl > 420 |
| 481 |
// |
| 482 |
// layers queries are also implemented as size queries |
| 483 |
crate::ImageQuery::Size { .. } | crate::ImageQuery::NumLayers => { |
| 484 |
if let TypeInner::Image { |
| 485 |
class: ImageClass::Storage { .. }, .. |
| 486 |
} = *info[image].ty.inner_with(&module.types) { |
| 487 |
features.request(Features::IMAGE_SIZE) |
| 488 |
} |
| 489 |
}, |
| 490 |
crate::ImageQuery::NumLevels => features.request(Features::TEXTURE_LEVELS), |
| 491 |
crate::ImageQuery::NumSamples => features.request(Features::TEXTURE_SAMPLES), |
| 492 |
} |
| 493 |
, |
| 494 |
// Check for image loads that needs bound checking on the sample |
| 495 |
// or level argument since this requires a feature |
| 496 |
Expression::ImageLoad { |
| 497 |
sample, level, .. |
| 498 |
} => { |
| 499 |
if policies.image_load != crate::proc::BoundsCheckPolicy::Unchecked { |
| 500 |
if sample.is_some() { |
| 501 |
features.request(Features::TEXTURE_SAMPLES) |
| 502 |
} |
| 503 |
|
| 504 |
if level.is_some() { |
| 505 |
features.request(Features::TEXTURE_LEVELS) |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
Expression::ImageSample { image, level, offset, .. } => { |
| 510 |
if let TypeInner::Image { |
| 511 |
dim, |
| 512 |
arrayed, |
| 513 |
class: ImageClass::Depth { .. }, |
| 514 |
} = *info[image].ty.inner_with(&module.types) { |
| 515 |
let lod = matches!(level, SampleLevel::Zero | SampleLevel::Exact(_)); |
| 516 |
let bias = matches!(level, SampleLevel::Bias(_)); |
| 517 |
let auto = matches!(level, SampleLevel::Auto); |
| 518 |
let cube = dim == ImageDimension::Cube; |
| 519 |
let array2d = dim == ImageDimension::D2 && arrayed; |
| 520 |
let gles = self.options.version.is_es(); |
| 521 |
|
| 522 |
// We have a workaround of using `textureGrad` instead of `textureLod` if the LOD is zero, |
| 523 |
// so we don't *need* this extension for those cases. |
| 524 |
// But if we're explicitly allowed to use the extension (`WriterFlags::TEXTURE_SHADOW_LOD`), |
| 525 |
// we always use it instead of the workaround. |
| 526 |
let grad_workaround_applicable = (array2d || (cube && !arrayed)) && level == SampleLevel::Zero; |
| 527 |
let prefer_grad_workaround = grad_workaround_applicable && !self.options.writer_flags.contains(WriterFlags::TEXTURE_SHADOW_LOD); |
| 528 |
|
| 529 |
let mut ext_used = false; |
| 530 |
|
| 531 |
// float texture(sampler2DArrayShadow sampler, vec4 P [, float bias]) |
| 532 |
// float texture(samplerCubeArrayShadow sampler, vec4 P, float compare [, float bias]) |
| 533 |
ext_used |= (array2d || cube && arrayed) && bias; |
| 534 |
|
| 535 |
// The non `bias` version of this was standardized in GL 4.3, but never in GLES. |
| 536 |
// float textureOffset(sampler2DArrayShadow sampler, vec4 P, ivec2 offset [, float bias]) |
| 537 |
ext_used |= array2d && (bias || (gles && auto)) && offset.is_some(); |
| 538 |
|
| 539 |
// float textureLod(sampler2DArrayShadow sampler, vec4 P, float lod) |
| 540 |
// float textureLodOffset(sampler2DArrayShadow sampler, vec4 P, float lod, ivec2 offset) |
| 541 |
// float textureLod(samplerCubeShadow sampler, vec4 P, float lod) |
| 542 |
// float textureLod(samplerCubeArrayShadow sampler, vec4 P, float compare, float lod) |
| 543 |
ext_used |= (cube || array2d) && lod && !prefer_grad_workaround; |
| 544 |
|
| 545 |
if ext_used { |
| 546 |
features.request(Features::TEXTURE_SHADOW_LOD); |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
Expression::SubgroupBallotResult | |
| 551 |
Expression::SubgroupOperationResult { .. } => { |
| 552 |
features.request(Features::SUBGROUP_OPERATIONS) |
| 553 |
} |
| 554 |
_ => {} |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
for blocks in module |
| 560 |
.functions |
| 561 |
.iter() |
| 562 |
.map(|(_, f)| &f.body) |
| 563 |
.chain(core::iter::once(&entry_point.function.body)) |
| 564 |
{ |
| 565 |
for (stmt, _) in blocks.span_iter() { |
| 566 |
match *stmt { |
| 567 |
crate::Statement::ImageAtomic { .. } => { |
| 568 |
features.request(Features::TEXTURE_ATOMICS) |
| 569 |
} |
| 570 |
_ => {} |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
self.features.check_availability(self.options.version) |
| 576 |
} |
| 577 |
|
| 578 |
/// Helper method that checks the [`Features`] needed by a scalar |
| 579 |
fn scalar_required_features(&mut self, scalar: Scalar) { |
| 580 |
if scalar.kind == ScalarKind::Float && scalar.width == 8 { |
| 581 |
self.features.request(Features::DOUBLE_TYPE); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
fn varying_required_features(&mut self, binding: Option<&Binding>, ty: Handle<Type>) { |
| 586 |
if let TypeInner::Struct { ref members, .. } = self.module.types[ty].inner { |
| 587 |
for member in members { |
| 588 |
self.varying_required_features(member.binding.as_ref(), member.ty); |
| 589 |
} |
| 590 |
} else if let Some(binding) = binding { |
| 591 |
match *binding { |
| 592 |
Binding::BuiltIn(built_in) => match built_in { |
| 593 |
crate::BuiltIn::ClipDistance => self.features.request(Features::CLIP_DISTANCE), |
| 594 |
crate::BuiltIn::CullDistance => self.features.request(Features::CULL_DISTANCE), |
| 595 |
crate::BuiltIn::SampleIndex => { |
| 596 |
self.features.request(Features::SAMPLE_VARIABLES) |
| 597 |
} |
| 598 |
crate::BuiltIn::ViewIndex => self.features.request(Features::MULTI_VIEW), |
| 599 |
crate::BuiltIn::InstanceIndex | crate::BuiltIn::DrawID => { |
| 600 |
self.features.request(Features::INSTANCE_INDEX) |
| 601 |
} |
| 602 |
_ => {} |
| 603 |
}, |
| 604 |
Binding::Location { |
| 605 |
location: _, |
| 606 |
interpolation, |
| 607 |
sampling, |
| 608 |
blend_src, |
| 609 |
} => { |
| 610 |
if interpolation == Some(Interpolation::Linear) { |
| 611 |
self.features.request(Features::NOPERSPECTIVE_QUALIFIER); |
| 612 |
} |
| 613 |
if sampling == Some(Sampling::Sample) { |
| 614 |
self.features.request(Features::SAMPLE_QUALIFIER); |
| 615 |
} |
| 616 |
if blend_src.is_some() { |
| 617 |
self.features.request(Features::DUAL_SOURCE_BLENDING); |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
} |