fix: image appearing over alternate screen

This commit is contained in:
2026-07-04 21:18:12 +02:00
parent 96b85f0159
commit 04b6776ebf
7 changed files with 344 additions and 162 deletions
+121 -67
View File
@@ -4,7 +4,7 @@
//! supporting the Kitty Graphics Protocol for inline image display.
use std::collections::HashMap;
use crate::gpu_types::ImageUniforms;
use crate::gpu_types::{ImageUniforms, PaneId};
use crate::graphics::{ImageData, ImagePlacement, ImageStorage};
// ═══════════════════════════════════════════════════════════════════════════════
@@ -15,7 +15,6 @@ use crate::graphics::{ImageData, ImagePlacement, ImageStorage};
pub struct GpuImage {
pub texture: wgpu::Texture,
pub view: wgpu::TextureView,
pub uniform_buffer: wgpu::Buffer,
pub bind_group: wgpu::BindGroup,
pub width: u32,
pub height: u32,
@@ -28,14 +27,23 @@ pub struct GpuImage {
/// Manages GPU resources for image rendering.
/// Handles uploading, caching, and preparing images for rendering.
pub struct ImageRenderer {
/// Bind group layout for image rendering.
bind_group_layout: wgpu::BindGroupLayout,
/// Bind group layout for uniforms.
uniform_layout: wgpu::BindGroupLayout,
/// Bind group layout for textures.
texture_layout: wgpu::BindGroupLayout,
/// Sampler for image textures.
sampler: wgpu::Sampler,
/// Cached GPU textures for images, keyed by image ID.
textures: HashMap<u32, GpuImage>,
/// Cached GPU textures for images, keyed by (pane_id, image_id).
textures: HashMap<(PaneId, u32), GpuImage>,
/// Global uniform buffer for image renders.
pub uniform_buffer: wgpu::Buffer,
/// Bind group for image uniforms.
uniform_bind_group: wgpu::BindGroup,
/// Minimum offset alignment for uniform buffers.
pub alignment: u64,
}
impl ImageRenderer {
/// Create a new ImageRenderer with the necessary GPU resources.
pub fn new(device: &wgpu::Device) -> Self {
@@ -51,20 +59,25 @@ impl ImageRenderer {
..Default::default()
});
// Create bind group layout for images
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Image Bind Group Layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
// Create bind group layout for uniforms (binding 0)
let uniform_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Image Uniform Layout"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: None,
},
count: None,
}],
});
// Create bind group layout for textures (binding 1, 2)
let texture_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Image Texture Layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
@@ -84,31 +97,71 @@ impl ImageRenderer {
],
});
// Create a large uniform buffer for all image renders in a frame
// Max 256 images per frame (65536 / 256)
let buffer_size = 65536;
let uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Image Uniform Buffer"),
size: buffer_size,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
// Create the uniform bind group
let uniform_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Image Uniform Bind Group"),
layout: &uniform_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: &uniform_buffer,
offset: 0,
size: std::num::NonZeroU64::new(std::mem::size_of::<ImageUniforms>() as u64),
}),
}],
});
let alignment = device.limits().min_uniform_buffer_offset_alignment as u64;
Self {
bind_group_layout,
uniform_layout,
texture_layout,
sampler,
textures: HashMap::new(),
uniform_buffer,
uniform_bind_group,
alignment,
}
}
/// Get the bind group layout for creating the image pipeline.
pub fn bind_group_layout(&self) -> &wgpu::BindGroupLayout {
&self.bind_group_layout
/// Get the uniform bind group layout.
pub fn uniform_layout(&self) -> &wgpu::BindGroupLayout {
&self.uniform_layout
}
/// Get the texture bind group layout.
pub fn texture_layout(&self) -> &wgpu::BindGroupLayout {
&self.texture_layout
}
/// Get the uniform bind group.
pub fn uniform_bind_group(&self) -> &wgpu::BindGroup {
&self.uniform_bind_group
}
/// Get a GPU image by ID.
pub fn get(&self, image_id: &u32) -> Option<&GpuImage> {
self.textures.get(image_id)
pub fn get(&self, pane_id: PaneId, image_id: &u32) -> Option<&GpuImage> {
self.textures.get(&(pane_id, *image_id))
}
/// Upload an image to the GPU, creating or updating its texture.
pub fn upload_image(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, image: &ImageData) {
log::debug!("upload_image: id={}, width={}, height={}, data_len={}", image.id, image.width, image.height, image.data.len());
pub fn upload_image(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, pane_id: PaneId, image: &ImageData) {
log::debug!("upload_image: pane_id={:?}, id={}, width={}, height={}, data_len={}", pane_id, image.id, image.width, image.height, image.data.len());
// Get current frame data (handles animation frames automatically)
let data = image.current_frame_data();
// Check if we already have this image
if let Some(existing) = self.textures.get(&image.id) {
if let Some(existing) = self.textures.get(&(pane_id, image.id)) {
if existing.width == image.width && existing.height == image.height {
// Same dimensions, just update the data
queue.write_texture(
@@ -137,7 +190,7 @@ impl ImageRenderer {
// Create new texture
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some(&format!("Image {}", image.id)),
label: Some(&format!("Image {} (pane {:?})", image.id, pane_id)),
size: wgpu::Extent3d {
width: image.width,
height: image.height,
@@ -174,23 +227,10 @@ impl ImageRenderer {
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
// Create per-image uniform buffer
let uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some(&format!("Image {} Uniform Buffer", image.id)),
size: std::mem::size_of::<ImageUniforms>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
// Create bind group for this image with its own uniform buffer
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some(&format!("Image {} Bind Group", image.id)),
layout: &self.bind_group_layout,
label: Some(&format!("Image {} (pane {:?}) Bind Group", image.id, pane_id)),
layout: &self.texture_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: uniform_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&view),
@@ -201,16 +241,16 @@ impl ImageRenderer {
},
],
});
self.textures.insert(image.id, GpuImage {
self.textures.insert((pane_id, image.id), GpuImage {
texture,
view,
uniform_buffer,
bind_group,
width: image.width,
height: image.height,
});
log::debug!(
"Uploaded image {} ({}x{}) to GPU",
image.id,
@@ -220,52 +260,61 @@ impl ImageRenderer {
}
/// Remove an image from the GPU.
pub fn remove_image(&mut self, image_id: u32) {
if self.textures.remove(&image_id).is_some() {
log::debug!("Removed image {} from GPU", image_id);
pub fn remove_image(&mut self, pane_id: PaneId, image_id: u32) {
if self.textures.remove(&(pane_id, image_id)).is_some() {
log::debug!("Removed image {} (pane {:?}) from GPU", image_id, pane_id);
}
}
/// Sync images from terminal's image storage to GPU.
/// Uploads new/changed images and removes deleted ones.
/// Also updates animation frames.
pub fn sync_images(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, storage: &mut ImageStorage) {
pub fn sync_images(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, pane_id: PaneId, storage: &mut ImageStorage) {
// Update animations and get list of changed image IDs
let changed_ids = storage.update_animations();
log::debug!("Sync images: changed_ids={:?}, dirty={}", changed_ids, storage.dirty);
log::debug!("Sync images: pane_id={:?}, changed_ids={:?}, dirty={}", pane_id, changed_ids, storage.dirty);
// Re-upload frames that changed due to animation
for id in &changed_ids {
if let Some(image) = storage.get_image(*id) {
self.upload_image(device, queue, image);
self.upload_image(device, queue, pane_id, image);
}
}
if !storage.dirty && changed_ids.is_empty() {
return;
}
// Upload all images (upload_image handles deduplication)
for image in storage.images().values() {
self.upload_image(device, queue, image);
self.upload_image(device, queue, pane_id, image);
}
storage.clear_dirty();
}
// Remove textures for deleted images
let current_ids: std::collections::HashSet<u32> = storage.images().keys().copied().collect();
let gpu_ids: Vec<u32> = self.textures.keys().copied().collect();
/// Remove images from the GPU that are not present in the provided set of active images.
/// active_images is a set of (pane_id, image_id) tuples.
pub fn gc_images(&mut self, active_images: &std::collections::HashSet<(PaneId, u32)>) {
let gpu_ids: Vec<(PaneId, u32)> = self.textures.keys().copied().collect();
let mut removed_count = 0;
for id in gpu_ids {
if !current_ids.contains(&id) {
self.remove_image(id);
if !active_images.contains(&id) {
self.remove_image(id.0, id.1);
removed_count += 1;
}
}
storage.clear_dirty();
if removed_count > 0 {
log::debug!("GC images: removed {} unused textures", removed_count);
}
}
/// Prepare image renders for a pane.
/// Returns a Vec of (image_id, uniforms) for deferred rendering.
pub fn prepare_image_renders(
&self,
pane_id: PaneId,
placements: &[ImagePlacement],
pane_x: f32,
pane_y: f32,
@@ -276,14 +325,19 @@ impl ImageRenderer {
scrollback_len: usize,
scroll_offset: usize,
visible_rows: usize,
dim_factor: f32,
) -> Vec<(u32, ImageUniforms)> {
log::debug!("prepare_image_renders: pane={:?}, placements={}, scrollback={}, offset={}, rows={}", pane_id, placements.len(), scrollback_len, scroll_offset, visible_rows);
let mut renders = Vec::new();
for placement in placements {
// Check if we have the GPU texture for this image
let gpu_image = match self.textures.get(&placement.image_id) {
let gpu_image = match self.textures.get(&(pane_id, placement.image_id)) {
Some(img) => img,
None => continue, // Skip if not uploaded yet
None => {
log::debug!("Image {} not found in GPU cache for pane {:?}", placement.image_id, pane_id);
continue;
},
};
// Convert absolute row to visible screen row
@@ -338,8 +392,8 @@ impl ImageRenderer {
src_y,
src_width,
src_height,
dim_factor,
_padding1: 0.0,
_padding2: 0.0,
};
renders.push((placement.image_id, uniforms));