sift-wgpu
A high-performance implementation of SIFT (David G. Lowe's Scale-Invariant Feature Transform) in Rust with CPU and GPU (WebGPU/wgpu) backends. Works out of the box natively and in WASM; ongoing work focuses on further performance improvements.
Features
- Multiple backends: CPU, WebGPU (GPU), WebGPU V2 (optimized texture-based pipeline)
- Automatic fallback: GPU with automatic CPU fallback if GPU is unavailable
- Full SIFT pipeline: Gaussian pyramid, DoG, extrema detection, orientation assignment, 128-dimensional descriptors
- Visualization: Built-in keypoint drawing on images
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Usage
Library API
use open;
use ;
Available Backends
| Backend | Description |
|---|---|
SiftBackend::Cpu |
Pure CPU implementation |
SiftBackend::WebGpu |
GPU implementation using wgpu |
SiftBackend::WebGpuV2 |
Optimized GPU pipeline (texture-based) |
SiftBackend::WebGpuWithCpuFallback |
Try GPU, fallback to CPU on failure (default) |
Custom Parameters
use Sift;
let sift = new;
Visualization
use ;
use ;
let img = open?;
let sift = default;
let = sift.detect_and_compute;
// Draw keypoints on image
let result = draw_keypoints_to_image;
result.save?;
CLI
# Build
# Run with default backend (auto GPU/CPU fallback)
# Specify backend
# Or use environment variable
SIFT_BACKEND=gpuv2
Web / WASM Support
The library supports compilation to WebAssembly (WASM) for use in browsers. It includes both a CPU backend (single-threaded) and a WebGPU backend.
Prerequisites
- Rust toolchain
wasm-pack
Building for Web
# optional: --out-dir to specify output folder
Running the Web Demo
The repository includes a webcam demo in the www folder.
-
Build the WASM package:
-
Link the package to the web folder:
(Or manually copy the
pkgfolder intowwwif you are on Windows) -
Serve the
wwwfolder with a local server (HTTPS or localhost required for Camera API):# Python # Node -
Open
http://localhost:8000in a browser with WebGPU support (Chrome 113+, Edge).- If using
localhost, Camera API works. - If using a network IP (e.g. on mobile), you must use HTTPS (e.g. via
ngrok) or the camera will fail.
- If using
Web API Usage
import init from './pkg/sift.js';
;
Performance Note
- CPU: Uses optimized SIMD (via
wasm-opt) but is single-threaded in the browser. Fast for 320p/480p, slower for HD. - WebGPU: High initialization cost but scales well with resolution (720p+). Requires optimized texture pipeline (V2) which is the default in the web binding.
Benchmarks
This repository includes a Python-based benchmark suite to compare CPU and GPU backends.
Prerequisites
- uv (fast Python package manager)
- Rust toolchain
Running Benchmarks
# Build the release binary first
# Run benchmarks using uv (handles dependencies automatically)
This will run SIFT on different backends and resolutions, generating a performance comparison.
CLI Options
Usage: sift [--backend cpu|gpu|gpuv2|gpu-fallback] <image_path>
Options:
--backend cpu Use CPU backend
--backend gpu Use GPU (WebGPU) backend
--backend gpuv2 Use GPU V2 (optimized) backend
--backend gpu-fallback Use GPU with CPU fallback (default)
-h, --help Show help
GPU Example
use ;
async
Project Structure
src/
├── lib.rs # Public API exports
├── main.rs # CLI application
├── sift.rs # Core SIFT implementation (CPU)
├── keypoints.rs # KeyPoint struct
├── gpu_sift.rs # GPU backend V1
├── gpu_sift_v2.rs # GPU backend V2 (optimized)
└── shaders/ # WGSL compute shaders
├── gpu_blur.wgsl
├── gpu_dog.wgsl
├── gpu_extrema.wgsl
├── gpu_orientation.wgsl
├── gpu_descriptor.wgsl
└── ...
References
- Lowe, D. G. (2004). Distinctive image features from scale-invariant keypoints. International Journal of Computer Vision, 60(2), 91-110.
- Lowe, D. G. (1999). Object recognition from local scale-invariant features. ICCV 1999.
- Lowe, D. G. (2004). SIFT: The scale invariant feature transform.
TODO
- Implement SIFT (CPU)
- Add support for different image types
- Add tests
- Add examples
- Add WebGPU support (V1 & V2)
- Add WASM support
- Add Web Demo with Camera
- Add documentation
- Add benchmarks
License
MIT
Legal Notice
SIFT was patented, but the patent has expired. This repo is primarily meant for educational purposes, but feel free to use the code for any purpose, commercial or otherwise. All I ask is that you cite or share this repo.