use mln_gfx::gpu::{ColorAttachment, DepthStencilAttachment, Gpu, GpuError, LoadOp, TargetFormats, TextureFormat};
use mln_gfx::scissor_rect::ScissorRect;
use mln_gfx::texture2d::{SamplerState, texture_format};
use mln_gfx::types::{TextureChannelDataType, TextureFilterType, TexturePixelType, TextureWrapType};
use mln_gfx::{Rgba, Size};
use crate::draw::context::Context;
use crate::draw::resources::{GpuTexture, Resources};
use crate::draw::store::{Shared, SharedTexture, TargetKey};
use crate::render::paint_parameters::{OpenPass, PaintParameters};
use crate::render::pass::RenderPass;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Target {
key: Shared<TargetKey>,
texture: SharedTexture,
}
pub(crate) struct TargetObjects {
size: Size,
clear: Rgba,
depth: Option<GpuTexture>,
holds: bool,
drawn: bool,
}
impl Target {
pub(crate) fn new(context: &mut Context, size: Size, type_: TextureChannelDataType, clear: Rgba) -> Target {
let format = texture_format(TexturePixelType::RGBA, context.render_target_type(type_));
let texture = context.add_texture_with(|texture| {
texture.set_size(size);
texture.set_format(format);
texture.set_render_target();
texture.set_sampler(SamplerState {
filter: TextureFilterType::Linear,
wrap_u: TextureWrapType::Clamp,
wrap_v: TextureWrapType::Clamp,
..SamplerState::default()
});
});
let key = context.add_target(TargetObjects { size, clear, depth: None, holds: false, drawn: false });
Target { key, texture }
}
pub(crate) fn key(&self) -> &Shared<TargetKey> {
&self.key
}
#[must_use]
pub fn texture(&self) -> &SharedTexture {
&self.texture
}
pub fn set_size(&self, res: &mut Resources<'_>, size: Size) {
let context = &mut *res.context;
let Some(objects) = context.target_mut(self).filter(|objects| objects.size != size) else {
return;
};
*objects = TargetObjects { size, clear: objects.clear, depth: None, holds: false, drawn: false };
if let Some(mut texture) = context.texture_mut(&self.texture) {
texture.set_size(size);
}
}
#[must_use]
pub fn holds(&self, res: &Resources<'_>) -> bool {
res.context.target(self).is_some_and(|objects| objects.holds)
}
pub(crate) fn render(
&self,
gpu: &mut dyn Gpu,
parameters: &mut PaintParameters<'_>,
mut draw: impl FnMut(&mut dyn Gpu, &mut PaintParameters<'_>) -> bool,
) {
let begun = self.pass(gpu, parameters.context).and_then(|pass| parameters.begin_pass(gpu, pass).map(|()| pass));
let pass = match begun {
Ok(pass) => pass,
Err(error) => {
parameters.context.fail(format!("Beginning a render target's pass failed: {error}"));
return;
}
};
let frame = (parameters.scissor_rect, parameters.pass, parameters.depth_range_size, parameters.current_layer);
parameters.scissor_rect = ScissorRect { x: 0, y: 0, width: pass.size.width, height: pass.size.height };
let num_sublayers = PaintParameters::NUM_SUBLAYERS as usize;
let mut drawn = true;
for pass in [RenderPass::Opaque, RenderPass::Translucent] {
parameters.pass = pass;
parameters.depth_range_size = 1.0 - (3 * num_sublayers) as f32 * PaintParameters::DEPTH_EPSILON;
parameters.current_layer = 0;
drawn &= draw(gpu, parameters);
}
let ended = parameters.end_pass(gpu);
if let Err(error) = &ended {
parameters.context.fail(format!("Ending a render target's pass failed: {error}"));
}
(parameters.scissor_rect, parameters.pass, parameters.depth_range_size, parameters.current_layer) = frame;
if let Some(objects) = parameters.context.target_mut(self) {
objects.drawn = ended.is_ok() && drawn;
}
}
fn pass(&self, gpu: &mut dyn Gpu, context: &mut Context) -> Result<OpenPass, GpuError> {
let (color, format, size) = self.color_attachment(gpu, context)?;
let depth_format = context.render_target_depth();
let objects = context.target(self).ok_or(GpuError::UnknownId("render target"))?;
if objects.depth.is_none()
&& let Some(depth_format) = depth_format
{
let depth = context.device().attachment(gpu, "render target depth", size, depth_format)?;
if let Some(objects) = context.target_mut(self) {
objects.depth = Some(depth);
}
}
let depth = context.target(self).and_then(|objects| objects.depth.as_ref()).map(GpuTexture::id);
Ok(OpenPass {
label: "render target",
color: Some(color),
depth_stencil: depth.map(|texture| DepthStencilAttachment {
texture,
depth: LoadOp::Clear(1.0),
stencil: LoadOp::Load,
}),
size,
targets: TargetFormats { color: Some(format), depth_stencil: depth.and(depth_format) },
})
}
}
impl Target {
pub(crate) fn color_attachment(
&self,
gpu: &mut dyn Gpu,
context: &mut Context,
) -> Result<(ColorAttachment, TextureFormat, Size), GpuError> {
let texture = context.create_texture(gpu, &self.texture)?;
let format = context.texture(&self.texture).ok_or(GpuError::UnknownId("texture"))?.format();
let objects = context.target(self).ok_or(GpuError::UnknownId("render target"))?;
Ok((ColorAttachment { texture, load: LoadOp::Clear(objects.clear) }, format, objects.size))
}
}
impl TargetObjects {
pub(crate) fn declare(&mut self) {
self.holds = true;
self.drawn = false;
}
pub(crate) fn end(&mut self, submitted: bool) {
self.holds = self.drawn && submitted;
self.drawn = false;
}
}