This commit is contained in:
2026-10-07 20:42:38 +01:00
commit 13518decc0
7 changed files with 1699 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
/target
*.spv
+16
View File
@@ -0,0 +1,16 @@
{
"configurations": [
{
"name": "Debug executable 'binding_array_reproduction'",
"type": "lldb",
"request": "launch",
"cargo": {
"args": [
"run",
"--bin=binding_array_reproduction"
]
},
"args": []
}
]
}
Generated
+1485
View File
File diff suppressed because it is too large Load Diff
+20
View File
@@ -0,0 +1,20 @@
[package]
name = "binding_array_reproduction"
version = "0.1.0"
edition = "2024"
[dependencies]
bytemuck = { version = "1.22.0", features = ["extern_crate_alloc"] }
env_logger = "0.11"
pollster = "1.0"
wgpu = { version = "30", features = ["spirv"] }
log = "*"
[build-dependencies]
naga = { version = "30", features = [
"wgsl-in",
"spv-out",
"termcolor",
"stderr",
"fs",
] }
+57
View File
@@ -0,0 +1,57 @@
use naga::back::spv::PipelineOptions;
fn main() {
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=Cargo.lock");
println!("cargo::rerun-if-changed=src/shader.wgsl");
let shader = std::fs::read_to_string("src/shader.wgsl").unwrap();
// Parse the source into a Module.
let module: naga::Module = naga::front::wgsl::parse_str(&shader)
.inspect_err(|e| e.emit_to_stderr_with_path(&shader, "src/shader.wgsl"))
.expect("Failed to parse WGSL");
// Validate the module.
// Validation can be made less restrictive by changing the ValidationFlags.
let module_info: naga::valid::ModuleInfo = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
)
.subgroup_stages(naga::valid::ShaderStages::all())
.subgroup_operations(naga::valid::SubgroupOperationSet::all())
.validate(&module)
.inspect_err(|e| e.emit_to_stderr_with_path(&shader, "src/shader.wgsl"))
.unwrap();
// Translate the module.
use naga::back::spv;
let mut spv_out = vec![];
let mut writer = spv::Writer::new(&spv::Options {
lang_version: (1, 6),
flags: spv::WriterFlags::DEBUG | spv::WriterFlags::PRINT_ON_TRACE_RAYS_FAIL,
trace_ray_argument_validation: true,
..Default::default()
})
.unwrap();
writer
.write(
&module,
&module_info,
Some(&PipelineOptions {
shader_stage: naga::ShaderStage::Compute,
entry_point: "main".to_owned(),
}),
&None,
&mut spv_out,
)
.unwrap();
std::fs::write(
"src/shader.spv",
spv_out
.into_iter()
.flat_map(|f| f.to_le_bytes())
.collect::<Vec<_>>(),
)
.unwrap();
}
+101
View File
@@ -0,0 +1,101 @@
fn main() {
env_logger::init();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
let adapter =
pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default()))
.expect("Failed to create adapter");
log::info!("Running on Adapter: {:#?}", adapter.get_info());
let downlevel_capabilities = adapter.get_downlevel_capabilities();
if !downlevel_capabilities
.flags
.contains(wgpu::DownlevelFlags::COMPUTE_SHADERS)
{
panic!("Adapter does not support compute shaders");
}
let (device, _) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
label: None,
required_features: wgpu::Features::BUFFER_BINDING_ARRAY
| wgpu::Features::TEXTURE_BINDING_ARRAY
| wgpu::Features::STORAGE_RESOURCE_BINDING_ARRAY
| wgpu::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING
| wgpu::Features::STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
| wgpu::Features::PARTIALLY_BOUND_BINDING_ARRAY,
required_limits: wgpu::Limits::downlevel_defaults(),
experimental_features: wgpu::ExperimentalFeatures::disabled(),
memory_hints: wgpu::MemoryHints::MemoryUsage,
trace: wgpu::Trace::Off,
}))
.expect("Failed to create device");
log::info!("Creating WGSL module...");
let wgsl_module = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl"));
log::logger().flush();
log::info!("Creating SPV module...");
let spv_module = device.create_shader_module(wgpu::include_spirv!("shader.spv"));
log::logger().flush();
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
min_binding_size: None,
has_dynamic_offset: false,
},
count: Some(core::num::NonZeroU32::new(500).unwrap()),
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
min_binding_size: None,
has_dynamic_offset: false,
},
count: Some(core::num::NonZeroU32::new(500).unwrap()),
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[Some(&bind_group_layout)],
immediate_size: 0,
});
log::info!("Creating WGSL pipeline...");
let wgsl_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
module: &wgsl_module,
entry_point: Some("main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
cache: None,
});
log::logger().flush();
log::info!("Creating SPV module...");
let spv_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
module: &spv_module,
entry_point: Some("main"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
cache: None,
});
log::logger().flush();
log::info!("WGSL module: {wgsl_module:?}");
log::info!("SPV module: {spv_module:?}");
log::info!("WGSL pipeline: {wgsl_pipeline:?}");
log::info!("SPV pipeline: {spv_pipeline:?}");
}
+18
View File
@@ -0,0 +1,18 @@
enable wgpu_binding_array;
struct Thing {
pad_a: u32,
pad_b: u32,
}
struct ThingBuffer {
metadata: array<Thing>,
}
@group(0) @binding(0)
var<storage, read_write> thing_buffers_rw: binding_array<ThingBuffer>;
@group(0) @binding(1)
var<storage> thing_buffers_ro: binding_array<ThingBuffer>;
@compute @workgroup_size(8, 8, 8)
fn main() {}