texture freeing + render updates done a bit nicer
This commit is contained in:
@@ -1,28 +1,43 @@
|
||||
use image::{DynamicImage, EncodableLayout};
|
||||
use wgpu::{util::DeviceExt, *};
|
||||
|
||||
use crate::TextureUpdates;
|
||||
use crate::{TextureUpdate, Textures};
|
||||
|
||||
pub struct GpuTextures {
|
||||
device: Device,
|
||||
queue: Queue,
|
||||
views: Vec<TextureView>,
|
||||
samplers: Vec<Sampler>,
|
||||
null_view: TextureView,
|
||||
no_views: Vec<TextureView>,
|
||||
}
|
||||
|
||||
impl GpuTextures {
|
||||
pub fn apply(&mut self, updates: TextureUpdates) -> bool {
|
||||
if let Some(images) = updates.images {
|
||||
for img in images {
|
||||
self.add_view(img);
|
||||
pub fn update(&mut self, textures: &mut Textures) -> bool {
|
||||
let mut changed = false;
|
||||
for update in textures.updates() {
|
||||
changed = true;
|
||||
match update {
|
||||
TextureUpdate::Push(image) => self.push(image),
|
||||
TextureUpdate::Set(i, image) => self.set(i, image),
|
||||
TextureUpdate::Free(i) => self.free(i),
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
changed
|
||||
}
|
||||
fn add_view(&mut self, image: &DynamicImage) {
|
||||
fn set(&mut self, i: u32, image: &DynamicImage) {
|
||||
let view = self.create_view(image);
|
||||
self.views[i as usize] = view;
|
||||
}
|
||||
fn free(&mut self, i: u32) {
|
||||
self.views[i as usize] = self.null_view.clone();
|
||||
}
|
||||
fn push(&mut self, image: &DynamicImage) {
|
||||
let view = self.create_view(image);
|
||||
self.views.push(view);
|
||||
}
|
||||
|
||||
fn create_view(&self, image: &DynamicImage) -> TextureView {
|
||||
let image = image.to_rgba8();
|
||||
let (width, height) = image.dimensions();
|
||||
let texture = self.device.create_texture_with_data(
|
||||
@@ -44,17 +59,18 @@ impl GpuTextures {
|
||||
wgt::TextureDataOrder::MipMajor,
|
||||
image.as_bytes(),
|
||||
);
|
||||
let view = texture.create_view(&TextureViewDescriptor::default());
|
||||
self.views.push(view);
|
||||
texture.create_view(&TextureViewDescriptor::default())
|
||||
}
|
||||
|
||||
pub fn new(device: &Device, queue: &Queue) -> Self {
|
||||
let null_view = null_texture_view(device);
|
||||
Self {
|
||||
device: device.clone(),
|
||||
queue: queue.clone(),
|
||||
views: Vec::new(),
|
||||
samplers: vec![default_sampler(device)],
|
||||
no_views: vec![null_texture_view(device)],
|
||||
no_views: vec![null_view.clone()],
|
||||
null_view,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user