//! Bevy auto exposure plugin and GPU capability probe.

use bevy::post_process::auto_exposure::{AutoExposure, AutoExposurePlugin};
use bevy::prelude::*;
use bevy::render::render_resource::DownlevelFlags;
use bevy::render::renderer::RenderAdapter;

pub struct AutoExposureRenderingPlugin;

impl Plugin for AutoExposureRenderingPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(AutoExposurePlugin);
    }
}

/// Returns whether the GPU supports Bevy's histogram auto exposure (compute shaders).
pub fn probe_auto_exposure_support(render_adapter: &RenderAdapter) -> bool {
    render_adapter
        .get_downlevel_capabilities()
        .flags
        .contains(DownlevelFlags::COMPUTE_SHADERS)
}

/// Builds an [`AutoExposure`] component from the resolved render profile.
pub fn auto_exposure_from_profile(profile: &settings::ActiveCameraRenderProfile) -> AutoExposure {
    AutoExposure {
        speed_brighten: profile.auto_exposure_speed_brighten,
        speed_darken: profile.auto_exposure_speed_darken,
        ..default()
    }
}
