Skip to content

Commit 7c747d7

Browse files
committed
Implement virglrenderer-based virtio-gpu device
Follow the installation guide from: https://gitlab.freedesktop.org/virgl/virglrenderer Prerequisite: `sudo apt install libepoxy-dev`
1 parent 85c56a2 commit 7c747d7

15 files changed

+2341
-1098
lines changed

Makefile

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,51 @@ ifeq ($(call has, VIRTIOINPUT), 1)
6363
OBJS_EXTRA += virtio-input.o
6464
endif
6565

66-
# virtio-gpu
66+
# virtio-gpu and virgl
6767
ENABLE_VIRTIOGPU ?= 1
68-
ifneq ($(UNAME_S),Linux)
69-
ENABLE_VIRTIOGPU := 0
70-
endif
68+
ENABLE_VIRGL ?= 1
7169

7270
# SDL2
7371
ENABLE_SDL ?= 1
7472
ifeq (, $(shell which sdl2-config))
7573
$(warning No sdl2-config in $$PATH. Check SDL2 installation in advance)
7674
override ENABLE_SDL := 0
7775
endif
78-
7976
ifeq ($(ENABLE_SDL),1)
8077
CFLAGS += $(shell sdl2-config --cflags)
8178
LDFLAGS += $(shell sdl2-config --libs)
8279
else
80+
# Disable virtio-gpu and virgl if SDL is not set
8381
override ENABLE_VIRTIOGPU := 0
82+
override ENABLE_VIRGL := 0
8483
endif
8584

85+
# virtio-gpu
86+
ifneq ($(UNAME_S),Linux)
87+
ENABLE_VIRTIOGPU := 0
88+
endif
8689
ifeq ($(ENABLE_VIRTIOGPU),1)
87-
OBJS_EXTRA += window.o
90+
OBJS_EXTRA += window-events.o
8891
OBJS_EXTRA += virtio-gpu.o
92+
else
93+
override ENABLE_VIRGL := 0
94+
endif
95+
96+
# VirGL
97+
ifeq ($(ENABLE_VIRGL),1)
98+
CFLAGS += $(shell pkg-config virglrenderer gl egl epoxy --cflags)
99+
LDFLAGS += $(shell pkg-config virglrenderer gl egl epoxy --libs)
100+
OBJS_EXTRA += virgl.o
101+
OBJS_EXTRA += window-gl.o
102+
else
103+
ifeq ($(ENABLE_VIRTIOGPU),1)
104+
OBJS_EXTRA += virtio-gpu-sw.o
105+
OBJS_EXTRA += window-sw.o
106+
endif
89107
endif
90108

91109
$(call set-feature, VIRTIOGPU)
110+
$(call set-feature, VIRGL)
92111

93112
BIN = semu
94113
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
@@ -266,8 +266,6 @@ void virtio_gpu_write(hart_t *vm,
266266
uint8_t width,
267267
uint32_t value);
268268

269-
void semu_virgl_init(virtio_gpu_state_t *vgpu);
270-
271269
void virtio_gpu_init(virtio_gpu_state_t *vgpu);
272270
void virtio_gpu_add_scanout(virtio_gpu_state_t *vgpu,
273271
uint32_t width,

feature.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#define SEMU_FEATURE_VIRTIOGPU 1
1818
#endif
1919

20+
/* VirGL */
21+
#ifndef SEMU_FEATURE_VIRGL
22+
#define SEMU_FEATURE_VIRGL 1
23+
#endif
24+
2025
/* virtio-input */
2126
#ifndef SEMU_FEATURE_VIRTIOINPUT
2227
#define SEMU_FEATURE_VIRTIOINPUT 1

main.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
#include "device.h"
1212
#include "riscv.h"
1313
#include "riscv_private.h"
14+
#include "virgl.h"
1415
#include "window.h"
1516

1617
#define PRIV(x) ((emu_state_t *) x->priv)
1718

19+
extern const struct window_backend g_window;
20+
1821
/* Define fetch separately since it is simpler (fixed width, already checked
1922
* alignment, only main RAM is executable).
2023
*/
@@ -722,19 +725,23 @@ static int semu_start(int argc, char **argv)
722725
emu.mtimer.mtimecmp = calloc(vm.n_hart, sizeof(uint64_t));
723726
emu.mswi.msip = calloc(vm.n_hart, sizeof(uint32_t));
724727
emu.sswi.ssip = calloc(vm.n_hart, sizeof(uint32_t));
725-
#if SEMU_HAS(VIRTIOGPU)
726-
emu.vgpu.ram = emu.ram;
727-
virtio_gpu_init(&(emu.vgpu));
728-
virtio_gpu_add_scanout(&(emu.vgpu), 1024, 768);
729-
window_init();
730-
#endif
731728
#if SEMU_HAS(VIRTIOINPUT)
732729
emu.vkeyboard.ram = emu.ram;
733730
virtio_input_init(&(emu.vkeyboard));
734731

735732
emu.vmouse.ram = emu.ram;
736733
virtio_input_init(&(emu.vmouse));
737734
#endif
735+
#if SEMU_HAS(VIRTIOGPU)
736+
emu.vgpu.ram = emu.ram;
737+
virtio_gpu_init(&(emu.vgpu));
738+
virtio_gpu_add_scanout(&(emu.vgpu), 1024, 768);
739+
740+
g_window.window_init();
741+
#endif
742+
#if SEMU_HAS(VIRGL)
743+
semu_virgl_init(&(emu.vgpu));
744+
#endif
738745

739746
/* Emulate */
740747
uint32_t peripheral_update_ctr = 0;
@@ -773,6 +780,10 @@ static int semu_start(int argc, char **argv)
773780
if (emu.vmouse.InterruptStatus)
774781
emu_update_vinput_mouse_interrupts(&vm);
775782
#endif
783+
784+
#if SEMU_HAS(VIRGL)
785+
semu_virgl_fence_poll();
786+
#endif
776787
}
777788

778789
emu_update_timer_interrupt(vm.hart[i]);

0 commit comments

Comments
 (0)