1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use crate::*;

/// A `<profile_*>` element.
#[derive(Clone, Debug)]
pub enum Profile {
    /// Opens a block of platform-independent declarations for the common,
    /// fixed-function shader.
    Common(ProfileCommon),
    /// Declares platform-specific data types and [`Technique`](TechniqueFx)s
    /// for the Cg language.
    CG(ProfileCG),
    /// Declares platform-specific data types and [`Technique`](TechniqueFx)s
    /// for OpenGL ES.
    GLES(ProfileGLES),
    /// Declares platform-specific data types and [`Technique`](TechniqueFx)s
    /// for OpenGL Shading Language.
    GLSL(ProfileGLSL),
}

impl From<ProfileCommon> for Profile {
    fn from(v: ProfileCommon) -> Self {
        Self::Common(v)
    }
}

impl From<ProfileCG> for Profile {
    fn from(v: ProfileCG) -> Self {
        Self::CG(v)
    }
}

impl From<ProfileGLES> for Profile {
    fn from(v: ProfileGLES) -> Self {
        Self::GLES(v)
    }
}

impl From<ProfileGLSL> for Profile {
    fn from(v: ProfileGLSL) -> Self {
        Self::GLSL(v)
    }
}

impl Profile {
    /// Parse a [`Profile`] from an XML element.
    pub fn parse(e: &Element) -> Result<Option<Self>> {
        Ok(Some(match e.name() {
            ProfileCommon::NAME => Self::Common(ProfileCommon::parse(e)?),
            ProfileCG::NAME => Self::CG(ProfileCG::parse(e)?),
            ProfileGLES::NAME => Self::GLES(ProfileGLES::parse(e)?),
            ProfileGLSL::NAME => Self::GLSL(ProfileGLSL::parse(e)?),
            _ => return Ok(None),
        }))
    }

    /// Variant extractor for [`ProfileCommon`].
    pub fn as_common(&self) -> Option<&ProfileCommon> {
        match self {
            Profile::Common(p) => Some(p),
            _ => None,
        }
    }
}

impl XNodeWrite for Profile {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        match self {
            Self::Common(e) => e.write_to(w),
            Self::CG(e) => e.write_to(w),
            Self::GLES(e) => e.write_to(w),
            Self::GLSL(e) => e.write_to(w),
        }
    }
}

/// Opens a block of platform-independent declarations for the common, fixed-function shader.
#[derive(Clone, Debug)]
pub struct ProfileCommon {
    /// Asset management information about this element.
    pub asset: Option<Box<Asset>>,
    /// Declares a standard COLLADA image resource.
    pub image: Vec<Image>,
    /// Creates a new parameter from a constrained set of
    /// types recognizable by all platforms, see [`ParamType`].
    pub new_param: Vec<NewParam>,
    /// Declares the only technique for this effect.
    pub technique: TechniqueFx<CommonData>,
    /// Provides arbitrary additional information about this element.
    pub extra: Vec<Extra>,
}

impl XNode for ProfileCommon {
    const NAME: &'static str = "profile_COMMON";
    fn parse(element: &Element) -> Result<Self> {
        debug_assert_eq!(element.name(), Self::NAME);
        let mut it = element.children().peekable();
        let asset = Asset::parse_opt_box(&mut it)?;
        let image_param = ImageParam::parse_list(&mut it)?;
        let mut image = vec![];
        let mut new_param = vec![];
        for ip in image_param {
            match ip {
                ImageParam::Image(e) => image.push(e),
                ImageParam::NewParam(e) => new_param.push(e),
            }
        }
        Ok(ProfileCommon {
            asset,
            image,
            new_param,
            technique: TechniqueFx::parse_one(&mut it)?,
            extra: Extra::parse_many(it)?,
        })
    }
}

impl XNodeWrite for ProfileCommon {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        let e = Self::elem().start(w)?;
        self.asset.write_to(w)?;
        self.image.write_to(w)?;
        self.new_param.write_to(w)?;
        self.technique.write_to(w)?;
        self.extra.write_to(w)?;
        e.end(w)
    }
}

impl ProfileCommon {
    /// Construct a new `ProfileCommon` from the `TechniqueFx` data.
    pub fn new(technique: TechniqueFx<CommonData>) -> Self {
        Self {
            asset: None,
            image: vec![],
            new_param: vec![],
            technique,
            extra: vec![],
        }
    }

    /// Get a parameter by name, looking in the parameters to the technique,
    /// the parameters to the profile, and finally the parameters to the parent effect.
    pub fn get_param<'a>(&'a self, parent: &'a Effect, sid: &str) -> Option<&'a NewParam> {
        for p in self.technique.data.image_param.iter().rev() {
            if let ImageParam::NewParam(p) = p {
                if p.sid == sid {
                    return Some(p);
                }
            }
        }
        for p in self.new_param.iter().rev() {
            if p.sid == sid {
                return Some(p);
            }
        }
        parent.get_param(sid)
    }
}

/// The extra data in a [`TechniqueFx`] as the child of [`ProfileCommon`].
#[derive(Clone, Default, Debug)]
pub struct CommonData {
    /// A list of [`Image`] and [`NewParam`] elements in no particular order.
    pub image_param: Vec<ImageParam>,
    /// A list of shader elements.
    pub shaders: Vec<Shader>,
}

impl ProfileData for CommonData {
    fn parse(it: &mut ElementIter<'_>) -> Result<Self> {
        Ok(CommonData {
            image_param: ImageParam::parse_list(it)?,
            shaders: parse_list_many(it, Shader::parse)?,
        })
    }
}

impl XNodeWrite for CommonData {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        self.image_param.write_to(w)?;
        self.shaders.write_to(w)
    }
}

impl CommonData {
    /// Construct a simple `CommonData` with one shader.
    pub fn shader(shader: impl Into<Shader>) -> Self {
        Self {
            image_param: vec![],
            shaders: vec![shader.into()],
        }
    }

    /// Run the function `f` on all arguments of type [`Texture`] in the profile.
    pub fn on_textures<'a, E>(
        &'a self,
        mut f: impl FnMut(&'a Texture) -> Result<(), E>,
    ) -> Result<(), E> {
        for s in &self.shaders {
            s.on_textures(&mut f)?
        }
        Ok(())
    }
}

/// The `<profile_CG>` element and its contents are unsupported
/// and represented here as a raw XML element.
#[derive(Clone, Debug)]
pub struct ProfileCG(pub Element); // TODO

impl XNode for ProfileCG {
    const NAME: &'static str = "profile_CG";
    fn parse(element: &Element) -> Result<Self> {
        Ok(ProfileCG(element.clone()))
    }
}

impl XNodeWrite for ProfileCG {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        XNodeWrite::write_to(&self.0, w)
    }
}

/// The `<profile_GLES>` element and its contents are unsupported
/// and represented here as a raw XML element.
#[derive(Clone, Debug)]
pub struct ProfileGLES(pub Element); // TODO

impl XNode for ProfileGLES {
    const NAME: &'static str = "profile_GLES";
    fn parse(element: &Element) -> Result<Self> {
        Ok(ProfileGLES(element.clone()))
    }
}

impl XNodeWrite for ProfileGLES {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        XNodeWrite::write_to(&self.0, w)
    }
}

/// The `<profile_GLSL>` element and its contents are unsupported
/// and represented here as a raw XML element.
#[derive(Clone, Debug)]
pub struct ProfileGLSL(pub Element); // TODO

impl XNode for ProfileGLSL {
    const NAME: &'static str = "profile_GLSL";
    fn parse(element: &Element) -> Result<Self> {
        Ok(ProfileGLSL(element.clone()))
    }
}

impl XNodeWrite for ProfileGLSL {
    fn write_to<W: Write>(&self, w: &mut XWriter<W>) -> Result<()> {
        XNodeWrite::write_to(&self.0, w)
    }
}