Skip to content

Commit 6c32cb7

Browse files
committed
bla2
1 parent 3384688 commit 6c32cb7

File tree

8 files changed

+50
-39
lines changed

8 files changed

+50
-39
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spir
2121
ash = "0.38"
2222
ash-window = "0.13"
2323
wgpu = { version = "27.0.1", default-features = false, features = ["std", "parking_lot", "vulkan", "vulkan-portability", "spirv", "wgsl"] }
24+
pollster = "0.4.0"
2425

2526
# rust-gpu
2627
cargo-gpu = { git = "https://github.com/Rust-GPU/cargo-gpu", rev = "bf24eb6060e0c7b0013eceddd23b5d7cee68f4cc" }

graphics/Cargo.toml.liquid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ash-window = "0.13"
2323
{%- endif -%}
2424
{%- if api == "wgpu" -%}
2525
wgpu = { version = "27.0.1", default-features = false, features = ["std", "parking_lot", "vulkan", "vulkan-portability", "spirv", "wgsl"] }
26+
2627
{%- endif %}
2728

2829
# rust-gpu

graphics/mygraphics/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mygraphics-shaders = { path = "../mygraphics-shaders" }
1818
ash.workspace = true
1919
ash-window.workspace = true
2020
wgpu.workspace = true
21+
pollster.workspace = true
2122

2223
# other
2324
raw-window-handle.workspace = true

graphics/mygraphics/Cargo.toml.liquid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ash-window.workspace = true
2828
{%- endif -%}
2929
{%- if api == "wgpu" -%}
3030
wgpu.workspace = true
31+
pollster.workspace = true
3132
{%- endif %}
3233

3334
# other

graphics/mygraphics/src/wgpu_renderer/mod.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
use crate::wgpu_renderer::render_pipeline::MyRenderPipeline;
21
use crate::wgpu_renderer::swapchain::MySwapchainManager;
32
use anyhow::Context;
43
use mygraphics_shaders::ShaderConstants;
5-
use wgpu::RenderPassDescriptor;
6-
use wgpu::wgt::CommandEncoderDescriptor;
4+
use std::sync::Arc;
75
use winit::event::{Event, WindowEvent};
86
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
97

108
mod render_pipeline;
119
mod renderer;
1210
mod swapchain;
1311

