Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Fn's to work with any kind of WebGl2RenderingContext source. #101

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ use std::fmt::{Display, Formatter};
use std::marker::PhantomData;
use std::rc::Rc;

#[cfg(target_arch = "wasm32")]
use web_sys::WebGl2RenderingContext;
#[cfg(any(feature = "image-loading", doc, doctest))]
use {
crate::image::ImageFileFormat,
Expand Down Expand Up @@ -503,6 +505,26 @@ impl GLRenderer
.get_webgl2_context(viewport_size_pixels)
}

/// Creates a `GLRenderer` from callback's result.
///
/// The parameter `viewport_size_pixels` should be set to
/// the initial canvas size, however this can be changed later using
/// [GLRenderer:: set_viewport_size_pixels()].
#[cfg(any(doc, doctest, target_arch = "wasm32"))]
pub fn new_for_web_canvas_from_callback<V, S>(
viewport_size_pixels: V,
webgl2_context_cb: S
) -> Result<Self, BacktraceError<GLRendererCreationError>>
where
V: Into<UVec2>,
S: FnOnce() -> WebGl2RenderingContext
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a public dependency on the web_sys crate. Would it be possible to create a new feature for this in Cargo.toml please?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why public? i see that on wasm32 target arch this dep is included with WebGl2RenderingContext feature . So it is working only on wasm32.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was unclear. I meant that this exposes WebGl2RenderingContext in the public Speedy2D API, which forces us to continue depending on web-sys in the future (to avoid breaking the code of existing Speedy2D users). If we ever want to replace web-sys with another library, we'd have to release a breaking v3.0.0 update.

I'm okay with this, as long as the use_existing_webgl2_context API is gated behind a feature. For example:

[features]
default = ["windowing", "image-loading"]
windowing = ["glutin"]
image-loading = ["image"]
web-sys-features = []
#[cfg(any(feature = "web-sys-features", doc, doctest))]
pub fn use_existing_webgl2_context(
    ...

Then the use_existing_webgl2_context function will only be enabled if web-sys-features is set.

{
WebCanvasElement::get_webgl2_context_from_callback(
viewport_size_pixels,
webgl2_context_cb
)
}

fn new_with_gl_backend<V: Into<UVec2>>(
viewport_size_pixels: V,
gl_backend: Rc<dyn GLBackend>,
Expand Down
19 changes: 19 additions & 0 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,25 @@ impl WebCanvasElement
)
}

pub fn get_webgl2_context_from_callback<V, CB>(
viewport_size_pixels: V,
cb: CB
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to pass a callback here, rather than just passing the web_sys::WebGl2RenderingContext directly?

E.g. the signature could be:

pub fn use_existing_webgl2_context(
        viewport_size_pixels: impl Into<UVec2>,
        context: web_sys::WebGl2RenderingContext
)  -> Result<GLRenderer, BacktraceError<GLRendererCreationError>>
{ ... }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason. Will change it.

) -> Result<GLRenderer, BacktraceError<GLRendererCreationError>>
where
V: Into<UVec2>,
CB: FnOnce() -> web_sys::WebGl2RenderingContext
{
let viewport_size_pixels = viewport_size_pixels.into();

let gl_context = glow::Context::from_webgl2_context(cb());

GLRenderer::new_with_gl_backend(
viewport_size_pixels,
Rc::new(GLBackendGlow::new(gl_context)),
GLVersion::WebGL2_0
)
}

#[cfg(feature = "windowing")]
pub fn set_buffer_dimensions(&self, size: &UVec2)
{
Expand Down
Loading