Compare commits

..

1 Commits

10 changed files with 613 additions and 724 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
/target
Cargo.lock
target/

View File

@@ -1,23 +1,24 @@
[package]
name = "strafe-client"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-executor = "1.5.1"
bytemuck = { version = "1.13.1", features = ["derive"] }
bytemuck = { version = "1.14.0", features = ["derive"] }
ddsfile = "0.5.1"
env_logger = "0.10.0"
glam = "0.24.1"
log = "0.4.20"
logga = "0.1.2"
obj = "0.10.2"
pollster = "0.3.0"
wgpu = "0.17.0"
winit = "0.28.6"
[profile.release]
lto = true
strip = true
codegen-units = 1
lto = true
opt-level = "z"
panic = "abort"
strip = true

View File

@@ -7,4 +7,4 @@ In development client for jumping on squares (and riding on triangles)
1. Have rust and git installed
2. `git clone https://git.itzana.me/StrafesNET/strafe-client`
3. `cd strafe-client`
4. `cargo run --release`
4. `cargo run --release`

View File

@@ -1,516 +0,0 @@
use std::future::Future;
#[cfg(target_arch = "wasm32")]
use std::str::FromStr;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use web_sys::{ImageBitmapRenderingContext, OffscreenCanvas};
use winit::{
event::{self, WindowEvent},
event_loop::{ControlFlow, EventLoop},
};
#[allow(dead_code)]
pub fn cast_slice<T>(data: &[T]) -> &[u8] {
use std::{mem::size_of, slice::from_raw_parts};
unsafe { from_raw_parts(data.as_ptr() as *const u8, data.len() * size_of::<T>()) }
}
#[allow(dead_code)]
pub enum ShaderStage {
Vertex,
Fragment,
Compute,
}
pub trait Example: 'static + Sized {
fn optional_features() -> wgpu::Features {
wgpu::Features::empty()
}
fn required_features() -> wgpu::Features {
wgpu::Features::empty()
}
fn required_downlevel_capabilities() -> wgpu::DownlevelCapabilities {
wgpu::DownlevelCapabilities {
flags: wgpu::DownlevelFlags::empty(),
shader_model: wgpu::ShaderModel::Sm5,
..wgpu::DownlevelCapabilities::default()
}
}
fn required_limits() -> wgpu::Limits {
wgpu::Limits::downlevel_webgl2_defaults() // These downlevel limits will allow the code to run on all possible hardware
}
fn init(
config: &wgpu::SurfaceConfiguration,
adapter: &wgpu::Adapter,
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self;
fn resize(
&mut self,
config: &wgpu::SurfaceConfiguration,
device: &wgpu::Device,
queue: &wgpu::Queue,
);
fn update(&mut self, event: WindowEvent);
fn move_mouse(&mut self, delta: (f64,f64));
fn render(
&mut self,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
spawner: &Spawner,
);
}
struct Setup {
window: winit::window::Window,
event_loop: EventLoop<()>,
instance: wgpu::Instance,
size: winit::dpi::PhysicalSize<u32>,
surface: wgpu::Surface,
adapter: wgpu::Adapter,
device: wgpu::Device,
queue: wgpu::Queue,
#[cfg(target_arch = "wasm32")]
offscreen_canvas_setup: Option<OffscreenCanvasSetup>,
}
#[cfg(target_arch = "wasm32")]
struct OffscreenCanvasSetup {
offscreen_canvas: OffscreenCanvas,
bitmap_renderer: ImageBitmapRenderingContext,
}
async fn setup<E: Example>(title: &str) -> Setup {
#[cfg(not(target_arch = "wasm32"))]
{
env_logger::init();
};
let event_loop = EventLoop::new();
let mut builder = winit::window::WindowBuilder::new();
builder = builder.with_title(title);
#[cfg(windows_OFF)] // TODO
{
use winit::platform::windows::WindowBuilderExtWindows;
builder = builder.with_no_redirection_bitmap(true);
}
let window = builder.build(&event_loop).unwrap();
#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;
let query_string = web_sys::window().unwrap().location().search().unwrap();
let level: log::Level = parse_url_query_string(&query_string, "RUST_LOG")
.and_then(|x| x.parse().ok())
.unwrap_or(log::Level::Error);
console_log::init_with_level(level).expect("could not initialize logger");
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
// On wasm, append the canvas to the document body
web_sys::window()
.and_then(|win| win.document())
.and_then(|doc| doc.body())
.and_then(|body| {
body.append_child(&web_sys::Element::from(window.canvas()))
.ok()
})
.expect("couldn't append canvas to document body");
}
#[cfg(target_arch = "wasm32")]
let mut offscreen_canvas_setup: Option<OffscreenCanvasSetup> = None;
#[cfg(target_arch = "wasm32")]
{
use wasm_bindgen::JsCast;
use winit::platform::web::WindowExtWebSys;
let query_string = web_sys::window().unwrap().location().search().unwrap();
if let Some(offscreen_canvas_param) =
parse_url_query_string(&query_string, "offscreen_canvas")
{
if FromStr::from_str(offscreen_canvas_param) == Ok(true) {
log::info!("Creating OffscreenCanvasSetup");
let offscreen_canvas =
OffscreenCanvas::new(1024, 768).expect("couldn't create OffscreenCanvas");
let bitmap_renderer = window
.canvas()
.get_context("bitmaprenderer")
.expect("couldn't create ImageBitmapRenderingContext (Result)")
.expect("couldn't create ImageBitmapRenderingContext (Option)")
.dyn_into::<ImageBitmapRenderingContext>()
.expect("couldn't convert into ImageBitmapRenderingContext");
offscreen_canvas_setup = Some(OffscreenCanvasSetup {
offscreen_canvas,
bitmap_renderer,
})
}
}
};
log::info!("Initializing the surface...");
let backends = wgpu::util::backend_bits_from_env().unwrap_or_else(wgpu::Backends::all);
let dx12_shader_compiler = wgpu::util::dx12_shader_compiler_from_env().unwrap_or_default();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
dx12_shader_compiler,
});
let (size, surface) = unsafe {
let size = window.inner_size();
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
let surface = instance.create_surface(&window).unwrap();
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
let surface = {
if let Some(offscreen_canvas_setup) = &offscreen_canvas_setup {
log::info!("Creating surface from OffscreenCanvas");
instance.create_surface_from_offscreen_canvas(
offscreen_canvas_setup.offscreen_canvas.clone(),
)
} else {
instance.create_surface(&window)
}
}
.unwrap();
(size, surface)
};
let adapter = wgpu::util::initialize_adapter_from_env_or_default(&instance, Some(&surface))
.await
.expect("No suitable GPU adapters found on the system!");
#[cfg(not(target_arch = "wasm32"))]
{
let adapter_info = adapter.get_info();
println!("Using {} ({:?})", adapter_info.name, adapter_info.backend);
}
let optional_features = E::optional_features();
let required_features = E::required_features();
let adapter_features = adapter.features();
assert!(
adapter_features.contains(required_features),
"Adapter does not support required features for this example: {:?}",
required_features - adapter_features
);
let required_downlevel_capabilities = E::required_downlevel_capabilities();
let downlevel_capabilities = adapter.get_downlevel_capabilities();
assert!(
downlevel_capabilities.shader_model >= required_downlevel_capabilities.shader_model,
"Adapter does not support the minimum shader model required to run this example: {:?}",
required_downlevel_capabilities.shader_model
);
assert!(
downlevel_capabilities
.flags
.contains(required_downlevel_capabilities.flags),
"Adapter does not support the downlevel capabilities required to run this example: {:?}",
required_downlevel_capabilities.flags - downlevel_capabilities.flags
);
// Make sure we use the texture resolution limits from the adapter, so we can support images the size of the surface.
let needed_limits = E::required_limits().using_resolution(adapter.limits());
let trace_dir = std::env::var("WGPU_TRACE");
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: (optional_features & adapter_features) | required_features,
limits: needed_limits,
},
trace_dir.ok().as_ref().map(std::path::Path::new),
)
.await
.expect("Unable to find a suitable GPU adapter!");
Setup {
window,
event_loop,
instance,
size,
surface,
adapter,
device,
queue,
#[cfg(target_arch = "wasm32")]
offscreen_canvas_setup,
}
}
fn start<E: Example>(
#[cfg(not(target_arch = "wasm32"))] Setup {
window,
event_loop,
instance,
size,
surface,
adapter,
device,
queue,
}: Setup,
#[cfg(target_arch = "wasm32")] Setup {
window,
event_loop,
instance,
size,
surface,
adapter,
device,
queue,
offscreen_canvas_setup,
}: Setup,
) {
let spawner = Spawner::new();
let mut config = surface
.get_default_config(&adapter, size.width, size.height)
.expect("Surface isn't supported by the adapter.");
let surface_view_format = config.format.add_srgb_suffix();
config.view_formats.push(surface_view_format);
surface.configure(&device, &config);
log::info!("Initializing the example...");
let mut example = E::init(&config, &adapter, &device, &queue);
#[cfg(not(target_arch = "wasm32"))]
let mut last_frame_inst = Instant::now();
#[cfg(not(target_arch = "wasm32"))]
let (mut frame_count, mut accum_time) = (0, 0.0);
log::info!("Entering render loop...");
event_loop.run(move |event, _, control_flow| {
let _ = (&instance, &adapter); // force ownership by the closure
*control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit
} else {
ControlFlow::Poll
};
match event {
event::Event::RedrawEventsCleared => {
#[cfg(not(target_arch = "wasm32"))]
spawner.run_until_stalled();
window.request_redraw();
}
event::Event::WindowEvent {
event:
WindowEvent::Resized(size)
| WindowEvent::ScaleFactorChanged {
new_inner_size: &mut size,
..
},
..
} => {
// Once winit is fixed, the detection conditions here can be removed.
// https://github.com/rust-windowing/winit/issues/2876
let max_dimension = adapter.limits().max_texture_dimension_2d;
if size.width > max_dimension || size.height > max_dimension {
log::warn!(
"The resizing size {:?} exceeds the limit of {}.",
size,
max_dimension
);
} else {
log::info!("Resizing to {:?}", size);
config.width = size.width.max(1);
config.height = size.height.max(1);
example.resize(&config, &device, &queue);
surface.configure(&device, &config);
}
}
event::Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
input:
event::KeyboardInput {
virtual_keycode: Some(event::VirtualKeyCode::Escape),
state: event::ElementState::Pressed,
..
},
..
}
| WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
#[cfg(not(target_arch = "wasm32"))]
WindowEvent::KeyboardInput {
input:
event::KeyboardInput {
virtual_keycode: Some(event::VirtualKeyCode::R),
state: event::ElementState::Pressed,
..
},
..
} => {
println!("{:#?}", instance.generate_report());
}
_ => {
example.update(event);
}
},
event::Event::DeviceEvent {
event:
winit::event::DeviceEvent::MouseMotion {
delta,
},
..
} => {
example.move_mouse(delta);
},
event::Event::RedrawRequested(_) => {
#[cfg(not(target_arch = "wasm32"))]
{
accum_time += last_frame_inst.elapsed().as_secs_f32();
last_frame_inst = Instant::now();
frame_count += 1;
if frame_count == 100 {
println!(
"Avg frame time {}ms",
accum_time * 1000.0 / frame_count as f32
);
accum_time = 0.0;
frame_count = 0;
}
}
let frame = match surface.get_current_texture() {
Ok(frame) => frame,
Err(_) => {
surface.configure(&device, &config);
surface
.get_current_texture()
.expect("Failed to acquire next surface texture!")
}
};
let view = frame.texture.create_view(&wgpu::TextureViewDescriptor {
format: Some(surface_view_format),
..wgpu::TextureViewDescriptor::default()
});
example.render(&view, &device, &queue, &spawner);
frame.present();
#[cfg(target_arch = "wasm32")]
{
if let Some(offscreen_canvas_setup) = &offscreen_canvas_setup {
let image_bitmap = offscreen_canvas_setup
.offscreen_canvas
.transfer_to_image_bitmap()
.expect("couldn't transfer offscreen canvas to image bitmap.");
offscreen_canvas_setup
.bitmap_renderer
.transfer_from_image_bitmap(&image_bitmap);
log::info!("Transferring OffscreenCanvas to ImageBitmapRenderer");
}
}
}
_ => {}
}
});
}
#[cfg(not(target_arch = "wasm32"))]
pub struct Spawner<'a> {
executor: async_executor::LocalExecutor<'a>,
}
#[cfg(not(target_arch = "wasm32"))]
impl<'a> Spawner<'a> {
fn new() -> Self {
Self {
executor: async_executor::LocalExecutor::new(),
}
}
#[allow(dead_code)]
pub fn spawn_local(&self, future: impl Future<Output = ()> + 'a) {
self.executor.spawn(future).detach();
}
fn run_until_stalled(&self) {
while self.executor.try_tick() {}
}
}
#[cfg(target_arch = "wasm32")]
pub struct Spawner {}
#[cfg(target_arch = "wasm32")]
impl Spawner {
fn new() -> Self {
Self {}
}
#[allow(dead_code)]
pub fn spawn_local(&self, future: impl Future<Output = ()> + 'static) {
wasm_bindgen_futures::spawn_local(future);
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn run<E: Example>(title: &str) {
let setup = pollster::block_on(setup::<E>(title));
start::<E>(setup);
}
#[cfg(target_arch = "wasm32")]
pub fn run<E: Example>(title: &str) {
use wasm_bindgen::prelude::*;
let title = title.to_owned();
wasm_bindgen_futures::spawn_local(async move {
let setup = setup::<E>(&title).await;
let start_closure = Closure::once_into_js(move || start::<E>(setup));
// make sure to handle JS exceptions thrown inside start.
// Otherwise wasm_bindgen_futures Queue would break and never handle any tasks again.
// This is required, because winit uses JS exception for control flow to escape from `run`.
if let Err(error) = call_catch(&start_closure) {
let is_control_flow_exception = error.dyn_ref::<js_sys::Error>().map_or(false, |e| {
e.message().includes("Using exceptions for control flow", 0)
});
if !is_control_flow_exception {
web_sys::console::error_1(&error);
}
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(catch, js_namespace = Function, js_name = "prototype.call.call")]
fn call_catch(this: &JsValue) -> Result<(), JsValue>;
}
});
}
#[cfg(target_arch = "wasm32")]
/// Parse the query string as returned by `web_sys::window()?.location().search()?` and get a
/// specific key out of it.
pub fn parse_url_query_string<'a>(query: &'a str, search_key: &str) -> Option<&'a str> {
let query_string = query.strip_prefix('?')?;
for pair in query_string.split('&') {
let mut pair = pair.split('=');
let key = pair.next()?;
let value = pair.next()?;
if key == search_key {
return Some(value);
}
}
None
}
// This allows treating the framework as a standalone example,
// thus avoiding listing the example names in `Cargo.toml`.
#[allow(dead_code)]
fn main() {}

