Skip to main content

fret_core/
effects.rs

1use crate::EffectId;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
5#[serde(rename_all = "snake_case")]
6pub enum CustomEffectProgramLanguage {
7    WgslUtf8,
8}
9
10/// Descriptor used to register a bounded custom effect (v1).
11///
12/// This is intentionally small and backend-agnostic. Backends may reject registration based on
13/// capability gating.
14#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
15pub struct CustomEffectDescriptorV1 {
16    pub language: CustomEffectProgramLanguage,
17    pub source: String,
18}
19
20impl CustomEffectDescriptorV1 {
21    pub fn wgsl_utf8(source: impl Into<String>) -> Self {
22        Self {
23            language: CustomEffectProgramLanguage::WgslUtf8,
24            source: source.into(),
25        }
26    }
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub enum CustomEffectRegistrationError {
31    Unsupported,
32    InvalidSource,
33}
34
35/// Descriptor used to register a bounded custom effect (v2).
36///
37/// v2 programs may reference additional renderer-provided bindings (e.g. a single user image input)
38/// via a versioned WGSL prelude, but remain fully backend-agnostic at the contract surface.
39#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
40pub struct CustomEffectDescriptorV2 {
41    pub language: CustomEffectProgramLanguage,
42    pub source: String,
43}
44
45impl CustomEffectDescriptorV2 {
46    pub fn wgsl_utf8(source: impl Into<String>) -> Self {
47        Self {
48            language: CustomEffectProgramLanguage::WgslUtf8,
49            source: source.into(),
50        }
51    }
52}
53
54/// Descriptor used to register a bounded custom effect (v3).
55///
56/// V3 programs may reference additional renderer-provided sources (e.g. `src_raw`, optional pyramid)
57/// via a versioned WGSL prelude, but remain fully backend-agnostic at the contract surface.
58#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
59pub struct CustomEffectDescriptorV3 {
60    pub language: CustomEffectProgramLanguage,
61    pub source: String,
62}
63
64impl CustomEffectDescriptorV3 {
65    pub fn wgsl_utf8(source: impl Into<String>) -> Self {
66        Self {
67            language: CustomEffectProgramLanguage::WgslUtf8,
68            source: source.into(),
69        }
70    }
71}
72
73/// Renderer-owned registry for bounded custom effects.
74///
75/// This mirrors the material registration pattern: callers obtain an `EffectId` handle without
76/// receiving backend handles. Backends may deterministically degrade unsupported effects.
77pub trait CustomEffectService {
78    fn register_custom_effect_v1(
79        &mut self,
80        desc: CustomEffectDescriptorV1,
81    ) -> Result<EffectId, CustomEffectRegistrationError>;
82
83    fn register_custom_effect_v2(
84        &mut self,
85        desc: CustomEffectDescriptorV2,
86    ) -> Result<EffectId, CustomEffectRegistrationError>;
87
88    fn register_custom_effect_v3(
89        &mut self,
90        desc: CustomEffectDescriptorV3,
91    ) -> Result<EffectId, CustomEffectRegistrationError>;
92
93    fn unregister_custom_effect(&mut self, id: EffectId) -> bool;
94}