Initial wgpu abstraction

This commit is contained in:
Werner
2021-11-09 21:07:16 -03:00
parent 15e8989bef
commit b23bc27f55
25 changed files with 775 additions and 209 deletions

View File

@ -0,0 +1,14 @@
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct IndexBuffer {
pub label: Cow<'static, str>,
pub content: Vec<u8>,
}
impl IndexBuffer {
pub fn GetLength(&self) -> u32 {
self.content.len() as u32
}
}

View File

@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};
#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum IndexFormat {
/// Indices are 16 bit unsigned integers.
UInt16 = 0,
/// Indices are 32 bit unsigned integers.
UInt32 = 1,
}
impl Default for IndexFormat {
fn default() -> Self {
Self::UInt32
}
}

View File

@ -0,0 +1,20 @@
use serde::{Deserialize, Serialize};
#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum PolygonMode {
/// Polygons are filled
Fill = 0,
/// Polygons are drawn as line segments
Line = 1,
/// Polygons are drawn as points
Point = 2,
}
impl Default for PolygonMode {
fn default() -> Self {
Self::Fill
}
}

154
Source/Render/Renderer.rs Normal file
View File

@ -0,0 +1,154 @@
use super::{IndexBuffer, UniformBuffer, VertexBuffer};
use crate::Shader::Shader;
use anyhow::Result;
use wgpu::util::DeviceExt;
use winit::window::Window;
pub struct Renderer {
pub surface: wgpu::Surface,
pub window: Window,
pub config: wgpu::SurfaceConfiguration,
pub device: wgpu::Device,
pub queue: wgpu::Queue,
}
impl Renderer {
pub async fn New(window: Window) -> Result<Self> {
let size = window.inner_size();
let instance = wgpu::Instance::new(wgpu::Backends::all());
let surface = unsafe { instance.create_surface(&window) };
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
.await
.unwrap();
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
},
None,
)
.await
.unwrap();
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface.get_preferred_format(&adapter).unwrap(),
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
};
surface.configure(&device, &config);
Ok(Self {
surface,
window,
config,
device,
queue,
})
}
pub fn SubmitShader(&self, shader: &Shader) -> wgpu::ShaderModule {
let src = shader.source.WgslToString().unwrap();
self.device
.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: Some(&shader.label),
source: wgpu::ShaderSource::Wgsl(src.as_str().into()),
})
}
pub fn SubmitVertexBuffer(&self, vertex_buffer: &VertexBuffer) -> wgpu::Buffer {
self.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(&vertex_buffer.label),
contents: vertex_buffer.content.as_ref(),
usage: wgpu::BufferUsages::VERTEX,
})
}
pub fn SubmitIndexBuffer(&self, index_buffer: &IndexBuffer) -> wgpu::Buffer {
self.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(&index_buffer.label),
contents: index_buffer.content.as_ref(),
usage: wgpu::BufferUsages::INDEX,
})
}
pub fn SubmitUniformBuffer(&self, uniform_buffer: &UniformBuffer) -> wgpu::Buffer {
self.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(&uniform_buffer.label),
contents: uniform_buffer.content.as_ref(),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
})
}
pub fn Draw(
&self,
pipeline: &wgpu::RenderPipeline,
vertex_buffer: &wgpu::Buffer,
index_buffer: &wgpu::Buffer,
num_indices: u32,
) -> Result<()> {
let output = self.surface.get_current_texture()?;
let view = output
.texture
.create_view(&wgpu::TextureViewDescriptor::default());
let mut encoder = self
.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("RENDER_ENCODER"),
});
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("RENDER_PASS"),
color_attachments: &[wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0,
}),
store: true,
},
}],
depth_stencil_attachment: None,
});
render_pass.set_pipeline(pipeline);
render_pass.set_vertex_buffer(0, vertex_buffer.slice(..));
render_pass.set_index_buffer(index_buffer.slice(..), wgpu::IndexFormat::Uint16);
render_pass.draw_indexed(0..num_indices, 0, 0..1);
}
self.queue.submit(std::iter::once(encoder.finish()));
output.present();
Ok(())
}
pub fn Resize(&mut self, width: u32, height: u32) {
self.config.width = width;
self.config.height = height;
self.surface.configure(&self.device, &self.config);
}
}

