Skip to main content

pebble/graphics/pipeline/
texture_view.rs

1use crate::graphics::{pipeline::textures::check_texture_dimensions, render::Backend, types::{TextureFormat, flags::TextureUsages}};
2
3/// A GPU texture view — what a [`ColorTarget`](crate::graphics::render::targets::ColorTarget)/
4/// [`DepthTarget`](crate::graphics::render::targets::DepthTarget) or a bind group entry actually
5/// points at. Build one via a texture's `get_view()`, or [`RenderTargetTextureBuilder`] for a
6/// standalone render target.
7#[derive(Clone)]
8pub struct TextureView {
9    view: wgpu::TextureView,
10    _texture: wgpu::Texture,
11}
12
13impl TextureView {
14    pub(crate) fn raw(&self) -> &wgpu::TextureView {
15        &self.view
16    }
17
18    pub(crate) fn from_raw(view: wgpu::TextureView, texture: wgpu::Texture) -> Self {
19        Self { view, _texture: texture }
20    }
21}
22
23/// Builds a standalone [`TextureView`] for use as a render target — e.g. a
24/// post-processing buffer or shadow map, not backed by any [`Texture`](super::textures::Texture) asset.
25pub struct RenderTargetTextureBuilder<'a> {
26    label: Option<&'a str>,
27    width: u32,
28    height: u32,
29    format: TextureFormat,
30    usage: TextureUsages,
31    mip_level_count: u32,
32    sample_count: u32,
33}
34
35impl<'a> RenderTargetTextureBuilder<'a> {
36    pub fn new(width: u32, height: u32, format: TextureFormat) -> Self {
37        Self {
38            label: None,
39            width,
40            height,
41            format,
42            usage: TextureUsages::empty(),
43            mip_level_count: 1,
44            sample_count: 1,
45        }
46    }
47
48    pub fn with_label(mut self, label: impl Into<Option<&'a str>>) -> Self {
49        self.label = label.into();
50        self
51    }
52
53    pub fn with_usage(mut self, usage: TextureUsages) -> Self {
54        self.usage = usage;
55        self
56    }
57
58    pub fn with_mip_level_count(mut self, count: u32) -> Self {
59        self.mip_level_count = count;
60        self
61    }
62
63    pub fn with_sample_count(mut self, count: u32) -> Self {
64        self.sample_count = count;
65        self
66    }
67
68    pub fn build(self, backend: &Backend) -> TextureView {
69        check_texture_dimensions(&backend.device, "RenderTargetTextureBuilder", self.width, self.height);
70
71        let texture = backend.device.create_texture(&wgpu::TextureDescriptor {
72            label: self.label,
73            size: wgpu::Extent3d { width: self.width, height: self.height, depth_or_array_layers: 1 },
74            mip_level_count: self.mip_level_count,
75            sample_count: self.sample_count,
76            dimension: wgpu::TextureDimension::D2,
77            format: self.format.into(),
78            usage: self.usage.into(),
79            view_formats: &[],
80        });
81        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
82        TextureView { view, _texture: texture }
83    }
84}