Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0u64 has been mutated to a wierd value after transferring to webgpu then copy back (Webassembly only) #6124

Closed
GopherJ opened this issue Aug 18, 2024 · 1 comment

Comments

@GopherJ
Copy link

GopherJ commented Aug 18, 2024

Description
https://github.com/GopherJ/webgpu-shaders/tree/webassembly-issue

this repo's tests can pass under macos but not in webassembly, after digging a bit I found that somehow 0 has been mutated to a wierd value

Repro steps

  1. clone and switchto webassembly-issue branch
  2. wasm-pack test --release --chrome --features no_std
  3. go to localhost:8000

Expected vs observed behavior
[0, 1, 2, 3] vs [18440006330879049729, 1, 2, 3]

Extra materials
Screenshots to help explain your problem.
Validation logs can be attached in case there are warnings and errors.
Zip-compressed API traces and GPU captures can also land here.

Platform
MacOs m3 max + brave (with webgpu enabled)

@GopherJ GopherJ changed the title 0u64 becomes 18439443380925628417vec2\<u32\> after transferring to webgpu then copy back (Webassembly only) 0u64 has been mutated to a wierd value after transferring to webgpu then copy back (Webassembly only) Aug 18, 2024
@GopherJ
Copy link
Author

GopherJ commented Aug 18, 2024

actually it's a rust thing, if we change to:

        if let Ok(Ok(())) = receiver.recv_async().await {
            // Gets contents of buffer
            let data = buffer_slice.get_mapped_range();
            // Since contents are got in bytes, this converts these bytes back to u32
            //let result:Vec<u64> = bytemuck::cast_slice(&data).to_vec();
            let result =
                unsafe { core::slice::from_raw_parts(data.as_ptr() as *mut [BaseElement; 4], n) }.to_vec();

            // With the current interface, we have to make sure all mapped views are
            // dropped before we unmap the buffer.
            drop(data);
            staging_buffer.unmap(); // Unmaps buffer from memory
                                    // If you are familiar with C++ these 2 lines can be thought of similarly to:
                                    //   delete myPointer;
                                    //   myPointer = NULL;
                                    // It effectively frees the memory

            // Returns data from buffer
            Some(result)
        } else {
            panic!("failed to run compute on gpu!")
        }

instead of

        if let Ok(Ok(())) = receiver.recv_async().await {
            // Gets contents of buffer
            let data = buffer_slice.get_mapped_range();
            // Since contents are got in bytes, this converts these bytes back to u32
            //let result:Vec<u64> = bytemuck::cast_slice(&data).to_vec();
            let result =
                unsafe { core::slice::from_raw_parts(data.as_ptr() as *mut [BaseElement; 4], n) };

            // With the current interface, we have to make sure all mapped views are
            // dropped before we unmap the buffer.
            drop(data);
            staging_buffer.unmap(); // Unmaps buffer from memory
                                    // If you are familiar with C++ these 2 lines can be thought of similarly to:
                                    //   delete myPointer;
                                    //   myPointer = NULL;
                                    // It effectively frees the memory

            // Returns data from buffer
            Some(result.to_vec())
        } else {
            panic!("failed to run compute on gpu!")
        }

seems fixed the issue

@GopherJ GopherJ closed this as completed Aug 18, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in WebGPU for Firefox Aug 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

1 participant