Next step towards multisampled text

This commit is contained in:
hodasemi 2023-01-21 23:34:01 +01:00
parent f4a8e48d67
commit de0a2bca70

View file

@ -193,7 +193,8 @@ impl GuiHandler {
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, text_sample_count)?;
let rect_objs = Self::init_rectangle_objects(device, &render_targets)?;
let single_color_objects = Self::init_single_color_objects(device, &render_targets)?;
@ -780,6 +781,7 @@ impl GuiHandler {
fn render(
&self,
buffer_recorder: &mut CommandBufferRecorder<'_>,
command_buffer_state: &CommandBufferState,
render_target: &RenderTarget,
single_color_objects: &GuiSeparator,
@ -787,117 +789,98 @@ impl GuiHandler {
text_objects: &GuiSeparator,
index: usize,
) -> Result<()> {
if !command_buffer_state.valid.load(SeqCst) {
let gui_command_buffer = &command_buffer_state.command_buffer;
let viewport = [VkViewport {
x: 0.0,
y: 0.0,
width: self.width.load(SeqCst) as f32,
height: self.height.load(SeqCst) as f32,
minDepth: 0.0,
maxDepth: 1.0,
}];
let inheritance_info = render_target.inheritance_info();
let scissor = [VkRect2D {
offset: VkOffset2D { x: 0, y: 0 },
extent: VkExtent2D {
width: self.width.load(SeqCst),
height: self.height.load(SeqCst),
},
}];
let mut command_buffer_begin_info = VkCommandBufferBeginInfo::new(
VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT
| VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
);
command_buffer_begin_info.set_inheritance_info(&inheritance_info);
let layers = self.layers.lock().unwrap();
let mut buffer_recorder = gui_command_buffer.begin(command_buffer_begin_info)?;
for (_, elements) in layers.iter() {
render_target.begin(buffer_recorder, VK_SUBPASS_CONTENTS_INLINE, index);
let viewport = [VkViewport {
x: 0.0,
y: 0.0,
width: self.width.load(SeqCst) as f32,
height: self.height.load(SeqCst) as f32,
minDepth: 0.0,
maxDepth: 1.0,
}];
if !elements.is_colorables_empty() {
buffer_recorder.bind_pipeline(&single_color_objects._pipeline)?;
let scissor = [VkRect2D {
offset: VkOffset2D { x: 0, y: 0 },
extent: VkExtent2D {
width: self.width.load(SeqCst),
height: self.height.load(SeqCst),
},
}];
buffer_recorder.set_scissor(&scissor);
buffer_recorder.set_viewport(&viewport);
let layers = self.layers.lock().unwrap();
// ---------- render colorables ----------
for colorable in elements.iter_colorables() {
buffer_recorder.bind_vertex_buffer(colorable.buffer());
for (_, elements) in layers.iter() {
render_target.begin(&mut buffer_recorder, VK_SUBPASS_CONTENTS_INLINE, index);
buffer_recorder.bind_descriptor_sets_minimal(&[&colorable.descriptor_set()]);
if !elements.is_colorables_empty() {
buffer_recorder.bind_pipeline(&single_color_objects._pipeline)?;
buffer_recorder.set_scissor(&scissor);
buffer_recorder.set_viewport(&viewport);
// ---------- render colorables ----------
for colorable in elements.iter_colorables() {
buffer_recorder.bind_vertex_buffer(colorable.buffer());
buffer_recorder
.bind_descriptor_sets_minimal(&[&colorable.descriptor_set()]);
buffer_recorder.draw_complete_single_instance(6);
}
buffer_recorder.draw_complete_single_instance(6);
}
if !elements.is_displayables_empty() || !elements.is_iconizables_empty() {
buffer_recorder.bind_pipeline(&rectangle_objects._pipeline)?;
buffer_recorder.set_scissor(&scissor);
buffer_recorder.set_viewport(&viewport);
// ---------- render displayables ----------
for displayable in elements.iter_displayables() {
buffer_recorder.bind_vertex_buffer(displayable.buffer());
buffer_recorder
.bind_descriptor_sets_minimal(&[&displayable.descriptor_set()]);
buffer_recorder.draw_complete_single_instance(6);
}
// ---------- render iconizables ----------
for iconizable in elements.iter_iconizables() {
buffer_recorder.bind_vertex_buffer(iconizable.buffer());
buffer_recorder
.bind_descriptor_sets_minimal(&[iconizable.descriptor_set()]);
buffer_recorder.draw_complete_single_instance(6);
}
}
render_target.next_subpass(&mut buffer_recorder, VK_SUBPASS_CONTENTS_INLINE);
if !elements.is_textables_empty() {
buffer_recorder.bind_pipeline(&text_objects._pipeline)?;
buffer_recorder.set_scissor(&scissor);
buffer_recorder.set_viewport(&viewport);
let mut text_buffers = command_buffer_state.text_buffers.write().unwrap();
text_buffers.clear();
// ---------- render textables ----------
for textable in elements.iter_textables() {
if let Some(text_buffer) = textable.buffer() {
buffer_recorder.bind_vertex_buffer(&text_buffer);
text_buffers.push(text_buffer);
buffer_recorder.bind_descriptor_sets_minimal(&[
&self.bitmap_desc_set,
&textable.descriptor_set(),
]);
buffer_recorder.draw_complete_single_instance(textable.vertex_count());
}
}
}
render_target.end(&mut buffer_recorder);
}
command_buffer_state.valid.store(true, SeqCst);
if !elements.is_displayables_empty() || !elements.is_iconizables_empty() {
buffer_recorder.bind_pipeline(&rectangle_objects._pipeline)?;
buffer_recorder.set_scissor(&scissor);
buffer_recorder.set_viewport(&viewport);
// ---------- render displayables ----------
for displayable in elements.iter_displayables() {
buffer_recorder.bind_vertex_buffer(displayable.buffer());
buffer_recorder.bind_descriptor_sets_minimal(&[&displayable.descriptor_set()]);
buffer_recorder.draw_complete_single_instance(6);
}
// ---------- render iconizables ----------
for iconizable in elements.iter_iconizables() {
buffer_recorder.bind_vertex_buffer(iconizable.buffer());
buffer_recorder.bind_descriptor_sets_minimal(&[iconizable.descriptor_set()]);
buffer_recorder.draw_complete_single_instance(6);
}
}
render_target.next_subpass(buffer_recorder, VK_SUBPASS_CONTENTS_INLINE);
if !elements.is_textables_empty() {
buffer_recorder.bind_pipeline(&text_objects._pipeline)?;
buffer_recorder.set_scissor(&scissor);
buffer_recorder.set_viewport(&viewport);
let mut text_buffers = command_buffer_state.text_buffers.write().unwrap();
text_buffers.clear();
// ---------- render textables ----------
for textable in elements.iter_textables() {
if let Some(text_buffer) = textable.buffer() {
buffer_recorder.bind_vertex_buffer(&text_buffer);
text_buffers.push(text_buffer);
buffer_recorder.bind_descriptor_sets_minimal(&[
&self.bitmap_desc_set,
&textable.descriptor_set(),
]);
buffer_recorder.draw_complete_single_instance(textable.vertex_count());
}
}
}
render_target.end(buffer_recorder);
}
Ok(())
@ -1214,7 +1197,7 @@ impl GuiHandler {
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(),
format: VK_FORMAT_R8G8B8A8_UNORM,
clear_on_load: false,
store_on_save: true,
attach_sampler: false,
@ -1222,7 +1205,15 @@ impl GuiHandler {
clear_value: ClearValue::Color([0.0, 0.0, 0.0, 0.0]),
})
.set_sample_count(sample_count)
.add_resolve_targets(target_images.clone())
.add_resolve_targets(CustomTarget {
usage: VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT.into(),
format: VK_FORMAT_R8G8B8A8_UNORM,
clear_on_load: true,
store_on_save: true,
attach_sampler: false,
use_as_input: false,
clear_value: ClearValue::Color([0.0, 0.0, 0.0, 0.0]),
})
.use_queue(queue.clone())
.build(device)?,
);
@ -1282,6 +1273,7 @@ impl GuiHandler {
fn init_text_objects(
device: &Arc<Device>,
render_targets: &TargetMode<RwLock<RenderTarget>>,
sample_count: VkSampleCountFlags,
) -> Result<(TargetMode<GuiSeparator>, Arc<DescriptorSetLayout>)> {
// --- layout creation ---
let descriptor_layout = DescriptorSetLayout::builder()
@ -1331,6 +1323,7 @@ impl GuiHandler {
&pipeline_layout,
vertex_shader.clone(),
fragment_shader.clone(),
sample_count,
1,
)?,
})
@ -1382,6 +1375,7 @@ impl GuiHandler {
&pipeline_layout,
vertex_shader.clone(),
fragment_shader.clone(),
VK_SAMPLE_COUNT_1_BIT,
0,
)?,
})
@ -1431,6 +1425,7 @@ impl GuiHandler {
&pipeline_layout,
vertex_shader.clone(),
fragment_shader.clone(),
VK_SAMPLE_COUNT_1_BIT,
0,
)?,
})
@ -1445,13 +1440,14 @@ impl GuiHandler {
pipeline_layout: &Arc<PipelineLayout>,
vertex_shader: Arc<ShaderModule>,
fragment_shader: Arc<ShaderModule>,
sample_count: VkSampleCountFlags,
sub_pass: u32,
) -> Result<Arc<Pipeline>> {
Pipeline::new_graphics()
.set_vertex_shader(vertex_shader, binding_description, attribute_description)
.set_fragment_shader(fragment_shader)
.input_assembly(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, false)
.default_multisample(VK_SAMPLE_COUNT_1_BIT)
.default_multisample(sample_count)
.default_color_blend(vec![VkPipelineColorBlendAttachmentState::default()])
.default_rasterization(VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE)
.build(device.clone(), pipeline_layout, render_pass, sub_pass)
@ -1471,6 +1467,7 @@ impl GuiHandler {
let command_buffer_state = &command_buffer_states[index];
self.render(
buffer_recorder,
command_buffer_state,
render_target,
single_color_objects,
@ -1479,8 +1476,6 @@ impl GuiHandler {
index,
)?;
buffer_recorder.execute_commands(&[&command_buffer_state.command_buffer]);
Ok(())
}
}