use super::pipeline::TARGET_FORMAT;
use super::readback::Readback;
use crate::utils::RenderError;
pub(super) struct Layer {
width: u32,
height: u32,
texture: wgpu::Texture,
view: wgpu::TextureView,
bind_group: wgpu::BindGroup,
readback: Readback,
}
impl Layer {
pub(super) fn new(
device: &wgpu::Device,
tile_layout: &wgpu::BindGroupLayout,
width: u32,
height: u32,
) -> Self {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("ass-gpu-layer"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: TARGET_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("ass-gpu-layer-bind"),
layout: tile_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&view),
}],
});
let readback = Readback::new(device, width, height);
Self {
width,
height,
texture,
view,
bind_group,
readback,
}
}
pub(super) fn matches(&self, width: u32, height: u32) -> bool {
self.width == width && self.height == height
}
pub(super) fn view(&self) -> &wgpu::TextureView {
&self.view
}
pub(super) fn bind_group(&self) -> &wgpu::BindGroup {
&self.bind_group
}
pub(super) fn copy_into_readback(&self, encoder: &mut wgpu::CommandEncoder) {
self.readback.copy_from(encoder, &self.texture);
}
pub(super) fn read_back(&self, device: &wgpu::Device) -> Result<Vec<u8>, RenderError> {
self.readback.read(device)
}
}
pub(super) struct Screen {
width: u32,
height: u32,
view: wgpu::TextureView,
}
impl Screen {
pub(super) fn new(device: &wgpu::Device, width: u32, height: u32) -> Self {
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("ass-gpu-screen"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: TARGET_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Self {
width,
height,
view,
}
}
pub(super) fn matches(&self, width: u32, height: u32) -> bool {
self.width == width && self.height == height
}
pub(super) fn view(&self) -> &wgpu::TextureView {
&self.view
}
}