bevy/crates/bevy_render/src/render_resource/texture.rs
JMS55 669d139c13
Upgrade to wgpu v24 (#17542)
Didn't remove WgpuWrapper. Not sure if it's needed or not still.

## Testing

- Did you test these changes? If so, how? Example runner
- Are there any parts that need more testing? Web (portable atomics
thingy?), DXC.

## Migration Guide
- Bevy has upgraded to [wgpu
v24](https://github.com/gfx-rs/wgpu/blob/trunk/CHANGELOG.md#v2400-2025-01-15).
- When using the DirectX 12 rendering backend, the new priority system
for choosing a shader compiler is as follows:
- If the `WGPU_DX12_COMPILER` environment variable is set at runtime, it
is used
- Else if the new `statically-linked-dxc` feature is enabled, a custom
version of DXC will be statically linked into your app at compile time.
- Else Bevy will look in the app's working directory for
`dxcompiler.dll` and `dxil.dll` at runtime.
- Else if they are missing, Bevy will fall back to FXC (not recommended)

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: IceSentry <c.giguere42@gmail.com>
Co-authored-by: François Mockers <francois.mockers@vleue.com>
2025-02-09 19:40:53 +00:00

167 lines
4.2 KiB
Rust

use crate::define_atomic_id;
use crate::renderer::WgpuWrapper;
use bevy_derive::{Deref, DerefMut};
use bevy_ecs::resource::Resource;
use core::ops::Deref;
define_atomic_id!(TextureId);
/// A GPU-accessible texture.
///
/// May be converted from and dereferences to a wgpu [`Texture`](wgpu::Texture).
/// Can be created via [`RenderDevice::create_texture`](crate::renderer::RenderDevice::create_texture).
///
/// Other options for storing GPU-accessible data are:
/// * [`BufferVec`](crate::render_resource::BufferVec)
/// * [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer)
/// * [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer)
/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer)
/// * [`RawBufferVec`](crate::render_resource::RawBufferVec)
/// * [`StorageBuffer`](crate::render_resource::StorageBuffer)
/// * [`UniformBuffer`](crate::render_resource::UniformBuffer)
#[derive(Clone, Debug)]
pub struct Texture {
id: TextureId,
value: WgpuWrapper<wgpu::Texture>,
}
impl Texture {
/// Returns the [`TextureId`].
#[inline]
pub fn id(&self) -> TextureId {
self.id
}
/// Creates a view of this texture.
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: WgpuWrapper::new(value),
}
}
}
impl Deref for Texture {
type Target = wgpu::Texture;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
define_atomic_id!(TextureViewId);
/// Describes a [`Texture`] with its associated metadata required by a pipeline or [`BindGroup`](super::BindGroup).
#[derive(Clone, Debug)]
pub struct TextureView {
id: TextureViewId,
value: WgpuWrapper<wgpu::TextureView>,
}
pub struct SurfaceTexture {
value: WgpuWrapper<wgpu::SurfaceTexture>,
}
impl SurfaceTexture {
pub fn present(self) {
self.value.into_inner().present();
}
}
impl TextureView {
/// Returns the [`TextureViewId`].
#[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: WgpuWrapper::new(value),
}
}
}
impl From<wgpu::SurfaceTexture> for SurfaceTexture {
fn from(value: wgpu::SurfaceTexture) -> Self {
SurfaceTexture {
value: 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);
/// A Sampler defines how a pipeline will sample from a [`TextureView`].
/// They define image filters (including anisotropy) and address (wrapping) modes, among other things.
///
/// May be converted from and dereferences to a wgpu [`Sampler`](wgpu::Sampler).
/// Can be created via [`RenderDevice::create_sampler`](crate::renderer::RenderDevice::create_sampler).
#[derive(Clone, Debug)]
pub struct Sampler {
id: SamplerId,
value: WgpuWrapper<wgpu::Sampler>,
}
impl Sampler {
/// Returns the [`SamplerId`].
#[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: WgpuWrapper::new(value),
}
}
}
impl Deref for Sampler {
type Target = wgpu::Sampler;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
/// A rendering resource for the default image sampler which is set during renderer
/// initialization.
///
/// The [`ImagePlugin`](crate::texture::ImagePlugin) can be set during app initialization to change the default
/// image sampler.
#[derive(Resource, Debug, Clone, Deref, DerefMut)]
pub struct DefaultImageSampler(pub(crate) Sampler);