summaryrefslogtreecommitdiff
path: root/src/gfx/util.rs
diff options
context:
space:
mode:
authorLizzy Fleckenstein <eliasfleckenstein@web.de>2023-02-28 18:14:06 +0100
committerLizzy Fleckenstein <eliasfleckenstein@web.de>2023-02-28 18:14:06 +0100
commita87186860ec602c19f0d11528bef2d5123bc7e48 (patch)
tree1a5bfc0b554cd0ed195b61b1bc19f2dd250f8c8c /src/gfx/util.rs
parent146702340fbad28f9146d75e298234f63c0d5033 (diff)
downloadmt_client-a87186860ec602c19f0d11528bef2d5123bc7e48.tar.xz
Basic map rendering
Diffstat (limited to 'src/gfx/util.rs')
-rw-r--r--src/gfx/util.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/gfx/util.rs b/src/gfx/util.rs
new file mode 100644
index 0000000..f93accd
--- /dev/null
+++ b/src/gfx/util.rs
@@ -0,0 +1,63 @@
+use cgmath::Matrix4;
+use wgpu::util::DeviceExt;
+
+pub struct MatrixUniform {
+ buffer: wgpu::Buffer,
+ pub bind_group: wgpu::BindGroup,
+}
+
+impl MatrixUniform {
+ pub fn new(
+ device: &wgpu::Device,
+ bind_group_layout: &wgpu::BindGroupLayout,
+ init: Matrix4<f32>,
+ name: &str,
+ writable: bool,
+ ) -> Self {
+ let uniform: [[f32; 4]; 4] = init.into();
+
+ let mut usage = wgpu::BufferUsages::UNIFORM;
+
+ if writable {
+ usage |= wgpu::BufferUsages::COPY_DST;
+ }
+
+ let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
+ label: Some(&format!("{name}.buffer")),
+ contents: bytemuck::cast_slice(&[uniform]),
+ usage,
+ });
+
+ let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
+ layout: bind_group_layout,
+ entries: &[wgpu::BindGroupEntry {
+ binding: 0,
+ resource: buffer.as_entire_binding(),
+ }],
+ label: Some(&format!("{name}.bind_group")),
+ });
+
+ Self { buffer, bind_group }
+ }
+
+ pub fn layout(device: &wgpu::Device, name: &str) -> wgpu::BindGroupLayout {
+ device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+ 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,
+ }],
+ label: Some(&format!("{name}.bind_group_layout")),
+ })
+ }
+
+ pub fn set(&self, queue: &wgpu::Queue, to: Matrix4<f32>) {
+ let uniform: [[f32; 4]; 4] = to.into();
+ queue.write_buffer(&self.buffer, 0, bytemuck::cast_slice(&[uniform]));
+ }
+}