|
|
@ -15,10 +15,16 @@ const CACHE_HEIGHT: usize = 1000;
|
|
|
|
|
|
|
|
|
|
|
|
/// Typed wrapper for a u32 shader handle (index id)
|
|
|
|
/// Typed wrapper for a u32 shader handle (index id)
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
|
|
|
pub struct CanvasShaderHandle {
|
|
|
|
pub struct CanvasTextHandle {
|
|
|
|
pub handle: u32
|
|
|
|
pub handle: u32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
|
|
|
|
|
|
|
pub struct CanvasTextCacheHandle {
|
|
|
|
|
|
|
|
pub handle: u32
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// So currently, I'm using these as container classes which vkprocessor owns
|
|
|
|
/// So currently, I'm using these as container classes which vkprocessor owns
|
|
|
|
/// I then use a CanvasFrame which accumulates lists of handles and vertices.
|
|
|
|
/// I then use a CanvasFrame which accumulates lists of handles and vertices.
|
|
|
|
pub struct CanvasText {
|
|
|
|
pub struct CanvasText {
|
|
|
@ -27,27 +33,27 @@ pub struct CanvasText {
|
|
|
|
font: Font<'static>,
|
|
|
|
font: Font<'static>,
|
|
|
|
cache: Cache<'static>,
|
|
|
|
cache: Cache<'static>,
|
|
|
|
cache_pixel_buffer: Vec<u8>,
|
|
|
|
cache_pixel_buffer: Vec<u8>,
|
|
|
|
texts: Vec<TextData>,
|
|
|
|
texts: Vec<u8>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl CanvasText {
|
|
|
|
impl CanvasText {
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new() -> CanvasText {
|
|
|
|
pub fn new(device: Arc<Device>, queue: Arc<Queue>) -> CanvasText {
|
|
|
|
|
|
|
|
|
|
|
|
let cache = Cache::builder().dimensions(CACHE_WIDTH as u32, CACHE_HEIGHT as u32).build();
|
|
|
|
let cache = Cache::builder().dimensions(CACHE_WIDTH as u32, CACHE_HEIGHT as u32).build();
|
|
|
|
let cache_pixel_buffer = vec!(0; CACHE_WIDTH * CACHE_HEIGHT);
|
|
|
|
let cache_pixel_buffer = vec!(0; CACHE_WIDTH * CACHE_HEIGHT);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// let font_data = include_bytes!("DejaVuSans.ttf");
|
|
|
|
let font_data = include_bytes!("../resources/fonts/sansation.ttf");
|
|
|
|
// let font = Font::from_bytes(font_data as &[u8]).unwrap();
|
|
|
|
let font = Font::from_bytes(font_data as &[u8]).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
CanvasText {
|
|
|
|
CanvasText {
|
|
|
|
device: (),
|
|
|
|
device: device.clone(),
|
|
|
|
queue: (),
|
|
|
|
queue: queue.clone(),
|
|
|
|
font: (),
|
|
|
|
font: font,
|
|
|
|
cache: (),
|
|
|
|
cache: cache,
|
|
|
|
cache_pixel_buffer: (),
|
|
|
|
cache_pixel_buffer: vec![],
|
|
|
|
texts: ()
|
|
|
|
texts: vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -60,132 +66,132 @@ impl CanvasText {
|
|
|
|
for glyph in &glyphs {
|
|
|
|
for glyph in &glyphs {
|
|
|
|
self.cache.queue_glyph(0, glyph.clone());
|
|
|
|
self.cache.queue_glyph(0, glyph.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.texts.push(TextData {
|
|
|
|
// self.texts.push(TextData {
|
|
|
|
glyphs: glyphs.clone(),
|
|
|
|
// glyphs: glyphs.clone(),
|
|
|
|
color: color,
|
|
|
|
// color: color,
|
|
|
|
});
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn draw_text(&mut self,
|
|
|
|
pub fn draw_text(&mut self,
|
|
|
|
command_buffer: AutoCommandBufferBuilder,
|
|
|
|
command_buffer: AutoCommandBufferBuilder,
|
|
|
|
image_num: usize
|
|
|
|
image_num: usize
|
|
|
|
) -> AutoCommandBufferBuilder {
|
|
|
|
) -> AutoCommandBufferBuilder {
|
|
|
|
|
|
|
|
|
|
|
|
let screen_width = 0;
|
|
|
|
|
|
|
|
let screen_height = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let cache_pixel_buffer = &mut self.cache_pixel_buffer;
|
|
|
|
|
|
|
|
let cache = &mut self.cache;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// update texture cache
|
|
|
|
|
|
|
|
cache.cache_queued(
|
|
|
|
|
|
|
|
|rect, src_data| {
|
|
|
|
|
|
|
|
let width = (rect.max.x - rect.min.x) as usize;
|
|
|
|
|
|
|
|
let height = (rect.max.y - rect.min.y) as usize;
|
|
|
|
|
|
|
|
let mut dst_index = rect.min.y as usize * CACHE_WIDTH + rect.min.x as usize;
|
|
|
|
|
|
|
|
let mut src_index = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _ in 0..height {
|
|
|
|
|
|
|
|
let dst_slice = &mut cache_pixel_buffer[dst_index..dst_index+width];
|
|
|
|
|
|
|
|
let src_slice = &src_data[src_index..src_index+width];
|
|
|
|
|
|
|
|
dst_slice.copy_from_slice(src_slice);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dst_index += CACHE_WIDTH;
|
|
|
|
|
|
|
|
src_index += width;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// need to get a hold of the cache buffer handle after I create it
|
|
|
|
|
|
|
|
// will then get swapped into this texture buffer
|
|
|
|
|
|
|
|
// Hmmm so this uninit call returns the texture and then a handle for whatever fills it up
|
|
|
|
|
|
|
|
let (cache_texture, cache_texture_write) = ImmutableImage::uninitialized(
|
|
|
|
|
|
|
|
self.device.clone(),
|
|
|
|
|
|
|
|
Dimensions::Dim2d { width: CACHE_WIDTH as u32, height: CACHE_HEIGHT as u32 },
|
|
|
|
|
|
|
|
R8Unorm,
|
|
|
|
|
|
|
|
1,
|
|
|
|
|
|
|
|
ImageUsage {
|
|
|
|
|
|
|
|
sampled: true,
|
|
|
|
|
|
|
|
transfer_destination: true,
|
|
|
|
|
|
|
|
.. ImageUsage::none()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
ImageLayout::General,
|
|
|
|
|
|
|
|
Some(self.queue.family())
|
|
|
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let set = Arc::new(
|
|
|
|
|
|
|
|
PersistentDescriptorSet::start(self.pipeline.clone(), 0)
|
|
|
|
|
|
|
|
.add_sampled_image(cache_texture.clone(), sampler).unwrap()
|
|
|
|
|
|
|
|
.build().unwrap()
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut command_buffer = command_buffer
|
|
|
|
|
|
|
|
.copy_buffer_to_image(
|
|
|
|
|
|
|
|
buffer.clone(),
|
|
|
|
|
|
|
|
cache_texture_write,
|
|
|
|
|
|
|
|
).unwrap()
|
|
|
|
|
|
|
|
.begin_render_pass(self.framebuffers[image_num].clone(), false, vec!(ClearValue::None)).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// draw
|
|
|
|
|
|
|
|
for text in &mut self.texts.drain(..) {
|
|
|
|
|
|
|
|
let vertices: Vec<Vertex2D> = text.glyphs.iter().flat_map(|g| {
|
|
|
|
|
|
|
|
if let Ok(Some((uv_rect, screen_rect))) = cache.rect_for(0, g) {
|
|
|
|
|
|
|
|
let gl_rect = Rect {
|
|
|
|
|
|
|
|
min: point(
|
|
|
|
|
|
|
|
(screen_rect.min.x as f32 / screen_width as f32 - 0.5) * 2.0,
|
|
|
|
|
|
|
|
(screen_rect.min.y as f32 / screen_height as f32 - 0.5) * 2.0
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
max: point(
|
|
|
|
|
|
|
|
(screen_rect.max.x as f32 / screen_width as f32 - 0.5) * 2.0,
|
|
|
|
|
|
|
|
(screen_rect.max.y as f32 / screen_height as f32 - 0.5) * 2.0
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
vec!(
|
|
|
|
|
|
|
|
// Vertex {
|
|
|
|
|
|
|
|
// position: [gl_rect.min.x, gl_rect.max.y],
|
|
|
|
|
|
|
|
// tex_position: [uv_rect.min.x, uv_rect.max.y],
|
|
|
|
|
|
|
|
// color: text.color,
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// Vertex {
|
|
|
|
|
|
|
|
// position: [gl_rect.min.x, gl_rect.min.y],
|
|
|
|
|
|
|
|
// tex_position: [uv_rect.min.x, uv_rect.min.y],
|
|
|
|
|
|
|
|
// color: text.color,
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
// Vertex {
|
|
|
|
|
|
|
|
// position: [gl_rect.max.x, gl_rect.min.y],
|
|
|
|
|
|
|
|
// tex_position: [uv_rect.max.x, uv_rect.min.y],
|
|
|
|
|
|
|
|
// color: text.color,
|
|
|
|
|
|
|
|
// },
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Vertex {
|
|
|
|
// let screen_width = 0;
|
|
|
|
// position: [gl_rect.max.x, gl_rect.min.y],
|
|
|
|
// let screen_height = 0;
|
|
|
|
// tex_position: [uv_rect.max.x, uv_rect.min.y],
|
|
|
|
//
|
|
|
|
// color: text.color,
|
|
|
|
// let cache_pixel_buffer = &mut self.cache_pixel_buffer;
|
|
|
|
// },
|
|
|
|
// let cache = &mut self.cache;
|
|
|
|
// Vertex {
|
|
|
|
//
|
|
|
|
// position: [gl_rect.max.x, gl_rect.max.y],
|
|
|
|
// // update texture cache
|
|
|
|
// tex_position: [uv_rect.max.x, uv_rect.max.y],
|
|
|
|
// cache.cache_queued(
|
|
|
|
// color: text.color,
|
|
|
|
// |rect, src_data| {
|
|
|
|
// },
|
|
|
|
// let width = (rect.max.x - rect.min.x) as usize;
|
|
|
|
// Vertex {
|
|
|
|
// let height = (rect.max.y - rect.min.y) as usize;
|
|
|
|
// position: [gl_rect.min.x, gl_rect.max.y],
|
|
|
|
// let mut dst_index = rect.min.y as usize * CACHE_WIDTH + rect.min.x as usize;
|
|
|
|
// tex_position: [uv_rect.min.x, uv_rect.max.y],
|
|
|
|
// let mut src_index = 0;
|
|
|
|
// color: text.color,
|
|
|
|
//
|
|
|
|
|
|
|
|
// for _ in 0..height {
|
|
|
|
|
|
|
|
// let dst_slice = &mut cache_pixel_buffer[dst_index..dst_index+width];
|
|
|
|
|
|
|
|
// let src_slice = &src_data[src_index..src_index+width];
|
|
|
|
|
|
|
|
// dst_slice.copy_from_slice(src_slice);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// dst_index += CACHE_WIDTH;
|
|
|
|
|
|
|
|
// src_index += width;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ).unwrap();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // need to get a hold of the cache buffer handle after I create it
|
|
|
|
|
|
|
|
// // will then get swapped into this texture buffer
|
|
|
|
|
|
|
|
// // Hmmm so this uninit call returns the texture and then a handle for whatever fills it up
|
|
|
|
|
|
|
|
// let (cache_texture, cache_texture_write) = ImmutableImage::uninitialized(
|
|
|
|
|
|
|
|
// self.device.clone(),
|
|
|
|
|
|
|
|
// Dimensions::Dim2d { width: CACHE_WIDTH as u32, height: CACHE_HEIGHT as u32 },
|
|
|
|
|
|
|
|
// R8Unorm,
|
|
|
|
|
|
|
|
// 1,
|
|
|
|
|
|
|
|
// ImageUsage {
|
|
|
|
|
|
|
|
// sampled: true,
|
|
|
|
|
|
|
|
// transfer_destination: true,
|
|
|
|
|
|
|
|
// .. ImageUsage::none()
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
).into_iter()
|
|
|
|
// ImageLayout::General,
|
|
|
|
}
|
|
|
|
// Some(self.queue.family())
|
|
|
|
else {
|
|
|
|
// ).unwrap();
|
|
|
|
vec!().into_iter()
|
|
|
|
//
|
|
|
|
}
|
|
|
|
//
|
|
|
|
}).collect();
|
|
|
|
//
|
|
|
|
|
|
|
|
// let set = Arc::new(
|
|
|
|
let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), vertices.into_iter()).unwrap();
|
|
|
|
// PersistentDescriptorSet::start(self.pipeline.clone(), 0)
|
|
|
|
command_buffer = command_buffer.draw(self.pipeline.clone(), &DynamicState::none(), vertex_buffer.clone(), set.clone(), ()).unwrap();
|
|
|
|
// .add_sampled_image(cache_texture.clone(), sampler).unwrap()
|
|
|
|
}
|
|
|
|
// .build().unwrap()
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// let mut command_buffer = command_buffer
|
|
|
|
|
|
|
|
// .copy_buffer_to_image(
|
|
|
|
|
|
|
|
// buffer.clone(),
|
|
|
|
|
|
|
|
// cache_texture_write,
|
|
|
|
|
|
|
|
// ).unwrap()
|
|
|
|
|
|
|
|
// .begin_render_pass(self.framebuffers[image_num].clone(), false, vec!(ClearValue::None)).unwrap();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// // draw
|
|
|
|
|
|
|
|
// for text in &mut self.texts.drain(..) {
|
|
|
|
|
|
|
|
// let vertices: Vec<Vertex2D> = text.glyphs.iter().flat_map(|g| {
|
|
|
|
|
|
|
|
// if let Ok(Some((uv_rect, screen_rect))) = cache.rect_for(0, g) {
|
|
|
|
|
|
|
|
// let gl_rect = Rect {
|
|
|
|
|
|
|
|
// min: point(
|
|
|
|
|
|
|
|
// (screen_rect.min.x as f32 / screen_width as f32 - 0.5) * 2.0,
|
|
|
|
|
|
|
|
// (screen_rect.min.y as f32 / screen_height as f32 - 0.5) * 2.0
|
|
|
|
|
|
|
|
// ),
|
|
|
|
|
|
|
|
// max: point(
|
|
|
|
|
|
|
|
// (screen_rect.max.x as f32 / screen_width as f32 - 0.5) * 2.0,
|
|
|
|
|
|
|
|
// (screen_rect.max.y as f32 / screen_height as f32 - 0.5) * 2.0
|
|
|
|
|
|
|
|
// )
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// vec!(
|
|
|
|
|
|
|
|
//// Vertex {
|
|
|
|
|
|
|
|
//// position: [gl_rect.min.x, gl_rect.max.y],
|
|
|
|
|
|
|
|
//// tex_position: [uv_rect.min.x, uv_rect.max.y],
|
|
|
|
|
|
|
|
//// color: text.color,
|
|
|
|
|
|
|
|
//// },
|
|
|
|
|
|
|
|
//// Vertex {
|
|
|
|
|
|
|
|
//// position: [gl_rect.min.x, gl_rect.min.y],
|
|
|
|
|
|
|
|
//// tex_position: [uv_rect.min.x, uv_rect.min.y],
|
|
|
|
|
|
|
|
//// color: text.color,
|
|
|
|
|
|
|
|
//// },
|
|
|
|
|
|
|
|
//// Vertex {
|
|
|
|
|
|
|
|
//// position: [gl_rect.max.x, gl_rect.min.y],
|
|
|
|
|
|
|
|
//// tex_position: [uv_rect.max.x, uv_rect.min.y],
|
|
|
|
|
|
|
|
//// color: text.color,
|
|
|
|
|
|
|
|
//// },
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
//// Vertex {
|
|
|
|
|
|
|
|
//// position: [gl_rect.max.x, gl_rect.min.y],
|
|
|
|
|
|
|
|
//// tex_position: [uv_rect.max.x, uv_rect.min.y],
|
|
|
|
|
|
|
|
//// color: text.color,
|
|
|
|
|
|
|
|
//// },
|
|
|
|
|
|
|
|
//// Vertex {
|
|
|
|
|
|
|
|
//// position: [gl_rect.max.x, gl_rect.max.y],
|
|
|
|
|
|
|
|
//// tex_position: [uv_rect.max.x, uv_rect.max.y],
|
|
|
|
|
|
|
|
//// color: text.color,
|
|
|
|
|
|
|
|
//// },
|
|
|
|
|
|
|
|
//// Vertex {
|
|
|
|
|
|
|
|
//// position: [gl_rect.min.x, gl_rect.max.y],
|
|
|
|
|
|
|
|
//// tex_position: [uv_rect.min.x, uv_rect.max.y],
|
|
|
|
|
|
|
|
//// color: text.color,
|
|
|
|
|
|
|
|
//// },
|
|
|
|
|
|
|
|
// ).into_iter()
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else {
|
|
|
|
|
|
|
|
// vec!().into_iter()
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }).collect();
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), vertices.into_iter()).unwrap();
|
|
|
|
|
|
|
|
// command_buffer = command_buffer.draw(self.pipeline.clone(), &DynamicState::none(), vertex_buffer.clone(), set.clone(), ()).unwrap();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
command_buffer.end_render_pass().unwrap()
|
|
|
|
command_buffer//.end_render_pass().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|