18 releases

0.3.0 Feb 29, 2024
0.3.0-alpha.1 Jan 26, 2024
0.2.2 Aug 17, 2023
0.1.10 May 12, 2023
0.1.1 Oct 22, 2022

#54 in Graphics APIs

Download history 6/week @ 2024-01-07 3/week @ 2024-01-21 26/week @ 2024-02-18 293/week @ 2024-02-25 73/week @ 2024-03-03 21/week @ 2024-03-10 223/week @ 2024-03-31

247 downloads per month

MIT license

200KB
6K SLoC

Dunge

Simple and portable 3d render based on WGPU.

Features

  • 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
#[repr(C)]
#[derive(Vertex)]
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.] },
    ]);

    cx.make_mesh(&VERTS)
};

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
    Then::Run
};

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]);

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

Now you can run our application and see the window:

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

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

cargo run -p window

Examples

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, make sure wasm-pack is installed and then run:

cargo xtask <example_name>

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

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

Dependencies

~9–51MB
~845K SLoC