165 lines
5 KiB
Rust
165 lines
5 KiB
Rust
use anyhow::*;
|
|
use image::GenericImageView;
|
|
use std::path::Path;
|
|
use wgpu::util::DeviceExt;
|
|
|
|
pub struct Texture {
|
|
pub texture: wgpu::Texture,
|
|
pub view: wgpu::TextureView,
|
|
pub sampler: wgpu::Sampler,
|
|
}
|
|
|
|
impl Texture {
|
|
pub fn _from_bytes(
|
|
device: &wgpu::Device,
|
|
queue: &wgpu::Queue,
|
|
bytes: &[u8],
|
|
is_normal_map: bool,
|
|
label: &str,
|
|
) -> Result<Self> {
|
|
let img = image::load_from_memory(bytes)?;
|
|
Self::from_image(device, queue, &img, is_normal_map, Some(label))
|
|
}
|
|
|
|
pub fn from_image(
|
|
device: &wgpu::Device,
|
|
queue: &wgpu::Queue,
|
|
img: &image::DynamicImage,
|
|
is_normal_map: bool,
|
|
label: Option<&str>,
|
|
) -> Result<Self> {
|
|
let rgba = img.to_rgba();
|
|
|
|
let (width, height) = img.dimensions();
|
|
let size = wgpu::Extent3d {
|
|
width,
|
|
height,
|
|
depth: 1,
|
|
};
|
|
|
|
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
label,
|
|
size,
|
|
mip_level_count: 1,
|
|
sample_count: 1,
|
|
dimension: wgpu::TextureDimension::D2,
|
|
format: if is_normal_map {
|
|
wgpu::TextureFormat::Rgba8Unorm
|
|
} else {
|
|
wgpu::TextureFormat::Rgba8UnormSrgb
|
|
},
|
|
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
|
|
});
|
|
|
|
let buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
label: Some("texture_buffer"),
|
|
contents: &rgba,
|
|
usage: wgpu::BufferUsage::COPY_SRC,
|
|
});
|
|
|
|
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
|
label: Some("texture_buffer_copy_encoder"),
|
|
});
|
|
|
|
encoder.copy_buffer_to_texture(
|
|
wgpu::BufferCopyView {
|
|
buffer: &buffer,
|
|
layout: wgpu::TextureDataLayout {
|
|
offset: 0,
|
|
bytes_per_row: 4 * width,
|
|
rows_per_image: height,
|
|
},
|
|
},
|
|
wgpu::TextureCopyView {
|
|
texture: &texture,
|
|
mip_level: 0,
|
|
origin: wgpu::Origin3d::ZERO,
|
|
},
|
|
size,
|
|
);
|
|
|
|
let command = encoder.finish();
|
|
|
|
let view = texture.create_view(&Default::default());
|
|
|
|
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
|
label: Some("texture_sampler"),
|
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
|
mag_filter: wgpu::FilterMode::Linear,
|
|
min_filter: wgpu::FilterMode::Nearest,
|
|
mipmap_filter: wgpu::FilterMode::Nearest,
|
|
lod_min_clamp: -100.0,
|
|
lod_max_clamp: 100.0,
|
|
compare: None,
|
|
anisotropy_clamp: None,
|
|
});
|
|
|
|
queue.submit(Some(command));
|
|
|
|
Ok(Texture {
|
|
texture,
|
|
view,
|
|
sampler,
|
|
})
|
|
}
|
|
|
|
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
|
|
|
|
pub fn create_depth_texture(
|
|
device: &wgpu::Device,
|
|
sc_desc: &wgpu::SwapChainDescriptor,
|
|
label: &str,
|
|
) -> Self {
|
|
let size = wgpu::Extent3d {
|
|
width: sc_desc.width,
|
|
height: sc_desc.height,
|
|
depth: 1,
|
|
};
|
|
let desc = wgpu::TextureDescriptor {
|
|
label: Some(label),
|
|
size,
|
|
mip_level_count: 1,
|
|
sample_count: 1,
|
|
dimension: wgpu::TextureDimension::D2,
|
|
format: Self::DEPTH_FORMAT,
|
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::SAMPLED,
|
|
};
|
|
|
|
let texture = device.create_texture(&desc);
|
|
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
|
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
|
mag_filter: wgpu::FilterMode::Linear,
|
|
min_filter: wgpu::FilterMode::Linear,
|
|
mipmap_filter: wgpu::FilterMode::Nearest,
|
|
lod_min_clamp: -100.0,
|
|
lod_max_clamp: 100.0,
|
|
compare: Some(wgpu::CompareFunction::LessEqual),
|
|
..Default::default()
|
|
});
|
|
|
|
Self {
|
|
texture,
|
|
view,
|
|
sampler,
|
|
}
|
|
}
|
|
|
|
pub fn load<P: AsRef<Path>>(
|
|
device: &wgpu::Device,
|
|
queue: &wgpu::Queue,
|
|
path: P,
|
|
is_normal_map: bool,
|
|
) -> Result<Self> {
|
|
// Needed to appease the borrow checker
|
|
let path_copy = path.as_ref().to_path_buf();
|
|
let label = path_copy.to_str();
|
|
|
|
let img = image::open(path)?;
|
|
Self::from_image(device, queue, &img, is_normal_map, label)
|
|
}
|
|
}
|