Skip to main content

bevy_render/render_resource/
bind_group_layout.rs

1use crate::renderer::wgpu_wrapper;
2use bevy_utils::define_atomic_id;
3use core::ops::Deref;
4
5define_atomic_id!(BindGroupLayoutId);
6
7wgpu_wrapper! {
8    #[derive(Clone, Debug)]
9    struct WgpuBindGroupLayout(wgpu::BindGroupLayout);
10}
11
12/// Bind group layouts define the interface of resources (e.g. buffers, textures, samplers)
13/// for a shader. The actual resource binding is done via a [`BindGroup`](super::BindGroup).
14///
15/// This is a lightweight thread-safe wrapper around wgpu's own [`BindGroupLayout`](wgpu::BindGroupLayout),
16/// which can be cloned as needed to workaround lifetime management issues. It may be converted
17/// from and dereferences to wgpu's [`BindGroupLayout`](wgpu::BindGroupLayout).
18///
19/// Can be created via [`RenderDevice::create_bind_group_layout`](crate::renderer::RenderDevice::create_bind_group_layout).
20#[derive(Clone, Debug)]
21pub struct BindGroupLayout {
22    id: BindGroupLayoutId,
23    value: WgpuBindGroupLayout,
24}
25
26impl PartialEq for BindGroupLayout {
27    fn eq(&self, other: &Self) -> bool {
28        self.id == other.id
29    }
30}
31
32impl Eq for BindGroupLayout {}
33
34impl core::hash::Hash for BindGroupLayout {
35    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
36        self.id.0.hash(state);
37    }
38}
39
40impl BindGroupLayout {
41    /// Returns the [`BindGroupLayoutId`] representing the unique ID of the bind group layout.
42    #[inline]
43    pub fn id(&self) -> BindGroupLayoutId {
44        self.id
45    }
46
47    #[inline]
48    pub fn value(&self) -> &wgpu::BindGroupLayout {
49        &self.value
50    }
51}
52
53impl From<wgpu::BindGroupLayout> for BindGroupLayout {
54    fn from(value: wgpu::BindGroupLayout) -> Self {
55        BindGroupLayout {
56            id: BindGroupLayoutId::new(),
57            value: WgpuBindGroupLayout::new(value),
58        }
59    }
60}
61
62impl Deref for BindGroupLayout {
63    type Target = wgpu::BindGroupLayout;
64
65    #[inline]
66    fn deref(&self) -> &Self::Target {
67        &self.value
68    }
69}