bevyengine / bevyengine/bevy

Material BindGroup Storage returns arraylength of 1 when the vec is empty

Open
#12,418 1 comment 0 reactions 0 assignees View on GitHub
A-Rendering C-Bug C-Docs
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## Bevy version

13.0

## \[Optional\] Relevant system information

Rust version: cargo 1.76.0 (c84b36747 2024-01-18)

``` ignore
`SystemInfo { os: "Windows 11 Home", kernel: "22631", cpu: "AMD Ryzen 5 4500U with Radeon Graphics", core_count: "6", memory: "15.4 GiB" }`
`AdapterInfo { name: "AMD Radeon(TM) Graphics", vendor: 4098, device: 5686, device_type: IntegratedGpu, driver: "AMD proprietary driver", driver_info: "21.30.23.04", backend: Vulkan }`
```

## What you did

I created a custom material with a storage buffer:
``` Rust
fn init_shader(
mut commands: Commands,
mut meshes: ResMut>,
mut materials: ResMut>,
) {
commands.spawn((
MaterialMeshBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0),
mesh: meshes.add(Plane3d::new(Vec3::Y).mesh().size(100.0, 100.0)),
material: materials.add(CustomMaterial {
curves: Vec::with_capacity(0),
}),
..default()
},
ShaderMarker,
));
}

fn update_shader(
shader: Query<&Handle, With>,
edges: Query<&RoadEdge>,
mut materials: ResMut>,
) {
let handle = shader.single();
let mat = materials.get_mut(handle).unwrap();

let curves = edges
.into_iter()
.map(|edge| Curve::from(edge))
.collect::>();

println!("{:?}", curves);

mat.curves = curves;
}

#[derive(Component)]
struct ShaderMarker;

#[derive(ShaderType, Debug, Clone)]
struct Curve {
rotation: Vec2,
center: Vec2,
angle: Vec2,
radius: f32,
thickness: f32,
}

impl From<&RoadEdge> for Curve {
fn from(edge: &RoadEdge) -> Self {
let curve = Self {
rotation: edge.rotation(),
center: edge.center().xz(),
angle: Vec2::new((edge.angle() * 0.5).sin(), (edge.angle() * 0.5).cos()),
radius: edge.radius(),
thickness: edge.lanes() as f32 * ROAD_WIDTH,
};

curve
}
}

// This struct defines the data that will be passed to your shader
#[derive(Asset, AsBindGroup, TypePath, Debug, Clone)]
struct CustomMaterial {
#[storage(2, read_only)]
pub curves: Vec,
}

/// The Material trait is very configurable, but comes with sensible defaults for all methods.
/// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!
impl Material for CustomMaterial {
fn fragment_shader() -> ShaderRef {
"shaders/curves.wgsl".into()
}
}
```
``` wgsl
#import bevy_pbr::forward_io::VertexOutput;

struct Curve {
rotation: vec2,
center: vec2,
angle: vec2,
radius: f32,
thickness: f32,
}

@group(2) @binding(2) var curves: array;

fn sd_arc(p_in: vec2, sc: vec2, ra: f32, rb: f32) -> f32 {
var p = p_in;
p.x = abs(p.x);
return select(
abs(length(p) - ra),
length(p - sc * ra),
sc.y * p.x > sc.x * p.y
) - rb;
}

@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4 {
var col: vec4;

for (var i = u32(0); i < arrayLength(&curves); i++) {
let rotation = mat2x2(curves[i].rotation.y, -curves[i].rotation.x, curves[i].rotation.x, curves[i].rotation.y);
let pos = (in.world_position.xz - curves[i].center) * rotation;
col += mix(vec4(0.0), vec4(1.0), step(sd_arc(pos, curves[i].angle, curves[i].radius, curves[i].thickness), curves[i].thickness));
}

return col;
}
```

## What went wrong

The buffer gives a size of 1 while the vec should be empty. I confirmed the Vec is empty with the println!() statement in the update_shader system. When checked with render doc I can see arrayLength(&curves) returns a 1 even when it shouldn't. The struct is filled with zeroes.
![afbeelding](https://github.com/bevyengine/bevy/assets/1730282/5705470b-779f-4cdd-a515-5fb0ebd328a7)

Once the first element has been filled the code works fantastically.

## Additional information
White background because of error resulting from 0 multiplication with rotation at the beginning.
![afbeelding](https://github.com/bevyengine/bevy/assets/1730282/bc2cbcc4-edcb-4492-b4a8-6cf1969342ba)

Once the first curve is in everything works fine
![afbeelding](https://github.com/bevyengine/bevy/assets/1730282/df19c888-61b7-4beb-92ee-1aaffa7985c8)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.