oxiui-render-wgpu 0.1.1

wgpu GPU render surface for OxiUI
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
//! GPU pass execution helpers and per-frame statistics for [`WgpuBackend`].
//!
//! This module owns:
//!
//! - [`FrameStats`] — per-frame draw-call and render-pass counters.
//! - `run_solid_pass` — executes the solid vertex-buffer pass using a
//!   persistent, reused, growable vertex buffer.
//! - `run_gradient_pass_batched` — executes ALL gradient draws in a single
//!   render pass using dynamic-offset uniforms.
//! - `run_textured_pass` — executes one textured render pass.
//!
//! [`WgpuBackend`]: super::renderer::WgpuBackend

use oxiui_core::UiError;
use wgpu::util::DeviceExt;

use crate::gpu::buffer::{GradientUniforms, GradientVertex, Vertex};
use crate::gpu::geometry::{DrawSegment, GradientDraw};
use crate::gpu::pipeline::{GradientPipeline, SolidPipeline, TexturedPipeline};
use crate::gpu::texture::{upload_image, TexturedDraw};

// ── FrameStats ────────────────────────────────────────────────────────────────

/// Per-frame draw-call and render-pass counters.
///
/// Populated during `WgpuBackend::execute` and accessible via
/// [`WgpuBackend::frame_stats`].
///
/// [`WgpuBackend::frame_stats`]: super::renderer::WgpuBackend::frame_stats
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FrameStats {
    /// Number of `pass.draw(...)` calls issued during the last `execute()` call.
    ///
    /// Counts only real GPU draws; skipped/dead segments are excluded.
    /// Shadow passes are included.
    pub draw_calls: u32,
    /// Number of `begin_render_pass` calls issued during the last `execute()` call.
    ///
    /// Includes the clear pass, shadow passes, solid pass, gradient pass(es),
    /// and textured pass(es).
    pub render_passes: u32,
}

// ── align_up ──────────────────────────────────────────────────────────────────

/// Round `n` up to the next multiple of `align` (which must be a power of two).
#[inline]
pub(crate) fn align_up(n: u32, align: u32) -> u32 {
    (n + align - 1) & !(align - 1)
}

// ── Solid pass ────────────────────────────────────────────────────────────────

/// Parameters for the solid-geometry render pass.
pub(crate) struct SolidPassParams<'a> {
    pub(crate) device: &'a wgpu::Device,
    pub(crate) queue: &'a wgpu::Queue,
    pub(crate) encoder: &'a mut wgpu::CommandEncoder,
    pub(crate) screen_view: &'a wgpu::TextureView,
    pub(crate) screen_resolve: Option<&'a wgpu::TextureView>,
    pub(crate) pipeline: &'a SolidPipeline,
    pub(crate) globals_bind_group: &'a wgpu::BindGroup,
    pub(crate) verts: &'a [Vertex],
    pub(crate) segments: &'a [DrawSegment],
    pub(crate) viewport_w: u32,
    pub(crate) viewport_h: u32,
    /// Persistent vertex buffer: mutated in-place if it needs to grow.
    pub(crate) solid_vertex_buf: &'a mut Option<wgpu::Buffer>,
    /// Byte capacity of the persistent vertex buffer.
    pub(crate) solid_vertex_buf_capacity: &'a mut usize,
}

