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;