WGPU compute shader inexplicably returns no data

I have a wgsl compute shader, that essentially checks pairs of datapoints and counts the number pairs that meet a certain requirement (passes(i,j) is true) for each point. (ie. if there are 5 passing pairs that include datapoint #3, then the 3rd element in the output buffer would be set to 5).

Here is a simplified MRE of the shader:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@group(0) @binding(0)
var<uniform> uniforms: Uniforms;
@group(0) @binding(1)
var<storage, read> data: array<f32>;
@group(0) @binding(2)
var<storage, read_write> output: array<atomic<u32>>;
fn passes(i: u32, j: u32) -> bool {
// To be discussed
}
@compute
@workgroup_size(32, 32, 1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
if(passes(global_id.x, global_id.y)) {
atomicAdd(&output[global_id.x], 1u);
}
}
</code>
<code>@group(0) @binding(0) var<uniform> uniforms: Uniforms; @group(0) @binding(1) var<storage, read> data: array<f32>; @group(0) @binding(2) var<storage, read_write> output: array<atomic<u32>>; fn passes(i: u32, j: u32) -> bool { // To be discussed } @compute @workgroup_size(32, 32, 1) fn main(@builtin(global_invocation_id) global_id: vec3<u32>) { if(passes(global_id.x, global_id.y)) { atomicAdd(&output[global_id.x], 1u); } } </code>
@group(0) @binding(0)
var<uniform> uniforms: Uniforms;
@group(0) @binding(1)
var<storage, read> data: array<f32>;
@group(0) @binding(2)
var<storage, read_write> output: array<atomic<u32>>;

fn passes(i: u32, j: u32) -> bool {
    // To be discussed
}


@compute
@workgroup_size(32, 32, 1)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
    if(passes(global_id.x, global_id.y)) {
        atomicAdd(&output[global_id.x], 1u);
    }
}

I had been using an approximation of this requirement, which was fairly easy to calculate, and all results were returning as expected. However, when I attempted to implement the actual requirement, which is slightly more expensive, the shader’s output suggested that there were no successful pairs of data (The output buffer was all 0s). I expected this meant the function that checks the requirement was only returning false, due to bad logic. However, if I replaced the final return result; call in the passes function with

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Calculate expensive result
let result = ...; // Lots of vector flops, but no branching
// Calculate cheap approximation
let approximation = ...; // Something along the lines of data[i] < data[j], something simple
return result || approximation;
</code>
<code>// Calculate expensive result let result = ...; // Lots of vector flops, but no branching // Calculate cheap approximation let approximation = ...; // Something along the lines of data[i] < data[j], something simple return result || approximation; </code>
// Calculate expensive result
let result = ...; // Lots of vector flops, but no branching

// Calculate cheap approximation
let approximation = ...; // Something along the lines of data[i] < data[j], something simple
return result || approximation;

I was still getting all 0’s. This is very odd as the approximation had worked fine on its own, and even if result was always 0 because of a bug, the approximation should still carry through to the return value. My next intuition was that it could be a shader timeout error (perhaps similar to what was discussed here or here). However, if I modify the code again to resemble this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Calculate expensive result
let result = ...; // Lots of vector flops, but no branching
// Calculate cheap approximation
let approximation = ...; // Something along the lines of data[i] < data[j], something simple
return approximation;
</code>
<code>// Calculate expensive result let result = ...; // Lots of vector flops, but no branching // Calculate cheap approximation let approximation = ...; // Something along the lines of data[i] < data[j], something simple return approximation; </code>
// Calculate expensive result
let result = ...; // Lots of vector flops, but no branching

// Calculate cheap approximation
let approximation = ...; // Something along the lines of data[i] < data[j], something simple
return approximation;

It runs just the same as when the whole function was just return approximation. The amount of code executed hasn’t really changed (I think) but it is now working just fine.

For completeness, here is the rust code responsible for saving that output buffer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
encoder.copy_buffer_to_buffer(&output_buffer, 0, &output_staging_buffer, 0, output_buffer.size());
queue.submit(Some(encoder.finish()));
let output_staging_slice = output_staging_buffer.slice(..);
let (s1, r1) = flume::bounded(1);
output_staging_slice.map_async(MapMode::Read, move |data| s1.send(data).unwrap());
device.poll(wgpu::Maintain::Wait);
if let Ok(Ok(())) = r1.recv() {
let output_data = output_staging_slice.get_mapped_range();
let output_result = bytemuck::cast_slice(&output).to_vec();
drop(output_data);
output_staging_buffer.unmap();
Some(output_result)
} else {
None
}
</code>
<code> encoder.copy_buffer_to_buffer(&output_buffer, 0, &output_staging_buffer, 0, output_buffer.size()); queue.submit(Some(encoder.finish())); let output_staging_slice = output_staging_buffer.slice(..); let (s1, r1) = flume::bounded(1); output_staging_slice.map_async(MapMode::Read, move |data| s1.send(data).unwrap()); device.poll(wgpu::Maintain::Wait); if let Ok(Ok(())) = r1.recv() { let output_data = output_staging_slice.get_mapped_range(); let output_result = bytemuck::cast_slice(&output).to_vec(); drop(output_data); output_staging_buffer.unmap(); Some(output_result) } else { None } </code>

encoder.copy_buffer_to_buffer(&output_buffer, 0, &output_staging_buffer, 0, output_buffer.size());
queue.submit(Some(encoder.finish()));

let output_staging_slice = output_staging_buffer.slice(..);

let (s1, r1) = flume::bounded(1);
output_staging_slice.map_async(MapMode::Read, move |data| s1.send(data).unwrap());

device.poll(wgpu::Maintain::Wait);

if let Ok(Ok(())) = r1.recv() {
    let output_data = output_staging_slice.get_mapped_range();
    let output_result = bytemuck::cast_slice(&output).to_vec();

    drop(output_data);
    output_staging_buffer.unmap();
    Some(output_result)
} else {
    None
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật