Skip to main content

fret_core/
materials.rs

1use crate::MaterialId;
2use serde::{Deserialize, Serialize};
3
4/// Framework-controlled material kinds intended for lightweight stylization (Tier B).
5///
6/// These are portable and backend-agnostic. Renderers may capability-gate registration.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub enum MaterialKind {
10    DotGrid,
11    Grid,
12    Checkerboard,
13    Stripe,
14    Noise,
15    Beam,
16    Sparkle,
17    ConicSweep,
18}
19
20/// Renderer-owned catalog textures that sampled materials may bind (ADR 0242).
21///
22/// These are framework-controlled identifiers, not backend handles.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
24#[serde(rename_all = "snake_case")]
25pub enum MaterialCatalogTextureKind {
26    /// A small baked noise texture (nominally “blue noise”).
27    BlueNoise64x64R8,
28    /// A repeated 8x8 Bayer dither matrix.
29    Bayer8x8R8,
30}
31
32/// Fixed and versioned material binding shapes.
33///
34/// v1 materials are params-only; v2 introduces a renderer-owned catalog texture bind.
35#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
36#[serde(rename_all = "snake_case")]
37pub enum MaterialBindingShape {
38    /// v1: fixed-size `MaterialParams` only.
39    #[default]
40    ParamsOnly,
41    /// v2: `MaterialParams` + one renderer-owned catalog texture + one sampler (ADR 0242).
42    ParamsPlusCatalogTexture { texture: MaterialCatalogTextureKind },
43}
44
45/// Backend-agnostic descriptor used to register a material pipeline.
46///
47/// v1: descriptors are intentionally small and fixed to keep the surface controlled.
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
49pub struct MaterialDescriptor {
50    pub kind: MaterialKind,
51    #[serde(default)]
52    pub binding: MaterialBindingShape,
53}
54
55impl MaterialDescriptor {
56    pub const fn new(kind: MaterialKind) -> Self {
57        Self {
58            kind,
59            binding: MaterialBindingShape::ParamsOnly,
60        }
61    }
62
63    pub const fn sampled_with_catalog_texture(
64        kind: MaterialKind,
65        texture: MaterialCatalogTextureKind,
66    ) -> Self {
67        Self {
68            kind,
69            binding: MaterialBindingShape::ParamsPlusCatalogTexture { texture },
70        }
71    }
72}
73
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum MaterialRegistrationError {
76    Unsupported,
77}
78
79/// Renderer-owned registry for framework-controlled materials.
80///
81/// This is exposed as a runtime service so components can obtain `MaterialId` handles without
82/// receiving backend handles or shader code.
83pub trait MaterialService {
84    fn register_material(
85        &mut self,
86        desc: MaterialDescriptor,
87    ) -> Result<MaterialId, MaterialRegistrationError>;
88
89    fn unregister_material(&mut self, id: MaterialId) -> bool;
90}