14-
pub async fn main() -> anyhow::Result<()> {
12+
pub fn main() -> anyhow::Result<()> {
13+
pollster::block_on(main_inner())
14+
}
15+
16+
pub async fn main_inner() -> anyhow::Result<()> {
1517
// env_logger::init();
1618
let event_loop = EventLoop::new()?;
1719
// FIXME(eddyb) incomplete `winit` upgrade, follow the guides in:
1820
// https://github.com/rust-windowing/winit/releases/tag/v0.30.0
1921
#[allow(deprecated)]
20-
let window = event_loop.create_window(
21-
winit::window::Window::default_attributes()
22-
.with_title("Rust GPU - wgpu")
23-
.with_inner_size(winit::dpi::LogicalSize::new(
24-
f64::from(1280),
25-
f64::from(720),
26-
)),
27-
)?;
22+
let window = Arc::new(
23+
event_loop.create_window(
24+
winit::window::Window::default_attributes()
25+
.with_title("Rust GPU - wgpu")
26+
.with_inner_size(winit::dpi::LogicalSize::new(
27+
f64::from(1280),
28+
f64::from(720),
29+
)),
30+
)?,
31+
);
2832

2933
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::from_env_or_default());
30-
let surface = instance.create_surface(&window)?;
34+
let surface = instance.create_surface(window.clone())?;
3135
let adapter =
3236
wgpu::util::initialize_adapter_from_env_or_default(&instance, Some(&surface)).await?;
3337

@@ -49,31 +53,20 @@ pub async fn main() -> anyhow::Result<()> {
4953
.context("Failed to create device")?;
5054

5155
let mut swapchain = MySwapchainManager::new(adapter.clone(), device.clone(), window, surface);
52-
let render_pipeline = MyRenderPipeline::new(&device, swapchain.format())?;
56+
let renderer = renderer::MyRenderer::new(device, queue, swapchain.format())?;
5357

5458
let start = std::time::Instant::now();
5559
let mut event_handler =
5660
move |event: Event<_>, event_loop_window_target: &ActiveEventLoop| match event {
57-
Event::AboutToWait => swapchain.render(|f| {
58-
let mut cmd = device.create_command_encoder(&CommandEncoderDescriptor {
59-
label: Some("main draw"),
60-
});
61-
let mut rpass = cmd.begin_render_pass(&RenderPassDescriptor {
62-
label: Some("main renderpass"),
63-
color_attachments: &[],
64-
depth_stencil_attachment: None,
65-
timestamp_writes: None,
66-
occlusion_query_set: None,
67-
});
68-
render_pipeline.draw(
69-
&mut rpass,
61+
Event::AboutToWait => swapchain.render(|render_target| {
62+
renderer.render(
7063
&ShaderConstants {
7164
time: start.elapsed().as_secs_f32(),
72-
width: f.texture().width(),
73-
height: f.texture().height(),
65+
width: render_target.texture().width(),
66+
height: render_target.texture().height(),
7467
},
68+
render_target,
7569
);
76-
queue.submit(std::iter::once(cmd));
7770
}),
7871
Event::WindowEvent { event, .. } => {
7972
match event {
@@ -91,7 +84,6 @@ pub async fn main() -> anyhow::Result<()> {
9184
WindowEvent::Resized(_) => swapchain.should_recreate(),
9285
_ => {}
9386
}
94-
9587
Ok(())
9688
}
9789
_ => {

graphics/mygraphics/src/wgpu_renderer/renderer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl MyRenderer {
4444
occlusion_query_set: None,
4545
});
4646
self.pipeline.draw(&mut rpass, shader_constants);
47+
drop(rpass);
4748

4849
self.queue.submit(std::iter::once(cmd.finish()));
4950
}

graphics/mygraphics/src/wgpu_renderer/swapchain.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use anyhow::Context;
2+
use std::sync::Arc;
23
use wgpu::{Adapter, Device, Surface, SurfaceError, TextureFormat, TextureView};
34
use winit::dpi::PhysicalSize;
45
use winit::window::Window;
56

6-
pub struct MySwapchainManager {
7+
pub struct MySwapchainManager<'a> {
78
adapter: Adapter,
89
device: Device,
9-
window: Window,
10-
surface: Surface<'static>,
10+
window: Arc<Window>,
11+
surface: Surface<'a>,
1112
format: TextureFormat,
1213

1314
// state below
@@ -19,12 +20,12 @@ pub struct ActiveConfiguration {
1920
size: PhysicalSize<u32>,
2021
}
2122

22-
impl MySwapchainManager {
23+
impl<'a> MySwapchainManager<'a> {
2324
pub fn new(
2425
adapter: Adapter,
2526
device: Device,
26-
window: Window,
27-
surface: Surface<'static>,
27+
window: Arc<Window>,
28+
surface: Surface<'a>,
2829
) -> Self {
2930
let caps = surface.get_capabilities(&adapter);
3031
Self {
@@ -47,9 +48,9 @@ impl MySwapchainManager {
4748
self.format
4849
}
4950

50-
pub fn render<R>(&mut self, f: impl FnOnce(TextureView) -> R) -> R {
51+
pub fn render<R>(&mut self, f: impl FnOnce(TextureView) -> R) -> anyhow::Result<R> {
5152
let size = self.window.inner_size();
52-
if let Some(active) = self.active {
53+
if let Some(active) = &self.active {
5354
if active.size != size {
5455
self.should_recreate();
5556
}
@@ -83,8 +84,14 @@ impl MySwapchainManager {
8384
Err(SurfaceError::Outdated | SurfaceError::Lost) => {
8485
self.should_recreate = true;
8586
}
87+
Err(e) => {
88+
anyhow::bail!("get_current_texture() failed: {}", e)
89+
}
8690
};
8791
}
92+
anyhow::bail!(
93+
"looped {RECREATE_ATTEMPTS} times trying to acquire swapchain image and failed repeatedly!"
94+
);
8895
}
8996

9097
fn configure_surface(&mut self, size: PhysicalSize<u32>) -> anyhow::Result<()> {

0 commit comments

Comments
 (0)