mirror of
https://github.com/bevyengine/bevy
synced 2024-11-24 21:53:07 +00:00
rustfmt crate
This commit is contained in:
parent
ee03942e40
commit
9f7e313dc4
36 changed files with 1043 additions and 694 deletions
|
@ -1,4 +1,9 @@
|
|||
use crate::{App, asset::AssetStorage, legion::prelude::{World, SystemScheduler}, render::{*, passes::*}, AppStage, Time};
|
||||
use crate::{
|
||||
asset::AssetStorage,
|
||||
legion::prelude::{SystemScheduler, World},
|
||||
render::{passes::*, *},
|
||||
App, AppStage, Time,
|
||||
};
|
||||
|
||||
pub struct AppBuilder {
|
||||
pub app: App,
|
||||
|
@ -26,7 +31,7 @@ impl AppBuilder {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn with_scheduler(mut self, scheduler: SystemScheduler::<AppStage>) -> Self {
|
||||
pub fn with_scheduler(mut self, scheduler: SystemScheduler<AppStage>) -> Self {
|
||||
self.app.scheduler = scheduler;
|
||||
self
|
||||
}
|
||||
|
@ -37,29 +42,36 @@ impl AppBuilder {
|
|||
}
|
||||
|
||||
pub fn add_default_passes(mut self) -> Self {
|
||||
self.app.render_graph.add_render_resource_manager(Box::new(render_resources::MaterialResourceManager));
|
||||
self.app.render_graph.add_render_resource_manager(Box::new(render_resources::LightResourceManager::new(10)));
|
||||
self.app.render_graph.add_render_resource_manager(Box::new(render_resources::GlobalResourceManager));
|
||||
self.app.render_graph.add_render_resource_manager(Box::new(render_resources::Global2dResourceManager));
|
||||
let render_graph = &mut self.app.render_graph;
|
||||
render_graph
|
||||
.add_render_resource_manager(Box::new(render_resources::MaterialResourceManager));
|
||||
render_graph
|
||||
.add_render_resource_manager(Box::new(render_resources::LightResourceManager::new(10)));
|
||||
render_graph.add_render_resource_manager(Box::new(render_resources::GlobalResourceManager));
|
||||
render_graph
|
||||
.add_render_resource_manager(Box::new(render_resources::Global2dResourceManager));
|
||||
|
||||
let depth_format = wgpu::TextureFormat::Depth32Float;
|
||||
self.app.render_graph.set_pass("forward", Box::new(ForwardPass::new(depth_format)));
|
||||
self.app.render_graph.set_pipeline("forward", "forward", Box::new(ForwardPipeline::new()));
|
||||
self.app.render_graph.set_pipeline("forward", "forward_instanced", Box::new(ForwardInstancedPipeline::new(depth_format)));
|
||||
self.app.render_graph.set_pipeline("forward", "ui", Box::new(UiPipeline::new()));
|
||||
render_graph.set_pass("forward", Box::new(ForwardPass::new(depth_format)));
|
||||
render_graph.set_pipeline("forward", "forward", Box::new(ForwardPipeline::new()));
|
||||
render_graph.set_pipeline(
|
||||
"forward",
|
||||
"forward_instanced",
|
||||
Box::new(ForwardInstancedPipeline::new(depth_format)),
|
||||
);
|
||||
render_graph.set_pipeline("forward", "ui", Box::new(UiPipeline::new()));
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_default_resources(mut self) -> Self {
|
||||
self.app.world.resources.insert(Time::new());
|
||||
self.app.world.resources.insert(AssetStorage::<Mesh, MeshType>::new());
|
||||
let resources = &mut self.app.world.resources;
|
||||
resources.insert(Time::new());
|
||||
resources.insert(AssetStorage::<Mesh, MeshType>::new());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_defaults(self) -> Self {
|
||||
self
|
||||
.add_default_resources()
|
||||
.add_default_passes()
|
||||
self.add_default_resources().add_default_passes()
|
||||
}
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
mod app_stage;
|
||||
mod app_builder;
|
||||
mod app_stage;
|
||||
|
||||
pub use app_stage::AppStage;
|
||||
pub use app_builder::AppBuilder;
|
||||
pub use app_stage::AppStage;
|
||||
|
||||
use winit::{
|
||||
event,
|
||||
|
@ -15,8 +15,7 @@ use legion::prelude::*;
|
|||
|
||||
use crate::{render::*, Time};
|
||||
|
||||
pub struct App
|
||||
{
|
||||
pub struct App {
|
||||
pub world: World,
|
||||
pub window: Option<Window>,
|
||||
pub render_graph: RenderGraph,
|
||||
|
@ -48,18 +47,15 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
fn resize(&mut self, width: u32, height: u32)
|
||||
{
|
||||
fn resize(&mut self, width: u32, height: u32) {
|
||||
self.swap_chain = Some(self.render_graph.resize(width, height, &mut self.world));
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, _: WindowEvent)
|
||||
{
|
||||
}
|
||||
fn handle_event(&mut self, _: WindowEvent) {}
|
||||
|
||||
fn render(&mut self)
|
||||
{
|
||||
self.render_graph.render(&mut self.world, self.swap_chain.as_mut().unwrap());
|
||||
fn render(&mut self) {
|
||||
self.render_graph
|
||||
.render(&mut self.world, self.swap_chain.as_mut().unwrap());
|
||||
}
|
||||
|
||||
pub fn run(mut self) {
|
||||
|
@ -102,11 +98,16 @@ impl App {
|
|||
let swap_chain = device.create_swap_chain(&surface, &swap_chain_descriptor);
|
||||
|
||||
log::info!("Initializing the example...");
|
||||
self.render_graph.initialize(&mut self.world, device, swap_chain_descriptor, queue, surface);
|
||||
self.render_graph.initialize(
|
||||
&mut self.world,
|
||||
device,
|
||||
swap_chain_descriptor,
|
||||
queue,
|
||||
surface,
|
||||
);
|
||||
self.window = Some(window);
|
||||
self.swap_chain = Some(swap_chain);
|
||||
|
||||
|
||||
log::info!("Entering render loop...");
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
*control_flow = if cfg!(feature = "metal-auto-capture") {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::{fs, io};
|
||||
use std::boxed::Box;
|
||||
use std::error::Error;
|
||||
use std::{fs, io};
|
||||
|
||||
// use crate::render::Mesh;
|
||||
|
||||
|
@ -10,8 +10,7 @@ pub fn load_gltf(path: &str) -> Result<(), Box<dyn Error>> {
|
|||
let reader = io::BufReader::new(file);
|
||||
let gltf = gltf::Gltf::from_reader(reader)?;
|
||||
for scene in gltf.scenes() {
|
||||
for _mesh in scene.nodes() {
|
||||
}
|
||||
for _mesh in scene.nodes() {}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -2,13 +2,17 @@ mod gltf;
|
|||
|
||||
pub use self::gltf::load_gltf;
|
||||
|
||||
use std::{sync::{Arc, RwLock}, marker::PhantomData, ops::Drop, collections::HashMap};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
marker::PhantomData,
|
||||
ops::Drop,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
pub struct Handle<T>
|
||||
{
|
||||
pub struct Handle<T> {
|
||||
pub id: Arc<RwLock<usize>>,
|
||||
marker: PhantomData<T>,
|
||||
free_indices: Arc<RwLock<Vec<usize>>>
|
||||
free_indices: Arc<RwLock<Vec<usize>>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for Handle<T> {
|
||||
|
@ -16,7 +20,7 @@ impl<T> Clone for Handle<T> {
|
|||
Handle {
|
||||
id: self.id.clone(),
|
||||
free_indices: self.free_indices.clone(),
|
||||
marker: PhantomData
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,14 +40,20 @@ pub trait Asset<D> {
|
|||
fn load(descriptor: D) -> Self;
|
||||
}
|
||||
|
||||
pub struct AssetStorage<T, D> where T: Asset<D> {
|
||||
pub struct AssetStorage<T, D>
|
||||
where
|
||||
T: Asset<D>,
|
||||
{
|
||||
assets: Vec<Option<T>>,
|
||||
free_indices: Arc<RwLock<Vec<usize>>>,
|
||||
names: HashMap<String, Arc<RwLock<usize>>>,
|
||||
marker: PhantomData<D>,
|
||||
}
|
||||
|
||||
impl<T, D> AssetStorage<T, D> where T: Asset<D> {
|
||||
impl<T, D> AssetStorage<T, D>
|
||||
where
|
||||
T: Asset<D>,
|
||||
{
|
||||
pub fn new() -> AssetStorage<T, D> {
|
||||
AssetStorage {
|
||||
assets: Vec::new(),
|
||||
|
@ -55,13 +65,11 @@ impl<T, D> AssetStorage<T, D> where T: Asset<D> {
|
|||
|
||||
pub fn get_named(&self, name: &str) -> Option<Handle<T>> {
|
||||
match self.names.get(name) {
|
||||
Some(id) => {
|
||||
Some(Handle {
|
||||
Some(id) => Some(Handle {
|
||||
id: id.clone(),
|
||||
marker: PhantomData,
|
||||
free_indices: self.free_indices.clone()
|
||||
})
|
||||
},
|
||||
free_indices: self.free_indices.clone(),
|
||||
}),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
@ -75,9 +83,9 @@ impl<T, D> AssetStorage<T, D> where T: Asset<D> {
|
|||
Handle {
|
||||
id: handle,
|
||||
marker: PhantomData,
|
||||
free_indices: self.free_indices.clone()
|
||||
free_indices: self.free_indices.clone(),
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
self.assets.push(Some(asset));
|
||||
let id = self.assets.len() - 1;
|
||||
|
@ -86,7 +94,7 @@ impl<T, D> AssetStorage<T, D> where T: Asset<D> {
|
|||
Handle {
|
||||
id: handle,
|
||||
marker: PhantomData,
|
||||
free_indices: self.free_indices.clone()
|
||||
free_indices: self.free_indices.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -95,14 +103,12 @@ impl<T, D> AssetStorage<T, D> where T: Asset<D> {
|
|||
pub fn get(&mut self, id: usize) -> Option<&mut T> {
|
||||
if id >= self.assets.len() {
|
||||
None
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if let Some(ref mut asset) = self.assets[id] {
|
||||
Some(asset)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@ use std::time::{Duration, Instant};
|
|||
pub struct Time {
|
||||
pub delta: Duration,
|
||||
pub instant: Instant,
|
||||
pub delta_seconds: f32
|
||||
pub delta_seconds: f32,
|
||||
}
|
||||
|
||||
impl Time {
|
||||
|
@ -21,6 +21,7 @@ impl Time {
|
|||
|
||||
pub fn stop(&mut self) {
|
||||
self.delta = Instant::now() - self.instant;
|
||||
self.delta_seconds = self.delta.as_secs() as f32 + (self.delta.subsec_nanos() as f32 / 1.0e9);
|
||||
self.delta_seconds =
|
||||
self.delta.as_secs() as f32 + (self.delta.subsec_nanos() as f32 / 1.0e9);
|
||||
}
|
||||
}
|
14
src/lib.rs
14
src/lib.rs
|
@ -1,17 +1,17 @@
|
|||
pub mod render;
|
||||
pub mod asset;
|
||||
mod app;
|
||||
mod vertex;
|
||||
pub mod asset;
|
||||
mod core;
|
||||
pub mod render;
|
||||
mod vertex;
|
||||
|
||||
pub use app::{App, AppStage, AppBuilder};
|
||||
pub use crate::core::*;
|
||||
pub use app::{App, AppBuilder, AppStage};
|
||||
|
||||
pub use wgpu;
|
||||
pub use glam as math;
|
||||
pub use legion;
|
||||
pub use legion_transform;
|
||||
pub use legion::prelude::*;
|
||||
pub use legion::schedule::Schedulable;
|
||||
pub use legion_transform;
|
||||
pub use legion_transform::prelude::*;
|
||||
pub use legion_transform::transform_system_bundle;
|
||||
pub use glam as math;
|
||||
pub use wgpu;
|
||||
|
|
|
@ -8,7 +8,7 @@ pub enum CameraType {
|
|||
fov: f32,
|
||||
aspect_ratio: f32,
|
||||
near: f32,
|
||||
far: f32
|
||||
far: f32,
|
||||
},
|
||||
Orthographic {
|
||||
left: f32,
|
||||
|
@ -35,14 +35,28 @@ impl Camera {
|
|||
|
||||
pub fn update(&mut self, width: u32, height: u32) {
|
||||
match &mut self.camera_type {
|
||||
CameraType::Projection { aspect_ratio, fov, near, far } => {
|
||||
CameraType::Projection {
|
||||
aspect_ratio,
|
||||
fov,
|
||||
near,
|
||||
far,
|
||||
} => {
|
||||
*aspect_ratio = width as f32 / height as f32;
|
||||
self.view_matrix = get_perspective_projection_matrix(*fov, *aspect_ratio, *near, *far)
|
||||
},
|
||||
CameraType::Orthographic { left, right, bottom, top, near, far} => {
|
||||
self.view_matrix =
|
||||
get_perspective_projection_matrix(*fov, *aspect_ratio, *near, *far)
|
||||
}
|
||||
CameraType::Orthographic {
|
||||
left,
|
||||
right,
|
||||
bottom,
|
||||
top,
|
||||
near,
|
||||
far,
|
||||
} => {
|
||||
*right = width as f32;
|
||||
*top = height as f32;
|
||||
self.view_matrix = get_orthographic_projection_matrix(*left, *right, *bottom, *top, *near, *far)
|
||||
self.view_matrix =
|
||||
get_orthographic_projection_matrix(*left, *right, *bottom, *top, *near, *far)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +68,14 @@ pub fn get_perspective_projection_matrix(fov: f32, aspect_ratio: f32, near: f32,
|
|||
opengl_to_wgpu_matrix() * projection
|
||||
}
|
||||
|
||||
pub fn get_orthographic_projection_matrix(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Mat4 {
|
||||
pub fn get_orthographic_projection_matrix(
|
||||
left: f32,
|
||||
right: f32,
|
||||
bottom: f32,
|
||||
top: f32,
|
||||
near: f32,
|
||||
far: f32,
|
||||
) -> Mat4 {
|
||||
let projection = Mat4::orthographic_rh_gl(left, right, bottom, top, near, far);
|
||||
|
||||
opengl_to_wgpu_matrix() * projection
|
||||
|
@ -62,9 +83,6 @@ pub fn get_orthographic_projection_matrix(left: f32, right: f32, bottom: f32, to
|
|||
|
||||
pub fn opengl_to_wgpu_matrix() -> Mat4 {
|
||||
Mat4::from_cols_array(&[
|
||||
1.0, 0.0, 0.0, 0.0,
|
||||
0.0, -1.0, 0.0, 0.0,
|
||||
0.0, 0.0, 0.5, 0.0,
|
||||
0.0, 0.0, 0.5, 1.0,
|
||||
1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 1.0,
|
||||
])
|
||||
}
|
|
@ -2,7 +2,6 @@ use crate::{math, render::camera, Translation};
|
|||
use std::ops::Range;
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
|
||||
|
||||
pub struct Light {
|
||||
pub color: wgpu::Color,
|
||||
pub fov: f32,
|
||||
|
@ -20,7 +19,12 @@ pub struct LightRaw {
|
|||
|
||||
impl LightRaw {
|
||||
pub fn from(light: &Light, transform: &math::Mat4, translation: &Translation) -> LightRaw {
|
||||
let proj = camera::get_perspective_projection_matrix(light.fov, 1.0, light.depth.start, light.depth.end) * *transform;
|
||||
let proj = camera::get_perspective_projection_matrix(
|
||||
light.fov,
|
||||
1.0,
|
||||
light.depth.start,
|
||||
light.depth.end,
|
||||
) * *transform;
|
||||
let (x, y, z) = translation.0.into();
|
||||
LightRaw {
|
||||
proj: proj.to_cols_array_2d(),
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use zerocopy::{AsBytes, FromBytes};
|
||||
use crate::math;
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
|
||||
pub struct Material {
|
||||
pub color: math::Vec4,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{vertex::Vertex, asset::Asset, math::*};
|
||||
use crate::{asset::Asset, math::*, vertex::Vertex};
|
||||
use wgpu::{Buffer, Device};
|
||||
use zerocopy::AsBytes;
|
||||
|
||||
|
@ -8,14 +8,14 @@ pub struct Mesh2d;
|
|||
pub enum MeshType {
|
||||
Cube,
|
||||
Plane {
|
||||
size: f32
|
||||
size: f32,
|
||||
},
|
||||
Quad {
|
||||
north_west: Vec2,
|
||||
north_east: Vec2,
|
||||
south_west: Vec2,
|
||||
south_east: Vec2,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
pub struct Mesh {
|
||||
|
@ -28,11 +28,15 @@ pub struct Mesh {
|
|||
impl Mesh {
|
||||
pub fn setup_buffers(&mut self, device: &Device) {
|
||||
if let None = self.vertex_buffer {
|
||||
self.vertex_buffer = Some(device.create_buffer_with_data(self.vertices.as_bytes(), wgpu::BufferUsage::VERTEX));
|
||||
self.vertex_buffer = Some(
|
||||
device.create_buffer_with_data(self.vertices.as_bytes(), wgpu::BufferUsage::VERTEX),
|
||||
);
|
||||
}
|
||||
|
||||
if let None = self.index_buffer {
|
||||
self.index_buffer = Some(device.create_buffer_with_data(self.indices.as_bytes(), wgpu::BufferUsage::INDEX));
|
||||
self.index_buffer = Some(
|
||||
device.create_buffer_with_data(self.indices.as_bytes(), wgpu::BufferUsage::INDEX),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +46,12 @@ impl Asset<MeshType> for Mesh {
|
|||
let (vertices, indices) = match descriptor {
|
||||
MeshType::Cube => create_cube(),
|
||||
MeshType::Plane { size } => create_plane(size),
|
||||
MeshType::Quad { north_west, north_east, south_west, south_east } => create_quad(north_west, north_east, south_west, south_east),
|
||||
MeshType::Quad {
|
||||
north_west,
|
||||
north_east,
|
||||
south_west,
|
||||
south_east,
|
||||
} => create_quad(north_west, north_east, south_west, south_east),
|
||||
};
|
||||
|
||||
Mesh {
|
||||
|
@ -54,7 +63,12 @@ impl Asset<MeshType> for Mesh {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn create_quad(north_west: Vec2, north_east: Vec2, south_west: Vec2, south_east: Vec2) -> (Vec<Vertex>, Vec<u16>) {
|
||||
pub fn create_quad(
|
||||
north_west: Vec2,
|
||||
north_east: Vec2,
|
||||
south_west: Vec2,
|
||||
south_east: Vec2,
|
||||
) -> (Vec<Vertex>, Vec<u16>) {
|
||||
let vertex_data = [
|
||||
Vertex::from(([south_west.x(), south_west.y(), 0.0], [0.0, 0.0, 1.0])),
|
||||
Vertex::from(([north_west.x(), north_west.y(), 0.0], [0.0, 0.0, 1.0])),
|
||||
|
@ -62,7 +76,7 @@ pub fn create_quad(north_west: Vec2, north_east: Vec2, south_west: Vec2, south_e
|
|||
Vertex::from(([south_east.x(), south_east.y(), 0.0], [0.0, 0.0, 1.0])),
|
||||
];
|
||||
|
||||
let index_data: &[u16] = &[ 0, 1, 2, 0, 2, 3 ];
|
||||
let index_data: &[u16] = &[0, 1, 2, 0, 2, 3];
|
||||
return (vertex_data.to_vec(), index_data.to_vec());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,25 +1,25 @@
|
|||
pub mod camera;
|
||||
pub mod shader;
|
||||
pub mod mesh;
|
||||
pub mod render_resources;
|
||||
pub mod passes;
|
||||
pub mod instancing;
|
||||
pub mod mesh;
|
||||
pub mod passes;
|
||||
pub mod render_resources;
|
||||
pub mod shader;
|
||||
|
||||
mod light;
|
||||
mod render_graph;
|
||||
mod material;
|
||||
mod rect;
|
||||
mod render_graph;
|
||||
|
||||
pub use camera::*;
|
||||
pub use light::*;
|
||||
pub use shader::*;
|
||||
pub use render_graph::*;
|
||||
pub use material::*;
|
||||
pub use mesh::*;
|
||||
pub use camera::*;
|
||||
pub use rect::*;
|
||||
pub use render_graph::*;
|
||||
pub use shader::*;
|
||||
|
||||
use std::mem;
|
||||
use crate::vertex::Vertex;
|
||||
use std::mem;
|
||||
|
||||
pub struct UniformBuffer {
|
||||
pub buffer: wgpu::Buffer,
|
||||
|
@ -30,7 +30,7 @@ impl UniformBuffer {
|
|||
pub fn get_binding_resource<'a>(&'a self) -> wgpu::BindingResource<'a> {
|
||||
wgpu::BindingResource::Buffer {
|
||||
buffer: &self.buffer,
|
||||
range: 0 .. self.size,
|
||||
range: 0..self.size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::render::*;
|
||||
use legion::prelude::*;
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
use wgpu::{Device, SwapChainDescriptor};
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
|
@ -16,11 +16,13 @@ pub struct ForwardPass {
|
|||
|
||||
impl ForwardPass {
|
||||
pub fn new(depth_format: wgpu::TextureFormat) -> Self {
|
||||
ForwardPass {
|
||||
depth_format,
|
||||
ForwardPass { depth_format }
|
||||
}
|
||||
}
|
||||
fn get_depth_texture(&self, device: &Device, swap_chain_descriptor: &SwapChainDescriptor) -> wgpu::TextureView {
|
||||
fn get_depth_texture(
|
||||
&self,
|
||||
device: &Device,
|
||||
swap_chain_descriptor: &SwapChainDescriptor,
|
||||
) -> wgpu::TextureView {
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
size: wgpu::Extent3d {
|
||||
width: swap_chain_descriptor.width,
|
||||
|
@ -43,10 +45,17 @@ const DEPTH_TEXTURE_NAME: &str = "forward_depth";
|
|||
|
||||
impl Pass for ForwardPass {
|
||||
fn initialize(&self, render_graph: &mut RenderGraphData) {
|
||||
let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
||||
let depth_texture =
|
||||
self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
||||
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
|
||||
}
|
||||
fn begin<'a>(&mut self, render_graph: &mut RenderGraphData, _: &mut World, encoder: &'a mut wgpu::CommandEncoder, frame: &'a wgpu::SwapChainOutput) -> Option<wgpu::RenderPass<'a>> {
|
||||
fn begin<'a>(
|
||||
&mut self,
|
||||
render_graph: &mut RenderGraphData,
|
||||
_: &mut World,
|
||||
encoder: &'a mut wgpu::CommandEncoder,
|
||||
frame: &'a wgpu::SwapChainOutput,
|
||||
) -> Option<wgpu::RenderPass<'a>> {
|
||||
let depth_texture = render_graph.get_texture(DEPTH_TEXTURE_NAME);
|
||||
Some(encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
|
||||
|
@ -74,7 +83,8 @@ impl Pass for ForwardPass {
|
|||
}
|
||||
|
||||
fn resize(&self, render_graph: &mut RenderGraphData) {
|
||||
let depth_texture = self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
||||
let depth_texture =
|
||||
self.get_depth_texture(&render_graph.device, &render_graph.swap_chain_descriptor);
|
||||
render_graph.set_texture(DEPTH_TEXTURE_NAME, depth_texture);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{render::*, asset::*, render::mesh::*};
|
||||
use crate::{asset::*, render::mesh::*, render::*};
|
||||
use legion::prelude::*;
|
||||
use wgpu::SwapChainOutput;
|
||||
|
||||
|
@ -13,24 +13,21 @@ impl ForwardPipeline {
|
|||
ForwardPipeline {
|
||||
pipeline: None,
|
||||
bind_group: None,
|
||||
depth_format: wgpu::TextureFormat::Depth32Float
|
||||
depth_format: wgpu::TextureFormat::Depth32Float,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Pipeline for ForwardPipeline {
|
||||
fn initialize(&mut self, render_graph: &mut RenderGraphData, _: &mut World) {
|
||||
let vs_bytes = shader::load_glsl(
|
||||
include_str!("forward.vert"),
|
||||
shader::ShaderStage::Vertex,
|
||||
);
|
||||
let fs_bytes = shader::load_glsl(
|
||||
include_str!("forward.frag"),
|
||||
shader::ShaderStage::Fragment,
|
||||
);
|
||||
let vs_bytes = shader::load_glsl(include_str!("forward.vert"), shader::ShaderStage::Vertex);
|
||||
let fs_bytes =
|
||||
shader::load_glsl(include_str!("forward.frag"), shader::ShaderStage::Fragment);
|
||||
|
||||
let bind_group_layout =
|
||||
render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[
|
||||
wgpu::BindGroupLayoutBinding {
|
||||
binding: 0, // global
|
||||
|
@ -41,17 +38,22 @@ impl Pipeline for ForwardPipeline {
|
|||
binding: 1, // lights
|
||||
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
||||
}
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
self.bind_group = Some({
|
||||
|
||||
let forward_uniform_buffer = render_graph.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let forward_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
let light_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
|
||||
// Create bind group
|
||||
render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
bindings: &[
|
||||
wgpu::Binding {
|
||||
|
@ -61,14 +63,19 @@ impl Pipeline for ForwardPipeline {
|
|||
wgpu::Binding {
|
||||
binding: 1,
|
||||
resource: light_uniform_buffer.get_binding_resource(),
|
||||
}
|
||||
},
|
||||
],
|
||||
})
|
||||
});
|
||||
|
||||
let material_bind_group_layout = render_graph.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME).unwrap();
|
||||
let material_bind_group_layout = render_graph
|
||||
.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME)
|
||||
.unwrap();
|
||||
|
||||
let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
let pipeline_layout =
|
||||
render_graph
|
||||
.device
|
||||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&bind_group_layout, material_bind_group_layout],
|
||||
});
|
||||
|
||||
|
@ -77,7 +84,8 @@ impl Pipeline for ForwardPipeline {
|
|||
let vs_module = render_graph.device.create_shader_module(&vs_bytes);
|
||||
let fs_module = render_graph.device.create_shader_module(&fs_bytes);
|
||||
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(
|
||||
&wgpu::RenderPipelineDescriptor {
|
||||
layout: &pipeline_layout,
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
|
@ -95,14 +103,12 @@ impl Pipeline for ForwardPipeline {
|
|||
depth_bias_clamp: 0.0,
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[
|
||||
wgpu::ColorStateDescriptor {
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: render_graph.swap_chain_descriptor.format,
|
||||
color_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
},
|
||||
],
|
||||
}],
|
||||
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
||||
format: self.depth_format,
|
||||
depth_write_enabled: true,
|
||||
|
@ -117,16 +123,25 @@ impl Pipeline for ForwardPipeline {
|
|||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
}));
|
||||
},
|
||||
));
|
||||
}
|
||||
fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, _: &SwapChainOutput, world: &mut World) {
|
||||
fn render(
|
||||
&mut self,
|
||||
render_graph: &RenderGraphData,
|
||||
pass: &mut wgpu::RenderPass,
|
||||
_: &SwapChainOutput,
|
||||
world: &mut World,
|
||||
) {
|
||||
pass.set_bind_group(0, self.bind_group.as_ref().unwrap(), &[]);
|
||||
|
||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
||||
let mut mesh_storage = world
|
||||
.resources
|
||||
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||
.unwrap();
|
||||
let mut last_mesh_id = None;
|
||||
let mut mesh_query =
|
||||
<(Read<Material>, Read<Handle<Mesh>>)>::query()
|
||||
.filter(!component::<Instanced>());
|
||||
<(Read<Material>, Read<Handle<Mesh>>)>::query().filter(!component::<Instanced>());
|
||||
for (material, mesh) in mesh_query.iter_immutable(world) {
|
||||
let current_mesh_id = *mesh.id.read().unwrap();
|
||||
|
||||
|
@ -145,15 +160,14 @@ impl Pipeline for ForwardPipeline {
|
|||
|
||||
if let Some(ref mesh_asset) = mesh_storage.get(*mesh.id.read().unwrap()) {
|
||||
pass.set_bind_group(1, material.bind_group.as_ref().unwrap(), &[]);
|
||||
pass.draw_indexed(0 .. mesh_asset.indices.len() as u32, 0, 0 .. 1);
|
||||
pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1);
|
||||
};
|
||||
|
||||
last_mesh_id = Some(current_mesh_id);
|
||||
}
|
||||
}
|
||||
|
||||
fn resize(&mut self, _: &RenderGraphData) {
|
||||
}
|
||||
fn resize(&mut self, _: &RenderGraphData) {}
|
||||
|
||||
fn get_pipeline(&self) -> &wgpu::RenderPipeline {
|
||||
self.pipeline.as_ref().unwrap()
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
mod forward_pipeline;
|
||||
mod forward_pass;
|
||||
mod forward_pipeline;
|
||||
|
||||
pub use forward_pipeline::*;
|
||||
pub use forward_pass::*;
|
||||
pub use forward_pipeline::*;
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
use crate::{render::{*, instancing::InstanceBufferInfo}, asset::*, render::mesh::*, LocalToWorld};
|
||||
use crate::{
|
||||
asset::*,
|
||||
render::mesh::*,
|
||||
render::{instancing::InstanceBufferInfo, *},
|
||||
LocalToWorld,
|
||||
};
|
||||
use legion::prelude::*;
|
||||
use std::mem;
|
||||
use zerocopy::AsBytes;
|
||||
use wgpu::{Device, SwapChainOutput};
|
||||
use zerocopy::AsBytes;
|
||||
|
||||
pub struct ForwardInstancedPipeline {
|
||||
pub pipeline: Option<wgpu::RenderPipeline>,
|
||||
|
@ -22,7 +27,12 @@ impl ForwardInstancedPipeline {
|
|||
}
|
||||
|
||||
fn create_instance_buffer_infos(device: &Device, world: &World) -> Vec<InstanceBufferInfo> {
|
||||
let mut entities = <(Read<Material>, Read<LocalToWorld>, Read<Handle<Mesh>>, Read<Instanced>)>::query();
|
||||
let mut entities = <(
|
||||
Read<Material>,
|
||||
Read<LocalToWorld>,
|
||||
Read<Handle<Mesh>>,
|
||||
Read<Instanced>,
|
||||
)>::query();
|
||||
let entities_count = entities.iter_immutable(world).count();
|
||||
if entities_count == 0 {
|
||||
return Vec::new();
|
||||
|
@ -31,16 +41,18 @@ impl ForwardInstancedPipeline {
|
|||
let size = mem::size_of::<SimpleMaterialUniforms>();
|
||||
|
||||
// TODO: use a staging buffer for more efficient gpu reads
|
||||
let temp_buf_data = device
|
||||
.create_buffer_mapped(entities_count * size, wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX);
|
||||
let temp_buf_data = device.create_buffer_mapped(
|
||||
entities_count * size,
|
||||
wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX,
|
||||
);
|
||||
|
||||
// TODO: generate these buffers for multiple meshes
|
||||
|
||||
let mut last_mesh_id = None;
|
||||
for ((material, transform, mesh, _), slot) in entities.iter_immutable(world)
|
||||
for ((material, transform, mesh, _), slot) in entities
|
||||
.iter_immutable(world)
|
||||
.zip(temp_buf_data.data.chunks_exact_mut(size))
|
||||
{
|
||||
|
||||
last_mesh_id = Some(*mesh.id.read().unwrap());
|
||||
let (_, _, translation) = transform.0.to_scale_rotation_translation();
|
||||
slot.copy_from_slice(
|
||||
|
@ -63,15 +75,21 @@ impl ForwardInstancedPipeline {
|
|||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn create_instance_buffer_infos_direct(device: &Device, world: &World) -> Vec<InstanceBufferInfo> {
|
||||
let mut entities = <(Read<Material>, Read<LocalToWorld>, Read<Handle<Mesh>>, Read<Instanced>)>::query();
|
||||
fn create_instance_buffer_infos_direct(
|
||||
device: &Device,
|
||||
world: &World,
|
||||
) -> Vec<InstanceBufferInfo> {
|
||||
let mut entities = <(
|
||||
Read<Material>,
|
||||
Read<LocalToWorld>,
|
||||
Read<Handle<Mesh>>,
|
||||
Read<Instanced>,
|
||||
)>::query();
|
||||
let entities_count = entities.iter_immutable(world).count();
|
||||
|
||||
let mut last_mesh_id = None;
|
||||
let mut data = Vec::with_capacity(entities_count);
|
||||
for (material, transform, mesh, _) in entities.iter_immutable(world)
|
||||
{
|
||||
|
||||
for (material, transform, mesh, _) in entities.iter_immutable(world) {
|
||||
last_mesh_id = Some(*mesh.id.read().unwrap());
|
||||
let (_, _, translation) = transform.0.to_scale_rotation_translation();
|
||||
|
||||
|
@ -81,9 +99,10 @@ impl ForwardInstancedPipeline {
|
|||
});
|
||||
}
|
||||
|
||||
let buffer = device
|
||||
.create_buffer_with_data(data.as_bytes(), wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX);
|
||||
|
||||
let buffer = device.create_buffer_with_data(
|
||||
data.as_bytes(),
|
||||
wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX,
|
||||
);
|
||||
|
||||
let mut instance_buffer_infos = Vec::new();
|
||||
instance_buffer_infos.push(InstanceBufferInfo {
|
||||
|
@ -108,7 +127,9 @@ impl Pipeline for ForwardInstancedPipeline {
|
|||
);
|
||||
|
||||
let bind_group_layout =
|
||||
render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[
|
||||
wgpu::BindGroupLayoutBinding {
|
||||
binding: 0, // global
|
||||
|
@ -119,17 +140,23 @@ impl Pipeline for ForwardInstancedPipeline {
|
|||
binding: 1, // lights
|
||||
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
||||
}
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// TODO: this is the same as normal forward pipeline. we can probably reuse
|
||||
self.local_bind_group = Some({
|
||||
let forward_uniform_buffer = render_graph.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let forward_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
let light_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
|
||||
// Create bind group
|
||||
render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
bindings: &[
|
||||
wgpu::Binding {
|
||||
|
@ -139,7 +166,7 @@ impl Pipeline for ForwardInstancedPipeline {
|
|||
wgpu::Binding {
|
||||
binding: 1,
|
||||
resource: light_uniform_buffer.get_binding_resource(),
|
||||
}
|
||||
},
|
||||
],
|
||||
})
|
||||
});
|
||||
|
@ -164,14 +191,18 @@ impl Pipeline for ForwardInstancedPipeline {
|
|||
|
||||
let vertex_buffer_descriptor = get_vertex_buffer_descriptor();
|
||||
|
||||
let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
let pipeline_layout =
|
||||
render_graph
|
||||
.device
|
||||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
});
|
||||
|
||||
let vs_module = render_graph.device.create_shader_module(&vs_bytes);
|
||||
let fs_module = render_graph.device.create_shader_module(&fs_bytes);
|
||||
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(
|
||||
&wgpu::RenderPipelineDescriptor {
|
||||
layout: &pipeline_layout,
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
|
@ -189,14 +220,12 @@ impl Pipeline for ForwardInstancedPipeline {
|
|||
depth_bias_clamp: 0.0,
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[
|
||||
wgpu::ColorStateDescriptor {
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: render_graph.swap_chain_descriptor.format,
|
||||
color_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
},
|
||||
],
|
||||
}],
|
||||
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
||||
format: self.depth_format,
|
||||
depth_write_enabled: true,
|
||||
|
@ -211,30 +240,48 @@ impl Pipeline for ForwardInstancedPipeline {
|
|||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
}));
|
||||
},
|
||||
));
|
||||
|
||||
self.instance_buffer_infos = Some(Self::create_instance_buffer_infos(&render_graph.device, world));
|
||||
self.instance_buffer_infos = Some(Self::create_instance_buffer_infos(
|
||||
&render_graph.device,
|
||||
world,
|
||||
));
|
||||
}
|
||||
|
||||
fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, _: &SwapChainOutput, world: &mut World) {
|
||||
self.instance_buffer_infos = Some(Self::create_instance_buffer_infos(&render_graph.device, world));
|
||||
fn render(
|
||||
&mut self,
|
||||
render_graph: &RenderGraphData,
|
||||
pass: &mut wgpu::RenderPass,
|
||||
_: &SwapChainOutput,
|
||||
world: &mut World,
|
||||
) {
|
||||
self.instance_buffer_infos = Some(Self::create_instance_buffer_infos(
|
||||
&render_graph.device,
|
||||
world,
|
||||
));
|
||||
pass.set_bind_group(0, self.local_bind_group.as_ref().unwrap(), &[]);
|
||||
|
||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
||||
let mut mesh_storage = world
|
||||
.resources
|
||||
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||
.unwrap();
|
||||
for instance_buffer_info in self.instance_buffer_infos.as_ref().unwrap().iter() {
|
||||
if let Some(mesh_asset) = mesh_storage.get(instance_buffer_info.mesh_id) {
|
||||
mesh_asset.setup_buffers(&render_graph.device);
|
||||
pass.set_index_buffer(mesh_asset.index_buffer.as_ref().unwrap(), 0);
|
||||
pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]);
|
||||
pass.set_vertex_buffers(1, &[(&instance_buffer_info.buffer, 0)]);
|
||||
pass.draw_indexed(0 .. mesh_asset.indices.len() as u32, 0, 0 .. instance_buffer_info.instance_count as u32);
|
||||
pass.draw_indexed(
|
||||
0..mesh_asset.indices.len() as u32,
|
||||
0,
|
||||
0..instance_buffer_info.instance_count as u32,
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn resize(&mut self, _: &RenderGraphData) {
|
||||
|
||||
}
|
||||
fn resize(&mut self, _: &RenderGraphData) {}
|
||||
|
||||
fn get_pipeline(&self) -> &wgpu::RenderPipeline {
|
||||
self.pipeline.as_ref().unwrap()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{render::*, render::passes::shadow, asset::*, render::mesh::*};
|
||||
use crate::{asset::*, render::mesh::*, render::passes::shadow, render::*};
|
||||
use legion::prelude::*;
|
||||
use wgpu::SwapChainOutput;
|
||||
|
||||
|
@ -30,7 +30,9 @@ impl Pipeline for ForwardShadowPassNew {
|
|||
);
|
||||
|
||||
let bind_group_layout =
|
||||
render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[
|
||||
wgpu::BindGroupLayoutBinding {
|
||||
binding: 0, // global
|
||||
|
@ -59,13 +61,23 @@ impl Pipeline for ForwardShadowPassNew {
|
|||
});
|
||||
|
||||
self.bind_group = Some({
|
||||
let forward_uniform_buffer = render_graph.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let shadow_sampler = render_graph.get_sampler(shadow::SHADOW_SAMPLER_NAME).unwrap();
|
||||
let shadow_texture = render_graph.get_texture(shadow::SHADOW_TEXTURE_NAME).unwrap();
|
||||
let forward_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(render_resources::FORWARD_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
let light_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
let shadow_sampler = render_graph
|
||||
.get_sampler(shadow::SHADOW_SAMPLER_NAME)
|
||||
.unwrap();
|
||||
let shadow_texture = render_graph
|
||||
.get_texture(shadow::SHADOW_TEXTURE_NAME)
|
||||
.unwrap();
|
||||
|
||||
// Create bind group
|
||||
render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
bindings: &[
|
||||
wgpu::Binding {
|
||||
|
@ -88,9 +100,14 @@ impl Pipeline for ForwardShadowPassNew {
|
|||
})
|
||||
});
|
||||
|
||||
let material_bind_group_layout = render_graph.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME).unwrap();
|
||||
let material_bind_group_layout = render_graph
|
||||
.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME)
|
||||
.unwrap();
|
||||
|
||||
let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
let pipeline_layout =
|
||||
render_graph
|
||||
.device
|
||||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&bind_group_layout, material_bind_group_layout],
|
||||
});
|
||||
|
||||
|
@ -99,7 +116,8 @@ impl Pipeline for ForwardShadowPassNew {
|
|||
|
||||
let vertex_buffer_descriptor = get_vertex_buffer_descriptor();
|
||||
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(
|
||||
&wgpu::RenderPipelineDescriptor {
|
||||
layout: &pipeline_layout,
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
|
@ -117,14 +135,12 @@ impl Pipeline for ForwardShadowPassNew {
|
|||
depth_bias_clamp: 0.0,
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[
|
||||
wgpu::ColorStateDescriptor {
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: render_graph.swap_chain_descriptor.format,
|
||||
color_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
},
|
||||
],
|
||||
}],
|
||||
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
||||
format: self.depth_format,
|
||||
depth_write_enabled: true,
|
||||
|
@ -139,27 +155,36 @@ impl Pipeline for ForwardShadowPassNew {
|
|||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
}));
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, _swap_chain_output: &SwapChainOutput, world: &mut World) {
|
||||
fn render(
|
||||
&mut self,
|
||||
render_graph: &RenderGraphData,
|
||||
pass: &mut wgpu::RenderPass,
|
||||
_swap_chain_output: &SwapChainOutput,
|
||||
world: &mut World,
|
||||
) {
|
||||
let mut mesh_query = <(Read<Material>, Read<Handle<Mesh>>)>::query();
|
||||
pass.set_bind_group(0, self.bind_group.as_ref().unwrap(), &[]);
|
||||
|
||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
||||
let mut mesh_storage = world
|
||||
.resources
|
||||
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||
.unwrap();
|
||||
for (material, mesh) in mesh_query.iter_immutable(world) {
|
||||
if let Some(mesh_asset) = mesh_storage.get(*mesh.id.read().unwrap()) {
|
||||
mesh_asset.setup_buffers(&render_graph.device);
|
||||
pass.set_bind_group(1, material.bind_group.as_ref().unwrap(), &[]);
|
||||
pass.set_index_buffer(mesh_asset.index_buffer.as_ref().unwrap(), 0);
|
||||
pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]);
|
||||
pass.draw_indexed(0 .. mesh_asset.indices.len() as u32, 0, 0 .. 1);
|
||||
pass.draw_indexed(0..mesh_asset.indices.len() as u32, 0, 0..1);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn resize(&mut self, _render_graph: &RenderGraphData) {
|
||||
}
|
||||
fn resize(&mut self, _render_graph: &RenderGraphData) {}
|
||||
|
||||
fn get_pipeline(&self) -> &wgpu::RenderPipeline {
|
||||
self.pipeline.as_ref().unwrap()
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
|
||||
mod forward;
|
||||
mod forward_shadow;
|
||||
mod forward_instanced;
|
||||
mod forward_shadow;
|
||||
mod shadow;
|
||||
mod ui;
|
||||
|
||||
pub use forward::{ForwardUniforms, ForwardPipeline, ForwardPass};
|
||||
pub use forward_shadow::{ForwardShadowPassNew};
|
||||
pub use forward::{ForwardPass, ForwardPipeline, ForwardUniforms};
|
||||
pub use forward_instanced::ForwardInstancedPipeline;
|
||||
pub use forward_shadow::ForwardShadowPassNew;
|
||||
pub use shadow::ShadowPass;
|
||||
pub use ui::UiPipeline;
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{render::*, render::passes::shadow, LocalToWorld, Translation};
|
||||
use crate::{render::passes::shadow, render::*, LocalToWorld, Translation};
|
||||
use legion::prelude::*;
|
||||
use std::mem;
|
||||
|
||||
|
@ -13,7 +13,11 @@ pub struct ShadowPass {
|
|||
pub const SHADOW_TEXTURE_NAME: &str = "shadow_texture";
|
||||
|
||||
impl ShadowPass {
|
||||
pub fn new(shadow_size: wgpu::Extent3d, shadow_format: wgpu::TextureFormat, max_lights: usize) -> Self {
|
||||
pub fn new(
|
||||
shadow_size: wgpu::Extent3d,
|
||||
shadow_format: wgpu::TextureFormat,
|
||||
max_lights: usize,
|
||||
) -> Self {
|
||||
ShadowPass {
|
||||
light_index: -1,
|
||||
shadow_texture: None,
|
||||
|
@ -26,7 +30,9 @@ impl ShadowPass {
|
|||
|
||||
impl Pass for ShadowPass {
|
||||
fn initialize(&self, render_graph: &mut RenderGraphData) {
|
||||
let shadow_texture = render_graph.device.create_texture(&wgpu::TextureDescriptor {
|
||||
let shadow_texture = render_graph
|
||||
.device
|
||||
.create_texture(&wgpu::TextureDescriptor {
|
||||
size: self.shadow_size,
|
||||
array_layer_count: self.max_lights as u32,
|
||||
mip_level_count: 1,
|
||||
|
@ -74,8 +80,12 @@ impl Pass for ShadowPass {
|
|||
|
||||
// The light uniform buffer already has the projection,
|
||||
// let's just copy it over to the shadow uniform buffer.
|
||||
let light_uniform_buffer = render_graph.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let shadow_pipeline_uniform_buffer = render_graph.get_uniform_buffer(shadow::SHADOW_PIPELINE_UNIFORMS).unwrap();
|
||||
let light_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(render_resources::LIGHT_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
let shadow_pipeline_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(shadow::SHADOW_PIPELINE_UNIFORMS)
|
||||
.unwrap();
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&light_uniform_buffer.buffer,
|
||||
(i * mem::size_of::<LightRaw>()) as wgpu::BufferAddress,
|
||||
|
|
|
@ -30,7 +30,10 @@ impl ShadowPipeline {
|
|||
|
||||
impl Pipeline for ShadowPipeline {
|
||||
fn initialize(&mut self, render_graph: &mut RenderGraphData, _: &mut World) {
|
||||
let bind_group_layout = render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
let bind_group_layout =
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||
binding: 0, // global
|
||||
visibility: wgpu::ShaderStage::VERTEX,
|
||||
|
@ -38,13 +41,15 @@ impl Pipeline for ShadowPipeline {
|
|||
}],
|
||||
});
|
||||
|
||||
let material_bind_group_layout = render_graph.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME).unwrap();
|
||||
let material_bind_group_layout = render_graph
|
||||
.get_bind_group_layout(render_resources::MATERIAL_BIND_GROUP_LAYOUT_NAME)
|
||||
.unwrap();
|
||||
|
||||
let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[
|
||||
&bind_group_layout,
|
||||
material_bind_group_layout,
|
||||
],
|
||||
let pipeline_layout =
|
||||
render_graph
|
||||
.device
|
||||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&bind_group_layout, material_bind_group_layout],
|
||||
});
|
||||
|
||||
let uniform_size = mem::size_of::<ShadowUniforms>() as wgpu::BufferAddress;
|
||||
|
@ -54,7 +59,10 @@ impl Pipeline for ShadowPipeline {
|
|||
});
|
||||
|
||||
// Create bind group
|
||||
self.bind_group = Some(render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
self.bind_group = Some(
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
bindings: &[wgpu::Binding {
|
||||
binding: 0,
|
||||
|
@ -63,15 +71,21 @@ impl Pipeline for ShadowPipeline {
|
|||
range: 0..uniform_size,
|
||||
},
|
||||
}],
|
||||
}));
|
||||
}),
|
||||
);
|
||||
|
||||
render_graph.set_uniform_buffer(SHADOW_PIPELINE_UNIFORMS, UniformBuffer {
|
||||
render_graph.set_uniform_buffer(
|
||||
SHADOW_PIPELINE_UNIFORMS,
|
||||
UniformBuffer {
|
||||
buffer: uniform_buf,
|
||||
size: uniform_size,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// Create other resources
|
||||
let shadow_sampler = render_graph.device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
let shadow_sampler = render_graph
|
||||
.device
|
||||
.create_sampler(&wgpu::SamplerDescriptor {
|
||||
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
||||
|
@ -88,14 +102,14 @@ impl Pipeline for ShadowPipeline {
|
|||
let vertex_buffer_descriptor = get_vertex_buffer_descriptor();
|
||||
|
||||
// Create the render pipeline
|
||||
let vs_bytes =
|
||||
shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex);
|
||||
let vs_bytes = shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex);
|
||||
let fs_bytes =
|
||||
shader::load_glsl(include_str!("shadow.frag"), shader::ShaderStage::Fragment);
|
||||
let vs_module = render_graph.device.create_shader_module(&vs_bytes);
|
||||
let fs_module = render_graph.device.create_shader_module(&fs_bytes);
|
||||
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(
|
||||
&wgpu::RenderPipelineDescriptor {
|
||||
layout: &pipeline_layout,
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
|
@ -128,7 +142,8 @@ impl Pipeline for ShadowPipeline {
|
|||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
}));
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn render(
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
use crate::{render::{*, instancing::InstanceBufferInfo}, asset::*, render::mesh::*, math};
|
||||
use crate::{
|
||||
asset::*,
|
||||
math,
|
||||
render::mesh::*,
|
||||
render::{instancing::InstanceBufferInfo, *},
|
||||
};
|
||||
use legion::prelude::*;
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
use wgpu::SwapChainOutput;
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
|
@ -25,11 +30,15 @@ impl UiPipeline {
|
|||
pipeline: None,
|
||||
bind_group: None,
|
||||
quad: None,
|
||||
depth_format: wgpu::TextureFormat::Depth32Float
|
||||
depth_format: wgpu::TextureFormat::Depth32Float,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_rect_buffers(&self, device: &wgpu::Device, world: &World) -> Vec<InstanceBufferInfo> {
|
||||
pub fn create_rect_buffers(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
world: &World,
|
||||
) -> Vec<InstanceBufferInfo> {
|
||||
let mut rect_query = <Read<Rect>>::query();
|
||||
let rect_count = rect_query.iter_immutable(world).count();
|
||||
|
||||
|
@ -40,8 +49,7 @@ impl UiPipeline {
|
|||
let mut data = Vec::with_capacity(rect_count);
|
||||
// TODO: this probably isn't the best way to handle z-ordering
|
||||
let mut z = 0.9999;
|
||||
for rect in rect_query.iter_immutable(world)
|
||||
{
|
||||
for rect in rect_query.iter_immutable(world) {
|
||||
data.push(RectData {
|
||||
position: rect.position.into(),
|
||||
dimensions: rect.dimensions.into(),
|
||||
|
@ -52,8 +60,10 @@ impl UiPipeline {
|
|||
z -= 0.0001;
|
||||
}
|
||||
|
||||
let buffer = device
|
||||
.create_buffer_with_data(data.as_bytes(), wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX);
|
||||
let buffer = device.create_buffer_with_data(
|
||||
data.as_bytes(),
|
||||
wgpu::BufferUsage::COPY_SRC | wgpu::BufferUsage::VERTEX,
|
||||
);
|
||||
|
||||
let mesh_id = *self.quad.as_ref().unwrap().id.read().unwrap();
|
||||
|
||||
|
@ -70,44 +80,42 @@ impl UiPipeline {
|
|||
|
||||
impl Pipeline for UiPipeline {
|
||||
fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World) {
|
||||
let vs_bytes = shader::load_glsl(
|
||||
include_str!("ui.vert"),
|
||||
shader::ShaderStage::Vertex,
|
||||
);
|
||||
let fs_bytes = shader::load_glsl(
|
||||
include_str!("ui.frag"),
|
||||
shader::ShaderStage::Fragment,
|
||||
);
|
||||
let vs_bytes = shader::load_glsl(include_str!("ui.vert"), shader::ShaderStage::Vertex);
|
||||
let fs_bytes = shader::load_glsl(include_str!("ui.frag"), shader::ShaderStage::Fragment);
|
||||
|
||||
let bind_group_layout =
|
||||
render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[
|
||||
wgpu::BindGroupLayoutBinding {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||
binding: 0, // global_2d
|
||||
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::UniformBuffer { dynamic: false },
|
||||
},
|
||||
],
|
||||
}],
|
||||
});
|
||||
|
||||
self.bind_group = Some({
|
||||
|
||||
let global_2d_uniform_buffer = render_graph.get_uniform_buffer(render_resources::GLOBAL_2D_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let global_2d_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(render_resources::GLOBAL_2D_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
|
||||
// Create bind group
|
||||
render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_group_layout,
|
||||
bindings: &[
|
||||
wgpu::Binding {
|
||||
bindings: &[wgpu::Binding {
|
||||
binding: 0,
|
||||
resource: global_2d_uniform_buffer.get_binding_resource(),
|
||||
},
|
||||
],
|
||||
}],
|
||||
})
|
||||
});
|
||||
|
||||
{
|
||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
||||
let mut mesh_storage = world
|
||||
.resources
|
||||
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||
.unwrap();
|
||||
|
||||
let quad = Mesh::load(MeshType::Quad {
|
||||
north_west: math::vec2(-0.5, 0.5),
|
||||
|
@ -118,7 +126,10 @@ impl Pipeline for UiPipeline {
|
|||
self.quad = Some(mesh_storage.add(quad, "ui_quad"));
|
||||
}
|
||||
|
||||
let pipeline_layout = render_graph.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
let pipeline_layout =
|
||||
render_graph
|
||||
.device
|
||||
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&bind_group_layout],
|
||||
});
|
||||
|
||||
|
@ -154,7 +165,8 @@ impl Pipeline for UiPipeline {
|
|||
let vs_module = render_graph.device.create_shader_module(&vs_bytes);
|
||||
let fs_module = render_graph.device.create_shader_module(&fs_bytes);
|
||||
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
self.pipeline = Some(render_graph.device.create_render_pipeline(
|
||||
&wgpu::RenderPipelineDescriptor {
|
||||
layout: &pipeline_layout,
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
|
@ -172,14 +184,12 @@ impl Pipeline for UiPipeline {
|
|||
depth_bias_clamp: 0.0,
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[
|
||||
wgpu::ColorStateDescriptor {
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: render_graph.swap_chain_descriptor.format,
|
||||
color_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
},
|
||||
],
|
||||
}],
|
||||
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
||||
format: self.depth_format,
|
||||
depth_write_enabled: true,
|
||||
|
@ -194,27 +204,40 @@ impl Pipeline for UiPipeline {
|
|||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
}));
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, _: &SwapChainOutput, world: &mut World) {
|
||||
fn render(
|
||||
&mut self,
|
||||
render_graph: &RenderGraphData,
|
||||
pass: &mut wgpu::RenderPass,
|
||||
_: &SwapChainOutput,
|
||||
world: &mut World,
|
||||
) {
|
||||
let instance_buffer_infos = Some(self.create_rect_buffers(&render_graph.device, world));
|
||||
pass.set_bind_group(0, self.bind_group.as_ref().unwrap(), &[]);
|
||||
|
||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
||||
let mut mesh_storage = world
|
||||
.resources
|
||||
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||
.unwrap();
|
||||
for instance_buffer_info in instance_buffer_infos.as_ref().unwrap().iter() {
|
||||
if let Some(mesh_asset) = mesh_storage.get(instance_buffer_info.mesh_id) {
|
||||
mesh_asset.setup_buffers(&render_graph.device);
|
||||
pass.set_index_buffer(mesh_asset.index_buffer.as_ref().unwrap(), 0);
|
||||
pass.set_vertex_buffers(0, &[(&mesh_asset.vertex_buffer.as_ref().unwrap(), 0)]);
|
||||
pass.set_vertex_buffers(1, &[(&instance_buffer_info.buffer, 0)]);
|
||||
pass.draw_indexed(0 .. mesh_asset.indices.len() as u32, 0, 0 .. instance_buffer_info.instance_count as u32);
|
||||
pass.draw_indexed(
|
||||
0..mesh_asset.indices.len() as u32,
|
||||
0,
|
||||
0..instance_buffer_info.instance_count as u32,
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn resize(&mut self, _: &RenderGraphData) {
|
||||
}
|
||||
fn resize(&mut self, _: &RenderGraphData) {}
|
||||
|
||||
fn get_pipeline(&self) -> &wgpu::RenderPipeline {
|
||||
self.pipeline.as_ref().unwrap()
|
||||
|
|
|
@ -7,8 +7,8 @@ pub use pipeline::Pipeline;
|
|||
pub use render_resource_manager::RenderResourceManager;
|
||||
|
||||
use crate::render::UniformBuffer;
|
||||
use std::collections::HashMap;
|
||||
use legion::world::World;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct RenderGraph {
|
||||
pub data: Option<RenderGraphData>,
|
||||
|
@ -29,7 +29,12 @@ pub struct RenderGraphData {
|
|||
bind_group_layouts: HashMap<String, wgpu::BindGroupLayout>,
|
||||
}
|
||||
impl RenderGraphData {
|
||||
pub fn new(device: wgpu::Device, swap_chain_descriptor: wgpu::SwapChainDescriptor, queue: wgpu::Queue, surface: wgpu::Surface) -> Self {
|
||||
pub fn new(
|
||||
device: wgpu::Device,
|
||||
swap_chain_descriptor: wgpu::SwapChainDescriptor,
|
||||
queue: wgpu::Queue,
|
||||
surface: wgpu::Surface,
|
||||
) -> Self {
|
||||
RenderGraphData {
|
||||
textures: HashMap::new(),
|
||||
samplers: HashMap::new(),
|
||||
|
@ -43,7 +48,8 @@ impl RenderGraphData {
|
|||
}
|
||||
|
||||
pub fn set_uniform_buffer(&mut self, name: &str, uniform_buffer: UniformBuffer) {
|
||||
self.uniform_buffers.insert(name.to_string(), uniform_buffer);
|
||||
self.uniform_buffers
|
||||
.insert(name.to_string(), uniform_buffer);
|
||||
}
|
||||
|
||||
pub fn get_uniform_buffer(&self, name: &str) -> Option<&UniformBuffer> {
|
||||
|
@ -51,7 +57,8 @@ impl RenderGraphData {
|
|||
}
|
||||
|
||||
pub fn set_bind_group_layout(&mut self, name: &str, bind_group_layout: wgpu::BindGroupLayout) {
|
||||
self.bind_group_layouts.insert(name.to_string(), bind_group_layout);
|
||||
self.bind_group_layouts
|
||||
.insert(name.to_string(), bind_group_layout);
|
||||
}
|
||||
|
||||
pub fn get_bind_group_layout(&self, name: &str) -> Option<&wgpu::BindGroupLayout> {
|
||||
|
@ -86,7 +93,14 @@ impl RenderGraph {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn initialize(&mut self, world: &mut World, device: wgpu::Device, swap_chain_descriptor: wgpu::SwapChainDescriptor, queue: wgpu::Queue, surface: wgpu::Surface) {
|
||||
pub fn initialize(
|
||||
&mut self,
|
||||
world: &mut World,
|
||||
device: wgpu::Device,
|
||||
swap_chain_descriptor: wgpu::SwapChainDescriptor,
|
||||
queue: wgpu::Queue,
|
||||
surface: wgpu::Surface,
|
||||
) {
|
||||
let mut data = RenderGraphData::new(device, swap_chain_descriptor, queue, surface);
|
||||
for render_resource_manager in self.render_resource_managers.iter_mut() {
|
||||
render_resource_manager.initialize(&mut data, world);
|
||||
|
@ -109,8 +123,9 @@ impl RenderGraph {
|
|||
.expect("Timeout when acquiring next swap chain texture");
|
||||
|
||||
let data = self.data.as_mut().unwrap();
|
||||
let mut encoder =
|
||||
data.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
|
||||
let mut encoder = data
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
|
||||
|
||||
for render_resource_manager in self.render_resource_managers.iter_mut() {
|
||||
render_resource_manager.update(data, &mut encoder, world);
|
||||
|
@ -144,9 +159,12 @@ impl RenderGraph {
|
|||
let data = self.data.as_mut().unwrap();
|
||||
data.swap_chain_descriptor.width = width;
|
||||
data.swap_chain_descriptor.height = height;
|
||||
let swap_chain = data.device.create_swap_chain(&data.surface, &data.swap_chain_descriptor);
|
||||
let mut encoder =
|
||||
data.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
|
||||
let swap_chain = data
|
||||
.device
|
||||
.create_swap_chain(&data.surface, &data.swap_chain_descriptor);
|
||||
let mut encoder = data
|
||||
.device
|
||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
|
||||
|
||||
for render_resource_manager in self.render_resource_managers.iter_mut() {
|
||||
render_resource_manager.resize(data, &mut encoder, world);
|
||||
|
@ -166,11 +184,19 @@ impl RenderGraph {
|
|||
swap_chain
|
||||
}
|
||||
|
||||
pub fn add_render_resource_manager(&mut self, render_resource_manager: Box<dyn RenderResourceManager>) {
|
||||
pub fn add_render_resource_manager(
|
||||
&mut self,
|
||||
render_resource_manager: Box<dyn RenderResourceManager>,
|
||||
) {
|
||||
self.render_resource_managers.push(render_resource_manager);
|
||||
}
|
||||
|
||||
pub fn set_pipeline(&mut self, pass_name: &str, pipeline_name: &str, pipeline: Box<dyn Pipeline>) {
|
||||
pub fn set_pipeline(
|
||||
&mut self,
|
||||
pass_name: &str,
|
||||
pipeline_name: &str,
|
||||
pipeline: Box<dyn Pipeline>,
|
||||
) {
|
||||
self.pipelines.insert(pipeline_name.to_string(), pipeline);
|
||||
if let None = self.pass_pipelines.get_mut(pass_name) {
|
||||
let pipelines = Vec::new();
|
||||
|
|
|
@ -3,7 +3,13 @@ use legion::world::World;
|
|||
|
||||
pub trait Pass {
|
||||
fn initialize(&self, render_graph: &mut RenderGraphData);
|
||||
fn begin<'a>(&mut self, render_graph: &mut RenderGraphData, world: &mut World, encoder: &'a mut wgpu::CommandEncoder, frame: &'a wgpu::SwapChainOutput) -> Option<wgpu::RenderPass<'a>>;
|
||||
fn begin<'a>(
|
||||
&mut self,
|
||||
render_graph: &mut RenderGraphData,
|
||||
world: &mut World,
|
||||
encoder: &'a mut wgpu::CommandEncoder,
|
||||
frame: &'a wgpu::SwapChainOutput,
|
||||
) -> Option<wgpu::RenderPass<'a>>;
|
||||
fn should_repeat(&self) -> bool;
|
||||
fn resize(&self, render_graph: &mut RenderGraphData);
|
||||
}
|
|
@ -1,10 +1,16 @@
|
|||
use crate::render::RenderGraphData;
|
||||
use legion::world::World;
|
||||
use wgpu::SwapChainOutput;
|
||||
use crate::render::RenderGraphData;
|
||||
|
||||
pub trait Pipeline {
|
||||
fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World);
|
||||
fn render(&mut self, render_graph: &RenderGraphData, pass: &mut wgpu::RenderPass, frame: &SwapChainOutput, world: &mut World);
|
||||
fn render(
|
||||
&mut self,
|
||||
render_graph: &RenderGraphData,
|
||||
pass: &mut wgpu::RenderPass,
|
||||
frame: &SwapChainOutput,
|
||||
world: &mut World,
|
||||
);
|
||||
fn resize(&mut self, render_graph: &RenderGraphData);
|
||||
fn get_pipeline(&self) -> &wgpu::RenderPipeline;
|
||||
}
|
|
@ -1,8 +1,18 @@
|
|||
use legion::world::World;
|
||||
use crate::render::RenderGraphData;
|
||||
use legion::world::World;
|
||||
|
||||
pub trait RenderResourceManager {
|
||||
fn initialize(&self, render_graph: &mut RenderGraphData, world: &mut World);
|
||||
fn update<'a>(&mut self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World);
|
||||
fn resize<'a>(&self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World);
|
||||
fn update<'a>(
|
||||
&mut self,
|
||||
render_graph: &mut RenderGraphData,
|
||||
encoder: &'a mut wgpu::CommandEncoder,
|
||||
world: &mut World,
|
||||
);
|
||||
fn resize<'a>(
|
||||
&self,
|
||||
render_graph: &mut RenderGraphData,
|
||||
encoder: &'a mut wgpu::CommandEncoder,
|
||||
world: &mut World,
|
||||
);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{render::*, math};
|
||||
use crate::{math, render::*};
|
||||
|
||||
use legion::prelude::*;
|
||||
use std::mem;
|
||||
|
@ -33,19 +33,40 @@ impl RenderResourceManager for Global2dResourceManager {
|
|||
render_graph.set_uniform_buffer(GLOBAL_2D_UNIFORM_BUFFER_NAME, uniform_buffer);
|
||||
}
|
||||
|
||||
fn update<'a>(&mut self, _render_graph: &mut RenderGraphData, _encoder: &'a mut wgpu::CommandEncoder, _world: &mut World) {
|
||||
|
||||
fn update<'a>(
|
||||
&mut self,
|
||||
_render_graph: &mut RenderGraphData,
|
||||
_encoder: &'a mut wgpu::CommandEncoder,
|
||||
_world: &mut World,
|
||||
) {
|
||||
}
|
||||
|
||||
fn resize<'a>(&self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World) {
|
||||
fn resize<'a>(
|
||||
&self,
|
||||
render_graph: &mut RenderGraphData,
|
||||
encoder: &'a mut wgpu::CommandEncoder,
|
||||
world: &mut World,
|
||||
) {
|
||||
for (mut camera, _) in <(Write<Camera>, Read<ActiveCamera2d>)>::query().iter(world) {
|
||||
camera.update(render_graph.swap_chain_descriptor.width, render_graph.swap_chain_descriptor.height);
|
||||
camera.update(
|
||||
render_graph.swap_chain_descriptor.width,
|
||||
render_graph.swap_chain_descriptor.height,
|
||||
);
|
||||
let camera_matrix: [[f32; 4]; 4] = camera.view_matrix.to_cols_array_2d();
|
||||
let matrix_size = mem::size_of::<[[f32; 4]; 4]>() as u64;
|
||||
let temp_camera_buffer =
|
||||
render_graph.device.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC);
|
||||
let global_2d_uniform_buffer = render_graph.get_uniform_buffer(GLOBAL_2D_UNIFORM_BUFFER_NAME).unwrap();
|
||||
encoder.copy_buffer_to_buffer(&temp_camera_buffer, 0, &global_2d_uniform_buffer.buffer, 0, matrix_size);
|
||||
let temp_camera_buffer = render_graph
|
||||
.device
|
||||
.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC);
|
||||
let global_2d_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(GLOBAL_2D_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&temp_camera_buffer,
|
||||
0,
|
||||
&global_2d_uniform_buffer.buffer,
|
||||
0,
|
||||
matrix_size,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{render::*, render::passes::ForwardUniforms, LocalToWorld, math};
|
||||
use crate::{math, render::passes::ForwardUniforms, render::*, LocalToWorld};
|
||||
|
||||
use legion::prelude::*;
|
||||
use std::mem;
|
||||
|
@ -28,18 +28,42 @@ impl RenderResourceManager for GlobalResourceManager {
|
|||
};
|
||||
render_graph.set_uniform_buffer(FORWARD_UNIFORM_BUFFER_NAME, uniform_buffer);
|
||||
}
|
||||
fn update<'a>(&mut self, _render_graph: &mut RenderGraphData, _encoder: &'a mut wgpu::CommandEncoder, _world: &mut World) {
|
||||
|
||||
fn update<'a>(
|
||||
&mut self,
|
||||
_render_graph: &mut RenderGraphData,
|
||||
_encoder: &'a mut wgpu::CommandEncoder,
|
||||
_world: &mut World,
|
||||
) {
|
||||
}
|
||||
fn resize<'a>(&self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World) {
|
||||
for (mut camera, local_to_world, _) in <(Write<Camera>, Read<LocalToWorld>, Read<ActiveCamera>)>::query().iter(world) {
|
||||
camera.update(render_graph.swap_chain_descriptor.width, render_graph.swap_chain_descriptor.height);
|
||||
let camera_matrix: [[f32; 4]; 4] = (camera.view_matrix * local_to_world.0).to_cols_array_2d();
|
||||
fn resize<'a>(
|
||||
&self,
|
||||
render_graph: &mut RenderGraphData,
|
||||
encoder: &'a mut wgpu::CommandEncoder,
|
||||
world: &mut World,
|
||||
) {
|
||||
for (mut camera, local_to_world, _) in
|
||||
<(Write<Camera>, Read<LocalToWorld>, Read<ActiveCamera>)>::query().iter(world)
|
||||
{
|
||||
camera.update(
|
||||
render_graph.swap_chain_descriptor.width,
|
||||
render_graph.swap_chain_descriptor.height,
|
||||
);
|
||||
let camera_matrix: [[f32; 4]; 4] =
|
||||
(camera.view_matrix * local_to_world.0).to_cols_array_2d();
|
||||
let matrix_size = mem::size_of::<[[f32; 4]; 4]>() as u64;
|
||||
let temp_camera_buffer =
|
||||
render_graph.device.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC);
|
||||
let forward_uniform_buffer = render_graph.get_uniform_buffer(FORWARD_UNIFORM_BUFFER_NAME).unwrap();
|
||||
encoder.copy_buffer_to_buffer(&temp_camera_buffer, 0, &forward_uniform_buffer.buffer, 0, matrix_size);
|
||||
let temp_camera_buffer = render_graph
|
||||
.device
|
||||
.create_buffer_with_data(camera_matrix.as_bytes(), wgpu::BufferUsage::COPY_SRC);
|
||||
let forward_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(FORWARD_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&temp_camera_buffer,
|
||||
0,
|
||||
&forward_uniform_buffer.buffer,
|
||||
0,
|
||||
matrix_size,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -37,7 +37,12 @@ impl RenderResourceManager for LightResourceManager {
|
|||
|
||||
render_graph.set_uniform_buffer(LIGHT_UNIFORM_BUFFER_NAME, light_uniform_buffer);
|
||||
}
|
||||
fn update<'a>(&mut self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World) {
|
||||
fn update<'a>(
|
||||
&mut self,
|
||||
render_graph: &mut RenderGraphData,
|
||||
encoder: &'a mut wgpu::CommandEncoder,
|
||||
world: &mut World,
|
||||
) {
|
||||
if self.lights_are_dirty {
|
||||
let mut light_query = <(Read<Light>, Read<LocalToWorld>, Read<Translation>)>::query();
|
||||
let light_count = light_query.iter(world).count();
|
||||
|
@ -49,16 +54,21 @@ impl RenderResourceManager for LightResourceManager {
|
|||
self.lights_are_dirty = false;
|
||||
let size = mem::size_of::<LightRaw>();
|
||||
let total_size = size * light_count;
|
||||
let temp_buf_data =
|
||||
render_graph.device.create_buffer_mapped(total_size, wgpu::BufferUsage::COPY_SRC);
|
||||
let temp_buf_data = render_graph
|
||||
.device
|
||||
.create_buffer_mapped(total_size, wgpu::BufferUsage::COPY_SRC);
|
||||
for ((light, local_to_world, translation), slot) in light_query
|
||||
.iter(world)
|
||||
.zip(temp_buf_data.data.chunks_exact_mut(size))
|
||||
{
|
||||
slot.copy_from_slice(LightRaw::from(&light, &local_to_world.0, &translation).as_bytes());
|
||||
slot.copy_from_slice(
|
||||
LightRaw::from(&light, &local_to_world.0, &translation).as_bytes(),
|
||||
);
|
||||
}
|
||||
|
||||
let light_uniform_buffer = render_graph.get_uniform_buffer(LIGHT_UNIFORM_BUFFER_NAME).unwrap();
|
||||
let light_uniform_buffer = render_graph
|
||||
.get_uniform_buffer(LIGHT_UNIFORM_BUFFER_NAME)
|
||||
.unwrap();
|
||||
encoder.copy_buffer_to_buffer(
|
||||
&temp_buf_data.finish(),
|
||||
0,
|
||||
|
@ -66,8 +76,13 @@ impl RenderResourceManager for LightResourceManager {
|
|||
0,
|
||||
total_size as wgpu::BufferAddress,
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
fn resize<'a>(&self, _render_graph: &mut RenderGraphData, _encoder: &'a mut wgpu::CommandEncoder, _world: &mut World) { }
|
||||
fn resize<'a>(
|
||||
&self,
|
||||
_render_graph: &mut RenderGraphData,
|
||||
_encoder: &'a mut wgpu::CommandEncoder,
|
||||
_world: &mut World,
|
||||
) {
|
||||
}
|
||||
}
|
|
@ -11,7 +11,9 @@ pub struct MaterialResourceManager;
|
|||
impl RenderResourceManager for MaterialResourceManager {
|
||||
fn initialize(&self, render_graph: &mut RenderGraphData, _world: &mut World) {
|
||||
let material_bind_group_layout =
|
||||
render_graph.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
||||
|
@ -19,22 +21,30 @@ impl RenderResourceManager for MaterialResourceManager {
|
|||
}],
|
||||
});
|
||||
|
||||
render_graph.set_bind_group_layout(MATERIAL_BIND_GROUP_LAYOUT_NAME, material_bind_group_layout);
|
||||
render_graph
|
||||
.set_bind_group_layout(MATERIAL_BIND_GROUP_LAYOUT_NAME, material_bind_group_layout);
|
||||
}
|
||||
|
||||
fn update<'a>(&mut self, render_graph: &mut RenderGraphData, encoder: &'a mut wgpu::CommandEncoder, world: &mut World) {
|
||||
let mut entities = <(Write<Material>, Read<LocalToWorld>)>::query()
|
||||
.filter(!component::<Instanced>());
|
||||
fn update<'a>(
|
||||
&mut self,
|
||||
render_graph: &mut RenderGraphData,
|
||||
encoder: &'a mut wgpu::CommandEncoder,
|
||||
world: &mut World,
|
||||
) {
|
||||
let mut entities =
|
||||
<(Write<Material>, Read<LocalToWorld>)>::query().filter(!component::<Instanced>());
|
||||
let entities_count = entities.iter(world).count();
|
||||
if entities_count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let size = mem::size_of::<MaterialUniforms>();
|
||||
let temp_buf_data = render_graph.device
|
||||
let temp_buf_data = render_graph
|
||||
.device
|
||||
.create_buffer_mapped(entities_count * size, wgpu::BufferUsage::COPY_SRC);
|
||||
|
||||
for ((material, transform), slot) in entities.iter(world)
|
||||
for ((material, transform), slot) in entities
|
||||
.iter(world)
|
||||
.zip(temp_buf_data.data.chunks_exact_mut(size))
|
||||
{
|
||||
slot.copy_from_slice(
|
||||
|
@ -46,23 +56,32 @@ impl RenderResourceManager for MaterialResourceManager {
|
|||
);
|
||||
}
|
||||
|
||||
let material_bind_group_layout = render_graph.get_bind_group_layout(MATERIAL_BIND_GROUP_LAYOUT_NAME).unwrap();
|
||||
let material_bind_group_layout = render_graph
|
||||
.get_bind_group_layout(MATERIAL_BIND_GROUP_LAYOUT_NAME)
|
||||
.unwrap();
|
||||
|
||||
for mut material in <Write<Material>>::query().filter(!component::<Instanced>()).iter(world) {
|
||||
for mut material in <Write<Material>>::query()
|
||||
.filter(!component::<Instanced>())
|
||||
.iter(world)
|
||||
{
|
||||
if let None = material.bind_group {
|
||||
let material_uniform_size = mem::size_of::<MaterialUniforms>() as wgpu::BufferAddress;
|
||||
let material_uniform_size =
|
||||
mem::size_of::<MaterialUniforms>() as wgpu::BufferAddress;
|
||||
let uniform_buf = render_graph.device.create_buffer(&wgpu::BufferDescriptor {
|
||||
size: material_uniform_size,
|
||||
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
});
|
||||
|
||||
let bind_group = render_graph.device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
let bind_group =
|
||||
render_graph
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: material_bind_group_layout,
|
||||
bindings: &[wgpu::Binding {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::Buffer {
|
||||
buffer: &uniform_buf,
|
||||
range: 0 .. material_uniform_size,
|
||||
range: 0..material_uniform_size,
|
||||
},
|
||||
}],
|
||||
});
|
||||
|
@ -83,7 +102,11 @@ impl RenderResourceManager for MaterialResourceManager {
|
|||
);
|
||||
}
|
||||
}
|
||||
fn resize<'a>(&self, _render_graph: &mut RenderGraphData, _encoder: &'a mut wgpu::CommandEncoder, _world: &mut World) {
|
||||
|
||||
fn resize<'a>(
|
||||
&self,
|
||||
_render_graph: &mut RenderGraphData,
|
||||
_encoder: &'a mut wgpu::CommandEncoder,
|
||||
_world: &mut World,
|
||||
) {
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
mod light_resource_manager;
|
||||
mod global_resource_manager;
|
||||
mod global_2d_resource_manager;
|
||||
mod global_resource_manager;
|
||||
mod light_resource_manager;
|
||||
mod material_resource_manager;
|
||||
|
||||
pub use light_resource_manager::*;
|
||||
pub use global_resource_manager::*;
|
||||
pub use global_2d_resource_manager::*;
|
||||
pub use global_resource_manager::*;
|
||||
pub use light_resource_manager::*;
|
||||
pub use material_resource_manager::*;
|
|
@ -1,5 +1,5 @@
|
|||
use zerocopy::{AsBytes, FromBytes};
|
||||
use std::convert::From;
|
||||
use zerocopy::{AsBytes, FromBytes};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
||||
|
@ -20,7 +20,12 @@ impl From<([f32; 4], [f32; 4])> for Vertex {
|
|||
impl From<([f32; 3], [f32; 3])> for Vertex {
|
||||
fn from((position, normal): ([f32; 3], [f32; 3])) -> Self {
|
||||
Vertex {
|
||||
position: [position[0] as f32, position[1] as f32, position[2] as f32, 1.0],
|
||||
position: [
|
||||
position[0] as f32,
|
||||
position[1] as f32,
|
||||
position[2] as f32,
|
||||
1.0,
|
||||
],
|
||||
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0],
|
||||
}
|
||||
}
|
||||
|
@ -29,8 +34,18 @@ impl From<([f32; 3], [f32; 3])> for Vertex {
|
|||
impl From<([i8; 4], [i8; 4])> for Vertex {
|
||||
fn from((position, normal): ([i8; 4], [i8; 4])) -> Self {
|
||||
Vertex {
|
||||
position: [position[0] as f32, position[1] as f32, position[2] as f32, position[3] as f32],
|
||||
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, normal[3] as f32],
|
||||
position: [
|
||||
position[0] as f32,
|
||||
position[1] as f32,
|
||||
position[2] as f32,
|
||||
position[3] as f32,
|
||||
],
|
||||
normal: [
|
||||
normal[0] as f32,
|
||||
normal[1] as f32,
|
||||
normal[2] as f32,
|
||||
normal[3] as f32,
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +53,12 @@ impl From<([i8; 4], [i8; 4])> for Vertex {
|
|||
impl From<([i8; 3], [i8; 3])> for Vertex {
|
||||
fn from((position, normal): ([i8; 3], [i8; 3])) -> Self {
|
||||
Vertex {
|
||||
position: [position[0] as f32, position[1] as f32, position[2] as f32, 1.0],
|
||||
position: [
|
||||
position[0] as f32,
|
||||
position[1] as f32,
|
||||
position[2] as f32,
|
||||
1.0,
|
||||
],
|
||||
normal: [normal[0] as f32, normal[1] as f32, normal[2] as f32, 0.0],
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue