scrcap 0.1.0-alpha.2

Screen and system-audio capture for Windows, macOS and Linux
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! This example use `wgpu` to display the captured frames (For test only).

use std::{sync::Arc, thread, time::Duration};

use anyhow::Result;
use crossbeam_channel::{Receiver, TryRecvError, bounded};
use log::{error, info, warn};
#[cfg(any(target_os = "macos", target_os = "windows"))]
use wgpu::rwh::HasWindowHandle;
use wgpu::{CurrentSurfaceTexture, util::DeviceExt as _};
use winit::{
    application::ApplicationHandler,
    event::{KeyEvent, WindowEvent},
    event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
    keyboard::{KeyCode, PhysicalKey},
    window::{Window, WindowId, WindowLevel},
};

use scrcap::{CaptureConfig, CaptureDesc, CaptureDescriptor as _, Target, VideoConfig, VideoFrame};

fn main() {
    env_logger::builder()
        .filter_level(log::LevelFilter::Debug)
        .init();
    let event_loop = EventLoop::with_user_event().build().unwrap();
    event_loop
        .run_app(&mut App::default())
        .expect("Event loop should run successfully");
}

#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C)]
struct Vertex {
    position: [f32; 3],
    tex_coords: [f32; 2],
}

impl Vertex {
    fn desc() -> wgpu::VertexBufferLayout<'static> {
        const ATTRIBS: [wgpu::VertexAttribute; 2] =
            wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2];
        wgpu::VertexBufferLayout {
            array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
            step_mode: wgpu::VertexStepMode::Vertex,
            attributes: &ATTRIBS,
        }
    }
}

const VERTICES: &[Vertex] = &[
    Vertex {
        position: [-1.0, 1.0, 0.0],
        tex_coords: [0.0, 0.0],
    }, // A
    Vertex {
        position: [-1.0, -1.0, 0.0],
        tex_coords: [0.0, 1.0],
    }, // B
    Vertex {
        position: [1.0, -1.0, 0.0],
        tex_coords: [1.0, 1.0],
    }, // C
    Vertex {
        position: [1.0, 1.0, 0.0],
        tex_coords: [1.0, 0.0],
    }, // D
];

const INDICES: &[u16] = &[0, 1, 2, 0, 2, 3];

#[derive(Debug, Default)]
struct App {
    state: Option<State>,
    /// The capture being built on another thread.
    pending: Option<Receiver<scrcap::error::Result<CaptureDesc>>>,
}

/// This window's own id, in the form [`VideoConfig::hide`] wants, so the preview does not
/// end up recording itself. Empty where the platform cannot hide a window.
#[allow(unused_variables, unused_mut)]
fn hide_self(window: &Window) -> Vec<isize> {
    let mut hide = Vec::new();
    #[cfg(target_os = "macos")]
    if let Ok(wgpu::rwh::RawWindowHandle::AppKit(handle)) =
        window.window_handle().map(|handle| handle.as_raw())
    {
        // The view belongs to this window, so it is alive for the call.
        let id =
            unsafe { scrcap::platform::window_id_from_ns_view(handle.ns_view.as_ptr() as isize) };
        hide.extend(id.map(|id| id as isize));
    }
    #[cfg(target_os = "windows")]
    if let Ok(wgpu::rwh::RawWindowHandle::Win32(handle)) =
        window.window_handle().map(|handle| handle.as_raw())
    {
        hide.push(handle.hwnd.get());
    }
    hide
}

/// Build the capture on another thread.
///
/// `Target::Pick` blocks `create` until the user chooses, and on Windows and macOS the picker
/// answers on this very thread, so `create` has to run somewhere else.
fn start_capture(window: &Window) -> Receiver<scrcap::error::Result<CaptureDesc>> {
    let hide = hide_self(window);
    #[cfg(target_os = "windows")]
    let target = Target::Pick(match window.window_handle().map(|handle| handle.as_raw()) {
        Ok(wgpu::rwh::RawWindowHandle::Win32(handle)) => handle.hwnd.get(),
        _ => panic!("the picker needs a Win32 window to present from"),
    });
    #[cfg(not(target_os = "windows"))]
    let target = Target::Pick;

    let (tx, rx) = bounded(1);
    thread::spawn(move || {
        let _ = tx.send(
            CaptureConfig {
                video: VideoConfig {
                    channel_capacity: 2,
                    hide,
                    target,
                    fps: Some(60),
                },
                audio: None,
            }
            .create(),
        );
    });
    rx
}

impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        let window = event_loop
            .create_window(Window::default_attributes().with_window_level(WindowLevel::AlwaysOnTop))
            .expect("Window should be created");
        let state = pollster::block_on(State::new(window)).expect("State should be initialized");
        state.window.request_redraw();
        self.state = Some(state);
    }

    fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
        let (Some(rx), Some(state)) = (self.pending.as_ref(), self.state.as_mut()) else {
            return;
        };
        match rx.try_recv() {
            Ok(Ok(capture_desc)) => {
                state.attach(capture_desc);
                self.pending = None;
                event_loop.set_control_flow(ControlFlow::Wait);
            }
            Ok(Err(e)) => {
                error!("failed to start the capture: {e}");
                event_loop.exit();
            }
            Err(TryRecvError::Empty) => {
                event_loop.set_control_flow(ControlFlow::wait_duration(Duration::from_millis(50)));
            }
            Err(TryRecvError::Disconnected) => {
                error!("the capture thread went away");
                event_loop.exit();
            }
        }
    }

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        _window_id: WindowId,
        event: WindowEvent,
    ) {
        let Some(state) = &mut self.state else {
            return;
        };

        match event {
            WindowEvent::CloseRequested => event_loop.exit(),
            WindowEvent::Resized(size) => state.resize(size.width, size.height),
            WindowEvent::RedrawRequested => {
                if !state.render() {
                    event_loop.exit();
                }
                // Only once the window has shown a frame: GNOME's mutter has crashed capturing
                // a window, picked through the portal, that had not drawn yet.
                if state.presented && state.capture_desc.is_none() && self.pending.is_none() {
                    self.pending = Some(start_capture(&state.window));
                    event_loop
                        .set_control_flow(ControlFlow::wait_duration(Duration::from_millis(50)));
                }
            }
            WindowEvent::KeyboardInput {
                event:
                    KeyEvent {
                        physical_key: PhysicalKey::Code(code),
                        state: key_state,
                        ..
                    },
                ..
            } => state.handle_key(event_loop, code, key_state.is_pressed()),
            _ => {}
        }
    }
}

#[derive(Debug)]
struct State {
    window: Arc<Window>,
    surface: wgpu::Surface<'static>,
    device: wgpu::Device,
    queue: wgpu::Queue,
    config: wgpu::SurfaceConfiguration,
    is_surface_configured: bool,
    render_pipeline: wgpu::RenderPipeline,
    vertex_buffer: wgpu::Buffer,
    index_buffer: wgpu::Buffer,
    num_indices: u32,
    texture_bind_group_layout: wgpu::BindGroupLayout,
    diffuse_texture: Texture,
    diffuse_bind_group: wgpu::BindGroup,
    /// Whether a frame has reached the screen yet.
    presented: bool,
    capture_desc: Option<CaptureDesc>,
}

