This repository was archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathaccess.wgsl
187 lines (149 loc) · 4.29 KB
/
access.wgsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// This snapshot tests accessing various containers, dereferencing pointers.
struct GlobalConst {
a: u32,
b: vec3<u32>,
c: i32,
}
// tests msl padding insertion for global constants
var<private> global_const: GlobalConst = GlobalConst(0u, vec3<u32>(0u, 0u, 0u), 0);
struct AlignedWrapper {
@align(8) value: i32
}
struct Bar {
_matrix: mat4x3<f32>,
matrix_array: array<mat2x2<f32>, 2>,
atom: atomic<i32>,
atom_arr: array<atomic<i32>, 10>,
arr: array<vec2<u32>, 2>,
data: array<AlignedWrapper>,
}
@group(0) @binding(0)
var<storage,read_write> bar: Bar;
struct Baz {
m: mat3x2<f32>,
}
@group(0) @binding(1)
var<uniform> baz: Baz;
@group(0) @binding(2)
var<storage,read_write> qux: vec2<i32>;
fn test_matrix_within_struct_accesses() {
var idx = 1;
idx--;
// loads
_ = baz.m;
_ = baz.m[0];
_ = baz.m[idx];
_ = baz.m[0][1];
_ = baz.m[0][idx];
_ = baz.m[idx][1];
_ = baz.m[idx][idx];
var t = Baz(mat3x2<f32>(vec2<f32>(1.0), vec2<f32>(2.0), vec2<f32>(3.0)));
idx++;
// stores
t.m = mat3x2<f32>(vec2<f32>(6.0), vec2<f32>(5.0), vec2<f32>(4.0));
t.m[0] = vec2<f32>(9.0);
t.m[idx] = vec2<f32>(90.0);
t.m[0][1] = 10.0;
t.m[0][idx] = 20.0;
t.m[idx][1] = 30.0;
t.m[idx][idx] = 40.0;
}
struct MatCx2InArray {
am: array<mat4x2<f32>, 2>,
}
@group(0) @binding(3)
var<uniform> nested_mat_cx2: MatCx2InArray;
fn test_matrix_within_array_within_struct_accesses() {
var idx = 1;
idx--;
// loads
_ = nested_mat_cx2.am;
_ = nested_mat_cx2.am[0];
_ = nested_mat_cx2.am[0][0];
_ = nested_mat_cx2.am[0][idx];
_ = nested_mat_cx2.am[0][0][1];
_ = nested_mat_cx2.am[0][0][idx];
_ = nested_mat_cx2.am[0][idx][1];
_ = nested_mat_cx2.am[0][idx][idx];
var t = MatCx2InArray(array<mat4x2<f32>, 2>());
idx++;
// stores
t.am = array<mat4x2<f32>, 2>();
t.am[0] = mat4x2<f32>(vec2<f32>(8.0), vec2<f32>(7.0), vec2<f32>(6.0), vec2<f32>(5.0));
t.am[0][0] = vec2<f32>(9.0);
t.am[0][idx] = vec2<f32>(90.0);
t.am[0][0][1] = 10.0;
t.am[0][0][idx] = 20.0;
t.am[0][idx][1] = 30.0;
t.am[0][idx][idx] = 40.0;
}
fn read_from_private(foo: ptr<function, f32>) -> f32 {
return *foo;
}
fn test_arr_as_arg(a: array<array<f32, 10>, 5>) -> f32 {
return a[4][9];
}
@vertex
fn foo_vert(@builtin(vertex_index) vi: u32) -> @builtin(position) vec4<f32> {
var foo: f32 = 0.0;
// We should check that backed doesn't skip this expression
let baz: f32 = foo;
foo = 1.0;
test_matrix_within_struct_accesses();
test_matrix_within_array_within_struct_accesses();
// test storage loads
let _matrix = bar._matrix;
let arr = bar.arr;
let index = 3u;
let b = bar._matrix[index].x;
let a = bar.data[arrayLength(&bar.data) - 2u].value;
let c = qux;
// test pointer types
let data_pointer: ptr<storage, i32, read_write> = &bar.data[0].value;
let foo_value = read_from_private(&foo);
// test array indexing
var c2 = array<i32, 5>(a, i32(b), 3, 4, 5);
c2[vi + 1u] = 42;
let value = c2[vi];
_ = test_arr_as_arg(array<array<f32, 10>, 5>());
return vec4<f32>(_matrix * vec4<f32>(vec4<i32>(value)), 2.0);
}
@fragment
fn foo_frag() -> @location(0) vec4<f32> {
// test storage stores
bar._matrix[1].z = 1.0;
bar._matrix = mat4x3<f32>(vec3<f32>(0.0), vec3<f32>(1.0), vec3<f32>(2.0), vec3<f32>(3.0));
bar.arr = array<vec2<u32>, 2>(vec2<u32>(0u), vec2<u32>(1u));
bar.data[1].value = 1;
qux = vec2<i32>();
return vec4<f32>(0.0);
}
@compute @workgroup_size(1)
fn atomics() {
var tmp: i32;
let value = atomicLoad(&bar.atom);
tmp = atomicAdd(&bar.atom, 5);
tmp = atomicSub(&bar.atom, 5);
tmp = atomicAnd(&bar.atom, 5);
tmp = atomicOr(&bar.atom, 5);
tmp = atomicXor(&bar.atom, 5);
tmp = atomicMin(&bar.atom, 5);
tmp = atomicMax(&bar.atom, 5);
tmp = atomicExchange(&bar.atom, 5);
// https://github.com/gpuweb/gpuweb/issues/2021
// tmp = atomicCompareExchangeWeak(&bar.atom, 5, 5);
atomicStore(&bar.atom, value);
}
var<workgroup> val: u32;
fn assign_through_ptr_fn(p: ptr<workgroup, u32>) {
*p = 42u;
}
fn assign_array_through_ptr_fn(foo: ptr<function, array<vec4<f32>, 2>>) {
*foo = array<vec4<f32>, 2>(vec4(1.0), vec4(2.0));
}
@compute @workgroup_size(1)
fn assign_through_ptr() {
var arr = array<vec4<f32>, 2>(vec4(6.0), vec4(7.0));
assign_through_ptr_fn(&val);
assign_array_through_ptr_fn(&arr);
}