Skip to content

Commit

Permalink
examples/snake can set grid square to change color from main
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Berg committed Jan 22, 2025
1 parent d6052ac commit 2afb7c6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
16 changes: 11 additions & 5 deletions examples/snake/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn main() !void {

var snake: [GRID_SIZE * GRID_SIZE]u32 = undefined;
for (0..snake.len) |i| snake[i] = @intFromBool(false);
snake[0] = @intFromBool(true);
snake[0] = @intFromBool(true); // set head to active

const snake_buffer = try device.CreateBuffer(&.{
.label = wgpu.StringView.fromSlice("snake buffer"),
Expand Down Expand Up @@ -116,9 +116,10 @@ pub fn main() !void {
.entryPoint = wgpu.StringView.fromSlice("vs_main"),
.bufferCount = 1,
.buffers = &[1]wgpu.VertexBufferLayout{
// grid?
wgpu.VertexBufferLayout{
.stepMode = .Vertex,
.arrayStride = 0,
.arrayStride = 2 * @sizeOf(f32),
.attributeCount = 1,
.attributes = &[1]wgpu.VertextAttribute{
wgpu.VertextAttribute{
Expand All @@ -127,7 +128,7 @@ pub fn main() !void {
.shaderLocation = 0
},
}
}
},
}
},
.multisample = .{
Expand Down Expand Up @@ -172,13 +173,18 @@ pub fn main() !void {
const bind_group = try device.CreateBindGroup(&.{
.label = wgpu.StringView.fromSlice("bind group"),
.layout = layout,
.entryCount = 1,
.entries = &[1]wgpu.BindGroup.Entry {
.entryCount = 2,
.entries = &[2]wgpu.BindGroup.Entry {
wgpu.BindGroup.Entry{
.binding = 0,
.buffer = uniform_buffer._impl,
.size = uniform_buffer.GetSize()
},
wgpu.BindGroup.Entry{
.binding = 1,
.buffer = snake_buffer._impl,
.size = snake_buffer.GetSize()
},
}
});
defer bind_group.Release();
Expand Down
38 changes: 31 additions & 7 deletions examples/snake/src/shaders/square.wgsl
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@

@binding(0) @group(0) var<uniform> grid: vec2f;
//@binding(1) @group(0) var<storage, read> snake: array<u32>;

// If buffer isn't used one get the following error
// https://www.reddit.com/r/wgpu/comments/xhqxrk/number_of_bindings_in_bind_group_descriptor_2/
//Number of bindings in bind group descriptor (2) does not match the number of bindings defined in the bind group layout (1)
@binding(1) @group(0) var<storage> snake: array<u32>;


struct VertOut {
@builtin(position) position: vec4f,
@location(0) color: vec3f
}

@vertex
fn vs_main(
@location(0) pos: vec2f,
@builtin(instance_index) instance: u32
) -> @builtin(position) vec4f {
) -> VertOut {
let i = f32(instance);



let cell = vec2f(i % grid.x, floor(i / grid.x));
let cell_offset = cell / grid * 2;
let grid_pos = (pos + 1) / grid - 1 + cell_offset;

return vec4f(grid_pos, 0, 1); // (X, Y, Z, W)
}
var output: VertOut;

output.position = vec4f(grid_pos, 0, 1);

@fragment
fn fs_main() -> @location(0) vec4f {
let dark_grey = vec3f(27, 27, 27);
let max = vec3f(255, 255, 255);
return vec4f(dark_grey / max, 1); // red

if (snake[instance] == 1) {
output.color = vec3f(1, 0, 0);
} else {
output.color = dark_grey / max;

}

return output; // (X, Y, Z, W)
}

@fragment
fn fs_main(@location(0) in_color: vec3f) -> @location(0) vec4f {
return vec4f(in_color, 1);
}

0 comments on commit 2afb7c6

Please sign in to comment.