Path: blob/main/crates/bevy_render/src/render_resource/bind_group_layout.rs
6596 views
use crate::{define_atomic_id, renderer::RenderDevice, renderer::WgpuWrapper};1use bevy_ecs::system::Res;2use bevy_platform::sync::OnceLock;3use core::ops::Deref;45define_atomic_id!(BindGroupLayoutId);67/// Bind group layouts define the interface of resources (e.g. buffers, textures, samplers)8/// for a shader. The actual resource binding is done via a [`BindGroup`](super::BindGroup).9///10/// This is a lightweight thread-safe wrapper around wgpu's own [`BindGroupLayout`](wgpu::BindGroupLayout),11/// which can be cloned as needed to workaround lifetime management issues. It may be converted12/// from and dereferences to wgpu's [`BindGroupLayout`](wgpu::BindGroupLayout).13///14/// Can be created via [`RenderDevice::create_bind_group_layout`](crate::renderer::RenderDevice::create_bind_group_layout).15#[derive(Clone, Debug)]16pub struct BindGroupLayout {17id: BindGroupLayoutId,18value: WgpuWrapper<wgpu::BindGroupLayout>,19}2021impl PartialEq for BindGroupLayout {22fn eq(&self, other: &Self) -> bool {23self.id == other.id24}25}2627impl Eq for BindGroupLayout {}2829impl core::hash::Hash for BindGroupLayout {30fn hash<H: core::hash::Hasher>(&self, state: &mut H) {31self.id.0.hash(state);32}33}3435impl BindGroupLayout {36/// Returns the [`BindGroupLayoutId`] representing the unique ID of the bind group layout.37#[inline]38pub fn id(&self) -> BindGroupLayoutId {39self.id40}4142#[inline]43pub fn value(&self) -> &wgpu::BindGroupLayout {44&self.value45}46}4748impl From<wgpu::BindGroupLayout> for BindGroupLayout {49fn from(value: wgpu::BindGroupLayout) -> Self {50BindGroupLayout {51id: BindGroupLayoutId::new(),52value: WgpuWrapper::new(value),53}54}55}5657impl Deref for BindGroupLayout {58type Target = wgpu::BindGroupLayout;5960#[inline]61fn deref(&self) -> &Self::Target {62&self.value63}64}6566static EMPTY_BIND_GROUP_LAYOUT: OnceLock<BindGroupLayout> = OnceLock::new();6768pub(crate) fn init_empty_bind_group_layout(render_device: Res<RenderDevice>) {69let layout = render_device.create_bind_group_layout(Some("empty_bind_group_layout"), &[]);70EMPTY_BIND_GROUP_LAYOUT71.set(layout)72.expect("init_empty_bind_group_layout was called more than once");73}7475pub fn empty_bind_group_layout() -> BindGroupLayout {76EMPTY_BIND_GROUP_LAYOUT77.get()78.expect("init_empty_bind_group_layout was not called")79.clone()80}818283