Path: blob/main/crates/bevy_post_process/src/auto_exposure/pipeline.rs
9421 views
use super::compensation_curve::{1AutoExposureCompensationCurve, AutoExposureCompensationCurveUniform,2};3use bevy_asset::{load_embedded_asset, prelude::*};4use bevy_ecs::prelude::*;5use bevy_image::Image;6use bevy_render::{7globals::GlobalsUniform,8render_resource::{binding_types::*, *},9view::ViewUniform,10};11use bevy_shader::Shader;12use bevy_utils::default;13use core::num::NonZero;1415#[derive(Resource)]16pub struct AutoExposurePipeline {17pub histogram_layout: BindGroupLayoutDescriptor,18pub histogram_shader: Handle<Shader>,19}2021#[derive(Component)]22pub struct ViewAutoExposurePipeline {23pub histogram_pipeline: CachedComputePipelineId,24pub mean_luminance_pipeline: CachedComputePipelineId,25pub compensation_curve: Handle<AutoExposureCompensationCurve>,26pub metering_mask: Handle<Image>,27}2829#[derive(ShaderType, Clone, Copy)]30pub struct AutoExposureUniform {31pub(super) min_log_lum: f32,32pub(super) inv_log_lum_range: f32,33pub(super) log_lum_range: f32,34pub(super) low_percent: f32,35pub(super) high_percent: f32,36pub(super) speed_up: f32,37pub(super) speed_down: f32,38pub(super) exponential_transition_distance: f32,39}4041#[derive(PartialEq, Eq, Hash, Clone)]42pub enum AutoExposurePass {43Histogram,44Average,45}4647pub const HISTOGRAM_BIN_COUNT: u64 = 64;4849pub fn init_auto_exposure_pipeline(mut commands: Commands, asset_server: Res<AssetServer>) {50commands.insert_resource(AutoExposurePipeline {51histogram_layout: BindGroupLayoutDescriptor::new(52"compute histogram bind group",53&BindGroupLayoutEntries::sequential(54ShaderStages::COMPUTE,55(56uniform_buffer::<GlobalsUniform>(false),57uniform_buffer::<AutoExposureUniform>(false),58texture_2d(TextureSampleType::Float { filterable: false }),59texture_2d(TextureSampleType::Float { filterable: false }),60texture_1d(TextureSampleType::Float { filterable: false }),61uniform_buffer::<AutoExposureCompensationCurveUniform>(false),62storage_buffer_sized(false, NonZero::<u64>::new(HISTOGRAM_BIN_COUNT * 4)),63storage_buffer_sized(false, NonZero::<u64>::new(4)),64storage_buffer::<ViewUniform>(true),65),66),67),68histogram_shader: load_embedded_asset!(asset_server.as_ref(), "auto_exposure.wgsl"),69});70}7172impl SpecializedComputePipeline for AutoExposurePipeline {73type Key = AutoExposurePass;7475fn specialize(&self, pass: AutoExposurePass) -> ComputePipelineDescriptor {76ComputePipelineDescriptor {77label: Some("luminance compute pipeline".into()),78layout: vec![self.histogram_layout.clone()],79shader: self.histogram_shader.clone(),80shader_defs: vec![],81entry_point: Some(match pass {82AutoExposurePass::Histogram => "compute_histogram".into(),83AutoExposurePass::Average => "compute_average".into(),84}),85..default()86}87}88}899091