2020-04-24 00:24:41 +00:00
|
|
|
use super::RenderGraphError;
|
2020-05-12 23:35:49 +00:00
|
|
|
use crate::{render_resource::RenderResource, shader::FieldBindType};
|
2020-04-24 00:24:41 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2020-04-24 03:53:38 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-04-24 00:24:41 +00:00
|
|
|
pub struct ResourceSlot {
|
|
|
|
pub resource: Option<RenderResource>,
|
|
|
|
pub info: ResourceSlotInfo,
|
|
|
|
}
|
|
|
|
|
2020-04-24 03:53:38 +00:00
|
|
|
#[derive(Default, Debug, Clone)]
|
2020-04-24 00:24:41 +00:00
|
|
|
pub struct ResourceSlots {
|
|
|
|
slots: Vec<ResourceSlot>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub enum SlotLabel {
|
|
|
|
Index(usize),
|
|
|
|
Name(Cow<'static, str>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&SlotLabel> for SlotLabel {
|
|
|
|
fn from(value: &SlotLabel) -> Self {
|
|
|
|
value.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for SlotLabel {
|
|
|
|
fn from(value: String) -> Self {
|
|
|
|
SlotLabel::Name(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&'static str> for SlotLabel {
|
|
|
|
fn from(value: &'static str) -> Self {
|
|
|
|
SlotLabel::Name(value.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<usize> for SlotLabel {
|
|
|
|
fn from(value: usize) -> Self {
|
|
|
|
SlotLabel::Index(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResourceSlots {
|
|
|
|
pub fn set(&mut self, label: impl Into<SlotLabel>, resource: RenderResource) {
|
|
|
|
let mut slot = self.get_slot_mut(label).unwrap();
|
|
|
|
slot.resource = Some(resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, label: impl Into<SlotLabel>) -> Option<RenderResource> {
|
|
|
|
let slot = self.get_slot(label).unwrap();
|
|
|
|
slot.resource.clone()
|
|
|
|
}
|
|
|
|
|
2020-04-25 01:55:15 +00:00
|
|
|
pub fn get_slot(&self, label: impl Into<SlotLabel>) -> Result<&ResourceSlot, RenderGraphError> {
|
2020-04-24 00:24:41 +00:00
|
|
|
let label = label.into();
|
|
|
|
let index = self.get_slot_index(&label)?;
|
|
|
|
self.slots
|
|
|
|
.get(index)
|
|
|
|
.ok_or_else(|| RenderGraphError::InvalidNodeSlot(label))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_slot_mut(
|
|
|
|
&mut self,
|
|
|
|
label: impl Into<SlotLabel>,
|
|
|
|
) -> Result<&mut ResourceSlot, RenderGraphError> {
|
|
|
|
let label = label.into();
|
|
|
|
let index = self.get_slot_index(&label)?;
|
|
|
|
self.slots
|
|
|
|
.get_mut(index)
|
|
|
|
.ok_or_else(|| RenderGraphError::InvalidNodeSlot(label))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_slot_index(&self, label: impl Into<SlotLabel>) -> Result<usize, RenderGraphError> {
|
|
|
|
let label = label.into();
|
|
|
|
match label {
|
|
|
|
SlotLabel::Index(index) => Ok(index),
|
|
|
|
SlotLabel::Name(ref name) => self
|
|
|
|
.slots
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_i, s)| s.info.name == *name)
|
|
|
|
.map(|(i, _s)| i)
|
|
|
|
.ok_or_else(|| RenderGraphError::InvalidNodeSlot(label)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &ResourceSlot> {
|
|
|
|
self.slots.iter()
|
|
|
|
}
|
|
|
|
|
2020-04-24 03:53:38 +00:00
|
|
|
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut ResourceSlot> {
|
|
|
|
self.slots.iter_mut()
|
|
|
|
}
|
|
|
|
|
2020-04-24 00:24:41 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.slots.len()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&ResourceSlotInfo> for ResourceSlot {
|
|
|
|
fn from(slot: &ResourceSlotInfo) -> Self {
|
|
|
|
ResourceSlot {
|
|
|
|
resource: None,
|
|
|
|
info: slot.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&[ResourceSlotInfo]> for ResourceSlots {
|
|
|
|
fn from(slots: &[ResourceSlotInfo]) -> Self {
|
|
|
|
ResourceSlots {
|
|
|
|
slots: slots
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.into())
|
|
|
|
.collect::<Vec<ResourceSlot>>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ResourceSlotInfo {
|
|
|
|
pub name: Cow<'static, str>,
|
2020-05-12 04:28:11 +00:00
|
|
|
pub resource_type: FieldBindType,
|
2020-04-24 00:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ResourceSlotInfo {
|
2020-05-12 04:28:11 +00:00
|
|
|
pub fn new(name: impl Into<Cow<'static, str>>, resource_type: FieldBindType) -> Self {
|
2020-04-24 00:24:41 +00:00
|
|
|
ResourceSlotInfo {
|
|
|
|
name: name.into(),
|
|
|
|
resource_type,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|