r/wgpu • • Apr 14 '23

Buffer binding size 36 is less than minimum 288

2 Upvotes

I'm trying to pass a uniform to a compute shader. Here is the struct I want to pass:

pub struct ParticleSystemParameters {
noise_scale: f32,
speed_multiplier: f32,
curl_multiplier: f32,
constant_force: [f32; 3],
potential_curl_mix: f32,
elapsed_time: f32,
time_multiplier: f32,
}

It is 36 bytes long. I'm getting this error message when I try to create the bind group:
In Device::create_bind_group

buffer binding size 36 is less than minimum 288

note: buffer = `Params Buffer`

How can I pass a uniform that's 36 bytes long to my shader? I'm trying to parameterize my shader execution at runtime. Currently I'm just hardcoding these values into the shader, but I want a GUI where I can edit them at runtime.


r/wgpu • • Sep 25 '22

"I made this" 3D Cellular Automata | Rust | WGPU | 2,6,9/4,6,8,9/10/M

42 Upvotes

r/wgpu • • Sep 18 '22

Number of bindings in bind group descriptor (2) does not match the number of bindings defined in the bind group layout (1)

7 Upvotes

I'm working on making a compute shader that calculates the mandelbrot set. I've recently written a cellular automata simulator using compute shaders and I've got that working great. But I can't figure out this problem.

In my other project, I grab the bind group layout from the pipeline using

pipeline.get_bind_group_layout(0)

and everything works great. However, in this new project, I'm getting an error saying that the number of bindings doesn't match the bindings defined in the layout.

My compute shader has the following bindings:

@group(0) @binding(0) var output_texture : texture_storage_2d<rgba8unorm, write>;
@group(0) @binding(1) var<uniform> m_params: MandelbrotParams;

Why is my code telling me that my bind_group_layout only has 1 binding when there are clearly 2 bindings in my compute shader code?

Would it be better to provide by bind group layout explicitly instead of grabbing it from my compute pipeline? I tried that already and there was a different error about the bindings. It seems like my pipeline is somehow missing the second binding I've providing in the shader code.

Edit: I just tried to put my Mandelbrot params into their own BindGroup by using

@group(1) @binding(0) 

and got the following: "thread 'main' panicked at 'Error reflecting bind group 1: invalid group index 1', "

It seems like my second binding is being ignored for some reason regardless of what group and binding index I give it in the shader.


r/wgpu • • Jun 06 '23

Prospecting: wgpu app in visionOS / RealityKit

8 Upvotes

Welp, looks like we have another graphics platform to support.

What are the prospects for making multi-platform Rust+WGPU games/interactives, with support for Apple's RealityKit (which has actually been around for a while already) on the new Vision Pro's App Store?

Is anyone running wgpu graphics on iOS RealityKit yet? Can a Rust-wgpu app be (theoretically) applicable to Apple's walled garden?


r/wgpu • • Sep 15 '22

What's the WGSL equivalent of GLSL's "in" keyword? I'd like to pass a mutable reference of a variable in one of my functions.

9 Upvotes

I have the following GLSL function which uses the in keyword

float fbm (in vec2 st) {
    st *= 2.;
    ..
}

How can I accomplish the same thing in WGSL? I'd like to pass uv as a mutable reference so I can modify it.

fn fbm2 (uv: vec2<f32>) -> vec3<f32> {
    uv = uv * 2.;
    ..
}

I tried searching DuckDuckGo and also looked at the spec documentation, but I'm honestly lost on how to accomplish this.


r/wgpu • • Jul 06 '22

Question Unable to find any adapters

4 Upvotes

I've stumbled upon WGPU and found the project incredibly interesting but have yet to be able to work with it due to what I believe might be a runtime error but I am not quite sure. I am running macOS 10.13.6 on a 21.5-inch, Late 2009 iMac (old hardware I know, but it's what I got). It has an NVIDIA 9400 GPU that every other application detects. I have run the following to ensure that OpenGL is running under the GPU:

$ glxinfo | grep -i nvidia
OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: NVIDIA GeForce 9400 OpenGL Engine
OpenGL version string: 2.1 NVIDIA-10.4.14 310.90.30.05b27

