|
| 1 | +use std::borrow::Cow; |
| 2 | + |
| 3 | +use wgpu_test::{fail, valid}; |
| 4 | + |
| 5 | +const DEBUG_PRINTF_SHADER: &str = r#" |
| 6 | +enable wgpu_debug_printf; |
| 7 | +
|
| 8 | +@compute @workgroup_size(1) |
| 9 | +fn main() { |
| 10 | + debugPrintf("debug value: %d", 1i); |
| 11 | +} |
| 12 | +"#; |
| 13 | + |
| 14 | +const DEBUG_PRINTF_WITHOUT_ENABLE_SHADER: &str = r#" |
| 15 | +@compute @workgroup_size(1) |
| 16 | +fn main() { |
| 17 | + debugPrintf("debug value: %d", 1i); |
| 18 | +} |
| 19 | +"#; |
| 20 | + |
| 21 | +const STRING_LITERAL_OUTSIDE_DEBUG_PRINTF_SHADER: &str = r#" |
| 22 | +enable wgpu_debug_printf; |
| 23 | +
|
| 24 | +@compute @workgroup_size(1) |
| 25 | +fn main() { |
| 26 | + let _value = "not a debugPrintf format"; |
| 27 | +} |
| 28 | +"#; |
| 29 | + |
| 30 | +fn debug_printf_device() -> wgpu::Device { |
| 31 | + let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor { |
| 32 | + required_features: wgpu::Features::DEBUG_PRINTF, |
| 33 | + ..Default::default() |
| 34 | + }); |
| 35 | + device |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn shader_module() { |
| 40 | + let device = debug_printf_device(); |
| 41 | + valid(&device, || { |
| 42 | + create_shader_module(&device, DEBUG_PRINTF_SHADER); |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn requires_feature() { |
| 48 | + let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); |
| 49 | + fail( |
| 50 | + &device, |
| 51 | + || create_shader_module(&device, DEBUG_PRINTF_SHADER), |
| 52 | + Some("DEBUG_PRINTF"), |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +fn requires_enable_extension() { |
| 58 | + let device = debug_printf_device(); |
| 59 | + fail( |
| 60 | + &device, |
| 61 | + || create_shader_module(&device, DEBUG_PRINTF_WITHOUT_ENABLE_SHADER), |
| 62 | + Some("enable extension is not enabled"), |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn rejects_string_literal_outside_call() { |
| 68 | + let device = debug_printf_device(); |
| 69 | + fail( |
| 70 | + &device, |
| 71 | + || create_shader_module(&device, STRING_LITERAL_OUTSIDE_DEBUG_PRINTF_SHADER), |
| 72 | + Some("String literals are only supported in debugPrintf"), |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +fn create_shader_module(device: &wgpu::Device, source: &str) -> wgpu::ShaderModule { |
| 77 | + device.create_shader_module(wgpu::ShaderModuleDescriptor { |
| 78 | + label: Some("debugPrintf shader"), |
| 79 | + source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(source)), |
| 80 | + }) |
| 81 | +} |
0 commit comments