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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//! # `bevy_gpu_test`
//!
//! Run WGSL shader code on a real GPU from a Rust test and get results back.
//!
//! Testing GPU shader code is hard. You need a headless Bevy app, storage buffers,
//! a compute pipeline, a render graph node, workgroup dispatch, GPU readback — easily
//! 300+ lines of boilerplate before you can even check a single value. This crate
//! handles all of that.
//!
//! The idea is simple: wrap whatever WGSL code you want to test in a thin compute
//! shader, pass inputs in, get outputs back, and assert in Rust. This works for
//! testing any shader logic — noise functions, vertex displacement, lighting math,
//! procedural generation, simulation kernels — anything you can call from WGSL.
//!
//! ## Quick start
//!
//! Write a compute shader that reads inputs and writes outputs:
//!
//! ```wgsl
//! // assets/shaders/add.wgsl
//!
//! struct Input {
//! a: f32,
//! b: f32,
//! _pad1: f32,
//! _pad2: f32,
//! }
//!
//! struct Output {
//! sum: f32,
//! _pad1: f32,
//! _pad2: f32,
//! _pad3: f32,
//! }
//!
//! @group(0) @binding(0) var<storage, read> inputs: array<Input>;
//! @group(0) @binding(1) var<storage, read_write> outputs: array<Output>;
//!
//! @compute @workgroup_size(64)
//! fn main(@builtin(global_invocation_id) id: vec3<u32>) {
//! let i = id.x;
//! if i >= arrayLength(&inputs) { return; }
//! outputs[i] = Output(inputs[i].a + inputs[i].b, 0.0, 0.0, 0.0);
//! }
//! ```
//!
//! Then test it from Rust:
//!
//! ```rust,no_run
//! use bevy::render::render_resource::ShaderType;
//! use bevy_gpu_test::ComputeTest;
//!
//! #[derive(Clone, Copy, Debug, ShaderType)]
//! struct Input {
//! a: f32,
//! b: f32,
//! _pad1: f32,
//! _pad2: f32,
//! }
//!
//! #[derive(Clone, Copy, Debug, Default, ShaderType)]
//! struct Output {
//! sum: f32,
//! _pad1: f32,
//! _pad2: f32,
//! _pad3: f32,
//! }
//!
//! #[test]
//! fn addition_on_gpu() {
//! let inputs = vec![
//! Input { a: 1.0, b: 2.0, _pad1: 0.0, _pad2: 0.0 },
//! Input { a: -5.0, b: 3.0, _pad1: 0.0, _pad2: 0.0 },
//! ];
//!
//! let results: Vec<Output> = ComputeTest::new("shaders/add.wgsl", inputs).run();
//!
//! assert!((results[0].sum - 3.0).abs() < 1e-6);
//! assert!((results[1].sum - -2.0).abs() < 1e-6);
//! }
//! ```
//!
//! ## With a uniform buffer
//!
//! If your shader needs configuration parameters, use [`ComputeTest::with_uniform`].
//! This adds a uniform at `@binding(0)`, shifting the storage buffers to 1 and 2:
//!
//! ```rust,no_run
//! # use bevy::render::render_resource::ShaderType;
//! # use bevy_gpu_test::ComputeTest;
//! #[derive(Clone, Copy, Debug, ShaderType)]
//! struct Config {
//! scale: f32,
//! offset: f32,
//! _pad1: f32,
//! _pad2: f32,
//! }
//!
//! # #[derive(Clone, Copy, Debug, ShaderType)]
//! # struct Input { x: f32, _p1: f32, _p2: f32, _p3: f32 }
//! # #[derive(Clone, Copy, Debug, Default, ShaderType)]
//! # struct Output { v: f32, _p1: f32, _p2: f32, _p3: f32 }
//! let inputs = vec![Input { x: 5.0, _p1: 0.0, _p2: 0.0, _p3: 0.0 }];
//! let results: Vec<Output> = ComputeTest::new("shaders/scale.wgsl", inputs)
//! .with_uniform(Config { scale: 2.0, offset: 10.0, _pad1: 0.0, _pad2: 0.0 })
//! .run();
//! ```
//!
//! ## Testing non-compute shaders
//!
//! You can test *any* WGSL code this way, not just compute shaders. Write your
//! reusable logic as WGSL functions, `#import` them into a thin compute shader
//! wrapper, and test through that. Vertex displacement, fragment math, noise,
//! simulation — if it runs on the GPU, you can test it.
//!
//! ## How it works
//!
//! 1. Spins up a headless Bevy app (no window)
//! 2. Uploads your input data to a GPU storage buffer
//! 3. Creates a compute pipeline from your shader
//! 4. Dispatches it via a render graph node
//! 5. Reads the output buffer back to the CPU via [`Readback`](bevy::render::gpu_readback::Readback)
//! 6. Returns the typed results to your test
//!
//! Tests run on a real GPU with the real WGSL compiler — no mocking, no
//! approximations.
//!
//! ## Bind group layout
//!
//! **Without uniform** (default):
//! - `@binding(0)`: `storage<read>` — input buffer
//! - `@binding(1)`: `storage<read_write>` — output buffer
//!
//! **With uniform** ([`with_uniform`](ComputeTest::with_uniform)):
//! - `@binding(0)`: `uniform` — config/params
//! - `@binding(1)`: `storage<read>` — input buffer
//! - `@binding(2)`: `storage<read_write>` — output buffer
//!
//! ## Timeout
//!
//! By default, tests time out after 5 seconds with a diagnostic panic message
//! that includes the pipeline state and common failure causes. Override with
//! [`ComputeTest::with_timeout`].
use ;
use run_compute_test;
use ;
/// Builder for a GPU compute test.
///
/// Configures a headless Bevy app that loads a compute shader, uploads input data
/// to the GPU, dispatches the shader, and reads back the output buffer.
///
/// # Type parameters
///
/// - `I`: The input element type. Must derive [`ShaderType`].
/// - `O`: The output element type. Must derive [`ShaderType`] and [`Default`].