summaryrefslogtreecommitdiff
path: root/src/camera.rs
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2022-10-12 11:34:48 -0400
committerMicha White <botahamec@outlook.com>2022-10-12 11:34:48 -0400
commitd88e1dc1643c04377b685bc7409cb7bcbd82c019 (patch)
tree0399c7507960a320793db9ac75e29dc70b3b981d /src/camera.rs
parent76ed4fe2d3f96c1c25905875a0d5dacc5ff7ed8a (diff)
Shortened the Camera::new function
Diffstat (limited to 'src/camera.rs')
-rw-r--r--src/camera.rs73
1 files changed, 43 insertions, 30 deletions
diff --git a/src/camera.rs b/src/camera.rs
index a9bfa9a..3bd729d 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -19,43 +19,56 @@ fn inverse_aspect_ratio(width: u32, height: u32) -> f32 {
(height as f32) / (width as f32)
}
+fn create_buffer(device: &wgpu::Device) -> wgpu::Buffer {
+ device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("Camera Uniform"),
+ size: size_of::<CameraUniform>() as wgpu::BufferAddress,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ })
+}
+
+fn create_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
+ device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ label: Some("Camera Bind Group Layout"),
+ entries: &[wgpu::BindGroupLayoutEntry {
+ binding: 0,
+ visibility: wgpu::ShaderStages::VERTEX,
+ ty: wgpu::BindingType::Buffer {
+ ty: wgpu::BufferBindingType::Uniform,
+ has_dynamic_offset: false,
+ min_binding_size: None,
+ },
+ count: None,
+ }],
+ })
+}
+
+fn create_bind_group(
+ device: &wgpu::Device,
+ buffer: &wgpu::Buffer,
+ layout: &wgpu::BindGroupLayout,
+) -> wgpu::BindGroup {
+ device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some("Camera Bind Group"),
+ layout,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: buffer.as_entire_binding(),
+ }],
+ })
+}
+
impl Camera {
/// Create a new camera, with a position of (0, 0), and a zoom of 1.0
- // TODO this can still be a little smaller
pub(crate) fn new(
device: &wgpu::Device,
width: u32,
height: u32,
) -> (Self, wgpu::BindGroupLayout) {
- let buffer = device.create_buffer(&wgpu::BufferDescriptor {
- label: Some("Camera Uniform"),
- size: size_of::<CameraUniform>() as wgpu::BufferAddress,
- usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
- mapped_at_creation: false,
- });
-
- let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
- label: Some("Camera Bind Group Layout"),
- entries: &[wgpu::BindGroupLayoutEntry {
- binding: 0,
- visibility: wgpu::ShaderStages::VERTEX,
- ty: wgpu::BindingType::Buffer {
- ty: wgpu::BufferBindingType::Uniform,
- has_dynamic_offset: false,
- min_binding_size: None,
- },
- count: None,
- }],
- });
-
- let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("Camera Bind Group"),
- layout: &bind_group_layout,
- entries: &[wgpu::BindGroupEntry {
- binding: 0,
- resource: buffer.as_entire_binding(),
- }],
- });
+ let buffer = create_buffer(device);
+ let bind_group_layout = create_bind_group_layout(device);
+ let bind_group = create_bind_group(device, &buffer, &bind_group_layout);
(
Self {