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

Fixing default Cursor for x11 #368

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,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 +1122,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
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
1 change: 0 additions & 1 deletion src/native/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,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
62 changes: 34 additions & 28 deletions src/native/linux_x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ 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")
eprintln!("set_window_size not implemented on linux/x11")
}

fn set_fullscreen(&mut self, fullscreen: bool) {
Expand Down Expand Up @@ -192,7 +192,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 @@ -634,32 +634,38 @@ impl X11Display {
let libx11 = &mut self.libx11;
let display = self.display;

let cursor = match cursor {
None => {
// empty_cursor was created during create_window
self.empty_cursor.unwrap()
}
Some(cursor_icon) => *self.cursor_cache.entry(cursor_icon).or_insert_with(|| {
(libx11.XCreateFontCursor)(
display,
match cursor_icon {
CursorIcon::Default => libx11::XC_left_ptr,
CursorIcon::Help => libx11::XC_question_arrow,
CursorIcon::Pointer => libx11::XC_hand2,
CursorIcon::Wait => libx11::XC_watch,
CursorIcon::Crosshair => libx11::XC_crosshair,
CursorIcon::Text => libx11::XC_xterm,
CursorIcon::Move => libx11::XC_fleur,
CursorIcon::NotAllowed => libx11::XC_pirate,
CursorIcon::EWResize => libx11::XC_sb_h_double_arrow,
CursorIcon::NSResize => libx11::XC_sb_v_double_arrow,
CursorIcon::NESWResize => libx11::XC_top_right_corner,
CursorIcon::NWSEResize => libx11::XC_top_left_corner,
},
)
}),
};
(libx11.XDefineCursor)(display, window, cursor);
if cursor == Some(CursorIcon::Default) {
Copy link
Owner

Choose a reason for hiding this comment

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

I understand the problem, but I do not really like the extra branching and a special case for CursorIcon::Default, can't really propose anything better right now tho, will take a closer look into it a bit later

(libx11.XUndefineCursor)(display, window);
} else {
let cursor = match cursor {
None => {
// empty_cursor was created during create_window
self.empty_cursor.unwrap()
}
Some(cursor_icon) => *self.cursor_cache.entry(cursor_icon).or_insert_with(|| {
(libx11.XCreateFontCursor)(
display,
match cursor_icon {
CursorIcon::Default => libx11::XC_left_ptr,
CursorIcon::Help => libx11::XC_question_arrow,
CursorIcon::Pointer => libx11::XC_hand2,
CursorIcon::Wait => libx11::XC_watch,
CursorIcon::Crosshair => libx11::XC_crosshair,
CursorIcon::Text => libx11::XC_xterm,
CursorIcon::Move => libx11::XC_fleur,
CursorIcon::NotAllowed => libx11::XC_pirate,
CursorIcon::EWResize => libx11::XC_sb_h_double_arrow,
CursorIcon::NSResize => libx11::XC_sb_v_double_arrow,
CursorIcon::NESWResize => libx11::XC_top_right_corner,
CursorIcon::NWSEResize => libx11::XC_top_left_corner,
},
)
}),
};
(libx11.XDefineCursor)(display, window, cursor);
}


}

// pub unsafe fn process_requests(&mut self, window: Window, event_handler: &mut super::UserData) {
Expand Down
6 changes: 3 additions & 3 deletions src/native/linux_x11/glx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,19 @@ impl Glx {

if (libgl.glxQueryExtension.unwrap())(display.display, &mut errorbase, &mut eventbase) == 0
{
println!("GLX: GLX extension not found");
eprintln!("GLX: GLX extension not found");
return None;
}

let mut glx_major = 0;
let mut glx_minor = 0;

if (libgl.glxQueryVersion.unwrap())(display.display, &mut glx_major, &mut glx_minor) == 0 {
println!("GLX: Failed to query GLX version");
eprintln!("GLX: Failed to query GLX version");
return None;
}
if glx_major == 1 && glx_minor < 3 {
println!("GLX: GLX version 1.3 is required");
eprintln!("GLX: GLX version 1.3 is required");
return None;
}

Expand Down
3 changes: 3 additions & 0 deletions src/native/linux_x11/libx11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ pub type XCreatePixmapCursor = unsafe extern "C" fn(
) -> Cursor;
pub type XFreePixmap = unsafe extern "C" fn(_: *mut Display, _: Pixmap) -> libc::c_int;
pub type XDefineCursor = unsafe extern "C" fn(_: *mut Display, _: Window, _: Cursor) -> libc::c_int;
pub type XUndefineCursor = unsafe extern "C" fn(_: *mut Display, _: Window) -> libc::c_int;

pub struct LibX11 {
pub module: module::Module,
Expand Down Expand Up @@ -1023,6 +1024,7 @@ pub struct LibX11 {
pub XCreatePixmapCursor: XCreatePixmapCursor,
pub XFreePixmap: XFreePixmap,
pub XDefineCursor: XDefineCursor,
pub XUndefineCursor: XUndefineCursor,
}

impl LibX11 {
Expand Down Expand Up @@ -1076,6 +1078,7 @@ impl LibX11 {
XCreatePixmapCursor: module.get_symbol("XCreatePixmapCursor").unwrap(),
XFreePixmap: module.get_symbol("XFreePixmap").unwrap(),
XDefineCursor: module.get_symbol("XDefineCursor").unwrap(),
XUndefineCursor: module.get_symbol("XUndefineCursor").unwrap(),
module,
})
.ok()
Expand Down
Loading