use glam::Vec2;
use image::GenericImageView;
use wgpu::util::DeviceExt;
use crate::backend::RenderState;
pub struct TextureSet {
pub texture: wgpu::Texture,
pub texture_view: wgpu::TextureView,
}
impl TextureSet {
pub fn new(
RenderState { device, queue, .. }: &RenderState,
image: image::DynamicImage,
) -> Self {
let size = image.dimensions();
let data = image.to_rgba8();
let texture = device.create_texture_with_data(
queue,
&wgpu::TextureDescriptor {
size: wgpu::Extent3d {
width: size.0,
height: size.1,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
label: Some("texture"),
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
&data,
);
let texture_view = texture.create_view(&wgpu::TextureViewDescriptor::default());
TextureSet {
texture,
texture_view,
}
}
pub fn from_texture(texture: wgpu::Texture) -> Self {
TextureSet {
texture_view: texture.create_view(&wgpu::TextureViewDescriptor::default()),
texture,
}
}
pub fn size(&self) -> Vec2 {
Vec2::new(
self.texture.size().width as f32,
self.texture.size().height as f32,
)
}
}