aboutsummaryrefslogtreecommitdiff
path: root/flowy/src/motion/mod.rs
blob: dfdf7a68c0672d215565581a2bb86a4169b4211d (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
pub mod debug;
pub mod dec;
pub mod enc;

use std::mem::size_of;
use wgpu::{
    Buffer, BufferUsages, CommandEncoder, Device, Extent3d, ImageCopyTexture, Origin3d, Queue,
    Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
};

pub struct Params {
    pub width: usize,
    pub height: usize,
    pub extent: Extent3d,
    pub block_width: usize,
    pub block_height: usize,
    pub blocks_x: usize,
    pub blocks_y: usize,
    pub blocks: usize,
}

pub struct RoundParams {
    pub swap: usize,
}

pub struct CommonBuffers {
    textures: [Texture; 2],
    offsets: Buffer,
    offsets_download: Option<Buffer>,
    debug_output: Option<Texture>,
    texture_download: Option<Buffer>,
}

#[repr(C)]
pub struct BlockOffset {
    score: f32,
    _pad: u32,
    offset: [f32; 2],
    tint: [f32; 3],
}

impl CommonBuffers {
    pub fn create(device: &Device, params: &Params) -> Self {
        let textures = [(), ()].map(|_| {
            device.create_texture(&TextureDescriptor {
                label: None,
                size: params.extent,
                mip_level_count: 1,
                sample_count: 1,
                dimension: TextureDimension::D2,
                format: TextureFormat::Bgra8Unorm,
                usage: TextureUsages::TEXTURE_BINDING
                    | TextureUsages::STORAGE_BINDING
                    | TextureUsages::COPY_DST
                    | TextureUsages::COPY_SRC,
                view_formats: &[],
            })
        });

        let debug_output = Some(device.create_texture(&TextureDescriptor {
            label: None,
            size: params.extent,
            mip_level_count: 1,
            sample_count: 1,
            dimension: TextureDimension::D2,
            format: TextureFormat::Bgra8Unorm,
            usage: TextureUsages::STORAGE_BINDING | TextureUsages::COPY_SRC,
            view_formats: &[],
        }));

        let texture_download = Some(device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: (params.width * params.height * 4) as u64,
            usage: BufferUsages::COPY_DST | BufferUsages::MAP_READ,
            mapped_at_creation: false,
        }));

        let offsets = device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: (params.blocks * size_of::<BlockOffset>()) as u64,
            usage: BufferUsages::COPY_DST | BufferUsages::STORAGE,
            mapped_at_creation: false,
        });
        let offsets_download = Some(device.create_buffer(&wgpu::BufferDescriptor {
            label: None,
            size: (params.blocks * size_of::<BlockOffset>()) as u64,
            usage: BufferUsages::COPY_DST | BufferUsages::MAP_READ,
            mapped_at_creation: false,
        }));

        Self {
            debug_output,
            textures,
            offsets_download,
            offsets,
            texture_download,
        }
    }

    pub fn upload(&self, queue: &Queue, params: &Params, rp: &RoundParams, buffer: &[u8]) {
        queue.write_texture(
            ImageCopyTexture {
                aspect: wgpu::TextureAspect::All,
                mip_level: 0,
                origin: Origin3d::ZERO,
                texture: &self.textures[rp.swap],
            },
            buffer,
            wgpu::ImageDataLayout {
                offset: 0,
                bytes_per_row: Some(params.extent.width * 4),
                rows_per_image: Some(params.extent.height),
            },
            params.extent,
        );
    }

    pub fn prepare_texture_download(
        &self,
        encoder: &mut CommandEncoder,
        params: &Params,
        rp: &RoundParams,
    ) {
        encoder.copy_texture_to_buffer(
            wgpu::ImageCopyTexture {
                texture: &self.textures[rp.swap],
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            wgpu::ImageCopyBuffer {
                buffer: self.texture_download.as_ref().unwrap(),
                layout: wgpu::ImageDataLayout {
                    offset: 0,
                    bytes_per_row: Some(params.extent.width * 4),
                    rows_per_image: Some(params.extent.height),
                },
            },
            params.extent,
        );
    }

    pub fn download(&self, device: &Device, buffer: &mut [u8]) {
        let buffer_slice = self.texture_download.as_ref().unwrap().slice(..);
        let (sender, receiver) = oneshot::channel();
        buffer_slice.map_async(wgpu::MapMode::Read, move |r| sender.send(r).unwrap());
        device.poll(wgpu::Maintain::Wait);
        receiver.recv().unwrap().unwrap();
        {
            let view = buffer_slice.get_mapped_range();
            buffer.copy_from_slice(&view[..]);
        }
        self.texture_download.as_ref().unwrap().unmap();
    }
}