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

Add a get_info method to get GL_VENDOR, GL_RENDERER, GL_VERSION #375

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
14 changes: 13 additions & 1 deletion js/gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ function animation() {
const SAPP_EVENTTYPE_TOUCHES_BEGAN = 10;
const SAPP_EVENTTYPE_TOUCHES_MOVED = 11;
const SAPP_EVENTTYPE_TOUCHES_ENDED = 12;
const SAPP_EVENTTYPE_TOUCHES_CANCELLED = 13;
const SAPP_EVENTTYPE_TOUCHES_CANCELED = 13;

const SAPP_MODIFIER_SHIFT = 1;
const SAPP_MODIFIER_CTRL = 2;
Expand Down Expand Up @@ -1253,6 +1253,18 @@ var importObject = {
wasm_exports.on_files_dropped_finish();
};

let lastFocus = document.hasFocus();
var checkFocus = function() {
let hasFocus = document.hasFocus();
if(lastFocus == hasFocus){
wasm_exports.focus(hasFocus);
lastFocus = hasFocus;
}
}
document.addEventListener("visibilitychange", checkFocus);
window.addEventListener("focus", checkFocus);
window.addEventListener("blur", checkFocus);

window.requestAnimationFrame(animation);
},

Expand Down
8 changes: 6 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,15 @@ pub trait EventHandler {
fn raw_mouse_motion(&mut self, _ctx: &mut Context, _dx: f32, _dy: f32) {}

/// Window has been minimized
/// Right now is only implemented on Android, and is called on a Pause ndk callback
/// Right now is only implemented on Android, X11 and wasm,
/// On Andoid window_minimized_event is called on a Pause ndk callback
/// On X11 and wasm it will be called on focus change events.
fn window_minimized_event(&mut self, _ctx: &mut Context) {}

/// Window has been restored
/// Right now is only implemented on Android, and is called on a Resume ndk callback
/// Right now is only implemented on Android, X11 and wasm,
/// On Andoid window_minimized_event is called on a Pause ndk callback
/// On X11 and wasm it will be called on focus change events.
fn window_restored_event(&mut self, _ctx: &mut Context) {}

/// This event is sent when the userclicks the window's close button
Expand Down
43 changes: 38 additions & 5 deletions src/graphics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{ffi::CString, mem};
use std::ffi::CStr;
use std::ffi::CString;
use std::mem;

mod texture;

Expand Down Expand Up @@ -634,14 +636,12 @@ pub const MAX_SHADERSTAGE_IMAGES: usize = 12;

pub struct Features {
pub instancing: bool,
pub alpha_texture: bool,
}

impl Features {
pub fn from_gles2(is_gles2: bool) -> Self {
Features {
instancing: !is_gles2,
alpha_texture: is_gles2,
}
}
}
Expand Down Expand Up @@ -1124,8 +1124,8 @@ impl GraphicsContext {
);

if !self.features.instancing && num_instances != 1 {
println!("Instanced rendering is not supported by the GPU");
println!("Ignoring this draw call");
eprintln!("Instanced rendering is not supported by the GPU");
eprintln!("Ignoring this draw call");
return;
}

Expand Down Expand Up @@ -2004,3 +2004,36 @@ impl ElapsedQuery {
self.gl_query = 0;
}
}

#[allow(dead_code)]
#[derive(Debug)]
pub struct Info {
pub vendor: String,
pub renderer: String,
pub version: String,
}

impl Info {
pub fn get_info() -> Info {
Info {
vendor: unsafe {
CStr::from_ptr(glGetString(GL_VENDOR) as *const i8)
.to_str()
.unwrap()
.to_string()
},
renderer: unsafe {
CStr::from_ptr(glGetString(GL_RENDERER) as *const i8)
.to_str()
.unwrap()
.to_string()
},
version: unsafe {
CStr::from_ptr(glGetString(GL_VERSION) as *const i8)
.to_str()
.unwrap()
.to_string()
},
}
}
}
69 changes: 26 additions & 43 deletions src/graphics/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,23 @@ pub enum TextureFormat {
RGBA8,
Depth,
Alpha,
LuminanceAlpha,
}

impl TextureFormat {
/// Converts from TextureFormat to (internal_format, format, pixel_type)
fn into_gl_params(self, alpha_texture: bool) -> (GLenum, GLenum, GLenum) {
match self {
/// Converts from TextureFormat to (internal_format, format, pixel_type)
impl From<TextureFormat> for (GLenum, GLenum, GLenum) {
fn from(format: TextureFormat) -> Self {
match format {
TextureFormat::RGB8 => (GL_RGB, GL_RGB, GL_UNSIGNED_BYTE),
TextureFormat::RGBA8 => (GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE),
TextureFormat::Depth => (GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT),

#[cfg(target_arch = "wasm32")]
TextureFormat::Alpha => (GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE),
#[cfg(not(target_arch = "wasm32"))]
TextureFormat::Alpha if alpha_texture => (GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE),
#[cfg(not(target_arch = "wasm32"))]
TextureFormat::Alpha => (GL_R8, GL_RED, GL_UNSIGNED_BYTE), // texture updates will swizzle Red -> Alpha to match WASM

#[cfg(target_arch = "wasm32")]
TextureFormat::LuminanceAlpha => {
(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE)
}
#[cfg(not(target_arch = "wasm32"))]
TextureFormat::LuminanceAlpha if alpha_texture => {
(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE)
}
#[cfg(not(target_arch = "wasm32"))]
TextureFormat::LuminanceAlpha => (GL_RG, GL_RG, GL_UNSIGNED_BYTE), // texture updates will swizzle Green -> Alpha to match WASM
}
}

}
impl TextureFormat {
/// Returns the size in bytes of texture with `dimensions`.
pub fn size(self, width: u32, height: u32) -> u32 {
let square = width * height;
Expand All @@ -93,7 +79,6 @@ impl TextureFormat {
TextureFormat::RGBA8 => 4 * square,
TextureFormat::Depth => 2 * square,
TextureFormat::Alpha => 1 * square,
TextureFormat::LuminanceAlpha => 2 * square,
}
}
}
Expand Down Expand Up @@ -163,8 +148,7 @@ impl Texture {
);
}

let (internal_format, format, pixel_type) =
params.format.into_gl_params(ctx.features().alpha_texture);
let (internal_format, format, pixel_type) = params.format.into();

ctx.cache.store_texture_binding(0);

Expand Down Expand Up @@ -195,21 +179,16 @@ impl Texture {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, params.filter as i32);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, params.filter as i32);

#[cfg(not(target_arch = "wasm32"))]
match params.format {
// on non-WASM alpha value is stored in red channel
// swizzle red -> alpha, zero red
TextureFormat::Alpha if !ctx.features().alpha_texture => {
if cfg!(not(target_arch = "wasm32")) {
// if not WASM
if params.format == TextureFormat::Alpha {
// if alpha miniquad texture, the value on non-WASM is stored in red channel
// swizzle red -> alpha
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED as _);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ZERO as _);
} else {
// keep alpha -> alpha
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA as _);
}
// on non-WASM luminance is stored in red channel, alpha is stored in green channel
// keep red, swizzle green -> alpha, zero green
TextureFormat::LuminanceAlpha if !ctx.features().alpha_texture => {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_GREEN as _);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ZERO as _);
}
_ => {}
}
}
ctx.cache.restore_texture_binding(0);
Expand Down Expand Up @@ -256,7 +235,12 @@ impl Texture {
}

/// Set the min and mag filter separately
pub fn set_filter_min_mag(&self, ctx: &mut Context, min_filter: FilterMode, mag_filter: FilterMode) {
pub fn set_filter_min_mag(
&self,
ctx: &mut Context,
min_filter: FilterMode,
mag_filter: FilterMode,
) {
ctx.cache.store_texture_binding(0);
ctx.cache.bind_texture(0, self.texture);
unsafe {
Expand Down Expand Up @@ -292,8 +276,7 @@ impl Texture {
ctx.cache.store_texture_binding(0);
ctx.cache.bind_texture(0, self.texture);

let (internal_format, format, pixel_type) =
self.format.into_gl_params(ctx.features().alpha_texture);
let (internal_format, format, pixel_type) = self.format.into();

self.width = width;
self.height = height;
Expand Down Expand Up @@ -351,7 +334,7 @@ impl Texture {
ctx.cache.store_texture_binding(0);
ctx.cache.bind_texture(0, self.texture);

let (_, format, pixel_type) = self.format.into_gl_params(ctx.features().alpha_texture);
let (_, format, pixel_type) = self.format.into();

unsafe {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // miniquad always uses row alignment of 1
Expand All @@ -374,10 +357,10 @@ impl Texture {

/// Read texture data into CPU memory
pub fn read_pixels(&self, bytes: &mut [u8]) {
if self.format == TextureFormat::Alpha || self.format == TextureFormat::LuminanceAlpha {
unimplemented!("read_pixels is not implement for Alpha and LuminanceAlpha textures");
if self.format == TextureFormat::Alpha {
unimplemented!("read_pixels is not implement for Alpha textures");
}
let (_, format, pixel_type) = self.format.into_gl_params(false);
let (_, format, pixel_type) = self.format.into();

let mut fbo = 0;
unsafe {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ where
}
conf::LinuxBackend::X11WithWaylandFallback => {
if native::linux_x11::run(&conf, f).is_none() {
println!("Failed to initialize through X11! Trying wayland instead");
eprintln!("Failed to initialize through X11! Trying wayland instead");
native::linux_wayland::run(&conf, f);
}
}
conf::LinuxBackend::WaylandWithX11Fallback => {
if native::linux_wayland::run(&conf, f).is_none() {
println!("Failed to initialize through wayland! Trying X11 instead");
eprintln!("Failed to initialize through wayland! Trying X11 instead");
native::linux_x11::run(&conf, f);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/native/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub const GL_TIME_ELAPSED: u32 = 35007;
pub const GL_QUERY_RESULT: u32 = 34918;
pub const GL_QUERY_RESULT_AVAILABLE: u32 = 34919;
pub const GL_VENDOR: u32 = 0x1F00;
pub const GL_RENDERER: u32 = 0x1F01;
pub const GL_VERSION: u32 = 0x1F02;

pub const WGL_NUMBER_PIXEL_FORMATS_ARB: u32 = 0x2000;
Expand Down Expand Up @@ -635,7 +636,6 @@ pub unsafe fn is_gl2() -> bool {
.to_str()
.unwrap();

println!("GL_VERSION: {}", version_string);
version_string.is_empty()
|| version_string.starts_with("2")
|| version_string.starts_with("OpenGL ES 2")
Expand Down
5 changes: 2 additions & 3 deletions src/native/linux_wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ unsafe extern "C" fn registry_add_object(
let display = &mut payload.display;

let interface = std::ffi::CStr::from_ptr(interface).to_str().unwrap();
println!("{:?}", interface);
match interface {
"wl_compositor" => {
display.compositor = display.client.wl_registry_bind(
Expand Down Expand Up @@ -315,7 +314,7 @@ where

let wdisplay = (client.wl_display_connect)(std::ptr::null_mut());
if wdisplay.is_null() {
println!("Failed to connect to Wayland payload.display.");
eprintln!("Failed to connect to Wayland payload.display.");
return None;
}

Expand Down Expand Up @@ -371,7 +370,7 @@ where
assert!(payload.display.seat.is_null() == false);

if payload.display.decoration_manager.is_null() {
println!("Decoration manager not found, will draw fallback decorations");
eprintln!("Decoration manager not found, will draw fallback decorations");
}

let mut libegl = egl::LibEgl::try_load()?;
Expand Down
32 changes: 23 additions & 9 deletions src/native/linux_x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ impl crate::native::NativeDisplay for X11Display {
}
}

fn set_window_size(&mut self, _new_width: u32, _new_height: u32) {
println!("set_window_size not implemented on linux/x11")
fn set_window_size(&mut self, new_width: u32, new_height: u32) {
unsafe {
self.set_window_size(self.window, new_width as i32, new_height as i32);
}
}

fn set_fullscreen(&mut self, fullscreen: bool) {
Expand Down Expand Up @@ -192,7 +194,7 @@ impl X11Display {
mut _display: *mut Display,
event: *mut XErrorEvent,
) -> libc::c_int {
println!("Error: {}", (*event).error_code);
eprintln!("Error: {}", (*event).error_code);
return 0 as libc::c_int;
}

Expand Down Expand Up @@ -347,9 +349,14 @@ impl X11Display {
(self.libx11.XFlush)(self.display);
}

// TODO: _fullscreen is not used, this function always setting window fullscreen
// should be able to able to go back from fullscreen to windowed instead
unsafe fn set_fullscreen(&mut self, window: Window, _fullscreen: bool) {
unsafe fn set_window_size(&mut self, window: Window, new_width: i32, new_height: i32) {
(self.libx11.XResizeWindow)(self.display, window, new_width, new_height);
(self.libx11.XFlush)(self.display);
}

// Not 100% this is the correct way to leave fullscreen, but seems to work.
// TODO: Restore window size.
unsafe fn set_fullscreen(&mut self, window: Window, fullscreen: bool) {
let wm_state = (self.libx11.XInternAtom)(
self.display,
b"_NET_WM_STATE\x00" as *const u8 as *const _,
Expand All @@ -369,7 +376,7 @@ impl X11Display {
(self.libx11.XUnmapWindow)(self.display, window);
(self.libx11.XSync)(self.display, false as _);

let mut atoms: [Atom; 2] = [wm_fullscreen, 0 as _];
let mut atoms: [Atom; 2] = [if fullscreen { wm_fullscreen } else { 0 as _ }, 0 as _];
(self.libx11.XChangeProperty)(
self.display,
window,
Expand All @@ -378,7 +385,7 @@ impl X11Display {
32,
PropModeReplace,
atoms.as_mut_ptr() as *mut _ as *mut _,
1,
if fullscreen { 1 } else { 0 },
);
(self.libx11.XMapWindow)(self.display, window);
(self.libx11.XRaiseWindow)(self.display, window);
Expand All @@ -390,7 +397,8 @@ impl X11Display {
{
let mut data = [0isize; 5];

data[0] = 1;
// changing this 1 to 0 seems to help with closing fullscreen
data[0] = if fullscreen { 1 } else { 0 };
data[1] = wm_fullscreen as isize;
data[2] = 0;

Expand Down Expand Up @@ -574,6 +582,12 @@ impl X11Display {
let y = (*event).xmotion.y as libc::c_float;
event_handler.mouse_motion_event(context.with_display(&mut *self), x, y);
}
9 => {
event_handler.window_restored_event(context.with_display(&mut *self));
}
10 => {
event_handler.window_minimized_event(context.with_display(&mut *self));
}
22 => {
if (*event).xconfigure.width != self.data.screen_width
|| (*event).xconfigure.height != self.data.screen_height
Expand Down
Loading