use crate::utils::RenderError;
use super::pipeline::TARGET_FORMAT;
use super::readback::Readback;
pub(super) struct Target {
width: u32,
height: u32,
texture: wgpu::Texture,
view: wgpu::TextureView,
readback: Readback,
}
impl Target {
pub(super) fn new(device: &wgpu::Device, width: u32, height: u32) -> Self {
let extent = wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("ass-gpu-target"),
size: extent,
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());
let readback = Readback::new(device, width, height);
Self {
width,
height,
texture,
view,
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 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)
}
}