1//go:build linux
2
3// Package session is the Wayland-free presentation core: Vulkan rendering into
4// DMA-BUF buffers, DRM syncobj timelines and buffer ownership.
5package session
6
7import (
8 "fmt"
9
10 "github.com/bnema/nefergui/internal/presentation/buffers"
11 "github.com/bnema/nefergui/internal/presentation/vkdevice"
12)
13
14// Slot is the GPU side of one pool buffer. Descriptors are -1 when unset.
15type Slot struct {
16 Buffer *buffers.Buffer
17 Image *vkdevice.Image
18 Frame *vkdevice.Frame
19 First bool
20 Wait *vkdevice.BinaryFence
21 PendingWait *vkdevice.BinaryFence
22 WaitInFlight bool
23 spare *vkdevice.BinaryFence // consumed, unsignaled wait semaphore kept for reuse
24 ReleaseHandle uint32
25 ReleaseFD int // eventfd armed on each commit's release point
26 ReleaseExport int // exported syncobj fd of the release timeline
27 Presented bool // the caller has been told to import this buffer
28}
29
30// beginReady does not change Pool ownership until the previous GPU submission
31// has completed. An unready available buffer is retried on a bounded wakeup.
32func beginReady(pool *buffers.Pool, slots map[uint64]*Slot, ready func(*Slot) (bool, error)) (*buffers.Buffer, *Slot, bool, error) {
33 waiting := false
34 for _, b := range pool.Buffers {
35 if b.Generation != pool.Generation || b.State != buffers.Available {
36 continue
37 }
38 slot := slots[b.ID]
39 if slot == nil {
40 return nil, nil, false, fmt.Errorf("missing slot %d", b.ID)
41 }
42 ok, err := ready(slot)
43 if err != nil {
44 return nil, nil, false, err
45 }
46 if !ok {
47 waiting = true
48 continue
49 }
50 b.State = buffers.Rendering
51 b.WlReleased, b.PointSignaled = false, false
52 return b, slot, false, nil
53 }
54 return nil, nil, waiting, nil
55}
56
57// installReleaseWait holds the new semaphore until the old submission is done.
58// Only the state owner calls it. On an unready fence it leaves Pool Released.
59func installReleaseWait(slot *Slot, next *vkdevice.BinaryFence, ready func() (bool, error)) (bool, error) {
60 if slot.Wait != nil && slot.WaitInFlight {
61 ok, err := ready()
62 if err != nil {
63 return false, err
64 }
65 if !ok {
66 slot.PendingWait = next
67 return false, nil
68 }
69 }
70 if slot.Wait != nil {
71 if slot.WaitInFlight {
72 slot.recycle(slot.Wait) // submission done: the wait was consumed
73 } else {
74 slot.Wait.Close() // never waited on: its imported payload is still signaled
75 }
76 }
77 slot.Wait, slot.PendingWait, slot.WaitInFlight = next, nil, false
78 return true, nil
79}
80
81// recycle keeps a release-wait semaphore for reuse. The caller guarantees the
82// submission that waited on it has completed: a wait operation consumes the
83// temporarily imported payload and leaves the semaphore unsignaled and idle.
84func (s *Slot) recycle(w *vkdevice.BinaryFence) {
85 if s.spare != nil {
86 w.Close()
87 return
88 }
89 s.spare = w
90}
91
92type retiredPipelines struct {
93 generation uint64
94 list *vkdevice.Pipeline
95}