use windows::Win32::Graphics::Dxgi::Common::{
DXGI_FORMAT, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_R8G8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM,
};
use super::card_frames::Slot;
pub(super) use crate::app::card_read::Readers;
use crate::{
app::card_read::Read,
images::{CardFrame, CardFrames},
};
fn kept_as(format: DXGI_FORMAT) -> Option<wgpu::TextureFormat> {
Some(match format {
DXGI_FORMAT_R8_UNORM => wgpu::TextureFormat::R8Unorm,
DXGI_FORMAT_R8G8_UNORM => wgpu::TextureFormat::Rg8Unorm,
DXGI_FORMAT_R8G8B8A8_UNORM => wgpu::TextureFormat::Rgba8Unorm,
DXGI_FORMAT_B8G8R8A8_UNORM => wgpu::TextureFormat::Bgra8Unorm,
_ => return None,
})
}
pub(super) struct Copied {
read: Read,
slot: Slot,
}
impl Copied {
pub(super) fn open(
device: &wgpu::Device,
frames: &CardFrames,
readers: &mut Readers,
bind: &impl Fn(&wgpu::TextureView, Option<&wgpu::TextureView>) -> wgpu::BindGroup,
) -> Option<Self> {
let read = Read::open(frames, readers)?;
let (_, wide, high) = *read.parts().first()?;
let textures = read
.parts()
.iter()
.map(|&(format, width, height)| {
Some(device.create_texture(&wgpu::TextureDescriptor {
label: Some("lurq_card_frame_copied"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: kept_as(format)?,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
}))
})
.collect::<Option<Vec<wgpu::Texture>>>()?;
let first_view = textures[0].create_view(&Default::default());
let second_view = textures.get(1).map(|texture| texture.create_view(&Default::default()));
Some(Self {
read,
slot: Slot {
bind_group: bind(&first_view, second_view.as_ref()),
first: first_view,
second: second_view,
size: (wide, high),
textures,
},
})
}
pub(super) fn slot(&self) -> &Slot {
&self.slot
}
pub(super) fn reached(&self) -> Option<u64> {
self.read.reached()
}
pub(super) fn is_copying(&self) -> bool {
self.read.is_copying()
}
pub(super) fn copy(&mut self, count: u64, frame: CardFrame) {
self.read.copy(count, frame);
}
pub(super) fn arrived(&mut self, queue: &wgpu::Queue) -> Option<(u64, CardFrame)> {
let textures = &self.slot.textures;
self.read.arrived(|part, rows, pitch| {
if let Some(texture) = textures.get(part) {
unsafe { send(queue, texture, rows, pitch) };
}
})
}
}
unsafe fn send(queue: &wgpu::Queue, texture: &wgpu::Texture, rows: *const u8, pitch: u32) {
let (wide, high) = (texture.width(), texture.height());
let row = wide * texture.format().block_copy_size(None).unwrap_or(4);
let bytes = pitch as usize * (high as usize - 1) + row as usize;
let bytes = unsafe { std::slice::from_raw_parts(rows, bytes) };
queue.write_texture(
texture.as_image_copy(),
bytes,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(pitch),
rows_per_image: None,
},
texture.size(),
);
}