RISC-V emulator in the form of a pixel shader, enabling Linux to run in VRChat

The results of the experiment on launching Linux within a virtual 3D space of the multiplayer online game VRChat, which allows the uploading of 3D models with their own shaders, have been published. To realize the conceived idea, an emulator of the RISC-V architecture was created, executed on the GPU as a pixel (fragment) shader (since VRChat does not support compute shaders and UAV). The emulator's code is published under the MIT license.

The emulator is based on an implementation in the C language, which itself utilized the developments of the minimalist riscv-rust emulator, developed in Rust. The prepared C code is translated into a pixel shader in HLSL, suitable for loading into VRChat. The emulator provides full support for the rv32imasu instruction set architecture, the SV32 memory management unit, and a minimal set of peripheral devices (UART and timer). The prepared capabilities are sufficient for loading the Linux kernel 5.13.5 and the basic BusyBox command-line environment, which can be interacted with directly from the virtual world of VRChat.

RISC-V emulator in the form of a pixel shader, enabling Linux to run in VRChat
RISC-V emulator in the form of a pixel shader, enabling Linux to run in VRChat

The emulator is implemented in the shader as its own dynamic texture (Unity Custom Render Texture), complemented by the Udon scripts provided for VRChat, which are used to control the emulator during execution. The contents of the RAM and the state of the emulated system's processor are saved as a texture, sized 2048×2048 pixels. The emulated processor operates at a frequency of 250 kHz. Besides Linux, Micropython can also be run in the emulator.

RISC-V emulator in the form of a pixel shader, enabling Linux to run in VRChat

To organize persistent data storage with read and write support, a trick is employed involving the use of a Camera object attached to a rectangular area generated by the shader, directing the output of the rendered texture into the shader's input. Thus, any pixel written during the execution of the pixel shader can be read during the processing of the next frame.

When pixel shaders are applied, a separate instance of the shader is launched for each pixel of the texture in parallel. This characteristic significantly complicates implementation and requires separate coordination of the entire emulated system's state and matching the position of the processed pixel with the CPU state or the RAM content of the emulated system (each pixel can encode 128 bits of information). The shader code requires the inclusion of a vast number of checks, for which a Perl preprocessor (perlpp) was utilized to simplify implementation.

Play video


Source: opennet.ru
Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster