Skip to content

Commit

Permalink
Expose renderer properties to the application
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyas-taouaou committed Oct 29, 2024
1 parent 4ea7942 commit a26df6e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 40 deletions.
24 changes: 11 additions & 13 deletions engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ mod rendering_context;

use crate::rendering_context::{queue_family_picker, RenderingContext, RenderingContextAttributes};
use anyhow::Result;
use ash::vk;
use renderer::window_renderer::WindowRenderer;
use std::collections::HashMap;
use std::sync::Arc;
use winit::event::WindowEvent;
use winit::event_loop::ActiveEventLoop;
use winit::window::{Window, WindowAttributes, WindowId};

pub use crate::renderer::window_renderer::WindowRendererAttributes;
pub use anyhow;
pub use ash::vk;
pub use winit;

pub struct Engine {
Expand All @@ -24,8 +25,12 @@ pub struct Engine {
}

impl Engine {
pub fn new(event_loop: &ActiveEventLoop) -> Result<Self> {
let primary_window = Arc::new(event_loop.create_window(Default::default())?);
pub fn new(
event_loop: &ActiveEventLoop,
primary_window_attributes: WindowAttributes,
primary_renderer_attributes: WindowRendererAttributes,
) -> Result<Self> {
let primary_window = Arc::new(event_loop.create_window(primary_window_attributes)?);
let primary_window_id = primary_window.id();

let rendering_context = Arc::new(RenderingContext::new(RenderingContextAttributes {
Expand All @@ -41,11 +46,7 @@ impl Engine {
let renderer = WindowRenderer::new(
rendering_context.clone(),
window.clone(),
2,
vk::Format::R16G16B16A16_SFLOAT,
vk::ClearColorValue {
float32: [0.0, 0.0, 0.0, 1.0],
},
primary_renderer_attributes.clone(),
)
.unwrap();
(*id, renderer)
Expand Down Expand Up @@ -98,6 +99,7 @@ impl Engine {
&mut self,
event_loop: &ActiveEventLoop,
attributes: WindowAttributes,
renderer_attributes: WindowRendererAttributes,
) -> Result<WindowId> {
let window = Arc::new(event_loop.create_window(attributes)?);
let window_id = window.id();
Expand All @@ -106,11 +108,7 @@ impl Engine {
let renderer = WindowRenderer::new(
self.rendering_context.clone(),
window.clone(),
2,
vk::Format::R16G16B16A16_SFLOAT,
vk::ClearColorValue {
float32: [0.0, 0.0, 0.0, 1.0],
},
renderer_attributes,
)?;
self.renderers.insert(window_id, renderer);

Expand Down
13 changes: 11 additions & 2 deletions engine/src/renderer/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Commands {
dst_image: &mut Image,
src_offsets: [vk::Offset3D; 2],
dst_offsets: [vk::Offset3D; 2],
filter: vk::Filter,
) -> &Self {
self.ensure_image_layout(src_image, ImageLayoutState::transfer_source())
.ensure_image_layout(dst_image, ImageLayoutState::transfer_destination());
Expand All @@ -141,7 +142,7 @@ impl Commands {
.src_offsets(src_offsets)
.dst_subresource(dst_image.subresource_layers())
.dst_offsets(dst_offsets)],
vk::Filter::NEAREST,
filter,
);
}

Expand All @@ -154,6 +155,7 @@ impl Commands {
dst_image: &mut Image,
src_extent: vk::Extent3D,
dst_extent: vk::Extent3D,
filter: vk::Filter,
) -> &Self {
self.blit_image(
src_image,
Expand All @@ -174,15 +176,22 @@ impl Commands {
z: dst_extent.depth as i32,
},
],
filter,
)
}

pub fn blit_full_image(&self, src_image: &mut Image, dst_image: &mut Image) -> &Self {
pub fn blit_full_image(
&self,
src_image: &mut Image,
dst_image: &mut Image,
filter: vk::Filter,
) -> &Self {
self.blit_image_extent(
src_image,
dst_image,
src_image.attributes.extent,
dst_image.attributes.extent,
filter,
)
}

Expand Down
1 change: 0 additions & 1 deletion engine/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ struct Vertex {
struct PushConstants {
vertex_buffer_address: vk::DeviceAddress,
}

impl Renderer {
pub fn new(
context: Arc<RenderingContext>,
Expand Down
43 changes: 28 additions & 15 deletions engine/src/renderer/window_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,40 @@ struct Frame {
in_flight_fence: vk::Fence,
}

#[derive(Clone)]
pub struct WindowRendererAttributes {
pub format: vk::Format,
pub clear_color: vk::ClearColorValue,
pub ssaa: f32,
pub ssaa_filter: vk::Filter,
pub in_flight_frames_count: usize,
}

pub struct WindowRenderer {
in_flight_frames_count: usize,
frame_index: usize,
frames: Vec<Frame>,
command_pool: vk::CommandPool,
swapchain: Swapchain,
context: Arc<RenderingContext>,

clear_color: vk::ClearColorValue,
attributes: WindowRendererAttributes,

pub renderer: Renderer,
pub window: Arc<Window>,
}

fn scale_extent(extent: vk::Extent2D, scale: f32) -> vk::Extent2D {
vk::Extent2D {
width: (extent.width as f32 * scale) as u32,
height: (extent.height as f32 * scale) as u32,
}
}

impl WindowRenderer {
pub fn new(
context: Arc<RenderingContext>,
window: Arc<Window>,
in_flight_frames_count: usize,
format: vk::Format,
clear_color: vk::ClearColorValue,
attributes: WindowRendererAttributes,
) -> Result<Self> {
let mut swapchain = Swapchain::new(context.clone(), window.clone())?;
swapchain.resize()?;
Expand All @@ -54,7 +67,7 @@ impl WindowRenderer {
&vk::CommandBufferAllocateInfo::default()
.command_pool(command_pool)
.level(vk::CommandBufferLevel::PRIMARY)
.command_buffer_count(in_flight_frames_count as u32),
.command_buffer_count(attributes.in_flight_frames_count as u32),
)?;

let mut frames = Vec::with_capacity(command_buffers.len());
Expand Down Expand Up @@ -85,9 +98,9 @@ impl WindowRenderer {

let mut renderer = Renderer::new(
context.clone(),
swapchain.extent,
format,
in_flight_frames_count,
scale_extent(swapchain.extent, attributes.ssaa),
attributes.format,
attributes.in_flight_frames_count,
&commands,
)?;

Expand All @@ -107,15 +120,14 @@ impl WindowRenderer {
context.device.destroy_fence(fence, None);

Ok(Self {
in_flight_frames_count,
frame_index: 0,
frames,
command_pool,
swapchain,
context,
clear_color,
renderer,
window,
attributes,
})
}
}
Expand All @@ -139,7 +151,8 @@ impl WindowRenderer {
if swapchain_extent.width == 0 || swapchain_extent.height == 0 {
return Ok(());
}
self.renderer.resize(swapchain_extent)?;
self.renderer
.resize(scale_extent(swapchain_extent, self.attributes.ssaa))?;
}

let swapchain_extent = self.swapchain.extent;
Expand Down Expand Up @@ -175,9 +188,9 @@ impl WindowRenderer {
let commands = Commands::new(self.context.clone(), command_buffer)?;
let render_target =
self.renderer
.render(&commands, self.clear_color, self.frame_index)?;
.render(&commands, self.attributes.clear_color, self.frame_index)?;
commands
.blit_full_image(render_target, swapchain_image)
.blit_full_image(render_target, swapchain_image, self.attributes.ssaa_filter)
.transition_image_layout(swapchain_image, ImageLayoutState::present())
.submit(
graphics_queue,
Expand All @@ -195,7 +208,7 @@ impl WindowRenderer {
self.swapchain
.present(image_index, frame.render_finished_semaphore)?;

self.frame_index = (self.frame_index + 1) % self.in_flight_frames_count;
self.frame_index = (self.frame_index + 1) % self.attributes.in_flight_frames_count;
Ok(())
}
}
Expand Down
51 changes: 43 additions & 8 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use engine::winit;
use engine::winit::window::WindowAttributes;
use ::engine::Engine;
use engine::{vk, winit, WindowRendererAttributes};
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ActiveEventLoop;
Expand All @@ -13,14 +13,49 @@ pub struct App {

impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
self.engine = Some(Engine::new(event_loop).unwrap());
let primary_window_attributes = WindowAttributes::default().with_title("Primary window");
let primary_window_renderer_attributes = WindowRendererAttributes {
format: vk::Format::R16G16B16A16_SFLOAT,
clear_color: vk::ClearColorValue {
float32: [0.0, 0.0, 0.0, 1.0],
},
ssaa: 1.0,
ssaa_filter: vk::Filter::NEAREST,
in_flight_frames_count: 2,
};

let secondary_window_attributes =
WindowAttributes::default().with_title("Secondary window");
let secondary_window_renderer_attributes = WindowRendererAttributes {
format: vk::Format::R16G16B16A16_SFLOAT,
clear_color: vk::ClearColorValue {
float32: [0.0, 0.0, 0.0, 1.0],
},
ssaa: 0.1,
ssaa_filter: vk::Filter::NEAREST,
in_flight_frames_count: 2,
};

let secondary_window_count = 3;

self.engine = Some(
Engine::new(
event_loop,
primary_window_attributes,
primary_window_renderer_attributes,
)
.unwrap(),
);
if let Some(engine) = self.engine.as_mut() {
_ = engine
.create_window(
event_loop,
WindowAttributes::default().with_title("Secondary window"),
)
.unwrap();
for _ in 0..secondary_window_count {
_ = engine
.create_window(
event_loop,
secondary_window_attributes.clone(),
secondary_window_renderer_attributes.clone(),
)
.unwrap();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod app;

fn main() -> Result<()> {
tracing_subscriber::fmt::fmt()
.with_max_level(tracing::Level::TRACE)
.with_max_level(tracing::Level::DEBUG)
.init();

let mut app = App::default();
Expand Down

0 comments on commit a26df6e

Please sign in to comment.