23
Source/Render/StepMode.rs Normal file
View File

@ -0,0 +1,23 @@
use serde::{Deserialize, Serialize};
#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum StepMode {
Vertex = 0,
Instance = 1,
}
impl Default for StepMode {
fn default() -> Self {
StepMode::Vertex
}
}
impl From<StepMode> for wgpu::VertexStepMode {
fn from(step_mode: StepMode) -> Self {
match step_mode {
StepMode::Vertex => wgpu::VertexStepMode::Vertex,
StepMode::Instance => wgpu::VertexStepMode::Instance,
}
}
}

View File

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct UniformBuffer {
pub label: Cow<'static, str>,
pub content: Vec<u8>,
}

View File

@ -1,3 +1,5 @@
use super::VertexBufferLayout;
pub trait Vertex {
fn GetDescriptor<'a>() -> wgpu::VertexBufferLayout<'a>;
fn GetLayout() -> VertexBufferLayout;
}

View File

@ -0,0 +1,21 @@
use super::VertexFormat;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct VertexAttribute {
pub label: Cow<'static, str>,
pub format: VertexFormat,
pub offset: usize,
pub shader_location: u32,
}
impl From<VertexAttribute> for wgpu::VertexAttribute {
fn from(attr: VertexAttribute) -> Self {
wgpu::VertexAttribute {
offset: attr.offset as wgpu::BufferAddress,
shader_location: attr.shader_location,
format: attr.format.into(),
}
}
}

View File

@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct VertexBuffer {
pub label: Cow<'static, str>,
pub content: Vec<u8>,
}

View File

@ -0,0 +1,11 @@
use super::{StepMode, VertexAttribute};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct VertexBufferLayout {
pub label: Cow<'static, str>,
pub stride: usize,
pub step_mode: StepMode,
pub attributes: Vec<VertexAttribute>,
}

View File

@ -0,0 +1,190 @@
use serde::{Deserialize, Serialize};
#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum VertexFormat {
/// Two unsigned bytes (u8). `uvec2` in shaders.
UInt8x2 = 0,
/// Four unsigned bytes (u8). `uvec4` in shaders.
UInt8x4 = 1,
/// Two signed bytes (i8). `ivec2` in shaders.
Int8x2 = 2,
/// Four signed bytes (i8). `ivec4` in shaders.
Int8x4 = 3,
/// Two unsigned bytes (u8). [0, 255] converted to float [0, 1] `vec2` in shaders.
UNorm8x2 = 4,
/// Four unsigned bytes (u8). [0, 255] converted to float [0, 1] `vec4` in shaders.
UNorm8x4 = 5,
/// Two signed bytes (i8). [-127, 127] converted to float [-1, 1] `vec2` in shaders.
Norm8x2 = 6,
/// Four signed bytes (i8). [-127, 127] converted to float [-1, 1] `vec4` in shaders.
Norm8x4 = 7,
/// Two unsigned shorts (u16). `uvec2` in shaders.
UInt16x2 = 8,
/// Four unsigned shorts (u16). `uvec4` in shaders.
UInt16x4 = 9,
/// Two signed shorts (i16). `ivec2` in shaders.
Int16x2 = 10,
/// Four signed shorts (i16). `ivec4` in shaders.
Int16x4 = 11,
/// Two unsigned shorts (u16). [0, 65535] converted to float [0, 1] `vec2` in shaders.
UNorm16x2 = 12,
/// Four unsigned shorts (u16). [0, 65535] converted to float [0, 1] `vec4` in shaders.
UNorm16x4 = 13,
/// Two signed shorts (i16). [-32767, 32767] converted to float [-1, 1] `vec2` in shaders.
Norm16x2 = 14,
/// Four signed shorts (i16). [-32767, 32767] converted to float [-1, 1] `vec4` in shaders.
Norm16x4 = 15,
/// Two half-precision floats (no Rust equiv). `vec2` in shaders.
Float16x2 = 16,
/// Four half-precision floats (no Rust equiv). `vec4` in shaders.
Float16x4 = 17,
/// One single-precision float (f32). `float` in shaders.
Float32 = 18,
/// Two single-precision floats (f32). `vec2` in shaders.
Float32x2 = 19,
/// Three single-precision floats (f32). `vec3` in shaders.
Float32x3 = 20,
/// Four single-precision floats (f32). `vec4` in shaders.
Float32x4 = 21,
/// One unsigned int (u32). `uint` in shaders.
UInt32 = 22,
/// Two unsigned ints (u32). `uvec2` in shaders.
UInt32x2 = 23,
/// Three unsigned ints (u32). `uvec3` in shaders.
UInt32x3 = 24,
/// Four unsigned ints (u32). `uvec4` in shaders.
UInt32x4 = 25,
/// One signed int (i32). `int` in shaders.
Int32 = 26,
/// Two signed ints (i32). `ivec2` in shaders.
Int32x2 = 27,
/// Three signed ints (i32). `ivec3` in shaders.
Int32x3 = 28,
/// Four signed ints (i32). `ivec4` in shaders.
Int32x4 = 29,
/// One double-precision float (f64). `double` in shaders.
Float64 = 30,
/// Two double-precision floats (f64). `dvec2` in shaders.
Float64x2 = 31,
/// Three double-precision floats (f64). `dvec3` in shaders.
Float64x3 = 32,
/// Four double-precision floats (f64). `dvec4` in shaders.
Float64x4 = 33,
}
impl VertexFormat {
/// Returns the size in bytes of this format.
pub const fn GetSize(&self) -> u64 {
match *self {
Self::UInt8x2 => 2,
Self::UInt8x4 => 4,
Self::Int8x2 => 2,
Self::Int8x4 => 4,
Self::UNorm8x2 => 2,
Self::UNorm8x4 => 4,
Self::Norm8x2 => 2,
Self::Norm8x4 => 4,
Self::UInt16x2 => 2 * 2,
Self::UInt16x4 => 4 * 4,
Self::Int16x2 => 2 * 2,
Self::Int16x4 => 2 * 4,
Self::UNorm16x2 => 2 * 2,
Self::UNorm16x4 => 2 * 4,
Self::Norm16x2 => 2 * 2,
Self::Norm16x4 => 2 * 4,
Self::Float16x2 => 2 * 2,
Self::Float16x4 => 2 * 4,
Self::Float32 => 4,
Self::Float32x2 => 4 * 2,
Self::Float32x3 => 4 * 3,
Self::Float32x4 => 4 * 4,
Self::UInt32 => 4,
Self::UInt32x2 => 4 * 2,
Self::UInt32x3 => 4 * 3,
Self::UInt32x4 => 4 * 4,
Self::Int32 => 4,
Self::Int32x2 => 4 * 2,
Self::Int32x3 => 4 * 3,
Self::Int32x4 => 4 * 4,
Self::Float64 => 8,
Self::Float64x2 => 8 * 2,
Self::Float64x3 => 8 * 3,
Self::Float64x4 => 8 * 4,
}
}
}
impl From<VertexFormat> for wgpu::VertexFormat {
fn from(format: VertexFormat) -> Self {
match format {
VertexFormat::UInt8x2 => wgpu::VertexFormat::Uint8x2,
VertexFormat::UInt8x4 => wgpu::VertexFormat::Uint8x4,
VertexFormat::Int8x2 => wgpu::VertexFormat::Sint8x2,
VertexFormat::Int8x4 => wgpu::VertexFormat::Sint8x4,
VertexFormat::UNorm8x2 => wgpu::VertexFormat::Unorm8x2,
VertexFormat::UNorm8x4 => wgpu::VertexFormat::Unorm8x4,
VertexFormat::Norm8x2 => wgpu::VertexFormat::Snorm8x2,
VertexFormat::Norm8x4 => wgpu::VertexFormat::Snorm8x4,
VertexFormat::UInt16x2 => wgpu::VertexFormat::Uint16x2,
VertexFormat::UInt16x4 => wgpu::VertexFormat::Uint16x4,
VertexFormat::Int16x2 => wgpu::VertexFormat::Sint16x2,
VertexFormat::Int16x4 => wgpu::VertexFormat::Sint16x4,
VertexFormat::UNorm16x2 => wgpu::VertexFormat::Unorm16x2,
VertexFormat::UNorm16x4 => wgpu::VertexFormat::Unorm16x4,
VertexFormat::Norm16x2 => wgpu::VertexFormat::Snorm16x2,
VertexFormat::Norm16x4 => wgpu::VertexFormat::Snorm16x4,
VertexFormat::Float16x2 => wgpu::VertexFormat::Float16x2,
VertexFormat::Float16x4 => wgpu::VertexFormat::Float16x4,
VertexFormat::Float32 => wgpu::VertexFormat::Float32,
VertexFormat::Float32x2 => wgpu::VertexFormat::Float32x2,
VertexFormat::Float32x3 => wgpu::VertexFormat::Float32x3,
VertexFormat::Float32x4 => wgpu::VertexFormat::Float32x4,
VertexFormat::UInt32 => wgpu::VertexFormat::Uint32,
VertexFormat::UInt32x2 => wgpu::VertexFormat::Uint32x2,
VertexFormat::UInt32x3 => wgpu::VertexFormat::Uint32x3,
VertexFormat::UInt32x4 => wgpu::VertexFormat::Uint32x4,
VertexFormat::Int32 => wgpu::VertexFormat::Sint32,
VertexFormat::Int32x2 => wgpu::VertexFormat::Sint32x2,
VertexFormat::Int32x3 => wgpu::VertexFormat::Sint32x3,
VertexFormat::Int32x4 => wgpu::VertexFormat::Sint32x4,
VertexFormat::Float64 => wgpu::VertexFormat::Float64,
VertexFormat::Float64x2 => wgpu::VertexFormat::Float64x2,
VertexFormat::Float64x3 => wgpu::VertexFormat::Float64x3,
VertexFormat::Float64x4 => wgpu::VertexFormat::Float64x4,
}
}
}

