use crate::RenderConfig;
use std::sync::Arc;
use vulkano::format::Format;
use vulkano::device::Device;
use vulkano::render_pass::RenderPass;

pub fn create_render_pass(device: Arc<Device>, render_config: &RenderConfig, swapchain_format: Format) -> Arc<RenderPass> {
    if render_config.msaa_samples > 0 {
        Arc::new(vulkano::single_pass_renderpass!(
            device.clone(),
            attachments: {
                color: {
                    load: DontCare,
                    store: Store,
                    format: swapchain_format,
                    samples: 1,
                },
                intermediary: {
                    load: Clear,
                    store: DontCare,
                    format: swapchain_format,
                    samples: render_config.msaa_samples,
                },
                depth: {
                    load: Clear,
                    store: Store,
                    format: Format::D16Unorm,
                    samples: render_config.msaa_samples,
                    initial_layout: ImageLayout::Undefined,
                    final_layout: ImageLayout::DepthStencilAttachmentOptimal,
                }
            },
            pass: {
                color: [intermediary],
                depth_stencil: {depth},
                resolve: [color]
            }
        ).unwrap())
    } else {
        Arc::new(vulkano::single_pass_renderpass!(
            device.clone(),
            attachments: {
                color: {
                    load: Clear,
                    store: Store,
                    format: swapchain_format,
                    samples: 1,
                },
                depth: {
                    load: Clear,
                    store: Store,
                    format: Format::D16Unorm,
                    samples: 1,
                    initial_layout: ImageLayout::Undefined,
                    final_layout: ImageLayout::DepthStencilAttachmentOptimal,
                }
            },
            pass: {
                color: [color],
                depth_stencil: {depth}
            }
        ).unwrap())
    }
}