How Fastfetch Detects and Displays GPU Information: A Deep Dive into the Source Code
Fastfetch detects GPU information using a cascading pipeline of PCI, Vulkan, OpenCL, and OpenGL backends via ffDetectGPU in src/detection/gpu/gpu.c, then formats the results through ffPrintGPU in src/modules/gpu/gpu.c with customizable output templates and percentage bars.
The fastfetch-cli/fastfetch repository implements a modular, multi-backend approach to GPU detection that prioritizes accuracy while maintaining cross-platform compatibility. Understanding how fastfetch detects and displays GPU information requires examining the two-stage architecture that separates hardware detection from presentation formatting.
The Two-Stage Architecture: Detection and Presentation
Fastfetch splits GPU handling into distinct detection and presentation phases to maintain clean separation between hardware queries and output formatting.
The ffDetectGPU function (located in src/detection/gpu/gpu.c) orchestrates the detection pipeline, attempting multiple backends until it successfully populates a list of FFGPUResult structures. Once detection completes, ffPrintGPU (in src/modules/gpu/gpu.c) handles the visual presentation, applying user-configured filters and format strings to render the final output.
The GPU Detection Pipeline
The detection system uses a cascading fallback mechanism defined by the FFGPUOptions.detectionMethod setting (default: auto). When ffDetectGPU executes, it attempts backends from most specific to most portable until the GPU list is populated.
Stage 1: PCI and Native Platform Detection
The primary detection method invokes ffDetectGPUImpl, a platform-specific implementation (found in files like gpu_linux.c or gpu_windows.c) that queries PCI device data directly from the system. This backend extracts vendor ID, device ID, memory size, and other hardware identifiers from the PCI subsystem.
If the PCI scan returns valid GPU data, the pipeline terminates immediately and returns the populated FFGPUResult list to the caller.
Stage 2: Vulkan Backend
When PCI detection fails or returns incomplete data, fastfetch attempts ffDetectVulkan() from src/detection/vulkan/vulkan.c. This function:
- Creates a Vulkan instance using the platform's Vulkan loader
- Enumerates physical devices via
vkEnumeratePhysicalDevices - Extracts device type, name, memory heaps, driver version, and API version
- Populates
FFVulkanResultstructures that are converted intoFFGPUResultentries
The Vulkan backend provides detailed information about dedicated and shared GPU memory, making it ideal for modern discrete graphics cards.
Stage 3: OpenCL Backend
If Vulkan is unavailable, ffDetectOpenCL() (in src/detection/opencl/opencl.c) queries OpenCL platforms and devices. This backend enumerates GPU devices from OpenCL platforms, extracting device names, vendor strings, and memory statistics. Results are mapped into the standard FFGPUResult format, ensuring the presentation layer remains backend-agnostic.
Stage 4: OpenGL Fallback
When all hardware APIs fail, fastfetch executes detectByOpenGL within src/detection/gpu/gpu.c. This runs a minimal OpenGL context to call glGetString(GL_VENDOR), glGetString(GL_RENDERER), and glGetString(GL_VERSION), creating a single synthetic FFGPUResult with the renderer name and a "OpenGL" platform tag.
If every backend returns an error or empty list, ffDetectGPU returns the error string "GPU detection failed".
Configuration and Detection Method Selection
Users control the pipeline via ffParseGPUJsonObject (lines 30-67 in the module), which parses JSON/YAML configuration:
{
"gpu": {
"detectionMethod": "vulkan",
"hideType": "none"
}
}
Available detection methods include auto, pci, vulkan, opencl, and opengl. Setting detectionMethod to a specific backend bypasses the auto-detection cascade and forces that particular implementation.
Formatting and Display Logic
The ffPrintGPU function transforms raw FFGPUResult data into human-readable terminal output through several processing stages.
Filtering by GPU Type
Before formatting, fastfetch applies the hideType filter (lines 79-90 in src/modules/gpu/gpu.c). This option accepts none, unknown, integrated, or discrete, allowing users to suppress specific GPU categories from the output. For example, setting "hideType": "integrated" removes Intel UHD or AMD APU entries while preserving discrete NVIDIA or AMD Radeon cards.
Output Formatting and Custom Templates
The printGPUResult function constructs display strings using either default formatting or user-supplied templates via FF_PRINT_FORMAT_CHECKED. Available template variables include:
{vendor}- GPU manufacturer{name}- Device name string{temperature}- Current temperature with unit{dedicated-total}- Total dedicated VRAM{dedicated-used}- Used dedicated VRAM{shared-total}- Total shared system memory{core-count}- Number of compute units{core-frequency}- Current GPU clock speed
Example custom format execution:
fastfetch --config none --module gpu --format "{name} ({vendor}) - {dedicated-used}/{dedicated-total} ({dedicated-percentage-num}%)"
This outputs:
Intel(R) UHD Graphics 620 (Intel) - 256MiB/1.2GiB (21%)
Memory Statistics and Temperature Display
The presentation layer integrates with ffTempsAppendNum for temperature readouts and src/common/percent.h for memory calculations. When options->percent.type is enabled, fastfetch calculates usage percentages and renders ASCII bar graphs alongside numeric values (e.g., 256MiB / 1.2GiB, 21%).
After printing, ffPrintGPU destroys all ffStrbuf fields within each FFGPUResult to prevent memory leaks.
Configuration Options
GPU behavior is controlled through the FFGPUOptions structure defined in src/options/gpu.h. Key configuration parameters parsed by ffParseGPUJsonObject include:
- detectionMethod: Backend selection (
auto,pci,vulkan,opencl,opengl) - hideType: Filtering (
none,unknown,integrated,discrete) - percent: Display style for memory statistics (
num,bar, orhide)
Default configurations are generated by ffGenerateGPUJsonConfig for export to JSON configuration files.
Summary
- Fastfetch uses a cascading detection pipeline starting with PCI platform APIs, falling back through Vulkan, OpenCL, and finally OpenGL via
ffDetectGPUinsrc/detection/gpu/gpu.c - The presentation layer in
src/modules/gpu/gpu.chandles filtering by GPU type, temperature display, and custom format strings throughffPrintGPU - Configuration is JSON-driven through
ffParseGPUJsonObject, supporting detection method selection and output customization - Template variables like
{dedicated-used},{temperature}, and{name}allow precise control over GPU information display - Memory safety is enforced through explicit cleanup of
ffStrbufstructures after output generation
Frequently Asked Questions
What detection methods does fastfetch use for GPU information?
Fastfetch attempts detection in this order: PCI/native platform APIs first (via ffDetectGPUImpl), then Vulkan (ffDetectVulkan), followed by OpenCL (ffDetectOpenCL), and finally OpenGL (detectByOpenGL) as a last resort. The auto setting in detectionMethod enables this cascade, while specific values force a single backend.
How can I force fastfetch to use Vulkan instead of PCI for GPU detection?
Create or modify your fastfetch configuration file (typically ~/.config/fastfetch/config.jsonc) to specify the Vulkan backend:
{
"gpu": {
"detectionMethod": "vulkan"
}
}
This bypasses PCI detection entirely and uses ffDetectVulkan() from src/detection/vulkan/vulkan.c to query physical devices directly through the Vulkan API.
What GPU information can fastfetch display?
According to the FFGPUResult structure and printGPUResult implementation, fastfetch can display vendor name, device name, GPU type (integrated/discrete/unknown), temperature, core count, core frequency, dedicated VRAM (total/used/percentage), and shared system memory (total/used/percentage). Custom formats support placeholders like {vendor}, {name}, {temperature}, and memory statistics variables.
Where is the GPU detection logic located in the fastfetch source code?
The core detection dispatcher resides in src/detection/gpu/gpu.c (implementing ffDetectGPU and the OpenGL fallback). Platform-specific PCI implementations are in files like gpu_linux.c or gpu_windows.c. Vulkan detection is in src/detection/vulkan/vulkan.c, OpenCL in src/detection/opencl/opencl.c, and the presentation module is in src/modules/gpu/gpu.c. Configuration options are defined in src/options/gpu.h.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →