use super::{PassTarget, Renderer};
use std::collections::HashMap;
use crate::textures::{DepthTexture, Texture, TextureOwner, TextureType};
pub(super) struct ScreenReads {
width: u32,
height: u32,
color_format: wgpu::TextureFormat,
depth_format: Option<wgpu::TextureFormat>,
color: bool,
depth: bool,
pub(super) draw_index: usize,
pub(super) copies: Vec<(usize, FramebufferCopy)>,
}
#[derive(Default)]
pub(super) struct Destinations(HashMap<(bool, usize), (TextureOwner, wgpu::Texture)>);
impl Destinations {
fn owns(&self, key: (bool, usize), gpu: &wgpu::Texture) -> bool {
self.0.get(&key).is_some_and(|(_, made)| made == gpu)
}
fn insert(&mut self, key: (bool, usize), owner: TextureOwner, gpu: wgpu::Texture) {
self.0.retain(|_, (owner, _)| owner.strong_count() > 0);
self.0.insert(key, (owner, gpu));
}
}
pub(super) enum FramebufferCopy {
OpaqueFrame(Texture),
Color(wgpu::Texture),
Depth(wgpu::Texture),
}
impl ScreenReads {
pub(super) fn new(target: &PassTarget) -> Self {
Self {
width: target.width,
height: target.height,
color_format: target.color_format,
depth_format: target.depth_format,
color: target.color_texture.is_some(),
depth: target.depth_texture.is_some() && target.sample_count == 1,
draw_index: 0,
copies: Vec::new(),
}
}
}
impl Renderer {
pub(crate) fn copy_framebuffer_to_texture(&mut self, texture: &Texture) {
let (width, height, format, copy) = match &self.screen_reads {
Some(reads) => (reads.width, reads.height, reads.color_format, reads.color),
None => (1, 1, wgpu::TextureFormat::Rgba8Unorm, false),
};
let current = texture
.has_gpu()
.then(|| texture.with_gpu(|gpu| gpu.clone()))
.filter(|gpu| gpu.width() == width && gpu.height() == height && gpu.format() == format)
.filter(|gpu| self.screen_read_textures.owns((false, texture.id()), gpu));
let gpu = match current {
Some(gpu) => gpu,
None => {
let gpu = self.device.create_texture(&wgpu::TextureDescriptor {
label: Some("three-rs FramebufferTexture"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
texture.set_size(width, height);
texture.set_format(format);
texture.set_gpu(gpu.clone());
self.screen_read_textures.insert(
(false, texture.id()),
texture.owner(),
gpu.clone(),
);
gpu
}
};
if let Some(reads) = self.screen_reads.as_mut().filter(|_| copy) {
reads
.copies
.push((reads.draw_index, FramebufferCopy::Color(gpu)));
}
}
pub(crate) fn copy_framebuffer_to_depth_texture(&mut self, depth: &DepthTexture) {
let (width, height, format, copy) = match &self.screen_reads {
Some(reads) => (
reads.width,
reads.height,
reads
.depth_format
.unwrap_or(wgpu::TextureFormat::Depth24Plus),
reads.depth,
),
None => (1, 1, wgpu::TextureFormat::Depth24Plus, false),
};
let current = depth
.inner()
.borrow()
.gpu
.clone()
.filter(|gpu| gpu.width() == width && gpu.height() == height && gpu.format() == format)
.filter(|gpu| self.screen_read_textures.owns((true, depth.id()), gpu));
let gpu = match current {
Some(gpu) => gpu,
None => {
let gpu = self.device.create_texture(&wgpu::TextureDescriptor {
label: Some("three-rs viewport DepthTexture"),
size: wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
let texture_type = match format {
wgpu::TextureFormat::Depth32Float => TextureType::Float,
_ => TextureType::UnsignedInt,
};
depth
.set_type(texture_type)
.expect("three-rs: a depth format maps to a depth type");
let mut inner = depth.inner().borrow_mut();
inner.width = width;
inner.height = height;
inner.gpu = Some(gpu.clone());
drop(inner);
self.screen_read_textures
.insert((true, depth.id()), depth.owner(), gpu.clone());
gpu
}
};
if let Some(reads) = self.screen_reads.as_mut().filter(|_| copy) {
reads
.copies
.push((reads.draw_index, FramebufferCopy::Depth(gpu)));
}
}
pub(super) fn run_framebuffer_copies<'a>(
&mut self,
target: &PassTarget,
copies: impl Iterator<Item = &'a FramebufferCopy>,
) {
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("three-rs copyFramebufferToTexture"),
});
let mut opaque_frames = Vec::new();
for copy in copies {
let (source, destination) = match copy {
FramebufferCopy::OpaqueFrame(texture) => {
opaque_frames.push(texture.clone());
continue;
}
FramebufferCopy::Color(destination) => (target.color_texture.as_ref(), destination),
FramebufferCopy::Depth(destination) => (target.depth_texture.as_ref(), destination),
};
let Some(source) = source else { continue };
if source.width() != destination.width()
|| source.height() != destination.height()
|| source.format() != destination.format()
{
continue;
}
encoder.copy_texture_to_texture(
source.as_image_copy(),
destination.as_image_copy(),
wgpu::Extent3d {
width: source.width(),
height: source.height(),
depth_or_array_layers: 1,
},
);
}
self.queue.submit(Some(encoder.finish()));
if !opaque_frames.is_empty() {
let source = target.color_texture.clone().expect(
"three-rs: a transmissive split only happens on a target with a copy source",
);
for texture in &opaque_frames {
self.copy_framebuffer_to_opaque_frame(&source, texture);
}
}
}
}