use core::fmt;
use crcbl_core::Handle;
use crate::{BackendKind, Features, HalError};
#[derive(Debug)]
pub enum ShaderModule {}
pub type ShaderModuleHandle = Handle<ShaderModule>;
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ShaderStages: u32 {
const VERTEX = 1 << 0;
const FRAGMENT = 1 << 1;
const COMPUTE = 1 << 2;
const MESH = 1 << 3;
const TASK = 1 << 4;
const GRAPHICS = Self::VERTEX.bits() | Self::FRAGMENT.bits();
const ALL = Self::GRAPHICS.bits() | Self::COMPUTE.bits();
}
}
impl ShaderStages {
pub fn check_supported(self, features: Features, backend: BackendKind) -> Result<(), HalError> {
for (stage, required, what) in [
(
Self::MESH,
Features::MESH_SHADER,
"ShaderStages::MESH on a device without MESH_SHADER",
),
(
Self::TASK,
Features::TASK_SHADER,
"ShaderStages::TASK on a device without TASK_SHADER",
),
] {
if self.contains(stage) && !features.contains(required) {
return Err(HalError::Unsupported { backend, what });
}
}
Ok(())
}
}
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ShaderSources: u32 {
const SPIRV = 1 << 0;
const WGSL = 1 << 1;
const MSL = 1 << 2;
const DXIL = 1 << 3;
}
}
impl fmt::Display for ShaderSources {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut first = true;
for (bit, name) in [
(Self::SPIRV, "SPIR-V"),
(Self::WGSL, "WGSL"),
(Self::MSL, "MSL"),
(Self::DXIL, "DXIL"),
] {
if self.contains(bit) {
if !first {
f.write_str(" and ")?;
}
f.write_str(name)?;
first = false;
}
}
if first {
f.write_str("nothing")
} else {
Ok(())
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ShaderModuleDesc<'a> {
pub label: Option<&'a str>,
pub spirv: &'a [u32],
pub wgsl: Option<&'a str>,
pub msl: Option<&'a str>,
pub dxil: &'a [(&'a str, &'a [u8])],
}
impl ShaderModuleDesc<'_> {
#[must_use]
pub fn provided(&self) -> ShaderSources {
let mut sources = ShaderSources::empty();
if !self.spirv.is_empty() {
sources |= ShaderSources::SPIRV;
}
if self.wgsl.is_some() {
sources |= ShaderSources::WGSL;
}
if self.msl.is_some() {
sources |= ShaderSources::MSL;
}
if !self.dxil.is_empty() {
sources |= ShaderSources::DXIL;
}
sources
}
#[must_use]
pub fn unusable(&self, accepted: ShaderSources) -> HalError {
HalError::ShaderCompilation(format!(
"shader module `{label}` was given {given}, but this backend can only compile \
{accepted}",
label = self.label.unwrap_or("<unlabelled>"),
given = self.provided(),
))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ShaderEntry<'a> {
pub module: ShaderModuleHandle,
pub entry_point: &'a str,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stage_groups_are_unions_of_their_members() {
assert!(ShaderStages::GRAPHICS.contains(ShaderStages::VERTEX));
assert!(ShaderStages::GRAPHICS.contains(ShaderStages::FRAGMENT));
assert!(!ShaderStages::GRAPHICS.contains(ShaderStages::COMPUTE));
assert!(ShaderStages::ALL.contains(ShaderStages::GRAPHICS));
assert!(ShaderStages::ALL.contains(ShaderStages::COMPUTE));
}
#[test]
fn the_mesh_stages_are_in_no_composite() {
for composite in [ShaderStages::GRAPHICS, ShaderStages::ALL] {
assert!(!composite.intersects(ShaderStages::MESH | ShaderStages::TASK));
}
assert!(ShaderStages::MESH.intersects(ShaderStages::MESH | ShaderStages::TASK));
assert_ne!(ShaderStages::MESH, ShaderStages::TASK);
}
#[test]
fn the_gated_stages_need_their_own_feature() {
let backend = crate::BackendKind::Null;
for (stage, feature, other) in [
(
ShaderStages::MESH,
Features::MESH_SHADER,
Features::TASK_SHADER,
),
(
ShaderStages::TASK,
Features::TASK_SHADER,
Features::MESH_SHADER,
),
] {
stage
.check_supported(feature, backend)
.expect("the stage's own feature is what admits it");
let HalError::Unsupported { what, .. } = stage
.check_supported(other, backend)
.expect_err("the other mesh flag is not this stage's")
else {
panic!("a missing capability is an Unsupported, not a generic failure");
};
assert!(what.contains("without"), "{what}");
}
ShaderStages::ALL
.check_supported(Features::empty(), backend)
.expect("no composite carries a gated stage");
ShaderStages::empty()
.check_supported(Features::empty(), backend)
.expect("an empty stage set names nothing to refuse");
}
#[test]
fn a_stage_set_naming_both_names_the_one_that_is_missing() {
let both = ShaderStages::MESH | ShaderStages::TASK;
let HalError::Unsupported { what, .. } = both
.check_supported(Features::MESH_SHADER, crate::BackendKind::Null)
.expect_err("this device has the mesh stage and not the task stage")
else {
panic!("a missing capability is an Unsupported");
};
assert_eq!(what, "ShaderStages::TASK on a device without TASK_SHADER");
both.check_supported(
Features::MESH_SHADER | Features::TASK_SHADER,
crate::BackendKind::Null,
)
.expect("both features present");
}
const MAGIC: u32 = 0x0723_0203;
#[test]
fn provided_reports_exactly_the_formats_that_are_present() {
let spirv = [MAGIC, 0x0001_0600, 0, 0, 0];
assert_eq!(
ShaderModuleDesc::default().provided(),
ShaderSources::empty()
);
assert_eq!(
ShaderModuleDesc {
spirv: &spirv,
..ShaderModuleDesc::default()
}
.provided(),
ShaderSources::SPIRV
);
assert_eq!(
ShaderModuleDesc {
wgsl: Some("@fragment fn main() {}"),
..ShaderModuleDesc::default()
}
.provided(),
ShaderSources::WGSL
);
assert_eq!(
ShaderModuleDesc {
msl: Some("[[fragment]] float4 main() { return 0; }"),
..ShaderModuleDesc::default()
}
.provided(),
ShaderSources::MSL
);
assert_eq!(
ShaderModuleDesc {
dxil: &[("mainVs", b"DXBC")],
..ShaderModuleDesc::default()
}
.provided(),
ShaderSources::DXIL
);
assert_eq!(
ShaderModuleDesc {
spirv: &spirv,
wgsl: Some("@fragment fn main() {}"),
msl: Some("[[fragment]] float4 main() { return 0; }"),
dxil: &[("mainVs", b"DXBC"), ("mainPs", b"DXBC")],
..ShaderModuleDesc::default()
}
.provided(),
ShaderSources::all(),
"a module carrying a container for each of its two entry points \
offers DXIL once, not twice"
);
}
#[test]
fn an_empty_dxil_blob_is_present_not_absent() {
assert_eq!(
ShaderModuleDesc {
dxil: &[("mainVs", b"")],
..ShaderModuleDesc::default()
}
.provided(),
ShaderSources::DXIL
);
assert_eq!(
ShaderModuleDesc::default().provided(),
ShaderSources::empty(),
"the default must still report nothing, or the check above is vacuous"
);
}
#[test]
fn an_empty_text_artifact_is_present_not_absent() {
assert_eq!(
ShaderModuleDesc {
wgsl: Some(""),
..ShaderModuleDesc::default()
}
.provided(),
ShaderSources::WGSL
);
assert_eq!(
ShaderModuleDesc {
msl: Some(""),
..ShaderModuleDesc::default()
}
.provided(),
ShaderSources::MSL
);
}
#[test]
fn source_sets_name_themselves_in_a_sentence() {
assert_eq!(ShaderSources::empty().to_string(), "nothing");
assert_eq!(ShaderSources::SPIRV.to_string(), "SPIR-V");
assert_eq!(ShaderSources::WGSL.to_string(), "WGSL");
assert_eq!(ShaderSources::MSL.to_string(), "MSL");
assert_eq!(ShaderSources::DXIL.to_string(), "DXIL");
assert_eq!(
(ShaderSources::SPIRV | ShaderSources::WGSL).to_string(),
"SPIR-V and WGSL"
);
assert_eq!(
ShaderSources::all().to_string(),
"SPIR-V and WGSL and MSL and DXIL",
"every bit must have a name in the sentence, or a backend's refusal \
would silently omit the format the caller actually supplied"
);
}
#[test]
fn the_unusable_error_names_the_gap() {
let desc = ShaderModuleDesc {
label: Some("mesh.slang"),
wgsl: Some("@fragment fn main() {}"),
..ShaderModuleDesc::default()
};
let HalError::ShaderCompilation(message) = desc.unusable(ShaderSources::SPIRV) else {
panic!("an unusable descriptor is a shader-compilation failure");
};
assert!(message.contains("mesh.slang"), "{message}");
assert!(message.contains("was given WGSL"), "{message}");
assert!(message.contains("only compile SPIR-V"), "{message}");
}
#[test]
fn a_descriptor_with_no_artifact_at_all_says_so() {
let HalError::ShaderCompilation(message) =
ShaderModuleDesc::default().unusable(ShaderSources::all())
else {
panic!("an unusable descriptor is a shader-compilation failure");
};
assert!(message.contains("was given nothing"), "{message}");
}
}