wgui: introduce SharedContext to share between Contexts

This commit is contained in:
galister
2025-06-25 21:06:05 +09:00
parent 44b6da8967
commit 158562031f
5 changed files with 363 additions and 322 deletions

View File

@@ -13,7 +13,8 @@ use vulkano::{
format::Format,
image::{ImageUsage, view::ImageView},
swapchain::{
Surface, SurfaceInfo, Swapchain, SwapchainCreateInfo, SwapchainPresentInfo, acquire_next_image,
Surface, SurfaceInfo, Swapchain, SwapchainCreateInfo, SwapchainPresentInfo,
acquire_next_image,
},
sync::GpuFuture,
};
@@ -95,10 +96,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut mouse = Vec2::ZERO;
let mut render_context =
renderer_vk::context::Context::new(gfx.clone(), gfx.surface_format, scale)?;
let mut shared_context = renderer_vk::context::SharedContext::new(gfx.clone())?;
let mut render_context = renderer_vk::context::Context::new(&mut shared_context, scale)?;
render_context.update_viewport(swapchain_size, scale)?;
render_context.update_viewport(&mut shared_context, swapchain_size, scale)?;
println!("new swapchain_size: {swapchain_size:?}");
let mut profiler = profiler::Profiler::new(100);
@@ -180,14 +181,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if event.physical_key == PhysicalKey::Code(KeyCode::Equal) {
scale *= 1.25;
render_context
.update_viewport(swapchain_size, scale)
.update_viewport(&mut shared_context, swapchain_size, scale)
.unwrap();
}
if event.physical_key == PhysicalKey::Code(KeyCode::Minus) {
scale *= 0.75;
render_context
.update_viewport(swapchain_size, scale)
.update_viewport(&mut shared_context, swapchain_size, scale)
.unwrap();
}
}
@@ -215,7 +216,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
swapchain_create_info.image_extent = swapchain_size;
(swapchain, images) = {
let (swapchain, images) = swapchain.recreate(swapchain_create_info.clone()).unwrap();
let (swapchain, images) =
swapchain.recreate(swapchain_create_info.clone()).unwrap();
let image_views = images
.into_iter()
@@ -226,7 +228,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};
render_context
.update_viewport(swapchain_size, scale)
.update_viewport(&mut shared_context, swapchain_size, scale)
.unwrap();
println!("new swapchain_size: {swapchain_size:?}");
@@ -258,8 +260,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
profiler.start();
{
let (image_index, _, acquire_future) =
match acquire_next_image(swapchain.clone(), None).map_err(Validated::unwrap) {
let (image_index, _, acquire_future) = match acquire_next_image(
swapchain.clone(),
None,
)
.map_err(Validated::unwrap)
{
Ok(r) => r,
Err(VulkanError::OutOfDate) => {
recreate = true;
@@ -279,7 +285,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let primitives = wgui::drawing::draw(testbed.layout()).unwrap();
render_context
.draw(&gfx, &mut cmd_buf, &primitives)
.draw(&mut shared_context, &mut cmd_buf, &primitives)
.unwrap();
cmd_buf.end_rendering().unwrap();
@@ -291,7 +297,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.unwrap()
.then_swapchain_present(
gfx.queue_gfx.clone(),
SwapchainPresentInfo::swapchain_image_index(swapchain.clone(), image_index),
SwapchainPresentInfo::swapchain_image_index(
swapchain.clone(),
image_index,
),
)
.then_signal_fence_and_flush()
.unwrap()

View File

@@ -2,6 +2,7 @@ use std::{cell::RefCell, rc::Rc, sync::Arc};
use cosmic_text::Buffer;
use glam::{Mat4, Vec2, Vec3};
use slotmap::{SlotMap, new_key_type};
use crate::{
drawing,
@@ -70,48 +71,81 @@ impl RendererPass<'_> {
}
}
pub struct Context {
viewport: Viewport,
text_atlas: TextAtlas,
new_key_type! {
struct SharedContextKey;
}
pub struct SharedContext {
gfx: Arc<WGfx>,
atlas_map: SlotMap<SharedContextKey, SharedAtlas>,
rect_pipeline: RectPipeline,
text_pipeline: TextPipeline,
}
impl SharedContext {
pub fn new(gfx: Arc<WGfx>) -> anyhow::Result<Self> {
let rect_pipeline = RectPipeline::new(gfx.clone(), gfx.surface_format)?;
let text_pipeline = TextPipeline::new(gfx.clone(), gfx.surface_format)?;
Ok(Self {
gfx,
atlas_map: SlotMap::with_key(),
rect_pipeline,
text_pipeline,
})
}
fn atlas_for_pixel_scale(&mut self, pixel_scale: f32) -> anyhow::Result<SharedContextKey> {
for (key, atlas) in self.atlas_map.iter() {
if (atlas.pixel_scale - pixel_scale).abs() < f32::EPSILON {
return Ok(key);
}
}
log::debug!("Initializing SharedAtlas for pixel scale {pixel_scale:.1}");
let text_atlas = TextAtlas::new(self.text_pipeline.clone())?;
Ok(self.atlas_map.insert(SharedAtlas {
text_atlas,
pixel_scale,
}))
}
}
pub struct SharedAtlas {
text_atlas: TextAtlas,
pixel_scale: f32,
}
pub struct Context {
viewport: Viewport,
shared_ctx_key: SharedContextKey,
pub dirty: bool,
pixel_scale: f32,
empty_text: Rc<RefCell<Buffer>>,
}
impl Context {
pub fn new(
gfx: Arc<WGfx>,
native_format: vulkano::format::Format,
pixel_scale: f32,
) -> anyhow::Result<Self> {
let rect_pipeline = RectPipeline::new(gfx.clone(), native_format)?;
let text_pipeline = TextPipeline::new(gfx.clone(), native_format)?;
let viewport = Viewport::new(gfx.clone())?;
let text_atlas = TextAtlas::new(text_pipeline.clone())?;
pub fn new(shared: &mut SharedContext, pixel_scale: f32) -> anyhow::Result<Self> {
let viewport = Viewport::new(shared.gfx.clone())?;
let shared_ctx_key = shared.atlas_for_pixel_scale(pixel_scale)?;
Ok(Self {
viewport,
text_atlas,
rect_pipeline,
text_pipeline,
shared_ctx_key,
pixel_scale,
dirty: true,
empty_text: Rc::new(RefCell::new(Buffer::new_empty(DEFAULT_METRICS))),
})
}
pub fn regen(&mut self) -> anyhow::Result<()> {
self.text_atlas = TextAtlas::new(self.text_pipeline.clone())?;
self.dirty = true;
Ok(())
}
pub fn update_viewport(&mut self, resolution: [u32; 2], pixel_scale: f32) -> anyhow::Result<()> {
if self.pixel_scale != pixel_scale {
pub fn update_viewport(
&mut self,
shared: &mut SharedContext,
resolution: [u32; 2],
pixel_scale: f32,
) -> anyhow::Result<()> {
if (self.pixel_scale - pixel_scale).abs() < f32::EPSILON {
self.pixel_scale = pixel_scale;
self.regen()?;
self.shared_ctx_key = shared.atlas_for_pixel_scale(pixel_scale)?;
}
if self.viewport.resolution() != resolution {
@@ -143,34 +177,20 @@ impl Context {
Ok(())
}
fn new_pass(&mut self, passes: &mut Vec<RendererPass>) -> anyhow::Result<()> {
passes.push(RendererPass::new(
&mut self.text_atlas,
self.rect_pipeline.clone(),
)?);
Ok(())
}
fn submit_pass(
&mut self,
gfx: &Arc<WGfx>,
cmd_buf: &mut GfxCommandBuffer,
pass: &mut RendererPass,
) -> anyhow::Result<()> {
pass.submit(gfx, &mut self.viewport, cmd_buf, &mut self.text_atlas)?;
Ok(())
}
pub fn draw(
&mut self,
gfx: &Arc<WGfx>,
shared: &mut SharedContext,
cmd_buf: &mut GfxCommandBuffer,
primitives: &[drawing::RenderPrimitive],
) -> anyhow::Result<()> {
self.dirty = false;
let mut passes = Vec::<RendererPass>::new();
self.new_pass(&mut passes)?;
let atlas = shared.atlas_map.get_mut(self.shared_ctx_key).unwrap();
let mut passes = vec![RendererPass::new(
&mut atlas.text_atlas,
shared.rect_pipeline.clone(),
)?];
for primitive in primitives.iter() {
let pass = passes.last_mut().unwrap(); // always safe
@@ -214,7 +234,12 @@ impl Context {
}
let pass = passes.last_mut().unwrap();
self.submit_pass(gfx, cmd_buf, pass)?;
pass.submit(
&shared.gfx,
&mut self.viewport,
cmd_buf,
&mut atlas.text_atlas,
)?;
Ok(())
}

View File

@@ -33,11 +33,11 @@ pub struct GuiPanel {
}
impl GuiPanel {
pub fn new_from_template(app: &AppState, path: &str) -> anyhow::Result<Self> {
pub fn new_from_template(app: &mut AppState, path: &str) -> anyhow::Result<Self> {
let (layout, _state) =
wgui::parser::new_layout_from_assets(Box::new(gui::asset::GuiAsset {}), path)?;
let context = WguiContext::new(app.gfx.clone(), app.gfx.surface_format, 1.0)?;
let context = WguiContext::new(&mut app.wgui_shared, 1.0)?;
let mut timestep = Timestep::new();
timestep.set_tps(60.0);
@@ -48,9 +48,9 @@ impl GuiPanel {
})
}
pub fn new_blank(app: &AppState) -> anyhow::Result<Self> {
pub fn new_blank(app: &mut AppState) -> anyhow::Result<Self> {
let layout = Layout::new(Box::new(GuiAsset {}))?;
let context = WguiContext::new(app.gfx.clone(), app.gfx.surface_format, 1.0)?;
let context = WguiContext::new(&mut app.wgui_shared, 1.0)?;
let mut timestep = Timestep::new();
timestep.set_tps(60.0);
@@ -177,7 +177,8 @@ impl OverlayRenderer for GuiPanel {
buf: &mut CommandBuffers,
_alpha: f32,
) -> anyhow::Result<bool> {
self.context.update_viewport(tgt.extent_u32arr(), 1.0)?;
self.context
.update_viewport(&mut app.wgui_shared, tgt.extent_u32arr(), 1.0)?;
self.layout.update(MAX_SIZE_VEC2, self.timestep.alpha)?;
let mut cmd_buf = app
@@ -187,7 +188,8 @@ impl OverlayRenderer for GuiPanel {
cmd_buf.begin_rendering(tgt)?;
let primitives = wgui::drawing::draw(&self.layout)?;
self.context.draw(&app.gfx, &mut cmd_buf, &primitives)?;
self.context
.draw(&mut app.wgui_shared, &mut cmd_buf, &primitives)?;
cmd_buf.end_rendering()?;
buf.push(cmd_buf.build()?);

View File

@@ -31,7 +31,7 @@ const PIXELS_PER_UNIT: f32 = 80.;
#[allow(clippy::too_many_lines, clippy::significant_drop_tightening)]
pub fn create_keyboard<O>(
app: &AppState,
app: &mut AppState,
mut keymap: Option<XkbKeymap>,
) -> anyhow::Result<OverlayData<O>>
where

View File

@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use smallvec::{SmallVec, smallvec};
use std::sync::Arc;
use vulkano::image::view::ImageView;
use wgui::gfx::WGfx;
use wgui::{gfx::WGfx, renderer_vk::context::SharedContext as WSharedContext};
#[cfg(feature = "wayvr")]
use {
@@ -34,6 +34,8 @@ pub struct AppState {
pub hid_provider: HidWrapper,
pub audio_provider: AudioOutput,
pub wgui_shared: WSharedContext,
pub input_state: InputState,
pub screens: SmallVec<[ScreenMeta; 8]>,
pub anchor: Affine3A,
@@ -71,6 +73,8 @@ impl AppState {
include_bytes!("res/557297.wav"),
);
let wgui_shared = WSharedContext::new(gfx.clone())?;
Ok(Self {
session,
tasks,
@@ -78,6 +82,7 @@ impl AppState {
gfx_extras,
hid_provider: HidWrapper::new(),
audio_provider: AudioOutput::new(),
wgui_shared,
input_state: InputState::new(),
screens: smallvec![],
anchor: Affine3A::IDENTITY,