wallr-core 0.3.0

Core library for wallr — wallpaper engine, animation system, theme provider dispatch, and package management.
Documentation
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use image::GenericImageView;
use wgpu::util::DeviceExt;

pub struct Renderer {
    pub instance: wgpu::Instance,
    pub adapter: wgpu::Adapter,
    pub device: wgpu::Device,
    pub queue: wgpu::Queue,
    pub bind_group_layout_tex: wgpu::BindGroupLayout,
    pub bind_group_layout_uni: wgpu::BindGroupLayout,
    pipeline_layout: wgpu::PipelineLayout,
    shader: wgpu::ShaderModule,
    /// Cached pipeline for a specific surface format. Created lazily.
    pipeline: std::sync::Mutex<Option<(wgpu::TextureFormat, wgpu::RenderPipeline)>>,
}

/// Per-output uniform buffer and bind group. Each output gets its own so
/// concurrent renders never race on shared GPU state.
pub struct PerOutputUniforms {
    pub buffer: wgpu::Buffer,
    pub bind_group: wgpu::BindGroup,
}

#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Uniforms {
    pub time: f32,
    pub progress: f32,
    pub effect_type: u32,
    pub padding: u32,
    pub resolution: [f32; 2],
    pub image_resolution: [f32; 2],
    pub old_image_resolution: [f32; 2],
    pub param_a: f32,
    pub param_b: f32,
    pub param_c: f32,
    pub param_d: f32,
    pub origin: [f32; 2],
    pub direction: [f32; 2],
    pub easing: u32,
    pub scaling_mode: u32,
}

impl Uniforms {
    pub fn from_effect(effect: &crate::animation::EffectUniforms) -> Self {
        Self {
            time: effect.progress,
            progress: effect.progress,
            effect_type: effect.effect_type,
            padding: 0,
            resolution: [1920.0, 1080.0],
            image_resolution: [1920.0, 1080.0],
            old_image_resolution: [1920.0, 1080.0],
            param_a: effect.param_a,
            param_b: effect.param_b,
            param_c: effect.param_c,
            param_d: effect.param_d,
            origin: effect.origin,
            direction: effect.direction,
            easing: effect.easing,
            scaling_mode: 0,
        }
    }
}

impl Default for Uniforms {
    fn default() -> Self {
        Self {
            time: 0.0,
            progress: 0.0,
            effect_type: 0,
            padding: 0,
            resolution: [1920.0, 1080.0],
            image_resolution: [1920.0, 1080.0],
            old_image_resolution: [1920.0, 1080.0],
            param_a: 0.0,
            param_b: 0.0,
            param_c: 0.0,
            param_d: 0.0,
            origin: [0.5, 0.5],
            direction: [0.0, 0.0],
            easing: 3,
            scaling_mode: 0,
        }
    }
}

impl Renderer {
    pub async fn new() -> anyhow::Result<Self> {
        let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
            backends: wgpu::Backends::all(),
            ..Default::default()
        });

        let adapter = instance
            .request_adapter(&wgpu::RequestAdapterOptions {
                power_preference: wgpu::PowerPreference::HighPerformance,
                compatible_surface: None,
                force_fallback_adapter: false,
            })
            .await
            .ok_or_else(|| anyhow::anyhow!("Failed to find suitable adapter"))?;

        let (device, queue) = adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: None,
                    required_features: wgpu::Features::empty(),
                    required_limits: wgpu::Limits::default(),
                    memory_hints: wgpu::MemoryHints::default(),
                },
                None,
            )
            .await?;

        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("Effects Shader"),
            source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(
                crate::shader::EFFECTS_SHADER,
            )),
        });

        let bind_group_layout_tex =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                entries: &[
                    wgpu::BindGroupLayoutEntry {
                        binding: 0,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Texture {
                            multisampled: false,
                            view_dimension: wgpu::TextureViewDimension::D2,
                            sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        },
                        count: None,
                    },
                    wgpu::BindGroupLayoutEntry {
                        binding: 1,
                        visibility: wgpu::ShaderStages::FRAGMENT,
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                ],
                label: Some("texture_bind_group_layout"),
            });

        let bind_group_layout_uni =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                }],
                label: Some("uniform_bind_group_layout"),
            });

        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("Render Pipeline Layout"),
            bind_group_layouts: &[
                &bind_group_layout_tex,
                &bind_group_layout_tex,
                &bind_group_layout_uni,
            ],
            push_constant_ranges: &[],
        });

        Ok(Self {
            instance,
            adapter,
            device,
            queue,
            pipeline_layout,
            shader,
            bind_group_layout_tex,
            bind_group_layout_uni,
            pipeline: std::sync::Mutex::new(None),
        })
    }

    /// Create a per-output uniform buffer and bind group so each output
    /// renders with its own GPU state, eliminating cross-output races.
    pub fn create_per_output_uniforms(&self) -> PerOutputUniforms {
        let uniforms = Uniforms::default();
        let buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("Per-Output Uniform Buffer"),
                contents: bytemuck::cast_slice(&[uniforms]),
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            });
        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout: &self.bind_group_layout_uni,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: buffer.as_entire_binding(),
            }],
            label: Some("per_output_uniform_bind_group"),
        });
        PerOutputUniforms { buffer, bind_group }
    }

    fn get_pipeline(&self, format: wgpu::TextureFormat) -> wgpu::RenderPipeline {
        let mut cache = self.pipeline.lock().unwrap();
        if let Some((cached_fmt, ref pipeline)) = *cache
            && cached_fmt == format
        {
            return pipeline.clone();
        }

        let pipeline = self
            .device
            .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
                label: Some("Render Pipeline"),
                layout: Some(&self.pipeline_layout),
                vertex: wgpu::VertexState {
                    module: &self.shader,
                    entry_point: Some("vs_main"),
                    buffers: &[],
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                },
                fragment: Some(wgpu::FragmentState {
                    module: &self.shader,
                    entry_point: Some("fs_main"),
                    targets: &[Some(wgpu::ColorTargetState {
                        format,
                        blend: Some(wgpu::BlendState::REPLACE),
                        write_mask: wgpu::ColorWrites::ALL,
                    })],
                    compilation_options: wgpu::PipelineCompilationOptions::default(),
                }),
                primitive: wgpu::PrimitiveState {
                    topology: wgpu::PrimitiveTopology::TriangleList,
                    strip_index_format: None,
                    front_face: wgpu::FrontFace::Ccw,
                    cull_mode: None, // Don't cull — fullscreen quad
                    polygon_mode: wgpu::PolygonMode::Fill,
                    unclipped_depth: false,
                    conservative: false,
                },
                depth_stencil: None,
                multisample: wgpu::MultisampleState {
                    count: 1,
                    mask: !0,
                    alpha_to_coverage_enabled: false,
                },
                multiview: None,
                cache: None,
            });

        *cache = Some((format, pipeline.clone()));
        pipeline
    }

    pub fn create_texture(&self, width: u32, height: u32) -> (wgpu::Texture, wgpu::BindGroup) {
        let size = wgpu::Extent3d {
            width,
            height,
            depth_or_array_layers: 1,
        };

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

        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        let sampler = self.device.create_sampler(&wgpu::SamplerDescriptor {
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Nearest,
            mipmap_filter: wgpu::FilterMode::Nearest,
            ..Default::default()
        });

        let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout: &self.bind_group_layout_tex,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&sampler),
                },
            ],
            label: None,
        });

        (texture, bind_group)
    }

    pub fn update_texture(&self, texture: &wgpu::Texture, rgba: &[u8], width: u32, height: u32) {
        self.queue.write_texture(
            wgpu::TexelCopyTextureInfo {
                texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            rgba,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(4 * width),
                rows_per_image: Some(height),
            },
            wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 1,
            },
        );
    }

    pub fn load_texture(
        &self,
        image: &image::DynamicImage,
    ) -> anyhow::Result<(wgpu::Texture, wgpu::BindGroup)> {
        let rgba = image.to_rgba8();
        let (width, height) = image.dimensions();
        let (texture, bind_group) = self.create_texture(width, height);
        self.update_texture(&texture, &rgba, width, height);
        Ok((texture, bind_group))
    }

    pub fn update_uniforms(&self, buffer: &wgpu::Buffer, uniforms: Uniforms) {
        self.queue
            .write_buffer(buffer, 0, bytemuck::cast_slice(&[uniforms]));
    }

    pub fn render_frame(
        &self,
        request: FrameRequest,
        per_output: &PerOutputUniforms,
    ) -> anyhow::Result<FrameStatus> {
        let FrameRequest {
            surface,
            format,
            bg_bind,
            new_bind,
            effect,
            width,
            height,
            img_width,
            img_height,
            old_img_width,
            old_img_height,
            scaling_mode,
        } = request;
        let mut uniforms = Uniforms::from_effect(effect);
        uniforms.resolution = [width as f32, height as f32];
        uniforms.image_resolution = [img_width as f32, img_height as f32];
        uniforms.old_image_resolution = [old_img_width as f32, old_img_height as f32];
        uniforms.scaling_mode = scaling_mode;
        self.update_uniforms(&per_output.buffer, uniforms);

        let pipeline = self.get_pipeline(format);
        // `get_current_texture` blocks until the compositor presents (Fifo),
        // which paces rendering to the refresh rate. A stalled compositor
        // parks this call, which is safe because transition loops always run
        // on detached tasks that never block the daemon's IPC loop.
        let output = match surface.get_current_texture() {
            Ok(texture) => texture,
            Err(wgpu::SurfaceError::Timeout) => return Ok(FrameStatus::TimedOut),
            Err(err) => {
                return Err(anyhow::anyhow!(
                    "failed to acquire swapchain texture: {err:?}"
                ));
            }
        };
        let view = output
            .texture
            .create_view(&wgpu::TextureViewDescriptor::default());

        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Render Encoder"),
            });

        {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Wallpaper Render Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &view,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                occlusion_query_set: None,
                timestamp_writes: None,
            });

            render_pass.set_pipeline(&pipeline);
            render_pass.set_bind_group(0, bg_bind, &[]);
            render_pass.set_bind_group(1, new_bind, &[]);
            render_pass.set_bind_group(2, &per_output.bind_group, &[]);
            // The vertex shader generates a fullscreen triangle from vertex_index 0..3
            render_pass.draw(0..3, 0..1);
        }

        self.queue.submit(std::iter::once(encoder.finish()));
        output.present();

        Ok(FrameStatus::Presented)
    }
}

/// Whether a frame was actually presented to the surface. A `TimedOut` frame
/// means the compositor is not requesting frames right now (e.g. the monitor
/// is off), and callers should stop rendering instead of spinning.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameStatus {
    Presented,
    TimedOut,
}

/// Everything needed to present one transition frame to a surface.
pub struct FrameRequest<'a> {
    pub surface: &'a wgpu::Surface<'a>,
    pub format: wgpu::TextureFormat,
    /// Outgoing wallpaper frame, normally the daemon's previous wallpaper.
    pub bg_bind: &'a wgpu::BindGroup,
    /// New wallpaper.
    pub new_bind: &'a wgpu::BindGroup,
    /// Effect uniforms for this frame (progress, params, origin, easing...).
    pub effect: &'a crate::animation::EffectUniforms,
    pub width: u32,
    pub height: u32,
    pub img_width: u32,
    pub img_height: u32,
    pub old_img_width: u32,
    pub old_img_height: u32,
    /// Scaling mode: 0=Fill, 1=Fit, 2=Stretch, 3=Center, 4=Tile.
    pub scaling_mode: u32,
}