use alloc::{format, string::String, vec::Vec};
use core::sync::atomic::{AtomicBool, Ordering};
use parking_lot::RwLock;
use wasm_bindgen::{JsCast, JsValue};
use super::TextureFormatDesc;
static ONLY_USED_EXTENSIONS: AtomicBool = AtomicBool::new(false);
pub fn ask_only_for_used_extensions() {
ONLY_USED_EXTENSIONS.store(true, Ordering::Relaxed);
}
fn enables_extension(name: &str) -> bool {
matches!(
name,
"WEBGL_debug_renderer_info"
| "WEBGL_lose_context"
| "KHR_parallel_shader_compile"
| "EXT_disjoint_timer_query_webgl2"
)
}
fn glow_context(webgl2_context: &web_sys::WebGl2RenderingContext) -> glow::Context {
if !ONLY_USED_EXTENSIONS.load(Ordering::Relaxed) {
return glow::Context::from_webgl2_context(webgl2_context.clone());
}
let names = js_sys::Array::new();
let enabled = js_sys::Map::new();
for name in webgl2_context.get_supported_extensions().iter().flat_map(|names| names.iter()) {
if let Some(text) = name.as_string().filter(|text| enables_extension(text)) {
if let Ok(Some(extension)) = webgl2_context.get_extension(&text) {
names.push(&name);
enabled.set(&name, &extension);
}
}
}
let context: &js_sys::Object = webgl2_context.as_ref();
let answers = [
("getSupportedExtensions", bound_method(&names, "slice")),
("getExtension", bound_method(&enabled, "get")),
];
let shadowed = answers.iter().all(|(property, answer)| {
answer.as_ref().is_some_and(|answer| {
js_sys::Reflect::set(context, &JsValue::from_str(property), answer).unwrap_or(false)
})
});
let gl = shadowed.then(|| glow::Context::from_webgl2_context(webgl2_context.clone()));
for (property, _) in &answers {
let _ = js_sys::Reflect::delete_property(context, &JsValue::from_str(property));
}
gl.unwrap_or_else(|| glow::Context::from_webgl2_context(webgl2_context.clone()))
}
fn bound_method(target: &JsValue, name: &str) -> Option<js_sys::Function> {
js_sys::Reflect::get(target, &JsValue::from_str(name))
.ok()?
.dyn_into::<js_sys::Function>()
.ok()
.map(|method| method.bind0(target))
}
#[derive(Debug)]
pub struct AdapterContext {
pub glow_context: glow::Context,
pub webgl2_context: web_sys::WebGl2RenderingContext,
}
impl AdapterContext {
pub fn is_owned(&self) -> bool {
false
}
#[track_caller]
pub fn lock(&self) -> &glow::Context {
&self.glow_context
}
}
#[derive(Debug)]
pub struct Instance {
options: wgt::GlBackendOptions,
}
impl Instance {
pub fn create_surface_from_canvas(
&self,
canvas: web_sys::HtmlCanvasElement,
) -> Result<Surface, crate::InstanceError> {
let result =
canvas.get_context_with_context_options("webgl2", &Self::create_context_options());
self.create_surface_from_context(Canvas::Canvas(canvas), result)
}
pub fn create_surface_from_offscreen_canvas(
&self,
canvas: web_sys::OffscreenCanvas,
) -> Result<Surface, crate::InstanceError> {
let result =
canvas.get_context_with_context_options("webgl2", &Self::create_context_options());
self.create_surface_from_context(Canvas::Offscreen(canvas), result)
}
fn create_surface_from_context(
&self,
canvas: Canvas,
context_result: Result<Option<js_sys::Object>, JsValue>,
) -> Result<Surface, crate::InstanceError> {
let context_object: js_sys::Object = match context_result {
Ok(Some(context)) => context,
Ok(None) => {
return Err(crate::InstanceError::new(String::from(concat!(
"canvas.getContext() returned null; ",
"webgl2 not available or canvas already in use"
))));
}
Err(js_error) => {
return Err(crate::InstanceError::new(format!(
"canvas.getContext() threw exception {js_error:?}",
)));
}
};
let webgl2_context: web_sys::WebGl2RenderingContext = context_object
.dyn_into()
.expect("canvas context is not a WebGl2RenderingContext");
Ok(Surface {
canvas,
webgl2_context,
swapchain: RwLock::new(None),
presentable: true,
})
}
fn create_context_options() -> js_sys::Object {
let context_options = js_sys::Object::new();
for (option, value) in [("antialias", false), ("depth", true), ("stencil", true)] {
js_sys::Reflect::set(&context_options, &option.into(), &JsValue::from_bool(value))
.expect("Cannot create context options");
}
context_options
}
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(Instance: Send, Sync);
impl crate::Instance for Instance {
type A = super::Api;
unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {
profiling::scope!("Init OpenGL (WebGL) Backend");
Ok(Instance {
options: desc.backend_options.gl.clone(),
})
}
unsafe fn enumerate_adapters(
&self,
surface_hint: Option<&Surface>,
) -> Vec<crate::ExposedAdapter<super::Api>> {
if let Some(surface_hint) = surface_hint {
let gl = glow_context(&surface_hint.webgl2_context);
unsafe {
super::Adapter::expose(
AdapterContext {
glow_context: gl,
webgl2_context: surface_hint.webgl2_context.clone(),
},
self.options.clone(),
)
}
.into_iter()
.collect()
} else {
Vec::new()
}
}
unsafe fn create_surface(
&self,
_display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
) -> Result<Surface, crate::InstanceError> {
let canvas: web_sys::HtmlCanvasElement = match window_handle {
raw_window_handle::RawWindowHandle::Web(handle) => web_sys::window()
.and_then(|win| win.document())
.expect("Cannot get document")
.query_selector(&format!("canvas[data-raw-handle=\"{}\"]", handle.id))
.expect("Cannot query for canvas")
.expect("Canvas is not found")
.dyn_into()
.expect("Failed to downcast to canvas type"),
raw_window_handle::RawWindowHandle::WebCanvas(handle) => {
let value: &JsValue = unsafe { handle.obj.cast().as_ref() };
value.clone().unchecked_into()
}
raw_window_handle::RawWindowHandle::WebOffscreenCanvas(handle) => {
let value: &JsValue = unsafe { handle.obj.cast().as_ref() };
let canvas: web_sys::OffscreenCanvas = value.clone().unchecked_into();
return self.create_surface_from_offscreen_canvas(canvas);
}
_ => {
return Err(crate::InstanceError::new(format!(
"window handle {window_handle:?} is not a web handle"
)))
}
};
self.create_surface_from_canvas(canvas)
}
}
impl super::Adapter {
pub unsafe fn new_external(
webgl2_context: web_sys::WebGl2RenderingContext,
options: wgt::GlBackendOptions,
) -> Option<crate::ExposedAdapter<super::Api>> {
let glow_context = glow_context(&webgl2_context);
unsafe {
Self::expose(
AdapterContext {
glow_context,
webgl2_context,
},
options,
)
}
}
pub fn adapter_context(&self) -> &AdapterContext {
&self.shared.context
}
}
impl super::Device {
pub fn context(&self) -> &AdapterContext {
&self.shared.context
}
}
#[derive(Debug)]
pub struct Surface {
canvas: Canvas,
pub(super) webgl2_context: web_sys::WebGl2RenderingContext,
pub(super) swapchain: RwLock<Option<Swapchain>>,
pub(super) presentable: bool,
}
impl Clone for Surface {
fn clone(&self) -> Self {
Self {
canvas: self.canvas.clone(),
webgl2_context: self.webgl2_context.clone(),
swapchain: RwLock::new(self.swapchain.read().clone()),
presentable: self.presentable,
}
}
}
#[cfg(send_sync)]
unsafe impl Sync for Surface {}
#[cfg(send_sync)]
unsafe impl Send for Surface {}
#[derive(Clone, Debug)]
enum Canvas {
Canvas(web_sys::HtmlCanvasElement),
Offscreen(web_sys::OffscreenCanvas),
}
#[derive(Clone, Debug)]
pub struct Swapchain {
pub(crate) extent: wgt::Extent3d,
pub(super) format: wgt::TextureFormat,
pub(super) format_desc: TextureFormatDesc,
}
impl Surface {
pub(super) unsafe fn present(
&self,
_suf_texture: super::Texture,
_context: &AdapterContext,
) -> Result<(), crate::SurfaceError> {
match *self.swapchain.read() {
Some(_) => Ok(()),
None => Err(crate::SurfaceError::Other(
"need to configure surface before presenting",
)),
}
}
}
impl crate::Surface for Surface {
type A = super::Api;
unsafe fn configure(
&self,
device: &super::Device,
config: &crate::SurfaceConfiguration,
) -> Result<(), crate::SurfaceError> {
match self.canvas {
Canvas::Canvas(ref canvas) => {
canvas.set_width(config.extent.width);
canvas.set_height(config.extent.height);
if let Ok(style) = js_sys::Reflect::get(canvas, &JsValue::from_str("style")) {
let _ = js_sys::Reflect::set(
&style,
&JsValue::from_str("scale"),
&JsValue::from_str("1 -1"),
);
}
}
Canvas::Offscreen(ref canvas) => {
canvas.set_width(config.extent.width);
canvas.set_height(config.extent.height);
}
}
*self.swapchain.write() = Some(Swapchain {
extent: config.extent,
format: config.format,
format_desc: device.shared.describe_texture_format(config.format),
});
Ok(())
}
unsafe fn unconfigure(&self, _device: &super::Device) {
self.swapchain.write().take();
}
unsafe fn acquire_texture(
&self,
_timeout_ms: Option<core::time::Duration>, _fence: &super::Fence,
) -> Result<crate::AcquiredSurfaceTexture<super::Api>, crate::SurfaceError> {
let swapchain = self.swapchain.read();
let sc = swapchain.as_ref().unwrap();
let texture = super::Texture {
inner: super::TextureInner::DefaultRenderbuffer,
drop_guard: None,
array_layer_count: 1,
mip_level_count: 1,
format: sc.format,
format_desc: sc.format_desc.clone(),
copy_size: crate::CopyExtent {
width: sc.extent.width,
height: sc.extent.height,
depth: 1,
},
};
Ok(crate::AcquiredSurfaceTexture {
texture,
suboptimal: false,
images: 1,
})
}
unsafe fn discard_texture(&self, _texture: super::Texture) {}
}