When combined with a 3D game engine, such as the Source engine, the libretro framework is able to run on any surface in the game.
Replacing the simple game texture shown on an in-game TV screen with a fully functional libretro instance can do anything ranging from playing a full-length video with the ffmpeg core, to running interactive homebrew software, all on the in-game screen.
Adding libretro support to a 3D frontend allows a wide range of media to be loaded using libretro’s ever growing collection of cores. Only a few key components need to be in place to get started. This article outlines what was required to add libretro support to the Source engine for the 3D-frontend Anarchy Arcade (www.anarchyarcade.com). The process for other popular game engines is also very similar.
The Source engine has three notable road bumps when implementing libretro.
First is that the Source engine is DirectX, while libretro requires OpenGL for 3D acceleration. Without OpenGL, only the cores that support software mode will be able to function. Generally, this means only 2D cores will work properly, but technically it will depend on what the core supports.
The second road bump is audio. Anarchy Arcade uses PortAudio (www.portaudio.com) to play the audio streams that libretro provides. However, the Source engine itself may be capable of playing such audio streams more optimally. What ever the sound solution, it is still simple enough to get the audio streams from libretro and send them where ever they need to go.
The third road bump is frame scaling. The resolution of the frame that libretro generates will not match the resolution of your texture. The frame data must be re-scaled in real time before it can be written to the texture.
After libretro is initialized, a core module must be loaded and its interface built so that it can communicate with your frontend. The libretro core will be asking your frontend for different variables and options that your frontend must provide answers to. The core will also be delivering the video buffer and the audio streams to your frontend.
All that your frontend needs to do is display what libretro gives it. Your frontend can also forward keyboard & gamepad input to the libretro core so the user can interact with it. This communication all takes place during the main loop.
Source Engine Main Loop
After libretro is initialized, its retro_run method needs be able to be called as often as possible, without interfering with normal engine operations. A good place to plug your code into the Source engine for this is in CAutoGameSystemPerFrame::Update.
With a core loaded and retro_run being called from your main loop, libretro is fully functioning. Now the frontend just needs to display what it is given, and forward user input to the libretro core.
Source Engine Texture Access
Cores that have software rendering (such as most 2D cores) will basically require a memcopy of the frame that libretro gives your frontend. The Source engine has a special kind of texture called a procedural texture that allows you to write directly to its pixels, or do memcopies onto it. It works by plugging into the logic of the ITextureRegenerator::RegenerateTextureBits method to do a memcopy of the libretro frame buffer. This effectively draws what ever libretro is rendering onto the in-game texture.
Source Engine Input Capture
Finally, your frontend needs to send button state info to the libretro core so that the user can interact with it. In the Source engine, button states can be determined whenever needed by using the vgui::input()->IsKeyDown method.
Libretro Frontend on Source Engine w/ VR Mode
After all of these components are implemented into your frontend, you can use your interactive texture on how ever many surfaces you want. There is no additional performance impact for using it on more than 1 surface. You can also optimize your implementation so that memcopies only occur when they need to by assigning a CEntityMaterialProxy to the material that references your procedural texture. With these 3 simple components, you are able to run many of the libretro cores available at www.libretro.com, or your own homebrew software on the in-game screens of your Source engine 3D frontend.