[oden] Garbage assets, need to rewrite to IO

This commit is contained in:
John Doty 2023-06-27 17:16:37 -07:00
parent 75fcc427ac
commit 17805fa4a6
17 changed files with 240 additions and 94 deletions

View file

@ -1,4 +1,5 @@
use bytemuck;
use std::collections::HashMap;
use wgpu::util::DeviceExt;
use winit::{
event::*,
@ -65,7 +66,8 @@ struct State {
vertex_buffer: wgpu::Buffer,
max_vertices: usize,
diffuse_bind_group: wgpu::BindGroup,
sprite_bind_group_layout: wgpu::BindGroupLayout,
sprite_textures: HashMap<u32, wgpu::BindGroup>,
screen_uniform: ScreenUniforms,
screen_uniform_buffer: wgpu::Buffer,
@ -146,11 +148,12 @@ impl State {
};
surface.configure(&device, &config);
let diffuse_bytes = include_bytes!("happy-tree.png");
let diffuse_texture =
texture::Texture::from_bytes(&device, &queue, diffuse_bytes, "happy-tree.png").unwrap();
// TODO: DELETE THIS
// let diffuse_bytes = include_bytes!("happy-tree.png");
// let diffuse_texture =
// texture::Texture::from_bytes(&device, &queue, diffuse_bytes, "happy-tree.png").unwrap();
let texture_bind_group_layout =
let sprite_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutEntry {
@ -172,23 +175,24 @@ impl State {
count: None,
},
],
label: Some("texture_bind_group_layout"),
label: Some("sprite_bind_group_layout"),
});
let diffuse_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &texture_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
},
],
label: Some("diffuse_bind_group"),
});
// TODO: DELETE THIS
// let sprite_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
// layout: &sprite_bind_group_layout,
// entries: &[
// wgpu::BindGroupEntry {
// binding: 0,
// resource: wgpu::BindingResource::TextureView(&diffuse_texture.view),
// },
// wgpu::BindGroupEntry {
// binding: 1,
// resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler),
// },
// ],
// label: Some("diffuse_bind_group"),
// });
let screen_uniform = ScreenUniforms::new(size.width, size.height);
let screen_uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@ -229,10 +233,7 @@ impl State {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[
&texture_bind_group_layout,
&screen_uniform_bind_group_layout,
],
bind_group_layouts: &[&sprite_bind_group_layout, &screen_uniform_bind_group_layout],
push_constant_ranges: &[],
});
@ -294,7 +295,8 @@ impl State {
render_pipeline,
vertex_buffer,
max_vertices,
diffuse_bind_group,
sprite_bind_group_layout,
sprite_textures: HashMap::new(),
screen_uniform,
screen_uniform_buffer,
screen_uniform_bind_group,
@ -348,6 +350,31 @@ impl State {
color: Some(cc.color),
commands: Vec::new(),
}),
GraphicsCommand::CreateTexture(ct) => {
let texture = texture::Texture::from_image(
&self.device,
&self.queue,
&ct.image,
Some(&ct.label),
);
let sprite_bind_group =
self.device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &self.sprite_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&texture.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&texture.sampler),
},
],
label: Some(&ct.label),
});
self.sprite_textures.insert(ct.id, sprite_bind_group);
}
GraphicsCommand::EndFrame => (),
other => match passes.last_mut() {
Some(pass) => pass.commands.push(other),
@ -395,6 +422,7 @@ impl State {
depth_stencil_attachment: None,
});
let mut texture_id = None;
vertices.clear();
for command in pass.commands {
match command {
@ -428,19 +456,29 @@ impl State {
});
}
GraphicsCommand::Clear(_) => (), // Already handled
GraphicsCommand::EndFrame => (), // Should never appear
GraphicsCommand::UseTexture(id) => texture_id = Some(id),
GraphicsCommand::CreateTexture(_) => (), // Already handled
GraphicsCommand::Clear(_) => (), // Already handled
GraphicsCommand::EndFrame => (), // Should never appear
}
}
assert!(vertices.len() < self.max_vertices); // !
self.queue
.write_buffer(&self.vertex_buffer, 0, bytemuck::cast_slice(&vertices));
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
render_pass.set_bind_group(1, &self.screen_uniform_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.draw(0..(vertices.len() as u32), 0..1);
if let Some(id) = texture_id {
assert!(vertices.len() < self.max_vertices); // !
self.queue.write_buffer(
&self.vertex_buffer,
0,
bytemuck::cast_slice(&vertices),
);
render_pass.set_pipeline(&self.render_pipeline);
let bind_group = self.sprite_textures.get(&id).unwrap();
render_pass.set_bind_group(0, bind_group, &[]);
render_pass.set_bind_group(1, &self.screen_uniform_bind_group, &[]);
render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
render_pass.draw(0..(vertices.len() as u32), 0..1);
}
}
// Submit will accept anything that implements IntoIter