use super::EmbedError;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum EmbedAlpha {
#[default]
Opaque,
Premultiplied,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EmbedColorSpace {
Srgb,
Linear,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct EmbedOptions {
pub format: wgpu::TextureFormat,
pub sample_count: u32,
pub depth_format: Option<wgpu::TextureFormat>,
pub alpha: EmbedAlpha,
}
impl EmbedOptions {
pub const fn new(format: wgpu::TextureFormat) -> Self {
Self {
format,
sample_count: 1,
depth_format: None,
alpha: EmbedAlpha::Opaque,
}
}
pub const fn sample_count(mut self, sample_count: u32) -> Self {
self.sample_count = sample_count;
self
}
pub const fn depth_format(mut self, depth_format: wgpu::TextureFormat) -> Self {
self.depth_format = Some(depth_format);
self
}
pub const fn alpha(mut self, alpha: EmbedAlpha) -> Self {
self.alpha = alpha;
self
}
pub fn attachment_format(&self) -> wgpu::TextureFormat {
self.format.add_srgb_suffix()
}
pub fn color_space(&self) -> EmbedColorSpace {
if self.attachment_format().is_srgb() {
EmbedColorSpace::Srgb
} else {
EmbedColorSpace::Linear
}
}
pub(super) fn validate(&self, features: wgpu::Features) -> Result<(), EmbedError> {
use wgpu::TextureFormat as F;
let blendable = match self.attachment_format() {
F::Rgba8UnormSrgb | F::Bgra8UnormSrgb | F::Rgba16Float | F::Rgb10a2Unorm => true,
F::Rgba32Float => features.contains(wgpu::Features::FLOAT32_BLENDABLE),
_ => false,
};
if !blendable {
return Err(EmbedError::InvalidOptions(
"format must be Rgba8Unorm[Srgb], Bgra8Unorm[Srgb], Rgba16Float, Rgb10a2Unorm \
or Rgba32Float with FLOAT32_BLENDABLE",
));
}
if !matches!(self.sample_count, 1 | 2 | 4 | 8 | 16) {
return Err(EmbedError::InvalidOptions(
"sample count must be 1, 2, 4, 8 or 16",
));
}
if self
.depth_format
.is_some_and(|f| !f.is_depth_stencil_format())
{
return Err(EmbedError::InvalidOptions(
"depth format must be a depth or stencil format",
));
}
Ok(())
}
}