/// Execute the solid geometry pass, returning the number of draw calls issued.
///
/// Uses a persistent, reusable vertex buffer that is grown (next power-of-two)
/// whenever the current frame requires more capacity, then updated via
/// `queue.write_buffer` instead of creating a new buffer.
pub(crate) fn run_solid_pass(p: SolidPassParams<'_>) -> u32 {
    let mut draw_calls = 0u32;

    // ── Persistent buffer management ─────────────────────────────────────────
    // If we have vertices to draw, ensure the persistent buffer is large enough
    // and upload the current frame's geometry.
    if !p.verts.is_empty() {
        let verts_bytes: &[u8] = bytemuck::cast_slice(p.verts);
        let needed = verts_bytes.len();

        let needs_grow = p.solid_vertex_buf.is_none() || *p.solid_vertex_buf_capacity < needed;

        if needs_grow {
            // Grow to the next power of two, with a minimum of 64 vertices.
            let min_bytes = core::mem::size_of::<Vertex>() * 64;
            let new_cap = needed.next_power_of_two().max(min_bytes);
            *p.solid_vertex_buf = Some(p.device.create_buffer(&wgpu::BufferDescriptor {
                label: Some("oxiui-render-wgpu solid-verts-persistent"),
                size: new_cap as u64,
                usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
                mapped_at_creation: false,
            }));
            *p.solid_vertex_buf_capacity = new_cap;
        }

        // Upload current frame's geometry into the persistent buffer.
        if let Some(buf) = p.solid_vertex_buf.as_ref() {
            p.queue.write_buffer(buf, 0, verts_bytes);
        }
    }

    let mut pass = p.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
        label: Some("oxiui-render-wgpu solid pass"),
        color_attachments: &[Some(wgpu::RenderPassColorAttachment {
            view: p.screen_view,
            depth_slice: None,
            resolve_target: p.screen_resolve,
            ops: wgpu::Operations {
                load: wgpu::LoadOp::Load,
                store: wgpu::StoreOp::Store,
            },
        })],
        depth_stencil_attachment: None,
        timestamp_writes: None,
        occlusion_query_set: None,
        multiview_mask: None,
    });

    if let Some(ref vb) = p.solid_vertex_buf {
        if !p.verts.is_empty() {
            // ── Pipeline state caching ────────────────────────────────────
            // Track raw pointers to the last-set pipeline and globals bind
            // group so that redundant `set_pipeline` / `set_bind_group`
            // calls can be skipped.  These are frame-local variables;
            // each RenderPass starts from a clean GPU state so there is
            // nothing to carry across pass boundaries.
            let mut last_pipeline_ptr: Option<*const wgpu::RenderPipeline> = None;
            let mut last_globals_bg_ptr: Option<*const wgpu::BindGroup> = None;

            let cur_pipeline_ptr = &p.pipeline.pipeline as *const wgpu::RenderPipeline;
            let cur_globals_bg_ptr = p.globals_bind_group as *const wgpu::BindGroup;

            if last_pipeline_ptr != Some(cur_pipeline_ptr) {
                pass.set_pipeline(&p.pipeline.pipeline);
                last_pipeline_ptr = Some(cur_pipeline_ptr);
            }
            if last_globals_bg_ptr != Some(cur_globals_bg_ptr) {
                pass.set_bind_group(0, p.globals_bind_group, &[]);
                last_globals_bg_ptr = Some(cur_globals_bg_ptr);
            }
            // Anchor the caching locals so the compiler does not warn
            // about dead assignments when there is only one pipeline/BG.
            let _ = last_pipeline_ptr;
            let _ = last_globals_bg_ptr;

            // Draw only the vertices for this frame (0..verts.len()), not
            // the whole buffer capacity.
            pass.set_vertex_buffer(
                0,
                vb.slice(..p.verts.len() as u64 * core::mem::size_of::<Vertex>() as u64),
            );

            for seg in p.segments {
                match seg.scissor {
                    Some([_, _, 0, _]) | Some([_, _, _, 0]) => continue,
                    Some([x, y, w, h]) => pass.set_scissor_rect(x, y, w, h),
                    None => pass.set_scissor_rect(0, 0, p.viewport_w, p.viewport_h),
                }
                pass.draw(seg.start..seg.end, 0..1);
                draw_calls += 1;
            }
        }
    }

    draw_calls
}

// ── Gradient pass (batched) ───────────────────────────────────────────────────

/// Parameters for the batched gradient render pass.
///
/// All gradient draws are coalesced into a single render pass using a
/// dynamic-offset uniform buffer.  The pipeline bind group layout must have
/// binding 1 with `has_dynamic_offset: true`.
pub(crate) struct GradientPassParams<'a> {
    pub(crate) device: &'a wgpu::Device,
    pub(crate) queue: &'a wgpu::Queue,
    pub(crate) encoder: &'a mut wgpu::CommandEncoder,
    pub(crate) screen_view: &'a wgpu::TextureView,
    pub(crate) screen_resolve: Option<&'a wgpu::TextureView>,
    pub(crate) pipeline: &'a GradientPipeline,
    pub(crate) globals_buffer: &'a wgpu::Buffer,
    pub(crate) gradient_draws: &'a [GradientDraw],
    pub(crate) viewport_w: u32,
    pub(crate) viewport_h: u32,
}

