Skip to content

Commit

Permalink
feature(wgpu): WGPU renderer (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Jul 10, 2023
1 parent 02d0ba0 commit 8e074b9
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 162 deletions.
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "helix"
crate-type = ["lib", "staticlib"]

[features]
default = ["f3dex2", "opengl_renderer"]
default = ["f3dex2", "wgpu_renderer"]

# Graphics Features
f3dex2 = ["fast3d/f3dex2"]
Expand All @@ -31,7 +31,7 @@ env_logger = "0.10.0"
gilrs = { version = "0.10.2", features = ["serde", "serde-serialize"] }
glium = { version = "0.32.1", optional = true }
imgui = { version = "0.11.0", features = ["docking"] }
imgui-wgpu = { git = "https://github.com/retrofoundry/imgui-wgpu-rs.git", branch = "helix", optional = true }
imgui-wgpu = { git = "https://github.com/Yatekii/imgui-wgpu-rs.git", optional = true }
imgui-winit-support = "0.11.0"
imgui-glium-renderer = { version = "0.11.0", optional = true }
winit = { version = "0.27.5", features = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"] }
Expand All @@ -44,9 +44,10 @@ glutin = { version = "0.29.1", optional = true }
wgpu = { version = "0.16", optional = true }
spin_sleep = "1.1.1"
arie = "0.2.0"
fast3d = { version = "0.3.1", default-features = false }
fast3d-glium-renderer = { version = "0.3.1", optional = true }
fast3d-wgpu-renderer = { version = "0.3.1", optional = true }
fast3d = { version = "0.4.0", default-features = false }
fast3d-glium-renderer = { version = "0.4.0", optional = true }
fast3d-wgpu-renderer = { version = "0.4.0", optional = true }
rustc-hash = "1.1.0"

[patch.crates-io]
#fast3d = { path = "../../fast3d-rs/fast3d" }
Expand Down
19 changes: 11 additions & 8 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ impl EventLoopWrapper {
}
}

impl Default for EventLoopWrapper {
fn default() -> Self {
Self::new()
}
}

pub struct Gui<'a> {
// imgui
imgui: imgui::Context,
Expand Down Expand Up @@ -210,7 +216,7 @@ impl<'a> Gui<'a> {

pub fn process_draw_lists(&mut self, commands: usize) -> anyhow::Result<()> {
// Set RDP output dimensions
let size = self.gfx_renderer.window_size();
let size = self.gfx_renderer.content_size();
let dimensions = OutputDimensions {
width: size.width,
height: size.height,
Expand All @@ -224,11 +230,7 @@ impl<'a> Gui<'a> {
// Grab the frame
let mut frame = self.gfx_renderer.get_current_texture().unwrap();

// Render RCP output
self.gfx_renderer
.process_rcp_output(&mut frame, &mut self.rcp_output)?;

// Render ImGui on top of any game content
// Draw the UI
let ui = self.imgui.new_frame();
ui.main_menu_bar(|| (self.draw_menu_callback)(ui));
(self.draw_windows_callback)(ui);
Expand All @@ -238,9 +240,10 @@ impl<'a> Gui<'a> {
self.gfx_renderer.prepare_render(&mut self.platform, ui);
}

// Render RCPOutput and ImGui content
let draw_data = self.imgui.render();
self.gfx_renderer
.draw_imgui_content(&mut frame, draw_data)?;
.draw_content(&mut frame, &mut self.rcp_output, draw_data)?;

// Clear the draw calls
self.rcp_output.clear_draw_calls();
Expand All @@ -262,7 +265,7 @@ type OnDraw = unsafe extern "C" fn(ui: &imgui::Ui);

#[no_mangle]
pub extern "C" fn GUICreateEventLoop() -> Box<EventLoopWrapper> {
let event_loop = EventLoopWrapper::new();
let event_loop = EventLoopWrapper::default();
Box::new(event_loop)
}

Expand Down
31 changes: 17 additions & 14 deletions src/gui/glium_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::gui::{EventLoopWrapper, Frame};
use fast3d::output::RCPOutput;
use fast3d::rdp::OutputDimensions;
use fast3d_glium_renderer::glium_device::GliumGraphicsDevice;

pub struct Renderer<'a> {
Expand Down Expand Up @@ -31,10 +32,14 @@ impl<'a> Renderer<'a> {
// Create the renderer
let renderer = imgui_glium_renderer::Renderer::init(imgui, &display)?;

// Create graphics device
let size = display.gl_window().window().inner_size();
let graphics_device = GliumGraphicsDevice::new([size.width, size.height]);

Ok(Self {
display,
renderer,
graphics_device: GliumGraphicsDevice::default(),
graphics_device,
})
}

Expand Down Expand Up @@ -80,48 +85,46 @@ impl<'a> Renderer<'a> {

// Rendering Functions

pub fn window_size(&self) -> winit::dpi::PhysicalSize<u32> {
pub fn content_size(&self) -> winit::dpi::PhysicalSize<u32> {
self.display.gl_window().window().inner_size()
}

pub fn resize(&mut self, width: u32, height: u32) {
// there's a bug where at first the size is u32::MAX so we just ignore it
if width == u32::MAX || height == u32::MAX {
return;
}

log::trace!("Resizing to {:?}x{:?}", width, height);
self.display
.gl_window()
.resize(glutin::dpi::PhysicalSize::new(width, height));
self.graphics_device.resize([width, height]);
}

pub fn get_current_texture(&self) -> Option<Frame> {
let frame = self.display.draw();
Some(frame)
}

pub fn process_rcp_output(
pub fn draw_content(
&mut self,
frame: &mut Frame,
rcp_output: &mut RCPOutput,
imgui_draw_data: &imgui::DrawData,
) -> anyhow::Result<()> {
// Prepare the context device
self.graphics_device.start_frame(frame);

// Process the RCP output
self.render_game(frame, rcp_output)?;

// Finish rendering
self.graphics_device.end_frame();
// Render the ImGui content
self.renderer.render(frame, imgui_draw_data)?;

Ok(())
}

pub fn draw_imgui_content(
&mut self,
frame: &mut Frame,
draw_data: &imgui::DrawData,
) -> anyhow::Result<()> {
self.renderer.render(frame, draw_data)?;
Ok(())
}

pub fn finish_render(&mut self, frame: Frame) -> anyhow::Result<()> {
frame.finish()?;
Ok(())
Expand Down
Loading

0 comments on commit 8e074b9

Please sign in to comment.