However, when I run any of the examples:

$ cargo run --example hello
    Finished dev [unoptimized + debuginfo] target(s) in 2m 45s
     Running `target/debug/examples/hello`
Available adapters:
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', wgpu/examples/hello/main.rs:16:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[2022-07-06T21:04:04Z INFO  wgpu_core::hub] Dropping Global
$ cargo run --bin wgpu-info
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target/debug/wgpu-info`
[2022-07-06T21:11:51Z INFO  wgpu_core::hub] Dropping Global

I've tried running with the environment variables WGPU_BACKEND, BACKEND both set to 'gl' as well as WGPU_ADAPTER_NAME set to 9400, GeForce, NVIDIA, etc.

Any help would be much appreciated!


r/wgpu • • Sep 20 '22

Extremely slow Instance::new()

6 Upvotes

As you are all aware, the first thing that must be done to use WGPU is to create a new instance, like this:

let instance = wgpu::Instance::new(wgpu::Backends::all());

Running this line on my Mac (M1) works very well, completing it in less than 1 ms. However, when running it on my Surface Book 3 (Window 11), it takes in the best case around 700ms, and very often over 2s!

I have tried changing from all() to specific backends, but they all create these large times (DX12 actually finishes the instance creation in <100ms, but then the adapter generation takes over 2s instead so the sum is the same)

Unfortunately, I only have one computer with Windows available, so I was simply wondering what kind of times others are getting. Are these large times to be expected on Windows? Or is there something else faulty with my machine/installation/the library?

Thank you in advance!

/Gustav


r/wgpu • • Jun 02 '23

Question Should I learn Opengl first

10 Upvotes

I'm just starting my journey into graphics programming and I have been looking for an opportunity to learn rust. I wanted to know if its okay to begin with wgpu without much graphics programming experience or should I learn opengl first before diving into wgpu, since I do know a bit of c++. I only have an m1 macbook so I don't if opengl is a good graphics api to start with too.


r/wgpu • • Dec 20 '22

Question [WGPU][WASM][HELP] Any way to reduce latency sending data from GPU->CPU?

Thumbnail self.rust_gamedev
1 Upvotes

r/wgpu • • Sep 19 '22

Setting Scissor Rectangle before Clearing

2 Upvotes

Hello everyone!

I am currently converting some code from OpenGL to using WGPU, and I have hit a problem I can't seem to resolve. In my original code, I set the scissor rectangle before clearing the render area, as I want to keep most of my framebuffer intact and only re-render the area that has been modified.

However, in WGPU, the clearing operation seems to be only available as a loading operation, when creating the render pass. So when I set the scissor rectangle, which is done on the render pass, the clearing has already been performed, clearing the entire framebuffer.

Am I missing something or is this currently not possible on WGPU?

Regards,

Gustav

let mut render_pass = encoder.begin_render_pass(
    &wgpu::RenderPassDescriptor {
        label: Some("Render Pass"),
        color_attachments: &[Some(
            wgpu::RenderPassColorAttachment {
                view: &view,
                resolve_target: None,
                ops: wgpu::Operations {
                    load: wgpu::LoadOp::Clear(
                        wgpu::Color {
                            r: 0.04,
                            g: 0.08,
                            b: 0.08,
                            a: 1.0,
                        }
                    ),
                    store: true,
                },
            }
        )],
        depth_stencil_attachment: None,
    }
);

//Here the render pass has already been generated, with the entire area cleared
render_pass.set_scissor_rect(
    200,
    200,
    800, 
    800
);

r/wgpu • • Jul 28 '22

Question Why is my triangle rainbow?

5 Upvotes

Hello! I am following this guide: https://sotrh.github.io/learn-wgpu/beginner/tutorial4-buffer/#so-what-do-i-do-with-it . So far I am doing pretty well and understand what most things do, but I don't understand is why the triangle is rainbow.

From my understanding, the vertex shader is applied only to every vertex. I was expecting the top pixel of the triangle to be red, the left to be green, and the right to be blue. But what is setting the color of every pixel inside the triangle to a combination of red, green, and blue depending on the position?

I think relevant code below: (full code on GitHub here: https://github.com/LelsersLasers/LearningWGPU)

// lib.rs
const VERTICES: &[Vertex] = &[
    Vertex { // top
        position: [0.0, 0.5, 0.0],
        color: [1.0, 0.0, 0.0],
    },
    Vertex { // bottom left
        position: [-0.5, -0.5, 0.0],
        color: [0.0, 1.0, 0.0],
    },
    Vertex { // bottom right
        position: [0.5, -0.5, 0.0],
        color: [0.0, 0.0, 1.0],
    },
];

// shader.wgsl
struct VertexInput {
    @location(0) position: vec3<f32>,
    @location(1) color: vec3<f32>,
}
struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) color: vec3<f32>,
};
@vertex
fn vs_main(model: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.color = model.color;
    out.clip_position = vec4(model.position, 1.0);
    return out;
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    return vec4(in.color, 1.0);
}

r/wgpu • • Jun 03 '23

Question Can someone please explain to me the whole buffer mapping thing and why there can be a write_buffer without mapping but not read_buffer?

13 Upvotes

Solved; this is now a guide (at least I've finally got a reasonably satisfactory answer from my research learning Vulkan) (see below.)

Question: Everything I find on this topic is just 'mapping lets the cpu access the memory but the gpu needs it to be unmapped'. I'd really like to know what's actually going on in the gpu under the hood when it comes to buffer mapping and what write_buffer does, why it doesn't need the buffer to be mapped, and why the same technique can't be applied to create a read_buffer. It'd be nice to just be able to not use a staging buffer when it comes to compute shaders.

My answer for others looking this up: To my understanding, graphics cards have memory that can be accessed outside of the graphics card and internal-only memory that can only be accessed by the GPU. A buffer being "mappable" means that it is accessible by the CPU. The problem with the publicly accessible memory is that it is to some apparently significant degree, slower than the internal memory. For that reason, some APIs (including WebGPU which WGPU is an implementation of) straight-up don't allow you to use public/"mappable" buffers for anything but memory transfers. Note though that WGPU provides an adapter feature, "MAPPABLE_PRIMARY_BUFFERS" that allows you to do just that but only works for certain backends (backends that don't suffer from a difference in speed between these two types of memory) without significant slowdowns.

This explains why buffers that have a mappable usage can only be used in specific ways; they're going to be stored in fundamentally different parts of memory (again, how this translates to hardware is where I'm still shaky).

The process of "mapping" means obtaining a pointer to the memory in VRAM to be used by the CPU. I'm going to be talking here about pointers like it's C or unsafe Rust so just have that be in your mind. Mapping doesn't do anything to the memory formatting wise; to my understanding, it just retrieves a pointer that can be used like any other pointer for performing accesses and memcpy's.

Well then, why not have the mappable buffers mapped all the time? Well in some APIs, you can! WGPU is an implementation of WebGPU though and WebGPU disallows this and WGPU doesn't include a feature to bypass it for the same reason! Because you can never quite tell what the GPU is doing and when, having a CPU pointer to memory the GPU is actively using is a dangerous game. WebGPU avoids nasty race conditions by taking a page out of Rust's book and sort of thinking of either the CPU or GPU having "ownership" of the buffer through mapping. When the memory is mapped and thus could be being accessed by the CPU, the GPU isn't allowed to use it and vica versa.

Learning Vulkan has been a huge help in learning what's actually going on in the GPU and what's just a formality by WebGPU and thus WGPU.

I hope someone now finds this useful!


r/wgpu • • Sep 25 '22

Why can't I clone a BindGroupLayout?

6 Upvotes

I'm trying to make my bind group creation process a little bit more encapsulated by letting my structs which contain the data for the BindGroup can create their own BindGroup. Basically just a trait called ToBindGroup that I can implement on any state struct I want to upload to the gpu for computation.

To create a BindGroup, you must supply a BindGroupLayout. I decided to just store the BindGroupLayout as a field in my struct. Then I ran into an error when trying to clone this struct in another part of the program. I assumed BindGroupLayout would be Clone-able since it's just a simple type describing the layout and containing no real data.

What is the reason for this? I can get around this issue by just recreating the layout every time I need a the BindGroup, or by getting the layout from the Pipeline, but that just seems kind of silly.

So.. is there a reason that a BindGroupLayout can't be cloned?

Edit: I got around this issue by letting my struct have a Rc<wgpu::BindGroupLayout> field. I populate this field when I create the struct by grabbing the layout from the pipeline, creating an rc from it, then creating my struct by passing this rc to its new() function. Is this the right way to solve this sort of issue?


r/wgpu • • Oct 10 '22

Is there a blit command available in wgpu?

6 Upvotes

This seems like something quite basic and standard in graphics, but so far I haven't found anything. Is there a blit command available?


r/wgpu • • Sep 18 '22

Resources on WGSL clip planes?

Thumbnail self.graphicsprogramming
1 Upvotes

r/wgpu • • Sep 04 '22

Writing to a Texture from a Compute Shader?

5 Upvotes

I made a post on here yesterday (https://www.reddit.com/r/wgpu/comments/x5b8qg/updating_textures/) asking how to update a texture from the CPU. I was able to get that working, and now I'm ready to do this using a compute shader instead. I've looked around the usual places trying to find how to do this but I'm not having much luck. I've currently got my render shader working fine. It's pretty simple, just draws a texture onto a quad.

I now need to be able to update the pixels in this texture from a compute shader. I found a project where someone does something similar, but the buffers he uses in his compute shader don't appear to be textures. I haven't been able to grok exactly how he's changing the texture data from his computer shader. For reference, here is his project: https://github.com/blakej11/wgpu-life

Can anyone explain either how he is accomplishing this task, or how I could do it the way I was planning originally by directly modifying the pixels of a texture from a compute shader?


r/wgpu • • Sep 20 '22

Is there any example out there for doing double buffering in wgpu?

2 Upvotes

I saw that swapchain has been removed and all of the related stuff are moved into surface API, I would like to know how do I achieve double buffering with it, as I couldn’t find any API related to swap buffer


r/wgpu • • Jun 14 '22

Difference between wgpu.rs and webgpu (preformance)

3 Upvotes

I would like to know that the difference is between wgpu.rs and webgpu in terms of preformance.
I think that wgpu.rs is mutch faster because it runs natively and you have more control over memory then for example javascript but i might be wrong..


r/wgpu • • Sep 17 '22

Question Is there anywhere to see a roadmap of features to be standardized on the browser?

2 Upvotes

For instance, I see that Push Constants are supported on all the relevant back-ends: Vulkan, Metal, DX12, DX11 and OpenGL. I would be interested to understand if potentially it will land in the browser, and to understand which features will and will not be supported in the browser in the foreseeable future.


r/wgpu • • Jun 06 '23

Question WGPU Chrome Canary Downlevel flags compatibility

1 Upvotes

Hey, I had a question about Storage buffers and downlevel frags for using WGPU through WASM.

When running my code on chrome canary, I get the following error when creating a "read_only: true" storage buffer:

" In Device::create_bind_group_layout

note: label = `bindgroup layout`

Binding 1 entry is invalid

Downlevel flags DownlevelFlags(VERTEX_STORAGE) are required but not supported on the device. ..."

After logging my adapter's downlevel flags in chrome, VERTEX_STORAGE is indeed missing, it is however present when running in winit.

The interesting thing is that the same code built using the javascript WebGPU API works and seems to have support for VERTEX_STORAGE in Chrome canary. Here is a snippet of my Rust implementation followed by the JS.

Is this a Wgpu support thing or am I missing something?

EDIT:

https://docs.rs/wgpu/latest/wgpu/struct.DownlevelCapabilities.html

From the documentation, it seems that adapter.get_downlevel_capabilities() returns a list of features that are NOT supported, instead of the ones that are supported:When logging "adapter.get_downlevel_capabilities()" I get:

DownlevelCapabilities { flags: DownlevelFlags(NON_POWER_OF_TWO_MIPMAPPED_TEXTURES | CUBE_ARRAY_TEXTURES | COMPARISON_SAMPLERS | ANISOTROPIC_FILTERING), limits: DownlevelLimits, shader_model: Sm5 }

Since VERTEX_STORAGE is not in there, I don't Understand why i'm getting:" Downlevel flags DownlevelFlags(VERTEX_STORAGE) are required but not supported on the device."

------ RUST --------

```rust

