Skip to content

Repository files navigation

Goldy Logo

Goldy: Rust GPU runtime

License: MIT

Goldy is a cross-platform, opinionated GPGPU and Graphics library for modern hardware written entirely in Rust with bindings in Python, C++, and dotnet and backends for Vulkan, Metal, D3D12, wgpu, and CUDA. Shaders can be authored in Slang (full support) or Rust (partial support).

Goldy realizes an abstract machine (https://koubaa.github.io/goldy/fondaco/specification.html) that can be said to be both data-oriented and functional.

The machine model admits schemes, or graphs of computations over data and does not provide any synchronization to user-space. Instead, synchronization is derived inside the runtime by data access patterns and submission order, using a ledger. The runtime modifies shaders to pass data it uses to its entrypoint, and introspects the IR of shaders as part of its execution. Operations on data not owned by the abstract machine happen at the edge of the runtime in exchanges.

Shader entry points are modified so that shaders see data they operate on according to the ownership model of the invocation.

GPU memory is completely virtualized, and the runtime may relocate objects between dispatches or shader invocations, as long as it does not change the behavior of the program.

Quick example: compute-to-surface

#[goldy::gpu]
struct Uniforms {
    width: u32,
    height: u32,
    time: f32,
}

#[goldy::compute(workgroup_size = [8, 8, 1])]
fn plasma(uniforms: &[Uniforms], output: gpu::DirectSpatial<gpu::Float4>) {
    let tid = gpu::global_id();
    let u: Uniforms = uniforms[0];
    if tid.x >= u.width || tid.y >= u.height {
        return;
    }

    let uv = gpu::float2(tid.x as f32 / u.width as f32, tid.y as f32 / u.height as f32);
    let mut p = uv * 2.0 - 1.0;
    p.x *= u.width as f32 / u.height as f32;

    let mut v = 0.0;
    v += gpu::sin(p.x * 6.0 + u.time);
    v += gpu::sin(p.y * 6.0 + u.time * 1.3);
    v += gpu::sin((p.x + p.y) * 4.0 + u.time * 0.7);
    v += gpu::sin(gpu::length(p) * 8.0 - u.time * 2.0);
    v *= 0.25;

    let col = gpu::float3(
        0.5 + 0.5 * gpu::sin(v * 3.14159 + 0.0),
        0.5 + 0.5 * gpu::sin(v * 3.14159 + 2.094),
        0.5 + 0.5 * gpu::sin(v * 3.14159 + 4.188),
    );
    output[tid.xy] = gpu::float4(col.x, col.y, col.z, 1.0);
}

let instance = Instance::new()?;
let runtime = instance
    .request_adapter(&RequestAdapterOptions::default())?
    .request_runtime(&RuntimeDescriptor::default())?;
let ctx = runtime.create_context()?;

// Scattered buffer: kernel reads uniforms[0] (width, height, time).
let uniforms_data = [Uniforms { width, height, time: 0.0 }];
let uniforms = runtime.acquire_buffer_with_data(&uniforms_data, BufferKind::Scattered)?;

// Window swapchain exchange; later bind_destination leases a drawable for the kernel.
let surface = SurfaceExchange::new(&ctx, &window, SurfaceConfig::default())?;

// Compile the Rust kernel to [goldy_compute] Slang (or hit the shader cache).
let kernel = plasma::Kernel::prepare(&runtime)?;


let mut scheme = Scheme::new(&ctx);
// Lease is the drawable the kernel writes; transaction is how the frame is presented.
let (lease, present) = surface.bind_destination(&mut scheme)?;
kernel
    .record(&mut scheme, "render", &uniforms, &lease)
    .over_2d(width, height); // workgroups: ceil(width/8) × ceil(height/8) × 1

// Each frame: submit the recorded graph, then present the claimed drawable.
let mut submission = scheme.submit()?;
(&mut submission >> &present).take()?;

Installation

[dependencies]
goldy = "0.2"

Slang is embedded at build time and extracted at runtime — application developers need not install Slang separately. Set GOLDY_SLANG_PATH only to override.

Release packaging and shader debugging notes live in the GitHub repo.

Platforms

Platform Backend Window surfaces
Windows DX12 (default), Vulkan Yes
Linux Vulkan Wayland (X11 not supported)
macOS Metal Yes

Override backend: GOLDY_BACKEND=vulkan|dx12|metal.

Minimum hardware: Vulkan 1.4+, DX12 with Enhanced Barriers, Metal Argument Buffers Tier 2+. See Target Hardware.

Documentation

License

MIT — see LICENSE.

Author

Mohamed Koubaa

About

Runtime for GPU accelerated compute

Resources

Stars

11 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages