Guest User

Untitled

a guest
Jul 10th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. pub fn render_text(
  2. &mut self,
  3. instance: &VulkanInstance,
  4. command_buffer: vk::CommandBuffer,
  5. buffer_index: usize,
  6. text: &str,
  7. x: f32,
  8. y: f32,
  9. scale: f32,
  10. color: u32,
  11. ) {
  12. if let Some(font) = &mut self.font {
  13. unsafe {
  14. instance.device.cmd_bind_pipeline(
  15. command_buffer,
  16. vk::PipelineBindPoint::GRAPHICS,
  17. self.pipeline.pipeline,
  18. )
  19. };
  20. let viewport = vk::Viewport::default()
  21. .width(instance.surface_resolution.width as f32)
  22. .height(instance.surface_resolution.height as f32)
  23. .max_depth(1.0);
  24. unsafe {
  25. instance
  26. .device
  27. .cmd_set_viewport(command_buffer, 0, &[viewport]);
  28. }
  29. let p_scale = Vec2::new(2.0 / x, 2.0 / y);
  30. let translate = Vec2::new(-1.0 - x * p_scale.x, -1.0 - y * p_scale.y);
  31.  
  32. self.push_constant = PushConstant {
  33. scale: p_scale,
  34. translate,
  35. };
  36.  
  37. unsafe {
  38. instance.device.cmd_push_constants(
  39. command_buffer,
  40. self.pipeline_layout,
  41. vk::ShaderStageFlags::VERTEX,
  42. 0,
  43. any_as_u8_slice(&self.push_constant),
  44. )
  45. }
  46. font.render_text(
  47. instance,
  48. command_buffer,
  49. self.pipeline_layout,
  50. buffer_index,
  51. text,
  52. x,
  53. y,
  54. scale,
  55. color,
  56. );
  57. }
  58. }
  59.  
  60. #[allow(clippy::too_many_arguments)]
  61. pub fn render_text(
  62. &mut self,
  63. instance: &VulkanInstance,
  64. command_buffer: vk::CommandBuffer,
  65. pipeline_layout: vk::PipelineLayout,
  66. buffer_index: usize,
  67. text: &str,
  68. x: f32,
  69. y: f32,
  70. scale: f32,
  71. color: u32,
  72. ) {
  73. let mut offset_x = x;
  74. let characters = &self.characters;
  75.  
  76. // Loop through each character in the text
  77. for character in text.chars() {
  78. let character_index = character as usize;
  79.  
  80. // Check if the character is within the loaded characters
  81. if character_index < characters.len() {
  82. let character = &characters[character_index];
  83.  
  84. // Calculate vertex positions and texture coordinates based on scale
  85. let xpos = offset_x + character.bearing.x as f32 * scale;
  86. let ypos = y - (character.size.y - character.bearing.y) as f32 * scale;
  87. let width = character.size.x as f32 * scale;
  88. let height = character.size.y as f32 * scale;
  89.  
  90. // Check if buffer contains in the cache, When not create a new vertex buffer
  91. if !self.buffer_cache.contains_key(&character_index) {
  92. let color = [color as f32, color as f32, color as f32, 1.0];
  93. let vertices: [Vertex2D; 6] = [
  94. Vertex2D {
  95. position: [xpos, ypos + height],
  96. tex_coord: [0.0, 0.0],
  97. color,
  98. },
  99. Vertex2D {
  100. position: [xpos, ypos],
  101. tex_coord: [0.0, 1.0],
  102. color,
  103. },
  104. Vertex2D {
  105. position: [xpos + width, ypos],
  106. tex_coord: [1.0, 1.0],
  107. color,
  108. },
  109. Vertex2D {
  110. position: [xpos, ypos + height],
  111. tex_coord: [0.0, 0.0],
  112. color,
  113. },
  114. Vertex2D {
  115. position: [xpos + width, ypos],
  116. tex_coord: [1.0, 1.0],
  117. color,
  118. },
  119. Vertex2D {
  120. position: [xpos + width, ypos + height],
  121. tex_coord: [1.0, 0.0],
  122. color,
  123. },
  124. ];
  125. Self::create_buffer(
  126. &mut self.buffer_cache,
  127. instance,
  128. character_index,
  129. &vertices,
  130. );
  131. }
  132. let buffer = &self.buffer_cache[&character_index];
  133.  
  134. let render_area = vk::Rect2D::default()
  135. .offset(vk::Offset2D::default())
  136. .extent(instance.surface_resolution);
  137.  
  138. unsafe {
  139. instance
  140. .device
  141. .cmd_set_scissor(command_buffer, 0, &[render_area]);
  142. instance.device.cmd_bind_descriptor_sets(
  143. command_buffer,
  144. vk::PipelineBindPoint::GRAPHICS,
  145. pipeline_layout,
  146. 0,
  147. &character.descriptor_sets[buffer_index..=buffer_index],
  148. &[],
  149. );
  150. instance.device.cmd_bind_vertex_buffers2(
  151. command_buffer,
  152. 0,
  153. &[**buffer],
  154. &[0],
  155. None,
  156. None,
  157. );
  158. instance.device.cmd_draw(command_buffer, 6, 1, 0, 0)
  159. }
  160.  
  161. // Update offset for the next character
  162. offset_x += (character.advance >> 6) as f32 * scale;
  163. }
  164. }
  165. }
  166.  
  167. fn create_buffer(
  168. buffer_cache: &mut HashMap<usize, VulkanBuffer>,
  169. instance: &VulkanInstance,
  170. index: usize,
  171. vertices: &[Vertex2D],
  172. ) {
  173. let vertex_size = std::mem::size_of_val(vertices) as vk::DeviceSize;
  174.  
  175. let vulkan_buffer = VulkanBuffer::cpu_to_gpu(
  176. instance,
  177. vertices,
  178. vertex_size,
  179. vk::BufferUsageFlags::VERTEX_BUFFER,
  180. Some("Font Buffer"),
  181. );
  182. buffer_cache.insert(index, vulkan_buffer);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment