Wgpu blitting chars to screen

Suppose we have:

struct Color { r: u8, g: u8, b: u8 }

struct Colored_u8 { value: u8, color: Color }

my_screen: [[Colored_u8; 100]; 50]

is there a simple to use & efficient wgpu library for blitting my_sreen to a wgpu surface ?

If you are already using wgpu, then you do not need very much more than you have.

  1. Change your struct declarations to have a more convenient, guaranteed layout:

    #[derive(Clone, Copy, bytemuck::NoUninit)]
    #[repr(C)]
    struct Color { r: u8, g: u8, b: u8 }
    
    #[derive(Clone, Copy, bytemuck::NoUninit)]
    #[repr(C)]
    struct Colored_u8 { color: Color, value: u8 }
    

    Now, an array of these structs may be interpreted as a standard RGBA image (ignoring the alpha), because Colored_u8 is guaranteed (via repr(C)) to be exactly 4 bytes, r, g, b, value.

  2. Take bytemuck::bytes_of() this array and upload it as a texture, just like if it were an image.

  3. Draw 1 full-screen triangle and have the fragment shader read your texture, ignoring the alpha component.

This is more or less as efficient as it gets; there are no extraneous copies or conversions.

1 Like

Is there a crate that handles this part? Takes a font, creates sdf texture atlas, has sdf texture shader ?

Oh, I thought you meant you had an array whose elements were pixels.

I don't have any advice for high-quality text rendering. Sorry.

bump; in case anyone with a good solution missed the question

note: the above Colored_u8 is NOT a pixel, it is a colored u8 character, i.e. '0' - '9', 'a' - 'z', 'A' -'Z', ...

The pix crate provides a generic compositor/blitter that you can use for texture-to-texture compositing on the CPU side (prior to sending the texture data to the GPU). This is quite a low-level option and has nothing to do with text or even wgpu. But it's a good building block for making what you are asking for.

Some options for more text-oriented crates (which generally operate in terms of char instead of u8) are:

These are all overkill if the only thing you want is rudimentary fixed-width pixel-font printing to a texture. That's why I initially recommended pix.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.