Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn render_text(
- &mut self,
- instance: &VulkanInstance,
- command_buffer: vk::CommandBuffer,
- buffer_index: usize,
- text: &str,
- x: f32,
- y: f32,
- scale: f32,
- color: u32,
- ) {
- if let Some(font) = &mut self.font {
- unsafe {
- instance.device.cmd_bind_pipeline(
- command_buffer,
- vk::PipelineBindPoint::GRAPHICS,
- self.pipeline.pipeline,
- )
- };
- let viewport = vk::Viewport::default()
- .width(instance.surface_resolution.width as f32)
- .height(instance.surface_resolution.height as f32)
- .max_depth(1.0);
- unsafe {
- instance
- .device
- .cmd_set_viewport(command_buffer, 0, &[viewport]);
- }
- let p_scale = Vec2::new(2.0 / x, 2.0 / y);
- let translate = Vec2::new(-1.0 - x * p_scale.x, -1.0 - y * p_scale.y);
- self.push_constant = PushConstant {
- scale: p_scale,
- translate,
- };
- unsafe {
- instance.device.cmd_push_constants(
- command_buffer,
- self.pipeline_layout,
- vk::ShaderStageFlags::VERTEX,
- 0,
- any_as_u8_slice(&self.push_constant),
- )
- }
- font.render_text(
- instance,
- command_buffer,
- self.pipeline_layout,
- buffer_index,
- text,
- x,
- y,
- scale,
- color,
- );
- }
- }
- #[allow(clippy::too_many_arguments)]
- pub fn render_text(
- &mut self,
- instance: &VulkanInstance,
- command_buffer: vk::CommandBuffer,
- pipeline_layout: vk::PipelineLayout,
- buffer_index: usize,
- text: &str,
- x: f32,
- y: f32,
- scale: f32,
- color: u32,
- ) {
- let mut offset_x = x;
- let characters = &self.characters;
- // Loop through each character in the text
- for character in text.chars() {
- let character_index = character as usize;
- // Check if the character is within the loaded characters
- if character_index < characters.len() {
- let character = &characters[character_index];
- // Calculate vertex positions and texture coordinates based on scale
- let xpos = offset_x + character.bearing.x as f32 * scale;
- let ypos = y - (character.size.y - character.bearing.y) as f32 * scale;
- let width = character.size.x as f32 * scale;
- let height = character.size.y as f32 * scale;
- // Check if buffer contains in the cache, When not create a new vertex buffer
- if !self.buffer_cache.contains_key(&character_index) {
- let color = [color as f32, color as f32, color as f32, 1.0];
- let vertices: [Vertex2D; 6] = [
- Vertex2D {
- position: [xpos, ypos + height],
- tex_coord: [0.0, 0.0],
- color,
- },
- Vertex2D {
- position: [xpos, ypos],
- tex_coord: [0.0, 1.0],
- color,
- },
- Vertex2D {
- position: [xpos + width, ypos],
- tex_coord: [1.0, 1.0],
- color,
- },
- Vertex2D {
- position: [xpos, ypos + height],
- tex_coord: [0.0, 0.0],
- color,
- },
- Vertex2D {
- position: [xpos + width, ypos],
- tex_coord: [1.0, 1.0],
- color,
- },
- Vertex2D {
- position: [xpos + width, ypos + height],
- tex_coord: [1.0, 0.0],
- color,
- },
- ];
- Self::create_buffer(
- &mut self.buffer_cache,
- instance,
- character_index,
- &vertices,
- );
- }
- 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,
- pipeline_layout,
- 0,
- &character.descriptor_sets[buffer_index..=buffer_index],
- &[],
- );
- instance.device.cmd_bind_vertex_buffers2(
- command_buffer,
- 0,
- &[**buffer],
- &[0],
- None,
- None,
- );
- instance.device.cmd_draw(command_buffer, 6, 1, 0, 0)
- }
- // Update offset for the next character
- offset_x += (character.advance >> 6) as f32 * scale;
- }
- }
- }
- fn create_buffer(
- buffer_cache: &mut HashMap<usize, VulkanBuffer>,
- instance: &VulkanInstance,
- index: usize,
- vertices: &[Vertex2D],
- ) {
- let vertex_size = std::mem::size_of_val(vertices) as vk::DeviceSize;
- let vulkan_buffer = VulkanBuffer::cpu_to_gpu(
- instance,
- vertices,
- vertex_size,
- vk::BufferUsageFlags::VERTEX_BUFFER,
- Some("Font Buffer"),
- );
- buffer_cache.insert(index, vulkan_buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment