Skip to content

Commit

Permalink
remove ultraviolet and fix viewport size issue
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-ben committed Apr 30, 2024
1 parent 1ba5868 commit 1563da2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ thiserror = "1.0"
log = "0.4"
egui = { version = ">=0.26, <=0.27", default-features = false }
ash = { version = ">=0.34, <=0.37", default-features = false, features = ["debug"] }
ultraviolet = "0.9"

gpu-allocator = { version = "0.25", default-features = false, features = ["vulkan"], optional = true }
vk-mem = { version = "0.3", optional = true }
Expand Down
49 changes: 42 additions & 7 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use egui::{
ClippedPrimitive, ImageData, TextureId,
};
use mesh::*;
use ultraviolet::projection::orthographic_vk;
use vulkan::*;

use self::allocator::Allocator;
Expand Down Expand Up @@ -513,13 +512,16 @@ impl Renderer {
)
};

let screen_width = extent.width as f32;
let screen_height = extent.height as f32;

unsafe {
self.device.cmd_set_viewport(
command_buffer,
0,
&[vk::Viewport {
width: extent.width as f32,
height: extent.height as f32,
width: screen_width,
height: screen_height,
max_depth: 1.0,
..Default::default()
}],
Expand All @@ -529,9 +531,9 @@ impl Renderer {
// Ortho projection
let projection = orthographic_vk(
0.0,
extent.width as f32 / pixels_per_point,
screen_width / pixels_per_point,
0.0,
-(extent.height as f32 / pixels_per_point),
-(screen_height / pixels_per_point),
-1.0,
1.0,
);
Expand Down Expand Up @@ -579,8 +581,8 @@ impl Renderer {
y: (clip_y as i32).max(0),
},
extent: vk::Extent2D {
width: clip_w as _,
height: clip_h as _,
width: clip_w.min(screen_width) as _,
height: clip_h.min(screen_height) as _,
},
}];

Expand Down Expand Up @@ -844,3 +846,36 @@ mod mesh {
indices
}
}

/// Orthographic projection matrix for use with Vulkan.
///
/// This matrix is meant to be used when the source coordinate space is right-handed and y-up
/// (the standard computer graphics coordinate space)and the destination space is right-handed
/// and y-down, with Z (depth) clip extending from 0.0 (close) to 1.0 (far).
///
/// from: https://github.com/fu5ha/ultraviolet (to limit dependencies)
#[inline]
pub fn orthographic_vk(
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) -> [f32; 16] {
let rml = right - left;
let rpl = right + left;
let tmb = top - bottom;
let tpb = top + bottom;
let fmn = far - near;

#[rustfmt::skip]
let res = [
2.0 / rml, 0.0, 0.0, 0.0,
0.0, -2.0 / tmb, 0.0, 0.0,
0.0, 0.0, -1.0 / fmn, 0.0,
-(rpl / rml), -(tpb / tmb), -(near / fmn), 1.0
];

res
}
4 changes: 1 addition & 3 deletions src/renderer/vulkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ pub(crate) fn create_vulkan_pipeline_layout(
device: &Device,
descriptor_set_layout: vk::DescriptorSetLayout,
) -> RendererResult<vk::PipelineLayout> {
use ultraviolet::mat::Mat4;

log::debug!("Creating vulkan pipeline layout");
let push_const_range = [vk::PushConstantRange {
stage_flags: vk::ShaderStageFlags::VERTEX,
offset: 0,
size: mem::size_of::<Mat4>() as u32,
size: mem::size_of::<[f32; 16]>() as u32,
}];

let descriptor_set_layouts = [descriptor_set_layout];
Expand Down

0 comments on commit 1563da2

Please sign in to comment.