Skip to content

Commit

Permalink
Simplify surface creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Nov 15, 2024
1 parent b1ee0c5 commit f847a4e
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 70 deletions.
1 change: 1 addition & 0 deletions blade-graphics/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct InternalFrame {
struct Swapchain {
raw: vk::SwapchainKHR,
format: crate::TextureFormat,
alpha: crate::AlphaMode,
target_size: [u16; 2],
}

Expand Down
29 changes: 19 additions & 10 deletions blade-graphics/src/vulkan/surface.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use ash::vk;
use std::mem;

impl super::Surface {
pub fn info(&self) -> crate::SurfaceInfo {
crate::SurfaceInfo {
format: self.swapchain.format,
alpha: self.swapchain.alpha,
}
}
}

impl super::Context {
pub fn create_surface<
I: raw_window_handle::HasWindowHandle + raw_window_handle::HasDisplayHandle,
>(
&self,
window: &I,
config: crate::SurfaceConfig,
) -> Result<super::Surface, crate::NotSupportedError> {
let raw = unsafe {
ash_window::create_surface(
Expand Down Expand Up @@ -60,17 +70,21 @@ impl super::Context {
.create_semaphore(&semaphore_create_info, None)
.unwrap()
};
Ok(super::Surface {

let mut this = super::Surface {
raw,
frames: Vec::new(),
next_semaphore,
swapchain: super::Swapchain {
raw: vk::SwapchainKHR::null(),
target_size: [0; 2],
format: crate::TextureFormat::Rgba8Unorm,
alpha: crate::AlphaMode::Ignored,
target_size: [0; 2],
},
_full_screen_exclusive: fullscreen_exclusive_ext.full_screen_exclusive_supported != 0,
})
};
self.reconfigure_surface(&mut this, config);
Ok(this)
}

pub fn destroy_surface(&self, surface: &mut super::Surface) {
Expand All @@ -96,11 +110,7 @@ impl super::Context {
}
}

pub fn configure_surface(
&self,
surface: &mut super::Surface,
config: crate::SurfaceConfig,
) -> crate::SurfaceInfo {
pub fn reconfigure_surface(&self, surface: &mut super::Surface, config: crate::SurfaceConfig) {
let khr_swapchain = self.device.swapchain.as_ref().unwrap();
let khr_surface = self.instance.surface.as_ref().unwrap();

Expand Down Expand Up @@ -333,10 +343,9 @@ impl super::Context {
surface.swapchain = super::Swapchain {
raw: raw_swapchain,
format,
alpha,
target_size,
};

crate::SurfaceInfo { format, alpha }
}

pub fn acquire_frame(&self, surface: &mut super::Surface) -> super::Frame {
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog for Blade
## blade-graphics-0.6 (TBD)

- graphics:
- API for surface creation
- allows multiple windows used by the same context
- API for destruction of pipelines
- every pass now takes a label
- automatic GPU pass markers
Expand Down
31 changes: 14 additions & 17 deletions examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ struct Example {
texture: gpu::Texture,
view: gpu::TextureView,
sampler: gpu::Sampler,
vertex_buf: gpu::Buffer,
window_size: winit::dpi::PhysicalSize<u32>,
bunnies: Vec<Sprite>,
rng: nanorand::WyRand,
surface: gpu::Surface,
context: gpu::Context,
vertex_buf: gpu::Buffer,
}

impl Example {
Expand All @@ -76,20 +76,17 @@ impl Example {
let window_size = window.inner_size();
log::info!("Initial size: {:?}", window_size);

let mut surface = context.create_surface(window).unwrap();
let surface_info = context.configure_surface(
&mut surface,
gpu::SurfaceConfig {
size: gpu::Extent {
width: window_size.width,
height: window_size.height,
depth: 1,
},
usage: gpu::TextureUsage::TARGET,
display_sync: gpu::DisplaySync::Recent,
..Default::default()
let surface_config = gpu::SurfaceConfig {
size: gpu::Extent {
width: window_size.width,
height: window_size.height,
depth: 1,
},
);
usage: gpu::TextureUsage::TARGET,
display_sync: gpu::DisplaySync::Recent,
..Default::default()
};
let surface = context.create_surface(window, surface_config).unwrap();

let global_layout = <Params as gpu::ShaderData>::layout();
let local_layout = <SpriteData as gpu::ShaderData>::layout();
Expand All @@ -116,7 +113,7 @@ impl Example {
depth_stencil: None,
fragment: shader.at("fs_main"),
color_targets: &[gpu::ColorTargetState {
format: surface_info.format,
format: surface.info().format,
blend: Some(gpu::BlendState::ALPHA_BLENDING),
write_mask: gpu::ColorWrites::default(),
}],
Expand Down Expand Up @@ -220,12 +217,12 @@ impl Example {
texture,
view,
sampler,
vertex_buf,
window_size,
bunnies,
rng: nanorand::WyRand::new_seed(73),
surface,
context,
vertex_buf,
}
}

Expand Down Expand Up @@ -338,10 +335,10 @@ impl Example {
if let Some(sp) = self.prev_sync_point.take() {
self.context.wait_for(&sp, !0);
}
self.context.destroy_buffer(self.vertex_buf);
self.context.destroy_texture_view(self.view);
self.context.destroy_texture(self.texture);
self.context.destroy_sampler(self.sampler);
self.context.destroy_buffer(self.vertex_buf);
self.context
.destroy_command_encoder(&mut self.command_encoder);
self.context.destroy_render_pipeline(&mut self.pipeline);
Expand Down
29 changes: 16 additions & 13 deletions examples/particle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct Example {
command_encoder: gpu::CommandEncoder,
prev_sync_point: Option<gpu::SyncPoint>,
context: gpu::Context,
surface: gpu::Surface,
gui_painter: blade_egui::GuiPainter,
particle_system: particle::System,
}
Expand All @@ -16,19 +17,16 @@ impl Example {
fn new(window: &winit::window::Window) -> Self {
let window_size = window.inner_size();
let context = unsafe {
gpu::Context::init_windowed(
window,
gpu::ContextDesc {
validation: cfg!(debug_assertions),
timing: true,
capture: true,
overlay: false,
},
)
gpu::Context::init(gpu::ContextDesc {
presentation: true,
validation: cfg!(debug_assertions),
timing: true,
capture: true,
overlay: false,
})
.unwrap()
};

let surface_info = context.resize(gpu::SurfaceConfig {
let surface_config = gpu::SurfaceConfig {
size: gpu::Extent {
width: window_size.width,
height: window_size.height,
Expand All @@ -37,7 +35,10 @@ impl Example {
usage: gpu::TextureUsage::TARGET,
display_sync: gpu::DisplaySync::Block,
..Default::default()
});
};
let surface = context.create_surface(window, surface_config).unwrap();
let surface_info = surface.info();

let gui_painter = blade_egui::GuiPainter::new(surface_info, &context);
let particle_system = particle::System::new(
&context,
Expand All @@ -60,6 +61,7 @@ impl Example {
command_encoder,
prev_sync_point: Some(sync_point),
context,
surface,
gui_painter,
particle_system,
}
Expand All @@ -73,6 +75,7 @@ impl Example {
.destroy_command_encoder(&mut self.command_encoder);
self.gui_painter.destroy(&self.context);
self.particle_system.destroy(&self.context);
self.context.destroy_surface(&mut self.surface);
}

fn render(
Expand All @@ -81,7 +84,7 @@ impl Example {
gui_textures: &egui::TexturesDelta,
screen_desc: &blade_egui::ScreenDescriptor,
) {
let frame = self.context.acquire_frame();
let frame = self.context.acquire_frame(&mut self.surface);
self.command_encoder.start();
self.command_encoder.init_texture(frame.texture());

Expand Down
33 changes: 17 additions & 16 deletions examples/ray-query/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@ struct Example {
prev_sync_point: Option<gpu::SyncPoint>,
screen_size: gpu::Extent,
context: gpu::Context,
surface: gpu::Surface,
}

impl Example {
fn new(window: &winit::window::Window) -> Self {
let window_size = window.inner_size();
let context = unsafe {
gpu::Context::init_windowed(
window,
gpu::ContextDesc {
validation: cfg!(debug_assertions),
..Default::default()
},
)
gpu::Context::init(gpu::ContextDesc {
presentation: true,
validation: cfg!(debug_assertions),
..Default::default()
})
.unwrap()
};
let capabilities = context.capabilities();
Expand All @@ -66,6 +65,13 @@ impl Example {
height: window_size.height,
depth: 1,
};
let surface_config = gpu::SurfaceConfig {
size: screen_size,
usage: gpu::TextureUsage::TARGET,
transparent: true,
..Default::default()
};
let surface = context.create_surface(window, surface_config).unwrap();

let target = context.create_texture(gpu::TextureDesc {
name: "main",
Expand All @@ -86,13 +92,6 @@ impl Example {
},
);

let surface_info = context.resize(gpu::SurfaceConfig {
size: screen_size,
usage: gpu::TextureUsage::TARGET,
transparent: true,
..Default::default()
});

let source = std::fs::read_to_string("examples/ray-query/shader.wgsl").unwrap();
let shader = context.create_shader(gpu::ShaderDesc { source: &source });
let rt_layout = <ShaderData as gpu::ShaderData>::layout();
Expand All @@ -112,7 +111,7 @@ impl Example {
vertex: shader.at("draw_vs"),
vertex_fetches: &[],
fragment: shader.at("draw_fs"),
color_targets: &[surface_info.format.into()],
color_targets: &[surface.info().format.into()],
depth_stencil: None,
});

Expand Down Expand Up @@ -241,6 +240,7 @@ impl Example {
command_encoder,
prev_sync_point: None,
screen_size,
surface,
context,
}
}
Expand All @@ -258,6 +258,7 @@ impl Example {
self.context.destroy_compute_pipeline(&mut self.rt_pipeline);
self.context
.destroy_render_pipeline(&mut self.draw_pipeline);
self.context.destroy_surface(&mut self.surface);
}

fn render(&mut self) {
Expand Down Expand Up @@ -289,7 +290,7 @@ impl Example {
}
}

let frame = self.context.acquire_frame();
let frame = self.context.acquire_frame(&mut self.surface);
self.command_encoder.init_texture(frame.texture());

if let mut pass = self.command_encoder.render(
Expand Down
Loading

0 comments on commit f847a4e

Please sign in to comment.