Skip to content

Commit 56721e0

Browse files
committed
Implement virglrenderer backend for virtio-gpu
Follow the installation guide from: https://gitlab.freedesktop.org/virgl/virglrenderer Prerequisite: `sudo apt install libepoxy-dev`
1 parent b3f1300 commit 56721e0

15 files changed

+2344
-1097
lines changed

Makefile

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,30 +128,53 @@ endif
128128

129129
# virtio-gpu
130130
ENABLE_VIRTIOGPU ?= 1
131-
ifneq ($(UNAME_S),Linux)
132-
ENABLE_VIRTIOGPU := 0
133-
endif
131+
132+
# Enable 3D acceleration for virtio-gpu using virglrenderer.
133+
# When set to 1, virtio-gpu uses the virglrenderer backend instead of the software
134+
# renderer.
135+
ENABLE_VIRGL ?= 1
134136

135137
# SDL2
136138
ENABLE_SDL ?= 1
137139
ifeq (, $(shell which sdl2-config))
138140
$(warning No sdl2-config in $$PATH. Check SDL2 installation in advance)
139141
override ENABLE_SDL := 0
140142
endif
141-
142143
ifeq ($(ENABLE_SDL),1)
143144
CFLAGS += $(shell sdl2-config --cflags)
144145
LDFLAGS += $(shell sdl2-config --libs)
145146
else
147+
# Disable virtio-gpu and virgl if SDL is not set
146148
override ENABLE_VIRTIOGPU := 0
149+
override ENABLE_VIRGL := 0
147150
endif
148151

152+
# virtio-gpu
153+
ifneq ($(UNAME_S),Linux)
154+
ENABLE_VIRTIOGPU := 0
155+
endif
149156
ifeq ($(ENABLE_VIRTIOGPU),1)
150-
OBJS_EXTRA += window.o
157+
OBJS_EXTRA += window-events.o
151158
OBJS_EXTRA += virtio-gpu.o
159+
else
160+
override ENABLE_VIRGL := 0
161+
endif
162+
163+
# VirGL
164+
ifeq ($(ENABLE_VIRGL),1)
165+
CFLAGS += $(shell pkg-config virglrenderer gl egl epoxy --cflags)
166+
LDFLAGS += $(shell pkg-config virglrenderer gl egl epoxy --libs)
167+
OBJS_EXTRA += virgl.o
168+
OBJS_EXTRA += window-gl.o
169+
else
170+
ifeq ($(ENABLE_VIRTIOGPU),1)
171+
OBJS_EXTRA += virtio-gpu-sw.o
172+
OBJS_EXTRA += window-sw.o
173+
endif
152174
endif
153175

154176
$(call set-feature, VIRTIOGPU)
177+
$(call set-feature, VIRGL)
155178

156179
BIN = semu
157180
all: $(BIN) minimal.dtb

common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#pragma once
22

3+
#include <stddef.h>
4+
#include <stdint.h>
5+
#include <sys/uio.h>
6+
37
#include "feature.h"
48

59
#define BITS_PER_CHAR 8

device.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,6 @@ void virtio_gpu_write(hart_t *vm,
270270
uint8_t width,
271271
uint32_t value);
272272

273-
void semu_virgl_init(virtio_gpu_state_t *vgpu);
274-
275273
void virtio_gpu_init(virtio_gpu_state_t *vgpu);
276274
void virtio_gpu_add_scanout(virtio_gpu_state_t *vgpu,
277275
uint32_t width,

feature.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#define SEMU_FEATURE_VIRTIOGPU 1
2323
#endif
2424

25+
/* VirGL */
26+
#ifndef SEMU_FEATURE_VIRGL
27+
#define SEMU_FEATURE_VIRGL 1
28+
#endif
29+
2530
/* virtio-input */
2631
#ifndef SEMU_FEATURE_VIRTIOINPUT
2732
#define SEMU_FEATURE_VIRTIOINPUT 1

main.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
#include "mini-gdbstub/include/gdbstub.h"
1414
#include "riscv.h"
1515
#include "riscv_private.h"
16+
#include "virgl.h"
1617
#include "window.h"
1718

1819
#define PRIV(x) ((emu_state_t *) x->priv)
1920

21+
extern const struct window_backend g_window;
22+
2023
/* Define fetch separately since it is simpler (fixed width, already checked
2124
* alignment, only main RAM is executable).
2225
*/
@@ -760,19 +763,23 @@ static int semu_init(emu_state_t *emu, int argc, char **argv)
760763
fprintf(stderr, "No virtio-snd functioned\n");
761764
emu->vsnd.ram = emu->ram;
762765
#endif
763-
#if SEMU_HAS(VIRTIOGPU)
764-
emu->vgpu.ram = emu->ram;
765-
virtio_gpu_init(&(emu->vgpu));
766-
virtio_gpu_add_scanout(&(emu->vgpu), 1024, 768);
767-
window_init();
768-
#endif
769766
#if SEMU_HAS(VIRTIOINPUT)
770767
emu->vkeyboard.ram = emu->ram;
771768
virtio_input_init(&(emu->vkeyboard));
772769

773770
emu->vmouse.ram = emu->ram;
774771
virtio_input_init(&(emu->vmouse));
775772
#endif
773+
#if SEMU_HAS(VIRTIOGPU)
774+
emu->vgpu.ram = emu->ram;
775+
virtio_gpu_init(&(emu->vgpu));
776+
virtio_gpu_add_scanout(&(emu->vgpu), 1024, 768);
777+
778+
g_window.window_init();
779+
#endif
780+
#if SEMU_HAS(VIRGL)
781+
semu_virgl_init(&(emu->vgpu));
782+
#endif
776783

777784
emu->peripheral_update_ctr = 0;
778785
emu->debug = debug;
@@ -823,6 +830,10 @@ static int semu_step(emu_state_t *emu)
823830
if (emu->vmouse.InterruptStatus)
824831
emu_update_vinput_mouse_interrupts(vm);
825832
#endif
833+
834+
#if SEMU_HAS(VIRGL)
835+
semu_virgl_fence_poll();
836+
#endif
826837
}
827838

828839
emu_update_timer_interrupt(vm->hart[i]);

0 commit comments

Comments
 (0)