#[cfg(has_gl_window)]
use std::sync::Arc;
#[cfg(has_gl_exported)]
use mln_gfx::{ExportKind, ExportedRing};
#[cfg(has_gl_window)]
use mln_gfx::{RendererBackend as _, Window};
use mln_gfx::{SyncKind, SyncKinds};
use mln_style::error::Error;
#[cfg(has_gl_device)]
use super::adapter_index;
use crate::render::gfx_error;
#[cfg(has_gl_exported)]
use crate::render::local::Exported;
#[cfg(has_gl_window)]
use crate::render::local::overlay_of;
use crate::render::local::{BackendRef, Borrowed, FramesInit, Gfx, LocalBackend, gfx, presenting};
#[cfg(any(has_gl_device, has_webgl, has_gl_exported))]
use crate::render::target::Device;
#[cfg(has_gl_device)]
use crate::render::target::{BackendType, DeviceOptions, GlApi};
use crate::render::target::{BorrowedRing, FrameTexture, FrameTextureKind, RingKind};
#[cfg(has_native_gl)]
use crate::render::target::{FrameDepth, NativeTexture, Surface, SurfaceKind, SurfaceOverlay};
impl BorrowedRing {
#[must_use]
pub fn gl(textures: mln_gl::BorrowedTextures) -> BorrowedRing {
BorrowedRing { kind: RingKind::Gl(textures) }
}
}
#[cfg(has_native_gl)]
impl Surface {
#[must_use]
pub fn gl(surface: mln_gl::HostSurface) -> Surface {
Surface::of(SurfaceKind::Gl(surface))
}
}
#[cfg(has_webgl)]
impl Device {
#[must_use]
pub fn is_wrapped_web_gl(&self) -> bool {
matches!(self, Device::WebGl(Some(_)))
}
pub fn web_gl(context: &wasm_bindgen::JsValue) -> Result<Device, Error> {
let context = wasm_bindgen::JsCast::dyn_ref::<web_sys::WebGl2RenderingContext>(context)
.ok_or_else(|| Error::invalid_argument("a WebGL device wraps a WebGL2RenderingContext"))?;
Ok(Device::WebGl(Some(mln_gl::WebGlDevice::new(context.clone()))))
}
}
pub(in super::super) fn ring_same_texture(
textures: &mln_gl::BorrowedTextures,
slot: usize,
others: &mln_gl::BorrowedTextures,
other_slot: usize,
) -> bool {
textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
}
#[cfg(has_native_gl)]
pub(in super::super) fn ring_texture_native(textures: &mln_gl::BorrowedTextures, slot: usize) -> Option<FrameTexture> {
textures.textures.get(slot).map(|&texture| FrameTexture {
size: textures.size,
kind: FrameTextureKind::OpenGl { texture, target: 0x0DE1 },
native: None,
depth: textures.depth_textures.get(slot).map(|&depth| FrameDepth {
texture: NativeTexture::OpenGl { texture: depth, target: 0x0DE1, internal_format: 0 },
export: None,
}),
})
}
#[cfg(has_webgl)]
pub(in super::super) fn ring_texture_webgl(textures: &mln_gl::BorrowedTextures, slot: usize) -> Option<FrameTexture> {
textures.textures.get(slot).map(|texture| FrameTexture {
size: textures.size,
kind: FrameTextureKind::WebGl {
texture: texture.clone(),
target: 0x0DE1,
depth: textures.depth_textures.get(slot).cloned(),
},
native: None,
depth: None,
})
}
#[cfg(windows)]
pub(in super::super) fn check_window_pixel_format(window: &dyn Window) -> Result<(), Error> {
mln_gl::SurfaceTarget::check_window(window).map_err(gfx_error)
}
#[cfg(has_gl_device)]
pub(in super::super) fn create_device(options: DeviceOptions) -> Result<Device, Error> {
match adapter_index(BackendType::OpenGL, options.adapter)? {
None | Some(0) => {}
Some(index) => {
return Err(Error::not_found(format!("adapter_index {index} is past OpenGL's one GPU")));
}
}
let api = match options.gl_api {
GlApi::Gles => mln_gl::Api::Gles,
GlApi::Gl => mln_gl::Api::Gl,
};
let options = mln_gl::DeviceOptions { api, single_context: options.gl_single_context };
mln_gl::Device::with_options(options).map(Device::Gl).map_err(gfx_error)
}
#[cfg(has_native_gl)]
pub(in super::super) fn borrowed_sync_kinds_native(device: &mln_gl::Device) -> Result<(SyncKinds, SyncKinds), Error> {
if !device.is_host() {
return Err(Error::unsupported("an OpenGL ring renders on a device in the share group of the host's context"));
}
Ok(mln_gl::BorrowedTextures::sync_kinds())
}
#[cfg(has_webgl)]
pub(in super::super) fn borrowed_sync_kinds_webgl(
device: Option<&mln_gl::WebGlDevice>,
) -> Result<(SyncKinds, SyncKinds), Error> {
if device.is_none() {
return Err(Error::unsupported("a WebGL ring renders on a device that wraps your WebGL2 context"));
}
Ok(mln_gl::BorrowedTextures::sync_kinds())
}
#[cfg(has_gl_exported)]
pub(in super::super) fn export_plan(
device: &mln_gl::Device,
ring: &ExportedRing,
) -> Result<(ExportKind, SyncKinds, SyncKinds), Error> {
let kind = mln_gl::exported_target::plan(device, ring).map_err(gfx_error)?;
let (producer, consumer) = mln_gl::exported_target::sync_kinds(device);
Ok((kind, producer, consumer))
}
impl LocalBackend for mln_gl::RendererBackend {
type RingTextures = mln_gl::BorrowedTextures;
fn ring_textures(ring: RingKind) -> Option<Self::RingTextures> {
match ring {
RingKind::Gl(textures) => Some(textures),
_ => None,
}
}
fn by_type(&self) -> BackendRef<'_> {
BackendRef::Gl(self)
}
fn borrowed(&mut self, textures: Self::RingTextures, producer: SyncKind) -> Result<Box<dyn Borrowed<Self>>, Error> {
Ok(Box::new(mln_gl::BorrowedTarget::new(self, textures, producer).map_err(gfx_error)?))
}
#[cfg(has_gl_exported)]
fn exported(
&mut self,
_device: &Device,
ring: &ExportedRing,
kind: ExportKind,
size: mln_gfx::Size,
producer: SyncKind,
) -> Result<Box<dyn Exported<Self>>, Error> {
Ok(Box::new(mln_gl::ExportedTarget::new(self, ring, kind, size, producer).map_err(gfx_error)?))
}
}
#[cfg(has_native_gl)]
pub(in super::super) fn new_gfx_native(device: &mln_gl::Device, init: FramesInit<'_>) -> Result<Box<dyn Gfx>, Error> {
gfx(mln_gl::RendererBackend::new(device).map_err(gfx_error)?, init)
}
#[cfg(has_webgl)]
pub(in super::super) fn new_gfx_webgl(
device: Option<&mln_gl::WebGlDevice>,
init: FramesInit<'_>,
) -> Result<Box<dyn Gfx>, Error> {
if let Some(device) = device {
gfx(mln_gl::RendererBackend::from_web_gl(device), init)
} else if matches!(init, FramesInit::Offscreen(_)) {
gfx(mln_gl::RendererBackend::offscreen_web_gl().map_err(gfx_error)?, init)
} else {
drop(init);
Err(Error::unsupported("WebGL2 renders into a canvas, or into borrowed textures on a wrapped WebGL2 context"))
}
}
#[cfg(has_native_gl)]
pub(in super::super) fn host_surface_gfx(
device: &mln_gl::Device,
host: mln_gl::HostSurface,
overlay: Option<&SurfaceOverlay>,
size: mln_gfx::Size,
) -> Result<Box<dyn Gfx>, Error> {
if overlay.is_some() {
return Err(Error::unsupported("a host's EGL surface takes no overlay"));
}
let (backend, target) = mln_gl::HostSurfaceTarget::with_backend(device, host, size).map_err(gfx_error)?;
Ok(presenting(backend, target))
}
#[cfg(has_gl_window)]
pub(in super::super) fn window_surface_gfx(
window: Arc<dyn Window>,
overlay: Option<SurfaceOverlay>,
size: mln_gfx::Size,
cap_frame_rate: bool,
) -> Result<Box<dyn Gfx>, Error> {
let overlay = overlay_of(overlay, |overlay| match overlay {
SurfaceOverlay::Gl(overlay) => Ok(overlay),
other => Err(other),
})?;
let (mut backend, mut target) =
mln_gl::SurfaceTarget::with_backend(window, size, cap_frame_rate).map_err(gfx_error)?;
target.set_overlay(&mut backend.gpu(), overlay);
Ok(presenting(backend, target))
}
#[cfg(has_webgl)]
pub(in super::super) fn web_canvas_gfx(
wrapped: Option<&mln_gl::WebGlDevice>,
canvas: wasm_bindgen::JsValue,
size: mln_gfx::Size,
) -> Result<Box<dyn Gfx>, Error> {
use wasm_bindgen::JsCast as _;
let canvas = match canvas.dyn_into::<web_sys::OffscreenCanvas>() {
Ok(canvas) => mln_gl::WebCanvas::Offscreen(canvas),
Err(canvas) => mln_gl::WebCanvas::Element(canvas.unchecked_into()),
};
let (backend, target) = match wrapped {
Some(device) => mln_gl::CanvasTarget::with_wrapped(device, canvas, size),
None => mln_gl::CanvasTarget::with_backend(canvas, size),
}
.map_err(gfx_error)?;
Ok(presenting(backend, target))
}