Crate ewgpu

Source
Expand description

A wrapper for the wgpu crate.

This library provides wrappers for the wgpu crate that are typesafe and easy to use. For this Builders are provided for most of wgpu’s structs as well as wrappers with generics for structs of wgpu that hold data to prevent unintended casting. This crate also provides macros for creating vertices and a Framework that enables easy initialisation.

An example of how to render to a image:

 use ewgpu::*;
 use ewgpu::mesh::*;

 #[repr(C)]
 #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
 struct Consts{
      pub color: [f32; 4],
 }
 use winit::{window::WindowBuilder, event_loop::EventLoop};
 
 env_logger::init();
 
 let event_loop = EventLoop::new();
 
 let mut gpu = GPUContextBuilder::new()
     .set_features_util()
     .set_limits(wgpu::Limits{
         max_push_constant_size: 128,
         ..Default::default()
     })
     .build();

 let mesh = Mesh::<Vert2>::new(&gpu.device, &Vert2::QUAD_VERTS,
 &Vert2::QUAD_IDXS).unwrap();

 let vshader = VertexShader::from_src(&gpu.device, "
 #version 460
 #if VERTEX_SHADER
         
 layout(location = 0) in vec2 i_pos;
 layout(location = 1) in vec2 i_uv;
         
 layout(location = 0) out vec2 f_pos;
 layout(location = 1) out vec2 f_uv;
         
 void main(){
     f_pos = i_pos;
     f_uv = i_uv;
         
     gl_Position = vec4(i_pos, 0.0, 1.0);
 }
         
 #endif
 ", None).unwrap();

 let fshader = FragmentShader::from_src(&gpu.device, "
 #version 460
 #if FRAGMENT_SHADER
         
 layout(location = 0) in vec2 f_pos;
 layout(location = 1) in vec2 f_uv;
         
 layout(location = 0) out vec4 o_color;
         
 layout(push_constant) uniform PushConstants{
     vec4 color;
 } constants;
         
 void main(){
     o_color = vec4(1.0, 0., 0., 1.);
 }
         
 #endif
 ", None).unwrap();

 let layout = pipeline_layout!(&gpu.device,
     bind_groups: {},
     push_constants: {
         Consts,
     }
 );

 let pipeline = RenderPipelineBuilder::new(&vshader, &fshader)
     .push_drawable_layouts::<Mesh<Vert2>>()
     .push_target_replace(wgpu::TextureFormat::Rgba8Unorm)
     .set_layout(&layout)
     .build(&gpu.device);
 
 gpu.encode_img([1920, 1080], |gpu, dst, encoder|{
     let mut rpass = RenderPassBuilder::new()
         .push_color_attachment(dst.color_attachment_clear())
         .begin(encoder, None);

     let mut rpass_ppl = rpass.set_pipeline(&pipeline);

     let consts = Consts{
         color: [1.0, 0.0, 0.0, 1.0]
     };

     rpass_ppl.set_push_const(0, &consts);

     mesh.draw(&mut rpass_ppl);
 });

Re-exports§

pub extern crate ewgpu_macros;
pub use self::binding::*;
pub use self::buffer::*;
pub use self::framework::*;
pub use self::pipeline::*;
pub use self::render_target::*;
pub use self::texture::*;
pub use self::uniform::*;
pub use self::vert::*;
pub use self::push_constants::*;
pub use self::shader::*;
pub use context::*;
pub use crate::drawable::*;

Modules§

binding
buffer
context
drawable
framework
mesh
pipeline
push_constants
render_target
shader
texture
uniform
vert

Macros§

bind_group_layouts
pipeline_layout
A macro for generating pipeline layouts. TODO: find way to use with and without visibility qualifier.
push_constant_ranges

Derive Macros§

BindGroupContent
A macro to derive the BindGroupContent trait from a struct whos fields implement BindGroupContent.
Inst
A Macro for deriving A instance vector from a struct:
Vert