use mln_gfx::gfx_types::AttributeDataType;
use mln_gfx::gpu::{BufferId, Gpu, GpuError, ShaderKey};
use mln_gfx::shaders::bindings::MAX_ATTRIBUTES;
use mln_gfx::shaders::program::Attribute;
use crate::draw::context::Context;
use crate::draw::owned::Held;
use crate::draw::vertex_attribute::{AttributeSource, VertexAttributeArray};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct AttributeDescriptor {
pub(crate) data_type: AttributeDataType,
pub(crate) offset: u32,
}
#[derive(Clone)]
pub(crate) struct AttributeBinding {
pub(crate) attribute: AttributeDescriptor,
pub(crate) vertex_stride: u32,
pub(crate) buffer: BufferId,
pub(crate) vertex_offset: u32,
pub(crate) held: Option<Held>,
}
impl PartialEq for AttributeBinding {
fn eq(&self, rhs: &Self) -> bool {
self.attribute == rhs.attribute
&& self.vertex_stride == rhs.vertex_stride
&& self.buffer == rhs.buffer
&& self.vertex_offset == rhs.vertex_offset
}
}
pub(crate) type AttributeBindingArray = Vec<Option<AttributeBinding>>;
#[derive(Default)]
pub(crate) struct AttributeSlots {
layout: Option<u64>,
slots: [Option<u8>; MAX_ATTRIBUTES],
}
impl AttributeSlots {
fn update(&mut self, shader: &ShaderKey, attributes: &[Attribute], array: &VertexAttributeArray) {
if self.layout == Some(array.layout()) {
return;
}
self.slots = [None; MAX_ATTRIBUTES];
for (slot, attribute) in self.slots.iter_mut().zip(attributes) {
*slot = shader
.reads(attribute)
.then(|| array.slot_of(attribute.name))
.flatten()
.and_then(|slot| u8::try_from(slot).ok());
}
self.layout = Some(array.layout());
}
fn get(&self, index: usize) -> Option<usize> {
self.slots.get(index).copied().flatten().map(usize::from)
}
}
pub(crate) struct AttributeBindingState<'a> {
pub(crate) overrides: &'a mut VertexAttributeArray,
pub(crate) slots: &'a mut AttributeSlots,
pub(crate) bindings: &'a mut AttributeBindingArray,
}
pub(crate) fn update_attribute_bindings(
gpu: &mut dyn Gpu,
context: &mut Context,
shader: &ShaderKey,
attributes: &[Attribute],
vertex_count: usize,
state: AttributeBindingState<'_>,
) -> Result<bool, GpuError> {
let AttributeBindingState { overrides, slots, bindings } = state;
bindings.reserve_exact(attributes.len().saturating_sub(bindings.len()));
slots.update(shader, attributes, overrides);
let mut changed = false;
for (index, attribute @ &Attribute { format: default, .. }) in attributes.iter().enumerate() {
let binding = if shader.reads(attribute) {
Some(binding(
gpu,
context,
default,
slots.get(index).and_then(|slot| overrides.at_mut(slot)),
vertex_count,
)?)
} else {
None
};
match bindings.get_mut(index) {
Some(bound) if *bound == binding => {}
Some(bound) => {
*bound = binding;
changed = true;
}
None => {
bindings.push(binding);
changed = true;
}
}
}
Ok(changed)
}
fn binding(
gpu: &mut dyn Gpu,
context: &mut Context,
default: AttributeDataType,
attribute: Option<&mut AttributeSource>,
vertex_count: usize,
) -> Result<AttributeBinding, GpuError> {
if let Some(binding) = attribute.map(|attribute| attribute.binding(gpu, context)).transpose()?.flatten() {
return Ok(binding);
}
let buffer = context.default_vertex_buffer(gpu, default, vertex_count)?;
Ok(AttributeBinding {
attribute: AttributeDescriptor { data_type: default, offset: 0 },
vertex_stride: default.stride() as u32,
buffer: buffer.id(),
vertex_offset: 0,
held: Some(Held::Buffer(buffer)),
})
}
pub(crate) fn needs_upload(context: &Context, overrides: &VertexAttributeArray, slots: &AttributeSlots) -> bool {
slots
.slots
.iter()
.flatten()
.any(|&slot| overrides.at(usize::from(slot)).is_some_and(|attribute| attribute.needs_upload(context)))
}