texture freeing + render updates done a bit nicer
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::num::NonZero;
|
||||
|
||||
use crate::{
|
||||
Ui, UiRenderUpdates,
|
||||
Ui,
|
||||
render::{data::PrimitiveInstance, texture::GpuTextures, util::ArrBuf},
|
||||
};
|
||||
use data::WindowUniform;
|
||||
@@ -21,7 +21,6 @@ pub use primitive::*;
|
||||
const SHAPE_SHADER: &str = include_str!("./shader.wgsl");
|
||||
|
||||
pub struct UiRenderer {
|
||||
uniform_layout: BindGroupLayout,
|
||||
uniform_group: BindGroup,
|
||||
primitive_layout: BindGroupLayout,
|
||||
primitive_group: BindGroup,
|
||||
@@ -33,8 +32,6 @@ pub struct UiRenderer {
|
||||
window_buffer: Buffer,
|
||||
instance: ArrBuf<PrimitiveInstance>,
|
||||
primitives: PrimitiveBuffers,
|
||||
|
||||
limits: UiLimits,
|
||||
textures: GpuTextures,
|
||||
}
|
||||
|
||||
@@ -51,14 +48,15 @@ impl UiRenderer {
|
||||
pass.draw(0..4, 0..self.instance.len() as u32);
|
||||
}
|
||||
|
||||
pub fn update(&mut self, device: &Device, queue: &Queue, updates: UiRenderUpdates) {
|
||||
if let Some(primitives) = updates.primitives {
|
||||
self.instance.update(device, queue, &primitives.instances);
|
||||
self.primitives.update(device, queue, &primitives.data);
|
||||
pub fn update<Ctx>(&mut self, device: &Device, queue: &Queue, ui: &mut Ui<Ctx>) {
|
||||
if ui.primitives.updated {
|
||||
self.instance
|
||||
.update(device, queue, &ui.primitives.instances);
|
||||
self.primitives.update(device, queue, &ui.primitives.data);
|
||||
self.primitive_group =
|
||||
Self::primitive_group(device, &self.primitive_layout, self.primitives.buffers())
|
||||
}
|
||||
if self.textures.apply(updates.textures) {
|
||||
if self.textures.update(&mut ui.textures) {
|
||||
self.rsc_group = Self::rsc_group(device, &self.rsc_layout, &self.textures)
|
||||
}
|
||||
}
|
||||
@@ -179,7 +177,6 @@ impl UiRenderer {
|
||||
});
|
||||
|
||||
Self {
|
||||
uniform_layout,
|
||||
uniform_group,
|
||||
primitive_layout,
|
||||
primitive_group,
|
||||
@@ -189,7 +186,6 @@ impl UiRenderer {
|
||||
window_buffer,
|
||||
instance,
|
||||
primitives,
|
||||
limits,
|
||||
textures: tex_manager,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,20 @@ use crate::{
|
||||
use bytemuck::Pod;
|
||||
use wgpu::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Primitives {
|
||||
pub(super) instances: Vec<PrimitiveInstance>,
|
||||
pub(super) data: PrimitiveData,
|
||||
pub updated: bool,
|
||||
}
|
||||
|
||||
impl Default for Primitives {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
instances: Default::default(),
|
||||
data: Default::default(),
|
||||
updated: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Primitive: Pod {
|
||||
|
||||
@@ -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