Crate vee_wgpu

Crate vee_wgpu 

Source
Expand description

A library for using vfl to render Caricatures cross-platform. To use this library to render a Char from vfl, implement ProgramState on your project’s State method — that is the method that contains handles like wgpu::Device.

If you don’t need to work in real time, you might want to just use this library’s HeadlessRenderer. If you need more help integrating this library, try reading the source of lightweight_viewer for examples.

§Example

use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::rc::Rc;
use wgpu::wgt::CommandEncoderDescriptor;
use vee_wgpu::draw::CharModel;
use vee_wgpu::ProgramState;
use vee_wgpu::texture::TextureBundle;
use vee_parse::{BinRead, NxCharInfo};
use vee_resources::shape::ResourceShape;
use vee_resources::tex::ResourceTexture;

pub struct ResourceData {
    pub(crate) texture_header: ResourceTexture,
    pub(crate) shape_header: ResourceShape,
    pub(crate) texture_data: Rc<Vec<u8>>,
    pub(crate) shape_data: Rc<Vec<u8>>,
}

pub struct State {
    device: wgpu::Device,
    queue: wgpu::Queue,
    camera_bgl: wgpu::BindGroupLayout,
    camera_bg: wgpu::BindGroup,
    surface_fmt: wgpu::TextureFormat,
    depth_texture: TextureBundle,
    resource_data: ResourceData,
    char: String,
}

// If you bug me enough about this, I might just make a macro for this.
impl ProgramState for State {
    fn device(&self) -> wgpu::Device {
        self.device.clone()
    }

    fn queue(&self) -> wgpu::Queue {
        self.queue.clone()
    }

    fn camera_bgl(&self) -> &wgpu::BindGroupLayout {
        &self.camera_bgl
    }

    fn camera_bg(&self) -> &wgpu::BindGroup {
        &self.camera_bg
    }

    fn surface_fmt(&self) -> wgpu::TextureFormat {
        self.surface_fmt
    }

    fn depth_texture(&self) -> &TextureBundle {
        &self.depth_texture
    }

    fn texture_header(&self) -> ResourceTexture {
        self.resource_data.texture_header
    }

    fn shape_header(&self) -> ResourceShape {
        self.resource_data.shape_header
    }

    fn texture_data(&self) -> Rc<Vec<u8>> {
        self.resource_data.texture_data.clone()
    }

    fn shape_data(&self) -> Rc<Vec<u8>> {
        self.resource_data.shape_data.clone()
    }
}

impl State {
    fn texture_view(&mut self) -> wgpu::TextureView {
        // You'll need to render TO something. This is where you would pull in a dependency
        // like `winit` for drawing to a window, or draw to an image and save it
        // if you don't need to work in real time.
        todo!()
    }

    fn render(&mut self) -> Result<(), Box<dyn Error>> {
        let texture_view = self.texture_view();

        let mut encoder = self.device.create_command_encoder(&Default::default());

        let char = NxCharInfo::read(&mut BufReader::new(File::open(&self.char)?))?;
        let mut char = CharModel::new(self, &char, &mut encoder);
        char.render(self, &texture_view, &mut encoder);

        let command_buffer = encoder.finish();
        // Present here

        Ok(())
    }
}

Modules§

draw
Drawing models and textures.
headless
Constructs for rendering without a surface (headlessly) i.e. on a server.
texture
Holding texture data in GPU VRAM.

Traits§

ProgramState
wgpu requires a lot of state. Implement this on your state structure to use this library’s functions.

Type Aliases§

Model3d
A 3d model.