Skip to content

Bindings

The Zig and Rust packages build on the C library.

Add Zig package

Zig projects can depend on the library with the Zig package manager, which builds the C library from source without CMake. The package requires Zig 0.17. Add it to your build.zig.zon with:

zig fetch --save git+https://github.com/AcademySoftwareFoundation/openqmc#main

Then import the oqmc module in your build.zig. Importing the module also links the library.

const openqmc = b.dependency("openqmc", .{
    .target = target,
    .optimize = optimize,
    .sampler = .pmjbn,
});
exe.root_module.addImport("oqmc", openqmc.module("oqmc"));

The module is translated from the C header, so the function definitions directly match the C API:

const oqmc = @import("oqmc");

const cache = try gpa.alignedAlloc(u8, .of(u32), oqmc.oqmc_sampler_cache_size());
defer gpa.free(cache);
oqmc.oqmc_sampler_initialise_cache(cache.ptr);

const pixel = oqmc.oqmc_sampler_create(x, y, 0, index, cache.ptr);
const domain = oqmc.oqmc_sampler_new_domain(&pixel, 0);

var sample: [2]f32 = undefined;
oqmc.oqmc_sampler_draw_sample_f(&domain, sample.len, &sample);

The package options are:

  • sampler: Sets the sampler wrapped by the library. Option values can be pmj, pmjbn, sobol, sobolbn, lattice or latticebn. Default value is pmjbn. The package build script exports the Sampler type, so your build script can declare a matching option.
  • shared: Builds a shared library instead of the default static library, for use from C and C++. Option values can be true or false. Default value is false.

The library needs no C or C++ library, so it also builds for freestanding targets, such as zig build -Dtarget=wasm32-freestanding.

A working example is in the examples/zig directory, and a WASM (freestanding) example, run with Wasmtime, is in the examples/wasm directory. Both are continuously validated.

Add Rust package

Rust projects can depend on the openqmc package with Cargo, which builds the C library from source without CMake. The package requires Rust 1.85. Add it to your Cargo.toml with:

cargo add openqmc --git https://github.com/AcademySoftwareFoundation/openqmc --branch main --features sobol

The crate wraps the C API in a safe Rust API, so no unsafe code is needed. Samplers borrow the cache, so they can't outlive it:

let cache = openqmc::Cache::new();

let pixel = cache.sampler(x, y, 0, index);
let domain = pixel.new_domain(0);

let sample: [f32; 2] = domain.draw_sample_f32();

The sampler wrapped by the library is set with one of the features pmj, pmjbn, sobol, sobolbn, lattice or latticebn. With no feature the default is pmjbn. Libraries should leave the sampler to the final binary.

A working example is in the examples/rust directory, and is continuously validated.