Crate wrld

source · []
Expand description

Description

wrld is a easy, fast, and more secure way of writing buffer descriptor for wgpu renderpipeline

How it works ?

Wrld take derived structure and crate a VertexBufferLayout according to the attribute passed.

Wrld come with 3 macros :

  • Desc
  • DescInstance
  • BufferData

Desc

Desc derive macro allow to create a VertexBufferLayout from a structure.

Example
#[repr(C)]
#[derive(wrld::Desc)]
struct Vertex {
    #[f32x2(0)] position: [f32; 2],
    #[f32x4(1)] color: [f32; 4]
}
Thing to know
  • Desc will not handle data transformation. (bytemuck slice)
  • Desc does not handle chaotic structure.

DescInstance

DescInstance is the same as the Desc macro. The only difference with DescInstance is that it change the vertex step mode but the result is the same.

Example
#[derive(wrld::DescInstance)]
struct Vertex {
    #[f32x2(0)] position: [f32; 2],
    #[f32x4(1)] color: [f32; 4]
}

Chaotic and ordered structure.

Before aboarding the next macro. We need to know what is the difference between chaotic and ordered structure type.

Chaotic structure

A chaotic structure is a structure that have attributes put anywhere in the struct

Example
#[derive(wrld::Desc)]
struct Vertex {
    #[f32x2(0)] position: [f32; 2],
    data: String,
    #[f32x4(1)] color: [f32; 4]
}

If you try to cast slice with bytemuck on this structure. It will result in this.

struct Vertex {
    position: [f32; 2],
    data: String
}

And this is not good, because this is not the data that we are expecting to get from bytemuck.

A simple fix to that is to create a ordered structure and have one or multiple function that convert this structure to a ordered one or to sort this one.

Ordered structure

A ordered structure is a structure that put the attribute field on top of the structure.

Example
#[derive(wrld::Desc)]
struct Vertex {
    #[f32x2(0)] position: [f32; 2],
    #[f32x4(1)] color: [f32; 4],
    data: String
}

If you try to cast slice with bytemuck on this structure. It will result in this.

struct Vertex {
    position: [f32; 2],
    color: [f32; 4]
}

While this technique work. It could be annoying to rewrite thousand and thousand of structure just to fix this. This is why the next macro was created for.

BufferData

BufferData create a ordered structure from a chaotic structure. It come with bytemuck derive macro out of the box.

Example
#[repr(C)]
#[derive(wrld::Desc, wrld::BufferData)]
struct Vertex {
    uv: [f32; 2],
    #[f32x2(0)] position: [f32; 2],
    data: String,
    #[f32x4(1)] color: [f32; 4]
}

Derive Macros

A macro to handle any type of chaotic structure.

Desc is a proc derive macro that allow you to describe a structure as a description to pass to a renderpipeline.

DescInstance is the same as Desc. The only difference is that it change the step mode to Instance instead of Vertex