Skip to content

Commit

Permalink
Vulkan boilerplate and beginning of atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
someone13574 committed Mar 9, 2024
1 parent a03feca commit c29295d
Show file tree
Hide file tree
Showing 9 changed files with 787 additions and 75 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/gpui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ test-support = [
]
runtime_shaders = []
macos-blade = ["blade-graphics", "blade-macros", "blade-rwh", "bytemuck"]
vulkan-render = ["ash"]
# default = ["vulkan-render"]

[lib]
path = "src/gpui.rs"
doctest = false

[dependencies]
anyhow.workspace = true
ash = { version = "0.37.3", features = ["linked"], optional = true }
async-task = "4.7"
backtrace = { version = "0.3", optional = true }
blade-graphics = { workspace = true, optional = true }
Expand Down
8 changes: 7 additions & 1 deletion crates/gpui/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ mod linux;
#[cfg(target_os = "macos")]
mod mac;

#[cfg(any(target_os = "linux", target_os = "windows", feature = "macos-blade"))]
#[cfg(all(
any(target_os = "linux", target_os = "windows", feature = "macos-blade"),
not(feature = "vulkan-render")
))]
mod blade;

#[cfg(all(target_os = "linux", feature = "vulkan-render"))]
mod vulkan;

#[cfg(any(test, feature = "test-support"))]
mod test;

Expand Down
36 changes: 19 additions & 17 deletions crates/gpui/src/platform/blade/blade_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use super::{BladeAtlas, BladeBelt, BladeBeltDescriptor, PATH_TEXTURE_FORMAT};
use crate::{
AtlasTextureKind, AtlasTile, Bounds, ContentMask, Hsla, MonochromeSprite, Path, PathId,
PathVertex, PolychromeSprite, PrimitiveBatch, Quad, ScaledPixels, Scene, Shadow, Size,
PathVertex, Pixels, PolychromeSprite, PrimitiveBatch, Quad, ScaledPixels, Scene, Shadow, Size,
Underline,
};
use bytemuck::{Pod, Zeroable};
Expand Down Expand Up @@ -343,7 +343,7 @@ pub struct BladeRenderer {
last_sync_point: Option<gpu::SyncPoint>,
pipelines: BladePipelines,
instance_belt: BladeBelt,
viewport_size: gpu::Extent,
viewport_size: Size<Pixels>,
path_tiles: HashMap<PathId, AtlasTile>,
atlas: Arc<BladeAtlas>,
atlas_sampler: gpu::Sampler,
Expand All @@ -363,8 +363,12 @@ impl BladeRenderer {
}
}