/// Execute ALL gradient draws in a single render pass via dynamic-offset uniforms.
///
/// Returns `(render_passes_added, draw_calls_added)`.
/// Returns `(0, 0)` when `gradient_draws` is empty.
///
/// # Dynamic-offset batching
///
/// All per-gradient [`GradientUniforms`] are packed into a single combined
/// buffer with a stride of `align_up(sizeof(GradientUniforms),
/// min_uniform_buffer_offset_alignment)`.  Inside the one render pass each
/// draw call issues `set_bind_group` with a different byte offset — this is
/// valid because binding 1 in the gradient bind group layout has
/// `has_dynamic_offset: true`.
pub(crate) fn run_gradient_pass_batched(p: GradientPassParams<'_>) -> (u32, u32) {
    if p.gradient_draws.is_empty() {
        return (0, 0);
    }

    // ── Compute per-element stride (device-aligned) ───────────────────────
    let struct_size = core::mem::size_of::<GradientUniforms>() as u32;
    let min_align = p.device.limits().min_uniform_buffer_offset_alignment;
    let grad_stride = align_up(struct_size, min_align) as u64;

    let n_grads = p.gradient_draws.len() as u64;

    // ── Build combined uniforms buffer ────────────────────────────────────
    let grad_uniform_buf = p.device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("oxiui-render-wgpu grad-uniforms-combined"),
        size: n_grads * grad_stride,
        usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    for (i, gd) in p.gradient_draws.iter().enumerate() {
        p.queue.write_buffer(
            &grad_uniform_buf,
            i as u64 * grad_stride,
            bytemuck::bytes_of(&gd.uniforms),
        );
    }

    // ── Build combined vertex buffer (all quads concatenated) ─────────────
    let all_verts: Vec<GradientVertex> = p
        .gradient_draws
        .iter()
        .flat_map(|gd| gd.verts.iter().copied())
        .collect();

    if all_verts.is_empty() {
        return (0, 0);
    }

    let grad_vert_buf = p
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("oxiui-render-wgpu grad-verts-combined"),
            contents: bytemuck::cast_slice(&all_verts),
            usage: wgpu::BufferUsages::VERTEX,
        });

    // ── Create ONE bind group using the combined uniforms buffer ──────────
    // Binding 1 (dynamic) needs a sized binding so the driver knows which
    // chunk of the buffer each draw uses.  We provide a BufferBinding with
    // offset=0 and size=sizeof(GradientUniforms); the actual per-draw offset
    // is supplied via the dynamic offset argument to set_bind_group.
    let grad_uniform_size =
        core::num::NonZeroU64::new(core::mem::size_of::<GradientUniforms>() as u64);
    let grad_bg = p.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("oxiui-render-wgpu gradient-batched bg"),
        layout: &p.pipeline.bind_group_layout,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: p.globals_buffer.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
                    buffer: &grad_uniform_buf,
                    offset: 0,
                    size: grad_uniform_size,
                }),
            },
        ],
    });

    // ── ONE render pass for all gradient draws ────────────────────────────
    let mut pass = p.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
        label: Some("oxiui-render-wgpu gradient-batched pass"),
        color_attachments: &[Some(wgpu::RenderPassColorAttachment {
            view: p.screen_view,
            depth_slice: None,
            resolve_target: p.screen_resolve,
            ops: wgpu::Operations {
                load: wgpu::LoadOp::Load,
                store: wgpu::StoreOp::Store,
            },
        })],
        depth_stencil_attachment: None,
        timestamp_writes: None,
        occlusion_query_set: None,
        multiview_mask: None,
    });

    pass.set_pipeline(&p.pipeline.pipeline);
    pass.set_vertex_buffer(0, grad_vert_buf.slice(..));

    let mut draw_calls = 0u32;
    let mut vertex_offset: u32 = 0;

    for (i, gd) in p.gradient_draws.iter().enumerate() {
        if gd.verts.is_empty() {
            continue;
        }

        // Dynamic offset selects this gradient's uniform slice.
        let dyn_offset = (i as u64 * grad_stride) as u32;
        pass.set_bind_group(0, &grad_bg, &[dyn_offset]);

        // Apply scissor for this gradient draw.
        match gd.scissor {
            Some([_, _, 0, _]) | Some([_, _, _, 0]) => {
                vertex_offset += gd.verts.len() as u32;
                continue;
            }
            Some([x, y, w, h]) => pass.set_scissor_rect(x, y, w, h),
            None => pass.set_scissor_rect(0, 0, p.viewport_w, p.viewport_h),
        }

        let n_verts = gd.verts.len() as u32;
        pass.draw(vertex_offset..vertex_offset + n_verts, 0..1);
        draw_calls += 1;
        vertex_offset += n_verts;
    }

    (1, draw_calls)
}

