v0.4.0 2de4e1d

readback.go RawHistoryFolder

1//go:build linux
2
3package vkdevice
4
5import (
6	"fmt"
7	"image"
8	"math"
9	"unsafe"
10
11	"github.com/bnema/purego-vulkan/vulkan"
12)
13
14// Readback is only allocated when NEFERGUI_DEBUG_DIR is set. Its transfer copy is
15// recorded after drawing and before the acquire semaphore signals; mapping
16// waits for the submission fence, never for compositor release.
17type Readback struct {
18	device        *Device
19	Buffer        vulkan.Buffer
20	Memory        vulkan.DeviceMemory
21	width, height int32
22}
23
24func (d *Device) NewReadback(width, height int32) (r *Readback, err error) {
25	if width <= 0 || height <= 0 || uint64(width)*uint64(height) > math.MaxInt/4 {
26		return nil, fmt.Errorf("invalid readback dimensions %dx%d", width, height)
27	}
28	r = &Readback{device: d, width: width, height: height}
29	defer func() {
30		if err != nil {
31			r.Close()
32		}
33	}()
34	ci := vulkan.BufferCreateInfo{SType: vulkan.StructureTypeBufferCreateInfo, Size: vulkan.DeviceSize(width) * vulkan.DeviceSize(height) * 4, Usage: vulkan.BufferUsageTransferDstBit, SharingMode: vulkan.SharingModeExclusive}
35	if err = vulkan.Check(d.Dispatch.CreateBuffer(d.Logical, &ci, nil, &r.Buffer)); err != nil {
36		return nil, err
37	}
38	var req vulkan.MemoryRequirements
39	d.Dispatch.GetBufferMemoryRequirements(d.Logical, r.Buffer, &req)
40	var memory vulkan.PhysicalDeviceMemoryProperties
41	d.InstanceDispatch.GetPhysicalDeviceMemoryProperties(d.Physical, &memory)
42	// Prefer host-cached memory: CPU reads from uncached (write-combined)
43	// memory are orders of magnitude slower for window-sized images.
44	index := uint32(math.MaxUint32)
45	coherent := vulkan.MemoryPropertyFlags(vulkan.MemoryPropertyHostVisibleBit | vulkan.MemoryPropertyHostCoherentBit)
46	for _, want := range []vulkan.MemoryPropertyFlags{coherent | vulkan.MemoryPropertyHostCachedBit, coherent} {
47		for i := uint32(0); i < memory.MemoryTypeCount && index == math.MaxUint32; i++ {
48			if req.MemoryTypeBits&(1<<i) != 0 && memory.MemoryTypes[i].PropertyFlags&want == want {
49				index = i
50			}
51		}
52	}
53	if index == math.MaxUint32 {
54		return nil, fmt.Errorf("no host-coherent readback memory type")
55	}
56	allocate := vulkan.MemoryAllocateInfo{SType: vulkan.StructureTypeMemoryAllocateInfo, AllocationSize: req.Size, MemoryTypeIndex: index}
57	if err = vulkan.Check(d.Dispatch.AllocateMemory(d.Logical, &allocate, nil, &r.Memory)); err != nil {
58		return nil, err
59	}
60	if err = vulkan.Check(d.Dispatch.BindBufferMemory(d.Logical, r.Buffer, r.Memory, 0)); err != nil {
61		return nil, err
62	}
63	return r, nil
64}
65func (r *Readback) record(cmd vulkan.CommandBuffer, img vulkan.Image) {
66	copy := vulkan.BufferImageCopy{ImageSubresource: vulkan.ImageSubresourceLayers{AspectMask: vulkan.ImageAspectColorBit, LayerCount: 1}, ImageExtent: vulkan.Extent3D{Width: uint32(r.width), Height: uint32(r.height), Depth: 1}}
67	r.device.Dispatch.CmdCopyImageToBuffer(cmd, img, vulkan.ImageLayoutTransferSrcOptimal, r.Buffer, 1, &copy)
68}
69
70// Copy returns the completed readback as an owned image whose pixels are still
71// in B8G8R8A8 order. It performs one bulk copy; call SwizzleBGRA off the state
72// owner before encoding.
73func (r *Readback) Copy() (*image.NRGBA, error) {
74	var ptr unsafe.Pointer
75	size := vulkan.DeviceSize(r.width) * vulkan.DeviceSize(r.height) * 4
76	if err := vulkan.Check(r.device.Dispatch.MapMemory(r.device.Logical, r.Memory, 0, size, 0, &ptr)); err != nil {
77		return nil, err
78	}
79	defer r.device.Dispatch.UnmapMemory(r.device.Logical, r.Memory)
80	out := image.NewNRGBA(image.Rect(0, 0, int(r.width), int(r.height)))
81	copy(out.Pix, unsafe.Slice((*byte)(ptr), int(size)))
82	return out, nil
83}
84
85// SwizzleBGRA converts B8G8R8A8 pixels to RGBA in place.
86func SwizzleBGRA(img *image.NRGBA) {
87	p := img.Pix
88	for i := 0; i+3 < len(p); i += 4 {
89		p[i], p[i+2] = p[i+2], p[i]
90	}
91}
92func (r *Readback) Close() {
93	if r == nil || r.device == nil {
94		return
95	}
96	if r.Buffer != 0 {
97		r.device.Dispatch.DestroyBuffer(r.device.Logical, r.Buffer, nil)
98		r.Buffer = 0
99	}
100	if r.Memory != 0 {
101		r.device.Dispatch.FreeMemory(r.device.Logical, r.Memory, nil)
102		r.Memory = 0
103	}
104}

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