kovidgoyal / kitty

Cross-platform, fast, feature-rich, GPU based terminal
https://sw.kovidgoyal.net/kitty/
GNU General Public License v3.0
24.12k stars 972 forks source link

Visual selection lost in aarch64 linux, vmware guest, mesa svga driver #5395

Closed sandangel closed 2 years ago

sandangel commented 2 years ago

Describe the bug The selection works (copy on select), but there is no visual effect.

To Reproduce Steps to reproduce the behavior:

  1. create VM from vmware fusion tech preview on M1 mac.
  2. make sure mesa 22.1.4 is installed with svga driver and vmwgfx kernel module is loaded.
  3. install kitty, try selecting any text

Screenshots If applicable, add screenshots to help explain your problem.

Screen Recording 2022-08-20 at 13.58.28.mov

Environment details

kitty 0.25.2 created by Kovid Goyal
Linux nixos 5.19.1 #1-NixOS SMP Thu Aug 11 11:22:05 UTC 2022 aarch64

<<< Welcome to NixOS 22.11.20220819.c3dec21 (aarch64) - /dev/tty >>>

DISTRIB_CODENAME=raccoon
DISTRIB_DESCRIPTION="NixOS 22.11 (Raccoon)"
DISTRIB_ID=nixos
DISTRIB_RELEASE="22.11"
LSB_VERSION="22.11 (Raccoon)"
Running under: Wayland
Frozen: False
Paths:
  kitty: /nix/store/b91cjk6isrdncid5b4h9qk7w41ypgc59-kitty-0.25.2/bin/kitty
  base dir: /nix/store/b91cjk6isrdncid5b4h9qk7w41ypgc59-kitty-0.25.2/lib/kitty
  extensions dir: /nix/store/b91cjk6isrdncid5b4h9qk7w41ypgc59-kitty-0.25.2/lib/kitty/kitty
  system shell: /run/current-system/sw/bin/zsh
Loaded config files:
  /home/sand/.config/kitty/kitty.conf

Config options different from defaults:
active_tab_font_style              (False, False)
adjust_line_height                 1.1
allow_remote_control               y
bold_font                          Comic Code Ligatures Medium
bold_italic_font                   Comic Code Ligatures Medium Italic
confirm_os_window_close            2
copy_on_select                     clipboard
cursor_blink_interval              0.0
cursor_shape                       3
cursor_underline_thickness         4.0
default_pointer_shape              arrow
enable_audio_bell                  False
enabled_layouts                    ['fat:bias=70', 'stack', 'splits']
font_family                        Comic Code Ligatures Regular
hide_window_decorations            1
italic_font                        Comic Code Ligatures Italic
listen_on                          unix:@mykitty
macos_option_as_alt                3
macos_quit_when_last_window_closed True
macos_thicken_font                 0.5
pointer_shape_when_grabbed         hand
scrollback_fill_enlarged_window    True
scrollback_lines                   90000
shell_integration                  frozenset({'no-rc', 'no-cursor'})
strip_trailing_spaces              smart
symbol_map:
        U+23fb - U+23fe → ProFontIIx Nerd Font Mono
        U+2665 - U+2665 → ProFontIIx Nerd Font Mono
        U+26a1 - U+26a1 → ProFontIIx Nerd Font Mono
        U+2b58 - U+2b58 → ProFontIIx Nerd Font Mono
        U+e000 - U+e00a → ProFontIIx Nerd Font Mono
        U+e0a0 - U+e0a3 → ProFontIIx Nerd Font Mono
        U+e0b0 - U+e0c8 → ProFontIIx Nerd Font Mono
        U+e0ca - U+e0ca → ProFontIIx Nerd Font Mono
        U+e0cc - U+e0d2 → ProFontIIx Nerd Font Mono
        U+e0d4 - U+e0d4 → ProFontIIx Nerd Font Mono
        U+e200 - U+e2a9 → ProFontIIx Nerd Font Mono
        U+e300 - U+e3e3 → ProFontIIx Nerd Font Mono
        U+e5fa - U+e62f → ProFontIIx Nerd Font Mono
        U+e700 - U+e7c5 → ProFontIIx Nerd Font Mono
        U+f000 - U+f2e0 → ProFontIIx Nerd Font Mono
        U+f300 - U+f31c → ProFontIIx Nerd Font Mono
        U+f400 - U+f4a9 → ProFontIIx Nerd Font Mono
        U+f500 - U+f8ff → ProFontIIx Nerd Font Mono