let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("bindgroup layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX
| wgpu::ShaderStages::COMPUTE
| wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::VERTEX
| wgpu::ShaderStages::COMPUTE
| wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::COMPUTE | wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});

---------- JS ------------

```javascript

const bindGroupLayout = device.createBindGroupLayout({
label: "Cell Bind Group Layout",
entries: [
{
binding: 0,
visibility: GPUShaderStage.VERTEX | GPUShaderStage.COMPUTE | GPUShaderStage.FRAGMENT,
buffer: {}, // Grid uniform buffer
},
{
binding: 1,
visibility: GPUShaderStage.VERTEX | GPUShaderStage.COMPUTE | GPUShaderStage.FRAGMENT,
buffer: { type: "read-only-storage" }, // Cell state input buffer
},
{
binding: 2,
visibility: GPUShaderStage.COMPUTE | GPUShaderStage.FRAGMENT,
buffer: { type: "storage" }, // Cell state output buffer
},
],
});

```


r/wgpu • • Mar 06 '23

Noob question: What's the difference between Skia and wgpu?

6 Upvotes

I am pretty new in computer graphics. Could someone explain to me the difference practically? Seems both support different platforms like Vulkan, OpenGL etc.


r/wgpu • • Apr 30 '23

Noob Question: Why Am I Getting The Error "The pipeline layout, associated with the current compute pipeline, contains a bind group layout at index 0 which is incompatible with the bind group layout associated with the bind group at 0"?

