Skip to main content

sg_guest/
gpu.rs

1//! A wgpu custom backend that forwards to the host through the sg GPU API.
2//!
3//! ```ignore
4//! let instance = sg_guest::gpu::instance();
5//! let surface = sg_guest::gpu::surface(&instance);
6//! let adapter = instance.request_adapter(&Default::default()).await?;   // resolves immediately
7//! ```
8//!
9//! Object handles are host ids. Command encoders, passes and bundles are
10//! recorded here and shipped in one request when finished. Objects referenced
11//! by a recording are kept alive until the host has the finished command
12//! buffer. Futures returned by this backend are always already resolved, so a
13//! trivial executor (see [`block_on`]) is enough. Callbacks (buffer mapping,
14//! work-done, uncaptured errors, device loss) run from `Device::poll` /
15//! [`pump`], which the game should call once per frame, the way a browser runs
16//! them from its event loop.
17
18use sg_abi::gpu::{self as p, Id, Reply, Req};
19use sg_abi::wgt;
20use std::cell::{Cell, RefCell};
21use std::collections::HashMap;
22use std::future::Future;
23use std::ops::Range;
24use std::pin::Pin;
25use std::rc::Rc;
26use std::sync::Arc;
27use wgpu::custom::*;
28
29use crate::sys;
30
31// ------------------------------------------------------------------ transport
32
33#[derive(Default)]
34struct State {
35    reply: Vec<u8>,
36    drops: Vec<Id>,
37    next_token: u64,
38    map_cbs: HashMap<u64, (Rc<BufState>, bool, u64, u64, BufferMapCallback)>,
39    done_cbs: HashMap<u64, BoxSubmittedWorkDoneCallback>,
40    uncaptured: Option<Arc<dyn wgpu::UncapturedErrorHandler>>,
41    lost: Option<BoxDeviceLostCallback>,
42    scope_depth: u32,
43}
44
45thread_local! {
46    static ST: RefCell<State> = RefCell::new(State { reply: Vec::with_capacity(1 << 16), ..Default::default() });
47}
48
49fn raw_call(req: &Req) -> Reply {
50    let bytes = p::encode(req);
51    let mut out = ST.with(|s| std::mem::take(&mut s.borrow_mut().reply));
52    let cap = out.capacity();
53    out.resize(cap, 0);
54    let n = unsafe { sys::gpu_call(bytes.as_ptr(), bytes.len(), out.as_mut_ptr(), cap) } as usize;
55    if n > cap {
56        out.resize(n, 0);
57        unsafe { sys::gpu_reply(out.as_mut_ptr(), n) };
58    }
59    let r: Reply = p::decode(&out[..n]).expect("sg-gpu: undecodable reply");
60    out.clear();
61    ST.with(|s| s.borrow_mut().reply = out);
62    r
63}
64
65fn flush_drops() {
66    let ids = ST.with(|s| std::mem::take(&mut s.borrow_mut().drops));
67    if !ids.is_empty() {
68        raw_call(&Req::Drop { ids });
69    }
70}
71
72fn call(req: Req) -> Reply {
73    flush_drops();
74    raw_call(&req)
75}
76
77fn call_id(req: Req) -> Id {
78    match call(req) {
79        Reply::Id(id) => id,
80        r => panic!("sg-gpu: unexpected reply {r:?}"),
81    }
82}
83
84fn token() -> u64 {
85    ST.with(|s| {
86        let mut s = s.borrow_mut();
87        s.next_token += 1;
88        s.next_token
89    })
90}
91
92/// Polls the host and runs completed callbacks. Call once per frame.
93pub fn pump() {
94    poll_events(false);
95}
96
97fn poll_events(wait: bool) {
98    let evs = match call(Req::Poll { wait }) {
99        Reply::Events(e) => e,
100        r => panic!("sg-gpu: unexpected reply {r:?}"),
101    };
102    for e in evs {
103        match e {
104            p::Event::Mapped { token, ok } => {
105                let cb = ST.with(|s| s.borrow_mut().map_cbs.remove(&token));
106                if let Some((buf, write, start, end, cb)) = cb {
107                    if ok {
108                        *buf.mapped.borrow_mut() = Some((write, start, end));
109                        buf.fresh.set(false);
110                    }
111                    cb(if ok { Ok(()) } else { Err(wgpu::BufferAsyncError) });
112                }
113            }
114            p::Event::WorkDone { token } => {
115                if let Some(cb) = ST.with(|s| s.borrow_mut().done_cbs.remove(&token)) {
116                    cb();
117                }
118            }
119            p::Event::Uncaptured(e) => {
120                let h = ST.with(|s| s.borrow().uncaptured.clone());
121                match h {
122                    Some(h) => h(to_error(e)),
123                    None => crate::log_str(&format!("uncaptured GPU error: {}", e.message)),
124                }
125            }
126            p::Event::DeviceLost(msg) => {
127                if let Some(cb) = ST.with(|s| s.borrow_mut().lost.take()) {
128                    cb(wgpu::DeviceLostReason::Unknown, msg);
129                }
130            }
131        }
132    }
133}
134
135#[derive(Debug)]
136struct HostError(String);
137impl std::fmt::Display for HostError {
138    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139        f.write_str(&self.0)
140    }
141}
142impl std::error::Error for HostError {}
143
144fn to_error(e: p::ErrorInfo) -> wgpu::Error {
145    let source: wgpu::ErrorSource = Box::new(HostError(e.message.clone()));
146    match e.kind {
147        p::error_kind::OUT_OF_MEMORY => wgpu::Error::OutOfMemory { source },
148        p::error_kind::INTERNAL => wgpu::Error::Internal { source, description: e.message },
149        _ => wgpu::Error::Validation { source, description: e.message },
150    }
151}
152
153/// Runs a future that this backend returns (they are always ready).
154pub fn block_on<F: Future>(f: F) -> F::Output {
155    use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
156    fn noop(_: *const ()) {}
157    fn clone(p: *const ()) -> RawWaker {
158        RawWaker::new(p, &VT)
159    }
160    static VT: RawWakerVTable = RawWakerVTable::new(clone, noop, noop, noop);
161    let waker = unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VT)) };
162    let mut f = std::pin::pin!(f);
163    let mut cx = Context::from_waker(&waker);
164    loop {
165        if let Poll::Ready(v) = f.as_mut().poll(&mut cx) {
166            return v;
167        }
168        poll_events(true);
169    }
170}
171
172fn ready<T: 'static>(v: T) -> Pin<Box<dyn Future<Output = T>>> {
173    Box::pin(std::future::ready(v))
174}
175
176// ------------------------------------------------------------------ handles
177
178/// A host object id; dropping the last reference frees the host object.
179#[derive(Debug)]
180struct H {
181    id: Id,
182    owned: Cell<bool>,
183}
184
185impl H {
186    fn new(id: Id) -> Rc<H> {
187        Rc::new(H { id, owned: Cell::new(true) })
188    }
189}
190
191impl Drop for H {
192    fn drop(&mut self) {
193        if self.owned.get() && self.id != 0 {
194            let _ = ST.try_with(|s| s.borrow_mut().drops.push(self.id));
195        }
196    }
197}
198
199macro_rules! simple {
200    ($name:ident, $iface:ident) => {
201        #[derive(Debug)]
202        pub struct $name(Rc<H>);
203        impl $iface for $name {}
204    };
205}
206
207simple!(SgBindGroupLayout, BindGroupLayoutInterface);
208simple!(SgBindGroup, BindGroupInterface);
209simple!(SgTextureView, TextureViewInterface);
210simple!(SgSampler, SamplerInterface);
211simple!(SgTlas, TlasInterface);
212simple!(SgPipelineLayout, PipelineLayoutInterface);
213simple!(SgCommandBuffer, CommandBufferInterface);
214simple!(SgRenderBundle, RenderBundleInterface);
215
216macro_rules! idof {
217    ($d:expr, $t:ty) => {
218        $d.as_custom::<$t>().expect(concat!("sg-gpu: foreign ", stringify!($t))).0.clone()
219    };
220}
221
222fn bufh(b: &wgpu::Buffer) -> Rc<H> {
223    b.as_custom::<SgBuffer>().expect("sg-gpu: foreign buffer").h.clone()
224}
225
226fn keep_h(keep: &mut Vec<Rc<H>>, h: &Rc<H>) -> Id {
227    keep.push(h.clone());
228    h.id
229}
230
231fn lbl(l: wgpu::Label<'_>) -> p::Label {
232    l.map(|s| s.chars().take(200).collect())
233}
234
235// ------------------------------------------------------------------ instance / adapter
236
237#[derive(Debug)]
238pub struct SgInstance;
239
240/// Creates a `wgpu::Instance` backed by the host GPU.
241pub fn instance() -> wgpu::Instance {
242    match call(Req::Hello { version: p::VERSION }) {
243        Reply::Hello { version } if version == p::VERSION => {}
244        r => panic!("sg-gpu: host protocol mismatch: {r:?}"),
245    }
246    wgpu::Instance::from_custom(SgInstance)
247}
248
249/// Creates the game window's surface.
250pub fn surface(instance: &wgpu::Instance) -> wgpu::Surface<'static> {
251    use wgpu::rwh::{RawDisplayHandle, RawWindowHandle, WebDisplayHandle, WebWindowHandle};
252    unsafe {
253        instance
254            .create_surface_unsafe(wgpu::SurfaceTargetUnsafe::RawHandle {
255                raw_display_handle: Some(RawDisplayHandle::Web(WebDisplayHandle::new())),
256                raw_window_handle: RawWindowHandle::Web(WebWindowHandle::new(1)),
257            })
258            .expect("sg-gpu: surface")
259    }
260}
261
262impl InstanceInterface for SgInstance {
263    fn new(_desc: wgpu::InstanceDescriptor) -> Self {
264        SgInstance
265    }
266
267    unsafe fn create_surface(&self, _target: wgpu::SurfaceTargetUnsafe) -> Result<DispatchSurface, wgpu::CreateSurfaceError> {
268        Ok(DispatchSurface::custom(SgSurface))
269    }
270
271    fn request_adapter(&self, _options: &wgpu::RequestAdapterOptions<'_, '_>) -> Pin<Box<dyn RequestAdapterFuture>> {
272        let a = match call(Req::Adapter) {
273            Reply::Adapter(a) => a,
274            r => panic!("sg-gpu: unexpected reply {r:?}"),
275        };
276        Box::pin(std::future::ready(Ok(DispatchAdapter::custom(SgAdapter(Rc::new(a))))))
277    }
278
279    fn poll_all_devices(&self, force_wait: bool) -> bool {
280        poll_events(force_wait);
281        true
282    }
283
284    fn wgsl_language_features(&self) -> wgpu::WgslLanguageFeatures {
285        wgpu::WgslLanguageFeatures::all()
286    }
287
288    fn enumerate_adapters(&self, _backends: wgpu::Backends) -> Pin<Box<dyn EnumerateAdapterFuture>> {
289        let a = match call(Req::Adapter) {
290            Reply::Adapter(a) => a,
291            r => panic!("sg-gpu: unexpected reply {r:?}"),
292        };
293        Box::pin(std::future::ready(vec![DispatchAdapter::custom(SgAdapter(Rc::new(a)))]))
294    }
295}
296
297#[derive(Debug)]
298pub struct SgAdapter(Rc<p::AdapterDesc>);
299
300impl AdapterInterface for SgAdapter {
301    fn request_device(&self, desc: &wgpu::DeviceDescriptor<'_>) -> Pin<Box<dyn RequestDeviceFuture>> {
302        let r = call(Req::RequestDevice { label: lbl(desc.label), features: desc.required_features.bits().0, limits: desc.required_limits.clone() });
303        let Reply::Device { device, queue, features, limits } = r else { panic!("sg-gpu: unexpected reply {r:?}") };
304        let d = SgDevice(Rc::new(DevInner {
305            _h: H::new(device),
306            features: wgpu::Features::from_bits_truncate(wgt::FeatureBits(features)),
307            limits,
308            info: self.0.info.clone(),
309        }));
310        let q = SgQueue(H::new(queue));
311        Box::pin(std::future::ready(Ok((DispatchDevice::custom(d), DispatchQueue::custom(q)))))
312    }
313
314    fn is_surface_supported(&self, _surface: &DispatchSurface) -> bool {
315        true
316    }
317
318    fn features(&self) -> wgpu::Features {
319        wgpu::Features::from_bits_truncate(wgt::FeatureBits(self.0.features))
320    }
321
322    fn limits(&self) -> wgpu::Limits {
323        self.0.limits.clone()
324    }
325
326    fn downlevel_capabilities(&self) -> wgpu::DownlevelCapabilities {
327        self.0.downlevel.clone()
328    }
329
330    fn get_info(&self) -> wgpu::AdapterInfo {
331        self.0.info.clone()
332    }
333
334    fn get_texture_format_features(&self, format: wgpu::TextureFormat) -> wgpu::TextureFormatFeatures {
335        match call(Req::TextureFormatFeatures(format)) {
336            Reply::FormatFeatures(f) => f,
337            r => panic!("sg-gpu: unexpected reply {r:?}"),
338        }
339    }
340
341    fn get_presentation_timestamp(&self) -> wgpu::PresentationTimestamp {
342        wgpu::PresentationTimestamp::INVALID_TIMESTAMP
343    }
344
345    fn cooperative_matrix_properties(&self) -> Vec<wgt::CooperativeMatrixProperties> {
346        vec![]
347    }
348}
349
350// ------------------------------------------------------------------ device
351
352#[derive(Debug)]
353struct DevInner {
354    _h: Rc<H>,
355    features: wgpu::Features,
356    limits: wgpu::Limits,
357    info: wgpu::AdapterInfo,
358}
359
360#[derive(Debug)]
361pub struct SgDevice(Rc<DevInner>);
362
363#[derive(Debug)]
364pub struct SgShaderModule(Rc<H>);
365
366impl ShaderModuleInterface for SgShaderModule {
367    fn get_compilation_info(&self) -> Pin<Box<dyn ShaderCompilationInfoFuture>> {
368        let msgs = match call(Req::ShaderCompilationInfo { module: self.0.id }) {
369            Reply::Compilation(m) => m,
370            r => panic!("sg-gpu: unexpected reply {r:?}"),
371        };
372        let info = wgpu::CompilationInfo {
373            messages: msgs
374                .into_iter()
375                .map(|m| wgpu::CompilationMessage {
376                    message: m.message,
377                    message_type: if m.is_error { wgpu::CompilationMessageType::Error } else { wgpu::CompilationMessageType::Warning },
378                    location: m.location.map(|(l, c, o, n)| wgpu::SourceLocation { line_number: l, line_position: c, offset: o, length: n }),
379                })
380                .collect(),
381        };
382        Box::pin(std::future::ready(info))
383    }
384}
385
386#[derive(Debug)]
387pub struct SgRenderPipeline(Rc<H>);
388impl RenderPipelineInterface for SgRenderPipeline {
389    fn get_bind_group_layout(&self, index: u32) -> DispatchBindGroupLayout {
390        DispatchBindGroupLayout::custom(SgBindGroupLayout(H::new(call_id(Req::GetBindGroupLayout { pipeline: self.0.id, index }))))
391    }
392}
393
394#[derive(Debug)]
395pub struct SgComputePipeline(Rc<H>);
396impl ComputePipelineInterface for SgComputePipeline {
397    fn get_bind_group_layout(&self, index: u32) -> DispatchBindGroupLayout {
398        DispatchBindGroupLayout::custom(SgBindGroupLayout(H::new(call_id(Req::GetBindGroupLayout { pipeline: self.0.id, index }))))
399    }
400}
401
402#[derive(Debug)]
403pub struct SgPipelineCache;
404impl PipelineCacheInterface for SgPipelineCache {
405    fn get_data(&self) -> Option<Vec<u8>> {
406        None
407    }
408}
409
410#[derive(Debug)]
411pub struct SgQuerySet(Rc<H>);
412impl QuerySetInterface for SgQuerySet {
413    fn destroy(&self) {
414        call(Req::QuerySetDestroy { query_set: self.0.id });
415    }
416}
417
418#[derive(Debug)]
419pub struct SgExternalTexture;
420impl ExternalTextureInterface for SgExternalTexture {
421    fn destroy(&self) {}
422}
423
424#[derive(Debug)]
425pub struct SgBlas(Rc<H>);
426
427impl SgBlas {
428    /// The host id (for tests that speak the wire protocol).
429    pub fn id(&self) -> Id {
430        self.0.id
431    }
432}
433
434impl SgTlas {
435    /// The host id (for tests that speak the wire protocol).
436    pub fn id(&self) -> Id {
437        self.0.id
438    }
439}
440impl BlasInterface for SgBlas {
441    fn prepare_compact_async(&self, callback: BlasCompactCallback) {
442        callback(Err(wgpu::BlasAsyncError));
443    }
444    fn ready_for_compaction(&self) -> bool {
445        false
446    }
447}
448
449fn stage(module: &wgpu::ShaderModule, entry: Option<&str>, o: &wgpu::PipelineCompilationOptions<'_>) -> p::ProgrammableStage {
450    p::ProgrammableStage {
451        module: idof!(module, SgShaderModule).id,
452        entry_point: entry.map(String::from),
453        constants: o.constants.iter().map(|(k, v)| (k.to_string(), *v)).collect(),
454        zero_initialize_workgroup_memory: o.zero_initialize_workgroup_memory,
455    }
456}
457
458fn layout_id(l: Option<&wgpu::PipelineLayout>) -> Id {
459    l.map_or(0, |l| idof!(l, SgPipelineLayout).id)
460}
461
462fn buffer_binding(b: &wgpu::BufferBinding<'_>) -> p::BufferBinding {
463    p::BufferBinding { buffer: bufh(b.buffer).id, offset: b.offset, size: b.size.map(|s| s.get()) }
464}
465
466impl DeviceInterface for SgDevice {
467    fn features(&self) -> wgpu::Features {
468        self.0.features
469    }
470
471    fn limits(&self) -> wgpu::Limits {
472        self.0.limits.clone()
473    }
474
475    fn adapter_info(&self) -> wgpu::AdapterInfo {
476        self.0.info.clone()
477    }
478
479    fn create_shader_module(&self, desc: wgpu::ShaderModuleDescriptor<'_>, checks: wgpu::ShaderRuntimeChecks) -> DispatchShaderModule {
480        let wgsl = match desc.source {
481            wgpu::ShaderSource::Wgsl(s) => s.into_owned(),
482            _ => panic!("sg-gpu: only WGSL shaders are supported"),
483        };
484        let runtime_checks = checks.bounds_checks;
485        DispatchShaderModule::custom(SgShaderModule(H::new(call_id(Req::CreateShaderModule { label: lbl(desc.label), wgsl, runtime_checks }))))
486    }
487
488    unsafe fn create_shader_module_passthrough(&self, _desc: &wgpu::ShaderModuleDescriptorPassthrough<'_>) -> DispatchShaderModule {
489        panic!("sg-gpu: passthrough shaders are not allowed")
490    }
491
492    fn create_bind_group_layout(&self, desc: &wgpu::BindGroupLayoutDescriptor<'_>) -> DispatchBindGroupLayout {
493        let id = call_id(Req::CreateBindGroupLayout { label: lbl(desc.label), entries: desc.entries.to_vec() });
494        DispatchBindGroupLayout::custom(SgBindGroupLayout(H::new(id)))
495    }
496
497    fn create_bind_group(&self, desc: &wgpu::BindGroupDescriptor<'_>) -> DispatchBindGroup {
498        use wgpu::BindingResource as B;
499        let entries = desc
500            .entries
501            .iter()
502            .map(|e| p::BindGroupEntry {
503                binding: e.binding,
504                resource: match &e.resource {
505                    B::Buffer(b) => p::Resource::Buffer(buffer_binding(b)),
506                    B::BufferArray(v) => p::Resource::BufferArray(v.iter().map(buffer_binding).collect()),
507                    B::Sampler(s) => p::Resource::Sampler(idof!(s, SgSampler).id),
508                    B::SamplerArray(v) => p::Resource::SamplerArray(v.iter().map(|s| idof!(s, SgSampler).id).collect()),
509                    B::TextureView(t) => p::Resource::TextureView(idof!(t, SgTextureView).id),
510                    B::TextureViewArray(v) => p::Resource::TextureViewArray(v.iter().map(|s| idof!(s, SgTextureView).id).collect()),
511                    B::AccelerationStructure(t) => p::Resource::Tlas(idof!(t, SgTlas).id),
512                    B::AccelerationStructureArray(v) => p::Resource::TlasArray(v.iter().map(|s| idof!(s, SgTlas).id).collect()),
513                    _ => panic!("sg-gpu: unsupported binding resource"),
514                },
515            })
516            .collect();
517        let id = call_id(Req::CreateBindGroup { label: lbl(desc.label), layout: idof!(desc.layout, SgBindGroupLayout).id, entries });
518        DispatchBindGroup::custom(SgBindGroup(H::new(id)))
519    }
520
521    fn create_pipeline_layout(&self, desc: &wgpu::PipelineLayoutDescriptor<'_>) -> DispatchPipelineLayout {
522        let bgls = desc.bind_group_layouts.iter().map(|l| l.map_or(0, |l| idof!(l, SgBindGroupLayout).id)).collect();
523        let id = call_id(Req::CreatePipelineLayout { label: lbl(desc.label), bind_group_layouts: bgls, immediate_size: desc.immediate_size });
524        DispatchPipelineLayout::custom(SgPipelineLayout(H::new(id)))
525    }
526
527    fn create_render_pipeline(&self, desc: &wgpu::RenderPipelineDescriptor<'_>) -> DispatchRenderPipeline {
528        let d = p::RenderPipelineDesc {
529            label: lbl(desc.label),
530            layout: layout_id(desc.layout),
531            vertex: stage(desc.vertex.module, desc.vertex.entry_point, &desc.vertex.compilation_options),
532            buffers: desc
533                .vertex
534                .buffers
535                .iter()
536                .map(|b| b.as_ref().map(|b| p::VertexBufferLayout { array_stride: b.array_stride, step_mode: b.step_mode, attributes: b.attributes.to_vec() }))
537                .collect(),
538            primitive: desc.primitive,
539            depth_stencil: desc.depth_stencil.clone(),
540            multisample: desc.multisample,
541            fragment: desc.fragment.as_ref().map(|f| (stage(f.module, f.entry_point, &f.compilation_options), f.targets.to_vec())),
542            multiview_mask: desc.multiview_mask,
543        };
544        DispatchRenderPipeline::custom(SgRenderPipeline(H::new(call_id(Req::CreateRenderPipeline(d)))))
545    }
546
547    fn create_mesh_pipeline(&self, desc: &wgpu::MeshPipelineDescriptor<'_>) -> DispatchRenderPipeline {
548        let d = p::MeshPipelineDesc {
549            label: lbl(desc.label),
550            layout: layout_id(desc.layout),
551            task: desc.task.as_ref().map(|t| stage(t.module, t.entry_point, &t.compilation_options)),
552            mesh: stage(desc.mesh.module, desc.mesh.entry_point, &desc.mesh.compilation_options),
553            primitive: desc.primitive,
554            depth_stencil: desc.depth_stencil.clone(),
555            multisample: desc.multisample,
556            fragment: desc.fragment.as_ref().map(|f| (stage(f.module, f.entry_point, &f.compilation_options), f.targets.to_vec())),
557            multiview: desc.multiview,
558        };
559        DispatchRenderPipeline::custom(SgRenderPipeline(H::new(call_id(Req::CreateMeshPipeline(d)))))
560    }
561
562    fn create_compute_pipeline(&self, desc: &wgpu::ComputePipelineDescriptor<'_>) -> DispatchComputePipeline {
563        let d = p::ComputePipelineDesc { label: lbl(desc.label), layout: layout_id(desc.layout), stage: stage(desc.module, desc.entry_point, &desc.compilation_options) };
564        DispatchComputePipeline::custom(SgComputePipeline(H::new(call_id(Req::CreateComputePipeline(d)))))
565    }
566
567    unsafe fn create_pipeline_cache(&self, _desc: &wgpu::PipelineCacheDescriptor<'_>) -> DispatchPipelineCache {
568        DispatchPipelineCache::custom(SgPipelineCache)
569    }
570
571    fn create_buffer(&self, desc: &wgpu::BufferDescriptor<'_>) -> DispatchBuffer {
572        let id = call_id(Req::CreateBuffer { label: lbl(desc.label), size: desc.size, usage: desc.usage.bits(), mapped_at_creation: desc.mapped_at_creation });
573        let st = Rc::new(BufState {
574            mapped: RefCell::new(desc.mapped_at_creation.then_some((true, 0, desc.size))),
575            fresh: Cell::new(desc.mapped_at_creation),
576        });
577        DispatchBuffer::custom(SgBuffer { h: H::new(id), st })
578    }
579
580    fn create_texture(&self, desc: &wgpu::TextureDescriptor<'_>) -> DispatchTexture {
581        let d = desc.map_label_and_view_formats(|l| lbl(*l), |v| v.to_vec());
582        DispatchTexture::custom(SgTexture(H::new(call_id(Req::CreateTexture(d)))))
583    }
584
585    fn create_external_texture(&self, _desc: &wgpu::ExternalTextureDescriptor<'_>, _planes: &[&wgpu::TextureView]) -> DispatchExternalTexture {
586        panic!("sg-gpu: external textures are not supported")
587    }
588
589    fn create_blas(&self, desc: &wgpu::CreateBlasDescriptor<'_>, sizes: wgpu::BlasGeometrySizeDescriptors) -> (Option<u64>, DispatchBlas) {
590        let r = call(Req::CreateBlas { desc: desc.map_label(|l| lbl(*l)), sizes });
591        let Reply::Blas { id, handle } = r else { panic!("sg-gpu: unexpected reply {r:?}") };
592        (handle, DispatchBlas::custom(SgBlas(H::new(id))))
593    }
594
595    fn create_tlas(&self, desc: &wgpu::CreateTlasDescriptor<'_>) -> DispatchTlas {
596        DispatchTlas::custom(SgTlas(H::new(call_id(Req::CreateTlas(desc.map_label(|l| lbl(*l)))))))
597    }
598
599    fn create_sampler(&self, desc: &wgpu::SamplerDescriptor<'_>) -> DispatchSampler {
600        DispatchSampler::custom(SgSampler(H::new(call_id(Req::CreateSampler(desc.map_label(|l| lbl(*l)))))))
601    }
602
603    fn create_query_set(&self, desc: &wgpu::QuerySetDescriptor<'_>) -> DispatchQuerySet {
604        DispatchQuerySet::custom(SgQuerySet(H::new(call_id(Req::CreateQuerySet(desc.map_label(|l| lbl(*l)))))))
605    }
606
607    fn create_command_encoder(&self, desc: &wgpu::CommandEncoderDescriptor<'_>) -> DispatchCommandEncoder {
608        DispatchCommandEncoder::custom(SgCommandEncoder { label: lbl(desc.label), rec: Rc::new(RefCell::new(Recording::default())) })
609    }
610
611    fn create_render_bundle_encoder(&self, desc: &wgpu::RenderBundleEncoderDescriptor<'_>) -> DispatchRenderBundleEncoder {
612        DispatchRenderBundleEncoder::custom(SgRenderBundleEncoder {
613            desc: p::RenderBundleEncoderDesc {
614                label: lbl(desc.label),
615                color_formats: desc.color_formats.to_vec(),
616                depth_stencil: desc.depth_stencil,
617                sample_count: desc.sample_count,
618                multiview: desc.multiview,
619            },
620            cmds: Vec::new(),
621            keep: Vec::new(),
622        })
623    }
624
625    fn set_device_lost_callback(&self, cb: BoxDeviceLostCallback) {
626        ST.with(|s| s.borrow_mut().lost = Some(cb));
627    }
628
629    fn on_uncaptured_error(&self, handler: Arc<dyn wgpu::UncapturedErrorHandler>) {
630        ST.with(|s| s.borrow_mut().uncaptured = Some(handler));
631    }
632
633    fn push_error_scope(&self, filter: wgpu::ErrorFilter) -> u32 {
634        let f = match filter {
635            wgpu::ErrorFilter::OutOfMemory => p::error_kind::OUT_OF_MEMORY,
636            wgpu::ErrorFilter::Validation => p::error_kind::VALIDATION,
637            wgpu::ErrorFilter::Internal => p::error_kind::INTERNAL,
638        };
639        call(Req::PushErrorScope { filter: f });
640        ST.with(|s| {
641            let mut s = s.borrow_mut();
642            s.scope_depth += 1;
643            s.scope_depth - 1
644        })
645    }
646
647    fn pop_error_scope(&self, _index: u32) -> Pin<Box<dyn PopErrorScopeFuture>> {
648        let e = match call(Req::PopErrorScope) {
649            Reply::Error(e) => e,
650            r => panic!("sg-gpu: unexpected reply {r:?}"),
651        };
652        ST.with(|s| s.borrow_mut().scope_depth -= 1);
653        Box::pin(std::future::ready(e.map(to_error)))
654    }
655
656    unsafe fn start_graphics_debugger_capture(&self) {}
657    unsafe fn stop_graphics_debugger_capture(&self) {}
658
659    fn poll(&self, poll_type: wgt::PollType<u64>) -> Result<wgpu::PollStatus, wgpu::PollError> {
660        let wait = !matches!(poll_type, wgt::PollType::Poll);
661        poll_events(wait);
662        Ok(if wait { wgpu::PollStatus::WaitSucceeded } else { wgpu::PollStatus::QueueEmpty })
663    }
664
665    fn get_internal_counters(&self) -> wgpu::InternalCounters {
666        Default::default()
667    }
668
669    fn generate_allocator_report(&self) -> Option<wgpu::AllocatorReport> {
670        None
671    }
672
673    fn destroy(&self) {}
674}
675
676// ------------------------------------------------------------------ queue
677
678#[derive(Debug)]
679pub struct SgQueue(#[allow(dead_code)] Rc<H>);
680
681#[derive(Debug)]
682pub struct SgStaging(Vec<u8>);
683
684impl QueueWriteBufferInterface for SgStaging {
685    fn len(&self) -> usize {
686        self.0.len()
687    }
688    unsafe fn write_slice(&mut self) -> wgpu::WriteOnly<'_, [u8]> {
689        wgpu::WriteOnly::from(&mut self.0[..])
690    }
691}
692
693impl QueueInterface for SgQueue {
694    fn write_buffer(&self, buffer: &DispatchBuffer, offset: wgpu::BufferAddress, data: &[u8]) {
695        flush_drops();
696        let b = buffer.as_custom::<SgBuffer>().expect("sg-gpu: foreign buffer");
697        unsafe { sys::gpu_write_buffer(b.h.id, offset, data.as_ptr(), data.len()) }
698    }
699
700    fn create_staging_buffer(&self, size: wgpu::BufferSize) -> Option<DispatchQueueWriteBuffer> {
701        Some(DispatchQueueWriteBuffer::custom(SgStaging(vec![0; size.get() as usize])))
702    }
703
704    fn validate_write_buffer(&self, _buffer: &DispatchBuffer, _offset: wgpu::BufferAddress, _size: wgpu::BufferSize) -> Option<()> {
705        Some(())
706    }
707
708    fn write_staging_buffer(&self, buffer: &DispatchBuffer, offset: wgpu::BufferAddress, staging: &DispatchQueueWriteBuffer) {
709        let s = staging.as_custom::<SgStaging>().expect("sg-gpu: foreign staging buffer");
710        self.write_buffer(buffer, offset, &s.0);
711    }
712
713    fn write_texture(&self, texture: wgpu::TexelCopyTextureInfo<'_>, data: &[u8], layout: wgpu::TexelCopyBufferLayout, size: wgpu::Extent3d) {
714        flush_drops();
715        let d = p::WriteTexture {
716            texture: p::TexelCopyTextureInfo { texture: idof!(texture.texture, SgTexture).id, mip_level: texture.mip_level, origin: texture.origin, aspect: texture.aspect },
717            layout,
718            size,
719        };
720        let db = p::encode(&d);
721        unsafe { sys::gpu_write_texture(db.as_ptr(), db.len(), data.as_ptr(), data.len()) }
722    }
723
724    fn submit(&self, command_buffers: &mut dyn Iterator<Item = DispatchCommandBuffer>) -> u64 {
725        let cbs: Vec<DispatchCommandBuffer> = command_buffers.collect();
726        let ids = cbs
727            .iter()
728            .map(|c| {
729                let h = &c.as_custom::<SgCommandBuffer>().expect("sg-gpu: foreign command buffer").0;
730                h.owned.set(false); // consumed by the host
731                h.id
732            })
733            .collect();
734        match call(Req::Submit { command_buffers: ids }) {
735            Reply::U64(i) => i,
736            r => panic!("sg-gpu: unexpected reply {r:?}"),
737        }
738    }
739
740    fn get_timestamp_period(&self) -> f32 {
741        1.0
742    }
743
744    fn on_submitted_work_done(&self, callback: BoxSubmittedWorkDoneCallback) {
745        let t = token();
746        ST.with(|s| s.borrow_mut().done_cbs.insert(t, callback));
747        call(Req::OnSubmittedWorkDone { token: t });
748    }
749
750    fn compact_blas(&self, blas: &DispatchBlas) -> (Option<u64>, DispatchBlas) {
751        (None, blas.clone())
752    }
753
754    fn present(&self, detail: &DispatchSurfaceOutputDetail) {
755        let d = detail.as_custom::<SgSurfaceDetail>().expect("sg-gpu: foreign surface texture");
756        call(Req::SurfacePresent { texture: d.0 });
757    }
758}
759
760// ------------------------------------------------------------------ buffers & textures
761
762#[derive(Debug)]
763struct BufState {
764    /// (write, start, end) while mapped
765    mapped: RefCell<Option<(bool, u64, u64)>>,
766    /// mapped_at_creation and not yet viewed: contents are known to be zero
767    fresh: Cell<bool>,
768}
769
770#[derive(Debug)]
771pub struct SgBuffer {
772    h: Rc<H>,
773    st: Rc<BufState>,
774}
775
776#[derive(Debug)]
777pub struct SgMappedRange {
778    buffer: Id,
779    offset: u64,
780    data: Vec<u8>,
781    write: bool,
782}
783
784impl BufferMappedRangeInterface for SgMappedRange {
785    fn len(&self) -> usize {
786        self.data.len()
787    }
788    unsafe fn read_slice(&self) -> &[u8] {
789        &self.data
790    }
791    unsafe fn write_slice(&mut self) -> wgpu::WriteOnly<'_, [u8]> {
792        wgpu::WriteOnly::from(&mut self.data[..])
793    }
794}
795
796impl Drop for SgMappedRange {
797    fn drop(&mut self) {
798        if self.write && !self.data.is_empty() {
799            unsafe { sys::gpu_mapped_write(self.buffer, self.offset, self.data.as_ptr(), self.data.len()) }
800        }
801    }
802}
803
804impl BufferInterface for SgBuffer {
805    fn map_async(&self, mode: wgpu::MapMode, range: Range<wgpu::BufferAddress>, callback: BufferMapCallback) {
806        let t = token();
807        let write = mode == wgpu::MapMode::Write;
808        ST.with(|s| s.borrow_mut().map_cbs.insert(t, (self.st.clone(), write, range.start, range.end, callback)));
809        call(Req::BufferMapAsync { buffer: self.h.id, write, start: range.start, end: range.end, token: t });
810    }
811
812    fn get_mapped_range(&self, sub: Range<wgpu::BufferAddress>) -> Result<DispatchBufferMappedRange, wgpu::MapRangeError> {
813        let m = *self.st.mapped.borrow();
814        let len = sub.end.saturating_sub(sub.start) as usize;
815        let range = match m {
816            Some((write, start, end)) if sub.start >= start && sub.end <= end && len > 0 => {
817                let mut data = vec![0u8; len];
818                if !self.st.fresh.get() {
819                    flush_drops();
820                    unsafe { sys::gpu_mapped_read(self.h.id, sub.start, data.as_mut_ptr(), len) }
821                }
822                SgMappedRange { buffer: self.h.id, offset: sub.start, data, write }
823            }
824            // Not mapped (or out of range): wgpu's own map-context check rejects the view.
825            _ => SgMappedRange { buffer: self.h.id, offset: 0, data: vec![0u8; len.min(1 << 20)], write: false },
826        };
827        Ok(DispatchBufferMappedRange::custom(range))
828    }
829
830    fn unmap(&self) {
831        self.st.fresh.set(false);
832        *self.st.mapped.borrow_mut() = None;
833        call(Req::BufferUnmap { buffer: self.h.id });
834    }
835
836    fn destroy(&self) {
837        call(Req::BufferDestroy { buffer: self.h.id });
838    }
839}
840
841#[derive(Debug)]
842pub struct SgTexture(Rc<H>);
843
844impl TextureInterface for SgTexture {
845    fn create_view(&self, desc: &wgpu::TextureViewDescriptor<'_>) -> DispatchTextureView {
846        let id = call_id(Req::CreateTextureView { texture: self.0.id, desc: desc.map_label(|l| lbl(*l)) });
847        DispatchTextureView::custom(SgTextureView(H::new(id)))
848    }
849
850    fn destroy(&self) {
851        call(Req::TextureDestroy { texture: self.0.id });
852    }
853}
854
855// ------------------------------------------------------------------ surface
856
857#[derive(Debug)]
858pub struct SgSurface;
859
860#[derive(Debug)]
861pub struct SgSurfaceDetail(Id);
862
863impl SurfaceOutputDetailInterface for SgSurfaceDetail {
864    fn texture_discard(&self) {
865        call(Req::SurfaceDiscard { texture: self.0 });
866    }
867    fn texture_release(&self) {}
868}
869
870impl SurfaceInterface for SgSurface {
871    fn get_capabilities(&self, _adapter: &DispatchAdapter) -> wgpu::SurfaceCapabilities {
872        match call(Req::SurfaceCaps) {
873            Reply::SurfaceCaps(c) => wgpu::SurfaceCapabilities {
874                formats: c.formats,
875                present_modes: c.present_modes,
876                alpha_modes: c.alpha_modes,
877                usages: wgpu::TextureUsages::from_bits_truncate(c.usages),
878                format_capabilities: c
879                    .format_capabilities
880                    .iter()
881                    .map(|(format, bits)| wgpu::SurfaceFormatCapabilities { format: *format, color_spaces: wgpu::SurfaceColorSpaces::from_bits_truncate(*bits) })
882                    .collect(),
883            },
884            r => panic!("sg-gpu: unexpected reply {r:?}"),
885        }
886    }
887
888    fn configure(&self, _device: &DispatchDevice, config: &wgpu::SurfaceConfiguration) {
889        call(Req::SurfaceConfigure(config.clone()));
890    }
891
892    fn get_current_texture(&self) -> (Option<DispatchTexture>, wgpu::SurfaceStatus, DispatchSurfaceOutputDetail) {
893        let r = call(Req::SurfaceAcquire);
894        let Reply::Surface { texture, status, .. } = r else { panic!("sg-gpu: unexpected reply {r:?}") };
895        let st = match status {
896            p::surface_status::GOOD => wgpu::SurfaceStatus::Good,
897            p::surface_status::SUBOPTIMAL => wgpu::SurfaceStatus::Suboptimal,
898            p::surface_status::TIMEOUT => wgpu::SurfaceStatus::Timeout,
899            p::surface_status::OUTDATED => wgpu::SurfaceStatus::Outdated,
900            p::surface_status::OCCLUDED => wgpu::SurfaceStatus::Occluded,
901            _ => wgpu::SurfaceStatus::Lost,
902        };
903        let tex = (texture != 0).then(|| {
904            let h = H::new(texture);
905            h.owned.set(false); // the host owns swapchain textures
906            DispatchTexture::custom(SgTexture(h))
907        });
908        (tex, st, DispatchSurfaceOutputDetail::custom(SgSurfaceDetail(texture)))
909    }
910}
911
912// ------------------------------------------------------------------ command recording
913
914#[derive(Default, Debug)]
915struct Recording {
916    cmds: Vec<p::Cmd>,
917    keep: Vec<Rc<H>>,
918}
919
920#[derive(Debug)]
921pub struct SgCommandEncoder {
922    label: p::Label,
923    rec: Rc<RefCell<Recording>>,
924}
925
926fn tex_info(t: &wgpu::TexelCopyTextureInfo<'_>, keep: &mut Vec<Rc<H>>) -> p::TexelCopyTextureInfo {
927    let h = idof!(t.texture, SgTexture);
928    p::TexelCopyTextureInfo { texture: keep_h(keep, &h), mip_level: t.mip_level, origin: t.origin, aspect: t.aspect }
929}
930
931fn buf_info(b: &wgpu::TexelCopyBufferInfo<'_>, keep: &mut Vec<Rc<H>>) -> p::TexelCopyBufferInfo {
932    let h = bufh(b.buffer);
933    p::TexelCopyBufferInfo { buffer: keep_h(keep, &h), layout: b.layout }
934}
935
936fn dbuf(d: &DispatchBuffer, keep: &mut Vec<Rc<H>>) -> Id {
937    let h = d.as_custom::<SgBuffer>().expect("sg-gpu: foreign buffer").h.clone();
938    keep_h(keep, &h)
939}
940
941fn dqs(d: &DispatchQuerySet, keep: &mut Vec<Rc<H>>) -> Id {
942    let h = d.as_custom::<SgQuerySet>().expect("sg-gpu: foreign query set").0.clone();
943    keep_h(keep, &h)
944}
945
946impl CommandEncoderInterface for SgCommandEncoder {
947    fn copy_buffer_to_buffer(&self, src: &DispatchBuffer, src_offset: wgpu::BufferAddress, dst: &DispatchBuffer, dst_offset: wgpu::BufferAddress, size: Option<wgpu::BufferAddress>) {
948        let mut r = self.rec.borrow_mut();
949        let (s, d) = (dbuf(src, &mut r.keep), dbuf(dst, &mut r.keep));
950        r.cmds.push(p::Cmd::CopyBufferToBuffer { src: s, src_offset, dst: d, dst_offset, size });
951    }
952
953    fn copy_buffer_to_texture(&self, source: wgpu::TexelCopyBufferInfo<'_>, destination: wgpu::TexelCopyTextureInfo<'_>, copy_size: wgpu::Extent3d) {
954        let mut r = self.rec.borrow_mut();
955        let (s, d) = (buf_info(&source, &mut r.keep), tex_info(&destination, &mut r.keep));
956        r.cmds.push(p::Cmd::CopyBufferToTexture { src: s, dst: d, size: copy_size });
957    }
958
959    fn copy_texture_to_buffer(&self, source: wgpu::TexelCopyTextureInfo<'_>, destination: wgpu::TexelCopyBufferInfo<'_>, copy_size: wgpu::Extent3d) {
960        let mut r = self.rec.borrow_mut();
961        let (s, d) = (tex_info(&source, &mut r.keep), buf_info(&destination, &mut r.keep));
962        r.cmds.push(p::Cmd::CopyTextureToBuffer { src: s, dst: d, size: copy_size });
963    }
964
965    fn copy_texture_to_texture(&self, source: wgpu::TexelCopyTextureInfo<'_>, destination: wgpu::TexelCopyTextureInfo<'_>, copy_size: wgpu::Extent3d) {
966        let mut r = self.rec.borrow_mut();
967        let (s, d) = (tex_info(&source, &mut r.keep), tex_info(&destination, &mut r.keep));
968        r.cmds.push(p::Cmd::CopyTextureToTexture { src: s, dst: d, size: copy_size });
969    }
970
971    fn begin_compute_pass(&self, desc: &wgpu::ComputePassDescriptor<'_>) -> DispatchComputePass {
972        let mut keep = Vec::new();
973        let tw = desc.timestamp_writes.as_ref().map(|t| p::TimestampWrites {
974            query_set: keep_h(&mut keep, &idof!(t.query_set, SgQuerySet)),
975            beginning: t.beginning_of_pass_write_index,
976            end: t.end_of_pass_write_index,
977        });
978        DispatchComputePass::custom(SgComputePass { label: lbl(desc.label), tw, cmds: Vec::new(), keep, rec: self.rec.clone() })
979    }
980
981    fn begin_render_pass(&self, desc: &wgpu::RenderPassDescriptor<'_>) -> DispatchRenderPass {
982        let mut keep = Vec::new();
983        let color_attachments = desc
984            .color_attachments
985            .iter()
986            .map(|a| {
987                a.as_ref().map(|a| p::ColorAttachment {
988                    view: keep_h(&mut keep, &idof!(a.view, SgTextureView)),
989                    depth_slice: a.depth_slice,
990                    resolve_target: a.resolve_target.map_or(0, |r| keep_h(&mut keep, &idof!(r, SgTextureView))),
991                    ops: a.ops,
992                })
993            })
994            .collect();
995        let depth_stencil = desc.depth_stencil_attachment.as_ref().map(|a| p::DepthStencilAttachment {
996            view: keep_h(&mut keep, &idof!(a.view, SgTextureView)),
997            depth_ops: a.depth_ops,
998            stencil_ops: a.stencil_ops,
999        });
1000        let timestamp_writes = desc.timestamp_writes.as_ref().map(|t| p::TimestampWrites {
1001            query_set: keep_h(&mut keep, &idof!(t.query_set, SgQuerySet)),
1002            beginning: t.beginning_of_pass_write_index,
1003            end: t.end_of_pass_write_index,
1004        });
1005        let occlusion_query_set = desc.occlusion_query_set.map_or(0, |q| keep_h(&mut keep, &idof!(q, SgQuerySet)));
1006        let d = p::RenderPassDesc { label: lbl(desc.label), color_attachments, depth_stencil, timestamp_writes, occlusion_query_set, multiview_mask: desc.multiview_mask };
1007        DispatchRenderPass::custom(SgRenderPass { desc: Some(d), cmds: Vec::new(), keep, rec: self.rec.clone() })
1008    }
1009
1010    fn finish(&mut self) -> DispatchCommandBuffer {
1011        let rec = std::mem::take(&mut *self.rec.borrow_mut());
1012        let id = call_id(Req::CreateCommandBuffer { label: self.label.clone(), cmds: rec.cmds });
1013        drop(rec.keep); // the host command buffer now holds its own references
1014        DispatchCommandBuffer::custom(SgCommandBuffer(H::new(id)))
1015    }
1016
1017    fn clear_texture(&self, texture: &DispatchTexture, range: &wgpu::ImageSubresourceRange) {
1018        let mut r = self.rec.borrow_mut();
1019        let h = texture.as_custom::<SgTexture>().expect("sg-gpu: foreign texture").0.clone();
1020        let t = keep_h(&mut r.keep, &h);
1021        r.cmds.push(p::Cmd::ClearTexture { texture: t, range: *range });
1022    }
1023
1024    fn clear_buffer(&self, buffer: &DispatchBuffer, offset: wgpu::BufferAddress, size: Option<wgpu::BufferAddress>) {
1025        let mut r = self.rec.borrow_mut();
1026        let b = dbuf(buffer, &mut r.keep);
1027        r.cmds.push(p::Cmd::ClearBuffer { buffer: b, offset, size });
1028    }
1029
1030    fn insert_debug_marker(&self, label: &str) {
1031        self.rec.borrow_mut().cmds.push(p::Cmd::InsertDebugMarker(label.into()));
1032    }
1033
1034    fn push_debug_group(&self, label: &str) {
1035        self.rec.borrow_mut().cmds.push(p::Cmd::PushDebugGroup(label.into()));
1036    }
1037
1038    fn pop_debug_group(&self) {
1039        self.rec.borrow_mut().cmds.push(p::Cmd::PopDebugGroup);
1040    }
1041
1042    fn write_timestamp(&self, query_set: &DispatchQuerySet, query_index: u32) {
1043        let mut r = self.rec.borrow_mut();
1044        let q = dqs(query_set, &mut r.keep);
1045        r.cmds.push(p::Cmd::WriteTimestamp { query_set: q, index: query_index });
1046    }
1047
1048    fn resolve_query_set(&self, query_set: &DispatchQuerySet, first_query: u32, query_count: u32, destination: &DispatchBuffer, destination_offset: wgpu::BufferAddress) {
1049        let mut r = self.rec.borrow_mut();
1050        let q = dqs(query_set, &mut r.keep);
1051        let d = dbuf(destination, &mut r.keep);
1052        r.cmds.push(p::Cmd::ResolveQuerySet { query_set: q, first: first_query, count: query_count, dst: d, dst_offset: destination_offset });
1053    }
1054
1055    fn mark_acceleration_structures_built<'a>(&self, _blas: &mut dyn Iterator<Item = &'a wgpu::Blas>, _tlas: &mut dyn Iterator<Item = &'a wgpu::Tlas>) {}
1056
1057    fn build_acceleration_structures<'a>(&self, blas: &mut dyn Iterator<Item = &'a wgpu::BlasBuildEntry<'a>>, tlas: &mut dyn Iterator<Item = &'a wgpu::Tlas>) {
1058        let mut r = self.rec.borrow_mut();
1059        let keep = &mut r.keep;
1060        let mut bl = Vec::new();
1061        for e in blas {
1062            let b = keep_h(keep, &idof!(e.blas, SgBlas));
1063            let tris = match &e.geometry {
1064                wgpu::BlasGeometries::TriangleGeometries(g) => g
1065                    .iter()
1066                    .map(|t| p::BlasTriangles {
1067                        size: t.size.clone(),
1068                        vertex_buffer: keep_h(keep, &bufh(t.vertex_buffer)),
1069                        first_vertex: t.first_vertex,
1070                        vertex_stride: t.vertex_stride,
1071                        index_buffer: t.index_buffer.map_or(0, |b| keep_h(keep, &bufh(b))),
1072                        first_index: t.first_index,
1073                        transform_buffer: t.transform_buffer.map_or(0, |b| keep_h(keep, &bufh(b))),
1074                        transform_buffer_offset: t.transform_buffer_offset,
1075                    })
1076                    .collect(),
1077                _ => panic!("sg-gpu: AABB BLAS geometry is not supported"),
1078            };
1079            bl.push(p::BlasBuild { blas: b, triangles: tris });
1080        }
1081        // The instances as the game set them (`tlas[i] = Some(TlasInstance::new(..))`), up to the last used slot.
1082        let tl = tlas
1083            .map(|t| {
1084                let id = keep_h(keep, &idof!(t, SgTlas));
1085                let all = t.get();
1086                let used = all.iter().rposition(|i| i.is_some()).map_or(0, |i| i + 1);
1087                let instances = all[..used]
1088                    .iter()
1089                    .map(|i| {
1090                        i.as_ref().map(|i| {
1091                            let b = i.blas_as_custom::<SgBlas>().expect("sg-gpu: foreign blas").0.clone();
1092                            p::TlasInstance { blas: keep_h(keep, &b), transform: i.transform, custom_data: i.custom_data, mask: i.mask }
1093                        })
1094                    })
1095                    .collect();
1096                p::TlasBuild { tlas: id, instances }
1097            })
1098            .collect();
1099        r.cmds.push(p::Cmd::BuildAccelerationStructures { blas: bl, tlas: tl });
1100    }
1101
1102    fn transition_resources<'a>(
1103        &mut self,
1104        _b: &mut dyn Iterator<Item = wgt::BufferTransition<&'a DispatchBuffer>>,
1105        _t: &mut dyn Iterator<Item = wgt::TextureTransition<&'a DispatchTexture>>,
1106    ) {
1107    }
1108}
1109
1110/// Per-frame inputs for the host's frame pass (DLSS5-NR when the user enabled it): call once
1111/// per frame before presenting. `motion` is an `Rg32Float` texture of the surface's size
1112/// (`COPY_SRC`) of per-pixel offsets, in pixels, to the previous frame's position (see
1113/// `sg_abi::gpu::FrameControls`). Returns a `sg_abi::gpu::frame_controls` status.
1114pub fn frame_controls(motion: Option<&wgpu::Texture>, reset: bool, style: u8) -> u64 {
1115    let motion = motion.map(|t| t.as_custom::<SgTexture>().expect("sg-gpu: foreign texture").0.id);
1116    match call(Req::FrameControls(p::FrameControls { motion, reset, style })) {
1117        Reply::U64(status) => status,
1118        r => panic!("sg-gpu: unexpected reply {r:?}"),
1119    }
1120}
1121
1122/// Sends one raw sg-gpu request and returns the host's reply. For tests and tools that need to
1123/// speak the wire protocol directly (the sandbox tests send hostile requests with it); games use wgpu.
1124pub fn raw_request(req: Req) -> Reply {
1125    call(req)
1126}
1127
1128
1129#[derive(Debug)]
1130pub struct SgComputePass {
1131    label: p::Label,
1132    tw: Option<p::TimestampWrites>,
1133    cmds: Vec<p::ComputeCmd>,
1134    keep: Vec<Rc<H>>,
1135    rec: Rc<RefCell<Recording>>,
1136}
1137
1138impl Drop for SgComputePass {
1139    fn drop(&mut self) {
1140        let mut r = self.rec.borrow_mut();
1141        r.keep.append(&mut self.keep);
1142        r.cmds.push(p::Cmd::ComputePass { label: self.label.take(), timestamp_writes: self.tw.take(), cmds: std::mem::take(&mut self.cmds) });
1143    }
1144}
1145
1146impl ComputePassInterface for SgComputePass {
1147    fn set_pipeline(&mut self, pipeline: &DispatchComputePipeline) {
1148        let h = pipeline.as_custom::<SgComputePipeline>().expect("sg-gpu: foreign pipeline").0.clone();
1149        let id = keep_h(&mut self.keep, &h);
1150        self.cmds.push(p::ComputeCmd::SetPipeline(id));
1151    }
1152
1153    fn set_bind_group(&mut self, index: u32, bind_group: Option<&DispatchBindGroup>, offsets: &[wgpu::DynamicOffset]) {
1154        let g = bind_group.map_or(0, |b| {
1155            let h = b.as_custom::<SgBindGroup>().expect("sg-gpu: foreign bind group").0.clone();
1156            keep_h(&mut self.keep, &h)
1157        });
1158        self.cmds.push(p::ComputeCmd::SetBindGroup { index, group: g, offsets: offsets.to_vec() });
1159    }
1160
1161    fn set_immediates(&mut self, offset: u32, data: &[u8]) {
1162        self.cmds.push(p::ComputeCmd::SetImmediates { offset, data: data.to_vec() });
1163    }
1164
1165    fn insert_debug_marker(&mut self, label: &str) {
1166        self.cmds.push(p::ComputeCmd::InsertDebugMarker(label.into()));
1167    }
1168
1169    fn push_debug_group(&mut self, group_label: &str) {
1170        self.cmds.push(p::ComputeCmd::PushDebugGroup(group_label.into()));
1171    }
1172
1173    fn pop_debug_group(&mut self) {
1174        self.cmds.push(p::ComputeCmd::PopDebugGroup);
1175    }
1176
1177    fn write_timestamp(&mut self, query_set: &DispatchQuerySet, query_index: u32) {
1178        let q = dqs(query_set, &mut self.keep);
1179        self.cmds.push(p::ComputeCmd::WriteTimestamp { query_set: q, index: query_index });
1180    }
1181
1182    fn begin_pipeline_statistics_query(&mut self, query_set: &DispatchQuerySet, query_index: u32) {
1183        let q = dqs(query_set, &mut self.keep);
1184        self.cmds.push(p::ComputeCmd::BeginPipelineStatistics { query_set: q, index: query_index });
1185    }
1186
1187    fn end_pipeline_statistics_query(&mut self) {
1188        self.cmds.push(p::ComputeCmd::EndPipelineStatistics);
1189    }
1190
1191    fn dispatch_workgroups(&mut self, x: u32, y: u32, z: u32) {
1192        self.cmds.push(p::ComputeCmd::Dispatch([x, y, z]));
1193    }
1194
1195    fn dispatch_workgroups_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress) {
1196        let b = dbuf(indirect_buffer, &mut self.keep);
1197        self.cmds.push(p::ComputeCmd::DispatchIndirect { buffer: b, offset: indirect_offset });
1198    }
1199
1200    fn transition_resources<'a>(
1201        &mut self,
1202        _b: &mut dyn Iterator<Item = wgt::BufferTransition<&'a DispatchBuffer>>,
1203        _t: &mut dyn Iterator<Item = wgt::TextureTransition<&'a DispatchTextureView>>,
1204    ) {
1205    }
1206}
1207
1208/// Shared by render passes and bundle encoders.
1209trait RenderRec {
1210    fn push(&mut self, c: p::RenderCmd);
1211    fn keep(&mut self) -> &mut Vec<Rc<H>>;
1212
1213    fn r_set_pipeline(&mut self, pipeline: &DispatchRenderPipeline) {
1214        let h = pipeline.as_custom::<SgRenderPipeline>().expect("sg-gpu: foreign pipeline").0.clone();
1215        let id = keep_h(self.keep(), &h);
1216        self.push(p::RenderCmd::SetPipeline(id));
1217    }
1218    fn r_set_bind_group(&mut self, index: u32, bind_group: Option<&DispatchBindGroup>, offsets: &[wgpu::DynamicOffset]) {
1219        let g = bind_group.map_or(0, |b| {
1220            let h = b.as_custom::<SgBindGroup>().expect("sg-gpu: foreign bind group").0.clone();
1221            keep_h(self.keep(), &h)
1222        });
1223        self.push(p::RenderCmd::SetBindGroup { index, group: g, offsets: offsets.to_vec() });
1224    }
1225    fn r_set_index_buffer(&mut self, buffer: &DispatchBuffer, format: wgpu::IndexFormat, offset: wgpu::BufferAddress, size: Option<wgpu::BufferSize>) {
1226        let b = dbuf(buffer, self.keep());
1227        self.push(p::RenderCmd::SetIndexBuffer { buffer: b, format, offset, size: size.map(|s| s.get()) });
1228    }
1229    fn r_set_vertex_buffer(&mut self, slot: u32, buffer: Option<&DispatchBuffer>, offset: wgpu::BufferAddress, size: Option<wgpu::BufferSize>) {
1230        let b = buffer.map_or(0, |b| dbuf(b, self.keep()));
1231        self.push(p::RenderCmd::SetVertexBuffer { slot, buffer: b, offset, size: size.map(|s| s.get()) });
1232    }
1233    fn r_draw_indirect(&mut self, buffer: &DispatchBuffer, offset: wgpu::BufferAddress, indexed: bool) {
1234        let b = dbuf(buffer, self.keep());
1235        self.push(if indexed { p::RenderCmd::DrawIndexedIndirect { buffer: b, offset } } else { p::RenderCmd::DrawIndirect { buffer: b, offset } });
1236    }
1237}
1238
1239#[derive(Debug)]
1240pub struct SgRenderPass {
1241    desc: Option<p::RenderPassDesc>,
1242    cmds: Vec<p::RenderCmd>,
1243    keep: Vec<Rc<H>>,
1244    rec: Rc<RefCell<Recording>>,
1245}
1246
1247impl Drop for SgRenderPass {
1248    fn drop(&mut self) {
1249        let mut r = self.rec.borrow_mut();
1250        r.keep.append(&mut self.keep);
1251        r.cmds.push(p::Cmd::RenderPass { desc: self.desc.take().unwrap(), cmds: std::mem::take(&mut self.cmds) });
1252    }
1253}
1254
1255impl RenderRec for SgRenderPass {
1256    fn push(&mut self, c: p::RenderCmd) {
1257        self.cmds.push(c);
1258    }
1259    fn keep(&mut self) -> &mut Vec<Rc<H>> {
1260        &mut self.keep
1261    }
1262}
1263
1264impl RenderPassInterface for SgRenderPass {
1265    fn set_pipeline(&mut self, pipeline: &DispatchRenderPipeline) {
1266        self.r_set_pipeline(pipeline)
1267    }
1268    fn set_bind_group(&mut self, index: u32, bind_group: Option<&DispatchBindGroup>, offsets: &[wgpu::DynamicOffset]) {
1269        self.r_set_bind_group(index, bind_group, offsets)
1270    }
1271    fn set_index_buffer(&mut self, buffer: &DispatchBuffer, index_format: wgpu::IndexFormat, offset: wgpu::BufferAddress, size: Option<wgpu::BufferSize>) {
1272        self.r_set_index_buffer(buffer, index_format, offset, size)
1273    }
1274    fn set_vertex_buffer(&mut self, slot: u32, buffer: Option<&DispatchBuffer>, offset: wgpu::BufferAddress, size: Option<wgpu::BufferSize>) {
1275        self.r_set_vertex_buffer(slot, buffer, offset, size)
1276    }
1277    fn set_immediates(&mut self, offset: u32, data: &[u8]) {
1278        self.cmds.push(p::RenderCmd::SetImmediates { offset, data: data.to_vec() });
1279    }
1280    fn set_blend_constant(&mut self, color: wgpu::Color) {
1281        self.cmds.push(p::RenderCmd::SetBlendConstant(color));
1282    }
1283    fn set_scissor_rect(&mut self, x: u32, y: u32, width: u32, height: u32) {
1284        self.cmds.push(p::RenderCmd::SetScissorRect([x, y, width, height]));
1285    }
1286    fn set_viewport(&mut self, x: f32, y: f32, width: f32, height: f32, min_depth: f32, max_depth: f32) {
1287        self.cmds.push(p::RenderCmd::SetViewport([x, y, width, height, min_depth, max_depth]));
1288    }
1289    fn set_stencil_reference(&mut self, reference: u32) {
1290        self.cmds.push(p::RenderCmd::SetStencilReference(reference));
1291    }
1292    fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
1293        self.cmds.push(p::RenderCmd::Draw { vertices: (vertices.start, vertices.end), instances: (instances.start, instances.end) });
1294    }
1295    fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
1296        self.cmds.push(p::RenderCmd::DrawIndexed { indices: (indices.start, indices.end), base_vertex, instances: (instances.start, instances.end) });
1297    }
1298    fn draw_mesh_tasks(&mut self, x: u32, y: u32, z: u32) {
1299        self.cmds.push(p::RenderCmd::DrawMeshTasks([x, y, z]));
1300    }
1301    fn draw_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress) {
1302        self.r_draw_indirect(indirect_buffer, indirect_offset, false)
1303    }
1304    fn draw_indexed_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress) {
1305        self.r_draw_indirect(indirect_buffer, indirect_offset, true)
1306    }
1307    fn draw_mesh_tasks_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress) {
1308        let b = dbuf(indirect_buffer, &mut self.keep);
1309        self.cmds.push(p::RenderCmd::DrawMeshTasksIndirect { buffer: b, offset: indirect_offset });
1310    }
1311    fn multi_draw_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress, count: u32) {
1312        let b = dbuf(indirect_buffer, &mut self.keep);
1313        self.cmds.push(p::RenderCmd::MultiDrawIndirect { buffer: b, offset: indirect_offset, count });
1314    }
1315    fn multi_draw_indexed_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress, count: u32) {
1316        let b = dbuf(indirect_buffer, &mut self.keep);
1317        self.cmds.push(p::RenderCmd::MultiDrawIndexedIndirect { buffer: b, offset: indirect_offset, count });
1318    }
1319    fn multi_draw_indirect_count(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress, count_buffer: &DispatchBuffer, count_buffer_offset: wgpu::BufferAddress, max_count: u32) {
1320        let b = dbuf(indirect_buffer, &mut self.keep);
1321        let c = dbuf(count_buffer, &mut self.keep);
1322        self.cmds.push(p::RenderCmd::MultiDrawIndirectCount { buffer: b, offset: indirect_offset, count_buffer: c, count_offset: count_buffer_offset, max_count });
1323    }
1324    fn multi_draw_mesh_tasks_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress, count: u32) {
1325        let b = dbuf(indirect_buffer, &mut self.keep);
1326        self.cmds.push(p::RenderCmd::MultiDrawMeshTasksIndirect { buffer: b, offset: indirect_offset, count });
1327    }
1328    fn multi_draw_indexed_indirect_count(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress, count_buffer: &DispatchBuffer, count_buffer_offset: wgpu::BufferAddress, max_count: u32) {
1329        let b = dbuf(indirect_buffer, &mut self.keep);
1330        let c = dbuf(count_buffer, &mut self.keep);
1331        self.cmds.push(p::RenderCmd::MultiDrawIndexedIndirectCount { buffer: b, offset: indirect_offset, count_buffer: c, count_offset: count_buffer_offset, max_count });
1332    }
1333    fn multi_draw_mesh_tasks_indirect_count(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress, count_buffer: &DispatchBuffer, count_buffer_offset: wgpu::BufferAddress, max_count: u32) {
1334        let b = dbuf(indirect_buffer, &mut self.keep);
1335        let c = dbuf(count_buffer, &mut self.keep);
1336        self.cmds.push(p::RenderCmd::MultiDrawMeshTasksIndirectCount { buffer: b, offset: indirect_offset, count_buffer: c, count_offset: count_buffer_offset, max_count });
1337    }
1338    fn insert_debug_marker(&mut self, label: &str) {
1339        self.cmds.push(p::RenderCmd::InsertDebugMarker(label.into()));
1340    }
1341    fn push_debug_group(&mut self, group_label: &str) {
1342        self.cmds.push(p::RenderCmd::PushDebugGroup(group_label.into()));
1343    }
1344    fn pop_debug_group(&mut self) {
1345        self.cmds.push(p::RenderCmd::PopDebugGroup);
1346    }
1347    fn write_timestamp(&mut self, query_set: &DispatchQuerySet, query_index: u32) {
1348        let q = dqs(query_set, &mut self.keep);
1349        self.cmds.push(p::RenderCmd::WriteTimestamp { query_set: q, index: query_index });
1350    }
1351    fn begin_occlusion_query(&mut self, query_index: u32) {
1352        self.cmds.push(p::RenderCmd::BeginOcclusionQuery(query_index));
1353    }
1354    fn end_occlusion_query(&mut self) {
1355        self.cmds.push(p::RenderCmd::EndOcclusionQuery);
1356    }
1357    fn begin_pipeline_statistics_query(&mut self, query_set: &DispatchQuerySet, query_index: u32) {
1358        let q = dqs(query_set, &mut self.keep);
1359        self.cmds.push(p::RenderCmd::BeginPipelineStatistics { query_set: q, index: query_index });
1360    }
1361    fn end_pipeline_statistics_query(&mut self) {
1362        self.cmds.push(p::RenderCmd::EndPipelineStatistics);
1363    }
1364    fn execute_bundles(&mut self, render_bundles: &mut dyn Iterator<Item = &DispatchRenderBundle>) {
1365        let ids = render_bundles
1366            .map(|b| {
1367                let h = b.as_custom::<SgRenderBundle>().expect("sg-gpu: foreign bundle").0.clone();
1368                keep_h(&mut self.keep, &h)
1369            })
1370            .collect();
1371        self.cmds.push(p::RenderCmd::ExecuteBundles(ids));
1372    }
1373}
1374
1375#[derive(Debug)]
1376pub struct SgRenderBundleEncoder {
1377    desc: p::RenderBundleEncoderDesc,
1378    cmds: Vec<p::RenderCmd>,
1379    keep: Vec<Rc<H>>,
1380}
1381
1382impl RenderRec for SgRenderBundleEncoder {
1383    fn push(&mut self, c: p::RenderCmd) {
1384        self.cmds.push(c);
1385    }
1386    fn keep(&mut self) -> &mut Vec<Rc<H>> {
1387        &mut self.keep
1388    }
1389}
1390
1391impl RenderBundleEncoderInterface for SgRenderBundleEncoder {
1392    fn set_pipeline(&mut self, pipeline: &DispatchRenderPipeline) {
1393        self.r_set_pipeline(pipeline)
1394    }
1395    fn set_bind_group(&mut self, index: u32, bind_group: Option<&DispatchBindGroup>, offsets: &[wgpu::DynamicOffset]) {
1396        self.r_set_bind_group(index, bind_group, offsets)
1397    }
1398    fn set_index_buffer(&mut self, buffer: &DispatchBuffer, index_format: wgpu::IndexFormat, offset: wgpu::BufferAddress, size: Option<wgpu::BufferSize>) {
1399        self.r_set_index_buffer(buffer, index_format, offset, size)
1400    }
1401    fn set_vertex_buffer(&mut self, slot: u32, buffer: Option<&DispatchBuffer>, offset: wgpu::BufferAddress, size: Option<wgpu::BufferSize>) {
1402        self.r_set_vertex_buffer(slot, buffer, offset, size)
1403    }
1404    fn set_immediates(&mut self, offset: u32, data: &[u8]) {
1405        self.cmds.push(p::RenderCmd::SetImmediates { offset, data: data.to_vec() });
1406    }
1407    fn draw(&mut self, vertices: Range<u32>, instances: Range<u32>) {
1408        self.cmds.push(p::RenderCmd::Draw { vertices: (vertices.start, vertices.end), instances: (instances.start, instances.end) });
1409    }
1410    fn draw_indexed(&mut self, indices: Range<u32>, base_vertex: i32, instances: Range<u32>) {
1411        self.cmds.push(p::RenderCmd::DrawIndexed { indices: (indices.start, indices.end), base_vertex, instances: (instances.start, instances.end) });
1412    }
1413    fn draw_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress) {
1414        self.r_draw_indirect(indirect_buffer, indirect_offset, false)
1415    }
1416    fn draw_indexed_indirect(&mut self, indirect_buffer: &DispatchBuffer, indirect_offset: wgpu::BufferAddress) {
1417        self.r_draw_indirect(indirect_buffer, indirect_offset, true)
1418    }
1419    fn finish(self, desc: &wgpu::RenderBundleDescriptor<'_>) -> DispatchRenderBundle {
1420        let id = call_id(Req::CreateRenderBundle { desc: self.desc.clone(), label: lbl(desc.label), cmds: self.cmds.clone() });
1421        DispatchRenderBundle::custom(SgRenderBundle(H::new(id)))
1422    }
1423    fn finish_boxed(self: Box<Self>, desc: &wgpu::RenderBundleDescriptor<'_>) -> DispatchRenderBundle {
1424        (*self).finish(desc)
1425    }
1426}
1427
1428#[allow(dead_code)]
1429fn _assert_ready() -> Pin<Box<dyn Future<Output = ()>>> {
1430    ready(())
1431}