kopki/
texture.rs

1use wgpu::util::DeviceExt;
2
3use crate::{ArcedRenderDevice, RenderSurface};
4
5pub struct RenderableTexture {
6    device: ArcedRenderDevice,
7    texture: wgpu::Texture,
8}
9
10pub struct TextureSampler {
11    sampler: wgpu::Sampler,
12}
13
14impl RenderableTexture {
15    pub fn from_surface(device: &ArcedRenderDevice, surface: &RenderSurface) -> RenderableTexture {
16        let texture_data = vec![
17            255u8;
18            (surface.configuration.width
19                * surface.configuration.height
20                * surface.format.target_pixel_byte_cost().unwrap())
21                as usize
22        ];
23        let texture = device.device.create_texture_with_data(
24            &device.queue,
25            &wgpu::TextureDescriptor {
26                view_formats: &[],
27                label: Some("Texture 2D"),
28                mip_level_count: 1,
29                sample_count: 1,
30                size: wgpu::Extent3d {
31                    width: surface.configuration.width,
32                    height: surface.configuration.height,
33                    depth_or_array_layers: 1,
34                },
35                dimension: wgpu::TextureDimension::D2,
36                format: surface.format,
37                usage: wgpu::TextureUsages::TEXTURE_BINDING
38                    | wgpu::TextureUsages::RENDER_ATTACHMENT,
39            },
40            wgpu::util::TextureDataOrder::LayerMajor,
41            &texture_data,
42        );
43
44        RenderableTexture {
45            device: device.clone(),
46            texture,
47        }
48    }
49    pub fn wgpu_texture(&self) -> &wgpu::Texture {
50        &self.texture
51    }
52    pub fn clear_pass(&self, r: f64, g: f64, b: f64, a: f64) {
53        let view = self.texture.create_view(&wgpu::TextureViewDescriptor {
54            label: Some("Renderable Texture View"),
55            ..Default::default()
56        });
57
58        let mut encoder =
59            self.device
60                .device
61                .create_command_encoder(&wgpu::CommandEncoderDescriptor {
62                    label: Some("Present Framebuffer Command Encoder"),
63                });
64
65        {
66            _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
67                label: Some("Present Framebuffer Render Pass"),
68                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
69                    view: &view,
70                    resolve_target: None,
71                    ops: wgpu::Operations {
72                        load: wgpu::LoadOp::Clear(wgpu::Color { r, g, b, a }),
73                        store: wgpu::StoreOp::Store,
74                    },
75                })],
76                depth_stencil_attachment: None,
77                timestamp_writes: None,
78                occlusion_query_set: None,
79            });
80        }
81
82        self.device.queue.submit([encoder.finish()]);
83    }
84    pub fn clear_pass_with_encoder(
85        &self,
86        encoder: &mut wgpu::CommandEncoder,
87        r: f64,
88        g: f64,
89        b: f64,
90        a: f64,
91    ) {
92        let view = self.texture.create_view(&wgpu::TextureViewDescriptor {
93            label: Some("Renderable Texture View"),
94            ..Default::default()
95        });
96
97        {
98            _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
99                label: Some("Present Framebuffer Render Pass"),
100                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
101                    view: &view,
102                    resolve_target: None,
103                    ops: wgpu::Operations {
104                        load: wgpu::LoadOp::Clear(wgpu::Color { r, g, b, a }),
105                        store: wgpu::StoreOp::Store,
106                    },
107                })],
108                depth_stencil_attachment: None,
109                timestamp_writes: None,
110                occlusion_query_set: None,
111            });
112        }
113    }
114}
115
116impl TextureSampler {
117    pub fn new(device: &ArcedRenderDevice) -> TextureSampler {
118        let sampler = device.device.create_sampler(&wgpu::SamplerDescriptor {
119            label: Some("Sampler"),
120            address_mode_u: wgpu::AddressMode::Repeat,
121            address_mode_v: wgpu::AddressMode::Repeat,
122            address_mode_w: wgpu::AddressMode::Repeat,
123            mag_filter: wgpu::FilterMode::Linear,
124            min_filter: wgpu::FilterMode::Nearest,
125            ..Default::default()
126        });
127        TextureSampler {
128            sampler,
129        }
130    }
131    pub fn wgpu_sampler(&self) -> &wgpu::Sampler {
132        &self.sampler
133    }
134}