use std::sync::Arc;
use super::command::UiCmd;
use crate::{Error, Result};
static CLEAR_COMPOSE_SPV: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/ui_clear_compose.spv"));
static BLIT_RGBA_SPV: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/ui_blit_rgba.spv"));
static DRAW_RECT_SPV: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_rect.spv"));
static DRAW_RECT_OUTLINE_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_rect_outline.spv"));
static DRAW_LINE_SPV: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_line.spv"));
static DRAW_PLOT_LINE_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_plot_line.spv"));
static DRAW_GLYPHS_SPV: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_glyphs.spv"));
static CLEAR_COMPOSE_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_clear_compose_bounded.spv"));
static BLIT_RGBA_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_blit_rgba_bounded.spv"));
static DRAW_RECT_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_rect_bounded.spv"));
static DRAW_RECT_OUTLINE_BOUNDED_SPV: &[u8] = include_bytes!(concat!(
env!("OUT_DIR"),
"/ui_draw_rect_outline_bounded.spv"
));
static DRAW_LINE_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_line_bounded.spv"));
static DRAW_PLOT_LINE_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_plot_line_bounded.spv"));
static DRAW_GLYPHS_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_glyphs_bounded.spv"));
static DRAW_CANVAS_GRID_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_canvas_grid.spv"));
static DRAW_CANVAS_GRID_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_canvas_grid_bounded.spv"));
static DRAW_TRIANGLE_SPV: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_triangle.spv"));
static DRAW_TRIANGLE_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_triangle_bounded.spv"));
static MASK_ROUNDED_VIEWPORT_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_mask_rounded_viewport.spv"));
static MASK_ROUNDED_VIEWPORT_BOUNDED_SPV: &[u8] = include_bytes!(concat!(
env!("OUT_DIR"),
"/ui_mask_rounded_viewport_bounded.spv"
));
static DRAW_WAVEFORM_SPV: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_waveform.spv"));
static DRAW_WAVEFORM_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_waveform_bounded.spv"));
static DRAW_HEATMAP_SPV: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_heatmap.spv"));
static DRAW_HEATMAP_BOUNDED_SPV: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/ui_draw_heatmap_bounded.spv"));
pub(super) struct UiPipelines {
pub(super) clear_compose: UiPipeline,
pub(super) blit_rgba: UiPipeline,
pub(super) draw_rect: UiPipeline,
pub(super) draw_rect_outline: UiPipeline,
pub(super) draw_line: UiPipeline,
pub(super) draw_plot_line: UiPipeline,
pub(super) draw_glyphs: UiPipeline,
pub(super) draw_canvas_grid: UiPipeline,
pub(super) draw_triangle: UiPipeline,
pub(super) mask_rounded_viewport: UiPipeline,
pub(super) draw_waveform: UiPipeline,
pub(super) draw_heatmap: UiPipeline,
pub(super) bounded: Option<UiLayouts>,
}
impl UiPipelines {
pub(super) fn new(
device: &Arc<vk::Device>,
layout: vk::PipelineLayout,
bounded: bool,
cache: vk::PipelineCache,
) -> Result<Self> {
let layouts = if bounded {
Some(UiLayouts::new(device)?)
} else {
None
};
let select = |count: usize| {
layouts
.as_ref()
.map_or(layout, |layouts| layouts.pipelines[count])
};
Ok(Self {
clear_compose: UiPipeline::new(
device,
select(0),
if bounded {
CLEAR_COMPOSE_BOUNDED_SPV
} else {
CLEAR_COMPOSE_SPV
},
cache,
)?,
blit_rgba: UiPipeline::new(
device,
select(1),
if bounded {
BLIT_RGBA_BOUNDED_SPV
} else {
BLIT_RGBA_SPV
},
cache,
)?,
draw_rect: UiPipeline::new(
device,
select(0),
if bounded {
DRAW_RECT_BOUNDED_SPV
} else {
DRAW_RECT_SPV
},
cache,
)?,
draw_rect_outline: UiPipeline::new(
device,
select(0),
if bounded {
DRAW_RECT_OUTLINE_BOUNDED_SPV
} else {
DRAW_RECT_OUTLINE_SPV
},
cache,
)?,
draw_line: UiPipeline::new(
device,
select(0),
if bounded {
DRAW_LINE_BOUNDED_SPV
} else {
DRAW_LINE_SPV
},
cache,
)?,
draw_plot_line: UiPipeline::new(
device,
select(1),
if bounded {
DRAW_PLOT_LINE_BOUNDED_SPV
} else {
DRAW_PLOT_LINE_SPV
},
cache,
)?,
draw_glyphs: UiPipeline::new(
device,
select(2),
if bounded {
DRAW_GLYPHS_BOUNDED_SPV
} else {
DRAW_GLYPHS_SPV
},
cache,
)?,
draw_canvas_grid: UiPipeline::new(
device,
select(0),
if bounded {
DRAW_CANVAS_GRID_BOUNDED_SPV
} else {
DRAW_CANVAS_GRID_SPV
},
cache,
)?,
draw_triangle: UiPipeline::new(
device,
select(0),
if bounded {
DRAW_TRIANGLE_BOUNDED_SPV
} else {
DRAW_TRIANGLE_SPV
},
cache,
)?,
mask_rounded_viewport: UiPipeline::new(
device,
select(0),
if bounded {
MASK_ROUNDED_VIEWPORT_BOUNDED_SPV
} else {
MASK_ROUNDED_VIEWPORT_SPV
},
cache,
)?,
draw_waveform: UiPipeline::new(
device,
select(1),
if bounded {
DRAW_WAVEFORM_BOUNDED_SPV
} else {
DRAW_WAVEFORM_SPV
},
cache,
)?,
draw_heatmap: UiPipeline::new(
device,
select(1),
if bounded {
DRAW_HEATMAP_BOUNDED_SPV
} else {
DRAW_HEATMAP_SPV
},
cache,
)?,
bounded: layouts,
})
}
}
pub(super) struct UiPipeline {
handle: vk::Pipeline,
layout: vk::PipelineLayout,
device: Arc<vk::Device>,
}
impl UiPipeline {
fn new(
device: &Arc<vk::Device>,
layout: vk::PipelineLayout,
spv: &[u8],
cache: vk::PipelineCache,
) -> Result<Self> {
let words = decode_spirv(spv)?;
let module = create_shader_module(device, &words)?;
let entry = c"main";
let stage = vk::PipelineShaderStageCreateInfo::default()
.stage(vk::ShaderStageFlagBits::COMPUTE_BIT)
.module(module)
.name(entry);
let create_info = vk::ComputePipelineCreateInfo::default()
.stage(stage)
.layout(layout);
let mut pipelines = [vk::Pipeline::default(); 1];
let result = unsafe {
device.create_compute_pipelines(
cache,
std::slice::from_ref(&create_info),
None,
&mut pipelines,
)
};
let _ = unsafe { device.destroy_shader_module(module, None) };
let handle = match result {
Ok(vk::Result::SUCCESS) => pipelines[0],
result => {
for pipeline in pipelines
.into_iter()
.filter(|handle| *handle != vk::Pipeline::default())
{
let _ = unsafe { device.destroy_pipeline(pipeline, None) };
}
return Err(Error::backend_dispatch_failure(
"Vulkan",
"UI compute pipeline creation",
result,
));
}
};
Ok(Self {
handle,
layout,
device: device.clone(),
})
}
pub(super) const fn layout(&self) -> vk::PipelineLayout {
self.layout
}
pub(super) const fn raw(&self) -> vk::Pipeline {
self.handle
}
}
impl Drop for UiPipeline {
fn drop(&mut self) {
let _ = unsafe { self.device.destroy_pipeline(self.handle, None) };
}
}
fn decode_spirv(bytes: &[u8]) -> Result<Vec<u32>> {
if !bytes.len().is_multiple_of(4) {
return Err(Error::backend_failure(
"SPIR-V",
"UI shader byte alignment",
std::io::Error::other("SPIR-V byte count is not a multiple of 4"),
));
}
Ok(
bytes
.as_chunks::<4>()
.0
.iter()
.map(|chunk| u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
.collect(),
)
}
fn create_shader_module(device: &vk::Device, words: &[u32]) -> Result<vk::ShaderModule> {
let info = vk::ShaderModuleCreateInfo::default().code(words);
unsafe { device.create_shader_module(&info, None) }
.map_err(|source| Error::backend_failure("Vulkan", "UI shader module creation", source))
}
pub(super) struct UiLayouts {
device: Arc<vk::Device>,
descriptors: Vec<vk::DescriptorSetLayout>,
pipelines: Vec<vk::PipelineLayout>,
}
impl UiLayouts {
fn new(device: &Arc<vk::Device>) -> Result<Self> {
let mut result = Self {
device: device.clone(),
descriptors: Vec::new(),
pipelines: Vec::new(),
};
for count in 0..=2 {
let mut bindings = vec![
vk::DescriptorSetLayoutBinding::default()
.binding(1)
.descriptor_type(vk::DescriptorType::STORAGE_IMAGE)
.descriptor_count(1)
.stage_flags(vk::ShaderStageFlagBits::COMPUTE_BIT),
];
if count != 0 {
bindings.push(
vk::DescriptorSetLayoutBinding::default()
.binding(0)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER)
.descriptor_count(count)
.stage_flags(vk::ShaderStageFlagBits::COMPUTE_BIT),
);
}
let descriptor = unsafe {
device.create_descriptor_set_layout(
&(vk::DescriptorSetLayoutCreateInfo::default()
.bindings(&bindings)
.map_err(Error::from)?),
None,
)
}
.map_err(|e| Error::backend_failure("Vulkan", "bounded UI descriptor layout", e))?;
result.descriptors.push(descriptor);
let descriptors = [descriptor];
let push = [vk::PushConstantRange::default()
.size(128)
.stage_flags(vk::ShaderStageFlagBits::COMPUTE_BIT)];
let pipeline = unsafe {
device.create_pipeline_layout(
&vk::PipelineLayoutCreateInfo::default()
.set_layouts(&descriptors)
.map_err(Error::from)?
.push_constant_ranges(&push)
.map_err(Error::from)?,
None,
)
}
.map_err(|e| Error::backend_failure("Vulkan", "bounded UI pipeline layout", e))?;
result.pipelines.push(pipeline);
}
Ok(result)
}
}
impl Drop for UiLayouts {
fn drop(&mut self) {
unsafe {
for layout in &self.pipelines {
let _ = self.device.destroy_pipeline_layout(*layout, None);
}
for layout in &self.descriptors {
let _ = self.device.destroy_descriptor_set_layout(*layout, None);
}
}
}
}
pub(super) struct UiFrameBindings {
device: Arc<vk::Device>,
_engine: crate::runtime::EngineHandle,
pool: vk::DescriptorPool,
pub(super) sets: Vec<vk::DescriptorSet>,
pub(super) commands: Vec<UiCmd>,
pub(super) layouts: Vec<vk::PipelineLayout>,
}
impl UiFrameBindings {
pub(super) fn new(
engine: &crate::runtime::EngineHandle,
layouts: &UiLayouts,
image: vk::ImageView,
commands: &[UiCmd],
resolve: impl Fn(u32) -> Result<vk::DescriptorBufferInfo>,
) -> Result<Self> {
let device = engine.raw_device();
let mut buffers = Vec::with_capacity(commands.len());
let mut remapped = commands.to_vec();
for command in &mut remapped {
let indices = match command {
UiCmd::BlitRgba { src_idx, .. } => {
let index = *src_idx;
*src_idx = 0;
vec![index]
}
UiCmd::PlotLine { values_idx, .. }
| UiCmd::Waveform { values_idx, .. }
| UiCmd::Heatmap { values_idx, .. } => {
let index = *values_idx;
*values_idx = 0;
vec![index]
}
UiCmd::Glyphs {
glyph_idx,
atlas_idx,
..
} => {
let indices = vec![*glyph_idx, *atlas_idx];
*glyph_idx = 0;
*atlas_idx = 1;
indices
}
_ => Vec::new(),
};
buffers.push(
indices
.into_iter()
.map(&resolve)
.collect::<Result<Vec<_>>>()?,
);
}
let count = u32::try_from(commands.len())
.map_err(|_| Error::resource_exhausted("too many UI commands"))?;
let buffer_count = buffers.iter().try_fold(0_u32, |total, buffer| {
total
.checked_add(buffer.len() as u32)
.ok_or_else(|| Error::resource_exhausted("UI descriptor count overflow"))
})?;
let mut sizes = vec![
vk::DescriptorPoolSize::default()
.r#type(vk::DescriptorType::STORAGE_IMAGE)
.descriptor_count(count.max(1)),
];
if buffer_count != 0 {
sizes.push(
vk::DescriptorPoolSize::default()
.r#type(vk::DescriptorType::STORAGE_BUFFER)
.descriptor_count(buffer_count),
);
}
let pool = unsafe {
device.create_descriptor_pool(
&(vk::DescriptorPoolCreateInfo::default())
.max_sets(count.max(1))
.pool_sizes(&sizes)
.map_err(Error::from)?,
None,
)
}
.map_err(|e| Error::backend_failure("Vulkan", "bounded UI descriptor pool", e))?;
let mut result = Self {
device: device.clone(),
_engine: engine.clone(),
pool,
sets: Vec::new(),
commands: remapped,
layouts: buffers.iter().map(|b| layouts.pipelines[b.len()]).collect(),
};
if count == 0 {
return Ok(result);
}
let descriptors: Vec<_> = buffers
.iter()
.map(|b| layouts.descriptors[b.len()])
.collect();
let mut sets = vec![vk::DescriptorSet::default(); descriptors.len()];
unsafe {
device.allocate_descriptor_sets(
&vk::DescriptorSetAllocateInfo::default()
.descriptor_pool(pool)
.set_layouts(&descriptors)
.map_err(Error::from)?,
&mut sets,
)
}
.map_err(|e| Error::backend_failure("Vulkan", "bounded UI descriptor allocation", e))?;
result.sets = sets;
let images = [vk::DescriptorImageInfo::default()
.image_view(image)
.image_layout(vk::ImageLayout::GENERAL)];
for (&set, buffers) in result.sets.iter().zip(&buffers) {
let mut writes = vec![
vk::WriteDescriptorSet::default()
.dst_set(set)
.dst_binding(1)
.descriptor_type(vk::DescriptorType::STORAGE_IMAGE)
.image_info(&images)
.map_err(Error::from)?,
];
if !buffers.is_empty() {
writes.push(
vk::WriteDescriptorSet::default()
.dst_set(set)
.dst_binding(0)
.descriptor_type(vk::DescriptorType::STORAGE_BUFFER)
.buffer_info(buffers)
.map_err(Error::from)?,
);
}
unsafe {
device.update_descriptor_sets(&writes, &[])?;
}
}
Ok(result)
}
}
impl Drop for UiFrameBindings {
fn drop(&mut self) {
let _ = unsafe { self.device.destroy_descriptor_pool(self.pool, None) };
}
}