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
//! Extensions for [glutin](https://crates.io/crates/glutin) to initialize & update old school
//! [gfx](https://crates.io/crates/gfx). _An alternative to gfx_window_glutin_.
//!
//! # Example
//! ```no_run
//! type ColorFormat = gfx::format::Srgba8;
//! type DepthFormat = gfx::format::DepthStencil;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let event_loop = winit::event_loop::EventLoop::new()?;
//! let window_builder = winit::window::WindowBuilder::new();
//!
//! // Initialise winit window, glutin context & gfx views
//! let old_school_gfx_glutin_ext::Init {
//!     // winit window
//!     window,
//!     // glutin bits
//!     gl_config,
//!     gl_surface,
//!     gl_context,
//!     // gfx bits
//!     mut device,
//!     mut factory,
//!     mut color_view,
//!     mut depth_view,
//!     ..
//! } = old_school_gfx_glutin_ext::window_builder(&event_loop, window_builder)
//!     .build::<ColorFormat, DepthFormat>()?;
//!
//! # let new_size = winit::dpi::PhysicalSize::new(1, 1);
//! // Update gfx views, e.g. after a window resize
//! old_school_gfx_glutin_ext::resize_views(new_size, &mut color_view, &mut depth_view);
//! # Ok(()) }
//! ```
use gfx_core::{
    format::{ChannelType, DepthFormat, Format, RenderFormat},
    handle::{DepthStencilView, RawDepthStencilView, RawRenderTargetView, RenderTargetView},
    memory::Typed,
    texture,
};
use gfx_device_gl::Resources as R;
use glutin::{
    config::{ColorBufferType, ConfigTemplateBuilder},
    context::ContextAttributesBuilder,
    display::GetGlDisplay,
    prelude::{GlConfig, GlDisplay, NotCurrentGlContext},
    surface::{SurfaceAttributesBuilder, WindowSurface},
};
use glutin_winit::GlWindow;
use raw_window_handle::HasRawWindowHandle;
use std::{error::Error, ffi::CString};

/// Returns a builder for initialising a winit window, glutin context & gfx views.
pub fn window_builder<T: 'static>(
    event_loop: &winit::event_loop::EventLoop<T>,
    winit: winit::window::WindowBuilder,
) -> Builder<'_, T> {
    Builder {
        event_loop,
        winit,
        surface_attrs: <_>::default(),
        ctx_attrs: <_>::default(),
        config_attrs: <_>::default(),
        sample_number_pref: <_>::default(),
    }
}

/// Builder for initialising a winit window, glutin context & gfx views.
#[derive(Debug, Clone)]
pub struct Builder<'a, T: 'static> {
    event_loop: &'a winit::event_loop::EventLoop<T>,
    winit: winit::window::WindowBuilder,
    surface_attrs: Option<SurfaceAttributesBuilder<WindowSurface>>,
    ctx_attrs: ContextAttributesBuilder,
    config_attrs: ConfigTemplateBuilder,
    sample_number_pref: NumberOfSamples,
}

