Skip to content

Commit

Permalink
Make triangle asymmetric to properly test shakiness
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 26, 2024
1 parent e9397fe commit 1278b37
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ block2 = "0.5.1"
objc2 = "0.5.2"
objc2-foundation = { version = "0.2.2", features = ["all", "block2"] }
objc2-quartz-core = { version = "0.2.2", features = ["all"] }
bytemuck = "1.16.1"

[target.'cfg(target_os = "macos")'.dependencies]
objc2-app-kit = { version = "0.2.2", features = ["all"] }
Expand Down
15 changes: 11 additions & 4 deletions src/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// Copied verbatim from `wgpu/examples/src/hello_triangle/shader.wgsl`
// Adapted from `wgpu/examples/src/hello_triangle/shader.wgsl`
@group(0)
@binding(0)
var<uniform> canvas_width: f32;

@vertex
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
let x = f32(i32(in_vertex_index) - 1);
let y = f32(i32(in_vertex_index & 1u) * 2 - 1);
return vec4<f32>(x, y, 0.0, 1.0);
var positions = array<vec4<f32>, 3>(
vec4<f32>(1.0 - 200.0 / canvas_width, 1.0, 0.0, 1.0), // Top vertex
vec4<f32>(-1.0, -1.0, 0.0, 1.0), // Bottom left vertex
vec4<f32>(1.0, -1.0, 0.0, 1.0) // Bottom right vertex
);
return positions[in_vertex_index];
}

@fragment
Expand Down
39 changes: 38 additions & 1 deletion src/wgpu_triangle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Adapted from `wgpu/examples/src/hello_triangle`.
use std::cell::RefCell;
use wgpu::util::DeviceExt;

use crate::frame_counter::FrameCounter;

Expand All @@ -13,6 +14,8 @@ pub struct Triangle<'window> {
queue: wgpu::Queue,
shader: wgpu::ShaderModule,
pipeline_layout: wgpu::PipelineLayout,
bind_group: wgpu::BindGroup,
uniform_buf: wgpu::Buffer,
render_pipeline: wgpu::RenderPipeline,
config: RefCell<wgpu::SurfaceConfiguration>,
frame_counter: FrameCounter,
Expand Down Expand Up @@ -58,12 +61,40 @@ impl<'window> Triangle<'window> {
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
});

let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(4),
},
count: None,
}],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[],
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});

let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Uniform Buffer"),
contents: bytemuck::cast_slice(&[width as f32]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});

let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buf.as_entire_binding(),
}],
label: None,
});

let swapchain_capabilities = surface.get_capabilities(&adapter);
let swapchain_format = swapchain_capabilities.formats[0];

Expand Down Expand Up @@ -106,13 +137,18 @@ impl<'window> Triangle<'window> {
queue,
shader,
pipeline_layout,
uniform_buf,
bind_group,
render_pipeline,
config: RefCell::new(config),
frame_counter: FrameCounter::new(),
}
}

pub fn resize(&self, width: u32, height: u32) {
self.queue
.write_buffer(&self.uniform_buf, 0, bytemuck::cast_slice(&[width as f32]));

let mut config = self.config.borrow_mut();
config.width = width;
config.height = height;
Expand Down Expand Up @@ -146,6 +182,7 @@ impl<'window> Triangle<'window> {
occlusion_query_set: None,
});
rpass.set_pipeline(&self.render_pipeline);
rpass.set_bind_group(0, &self.bind_group, &[]);
rpass.draw(0..3, 0..1);
}

Expand Down

0 comments on commit 1278b37

Please sign in to comment.