Path: blob/main/crates/bevy_render/src/render_graph/node_slot.rs
6596 views
use alloc::borrow::Cow;1use bevy_ecs::entity::Entity;2use core::fmt;3use derive_more::derive::From;45use crate::render_resource::{Buffer, Sampler, TextureView};67/// A value passed between render [`Nodes`](super::Node).8/// Corresponds to the [`SlotType`] specified in the [`RenderGraph`](super::RenderGraph).9///10/// Slots can have four different types of values:11/// [`Buffer`], [`TextureView`], [`Sampler`] and [`Entity`].12///13/// These values do not contain the actual render data, but only the ids to retrieve them.14#[derive(Debug, Clone, From)]15pub enum SlotValue {16/// A GPU-accessible [`Buffer`].17Buffer(Buffer),18/// A [`TextureView`] describes a texture used in a pipeline.19TextureView(TextureView),20/// A texture [`Sampler`] defines how a pipeline will sample from a [`TextureView`].21Sampler(Sampler),22/// An entity from the ECS.23Entity(Entity),24}2526impl SlotValue {27/// Returns the [`SlotType`] of this value.28pub fn slot_type(&self) -> SlotType {29match self {30SlotValue::Buffer(_) => SlotType::Buffer,31SlotValue::TextureView(_) => SlotType::TextureView,32SlotValue::Sampler(_) => SlotType::Sampler,33SlotValue::Entity(_) => SlotType::Entity,34}35}36}3738/// Describes the render resources created (output) or used (input) by39/// the render [`Nodes`](super::Node).40///41/// This should not be confused with [`SlotValue`], which actually contains the passed data.42#[derive(Debug, Copy, Clone, Eq, PartialEq)]43pub enum SlotType {44/// A GPU-accessible [`Buffer`].45Buffer,46/// A [`TextureView`] describes a texture used in a pipeline.47TextureView,48/// A texture [`Sampler`] defines how a pipeline will sample from a [`TextureView`].49Sampler,50/// An entity from the ECS.51Entity,52}5354impl fmt::Display for SlotType {55fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {56let s = match self {57SlotType::Buffer => "Buffer",58SlotType::TextureView => "TextureView",59SlotType::Sampler => "Sampler",60SlotType::Entity => "Entity",61};6263f.write_str(s)64}65}6667/// A [`SlotLabel`] is used to reference a slot by either its name or index68/// inside the [`RenderGraph`](super::RenderGraph).69#[derive(Debug, Clone, Eq, PartialEq, From)]70pub enum SlotLabel {71Index(usize),72Name(Cow<'static, str>),73}7475impl From<&SlotLabel> for SlotLabel {76fn from(value: &SlotLabel) -> Self {77value.clone()78}79}8081impl From<String> for SlotLabel {82fn from(value: String) -> Self {83SlotLabel::Name(value.into())84}85}8687impl From<&'static str> for SlotLabel {88fn from(value: &'static str) -> Self {89SlotLabel::Name(value.into())90}91}9293/// The internal representation of a slot, which specifies its [`SlotType`] and name.94#[derive(Clone, Debug)]95pub struct SlotInfo {96pub name: Cow<'static, str>,97pub slot_type: SlotType,98}99100impl SlotInfo {101pub fn new(name: impl Into<Cow<'static, str>>, slot_type: SlotType) -> Self {102SlotInfo {103name: name.into(),104slot_type,105}106}107}108109/// A collection of input or output [`SlotInfos`](SlotInfo) for110/// a [`NodeState`](super::NodeState).111#[derive(Default, Debug)]112pub struct SlotInfos {113slots: Vec<SlotInfo>,114}115116impl<T: IntoIterator<Item = SlotInfo>> From<T> for SlotInfos {117fn from(slots: T) -> Self {118SlotInfos {119slots: slots.into_iter().collect(),120}121}122}123124impl SlotInfos {125/// Returns the count of slots.126#[inline]127pub fn len(&self) -> usize {128self.slots.len()129}130131/// Returns true if there are no slots.132#[inline]133pub fn is_empty(&self) -> bool {134self.slots.is_empty()135}136137/// Retrieves the [`SlotInfo`] for the provided label.138pub fn get_slot(&self, label: impl Into<SlotLabel>) -> Option<&SlotInfo> {139let label = label.into();140let index = self.get_slot_index(label)?;141self.slots.get(index)142}143144/// Retrieves the [`SlotInfo`] for the provided label mutably.145pub fn get_slot_mut(&mut self, label: impl Into<SlotLabel>) -> Option<&mut SlotInfo> {146let label = label.into();147let index = self.get_slot_index(label)?;148self.slots.get_mut(index)149}150151/// Retrieves the index (inside input or output slots) of the slot for the provided label.152pub fn get_slot_index(&self, label: impl Into<SlotLabel>) -> Option<usize> {153let label = label.into();154match label {155SlotLabel::Index(index) => Some(index),156SlotLabel::Name(ref name) => self.slots.iter().position(|s| s.name == *name),157}158}159160/// Returns an iterator over the slot infos.161pub fn iter(&self) -> impl Iterator<Item = &SlotInfo> {162self.slots.iter()163}164}165166167