# Rendering The 3D renderer lives in `src/render/` and is built on **SDL_GPU**: Metal on macOS/iOS, Vulkan on Linux, Android and Windows. PCs whose GPU has no Vulkan get it through our own implementation of the SDL_GPU calls we use on OpenGL ES 3.0 ([OpenGL ES](#opengl-es)). The 2D UI (`Ui`, `SDL_Renderer`) keeps working unchanged on top of it. ## Frame structure ``` App::init GpuContext::create(window) SDL_CreateGPUDevice(SPIR-V | MSL) SDL_CreateGPURenderer(device, window) 2D renderer on the same device; it owns the swapchain (failure) -> SDL_CreateRenderer(window, nullptr), 2D only each frame (App::iterate) (gamma not 1) GammaPass::begin the 2D renderer draws into a window-sized target Ui::clear Screen::render View3D::draw(rect, camera, models) own command buffer: render pass -> offscreen RGBA8 (+MSAA resolve, +depth) submit SDL_RenderTexture(wrapper) wrapper = SDL_Texture around the GPU texture ui.text(...) UI on top (gamma not 1) GammaPass::end that target onto the window through gamma.frag SDL_RenderPresent 2D command buffer, submitted after ours ``` The 3D views render into offscreen textures. Each view's command buffer is submitted before the 2D renderer's, so GPU ordering is guaranteed. The 2D renderer then composites the view as an ordinary texture (`SDL_PROP_TEXTURE_CREATE_GPU_TEXTURE_POINTER`). This means: - Several 3D views per screen work (e.g. a car preview inside a menu). - `--shot` and F12 (`SDL_RenderReadPixels`) include the 3D layer without extra code. - HiDPI: the offscreen target is sized in physical pixels (logical rectangle × logical-presentation scale), so 3D stays sharp at 640×360 UI scale. - Resizing is handled lazily: the target is recreated when the pixel size or MSAA level changes. - The player's gamma (`[video] gamma`, `render::GammaPass`) is the one pass over the whole frame. At 1, the default, it does not run and the frame goes to the window as before. Otherwise the 2D renderer draws the frame into an RGBA8 target the window's size (wrapped like a 3D view's, `render::wrap_texture`, with the window's logical presentation), and before the present draws it over the whole window with `gamma.frag`, `out = in ^ (1 / gamma)` per channel: on SDL_GPU as the GPU renderer's own fragment shader (`SDL_GPURenderState`, in the frame's command buffer, so in order); on OpenGL ES, whose 2D renderer takes no shaders, through our own pass (`gamma.vert`'s triangle) into a second target that the 2D renderer then copies (gpu_gles.cpp draws pipelines without vertex attributes with no vertex buffer bound, as SDL_GPU does). UI text in the target is rasterised at the window's pixel size, as on the window (`render::drawing_to_window`), so the frame matches one drawn to the window exactly. Screenshots are read before the pass, so they keep GT3's colours (`GT3R_SHOT_GAMMA=1`: after it, as shown). There is none in VR (the canvases go to the headset as they are) or without the 3D renderer. - The dynamic scale allocates nothing: the targets are sized for its ceiling (`GpuContext::render_ceiling`) and each frame draws its picture into their top-left corner (viewport and scissor), which the 2D renderer upscales with a source rectangle half a texel short of the unused part. The post passes map each picture they blend (the last frame, the dissolve's frozen one) from the scale it was taken at. Recreating the targets at every 5% step would stall a Moto G Power 2024 for 90–440 ms each time (SDL allocates a new image per target for every frame in flight); with the fixed size, 15 steps in one race had a worst frame of 29 ms. `GT3R_DYN_CYCLE=` sweeps the scale from its ceiling to 50% and back (also in `--shot` runs) for checking. **SDL version:** this needs SDL ≥ 3.4 (`SDL_CreateGPURenderer`, wrapping GPU textures). The code compiles against 3.2 (guarded by `SDL_VERSION_ATLEAST(3, 4, 0)`) and then runs 2D-only. The build, CI and Android all fetch `release-3.4.12` (`cmake/Dependencies.cmake`); the VR builds take a pinned commit of SDL's 3.6 development branch ([VR](VR.md#building)). ## Fallback `GpuContext::create` returns nullptr, and the app logs it and continues 2D-only, when: - SDL is older than 3.4; - no GPU backend accepts SPIR-V or MSL (e.g. a PC without Vulkan drivers: we don't ship DXIL yet), and OpenGL ES 3.0 is not there either ([OpenGL ES](#opengl-es)); - device, renderer, shader or sampler creation fails; - the user disabled it: `[video] gpu = 0` in `config.ini` (Settings → Display → 3D renderer → Off), or `--no-gpu`. `App::gpu()` is nullptr in that case. Screens must check it. The model viewer (`ModelViewerScreen`) shows the list and statistics with a "3D renderer unavailable" note. A GPU device lost while running (Vulkan's `VK_ERROR_DEVICE_LOST`, which SDL reports only in its error text) does not come back: `App::iterate` closes the game with a message. ## Configuration | `config.ini [video]` | Command line | Default | Effect | |---|---|---|---| | `gpu` | `--no-gpu`, `--gles`, `--gles-driver` | auto | Settings → Display → 3D renderer. `auto`: SDL_GPU, then OpenGL ES where built in (Windows, Linux); `gles`: OpenGL ES first; `gles_driver`: OpenGL ES first, on Windows the driver's rather than ANGLE; `0`: 2D only. Takes effect after a restart | | `msaa` | `--msaa N` | 4 (desktop), 2 (Android: phones, tablets, Quest) | 1/2/4/8, the nearest the device offers: a missing 2x becomes 4x (Mali GPUs offer 1x, 4x and 8x only; on a Galaxy A15 4x cost about 5 points of dynamic resolution), other missing levels the next lower. Phones default to 2x: on a Moto G Power 2024 (Trial Mountain, 6 cars, fixed 60% scale) 4x cost about 19% of the frame and 2x about 3% vs off, and with 4x dynamic resolution sat at its 50% floor. See `platform::default_msaa` | | `vsync` | | 1 | Present with vsync | | `aspect` | | 16:9 | Settings → Display → Aspect ratio: the game area of the game's own screens (`Screen::follows_aspect`: its menus, races, movies), 360 UI units tall and 640 (`16:9`), 480 (`4:3`) or the window's shape (`fill`, 480 to 1440 wide) across, letterboxed into the window (`App::layout_ui`, `platform::game_width`). Our own screens keep the 640×360 space or the whole window. 3D views are Hor+ (the vertical FOV is fixed). The race HUD keeps GT3's 640×448 layout in the middle and pushes the elements GT3 pushes in its 16:9 option to the game area's sides: 106.667 frame units on 16:9, more with `fill`, none at 4:3 (GT3's 4:3 layout). Always 16:9 in VR, where Settings has no such row | | `gamma` | | 1 | Settings → Display → Gamma, 0.50..2.00 in steps of 0.05 (`render::parse_gamma` clamps the file's value): the whole picture through `out = in ^ (1 / gamma)`, higher brighter ("Frame structure"). 1 removes the key, and Settings shows a "Reset gamma to 1.00" row while it is not 1. The pass costs a window-sized target (SDL keeps one per frame in flight) and a full-window draw a frame | | `limit_60fps` | | off | `on` (Settings → Display → Limit to 60 fps): on 120 Hz and faster displays draw every Nth refresh (120 Hz → 60 fps, 144 → 72) through SDL's main loop rate (`App::apply_frame_limit`; not in VR, where the headset paces the frames); off: every refresh | | `show_fps` | | off | `on` (Settings → Display → FPS counter): the frames drawn per second, averaged over about half a second, at the top left of the window over everything; on our own screens (`Screen::adaptive_ui`), whose header runs along the top, at the bottom right (`App::draw_fps`). In the safe area on phones, on the virtual screen in VR | | `full_car_detail` | | on (desktop), off (Android, iOS) | Settings → Display → Always show full car detail: every car at LOD 0 at any distance (`race::car_lod`, `App::full_car_detail`); off: GT3's LODs by distance. On a Galaxy A15 (Trial Mountain, 6 cars) on drew 32% more triangles and 25% more draw calls: +1.4 ms a frame at a fixed 70% scale with `gpusync=1` (+0.7 ms of it CPU), and in normal play about 1.5 points less dynamic resolution at the same 45 fps | | `showroom_wheels` | | on (desktop), off (Android, iOS) | Settings → Display → Showroom wheels in races: race cars' LOD 0 wheels are the menus' (`menu/cars` rim and 256x128 tyre texture, a fitted wheel's `menu/wheel/