Test custom types with cubes example

This commit is contained in:
Werner
2022-01-05 09:41:22 -03:00
parent f45d465c3f
commit 1ac2662011
10 changed files with 153 additions and 130 deletions

View File

@ -6,8 +6,8 @@ pub const NUM_INSTANCES: u32 = NUM_INSTANCES_PER_ROW * NUM_INSTANCES_PER_ROW;
pub const INSTANCE_DISPLACEMENT: cgmath::Vector3<f32> = cgmath::Vector3::new(
NUM_INSTANCES_PER_ROW as f32 * 0.5,
0.0,
NUM_INSTANCES_PER_ROW as f32 * 0.5,
0.0
);
pub struct Instance {
@ -32,7 +32,7 @@ pub struct InstanceRaw {
}
impl InstanceRaw {
pub fn GetDescriptor<'a>() -> wgpu::VertexBufferLayout<'a> {
pub fn GetLayout<'a>() -> wgpu::VertexBufferLayout<'a> {
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<InstanceRaw>() as wgpu::BufferAddress,
// We need to switch from using a step mode of Vertex to Instance

View File

@ -1,5 +1,6 @@
use super::Texture;
use super::{Material, Mesh, Vertex};
use super::{
Material, Mesh, StepMode, Texture, Vertex, VertexAttribute, VertexBufferLayout, VertexFormat,
};
use anyhow::Result;
use bytemuck::{Pod, Zeroable};
use std::path::Path;
@ -112,26 +113,30 @@ pub struct ModelVertex {
}
impl Vertex for ModelVertex {
fn GetDescriptor<'a>() -> wgpu::VertexBufferLayout<'a> {
fn GetLayout() -> VertexBufferLayout {
use std::mem;
wgpu::VertexBufferLayout {
array_stride: mem::size_of::<ModelVertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[
wgpu::VertexAttribute {
VertexBufferLayout {
label: "Model".into(),
stride: mem::size_of::<ModelVertex>(),
step_mode: StepMode::Vertex,
attributes: vec![
VertexAttribute {
label: "Position".into(),
format: VertexFormat::Float32x3,
offset: 0,
shader_location: 0,
format: wgpu::VertexFormat::Float32x3,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
VertexAttribute {
label: "Position".into(),
shader_location: 1,
format: wgpu::VertexFormat::Float32x2,
offset: mem::size_of::<[f32; 3]>(),
format: VertexFormat::Float32x2,
},
wgpu::VertexAttribute {
offset: mem::size_of::<[f32; 5]>() as wgpu::BufferAddress,
VertexAttribute {
label: "Color".into(),
shader_location: 2,
format: wgpu::VertexFormat::Float32x3,
offset: mem::size_of::<[f32; 5]>(),
format: VertexFormat::Float32x3,
},
],
}

View File

@ -5,5 +5,4 @@ pub struct PipelineDescriptor {
pub label: Cow<'static, str>,
pub layout: Option<String>,
pub shader_stages: String,
}

View File

@ -22,12 +22,12 @@ impl Renderer {
pub async fn New(window: Window) -> Result<Self> {
let size = window.inner_size();
let instance = wgpu::Instance::new(wgpu::Backends::all());
let instance = wgpu::Instance::new(wgpu::Backends::PRIMARY);
let surface = unsafe { instance.create_surface(&window) };
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
@ -122,10 +122,13 @@ impl Renderer {
pub fn Draw(
&self,
pipeline: &wgpu::RenderPipeline,
vertex_buffer: &wgpu::Buffer,
index_buffer: &wgpu::Buffer,
vertex_buffer: &str,
index_buffer: &str,
num_indices: u32,
) -> Result<()> {
let vertex_buffer = self.vertex_buffers.get(vertex_buffer).unwrap();
let index_buffer = self.index_buffers.get(index_buffer).unwrap();
let output = self.surface.get_current_texture()?;
let view = output

View File

@ -1,24 +1,24 @@
pub mod Pipeline;
//#[path = "DrawModel.rs"]
//mod _DrawModel;
//pub use self::_DrawModel::*;
#[path = "DrawModel.rs"]
mod _DrawModel;
pub use self::_DrawModel::*;
#[path = "IndexFormat.rs"]
mod _IndexFormat;
pub use self::_IndexFormat::*;
//#[path = "Instance.rs"]
//mod _Instance;
//pub use self::_Instance::*;
#[path = "Instance.rs"]
mod _Instance;
pub use self::_Instance::*;
//#[path = "Material.rs"]
//mod _Material;
//pub use self::_Material::*;
#[path = "Material.rs"]
mod _Material;
pub use self::_Material::*;
//#[path = "Model.rs"]
//mod _Model;
//pub use self::_Model::*;
#[path = "Model.rs"]
mod _Model;
pub use self::_Model::*;
#[path = "Renderer.rs"]
mod _Renderer;
@ -28,13 +28,13 @@ pub use self::_Renderer::*;
mod _StepMode;
pub use self::_StepMode::*;
//#[path = "Mesh.rs"]
//mod _Mesh;
//pub use self::_Mesh::*;
#[path = "Mesh.rs"]
mod _Mesh;
pub use self::_Mesh::*;
//#[path = "Texture.rs"]
//mod _Texture;
//pub use self::_Texture::*;
#[path = "Texture.rs"]
mod _Texture;
pub use self::_Texture::*;
#[path = "Vertex.rs"]
mod _Vertex;

View File

@ -23,7 +23,7 @@ impl Runtime {
let mut renderer = pollster::block_on(Renderer::New(window))?;
let mut app = T::Init(&renderer)?;
let mut app = T::Init(&mut renderer)?;
let mut last_update = Instant::now();
@ -47,7 +47,7 @@ impl Runtime {
let delta = now - last_update;
last_update = now;
app.Update(&renderer, delta);
app.Update(&mut renderer, delta);
match app.Draw(&mut renderer) {
Ok(_) => {}
@ -55,7 +55,7 @@ impl Runtime {
Err(wgpu::SurfaceError::Lost) => {
let size = renderer.window.inner_size();
renderer.Resize(size.width, size.height);
app.Resize(&renderer);
app.Resize(&mut renderer);
}
// The system is out of memory, we should probably quit
Err(wgpu::SurfaceError::OutOfMemory) => {
@ -78,7 +78,7 @@ impl Runtime {
}
}
Event::WindowEvent { event, window_id } if window_id == renderer.window.id() => {
if !app.Input(&renderer, &event) {
if !app.Input(&mut renderer, &event) {
match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
@ -93,11 +93,11 @@ impl Runtime {
WindowEvent::Focused(focused) => is_focused = focused,
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
renderer.Resize(new_inner_size.width, new_inner_size.height);
app.Resize(&renderer);
app.Resize(&mut renderer);
}
WindowEvent::Resized(physical_size) => {
renderer.Resize(physical_size.width, physical_size.height);
app.Resize(&renderer);
app.Resize(&mut renderer);
}
_ => {}
}

View File

@ -30,7 +30,7 @@ impl Shader {
pub fn FromGlsl(stage: ShaderStage, glsl: &str) -> Self {
Self {
label: "".into(),
label: "Glsl".into(),
source: ShaderSource::Glsl(glsl.to_string()),
stage,
}
@ -38,7 +38,7 @@ impl Shader {
pub fn FromWgsl(wgsl: &str) -> Self {
Self {
label: "".into(),
label: "Wgsl".into(),
source: ShaderSource::Wgsl(wgsl.to_string()),
stage: ShaderStage::Multiple,
}

View File

@ -5,9 +5,9 @@ use winit::event::*;
/// Represents a application with reactive state.
pub trait State: Sized + 'static {
fn Init(renderer: &Renderer) -> Result<Self>;
fn Input(&mut self, renderer: &Renderer, event: &WindowEvent) -> bool;
fn Update(&mut self, renderer: &Renderer, delta: Duration);
fn Resize(&mut self, renderer: &Renderer);
fn Init(renderer: &mut Renderer) -> Result<Self>;
fn Input(&mut self, renderer: &mut Renderer, event: &WindowEvent) -> bool;
fn Update(&mut self, renderer: &mut Renderer, delta: Duration);
fn Resize(&mut self, renderer: &mut Renderer);
fn Draw(&mut self, renderer: &mut Renderer) -> Result<(), wgpu::SurfaceError>;
}