impl<T> Builder<'_, T> {
    /// Configure surface attributes.
    ///
    /// If not called glutin default settings are used.
    pub fn surface_attributes(
        mut self,
        surface_attrs: SurfaceAttributesBuilder<WindowSurface>,
    ) -> Self {
        self.surface_attrs = Some(surface_attrs);
        self
    }

    /// Configure context attributes.
    ///
    /// If not called glutin default settings are used.
    pub fn context_attributes(mut self, ctx_attrs: ContextAttributesBuilder) -> Self {
        self.ctx_attrs = ctx_attrs;
        self
    }

    /// Configure [`ConfigTemplateBuilder`].
    pub fn config_template(mut self, conf: ConfigTemplateBuilder) -> Self {
        self.config_attrs = conf;
        self
    }

    /// Configure [`NumberOfSamples`] preference.
    ///
    /// Default `0` / no samples.
    pub fn number_of_samples(mut self, pref: impl Into<NumberOfSamples>) -> Self {
        self.sample_number_pref = pref.into();
        self
    }

    /// Initialise a winit window, glutin context & gfx views.
    pub fn build<Color, Depth>(self) -> Result<Init<Color, Depth>, Box<dyn Error>>
    where
        Color: RenderFormat,
        Depth: DepthFormat,
    {
        self.build_raw(Color::get_format(), Depth::get_format())
            .map(|i| i.into_typed())
    }

    /// Initialise a winit window, glutin context & gfx views.
    pub fn build_raw(
        self,
        color_format: Format,
        depth_format: Format,
    ) -> Result<RawInit, Box<dyn Error>> {
        let Format(color_surface, color_channel) = color_format;
        let color_total_bits = color_surface.get_total_bits();
        let alpha_bits = color_surface.get_alpha_stencil_bits();
        let depth_total_bits = depth_format.0.get_total_bits();
        let stencil_bits = depth_format.0.get_alpha_stencil_bits();
        let srgb = color_channel == ChannelType::Srgb;
        let surface_attrs = self
            .surface_attrs
            .unwrap_or_else(|| SurfaceAttributesBuilder::new().with_srgb(srgb.then_some(true)));

        let config_attrs = self
            .config_attrs
            .with_alpha_size(alpha_bits)
            .with_depth_size(depth_total_bits - stencil_bits)
            .with_stencil_size(stencil_bits);

        let mut no_suitable_config = false;
        let (window, gl_config) = glutin_winit::DisplayBuilder::new()
            .with_window_builder(Some(self.winit))
            .build(self.event_loop, config_attrs, |configs| {
                let mut configs: Vec<_> = configs.collect();
                assert!(!configs.is_empty(), "no gl configs?");

                let best = self
                    .sample_number_pref
                    .find(configs.iter().enumerate().filter(|(_, c)| {
                        let color_bits = match c.color_buffer_type() {
                            None => 0,
                            Some(ColorBufferType::Luminance(s)) => s,
                            Some(ColorBufferType::Rgb {
                                r_size,
                                g_size,
                                b_size,
                            }) => r_size + g_size + b_size,
                        };

                        (!srgb || c.srgb_capable())
                            && color_bits == color_total_bits - alpha_bits
                            && c.alpha_size() == alpha_bits
                            && (c.depth_size() == depth_total_bits - stencil_bits
                                // workaround to allow depth_size=32 & stencil_size=8 on macos software renderer
                                || c.depth_size() == depth_total_bits)
                            && c.stencil_size() == stencil_bits
                    }));
                match best {
                    Some((idx, _)) => configs.swap_remove(idx),
                    None => {
                        no_suitable_config = true;
                        configs.swap_remove(0)
                    }
                }
            })?;

        if no_suitable_config {
            return Err("no suitable gl config found, color+depth not supported?".into());
        }

        let window = window.unwrap(); // set in display builder
        let raw_window_handle = window.raw_window_handle();
        let gl_display = gl_config.display();

        let (gl_surface, gl_context) = {
            let ctx_attrs = self.ctx_attrs.build(Some(raw_window_handle));
            let surface_attrs = window.build_surface_attributes(surface_attrs);
            let surface = unsafe { gl_display.create_window_surface(&gl_config, &surface_attrs)? };
            let context = unsafe { gl_display.create_context(&gl_config, &ctx_attrs)? }
                .make_current(&surface)?;
            (surface, context)
        };

        let (device, factory) =
            gfx_device_gl::create(|s| gl_display.get_proc_address(&CString::new(s).unwrap()) as _);

        let window_size = window.inner_size();
        let tex_dimensions = (
            window_size.width as _,
            window_size.height as _,
            1,
            gl_config.num_samples().into(),
        );
        let (color_view, depth_view) =
            gfx_device_gl::create_main_targets_raw(tex_dimensions, color_surface, depth_format.0);

        Ok(RawInit {
            window,
            gl_config,
            gl_surface,
            gl_context,
            device,
            factory,
            color_view,
            depth_view,
        })
    }
}

