-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validation tests for unary address-of and indirection
- Loading branch information
Showing
2 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
src/webgpu/shader/validation/expression/unary/address_of_and_indirection.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
export const description = ` | ||
Validation tests for unary address-of and indirection (dereference) | ||
`; | ||
|
||
import { makeTestGroup } from '../../../../../common/framework/test_group.js'; | ||
import { keysOf } from '../../../../../common/util/data_tables.js'; | ||
import { ShaderValidationTest } from '../../shader_validation_test.js'; | ||
|
||
export const g = makeTestGroup(ShaderValidationTest); | ||
|
||
const kAddressSpaces = ['function', 'private', 'workgroup', 'uniform', 'storage']; | ||
const kAccessModes = ['read', 'read_write']; | ||
const kStorageTypes = ['bool', 'u32', 'i32', 'f32', 'f16']; | ||
const kCompositeTypes = ['array', 'struct', 'vec']; | ||
const kDerefTypes = { | ||
deref_address_of_identifier: { | ||
wgsl: (id: string, ptr: string) => `(*(&${id}))`, | ||
requires_pointer_composite_access: false, | ||
}, | ||
deref_pointer: { | ||
wgsl: (id: string, ptr: string) => `(*${ptr})`, | ||
requires_pointer_composite_access: false, | ||
}, | ||
address_of_identifier: { | ||
wgsl: (id: string, ptr: string) => `(&${id})`, | ||
requires_pointer_composite_access: true, | ||
}, | ||
pointer: { | ||
wgsl: (id: string, ptr: string) => `${ptr}`, | ||
requires_pointer_composite_access: true, | ||
}, | ||
}; | ||
|
||
g.test('basic') | ||
.desc( | ||
`Validates address-of (&) every supported variable type, ensuring the type is correct by | ||
assigning to an explicitly typed pointer. Also validates dereferencing the reference, | ||
ensuring the type is correct by assigning to an explicitly typed variable.` | ||
) | ||
.params(u => | ||
u | ||
.combine('addressSpace', kAddressSpaces) | ||
.combine('accessMode', kAccessModes) | ||
.combine('storageType', kStorageTypes) | ||
.combine('derefType', keysOf(kDerefTypes)) | ||
.filter(t => { | ||
if (t.storageType === 'bool') { | ||
return t.addressSpace === 'function' || t.addressSpace === 'private'; | ||
} | ||
return true; | ||
}) | ||
.filter(t => { | ||
// This test does not test composite access | ||
return !kDerefTypes[t.derefType].requires_pointer_composite_access; | ||
}) | ||
) | ||
.beforeAllSubcases(t => { | ||
if (t.params.storageType === 'f16') { | ||
t.selectDeviceOrSkipTestCase({ requiredFeatures: ['shader-f16'] }); | ||
} | ||
}) | ||
.fn(t => { | ||
const isLocal = t.params.addressSpace === 'function'; | ||
const deref = kDerefTypes[t.params.derefType]; | ||
// Only specify access mode for storage buffers | ||
const commaAccessMode = t.params.addressSpace === 'storage' ? `, ${t.params.accessMode}` : ''; | ||
|
||
let varDecl = ''; | ||
if (t.params.addressSpace === 'uniform' || t.params.addressSpace === 'storage') { | ||
varDecl += '@group(0) @binding(0) '; | ||
} | ||
varDecl += `var<${t.params.addressSpace}${commaAccessMode}> a : VarType;`; | ||
|
||
const wgsl = ` | ||
${t.params.storageType === 'f16' ? 'enable f16;' : ''} | ||
alias VarType = ${t.params.storageType}; | ||
alias PtrType = ptr<${t.params.addressSpace}, VarType ${commaAccessMode}>; | ||
${isLocal ? '' : varDecl} | ||
fn foo() { | ||
${isLocal ? varDecl : ''} | ||
let p : PtrType = &a; | ||
var deref : VarType = ${deref.wgsl('a', 'p')}; | ||
} | ||
`; | ||
|
||
t.expectCompileResult(true, wgsl); | ||
}); | ||
|
||
g.test('composite') | ||
.desc( | ||
`Validates address-of (&) every supported variable type for composite types, ensuring the type | ||
is correct by assigning to an explicitly typed pointer. Also validates dereferencing the | ||
reference followed by member/index access, ensuring the type is correct by assigning to an | ||
explicitly typed variable.` | ||
) | ||
.params(u => | ||
u | ||
.combine('addressSpace', kAddressSpaces) | ||
.combine('accessMode', kAccessModes) | ||
.combine('storageType', kStorageTypes) | ||
.combine('compositeType', kCompositeTypes) | ||
.combine('derefType', keysOf(kDerefTypes)) | ||
.filter(t => { | ||
if (t.storageType === 'bool') { | ||
return t.addressSpace === 'function' || t.addressSpace === 'private'; | ||
} | ||
return true; | ||
}) | ||
) | ||
.beforeAllSubcases(t => { | ||
if (t.params.storageType === 'f16') { | ||
t.selectDeviceOrSkipTestCase({ requiredFeatures: ['shader-f16'] }); | ||
} | ||
}) | ||
.fn(t => { | ||
const isLocal = t.params.addressSpace === 'function'; | ||
const deref = kDerefTypes[t.params.derefType]; | ||
// Only specify access mode for storage buffers | ||
const commaAccessMode = t.params.addressSpace === 'storage' ? `, ${t.params.accessMode}` : ''; | ||
|
||
let varDecl = ''; | ||
if (t.params.addressSpace === 'uniform' || t.params.addressSpace === 'storage') { | ||
varDecl += '@group(0) @binding(0) '; | ||
} | ||
varDecl += `var<${t.params.addressSpace}${commaAccessMode}> a : VarType;`; | ||
|
||
let wgsl = ` | ||
${t.params.storageType === 'f16' ? 'enable f16;' : ''}`; | ||
|
||
switch (t.params.compositeType) { | ||
case 'array': | ||
wgsl += ` | ||
struct S { @align(16) member : ${t.params.storageType} } | ||
alias VarType = array<S, 10>; | ||
alias PtrType = ptr<${t.params.addressSpace}, VarType ${commaAccessMode}>; | ||
${isLocal ? '' : varDecl} | ||
fn foo() { | ||
${isLocal ? varDecl : ''} | ||
let p : PtrType = &a; | ||
var deref : ${t.params.storageType} = ${deref.wgsl('a', 'p')}[0].member; | ||
} | ||
`; | ||
break; | ||
case 'struct': | ||
wgsl += ` | ||
struct S { member : ${t.params.storageType} } | ||
alias VarType = S; | ||
alias PtrType = ptr<${t.params.addressSpace}, VarType ${commaAccessMode}>; | ||
${isLocal ? '' : varDecl} | ||
fn foo() { | ||
${isLocal ? varDecl : ''} | ||
let p : PtrType = &a; | ||
var deref : ${t.params.storageType} = ${deref.wgsl('a', 'p')}.member; | ||
} | ||
`; | ||
break; | ||
case 'vec': | ||
wgsl += ` | ||
alias VarType = vec3<${t.params.storageType}>; | ||
alias PtrType = ptr<${t.params.addressSpace}, VarType ${commaAccessMode}>; | ||
${isLocal ? '' : varDecl} | ||
fn foo() { | ||
${isLocal ? varDecl : ''} | ||
let p : PtrType = &a; | ||
var deref_member : ${t.params.storageType} = ${deref.wgsl('a', 'p')}.x; | ||
var deref_index : ${t.params.storageType} = ${deref.wgsl('a', 'p')}[0]; | ||
} | ||
`; | ||
break; | ||
} | ||
|
||
let shouldPass = true; | ||
if ( | ||
kDerefTypes[t.params.derefType].requires_pointer_composite_access && | ||
!t.hasLanguageFeature('pointer_composite_access') | ||
) { | ||
shouldPass = false; | ||
} | ||
|
||
t.expectCompileResult(shouldPass, wgsl); | ||
}); |