impl State {
    async fn new(window: Window) -> Result<Self> {
        let window = Arc::new(window);
        let size = window.inner_size();

        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
            backends: wgpu::Backends::PRIMARY,
            ..wgpu::InstanceDescriptor::new_without_display_handle()
        });

        let surface = instance.create_surface(window.clone()).unwrap();

        let adapter = instance
            // Without `compatible_surface` an adapter that cannot present to this surface
            // is a valid answer, and `get_capabilities` then reports no formats at all.
            .request_adapter(&wgpu::RequestAdapterOptions {
                compatible_surface: Some(&surface),
                ..Default::default()
            })
            .await?;
        let (device, queue) = adapter
            .request_device(&wgpu::DeviceDescriptor::default())
            .await?;

        let surface_caps = surface.get_capabilities(&adapter);
        let surface_format = surface_caps
            .formats
            .iter()
            .find(|f| f.is_srgb())
            .copied()
            .unwrap_or(surface_caps.formats[0]);
        let config = wgpu::SurfaceConfiguration {
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
            format: surface_format,
            color_space: wgpu::SurfaceColorSpace::Auto,
            width: size.width,
            height: size.height,
            present_mode: surface_caps.present_modes[0],
            alpha_mode: surface_caps.alpha_modes[0],
            view_formats: vec![],
            desired_maximum_frame_latency: 2,
        };

        // A white placeholder until the capture, and with it the real size, arrives.
        let diffuse_texture =
            Texture::from_bytes(&device, &queue, &[255; 4], 1, 1, Some("diffuse_texture"))?;

        let texture_bind_group_layout =
            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,
                        // This should match the filterable field of the
                        // corresponding Texture entry above.
                        ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                        count: None,
                    },
                ],
                label: Some("texture_bind_group_layout"),
            });

        let diffuse_bind_group = diffuse_texture.bind_group(&device, &texture_bind_group_layout);

        let render_pipeline_layout =
            device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("Render Pipeline Layout"),
                bind_group_layouts: &[Some(&texture_bind_group_layout)],
                immediate_size: 0,
            });

        let shader = device.create_shader_module(wgpu::include_wgsl!("shader/shader.wgsl"));

        let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("Render Pipeline"),
            layout: Some(&render_pipeline_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vs_main"),
                buffers: &[Some(Vertex::desc())],
                compilation_options: wgpu::PipelineCompilationOptions::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fs_main"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: config.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: Some(wgpu::Face::Back),
                // Setting this to anything other than Fill requires Features::NON_FILL_POLYGON_MODE
                polygon_mode: wgpu::PolygonMode::Fill,
                // Requires Features::DEPTH_CLIP_CONTROL
                unclipped_depth: false,
                // Requires Features::CONSERVATIVE_RASTERIZATION
                conservative: false,
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState {
                count: 1,
                mask: !0,
                alpha_to_coverage_enabled: false,
            },
            multiview_mask: None,
            cache: None,
        });

        let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("Vertex Buffer"),
            contents: bytemuck::cast_slice(VERTICES),
            usage: wgpu::BufferUsages::VERTEX,
        });
        let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("Index Buffer"),
            contents: bytemuck::cast_slice(INDICES),
            usage: wgpu::BufferUsages::INDEX,
        });

        // Configure now: nothing can be presented before the first `Resized` otherwise.
        let is_surface_configured = size.width > 0 && size.height > 0;
        if is_surface_configured {
            surface.configure(&device, &config);
        }

        Ok(Self {
            window,
            surface,
            device,
            queue,
            config,
            is_surface_configured,
            render_pipeline,
            vertex_buffer,
            index_buffer,
            num_indices: INDICES.len() as u32,
            texture_bind_group_layout,
            diffuse_texture,
            diffuse_bind_group,
            presented: false,
            capture_desc: None,
        })
    }

    /// Start showing a capture, sizing the texture to it.
    fn attach(&mut self, capture_desc: CaptureDesc) {
        let (width, height) = capture_desc.size();
        self.resize_texture(width, height);
        self.capture_desc = Some(capture_desc);
    }

    fn resize_texture(&mut self, width: u32, height: u32) {
        self.diffuse_texture = Texture::from_bytes(
            &self.device,
            &self.queue,
            &vec![255; 4 * width as usize * height as usize],
            width,
            height,
            Some("diffuse_texture"),
        )
        .expect("texture should be created");
        self.diffuse_bind_group = self
            .diffuse_texture
            .bind_group(&self.device, &self.texture_bind_group_layout);
    }

    fn resize(&mut self, width: u32, height: u32) {
        info!("Resizing to {width}x{height}");
        if width > 0 && height > 0 {
            self.config.width = width;
            self.config.height = height;
            self.surface.configure(&self.device, &self.config);
            self.is_surface_configured = true;
        }
    }

    /// Returns false once the capture has ended, so the caller can leave the event loop.
    fn render(&mut self) -> bool {
        self.window.request_redraw();

        // We can't render unless the surface is configured
        if !self.is_surface_configured {
            return true;
        }

        // `get_current_texture` no longer returns a `Result`; every outcome, including
        // the ones that used to be a `SurfaceError`, is a `CurrentSurfaceTexture` variant.
        let (output, suboptimal) = match self.surface.get_current_texture() {
            CurrentSurfaceTexture::Success(output) => (output, false),
            // Usable, but the surface wants reconfiguring before the next frame.
            CurrentSurfaceTexture::Suboptimal(output) => (output, true),
            // Nothing to draw into right now; try again on the next redraw.
            CurrentSurfaceTexture::Timeout | CurrentSurfaceTexture::Occluded => return true,
            CurrentSurfaceTexture::Outdated | CurrentSurfaceTexture::Lost => {
                warn!("Surface lost or outdated, reconfiguring...");
                let size = self.window.inner_size();
                self.resize(size.width, size.height);
                return true;
            }
            CurrentSurfaceTexture::Validation => {
                error!("Unable to render: surface validation error");
                return true;
            }
        };
        if !self.update() {
            return false;
        }
        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("Render Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &view,
                    depth_slice: None,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color {
                            r: 1.0,
                            g: 1.0,
                            b: 1.0,
                            a: 0.0,
                        }),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                occlusion_query_set: None,
                timestamp_writes: None,
                multiview_mask: None,
            });

            render_pass.set_pipeline(&self.render_pipeline);
            render_pass.set_bind_group(0, &self.diffuse_bind_group, &[]);
            render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
            render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);

            render_pass.draw_indexed(0..self.num_indices, 0, 0..1);
        }

        // submit will accept anything that implements IntoIter
        self.queue.submit([encoder.finish()]);
        self.queue.present(output);
        self.presented = true;

        // Reconfigure only once the frame is presented, so the surface is not
        // reconfigured while a texture acquired from it is still alive.
        if suboptimal {
            warn!("Surface suboptimal, reconfiguring...");
            let size = self.window.inner_size();
            self.resize(size.width, size.height);
        }
        true
    }

    fn handle_key(&self, event_loop: &ActiveEventLoop, code: KeyCode, is_pressed: bool) {
        #[allow(clippy::single_match)]
        match (code, is_pressed) {
            (KeyCode::Escape, true) => event_loop.exit(),
            _ => {}
        }
    }

    /// Returns false once the capture has ended.
    ///
    /// Never blocks: this runs on the event loop, so waiting for a frame here would freeze
    /// the window whenever delivery stalls -- which is exactly what happens when the capture
    /// is stopped from outside, e.g. macOS's "Stop Sharing".
    fn update(&mut self) -> bool {
        let Some(capture_desc) = &self.capture_desc else {
            return true;
        };
        let frame = match capture_desc.video().try_recv() {
            Ok(frame) => frame,
            Err(TryRecvError::Empty) => return true,
            Err(TryRecvError::Disconnected) => {
                info!("capture ended");
                return false;
            }
        };
        let VideoFrame { vframe, size, .. } = frame;
        // `write_texture` panics on an extent mismatch, and the source can resize mid-capture.
        if (size.0, size.1)
            != (
                self.diffuse_texture.texture_size.width,
                self.diffuse_texture.texture_size.height,
            )
        {
            info!("capture size changed to {size:?}");
            self.resize_texture(size.0, size.1);
        }
        self.queue.write_texture(
            // Tells wgpu where to copy the pixel data
            wgpu::TexelCopyTextureInfo {
                texture: &self.diffuse_texture.texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            // The actual pixel data
            &vframe,
            // The layout of the texture
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(4 * size.0),
                rows_per_image: Some(size.1),
            },
            self.diffuse_texture.texture_size,
        );
        true
    }
}

#[derive(Debug)]
pub struct Texture {
    pub texture: wgpu::Texture,
    pub view: wgpu::TextureView,
    pub sampler: wgpu::Sampler,
    pub texture_size: wgpu::Extent3d,
}

impl Texture {
    pub fn from_bytes(
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        bytes: &[u8],
        width: u32,
        height: u32,
        label: Option<&str>,
    ) -> Result<Self> {
        let texture_size = wgpu::Extent3d {
            width,
            height,
            // All textures are stored as 3D, we represent our 2D texture
            // by setting depth to 1.
            depth_or_array_layers: 1,
        };
        let texture = device.create_texture(&wgpu::TextureDescriptor {
            size: texture_size,
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            // Most images are stored using sRGB, so we need to reflect that here.
            format: wgpu::TextureFormat::Bgra8UnormSrgb,
            // TEXTURE_BINDING tells wgpu that we want to use this texture in shaders
            // COPY_DST means that we want to copy data to this texture
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
            label,
            // This is the same as with the SurfaceConfig. It
            // specifies what texture formats can be used to
            // create TextureViews for this texture. The base
            // texture format (Bgra8UnormSrgb in this case) is
            // always supported. Note that using a different
            // texture format is not supported on the WebGL2
            // backend.
            view_formats: &[],
        });
        queue.write_texture(
            // Tells wgpu where to copy the pixel data
            wgpu::TexelCopyTextureInfo {
                texture: &texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            // The actual pixel data
            bytes,
            // The layout of the texture
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(4 * width),
                rows_per_image: Some(height),
            },
            texture_size,
        );

        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        let sampler = 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::MipmapFilterMode::Nearest,
            ..Default::default()
        });

        Ok(Self {
            texture,
            view,
            sampler,
            texture_size,
        })
    }

    fn bind_group(&self, device: &wgpu::Device, layout: &wgpu::BindGroupLayout) -> wgpu::BindGroup {
        device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: wgpu::BindingResource::TextureView(&self.view),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::Sampler(&self.sampler),
                },
            ],
            label: Some("diffuse_bind_group"),
        })
    }
}