mln_runtime/render/backends/
gl.rs1#[cfg(has_gl_window)]
4use std::sync::Arc;
5
6#[cfg(has_gl_exported)]
7use mln_gfx::{ExportKind, ExportedRing};
8#[cfg(has_gl_window)]
9use mln_gfx::{RendererBackend as _, Window};
10use mln_gfx::{SyncKind, SyncKinds};
11use mln_style::error::Error;
12
13#[cfg(has_gl_device)]
14use super::adapter_index;
15use crate::render::gfx_error;
16#[cfg(has_gl_exported)]
17use crate::render::local::Exported;
18#[cfg(has_gl_window)]
19use crate::render::local::overlay_of;
20use crate::render::local::{BackendRef, Borrowed, FramesInit, Gfx, LocalBackend, gfx, presenting};
21#[cfg(any(has_gl_device, has_webgl, has_gl_exported))]
22use crate::render::target::Device;
23#[cfg(has_gl_device)]
24use crate::render::target::{BackendType, DeviceOptions, GlApi};
25use crate::render::target::{BorrowedRing, FrameTexture, FrameTextureKind, RingKind};
26#[cfg(has_native_gl)]
27use crate::render::target::{FrameDepth, NativeTexture, Surface, SurfaceKind, SurfaceOverlay};
28
29impl BorrowedRing {
30 #[must_use]
33 pub fn gl(textures: mln_gl::BorrowedTextures) -> BorrowedRing {
34 BorrowedRing { kind: RingKind::Gl(textures) }
35 }
36}
37
38#[cfg(has_native_gl)]
39impl Surface {
40 #[must_use]
43 pub fn gl(surface: mln_gl::HostSurface) -> Surface {
44 Surface::of(SurfaceKind::Gl(surface))
45 }
46}
47
48#[cfg(has_webgl)]
49impl Device {
50 #[must_use]
52 pub fn is_wrapped_web_gl(&self) -> bool {
53 matches!(self, Device::WebGl(Some(_)))
54 }
55
56 pub fn web_gl(context: &wasm_bindgen::JsValue) -> Result<Device, Error> {
64 let context = wasm_bindgen::JsCast::dyn_ref::<web_sys::WebGl2RenderingContext>(context)
65 .ok_or_else(|| Error::invalid_argument("a WebGL device wraps a WebGL2RenderingContext"))?;
66 Ok(Device::WebGl(Some(mln_gl::WebGlDevice::new(context.clone()))))
67 }
68}
69
70pub(in super::super) fn ring_same_texture(
71 textures: &mln_gl::BorrowedTextures,
72 slot: usize,
73 others: &mln_gl::BorrowedTextures,
74 other_slot: usize,
75) -> bool {
76 textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
77}
78
79#[cfg(has_native_gl)]
80pub(in super::super) fn ring_texture_native(textures: &mln_gl::BorrowedTextures, slot: usize) -> Option<FrameTexture> {
81 textures.textures.get(slot).map(|&texture| FrameTexture {
83 size: textures.size,
84 kind: FrameTextureKind::OpenGl { texture, target: 0x0DE1 },
85 native: None,
86 depth: textures.depth_textures.get(slot).map(|&depth| FrameDepth {
87 texture: NativeTexture::OpenGl { texture: depth, target: 0x0DE1, internal_format: 0 },
88 export: None,
89 }),
90 })
91}
92
93#[cfg(has_webgl)]
94pub(in super::super) fn ring_texture_webgl(textures: &mln_gl::BorrowedTextures, slot: usize) -> Option<FrameTexture> {
95 textures.textures.get(slot).map(|texture| FrameTexture {
97 size: textures.size,
98 kind: FrameTextureKind::WebGl {
99 texture: texture.clone(),
100 target: 0x0DE1,
101 depth: textures.depth_textures.get(slot).cloned(),
102 },
103 native: None,
104 depth: None,
105 })
106}
107
108#[cfg(windows)]
109pub(in super::super) fn check_window_pixel_format(window: &dyn Window) -> Result<(), Error> {
110 mln_gl::SurfaceTarget::check_window(window).map_err(gfx_error)
111}
112
113#[cfg(has_gl_device)]
114pub(in super::super) fn create_device(options: DeviceOptions) -> Result<Device, Error> {
115 match adapter_index(BackendType::OpenGL, options.adapter)? {
118 None | Some(0) => {}
119 Some(index) => {
120 return Err(Error::not_found(format!("adapter_index {index} is past OpenGL's one GPU")));
121 }
122 }
123 let api = match options.gl_api {
124 GlApi::Gles => mln_gl::Api::Gles,
125 GlApi::Gl => mln_gl::Api::Gl,
126 };
127 let options = mln_gl::DeviceOptions { api, single_context: options.gl_single_context };
128 mln_gl::Device::with_options(options).map(Device::Gl).map_err(gfx_error)
129}
130
131#[cfg(has_native_gl)]
132pub(in super::super) fn borrowed_sync_kinds_native(device: &mln_gl::Device) -> Result<(SyncKinds, SyncKinds), Error> {
133 if !device.is_host() {
134 return Err(Error::unsupported("an OpenGL ring renders on a device in the share group of the host's context"));
135 }
136 Ok(mln_gl::BorrowedTextures::sync_kinds())
137}
138
139#[cfg(has_webgl)]
140pub(in super::super) fn borrowed_sync_kinds_webgl(
141 device: Option<&mln_gl::WebGlDevice>,
142) -> Result<(SyncKinds, SyncKinds), Error> {
143 if device.is_none() {
144 return Err(Error::unsupported("a WebGL ring renders on a device that wraps your WebGL2 context"));
145 }
146 Ok(mln_gl::BorrowedTextures::sync_kinds())
147}
148
149#[cfg(has_gl_exported)]
150pub(in super::super) fn export_plan(
151 device: &mln_gl::Device,
152 ring: &ExportedRing,
153) -> Result<(ExportKind, SyncKinds, SyncKinds), Error> {
154 let kind = mln_gl::exported_target::plan(device, ring).map_err(gfx_error)?;
155 let (producer, consumer) = mln_gl::exported_target::sync_kinds(device);
156 Ok((kind, producer, consumer))
157}
158
159impl LocalBackend for mln_gl::RendererBackend {
160 type RingTextures = mln_gl::BorrowedTextures;
161
162 fn ring_textures(ring: RingKind) -> Option<Self::RingTextures> {
163 match ring {
164 RingKind::Gl(textures) => Some(textures),
165 _ => None,
166 }
167 }
168
169 fn by_type(&self) -> BackendRef<'_> {
170 BackendRef::Gl(self)
171 }
172
173 fn borrowed(&mut self, textures: Self::RingTextures, producer: SyncKind) -> Result<Box<dyn Borrowed<Self>>, Error> {
174 Ok(Box::new(mln_gl::BorrowedTarget::new(self, textures, producer).map_err(gfx_error)?))
175 }
176
177 #[cfg(has_gl_exported)]
178 fn exported(
179 &mut self,
180 _device: &Device,
181 ring: &ExportedRing,
182 kind: ExportKind,
183 size: mln_gfx::Size,
184 producer: SyncKind,
185 ) -> Result<Box<dyn Exported<Self>>, Error> {
186 Ok(Box::new(mln_gl::ExportedTarget::new(self, ring, kind, size, producer).map_err(gfx_error)?))
187 }
188}
189
190#[cfg(has_native_gl)]
191pub(in super::super) fn new_gfx_native(device: &mln_gl::Device, init: FramesInit<'_>) -> Result<Box<dyn Gfx>, Error> {
192 gfx(mln_gl::RendererBackend::new(device).map_err(gfx_error)?, init)
193}
194
195#[cfg(has_webgl)]
196pub(in super::super) fn new_gfx_webgl(
197 device: Option<&mln_gl::WebGlDevice>,
198 init: FramesInit<'_>,
199) -> Result<Box<dyn Gfx>, Error> {
200 if let Some(device) = device {
201 gfx(mln_gl::RendererBackend::from_web_gl(device), init)
202 } else if matches!(init, FramesInit::Offscreen(_)) {
203 gfx(mln_gl::RendererBackend::offscreen_web_gl().map_err(gfx_error)?, init)
204 } else {
205 drop(init);
206 Err(Error::unsupported("WebGL2 renders into a canvas, or into borrowed textures on a wrapped WebGL2 context"))
207 }
208}
209
210#[cfg(has_native_gl)]
211pub(in super::super) fn host_surface_gfx(
212 device: &mln_gl::Device,
213 host: mln_gl::HostSurface,
214 overlay: Option<&SurfaceOverlay>,
215 size: mln_gfx::Size,
216) -> Result<Box<dyn Gfx>, Error> {
217 if overlay.is_some() {
218 return Err(Error::unsupported("a host's EGL surface takes no overlay"));
219 }
220 let (backend, target) = mln_gl::HostSurfaceTarget::with_backend(device, host, size).map_err(gfx_error)?;
221 Ok(presenting(backend, target))
222}
223
224#[cfg(has_gl_window)]
225pub(in super::super) fn window_surface_gfx(
226 window: Arc<dyn Window>,
227 overlay: Option<SurfaceOverlay>,
228 size: mln_gfx::Size,
229 cap_frame_rate: bool,
230) -> Result<Box<dyn Gfx>, Error> {
231 let overlay = overlay_of(overlay, |overlay| match overlay {
232 SurfaceOverlay::Gl(overlay) => Ok(overlay),
233 other => Err(other),
234 })?;
235 let (mut backend, mut target) =
236 mln_gl::SurfaceTarget::with_backend(window, size, cap_frame_rate).map_err(gfx_error)?;
237 target.set_overlay(&mut backend.gpu(), overlay);
238 Ok(presenting(backend, target))
239}
240
241#[cfg(has_webgl)]
242pub(in super::super) fn web_canvas_gfx(
243 wrapped: Option<&mln_gl::WebGlDevice>,
244 canvas: wasm_bindgen::JsValue,
245 size: mln_gfx::Size,
246) -> Result<Box<dyn Gfx>, Error> {
247 use wasm_bindgen::JsCast as _;
248 let canvas = match canvas.dyn_into::<web_sys::OffscreenCanvas>() {
250 Ok(canvas) => mln_gl::WebCanvas::Offscreen(canvas),
251 Err(canvas) => mln_gl::WebCanvas::Element(canvas.unchecked_into()),
252 };
253 let (backend, target) = match wrapped {
254 Some(device) => mln_gl::CanvasTarget::with_wrapped(device, canvas, size),
255 None => mln_gl::CanvasTarget::with_backend(canvas, size),
256 }
257 .map_err(gfx_error)?;
258 Ok(presenting(backend, target))
259}