/// Initialised winit, glutin & gfx state.
#[non_exhaustive]
pub struct InitState<ColorView, DepthView> {
    // winit
    pub window: winit::window::Window,
    // glutin
    pub gl_config: glutin::config::Config,
    pub gl_surface: glutin::surface::Surface<WindowSurface>,
    pub gl_context: glutin::context::PossiblyCurrentContext,
    // gfx
    pub device: gfx_device_gl::Device,
    pub factory: gfx_device_gl::Factory,
    pub color_view: ColorView,
    pub depth_view: DepthView,
}

/// "Raw" initialised winit, glutin & gfx state.
pub type RawInit = InitState<RawRenderTargetView<R>, RawDepthStencilView<R>>;
/// Initialised winit, glutin & gfx state.
pub type Init<Color, Depth> = InitState<RenderTargetView<R, Color>, DepthStencilView<R, Depth>>;

impl RawInit {
    fn into_typed<Color: RenderFormat, Depth: DepthFormat>(self) -> Init<Color, Depth> {
        Init {
            window: self.window,
            gl_config: self.gl_config,
            gl_surface: self.gl_surface,
            gl_context: self.gl_context,
            device: self.device,
            factory: self.factory,
            color_view: Typed::new(self.color_view),
            depth_view: Typed::new(self.depth_view),
        }
    }
}

/// Recreate and replace gfx views if the dimensions have changed.
pub fn resize_views<Color: RenderFormat, Depth: DepthFormat>(
    new_size: winit::dpi::PhysicalSize<u32>,
    color_view: &mut RenderTargetView<R, Color>,
    depth_view: &mut DepthStencilView<R, Depth>,
) {
    if let Some((cv, dv)) = resized_views(new_size, color_view, depth_view) {
        *color_view = cv;
        *depth_view = dv;
    }
}

/// Return new gfx views if the dimensions have changed.
#[must_use]
pub fn resized_views<Color: RenderFormat, Depth: DepthFormat>(
    new_size: winit::dpi::PhysicalSize<u32>,
    color_view: &RenderTargetView<R, Color>,
    depth_view: &DepthStencilView<R, Depth>,
) -> Option<(RenderTargetView<R, Color>, DepthStencilView<R, Depth>)> {
    let old_dimensions = color_view.get_dimensions();
    debug_assert_eq!(old_dimensions, depth_view.get_dimensions());

    let (cv, dv) = resized_views_raw(
        new_size,
        old_dimensions,
        Color::get_format(),
        Depth::get_format(),
    )?;

    Some((Typed::new(cv), Typed::new(dv)))
}

/// Return new gfx views if the dimensions have changed.
#[must_use]
pub fn resized_views_raw(
    new_size: winit::dpi::PhysicalSize<u32>,
    old_dimensions: texture::Dimensions,
    color_fmt: Format,
    ds_fmt: Format,
) -> Option<(RawRenderTargetView<R>, RawDepthStencilView<R>)> {
    let new_dimensions = (
        new_size.width as _,
        new_size.height as _,
        old_dimensions.2,
        old_dimensions.3,
    );
    if old_dimensions == new_dimensions {
        return None;
    }
    Some(gfx_device_gl::create_main_targets_raw(
        new_dimensions,
        color_fmt.0,
        ds_fmt.0,
    ))
}

/// Preference for picking [`glutin::config::GlConfig::num_samples`].
#[derive(Debug, Clone, Copy)]
pub enum NumberOfSamples {
    /// Pick a config with the highest number of samples.
    Max,
    /// Pick a config with a specific number of samples.
    ///
    /// E.g. `Specific(0)` mean no multisamples.
    Specific(u8),
}

impl Default for NumberOfSamples {
    fn default() -> Self {
        Self::Specific(0)
    }
}

impl From<u8> for NumberOfSamples {
    fn from(val: u8) -> Self {
        Self::Specific(val)
    }
}

impl NumberOfSamples {
    fn find<'a>(
        self,
        mut configs: impl Iterator<Item = (usize, &'a glutin::config::Config)>,
    ) -> Option<(usize, &'a glutin::config::Config)> {
        match self {
            Self::Max => configs.max_by_key(|(_, c)| c.num_samples()),
            Self::Specific(n) => configs.find(|(_, c)| c.num_samples() == n),
        }
    }
}