Skip to main content

repose_render_wgpu/
commands.rs

1//! Shared application of render commands queued by `RenderContext`.
2//!
3//! Platform runners (desktop/android/web) and embedders (bevy, baseview)
4//! can move image set/remove commands from the reactive scene into the GPU
5//! texture cache through this.
6
7use repose_core::RenderCommand;
8
9use crate::WgpuSceneRenderer;
10
11/// Apply a batch of [`RenderCommand`]s to a scene renderer.
12///
13/// `WgpuSurfaceBackend` derefs to `WgpuSceneRenderer`, so this works for
14/// both the windowed backends and offscreen-device embedders.
15pub fn apply_render_commands(renderer: &mut WgpuSceneRenderer, cmds: Vec<RenderCommand>) {
16    for cmd in cmds {
17        match cmd {
18            RenderCommand::SetImageEncoded {
19                handle,
20                bytes,
21                srgb,
22            } => {
23                if let Err(e) = renderer.set_image_from_bytes(handle, &bytes, srgb) {
24                    log::warn!("repose-render: SetImageEncoded({handle}): {e:#}");
25                }
26            }
27            RenderCommand::SetImageRgba8 {
28                handle,
29                w,
30                h,
31                rgba,
32                srgb,
33            } => {
34                if let Err(e) = renderer.set_image_rgba8(handle, w, h, &rgba, srgb) {
35                    log::warn!("repose-render: SetImageRgba8({handle}): {e:#}");
36                }
37            }
38            RenderCommand::SetImageNv12 {
39                handle,
40                w,
41                h,
42                y,
43                uv,
44                color_info,
45            } => {
46                if let Err(e) = renderer.set_image_nv12(handle, w, h, &y, &uv, color_info) {
47                    log::warn!("repose-render: SetImageNv12({handle}): {e:#}");
48                }
49            }
50            RenderCommand::SetImagePlanes {
51                handle,
52                w,
53                h,
54                pixel_format,
55                planes,
56                color_info,
57            } => {
58                let refs: Vec<&[u8]> = planes.iter().map(|p| p.as_ref()).collect();
59                if let Err(e) =
60                    renderer.set_image_planes(handle, w, h, pixel_format, &refs, color_info)
61                {
62                    log::warn!("repose-render: SetImagePlanes({handle}): {e:#}");
63                }
64            }
65            #[cfg(target_os = "linux")]
66            RenderCommand::SetImageDmaBuf {
67                handle,
68                w,
69                h,
70                fds,
71                fourcc: _,
72                modifier,
73                strides,
74                offsets,
75                color_info,
76            } => {
77                if let Err(e) = renderer
78                    .set_image_dmabuf(handle, w, h, fds, modifier, strides, offsets, color_info)
79                {
80                    log::warn!("repose-render: SetImageDmabuf({handle}): {e:#}");
81                }
82            }
83            RenderCommand::RemoveImage { handle } => {
84                renderer.remove_image(handle);
85            }
86        }
87    }
88}