Skip to content

Commit

Permalink
[vent-window] Rewrite Wayland Backend
Browse files Browse the repository at this point in the history
We now support:
- Client-Side Decorations
-Resizing
-Proper Close handling
- Frame Callback
and more
  • Loading branch information
Snowiiii committed Jun 23, 2024
1 parent fb992b0 commit 0bc6152
Show file tree
Hide file tree
Showing 10 changed files with 1,017 additions and 616 deletions.
334 changes: 270 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions crates/vent-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ downcast-rs = "1.2.0"

# egui
egui = "0.27"
# egui-winit = "0.27"

[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.13"
android_logger = "0.14"
android-activity = { version = "0.6", features = [ "game-activity" ] }
ndk = "0.9.0"

Expand Down
11 changes: 10 additions & 1 deletion crates/vent-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl VentApplication {
// TODO
let mut renderer = DefaultRuntimeRenderer::new(Dimension::D3, &vent_window);

let controller = CameraController3D::new(1000.0, 10.0);
let mut controller = CameraController3D::new(1000.0, 10.0);
let mut delta_time = 0.0;

event_loop.add_window(vent_window);
Expand All @@ -63,6 +63,15 @@ impl VentApplication {
delta_time,
);
}
WindowEvent::Mouse { button, state } => {
controller.process_mouse_input(&button, &state);
}
WindowEvent::Resize {
new_width,
new_height,
} => {
renderer.resize((new_width, new_height));
}
WindowEvent::Draw => delta_time = renderer.render(window_size), // Default,
_ => {}
}
Expand Down
23 changes: 11 additions & 12 deletions crates/vent-runtime/src/render/camera/camera_controller3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl CameraController3D {
state: keyboard::KeyState,
delta_time: f32,
) -> bool {
log::info!("{}", camera.position);
dbg!(camera.position);
if state == keyboard::KeyState::Pressed {
let (sin_pitch, cos_pitch) = camera.rotation.x.sin_cos();
match key {
Expand Down Expand Up @@ -68,17 +68,16 @@ impl CameraController3D {
false
}

// pub fn process_mouse_input(
// &mut self,
// window: &winit::window::Window,
// button: &winit::event::MouseButton,
// state: &winit::event::ElementState,
// ) {
// if button == &winit::event::MouseButton::Left {
// self.mouse_left_down = state == &winit::event::ElementState::Pressed;
// window.set_cursor_visible(!self.mouse_left_down);
// }
// }
pub fn process_mouse_input(
&mut self,
button: &vent_window::mouse::Button,
state: &vent_window::mouse::ButtonState,
) {
if button == &vent_window::mouse::Button::LEFT {
self.mouse_left_down = state == &vent_window::mouse::ButtonState::Pressed;
// window.set_cursor_visible(!self.mouse_left_down); TODO
}
}

pub fn process_mouse_movement(
&self,
Expand Down
201 changes: 100 additions & 101 deletions crates/vent-runtime/src/render/d3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::mem::size_of;

use ash::vk;
use glam::{Mat4, Vec3, Vec4};
use pollster::FutureExt;
use vent_assets::Mesh3D;

use vent_ecs::world::World;
Expand Down Expand Up @@ -88,113 +89,111 @@ impl Renderer for Renderer3D {
"/res/shaders/app/3D/shader.frag.spv"
);

pollster::block_on(async {
let mut mesh = Entity3D::new(
vent_assets::Model3D::load(
instance,
vertex_shader,
fragment_shader,
pipeline_layout,
model,
)
.await,
);
for pipeline in mesh.model.pipelines.iter_mut() {
for material in pipeline.materials.iter_mut() {
let descriptor_sets = VulkanInstance::allocate_descriptor_sets(
&instance.device,
instance.descriptor_pool,
instance.descriptor_set_layout,
instance.swapchain_images.len(),
let mut mesh = Entity3D::new(
vent_assets::Model3D::load(
instance,
vertex_shader,
fragment_shader,
pipeline_layout,
model,
)
.block_on(),
);
for pipeline in mesh.model.pipelines.iter_mut() {
for material in pipeline.materials.iter_mut() {
let descriptor_sets = VulkanInstance::allocate_descriptor_sets(
&instance.device,
instance.descriptor_pool,
instance.descriptor_set_layout,
instance.swapchain_images.len(),
);

for &descriptor_set in descriptor_sets.iter() {
let material = &material.material;
let diffuse_texture = &material.diffuse_texture;

let matieral_buffer = VulkanBuffer::new_init(
instance,
&instance.memory_allocator,
size_of::<MaterialUBO>() as vk::DeviceSize,
vk::BufferUsageFlags::UNIFORM_BUFFER,
any_as_u8_slice(&MaterialUBO {
base_color: Vec4::from_array(material.base_color),
alpha_mode: material.alpha_mode as u32,
alpha_cutoff: material.alpha_cut,
}),
vk::MemoryPropertyFlags::HOST_VISIBLE
| vk::MemoryPropertyFlags::DEVICE_LOCAL,
None,
);
let light_buffer = VulkanBuffer::new_init(
instance,
&instance.memory_allocator,
size_of::<LightUBO>() as vk::DeviceSize,
vk::BufferUsageFlags::UNIFORM_BUFFER,
any_as_u8_slice(&LightUBO {
position: [2.0, 100.0, 2.0].into(),
color: [1.0, 1.0, 1.0].into(),
}),
vk::MemoryPropertyFlags::HOST_VISIBLE
| vk::MemoryPropertyFlags::DEVICE_LOCAL,
None,
);

for &descriptor_set in descriptor_sets.iter() {
let material = &material.material;
let diffuse_texture = &material.diffuse_texture;

let matieral_buffer = VulkanBuffer::new_init(
instance,
&instance.memory_allocator,
size_of::<MaterialUBO>() as vk::DeviceSize,
vk::BufferUsageFlags::UNIFORM_BUFFER,
any_as_u8_slice(&MaterialUBO {
base_color: Vec4::from_array(material.base_color),
alpha_mode: material.alpha_mode as u32,
alpha_cutoff: material.alpha_cut,
}),
vk::MemoryPropertyFlags::HOST_VISIBLE
| vk::MemoryPropertyFlags::DEVICE_LOCAL,
None,
);
let light_buffer = VulkanBuffer::new_init(
instance,
&instance.memory_allocator,
size_of::<LightUBO>() as vk::DeviceSize,
vk::BufferUsageFlags::UNIFORM_BUFFER,
any_as_u8_slice(&LightUBO {
position: [2.0, 100.0, 2.0].into(),
color: [1.0, 1.0, 1.0].into(),
}),
vk::MemoryPropertyFlags::HOST_VISIBLE
| vk::MemoryPropertyFlags::DEVICE_LOCAL,
None,
);

let image_info = vk::DescriptorImageInfo::default()
.image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)
.image_view(diffuse_texture.image_view)
.sampler(diffuse_texture.sampler);

let material_buffer_info = vk::DescriptorBufferInfo::default()
.buffer(*matieral_buffer)
.offset(0)
.range(size_of::<MaterialUBO>() as vk::DeviceSize);

let light_buffer_info = vk::DescriptorBufferInfo::default()
.buffer(*light_buffer)
.offset(0)
.range(size_of::<LightUBO>() as vk::DeviceSize);

let desc_sets = [
vk::WriteDescriptorSet {
dst_set: descriptor_set,
dst_binding: 0, // From DescriptorSetLayoutBinding
descriptor_count: 1,
descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER,
p_image_info: &image_info,
..Default::default()
},
vk::WriteDescriptorSet {
dst_set: descriptor_set,
dst_binding: 1,
descriptor_count: 1,
descriptor_type: vk::DescriptorType::UNIFORM_BUFFER,
p_buffer_info: &material_buffer_info,
..Default::default()
},
vk::WriteDescriptorSet {
dst_set: descriptor_set,
dst_binding: 2,
descriptor_count: 1,
descriptor_type: vk::DescriptorType::UNIFORM_BUFFER,
p_buffer_info: &light_buffer_info,
..Default::default()
},
];

unsafe {
instance.device.update_descriptor_sets(&desc_sets, &[]);
}

material_ubos.push(matieral_buffer);
light_ubos.push(light_buffer);
let image_info = vk::DescriptorImageInfo::default()
.image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)
.image_view(diffuse_texture.image_view)
.sampler(diffuse_texture.sampler);

let material_buffer_info = vk::DescriptorBufferInfo::default()
.buffer(*matieral_buffer)
.offset(0)
.range(size_of::<MaterialUBO>() as vk::DeviceSize);

let light_buffer_info = vk::DescriptorBufferInfo::default()
.buffer(*light_buffer)
.offset(0)
.range(size_of::<LightUBO>() as vk::DeviceSize);

let desc_sets = [
vk::WriteDescriptorSet {
dst_set: descriptor_set,
dst_binding: 0, // From DescriptorSetLayoutBinding
descriptor_count: 1,
descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER,
p_image_info: &image_info,
..Default::default()
},
vk::WriteDescriptorSet {
dst_set: descriptor_set,
dst_binding: 1,
descriptor_count: 1,
descriptor_type: vk::DescriptorType::UNIFORM_BUFFER,
p_buffer_info: &material_buffer_info,
..Default::default()
},
vk::WriteDescriptorSet {
dst_set: descriptor_set,
dst_binding: 2,
descriptor_count: 1,
descriptor_type: vk::DescriptorType::UNIFORM_BUFFER,
p_buffer_info: &light_buffer_info,
..Default::default()
},
];

unsafe {
instance.device.update_descriptor_sets(&desc_sets, &[]);
}
material.descriptor_set = Some(descriptor_sets);

material_ubos.push(matieral_buffer);
light_ubos.push(light_buffer);
}
material.descriptor_set = Some(descriptor_sets);
}
}

mesh_renderer.insert(world.create_entity(), mesh);
});
mesh_renderer.insert(world.create_entity(), mesh);

let tmp_light_mesh = create_tmp_cube(instance);
let light_renderer = LightRenderer::new(instance);
Expand Down
2 changes: 1 addition & 1 deletion crates/vent-runtime/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl DefaultRuntimeRenderer {
return;
}

log::info!("Resizing to {:?} ", new_size);
log::debug!("Resizing to {:?} ", new_size);
self.camera
.recreate_projection(new_size.0 as f32 / new_size.1 as f32);
self.runtime_renderer
Expand Down
29 changes: 12 additions & 17 deletions crates/vent-window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ targets = [
]

[features]
default = ["wayland"]
wayland = [
"wayland-client",
"wayland-backend",
"wayland-protocols",
"wayland-protocols-plasma",
"wayland-protocols-wlr",
"wayland-scanner",
"wayland-csd-frame"
]

[dependencies]
log = "0.4"
Expand All @@ -31,17 +21,22 @@ rwh_06 = { package = "raw-window-handle", version = "0.6", features = [
]}

[target.'cfg(all(unix, not(any(target_os = "redox", target_family = "wasm", target_os = "android", target_os = "ios", target_os = "macos"))))'.dependencies]
wayland-backend = { version = "0.3", features = ["client_system"], optional = true }
wayland-client = { version = "0.31", optional = true }
wayland-backend = { version = "0.3", features = ["client_system"] }
wayland-client = { version = "0.31" }
wayland-protocols = { version = "0.32", features = [
"client", "staging", "unstable",
], optional = true }
] }
wayland-protocols-plasma = { version = "0.3.1", features = [
"client",
], optional = true }
wayland-protocols-wlr = { version = "0.3.1", features = ["client"], optional = true }
wayland-scanner = {version= "0.31", optional = true }
wayland-csd-frame = { version= "0.3.0", optional = true }
] }
wayland-protocols-wlr = { version = "0.3.1", features = ["client"] }
wayland-scanner = {version= "0.31" }
wayland-csd-frame = { version= "0.3.0" }

# Decorations
sctk = { package = "smithay-client-toolkit", version = "0.19.0" }
sctk-adwaita = { git = "https://github.com/PolyMeilex/sctk-adwaita.git", rev = "82656c8256763d2b91265ee1c6e392886d2d573a", default-features = false }
# sctk-adwaita = { version = "0.9.0", default-features = false }

# For keys
xkbcommon = { version = "0.7.0", features = ["wayland"] }
Expand Down
Loading

0 comments on commit 0bc6152

Please sign in to comment.