View File

@ -1,27 +1,67 @@
#[path = "DrawModel.rs"]
mod _DrawModel;
pub use self::_DrawModel::*;
//#[path = "DrawModel.rs"]
//mod _DrawModel;
//pub use self::_DrawModel::*;
#[path = "Instance.rs"]
mod _Instance;
pub use self::_Instance::*;
#[path = "IndexFormat.rs"]
mod _IndexFormat;
pub use self::_IndexFormat::*;
#[path = "Material.rs"]
mod _Material;
pub use self::_Material::*;
//#[path = "Instance.rs"]
//mod _Instance;
//pub use self::_Instance::*;
#[path = "Model.rs"]
mod _Model;
pub use self::_Model::*;
//#[path = "Material.rs"]
//mod _Material;
//pub use self::_Material::*;
#[path = "Mesh.rs"]
mod _Mesh;
pub use self::_Mesh::*;
//#[path = "Model.rs"]
//mod _Model;
//pub use self::_Model::*;
#[path = "Texture.rs"]
mod _Texture;
pub use self::_Texture::*;
#[path = "PolygonMode.rs"]
mod _PolygonMode;
pub use self::_PolygonMode::*;
#[path = "Renderer.rs"]
mod _Renderer;
pub use self::_Renderer::*;
#[path = "StepMode.rs"]
mod _StepMode;
pub use self::_StepMode::*;
//#[path = "Mesh.rs"]
//mod _Mesh;
//pub use self::_Mesh::*;
//#[path = "Texture.rs"]
//mod _Texture;
//pub use self::_Texture::*;
#[path = "Vertex.rs"]
mod _Vertex;
pub use self::_Vertex::*;
#[path = "VertexAttribute.rs"]
mod _VertexAttribute;
pub use self::_VertexAttribute::*;
#[path = "VertexBufferLayout.rs"]
mod _VertexBufferLayout;
pub use self::_VertexBufferLayout::*;
#[path = "VertexFormat.rs"]
mod _VertexFormat;
pub use self::_VertexFormat::*;
#[path = "VertexBuffer.rs"]
mod _VertexBuffer;
pub use self::_VertexBuffer::*;
#[path = "IndexBuffer.rs"]
mod _IndexBuffer;
pub use self::_IndexBuffer::*;
#[path = "UniformBuffer.rs"]
mod _UniformBuffer;
pub use self::_UniformBuffer::*;