Skip to main content

wasdk_wgpu/
texture.rs

1//! Textures and their views.
2
3use wasdk::webgpu::{
4    GpuDevice, GpuTexelCopyTextureInfo, GpuTexture, GpuTextureDescriptor, GpuTextureView,
5    GpuTextureViewDescriptor,
6};
7use wgpu::custom::{DispatchTexture, DispatchTextureView, TextureInterface, TextureViewInterface};
8
9use crate::convert;
10
11/// The texture behind a [`wgpu::Texture`] from this backend: `texture.as_custom::<WasdkTexture>()`.
12#[derive(Debug)]
13pub struct WasdkTexture {
14    texture: GpuTexture,
15}
16
17downcast!(WasdkTexture: api wgpu::Texture);
18
19impl WasdkTexture {
20    pub(crate) fn new(texture: GpuTexture) -> Self {
21        WasdkTexture { texture }
22    }
23
24    /// The wasi texture behind the wgpu one.
25    pub fn gpu_texture(&self) -> &GpuTexture {
26        &self.texture
27    }
28}
29
30pub(crate) fn create_texture(
31    device: &GpuDevice,
32    desc: &wgpu::TextureDescriptor<'_>,
33) -> DispatchTexture {
34    let descriptor = GpuTextureDescriptor {
35        size: convert::extent_to_wasi(desc.size),
36        mip_level_count: Some(desc.mip_level_count),
37        sample_count: Some(desc.sample_count),
38        dimension: Some(convert::texture_dimension_to_wasi(desc.dimension)),
39        format: convert::texture_format_to_wasi(desc.format),
40        usage: convert::texture_usages_to_wasi(desc.usage),
41        view_formats: Some(
42            desc.view_formats
43                .iter()
44                .map(|format| convert::texture_format_to_wasi(*format))
45                .collect(),
46        ),
47        texture_binding_view_dimension: None,
48        label: convert::label(desc.label),
49    };
50    DispatchTexture::custom(WasdkTexture::new(device.create_texture(&descriptor)))
51}
52
53pub(crate) fn texel_copy_texture_to_wasi<'a>(
54    info: &wgpu::TexelCopyTextureInfo<'a>,
55) -> GpuTexelCopyTextureInfo<'a> {
56    GpuTexelCopyTextureInfo {
57        texture: WasdkTexture::of(info.texture).gpu_texture(),
58        mip_level: Some(info.mip_level),
59        origin: Some(convert::origin_to_wasi(info.origin)),
60        aspect: Some(convert::texture_aspect_to_wasi(info.aspect)),
61    }
62}
63
64impl TextureInterface for WasdkTexture {
65    fn create_view(&self, desc: &wgpu::TextureViewDescriptor<'_>) -> DispatchTextureView {
66        let descriptor = GpuTextureViewDescriptor {
67            format: desc.format.map(convert::texture_format_to_wasi),
68            dimension: desc.dimension.map(convert::texture_view_dimension_to_wasi),
69            usage: desc.usage.map(convert::texture_usages_to_wasi),
70            aspect: Some(convert::texture_aspect_to_wasi(desc.aspect)),
71            base_mip_level: Some(desc.base_mip_level),
72            mip_level_count: desc.mip_level_count,
73            base_array_layer: Some(desc.base_array_layer),
74            array_layer_count: desc.array_layer_count,
75            swizzle: None,
76            label: convert::label(desc.label),
77        };
78        DispatchTextureView::custom(WasdkTextureView {
79            view: self.texture.create_view(Some(&descriptor)),
80        })
81    }
82
83    fn destroy(&self) {
84        self.texture.destroy();
85    }
86}
87
88#[derive(Debug)]
89pub struct WasdkTextureView {
90    view: GpuTextureView,
91}
92
93downcast!(WasdkTextureView: api wgpu::TextureView);
94
95impl WasdkTextureView {
96    pub(crate) fn gpu_texture_view(&self) -> &GpuTextureView {
97        &self.view
98    }
99}
100
101impl TextureViewInterface for WasdkTextureView {}