librashader_test/render/
wgpu.rs

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::render::{CommonFrameOptions, RenderTest};
use anyhow::anyhow;
use image::RgbaImage;
use librashader::runtime::wgpu::*;
use librashader::runtime::Viewport;
use librashader_runtime::image::{Image, UVDirection};
use std::io::{Cursor, Write};
use std::ops::DerefMut;
use std::path::Path;
use std::sync::Arc;
use wgpu::{Adapter, Device, Instance, Queue, Texture};
use wgpu_types::{
    BufferAddress, BufferDescriptor, BufferUsages, CommandEncoderDescriptor, ImageCopyBuffer,
    ImageDataLayout, Maintain, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
};

use librashader::presets::ShaderPreset;
use librashader::runtime::{FilterChainParameters, RuntimeParameters};
use parking_lot::Mutex;

pub struct Wgpu {
    _instance: Instance,
    _adapter: Adapter,
    device: Arc<Device>,
    queue: Arc<Queue>,
    _image: Image,
    texture: Arc<Texture>,
}

struct BufferDimensions {
    height: usize,
    unpadded_bytes_per_row: usize,
    padded_bytes_per_row: usize,
}

impl BufferDimensions {
    fn new(width: usize, height: usize) -> Self {
        let bytes_per_pixel = size_of::<u32>();
        let unpadded_bytes_per_row = width * bytes_per_pixel;
        let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize;
        let padded_bytes_per_row_padding = (align - unpadded_bytes_per_row % align) % align;
        let padded_bytes_per_row = unpadded_bytes_per_row + padded_bytes_per_row_padding;
        Self {
            height,
            unpadded_bytes_per_row,
            padded_bytes_per_row,
        }
    }
}

impl RenderTest for Wgpu {
    fn new(path: &Path) -> anyhow::Result<Self>
    where
        Self: Sized,
    {
        Wgpu::new(path)
    }

    fn render_with_preset_and_params(
        &mut self,
        preset: ShaderPreset,
        frame_count: usize,
        param_setter: Option<&dyn Fn(&RuntimeParameters)>,
        frame_options: Option<CommonFrameOptions>,
    ) -> anyhow::Result<image::RgbaImage> {
        let mut chain = FilterChain::load_from_preset(
            preset,
            Arc::clone(&self.device),
            Arc::clone(&self.queue),
            Some(&FilterChainOptions {
                force_no_mipmaps: false,
                enable_cache: true,
                adapter_info: None,
            }),
        )?;
        if let Some(setter) = param_setter {
            setter(&chain.parameters());
        }
        let mut cmd = self
            .device
            .create_command_encoder(&CommandEncoderDescriptor { label: None });

        let output_tex = self.device.create_texture(&TextureDescriptor {
            label: None,
            size: self.texture.size(),
            mip_level_count: 1,
            sample_count: 1,
            dimension: TextureDimension::D2,
            format: TextureFormat::Rgba8Unorm,
            usage: TextureUsages::RENDER_ATTACHMENT | TextureUsages::COPY_SRC,
            view_formats: &[wgpu::TextureFormat::Rgba8Unorm],
        });

        let buffer_dimensions =
            BufferDimensions::new(output_tex.width() as usize, output_tex.height() as usize);
        let output_buf = Arc::new(self.device.create_buffer(&BufferDescriptor {
            label: None,
            size: (buffer_dimensions.padded_bytes_per_row * buffer_dimensions.height)
                as BufferAddress, // 4bpp
            usage: BufferUsages::COPY_DST | BufferUsages::MAP_READ,
            mapped_at_creation: false,
        }));

        let view = output_tex.create_view(&wgpu::TextureViewDescriptor::default());
        let output = WgpuOutputView::new_from_raw(
            &view,
            output_tex.size().into(),
            TextureFormat::Rgba8Unorm,
        );

        let viewport = Viewport::new_render_target_sized_origin(output, None)?;
        let options = frame_options.map(|options| FrameOptions {
            clear_history: options.clear_history,
            frame_direction: options.frame_direction,
            rotation: options.rotation,
            total_subframes: options.total_subframes,
            current_subframe: options.current_subframe,
        });

        for frame in 0..=frame_count {
            chain.frame(
                Arc::clone(&self.texture),
                &viewport,
                &mut cmd,
                frame,
                options.as_ref(),
            )?;
        }

        cmd.copy_texture_to_buffer(
            output_tex.as_image_copy(),
            ImageCopyBuffer {
                buffer: &output_buf,
                layout: ImageDataLayout {
                    offset: 0,
                    bytes_per_row: Some(buffer_dimensions.padded_bytes_per_row as u32),
                    rows_per_image: None,
                },
            },
            output_tex.size(),
        );

        let si = self.queue.submit([cmd.finish()]);
        self.device.poll(Maintain::WaitForSubmissionIndex(si));

        let capturable = Arc::clone(&output_buf);

        let pixels = Arc::new(Mutex::new(Vec::new()));

        let pixels_async = Arc::clone(&pixels);
        output_buf
            .slice(..)
            .map_async(wgpu::MapMode::Read, move |r| {
                if r.is_ok() {
                    let buffer = capturable.slice(..).get_mapped_range();
                    let mut pixels = pixels_async.lock();
                    pixels.resize(buffer.len(), 0);

                    let mut cursor = Cursor::new(pixels.deref_mut());
                    for chunk in buffer.chunks(buffer_dimensions.padded_bytes_per_row) {
                        cursor
                            .write_all(&chunk[..buffer_dimensions.unpadded_bytes_per_row])
                            .unwrap()
                    }

                    cursor.into_inner();
                }
                capturable.unmap();
            });

        self.device.poll(Maintain::Wait);

        if pixels.lock().len() == 0 {
            return Err(anyhow!("failed to copy pixels from buffer"));
        }

        let image = RgbaImage::from_raw(
            output_tex.width(),
            output_tex.height(),
            pixels.lock().to_vec(),
        )
        .ok_or(anyhow!("Unable to create image from data"))?;

        Ok(image)
    }
}

