Added rendering pipeline and window
This commit is contained in:
878
Cargo.lock
generated
878
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -2,10 +2,13 @@
|
|||||||
name = "rust-opengl"
|
name = "rust-opengl"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
cfg-if = "1"
|
||||||
nalgebra = "0.32.3"
|
nalgebra = "0.32.3"
|
||||||
wgpu = "0.18.0"
|
winit = "0.28"
|
||||||
winit = "0.29.3"
|
env_logger = "0.10"
|
||||||
|
log = "0.4"
|
||||||
|
pollster = "0.3"
|
||||||
|
wgpu = "0.17"
|
||||||
|
|||||||
12
src/main.rs
12
src/main.rs
@@ -3,18 +3,14 @@
|
|||||||
//Use our ray module
|
//Use our ray module
|
||||||
mod ray;
|
mod ray;
|
||||||
use ray::Ray;
|
use ray::Ray;
|
||||||
|
//Use our window
|
||||||
|
mod window;
|
||||||
|
use window::run;
|
||||||
//Use linear algebra module
|
//Use linear algebra module
|
||||||
use nalgebra::Vector4;
|
use nalgebra::Vector4;
|
||||||
//Use modules for wgpu
|
//Use modules for wgpu
|
||||||
use std::borrow::Cow;
|
|
||||||
use winit::{
|
|
||||||
event::{Event, WindowEvent},
|
|
||||||
event_loop::EventLoop,
|
|
||||||
window::Window,
|
|
||||||
};
|
|
||||||
|
|
||||||
//MAIN ---------------------------------------------------------------------------------
|
//MAIN ---------------------------------------------------------------------------------
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello world");
|
pollster::block_on(run());
|
||||||
}
|
}
|
||||||
//NEXT ---------------------------------------------------------------------------------
|
|
||||||
|
|||||||
25
src/shader.wgsl
Normal file
25
src/shader.wgsl
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
// Vertex shader
|
||||||
|
|
||||||
|
struct VertexOutput {
|
||||||
|
@builtin(position) clip_position: vec4<f32>,
|
||||||
|
@location(0) color: vec4<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
@vertex
|
||||||
|
fn vs_main(@builtin(vertex_index) in_vertex_index: u32,) -> VertexOutput {
|
||||||
|
var out: VertexOutput;
|
||||||
|
let x = f32(1 - i32(in_vertex_index)) * 0.5;
|
||||||
|
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
|
||||||
|
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
|
||||||
|
out.color = vec4<f32>(1.0/(f32(in_vertex_index)+1.0),1.0/(f32(in_vertex_index)+1.0),1.0/(f32(in_vertex_index)+1.0),1.0);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
@fragment
|
||||||
|
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||||
|
return in.color;
|
||||||
|
}
|
||||||
|
fn fs_alt(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||||
|
return vec4<f32>{0.3f, 0.7f, 0.7f};
|
||||||
|
}
|
||||||
368
src/window.rs
Normal file
368
src/window.rs
Normal file
@@ -0,0 +1,368 @@
|
|||||||
|
use std::iter;
|
||||||
|
|
||||||
|
use winit::{
|
||||||
|
event::*,
|
||||||
|
event_loop::{ControlFlow, EventLoop},
|
||||||
|
window::Window,
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct State {
|
||||||
|
//Surface we will draw two
|
||||||
|
surface: wgpu::Surface,
|
||||||
|
// Description of a surface
|
||||||
|
config: wgpu::SurfaceConfiguration,
|
||||||
|
//Device we will he using to render
|
||||||
|
device: wgpu::Device,
|
||||||
|
// Command query for a divice
|
||||||
|
queue: wgpu::Queue,
|
||||||
|
size: winit::dpi::PhysicalSize<u32>,
|
||||||
|
// The window must be declared after the surface so
|
||||||
|
// it gets dropped after it as the surface contains
|
||||||
|
// unsafe references to the window's resources.
|
||||||
|
window: winit::window::Window,
|
||||||
|
// Handle to a rendering pipeline
|
||||||
|
render_pipeline1: wgpu::RenderPipeline,
|
||||||
|
render_pipeline2: wgpu::RenderPipeline,
|
||||||
|
// Clear color
|
||||||
|
clear_color: wgpu::Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
// Creating some of the wgpu types requires async code
|
||||||
|
async fn new(window: Window) -> Self {
|
||||||
|
//Self explaining, the window size
|
||||||
|
let size = window.inner_size();
|
||||||
|
// Backends to handle our gpu
|
||||||
|
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||||
|
backends: wgpu::Backends::all(),
|
||||||
|
dx12_shader_compiler: Default::default(),
|
||||||
|
});
|
||||||
|
// Surface needs to be alive as long as window is created
|
||||||
|
let surface = unsafe { instance.create_surface(&window) }.unwrap();
|
||||||
|
// Handles our graphics card, gets backend the adapter uses
|
||||||
|
// Compatible surface fields tells wgpu whats a adapter that can supply a surface
|
||||||
|
let adapter = instance
|
||||||
|
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||||
|
power_preference: wgpu::PowerPreference::default(),
|
||||||
|
compatible_surface: Some(&surface),
|
||||||
|
force_fallback_adapter: false,
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
// Use the adapter to create a device and queue
|
||||||
|
// Includes limitations on the graphics card
|
||||||
|
let (device, queue) = adapter
|
||||||
|
.request_device(
|
||||||
|
&wgpu::DeviceDescriptor {
|
||||||
|
features: wgpu::Features::empty(),
|
||||||
|
// WebGL doesn't support all of wgpu's features, so if
|
||||||
|
// we're building for the web we'll have to disable some.
|
||||||
|
limits: if cfg!(target_arch = "wasm32") {
|
||||||
|
wgpu::Limits::downlevel_webgl2_defaults()
|
||||||
|
} else {
|
||||||
|
wgpu::Limits::default()
|
||||||
|
},
|
||||||
|
label: None,
|
||||||
|
},
|
||||||
|
None, // Trace path
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
// Some surfaces have different capabilities, get the capabilities
|
||||||
|
let surface_caps = surface.get_capabilities(&adapter);
|
||||||
|
// Find how surface textures will be stored on the GPU
|
||||||
|
let surface_format = surface_caps
|
||||||
|
.formats
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.find(|f| f.is_srgb())
|
||||||
|
.unwrap_or(surface_caps.formats[0]);
|
||||||
|
let config = wgpu::SurfaceConfiguration {
|
||||||
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||||
|
format: surface_format,
|
||||||
|
width: size.width,
|
||||||
|
height: size.height,
|
||||||
|
present_mode: surface_caps.present_modes[0],
|
||||||
|
alpha_mode: surface_caps.alpha_modes[0],
|
||||||
|
view_formats: vec![],
|
||||||
|
};
|
||||||
|
surface.configure(&device, &config);
|
||||||
|
//Load a shader into our device and get a module handler
|
||||||
|
let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl"));
|
||||||
|
//Create pipeline layout using device
|
||||||
|
let render_pipeline_layout =
|
||||||
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
|
label: Some("Render Pipeline Layout"),
|
||||||
|
bind_group_layouts: &[],
|
||||||
|
push_constant_ranges: &[],
|
||||||
|
});
|
||||||
|
//Create final render pipeline
|
||||||
|
let render_pipeline1 = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||||
|
label: Some("Render Pipeline"),
|
||||||
|
layout: Some(&render_pipeline_layout),
|
||||||
|
vertex: wgpu::VertexState {
|
||||||
|
module: &shader,
|
||||||
|
entry_point: "vs_main", // 1.
|
||||||
|
buffers: &[], // 2.
|
||||||
|
},
|
||||||
|
fragment: Some(wgpu::FragmentState {
|
||||||
|
// 3.
|
||||||
|
module: &shader,
|
||||||
|
entry_point: "fs_main",
|
||||||
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
|
// 4.
|
||||||
|
format: config.format,
|
||||||
|
blend: Some(wgpu::BlendState::REPLACE),
|
||||||
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
|
})],
|
||||||
|
}),
|
||||||
|
primitive: wgpu::PrimitiveState {
|
||||||
|
topology: wgpu::PrimitiveTopology::TriangleList, // 1.
|
||||||
|
strip_index_format: None,
|
||||||
|
front_face: wgpu::FrontFace::Ccw, // 2.
|
||||||
|
cull_mode: Some(wgpu::Face::Back),
|
||||||
|
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||||
|
polygon_mode: wgpu::PolygonMode::Fill,
|
||||||
|
// Requires Features::DEPTH_CLIP_CONTROL
|
||||||
|
unclipped_depth: false,
|
||||||
|
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||||
|
conservative: false,
|
||||||
|
},
|
||||||
|
depth_stencil: None, // 1.
|
||||||
|
multisample: wgpu::MultisampleState {
|
||||||
|
count: 1, // 2.
|
||||||
|
mask: !0, // 3.
|
||||||
|
alpha_to_coverage_enabled: false, // 4.
|
||||||
|
},
|
||||||
|
multiview: None, // 5.
|
||||||
|
});
|
||||||
|
let render_pipeline2 = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||||
|
label: Some("Render Pipeline"),
|
||||||
|
layout: Some(&render_pipeline_layout),
|
||||||
|
vertex: wgpu::VertexState {
|
||||||
|
module: &shader,
|
||||||
|
entry_point: "vs_main", // 1.
|
||||||
|
buffers: &[], // 2.
|
||||||
|
},
|
||||||
|
fragment: Some(wgpu::FragmentState {
|
||||||
|
// 3.
|
||||||
|
module: &shader,
|
||||||
|
entry_point: "fs_main",
|
||||||
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
|
// 4.
|
||||||
|
format: config.format,
|
||||||
|
blend: Some(wgpu::BlendState::REPLACE),
|
||||||
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
|
})],
|
||||||
|
}),
|
||||||
|
primitive: wgpu::PrimitiveState {
|
||||||
|
topology: wgpu::PrimitiveTopology::TriangleList, // 1.
|
||||||
|
strip_index_format: None,
|
||||||
|
front_face: wgpu::FrontFace::Ccw, // 2.
|
||||||
|
cull_mode: Some(wgpu::Face::Back),
|
||||||
|
// Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
|
||||||
|
polygon_mode: wgpu::PolygonMode::Fill,
|
||||||
|
// Requires Features::DEPTH_CLIP_CONTROL
|
||||||
|
unclipped_depth: false,
|
||||||
|
// Requires Features::CONSERVATIVE_RASTERIZATION
|
||||||
|
conservative: false,
|
||||||
|
},
|
||||||
|
depth_stencil: None, // 1.
|
||||||
|
multisample: wgpu::MultisampleState {
|
||||||
|
count: 1, // 2.
|
||||||
|
mask: !0, // 3.
|
||||||
|
alpha_to_coverage_enabled: false, // 4.
|
||||||
|
},
|
||||||
|
multiview: None, // 5.
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
let clear_color = wgpu::Color {
|
||||||
|
r: 0.0,
|
||||||
|
g: 0.2,
|
||||||
|
b: 0.4,
|
||||||
|
a: 1.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
Self {
|
||||||
|
window,
|
||||||
|
surface,
|
||||||
|
device,
|
||||||
|
queue,
|
||||||
|
config,
|
||||||
|
size,
|
||||||
|
render_pipeline,
|
||||||
|
clear_color,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn window(&self) -> &Window {
|
||||||
|
&self.window
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
|
||||||
|
if new_size.width > 0 && new_size.height > 0 {
|
||||||
|
self.size = new_size;
|
||||||
|
self.config.width = new_size.width;
|
||||||
|
self.config.height = new_size.height;
|
||||||
|
self.surface.configure(&self.device, &self.config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn input(&mut self, event: &WindowEvent) -> bool {
|
||||||
|
match event {
|
||||||
|
WindowEvent::CursorLeft { .. } => {
|
||||||
|
ren
|
||||||
|
self.clear_color = wgpu::Color {
|
||||||
|
r: 0.5, // Example values for when mouse is clicked
|
||||||
|
g: 0.5,
|
||||||
|
b: 0.5,
|
||||||
|
a: 1.0,
|
||||||
|
};
|
||||||
|
true
|
||||||
|
}
|
||||||
|
WindowEvent::KeyboardInput { input, .. } => {
|
||||||
|
if let Some(virtual_keycode) = input.virtual_keycode {
|
||||||
|
match virtual_keycode {
|
||||||
|
VirtualKeyCode::Space => {
|
||||||
|
|
||||||
|
}
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self) {}
|
||||||
|
|
||||||
|
fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
|
||||||
|
let output = self.surface.get_current_texture()?;
|
||||||
|
|
||||||
|
let view = output
|
||||||
|
.texture
|
||||||
|
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
|
||||||
|
let mut encoder = self
|
||||||
|
.device
|
||||||
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||||
|
label: Some("Render Encoder"),
|
||||||
|
});
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
|
label: Some("Render Pass"),
|
||||||
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||||
|
view: &view,
|
||||||
|
resolve_target: None,
|
||||||
|
ops: wgpu::Operations {
|
||||||
|
load: wgpu::LoadOp::Clear(self.clear_color),
|
||||||
|
store: true,
|
||||||
|
},
|
||||||
|
})],
|
||||||
|
depth_stencil_attachment: None,
|
||||||
|
});
|
||||||
|
|
||||||
|
render_pass.set_pipeline(&self.render_pipeline); // 2.
|
||||||
|
render_pass.draw(0..3, 0..1); // 3.
|
||||||
|
}
|
||||||
|
|
||||||
|
// submit will accept anything that implements IntoIter
|
||||||
|
self.queue.submit(std::iter::once(encoder.finish()));
|
||||||
|
output.present();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub async fn run() {
|
||||||
|
cfg_if::cfg_if! {
|
||||||
|
if #[cfg(target_arch = "wasm32")] {
|
||||||
|
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||||
|
console_log::init_with_level(log::Level::Warn).expect("Couldn't initialize logger");
|
||||||
|
} else {
|
||||||
|
env_logger::init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let event_loop = EventLoop::new();
|
||||||
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
{
|
||||||
|
// Winit prevents sizing with CSS, so we have to set
|
||||||
|
// the size manually when on web.
|
||||||
|
use winit::dpi::PhysicalSize;
|
||||||
|
window.set_inner_size(PhysicalSize::new(450, 400));
|
||||||
|
|
||||||
|
use winit::platform::web::WindowExtWebSys;
|
||||||
|
web_sys::window()
|
||||||
|
.and_then(|win| win.document())
|
||||||
|
.and_then(|doc| {
|
||||||
|
let dst = doc.get_element_by_id("wasm-example")?;
|
||||||
|
let canvas = web_sys::Element::from(window.canvas());
|
||||||
|
dst.append_child(&canvas).ok()?;
|
||||||
|
Some(())
|
||||||
|
})
|
||||||
|
.expect("Couldn't append canvas to document body.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// State::new uses async code, so we're going to wait for it to finish
|
||||||
|
let mut state = State::new(window).await;
|
||||||
|
|
||||||
|
event_loop.run(move |event, _, control_flow| {
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
ref event,
|
||||||
|
window_id,
|
||||||
|
} if window_id == state.window().id() => {
|
||||||
|
if !state.input(event) {
|
||||||
|
// UPDATED!
|
||||||
|
match event {
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
input:
|
||||||
|
KeyboardInput {
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => *control_flow = ControlFlow::Exit,
|
||||||
|
WindowEvent::Resized(physical_size) => {
|
||||||
|
state.resize(*physical_size);
|
||||||
|
}
|
||||||
|
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||||
|
// new_inner_size is &&mut so w have to dereference it twice
|
||||||
|
state.resize(**new_inner_size);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::RedrawRequested(window_id) if window_id == state.window().id() => {
|
||||||
|
state.update();
|
||||||
|
match state.render() {
|
||||||
|
Ok(_) => {}
|
||||||
|
// Reconfigure the surface if it's lost or outdated
|
||||||
|
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
|
||||||
|
state.resize(state.size)
|
||||||
|
}
|
||||||
|
// The system is out of memory, we should probably quit
|
||||||
|
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||||
|
|
||||||
|
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::RedrawEventsCleared => {
|
||||||
|
// RedrawRequested will only trigger once, unless we manually
|
||||||
|
// request it.
|
||||||
|
state.window().request_redraw();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user