v0.7.3 bcc9a45

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

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