impl Wgpu {
    pub fn new(image: &Path) -> anyhow::Result<Self> {
        pollster::block_on(async {
            let instance = wgpu::Instance::default();
            let adapter = instance
                .request_adapter(&wgpu::RequestAdapterOptions {
                    power_preference: wgpu::PowerPreference::default(),
                    compatible_surface: None,
                    force_fallback_adapter: false,
                })
                .await
                .ok_or(anyhow!("Couldn't request WGPU adapter"))?;

            let (device, queue) = adapter
                .request_device(
                    &wgpu::DeviceDescriptor {
                        required_features: wgpu::Features::ADDRESS_MODE_CLAMP_TO_BORDER
                            | wgpu::Features::PIPELINE_CACHE
                            | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES
                            | wgpu::Features::FLOAT32_FILTERABLE,
                        required_limits: wgpu::Limits::default(),
                        label: None,
                        memory_hints: Default::default(),
                    },
                    None,
                )
                .await?;
            let (image, texture) = Self::load_image(&device, &queue, image)?;

            Ok(Self {
                _instance: instance,
                _adapter: adapter,
                device: Arc::new(device),
                queue: Arc::new(queue),
                _image: image,
                texture: Arc::new(texture),
            })
        })
    }

    fn load_image(device: &Device, queue: &Queue, path: &Path) -> anyhow::Result<(Image, Texture)> {
        let image = Image::load(path, UVDirection::TopLeft)?;
        let texture = device.create_texture(&TextureDescriptor {
            size: image.size.into(),
            mip_level_count: 1,
            sample_count: 1,
            dimension: TextureDimension::D2,
            usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
            format: wgpu::TextureFormat::Rgba8Unorm,
            view_formats: &[wgpu::TextureFormat::Rgba8Unorm],
            label: None,
        });

        queue.write_texture(
            wgpu::ImageCopyTexture {
                texture: &texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            &image.bytes,
            wgpu::ImageDataLayout {
                offset: 0,
                bytes_per_row: Some(4 * image.size.width),
                rows_per_image: None,
            },
            image.size.into(),
        );

        let si = queue.submit([]);

        device.poll(Maintain::WaitForSubmissionIndex(si));

        Ok((image, texture))
    }
}