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
#[deny(missing_docs)]
extern crate gfx_core as core;
extern crate gfx_device_gl as device_gl;
extern crate glutin;
use core::{format, handle, texture};
use core::memory::Typed;
use device_gl::Resources as R;
pub fn init<Cf, Df>(builder: glutin::WindowBuilder) ->
(glutin::Window, device_gl::Device, device_gl::Factory,
handle::RenderTargetView<R, Cf>, handle::DepthStencilView<R, Df>)
where
Cf: format::RenderFormat,
Df: format::DepthFormat,
{
let (window, device, factory, color_view, ds_view) = init_raw(builder, Cf::get_format(), Df::get_format());
(window, device, factory, Typed::new(color_view), Typed::new(ds_view))
}
pub fn init_existing<Cf, Df>(window: &glutin::Window) ->
(device_gl::Device, device_gl::Factory,
handle::RenderTargetView<R, Cf>, handle::DepthStencilView<R, Df>)
where
Cf: format::RenderFormat,
Df: format::DepthFormat,
{
let (device, factory, color_view, ds_view) = init_existing_raw(window, Cf::get_format(), Df::get_format());
(device, factory, Typed::new(color_view), Typed::new(ds_view))
}
fn get_window_dimensions(window: &glutin::Window) -> texture::Dimensions {
let (width, height) = window.get_inner_size().unwrap();
let aa = window.get_pixel_format().multisampling
.unwrap_or(0) as texture::NumSamples;
((width as f32 * window.hidpi_factor()) as texture::Size, (height as f32 * window.hidpi_factor()) as texture::Size, 1, aa.into())
}
pub fn init_raw(builder: glutin::WindowBuilder,
color_format: format::Format, ds_format: format::Format) ->
(glutin::Window, device_gl::Device, device_gl::Factory,
handle::RawRenderTargetView<R>, handle::RawDepthStencilView<R>)
{
let window = {
let color_total_bits = color_format.0.get_total_bits();
let alpha_bits = color_format.0.get_alpha_stencil_bits();
let depth_total_bits = ds_format.0.get_total_bits();
let stencil_bits = ds_format.0.get_alpha_stencil_bits();
builder
.with_depth_buffer(depth_total_bits - stencil_bits)
.with_stencil_buffer(stencil_bits)
.with_pixel_format(color_total_bits - alpha_bits, alpha_bits)
.with_srgb(Some(color_format.1 == format::ChannelType::Srgb))
.build()
}.unwrap();
let (device, factory, color_view, ds_view) = init_existing_raw(&window, color_format, ds_format);
(window, device, factory, color_view, ds_view)
}
pub fn init_existing_raw(window: &glutin::Window,
color_format: format::Format, ds_format: format::Format) ->
(device_gl::Device, device_gl::Factory,
handle::RawRenderTargetView<R>, handle::RawDepthStencilView<R>)
{
unsafe { window.make_current().unwrap() };
let (device, factory) = device_gl::create(|s|
window.get_proc_address(s) as *const std::os::raw::c_void);
let dim = get_window_dimensions(window);
let (color_view, ds_view) = device_gl::create_main_targets_raw(dim, color_format.0, ds_format.0);
(device, factory, color_view, ds_view)
}
pub fn update_views<Cf, Df>(window: &glutin::Window, color_view: &mut handle::RenderTargetView<R, Cf>,
ds_view: &mut handle::DepthStencilView<R, Df>)
where
Cf: format::RenderFormat,
Df: format::DepthFormat,
{
let dim = color_view.get_dimensions();
assert_eq!(dim, ds_view.get_dimensions());
if let Some((cv, dv)) = update_views_raw(window, dim, Cf::get_format(), Df::get_format()) {
*color_view = Typed::new(cv);
*ds_view = Typed::new(dv);
}
}
pub fn update_views_raw(window: &glutin::Window, old_dimensions: texture::Dimensions,
color_format: format::Format, ds_format: format::Format)
-> Option<(handle::RawRenderTargetView<R>, handle::RawDepthStencilView<R>)>
{
let dim = get_window_dimensions(window);
if dim != old_dimensions {
Some(device_gl::create_main_targets_raw(dim, color_format.0, ds_format.0))
}else {
None
}
}