Implement first draft for multisampled text

This commit is contained in:
hodasemi 2023-01-21 22:09:07 +01:00
parent f5b0f76173
commit f4a8e48d67

View file

@ -162,6 +162,8 @@ pub struct GuiHandler {
current_hoverable: RwLock<Option<Arc<Hoverable>>>, current_hoverable: RwLock<Option<Arc<Hoverable>>>,
current_clickable: RwLock<Option<Arc<Clickable>>>, current_clickable: RwLock<Option<Arc<Clickable>>>,
current_selectable: RwLock<Option<Arc<Selectable>>>, current_selectable: RwLock<Option<Arc<Selectable>>>,
text_sample_count: VkSampleCountFlags,
} }
impl GuiHandler { impl GuiHandler {
@ -186,11 +188,10 @@ impl GuiHandler {
} }
}; };
// let render_pass = let text_sample_count = device.max_supported_sample_count(VK_SAMPLE_COUNT_4_BIT);
// Self::create_render_pass(device, context.format(), context.image_layout())?;
// let framebuffers = Self::create_framebuffers(device, &context.images(), &render_pass)?;
let render_targets = Self::create_render_targets(device, &context.images())?; let render_targets =
Self::create_render_targets(device, &context.images(), queue, text_sample_count)?;
let (text_objs, color_layout) = Self::init_text_objects(device, &render_targets)?; let (text_objs, color_layout) = Self::init_text_objects(device, &render_targets)?;
let rect_objs = Self::init_rectangle_objects(device, &render_targets)?; let rect_objs = Self::init_rectangle_objects(device, &render_targets)?;
@ -306,6 +307,8 @@ impl GuiHandler {
current_hoverable: RwLock::new(None), current_hoverable: RwLock::new(None),
current_selectable: RwLock::new(None), current_selectable: RwLock::new(None),
current_writeable: RwLock::new(None), current_writeable: RwLock::new(None),
text_sample_count,
})) }))
} }
@ -782,6 +785,7 @@ impl GuiHandler {
single_color_objects: &GuiSeparator, single_color_objects: &GuiSeparator,
rectangle_objects: &GuiSeparator, rectangle_objects: &GuiSeparator,
text_objects: &GuiSeparator, text_objects: &GuiSeparator,
index: usize,
) -> Result<()> { ) -> Result<()> {
if !command_buffer_state.valid.load(SeqCst) { if !command_buffer_state.valid.load(SeqCst) {
let gui_command_buffer = &command_buffer_state.command_buffer; let gui_command_buffer = &command_buffer_state.command_buffer;
@ -816,6 +820,8 @@ impl GuiHandler {
let layers = self.layers.lock().unwrap(); let layers = self.layers.lock().unwrap();
for (_, elements) in layers.iter() { for (_, elements) in layers.iter() {
render_target.begin(&mut buffer_recorder, VK_SUBPASS_CONTENTS_INLINE, index);
if !elements.is_colorables_empty() { if !elements.is_colorables_empty() {
buffer_recorder.bind_pipeline(&single_color_objects._pipeline)?; buffer_recorder.bind_pipeline(&single_color_objects._pipeline)?;
@ -860,6 +866,8 @@ impl GuiHandler {
} }
} }
render_target.next_subpass(&mut buffer_recorder, VK_SUBPASS_CONTENTS_INLINE);
if !elements.is_textables_empty() { if !elements.is_textables_empty() {
buffer_recorder.bind_pipeline(&text_objects._pipeline)?; buffer_recorder.bind_pipeline(&text_objects._pipeline)?;
@ -885,6 +893,8 @@ impl GuiHandler {
} }
} }
} }
render_target.end(&mut buffer_recorder);
} }
command_buffer_state.valid.store(true, SeqCst); command_buffer_state.valid.store(true, SeqCst);
@ -1157,14 +1167,32 @@ impl GuiHandler {
fn create_render_targets( fn create_render_targets(
device: &Arc<Device>, device: &Arc<Device>,
target_images: &TargetMode<Vec<Arc<Image>>>, target_images: &TargetMode<Vec<Arc<Image>>>,
queue: &Arc<Mutex<Queue>>,
sample_count: VkSampleCountFlags,
) -> Result<TargetMode<RwLock<RenderTarget>>> { ) -> Result<TargetMode<RwLock<RenderTarget>>> {
Ok(match target_images { Ok(match target_images {
TargetMode::Mono(images) => TargetMode::Mono(RwLock::new(Self::create_render_target( TargetMode::Mono(images) => TargetMode::Mono(RwLock::new(Self::create_render_target(
device, images, None, device,
images,
None,
queue,
sample_count,
)?)), )?)),
TargetMode::Stereo(left_images, right_images) => TargetMode::Stereo( TargetMode::Stereo(left_images, right_images) => TargetMode::Stereo(
RwLock::new(Self::create_render_target(device, left_images, None)?), RwLock::new(Self::create_render_target(
RwLock::new(Self::create_render_target(device, right_images, None)?), device,
left_images,
None,
queue,
sample_count,
)?),
RwLock::new(Self::create_render_target(
device,
right_images,
None,
queue,
sample_count,
)?),
), ),
}) })
} }
@ -1173,12 +1201,31 @@ impl GuiHandler {
device: &Arc<Device>, device: &Arc<Device>,
target_images: &Vec<Arc<Image>>, target_images: &Vec<Arc<Image>>,
old_render_target: Option<&RenderTarget>, old_render_target: Option<&RenderTarget>,
queue: &Arc<Mutex<Queue>>,
sample_count: VkSampleCountFlags,
) -> Result<RenderTarget> { ) -> Result<RenderTarget> {
let mut builder = RenderTarget::builder().add_sub_pass( let mut builder = RenderTarget::builder()
SubPass::builder(target_images[0].width(), target_images[0].height()) .add_sub_pass(
.set_prepared_targets(target_images, 0, [0.0, 0.0, 0.0, 0.0], false) SubPass::builder(target_images[0].width(), target_images[0].height())
.build(device)?, .set_prepared_targets(target_images, 0, [0.0, 0.0, 0.0, 0.0], false)
); .build(device)?,
)
.add_sub_pass(
SubPass::builder(target_images[0].width(), target_images[0].height())
.add_target_info(CustomTarget {
usage: VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT.into(),
format: target_images[0].vk_format(),
clear_on_load: false,
store_on_save: true,
attach_sampler: false,
use_as_input: false,
clear_value: ClearValue::Color([0.0, 0.0, 0.0, 0.0]),
})
.set_sample_count(sample_count)
.add_resolve_targets(target_images.clone())
.use_queue(queue.clone())
.build(device)?,
);
if let Some(render_target) = old_render_target { if let Some(render_target) = old_render_target {
builder = builder.preserve_old_render_pass(render_target); builder = builder.preserve_old_render_pass(render_target);
@ -1284,6 +1331,7 @@ impl GuiHandler {
&pipeline_layout, &pipeline_layout,
vertex_shader.clone(), vertex_shader.clone(),
fragment_shader.clone(), fragment_shader.clone(),
1,
)?, )?,
}) })
})?, })?,
@ -1334,6 +1382,7 @@ impl GuiHandler {
&pipeline_layout, &pipeline_layout,
vertex_shader.clone(), vertex_shader.clone(),
fragment_shader.clone(), fragment_shader.clone(),
0,
)?, )?,
}) })
}) })
@ -1382,6 +1431,7 @@ impl GuiHandler {
&pipeline_layout, &pipeline_layout,
vertex_shader.clone(), vertex_shader.clone(),
fragment_shader.clone(), fragment_shader.clone(),
0,
)?, )?,
}) })
}) })
@ -1395,6 +1445,7 @@ impl GuiHandler {
pipeline_layout: &Arc<PipelineLayout>, pipeline_layout: &Arc<PipelineLayout>,
vertex_shader: Arc<ShaderModule>, vertex_shader: Arc<ShaderModule>,
fragment_shader: Arc<ShaderModule>, fragment_shader: Arc<ShaderModule>,
sub_pass: u32,
) -> Result<Arc<Pipeline>> { ) -> Result<Arc<Pipeline>> {
Pipeline::new_graphics() Pipeline::new_graphics()
.set_vertex_shader(vertex_shader, binding_description, attribute_description) .set_vertex_shader(vertex_shader, binding_description, attribute_description)
@ -1403,7 +1454,7 @@ impl GuiHandler {
.default_multisample(VK_SAMPLE_COUNT_1_BIT) .default_multisample(VK_SAMPLE_COUNT_1_BIT)
.default_color_blend(vec![VkPipelineColorBlendAttachmentState::default()]) .default_color_blend(vec![VkPipelineColorBlendAttachmentState::default()])
.default_rasterization(VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE) .default_rasterization(VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE)
.build(device.clone(), pipeline_layout, render_pass, 0) .build(device.clone(), pipeline_layout, render_pass, sub_pass)
} }
#[inline] #[inline]
@ -1425,18 +1476,11 @@ impl GuiHandler {
single_color_objects, single_color_objects,
rectangle_objects, rectangle_objects,
text_objects, text_objects,
index,
)?; )?;
render_target.begin(
buffer_recorder,
VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS,
index,
);
buffer_recorder.execute_commands(&[&command_buffer_state.command_buffer]); buffer_recorder.execute_commands(&[&command_buffer_state.command_buffer]);
render_target.end(buffer_recorder);
Ok(()) Ok(())
} }
} }
@ -1542,7 +1586,13 @@ impl GuiHandler {
(TargetMode::Mono(images), TargetMode::Mono(render_target)) => { (TargetMode::Mono(images), TargetMode::Mono(render_target)) => {
let mut rt_lock = render_target.write().unwrap(); let mut rt_lock = render_target.write().unwrap();
*rt_lock = Self::create_render_target(&self.device, &images, Some(&*rt_lock))?; *rt_lock = Self::create_render_target(
&self.device,
&images,
Some(&*rt_lock),
&self.queue,
self.text_sample_count,
)?;
} }
( (
TargetMode::Stereo(left_images, right_images), TargetMode::Stereo(left_images, right_images),
@ -1550,13 +1600,23 @@ impl GuiHandler {
) => { ) => {
let mut left_rt_lock = left_render_target.write().unwrap(); let mut left_rt_lock = left_render_target.write().unwrap();
*left_rt_lock = *left_rt_lock = Self::create_render_target(
Self::create_render_target(&self.device, &left_images, Some(&*left_rt_lock))?; &self.device,
&left_images,
Some(&*left_rt_lock),
&self.queue,
self.text_sample_count,
)?;
let mut right_rt_lock = right_render_target.write().unwrap(); let mut right_rt_lock = right_render_target.write().unwrap();
*right_rt_lock = *right_rt_lock = Self::create_render_target(
Self::create_render_target(&self.device, &right_images, Some(&*right_rt_lock))?; &self.device,
&right_images,
Some(&*right_rt_lock),
&self.queue,
self.text_sample_count,
)?;
} }
_ => unreachable!("unsupported case!"), _ => unreachable!("unsupported case!"),