pub fn new(gpu: Arc<gpu::Context>, size: gpu::Extent) -> Self {
let surface_format = gpu.resize(Self::make_surface_config(size));
pub fn new(gpu: Arc<gpu::Context>, size: Size<Pixels>) -> Self {
let surface_format = gpu.resize(Self::make_surface_config(gpu::Extent {
width: size.width.into(),
height: size.height.into(),
depth: 1,
}));
let command_encoder = gpu.create_command_encoder(gpu::CommandEncoderDesc {
name: "main",
buffer_count: 2,
Expand Down Expand Up @@ -412,21 +416,19 @@ impl BladeRenderer {
}
}

pub fn update_drawable_size(&mut self, size: Size<f64>) {
let gpu_size = gpu::Extent {
width: size.width as u32,
height: size.height as u32,
depth: 1,
};

if gpu_size != self.viewport_size() {
pub fn update_drawable_size(&mut self, size: Size<Pixels>) {
if size != self.viewport_size() {
self.wait_for_gpu();
self.gpu.resize(Self::make_surface_config(gpu_size));
self.viewport_size = gpu_size;
self.gpu.resize(Self::make_surface_config(gpu::Extent {
width: size.width.into(),
height: size.height.into(),
depth: 1,
}));
self.viewport_size = size;
}
}

pub fn viewport_size(&self) -> gpu::Extent {
pub fn viewport_size(&self) -> Size<Pixels> {
self.viewport_size
}

Expand Down Expand Up @@ -520,8 +522,8 @@ impl BladeRenderer {

let globals = GlobalParams {
viewport_size: [
self.viewport_size.width as f32,
self.viewport_size.height as f32,
self.viewport_size.width.into(),
self.viewport_size.height.into(),
],
pad: [0; 2],
};
Expand Down
128 changes: 103 additions & 25 deletions crates/gpui/src/platform/linux/wayland/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ use std::num::NonZeroU32;
use std::rc::Rc;
use std::sync::Arc;

#[cfg(not(feature = "vulkan-render"))]
use crate::platform::blade::BladeRenderer;
#[cfg(not(feature = "vulkan-render"))]
use blade_graphics as gpu;
#[cfg(not(feature = "vulkan-render"))]
use blade_rwh::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle};

#[cfg(feature = "vulkan-render")]
use crate::platform::vulkan::{SurfaceExtensionLoader, VulkanRawWindow, VulkanRenderer};

use collections::HashSet;
use futures::channel::oneshot::Receiver;
use raw_window_handle::{
Expand All @@ -17,12 +25,11 @@ use wayland_client::{protocol::wl_surface, Proxy};
use wayland_protocols::wp::viewporter::client::wp_viewport;
use wayland_protocols::xdg::shell::client::xdg_toplevel;

use crate::platform::blade::BladeRenderer;
use crate::platform::linux::wayland::display::WaylandDisplay;
use crate::platform::{PlatformAtlas, PlatformInputHandler, PlatformWindow};
use crate::scene::Scene;
use crate::{
px, size, Bounds, Modifiers, Pixels, PlatformDisplay, PlatformInput, Point, PromptLevel, Size,
px, Bounds, Modifiers, Pixels, PlatformDisplay, PlatformInput, Point, PromptLevel, Size,
WindowAppearance, WindowBounds, WindowOptions,
};

Expand All @@ -39,8 +46,13 @@ pub(crate) struct Callbacks {
appearance_changed: Option<Box<dyn FnMut()>>,
}

#[cfg(not(feature = "vulkan-render"))]
type Renderer = BladeRenderer;
#[cfg(feature = "vulkan-render")]
type Renderer = VulkanRenderer;

struct WaylandWindowInner {
renderer: BladeRenderer,
renderer: Renderer,
bounds: Bounds<u32>,
scale: f32,
fullscreen: bool,
Expand All @@ -53,6 +65,7 @@ struct RawWindow {
display: *mut c_void,
}

#[cfg(not(feature = "vulkan-render"))]
unsafe impl HasRawWindowHandle for RawWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
let mut wh = blade_rwh::WaylandWindowHandle::empty();
Expand All @@ -61,6 +74,7 @@ unsafe impl HasRawWindowHandle for RawWindow {
}
}

#[cfg(not(feature = "vulkan-render"))]
unsafe impl HasRawDisplayHandle for RawWindow {
fn raw_display_handle(&self) -> RawDisplayHandle {
let mut dh = blade_rwh::WaylandDisplayHandle::empty();
Expand All @@ -69,6 +83,54 @@ unsafe impl HasRawDisplayHandle for RawWindow {
}
}

#[cfg(feature = "vulkan-render")]
impl VulkanRawWindow for RawWindow {
fn extension_name(&self) -> &std::ffi::CStr {
ash::extensions::khr::WaylandSurface::name()
}

fn extension_loader(
&self,
entry: &ash::Entry,
instance: &ash::Instance,
) -> SurfaceExtensionLoader {
SurfaceExtensionLoader::Wayland(ash::extensions::khr::WaylandSurface::new(
&entry, &instance,
))
}

fn create_surface(&self, loader: &SurfaceExtensionLoader) -> ash::vk::SurfaceKHR {
if let SurfaceExtensionLoader::Wayland(loader) = loader {
let create_info = ash::vk::WaylandSurfaceCreateInfoKHR::builder()
.display(self.display)
.surface(self.window)
.build();

unsafe { loader.create_wayland_surface(&create_info, None) }.unwrap()
} else {
unreachable!()
}
}

fn get_physical_device_presentation_support(
&self,
loader: &SurfaceExtensionLoader,
physical_device: ash::vk::PhysicalDevice,
queue_family_index: u32,
) -> bool {
match loader {
SurfaceExtensionLoader::Wayland(loader) => unsafe {
loader.get_physical_device_wayland_presentation_support(
physical_device,
queue_family_index,
&mut *self.display,
)
},
_ => unreachable!(),
}
}
}

impl WaylandWindowInner {
fn new(wl_surf: &Arc<wl_surface::WlSurface>, bounds: Bounds<u32>) -> Self {
let raw = RawWindow {
Expand All @@ -80,26 +142,42 @@ impl WaylandWindowInner {
.display_ptr()
.cast::<c_void>(),
};
let gpu = Arc::new(
unsafe {
gpu::Context::init_windowed(
&raw,
gpu::ContextDesc {
validation: false,
capture: false,
overlay: false,
},
)
}
.unwrap(),
);
let extent = gpu::Extent {
width: bounds.size.width,
height: bounds.size.height,
depth: 1,

let extent = Size {
width: bounds.size.width.into(),
height: bounds.size.height.into(),
};

#[cfg(not(feature = "vulkan-render"))]
{
let gpu = Arc::new(
unsafe {
gpu::Context::init_windowed(
&raw,
gpu::ContextDesc {
validation: false,
capture: false,
overlay: false,
},
)
}
.unwrap(),
);

Self {
renderer: BladeRenderer::new(gpu, extent),
bounds,
scale: 1.0,
input_handler: None,
fullscreen: false,
// On wayland, decorations are by default provided by the client
decoration_state: WaylandDecorationState::Client,
}
}

#[cfg(feature = "vulkan-render")]
Self {
renderer: BladeRenderer::new(gpu, extent),
renderer: VulkanRenderer::new(Box::new(raw), extent),
bounds,
scale: 1.0,
fullscreen: false,
Expand Down Expand Up @@ -189,10 +267,10 @@ impl WaylandWindowState {
let width = inner.bounds.size.width;
let height = inner.bounds.size.height;
let scale = inner.scale;
inner.renderer.update_drawable_size(size(
width as f64 * scale as f64,
height as f64 * scale as f64,
));
inner.renderer.update_drawable_size(Size {
width: (width as f64 * scale as f64).into(),
height: (height as f64 * scale as f64).into(),
});
(width, height, scale)
};

Expand Down
Loading

0 comments on commit c29295d

Please sign in to comment.