8000 native/wasm: optional WebGL2 support · not-fl3/miniquad@65f1da6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65f1da6

Browse files
committed
native/wasm: optional WebGL2 support
1 parent 833799e commit 65f1da6

5 files changed

Lines changed: 107 additions & 72 deletions

File tree

‎examples/quad.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ fn main() {
123123
} else {
124124
conf::AppleGfxApi::OpenGl
125125
};
126+
conf.platform.webgl_version = conf::WebGLVersion::WebGL2;
126127

127128
miniquad::start(conf, move || Box::new(Stage::new()));
128129
}

‎js/gl.js‎

Lines changed: 75 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88

99
"use strict";
1010

11-
const version = 1;
11+
const version = 2;
1212

1313
const canvas = document.querySelector("#glcanvas");
14-
const gl = canvas.getContext("webgl");
15-
if (gl === null) {
16-
alert("Unable to initialize WebGL. Your browser or machine may not support it.");
17-
}
14+
var gl;
1815

1916
var clipboard = null;
2017

@@ -27,6 +24,77 @@ var high_dpi = false;
2724
// if false, requestAnimationFrame will be called at the end of each frame
2825
var blocking_event_loop = false;
2926

27+
function init_webgl(version) {
28+
if (version == 1) {
29+
gl = canvas.getContext("webgl");
30+
31+
function acquireVertexArrayObjectExtension(ctx) {
32+
// Extension available in WebGL 1 from Firefox 25 and WebKit 536.28/desktop Safari 6.0.3 onwards. Core feature in WebGL 2.
33+
var ext = ctx.getExtension('OES_vertex_array_object');
34+
if (ext) {
35+
ctx['createVertexArray'] = function () { return ext['createVertexArrayOES'](); };
36+
ctx['deleteVertexArray'] = function (vao) { ext['deleteVertexArrayOES'](vao); };
37+
ctx['bindVertexArray'] = function (vao) { ext['bindVertexArrayOES'](vao); };
38+
ctx['isVertexArray'] = function (vao) { return ext['isVertexArrayOES'](vao); };
39+
}
40+
else {
41+
alert("Unable to get OES_vertex_array_object extension");
42+
}
43+
}
44+
45+
46+
function acquireInstancedArraysExtension(ctx) {
47+
// Extension available in WebGL 1 from Firefox 26 and Google Chrome 30 onwards. Core feature in WebGL 2.
48+
var ext = ctx.getExtension('ANGLE_instanced_arrays');
49+
if (ext) {
50+
ctx['vertexAttribDivisor'] = function (index, divisor) { ext['vertexAttribDivisorANGLE'](index, divisor); };
51+
ctx['drawArraysInstanced'] = function (mode, first, count, primcount) { ext['drawArraysInstancedANGLE'](mode, first, count, primcount); };
52+
ctx['drawElementsInstanced'] = function (mode, count, type, indices, primcount) { ext['drawElementsInstancedANGLE'](mode, count, type, indices, primcount); };
53+
}
54+
}
55+
56+
function acquireDisjointTimerQueryExtension(ctx) {
57+
var ext = ctx.getExtension('EXT_disjoint_timer_query')< 10000 /span>;
58+
if (ext) {
59+
ctx['createQuery'] = function () { return ext['createQueryEXT'](); };
60+
ctx['beginQuery'] = function (target, query) { return ext['beginQueryEXT'](target, query); };
61+
ctx['endQuery'] = function (target) { return ext['endQueryEXT'](target); };
62+
ctx['deleteQuery'] = function (query) { ext['deleteQueryEXT'](query); };
63+
ctx['getQueryObject'] = function (query, pname) { return ext['getQueryObjectEXT'](query, pname); };
64+
}
65+
}
66+
67+
function acquireDrawBuffers(ctx) {
68+
var ext = ctx.getExtension('WEBGL_draw_buffers');
69+
if (ext) {
70+
ctx['drawBuffers'] = function (bufs) { return ext['drawBuffersWEBGL'](bufs); };
71+
}
72+
}
73+
74+
try {
75+
gl.getExtension("EXT_shader_texture_lod");
76+
gl.getExtension("OES_standard_derivatives");
77+
} catch (e) {
78+
console.warn(e);
79+
}
80+
81+
acquireVertexArrayObjectExtension(gl);
82+
acquireInstancedArraysExtension(gl);
83+
acquireDisjointTimerQueryExtension(gl);
84+
acquireDrawBuffers(gl);
85+
86+
// https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_depth_texture
87+
if (gl.getExtension('WEBGL_depth_texture') == null) {
88+
alert("Cant initialize WEBGL_depth_texture extension");
89+
}
90+
} else {
91+
gl = canvas.getContext("webgl2");
92+
}
93+
if (gl === null) {
94+
alert("Unable to initialize WebGL. Your browser or machine may not support it.");
95+
}
96+
}
97+
3098
canvas.focus();
3199