// ── Textured pass ─────────────────────────────────────────────────────────────

/// Parameters for a single textured render pass.
pub(crate) struct TexturedPassParams<'a> {
    pub(crate) device: &'a wgpu::Device,
    pub(crate) queue: &'a wgpu::Queue,
    pub(crate) encoder: &'a mut wgpu::CommandEncoder,
    pub(crate) screen_view: &'a wgpu::TextureView,
    pub(crate) screen_resolve: Option<&'a wgpu::TextureView>,
    pub(crate) pipeline: &'a TexturedPipeline,
    pub(crate) globals_bind_group: &'a wgpu::BindGroup,
    pub(crate) td: &'a TexturedDraw,
    pub(crate) viewport_w: u32,
    pub(crate) viewport_h: u32,
}

/// Execute one textured pass. Returns `(render_passes_added, draw_calls_added)`.
///
/// Returns `(0, 0)` if the textured draw has no vertices.
///
/// # Errors
///
/// Returns [`UiError::Render`] if texture upload fails.
pub(crate) fn run_textured_pass(p: TexturedPassParams<'_>) -> Result<(u32, u32), UiError> {
    if p.td.verts.is_empty() {
        return Ok((0, 0));
    }

    let (tex_view, tex_sampler) = upload_image(p.device, p.queue, &p.td.image, p.td.filter)
        .map_err(|e| UiError::Render(format!("texture upload failed: {e}")))?;

    let tex_vb = p
        .device
        .create_buffer_init(&wgpu::util::BufferInitDescriptor {
            label: Some("oxiui-render-wgpu tex verts"),
            contents: bytemuck::cast_slice(&p.td.verts),
            usage: wgpu::BufferUsages::VERTEX,
        });

    let tex_bg = p.device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("oxiui-render-wgpu tex bind group"),
        layout: &p.pipeline.texture_layout,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: wgpu::BindingResource::TextureView(&tex_view),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: wgpu::BindingResource::Sampler(&tex_sampler),
            },
        ],
    });

    let mut pass = p.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
        label: Some("oxiui-render-wgpu textured pass"),
        color_attachments: &[Some(wgpu::RenderPassColorAttachment {
            view: p.screen_view,
            depth_slice: None,
            resolve_target: p.screen_resolve,
            ops: wgpu::Operations {
                load: wgpu::LoadOp::Load,
                store: wgpu::StoreOp::Store,
            },
        })],
        depth_stencil_attachment: None,
        timestamp_writes: None,
        occlusion_query_set: None,
        multiview_mask: None,
    });

    pass.set_pipeline(&p.pipeline.pipeline);
    pass.set_bind_group(0, p.globals_bind_group, &[]);
    pass.set_bind_group(1, &tex_bg, &[]);
    pass.set_vertex_buffer(0, tex_vb.slice(..));

    match p.td.scissor {
        Some([_, _, 0, _]) | Some([_, _, _, 0]) => return Ok((1, 0)),
        Some([x, y, w, h]) => pass.set_scissor_rect(x, y, w, h),
        None => pass.set_scissor_rect(0, 0, p.viewport_w, p.viewport_h),
    }

    pass.draw(0..p.td.verts.len() as u32, 0..1);
    Ok((1, 1))
}