v0.1.0 47aaa0f

image.go RawHistoryFolder

1//go:build linux
2
3package vkdevice
4
5import (
6	"fmt"
7	"math"
8	"unsafe"
9
10	"github.com/bnema/purego-vulkan/vulkan"
11	"github.com/bnema/wlturbo/protocol/core"
12	"github.com/bnema/wlturbo/protocol/linuxdmabuf"
13	"golang.org/x/sys/unix"
14)
15
16// Image owns a Vulkan image, dedicated exportable memory and its Wayland buffer.
17// Close must only be called after both wl_buffer.release and the compositor's
18// release timeline point, or for an image never attached to a surface.
19type Image struct {
20	Image          vulkan.Image
21	Memory         vulkan.DeviceMemory
22	Buffer         *core.Buffer
23	Modifier       uint64
24	Offset, Stride uint32
25	device         *Device
26}
27
28func (d *Device) AllocateImage(width, height int32, selected Modifier, dmabuf *linuxdmabuf.LinuxDmabuf) (img *Image, err error) {
29	if width <= 0 || height <= 0 || dmabuf == nil {
30		return nil, fmt.Errorf("invalid DMA-BUF image dimensions or manager")
31	}
32	img = &Image{device: d}
33	defer func() {
34		if err != nil {
35			img.Close()
36			img = nil
37		}
38	}()
39	modifier := selected.Value
40	list := vulkan.ImageDrmFormatModifierListCreateInfoEXT{SType: vulkan.StructureTypeImageDRMFormatModifierListCreateInfoEXT, DrmFormatModifierCount: 1, DrmFormatModifiers: &modifier}
41	external := vulkan.ExternalMemoryImageCreateInfo{SType: vulkan.StructureTypeExternalMemoryImageCreateInfo, Next: unsafe.Pointer(&list), HandleTypes: vulkan.ExternalMemoryHandleTypeDMABUFBitEXT}
42	ci := vulkan.ImageCreateInfo{SType: vulkan.StructureTypeImageCreateInfo, Next: unsafe.Pointer(&external), ImageType: vulkan.ImageType2d, Format: vulkan.FormatB8g8r8a8Unorm, Extent: vulkan.Extent3D{Width: uint32(width), Height: uint32(height), Depth: 1}, MipLevels: 1, ArrayLayers: 1, Samples: vulkan.SampleCount1Bit, Tiling: vulkan.ImageTilingDRMFormatModifierEXT, Usage: vulkan.ImageUsageColorAttachmentBit | vulkan.ImageUsageTransferSrcBit, SharingMode: vulkan.SharingModeExclusive, InitialLayout: vulkan.ImageLayoutUndefined}
43	if err = vulkan.Check(d.Dispatch.CreateImage(d.Logical, &ci, nil, &img.Image)); err != nil {
44		return nil, fmt.Errorf("create DRM modifier image: %w", err)
45	}
46	props := vulkan.ImageDrmFormatModifierPropertiesEXT{SType: vulkan.StructureTypeImageDRMFormatModifierPropertiesEXT}
47	if err = vulkan.Check(d.Dispatch.GetImageDrmFormatModifierPropertiesEXT(d.Logical, img.Image, &props)); err != nil {
48		return nil, err
49	}
50	if props.DrmFormatModifier != selected.Value {
51		return nil, fmt.Errorf("selected DRM modifier %#x but Vulkan chose %#x", selected.Value, props.DrmFormatModifier)
52	}
53	img.Modifier = props.DrmFormatModifier
54	sub := vulkan.ImageSubresource{AspectMask: vulkan.ImageAspectMemoryPlane0BitEXT}
55	var layout vulkan.SubresourceLayout
56	d.Dispatch.GetImageSubresourceLayout(d.Logical, img.Image, &sub, &layout)
57	if layout.RowPitch > math.MaxUint32 || layout.Offset > math.MaxUint32 || layout.RowPitch == 0 {
58		return nil, fmt.Errorf("DMA-BUF plane layout not representable: offset %d stride %d", layout.Offset, layout.RowPitch)
59	}
60	img.Offset, img.Stride = uint32(layout.Offset), uint32(layout.RowPitch)
61	reqInfo := vulkan.ImageMemoryRequirementsInfo2{SType: vulkan.StructureTypeImageMemoryRequirementsInfo2, Image: img.Image}
62	reqs := vulkan.MemoryRequirements2{SType: vulkan.StructureTypeMemoryRequirements2}
63	d.Dispatch.GetImageMemoryRequirements2(d.Logical, &reqInfo, &reqs)
64	var mem vulkan.PhysicalDeviceMemoryProperties
65	d.InstanceDispatch.GetPhysicalDeviceMemoryProperties(d.Physical, &mem)
66	memoryIndex := uint32(math.MaxUint32)
67	for i := uint32(0); i < mem.MemoryTypeCount; i++ {
68		if reqs.MemoryRequirements.MemoryTypeBits&(1<<i) != 0 && mem.MemoryTypes[i].PropertyFlags&vulkan.MemoryPropertyDeviceLocalBit != 0 {
69			memoryIndex = i
70			break
71		}
72	}
73	if memoryIndex == math.MaxUint32 {
74		return nil, fmt.Errorf("no device-local DMA-BUF memory type")
75	}
76	dedicated := vulkan.MemoryDedicatedAllocateInfo{SType: vulkan.StructureTypeMemoryDedicatedAllocateInfo, Image: img.Image}
77	export := vulkan.ExportMemoryAllocateInfo{SType: vulkan.StructureTypeExportMemoryAllocateInfo, Next: unsafe.Pointer(&dedicated), HandleTypes: vulkan.ExternalMemoryHandleTypeDMABUFBitEXT}
78	allocate := vulkan.MemoryAllocateInfo{SType: vulkan.StructureTypeMemoryAllocateInfo, Next: unsafe.Pointer(&export), AllocationSize: reqs.MemoryRequirements.Size, MemoryTypeIndex: memoryIndex}
79	if err = vulkan.Check(d.Dispatch.AllocateMemory(d.Logical, &allocate, nil, &img.Memory)); err != nil {
80		return nil, fmt.Errorf("allocate dedicated DMA-BUF memory: %w", err)
81	}
82	bind := vulkan.BindImageMemoryInfo{SType: vulkan.StructureTypeBindImageMemoryInfo, Image: img.Image, Memory: img.Memory}
83	if err = vulkan.Check(d.Dispatch.BindImageMemory2(d.Logical, 1, &bind)); err != nil {
84		return nil, fmt.Errorf("bind exportable image: %w", err)
85	}
86	fdInfo := vulkan.MemoryGetFdInfoKHR{SType: vulkan.StructureTypeMemoryGetFDInfoKHR, Memory: img.Memory, HandleType: vulkan.ExternalMemoryHandleTypeDMABUFBitEXT}
87	fd := int32(-1)
88	if err = vulkan.Check(d.Dispatch.GetMemoryFdKHR(d.Logical, &fdInfo, &fd)); err != nil {
89		return nil, fmt.Errorf("export DMA-BUF: %w", err)
90	}
91	// WLTurbo consumes fd only when its complete Add request succeeds. On a
92	// failed send Vulkan still owns the image and we still own the exported FD.
93	owned := true
94	defer func() {
95		if owned {
96			_ = unix.Close(int(fd))
97		}
98	}()
99	params, e := dmabuf.CreateParams()
100	if e != nil {
101		return nil, e
102	}
103	defer params.Destroy()
104	if err = params.Add(int(fd), 0, img.Offset, img.Stride, uint32(img.Modifier>>32), uint32(img.Modifier)); err != nil {
105		return nil, err
106	}
107	owned = false
108	img.Buffer, err = params.CreateImmed(width, height, selected.DRMFormat, 0)
109	if err != nil {
110		return nil, err
111	}
112	return img, nil
113}
114
115func (img *Image) Close() {
116	if img == nil || img.device == nil {
117		return
118	}
119	if img.Buffer != nil {
120		_ = img.Buffer.Destroy()
121		img.Buffer = nil
122	}
123	if img.Image != 0 {
124		img.device.Dispatch.DestroyImage(img.device.Logical, img.Image, nil)
125		img.Image = 0
126	}
127	if img.Memory != 0 {
128		img.device.Dispatch.FreeMemory(img.device.Logical, img.Memory, nil)
129		img.Memory = 0
130	}
131}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 12

↑↓ moveenter openesc close