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
#[deny(missing_docs)]
extern crate gfx_core as core;
extern crate gfx_device_gl as device_gl;
extern crate glutin;
#[cfg(feature = "headless")]
pub use headless::{init_headless, init_headless_raw};
use core::{format, handle, texture};
use core::memory::Typed;
use device_gl::Resources as R;
use glutin::GlContext;
#[cfg(feature = "headless")]
mod headless;
#[cfg(target_os = "emscripten")]
pub mod emscripten;
pub fn init<Cf, Df>(window: glutin::WindowBuilder,
context: glutin::ContextBuilder,
events_loop: &glutin::EventsLoop) ->
(glutin::GlWindow, 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(
window,
context,
events_loop,
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::GlWindow) ->
(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::GlWindow) -> texture::Dimensions {
#[cfg(target_os = "emscripten")]
let (width, height) = emscripten::get_canvas_size();
#[cfg(not(target_os = "emscripten"))]
let (width, height) = {
let size = window.get_inner_size().unwrap().to_physical(window.get_hidpi_factor());
(size.width as _, size.height as _)
};
let aa = window
.get_pixel_format().multisampling
.unwrap_or(0) as texture::NumSamples;
(width, height, 1, aa.into())
}
pub fn init_raw(window: glutin::WindowBuilder,
mut context: glutin::ContextBuilder,
events_loop: &glutin::EventsLoop,
color_format: format::Format,
ds_format: format::Format) ->
(glutin::GlWindow, 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();
context = context
.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(color_format.1 == format::ChannelType::Srgb);
glutin::GlWindow::new(window, context, &events_loop).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::GlWindow,
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::GlWindow, 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::GlWindow, 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
}
}
pub fn new_views<Cf, Df>(window: &glutin::GlWindow)
-> (handle::RenderTargetView<R, Cf>, handle::DepthStencilView<R, Df>) where
Cf: format::RenderFormat,
Df: format::DepthFormat,
{
let dim = get_window_dimensions(window);
let (color_view_raw, depth_view_raw) =
device_gl::create_main_targets_raw(dim, Cf::get_format().0, Df::get_format().0);
(Typed::new(color_view_raw), Typed::new(depth_view_raw))
}