Skip to content

Commit

Permalink
[vent-ui] improve text loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Jul 9, 2024
1 parent 1a098a5 commit 3fdb01b
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 72 deletions.
2 changes: 1 addition & 1 deletion crates/vent-rendering/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl VulkanBuffer {
let buffer = VulkanBuffer::new(
instance,
size,
usage,
usage | vk::BufferUsageFlags::TRANSFER_DST,
vk::MemoryPropertyFlags::DEVICE_LOCAL,
name,
);
Expand Down
4 changes: 2 additions & 2 deletions crates/vent-rendering/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{collections::HashMap};
use std::collections::HashMap;

use ash::vk;

use crate::{SamplerInfo};
use crate::SamplerInfo;

pub struct VulkanCache {
sampler_cache: HashMap<SamplerInfo, vk::Sampler>,
Expand Down
31 changes: 30 additions & 1 deletion crates/vent-rendering/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl VulkanImage {
image_size,
1,
vk::ImageUsageFlags::TRANSFER_DST
| vk::ImageUsageFlags::TRANSFER_SRC
| vk::ImageUsageFlags::SAMPLED,
);
let memory = VulkanBuffer::new_image(&instance.device, &instance.memory_allocator, image);
Expand All @@ -64,6 +63,7 @@ impl VulkanImage {
instance.command_pool,
image_size,
1,
true,
);
staging_buffer.destroy(&instance.device);
let image_view = Self::create_image_view(
Expand Down Expand Up @@ -141,6 +141,7 @@ impl VulkanImage {
instance.command_pool,
image_size,
mip_level,
false,
);
staging_buffer.destroy(&instance.device);

Expand Down Expand Up @@ -210,6 +211,7 @@ impl VulkanImage {
command_pool: vk::CommandPool,
size: Extent2D,
mip_level: u32,
make_ready: bool,
) {
let device = &instance.device;

Expand Down Expand Up @@ -260,6 +262,33 @@ impl VulkanImage {

unsafe { device.cmd_copy_buffer_to_image2(command_buffer, &copy_image_info) };

if make_ready {
let image_barrier = vk::ImageMemoryBarrier2::default()
.old_layout(vk::ImageLayout::TRANSFER_DST_OPTIMAL)
.new_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL)
.src_access_mask(vk::AccessFlags2::TRANSFER_WRITE)
.dst_access_mask(vk::AccessFlags2::SHADER_READ)
.src_stage_mask(vk::PipelineStageFlags2::TRANSFER)
.dst_stage_mask(vk::PipelineStageFlags2::FRAGMENT_SHADER)
.src_queue_family_index(vk::QUEUE_FAMILY_IGNORED)
.image(image)
.dst_queue_family_index(vk::QUEUE_FAMILY_IGNORED)
.subresource_range(vk::ImageSubresourceRange {
aspect_mask: vk::ImageAspectFlags::COLOR,
level_count: mip_level,
layer_count: 1,
..Default::default()
});


let binding = [image_barrier];
let dep_info = vk::DependencyInfo::default()
.image_memory_barriers(&binding)
.dependency_flags(vk::DependencyFlags::BY_REGION);

unsafe { device.cmd_pipeline_barrier2(command_buffer, &dep_info) };
}

end_single_time_command(
device,
command_pool,
Expand Down
5 changes: 2 additions & 3 deletions crates/vent-rendering/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use ash::vk::{
use crate::allocator::MemoryAllocator;
use crate::cache::VulkanCache;
use crate::debug::{
self, check_validation_layer_support,
setup_debug_messenger, ENABLE_VALIDATION_LAYERS,
self, check_validation_layer_support, setup_debug_messenger, ENABLE_VALIDATION_LAYERS,
};
use crate::image::{DepthImage, VulkanImage};
use crate::surface;
Expand Down Expand Up @@ -731,7 +730,7 @@ impl VulkanInstance {

let create_info = vk::DescriptorPoolCreateInfo::default()
.pool_sizes(&pool_sizes)
.max_sets(512);
.max_sets(1028);

unsafe { device.create_descriptor_pool(&create_info, None) }.unwrap()
}
Expand Down
47 changes: 0 additions & 47 deletions crates/vent-runtime/src/render/d3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,56 +235,14 @@ impl Renderer for Renderer3D {
.height(instance.surface_resolution.height as f32)
.max_depth(1.0);

let color_clear_value = vk::ClearValue {
color: vk::ClearColorValue {
float32: [0.2, 0.9, 1.0, 1.0],
},
};

let depth_clear_value = vk::ClearValue {
depth_stencil: vk::ClearDepthStencilValue {
depth: 1.0,
stencil: 0,
},
};

let clear_values = &[color_clear_value, depth_clear_value];

unsafe {
instance
.device
.reset_command_buffer(
command_buffer,
vk::CommandBufferResetFlags::RELEASE_RESOURCES,
)
.unwrap();

let info = vk::CommandBufferBeginInfo::default();

instance
.device
.begin_command_buffer(command_buffer, &info)
.unwrap();

let info = vk::RenderPassBeginInfo::default()
.render_pass(instance.render_pass)
.framebuffer(instance.frame_buffers[image_index])
.render_area(render_area)
.clear_values(clear_values);

instance
.device
.cmd_set_scissor(command_buffer, 0, &[render_area]);
instance
.device
.cmd_set_viewport(command_buffer, 0, &[viewport]);

let subpass_info =
vk::SubpassBeginInfo::default().contents(vk::SubpassContents::INLINE);

instance
.device
.cmd_begin_render_pass2(command_buffer, &info, &subpass_info);
// self.light_renderer.render(instance, command_buffer, image_index, &self.tmp_light_mesh);

self.mesh_renderer.record_buffer(
Expand All @@ -298,11 +256,6 @@ impl Renderer for Renderer3D {
// camera.write(instance, self.pipeline_layout, command_buffer);

// END
let subpass_end_info = vk::SubpassEndInfo::default();
instance
.device
.cmd_end_render_pass2(command_buffer, &subpass_end_info);
instance.device.end_command_buffer(command_buffer).unwrap();
}
}

Expand Down
72 changes: 67 additions & 5 deletions crates/vent-runtime/src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::{Duration, Instant};

use ash::vk;
use ash::vk::{self, CommandBuffer};
use serde::{Deserialize, Serialize};
use vent_rendering::instance::VulkanInstance;
use vent_ui::renderer::GuiRenderer;
Expand Down Expand Up @@ -152,19 +152,43 @@ impl RawRuntimeRenderer {
match image {
Ok((image_index, _)) => {
let command_buffer = instance.command_buffers[image_index as usize];
unsafe {
instance
.device
.reset_command_buffer(
command_buffer,
vk::CommandBufferResetFlags::RELEASE_RESOURCES,
)
.unwrap();

let info = vk::CommandBufferBeginInfo::default();

instance
.device
.begin_command_buffer(command_buffer, &info)
.unwrap();
}
self.cmd_renderpass(instance, command_buffer, image_index as usize);

self.multi_renderer
.render(instance, image_index, command_buffer, camera);
self.gui_renderer.render_text(
instance,
command_buffer,
image_index as usize,
"Yay Text",
"Abc",
10.0,
10.0,
10.0,
50,
);

self.multi_renderer
.render(instance, image_index, command_buffer, camera);
let subpass_end_info = vk::SubpassEndInfo::default();
unsafe {
instance
.device
.cmd_end_render_pass2(command_buffer, &subpass_end_info)
};
unsafe { instance.device.end_command_buffer(command_buffer).unwrap() };
let result = instance.submit(image_index);
if let Err(vk::Result::ERROR_OUT_OF_DATE_KHR | vk::Result::SUBOPTIMAL_KHR) = result
{
Expand Down Expand Up @@ -207,6 +231,44 @@ impl RawRuntimeRenderer {
self.delta_time
}

fn cmd_renderpass(
&self,
instance: &VulkanInstance,
command_buffer: vk::CommandBuffer,
image_index: usize,
) {
let render_area = vk::Rect2D::default()
.offset(vk::Offset2D::default())
.extent(instance.surface_resolution);
let color_clear_value = vk::ClearValue {
color: vk::ClearColorValue {
float32: [0.2, 0.9, 1.0, 1.0],
},
};

let depth_clear_value = vk::ClearValue {
depth_stencil: vk::ClearDepthStencilValue {
depth: 1.0,
stencil: 0,
},
};

let clear_values = &[color_clear_value, depth_clear_value];

let info = vk::RenderPassBeginInfo::default()
.render_pass(instance.render_pass)
.framebuffer(instance.frame_buffers[image_index])
.render_area(render_area)
.clear_values(clear_values);
let subpass_info = vk::SubpassBeginInfo::default().contents(vk::SubpassContents::INLINE);

unsafe {
instance
.device
.cmd_begin_render_pass2(command_buffer, &info, &subpass_info)
};
}

fn calc_render_data(&mut self, frame_start: Instant) -> RenderData {
self.current_frames += 1;

Expand Down
10 changes: 5 additions & 5 deletions crates/vent-ui/src/font/freetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct FreeTypeLoader {
library: freetype::Library,
}

const CHARACTERS_SIZE: usize = 30;
const CHARACTERS_SIZE: usize = 128;

impl Default for FreeTypeLoader {
fn default() -> Self {
Expand All @@ -26,7 +26,7 @@ impl FreeTypeLoader {
}

// Loads an new Font
pub fn load<P>(&self, path: P, instance: &mut VulkanInstance) -> Font
pub fn load<P>(&self, path: P, descriptor_set_layout: vk::DescriptorSetLayout, instance: &mut VulkanInstance) -> Font
where
P: AsRef<OsStr>,
{
Expand All @@ -35,7 +35,7 @@ impl FreeTypeLoader {
face.set_pixel_sizes(0, 48).unwrap();
// unsafe { FT_Set_Pixel_Sizes(face, 0, 48) };
let mut characters = Vec::with_capacity(CHARACTERS_SIZE);
for char in 0..CHARACTERS_SIZE {
for char in 33..CHARACTERS_SIZE {
face.load_char(char, freetype::face::LoadFlag::RENDER)
.unwrap();
let glyph = face.glyph();
Expand All @@ -57,8 +57,8 @@ impl FreeTypeLoader {

let descriptor_sets = VulkanInstance::allocate_descriptor_sets(
&instance.device,
instance.descriptor_pool,
instance.descriptor_set_layout,
instance.descriptor_pool, // TODO: use own descriptor_pool
descriptor_set_layout,
instance.swapchain_images.len(),
);

Expand Down
10 changes: 9 additions & 1 deletion crates/vent-ui/src/font/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Font {
// Loop through each character in the text
for character in text.chars() {
let character_index = character as usize;
dbg!(character_index);

// Check if the character is within the loaded characters
if character_index < characters.len() {
Expand Down Expand Up @@ -107,7 +108,14 @@ impl Font {
}
let buffer = &self.buffer_cache[&character_index];

let render_area = vk::Rect2D::default()
.offset(vk::Offset2D::default())
.extent(instance.surface_resolution);

unsafe {
instance
.device
.cmd_set_scissor(command_buffer, 0, &[render_area]);
instance.device.cmd_bind_descriptor_sets(
command_buffer,
vk::PipelineBindPoint::GRAPHICS,
Expand Down Expand Up @@ -149,6 +157,6 @@ impl Font {
vk::BufferUsageFlags::VERTEX_BUFFER,
Some("Font Buffer"),
);
buffer_cache.insert(index, vulkan_buffer).unwrap();
buffer_cache.insert(index, vulkan_buffer);
}
}
Loading

0 comments on commit 3fdb01b

Please sign in to comment.