View File

@@ -1 +1,414 @@
pub mod framework;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use winit::{
event::{self, WindowEvent},
event_loop::{ControlFlow, EventLoop},
};
use logga;
#[allow(dead_code)]
pub fn cast_slice<T>(data: &[T]) -> &[u8] {
use std::{mem::size_of, slice::from_raw_parts};
unsafe { from_raw_parts(data.as_ptr() as *const u8, data.len() * size_of::<T>()) }
}
#[allow(dead_code)]
pub enum ShaderStage {
Vertex,
Fragment,
Compute,
}
pub trait Example: 'static + Sized {
fn optional_features() -> wgpu::Features {
wgpu::Features::empty()
}
fn required_features() -> wgpu::Features {
wgpu::Features::empty()
}
fn required_downlevel_capabilities() -> wgpu::DownlevelCapabilities {
wgpu::DownlevelCapabilities {
flags: wgpu::DownlevelFlags::empty(),
shader_model: wgpu::ShaderModel::Sm5,
..wgpu::DownlevelCapabilities::default()
}
}
fn required_limits() -> wgpu::Limits {
wgpu::Limits::downlevel_webgl2_defaults() // These downlevel limits will allow the code to run on all possible hardware
}
fn init(
config: &wgpu::SurfaceConfiguration,
adapter: &wgpu::Adapter,
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self;
fn resize(
&mut self,
config: &wgpu::SurfaceConfiguration,
device: &wgpu::Device,
queue: &wgpu::Queue,
);
fn update(&mut self, event: WindowEvent);
fn move_mouse(&mut self, delta: (f64,f64));
fn render(
&mut self,
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
);
}
struct Setup {
window: winit::window::Window,
event_loop: EventLoop<()>,
instance: wgpu::Instance,
size: winit::dpi::PhysicalSize<u32>,
surface: wgpu::Surface,
adapter: wgpu::Adapter,
device: wgpu::Device,
queue: wgpu::Queue,
#[cfg(target_arch = "wasm32")]
offscreen_canvas_setup: Option<OffscreenCanvasSetup>,
}
#[cfg(target_arch = "wasm32")]
struct OffscreenCanvasSetup {
offscreen_canvas: OffscreenCanvas,
bitmap_renderer: ImageBitmapRenderingContext,
}
async fn setup<E: Example>(title: &str) -> Setup {
//logga::init();
let event_loop = EventLoop::new();
let mut builder = winit::window::WindowBuilder::new();
builder = builder.with_title(title);
#[cfg(windows_OFF)] // TODO
{
use winit::platform::windows::WindowBuilderExtWindows;
builder = builder.with_no_redirection_bitmap(true);
}
let window = builder.build(&event_loop).unwrap();
#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;
let query_string = web_sys::window().unwrap().location().search().unwrap();
let level: log::Level = parse_url_query_string(&query_string, "RUST_LOG")
.and_then(|x| x.parse().ok())
.unwrap_or(log::Level::Error);
console_log::init_with_level(level).expect("could not initialize logger");
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
// On wasm, append the canvas to the document body
web_sys::window()
.and_then(|win| win.document())
.and_then(|doc| doc.body())
.and_then(|body| {
body.append_child(&web_sys::Element::from(window.canvas()))
.ok()
})
.expect("couldn't append canvas to document body");
}
#[cfg(target_arch = "wasm32")]
let mut offscreen_canvas_setup: Option<OffscreenCanvasSetup> = None;
#[cfg(target_arch = "wasm32")]
{
use wasm_bindgen::JsCast;
use winit::platform::web::WindowExtWebSys;
let query_string = web_sys::window().unwrap().location().search().unwrap();
if let Some(offscreen_canvas_param) =
parse_url_query_string(&query_string, "offscreen_canvas")
{
if FromStr::from_str(offscreen_canvas_param) == Ok(true) {
logga::info!("Creating OffscreenCanvasSetup");
let offscreen_canvas =
OffscreenCanvas::new(1024, 768).expect("couldn't create OffscreenCanvas");
let bitmap_renderer = window
.canvas()
.get_context("bitmaprenderer")
.expect("couldn't create ImageBitmapRenderingContext (Result)")
.expect("couldn't create ImageBitmapRenderingContext (Option)")
.dyn_into::<ImageBitmapRenderingContext>()
.expect("couldn't convert into ImageBitmapRenderingContext");
offscreen_canvas_setup = Some(OffscreenCanvasSetup {
offscreen_canvas,
bitmap_renderer,
})
}
}
};
logga::info!("Initializing the surface...");
let backends = wgpu::util::backend_bits_from_env().unwrap_or_else(wgpu::Backends::all);
let dx12_shader_compiler = wgpu::util::dx12_shader_compiler_from_env().unwrap_or_default();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
dx12_shader_compiler,
});
let (size, surface) = unsafe {
let size = window.inner_size();
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
let surface = instance.create_surface(&window).unwrap();
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
let surface = {
if let Some(offscreen_canvas_setup) = &offscreen_canvas_setup {
logga::info!("Creating surface from OffscreenCanvas");
instance.create_surface_from_offscreen_canvas(
offscreen_canvas_setup.offscreen_canvas.clone(),
)
} else {
instance.create_surface(&window)
}
}
.unwrap();
(size, surface)
};
let adapter = wgpu::util::initialize_adapter_from_env_or_default(&instance, Some(&surface))
.await
.expect("No suitable GPU adapters found on the system!");
#[cfg(not(target_arch = "wasm32"))]
{
let adapter_info = adapter.get_info();
println!("Using {} ({:?})", adapter_info.name, adapter_info.backend);
}
let optional_features = E::optional_features();
let required_features = E::required_features();
let adapter_features = adapter.features();
assert!(
adapter_features.contains(required_features),
"Adapter does not support required features for this example: {:?}",
required_features - adapter_features
);
let required_downlevel_capabilities = E::required_downlevel_capabilities();
let downlevel_capabilities = adapter.get_downlevel_capabilities();
assert!(
downlevel_capabilities.shader_model >= required_downlevel_capabilities.shader_model,
"Adapter does not support the minimum shader model required to run this example: {:?}",
required_downlevel_capabilities.shader_model
);
assert!(
downlevel_capabilities
.flags
.contains(required_downlevel_capabilities.flags),
"Adapter does not support the downlevel capabilities required to run this example: {:?}",
required_downlevel_capabilities.flags - downlevel_capabilities.flags
);
// Make sure we use the texture resolution limits from the adapter, so we can support images the size of the surface.
let needed_limits = E::required_limits().using_resolution(adapter.limits());
let trace_dir = std::env::var("WGPU_TRACE");
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: (optional_features & adapter_features) | required_features,
limits: needed_limits,
},
trace_dir.ok().as_ref().map(std::path::Path::new),
)
.await
.expect("Unable to find a suitable GPU adapter!");
Setup {
window,
event_loop,
instance,
size,
surface,
adapter,
device,
queue,
#[cfg(target_arch = "wasm32")]
offscreen_canvas_setup,
}
}
fn start<E: Example>(
#[cfg(not(target_arch = "wasm32"))] Setup {
window,
event_loop,
instance,
size,
surface,
adapter,
device,
queue,
}: Setup,
#[cfg(target_arch = "wasm32")] Setup {
window,
event_loop,
instance,
size,
surface,
adapter,
device,
queue,
offscreen_canvas_setup,
}: Setup,
) {
let mut config = surface
.get_default_config(&adapter, size.width, size.height)
.expect("Surface isn't supported by the adapter.");
let surface_view_format = config.format.add_srgb_suffix();
config.view_formats.push(surface_view_format);
surface.configure(&device, &config);
logga::info!("Initializing the example...");
let mut example = E::init(&config, &adapter, &device, &queue);
#[cfg(not(target_arch = "wasm32"))]
let mut last_frame_inst = Instant::now();
#[cfg(not(target_arch = "wasm32"))]
let (mut frame_count, mut accum_time) = (0, 0.0);
logga::info!("Entering render loop...");
event_loop.run(move |event, _, control_flow| {
let _ = (&instance, &adapter); // force ownership by the closure
*control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit
} else {
ControlFlow::Poll
};
match event {
event::Event::RedrawEventsCleared => {
#[cfg(not(target_arch = "wasm32"))]
window.request_redraw();
}
event::Event::WindowEvent {
event:
WindowEvent::Resized(size)
| WindowEvent::ScaleFactorChanged {
new_inner_size: &mut size,
..
},
..
} => {
// Once winit is fixed, the detection conditions here can be removed.
// https://github.com/rust-windowing/winit/issues/2876
let max_dimension = adapter.limits().max_texture_dimension_2d;
if size.width > max_dimension || size.height > max_dimension {
logga::warn!(
"The resizing size {:?} exceeds the limit of {}.",
size,
max_dimension
);
} else {
logga::info!("Resizing to {:?}", size);
config.width = size.width.max(1);
config.height = size.height.max(1);
example.resize(&config, &device, &queue);
surface.configure(&device, &config);
}
}
event::Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
input:
event::KeyboardInput {
virtual_keycode: Some(event::VirtualKeyCode::Escape),
state: event::ElementState::Pressed,
..
},
..
}
| WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
#[cfg(not(target_arch = "wasm32"))]
WindowEvent::KeyboardInput {
input:
event::KeyboardInput {
virtual_keycode: Some(event::VirtualKeyCode::R),
state: event::ElementState::Pressed,
..
},
..
} => {
println!("{:#?}", instance.generate_report());
}
_ => {
example.update(event);
}
},
event::Event::DeviceEvent {
event:
winit::event::DeviceEvent::MouseMotion {
delta,
},
..
} => {
example.move_mouse(delta);
},
event::Event::RedrawRequested(_) => {
#[cfg(not(target_arch = "wasm32"))]
{
accum_time += last_frame_inst.elapsed().as_secs_f32();
last_frame_inst = Instant::now();
frame_count += 1;
if frame_count == 100 {
println!(
"Avg frame time {}ms",
accum_time * 1000.0 / frame_count as f32
);
accum_time = 0.0;
frame_count = 0;
}
}
let frame = match surface.get_current_texture() {
Ok(frame) => frame,
Err(_) => {
surface.configure(&device, &config);
surface
.get_current_texture()
.expect("Failed to acquire next surface texture!")
}
};
let view = frame.texture.create_view(&wgpu::TextureViewDescriptor {
format: Some(surface_view_format),
..wgpu::TextureViewDescriptor::default()
});
example.render(&view, &device, &queue);
frame.present();
#[cfg(target_arch = "wasm32")]
{
if let Some(offscreen_canvas_setup) = &offscreen_canvas_setup {
let image_bitmap = offscreen_canvas_setup
.offscreen_canvas
.transfer_to_image_bitmap()
.expect("couldn't transfer offscreen canvas to image bitmap.");
offscreen_canvas_setup
.bitmap_renderer
.transfer_from_image_bitmap(&image_bitmap);
logga::info!("Transferring OffscreenCanvas to ImageBitmapRenderer");
}
}
}
_ => {}
}
});
}
#[cfg(not(target_arch = "wasm32"))]
pub fn run<E: Example>(title: &str) {
let setup = pollster::block_on(setup::<E>(title));
start::<E>(setup);
}

