aboutsummaryrefslogtreecommitdiff
path: root/flowy/src/motion/debug.rs
blob: 5e5f4eed2e6c2d9d16ef812413c67adf6a814018 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use super::{CommonBuffers, Params, RoundParams};
use bytemuck::{Pod, Zeroable};
use std::mem::size_of;
use wgpu::{
    include_wgsl, BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayoutDescriptor,
    BindGroupLayoutEntry, BindingType, Buffer, BufferDescriptor, BufferUsages, CommandEncoder,
    ComputePipeline, ComputePipelineDescriptor, Device, PipelineCompilationOptions,
    PipelineLayoutDescriptor, Queue, ShaderStages, TextureSampleType, TextureViewDimension,
};

pub struct MotionDebugger {
    pipeline: ComputePipeline,
    bind_groups: [BindGroup; 2],

    uniform_buffer: Buffer,
    uniform: DebuggerUniform,
}

#[repr(C)]
#[derive(Debug, Clone, Copy, Pod, Zeroable, Default)]
pub struct DebuggerUniform {
    block_size: [i32; 2],
    output_stride: i32,
    _pad: u32,
}

impl MotionDebugger {
    pub fn create(device: &Device, params: &Params, bufs: &CommonBuffers) -> Self {
        let uniform_buffer = device.create_buffer(&BufferDescriptor {
            label: Some("encoder uniforms"),
            size: size_of::<DebuggerUniform>() as u64,
            usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });
        let uniform = DebuggerUniform {
            block_size: [params.block_width as i32, params.block_height as i32],
            output_stride: (params.width / params.block_width) as i32,
            ..Default::default()
        };

        let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor {
            label: None,
            entries: &[
                BindGroupLayoutEntry {
                    binding: 0,
                    count: None,
                    ty: BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    visibility: ShaderStages::COMPUTE,
                },
                BindGroupLayoutEntry {
                    binding: 1,
                    count: None,
                    ty: BindingType::Buffer {
                        has_dynamic_offset: false,
                        min_binding_size: None,
                        ty: wgpu::BufferBindingType::Storage { read_only: true },
                    },
                    visibility: ShaderStages::COMPUTE,
                },
                BindGroupLayoutEntry {
                    binding: 2,
                    count: None,
                    ty: BindingType::Texture {
                        sample_type: TextureSampleType::Float { filterable: false },
                        view_dimension: TextureViewDimension::D2,
                        multisampled: false,
                    },
                    visibility: ShaderStages::COMPUTE,
                },
                BindGroupLayoutEntry {
                    binding: 3,
                    count: None,
                    ty: BindingType::Texture {
                        sample_type: TextureSampleType::Float { filterable: false },
                        view_dimension: TextureViewDimension::D2,
                        multisampled: false,
                    },
                    visibility: ShaderStages::COMPUTE,
                },
                BindGroupLayoutEntry {
                    binding: 4,
                    count: None,
                    ty: BindingType::StorageTexture {
                        access: wgpu::StorageTextureAccess::WriteOnly,
                        format: wgpu::TextureFormat::Rgba8Unorm,
                        view_dimension: TextureViewDimension::D2,
                    },
                    visibility: ShaderStages::COMPUTE,
                },
            ],
        });

        let bind_groups = [0, 1].map(|i| {
            device.create_bind_group(&BindGroupDescriptor {
                label: None,
                layout: &bind_group_layout,
                entries: &[
                    BindGroupEntry {
                        binding: 0,
                        resource: uniform_buffer.as_entire_binding(),
                    },
                    BindGroupEntry {
                        binding: 1,
                        resource: bufs.offsets.as_entire_binding(),
                    },
                    BindGroupEntry {
                        binding: 2,
                        resource: wgpu::BindingResource::TextureView(
                            &bufs.textures[i].create_view(&Default::default()),
                        ),
                    },
                    BindGroupEntry {
                        binding: 3,
                        resource: wgpu::BindingResource::TextureView(
                            &bufs.textures[1 - i].create_view(&Default::default()),
                        ),
                    },
                    BindGroupEntry {
                        binding: 4,
                        resource: wgpu::BindingResource::TextureView(
                            &bufs
                                .debug_output
                                .as_ref()
                                .unwrap()
                                .create_view(&Default::default()),
                        ),
                    },
                ],
            })
        });

        let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
            label: None,
            bind_group_layouts: &[&bind_group_layout],
            push_constant_ranges: &[],
        });

        let module = device.create_shader_module(include_wgsl!("debug.wgsl"));
        let pipeline = device.create_compute_pipeline(&ComputePipelineDescriptor {
            label: None,
            layout: Some(&pipeline_layout),
            module: &module,
            entry_point: "main",
            compilation_options: PipelineCompilationOptions::default(),
        });

        Self {
            bind_groups,
            uniform,
            uniform_buffer,
            pipeline,
        }
    }

    pub fn write_uniforms(&self, queue: &Queue) {
        queue.write_buffer(
            &self.uniform_buffer,
            0,
            bytemuck::cast_slice(&[self.uniform]),
        )
    }

    pub fn pass(&self, encoder: &mut CommandEncoder, params: &Params, rp: &RoundParams) {
        let mut cpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
            label: None,
            timestamp_writes: None,
        });
        cpass.set_pipeline(&self.pipeline);
        cpass.set_bind_group(0, &self.bind_groups[rp.swap], &[]);
        cpass.dispatch_workgroups(params.blocks_x as u32, params.blocks_y as u32, 1);
    }
}