Repository navigation
GPU backend
render/gpu.mojo. GpuRenderer runs the rasterizer as a kernel with one thread per pixel. It consumes the same RasterVertex list as the CPU rasterizer and produces the same image.
three.js has no equivalent. Mojo's GPU support is compute only, so this is a compute kernel and not a graphics pipeline.
MAX 26.5.0 and an accelerator. See How to use the GPU backend. render/gpu.mojo is the only module that imports MAX.
| Function | Meaning |
|---|---|
available() -> Bool |
Whether a GPU is present. |
render_triangles(corners, width, height, background, mode, textures, lighting) -> Framebuffer |
One-shot: draw and read back. |
flatten(corners) -> List[Float32] |
The corner buffer the kernel reads, sixteen floats per vertex. |
flatten_lights(lighting) -> List[Float32] |
The light buffer. |
flatten_textures(store) |
Every texture in one buffer, with a descriptor table. |
triangle_state(corners) -> List[Int32] |
Texture, blend and lit per triangle. |
Hold one across frames. The device buffers survive between draws.
| Member | Meaning |
|---|---|
GpuRenderer(width, height) |
Create the context and the buffers. Raises without a GPU. |
set_textures(store) |
Upload every texture. All or nothing. |
draw(corners, background, mode, lighting) |
Rasterize into the device target. |
read_back() -> Framebuffer |
Copy colour and depth to the host. |
draw checks every triangle's state and every texture id on the host before it launches. The kernel cannot raise.
set_textures builds the new buffers first and replaces the old ones together with the count. A failed upload leaves the previous upload whole.
Coverage is integer arithmetic and matches the CPU exactly. Shading is floating point and matches within one level per channel, because the device fuses multiply and add. tests/test_gpu.mojo holds both backends to those standards on hand-built triangles and on whole prepared scenes.
The kernel calls the same functions as the CPU for the fill rule, texture wrapping, texel blending and light falloff. See Why the CPU and GPU share code.
__deinit__ waits for the queue, releases every buffer, and then releases the context. That order prevents a hang under CUDA. See The CUDA teardown hang.
The coverage tool excludes this module. A kernel has no stderr for the probes to write to. The parity tests cover it instead.
make bench compares the two backends. On an Apple M-series GPU the kernel wins by three to seven times from 640 by 480 up. The numbers include allocation and the copy back.
Tutorials
How-to
- Install
- Run the checks
- GPU backend
- Coverage
- Measure examples
- Add a feature
- Add a scanned building
- Write documentation
Reference
- Scene graph
- Rotations
- Cameras
- Geometry
- Geometry addons
- Lofts and clipping groups
- Sculptor
- Procedural generators
- Meshes and assets
- Lines
- Points and sprites
- Helpers
- Scene objects
- Animated objects
- Raycasting
- Curves and paths
- Animation
- Skinning
- Materials
- Node materials
- TSL functions
- Lights
- Lighting addons
- Fog
- Textures
- Renderer
- Renderer hooks and material flags
- Rasterization
- GPU backend
- Render target
- Post-processing
- GPU computation
- Compute nodes
- Progressive light map
- Gaussian splats
- Voxel global illumination
- SVG renderer
- Image files
- Image decode queue
- Model files
- More model files
- Exporters
- Scene JSON
- Windowing and controls
- C ABI and hosts
- Math
- Norm consumers
- Math addons
- Units
- Coverage tool
- Commands
- Examples
- Benchmarks
- Extensions
- Shared physics
- Physics query snapshots
- Continuous collision
- Tick force restoration
- Water
- Vegetation
- Village
- Streamline
- Animals
- Buildings
- CARLA
- Femur
- Tibia
- Fibula
- Patella
- Knee
- Muscles
- Vessels
- Lymph
- Nerves
- Integument
- Leg
- Foot
- Pelvis
- Torso
- Arm
- Hand
- Head
- Genome
- Game humanoid
- Humanoid fidelity
- Mixamo animations
- Audio-aligned game faces
- Converted assets
- Mesh quality
- Garments
- Outfits and armor
- Cloth and spring bones
- Attire estimates
- Segment inertia
Explanation
- Software rasterizer
- Scene graph as an array
- Linear color
- Normals are not colors
- Perspective correction
- Fixed-point coverage
- Per-fragment shading
- Mipmaps
- Sorted transparency
- Types and checks
- Render hooks as a trait
- Shared CPU and GPU code
- Bands and threads
- PNG structure
- Extensions beside the port
- Scans as building models
- CUDA teardown hang
- Mojo compiler hang