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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::marker;
use std::sync::Arc;

use web_sys::WebGl2RenderingContext as Gl;

use crate::image::format::{
    InternalFormat, Multisamplable, Multisample, RenderbufferFormat, TextureFormat,
};
use crate::image::renderbuffer::{Renderbuffer, RenderbufferData};
use crate::image::texture_2d::{
    Level as Texture2DLevel, LevelMut as Texture2DLevelMut, Texture2DData,
};
use crate::image::texture_2d_array::{
    LevelLayer as Texture2DArrayLevelLayer, LevelLayerMut as Texture2DArrayLevelLayerMut,
    Texture2DArrayData,
};
use crate::image::texture_3d::{
    LevelLayer as Texture3DLevelLayer, LevelLayerMut as Texture3DLevelLayerMut, Texture3DData,
};
use crate::image::texture_cube::{
    CubeFace, LevelFace as TextureCubeLevelFace, LevelFaceMut as TextureCubeLevelFaceMut,
    TextureCubeData,
};
use crate::util::JsId;

/// Trait implemented for image references that can be attached to a render target.
///
/// See also [RenderTargetDescriptor].
pub trait AsAttachment {
    /// The type of image storage format the image is stored in.
    type Format: InternalFormat;

    /// Converts the image reference into a render target attachment.
    fn as_attachment(&mut self) -> Attachment<Self::Format>;
}

impl<'a, T> AsAttachment for &'a mut T
where
    T: AsAttachment,
{
    type Format = T::Format;

    fn as_attachment(&mut self) -> Attachment<Self::Format> {
        (*self).as_attachment()
    }
}

impl<'a, F> AsAttachment for Texture2DLevelMut<'a, F>
where
    F: TextureFormat,
{
    type Format = F;

    fn as_attachment(&mut self) -> Attachment<Self::Format> {
        Attachment::from_texture_2d_level(&self)
    }
}

impl<'a, F> AsAttachment for Texture2DArrayLevelLayerMut<'a, F>
where
    F: TextureFormat,
{
    type Format = F;

    fn as_attachment(&mut self) -> Attachment<Self::Format> {
        Attachment::from_texture_2d_array_level_layer(&self)
    }
}

impl<'a, F> AsAttachment for Texture3DLevelLayerMut<'a, F>
where
    F: TextureFormat,
{
    type Format = F;

    fn as_attachment(&mut self) -> Attachment<Self::Format> {
        Attachment::from_texture_3d_level_layer(&self)
    }
}

impl<'a, F> AsAttachment for TextureCubeLevelFaceMut<'a, F>
where
    F: TextureFormat,
{
    type Format = F;

    fn as_attachment(&mut self) -> Attachment<Self::Format> {
        Attachment::from_texture_cube_level_face(&self)
    }
}

impl<F> AsAttachment for Renderbuffer<F>
where
    F: RenderbufferFormat + 'static,
{
    type Format = F;

    fn as_attachment(&mut self) -> Attachment<Self::Format> {
        Attachment::from_renderbuffer(self)
    }
}

/// Exclusive reference to an image that may be attached to a [RenderTarget].
pub struct Attachment<'a, F> {
    data: AttachmentData,
    marker: marker::PhantomData<&'a F>,
}

impl<'a, F> Attachment<'a, F> {
    pub(crate) fn from_texture_2d_level(image: &Texture2DLevel<'a, F>) -> Self
    where
        F: TextureFormat,
    {
        Attachment {
            data: AttachmentData {
                context_id: image.texture_data().context_id(),
                kind: AttachableImageRefKind::Texture2DLevel {
                    data: image.texture_data().clone(),
                    level: image.level() as u8,
                },
                width: image.width(),
                height: image.height(),
            },
            marker: marker::PhantomData,
        }
    }

    pub(crate) fn from_texture_2d_array_level_layer(image: &Texture2DArrayLevelLayer<'a, F>) -> Self
    where
        F: TextureFormat,
    {
        Attachment {
            data: AttachmentData {
                context_id: image.texture_data().context_id(),
                kind: AttachableImageRefKind::Texture2DArrayLevelLayer {
                    data: image.texture_data().clone(),
                    level: image.level() as u8,
                    layer: image.layer() as u16,
                },
                width: image.width(),
                height: image.height(),
            },
            marker: marker::PhantomData,
        }
    }

    pub(crate) fn from_texture_3d_level_layer(image: &Texture3DLevelLayer<'a, F>) -> Self
    where
        F: TextureFormat,
    {
        Attachment {
            data: AttachmentData {
                context_id: image.texture_data().context_id(),
                kind: AttachableImageRefKind::Texture3DLevelLayer {
                    data: image.texture_data().clone(),
                    level: image.level() as u8,
                    layer: image.layer() as u16,
                },
                width: image.width(),
                height: image.height(),
            },
            marker: marker::PhantomData,
        }
    }

    pub(crate) fn from_texture_cube_level_face(image: &TextureCubeLevelFace<'a, F>) -> Self
    where
        F: TextureFormat,
    {
        Attachment {
            data: AttachmentData {
                context_id: image.texture_data().context_id(),
                kind: AttachableImageRefKind::TextureCubeLevelFace {
                    data: image.texture_data().clone(),
                    level: image.level() as u8,
                    face: image.face(),
                },
                width: image.width(),
                height: image.height(),
            },
            marker: marker::PhantomData,
        }
    }

    pub(crate) fn from_renderbuffer(render_buffer: &'a Renderbuffer<F>) -> Self {
        Attachment {
            data: AttachmentData {
                context_id: render_buffer.data().context_id(),
                kind: AttachableImageRefKind::Renderbuffer {
                    data: render_buffer.data().clone(),
                },
                width: render_buffer.width(),
                height: render_buffer.height(),
            },
            marker: marker::PhantomData,
        }
    }

    pub(crate) fn into_data(self) -> AttachmentData {
        self.data
    }
}

/// Trait implemented for image references that can be attached to a multisample render target.
///
/// See also [MultisampleRenderTargetDescriptor].
pub trait AsMultisampleAttachment {
    /// The type of image storage format the image is stored in.
    type SampleFormat: InternalFormat + Multisamplable;

    /// Converts the image reference into a render target attachment.
    fn as_multisample_attachment(&mut self) -> MultisampleAttachment<Self::SampleFormat>;
}

impl<'a, T> AsMultisampleAttachment for &'a mut T
where
    T: AsMultisampleAttachment,
{
    type SampleFormat = T::SampleFormat;

    fn as_multisample_attachment(&mut self) -> MultisampleAttachment<Self::SampleFormat> {
        (*self).as_multisample_attachment()
    }
}

impl<F> AsMultisampleAttachment for Renderbuffer<Multisample<F>>
where
    F: RenderbufferFormat + Multisamplable + 'static,
{
    type SampleFormat = F;

    fn as_multisample_attachment(&mut self) -> MultisampleAttachment<Self::SampleFormat> {
        MultisampleAttachment::from_renderbuffer(self)
    }
}

/// Exclusive reference to a multisample image that may be attached to a [MultisampleRenderTarget].
pub struct MultisampleAttachment<'a, F>
where
    F: Multisamplable,
{
    internal: MultisampleAttachmentInternal<'a, F>,
}

impl<'a, F> MultisampleAttachment<'a, F>
where
    F: Multisamplable,
{
    pub(crate) fn from_renderbuffer(renderbuffer: &'a Renderbuffer<Multisample<F>>) -> Self {
        MultisampleAttachment {
            internal: renderbuffer.into(),
        }
    }

    pub fn samples(&self) -> u8 {
        self.internal.samples()
    }

    pub(crate) fn into_data(self) -> AttachmentData {
        self.internal.into_data()
    }
}

enum MultisampleAttachmentInternal<'a, F>
where
    F: Multisamplable,
{
    Renderbuffer(&'a Renderbuffer<Multisample<F>>),
}

impl<'a, F> MultisampleAttachmentInternal<'a, F>
where
    F: Multisamplable,
{
    fn samples(&self) -> u8 {
        match self {
            MultisampleAttachmentInternal::Renderbuffer(renderbufer) => renderbufer.samples(),
        }
    }

    fn into_data(self) -> AttachmentData {
        match self {
            MultisampleAttachmentInternal::Renderbuffer(renderbuffer) => AttachmentData {
                context_id: renderbuffer.data().context_id(),
                kind: AttachableImageRefKind::Renderbuffer {
                    data: renderbuffer.data().clone(),
                },
                width: renderbuffer.width(),
                height: renderbuffer.height(),
            },
        }
    }
}

impl<'a, F> From<&'a Renderbuffer<Multisample<F>>> for MultisampleAttachmentInternal<'a, F>
where
    F: Multisamplable,
{
    fn from(renderbuffer: &'a Renderbuffer<Multisample<F>>) -> Self {
        MultisampleAttachmentInternal::Renderbuffer(renderbuffer)
    }
}

#[derive(Clone, Hash, PartialEq)]
pub(crate) struct AttachmentData {
    pub(crate) context_id: u64,
    pub(crate) kind: AttachableImageRefKind,
    pub(crate) width: u32,
    pub(crate) height: u32,
}

impl AttachmentData {
    pub(crate) fn id(&self) -> JsId {
        match &self.kind {
            AttachableImageRefKind::Texture2DLevel { data, .. } => data.id().unwrap(),
            AttachableImageRefKind::Texture2DArrayLevelLayer { data, .. } => data.id().unwrap(),
            AttachableImageRefKind::Texture3DLevelLayer { data, .. } => data.id().unwrap(),
            AttachableImageRefKind::TextureCubeLevelFace { data, .. } => data.id().unwrap(),
            AttachableImageRefKind::Renderbuffer { data, .. } => data.id().unwrap(),
        }
    }

    pub(crate) fn attach(&self, gl: &Gl, target: u32, slot: u32) {
        unsafe {
            match &self.kind {
                AttachableImageRefKind::Texture2DLevel { data, level } => {
                    data.id().unwrap().with_value_unchecked(|texture_object| {
                        gl.framebuffer_texture_2d(
                            target,
                            slot,
                            Gl::TEXTURE_2D,
                            Some(&texture_object),
                            *level as i32,
                        );
                    });
                }
                AttachableImageRefKind::Texture2DArrayLevelLayer { data, level, layer } => {
                    data.id().unwrap().with_value_unchecked(|texture_object| {
                        gl.framebuffer_texture_layer(
                            target,
                            slot,
                            Some(&texture_object),
                            *level as i32,
                            *layer as i32,
                        );
                    });
                }
                AttachableImageRefKind::Texture3DLevelLayer { data, level, layer } => {
                    data.id().unwrap().with_value_unchecked(|texture_object| {
                        gl.framebuffer_texture_layer(
                            target,
                            slot,
                            Some(&texture_object),
                            *level as i32,
                            *layer as i32,
                        );
                    });
                }
                AttachableImageRefKind::TextureCubeLevelFace { data, level, face } => {
                    data.id().unwrap().with_value_unchecked(|texture_object| {
                        gl.framebuffer_texture_2d(
                            target,
                            slot,
                            face.id(),
                            Some(&texture_object),
                            *level as i32,
                        );
                    });
                }
                AttachableImageRefKind::Renderbuffer { data } => {
                    data.id()
                        .unwrap()
                        .with_value_unchecked(|renderbuffer_object| {
                            gl.framebuffer_renderbuffer(
                                target,
                                slot,
                                Gl::RENDERBUFFER,
                                Some(&renderbuffer_object),
                            );
                        });
                }
            }
        }
    }
}

#[derive(Clone, Hash, PartialEq)]
pub(crate) enum AttachableImageRefKind {
    Texture2DLevel {
        data: Arc<Texture2DData>,
        level: u8,
    },
    Texture2DArrayLevelLayer {
        data: Arc<Texture2DArrayData>,
        level: u8,
        layer: u16,
    },
    Texture3DLevelLayer {
        data: Arc<Texture3DData>,
        level: u8,
        layer: u16,
    },
    TextureCubeLevelFace {
        data: Arc<TextureCubeData>,
        level: u8,
        face: CubeFace,
    },
    Renderbuffer {
        data: Arc<RenderbufferData>,
    },
}