9 releases

0.3.1 Apr 25, 2024
0.3.0 Feb 29, 2024
0.3.0-alpha.1 Jan 26, 2024
0.2.2 Aug 17, 2023

#887 in Procedural macros

Download history 7/week @ 2024-01-23 1/week @ 2024-02-13 11/week @ 2024-02-20 353/week @ 2024-02-27 36/week @ 2024-03-05 3/week @ 2024-03-12 17/week @ 2024-03-26 77/week @ 2024-04-02 134/week @ 2024-04-23 7/week @ 2024-04-30

141 downloads per month
Used in dunge

MIT license

568 lines


Simple and portable 3d render based on WGPU.


  • Simple and flexible API
  • Customizable vertices, groups and instances
  • Shader code described as a single rust function
  • High degree of typesafety with minimal runtime checks
  • Desktop, WASM and (later) Android support
  • Optional built-in window and event loop

Application area

Currently the library is for personal use only. Although, over time I plan to stabilize API so that someone could use it for their tasks.

Getting Started

To start using the library add it to your project:

cargo add dunge -F winit

Specify the winit feature if you need to create a windowed application. Although this is not necessary, for example, you can simply draw a scene directly to the image in RAM.

So what if you want to draw something on the screen? Let's say you want to draw a simple colored triangle. Then start by creating a vertex type. To do this, derive the Vertex trait for your struct:

use dunge::prelude::*;

// Create a vertex type
struct Vert {
    pos: [f32; 2],
    col: [f32; 3],

To render something on GPU you need to program a shader. In dunge you can do this via a normal (almost) rust function:

// Create a shader program
let triangle = |vert: sl::InVertex<Vert>| {
    // Describe the vertex position:
    // Take the vertex data as vec2 and expand it to vec4
    let place = sl::vec4_concat(vert.pos, sl::vec2(0., 1.));

    // Then describe the vertex color:
    // First you need to pass the color from
    // vertex shader stage to fragment shader stage
    let fragment_col = sl::fragment(vert.col);

    // Now create the final color by adding an alpha value
    let color = sl::vec4_with(fragment_col, 1.);

    // As a result, return a program that describes how to
    // compute the vertex position and the fragment color
    sl::Out { place, color }

As you can see from the snippet, the shader requires you to provide two things: the position of the vertex on the screen and the color of each fragment/pixel. The result is a triangle function, but if you ask for its type in the IDE you may notice that it is more complex than usual:

impl Fn(InVertex<Vert>) -> Out<Ret<Compose<Ret<ReadVertex, Vec2<f32>>, Ret<NewVec<(f32, f32), Vs>, Vec2<f32>>>, Vec4<f32>>, Ret<Compose<Ret<Fragment<Ret<ReadVertex, Vec3<f32>>>, Vec3<f32>>, f32>, Vec4<f32>>>

That's because this function doesn't actually compute anything. It is needed only to describe the method for computing what we need on GPU. During shader instantiation, this function is used to compile an actual shader. However, this saves us from having to write the shader in wgsl and allows to typecheck at compile time. For example, dunge checks that a vertex type in a shader matches with a mesh used during rendering. It also checks types inside the shader itself.

Now let's create the dunge context, window and other necessary things:

// Create the dunge context with a window
let window = dunge::window().await?;
let cx = window.context();

// You can use the context to manage dunge objects.
// Create a shader instance
let shader = cx.make_shader(triangle);
// And a layer for drawing a mesh on it
let layer = cx.make_layer(&shader, window.format());

You may notice the context creation requires async. This is WGPU specific, so you will have to add your favorite async runtime in the project.

Also create a triangle mesh that we're going to draw:

// Create a mesh from vertices
let mesh = {
    use dunge::mesh::MeshData;

    const VERTS: MeshData<'static, Vert> = MeshData::from_verts(&[
        Vert { pos: [-0.5, -0.5], col: [1., 0., 0.] },
        Vert { pos: [ 0.5, -0.5], col: [0., 1., 0.] },
        Vert { pos: [ 0. ,  0.5], col: [0., 0., 1.] },


Now to run the application we need two last things: handlers. One Update that is called every time before rendering and is used to control the render objects and manage the main event loop:

// Describe the `Update` handler
let upd = |ctrl: &Control| {
    for key in ctrl.pressed_keys() {
        // Exit by pressing escape key
        if key.code == KeyCode::Escape {
            return Then::Close;

    // Otherwise continue running

We don't do anything special here, we just check is Esc pressed and end the main loop if necessary. Note that this handler is only needed to use a window with the winit feature.

Second Draw is used directly to draw something in the final frame:

// Describe the `Draw` handler
let draw = |mut frame: Frame| {
    use dunge::color::Rgba;

    // Create a black RGBA background
    let bg = Rgba::from_bytes([0, 0, 0, !0]);

        // Select a layer to draw on it
        .layer(&layer, bg)
        // The shader has no bindings, so call empty bind
        // And finally draw the mesh

Now you can run our application and see the window:

// Run the window with handlers
window.run_local(dunge::update(upd, draw))?;

You can see full code from this example here and run it using:

cargo run -p window


For more examples using the window, see the examples directory. To build and run an example do:

cargo run -p <example_name>

To build and run a wasm example:

cargo xtask build <example_name>
cargo xtask serve <example_name>

If wasm-pack is already installed on the system, the build script will find it and use it to compile a wasm artifact. Otherwise, wasm-pack will be installed locally. To prevent this behavior add the no-install flag:

cargo xtask --no-install build <example_name>

Eventually it will start a local server and you can open http://localhost:3000 in your browser to see the application running. Only WebGPU backend is supported for the web platform, so make sure your browser supports it.

Also see the test directory for small examples of creation a single image.


~18K SLoC