mirror of
https://github.com/hannobraun/Fornjot
synced 2025-05-07 19:38:29 +00:00
Move more code into pipelines
This commit is contained in:
parent
4e3c0deda2
commit
b2fb28311e
@ -3,7 +3,10 @@ mod pipeline;
|
|||||||
pub mod triangles;
|
pub mod triangles;
|
||||||
pub mod vertices;
|
pub mod vertices;
|
||||||
|
|
||||||
use glam::Mat4;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
|
use glam::{Mat4, Vec3};
|
||||||
|
use wgpu::util::DeviceExt;
|
||||||
|
|
||||||
pub use self::pipeline::Pipeline;
|
pub use self::pipeline::Pipeline;
|
||||||
|
|
||||||
@ -16,19 +19,28 @@ impl Pipelines {
|
|||||||
pub fn new(
|
pub fn new(
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
config: &wgpu::SurfaceConfiguration,
|
config: &wgpu::SurfaceConfiguration,
|
||||||
uniforms: &wgpu::Buffer,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let aspect_ratio = config.width as f32 / config.height as f32;
|
||||||
|
let uniforms =
|
||||||
|
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: None,
|
||||||
|
contents: bytemuck::cast_slice(&[Uniforms::from_transform(
|
||||||
|
default_transform(aspect_ratio),
|
||||||
|
)]),
|
||||||
|
usage: wgpu::BufferUsages::UNIFORM,
|
||||||
|
});
|
||||||
|
|
||||||
let vertices = Pipeline::new(
|
let vertices = Pipeline::new(
|
||||||
device,
|
device,
|
||||||
config,
|
config,
|
||||||
wgpu::include_wgsl!("shaders/vertices.wgsl"),
|
wgpu::include_wgsl!("shaders/vertices.wgsl"),
|
||||||
uniforms,
|
&uniforms,
|
||||||
);
|
);
|
||||||
let triangles = Pipeline::new(
|
let triangles = Pipeline::new(
|
||||||
device,
|
device,
|
||||||
config,
|
config,
|
||||||
wgpu::include_wgsl!("shaders/triangles.wgsl"),
|
wgpu::include_wgsl!("shaders/triangles.wgsl"),
|
||||||
uniforms,
|
&uniforms,
|
||||||
);
|
);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
@ -55,3 +67,14 @@ impl Uniforms {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_transform(aspect_ratio: f32) -> Mat4 {
|
||||||
|
let fov_y_radians = std::f32::consts::PI / 2.;
|
||||||
|
let z_near = 0.1;
|
||||||
|
let z_far = 10.;
|
||||||
|
|
||||||
|
Mat4::perspective_rh(fov_y_radians, aspect_ratio, z_near, z_far)
|
||||||
|
* Mat4::from_translation(Vec3::new(0., 0., -2.))
|
||||||
|
* Mat4::from_rotation_x(-PI / 4.)
|
||||||
|
* Mat4::from_rotation_z(PI / 4.)
|
||||||
|
}
|
||||||
|
@ -1,16 +1,11 @@
|
|||||||
use std::{f32::consts::PI, sync::Arc};
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use glam::{Mat4, Vec3};
|
|
||||||
use wgpu::util::DeviceExt;
|
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
|
|
||||||
use crate::geometry::Operation;
|
use crate::geometry::Operation;
|
||||||
|
|
||||||
use super::{
|
use super::{geometry::Geometry, pipelines::Pipelines};
|
||||||
geometry::Geometry,
|
|
||||||
pipelines::{Pipelines, Uniforms},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
pub surface: wgpu::Surface<'static>,
|
pub surface: wgpu::Surface<'static>,
|
||||||
@ -49,17 +44,7 @@ impl Renderer {
|
|||||||
.ok_or_else(|| anyhow!("Failed to get default surface config"))?;
|
.ok_or_else(|| anyhow!("Failed to get default surface config"))?;
|
||||||
surface.configure(&device, &config);
|
surface.configure(&device, &config);
|
||||||
|
|
||||||
let aspect_ratio = size.width as f32 / size.height as f32;
|
let pipelines = Pipelines::new(&device, &config);
|
||||||
let uniforms =
|
|
||||||
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
|
||||||
label: None,
|
|
||||||
contents: bytemuck::cast_slice(&[Uniforms::from_transform(
|
|
||||||
default_transform(aspect_ratio),
|
|
||||||
)]),
|
|
||||||
usage: wgpu::BufferUsages::UNIFORM,
|
|
||||||
});
|
|
||||||
|
|
||||||
let pipelines = Pipelines::new(&device, &config, &uniforms);
|
|
||||||
|
|
||||||
let depth_view = {
|
let depth_view = {
|
||||||
let depth_texture =
|
let depth_texture =
|
||||||
@ -148,14 +133,3 @@ impl Renderer {
|
|||||||
frame.present();
|
frame.present();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_transform(aspect_ratio: f32) -> Mat4 {
|
|
||||||
let fov_y_radians = std::f32::consts::PI / 2.;
|
|
||||||
let z_near = 0.1;
|
|
||||||
let z_far = 10.;
|
|
||||||
|
|
||||||
Mat4::perspective_rh(fov_y_radians, aspect_ratio, z_near, z_far)
|
|
||||||
* Mat4::from_translation(Vec3::new(0., 0., -2.))
|
|
||||||
* Mat4::from_rotation_x(-PI / 4.)
|
|
||||||
* Mat4::from_rotation_z(PI / 4.)
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user