sync_to_monitor                    False
tab_bar_min_tabs                   1
tab_bar_style                      powerline
tab_separator                         |
tab_title_template                 {fmt.fg.red}{bell_symbol}{activity_symbol}{fmt.fg.tab}{index}: {layout_name} - {num_windows}
touch_scroll_multiplier            3.0
update_check_interval              24.0
window_border_width                (0.3, 'pt')
window_padding_width               FloatEdges(left=5.0, top=0, right=5.0, bottom=0)
Added mouse actions:
        alt+left press ungrabbed → mouse_selection rectangle
        ctrl+left click ungrabbed → mouse_handle_click selection link prompt
        ctrl+left doublepress ungrabbed → mouse_selection line_from_point
Removed mouse actions:
        left click ungrabbed → mouse_handle_click selection link prompt
        shift+left click ungrabbed → mouse_handle_click selection link prompt
        shift+left click grabbed → mouse_handle_click selection link prompt
        shift+left press grabbed → mouse_selection normal
        shift+left doublepress ungrabbed → mouse_selection word
        shift+left doublepress grabbed → mouse_selection word
        shift+left triplepress ungrabbed → mouse_selection line
        shift+left triplepress grabbed → mouse_selection line
        ctrl+shift+left release ungrabbed → mouse_handle_click link
        ctrl+shift+left release grabbed → mouse_handle_click link
        ctrl+shift+left press grabbed → discard_event
        ctrl+alt+left press ungrabbed → mouse_selection rectangle
        ctrl+alt+left triplepress ungrabbed → mouse_selection line_from_point
        ctrl+shift+alt+left press ungrabbed → mouse_selection rectangle
        ctrl+shift+alt+left press grabbed → mouse_selection rectangle
        ctrl+shift+alt+left triplepress ungrabbed → mouse_selection line_from_point
        ctrl+shift+alt+left triplepress grabbed → mouse_selection line_from_point
        right press ungrabbed → mouse_selection extend
        shift+right press ungrabbed → mouse_selection extend
        shift+right press grabbed → mouse_selection extend
        ctrl+shift+right press ungrabbed → mouse_show_command_output
        middle release ungrabbed → paste_from_selection
        shift+middle release ungrabbed → paste_selection
        shift+middle release grabbed → paste_selection
        shift+middle press grabbed → discard_event
Changed mouse actions:
        shift+left press ungrabbed → mouse_selection extend
Added shortcuts:
        shift+left → move_window left
        shift+right → move_window right
        shift+up → move_window up
        shift+down → move_window down
        alt+- → combine : goto_layout splits : launch --location=hsplit --cwd=current
        alt+0 → goto_tab 0
        alt+1 → goto_tab 1
        alt+2 → goto_tab 2
        alt+3 → goto_tab 3
        alt+4 → goto_tab 4
        alt+5 → goto_tab 5
        alt+6 → goto_tab 6
        alt+7 → goto_tab 7
        alt+8 → goto_tab 8
        alt+9 → goto_tab 9
        alt+= → combine : goto_layout splits : launch --location=vsplit --cwd=current
        alt+a → next_layout
        alt+d → detach_window new-tab
        alt+q → close_tab
        alt+t → kitten toggle_term.py
        alt+w → new_tab !neighbor
        alt+z → kitten toggle_zoom.py
        alt+enter → toggle_fullscreen
        ctrl+c → copy_or_interrupt
        ctrl+h → kitten pass_keys.py neighboring_window left   ctrl+h
        ctrl+j → kitten pass_keys.py neighboring_window bottom ctrl+j
        ctrl+k → kitten pass_keys.py neighboring_window top    ctrl+k
        ctrl+l → kitten pass_keys.py neighboring_window right  ctrl+l
        ctrl+v → paste_from_clipboard
        ctrl+backspace → send_text all \x1b\x7f
Removed shortcuts:
        shift+insert → paste_from_selection
        ctrl+tab → next_tab
        ctrl+shift++ → change_font_size all +2.0
        ctrl+shift+, → move_tab_backward
        ctrl+shift+. → move_tab_forward
        ctrl+shift+1 → first_window
        ctrl+shift+2 → second_window
        ctrl+shift+3 → third_window
        ctrl+shift+4 → fourth_window
        ctrl+shift+7 → seventh_window
        ctrl+shift+8 → eighth_window
        ctrl+shift+9 → ninth_window
        ctrl+shift+[ → previous_window
        ctrl+shift+] → next_window
        ctrl+shift+` → move_window_to_top
        ctrl+shift+a > 1 → set_background_opacity 1
        ctrl+shift+a > d → set_background_opacity default
        ctrl+shift+a > l → set_background_opacity -0.1
        ctrl+shift+a > m → set_background_opacity +0.1
        ctrl+shift+b → move_window_backward
        ctrl+shift+c → copy_to_clipboard
        ctrl+shift+f → move_window_forward
        ctrl+shift+l → next_layout
        ctrl+shift+n → new_os_window
        ctrl+shift+o → pass_selection_to_program
        ctrl+shift+q → close_tab
        ctrl+shift+r → start_resizing_window
        ctrl+shift+s → paste_from_selection
        ctrl+shift+t → new_tab
        ctrl+shift+u → kitten unicode_input
        ctrl+shift+v → paste_from_clipboard
        ctrl+shift+w → close_window
        ctrl+shift+escape → kitty_shell window
        ctrl+shift+tab → previous_tab
        ctrl+shift+backspace → change_font_size all 0
        ctrl+shift+left → previous_tab
        ctrl+shift+right → next_tab
        ctrl+shift+up → scroll_line_up
        ctrl+shift+down → scroll_line_down
        ctrl+shift+f2 → edit_config_file
        ctrl+shift+f5 → load_config_file
        ctrl+shift+f6 → debug_config
        ctrl+shift+f7 → focus_visible_window
        ctrl+shift+f8 → swap_with_window
        ctrl+shift+f10 → toggle_maximized
        ctrl+shift+f11 → toggle_fullscreen
        ctrl+shift+kp_subtract → change_font_size all -2.0
        ctrl+shift+kp_add → change_font_size all +2.0
        ctrl+shift+alt+t → set_tab_title
Changed shortcuts:
        ctrl+shift+0 → change_font_size all 0
        ctrl+shift+5 → load_config_file
        ctrl+shift+6 → debug_config
        ctrl+shift+enter → launch --cwd=current
        ctrl+shift+delete → clear_terminal to_cursor active
Colors:
        active_border_color                #393f4a
        active_tab_background              #61afef
        active_tab_foreground              #282c34
        background                         #282c34
        bell_border_color                  #ef7c2a
        color0                             #20232a
        color1                             #e86671
        color10                            #98c379
        color11                            #e5c07b
        color12                            #61afef
        color13                            #c678dd
        color14                            #56b6c2
        color15                            #abb2bf
        color2                             #98c379
        color3                             #e5c07b
        color4                             #61afef
        color5                             #c678dd
        color6                             #56b6c2
        color7                             #798294
        color8                             #5c6370
        color9                             #e86671
        cursor                             #528bff
        cursor_text_color                  #ffffff
        foreground                         #abb2bf
        inactive_border_color              #393f4a
        inactive_tab_background            #282c34
        inactive_tab_foreground            #abb2bf
        selection_background               #393f4a
        selection_foreground               #abb2bf
        url_color                          #98c379

Important environment variables seen by the kitty process:
        PATH                                /nix/store/b91cjk6isrdncid5b4h9qk7w41ypgc59-kitty-0.25.2/bin:/nix/store/5vxyrzhy1wmvm8y3axwv3qg43znlfpbj-imagemagick-7.1.0-46/bin:/nix/store/s0jnm2cyl8ildkqmlx7cg16fs73kmmbf-ncurses->
        LANG                                en_US.UTF-8
        SHELL                               /run/current-system/sw/bin/zsh
        DISPLAY                             :0
        WAYLAND_DISPLAY                     wayland-0
        USER                                sand
        XDG_CONFIG_DIRS                     /home/sand/.nix-profile/etc/xdg:/etc/profiles/per-user/sand/etc/xdg:/nix/var/nix/profiles/default/etc/xdg:/run/current-system/sw/etc/xdg:/nix/store/s09pik5y4rb7c1wilqp2mrwmcyk0l387-g>
        XDG_MENU_PREFIX                     gnome-
        XDG_DATA_HOME                       /home/sand/.local/share
        XDG_CONFIG_HOME                     /home/sand/.config
        XDG_SESSION_DESKTOP                 gnome
        XDG_SESSION_TYPE                    wayland
        XDG_CURRENT_DESKTOP                 GNOME
        XDG_CACHE_HOME                      /home/sand/.cache
        XDG_SESSION_CLASS                   user
        XDG_DESKTOP_PORTAL_DIR              /nix/store/58i8dww101715s5slfm8lpi9wr852xyr-xdg-portals/share/xdg-desktop-portal/portals
        XDG_STATE_HOME                      /home/sand/.local/state
        XDG_RUNTIME_DIR                     /run/user/1000
        XDG_DATA_DIRS                       /nix/store/2xj5lw4r3k5l1zsf4jrayfnaq2d1lf33-shared-mime-info-unstable-2021-12-03/share:/nix/store/7syziipdid6h97b68jmlf3rp36dg9jdb-gnome-shell-42.4/share:/nix/store/i6z1pnkzmrz6zmszl>

Additional context Try to reproduce the problem with kitty --config NONE if you cannot then post a minimal kitty.conf that reproduces the problem. If the problem involves interaction with some other terminal program post a minimal config for that program to reproduce the problem as well.

kovidgoyal commented 2 years ago

This is a bug in the GPU driver.

iverberk commented 1 year ago

Hi @kovidgoyal,

Unfortunately I'm running into the same issue. You mention that it's a bug in the GPU driver (vmwgfx) and I'd like to find out what I can do to get it resolved. I guess that @sandangel and I are basing our setup on the same example, set by Mitchell Hashimoto: https://github.com/mitchellh/nixos-config

I've tried different terminals (rxvt-unicode, wezterm, alacritty) and they all seem to work fine with showing the visual mouse selection. Could you help me point to the code in Kitty where the interaction with the GPU driver might fail? Is Kitty using a particular method that the other editors are not (as far as you know of course).

Any help in pointing me in the right direction for further investigation is highly appreciated and just wanted to say that I really enjoy using Kitty and appreciate the effort that you continue to show in developing and supporting Kitty.

kovidgoyal commented 1 year ago

There is nothing special in how visual selections are displayed. They are done by simply changing the background and foreground colors in the selected cells. If you want to trace the code look in shaders.c and cell_vertex.glsl

kristoff-it commented 1 year ago

I had a debugging session with @SpexGuy and we tracked down the issue to the fact that the driver doesn't support single byte vertex attributes. Changing is_selected to be a GL_UNSIGNED_INT makes everything work again.

If I understand correctly this is not something unique to this driver (Martin knew to look for this issue because it's also the case with other GPUs), but it's clearly not super common if hasn't been reported in other issues (searched briefly, couldn't find any reference).

Here's a patch to fix the problem (also includes a couple changes to shell.nix that I needed on NixOS 22.11). While the patch does indeed fix the problem it does also increase memory consumption, which is a complete waste for machines that do support single byte vertex attributes. If @kovidgoyal is interested in looking for a solution that works for everybody (be it some build flag or reworking the approach to rendering selected cells), great, otherwise I guess this is just just a patch for those of us who are affected by the bug.

In this second case maybe it would make sense for @mitchellh to consider tweaking mitchellh/nixos-config to make use of it (only the C diff, the build script used by nix packages is already correct). A few people would certainly be appreciative of that :^)

From 06e3b236cd59b4ca1d4f29618fca1f01dcd9a991 Mon Sep 17 00:00:00 2001
From: Loris Cro <kappaloris@gmail.com>
Date: Tue, 17 Jan 2023 13:03:42 +0100
Subject: [PATCH] vmware 3d-accel compat

---
 kitty/screen.c  | 4 ++--
 kitty/shaders.c | 4 ++--
 shell.nix       | 2 ++
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/kitty/screen.c b/kitty/screen.c
index e52dd98f..7e7d0a1f 100644
--- a/kitty/screen.c
+++ b/kitty/screen.c
@@ -2391,12 +2391,12 @@ iteration_data_is_empty(const Screen *self, const IterationData *idata) {
 }

 static void
-apply_selection(Screen *self, uint8_t *data, Selection *s, uint8_t set_mask) {
+apply_selection(Screen *self, uint32_t *data, Selection *s, uint8_t set_mask) {
     iteration_data(self, s, &s->last_rendered, -self->historybuf->count, true);

     for (int y = MAX(0, s->last_rendered.y); y < s->last_rendered.y_limit && y < (int)self->lines; y++) {
         Line *line = visual_line_(self, y);
-        uint8_t *line_start = data + self->columns * y;
+        uint32_t *line_start = data + self->columns * y;
         XRange xr = xrange_for_iteration(&s->last_rendered, y, line);
         for (index_type x = xr.x; x < xr.x_limit; x++) line_start[x] |= set_mask;
     }
diff --git a/kitty/shaders.c b/kitty/shaders.c
index a4ae4e90..ca6634df 100644
--- a/kitty/shaders.c
+++ b/kitty/shaders.c
@@ -233,7 +233,7 @@ create_cell_vao() {
     A1(colors, 3, GL_UNSIGNED_INT, fg);

     add_buffer_to_vao(vao_idx, GL_ARRAY_BUFFER);
-    A(is_selected, 1, GL_UNSIGNED_BYTE, NULL, 0);
+    A(is_selected, 1, GL_UNSIGNED_INT, NULL, 0);

     size_t bufnum = add_buffer_to_vao(vao_idx, GL_UNIFORM_BUFFER);
     alloc_vao_buffer(vao_idx, cell_program_layouts[CELL_PROGRAM].render_data.size, bufnum, GL_STREAM_DRAW);
@@ -399,7 +399,7 @@ cell_prepare_to_render(ssize_t vao_idx, ssize_t gvao_idx, Screen *screen, GLfloa
     }

     if (screen->reload_all_gpu_data || screen_resized || screen_is_selection_dirty(screen)) {
-        sz = (size_t)screen->lines * screen->columns;
+        sz = (size_t)screen->lines * screen->columns * sizeof(uint32_t);
         address = alloc_and_map_vao_buffer(vao_idx, sz, selection_buffer, GL_STREAM_DRAW, GL_WRITE_ONLY);
         screen_apply_selection(screen, address, sz);
         unmap_vao_buffer(vao_idx, selection_buffer); address = NULL;
diff --git a/shell.nix b/shell.nix
index 4e5744fd..5c731ae2 100644
--- a/shell.nix
+++ b/shell.nix
@@ -14,6 +14,7 @@ mkShell rec {
     ncurses
     lcms2
     librsync
+    openssl.dev
   ] ++ optionals stdenv.isDarwin [
     Cocoa
     CoreGraphics
@@ -57,6 +58,7 @@ mkShell rec {
   shellHook = if stdenv.isDarwin then ''
     export KITTY_NO_LTO=
   '' else ''
+    export KITTY_FONTCONFIG_LIBRARY='${fontconfig.lib}/lib/libfontconfig.so'
     export KITTY_EGL_LIBRARY='${lib.getLib libGL}/lib/libEGL.so.1'
     export KITTY_STARTUP_NOTIFICATION_LIBRARY='${libstartup_notification}/lib/libstartup-notification-1.so'
     export KITTY_CANBERRA_LIBRARY='${libcanberra}/lib/libcanberra.so'
-- 
2.38.1
kovidgoyal commented 1 year ago

I would be pretty reluctant to increase resource consumption (both memory and CPU -> GPU bandwidth for everybody to workaround an issue with a single GPU driver (havent heard of this affecting any other GPU driver).

I would consider doing this at runtime based on GPU detection, since it is a pretty simple patch. If you can come up with some reasonably robust code that identifies when we are running on an affected system, I can easily adapt your patch to be conditioned on a global set at startup.

sandangel commented 1 year ago

hi @kristoff-it , could you share with us your nix-config to apply your patch to fix this issue? I really appreciate it.

mitchellh commented 1 year ago

I just wanted to note that I reported this issue to someone with access to the VMware graphics driver team, so hopefully this can get on their radar.

mikeroySoft commented 1 year ago

Thanks for the ping on this @mitchellh We've (VMware) started investigating... The team notes that it would 'probably be really easy to fix' if we had an apitrace of the app that captures the problem (i.e. just record a trace on a system where this issue is happening). We could then add that apitrace file to our testing and make sure we never regress

mikeroySoft commented 1 year ago

Follow up, but we'll actually also take an apitrace of a working scenario... it'd be the same investigation for us... what are the calls being made. We'd then put that against our stack and see what fails.

kovidgoyal commented 1 year ago

Here's a trace from a working system. kitty.trace.zip

sandangel commented 1 year ago

Hi @mikeroySoft , may I ask if there is any update on this issue?

jrfonseca commented 1 year ago

This was fixed upstream by my colleague Charmaine Lee on https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22568

The issue was in a CPU fallback, used to expand the GL_UNSIGNED_BYTE array to GL_UNSIGNED_INT. Mesa uses JIT generated assembly code on x86 for this, but plain C code on Arm, and the C code had a bug.

There's also an upcoming follow up change that will avoid the CPU fallback altogether.

sandangel commented 1 year ago

@jrfonseca does it mean if we update mesa this issue will be fixed right? Coool. Not sure if it's released yet and which version this fix is included.

jrfonseca commented 1 year ago

@sandangel, the fix was cherry-picked into staging/23.1 branch, so I expect it will be included on Mesa release 23.1, and subsequent releases.

sandangel commented 1 year ago

Thanks a lot for sharing. Really appreciate it.

jrfonseca commented 1 year ago

I would be pretty reluctant to increase resource consumption (both memory and CPU -> GPU bandwidth for everybody to workaround an issue with a single GPU driver (havent heard of this affecting any other GPU driver).

@kovidgoyal, for the record, there are a few other Mesa drivers which are essentially expanding the bytes to uints behind the scenes. Pretty much every driver which reports PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY==1, namely:

Some of these are very old. And nobody sees the bug on x86 because Mesa uses a JIT assembly to do this on x86, which didn't had the bug.

Our fix applies to all the above. And we believe we can avoid the CPU fallback on svga driver (Intel or Arm), but keep in mind some drivers will continue to do the byte to uint expansion behind the scenes.

kovidgoyal commented 1 year ago

OK, thanks.