2021-01-06

Wgpu: getting started

Overview

After all the setup ... The draw call renders to the currently assigned frame buffer in the SwapChain and then the swap call handles displaying (or not displaying) of that frame that was just drawn.

Excessive use of the word descriptor? Descriptor is used to define how to create something. E.g SwapChainDescriptor describes how the SwapChain should be created.

Misc notes

Coordinates in wgpu places 0,0 in the centre of the screen.

Top left = -1, 1 Bottom right = 1, -1

Note however that texture coordinates starts at 0,0 for top left, and ends at 1,1 for bottom right.

Labels are just debug information.

VertexBufferDescriptor

A vertex buffer descriptor describes how the gpu interprets a vertex buffer, a struct defined by you.

 - stride -    - stride -
|x|y|z|r|g|b| |x|y|z|r|g|b

stride: is the number of bytes to move from one vertex to another: I.e if vertices are 4 bytes in size, and placed contiguously in memory then stride = 4.

step_mode: we default to stepmode::Vertex as this means move one vertex at a time.

attributes: is a slice of VertexAttributeDescriptor that contains information about the fields of the struct. For instance: colour and position.

Example:

#[repre(C)] // repr(C) here so the compiler doesn't reorder the fields
type Position = [f32; 3];
struct Vertex {
    position: Position,
    color: [f32; 3],
}

impl Vertex {
    fn attributes() -> &[VertexAttributeDescriptor] {
        &[
            VertexAttributeDescriptor {
                offset: 0, // offset for position is zero as it's the first field
                shader_location: 0, // location of the field in the shader, not on the struct
                format: wgpu::VertexFormat::Float3,
            },
            VertexAttributeDescriptor {
                offset: size_of::<Position>(), // colour comes after position
                shader_location: 1,
                format: wgpu::VertexFormat::Float3,
            },
        ]
    }
}

SwapChainDescriptor

It's a buffer of frames. Decides which frame to draw and which to display.

SwapChainDescriptor needs to have the same size as the Device.

present_mode is either:

See images here for further detail: https://software.intel.com/content/www/us/en/develop/articles/api-without-secrets-introduction-to-vulkan-part-2.html (somewhere in the middle)

usage: is a bit flag. Make sure TextureUsage::OUTPUT_ATTACHMENT is set or it won't be possible to render the texture.

SwapChain: it's a fancy buffer

The SwapChain provides frames.

RenderPipeline

Dictionary

Frame: A frame is the name of a Texture used to render to at the end of the render_pass. Frame is the name we give to the textures that the Swapchain gives you.

Surface: Something to present the final image on, e.g a window. When the SwapChain presents a frame it is copied to the surface.

Device: A connection to the gpu.

Adapter: A Physical graphics card (used to make one or more logical Devices)

Thanks

Thanks to:

Thanks to Temporus for: https://paste.rs/2yj.md