54 lines
1.5 KiB
Rust
54 lines
1.5 KiB
Rust
use std::sync::Arc;
|
|
|
|
use wgpu::{
|
|
Extent3d, ImageCopyTexture, ImageDataLayout, Origin3d, Queue, Texture, TextureDescriptor,
|
|
TextureFormat, TextureUsages,
|
|
};
|
|
|
|
#[derive(Debug)]
|
|
pub struct GameRenderer {
|
|
pub queue: Arc<Queue>,
|
|
pub eyes: Arc<Texture>,
|
|
}
|
|
|
|
impl GameRenderer {
|
|
pub fn render(&self, buffer: &[u8]) {
|
|
let texture = ImageCopyTexture {
|
|
texture: &self.eyes,
|
|
mip_level: 0,
|
|
origin: Origin3d::ZERO,
|
|
aspect: wgpu::TextureAspect::All,
|
|
};
|
|
let size = Extent3d {
|
|
width: 384,
|
|
height: 224,
|
|
depth_or_array_layers: 1,
|
|
};
|
|
let data_layout = ImageDataLayout {
|
|
offset: 0,
|
|
bytes_per_row: Some(384 * 2),
|
|
rows_per_image: Some(224),
|
|
};
|
|
self.queue.write_texture(texture, buffer, data_layout, size);
|
|
}
|
|
pub fn create_texture(device: &wgpu::Device, name: &str) -> Texture {
|
|
let desc = TextureDescriptor {
|
|
label: Some(name),
|
|
size: Extent3d {
|
|
width: 384,
|
|
height: 224,
|
|
depth_or_array_layers: 1,
|
|
},
|
|
mip_level_count: 1,
|
|
sample_count: 1,
|
|
dimension: wgpu::TextureDimension::D2,
|
|
format: TextureFormat::Rg8Unorm,
|
|
usage: TextureUsages::COPY_SRC
|
|
| TextureUsages::COPY_DST
|
|
| TextureUsages::TEXTURE_BINDING,
|
|
view_formats: &[TextureFormat::Rg8Unorm],
|
|
};
|
|
device.create_texture(&desc)
|
|
}
|
|
}
|