32100
canvas.requestPointerLock = canvas.requestPointerLock ||
@@ -45,66 +113,6 @@ function assert(flag, message) {
45113
}
46114
}
47115

48-
function acquireVertexArrayObjectExtension(ctx) {
49-
// Extension available in WebGL 1 from Firefox 25 and WebKit 536.28/desktop Safari 6.0.3 onwards. Core feature in WebGL 2.
50-
var ext = ctx.getExtension('OES_vertex_array_object');
51-
if (ext) {
52-
ctx['createVertexArray'] = function () { return ext['createVertexArrayOES'](); };
53-
ctx['deleteVertexArray'] = function (vao) { ext['deleteVertexArrayOES'](vao); };
54-
ctx['bindVertexArray'] = function (vao) { ext['bindVertexArrayOES'](vao); };
55-
ctx['isVertexArray'] = function (vao) { return ext['isVertexArrayOES'](vao); };
56-
}
57-
else {
58-
alert("Unable to get OES_vertex_array_object extension");
59-
}
60-
}
61-
62-
63-
function acquireInstancedArraysExtension(ctx) {
64-
// Extension available in WebGL 1 from Firefox 26 and Google Chrome 30 onwards. Core feature in WebGL 2.
65-
var ext = ctx.getExtension('ANGLE_instanced_arrays');
66-
if (ext) {
67-
ctx['vertexAttribDivisor'] = function (index, divisor) { ext['vertexAttribDivisorANGLE'](index, divisor); };
68-
ctx['drawArraysInstanced'] = function (mode, first, count, primcount) { ext['drawArraysInstancedANGLE'](mode, first, count, primcount); };
69-
ctx['drawElementsInstanced'] = function (mode, count, type, indices, primcount) { ext['drawElementsInstancedANGLE'](mode, count, type, indices, primcount); };
70-
}
71-
}
72-
73-
function acquireDisjointTimerQueryExtension(ctx) {
74-
var ext = ctx.getExtension('EXT_disjoint_timer_query');
75-
if (ext) {
76-
ctx['createQuery'] = function () { return ext['createQueryEXT'](); };
77-
ctx['beginQuery'] = function (target, query) { return ext['beginQueryEXT'](target, query); };
78-
ctx['endQuery'] = function (target) { return ext['endQueryEXT'](target); };
79-
ctx['deleteQuery'] = function (query) { ext['deleteQueryEXT'](query); };
80-
ctx['getQueryObject'] = function (query, pname) { return ext['getQueryObjectEXT'](query, pname); };
81-
}
82-
}
83-
84-
function acquireDrawBuffers(ctx) {
85-
var ext = ctx.getExtension('WEBGL_draw_buffers');
86-
if (ext) {
87-
ctx['drawBuffers'] = function (bufs) { return ext['drawBuffersWEBGL'](bufs); };
88-
}
89-
}
90-
91-
try {
92-
gl.getExtension("EXT_shader_texture_lod");
93-
gl.getExtension("OES_standard_derivatives");
94-
} catch (e) {
95-
console.warn(e);
96-
}
97-
98-
acquireVertexArrayObjectExtension(gl);
99-
acquireInstancedArraysExtension(gl);
100-
acquireDisjointTimerQueryExtension(gl);
101-
acquireDrawBuffers(gl);
102-
103-
// https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_depth_texture
104-
if (gl.getExtension('WEBGL_depth_texture') == null) {
105-
alert("Cant initialize WEBGL_depth_texture extension");
106-
}
107-
108116
function getArray(ptr, arr, n) {
109117
return new arr(wasm_memory.buffer, ptr, n);
110118
}
@@ -1403,7 +1411,8 @@ var importObject = {
14031411
window.cancelAnimationFrame(animation_frame_timeout);
14041412
}
14051413
animation_frame_timeout = window.requestAnimationFrame(animation);
1406-
}
1414+
},
1415+
init_webgl
14071416
}
14081417
};
14091418

‎src/conf.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ pub enum AppleGfxApi {
6868
Metal,
6969
}
7070

