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
use euclid::Size2D;
use luminance::{
context::GraphicsContext,
framebuffer::{Framebuffer, FramebufferError},
texture::Dim2,
};
pub use luminance_glow::ShaderVersion;
use luminance_glow::{Context as GlowContext, Glow, StateQueryError};
use surfman::{
Connection, Context, ContextAttributeFlags, ContextAttributes, Device, GLVersion,
SurfaceAccess, SurfaceType,
};
use winit::window::Window;
surfman::declare_surfman!();
#[derive(thiserror::Error, Debug)]
pub enum SurfmanError {
#[error("Surface error: {0}")]
SurfaceError(String),
#[error("GL error: {0}")]
GlError(#[from] StateQueryError),
#[error("Framebuffer error: {0}")]
FramebufferError(#[from] FramebufferError),
}
pub struct SurfmanSurface {
backend: Glow,
device: Device,
context: Context,
}
unsafe impl GraphicsContext for SurfmanSurface {
type Backend = Glow;
fn backend(&mut self) -> &mut Self::Backend {
&mut self.backend
}
}
impl SurfmanSurface {
pub fn from_winit_window(
window: &Window,
shader_version: ShaderVersion,
) -> Result<Self, SurfmanError> {
let conn = Connection::from_winit_window(&window).map_err(surface_err)?;
let native_widget = conn
.create_native_widget_from_winit_window(&window)
.map_err(surface_err)?;
let adapter = conn.create_hardware_adapter().map_err(surface_err)?;
let mut device = conn.create_device(&adapter).map_err(surface_err)?;
let context_attributes = ContextAttributes {
version: GLVersion::new(3, 3),
flags: ContextAttributeFlags::ALPHA
| ContextAttributeFlags::DEPTH
| ContextAttributeFlags::STENCIL,
};
let context_descriptor = device
.create_context_descriptor(&context_attributes)
.map_err(surface_err)?;
let surface_type = SurfaceType::Widget { native_widget };
let mut context = device
.create_context(&context_descriptor, None)
.map_err(surface_err)?;
let surface = device
.create_surface(&context, SurfaceAccess::GPUCPU, surface_type)
.map_err(surface_err)?;
device
.bind_surface_to_context(&mut context, surface)
.map_err(|(e, _)| surface_err(e))?;
device.make_context_current(&context).map_err(surface_err)?;
let gl = unsafe {
GlowContext::from_loader_function(
|s| device.get_proc_address(&context, s) as *const _,
shader_version,
)
};
let backend = Glow::from_context(gl)?;
Ok(SurfmanSurface {
backend,
device,
context,
})
}
pub fn back_buffer(&mut self) -> Result<Framebuffer<Glow, Dim2, (), ()>, SurfmanError> {
let surface = self
.device
.unbind_surface_from_context(&mut self.context)
.map_err(surface_err)?
.unwrap();
let surface_info = self.device.surface_info(&surface);
let width = surface_info.size.width as u32;
let height = surface_info.size.height as u32;
self.device
.bind_surface_to_context(&mut self.context, surface)
.map_err(|(e, _)| surface_err(e))?;
Ok(Framebuffer::back_buffer(self, [width, height])?)
}
pub fn swap_buffers(&mut self) -> Result<(), SurfmanError> {
let mut surface = self
.device
.unbind_surface_from_context(&mut self.context)
.map_err(surface_err)?
.unwrap();
self.device
.present_surface(&self.context, &mut surface)
.map_err(surface_err)?;
self.device
.bind_surface_to_context(&mut self.context, surface)
.map_err(|(e, _)| surface_err(e))?;
Ok(())
}
pub fn set_size(&mut self, size: [u32; 2]) -> Result<(), SurfmanError> {
let mut surface = self
.device
.unbind_surface_from_context(&mut self.context)
.map_err(surface_err)?
.unwrap();
self.device
.resize_surface(
&mut self.context,
&mut surface,
Size2D::new(size[0] as i32, size[1] as i32),
)
.map_err(surface_err)?;
self.device
.bind_surface_to_context(&mut self.context, surface)
.map_err(|(e, _)| surface_err(e))?;
Ok(())
}
}
impl Drop for SurfmanSurface {
fn drop(&mut self) {
self.device
.destroy_context(&mut self.context)
.unwrap_or_else(|e| eprintln!("Error destroying surfman context: {:?}", e));
}
}
fn surface_err(e: surfman::Error) -> SurfmanError {
SurfmanError::SurfaceError(format!("{:?}", e))
}