4 Upvotes

A little bit of context: I am working on translating https://observablehq.com/@flimsyhat/tdse-simulation / https://davidar.io/post/quantum-glsl to a wgpu based implementation. So far, I have adapter the triangle example by adding a buffer that can be read to and written to by both the render shader and a compute shader. However, when I try to initialize the values in the buffer inside of a compute shader ( which I know can be done on the CPU as well, but I need to write to the buffer on the GPU eventually ), I get the error "The pipeline layout, associated with the current compute pipeline, contains a bind group layout at index 0 which is incompatible with the bind group layout associated with the bind group at 0". The only reference of this error I saw online was https://github.com/sotrh/learn-wgpu/issues/40, where the buffer was not initialized before it the compute shader attempted to write to it, however I don't think that is the case with mine.

My code is https://github.com/AlistairKeiller/quantumv2/tree/94620f319798120a686ea235802541d18b21a591.

Any advice?


r/wgpu • • Oct 18 '22

Discussion What is the outlook for WebGPU being available in most browsers?

7 Upvotes

WebGPU is super interesting, and even with its many limitations it would be a huge step up compared to WebGL in terms of what it would enable.

What is a reasonable outlook for when WebGPU will be a feature available in most browsers? I guess it is still quite early days, but are we talking about a year? Two years? Five years or even 10? What might the process look like from here.


r/wgpu • • Sep 05 '22

Refactoring Code Causes Texture to Not Save

1 Upvotes

I've been working on a cellular automata simulator using wgpu-rs. I've gotten the simulation working now, and to test the states I've been saving the texture to a file after a single iteration. This all works perfectly as long as all the code is in one function. I tried refactoring my code, but when I did, my output texture always come out blank. I reverted, and started refactoring piece by piece. I originally thought it was something to do with the textures being members of the struct, but that doesn't seem to be it.

When I move my code that copies the texture to a buffer and dumps to a file into its own function, the output file is always blank. I've isolated it to only these specific lines. I'm including a pastebin of the code in question in hopes that someone sees something that I am missing.

Paste: https://pastebin.com/VE7WMKaW

At line 125 you'll see the comment. If I refactor the code below into a save_to_file function, the output is blank. The only difference when I refactor is that I have to create a new CommandEncoder. I've taken away all the asynchronous stuff I can think of, so I don't think its a race condition. Thanks in advance, you guys have been very helpful so far!