mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-23 07:51:08 +01:00
[wgsl] update texture definitions and storage access
Add more tests, enhance the snapshot testing.
This commit is contained in:
1 parent
dafca34877
commit
0efd6e75d7
10 files changed
+231
-158
No files matched your search
@@ -222,22 +222,6 @@ impl<'a> Lexer<'a> {
|
||||
Ok(format)
|
||||
}
|
||||
|
||||
pub(super) fn take_until(&mut self, what: Token<'_>) -> Result<Lexer<'a>, Error<'a>> {
|
||||
let original_input = self.input;
|
||||
let initial_len = self.input.len();
|
||||
let mut used_len = 0;
|
||||
loop {
|
||||
if self.next() == what {
|
||||
break;
|
||||
}
|
||||
used_len = initial_len - self.input.len();
|
||||
}
|
||||
|
||||
Ok(Lexer {
|
||||
input: &original_input[..used_len],
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn offset_from(&self, source: &'a str) -> usize {
|
||||
source.len() - self.input.len()
|
||||
}
|
||||
|
||||
+85
-116
@@ -14,6 +14,7 @@ use crate::{
|
||||
};
|
||||
|
||||
use self::lexer::Lexer;
|
||||
use std::num::NonZeroU32;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
@@ -63,6 +64,8 @@ pub enum Error<'a> {
|
||||
UnknownScalarKind(&'a str),
|
||||
#[error("unknown builtin: `{0}`")]
|
||||
UnknownBuiltin(&'a str),
|
||||
#[error("unknown access: `{0}`")]
|
||||
UnknownAccess(&'a str),
|
||||
#[error("unknown shader stage: `{0}`")]
|
||||
UnknownShaderStage(&'a str),
|
||||
#[error("unknown identifier: `{0}`")]
|
||||
@@ -251,6 +254,12 @@ impl Composition {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct TypeDecoration {
|
||||
stride: Option<NonZeroU32>,
|
||||
access: crate::StorageAccess,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Scope {
|
||||
Decoration,
|
||||
@@ -531,7 +540,8 @@ impl Parser {
|
||||
inner
|
||||
}
|
||||
_ => {
|
||||
let composite_ty = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
let (composite_ty, _access) =
|
||||
self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
lexer.expect(Token::Paren('('))?;
|
||||
let mut components = Vec::new();
|
||||
while !lexer.skip(Token::Paren(')')) {
|
||||
@@ -625,8 +635,13 @@ impl Parser {
|
||||
if let Some(expr) = self.parse_function_call_inner(lexer, word, ctx.reborrow())? {
|
||||
expr
|
||||
} else {
|
||||
let inner =
|
||||
self.parse_type_decl_impl(lexer, None, word, ctx.types, ctx.constants)?;
|
||||
let inner = self.parse_type_decl_impl(
|
||||
lexer,
|
||||
TypeDecoration::default(),
|
||||
word,
|
||||
ctx.types,
|
||||
ctx.constants,
|
||||
)?;
|
||||
let ty = ctx.types.fetch_or_append(crate::Type { name: None, inner });
|
||||
|
||||
lexer.expect(Token::Paren('('))?;
|
||||
@@ -903,11 +918,11 @@ impl Parser {
|
||||
lexer: &mut Lexer<'a>,
|
||||
type_arena: &mut Arena<crate::Type>,
|
||||
const_arena: &mut Arena<crate::Constant>,
|
||||
) -> Result<(&'a str, Handle<crate::Type>), Error<'a>> {
|
||||
) -> Result<(&'a str, Handle<crate::Type>, crate::StorageAccess), Error<'a>> {
|
||||
let name = lexer.next_ident()?;
|
||||
lexer.expect(Token::Separator(':'))?;
|
||||
let ty = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
Ok((name, ty))
|
||||
let (ty, access) = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
Ok((name, ty, access))
|
||||
}
|
||||
|
||||
fn parse_variable_decl<'a>(
|
||||
@@ -925,21 +940,8 @@ impl Parser {
|
||||
}
|
||||
let name = lexer.next_ident()?;
|
||||
lexer.expect(Token::Separator(':'))?;
|
||||
let ty = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
let access = match class {
|
||||
Some(crate::StorageClass::Storage) => crate::StorageAccess::all(),
|
||||
Some(crate::StorageClass::Handle) => {
|
||||
match type_arena[ty].inner {
|
||||
//TODO: RW textures
|
||||
crate::TypeInner::Image {
|
||||
class: crate::ImageClass::Storage(_),
|
||||
..
|
||||
} => crate::StorageAccess::LOAD,
|
||||
_ => crate::StorageAccess::empty(),
|
||||
}
|
||||
}
|
||||
_ => crate::StorageAccess::empty(),
|
||||
};
|
||||
let (ty, access) = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
|
||||
let init = if lexer.skip(Token::Operation('=')) {
|
||||
let handle = self.parse_const_expression(lexer, ty, type_arena, const_arena)?;
|
||||
Some(handle)
|
||||
@@ -998,7 +1000,7 @@ impl Parser {
|
||||
return Err(Error::MissingMemberOffset(name));
|
||||
}
|
||||
lexer.expect(Token::Separator(':'))?;
|
||||
let ty = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
let (ty, _access) = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
lexer.expect(Token::Separator(';'))?;
|
||||
members.push(crate::StructMember {
|
||||
name: Some(name.to_owned()),
|
||||
@@ -1011,7 +1013,7 @@ impl Parser {
|
||||
fn parse_type_decl_impl<'a>(
|
||||
&mut self,
|
||||
lexer: &mut Lexer<'a>,
|
||||
decoration_lexer: Option<Lexer<'a>>,
|
||||
decoration: TypeDecoration,
|
||||
word: &'a str,
|
||||
type_arena: &mut Arena<crate::Type>,
|
||||
const_arena: &mut Arena<crate::Constant>,
|
||||
@@ -1129,13 +1131,13 @@ impl Parser {
|
||||
lexer.expect(Token::Paren('<'))?;
|
||||
let class = conv::map_storage_class(lexer.next_ident()?)?;
|
||||
lexer.expect(Token::Separator(','))?;
|
||||
let base = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
let (base, _access) = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
lexer.expect(Token::Paren('>'))?;
|
||||
crate::TypeInner::Pointer { base, class }
|
||||
}
|
||||
"array" => {
|
||||
lexer.expect(Token::Paren('<'))?;
|
||||
let base = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
let (base, _access) = self.parse_type_decl(lexer, None, type_arena, const_arena)?;
|
||||
let size = match lexer.next() {
|
||||
Token::Separator(',') => {
|
||||
let value = lexer.next_uint_literal()?;
|
||||
@@ -1158,30 +1160,15 @@ impl Parser {
|
||||
other => return other.unexpected("generic separator"),
|
||||
};
|
||||
|
||||
let mut stride = None;
|
||||
if let Some(mut lexer) = decoration_lexer {
|
||||
self.scopes.push(Scope::Decoration);
|
||||
loop {
|
||||
match lexer.next() {
|
||||
Token::Word("stride") => {
|
||||
use std::num::NonZeroU32;
|
||||
stride = Some(
|
||||
NonZeroU32::new(lexer.next_uint_literal()?)
|
||||
.ok_or(Error::ZeroStride)?,
|
||||
);
|
||||
}
|
||||
Token::End => break,
|
||||
other => return other.unexpected("stride decoration"),
|
||||
}
|
||||
}
|
||||
self.scopes.pop();
|
||||
crate::TypeInner::Array {
|
||||
base,
|
||||
size,
|
||||
stride: decoration.stride,
|
||||
}
|
||||
|
||||
crate::TypeInner::Array { base, size, stride }
|
||||
}
|
||||
"sampler" => crate::TypeInner::Sampler { comparison: false },
|
||||
"sampler_comparison" => crate::TypeInner::Sampler { comparison: true },
|
||||
"texture_sampled_1d" => {
|
||||
"texture_1d" => {
|
||||
let (kind, _) = lexer.next_scalar_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D1,
|
||||
@@ -1189,7 +1176,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Sampled { kind, multi: false },
|
||||
}
|
||||
}
|
||||
"texture_sampled_1d_array" => {
|
||||
"texture_1d_array" => {
|
||||
let (kind, _) = lexer.next_scalar_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D1,
|
||||
@@ -1197,7 +1184,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Sampled { kind, multi: false },
|
||||
}
|
||||
}
|
||||
"texture_sampled_2d" => {
|
||||
"texture_2d" => {
|
||||
let (kind, _) = lexer.next_scalar_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D2,
|
||||
@@ -1205,7 +1192,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Sampled { kind, multi: false },
|
||||
}
|
||||
}
|
||||
"texture_sampled_2d_array" => {
|
||||
"texture_2d_array" => {
|
||||
let (kind, _) = lexer.next_scalar_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D2,
|
||||
@@ -1213,7 +1200,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Sampled { kind, multi: false },
|
||||
}
|
||||
}
|
||||
"texture_sampled_3d" => {
|
||||
"texture_3d" => {
|
||||
let (kind, _) = lexer.next_scalar_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D3,
|
||||
@@ -1221,7 +1208,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Sampled { kind, multi: false },
|
||||
}
|
||||
}
|
||||
"texture_sampled_cube" => {
|
||||
"texture_cube" => {
|
||||
let (kind, _) = lexer.next_scalar_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::Cube,
|
||||
@@ -1229,7 +1216,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Sampled { kind, multi: false },
|
||||
}
|
||||
}
|
||||
"texture_sampled_cube_array" => {
|
||||
"texture_cube_array" => {
|
||||
let (kind, _) = lexer.next_scalar_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::Cube,
|
||||
@@ -1265,7 +1252,7 @@ impl Parser {
|
||||
arrayed: true,
|
||||
class: crate::ImageClass::Depth,
|
||||
},
|
||||
"texture_ro_1d" => {
|
||||
"texture_storage_1d" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D1,
|
||||
@@ -1273,7 +1260,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_ro_1d_array" => {
|
||||
"texture_storage_1d_array" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D1,
|
||||
@@ -1281,7 +1268,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_ro_2d" => {
|
||||
"texture_storage_2d" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D2,
|
||||
@@ -1289,7 +1276,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_ro_2d_array" => {
|
||||
"texture_storage_2d_array" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D2,
|
||||
@@ -1297,47 +1284,7 @@ impl Parser {
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_ro_3d" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D3,
|
||||
arrayed: false,
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_wo_1d" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D1,
|
||||
arrayed: false,
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_wo_1d_array" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D1,
|
||||
arrayed: true,
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_wo_2d" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D2,
|
||||
arrayed: false,
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_wo_2d_array" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D2,
|
||||
arrayed: true,
|
||||
class: crate::ImageClass::Storage(format),
|
||||
}
|
||||
}
|
||||
"texture_wo_3d" => {
|
||||
"texture_storage_3d" => {
|
||||
let format = lexer.next_format_generic()?;
|
||||
crate::TypeInner::Image {
|
||||
dim: crate::ImageDimension::D3,
|
||||
@@ -1355,25 +1302,45 @@ impl Parser {
|
||||
self_name: Option<&'a str>,
|
||||
type_arena: &mut Arena<crate::Type>,
|
||||
const_arena: &mut Arena<crate::Constant>,
|
||||
) -> Result<Handle<crate::Type>, Error<'a>> {
|
||||
) -> Result<(Handle<crate::Type>, crate::StorageAccess), Error<'a>> {
|
||||
self.scopes.push(Scope::TypeDecl);
|
||||
let decoration_lexer = if lexer.skip(Token::DoubleParen('[')) {
|
||||
Some(lexer.take_until(Token::DoubleParen(']'))?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut decoration = TypeDecoration::default();
|
||||
|
||||
if lexer.skip(Token::DoubleParen('[')) {
|
||||
self.scopes.push(Scope::Decoration);
|
||||
loop {
|
||||
match lexer.next() {
|
||||
Token::Word("access") => {
|
||||
lexer.expect(Token::Paren('('))?;
|
||||
decoration.access = match lexer.next_ident()? {
|
||||
"read" => crate::StorageAccess::LOAD,
|
||||
"write" => crate::StorageAccess::STORE,
|
||||
"read_write" => crate::StorageAccess::all(),
|
||||
other => return Err(Error::UnknownAccess(other)),
|
||||
};
|
||||
lexer.expect(Token::Paren(')'))?;
|
||||
}
|
||||
Token::Word("stride") => {
|
||||
lexer.expect(Token::Paren('('))?;
|
||||
decoration.stride = Some(
|
||||
NonZeroU32::new(lexer.next_uint_literal()?).ok_or(Error::ZeroStride)?,
|
||||
);
|
||||
lexer.expect(Token::Paren(')'))?;
|
||||
}
|
||||
Token::DoubleParen(']') => break,
|
||||
other => return other.unexpected("type decoration"),
|
||||
}
|
||||
}
|
||||
self.scopes.pop();
|
||||
}
|
||||
|
||||
let storage_access = decoration.access;
|
||||
let name = lexer.next_ident()?;
|
||||
let handle = match self.lookup_type.get(name) {
|
||||
Some(&handle) => handle,
|
||||
None => {
|
||||
let inner = self.parse_type_decl_impl(
|
||||
lexer,
|
||||
decoration_lexer,
|
||||
name,
|
||||
type_arena,
|
||||
const_arena,
|
||||
)?;
|
||||
let inner =
|
||||
self.parse_type_decl_impl(lexer, decoration, name, type_arena, const_arena)?;
|
||||
type_arena.fetch_or_append(crate::Type {
|
||||
name: self_name.map(|s| s.to_string()),
|
||||
inner,
|
||||
@@ -1382,7 +1349,7 @@ impl Parser {
|
||||
};
|
||||
|
||||
self.scopes.pop();
|
||||
Ok(handle)
|
||||
Ok((handle, storage_access))
|
||||
}
|
||||
|
||||
fn parse_statement<'a>(
|
||||
@@ -1399,7 +1366,7 @@ impl Parser {
|
||||
self.scopes.push(Scope::Statement);
|
||||
let statement = match word {
|
||||
"const" => {
|
||||
let (name, _ty) =
|
||||
let (name, _ty, _access) =
|
||||
self.parse_variable_ident_decl(lexer, context.types, context.constants)?;
|
||||
lexer.expect(Token::Operation('='))?;
|
||||
let expr_id = self.parse_general_expression(lexer, context.as_expression())?;
|
||||
@@ -1414,7 +1381,7 @@ impl Parser {
|
||||
Variable(Handle<crate::Expression>),
|
||||
}
|
||||
|
||||
let (name, ty) =
|
||||
let (name, ty, _access) =
|
||||
self.parse_variable_ident_decl(lexer, context.types, context.constants)?;
|
||||
|
||||
let init = if lexer.skip(Token::Operation('=')) {
|
||||
@@ -1631,7 +1598,7 @@ impl Parser {
|
||||
if !arguments.is_empty() {
|
||||
lexer.expect(Token::Separator(','))?;
|
||||
}
|
||||
let (param_name, param_type) =
|
||||
let (param_name, param_type, _access) =
|
||||
self.parse_variable_ident_decl(lexer, &mut module.types, &mut module.constants)?;
|
||||
let param_index = arguments.len() as u32;
|
||||
let expression_token =
|
||||
@@ -1644,7 +1611,9 @@ impl Parser {
|
||||
}
|
||||
// read return type
|
||||
let return_type = if lexer.skip(Token::Arrow) && !lexer.skip(Token::Word("void")) {
|
||||
Some(self.parse_type_decl(lexer, None, &mut module.types, &mut module.constants)?)
|
||||
let (handle, _access) =
|
||||
self.parse_type_decl(lexer, None, &mut module.types, &mut module.constants)?;
|
||||
Some(handle)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -1792,7 +1761,7 @@ impl Parser {
|
||||
Token::Word("type") => {
|
||||
let name = lexer.next_ident()?;
|
||||
lexer.expect(Token::Operation('='))?;
|
||||
let ty = self.parse_type_decl(
|
||||
let (ty, _access) = self.parse_type_decl(
|
||||
lexer,
|
||||
Some(name),
|
||||
&mut module.types,
|
||||
@@ -1802,7 +1771,7 @@ impl Parser {
|
||||
lexer.expect(Token::Separator(';'))?;
|
||||
}
|
||||
Token::Word("const") => {
|
||||
let (name, ty) = self.parse_variable_ident_decl(
|
||||
let (name, ty, _access) = self.parse_variable_ident_decl(
|
||||
lexer,
|
||||
&mut module.types,
|
||||
&mut module.constants,
|
||||
|
||||
@@ -4,6 +4,18 @@ use super::parse_str;
|
||||
fn parse_types() {
|
||||
parse_str("const a : i32 = 2;").unwrap();
|
||||
assert!(parse_str("const a : x32 = 2;").is_err());
|
||||
parse_str("var t: texture_2d<f32>;").unwrap();
|
||||
parse_str("var t: texture_cube_array<i32>;").unwrap();
|
||||
parse_str("var t: texture_multisampled_2d<u32>;").unwrap();
|
||||
parse_str("var t: [[access(write)]] texture_storage_1d<rgba8uint>;").unwrap();
|
||||
parse_str("var t: [[access(read)]] texture_storage_3d<r32float>;").unwrap();
|
||||
parse_str(
|
||||
"
|
||||
[[block]] struct Foo { [[offset(0)]] x: i32; };
|
||||
var s: [[access(read_write)]] Foo;
|
||||
",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -268,6 +268,7 @@ bitflags::bitflags! {
|
||||
/// Flags describing an image.
|
||||
#[cfg_attr(feature = "serialize", derive(Serialize))]
|
||||
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
|
||||
#[derive(Default)]
|
||||
pub struct StorageAccess: u32 {
|
||||
/// Storage can be used as a source for load ops.
|
||||
const LOAD = 0x1;
|
||||
|
||||
+71
-22
@@ -16,12 +16,16 @@ struct BindSource {
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct BindTarget {
|
||||
#[cfg_attr(not(feature = "msl-out"), allow(dead_code))]
|
||||
#[serde(default)]
|
||||
buffer: Option<u8>,
|
||||
#[cfg_attr(not(feature = "msl-out"), allow(dead_code))]
|
||||
#[serde(default)]
|
||||
texture: Option<u8>,
|
||||
#[cfg_attr(not(feature = "msl-out"), allow(dead_code))]
|
||||
#[serde(default)]
|
||||
sampler: Option<u8>,
|
||||
#[cfg_attr(not(feature = "msl-out"), allow(dead_code))]
|
||||
#[serde(default)]
|
||||
mutable: bool,
|
||||
}
|
||||
@@ -37,9 +41,36 @@ struct Parameters {
|
||||
mtl_bindings: naga::FastHashMap<BindSource, BindTarget>,
|
||||
}
|
||||
|
||||
bitflags::bitflags! {
|
||||
struct Language: u32 {
|
||||
const SPIRV = 0x1;
|
||||
const METAL = 0x2;
|
||||
const GLSL = 0x4;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "spv-out")]
|
||||
fn check_output_spv(module: &naga::Module, name: &str, params: &Parameters) {
|
||||
use naga::back::spv;
|
||||
use rspirv::binary::Disassemble;
|
||||
|
||||
let spv = spv::write_vec(
|
||||
&module,
|
||||
spv::WriterFlags::NONE,
|
||||
params.spv_capabilities.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let dis = rspirv::dr::load_words(spv)
|
||||
.expect("Produced invalid SPIR-V")
|
||||
.disassemble();
|
||||
insta::assert_snapshot!(format!("{}.spvasm", name), dis);
|
||||
}
|
||||
|
||||
#[cfg(feature = "msl-out")]
|
||||
fn check_output_msl(module: &naga::Module, name: &str, params: &Parameters) {
|
||||
use naga::back::msl;
|
||||
|
||||
let mut binding_map = msl::BindingMap::default();
|
||||
for (key, value) in params.mtl_bindings.iter() {
|
||||
binding_map.insert(
|
||||
@@ -65,28 +96,30 @@ fn check_output_msl(module: &naga::Module, name: &str, params: &Parameters) {
|
||||
spirv_cross_compatibility: false,
|
||||
binding_map,
|
||||
};
|
||||
|
||||
let (msl, _) = msl::write_string(&module, &options).unwrap();
|
||||
insta::assert_snapshot!(format!("{}{}", name, ".msl"), msl);
|
||||
insta::assert_snapshot!(format!("{}.msl", name), msl);
|
||||
}
|
||||
|
||||
#[cfg(feature = "spv-out")]
|
||||
fn check_output_spv(module: &naga::Module, name: &str, params: &Parameters) {
|
||||
use naga::back::spv;
|
||||
use rspirv::binary::Disassemble;
|
||||
let spv = spv::write_vec(
|
||||
&module,
|
||||
spv::WriterFlags::NONE,
|
||||
params.spv_capabilities.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
let dis = rspirv::dr::load_words(spv)
|
||||
.expect("Produced invalid SPIR-V")
|
||||
.disassemble();
|
||||
insta::assert_snapshot!(format!("{}{}", name, ".spvasm"), dis);
|
||||
#[cfg(feature = "glsl-out")]
|
||||
fn check_output_glsl(module: &naga::Module, name: &str, stage: naga::ShaderStage, ep_name: &str) {
|
||||
use naga::back::glsl;
|
||||
|
||||
let options = glsl::Options {
|
||||
version: glsl::Version::Embedded(310),
|
||||
entry_point: (stage, ep_name.to_string()),
|
||||
};
|
||||
|
||||
let mut buffer = Vec::new();
|
||||
let mut writer = glsl::Writer::new(&mut buffer, &module, &options).unwrap();
|
||||
writer.write().unwrap();
|
||||
|
||||
let string = String::from_utf8(buffer).unwrap();
|
||||
insta::assert_snapshot!(format!("{}-{:?}.glsl", name, stage), string);
|
||||
}
|
||||
|
||||
#[cfg(feature = "wgsl-in")]
|
||||
fn convert_wgsl(name: &str) {
|
||||
fn convert_wgsl(name: &str, language: Language) {
|
||||
let params =
|
||||
match std::fs::read_to_string(format!("tests/snapshots/in/{}{}", name, ".param.ron")) {
|
||||
Ok(string) => ron::de::from_str(&string).expect("Couldn't find param file"),
|
||||
@@ -100,26 +133,42 @@ fn convert_wgsl(name: &str) {
|
||||
.unwrap();
|
||||
naga::proc::Validator::new().validate(&module).unwrap();
|
||||
|
||||
#[cfg(feature = "msl-out")]
|
||||
check_output_msl(&module, name, ¶ms);
|
||||
#[cfg(feature = "spv-out")]
|
||||
check_output_spv(&module, name, ¶ms);
|
||||
{
|
||||
if language.contains(Language::SPIRV) {
|
||||
check_output_spv(&module, name, ¶ms);
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "msl-out")]
|
||||
{
|
||||
if language.contains(Language::METAL) {
|
||||
check_output_msl(&module, name, ¶ms);
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "glsl-out")]
|
||||
{
|
||||
if language.contains(Language::GLSL) {
|
||||
for &(stage, ref ep_name) in module.entry_points.keys() {
|
||||
check_output_glsl(&module, name, stage, ep_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "wgsl-in")]
|
||||
#[test]
|
||||
fn converts_wgsl_quad() {
|
||||
convert_wgsl("quad");
|
||||
convert_wgsl("quad", Language::all());
|
||||
}
|
||||
|
||||
#[cfg(feature = "wgsl-in")]
|
||||
#[test]
|
||||
fn converts_wgsl_simple() {
|
||||
convert_wgsl("simple");
|
||||
convert_wgsl("simple", Language::all());
|
||||
}
|
||||
|
||||
#[cfg(feature = "wgsl-in")]
|
||||
#[test]
|
||||
fn converts_wgsl_boids() {
|
||||
convert_wgsl("boids");
|
||||
convert_wgsl("boids", Language::METAL);
|
||||
}
|
||||
@@ -56,12 +56,12 @@ struct SimParams {
|
||||
|
||||
[[block]]
|
||||
struct Particles {
|
||||
[[offset(0)]] particles : [[stride 16]] array<Particle, 5>;
|
||||
[[offset(0)]] particles : [[stride(16)]] array<Particle, 5>;
|
||||
};
|
||||
|
||||
[[group(0), binding(0)]] var<uniform> params : SimParams;
|
||||
[[group(0), binding(1)]] var<storage> particlesA : Particles;
|
||||
[[group(0), binding(2)]] var<storage> particlesB : Particles;
|
||||
[[group(0), binding(1)]] var<storage> particlesA : [[access(read_write)]] Particles;
|
||||
[[group(0), binding(2)]] var<storage> particlesB : [[access(read_write)]] Particles;
|
||||
|
||||
[[builtin(global_invocation_id)]] var gl_GlobalInvocationID : vec3<u32>;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ fn main() {
|
||||
|
||||
# fragment
|
||||
[[location(0)]] var<in> v_uv : vec2<f32>;
|
||||
[[group(0), binding(0)]] var u_texture : texture_sampled_2d<f32>;
|
||||
[[group(0), binding(0)]] var u_texture : texture_2d<f32>;
|
||||
[[group(0), binding(1)]] var u_sampler : sampler;
|
||||
[[location(0)]] var<out> o_color : vec4<f32>;
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
---
|
||||
source: tests/snapshots.rs
|
||||
expression: string
|
||||
---
|
||||
#version 310 es
|
||||
|
||||
precision highp float;
|
||||
|
||||
|
||||
|
||||
in vec2 v_uv1;
|
||||
uniform sampler2D u_texture;
|
||||
out vec4 o_color;
|
||||
|
||||
void main() {
|
||||
|
||||
o_color = texture(u_texture, v_uv1);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
source: tests/snapshots.rs
|
||||
expression: string
|
||||
---
|
||||
#version 310 es
|
||||
|
||||
precision highp float;
|
||||
|
||||
|
||||
|
||||
in vec2 a_pos;
|
||||
in vec2 a_uv;
|
||||
out vec2 v_uv;
|
||||
|
||||
void main() {
|
||||
|
||||
v_uv = a_uv;
|
||||
gl_Position = vec4((1.2 * a_pos), 0.0, 1.0);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
---
|
||||
source: tests/snapshots.rs
|
||||
expression: string
|
||||
---
|
||||
#version 310 es
|
||||
|
||||
precision highp float;
|
||||
|
||||
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
gl_Position = vec4(1);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user