bevy_render/render_resource/
texture.rsuse crate::define_atomic_id;
use crate::renderer::WgpuWrapper;
use alloc::sync::Arc;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::system::Resource;
use core::ops::Deref;
define_atomic_id!(TextureId);
#[derive(Clone, Debug)]
pub struct Texture {
id: TextureId,
value: Arc<WgpuWrapper<wgpu::Texture>>,
}
impl Texture {
#[inline]
pub fn id(&self) -> TextureId {
self.id
}
pub fn create_view(&self, desc: &wgpu::TextureViewDescriptor) -> TextureView {
TextureView::from(self.value.create_view(desc))
}
}
impl From<wgpu::Texture> for Texture {
fn from(value: wgpu::Texture) -> Self {
Texture {
id: TextureId::new(),
value: Arc::new(WgpuWrapper::new(value)),
}
}
}
impl Deref for Texture {
type Target = wgpu::Texture;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
define_atomic_id!(TextureViewId);
#[derive(Clone, Debug)]
pub struct TextureView {
id: TextureViewId,
value: Arc<WgpuWrapper<wgpu::TextureView>>,
}
pub struct SurfaceTexture {
value: Arc<WgpuWrapper<wgpu::SurfaceTexture>>,
}
impl SurfaceTexture {
pub fn try_unwrap(self) -> Option<wgpu::SurfaceTexture> {
Arc::try_unwrap(self.value)
.map(WgpuWrapper::into_inner)
.ok()
}
}
impl TextureView {
#[inline]
pub fn id(&self) -> TextureViewId {
self.id
}
}
impl From<wgpu::TextureView> for TextureView {
fn from(value: wgpu::TextureView) -> Self {
TextureView {
id: TextureViewId::new(),
value: Arc::new(WgpuWrapper::new(value)),
}
}
}
impl From<wgpu::SurfaceTexture> for SurfaceTexture {
fn from(value: wgpu::SurfaceTexture) -> Self {
SurfaceTexture {
value: Arc::new(WgpuWrapper::new(value)),
}
}
}
impl Deref for TextureView {
type Target = wgpu::TextureView;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl Deref for SurfaceTexture {
type Target = wgpu::SurfaceTexture;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
define_atomic_id!(SamplerId);
#[derive(Clone, Debug)]
pub struct Sampler {
id: SamplerId,
value: Arc<WgpuWrapper<wgpu::Sampler>>,
}
impl Sampler {
#[inline]
pub fn id(&self) -> SamplerId {
self.id
}
}
impl From<wgpu::Sampler> for Sampler {
fn from(value: wgpu::Sampler) -> Self {
Sampler {
id: SamplerId::new(),
value: Arc::new(WgpuWrapper::new(value)),
}
}
}
impl Deref for Sampler {
type Target = wgpu::Sampler;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
#[derive(Resource, Debug, Clone, Deref, DerefMut)]
pub struct DefaultImageSampler(pub(crate) Sampler);