fuck what I wanted to do
This commit is contained in:
197
src/display.rs
197
src/display.rs
@@ -1,197 +0,0 @@
|
||||
use std::borrow::Cow;
|
||||
use std::fs;
|
||||
use std::iter;
|
||||
|
||||
use wgpu::ShaderModuleDescriptor;
|
||||
use winit::{
|
||||
event::*,
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
window::Window,
|
||||
window::WindowBuilder,
|
||||
};
|
||||
|
||||
pub struct Display {
|
||||
//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_pipeline: wgpu::RenderPipeline,
|
||||
clear_color: wgpu::Color,
|
||||
}
|
||||
|
||||
impl Display {
|
||||
// Creating some of the wgpu types requires async code
|
||||
pub async fn new(window: Window, shader_filename: String) -> 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: 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_contents =
|
||||
fs::read_to_string(shader_filename).expect("Failed to read shader file");
|
||||
|
||||
let shader = device.create_shader_module(ShaderModuleDescriptor {
|
||||
label: Some("Shader.wgsl"),
|
||||
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(&shader_contents)),
|
||||
});
|
||||
|
||||
//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_pipeline = 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.0,
|
||||
b: 0.0,
|
||||
a: 1.0,
|
||||
};
|
||||
|
||||
Self {
|
||||
window,
|
||||
surface,
|
||||
device,
|
||||
queue,
|
||||
config,
|
||||
size,
|
||||
render_pipeline,
|
||||
clear_color,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Display {
|
||||
pub fn render(
|
||||
&mut self,
|
||||
render_function: fn(
|
||||
&wgpu::Device,
|
||||
&wgpu::Queue,
|
||||
&wgpu::SurfaceTexture,
|
||||
&wgpu::RenderPipeline,
|
||||
) -> Result<(), wgpu::SurfaceError>,
|
||||
) -> Result<(), wgpu::SurfaceError> {
|
||||
let texture = &self.surface.get_current_texture().expect("All good");
|
||||
render_function(&self.device, &self.queue, &texture, &self.render_pipeline)
|
||||
}
|
||||
}
|
||||
impl Display {
|
||||
pub fn window(&self) -> &Window {
|
||||
&self.window
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
|
||||
self.size = new_size;
|
||||
self.config.width = new_size.width;
|
||||
self.config.height = new_size.height;
|
||||
self.surface.configure(&self.device, &self.config);
|
||||
}
|
||||
|
||||
pub fn get_size(&self) -> winit::dpi::PhysicalSize<u32> {
|
||||
self.size
|
||||
}
|
||||
|
||||
pub fn update_surface(&self) {
|
||||
self.resize(self.get_size())
|
||||
}
|
||||
}
|
||||
145
src/main.rs
145
src/main.rs
@@ -1,146 +1,11 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_variables)]
|
||||
//Use our ray module
|
||||
mod ray;
|
||||
use ray::Ray;
|
||||
//Use our display
|
||||
pub mod display;
|
||||
use display::Display;
|
||||
//Use linear algebra module
|
||||
use nalgebra::Vector4;
|
||||
//Use modules for wgpu
|
||||
use std::iter;
|
||||
use winit::{
|
||||
event::*,
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
window::Window,
|
||||
window::WindowBuilder,
|
||||
};
|
||||
|
||||
pub struct Program {
|
||||
display: Display,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
pub async fn new() -> Self {
|
||||
let event_loop = EventLoop::new();
|
||||
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||
let display = Display::new(window, "shader.wgsl".to_string()).await;
|
||||
Program { display }
|
||||
}
|
||||
impl Program {}
|
||||
async fn start_event_loop(mut self) {
|
||||
let event_loop = EventLoop::new();
|
||||
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
||||
|
||||
event_loop.run(move |event, _, control_flow| match event {
|
||||
Event::WindowEvent {
|
||||
ref event,
|
||||
window_id,
|
||||
} if window_id == window.id() => {
|
||||
if !self.input(event) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested
|
||||
| WindowEvent::KeyboardInput {
|
||||
input:
|
||||
KeyboardInput {
|
||||
state: ElementState::Pressed,
|
||||
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||||
..
|
||||
},
|
||||
..
|
||||
} => *control_flow = ControlFlow::Exit,
|
||||
WindowEvent::Resized(physical_size) => {
|
||||
self.display.resize(*physical_size);
|
||||
}
|
||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||
self.display.resize(**new_inner_size);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
||||
self.update();
|
||||
match self.display.render(Self::render) {
|
||||
Ok(_) => {}
|
||||
Err(wgpu::SurfaceError::Lost | wgpu::SurfaceError::Outdated) => {
|
||||
self.display.update_surface();
|
||||
}
|
||||
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||
Err(wgpu::SurfaceError::Timeout) => log::warn!("Surface timeout"),
|
||||
}
|
||||
}
|
||||
Event::RedrawEventsCleared => {
|
||||
window.request_redraw();
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
}
|
||||
|
||||
fn input(&self, event: &WindowEvent) -> bool {
|
||||
match event {
|
||||
WindowEvent::MouseInput { .. } => true,
|
||||
WindowEvent::KeyboardInput { input, .. } => {
|
||||
match input.virtual_keycode.expect("Not a keycode") {
|
||||
VirtualKeyCode::Space => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self) {}
|
||||
|
||||
fn render(
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
output: &wgpu::SurfaceTexture,
|
||||
render_pipeline: &wgpu::RenderPipeline,
|
||||
) -> Result<(), wgpu::SurfaceError> {
|
||||
let view = output
|
||||
.texture
|
||||
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
|
||||
let mut encoder = 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(wgpu::Color {
|
||||
r: 1.0,
|
||||
b: 1.0,
|
||||
g: 1.0,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: true,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
});
|
||||
|
||||
render_pass.set_pipeline(render_pipeline);
|
||||
render_pass.draw(0..3, 0..1); // 3.
|
||||
}
|
||||
|
||||
// submit will accept anything that implements IntoIter
|
||||
queue.submit(std::iter::once(encoder.finish()));
|
||||
output.present();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
//MAIN ---------------------------------------------------------------------------------
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let program = Program::new();
|
||||
program.start_event_loop().await;
|
||||
pub mod state;
|
||||
use state::run;
|
||||
|
||||
fn main() {
|
||||
pollster::block_on(run());
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
// 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;
|
||||
}
|
||||
@fragment
|
||||
fn fs_alt(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
return vec4<f32>(0.3, 0.7, 0.7, 1.0);
|
||||
}
|
||||
Reference in New Issue
Block a user