use super::AssetCache;
use crate::coordinator::{Capacity, ViewerSession};
use indicatrix_net::{
SceneState,
messages::{ErrorMsg, RenderCapability, error_codes},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HdrRoute {
NotHdr,
Serve,
Refuse(ErrorMsg),
}
#[must_use]
pub fn hdr_route(
scene: &SceneState,
assets: Option<&AssetCache>,
session: Option<&ViewerSession>,
) -> HdrRoute {
if scene.hdr().is_none() {
return HdrRoute::NotHdr;
}
let Some(session) = session else {
return if assets.is_some() {
HdrRoute::Serve
} else {
refuse("this server does not render HDR environments (it keeps no asset cache)")
};
};
let coordinator = &session.coordinator;
if coordinator.assets().is_none() {
return refuse(
"this coordinator keeps no HDR asset cache (see INDICATRIX_ASSET_CACHE_DIR), so it cannot \
hold an HDR map for its render lanes",
);
}
let hdr_workers = coordinator
.registry()
.map_or(0, |registry| registry.capacity().hdr_workers);
if coordinator.own().is_some() || hdr_workers > 0 {
return HdrRoute::Serve;
}
refuse(
"no render lane of this coordinator renders HDR environments: it has no own lane (--render) and \
no joined worker with an asset cache",
)
}
#[must_use]
pub fn coordinator_advertises_hdr(
own: Option<&RenderCapability>,
workers: Capacity,
holds_assets: bool,
) -> bool {
holds_assets && (own.is_some_and(|own| own.hdr) || workers.hdr_workers > 0)
}
fn refuse(message: &str) -> HdrRoute {
HdrRoute::Refuse(ErrorMsg {
code: error_codes::UNSUPPORTED_REQUEST,
message: message.to_string(),
request_id: None,
})
}