use super::*;
unsafe extern "C" {
fn dlopen(filename: *const c_char, flag: i32) -> *mut c_void;
fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
fn open(pathname: *const c_char, flags: i32, ...) -> i32;
fn unlink(pathname: *const c_char) -> i32;
fn ftruncate(fd: i32, length: i64) -> i32;
fn mmap(
addr: *mut c_void,
length: usize,
prot: i32,
flags: i32,
fd: i32,
offset: isize,
) -> *mut c_void;
fn munmap(addr: *mut c_void, length: usize) -> i32;
fn close(fd: i32) -> i32;
fn getpid() -> i32;
fn poll(fds: *mut c_void, nfds: usize, timeout: i32) -> i32;
fn pipe2(fds: *mut i32, flags: i32) -> i32;
fn read(fd: i32, buf: *mut c_void, count: usize) -> isize;
fn write(fd: i32, buf: *const c_void, count: usize) -> isize;
}
pub(crate) fn linux_dlsym(handle: *mut c_void, sym: &'static str) -> *mut c_void {
if handle.is_null() {
return null_mut();
}
unsafe { dlsym(handle, sym.as_ptr().cast::<c_char>()) }
}
pub(crate) fn linux_x11_syms() -> &'static LinuxX11Syms {
LINUX_X11_SYMS.get_or_init(|| -> LinuxX11Syms {
unsafe {
let x11: *mut c_void = dlopen(LINUX_LIB_X11.as_ptr().cast::<c_char>(), RTLD_LAZY);
if x11.is_null() {
return LinuxX11Syms::default();
}
LinuxX11Syms {
lib: x11 as usize,
open_display: linux_dlsym(x11, SYM_X_OPEN_DISPLAY) as usize,
default_screen: linux_dlsym(x11, SYM_X_DEFAULT_SCREEN) as usize,
root_window: linux_dlsym(x11, SYM_X_ROOT_WINDOW) as usize,
create_simple_window: linux_dlsym(x11, SYM_X_CREATE_SIMPLE_WINDOW) as usize,
store_name: linux_dlsym(x11, SYM_X_STORE_NAME) as usize,
map_window: linux_dlsym(x11, SYM_X_MAP_WINDOW) as usize,
resize_window: linux_dlsym(x11, SYM_X_RESIZE_WINDOW) as usize,
destroy_window: linux_dlsym(x11, SYM_X_DESTROY_WINDOW) as usize,
flush: linux_dlsym(x11, SYM_X_FLUSH) as usize,
default_gc: linux_dlsym(x11, SYM_X_DEFAULT_GC) as usize,
default_visual: linux_dlsym(x11, SYM_X_DEFAULT_VISUAL) as usize,
default_depth: linux_dlsym(x11, SYM_X_DEFAULT_DEPTH) as usize,
create_image: linux_dlsym(x11, SYM_X_CREATE_IMAGE) as usize,
put_image: linux_dlsym(x11, SYM_X_PUT_IMAGE) as usize,
free: linux_dlsym(x11, SYM_X_FREE) as usize,
select_input: linux_dlsym(x11, SYM_X_SELECT_INPUT) as usize,
intern_atom: linux_dlsym(x11, SYM_X_INTERN_ATOM) as usize,
set_wm_protocols: linux_dlsym(x11, SYM_X_SET_WM_PROTOCOLS) as usize,
pending: linux_dlsym(x11, SYM_X_PENDING) as usize,
next_event: linux_dlsym(x11, SYM_X_NEXT_EVENT) as usize,
lookup_string: linux_dlsym(x11, SYM_X_LOOKUP_STRING) as usize,
resource_manager_string: linux_dlsym(x11, SYM_X_RESOURCE_MANAGER_STRING) as usize,
connection_number: linux_dlsym(x11, SYM_X_CONNECTION_NUMBER) as usize,
}
}
})
}
pub(crate) fn linux_xrandr_syms() -> &'static LinuxXRandrSyms {
LINUX_XRANDR_SYMS.get_or_init(|| -> LinuxXRandrSyms {
unsafe {
let xrandr: *mut c_void = dlopen(LINUX_LIB_XRANDR.as_ptr().cast::<c_char>(), RTLD_LAZY);
if xrandr.is_null() {
return LinuxXRandrSyms::default();
}
LinuxXRandrSyms {
get_screen_info: linux_dlsym(xrandr, SYM_XRR_GET_SCREEN_INFO) as usize,
config_current_rate: linux_dlsym(xrandr, SYM_XRR_CONFIG_CURRENT_RATE) as usize,
free_screen_config_info: linux_dlsym(xrandr, SYM_XRR_FREE_SCREEN_CONFIG_INFO)
as usize,
}
}
})
}
pub(crate) fn linux_xshm_syms() -> &'static LinuxXShmSyms {
LINUX_XSHM_SYMS.get_or_init(|| -> LinuxXShmSyms {
unsafe {
let xext: *mut c_void = dlopen(LINUX_LIB_XEXT.as_ptr().cast::<c_char>(), RTLD_LAZY);
if xext.is_null() {
return LinuxXShmSyms::default();
}
LinuxXShmSyms {
query_extension: linux_dlsym(xext, SYM_XSHM_QUERY_EXTENSION) as usize,
create_image: linux_dlsym(xext, SYM_XSHM_CREATE_IMAGE) as usize,
attach: linux_dlsym(xext, SYM_XSHM_ATTACH) as usize,
detach: linux_dlsym(xext, SYM_XSHM_DETACH) as usize,
put_image: linux_dlsym(xext, SYM_XSHM_PUT_IMAGE) as usize,
}
}
})
}
pub(crate) fn linux_wayland_syms() -> &'static LinuxWaylandSyms {
LINUX_WAYLAND_SYMS.get_or_init(|| -> LinuxWaylandSyms {
unsafe {
let vk: *mut c_void = dlopen(LINUX_LIB_VULKAN.as_ptr().cast::<c_char>(), RTLD_LAZY);
let vulkan_wsi_available: bool =
!vk.is_null() && !linux_dlsym(vk, SYM_VK_CREATE_WAYLAND_SURFACE).is_null();
let xlib_vulkan_wsi_available: bool =
!vk.is_null() && !linux_dlsym(vk, SYM_VK_CREATE_XLIB_SURFACE).is_null();
let wl: *mut c_void = dlopen(LINUX_LIB_WAYLAND.as_ptr().cast::<c_char>(), RTLD_LAZY);
if wl.is_null() {
return LinuxWaylandSyms {
vulkan_wsi_available,
xlib_vulkan_wsi_available,
..LinuxWaylandSyms::default()
};
}
LinuxWaylandSyms {
lib: wl as usize,
display_connect: linux_dlsym(wl, SYM_WL_DISPLAY_CONNECT) as usize,
display_disconnect: linux_dlsym(wl, SYM_WL_DISPLAY_DISCONNECT) as usize,
display_dispatch_pending: linux_dlsym(wl, SYM_WL_DISPLAY_DISPATCH_PENDING) as usize,
display_dispatch: linux_dlsym(wl, SYM_WL_DISPLAY_DISPATCH) as usize,
display_flush: linux_dlsym(wl, SYM_WL_DISPLAY_FLUSH) as usize,
display_roundtrip: linux_dlsym(wl, SYM_WL_DISPLAY_ROUNDTRIP) as usize,
display_get_error: linux_dlsym(wl, SYM_WL_DISPLAY_GET_ERROR) as usize,
display_get_fd: linux_dlsym(wl, SYM_WL_DISPLAY_GET_FD) as usize,
display_prepare_read: linux_dlsym(wl, SYM_WL_DISPLAY_PREPARE_READ) as usize,
display_read_events: linux_dlsym(wl, SYM_WL_DISPLAY_READ_EVENTS) as usize,
display_cancel_read: linux_dlsym(wl, SYM_WL_DISPLAY_CANCEL_READ) as usize,
proxy_marshal_flags: linux_dlsym(wl, SYM_WL_PROXY_MARSHAL_FLAGS) as usize,
proxy_add_listener: linux_dlsym(wl, SYM_WL_PROXY_ADD_LISTENER) as usize,
proxy_destroy: linux_dlsym(wl, SYM_WL_PROXY_DESTROY) as usize,
proxy_get_version: linux_dlsym(wl, SYM_WL_PROXY_GET_VERSION) as usize,
registry_interface: linux_dlsym(wl, SYM_WL_REGISTRY_INTERFACE) as usize,
compositor_interface: linux_dlsym(wl, SYM_WL_COMPOSITOR_INTERFACE) as usize,
surface_interface: linux_dlsym(wl, SYM_WL_SURFACE_INTERFACE) as usize,
shm_interface: linux_dlsym(wl, SYM_WL_SHM_INTERFACE) as usize,
shm_pool_interface: linux_dlsym(wl, SYM_WL_SHM_POOL_INTERFACE) as usize,
buffer_interface: linux_dlsym(wl, SYM_WL_BUFFER_INTERFACE) as usize,
callback_interface: linux_dlsym(wl, SYM_WL_CALLBACK_INTERFACE) as usize,
vulkan_wsi_available,
xlib_vulkan_wsi_available,
}
}
})
}
pub(crate) fn linux_egl_gles_syms() -> &'static LinuxEglGlesSyms {
LINUX_EGL_GLES_SYMS.get_or_init(|| -> LinuxEglGlesSyms {
unsafe {
let egl: *mut c_void = dlopen(LINUX_LIB_EGL.as_ptr().cast::<c_char>(), RTLD_LAZY);
let gles: *mut c_void = dlopen(LINUX_LIB_GLESV2.as_ptr().cast::<c_char>(), RTLD_LAZY);
let wl_egl: *mut c_void =
dlopen(LINUX_LIB_WAYLAND_EGL.as_ptr().cast::<c_char>(), RTLD_LAZY);
if egl.is_null() || gles.is_null() {
return LinuxEglGlesSyms::default();
}
let syms: LinuxEglGlesSyms = LinuxEglGlesSyms {
ready: false,
wayland_egl_ready: false,
egl_get_display: linux_dlsym(egl, SYM_EGL_GET_DISPLAY) as usize,
egl_initialize: linux_dlsym(egl, SYM_EGL_INITIALIZE) as usize,
egl_bind_api: linux_dlsym(egl, SYM_EGL_BIND_API) as usize,
egl_choose_config: linux_dlsym(egl, SYM_EGL_CHOOSE_CONFIG) as usize,
egl_create_context: linux_dlsym(egl, SYM_EGL_CREATE_CONTEXT) as usize,
egl_create_window_surface: linux_dlsym(egl, SYM_EGL_CREATE_WINDOW_SURFACE) as usize,
egl_make_current: linux_dlsym(egl, SYM_EGL_MAKE_CURRENT) as usize,
egl_swap_interval: linux_dlsym(egl, SYM_EGL_SWAP_INTERVAL) as usize,
egl_swap_buffers: linux_dlsym(egl, SYM_EGL_SWAP_BUFFERS) as usize,
egl_destroy_surface: linux_dlsym(egl, SYM_EGL_DESTROY_SURFACE) as usize,
egl_destroy_context: linux_dlsym(egl, SYM_EGL_DESTROY_CONTEXT) as usize,
egl_terminate: linux_dlsym(egl, SYM_EGL_TERMINATE) as usize,
wl_egl_window_create: linux_dlsym(wl_egl, SYM_WL_EGL_WINDOW_CREATE) as usize,
wl_egl_window_resize: linux_dlsym(wl_egl, SYM_WL_EGL_WINDOW_RESIZE) as usize,
wl_egl_window_destroy: linux_dlsym(wl_egl, SYM_WL_EGL_WINDOW_DESTROY) as usize,
gl_create_shader: linux_dlsym(gles, SYM_GL_CREATE_SHADER) as usize,
gl_shader_source: linux_dlsym(gles, SYM_GL_SHADER_SOURCE) as usize,
gl_compile_shader: linux_dlsym(gles, SYM_GL_COMPILE_SHADER) as usize,
gl_get_shaderiv: linux_dlsym(gles, SYM_GL_GET_SHADERIV) as usize,
gl_delete_shader: linux_dlsym(gles, SYM_GL_DELETE_SHADER) as usize,
gl_create_program: linux_dlsym(gles, SYM_GL_CREATE_PROGRAM) as usize,
gl_attach_shader: linux_dlsym(gles, SYM_GL_ATTACH_SHADER) as usize,
gl_link_program: linux_dlsym(gles, SYM_GL_LINK_PROGRAM) as usize,
gl_get_programiv: linux_dlsym(gles, SYM_GL_GET_PROGRAMIV) as usize,
gl_delete_program: linux_dlsym(gles, SYM_GL_DELETE_PROGRAM) as usize,
gl_use_program: linux_dlsym(gles, SYM_GL_USE_PROGRAM) as usize,
gl_get_uniform_location: linux_dlsym(gles, SYM_GL_GET_UNIFORM_LOCATION) as usize,
gl_uniform1i: linux_dlsym(gles, SYM_GL_UNIFORM1I) as usize,
gl_uniform1f: linux_dlsym(gles, GPU_GL_SYM_UNIFORM1F) as usize,
gl_uniform2f: linux_dlsym(gles, SYM_GL_UNIFORM2F) as usize,
gl_gen_vertex_arrays: linux_dlsym(gles, SYM_GL_GEN_VERTEX_ARRAYS) as usize,
gl_bind_vertex_array: linux_dlsym(gles, SYM_GL_BIND_VERTEX_ARRAY) as usize,
gl_delete_vertex_arrays: linux_dlsym(gles, SYM_GL_DELETE_VERTEX_ARRAYS) as usize,
gl_gen_buffers: linux_dlsym(gles, SYM_GL_GEN_BUFFERS) as usize,
gl_bind_buffer: linux_dlsym(gles, SYM_GL_BIND_BUFFER) as usize,
gl_buffer_data: linux_dlsym(gles, SYM_GL_BUFFER_DATA) as usize,
gl_buffer_sub_data: linux_dlsym(gles, SYM_GL_BUFFER_SUB_DATA) as usize,
gl_delete_buffers: linux_dlsym(gles, SYM_GL_DELETE_BUFFERS) as usize,
gl_enable_vertex_attrib_array: linux_dlsym(gles, SYM_GL_ENABLE_VERTEX_ATTRIB_ARRAY)
as usize,
gl_vertex_attrib_pointer: linux_dlsym(gles, SYM_GL_VERTEX_ATTRIB_POINTER) as usize,
gl_vertex_attrib_ipointer: linux_dlsym(gles, SYM_GL_VERTEX_ATTRIB_IPOINTER)
as usize,
gl_vertex_attrib_divisor: linux_dlsym(gles, SYM_GL_VERTEX_ATTRIB_DIVISOR) as usize,
gl_gen_textures: linux_dlsym(gles, SYM_GL_GEN_TEXTURES) as usize,
gl_bind_texture: linux_dlsym(gles, SYM_GL_BIND_TEXTURE) as usize,
gl_tex_parameteri: linux_dlsym(gles, SYM_GL_TEX_PARAMETERI) as usize,
gl_tex_storage_2d: linux_dlsym(gles, SYM_GL_TEX_STORAGE_2D) as usize,
gl_tex_image_2d: linux_dlsym(gles, SYM_GL_TEX_IMAGE_2D) as usize,
gl_tex_sub_image_2d: linux_dlsym(gles, SYM_GL_TEX_SUB_IMAGE_2D) as usize,
gl_delete_textures: linux_dlsym(gles, SYM_GL_DELETE_TEXTURES) as usize,
gl_active_texture: linux_dlsym(gles, SYM_GL_ACTIVE_TEXTURE) as usize,
gl_pixel_storei: linux_dlsym(gles, SYM_GL_PIXEL_STOREI) as usize,
gl_viewport: linux_dlsym(gles, SYM_GL_VIEWPORT) as usize,
gl_enable: linux_dlsym(gles, SYM_GL_ENABLE) as usize,
gl_disable: linux_dlsym(gles, SYM_GL_DISABLE) as usize,
gl_blend_func_separate: linux_dlsym(gles, SYM_GL_BLEND_FUNC_SEPARATE) as usize,
gl_scissor: linux_dlsym(gles, SYM_GL_SCISSOR) as usize,
gl_clear_color: linux_dlsym(gles, SYM_GL_CLEAR_COLOR) as usize,
gl_clear: linux_dlsym(gles, SYM_GL_CLEAR) as usize,
gl_draw_arrays_instanced: linux_dlsym(gles, SYM_GL_DRAW_ARRAYS_INSTANCED) as usize,
gl_draw_arrays: linux_dlsym(gles, SYM_GL_DRAW_ARRAYS) as usize,
gl_uniform4f: linux_dlsym(gles, SYM_GL_UNIFORM4F) as usize,
gl_uniform_matrix4fv: linux_dlsym(gles, SYM_GL_UNIFORM_MATRIX4FV) as usize,
gl_get_integerv: linux_dlsym(gles, SYM_GL_GET_INTEGERV) as usize,
gl_gen_framebuffers: linux_dlsym(gles, SYM_GL_GEN_FRAMEBUFFERS) as usize,
gl_bind_framebuffer: linux_dlsym(gles, SYM_GL_BIND_FRAMEBUFFER) as usize,
gl_framebuffer_texture_2d: linux_dlsym(gles, SYM_GL_FRAMEBUFFER_TEXTURE_2D)
as usize,
gl_delete_framebuffers: linux_dlsym(gles, SYM_GL_DELETE_FRAMEBUFFERS) as usize,
gl_gen_renderbuffers: linux_dlsym(gles, SYM_GL_GEN_RENDERBUFFERS) as usize,
gl_bind_renderbuffer: linux_dlsym(gles, SYM_GL_BIND_RENDERBUFFER) as usize,
gl_renderbuffer_storage: linux_dlsym(gles, SYM_GL_RENDERBUFFER_STORAGE) as usize,
gl_framebuffer_renderbuffer: linux_dlsym(gles, SYM_GL_FRAMEBUFFER_RENDERBUFFER)
as usize,
gl_delete_renderbuffers: linux_dlsym(gles, SYM_GL_DELETE_RENDERBUFFERS) as usize,
gl_depth_mask: linux_dlsym(gles, SYM_GL_DEPTH_MASK) as usize,
gl_depth_func: linux_dlsym(gles, SYM_GL_DEPTH_FUNC) as usize,
gl_clear_depthf: linux_dlsym(gles, SYM_GL_CLEAR_DEPTHF) as usize,
gl_draw_elements_instanced: linux_dlsym(gles, SYM_GL_DRAW_ELEMENTS_INSTANCED)
as usize,
gl_uniform4i: linux_dlsym(gles, SYM_GL_UNIFORM4I) as usize,
gl_uniform4fv: linux_dlsym(gles, SYM_GL_UNIFORM4FV) as usize,
gl_disable_vertex_attrib_array: linux_dlsym(
gles,
SYM_GL_DISABLE_VERTEX_ATTRIB_ARRAY,
) as usize,
gl_vertex_attrib_i4ui: linux_dlsym(gles, SYM_GL_VERTEX_ATTRIB_I4UI) as usize,
gl_vertex_attrib_4f: linux_dlsym(gles, SYM_GL_VERTEX_ATTRIB_4F) as usize,
gl_program_parameteri: linux_dlsym(gles, SYM_GL_PROGRAM_PARAMETERI) as usize,
gl_get_program_binary: linux_dlsym(gles, SYM_GL_GET_PROGRAM_BINARY) as usize,
gl_program_binary: linux_dlsym(gles, SYM_GL_PROGRAM_BINARY) as usize,
gl_get_string: linux_dlsym(gles, SYM_GL_GET_STRING) as usize,
};
let core_ok: bool = syms.get_egl_get_display() != 0
&& syms.get_egl_initialize() != 0
&& syms.get_egl_choose_config() != 0
&& syms.get_egl_create_context() != 0
&& syms.get_egl_create_window_surface() != 0
&& syms.get_egl_make_current() != 0
&& syms.get_egl_swap_buffers() != 0
&& syms.get_gl_create_shader() != 0
&& syms.get_gl_create_program() != 0
&& syms.get_gl_draw_arrays_instanced() != 0
&& syms.get_gl_vertex_attrib_divisor() != 0;
let wl_ok: bool = syms.get_wl_egl_window_create() != 0
&& syms.get_wl_egl_window_resize() != 0
&& syms.get_wl_egl_window_destroy() != 0;
LinuxEglGlesSyms {
ready: core_ok,
wayland_egl_ready: core_ok && wl_ok,
..syms
}
}
})
}
pub(crate) fn linux_create_wake_pipe() -> (i32, i32) {
let mut fds: [i32; 2] = [-1, -1];
let rc: i32 = unsafe { pipe2(fds.as_mut_ptr(), LINUX_PIPE_NONBLOCK_CLOEXEC) };
if rc == 0 { (fds[0], fds[1]) } else { (-1, -1) }
}
pub(crate) fn linux_signal_wake_pipe(write_fd: i32) -> bool {
if write_fd < 0 {
return false;
}
let byte: [u8; 1] = [1];
unsafe { write(write_fd, byte.as_ptr().cast::<c_void>(), 1) == 1 }
}
pub(crate) fn linux_drain_wake_pipe(read_fd: i32) {
if read_fd < 0 {
return;
}
let mut scratch: [u8; 64] = [0; 64];
unsafe {
while read(
read_fd,
scratch.as_mut_ptr().cast::<c_void>(),
scratch.len(),
) > 0
{}
}
}
pub(crate) unsafe extern "C" fn linux_wl_frame_callback_done(
data: *mut c_void,
callback: *mut c_void,
_callback_data: u32,
) {
if !data.is_null() {
let pending: &AtomicBool = unsafe { &*data.cast::<AtomicBool>() };
pending.store(false, Ordering::Release);
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if !callback.is_null() && wl.proxy_destroy != 0 {
unsafe {
let destroy_fn: unsafe extern "C" fn(*mut c_void) =
transmute(wl.proxy_destroy as *const c_void);
destroy_fn(callback);
}
}
}
pub(crate) fn linux_wl_arm_frame_callback(wl_surface: usize, vsync_flag: &AtomicBool) -> bool {
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl_surface == 0
|| wl.proxy_marshal_flags == 0
|| wl.proxy_add_listener == 0
|| wl.callback_interface == 0
{
return false;
}
if vsync_flag.swap(true, Ordering::AcqRel) {
return true;
}
unsafe {
let marshal: unsafe extern "C" fn(
*mut c_void,
u32,
*const c_void,
u32,
u32,
...
) -> *mut c_void = transmute(wl.proxy_marshal_flags as *const c_void);
let add_listener: unsafe extern "C" fn(*mut c_void, *const c_void, *mut c_void) -> i32 =
transmute(wl.proxy_add_listener as *const c_void);
let cb: *mut c_void = marshal(
wl_surface as *mut c_void,
WL_OPCODE_SURFACE_FRAME,
wl.callback_interface as *const c_void,
1,
0,
null_mut::<c_void>(),
);
if cb.is_null() {
vsync_flag.store(false, Ordering::Release);
return false;
}
let listener_ptr: *const c_void =
(&WL_CALLBACK_LISTENER as *const WlCallbackListener).cast();
let data_ptr: *mut c_void = (vsync_flag as *const AtomicBool).cast_mut().cast();
let _: i32 = add_listener(cb, listener_ptr, data_ptr);
true
}
}
unsafe fn linux_gles_compile_shader(
syms: &LinuxEglGlesSyms,
stage: u32,
source: &str,
) -> Option<u32> {
unsafe {
let create_shader: unsafe extern "C" fn(u32) -> u32 =
transmute(syms.get_gl_create_shader() as *const c_void);
let shader_source: unsafe extern "C" fn(u32, i32, *const *const c_char, *const i32) =
transmute(syms.get_gl_shader_source() as *const c_void);
let compile_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_compile_shader() as *const c_void);
let get_shaderiv: unsafe extern "C" fn(u32, u32, *mut i32) =
transmute(syms.get_gl_get_shaderiv() as *const c_void);
let delete_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_shader() as *const c_void);
let shader: u32 = create_shader(stage);
if shader == 0 {
return None;
}
let ptrs: [*const c_char; 1] = [source.as_ptr().cast::<c_char>()];
let lens: [i32; 1] = [source.len() as i32];
shader_source(shader, 1, ptrs.as_ptr(), lens.as_ptr());
compile_shader(shader);
let mut ok: i32 = 0;
get_shaderiv(shader, LINUX_GL_COMPILE_STATUS, &mut ok);
if ok == 0 {
delete_shader(shader);
return None;
}
Some(shader)
}
}
unsafe fn linux_gles_create_texture(
syms: &LinuxEglGlesSyms,
internal_fmt: u32,
pixel_fmt: u32,
width: u32,
height: u32,
pixels: &[u8],
alignment: i32,
) -> u32 {
unsafe {
let gen_textures: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_textures() as *const c_void);
let bind_texture: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let tex_param: unsafe extern "C" fn(u32, u32, i32) =
transmute(syms.get_gl_tex_parameteri() as *const c_void);
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
let tex_sub: unsafe extern "C" fn(u32, i32, i32, i32, i32, i32, u32, u32, *const c_void) =
transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
let mut tex: u32 = 0;
gen_textures(1, &mut tex);
bind_texture(LINUX_GL_TEXTURE_2D, tex);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MIN_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MAG_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_S,
LINUX_GL_CLAMP_TO_EDGE,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_T,
LINUX_GL_CLAMP_TO_EDGE,
);
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, alignment);
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
if syms.get_gl_tex_storage_2d() != 0 {
let tex_storage: unsafe extern "C" fn(u32, i32, u32, i32, i32) =
transmute(syms.get_gl_tex_storage_2d() as *const c_void);
tex_storage(
LINUX_GL_TEXTURE_2D,
1,
internal_fmt,
width.max(1) as i32,
height.max(1) as i32,
);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
width.max(1) as i32,
height.max(1) as i32,
pixel_fmt,
LINUX_GL_UNSIGNED_BYTE,
pixels.as_ptr().cast::<c_void>(),
);
} else {
let tex_image: unsafe extern "C" fn(
u32,
i32,
i32,
i32,
i32,
i32,
u32,
u32,
*const c_void,
) = transmute(syms.get_gl_tex_image_2d() as *const c_void);
tex_image(
LINUX_GL_TEXTURE_2D,
0,
internal_fmt as i32,
width.max(1) as i32,
height.max(1) as i32,
0,
pixel_fmt,
LINUX_GL_UNSIGNED_BYTE,
pixels.as_ptr().cast::<c_void>(),
);
}
tex
}
}
pub(crate) fn linux_egl_create_surface(
is_wayland: bool,
display: usize,
window: u64,
width: u32,
height: u32,
) -> Option<LinuxEglGlesSurface> {
let syms: &LinuxEglGlesSyms = linux_egl_gles_syms();
if display == 0 || window == 0 || !syms.get_ready() {
return None;
}
if is_wayland && !syms.get_wayland_egl_ready() {
return None;
}
let w: u32 = width.max(1);
let h: u32 = height.max(1);
unsafe {
let egl_get_display: unsafe extern "C" fn(*mut c_void) -> *mut c_void =
transmute(syms.get_egl_get_display() as *const c_void);
let egl_initialize: unsafe extern "C" fn(*mut c_void, *mut i32, *mut i32) -> u32 =
transmute(syms.get_egl_initialize() as *const c_void);
let egl_bind_api: unsafe extern "C" fn(u32) -> u32 =
transmute(syms.get_egl_bind_api() as *const c_void);
let egl_choose_config: unsafe extern "C" fn(
*mut c_void,
*const i32,
*mut *mut c_void,
i32,
*mut i32,
) -> u32 = transmute(syms.get_egl_choose_config() as *const c_void);
let egl_create_context: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*const i32,
) -> *mut c_void = transmute(syms.get_egl_create_context() as *const c_void);
let egl_create_window_surface: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
usize,
*const i32,
) -> *mut c_void = transmute(syms.get_egl_create_window_surface() as *const c_void);
let egl_make_current: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*mut c_void,
) -> u32 = transmute(syms.get_egl_make_current() as *const c_void);
let egl_swap_interval: unsafe extern "C" fn(*mut c_void, i32) -> u32 =
transmute(syms.get_egl_swap_interval() as *const c_void);
let egl_dpy: *mut c_void = egl_get_display(display as *mut c_void);
if egl_dpy.is_null() {
return None;
}
let mut major: i32 = 0;
let mut minor: i32 = 0;
if egl_initialize(egl_dpy, &mut major, &mut minor) == 0 {
return None;
}
let _: u32 = egl_bind_api(LINUX_EGL_OPENGL_ES_API);
let cfg_attribs: [i32; 13] = [
LINUX_EGL_SURFACE_TYPE,
LINUX_EGL_WINDOW_BIT,
LINUX_EGL_RENDERABLE_TYPE,
LINUX_EGL_OPENGL_ES3_BIT,
LINUX_EGL_RED_SIZE,
8,
LINUX_EGL_GREEN_SIZE,
8,
LINUX_EGL_BLUE_SIZE,
8,
LINUX_EGL_ALPHA_SIZE,
8,
LINUX_EGL_NONE,
];
let mut cfg: *mut c_void = null_mut();
let mut num_cfg: i32 = 0;
if egl_choose_config(egl_dpy, cfg_attribs.as_ptr(), &mut cfg, 1, &mut num_cfg) == 0
|| num_cfg < 1
|| cfg.is_null()
{
return None;
}
let ctx_attribs: [i32; 3] = [LINUX_EGL_CONTEXT_CLIENT_VERSION, 3, LINUX_EGL_NONE];
let egl_ctx: *mut c_void =
egl_create_context(egl_dpy, cfg, null_mut(), ctx_attribs.as_ptr());
if egl_ctx.is_null() {
return None;
}
let mut wl_egl_win: usize = 0;
let native_win_handle: usize = if is_wayland {
let create_wl_egl: unsafe extern "C" fn(*mut c_void, i32, i32) -> *mut c_void =
transmute(syms.get_wl_egl_window_create() as *const c_void);
let created: *mut c_void =
create_wl_egl(window as usize as *mut c_void, w as i32, h as i32);
if created.is_null() {
return None;
}
wl_egl_win = created as usize;
wl_egl_win
} else {
window as usize
};
let egl_surf: *mut c_void =
egl_create_window_surface(egl_dpy, cfg, native_win_handle, null());
let mut state: LinuxEglGlesSurface = LinuxEglGlesSurface {
egl_display: egl_dpy as usize,
egl_context: egl_ctx as usize,
egl_surface: egl_surf as usize,
wl_egl_window: wl_egl_win,
program: 0,
vao: 0,
quad_vbo: 0,
instance_vbo: 0,
instance_vbo_cap: 0,
glyph_r8_tex: 0,
glyph_r8_w: 1,
glyph_r8_h: 1,
glyph_r8_rev: 0,
glyph_rgba8_tex: 0,
image_tex: 0,
image_w: 1,
image_h: 1,
image_rev: 0,
external_tex: 0,
viewport_loc: -1,
scroll_loc: -1,
width: w,
height: h,
is_wayland,
max_tex_dim: 4096,
scene: None,
blur: None,
scratch_prims: Vec::new(),
scratch_ranges: Vec::new(),
vsync_pending: Box::new(AtomicBool::new(false)),
};
if egl_surf.is_null() || egl_make_current(egl_dpy, egl_surf, egl_surf, egl_ctx) == 0 {
return None;
}
if syms.get_egl_swap_interval() != 0 {
let _: u32 = egl_swap_interval(egl_dpy, 0);
}
if syms.get_gl_get_integerv() != 0 {
let get_int: unsafe extern "C" fn(u32, *mut i32) =
transmute(syms.get_gl_get_integerv() as *const c_void);
let mut max_tex: i32 = 4096;
get_int(LINUX_GL_MAX_TEXTURE_SIZE, &mut max_tex);
if max_tex > 0 {
state.set_max_tex_dim((max_tex as u32).clamp(256, 8192));
}
}
let vs: u32 =
linux_gles_compile_shader(syms, LINUX_GL_VERTEX_SHADER, GLSL_ES3_UI_VERTEX_SHADER)?;
let fs: u32 = match linux_gles_compile_shader(
syms,
LINUX_GL_FRAGMENT_SHADER,
GLSL_ES3_UI_FRAGMENT_SHADER,
) {
Some(shader) => shader,
None => {
let delete_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_shader() as *const c_void);
delete_shader(vs);
return None;
}
};
let create_program: unsafe extern "C" fn() -> u32 =
transmute(syms.get_gl_create_program() as *const c_void);
let attach_shader: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_attach_shader() as *const c_void);
let link_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_link_program() as *const c_void);
let get_programiv: unsafe extern "C" fn(u32, u32, *mut i32) =
transmute(syms.get_gl_get_programiv() as *const c_void);
let delete_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_shader() as *const c_void);
let program: u32 = create_program();
attach_shader(program, vs);
attach_shader(program, fs);
link_program(program);
delete_shader(vs);
delete_shader(fs);
let mut linked: i32 = 0;
get_programiv(program, LINUX_GL_LINK_STATUS, &mut linked);
if linked == 0 {
let delete_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
delete_program(program);
return None;
}
state.set_program(program);
let use_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let get_uniform: unsafe extern "C" fn(u32, *const c_char) -> i32 =
transmute(syms.get_gl_get_uniform_location() as *const c_void);
let uniform1i: unsafe extern "C" fn(i32, i32) =
transmute(syms.get_gl_uniform1i() as *const c_void);
use_program(program);
state.set_viewport_loc(get_uniform(
program,
LINUX_GL_UNIFORM_VIEWPORT.as_ptr().cast::<c_char>(),
));
state.set_scroll_loc(get_uniform(
program,
LINUX_GL_UNIFORM_SCROLL_OFFSET.as_ptr().cast::<c_char>(),
));
let u_gr8: i32 = get_uniform(program, LINUX_GL_UNIFORM_GLYPH_R8.as_ptr().cast::<c_char>());
if u_gr8 >= 0 {
uniform1i(u_gr8, 0);
}
let u_grgba: i32 = get_uniform(
program,
LINUX_GL_UNIFORM_GLYPH_RGBA8.as_ptr().cast::<c_char>(),
);
if u_grgba >= 0 {
uniform1i(u_grgba, 1);
}
let u_img: i32 = get_uniform(
program,
LINUX_GL_UNIFORM_IMAGE_TEX.as_ptr().cast::<c_char>(),
);
if u_img >= 0 {
uniform1i(u_img, 2);
}
let u_ext: i32 = get_uniform(
program,
LINUX_GL_UNIFORM_EXTERNAL_TEX.as_ptr().cast::<c_char>(),
);
if u_ext >= 0 {
uniform1i(u_ext, 3);
}
let gen_vaos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_vertex_arrays() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gen_bufs: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_buffers() as *const c_void);
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let buf_data: unsafe extern "C" fn(u32, isize, *const c_void, u32) =
transmute(syms.get_gl_buffer_data() as *const c_void);
let enable_attrib: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_enable_vertex_attrib_array() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
let mut vao: u32 = 0;
let mut bufs: [u32; 2] = [0; 2];
gen_vaos(1, &mut vao);
gen_bufs(2, bufs.as_mut_ptr());
state.set_vao(vao);
state.set_quad_vbo(bufs[0]);
state.set_instance_vbo(bufs[1]);
bind_vao(vao);
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[0]);
buf_data(
LINUX_GL_ARRAY_BUFFER,
(LINUX_GL_UNIT_QUAD.len() * size_of::<f32>()) as isize,
LINUX_GL_UNIT_QUAD.as_ptr().cast::<c_void>(),
LINUX_GL_STATIC_DRAW,
);
enable_attrib(0);
attrib_ptr(
0,
2,
LINUX_GL_FLOAT,
0,
(2 * size_of::<f32>()) as i32,
null(),
);
let one_r8: [u8; 1] = [255];
let one_rgba: [u8; 4] = [255, 255, 255, 255];
state.set_glyph_r8_tex(linux_gles_create_texture(
syms,
LINUX_GL_R8,
LINUX_GL_RED,
1,
1,
&one_r8,
1,
));
state.set_glyph_rgba8_tex(linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
1,
1,
&one_rgba,
4,
));
state.set_image_tex(linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
1,
1,
&one_rgba,
4,
));
state.set_external_tex(linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
1,
1,
&one_rgba,
4,
));
Some(state)
}
}
unsafe fn linux_gles_bind_instance_attribs(
syms: &LinuxEglGlesSyms,
instance_vbo: u32,
base_bytes: usize,
) {
unsafe {
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let enable_attrib: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_enable_vertex_attrib_array() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
let attrib_iptr: unsafe extern "C" fn(u32, i32, u32, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_ipointer() as *const c_void);
let attrib_div: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_vertex_attrib_divisor() as *const c_void);
bind_buf(LINUX_GL_ARRAY_BUFFER, instance_vbo);
let attribs: [Gles3InstanceAttribSpec; 5] = gles3_ui_instance_attribs();
for spec in attribs.iter() {
let loc: u32 = spec.get_location();
let offset: *const c_void =
(base_bytes.saturating_add(spec.get_byte_offset())) as *const c_void;
enable_attrib(loc);
if spec.get_is_integer() {
attrib_iptr(
loc,
spec.get_components(),
LINUX_GL_UNSIGNED_INT,
spec.get_stride(),
offset,
);
} else {
attrib_ptr(
loc,
spec.get_components(),
LINUX_GL_FLOAT,
0,
spec.get_stride(),
offset,
);
}
attrib_div(loc, 1);
}
}
}
pub(crate) fn linux_egl_present_batch(
surface: &mut LinuxEglGlesSurface,
wl_surface: usize,
batch: &DirectGpuFrameBatch,
) -> Result<(), PlatformError> {
let syms: &LinuxEglGlesSyms = linux_egl_gles_syms();
if !syms.get_ready()
|| surface.get_egl_display() == 0
|| surface.get_egl_surface() == 0
|| surface.get_egl_context() == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
let vw: u32 = batch.get_viewport_width().max(1);
let vh: u32 = batch.get_viewport_height().max(1);
unsafe {
if surface.get_is_wayland()
&& surface.get_wl_egl_window() != 0
&& (vw != surface.get_width() || vh != surface.get_height())
&& syms.get_wl_egl_window_resize() != 0
{
let resize_wl_egl: unsafe extern "C" fn(*mut c_void, i32, i32, i32, i32) =
transmute(syms.get_wl_egl_window_resize() as *const c_void);
resize_wl_egl(
surface.get_wl_egl_window() as *mut c_void,
vw as i32,
vh as i32,
0,
0,
);
surface.set_width(vw);
surface.set_height(vh);
}
let egl_make_current: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*mut c_void,
) -> u32 = transmute(syms.get_egl_make_current() as *const c_void);
if egl_make_current(
surface.get_egl_display() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
surface.get_egl_context() as *mut c_void,
) == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
let active_tex: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_active_texture() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
let tex_sub: unsafe extern "C" fn(u32, i32, i32, i32, i32, i32, u32, u32, *const c_void) =
transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
if let Some(glyph_atlas) = batch.glyph_r8_atlas_ref()
&& glyph_atlas.get_width() > 0
&& glyph_atlas.get_height() > 0
&& glyph_atlas.get_revision() != surface.get_glyph_r8_rev()
{
let gw: u32 = glyph_atlas.get_width();
let gh: u32 = glyph_atlas.get_height();
active_tex(LINUX_GL_TEXTURE0);
if gw == surface.get_glyph_r8_w() && gh == surface.get_glyph_r8_h() {
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_r8_tex());
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, 1);
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
gw as i32,
gh as i32,
LINUX_GL_RED,
LINUX_GL_UNSIGNED_BYTE,
glyph_atlas.get_pixels().as_ptr().cast::<c_void>(),
);
} else {
let old_tex: u32 = surface.get_glyph_r8_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_texture(
syms,
LINUX_GL_R8,
LINUX_GL_RED,
gw,
gh,
&glyph_atlas.get_pixels()[..],
1,
);
surface.set_glyph_r8_tex(new_tex);
surface.set_glyph_r8_w(gw);
surface.set_glyph_r8_h(gh);
}
surface.set_glyph_r8_rev(glyph_atlas.get_revision());
}
if let Some(img_atlas) = batch.image_rgba8_atlas_ref()
&& img_atlas.get_width() > 0
&& img_atlas.get_height() > 0
&& img_atlas.get_revision() != surface.get_image_rev()
{
let iw: u32 = img_atlas.get_width();
let ih: u32 = img_atlas.get_height();
active_tex(LINUX_GL_TEXTURE2);
if iw == surface.get_image_w() && ih == surface.get_image_h() {
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_image_tex());
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, 4);
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
iw as i32,
ih as i32,
LINUX_GL_RGBA,
LINUX_GL_UNSIGNED_BYTE,
img_atlas.get_pixels().as_ptr().cast::<c_void>(),
);
} else {
let old_tex: u32 = surface.get_image_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
iw,
ih,
&img_atlas.get_pixels()[..],
4,
);
surface.set_image_tex(new_tex);
surface.set_image_w(iw);
surface.set_image_h(ih);
}
surface.set_image_rev(img_atlas.get_revision());
}
let gl_viewport: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_viewport() as *const c_void);
let gl_disable: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable() as *const c_void);
let gl_enable: unsafe extern "C" fn(u32) = transmute(syms.get_gl_enable() as *const c_void);
let gl_clear_color: unsafe extern "C" fn(f32, f32, f32, f32) =
transmute(syms.get_gl_clear_color() as *const c_void);
let gl_clear: unsafe extern "C" fn(u32) = transmute(syms.get_gl_clear() as *const c_void);
let prims: &[GpuPrimitiveInstance] = batch.get_primitives().as_slice();
let total_prims: usize = prims.len();
let frame_has_blur: bool = prims.iter().any(|prim_item: &GpuPrimitiveInstance| {
prim_item.prim_kind() == PrimKind::BackdropBlur
});
let use_blur_fbo: bool = if frame_has_blur {
let LinuxEglGlesSurface { blur, .. } = surface;
linux_gles_ensure_blur_pipeline(syms, blur, vw, vh)
} else {
false
};
let target_fbo: u32 = if use_blur_fbo {
let LinuxEglGlesSurface { blur, .. } = surface;
blur.as_ref().map_or(0, LinuxEglBlurPipeline::get_scene_fbo)
} else {
0
};
if syms.get_gl_bind_framebuffer() != 0 {
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
bind_fbo(LINUX_GL_FRAMEBUFFER, target_fbo);
}
gl_viewport(0, 0, vw as i32, vh as i32);
gl_disable(LINUX_GL_SCISSOR_TEST);
let clear: [f32; 4] = batch.get_clear_rgba();
gl_clear_color(clear[0], clear[1], clear[2], clear[3]);
gl_clear(LINUX_GL_COLOR_BUFFER_BIT);
let byte_len: usize = total_prims.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
if byte_len > 0 {
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let buf_data: unsafe extern "C" fn(u32, isize, *const c_void, u32) =
transmute(syms.get_gl_buffer_data() as *const c_void);
let buf_sub: unsafe extern "C" fn(u32, isize, isize, *const c_void) =
transmute(syms.get_gl_buffer_sub_data() as *const c_void);
bind_buf(LINUX_GL_ARRAY_BUFFER, surface.get_instance_vbo());
if byte_len > surface.get_instance_vbo_cap() {
let alloc: usize = byte_len.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
alloc as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
surface.set_instance_vbo_cap(alloc);
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
byte_len as isize,
prims.as_ptr().cast::<c_void>(),
);
let blend_sep: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_blend_func_separate() as *const c_void);
let use_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let uniform2f: unsafe extern "C" fn(i32, f32, f32) =
transmute(syms.get_gl_uniform2f() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gl_scissor: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_scissor() as *const c_void);
let draw_instanced: unsafe extern "C" fn(u32, i32, i32, i32) =
transmute(syms.get_gl_draw_arrays_instanced() as *const c_void);
let ui_prog: u32 = surface.get_program();
let ui_vao: u32 = surface.get_vao();
let viewport_loc: i32 = surface.get_viewport_loc();
let scroll_loc: i32 = surface.get_scroll_loc();
let inst_vbo: u32 = surface.get_instance_vbo();
let glyph_r8_tex: u32 = surface.get_glyph_r8_tex();
let external_tex: u32 = surface.get_external_tex();
gl_enable(LINUX_GL_BLEND);
blend_sep(
LINUX_GL_SRC_ALPHA,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
LINUX_GL_ONE,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
);
gl_enable(LINUX_GL_SCISSOR_TEST);
use_program(ui_prog);
if viewport_loc >= 0 {
uniform2f(viewport_loc, vw as f32, vh as f32);
}
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, glyph_r8_tex);
active_tex(LINUX_GL_TEXTURE1);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_rgba8_tex());
active_tex(LINUX_GL_TEXTURE2);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_image_tex());
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, external_tex);
bind_vao(ui_vao);
let LinuxEglGlesSurface { blur, .. } = surface;
for range in batch.get_draw_ranges().iter() {
let base_inst: usize = range.get_base_instance() as usize;
let count: usize = range.get_instance_count() as usize;
if count == 0 || base_inst >= total_prims {
continue;
}
if range.get_scissor_w() == 0 || range.get_scissor_h() == 0 {
continue;
}
let draw_count: usize = count.min(total_prims - base_inst);
let range_slice: &[GpuPrimitiveInstance] =
&prims[base_inst..base_inst.saturating_add(draw_count)];
let blur_prim_opt: Option<&GpuPrimitiveInstance> = if use_blur_fbo {
range_slice
.iter()
.find(|prim_item: &&GpuPrimitiveInstance| {
prim_item.prim_kind() == PrimKind::BackdropBlur
})
} else {
None
};
if let (Some(blur_prim), Some(blur_pipe)) = (blur_prim_opt, blur.as_ref()) {
linux_gles_run_blur_pass(syms, blur_pipe, blur_prim, ui_vao, vw, vh);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
bind_fbo(LINUX_GL_FRAMEBUFFER, blur_pipe.get_scene_fbo());
gl_viewport(0, 0, vw as i32, vh as i32);
gl_enable(LINUX_GL_BLEND);
gl_enable(LINUX_GL_SCISSOR_TEST);
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, glyph_r8_tex);
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, blur_pipe.get_full_tex());
use_program(ui_prog);
if viewport_loc >= 0 {
uniform2f(viewport_loc, vw as f32, vh as f32);
}
bind_vao(ui_vao);
}
let sc: [i32; 4] = gles3_scissor_from_top_down(
vh,
range.get_scissor_x(),
range.get_scissor_y(),
range.get_scissor_w(),
range.get_scissor_h(),
);
if sc[2] <= 0 || sc[3] <= 0 {
continue;
}
gl_scissor(sc[0], sc[1], sc[2], sc[3]);
if scroll_loc >= 0 {
uniform2f(scroll_loc, range.get_scroll_x(), range.get_scroll_y());
}
let base_bytes: usize = base_inst.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
linux_gles_bind_instance_attribs(syms, inst_vbo, base_bytes);
draw_instanced(LINUX_GL_TRIANGLES, 0, 6, draw_count as i32);
if blur_prim_opt.is_some() {
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, external_tex);
}
}
gl_disable(LINUX_GL_SCISSOR_TEST);
}
if use_blur_fbo {
let ui_vao: u32 = surface.get_vao();
let LinuxEglGlesSurface { blur, .. } = surface;
if let Some(blur_pipe) = blur.as_ref() {
linux_gles_copy_blur_scene_to_default(syms, blur_pipe, ui_vao, vw, vh);
}
}
if surface.get_is_wayland() && wl_surface != 0 {
let LinuxEglGlesSurface { vsync_pending, .. } = surface;
let _: bool = linux_wl_arm_frame_callback(wl_surface, vsync_pending.as_ref());
}
let swap_buffers: unsafe extern "C" fn(*mut c_void, *mut c_void) -> u32 =
transmute(syms.get_egl_swap_buffers() as *const c_void);
if swap_buffers(
surface.get_egl_display() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
) == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
Ok(())
}
}
unsafe fn linux_gles_create_empty_rgba8_texture(
syms: &LinuxEglGlesSyms,
width: u32,
height: u32,
) -> u32 {
unsafe {
let gen_tex: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_textures() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let tex_param: unsafe extern "C" fn(u32, u32, i32) =
transmute(syms.get_gl_tex_parameteri() as *const c_void);
let mut tex: u32 = 0;
gen_tex(1, &mut tex);
bind_tex(LINUX_GL_TEXTURE_2D, tex);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MIN_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MAG_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_S,
LINUX_GL_CLAMP_TO_EDGE,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_T,
LINUX_GL_CLAMP_TO_EDGE,
);
let w: i32 = width.max(1) as i32;
let h: i32 = height.max(1) as i32;
if syms.get_gl_tex_storage_2d() != 0 {
let tex_storage: unsafe extern "C" fn(u32, i32, u32, i32, i32) =
transmute(syms.get_gl_tex_storage_2d() as *const c_void);
tex_storage(LINUX_GL_TEXTURE_2D, 1, LINUX_GL_RGBA8, w, h);
} else {
let tex_image: unsafe extern "C" fn(
u32,
i32,
i32,
i32,
i32,
i32,
u32,
u32,
*const c_void,
) = transmute(syms.get_gl_tex_image_2d() as *const c_void);
tex_image(
LINUX_GL_TEXTURE_2D,
0,
LINUX_GL_RGBA8 as i32,
w,
h,
0,
LINUX_GL_RGBA,
LINUX_GL_UNSIGNED_BYTE,
null(),
);
}
tex
}
}
unsafe fn linux_gles_link_program(
syms: &LinuxEglGlesSyms,
vs_src: &str,
fs_src: &str,
) -> Option<u32> {
unsafe {
let delete_shader: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_shader() as *const c_void);
let vs: u32 = linux_gles_compile_shader(syms, LINUX_GL_VERTEX_SHADER, vs_src)?;
let Some(fs) = linux_gles_compile_shader(syms, LINUX_GL_FRAGMENT_SHADER, fs_src) else {
delete_shader(vs);
return None;
};
let create_program: unsafe extern "C" fn() -> u32 =
transmute(syms.get_gl_create_program() as *const c_void);
let attach_shader: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_attach_shader() as *const c_void);
let link_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_link_program() as *const c_void);
let get_programiv: unsafe extern "C" fn(u32, u32, *mut i32) =
transmute(syms.get_gl_get_programiv() as *const c_void);
let program: u32 = create_program();
attach_shader(program, vs);
attach_shader(program, fs);
link_program(program);
delete_shader(vs);
delete_shader(fs);
let mut linked: i32 = 0;
get_programiv(program, LINUX_GL_LINK_STATUS, &mut linked);
if linked == 0 {
let delete_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
delete_program(program);
return None;
}
Some(program)
}
}
unsafe fn linux_gles_init_scene_pipeline(syms: &LinuxEglGlesSyms) -> Option<LinuxEglScenePipeline> {
if syms.get_gl_draw_arrays() == 0
|| syms.get_gl_uniform4f() == 0
|| syms.get_gl_uniform_matrix4fv() == 0
|| syms.get_gl_gen_framebuffers() == 0
|| syms.get_gl_bind_framebuffer() == 0
|| syms.get_gl_framebuffer_texture_2d() == 0
|| syms.get_gl_gen_renderbuffers() == 0
|| syms.get_gl_bind_renderbuffer() == 0
|| syms.get_gl_renderbuffer_storage() == 0
|| syms.get_gl_framebuffer_renderbuffer() == 0
|| syms.get_gl_depth_mask() == 0
|| syms.get_gl_depth_func() == 0
|| syms.get_gl_clear_depthf() == 0
{
return None;
}
unsafe {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
let ocean_program: u32 = linux_gles_link_program(
syms,
LINUX_GL_SCENE_FULLSCREEN_VERT,
LINUX_GL_SCENE_OCEAN_FRAG,
)?;
let Some(boat_program) =
linux_gles_link_program(syms, LINUX_GL_SCENE_BOAT_VERT, LINUX_GL_SCENE_BOAT_FRAG)
else {
del_prog(ocean_program);
return None;
};
let Some(sky3d_program) = linux_gles_link_program(
syms,
LINUX_GL_SCENE_FULLSCREEN_VERT,
LINUX_GL_SCENE_3D_SKY_FRAG,
) else {
del_prog(ocean_program);
del_prog(boat_program);
return None;
};
let Some(mesh3d_program) = linux_gles_link_program(
syms,
LINUX_GL_SCENE_3D_MESH_VERT,
LINUX_GL_SCENE_3D_MESH_FRAG,
) else {
del_prog(ocean_program);
del_prog(boat_program);
del_prog(sky3d_program);
return None;
};
let use_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let get_uni: unsafe extern "C" fn(u32, *const c_char) -> i32 =
transmute(syms.get_gl_get_uniform_location() as *const c_void);
let uni1i: unsafe extern "C" fn(i32, i32) =
transmute(syms.get_gl_uniform1i() as *const c_void);
use_prog(ocean_program);
let wake_loc: i32 = get_uni(
ocean_program,
LINUX_GL_UNIFORM_WAKE_TEX.as_ptr().cast::<c_char>(),
);
if wake_loc >= 0 {
uni1i(wake_loc, 0);
}
let ocean_time_loc: i32 = get_uni(
ocean_program,
LINUX_GL_UNIFORM_OCEAN_TIME.as_ptr().cast::<c_char>(),
);
let ocean_roll_loc: i32 = get_uni(
ocean_program,
LINUX_GL_UNIFORM_OCEAN_ROLL.as_ptr().cast::<c_char>(),
);
let ocean_sun_loc: i32 = get_uni(
ocean_program,
LINUX_GL_UNIFORM_OCEAN_SUN.as_ptr().cast::<c_char>(),
);
let sky3d_vp_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_VIEW_PROJ.as_ptr().cast::<c_char>(),
);
let sky3d_cam_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_CAM_POS.as_ptr().cast::<c_char>(),
);
let sky3d_sun_dir_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_SUN_FOG.as_ptr().cast::<c_char>(),
);
let sky3d_sun_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_SUN_COL.as_ptr().cast::<c_char>(),
);
let sky3d_hor_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_SKY_HOR.as_ptr().cast::<c_char>(),
);
let sky3d_zen_loc: i32 = get_uni(
sky3d_program,
LINUX_GL_UNIFORM_SKY_ZEN.as_ptr().cast::<c_char>(),
);
let mesh3d_vp_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_VIEW_PROJ.as_ptr().cast::<c_char>(),
);
let mesh3d_cam_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_CAM_POS.as_ptr().cast::<c_char>(),
);
let mesh3d_sun_fog_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_SUN_FOG.as_ptr().cast::<c_char>(),
);
let mesh3d_sun_col_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_SUN_COL.as_ptr().cast::<c_char>(),
);
let mesh3d_hor_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_SKY_HOR.as_ptr().cast::<c_char>(),
);
let mesh3d_zen_loc: i32 = get_uni(
mesh3d_program,
LINUX_GL_UNIFORM_SKY_ZEN.as_ptr().cast::<c_char>(),
);
let gen_fbos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_framebuffers() as *const c_void);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
let fbo_tex2d: unsafe extern "C" fn(u32, u32, u32, u32, i32) =
transmute(syms.get_gl_framebuffer_texture_2d() as *const c_void);
let gen_rbos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_renderbuffers() as *const c_void);
let bind_rbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_renderbuffer() as *const c_void);
let rbo_storage: unsafe extern "C" fn(u32, u32, i32, i32) =
transmute(syms.get_gl_renderbuffer_storage() as *const c_void);
let fbo_rbo: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_framebuffer_renderbuffer() as *const c_void);
let mut scene_fbo: u32 = 0;
let mut scene_depth_rbo: u32 = 0;
gen_fbos(1, &mut scene_fbo);
gen_rbos(1, &mut scene_depth_rbo);
let scene_color_tex: u32 = linux_gles_create_empty_rgba8_texture(syms, 1, 1);
let one_rgba: [u8; 4] = [128, 128, 0, 255];
let wake_tex: u32 =
linux_gles_create_texture(syms, LINUX_GL_RGBA8, LINUX_GL_RGBA, 1, 1, &one_rgba, 4);
bind_rbo(LINUX_GL_RENDERBUFFER, scene_depth_rbo);
rbo_storage(LINUX_GL_RENDERBUFFER, LINUX_GL_DEPTH_COMPONENT24, 1, 1);
bind_fbo(LINUX_GL_FRAMEBUFFER, scene_fbo);
fbo_tex2d(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_COLOR_ATTACHMENT0,
LINUX_GL_TEXTURE_2D,
scene_color_tex,
0,
);
fbo_rbo(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_DEPTH_ATTACHMENT,
LINUX_GL_RENDERBUFFER,
scene_depth_rbo,
);
bind_fbo(LINUX_GL_FRAMEBUFFER, 0);
let gen_vaos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_vertex_arrays() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gen_bufs: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_buffers() as *const c_void);
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let enable_attrib: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_enable_vertex_attrib_array() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
let attrib_div: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_vertex_attrib_divisor() as *const c_void);
let mut vaos: [u32; 2] = [0; 2];
let mut bufs: [u32; 3] = [0; 3];
gen_vaos(2, vaos.as_mut_ptr());
gen_bufs(3, bufs.as_mut_ptr());
bind_vao(vaos[0]);
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[0]);
enable_attrib(0);
attrib_ptr(0, 4, LINUX_GL_FLOAT, 0, 32, null());
enable_attrib(1);
attrib_ptr(1, 4, LINUX_GL_FLOAT, 0, 32, 16 as *const c_void);
bind_vao(vaos[1]);
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[1]);
for slot in 0u32..3u32 {
enable_attrib(slot);
attrib_ptr(
slot,
4,
LINUX_GL_FLOAT,
0,
48,
((slot as usize) * 16) as *const c_void,
);
attrib_div(slot, 0);
}
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[2]);
for slot in 0u32..6u32 {
enable_attrib(3 + slot);
attrib_ptr(
3 + slot,
4,
LINUX_GL_FLOAT,
0,
96,
((slot as usize) * 16) as *const c_void,
);
attrib_div(3 + slot, 1);
}
bind_vao(0);
Some(LinuxEglScenePipeline {
scene_fbo,
scene_color_tex,
scene_depth_rbo,
scene_w: 0,
scene_h: 0,
wake_tex,
wake_w: 0,
wake_h: 0,
ocean_program,
ocean_time_loc,
ocean_roll_loc,
ocean_sun_loc,
boat_program,
boat_vao: vaos[0],
boat_vbo: bufs[0],
boat_vbo_cap: 0,
sky3d_program,
sky3d_vp_loc,
sky3d_cam_loc,
sky3d_sun_dir_loc,
sky3d_sun_loc,
sky3d_hor_loc,
sky3d_zen_loc,
mesh3d_program,
mesh3d_vao: vaos[1],
mesh3d_vert_vbo: bufs[1],
mesh3d_vert_cap: 0,
mesh3d_vert_rev: 0,
mesh3d_inst_vbo: bufs[2],
mesh3d_inst_cap: 0,
mesh3d_vp_loc,
mesh3d_cam_loc,
mesh3d_sun_fog_loc,
mesh3d_sun_col_loc,
mesh3d_hor_loc,
mesh3d_zen_loc,
pbr: None,
})
}
}
unsafe fn linux_gles_program_cache_hash(
syms: &LinuxEglGlesSyms,
vert_src: &str,
frag_src: &str,
) -> u64 {
let mut hash: u64 = 0xCBF2_9CE4_8422_2325u64;
if syms.get_gl_get_string() != 0 {
unsafe {
let get_str: unsafe extern "C" fn(u32) -> *const c_char =
transmute(syms.get_gl_get_string() as *const c_void);
for query in [LINUX_GL_RENDERER, LINUX_GL_VERSION] {
let ptr: *const c_char = get_str(query);
if !ptr.is_null() {
for &byte in CStr::from_ptr(ptr).to_bytes() {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01B3u64);
}
}
}
}
}
for src in [vert_src.as_bytes(), frag_src.as_bytes()] {
for &byte in src {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01B3u64);
}
}
hash
}
unsafe fn linux_gles_link_program_cached(
syms: &LinuxEglGlesSyms,
vert_src: &str,
frag_src: &str,
) -> Option<u32> {
let can_cache: bool = syms.get_gl_program_binary() != 0
&& syms.get_gl_get_program_binary() != 0
&& syms.get_gl_program_parameteri() != 0
&& syms.get_gl_get_programiv() != 0
&& syms.get_gl_create_program() != 0
&& syms.get_gl_delete_program() != 0;
unsafe {
let hash: u64 = linux_gles_program_cache_hash(syms, vert_src, frag_src);
let cache_path: PathBuf = env::temp_dir().join(format!(
"{LINUX_GL_PROG_CACHE_PREFIX}{hash:016x}{LINUX_GL_PROG_CACHE_SUFFIX}"
));
if can_cache
&& let Ok(blob) = fs::read(&cache_path)
&& blob.len() > 4
{
let bin_fmt: u32 = u32::from_le_bytes([blob[0], blob[1], blob[2], blob[3]]);
let create_prog: unsafe extern "C" fn() -> u32 =
transmute(syms.get_gl_create_program() as *const c_void);
let prog_bin: unsafe extern "C" fn(u32, u32, *const c_void, i32) =
transmute(syms.get_gl_program_binary() as *const c_void);
let get_piv: unsafe extern "C" fn(u32, u32, *mut i32) =
transmute(syms.get_gl_get_programiv() as *const c_void);
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
let prog: u32 = create_prog();
if prog != 0 {
let payload: &[u8] = &blob[4..];
prog_bin(
prog,
bin_fmt,
payload.as_ptr().cast::<c_void>(),
payload.len() as i32,
);
let mut linked: i32 = 0;
get_piv(prog, LINUX_GL_LINK_STATUS, &mut linked);
if linked != 0 {
return Some(prog);
}
del_prog(prog);
}
}
let prog: u32 = linux_gles_link_program(syms, vert_src, frag_src)?;
if can_cache {
let prog_param: unsafe extern "C" fn(u32, u32, i32) =
transmute(syms.get_gl_program_parameteri() as *const c_void);
let get_piv: unsafe extern "C" fn(u32, u32, *mut i32) =
transmute(syms.get_gl_get_programiv() as *const c_void);
let get_bin: unsafe extern "C" fn(u32, i32, *mut i32, *mut u32, *mut c_void) =
transmute(syms.get_gl_get_program_binary() as *const c_void);
prog_param(prog, LINUX_GL_PROGRAM_BINARY_RETRIEVABLE_HINT, 1);
let mut bin_len: i32 = 0;
get_piv(prog, LINUX_GL_PROGRAM_BINARY_LENGTH, &mut bin_len);
if bin_len > 0 && bin_len <= 16 * 1024 * 1024 {
let mut out_buf: Vec<u8> = vec![0; 4 + (bin_len as usize)];
let mut written: i32 = 0;
let mut bin_fmt: u32 = 0;
get_bin(
prog,
bin_len,
&mut written,
&mut bin_fmt,
out_buf[4..].as_mut_ptr().cast::<c_void>(),
);
if written > 0 && (written as usize) <= bin_len as usize {
out_buf[0..4].copy_from_slice(&bin_fmt.to_le_bytes());
out_buf.truncate(4 + (written as usize));
let _: io::Result<()> = fs::write(&cache_path, &out_buf);
}
}
}
Some(prog)
}
}
unsafe fn linux_gles_ensure_pbr_resources(
syms: &LinuxEglGlesSyms,
slot: &mut Option<LinuxEglPbrResources>,
) -> bool {
if slot.is_some() {
return true;
}
if syms.get_gl_draw_elements_instanced() == 0
|| syms.get_gl_uniform4i() == 0
|| syms.get_gl_uniform4fv() == 0
|| syms.get_gl_vertex_attrib_ipointer() == 0
|| syms.get_gl_tex_storage_2d() == 0
|| syms.get_gl_disable_vertex_attrib_array() == 0
{
return false;
}
unsafe {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
let Some(sky_program) = linux_gles_link_program_cached(
syms,
GLSL_ES3_PBR_SKY_VERT_SHADER,
GLSL_ES3_PBR_SKY_FRAG_SHADER,
) else {
return false;
};
let Some(scene_program) = linux_gles_link_program_cached(
syms,
GLSL_ES3_PBR_SCENE_VERT_SHADER,
GLSL_ES3_PBR_SCENE_FRAG_SHADER,
) else {
del_prog(sky_program);
return false;
};
let get_uni: unsafe extern "C" fn(u32, *const c_char) -> i32 =
transmute(syms.get_gl_get_uniform_location() as *const c_void);
let use_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let uni1i: unsafe extern "C" fn(i32, i32) =
transmute(syms.get_gl_uniform1i() as *const c_void);
let gen_vaos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_vertex_arrays() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gen_bufs: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_buffers() as *const c_void);
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let enable_attr: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_enable_vertex_attrib_array() as *const c_void);
let disable_attr: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable_vertex_attrib_array() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
let attrib_iptr: unsafe extern "C" fn(u32, i32, u32, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_ipointer() as *const c_void);
let attrib_div: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_vertex_attrib_divisor() as *const c_void);
let gen_texs: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_textures() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let tex_param: unsafe extern "C" fn(u32, u32, i32) =
transmute(syms.get_gl_tex_parameteri() as *const c_void);
let tex_img: unsafe extern "C" fn(u32, i32, i32, i32, i32, i32, u32, u32, *const c_void) =
transmute(syms.get_gl_tex_image_2d() as *const c_void);
let mut vao: u32 = 0;
gen_vaos(1, &mut vao);
let mut bufs: [u32; 4] = [0; 4];
gen_bufs(4, bufs.as_mut_ptr());
let mut texs: [u32; 5] = [0; 5];
gen_texs(5, texs.as_mut_ptr());
bind_vao(vao);
bind_buf(LINUX_GL_ELEMENT_ARRAY_BUFFER, bufs[1]);
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[0]);
enable_attr(0);
attrib_iptr(0, 4, LINUX_GL_UNSIGNED_SHORT, 24, null());
attrib_div(0, 0);
enable_attr(1);
attrib_iptr(1, 4, LINUX_GL_SHORT, 24, 8 as *const c_void);
attrib_div(1, 0);
enable_attr(2);
attrib_iptr(2, 2, LINUX_GL_UNSIGNED_INT, 24, 16 as *const c_void);
attrib_div(2, 0);
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[2]);
disable_attr(3);
disable_attr(4);
if syms.get_gl_vertex_attrib_i4ui() != 0 {
let attr_i4ui: unsafe extern "C" fn(u32, u32, u32, u32, u32) =
transmute(syms.get_gl_vertex_attrib_i4ui() as *const c_void);
attr_i4ui(3, 0, 0, 0, 0);
}
if syms.get_gl_vertex_attrib_4f() != 0 {
let attr_4f: unsafe extern "C" fn(u32, f32, f32, f32, f32) =
transmute(syms.get_gl_vertex_attrib_4f() as *const c_void);
attr_4f(4, 1.0, 0.0, 0.0, 0.0);
}
bind_buf(LINUX_GL_ARRAY_BUFFER, bufs[3]);
for attr in 0u32..6u32 {
let loc: u32 = 5 + attr;
enable_attr(loc);
attrib_ptr(
loc,
4,
LINUX_GL_FLOAT,
0,
112,
((attr as usize) * 16) as *const c_void,
);
attrib_div(loc, 1);
}
enable_attr(11);
attrib_iptr(11, 4, LINUX_GL_UNSIGNED_INT, 112, 96 as *const c_void);
attrib_div(11, 1);
bind_vao(0);
let zero_f4: [f32; 4] = [0.0; 4];
for &dt in texs.iter().take(4) {
bind_tex(LINUX_GL_TEXTURE_2D, dt);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MIN_FILTER,
LINUX_GL_NEAREST,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MAG_FILTER,
LINUX_GL_NEAREST,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_S,
LINUX_GL_CLAMP_TO_EDGE,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_T,
LINUX_GL_CLAMP_TO_EDGE,
);
tex_img(
LINUX_GL_TEXTURE_2D,
0,
LINUX_GL_RGBA32F as i32,
1,
1,
0,
LINUX_GL_RGBA,
LINUX_GL_FLOAT,
zero_f4.as_ptr().cast::<c_void>(),
);
}
let white_px: [u8; 4] = [255, 255, 255, 255];
bind_tex(LINUX_GL_TEXTURE_2D, texs[4]);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MIN_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MAG_FILTER,
LINUX_GL_LINEAR,
);
tex_img(
LINUX_GL_TEXTURE_2D,
0,
LINUX_GL_RGBA8 as i32,
1,
1,
0,
LINUX_GL_RGBA,
LINUX_GL_UNSIGNED_BYTE,
white_px.as_ptr().cast::<c_void>(),
);
use_prog(scene_program);
let samplers: [(&str, i32); 9] = [
(LINUX_GL_PBR_UNIFORM_TEX_BASE, 0),
(LINUX_GL_PBR_UNIFORM_TEX_MR, 1),
(LINUX_GL_PBR_UNIFORM_TEX_NORMAL, 2),
(LINUX_GL_PBR_UNIFORM_TEX_OCC, 3),
(LINUX_GL_PBR_UNIFORM_TEX_EMISSIVE, 4),
(LINUX_GL_PBR_UNIFORM_BONE_TEX, 5),
(LINUX_GL_PBR_UNIFORM_MAT_TEX, 6),
(LINUX_GL_PBR_UNIFORM_LIGHT_TEX, 7),
(LINUX_GL_PBR_UNIFORM_MORPH_TEX, 8),
];
for &(name, unit) in samplers.iter() {
let loc: i32 = get_uni(scene_program, name.as_ptr().cast::<c_char>());
if loc >= 0 {
uni1i(loc, unit);
}
}
*slot = Some(LinuxEglPbrResources {
sky_program,
scene_program,
sky_cam_loc: get_uni(
sky_program,
LINUX_GL_PBR_UNIFORM_CAM_EXP.as_ptr().cast::<c_char>(),
),
sky_sun_fog_loc: get_uni(
sky_program,
LINUX_GL_UNIFORM_SUN_FOG.as_ptr().cast::<c_char>(),
),
sky_sun_col_loc: get_uni(
sky_program,
LINUX_GL_UNIFORM_SUN_COL.as_ptr().cast::<c_char>(),
),
sky_hor_loc: get_uni(
sky_program,
LINUX_GL_UNIFORM_SKY_HOR.as_ptr().cast::<c_char>(),
),
sky_zen_agx_loc: get_uni(
sky_program,
LINUX_GL_PBR_UNIFORM_SKY_ZEN_AGX.as_ptr().cast::<c_char>(),
),
vp_loc: get_uni(
scene_program,
LINUX_GL_UNIFORM_VIEW_PROJ.as_ptr().cast::<c_char>(),
),
cam_exp_loc: get_uni(
scene_program,
LINUX_GL_PBR_UNIFORM_CAM_EXP.as_ptr().cast::<c_char>(),
),
sun_fog_loc: get_uni(
scene_program,
LINUX_GL_UNIFORM_SUN_FOG.as_ptr().cast::<c_char>(),
),
sun_col_loc: get_uni(
scene_program,
LINUX_GL_UNIFORM_SUN_COL.as_ptr().cast::<c_char>(),
),
hor_loc: get_uni(
scene_program,
LINUX_GL_UNIFORM_SKY_HOR.as_ptr().cast::<c_char>(),
),
zen_agx_loc: get_uni(
scene_program,
LINUX_GL_PBR_UNIFORM_SKY_ZEN_AGX.as_ptr().cast::<c_char>(),
),
ibl_sh_loc: get_uni(
scene_program,
LINUX_GL_PBR_UNIFORM_IBL_SH.as_ptr().cast::<c_char>(),
),
batch_params_loc: get_uni(
scene_program,
LINUX_GL_PBR_UNIFORM_BATCH_PARAMS.as_ptr().cast::<c_char>(),
),
draw_offsets_loc: get_uni(
scene_program,
LINUX_GL_PBR_UNIFORM_DRAW_OFFSETS.as_ptr().cast::<c_char>(),
),
morph_weights_loc: get_uni(
scene_program,
LINUX_GL_PBR_UNIFORM_MORPH_WEIGHTS.as_ptr().cast::<c_char>(),
),
vao,
vert_vbo: bufs[0],
vert_cap: 0,
idx_ebo: bufs[1],
idx_cap: 0,
skin_vbo: bufs[2],
skin_cap: 0,
mesh_rev: 0,
inst_vbo: bufs[3],
inst_cap: 0,
bone_tex: texs[0],
mat_tex: texs[1],
light_tex: texs[2],
morph_tex: texs[3],
dummy_white_tex: texs[4],
textures: HashMap::new(),
data_tex_scratch: Vec::new(),
});
true
}
}
unsafe fn linux_gles_apply_pbr_mip_uploads(
syms: &LinuxEglGlesSyms,
res: &mut LinuxEglPbrResources,
uploads: &[GpuTextureMipUpload],
) {
if uploads.is_empty() {
return;
}
unsafe {
let gen_texs: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_textures() as *const c_void);
let del_texs: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let tex_storage: unsafe extern "C" fn(u32, i32, u32, i32, i32) =
transmute(syms.get_gl_tex_storage_2d() as *const c_void);
let tex_sub: unsafe extern "C" fn(u32, i32, i32, i32, i32, i32, u32, u32, *const c_void) =
transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
let tex_param: unsafe extern "C" fn(u32, u32, i32) =
transmute(syms.get_gl_tex_parameteri() as *const c_void);
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, 1);
for up in uploads.iter() {
let mip: u8 = up.get_mip_level();
let shift: u32 = u32::from(mip.min(13));
let base_w: u32 = up
.get_width()
.max(1)
.checked_shl(shift)
.unwrap_or(4096)
.clamp(1, 4096);
let base_h: u32 = up
.get_height()
.max(1)
.checked_shl(shift)
.unwrap_or(4096)
.clamp(1, 4096);
let slot: u32 = up.get_texture_slot();
let needs_alloc: bool = match res.textures.get(&slot) {
Some(ex) => ex.tex_id == 0 || ex.base_width != base_w || ex.base_height != base_h,
None => true,
};
if needs_alloc {
if let Some(old) = res.textures.remove(&slot)
&& old.tex_id != 0
{
del_texs(1, &old.tex_id);
}
let mut tex_id: u32 = 0;
gen_texs(1, &mut tex_id);
if tex_id == 0 {
continue;
}
let max_dim: u32 = base_w.max(base_h);
let total_mips: u8 =
(32u32.saturating_sub(max_dim.leading_zeros())).clamp(1, 13) as u8;
bind_tex(LINUX_GL_TEXTURE_2D, tex_id);
tex_storage(
LINUX_GL_TEXTURE_2D,
i32::from(total_mips),
LINUX_GL_RGBA8,
base_w as i32,
base_h as i32,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MIN_FILTER,
LINUX_GL_LINEAR_MIPMAP_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MAG_FILTER,
LINUX_GL_LINEAR,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_S,
LINUX_GL_REPEAT,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_WRAP_T,
LINUX_GL_REPEAT,
);
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_MAX_LEVEL,
i32::from(total_mips.saturating_sub(1)),
);
let _: Option<LinuxResidentMipTexture> = res.textures.insert(
slot,
LinuxResidentMipTexture {
tex_id,
base_width: base_w,
base_height: base_h,
total_mips,
min_lod_clamp: total_mips.saturating_sub(1),
},
);
}
let Some(entry) = res.textures.get_mut(&slot) else {
continue;
};
if mip >= entry.total_mips {
continue;
}
let mip_w: u32 = (entry.base_width >> shift).max(1);
let mip_h: u32 = (entry.base_height >> shift).max(1);
let need_bytes: usize = (mip_w as usize)
.saturating_mul(mip_h as usize)
.saturating_mul(4);
let rgba8: &[u8] = up.get_rgba8().as_slice();
if rgba8.len() < need_bytes {
continue;
}
bind_tex(LINUX_GL_TEXTURE_2D, entry.tex_id);
tex_sub(
LINUX_GL_TEXTURE_2D,
i32::from(mip),
0,
0,
mip_w as i32,
mip_h as i32,
LINUX_GL_RGBA,
LINUX_GL_UNSIGNED_BYTE,
rgba8.as_ptr().cast::<c_void>(),
);
let next_clamp: u8 = up
.get_min_lod_clamp()
.min(mip)
.min(entry.total_mips.saturating_sub(1));
entry.min_lod_clamp = next_clamp;
tex_param(
LINUX_GL_TEXTURE_2D,
LINUX_GL_TEXTURE_BASE_LEVEL,
i32::from(next_clamp),
);
}
}
}
unsafe fn linux_gles_execute_scene_pass(
syms: &LinuxEglGlesSyms,
surface: &mut LinuxEglGlesSurface,
pass: &DirectGpuScenePass,
) -> u32 {
let fallback_ext: u32 = surface.get_external_tex();
let max_dim: u32 = surface.get_max_tex_dim().clamp(256, 4096);
let ui_vao: u32 = surface.get_vao();
let LinuxEglGlesSurface { scene, .. } = surface;
unsafe {
if scene.is_none() {
*scene = linux_gles_init_scene_pipeline(syms);
}
let Some(sc) = scene.as_mut() else {
return fallback_ext;
};
let (req_w, req_h): (u32, u32) = match pass {
DirectGpuScenePass::OceanFluid(ocean) => (
ocean.get_target_width().clamp(64, max_dim),
ocean.get_target_height().clamp(64, max_dim),
),
DirectGpuScenePass::World3d(world) => (
world.get_target_width().clamp(64, max_dim),
world.get_target_height().clamp(64, max_dim),
),
DirectGpuScenePass::PbrScene(pbr) => (
pbr.get_target_width().clamp(64, max_dim),
pbr.get_target_height().clamp(64, max_dim),
),
};
let active_tex: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_active_texture() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
let fbo_tex2d: unsafe extern "C" fn(u32, u32, u32, u32, i32) =
transmute(syms.get_gl_framebuffer_texture_2d() as *const c_void);
let bind_rbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_renderbuffer() as *const c_void);
let rbo_storage: unsafe extern "C" fn(u32, u32, i32, i32) =
transmute(syms.get_gl_renderbuffer_storage() as *const c_void);
let fbo_rbo: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_framebuffer_renderbuffer() as *const c_void);
if sc.get_scene_w() != req_w || sc.get_scene_h() != req_h {
active_tex(LINUX_GL_TEXTURE3);
let old_tex: u32 = sc.get_scene_color_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_empty_rgba8_texture(syms, req_w, req_h);
sc.set_scene_color_tex(new_tex);
bind_rbo(LINUX_GL_RENDERBUFFER, sc.get_scene_depth_rbo());
rbo_storage(
LINUX_GL_RENDERBUFFER,
LINUX_GL_DEPTH_COMPONENT24,
req_w as i32,
req_h as i32,
);
bind_fbo(LINUX_GL_FRAMEBUFFER, sc.get_scene_fbo());
fbo_tex2d(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_COLOR_ATTACHMENT0,
LINUX_GL_TEXTURE_2D,
new_tex,
0,
);
fbo_rbo(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_DEPTH_ATTACHMENT,
LINUX_GL_RENDERBUFFER,
sc.get_scene_depth_rbo(),
);
sc.set_scene_w(req_w);
sc.set_scene_h(req_h);
}
let gl_viewport: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_viewport() as *const c_void);
let gl_disable: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable() as *const c_void);
let gl_enable: unsafe extern "C" fn(u32) = transmute(syms.get_gl_enable() as *const c_void);
let depth_mask: unsafe extern "C" fn(u8) =
transmute(syms.get_gl_depth_mask() as *const c_void);
let depth_func: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_depth_func() as *const c_void);
let clear_depthf: unsafe extern "C" fn(f32) =
transmute(syms.get_gl_clear_depthf() as *const c_void);
let clear_color: unsafe extern "C" fn(f32, f32, f32, f32) =
transmute(syms.get_gl_clear_color() as *const c_void);
let gl_clear: unsafe extern "C" fn(u32) = transmute(syms.get_gl_clear() as *const c_void);
let use_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let uni4f: unsafe extern "C" fn(i32, f32, f32, f32, f32) =
transmute(syms.get_gl_uniform4f() as *const c_void);
let uni_mat4: unsafe extern "C" fn(i32, i32, u8, *const f32) =
transmute(syms.get_gl_uniform_matrix4fv() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let buf_data: unsafe extern "C" fn(u32, isize, *const c_void, u32) =
transmute(syms.get_gl_buffer_data() as *const c_void);
let buf_sub: unsafe extern "C" fn(u32, isize, isize, *const c_void) =
transmute(syms.get_gl_buffer_sub_data() as *const c_void);
let draw_arrays: unsafe extern "C" fn(u32, i32, i32) =
transmute(syms.get_gl_draw_arrays() as *const c_void);
let draw_instanced: unsafe extern "C" fn(u32, i32, i32, i32) =
transmute(syms.get_gl_draw_arrays_instanced() as *const c_void);
let attrib_ptr: unsafe extern "C" fn(u32, i32, u32, u8, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_pointer() as *const c_void);
bind_fbo(LINUX_GL_FRAMEBUFFER, sc.get_scene_fbo());
gl_disable(LINUX_GL_SCISSOR_TEST);
gl_viewport(0, 0, req_w as i32, req_h as i32);
match pass {
DirectGpuScenePass::OceanFluid(ocean) => {
let wake_w: u32 = ocean.get_wake_width().max(1);
let wake_h: u32 = ocean.get_wake_height().max(1);
let wake_bytes: &[u8] = ocean.get_wake_rgba8().as_slice();
let req_wake: usize = (wake_w as usize)
.saturating_mul(wake_h as usize)
.saturating_mul(4);
if wake_bytes.len() >= req_wake
&& (sc.get_wake_w() != wake_w
|| sc.get_wake_h() != wake_h
|| ocean.get_has_active_wake())
{
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
let tex_sub: unsafe extern "C" fn(
u32,
i32,
i32,
i32,
i32,
i32,
u32,
u32,
*const c_void,
) = transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
active_tex(LINUX_GL_TEXTURE0);
if sc.get_wake_w() != wake_w || sc.get_wake_h() != wake_h {
let old_wake: u32 = sc.get_wake_tex();
if old_wake != 0 {
del_tex(1, &old_wake);
}
let new_wake: u32 = linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
wake_w,
wake_h,
wake_bytes,
4,
);
sc.set_wake_tex(new_wake);
sc.set_wake_w(wake_w);
sc.set_wake_h(wake_h);
} else {
bind_tex(LINUX_GL_TEXTURE_2D, sc.get_wake_tex());
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, 4);
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_ROWS, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_PIXELS, 0);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
wake_w as i32,
wake_h as i32,
LINUX_GL_RGBA,
LINUX_GL_UNSIGNED_BYTE,
wake_bytes.as_ptr().cast::<c_void>(),
);
}
}
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
gl_disable(LINUX_GL_BLEND);
use_prog(sc.get_ocean_program());
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, sc.get_wake_tex());
let boat_uv: [f32; 2] = ocean.get_boat_uv();
let sun: [f32; 3] = ocean.get_sun_dir();
if sc.get_ocean_time_loc() >= 0 {
uni4f(
sc.get_ocean_time_loc(),
ocean.get_sim_time(),
ocean.get_agitation(),
ocean.get_horizon_v(),
ocean.get_aspect(),
);
}
if sc.get_ocean_roll_loc() >= 0 {
uni4f(
sc.get_ocean_roll_loc(),
ocean.get_roll_cos(),
ocean.get_roll_sin(),
boat_uv[0],
boat_uv[1],
);
}
if sc.get_ocean_sun_loc() >= 0 {
uni4f(
sc.get_ocean_sun_loc(),
sun[0],
sun[1],
sun[2],
if ocean.get_has_active_wake() {
1.0
} else {
0.0
},
);
}
bind_vao(ui_vao);
draw_arrays(LINUX_GL_TRIANGLES, 0, 3);
let boat_verts: &[GpuOceanBoatVertex] = ocean.get_boat_vertices().as_slice();
if !boat_verts.is_empty() {
let byte_len: usize = boat_verts
.len()
.saturating_mul(size_of::<GpuOceanBoatVertex>());
gl_disable(LINUX_GL_BLEND);
depth_mask(1);
clear_depthf(1.0);
gl_clear(LINUX_GL_DEPTH_BUFFER_BIT);
gl_enable(LINUX_GL_DEPTH_TEST);
depth_func(LINUX_GL_LESS);
use_prog(sc.get_boat_program());
bind_vao(sc.get_boat_vao());
bind_buf(LINUX_GL_ARRAY_BUFFER, sc.get_boat_vbo());
if byte_len > sc.get_boat_vbo_cap() {
let cap: usize = byte_len.max(1024).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
sc.set_boat_vbo_cap(cap);
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
byte_len as isize,
boat_verts.as_ptr().cast::<c_void>(),
);
draw_arrays(LINUX_GL_TRIANGLES, 0, boat_verts.len() as i32);
bind_vao(0);
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
}
}
DirectGpuScenePass::World3d(world) => {
let cam: [f32; 4] = world.get_camera_pos();
let sun_fog: [f32; 4] = world.get_sun_dir_fog();
let sun_col: [f32; 4] = world.get_sun_color_ambient();
let sky_hor: [f32; 4] = world.get_sky_horizon_tod();
let sky_zen: [f32; 4] = world.get_sky_zenith_aspect();
gl_disable(LINUX_GL_BLEND);
depth_mask(1);
clear_depthf(1.0);
clear_color(0.04, 0.05, 0.09, 1.0);
gl_clear(LINUX_GL_COLOR_BUFFER_BIT | LINUX_GL_DEPTH_BUFFER_BIT);
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
use_prog(sc.get_sky3d_program());
let sky_vp: [f32; 16] = world.get_view_proj();
if sc.get_sky3d_vp_loc() >= 0 {
uni_mat4(sc.get_sky3d_vp_loc(), 1, 0, sky_vp.as_ptr());
}
if sc.get_sky3d_cam_loc() >= 0 {
uni4f(sc.get_sky3d_cam_loc(), cam[0], cam[1], cam[2], cam[3]);
}
if sc.get_sky3d_sun_dir_loc() >= 0 {
uni4f(
sc.get_sky3d_sun_dir_loc(),
sun_fog[0],
sun_fog[1],
sun_fog[2],
sun_fog[3],
);
}
if sc.get_sky3d_sun_loc() >= 0 {
uni4f(
sc.get_sky3d_sun_loc(),
sun_col[0],
sun_col[1],
sun_col[2],
sun_col[3],
);
}
if sc.get_sky3d_hor_loc() >= 0 {
uni4f(
sc.get_sky3d_hor_loc(),
sky_hor[0],
sky_hor[1],
sky_hor[2],
sky_hor[3],
);
}
if sc.get_sky3d_zen_loc() >= 0 {
uni4f(
sc.get_sky3d_zen_loc(),
sky_zen[0],
sky_zen[1],
sky_zen[2],
sky_zen[3],
);
}
bind_vao(ui_vao);
draw_arrays(LINUX_GL_TRIANGLES, 0, 3);
let verts: &[GpuWorld3dVertex] = world.get_mesh_buffer().get_vertices().as_slice();
let insts: &[GpuWorld3dInstance] = world.get_instances().as_slice();
if !verts.is_empty() && !insts.is_empty() {
let mesh_rev: u64 = world.get_mesh_buffer().get_revision();
bind_vao(sc.get_mesh3d_vao());
if sc.get_mesh3d_vert_rev() != mesh_rev || sc.get_mesh3d_vert_cap() == 0 {
let vbytes: usize =
verts.len().saturating_mul(size_of::<GpuWorld3dVertex>());
bind_buf(LINUX_GL_ARRAY_BUFFER, sc.get_mesh3d_vert_vbo());
if vbytes > sc.get_mesh3d_vert_cap() {
let cap: usize = vbytes.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STATIC_DRAW,
);
sc.set_mesh3d_vert_cap(cap);
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
vbytes as isize,
verts.as_ptr().cast::<c_void>(),
);
sc.set_mesh3d_vert_rev(mesh_rev);
}
let ibytes: usize = insts.len().saturating_mul(size_of::<GpuWorld3dInstance>());
bind_buf(LINUX_GL_ARRAY_BUFFER, sc.get_mesh3d_inst_vbo());
if ibytes > sc.get_mesh3d_inst_cap() {
let cap: usize = ibytes.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
sc.set_mesh3d_inst_cap(cap);
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
ibytes as isize,
insts.as_ptr().cast::<c_void>(),
);
gl_enable(LINUX_GL_DEPTH_TEST);
depth_func(LINUX_GL_LESS);
depth_mask(1);
use_prog(sc.get_mesh3d_program());
let vp: [f32; 16] = world.get_view_proj();
if sc.get_mesh3d_vp_loc() >= 0 {
uni_mat4(sc.get_mesh3d_vp_loc(), 1, 0, vp.as_ptr());
}
if sc.get_mesh3d_cam_loc() >= 0 {
uni4f(sc.get_mesh3d_cam_loc(), cam[0], cam[1], cam[2], cam[3]);
}
if sc.get_mesh3d_sun_fog_loc() >= 0 {
uni4f(
sc.get_mesh3d_sun_fog_loc(),
sun_fog[0],
sun_fog[1],
sun_fog[2],
sun_fog[3],
);
}
if sc.get_mesh3d_sun_col_loc() >= 0 {
uni4f(
sc.get_mesh3d_sun_col_loc(),
sun_col[0],
sun_col[1],
sun_col[2],
sun_col[3],
);
}
if sc.get_mesh3d_hor_loc() >= 0 {
uni4f(
sc.get_mesh3d_hor_loc(),
sky_hor[0],
sky_hor[1],
sky_hor[2],
sky_hor[3],
);
}
if sc.get_mesh3d_zen_loc() >= 0 {
uni4f(
sc.get_mesh3d_zen_loc(),
sky_zen[0],
sky_zen[1],
sky_zen[2],
sky_zen[3],
);
}
for batch in world.get_draw_batches().iter() {
if batch.get_vertex_count() == 0 || batch.get_instance_count() == 0 {
continue;
}
let base_byte_off: usize =
(batch.get_instance_start() as usize).saturating_mul(96);
for slot in 0u32..6u32 {
attrib_ptr(
3 + slot,
4,
LINUX_GL_FLOAT,
0,
96,
(base_byte_off + (slot as usize) * 16) as *const c_void,
);
}
draw_instanced(
LINUX_GL_TRIANGLES,
batch.get_vertex_start() as i32,
batch.get_vertex_count() as i32,
batch.get_instance_count() as i32,
);
}
bind_vao(0);
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
}
}
DirectGpuScenePass::PbrScene(pbr) => {
gl_disable(LINUX_GL_BLEND);
depth_mask(1);
clear_depthf(1.0);
clear_color(0.04, 0.05, 0.09, 1.0);
gl_clear(LINUX_GL_COLOR_BUFFER_BIT | LINUX_GL_DEPTH_BUFFER_BIT);
if linux_gles_ensure_pbr_resources(syms, &mut sc.pbr)
&& let Some(res) = sc.pbr.as_mut()
{
linux_gles_apply_pbr_mip_uploads(
syms,
res,
pbr.get_texture_uploads().as_slice(),
);
let tex_img: unsafe extern "C" fn(
u32,
i32,
i32,
i32,
i32,
i32,
u32,
u32,
*const c_void,
) = transmute(syms.get_gl_tex_image_2d() as *const c_void);
let uni4i: unsafe extern "C" fn(i32, i32, i32, i32, i32) =
transmute(syms.get_gl_uniform4i() as *const c_void);
let uni4fv: unsafe extern "C" fn(i32, i32, *const f32) =
transmute(syms.get_gl_uniform4fv() as *const c_void);
let draw_elem_inst: unsafe extern "C" fn(u32, i32, u32, *const c_void, i32) =
transmute(syms.get_gl_draw_elements_instanced() as *const c_void);
let attrib_iptr: unsafe extern "C" fn(u32, i32, u32, i32, *const c_void) =
transmute(syms.get_gl_vertex_attrib_ipointer() as *const c_void);
let enable_attr: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_enable_vertex_attrib_array() as *const c_void);
let disable_attr: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable_vertex_attrib_array() as *const c_void);
let gl_enable_blend: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_blend_func_separate() as *const c_void);
let cam_exp: [f32; 4] = pbr.get_camera_pos_exposure();
let sun_fog: [f32; 4] = pbr.get_sun_dir_fog();
let sun_col: [f32; 4] = pbr.get_sun_color_ambient();
let sky_hor: [f32; 4] = pbr.get_sky_horizon_tod();
let sky_zen: [f32; 4] = pbr.get_sky_zenith_agx();
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
use_prog(res.sky_program);
if res.sky_cam_loc >= 0 {
uni4f(
res.sky_cam_loc,
cam_exp[0],
cam_exp[1],
cam_exp[2],
cam_exp[3],
);
}
if res.sky_sun_fog_loc >= 0 {
uni4f(
res.sky_sun_fog_loc,
sun_fog[0],
sun_fog[1],
sun_fog[2],
sun_fog[3],
);
}
if res.sky_sun_col_loc >= 0 {
uni4f(
res.sky_sun_col_loc,
sun_col[0],
sun_col[1],
sun_col[2],
sun_col[3],
);
}
if res.sky_hor_loc >= 0 {
uni4f(
res.sky_hor_loc,
sky_hor[0],
sky_hor[1],
sky_hor[2],
sky_hor[3],
);
}
if res.sky_zen_agx_loc >= 0 {
uni4f(
res.sky_zen_agx_loc,
sky_zen[0],
sky_zen[1],
sky_zen[2],
sky_zen[3],
);
}
bind_vao(ui_vao);
draw_arrays(LINUX_GL_TRIANGLES, 0, 3);
let mesh_buf: &GpuPbrMeshBuffer = pbr.get_mesh_buffer();
let verts: &[QuantizedVertex3D] = mesh_buf.get_vertices().as_slice();
let indices: &[u32] = mesh_buf.get_indices().as_slice();
let insts: &[GpuPbrInstance3D] = pbr.get_instances().as_slice();
let batches: &[GpuPbrDrawBatch] = pbr.get_draw_batches().as_slice();
if !verts.is_empty()
&& !indices.is_empty()
&& !insts.is_empty()
&& !batches.is_empty()
{
bind_vao(res.vao);
let rev: u64 = mesh_buf.get_revision();
if res.mesh_rev != rev || res.vert_cap == 0 || res.idx_cap == 0 {
let vbytes: usize =
verts.len().saturating_mul(size_of::<QuantizedVertex3D>());
bind_buf(LINUX_GL_ARRAY_BUFFER, res.vert_vbo);
if vbytes > res.vert_cap {
let cap: usize = vbytes.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STATIC_DRAW,
);
res.vert_cap = cap;
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
vbytes as isize,
verts.as_ptr().cast::<c_void>(),
);
let ibytes: usize = indices.len().saturating_mul(size_of::<u32>());
bind_buf(LINUX_GL_ELEMENT_ARRAY_BUFFER, res.idx_ebo);
if ibytes > res.idx_cap {
let cap: usize = ibytes.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ELEMENT_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STATIC_DRAW,
);
res.idx_cap = cap;
}
buf_sub(
LINUX_GL_ELEMENT_ARRAY_BUFFER,
0,
ibytes as isize,
indices.as_ptr().cast::<c_void>(),
);
let skin_verts: &[QuantizedSkinVertex3D] =
mesh_buf.get_skin_vertices().as_slice();
if !skin_verts.is_empty() {
let sbytes: usize = skin_verts
.len()
.saturating_mul(size_of::<QuantizedSkinVertex3D>());
bind_buf(LINUX_GL_ARRAY_BUFFER, res.skin_vbo);
if sbytes > res.skin_cap {
let cap: usize = sbytes.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STATIC_DRAW,
);
res.skin_cap = cap;
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
sbytes as isize,
skin_verts.as_ptr().cast::<c_void>(),
);
enable_attr(3);
attrib_iptr(3, 4, LINUX_GL_UNSIGNED_SHORT, 16, null());
enable_attr(4);
attrib_ptr(4, 4, LINUX_GL_UNSIGNED_BYTE, 1, 16, 8 as *const c_void);
} else {
disable_attr(3);
disable_attr(4);
}
let morph_deltas: &[[f32; 4]] = mesh_buf.get_morph_deltas().as_slice();
if !morph_deltas.is_empty() {
let rows: usize = morph_deltas.len().div_ceil(256).max(1);
res.data_tex_scratch.clear();
res.data_tex_scratch.resize(rows * 256 * 4, 0.0);
for (mi, delta) in morph_deltas.iter().enumerate() {
let dst: usize = mi * 4;
res.data_tex_scratch[dst..dst + 4].copy_from_slice(delta);
}
bind_tex(LINUX_GL_TEXTURE_2D, res.morph_tex);
tex_img(
LINUX_GL_TEXTURE_2D,
0,
LINUX_GL_RGBA32F as i32,
256,
rows as i32,
0,
LINUX_GL_RGBA,
LINUX_GL_FLOAT,
res.data_tex_scratch.as_ptr().cast::<c_void>(),
);
}
res.mesh_rev = rev;
}
let inst_bytes: usize =
insts.len().saturating_mul(size_of::<GpuPbrInstance3D>());
bind_buf(LINUX_GL_ARRAY_BUFFER, res.inst_vbo);
if inst_bytes > res.inst_cap {
let cap: usize = inst_bytes.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
cap as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
res.inst_cap = cap;
}
buf_sub(
LINUX_GL_ARRAY_BUFFER,
0,
inst_bytes as isize,
insts.as_ptr().cast::<c_void>(),
);
let default_mat: [GpuMaterialData3D; 1] = [GpuMaterialData3D::default()];
let mats_slice: &[GpuMaterialData3D] = if pbr.get_materials().is_empty() {
&default_mat
} else {
pbr.get_materials().as_slice()
};
bind_tex(LINUX_GL_TEXTURE_2D, res.mat_tex);
tex_img(
LINUX_GL_TEXTURE_2D,
0,
LINUX_GL_RGBA32F as i32,
10,
mats_slice.len() as i32,
0,
LINUX_GL_RGBA,
LINUX_GL_FLOAT,
mats_slice.as_ptr().cast::<c_void>(),
);
let bones_slice: &[[f32; 16]] = pbr.get_bone_matrices().as_slice();
if !bones_slice.is_empty() {
let bone_rows: usize = bones_slice.len().div_ceil(64).max(1);
res.data_tex_scratch.clear();
res.data_tex_scratch.resize(bone_rows * 256 * 4, 0.0);
for (bi, bm) in bones_slice.iter().enumerate() {
let dst: usize = bi * 16;
res.data_tex_scratch[dst..dst + 16].copy_from_slice(bm);
}
bind_tex(LINUX_GL_TEXTURE_2D, res.bone_tex);
tex_img(
LINUX_GL_TEXTURE_2D,
0,
LINUX_GL_RGBA32F as i32,
256,
bone_rows as i32,
0,
LINUX_GL_RGBA,
LINUX_GL_FLOAT,
res.data_tex_scratch.as_ptr().cast::<c_void>(),
);
}
let lights_slice: &[GpuPackedLocalLight] =
pbr.get_local_lights().as_slice();
if !lights_slice.is_empty() {
let lcount: usize = lights_slice.len().min(PBR_MAX_LOCAL_LIGHTS);
bind_tex(LINUX_GL_TEXTURE_2D, res.light_tex);
tex_img(
LINUX_GL_TEXTURE_2D,
0,
LINUX_GL_RGBA32F as i32,
4,
lcount as i32,
0,
LINUX_GL_RGBA,
LINUX_GL_FLOAT,
lights_slice.as_ptr().cast::<c_void>(),
);
}
active_tex(LINUX_GL_TEXTURE0 + 5);
bind_tex(LINUX_GL_TEXTURE_2D, res.bone_tex);
active_tex(LINUX_GL_TEXTURE0 + 6);
bind_tex(LINUX_GL_TEXTURE_2D, res.mat_tex);
active_tex(LINUX_GL_TEXTURE0 + 7);
bind_tex(LINUX_GL_TEXTURE_2D, res.light_tex);
active_tex(LINUX_GL_TEXTURE0 + 8);
bind_tex(LINUX_GL_TEXTURE_2D, res.morph_tex);
gl_enable(LINUX_GL_DEPTH_TEST);
depth_func(LINUX_GL_LESS);
depth_mask(1);
use_prog(res.scene_program);
let vp: [f32; 16] = pbr.get_view_proj();
if res.vp_loc >= 0 {
uni_mat4(res.vp_loc, 1, 0, vp.as_ptr());
}
if res.cam_exp_loc >= 0 {
uni4f(
res.cam_exp_loc,
cam_exp[0],
cam_exp[1],
cam_exp[2],
cam_exp[3],
);
}
if res.sun_fog_loc >= 0 {
uni4f(
res.sun_fog_loc,
sun_fog[0],
sun_fog[1],
sun_fog[2],
sun_fog[3],
);
}
if res.sun_col_loc >= 0 {
uni4f(
res.sun_col_loc,
sun_col[0],
sun_col[1],
sun_col[2],
sun_col[3],
);
}
if res.hor_loc >= 0 {
uni4f(res.hor_loc, sky_hor[0], sky_hor[1], sky_hor[2], sky_hor[3]);
}
if res.zen_agx_loc >= 0 {
uni4f(
res.zen_agx_loc,
sky_zen[0],
sky_zen[1],
sky_zen[2],
sky_zen[3],
);
}
if res.ibl_sh_loc >= 0 {
let sh_arr: [[f32; 4]; 9] = pbr.get_ibl_sh();
uni4fv(res.ibl_sh_loc, 9, sh_arr.as_ptr().cast::<f32>());
}
if res.morph_weights_loc >= 0 {
let mut flat_mw: [f32; 16] = [0.0; 16];
let mw_src: &[f32] = pbr.get_morph_weights().as_slice();
let copy_len: usize = mw_src.len().min(16);
flat_mw[..copy_len].copy_from_slice(&mw_src[..copy_len]);
uni4fv(res.morph_weights_loc, 4, flat_mw.as_ptr());
}
let morph_stride: i32 = if !mesh_buf.get_morph_deltas().is_empty() {
verts.len() as i32
} else {
0
};
let light_count: i32 = lights_slice.len().min(PBR_MAX_LOCAL_LIGHTS) as i32;
let mut alpha_active: bool = false;
bind_buf(LINUX_GL_ARRAY_BUFFER, res.inst_vbo);
for batch in batches.iter() {
if batch.get_index_count() == 0 || batch.get_instance_count() == 0 {
continue;
}
let want_alpha: bool =
batch.get_pso_variant() == PBR_PSO_VARIANT_ALPHA_BLEND;
if want_alpha != alpha_active {
if want_alpha {
gl_enable(LINUX_GL_BLEND);
gl_enable_blend(
LINUX_GL_SRC_ALPHA,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
LINUX_GL_ONE,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
);
depth_mask(0);
} else {
gl_disable(LINUX_GL_BLEND);
depth_mask(1);
}
alpha_active = want_alpha;
}
let base_byte_off: usize =
(batch.get_instance_start() as usize).saturating_mul(112);
for attr in 0u32..6u32 {
attrib_ptr(
5 + attr,
4,
LINUX_GL_FLOAT,
0,
112,
(base_byte_off + (attr as usize) * 16) as *const c_void,
);
}
attrib_iptr(
11,
4,
LINUX_GL_UNSIGNED_INT,
112,
(base_byte_off + 96) as *const c_void,
);
let mat_idx: u32 = batch
.get_material_index()
.min(mats_slice.len().saturating_sub(1) as u32);
let mat_ref: &GpuMaterialData3D = &mats_slice[mat_idx as usize];
let tex_slots: [u32; 5] = [
mat_ref.get_tex_indices_a()[0],
mat_ref.get_tex_indices_a()[1],
mat_ref.get_tex_indices_a()[2],
mat_ref.get_tex_indices_a()[3],
mat_ref.get_tex_indices_b()[0],
];
let mut tex_mask: i32 = 0;
let mut mip_tier: i32 = 3;
for (unit, &slot_id) in tex_slots.iter().enumerate() {
active_tex(LINUX_GL_TEXTURE0 + (unit as u32));
if slot_id != GPU_MATERIAL_NO_TEXTURE
&& let Some(res_tex) = res.textures.get(&slot_id)
&& res_tex.tex_id != 0
{
tex_mask |= 1i32 << unit;
if unit == 0 {
mip_tier = if res_tex.min_lod_clamp == 0 {
0
} else if res_tex.min_lod_clamp <= 2 {
1
} else {
2
};
}
bind_tex(LINUX_GL_TEXTURE_2D, res_tex.tex_id);
} else {
bind_tex(LINUX_GL_TEXTURE_2D, res.dummy_white_tex);
}
}
let lod_mip_mask: i32 = i32::from(batch.get_lod_level() & 3)
| ((mip_tier & 255) << 8)
| ((tex_mask & 31) << 16);
if res.batch_params_loc >= 0 {
uni4i(
res.batch_params_loc,
i32::from(pbr.get_debug_view_mode()),
lod_mip_mask,
light_count,
mat_idx as i32,
);
}
if res.draw_offsets_loc >= 0 {
uni4i(
res.draw_offsets_loc,
batch.get_base_vertex() as i32,
batch.get_instance_start() as i32,
morph_stride,
batch.get_index_start() as i32,
);
}
let idx_off: usize =
(batch.get_index_start() as usize).saturating_mul(4);
draw_elem_inst(
LINUX_GL_TRIANGLES,
batch.get_index_count() as i32,
LINUX_GL_UNSIGNED_INT,
idx_off as *const c_void,
batch.get_instance_count() as i32,
);
}
if alpha_active {
gl_disable(LINUX_GL_BLEND);
depth_mask(1);
}
active_tex(LINUX_GL_TEXTURE0);
bind_vao(0);
gl_disable(LINUX_GL_DEPTH_TEST);
depth_mask(0);
}
}
}
}
bind_fbo(LINUX_GL_FRAMEBUFFER, 0);
sc.get_scene_color_tex()
}
}
unsafe fn linux_gles_destroy_scene_pipeline(
syms: &LinuxEglGlesSyms,
scene: &LinuxEglScenePipeline,
) {
unsafe {
if syms.get_gl_delete_textures() != 0 {
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let texs: [u32; 2] = [scene.get_scene_color_tex(), scene.get_wake_tex()];
del_tex(2, texs.as_ptr());
}
if syms.get_gl_delete_renderbuffers() != 0 && scene.get_scene_depth_rbo() != 0 {
let del_rbo: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_renderbuffers() as *const c_void);
let rbo: u32 = scene.get_scene_depth_rbo();
del_rbo(1, &rbo);
}
if syms.get_gl_delete_framebuffers() != 0 && scene.get_scene_fbo() != 0 {
let del_fbo: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_framebuffers() as *const c_void);
let fbo: u32 = scene.get_scene_fbo();
del_fbo(1, &fbo);
}
if syms.get_gl_delete_buffers() != 0 {
let del_buf: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_buffers() as *const c_void);
let bufs: [u32; 3] = [
scene.get_boat_vbo(),
scene.get_mesh3d_vert_vbo(),
scene.get_mesh3d_inst_vbo(),
];
del_buf(3, bufs.as_ptr());
}
if syms.get_gl_delete_vertex_arrays() != 0 {
let del_vao: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_vertex_arrays() as *const c_void);
let vaos: [u32; 2] = [scene.get_boat_vao(), scene.get_mesh3d_vao()];
del_vao(2, vaos.as_ptr());
}
if syms.get_gl_delete_program() != 0 {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
for prog in [
scene.get_ocean_program(),
scene.get_boat_program(),
scene.get_sky3d_program(),
scene.get_mesh3d_program(),
] {
if prog != 0 {
del_prog(prog);
}
}
}
if let Some(pbr_res) = scene.pbr.as_ref() {
if syms.get_gl_delete_textures() != 0 {
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let data_texs: [u32; 5] = [
pbr_res.bone_tex,
pbr_res.mat_tex,
pbr_res.light_tex,
pbr_res.morph_tex,
pbr_res.dummy_white_tex,
];
del_tex(5, data_texs.as_ptr());
for entry in pbr_res.textures.values() {
if entry.tex_id != 0 {
del_tex(1, &entry.tex_id);
}
}
}
if syms.get_gl_delete_buffers() != 0 {
let del_buf: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_buffers() as *const c_void);
let pbr_bufs: [u32; 4] = [
pbr_res.vert_vbo,
pbr_res.idx_ebo,
pbr_res.skin_vbo,
pbr_res.inst_vbo,
];
del_buf(4, pbr_bufs.as_ptr());
}
if syms.get_gl_delete_vertex_arrays() != 0 && pbr_res.vao != 0 {
let del_vao: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_vertex_arrays() as *const c_void);
del_vao(1, &pbr_res.vao);
}
if syms.get_gl_delete_program() != 0 {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
for prog in [pbr_res.sky_program, pbr_res.scene_program] {
if prog != 0 {
del_prog(prog);
}
}
}
}
}
}
unsafe fn linux_gles_ensure_blur_pipeline(
syms: &LinuxEglGlesSyms,
slot: &mut Option<LinuxEglBlurPipeline>,
width: u32,
height: u32,
) -> bool {
if syms.get_gl_gen_framebuffers() == 0
|| syms.get_gl_bind_framebuffer() == 0
|| syms.get_gl_framebuffer_texture_2d() == 0
|| syms.get_gl_uniform1f() == 0
|| syms.get_gl_uniform4f() == 0
|| syms.get_gl_draw_arrays() == 0
{
return false;
}
let req_w: u32 = width.max(1);
let req_h: u32 = height.max(1);
unsafe {
if slot.is_none() {
let Some(program) = linux_gles_link_program(
syms,
GLSL_ES3_BACKDROP_BLUR_VERTEX_SHADER,
GLSL_ES3_BACKDROP_BLUR_FRAGMENT_SHADER,
) else {
return false;
};
let use_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let get_uni: unsafe extern "C" fn(u32, *const c_char) -> i32 =
transmute(syms.get_gl_get_uniform_location() as *const c_void);
let uni1i: unsafe extern "C" fn(i32, i32) =
transmute(syms.get_gl_uniform1i() as *const c_void);
let gen_fbos: unsafe extern "C" fn(i32, *mut u32) =
transmute(syms.get_gl_gen_framebuffers() as *const c_void);
use_prog(program);
let tex_loc: i32 =
get_uni(program, GPU_BLUR_UNIFORM_TEX_CSTR.as_ptr().cast::<c_char>());
if tex_loc >= 0 {
uni1i(tex_loc, 0);
}
let texel_loc: i32 = get_uni(
program,
GPU_BLUR_UNIFORM_TEXEL_CSTR.as_ptr().cast::<c_char>(),
);
let offset_loc: i32 = get_uni(
program,
GPU_BLUR_UNIFORM_OFFSET_CSTR.as_ptr().cast::<c_char>(),
);
let mode_loc: i32 = get_uni(
program,
GPU_BLUR_UNIFORM_MODE_CSTR.as_ptr().cast::<c_char>(),
);
let clip_uv_loc: i32 = get_uni(
program,
GPU_BLUR_UNIFORM_CLIP_UV_CSTR.as_ptr().cast::<c_char>(),
);
let mut fbos: [u32; 3] = [0; 3];
gen_fbos(3, fbos.as_mut_ptr());
*slot = Some(LinuxEglBlurPipeline {
program,
texel_loc,
offset_loc,
mode_loc,
clip_uv_loc,
scene_fbo: fbos[0],
scene_tex: 0,
half_fbo: fbos[1],
half_tex: 0,
full_fbo: fbos[2],
full_tex: 0,
width: 0,
height: 0,
});
}
let Some(blur) = slot.as_mut() else {
return false;
};
if blur.get_width() != req_w || blur.get_height() != req_h {
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
let fbo_tex2d: unsafe extern "C" fn(u32, u32, u32, u32, i32) =
transmute(syms.get_gl_framebuffer_texture_2d() as *const c_void);
let old_texs: [u32; 3] = [
blur.get_scene_tex(),
blur.get_half_tex(),
blur.get_full_tex(),
];
for old_id in old_texs {
if old_id != 0 {
del_tex(1, &old_id);
}
}
let half_w: u32 = (req_w / 2).max(1);
let half_h: u32 = (req_h / 2).max(1);
let scene_tex: u32 = linux_gles_create_empty_rgba8_texture(syms, req_w, req_h);
let half_tex: u32 = linux_gles_create_empty_rgba8_texture(syms, half_w, half_h);
let full_tex: u32 = linux_gles_create_empty_rgba8_texture(syms, req_w, req_h);
bind_fbo(LINUX_GL_FRAMEBUFFER, blur.get_scene_fbo());
fbo_tex2d(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_COLOR_ATTACHMENT0,
LINUX_GL_TEXTURE_2D,
scene_tex,
0,
);
bind_fbo(LINUX_GL_FRAMEBUFFER, blur.get_half_fbo());
fbo_tex2d(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_COLOR_ATTACHMENT0,
LINUX_GL_TEXTURE_2D,
half_tex,
0,
);
bind_fbo(LINUX_GL_FRAMEBUFFER, blur.get_full_fbo());
fbo_tex2d(
LINUX_GL_FRAMEBUFFER,
LINUX_GL_COLOR_ATTACHMENT0,
LINUX_GL_TEXTURE_2D,
full_tex,
0,
);
bind_fbo(LINUX_GL_FRAMEBUFFER, 0);
blur.set_scene_tex(scene_tex);
blur.set_half_tex(half_tex);
blur.set_full_tex(full_tex);
blur.set_width(req_w);
blur.set_height(req_h);
}
blur.get_scene_tex() != 0 && blur.get_half_tex() != 0 && blur.get_full_tex() != 0
}
}
unsafe fn linux_gles_run_blur_pass(
syms: &LinuxEglGlesSyms,
blur: &LinuxEglBlurPipeline,
blur_prim: &GpuPrimitiveInstance,
ui_vao: u32,
vw: u32,
vh: u32,
) {
let half_w: u32 = (vw / 2).max(1);
let half_h: u32 = (vh / 2).max(1);
let blur_offset: f32 = blur_prim.backdrop_blur_offset();
let clip_uv: [f32; 4] =
blur_prim.backdrop_blur_clip_uv((vw as f32, vh as f32), [0.0, 0.0], true);
unsafe {
let gl_disable: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable() as *const c_void);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
let gl_viewport: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_viewport() as *const c_void);
let use_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let active_tex: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_active_texture() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let uni2f: unsafe extern "C" fn(i32, f32, f32) =
transmute(syms.get_gl_uniform2f() as *const c_void);
let uni1f: unsafe extern "C" fn(i32, f32) =
transmute(syms.get_gl_uniform1f() as *const c_void);
let uni1i: unsafe extern "C" fn(i32, i32) =
transmute(syms.get_gl_uniform1i() as *const c_void);
let uni4f: unsafe extern "C" fn(i32, f32, f32, f32, f32) =
transmute(syms.get_gl_uniform4f() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let draw_arrays: unsafe extern "C" fn(u32, i32, i32) =
transmute(syms.get_gl_draw_arrays() as *const c_void);
gl_disable(LINUX_GL_SCISSOR_TEST);
gl_disable(LINUX_GL_BLEND);
use_prog(blur.get_program());
bind_vao(ui_vao);
active_tex(LINUX_GL_TEXTURE0);
if blur.get_offset_loc() >= 0 {
uni1f(blur.get_offset_loc(), blur_offset);
}
if blur.get_clip_uv_loc() >= 0 {
uni4f(
blur.get_clip_uv_loc(),
clip_uv[0],
clip_uv[1],
clip_uv[2],
clip_uv[3],
);
}
bind_fbo(LINUX_GL_FRAMEBUFFER, blur.get_half_fbo());
gl_viewport(0, 0, half_w as i32, half_h as i32);
bind_tex(LINUX_GL_TEXTURE_2D, blur.get_scene_tex());
if blur.get_texel_loc() >= 0 {
uni2f(
blur.get_texel_loc(),
1.0 / (vw.max(1) as f32),
1.0 / (vh.max(1) as f32),
);
}
if blur.get_mode_loc() >= 0 {
uni1i(blur.get_mode_loc(), GPU_BLUR_PASS_MODE_DOWN as i32);
}
draw_arrays(LINUX_GL_TRIANGLES, 0, 3);
bind_fbo(LINUX_GL_FRAMEBUFFER, blur.get_full_fbo());
gl_viewport(0, 0, vw as i32, vh as i32);
bind_tex(LINUX_GL_TEXTURE_2D, blur.get_half_tex());
if blur.get_texel_loc() >= 0 {
uni2f(
blur.get_texel_loc(),
1.0 / (half_w as f32),
1.0 / (half_h as f32),
);
}
if blur.get_mode_loc() >= 0 {
uni1i(blur.get_mode_loc(), GPU_BLUR_PASS_MODE_UP as i32);
}
draw_arrays(LINUX_GL_TRIANGLES, 0, 3);
}
}
unsafe fn linux_gles_copy_blur_scene_to_default(
syms: &LinuxEglGlesSyms,
blur: &LinuxEglBlurPipeline,
ui_vao: u32,
vw: u32,
vh: u32,
) {
unsafe {
let gl_disable: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable() as *const c_void);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
let gl_viewport: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_viewport() as *const c_void);
let use_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let active_tex: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_active_texture() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let uni1i: unsafe extern "C" fn(i32, i32) =
transmute(syms.get_gl_uniform1i() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let draw_arrays: unsafe extern "C" fn(u32, i32, i32) =
transmute(syms.get_gl_draw_arrays() as *const c_void);
bind_fbo(LINUX_GL_FRAMEBUFFER, 0);
gl_disable(LINUX_GL_SCISSOR_TEST);
gl_disable(LINUX_GL_BLEND);
gl_viewport(0, 0, vw as i32, vh as i32);
use_prog(blur.get_program());
bind_vao(ui_vao);
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, blur.get_scene_tex());
if blur.get_mode_loc() >= 0 {
uni1i(blur.get_mode_loc(), GPU_BLUR_PASS_MODE_COPY as i32);
}
draw_arrays(LINUX_GL_TRIANGLES, 0, 3);
}
}
unsafe fn linux_gles_destroy_blur_pipeline(syms: &LinuxEglGlesSyms, blur: &LinuxEglBlurPipeline) {
unsafe {
if syms.get_gl_delete_textures() != 0 {
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let texs: [u32; 3] = [
blur.get_scene_tex(),
blur.get_half_tex(),
blur.get_full_tex(),
];
del_tex(3, texs.as_ptr());
}
if syms.get_gl_delete_framebuffers() != 0 {
let del_fbo: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_framebuffers() as *const c_void);
let fbos: [u32; 3] = [
blur.get_scene_fbo(),
blur.get_half_fbo(),
blur.get_full_fbo(),
];
del_fbo(3, fbos.as_ptr());
}
if syms.get_gl_delete_program() != 0 && blur.get_program() != 0 {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
del_prog(blur.get_program());
}
}
}
unsafe fn linux_gles_upload_atlas_dirty_rects(
syms: &LinuxEglGlesSyms,
pixel_fmt: u32,
alignment: i32,
atlas_w: u32,
atlas_h: u32,
pixels: &[u8],
dirty_rects: &[PixelDirtyRect],
) {
unsafe {
let pixel_store: unsafe extern "C" fn(u32, i32) =
transmute(syms.get_gl_pixel_storei() as *const c_void);
let tex_sub: unsafe extern "C" fn(u32, i32, i32, i32, i32, i32, u32, u32, *const c_void) =
transmute(syms.get_gl_tex_sub_image_2d() as *const c_void);
pixel_store(LINUX_GL_UNPACK_ALIGNMENT, alignment);
if dirty_rects.is_empty() {
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_ROWS, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_PIXELS, 0);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
0,
0,
atlas_w as i32,
atlas_h as i32,
pixel_fmt,
LINUX_GL_UNSIGNED_BYTE,
pixels.as_ptr().cast::<c_void>(),
);
return;
}
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, atlas_w as i32);
for rect in dirty_rects.iter() {
let rx: u32 = rect.get_x().min(atlas_w);
let ry: u32 = rect.get_y().min(atlas_h);
let rw: u32 = rect.get_width().min(atlas_w.saturating_sub(rx));
let rh: u32 = rect.get_height().min(atlas_h.saturating_sub(ry));
if rw == 0 || rh == 0 {
continue;
}
pixel_store(LINUX_GL_UNPACK_SKIP_PIXELS, rx as i32);
pixel_store(LINUX_GL_UNPACK_SKIP_ROWS, ry as i32);
tex_sub(
LINUX_GL_TEXTURE_2D,
0,
rx as i32,
ry as i32,
rw as i32,
rh as i32,
pixel_fmt,
LINUX_GL_UNSIGNED_BYTE,
pixels.as_ptr().cast::<c_void>(),
);
}
pixel_store(LINUX_GL_UNPACK_ROW_LENGTH, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_ROWS, 0);
pixel_store(LINUX_GL_UNPACK_SKIP_PIXELS, 0);
}
}
pub(crate) fn linux_egl_present_submission(
surface: &mut LinuxEglGlesSurface,
wl_surface: usize,
submission: &GpuFrameSubmission,
) -> Result<(usize, usize), PlatformError> {
let syms: &LinuxEglGlesSyms = linux_egl_gles_syms();
if !syms.get_ready()
|| surface.get_egl_display() == 0
|| surface.get_egl_surface() == 0
|| surface.get_egl_context() == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
let vw: u32 = submission.get_width().max(1);
let vh: u32 = submission.get_height().max(1);
unsafe {
if surface.get_is_wayland()
&& surface.get_wl_egl_window() != 0
&& (vw != surface.get_width() || vh != surface.get_height())
&& syms.get_wl_egl_window_resize() != 0
{
let resize_wl_egl: unsafe extern "C" fn(*mut c_void, i32, i32, i32, i32) =
transmute(syms.get_wl_egl_window_resize() as *const c_void);
resize_wl_egl(
surface.get_wl_egl_window() as *mut c_void,
vw as i32,
vh as i32,
0,
0,
);
surface.set_width(vw);
surface.set_height(vh);
}
let egl_make_current: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*mut c_void,
) -> u32 = transmute(syms.get_egl_make_current() as *const c_void);
if egl_make_current(
surface.get_egl_display() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
surface.get_egl_context() as *mut c_void,
) == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
let active_tex: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_active_texture() as *const c_void);
let bind_tex: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_texture() as *const c_void);
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
if let Some(glyph_atlas) = submission.glyph_atlas_r8_ref()
&& glyph_atlas.get_width() > 0
&& glyph_atlas.get_height() > 0
{
let gw: u32 = glyph_atlas.get_width();
let gh: u32 = glyph_atlas.get_height();
let pixels: &[u8] = &glyph_atlas.get_pixels()[..];
if pixels.len() >= (gw as usize).saturating_mul(gh as usize) {
active_tex(LINUX_GL_TEXTURE0);
if gw == surface.get_glyph_r8_w() && gh == surface.get_glyph_r8_h() {
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_r8_tex());
linux_gles_upload_atlas_dirty_rects(
syms,
LINUX_GL_RED,
1,
gw,
gh,
pixels,
submission.get_glyph_dirty_rects().as_slice(),
);
} else {
let old_tex: u32 = surface.get_glyph_r8_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_texture(
syms,
LINUX_GL_R8,
LINUX_GL_RED,
gw,
gh,
pixels,
1,
);
surface.set_glyph_r8_tex(new_tex);
surface.set_glyph_r8_w(gw);
surface.set_glyph_r8_h(gh);
}
surface.set_glyph_r8_rev(glyph_atlas.get_revision());
}
}
if let Some(img_atlas) = submission.image_atlas_rgba8_ref()
&& img_atlas.get_width() > 0
&& img_atlas.get_height() > 0
{
let iw: u32 = img_atlas.get_width();
let ih: u32 = img_atlas.get_height();
let pixels: &[u8] = &img_atlas.get_pixels()[..];
if pixels.len() >= (iw as usize).saturating_mul(ih as usize).saturating_mul(4) {
active_tex(LINUX_GL_TEXTURE2);
if iw == surface.get_image_w() && ih == surface.get_image_h() {
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_image_tex());
linux_gles_upload_atlas_dirty_rects(
syms,
LINUX_GL_RGBA,
4,
iw,
ih,
pixels,
submission.get_image_dirty_rects().as_slice(),
);
} else {
let old_tex: u32 = surface.get_image_tex();
if old_tex != 0 {
del_tex(1, &old_tex);
}
let new_tex: u32 = linux_gles_create_texture(
syms,
LINUX_GL_RGBA8,
LINUX_GL_RGBA,
iw,
ih,
pixels,
4,
);
surface.set_image_tex(new_tex);
surface.set_image_w(iw);
surface.set_image_h(ih);
}
surface.set_image_rev(img_atlas.get_revision());
}
}
let active_external_tex: u32 = if let Some(pass) = submission.gpu_scene_pass_ref() {
linux_gles_execute_scene_pass(syms, surface, pass)
} else {
surface.get_external_tex()
};
let gl_viewport: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_viewport() as *const c_void);
let gl_disable: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_disable() as *const c_void);
let gl_enable: unsafe extern "C" fn(u32) = transmute(syms.get_gl_enable() as *const c_void);
let gl_clear_color: unsafe extern "C" fn(f32, f32, f32, f32) =
transmute(syms.get_gl_clear_color() as *const c_void);
let gl_clear: unsafe extern "C" fn(u32) = transmute(syms.get_gl_clear() as *const c_void);
let (total_prims, total_ranges, frame_has_blur): (usize, usize, bool) = {
let LinuxEglGlesSurface {
scratch_prims,
scratch_ranges,
..
} = surface;
scratch_prims.clear();
scratch_ranges.clear();
let mut has_any_blur: bool = false;
for batch in submission.get_batches().iter() {
let prims: &[GpuPrimitiveInstance] = batch.get_primitives().as_slice();
if prims.is_empty() {
continue;
}
let has_blur: bool = prims.iter().any(|prim_item: &GpuPrimitiveInstance| {
prim_item.prim_kind() == PrimKind::BackdropBlur
});
has_any_blur |= has_blur;
let base_inst: usize = scratch_prims.len();
scratch_prims.extend_from_slice(prims);
scratch_ranges.push((
base_inst,
prims.len(),
batch.get_scissor(),
batch.get_scroll_offset(),
has_blur,
));
}
(scratch_prims.len(), scratch_ranges.len(), has_any_blur)
};
let use_blur_fbo: bool = if frame_has_blur {
let LinuxEglGlesSurface { blur, .. } = surface;
linux_gles_ensure_blur_pipeline(syms, blur, vw, vh)
} else {
false
};
let target_fbo: u32 = if use_blur_fbo {
let LinuxEglGlesSurface { blur, .. } = surface;
blur.as_ref().map_or(0, LinuxEglBlurPipeline::get_scene_fbo)
} else {
0
};
if syms.get_gl_bind_framebuffer() != 0 {
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
bind_fbo(LINUX_GL_FRAMEBUFFER, target_fbo);
}
gl_viewport(0, 0, vw as i32, vh as i32);
gl_disable(LINUX_GL_SCISSOR_TEST);
gl_disable(LINUX_GL_DEPTH_TEST);
let clear: [f32; 4] = submission.get_clear_color().to_f32_array();
gl_clear_color(clear[0], clear[1], clear[2], clear[3]);
gl_clear(LINUX_GL_COLOR_BUFFER_BIT);
let byte_len: usize = total_prims.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
if byte_len > 0 {
let bind_buf: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_buffer() as *const c_void);
let buf_data: unsafe extern "C" fn(u32, isize, *const c_void, u32) =
transmute(syms.get_gl_buffer_data() as *const c_void);
let buf_sub: unsafe extern "C" fn(u32, isize, isize, *const c_void) =
transmute(syms.get_gl_buffer_sub_data() as *const c_void);
bind_buf(LINUX_GL_ARRAY_BUFFER, surface.get_instance_vbo());
if byte_len > surface.get_instance_vbo_cap() {
let alloc: usize = byte_len.max(4096).next_power_of_two();
buf_data(
LINUX_GL_ARRAY_BUFFER,
alloc as isize,
null(),
LINUX_GL_STREAM_DRAW,
);
surface.set_instance_vbo_cap(alloc);
}
let prims_ptr: *const c_void = {
let LinuxEglGlesSurface { scratch_prims, .. } = surface;
scratch_prims.as_ptr().cast::<c_void>()
};
buf_sub(LINUX_GL_ARRAY_BUFFER, 0, byte_len as isize, prims_ptr);
let blend_sep: unsafe extern "C" fn(u32, u32, u32, u32) =
transmute(syms.get_gl_blend_func_separate() as *const c_void);
let use_program: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_use_program() as *const c_void);
let uniform2f: unsafe extern "C" fn(i32, f32, f32) =
transmute(syms.get_gl_uniform2f() as *const c_void);
let bind_vao: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_bind_vertex_array() as *const c_void);
let gl_scissor: unsafe extern "C" fn(i32, i32, i32, i32) =
transmute(syms.get_gl_scissor() as *const c_void);
let draw_instanced: unsafe extern "C" fn(u32, i32, i32, i32) =
transmute(syms.get_gl_draw_arrays_instanced() as *const c_void);
let ui_prog: u32 = surface.get_program();
let ui_vao: u32 = surface.get_vao();
let viewport_loc: i32 = surface.get_viewport_loc();
let scroll_loc: i32 = surface.get_scroll_loc();
let inst_vbo: u32 = surface.get_instance_vbo();
let glyph_r8_tex: u32 = surface.get_glyph_r8_tex();
gl_enable(LINUX_GL_BLEND);
blend_sep(
LINUX_GL_SRC_ALPHA,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
LINUX_GL_ONE,
LINUX_GL_ONE_MINUS_SRC_ALPHA,
);
gl_enable(LINUX_GL_SCISSOR_TEST);
use_program(ui_prog);
if viewport_loc >= 0 {
uniform2f(viewport_loc, vw as f32, vh as f32);
}
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, glyph_r8_tex);
active_tex(LINUX_GL_TEXTURE1);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_glyph_rgba8_tex());
active_tex(LINUX_GL_TEXTURE2);
bind_tex(LINUX_GL_TEXTURE_2D, surface.get_image_tex());
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, active_external_tex);
bind_vao(ui_vao);
let LinuxEglGlesSurface {
blur,
scratch_prims,
scratch_ranges,
..
} = surface;
for &(base_inst, count, scissor, scroll, has_blur) in scratch_ranges.iter() {
if count == 0 || base_inst >= total_prims {
continue;
}
if scissor.get_width() <= 0.0 || scissor.get_height() <= 0.0 {
continue;
}
let sx0: u32 = scissor.get_x().max(0.0).floor() as u32;
let sy0: u32 = scissor.get_y().max(0.0).floor() as u32;
let sx1: u32 = scissor.right().max(0.0).ceil() as u32;
let sy1: u32 = scissor.bottom().max(0.0).ceil() as u32;
if sx1 <= sx0 || sy1 <= sy0 {
continue;
}
let draw_count: usize = count.min(total_prims - base_inst);
let did_blur: bool = if has_blur
&& use_blur_fbo
&& let Some(blur_pipe) = blur.as_ref()
&& let Some(blur_prim) = scratch_prims
[base_inst..base_inst.saturating_add(draw_count)]
.iter()
.find(|prim_item: &&GpuPrimitiveInstance| {
prim_item.prim_kind() == PrimKind::BackdropBlur
}) {
linux_gles_run_blur_pass(syms, blur_pipe, blur_prim, ui_vao, vw, vh);
let bind_fbo: unsafe extern "C" fn(u32, u32) =
transmute(syms.get_gl_bind_framebuffer() as *const c_void);
bind_fbo(LINUX_GL_FRAMEBUFFER, blur_pipe.get_scene_fbo());
gl_viewport(0, 0, vw as i32, vh as i32);
gl_enable(LINUX_GL_BLEND);
gl_enable(LINUX_GL_SCISSOR_TEST);
active_tex(LINUX_GL_TEXTURE0);
bind_tex(LINUX_GL_TEXTURE_2D, glyph_r8_tex);
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, blur_pipe.get_full_tex());
use_program(ui_prog);
if viewport_loc >= 0 {
uniform2f(viewport_loc, vw as f32, vh as f32);
}
bind_vao(ui_vao);
true
} else {
false
};
let sc: [i32; 4] = gles3_scissor_from_top_down(vh, sx0, sy0, sx1 - sx0, sy1 - sy0);
if sc[2] <= 0 || sc[3] <= 0 {
if did_blur {
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, active_external_tex);
}
continue;
}
gl_scissor(sc[0], sc[1], sc[2], sc[3]);
if scroll_loc >= 0 {
uniform2f(scroll_loc, scroll[0], scroll[1]);
}
let base_bytes: usize = base_inst.saturating_mul(GPU_PRIMITIVE_INSTANCE_BYTES);
linux_gles_bind_instance_attribs(syms, inst_vbo, base_bytes);
draw_instanced(LINUX_GL_TRIANGLES, 0, 6, draw_count as i32);
if did_blur {
active_tex(LINUX_GL_TEXTURE3);
bind_tex(LINUX_GL_TEXTURE_2D, active_external_tex);
}
}
gl_disable(LINUX_GL_SCISSOR_TEST);
}
if use_blur_fbo {
let ui_vao: u32 = surface.get_vao();
let LinuxEglGlesSurface { blur, .. } = surface;
if let Some(blur_pipe) = blur.as_ref() {
linux_gles_copy_blur_scene_to_default(syms, blur_pipe, ui_vao, vw, vh);
}
}
if surface.get_is_wayland() && wl_surface != 0 {
let LinuxEglGlesSurface { vsync_pending, .. } = surface;
let _: bool = linux_wl_arm_frame_callback(wl_surface, vsync_pending.as_ref());
}
let swap_buffers: unsafe extern "C" fn(*mut c_void, *mut c_void) -> u32 =
transmute(syms.get_egl_swap_buffers() as *const c_void);
if swap_buffers(
surface.get_egl_display() as *mut c_void,
surface.get_egl_surface() as *mut c_void,
) == 0
{
return Err(PlatformError::SurfaceError(
ERR_LINUX_EGL_UNAVAILABLE.to_string(),
));
}
Ok((total_ranges.max(1), total_prims))
}
}
pub(crate) fn linux_egl_destroy_surface(surface: &mut LinuxEglGlesSurface) {
let syms: &LinuxEglGlesSyms = linux_egl_gles_syms();
let dpy: *mut c_void = surface.get_egl_display() as *mut c_void;
let ctx: *mut c_void = surface.get_egl_context() as *mut c_void;
let surf: *mut c_void = surface.get_egl_surface() as *mut c_void;
unsafe {
if !dpy.is_null() && !ctx.is_null() && !surf.is_null() && syms.get_egl_make_current() != 0 {
let make_current: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
*mut c_void,
*mut c_void,
) -> u32 = transmute(syms.get_egl_make_current() as *const c_void);
if make_current(dpy, surf, surf, ctx) != 0 {
let taken_scene: Option<LinuxEglScenePipeline> = {
let LinuxEglGlesSurface { scene, .. } = surface;
scene.take()
};
if let Some(scene_pipe) = taken_scene.as_ref() {
linux_gles_destroy_scene_pipeline(syms, scene_pipe);
}
let taken_blur: Option<LinuxEglBlurPipeline> = {
let LinuxEglGlesSurface { blur, .. } = surface;
blur.take()
};
if let Some(blur_pipe) = taken_blur.as_ref() {
linux_gles_destroy_blur_pipeline(syms, blur_pipe);
}
if syms.get_gl_delete_textures() != 0 {
let del_tex: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_textures() as *const c_void);
let texs: [u32; 4] = [
surface.get_glyph_r8_tex(),
surface.get_glyph_rgba8_tex(),
surface.get_image_tex(),
surface.get_external_tex(),
];
del_tex(4, texs.as_ptr());
}
if syms.get_gl_delete_buffers() != 0 {
let del_buf: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_buffers() as *const c_void);
let bufs: [u32; 2] = [surface.get_quad_vbo(), surface.get_instance_vbo()];
del_buf(2, bufs.as_ptr());
}
if syms.get_gl_delete_vertex_arrays() != 0 && surface.get_vao() != 0 {
let del_vao: unsafe extern "C" fn(i32, *const u32) =
transmute(syms.get_gl_delete_vertex_arrays() as *const c_void);
let vao: u32 = surface.get_vao();
del_vao(1, &vao);
}
if syms.get_gl_delete_program() != 0 && surface.get_program() != 0 {
let del_prog: unsafe extern "C" fn(u32) =
transmute(syms.get_gl_delete_program() as *const c_void);
del_prog(surface.get_program());
}
}
let _: u32 = make_current(dpy, null_mut(), null_mut(), null_mut());
}
if !dpy.is_null() && !surf.is_null() && syms.get_egl_destroy_surface() != 0 {
let del_surf: unsafe extern "C" fn(*mut c_void, *mut c_void) -> u32 =
transmute(syms.get_egl_destroy_surface() as *const c_void);
let _: u32 = del_surf(dpy, surf);
}
if !dpy.is_null() && !ctx.is_null() && syms.get_egl_destroy_context() != 0 {
let del_ctx: unsafe extern "C" fn(*mut c_void, *mut c_void) -> u32 =
transmute(syms.get_egl_destroy_context() as *const c_void);
let _: u32 = del_ctx(dpy, ctx);
}
if surface.get_wl_egl_window() != 0 && syms.get_wl_egl_window_destroy() != 0 {
let del_wl_egl: unsafe extern "C" fn(*mut c_void) =
transmute(syms.get_wl_egl_window_destroy() as *const c_void);
del_wl_egl(surface.get_wl_egl_window() as *mut c_void);
}
if !dpy.is_null() && syms.get_egl_terminate() != 0 {
let term: unsafe extern "C" fn(*mut c_void) -> u32 =
transmute(syms.get_egl_terminate() as *const c_void);
let _: u32 = term(dpy);
}
}
}
pub(crate) fn linux_alloc_shm_file(size: usize) -> Result<(i32, usize, usize), PlatformError> {
if size == 0 || size > (i32::MAX as usize) {
return Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SHM_ALLOC.to_string(),
));
}
unsafe {
let mut fd: i32 = -1;
let memfd_sym: *mut c_void = dlsym(null_mut(), SYM_MEMFD_CREATE.as_ptr().cast::<c_char>());
if !memfd_sym.is_null() {
let memfd_create: unsafe extern "C" fn(*const c_char, u32) -> i32 =
transmute(memfd_sym as *const c_void);
fd = memfd_create(WL_MEMFD_NAME.as_ptr().cast::<c_char>(), LINUX_MFD_CLOEXEC);
}
if fd < 0 {
let pid: i32 = getpid();
let seq: u64 = LINUX_SHM_SEQ.fetch_add(1, Ordering::Relaxed);
let path_str: String = format!("{LINUX_DEV_SHM_PREFIX}-{pid}-{seq}");
if let Ok(c_path) = CString::new(path_str) {
fd = open(c_path.as_ptr(), LINUX_SHM_OPEN_FLAGS, LINUX_SHM_FILE_MODE);
if fd >= 0 {
let _: i32 = unlink(c_path.as_ptr());
}
}
}
if fd < 0 {
return Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SHM_ALLOC.to_string(),
));
}
if ftruncate(fd, size as i64) != 0 {
let _: i32 = close(fd);
return Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SHM_ALLOC.to_string(),
));
}
let ptr: *mut c_void = mmap(
null_mut(),
size,
LINUX_MMAP_PROT_RW,
LINUX_MMAP_SHARED,
fd,
0,
);
if ptr.is_null() || (ptr as isize) == -1 {
let _: i32 = close(fd);
return Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SHM_ALLOC.to_string(),
));
}
Ok((fd, ptr as usize, size))
}
}
pub(crate) fn linux_free_shm_file(fd: i32, ptr: usize, len: usize) {
unsafe {
if ptr != 0 && len > 0 {
let _: i32 = munmap(ptr as *mut c_void, len);
}
if fd >= 0 {
let _: i32 = close(fd);
}
}
}
pub(crate) unsafe extern "C" fn linux_wl_registry_global(
data: *mut c_void,
registry: *mut c_void,
name: u32,
interface: *const c_char,
version: u32,
) {
if data.is_null() || registry.is_null() || interface.is_null() {
return;
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.proxy_marshal_flags == 0 {
return;
}
unsafe {
let Ok(iface_str) = CStr::from_ptr(interface).to_str() else {
return;
};
let state: &mut WlRegistryBindState = &mut *data.cast::<WlRegistryBindState>();
let marshal_bind: unsafe extern "C" fn(
*mut c_void,
u32,
*const c_void,
u32,
u32,
u32,
*const c_char,
u32,
*mut c_void,
) -> *mut c_void = transmute(wl.proxy_marshal_flags as *const c_void);
if iface_str == WL_IFACE_COMPOSITOR && state.compositor == 0 && wl.compositor_interface != 0
{
let bind_ver: u32 = version.clamp(1, WL_COMPOSITOR_MAX_VERSION);
let proxy: *mut c_void = marshal_bind(
registry,
WL_OPCODE_REGISTRY_BIND,
wl.compositor_interface as *const c_void,
bind_ver,
0,
name,
interface,
bind_ver,
null_mut(),
);
state.compositor = proxy as usize;
state.compositor_version = bind_ver;
} else if iface_str == WL_IFACE_SHM && state.shm == 0 && wl.shm_interface != 0 {
let bind_ver: u32 = version.clamp(1, WL_SHM_MAX_VERSION);
let proxy: *mut c_void = marshal_bind(
registry,
WL_OPCODE_REGISTRY_BIND,
wl.shm_interface as *const c_void,
bind_ver,
0,
name,
interface,
bind_ver,
null_mut(),
);
state.shm = proxy as usize;
}
}
}
pub(crate) unsafe extern "C" fn linux_wl_registry_global_remove(
_data: *mut c_void,
_registry: *mut c_void,
_name: u32,
) {
}
pub(crate) unsafe extern "C" fn linux_wl_buffer_release(data: *mut c_void, _buffer: *mut c_void) {
if data.is_null() {
return;
}
unsafe {
let busy_flag: &AtomicBool = &*data.cast::<AtomicBool>();
busy_flag.store(false, Ordering::Release);
}
}
fn linux_wayland_bind_globals(wl_display: *mut c_void) -> (usize, usize, u64, u32) {
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl_display.is_null()
|| wl.proxy_marshal_flags == 0
|| wl.proxy_add_listener == 0
|| wl.display_roundtrip == 0
|| wl.registry_interface == 0
{
return (0, 0, 0, 0);
}
unsafe {
let marshal_get_reg: unsafe extern "C" fn(
*mut c_void,
u32,
*const c_void,
u32,
u32,
*mut c_void,
) -> *mut c_void = transmute(wl.proxy_marshal_flags as *const c_void);
let add_listener: unsafe extern "C" fn(*mut c_void, *const c_void, *mut c_void) -> i32 =
transmute(wl.proxy_add_listener as *const c_void);
let roundtrip: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_roundtrip as *const c_void);
let registry: *mut c_void = marshal_get_reg(
wl_display,
WL_OPCODE_DISPLAY_GET_REGISTRY,
wl.registry_interface as *const c_void,
1,
0,
null_mut(),
);
if registry.is_null() {
return (0, 0, 0, 0);
}
let mut bind_state: WlRegistryBindState = WlRegistryBindState::default();
let _: i32 = add_listener(
registry,
(&raw const WL_REGISTRY_LISTENER).cast::<c_void>(),
(&raw mut bind_state).cast::<c_void>(),
);
let _: i32 = roundtrip(wl_display);
if wl.proxy_destroy != 0 {
let proxy_destroy: unsafe extern "C" fn(*mut c_void) =
transmute(wl.proxy_destroy as *const c_void);
proxy_destroy(registry);
}
let mut surface: u64 = 0;
let mut surface_version: u32 = bind_state.compositor_version;
if bind_state.compositor != 0 && wl.surface_interface != 0 {
let marshal_create_surface: unsafe extern "C" fn(
*mut c_void,
u32,
*const c_void,
u32,
u32,
*mut c_void,
) -> *mut c_void = transmute(wl.proxy_marshal_flags as *const c_void);
let wl_surface: *mut c_void = marshal_create_surface(
bind_state.compositor as *mut c_void,
WL_OPCODE_COMPOSITOR_CREATE_SURFACE,
wl.surface_interface as *const c_void,
bind_state.compositor_version.max(1),
0,
null_mut(),
);
surface = wl_surface as u64;
if !wl_surface.is_null() && wl.proxy_get_version != 0 {
let get_ver: unsafe extern "C" fn(*mut c_void) -> u32 =
transmute(wl.proxy_get_version as *const c_void);
surface_version = get_ver(wl_surface);
}
}
(
bind_state.compositor,
bind_state.shm,
surface,
surface_version,
)
}
}
#[must_use]
pub(crate) fn linux_display_refresh_rate_hz(display: *mut c_void, window: usize) -> f32 {
if let Ok(val) = env::var(LINUX_REFRESH_ENV_VAR)
&& let Ok(parsed) = val.trim().parse::<f32>()
&& parsed.is_finite()
&& parsed >= MIN_REFRESH_RATE_HZ
{
return parsed.max(MIN_REFRESH_RATE_HZ);
}
if display.is_null() || window == 0 {
return FALLBACK_REFRESH_RATE_HZ;
}
let syms: &LinuxXRandrSyms = linux_xrandr_syms();
if syms.get_screen_info == 0
|| syms.config_current_rate == 0
|| syms.free_screen_config_info == 0
{
return FALLBACK_REFRESH_RATE_HZ;
}
unsafe {
let get_info: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void =
transmute(syms.get_screen_info as *const c_void);
let cur_rate: unsafe extern "C" fn(*mut c_void) -> i16 =
transmute(syms.config_current_rate as *const c_void);
let free_info: unsafe extern "C" fn(*mut c_void) =
transmute(syms.free_screen_config_info as *const c_void);
let config: *mut c_void = get_info(display, window);
if config.is_null() {
return FALLBACK_REFRESH_RATE_HZ;
}
let rate: i16 = cur_rate(config);
free_info(config);
if rate >= 24 {
(rate as f32).max(MIN_REFRESH_RATE_HZ)
} else {
FALLBACK_REFRESH_RATE_HZ
}
}
}
pub(crate) fn linux_x11_query_scale_factor(display: *mut c_void) -> f32 {
for var_name in LINUX_DPI_ENV_VARS.iter() {
if let Ok(val) = env::var(var_name)
&& let Ok(parsed) = val.trim().parse::<f32>()
&& parsed.is_finite()
&& parsed > 0.0
{
return clamp_viewport_scale(parsed);
}
}
let syms: &LinuxX11Syms = linux_x11_syms();
if syms.resource_manager_string != 0 && !display.is_null() {
unsafe {
let x_rm: unsafe extern "C" fn(*mut c_void) -> *const c_char =
transmute(syms.resource_manager_string as *const c_void);
let raw_ptr: *const c_char = x_rm(display);
if !raw_ptr.is_null()
&& let Ok(text) = CStr::from_ptr(raw_ptr).to_str()
{
for line in text.lines() {
let trimmed: &str = line.trim();
if let Some(rest) = trimmed.strip_prefix(XFT_DPI_PREFIX)
&& let Ok(dpi) = rest.trim().parse::<f32>()
&& dpi.is_finite()
&& dpi > 24.0
{
return clamp_viewport_scale(dpi / X11_DEFAULT_DPI);
}
}
}
}
}
1.0
}
pub(crate) fn linux_wayland_resolve_buffer_scale(scale_factor: f32) -> i32 {
let safe_scale: f32 = clamp_viewport_scale(scale_factor);
safe_scale.ceil().clamp(1.0, VIEWPORT_SCALE_MAX) as i32
}
fn linux_x11_probe_mit_shm(display: *mut c_void) -> bool {
let shm: &LinuxXShmSyms = linux_xshm_syms();
if display.is_null()
|| shm.query_extension == 0
|| shm.create_image == 0
|| shm.attach == 0
|| shm.detach == 0
|| shm.put_image == 0
{
return false;
}
unsafe {
let query_fn: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(shm.query_extension as *const c_void);
query_fn(display) != 0
}
}
#[must_use]
fn linux_should_prefer_wayland() -> bool {
if let Ok(force_x11) = env::var(LINUX_FORCE_X11_ENV)
&& force_x11.trim() == "1"
{
return false;
}
env::var(LINUX_WAYLAND_DISPLAY_ENV)
.map(|val: String| !val.trim().is_empty())
.unwrap_or(false)
}
pub(crate) fn linux_create_native_window(
config: &WindowConfig,
) -> Result<LinuxNativeWindowHandle, PlatformError> {
unsafe {
if linux_should_prefer_wayland() {
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.lib != 0 && wl.display_connect != 0 {
let wl_connect: unsafe extern "C" fn(*const c_char) -> *mut c_void =
transmute(wl.display_connect as *const c_void);
let wl_display: *mut c_void = wl_connect(null());
if !wl_display.is_null() {
let (wl_compositor, wl_shm, wl_surface, wl_surface_version): (
usize,
usize,
u64,
u32,
) = linux_wayland_bind_globals(wl_display);
let scale_factor: f32 = linux_x11_query_scale_factor(null_mut());
return Ok(LinuxNativeWindowHandle {
lib: wl.lib,
is_wayland: true,
display: wl_display as usize,
window: wl_surface,
gc: 0,
visual: 0,
depth: 32,
wm_delete_atom: 0,
scale_factor,
mit_shm_available: false,
wayland_vulkan_wsi: wl.vulkan_wsi_available,
xlib_vulkan_wsi: false,
wl_compositor,
wl_shm,
wl_surface_version,
});
}
}
}
let x11: &LinuxX11Syms = linux_x11_syms();
if x11.lib != 0
&& x11.open_display != 0
&& x11.default_screen != 0
&& x11.root_window != 0
&& x11.create_simple_window != 0
{
let x_open: unsafe extern "C" fn(*const c_char) -> *mut c_void =
transmute(x11.open_display as *const c_void);
let display: *mut c_void = x_open(null());
if !display.is_null() {
let x_screen: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(x11.default_screen as *const c_void);
let x_root: unsafe extern "C" fn(*mut c_void, i32) -> u64 =
transmute(x11.root_window as *const c_void);
let x_create: unsafe extern "C" fn(
*mut c_void,
u64,
i32,
i32,
u32,
u32,
u32,
u64,
u64,
) -> u64 = transmute(x11.create_simple_window as *const c_void);
let screen: i32 = x_screen(display);
let root: u64 = x_root(display, screen);
let scale_factor: f32 = linux_x11_query_scale_factor(display);
let phys_w: u32 = ((config.get_width() as f32) * scale_factor)
.round()
.max(1.0) as u32;
let phys_h: u32 = ((config.get_height() as f32) * scale_factor)
.round()
.max(1.0) as u32;
let (pos_x, pos_y): (i32, i32) = config.position_value().unwrap_or((0, 0));
let win: u64 = x_create(display, root, pos_x, pos_y, phys_w, phys_h, 0, 0, 0);
let gc: usize = if x11.default_gc != 0 {
let x_gc: unsafe extern "C" fn(*mut c_void, i32) -> *mut c_void =
transmute(x11.default_gc as *const c_void);
x_gc(display, screen) as usize
} else {
0
};
let visual: usize = if x11.default_visual != 0 {
let x_vis: unsafe extern "C" fn(*mut c_void, i32) -> *mut c_void =
transmute(x11.default_visual as *const c_void);
x_vis(display, screen) as usize
} else {
0
};
let depth: u32 = if x11.default_depth != 0 {
let x_dep: unsafe extern "C" fn(*mut c_void, i32) -> i32 =
transmute(x11.default_depth as *const c_void);
x_dep(display, screen).max(24) as u32
} else {
24
};
if x11.select_input != 0 {
let x_select: unsafe extern "C" fn(*mut c_void, u64, isize) -> i32 =
transmute(x11.select_input as *const c_void);
let _: i32 = x_select(display, win, X11_EVENT_MASK);
}
let mut wm_delete_atom: u64 = 0;
if x11.intern_atom != 0 && x11.set_wm_protocols != 0 {
let x_intern: unsafe extern "C" fn(*mut c_void, *const c_char, i32) -> u64 =
transmute(x11.intern_atom as *const c_void);
let x_proto: unsafe extern "C" fn(*mut c_void, u64, *mut u64, i32) -> i32 =
transmute(x11.set_wm_protocols as *const c_void);
wm_delete_atom =
x_intern(display, ATOM_WM_DELETE_WINDOW.as_ptr().cast::<c_char>(), 0);
if wm_delete_atom != 0 {
let _: i32 = x_proto(display, win, &mut wm_delete_atom, 1);
}
}
if x11.store_name != 0
&& let Ok(c_title) = CString::new(config.get_title().as_str())
{
let x_store: unsafe extern "C" fn(*mut c_void, u64, *const c_char) -> i32 =
transmute(x11.store_name as *const c_void);
let _: i32 = x_store(display, win, c_title.as_ptr());
}
if x11.flush != 0 {
let x_flush: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(x11.flush as *const c_void);
let _: i32 = x_flush(display);
}
let mit_shm_available: bool = linux_x11_probe_mit_shm(display);
let wl_probe: &LinuxWaylandSyms = linux_wayland_syms();
return Ok(LinuxNativeWindowHandle {
lib: x11.lib,
is_wayland: false,
display: display as usize,
window: win,
gc,
visual,
depth,
wm_delete_atom,
scale_factor,
mit_shm_available,
wayland_vulkan_wsi: false,
xlib_vulkan_wsi: wl_probe.xlib_vulkan_wsi_available,
wl_compositor: 0,
wl_shm: 0,
wl_surface_version: 0,
});
}
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.lib != 0 && wl.display_connect != 0 {
let wl_connect: unsafe extern "C" fn(*const c_char) -> *mut c_void =
transmute(wl.display_connect as *const c_void);
let wl_display: *mut c_void = wl_connect(null());
if !wl_display.is_null() {
let (wl_compositor, wl_shm, wl_surface, wl_surface_version): (
usize,
usize,
u64,
u32,
) = linux_wayland_bind_globals(wl_display);
return Ok(LinuxNativeWindowHandle {
lib: wl.lib,
is_wayland: true,
display: wl_display as usize,
window: wl_surface,
gc: 0,
visual: 0,
depth: 32,
wm_delete_atom: 0,
scale_factor: linux_x11_query_scale_factor(null_mut()),
mit_shm_available: false,
wayland_vulkan_wsi: wl.vulkan_wsi_available,
xlib_vulkan_wsi: false,
wl_compositor,
wl_shm,
wl_surface_version,
});
}
}
}
Err(PlatformError::InitializationFailed(
ERR_LINUX_NO_DISPLAY.to_string(),
))
}
pub(crate) fn linux_x11_map_window(display: usize, window: u64) {
if display == 0 || window == 0 {
return;
}
let syms: &LinuxX11Syms = linux_x11_syms();
if syms.map_window == 0 {
return;
}
unsafe {
let x_map: unsafe extern "C" fn(*mut c_void, u64) -> i32 =
transmute(syms.map_window as *const c_void);
let _: i32 = x_map(display as *mut c_void, window);
if syms.flush != 0 {
let x_flush: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.flush as *const c_void);
let _: i32 = x_flush(display as *mut c_void);
}
}
}
fn linux_pack_bgra_subrect(
framebuffer: &Framebuffer,
x0: u32,
y0: u32,
width: u32,
height: u32,
) -> Vec<u8> {
let fb_w: usize = framebuffer.get_width() as usize;
let src: &[u8] = framebuffer.get_pixels().as_slice();
let mut bgra: Vec<u8> = Vec::with_capacity((width as usize) * (height as usize) * 4);
for row in y0..(y0 + height) {
let row_start: usize = ((row as usize) * fb_w + (x0 as usize)) * 4;
let row_end: usize = row_start + (width as usize) * 4;
if row_end <= src.len() {
for px in src[row_start..row_end].as_chunks::<4>().0 {
bgra.push(px[2]);
bgra.push(px[1]);
bgra.push(px[0]);
bgra.push(px[3]);
}
}
}
bgra
}
#[must_use]
pub(crate) fn linux_normalize_damage_rects(
framebuffer: &Framebuffer,
damage: DamageRegion,
) -> Vec<(u32, u32, u32, u32)> {
let fb_w: u32 = framebuffer.get_width();
let fb_h: u32 = framebuffer.get_height();
if fb_w == 0 || fb_h == 0 {
return Vec::new();
}
if damage.get_full_damage() {
return vec![(0, 0, fb_w, fb_h)];
}
let DamageRegion {
rects: raw_rects,
rect_count,
..
} = damage;
let count: usize = (rect_count as usize).min(raw_rects.len());
let mut rects: Vec<(u32, u32, u32, u32)> = Vec::new();
for rect in raw_rects[..count].iter() {
let x0: u32 = (rect.get_x().floor().max(0.0) as u32).min(fb_w);
let y0: u32 = (rect.get_y().floor().max(0.0) as u32).min(fb_h);
let x1: u32 = ((rect.get_x() + rect.get_width()).ceil().max(0.0) as u32).min(fb_w);
let y1: u32 = ((rect.get_y() + rect.get_height()).ceil().max(0.0) as u32).min(fb_h);
if x1 > x0 && y1 > y0 {
rects.push((x0, y0, x1 - x0, y1 - y0));
}
}
if rects.is_empty()
&& let Some(bounds) = damage.bounds_rect()
{
let x0: u32 = (bounds.get_x().floor().max(0.0) as u32).min(fb_w);
let y0: u32 = (bounds.get_y().floor().max(0.0) as u32).min(fb_h);
let x1: u32 = ((bounds.get_x() + bounds.get_width()).ceil().max(0.0) as u32).min(fb_w);
let y1: u32 = ((bounds.get_y() + bounds.get_height()).ceil().max(0.0) as u32).min(fb_h);
if x1 > x0 && y1 > y0 {
rects.push((x0, y0, x1 - x0, y1 - y0));
}
}
rects
}
pub(crate) fn linux_copy_rgba_to_wayland_shm(
framebuffer: &Framebuffer,
dst: &mut [u8],
stride: usize,
rects: &[(u32, u32, u32, u32)],
format: u32,
) -> u64 {
let fb_w: usize = framebuffer.get_width() as usize;
let src: &[u8] = framebuffer.get_pixels().as_slice();
let opaque_alpha: bool = format == WL_SHM_FORMAT_XRGB8888;
let mut copied_bytes: u64 = 0;
for &(x0, y0, rw, rh) in rects.iter() {
let row_pixels: usize = rw as usize;
let row_bytes: usize = row_pixels.saturating_mul(4);
for row in y0..(y0 + rh) {
let src_start: usize = ((row as usize) * fb_w + (x0 as usize)) * 4;
let src_end: usize = src_start + row_bytes;
let dst_start: usize = (row as usize) * stride + (x0 as usize) * 4;
let dst_end: usize = dst_start + row_bytes;
if src_end <= src.len() && dst_end <= dst.len() {
let src_row: &[u8] = &src[src_start..src_end];
let dst_row: &mut [u8] = &mut dst[dst_start..dst_end];
for (src_px, dst_px) in src_row
.as_chunks::<4>()
.0
.iter()
.zip(dst_row.as_chunks_mut::<4>().0.iter_mut())
{
dst_px[0] = src_px[2];
dst_px[1] = src_px[1];
dst_px[2] = src_px[0];
dst_px[3] = if opaque_alpha { 0xFF } else { src_px[3] };
}
copied_bytes = copied_bytes.saturating_add(row_bytes as u64);
}
}
}
copied_bytes
}
pub(crate) fn linux_present_window_region(
rec: &mut LinuxWindowRecord,
framebuffer: &Framebuffer,
damage: DamageRegion,
) -> Result<LinuxPresentTelemetry, PlatformError> {
if rec.get_is_wayland() {
let display: usize = rec.get_display();
let surface: usize = rec.get_window() as usize;
let wl_shm: usize = rec.get_wl_shm();
let surface_version: u32 = rec.get_wl_surface_version();
let buffer_scale: i32 =
linux_wayland_resolve_buffer_scale(rec.get_state().get_scale_factor());
let mut transport: NativeWaylandShmTransport =
NativeWaylandShmTransport::new(display, surface, wl_shm, surface_version);
transport.set_buffer_scale(buffer_scale);
let shm_arc: Arc<Mutex<WaylandShmSwapchain>> = rec.wayland_shm_handle();
let result: Result<LinuxPresentTelemetry, PlatformError> = match shm_arc.lock() {
Ok(mut guard) => guard.present_framebuffer(framebuffer, damage, &mut transport),
Err(_) => Err(PlatformError::SurfaceError(
ERR_LINUX_WL_SURFACE_MISSING.to_string(),
)),
};
match result {
Ok(telemetry) => {
rec.set_presentation_mode(LinuxPresentationMode::WaylandShmSoftware);
rec.set_cpu_mirror_active(telemetry.get_cpu_mirror_active());
rec.set_last_telemetry(telemetry);
Ok(telemetry)
}
Err(err) => {
let mut failed: LinuxPresentTelemetry = rec.get_last_telemetry();
failed.set_mode(LinuxPresentationMode::WaylandShmSoftware);
failed.set_outcome(LinuxPresentOutcome::Failed);
failed.set_cpu_mirror_active(true);
failed.set_upload_bytes(0);
failed.set_dirty_rect_count(0);
failed.set_failed_frames(failed.get_failed_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::WaylandShmSoftware);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(failed);
Err(err)
}
}
} else {
linux_x11_present_framebuffer_region(rec, framebuffer, damage)
}
}
fn linux_x11_present_framebuffer_region(
rec: &mut LinuxWindowRecord,
framebuffer: &Framebuffer,
damage: DamageRegion,
) -> Result<LinuxPresentTelemetry, PlatformError> {
let mut telemetry: LinuxPresentTelemetry = rec.get_last_telemetry();
telemetry.set_mode(LinuxPresentationMode::X11Software);
telemetry.set_cpu_mirror_active(true);
let fb_w: u32 = framebuffer.get_width();
let fb_h: u32 = framebuffer.get_height();
let required_bytes: usize = (fb_w as usize)
.saturating_mul(fb_h as usize)
.saturating_mul(4);
if fb_w == 0 || fb_h == 0 || framebuffer.get_pixels().len() < required_bytes {
telemetry.set_outcome(LinuxPresentOutcome::Failed);
telemetry.set_upload_bytes(0);
telemetry.set_dirty_rect_count(0);
telemetry.set_failed_frames(telemetry.get_failed_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
return Err(PlatformError::SurfaceError(
ERR_LINUX_INVALID_FRAMEBUFFER.to_string(),
));
}
let rects: Vec<(u32, u32, u32, u32)> = linux_normalize_damage_rects(framebuffer, damage);
if rects.is_empty() {
telemetry.set_outcome(LinuxPresentOutcome::SkippedUnchanged);
telemetry.set_upload_bytes(0);
telemetry.set_dirty_rect_count(0);
telemetry.set_skipped_unchanged_frames(
telemetry.get_skipped_unchanged_frames().saturating_add(1),
);
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
return Ok(telemetry);
}
let syms: &LinuxX11Syms = linux_x11_syms();
if rec.get_display() == 0
|| rec.get_window() == 0
|| rec.get_gc() == 0
|| rec.get_visual() == 0
|| syms.create_image == 0
|| syms.put_image == 0
{
telemetry.set_outcome(LinuxPresentOutcome::Failed);
telemetry.set_upload_bytes(0);
telemetry.set_dirty_rect_count(0);
telemetry.set_failed_frames(telemetry.get_failed_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
return Err(PlatformError::SurfaceError(
ERR_LINUX_X11_SURFACE_MISSING.to_string(),
));
}
let mut upload_bytes: u64 = 0;
unsafe {
let x_create_img: unsafe extern "C" fn(
*mut c_void,
*mut c_void,
u32,
i32,
i32,
*mut c_char,
u32,
u32,
i32,
i32,
) -> *mut c_void = transmute(syms.create_image as *const c_void);
let x_put_img: unsafe extern "C" fn(
*mut c_void,
u64,
*mut c_void,
*mut c_void,
i32,
i32,
i32,
i32,
u32,
u32,
) -> i32 = transmute(syms.put_image as *const c_void);
let display: *mut c_void = rec.get_display() as *mut c_void;
let visual: *mut c_void = rec.get_visual() as *mut c_void;
let gc: *mut c_void = rec.get_gc() as *mut c_void;
for &(x0, y0, rw, rh) in rects.iter() {
let mut bgra: Vec<u8> = if x0 == 0 && y0 == 0 && rw == fb_w && rh == fb_h {
framebuffer.to_bgra_bytes()
} else {
linux_pack_bgra_subrect(framebuffer, x0, y0, rw, rh)
};
let stride: i32 = (rw as i32) * 4;
let img: *mut c_void = x_create_img(
display,
visual,
rec.get_depth(),
X11_Z_PIXMAP,
0,
bgra.as_mut_ptr().cast::<c_char>(),
rw,
rh,
32,
stride,
);
if img.is_null() {
telemetry.set_outcome(LinuxPresentOutcome::Failed);
telemetry.set_upload_bytes(upload_bytes);
telemetry.set_dirty_rect_count(rects.len() as u32);
telemetry.set_failed_frames(telemetry.get_failed_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
return Err(PlatformError::SurfaceError(
ERR_LINUX_X11_SURFACE_MISSING.to_string(),
));
}
let _: i32 = x_put_img(
display,
rec.get_window(),
gc,
img,
0,
0,
x0 as i32,
y0 as i32,
rw,
rh,
);
upload_bytes = upload_bytes.saturating_add(bgra.len() as u64);
if syms.free != 0 {
let x_free: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.free as *const c_void);
let _: i32 = x_free(img);
}
}
if syms.flush != 0 {
let x_flush: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.flush as *const c_void);
let _: i32 = x_flush(display);
}
}
telemetry.set_outcome(LinuxPresentOutcome::Submitted);
telemetry.set_upload_bytes(upload_bytes);
telemetry.set_dirty_rect_count(rects.len() as u32);
telemetry.set_submitted_frames(telemetry.get_submitted_frames().saturating_add(1));
rec.set_presentation_mode(LinuxPresentationMode::X11Software);
rec.set_cpu_mirror_active(true);
rec.set_last_telemetry(telemetry);
Ok(telemetry)
}
#[must_use]
fn linux_x11_modifiers(state: u32) -> Modifiers {
Modifiers::new(
(state & X11_SHIFT_MASK) != 0,
(state & X11_CTRL_MASK) != 0,
(state & X11_ALT_MASK) != 0,
(state & X11_META_MASK) != 0,
)
}
#[must_use]
fn linux_x11_keysym_to_keycode(keysym: u64, ch: Option<char>) -> KeyCode {
match keysym {
0xFF08 => KeyCode::Backspace,
0xFF09 => KeyCode::Tab,
0xFF0D | 0xFF8D => KeyCode::Enter,
0xFF1B => KeyCode::Escape,
0xFF50 => KeyCode::Home,
0xFF51 => KeyCode::ArrowLeft,
0xFF52 => KeyCode::ArrowUp,
0xFF53 => KeyCode::ArrowRight,
0xFF54 => KeyCode::ArrowDown,
0xFF55 => KeyCode::PageUp,
0xFF56 => KeyCode::PageDown,
0xFF57 => KeyCode::End,
0xFFFF => KeyCode::Delete,
0x0020 => KeyCode::Space,
0x0021..=0x007E => KeyCode::Character((keysym as u8 as char).to_ascii_lowercase()),
_ => match ch {
Some(char_item) if !char_item.is_control() => KeyCode::Character(char_item),
_ => KeyCode::Unknown,
},
}
}
#[must_use]
fn linux_find_window(
xid: u64,
map: &HashMap<WindowId, LinuxWindowRecord>,
) -> Option<(WindowId, f32, u64)> {
for (wid, rec) in map.iter() {
if !rec.get_is_wayland() && rec.get_window() == xid {
return Some((
*wid,
rec.get_state().get_scale_factor(),
rec.get_wm_delete_atom(),
));
}
}
None
}
unsafe fn linux_x11_handle_raw_event(
raw: &X11EventRaw,
lookup_sym: *mut c_void,
windows: &mut HashMap<WindowId, LinuxWindowRecord>,
out_events: &mut Vec<WindowEvent>,
) {
let ev_type: i32 = ((raw.pad[0] as u64) & 0xFFFF_FFFF) as i32;
unsafe {
match ev_type {
X11_CONFIGURE_NOTIFY => {
let cfg_ev: X11ConfigureEvent =
*(raw as *const X11EventRaw).cast::<X11ConfigureEvent>();
if let Some((wid, scale, _)) = linux_find_window(cfg_ev.window, windows) {
let phys_w: u32 = cfg_ev.width.max(1) as u32;
let phys_h: u32 = cfg_ev.height.max(1) as u32;
let exact_lw: f32 = ((phys_w as f32) / scale).max(1.0);
let exact_lh: f32 = ((phys_h as f32) / scale).max(1.0);
let log_w: u32 = exact_lw.round().max(1.0) as u32;
let log_h: u32 = exact_lh.round().max(1.0) as u32;
if let Some(rec) = windows.get_mut(&wid) {
let prev_w: u32 = rec.get_state().get_physical_width();
let prev_h: u32 = rec.get_state().get_physical_height();
if prev_w != phys_w || prev_h != phys_h {
rec.get_mut_config().set_width(log_w);
rec.get_mut_config().set_height(log_h);
rec.get_mut_state().set_logical_width(exact_lw);
rec.get_mut_state().set_logical_height(exact_lh);
rec.get_mut_state().set_physical_width(phys_w);
rec.get_mut_state().set_physical_height(phys_h);
out_events.push(WindowEvent::Resized(wid, log_w, log_h, scale));
}
}
}
}
X11_EXPOSE => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, _, _)) = linux_find_window(kb.window, windows) {
out_events.push(WindowEvent::RedrawRequested(wid));
}
}
X11_CLIENT_MESSAGE => {
let cm: X11ClientMessageEvent =
*(raw as *const X11EventRaw).cast::<X11ClientMessageEvent>();
if let Some((wid, _, wm_del)) = linux_find_window(cm.window, windows)
&& wm_del != 0
&& cm.data[0] == wm_del
{
out_events.push(WindowEvent::CloseRequested(wid));
}
}
X11_FOCUS_IN | X11_FOCUS_OUT => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, _, _)) = linux_find_window(kb.window, windows) {
let focused: bool = ev_type == X11_FOCUS_IN;
if let Some(rec) = windows.get_mut(&wid) {
rec.get_mut_state().set_focused(focused);
}
if !focused {
out_events.push(WindowEvent::PointerCancel(wid, 0));
}
out_events.push(WindowEvent::Focused(wid, focused));
let lc_ev: SystemEvent = if focused {
SystemEvent::Foreground
} else {
SystemEvent::Inactive
};
out_events.push(WindowEvent::System(lc_ev));
}
}
X11_UNMAP_NOTIFY => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, _, _)) = linux_find_window(kb.window, windows) {
out_events.push(WindowEvent::PointerCancel(wid, 0));
out_events.push(WindowEvent::System(SystemEvent::Background));
}
}
X11_MAP_NOTIFY => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if linux_find_window(kb.window, windows).is_some() {
out_events.push(WindowEvent::System(SystemEvent::Foreground));
}
}
X11_DESTROY_NOTIFY => {
let kb: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if linux_find_window(kb.window, windows).is_some() {
out_events.push(WindowEvent::System(SystemEvent::Terminate));
}
}
X11_MOTION_NOTIFY => {
let mot: X11MotionEvent = *(raw as *const X11EventRaw).cast::<X11MotionEvent>();
if let Some((wid, scale, _)) = linux_find_window(mot.window, windows) {
let pos: Point = Point::new((mot.x as f32) / scale, (mot.y as f32) / scale);
let pressed_btn: f32 = if (mot.state & 0x0100) != 0 { 1.0 } else { 0.0 };
let point_coords: PointerEvent = PointerEvent::new(
1,
PointerDevice::Mouse,
pos,
PointerButton::Primary,
pressed_btn,
linux_x11_modifiers(mot.state),
);
out_events.push(WindowEvent::PointerMove(wid, point_coords));
}
}
X11_BUTTON_PRESS | X11_BUTTON_RELEASE => {
let button_node: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, scale, _)) = linux_find_window(button_node.window, windows) {
if ev_type == X11_BUTTON_PRESS
&& (button_node.detail == 4 || button_node.detail == 5)
{
let step: f32 = if button_node.detail == 4 { -52.0 } else { 52.0 };
let delta: Point = if (button_node.state & X11_SHIFT_MASK) != 0 {
Point::new(step, 0.0)
} else {
Point::new(0.0, step)
};
out_events.push(WindowEvent::Scroll(wid, delta));
} else if ev_type == X11_BUTTON_PRESS
&& (button_node.detail == 6 || button_node.detail == 7)
{
let dx: f32 = if button_node.detail == 6 { -52.0 } else { 52.0 };
out_events.push(WindowEvent::Scroll(wid, Point::new(dx, 0.0)));
} else if button_node.detail <= 3 {
let button: PointerButton = match button_node.detail {
2 => PointerButton::Auxiliary,
3 => PointerButton::Secondary,
_ => PointerButton::Primary,
};
let pos: Point = Point::new(
(button_node.x as f32) / scale,
(button_node.y as f32) / scale,
);
let pressed: bool = ev_type == X11_BUTTON_PRESS;
let pev: PointerEvent = PointerEvent::new(
1,
PointerDevice::Mouse,
pos,
button,
if pressed { 1.0 } else { 0.0 },
linux_x11_modifiers(button_node.state),
);
if pressed {
out_events.push(WindowEvent::PointerDown(wid, pev));
} else {
out_events.push(WindowEvent::PointerUp(wid, pev));
}
}
}
}
X11_KEY_PRESS | X11_KEY_RELEASE => {
let mut key_ev: X11KeyOrButtonEvent =
*(raw as *const X11EventRaw).cast::<X11KeyOrButtonEvent>();
if let Some((wid, _, _)) = linux_find_window(key_ev.window, windows) {
let mut buf: [u8; 16] = [0; 16];
let mut keysym: u64 = 0;
let mut len: i32 = 0;
if !lookup_sym.is_null() {
let lookup_fn: unsafe extern "C" fn(
*mut X11KeyOrButtonEvent,
*mut c_char,
i32,
*mut u64,
*mut c_void,
) -> i32 = transmute(lookup_sym);
len = lookup_fn(
&mut key_ev,
buf.as_mut_ptr().cast::<c_char>(),
16,
&mut keysym,
null_mut(),
);
}
let decoded_str: Option<&str> = if len > 0 {
from_utf8(&buf[..(len as usize).min(16)]).ok()
} else {
None
};
let first_ch: Option<char> =
decoded_str.and_then(|text_value: &str| text_value.chars().next());
let code: KeyCode = linux_x11_keysym_to_keycode(keysym, first_ch);
let mut mods: Modifiers = linux_x11_modifiers(key_ev.state);
let is_press: bool = ev_type == X11_KEY_PRESS;
match keysym {
0xFFE1 | 0xFFE2 => {
let _: &mut Modifiers = mods.set_shift(is_press);
}
0xFFE3 | 0xFFE4 => {
let _: &mut Modifiers = mods.set_ctrl(is_press);
}
0xFFE9 | 0xFFEA => {
let _: &mut Modifiers = mods.set_alt(is_press);
}
0xFFEB | 0xFFEC => {
let _: &mut Modifiers = mods.set_meta(is_press);
}
_ => {}
}
let kev: KeyEvent = KeyEvent::new(code, mods, false);
if ev_type == X11_KEY_PRESS {
out_events.push(WindowEvent::KeyDown(wid, kev));
if !mods.get_ctrl()
&& !mods.get_meta()
&& let Some(text) = decoded_str
&& text
.chars()
.all(|char_value: char| !char_value.is_control())
&& !text.is_empty()
{
out_events.push(WindowEvent::TextInput(wid, text.to_string()));
}
} else {
out_events.push(WindowEvent::KeyUp(wid, kev));
}
}
}
_ => {}
}
}
}
pub(crate) fn linux_x11_pump_events(
windows: &mut HashMap<WindowId, LinuxWindowRecord>,
out_events: &mut Vec<WindowEvent>,
) {
if windows.is_empty() {
return;
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.display_dispatch_pending != 0 {
let mut wl_displays: Vec<usize> = Vec::new();
for rec in windows.values() {
if rec.get_is_wayland()
&& rec.get_display() != 0
&& !wl_displays.contains(&rec.get_display())
{
wl_displays.push(rec.get_display());
}
}
unsafe {
let wl_dispatch_pending: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_dispatch_pending as *const c_void);
for disp in wl_displays.into_iter() {
let _: i32 = wl_dispatch_pending(disp as *mut c_void);
}
}
}
let syms: &LinuxX11Syms = linux_x11_syms();
if syms.pending == 0 || syms.next_event == 0 {
return;
}
let displays: Vec<usize> = {
let mut seen: Vec<usize> = Vec::new();
for rec in windows.values() {
if !rec.get_is_wayland() && rec.get_display() != 0 && !seen.contains(&rec.get_display())
{
seen.push(rec.get_display());
}
}
seen
};
let lookup_sym: *mut c_void = syms.lookup_string as *mut c_void;
unsafe {
let x_pending: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.pending as *const c_void);
let x_next: unsafe extern "C" fn(*mut c_void, *mut X11EventRaw) -> i32 =
transmute(syms.next_event as *const c_void);
for disp_addr in displays.into_iter() {
let disp: *mut c_void = disp_addr as *mut c_void;
let mut raw: X11EventRaw = X11EventRaw { pad: [0; 24] };
while x_pending(disp) > 0 {
let _: i32 = x_next(disp, &mut raw);
linux_x11_handle_raw_event(&raw, lookup_sym, windows, out_events);
}
}
}
}
pub(crate) fn linux_x11_wait_events(
windows: &mut HashMap<WindowId, LinuxWindowRecord>,
out_events: &mut Vec<WindowEvent>,
wake_read_fd: i32,
) {
linux_x11_wait_events_timeout(windows, out_events, wake_read_fd, -1);
}
pub(crate) fn linux_x11_wait_events_timeout(
windows: &mut HashMap<WindowId, LinuxWindowRecord>,
out_events: &mut Vec<WindowEvent>,
wake_read_fd: i32,
timeout_ms: i32,
) {
if windows.is_empty() {
return;
}
if let Some(wl_disp) = windows
.values()
.find(|rec: &&LinuxWindowRecord| rec.get_is_wayland() && rec.get_display() != 0)
.map(LinuxWindowRecord::get_display)
{
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.display_get_fd != 0
&& wl.display_prepare_read != 0
&& wl.display_read_events != 0
&& wl.display_cancel_read != 0
&& wl.display_dispatch_pending != 0
{
unsafe {
let get_fd: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_get_fd as *const c_void);
let prepare_read: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_prepare_read as *const c_void);
let read_events: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_read_events as *const c_void);
let cancel_read: unsafe extern "C" fn(*mut c_void) =
transmute(wl.display_cancel_read as *const c_void);
let dispatch_pending: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_dispatch_pending as *const c_void);
let dpy_ptr: *mut c_void = wl_disp as *mut c_void;
while prepare_read(dpy_ptr) != 0 {
let _: i32 = dispatch_pending(dpy_ptr);
}
if wl.display_flush != 0 {
let flush_fn: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_flush as *const c_void);
let _: i32 = flush_fn(dpy_ptr);
}
let wl_fd: i32 = get_fd(dpy_ptr);
let mut fds: [LinuxPollFd; 2] = [
LinuxPollFd {
fd: wl_fd,
events: LINUX_POLLIN,
revents: 0,
},
LinuxPollFd {
fd: wake_read_fd,
events: LINUX_POLLIN,
revents: 0,
},
];
let nfds: usize = if wake_read_fd >= 0 { 2 } else { 1 };
let ready: i32 = poll(fds.as_mut_ptr().cast::<c_void>(), nfds, timeout_ms);
if ready > 0 && (fds[0].revents & LINUX_POLLIN) != 0 {
let _: i32 = read_events(dpy_ptr);
let _: i32 = dispatch_pending(dpy_ptr);
} else {
cancel_read(dpy_ptr);
}
if nfds == 2 && (fds[1].revents & LINUX_POLLIN) != 0 {
linux_drain_wake_pipe(wake_read_fd);
}
}
return;
}
if wl.display_dispatch != 0 {
unsafe {
let wl_dispatch: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(wl.display_dispatch as *const c_void);
let _: i32 = wl_dispatch(wl_disp as *mut c_void);
}
}
return;
}
let first_disp: usize = windows
.values()
.find(|rec: &&LinuxWindowRecord| !rec.get_is_wayland() && rec.get_display() != 0)
.map(LinuxWindowRecord::get_display)
.unwrap_or(0);
if first_disp == 0 {
return;
}
let syms: &LinuxX11Syms = linux_x11_syms();
if syms.connection_number != 0 && syms.pending != 0 {
unsafe {
let x_pending: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.pending as *const c_void);
let x_conn_fd: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.connection_number as *const c_void);
let dpy_ptr: *mut c_void = first_disp as *mut c_void;
if x_pending(dpy_ptr) == 0 {
if syms.flush != 0 {
let x_flush: unsafe extern "C" fn(*mut c_void) -> i32 =
transmute(syms.flush as *const c_void);
let _: i32 = x_flush(dpy_ptr);
}
let x_fd: i32 = x_conn_fd(dpy_ptr);
let mut fds: [LinuxPollFd; 2] = [
LinuxPollFd {
fd: x_fd,
events: LINUX_POLLIN,
revents: 0,
},
LinuxPollFd {
fd: wake_read_fd,
events: LINUX_POLLIN,
revents: 0,
},
];
let nfds: usize = if wake_read_fd >= 0 { 2 } else { 1 };
let _: i32 = poll(fds.as_mut_ptr().cast::<c_void>(), nfds, timeout_ms);
if nfds == 2 && (fds[1].revents & LINUX_POLLIN) != 0 {
linux_drain_wake_pipe(wake_read_fd);
}
}
}
linux_x11_pump_events(windows, out_events);
return;
}
if syms.next_event == 0 {
return;
}
let lookup_sym: *mut c_void = syms.lookup_string as *mut c_void;
unsafe {
let x_next: unsafe extern "C" fn(*mut c_void, *mut X11EventRaw) -> i32 =
transmute(syms.next_event as *const c_void);
let mut raw: X11EventRaw = X11EventRaw { pad: [0; 24] };
let _: i32 = x_next(first_disp as *mut c_void, &mut raw);
linux_x11_handle_raw_event(&raw, lookup_sym, windows, out_events);
}
linux_x11_pump_events(windows, out_events);
}
pub(crate) fn linux_wayland_disconnect(display: usize) {
if display == 0 {
return;
}
let wl: &LinuxWaylandSyms = linux_wayland_syms();
if wl.display_disconnect == 0 {
return;
}
unsafe {
let disconnect: unsafe extern "C" fn(*mut c_void) =
transmute(wl.display_disconnect as *const c_void);
disconnect(display as *mut c_void);
}
}
pub(crate) fn linux_x11_set_title(display: usize, window: u64, title: &str) {
let syms: &LinuxX11Syms = linux_x11_syms();
if display == 0 || window == 0 || syms.store_name == 0 {
return;
}
if let Ok(c_title) = CString::new(title) {
unsafe {
let func: unsafe extern "C" fn(*mut c_void, u64, *const c_char) -> i32 =
transmute(syms.store_name as *const c_void);
let _: i32 = func(display as *mut c_void, window, c_title.as_ptr());
}
}
}
pub(crate) fn linux_x11_resize(display: usize, window: u64, width: u32, height: u32) {
let syms: &LinuxX11Syms = linux_x11_syms();
if display == 0 || window == 0 || syms.resize_window == 0 {
return;
}
unsafe {
let func: unsafe extern "C" fn(*mut c_void, u64, u32, u32) -> i32 =
transmute(syms.resize_window as *const c_void);
let _: i32 = func(display as *mut c_void, window, width.max(1), height.max(1));
}
}
pub(crate) fn linux_x11_destroy(display: usize, window: u64) {
let syms: &LinuxX11Syms = linux_x11_syms();
if display == 0 || window == 0 || syms.destroy_window == 0 {
return;
}
unsafe {
let func: unsafe extern "C" fn(*mut c_void, u64) -> i32 =
transmute(syms.destroy_window as *const c_void);
let _: i32 = func(display as *mut c_void, window);
}
}