1//go:build linux
2
3package vkdevice
4
5import (
6 "fmt"
7 "math"
8 "unsafe"
9
10 "github.com/bnema/purego-vulkan/vulkan"
11 "golang.org/x/sys/unix"
12)
13
14// Image owns a Vulkan image, dedicated exportable memory and the exported
15// DMA-BUF file descriptor. FD stays owned by the Image and is closed by Close;
16// a transport that consumes descriptors must be handed a dup. The compositor's
17// import keeps its own kernel reference, so Close may run while the compositor
18// still holds the buffer, but the GPU must be done with the image first.
19type Image struct {
20 Image vulkan.Image
21 Memory vulkan.DeviceMemory
22 FD int // exported DMA-BUF, -1 when none
23 Modifier uint64
24 Offset, Stride uint32
25 device *Device
26}
27
28func (d *Device) AllocateImage(width, height int32, selected Modifier) (img *Image, err error) {
29 if width <= 0 || height <= 0 {
30 return nil, fmt.Errorf("invalid DMA-BUF image dimensions")
31 }
32 img = &Image{device: d, FD: -1}
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 img.FD = int(fd)
92 return img, nil
93}
94
95func (img *Image) Close() {
96 if img == nil || img.device == nil {
97 return
98 }
99 if img.FD >= 0 {
100 _ = unix.Close(img.FD)
101 img.FD = -1
102 }
103 if img.Image != 0 {
104 img.device.Dispatch.DestroyImage(img.device.Logical, img.Image, nil)
105 img.Image = 0
106 }
107 if img.Memory != 0 {
108 img.device.Dispatch.FreeMemory(img.device.Logical, img.Memory, nil)
109 img.Memory = 0
110 }
111}