View File

@@ -1,3 +1,163 @@
//#![no_std]
#![no_main]
// I'm trying to replace the stdlib functions so don't delete this yet
/*
extern crate std;
use std::*;
pub struct Vec<T> {
data: [T; 16],
len: usize,
}
impl<T> Vec<T> {
pub fn new() -> Self {
Vec {
data: Default::default(),
len: 0,
}
}
pub fn push(&mut self, value: T) -> Result<(), &'static str> {
if self.len < self.data.len() {
self.data[self.len] = value;
self.len += 1;
Ok(())
} else {
Err("Vec is full")
}
}
pub fn pop(&mut self) -> Option<T> {
if self.len > 0 {
self.len -= 1;
Some(std::mem::replace(&mut self.data[self.len], Default::default()))
} else {
None
}
}
pub fn len(&self) -> usize {
self.len
}
pub fn capacity(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
}
pub struct Iter<'a, T> {
vec: &'a Vec<T>,
index: usize,
}
impl<'a, T> Vec<T> {
// ... previous methods ...
pub fn iter(&'a self) -> Iter<'a, T> {
Iter {
vec: self,
index: 0,
}
}
}
impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.vec.len {
let item = &self.vec.data[self.index];
self.index += 1;
Some(item)
} else {
None
}
}
}
impl<T> Vec<T> {
// (Previous methods omitted for brevity)
pub fn drain(&mut self, range: std::ops::Range<usize>) -> Drain<T> {
Drain {
vec: self,
range,
}
}
}
pub struct Drain<'a, T> {
vec: &'a mut Vec<T>,
range: std::ops::Range<usize>,
}
impl<'a, T> Iterator for Drain<'a, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if let Some(index) = self.range.next_back() {
if index < self.vec.len {
// Swap and remove the element from the Vec
let last_index = self.vec.len - 1;
self.vec.len -= 1;
Some(std::mem::replace(&mut self.vec.data[index], Default::default()))
} else {
None
}
} else {
None
}
}
}
*/
//extern crate logga;
//extern crate libc;
/*
#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
*/
//use logga;
use bytemuck::{Pod, Zeroable};
use std::{borrow::Cow, time::Instant};
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
@@ -20,16 +180,12 @@ struct Entity {
//temp?
struct ModelData {
transform: glam::Mat4,
transform_depth: glam::Mat4,
use_depth: glam::Vec4,
vertex_buf: wgpu::Buffer,
entities: Vec<Entity>,
}
struct Model {
transform: glam::Mat4,
transform_depth: glam::Mat4,
use_depth: glam::Vec4,
vertex_buf: wgpu::Buffer,
entities: Vec<Entity>,
bind_group: wgpu::BindGroup,
@@ -130,15 +286,11 @@ pub struct Skybox {
sky_pipeline: wgpu::RenderPipeline,
entity_pipeline: wgpu::RenderPipeline,
ground_pipeline: wgpu::RenderPipeline,
special_teapot: Model,
checkered_pipeline: wgpu::RenderPipeline,
depth_overwrite_pipeline: wgpu::RenderPipeline,
main_bind_group: wgpu::BindGroup,
camera_buf: wgpu::Buffer,
models: Vec<Model>,
depth_view: wgpu::TextureView,
staging_belt: wgpu::util::StagingBelt,
start_time: Instant,
}
impl Skybox {
@@ -167,18 +319,9 @@ impl Skybox {
}
}
fn get_model_uniform_data(model:&Model) -> [f32; 4*4*2+4] {
let mut raw = [0f32; 4*4*2+4];
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform)[..]);
raw[16..32].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform_depth)[..]);
raw[32..36].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&model.use_depth));
raw
}
fn get_modeldata_uniform_data(model:&ModelData) -> [f32; 4*4*2+4] {
let mut raw = [0f32; 4*4*2+4];
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform)[..]);
raw[16..32].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(&model.transform_depth)[..]);
raw[32..36].copy_from_slice(AsRef::<[f32; 4]>::as_ref(&model.use_depth));
fn get_transform_uniform_data(transform:&glam::Mat4) -> [f32; 4*4] {
let mut raw = [0f32; 4*4];
raw[0..16].copy_from_slice(&AsRef::<[f32; 4*4]>::as_ref(transform)[..]);
raw
}
@@ -226,15 +369,13 @@ fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec<ModelData>,source:&[u8]){
});
modeldatas.push(ModelData {
transform: glam::Mat4::default(),
transform_depth: glam::Mat4::default(),
use_depth: glam::Vec4::ZERO,
vertex_buf,
entities,
})
}
}
impl strafe_client::framework::Example for Skybox {
impl strafe_client::Example for Skybox {
fn optional_features() -> wgpu::Features {
wgpu::Features::TEXTURE_COMPRESSION_ASTC
| wgpu::Features::TEXTURE_COMPRESSION_ETC2
@@ -253,13 +394,7 @@ impl strafe_client::framework::Example for Skybox {
add_obj(device,& mut modeldatas,include_bytes!("../models/teapot.obj"));
println!("models.len = {:?}", modeldatas.len());
modeldatas[1].transform=glam::Mat4::from_translation(glam::vec3(10.,5.,10.));
let proj1 = glam::Mat4::orthographic_rh(-20.0, 20.0, -20.0, 20.0, -20.0, 20.0);
let model0 = glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)) * glam::Mat4::from_scale(glam::vec3(10.0,10.0,10.0));
modeldatas[2].transform=model0;
modeldatas[2].transform_depth=proj1;// * view1_inv
modeldatas[2].use_depth=glam::Vec4::Z;
modeldatas[2].transform=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.));
let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: None,
@@ -429,60 +564,6 @@ impl strafe_client::framework::Example for Skybox {
multisample: wgpu::MultisampleState::default(),
multiview: None,
});
let checkered_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Checkered"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_square",
buffers: &[],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_checkered",
targets: &[Some(config.view_formats[0].into())],
}),
primitive: wgpu::PrimitiveState {
front_face: wgpu::FrontFace::Cw,
..Default::default()
},
depth_stencil: Some(wgpu::DepthStencilState {
format: Self::DEPTH_FORMAT,
depth_write_enabled: false,
depth_compare: wgpu::CompareFunction::Always,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState::default(),
multiview: None,
});
let depth_overwrite_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Overwrite"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_square",
buffers: &[],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_overwrite",
targets: &[Some(config.view_formats[0].into())],
}),
primitive: wgpu::PrimitiveState {
front_face: wgpu::FrontFace::Cw,
..Default::default()
},
depth_stencil: Some(wgpu::DepthStencilState {
format: Self::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState::default(),
multiview: None,
});
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: None,
@@ -498,19 +579,19 @@ impl strafe_client::framework::Example for Skybox {
let device_features = device.features();
let skybox_format = if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ASTC) {
log::info!("Using ASTC");
logga::info!("Using ASTC");
wgpu::TextureFormat::Astc {
block: AstcBlock::B4x4,
channel: AstcChannel::UnormSrgb,
}
} else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_ETC2) {
log::info!("Using ETC2");
logga::info!("Using ETC2");
wgpu::TextureFormat::Etc2Rgb8UnormSrgb
} else if device_features.contains(wgpu::Features::TEXTURE_COMPRESSION_BC) {
log::info!("Using BC");
logga::info!("Using BC");
wgpu::TextureFormat::Bc1RgbaUnormSrgb
} else {
log::info!("Using plain");
logga::info!("Using plain");
wgpu::TextureFormat::Bgra8UnormSrgb
};
@@ -526,7 +607,7 @@ impl strafe_client::framework::Example for Skybox {
};
let max_mips = layer_size.max_mips(wgpu::TextureDimension::D2);
log::debug!(
logga::debug!(
"Copying {:?} skybox images of size {}, {}, 6 with {} mips to gpu",
skybox_format,
IMAGE_SIZE,
@@ -589,7 +670,7 @@ impl strafe_client::framework::Example for Skybox {
//drain the modeldata vec so entities can be /moved/ to models.entities
let mut models = Vec::<Model>::with_capacity(modeldatas.len());
for (i,modeldata) in modeldatas.drain(..).enumerate() {
let model_uniforms = get_modeldata_uniform_data(&modeldata);
let model_uniforms = get_transform_uniform_data(&modeldata.transform);
let model_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some(format!("Model{}",i).as_str()),
contents: bytemuck::cast_slice(&model_uniforms),
@@ -608,8 +689,6 @@ impl strafe_client::framework::Example for Skybox {
//all of these are being moved here
models.push(Model{
transform: modeldata.transform,
transform_depth: modeldata.transform_depth,
use_depth: modeldata.use_depth,
vertex_buf:modeldata.vertex_buf,
entities: modeldata.entities,
bind_group: model_bind_group,
@@ -617,8 +696,6 @@ impl strafe_client::framework::Example for Skybox {
})
}
let teapot = models.pop().unwrap();
let depth_view = Self::create_depth_texture(config, device);
Skybox {
@@ -626,15 +703,11 @@ impl strafe_client::framework::Example for Skybox {
sky_pipeline,
entity_pipeline,
ground_pipeline,
special_teapot: teapot,
checkered_pipeline,
depth_overwrite_pipeline,
main_bind_group,
camera_buf,
models,
depth_view,
staging_belt: wgpu::util::StagingBelt::new(0x100),
start_time: Instant::now(),
}
}
@@ -710,7 +783,6 @@ impl strafe_client::framework::Example for Skybox {
view: &wgpu::TextureView,
device: &wgpu::Device,
queue: &wgpu::Queue,
_spawner: &strafe_client::framework::Spawner,
) {
let time = Instant::now();
@@ -745,11 +817,6 @@ impl strafe_client::framework::Example for Skybox {
}
}
let proj1 = glam::Mat4::orthographic_rh(-20.0, 20.0, -20.0, 20.0, -20.0, 20.0);
let model1 = glam::Mat4::from_euler(glam::EulerRot::YXZ, self.start_time.elapsed().as_secs_f32(),0f32,0f32);
self.special_teapot.transform_depth=proj1 * model1;
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
@@ -764,22 +831,9 @@ impl strafe_client::framework::Example for Skybox {
device,
)
.copy_from_slice(bytemuck::cast_slice(&camera_uniforms));
//special teapot
{
let model_uniforms = get_model_uniform_data(&self.special_teapot);
self.staging_belt
.write_buffer(
&mut encoder,
&self.special_teapot.model_buf,//description of where data will be written when command is executed
0,//offset in staging belt?
wgpu::BufferSize::new((model_uniforms.len() * 4) as wgpu::BufferAddress).unwrap(),
device,
)
.copy_from_slice(bytemuck::cast_slice(&model_uniforms));
}
//This code only needs to run when the uniforms change
for model in self.models.iter() {
let model_uniforms = get_model_uniform_data(&model);
let model_uniforms = get_transform_uniform_data(&model.transform);
self.staging_belt
.write_buffer(
&mut encoder,
@@ -820,23 +874,6 @@ impl strafe_client::framework::Example for Skybox {
rpass.set_bind_group(0, &self.main_bind_group, &[]);
//draw special teapot
rpass.set_bind_group(1, &self.special_teapot.bind_group, &[]);
rpass.set_pipeline(&self.checkered_pipeline);
rpass.draw(0..6, 0..1);
rpass.set_pipeline(&self.entity_pipeline);
rpass.set_vertex_buffer(0, self.special_teapot.vertex_buf.slice(..));
for entity in self.special_teapot.entities.iter() {
rpass.set_index_buffer(entity.index_buf.slice(..), wgpu::IndexFormat::Uint16);
rpass.draw_indexed(0..entity.index_count, 0, 0..1);
}
rpass.set_pipeline(&self.depth_overwrite_pipeline);
rpass.draw(0..6, 0..1);
//draw models
rpass.set_pipeline(&self.entity_pipeline);
for model in self.models.iter() {
rpass.set_bind_group(1, &model.bind_group, &[]);
@@ -863,10 +900,12 @@ impl strafe_client::framework::Example for Skybox {
}
}
fn main() {
strafe_client::framework::run::<Skybox>(
#[no_mangle]
fn main(_argc: isize, _argv: *const *const u8) {
strafe_client::run::<Skybox>(
format!("Strafe Client v{}",
env!("CARGO_PKG_VERSION")
).as_str()
);
}
}

View File

@@ -68,15 +68,9 @@ struct EntityOutput {
@location(3) view: vec3<f32>,
};
struct TransformData {
transform: mat4x4<f32>,
depth: mat4x4<f32>,
use_depth: vec4<f32>,
};
@group(1)
@binding(0)
var<uniform> transform: TransformData;
var<uniform> r_EntityTransform: mat4x4<f32>;
@vertex
fn vs_entity(
@@ -84,42 +78,12 @@ fn vs_entity(
@location(1) texture: vec2<f32>,
@location(2) normal: vec3<f32>,
) -> EntityOutput {
var position_depth: vec4<f32> = transform.depth * vec4<f32>(pos, 1.0);
var position_depth_0: vec4<f32> = position_depth;
position_depth_0.z=0.0;
var position: vec4<f32> = transform.transform * mix(position_depth,position_depth_0,transform.use_depth);
var position: vec4<f32> = r_EntityTransform * vec4<f32>(pos, 1.0);
var result: EntityOutput;
result.normal = (transform.transform * mix(vec4<f32>(normal,0.0),vec4<f32>(0.0,0.0,1.0,0.0),transform.use_depth.z)).xyz;
result.normal = (r_EntityTransform * vec4<f32>(normal, 0.0)).xyz;
result.texture=texture;
result.view = position.xyz - r_data.cam_pos.xyz;
var screen_position: vec4<f32> = r_data.proj * r_data.view * position;
result.position = mix(screen_position,position_depth,transform.use_depth);
return result;
}
struct SquareOutput {
@builtin(position) position: vec4<f32>,
@location(2) normal: vec3<f32>,
@location(3) view: vec3<f32>,
@location(4) pos: vec3<f32>,
};
@vertex
fn vs_square(@builtin(vertex_index) vertex_index: u32) -> SquareOutput {
// hacky way to draw two triangles that make a square
let tmp1 = i32(vertex_index)/2-i32(vertex_index)/3;
let tmp2 = i32(vertex_index)&1;
let pos = vec3<f32>(
(f32(tmp1) - 0.5)*1.8,
f32(tmp2) - 0.5,
0.0
);
var result: SquareOutput;
result.normal = vec3<f32>(0.0,0.0,1.0);
result.pos = (transform.transform * vec4<f32>(pos, 1.0)).xyz;
result.view = result.pos - r_data.cam_pos.xyz;
result.position = r_data.proj * r_data.view * transform.transform * vec4<f32>(pos, 1.0);
result.position = r_data.proj * r_data.view * position;
return result;
}
@@ -165,24 +129,3 @@ fn fs_ground(vertex: GroundOutput) -> @location(0) vec4<f32> {
let dir = vec3<f32>(-1.0)+vec3<f32>(modulo_euclidean(vertex.pos.x/16.,1.0),0.0,modulo_euclidean(vertex.pos.z/16.,1.0))*2.0;
return vec4<f32>(textureSample(r_texture, r_sampler, dir).rgb, 1.0);
}
@fragment
fn fs_checkered(vertex: SquareOutput) -> @location(0) vec4<f32> {
let voxel_parity: f32 = f32(
u32(modulo_euclidean(vertex.pos.x,2.0)<1.0)
^ u32(modulo_euclidean(vertex.pos.y,2.0)<1.0)
//^ u32(modulo_euclidean(vertex.pos.z,2.0)<1.0)
);
let incident = normalize(vertex.view);
let normal = normalize(vertex.normal);
let d = dot(normal, incident);
let reflected = incident - 2.0 * d * normal;
let texture_color = vec3<f32>(1.0)*voxel_parity;
let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb;
return vec4<f32>(mix(vec3<f32>(0.1) + 0.5 * reflected_color,texture_color,1.0-pow(1.0-abs(d),2.0)), 1.0);
}
@fragment
fn fs_overwrite(vertex: SquareOutput) {}

8
std/lib.rs Normal file
View File

@@ -0,0 +1,8 @@
#[export_macro]
macro_rules! insssfo {
($($arg:tt)*) => {
};
}