aboutsummaryrefslogtreecommitdiff
path: root/old/vgcodec/src/app.rs
blob: b51284c504e51bf9adf04fdaefca52ae6e4a8f04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::sync::Arc;

use wgpu::{Adapter, Device, Extent3d, ImageCopyTexture, Instance, Origin3d, Queue, Texture};

pub struct App {
    pub instance: Instance,
    pub device: Device,
    pub adapter: Adapter,
    pub queue: Queue,
}

impl App {
    pub async fn new() -> Arc<Self> {
        let instance = wgpu::Instance::new(wgpu::Backends::all());
        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions::default())
            .await
            .unwrap();
        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: None,
                    features: wgpu::Features::empty(),
                    limits: wgpu::Limits::downlevel_defaults(),
                },
                None,
            )
            .await
            .unwrap();
        Arc::new(Self {
            adapter,
            device,
            instance,
            queue,
        })
    }

    pub fn copy_texture(&self, source: &Texture, destination: &Texture, size: Extent3d) {
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
        encoder.copy_texture_to_texture(
            ImageCopyTexture {
                aspect: wgpu::TextureAspect::All,
                mip_level: 0,
                origin: Origin3d::ZERO,
                texture: source,
            },
            ImageCopyTexture {
                aspect: wgpu::TextureAspect::All,
                mip_level: 0,
                origin: Origin3d::ZERO,
                texture: destination,
            },
            size,
        );
        self.queue.submit(Some(encoder.finish()));
    }
}