71+
#[derive(Debug, PartialEq, Clone, Copy)]
72+
pub enum WebGLVersion {
73+
WebGL1,
74+
WebGL2
75+
}
76+
7177
/// Platform specific settings.
7278
#[derive(Debug)]
7379
pub struct Platform {
@@ -84,6 +90,12 @@ pub struct Platform {
8490
/// Defaults to X11Only. Wayland implementation is way too unstable right now.
8591
pub linux_backend: LinuxBackend,
8692

93+
/// While miniquad itself only use webgl1 features, withing webgl2 context it
94+
/// is possible to:
95+
/// - use gles3 shaders
96+
/// - do raw webgl2 opengl calls
97+
pub webgl_version: WebGLVersion,
98+
8799
/// Which rendering context to create, Metal or OpenGL.
88100
/// Miniquad always links with Metal.framework (assuming it is always present)
89101
/// but it links with OpenGL dynamically and only if required.
@@ -122,6 +134,7 @@ impl Default for Platform {
122134
linux_x11_gl: LinuxX11Gl::GLXWithEGLFallback,
123135
linux_backend: LinuxBackend::X11Only,
124136
apple_gfx_api: AppleGfxApi::OpenGl,
137+
webgl_version: WebGLVersion::WebGL1,
125138
blocking_event_loop: false,
126139
swap_interval: None,
127140
framebuffer_alpha: false,

‎src/graphics/gl.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,6 @@ impl RenderingBackend for GlContext {
745745
.to_str()
746746
.unwrap()
747747
.to_string();
748-
let gles3 = gl_version_string.contains("OpenGL ES 3");
749748
//let gles2 = !gles3 && gl_version_string.contains("OpenGL ES");
750749

751750
let mut glsl_support = GlslSupport::default();
@@ -760,10 +759,17 @@ impl RenderingBackend for GlContext {
760759
{
761760
// on web, miniquad always loads EXT_shader_texture_lod and OES_standard_derivatives
762761
glsl_support.v100_ext = true;
762+
763+
let webgl2 = gl_version_string.contains("WebGL 2.0");
764+
if webgl2 {
765+
glsl_support.v300es = true;
766+
}
763767
}
764768

765769
#[cfg(not(target_arch = "wasm32"))]
766770
{
771+
let gles3 = gl_version_string.contains("OpenGL ES 3");
772+
767773
if gles3 {
768774
glsl_support.v300es = true;
769775
}
@@ -810,7 +816,7 @@ impl RenderingBackend for GlContext {
810816
}
811817

812818
fn delete_texture(&mut self, texture: TextureId) {
813-
self.cache.clear_texture_bindings();
819+
//self.cache.clear_texture_bindings();
814820

815821
let t = self.textures.get(texture);
816822
unsafe {

‎src/native/wasm.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ pub mod webgl;
33

44
mod keycodes;
55

6-
pub use webgl::*;
7-
86
use std::{
97
cell::RefCell,
108
path::PathBuf,
@@ -67,6 +65,14 @@ where
6765
}));
6866
}
6967

68+
let version = match conf.platform.webgl_version {
69+
crate::conf::WebGLVersion::WebGL1 => 1,
70+
crate::conf::WebGLVersion::WebGL2 => 2,
71+
};
72+
unsafe {
73+
init_webgl(version);
74+
}
75+
7076
// setup initial canvas size
7177
unsafe {
7278
setup_canvas_size(conf.high_dpi);
@@ -128,6 +134,7 @@ extern "C" {
128134
pub fn sapp_is_fullscreen() -> bool;
129135
pub fn sapp_set_window_size(new_width: u32, new_height: u32);
130136
pub fn sapp_schedule_update();
137+
pub fn init_webgl(version: i32);
131138
pub fn now() -> f64;
132139
}
133140

@@ -173,7 +180,7 @@ pub unsafe fn update_cursor() {
173180
// "crate_version" is a misleading, but it can't be changed for legacy reasons.
174181
#[no_mangle]
175182
pub extern "C" fn crate_version() -> u32 {
176-
1
183+
2
177184
}
178185

179186
#[no_mangle]
@@ -213,7 +220,6 @@ pub extern "C" fn on_clipboard_paste(msg: *mut u8, len: usize) {
213220
pub extern "C" fn frame() {
214221
REQUESTS.with(|r| {
215222
while let Ok(request) = r.borrow_mut().as_mut().unwrap().try_recv() {
216-
use Request::*;
217223
match request {
218224
Request::SetCursorGrab(grab) => unsafe { sapp_set_cursor_grab(grab) },
219225
Request::ShowMouse(show) => unsafe { show_mouse(show) },

0 commit comments

Comments
 (0)
0