1//go:build linux
2
3package vkdevice
4
5import (
6 "fmt"
7 "image"
8 "image/draw"
9 "math"
10 "reflect"
11 "unsafe"
12
13 "github.com/bnema/purego-vulkan/vulkan"
14)
15
16type gpuImage struct {
17 Image vulkan.Image
18 Memory vulkan.DeviceMemory
19 View vulkan.ImageView
20 Texture ImageTexture
21}
22
23type imageCache struct {
24 device *Device
25 policy *imageCachePolicy
26 Layout vulkan.DescriptorSetLayout
27 Pool vulkan.DescriptorPool
28 Sampler vulkan.Sampler
29 fallback *gpuImage
30 fallbackReady bool
31 sets [imageDescriptorSlots]vulkan.DescriptorSet
32 textures map[*imageEntry]*gpuImage
33 serial uint64
34 done map[uint64]bool
35 // inflight holds submitted frames whose image serial is not settled yet.
36 // resolve polls them so an idle frame slot cannot stall completion.
37 inflight map[*Frame]struct{}
38}
39
40func (c *imageCache) Close() {
41 if c == nil || c.device == nil {
42 return
43 }
44 for _, t := range c.textures {
45 c.destroy(t)
46 }
47 if c.fallback != nil {
48 c.destroy(c.fallback)
49 c.fallback = nil
50 }
51 d := c.device.Dispatch
52 if c.Pool != 0 {
53 d.DestroyDescriptorPool(c.device.Logical, c.Pool, nil)
54 c.Pool = 0
55 }
56 if c.Layout != 0 {
57 d.DestroyDescriptorSetLayout(c.device.Logical, c.Layout, nil)
58 c.Layout = 0
59 }
60 if c.Sampler != 0 {
61 d.DestroySampler(c.device.Logical, c.Sampler, nil)
62 c.Sampler = 0
63 }
64}
65func (c *imageCache) destroy(t *gpuImage) {
66 d := c.device.Dispatch
67 if t.View != 0 {
68 d.DestroyImageView(c.device.Logical, t.View, nil)
69 }
70 if t.Image != 0 {
71 d.DestroyImage(c.device.Logical, t.Image, nil)
72 }
73 if t.Memory != 0 {
74 d.FreeMemory(c.device.Logical, t.Memory, nil)
75 }
76}
77func (c *imageCache) dispose(entries []*imageEntry) {
78 for _, e := range entries {
79 if t := c.textures[e]; t != nil {
80 c.destroy(t)
81 delete(c.textures, e)
82 }
83 }
84}
85func (d *Device) newImageCache() (c *imageCache, err error) {
86 c = &imageCache{device: d, policy: newImageCachePolicy(), textures: make(map[*imageEntry]*gpuImage), done: make(map[uint64]bool), inflight: make(map[*Frame]struct{})}
87 defer func() {
88 if err != nil {
89 c.Close()
90 }
91 }()
92 binding := vulkan.DescriptorSetLayoutBinding{DescriptorType: vulkan.DescriptorTypeCombinedImageSampler, DescriptorCount: 1, StageFlags: vulkan.ShaderStageFragmentBit}
93 li := vulkan.DescriptorSetLayoutCreateInfo{SType: vulkan.StructureTypeDescriptorSetLayoutCreateInfo, BindingCount: 1, Bindings: &binding}
94 if err = vulkan.Check(d.Dispatch.CreateDescriptorSetLayout(d.Logical, &li, nil, &c.Layout)); err != nil {
95 return nil, err
96 }
97 // Allocate the full cache budget once. No FreeDescriptorSets binding is needed.
98 size := vulkan.DescriptorPoolSize{Type: vulkan.DescriptorTypeCombinedImageSampler, DescriptorCount: imageDescriptorSlots}
99 pi := vulkan.DescriptorPoolCreateInfo{SType: vulkan.StructureTypeDescriptorPoolCreateInfo, MaxSets: imageDescriptorSlots, PoolSizeCount: 1, PoolSizes: &size}
100 if err = vulkan.Check(d.Dispatch.CreateDescriptorPool(d.Logical, &pi, nil, &c.Pool)); err != nil {
101 return nil, err
102 }
103 for i := range c.sets {
104 ai := vulkan.DescriptorSetAllocateInfo{SType: vulkan.StructureTypeDescriptorSetAllocateInfo, DescriptorPool: c.Pool, DescriptorSetCount: 1, SetLayouts: &c.Layout}
105 if err = vulkan.Check(d.Dispatch.AllocateDescriptorSets(d.Logical, &ai, &c.sets[i])); err != nil {
106 return nil, err
107 }
108 }
109 sampler := vulkan.SamplerCreateInfo{SType: vulkan.StructureTypeSamplerCreateInfo, MagFilter: vulkan.FilterLinear, MinFilter: vulkan.FilterLinear, MipmapMode: vulkan.SamplerMipmapModeNearest, AddressModeU: vulkan.SamplerAddressModeClampToEdge, AddressModeV: vulkan.SamplerAddressModeClampToEdge, AddressModeW: vulkan.SamplerAddressModeClampToEdge}
110 if err = vulkan.Check(d.Dispatch.CreateSampler(d.Logical, &sampler, nil, &c.Sampler)); err != nil {
111 return nil, err
112 }
113 c.fallback, err = c.create(image.NewNRGBA(image.Rect(0, 0, 1, 1)), 0)
114 if err != nil {
115 return nil, err
116 }
117 return c, nil
118}
119
120// complete releases resources only after their final sampling fence is signaled.
121func (c *imageCache) complete(serial uint64) {
122 if serial == 0 {
123 return
124 }
125 c.done[serial] = true
126 for c.done[c.policy.completed+1] {
127 delete(c.done, c.policy.completed+1)
128 c.policy.completed++
129 }
130 c.dispose(c.policy.collect())
131}
132
133// resolve is invoked after a reusable frame slot has been selected. Upload
134// bytes live in that slot's staging buffer until its fence signals.
135// On error the frame's serial and new entries are released by abort (the
136// caller invokes it for every failure before a successful Submit).
137func (c *imageCache) resolve(f *Frame, batches []Batch) ([]Batch, error) {
138 // Completion is otherwise observed only when a slot is recorded again; a
139 // slot the compositor keeps idle would block the contiguous completed
140 // serial and pin every later texture. Poll without waiting.
141 if err := c.pollInflight((*Frame).Ready); err != nil {
142 return nil, err
143 }
144 c.serial++
145 f.imageSerial = c.serial
146 f.imageCopies = f.imageCopies[:0]
147 c.dispose(c.policy.begin(c.serial, c.policy.completed))
148 result := append([]Batch(nil), batches...)
149 var pixels []byte
150 // Validate the entire frame before creating or changing any GPU resources.
151 // In particular, do not partially record an over-budget frame.
152 seen := make(map[image.Image]bool)
153 count, bytes := 0, 0
154 for _, batch := range result {
155 key := batch.Source
156 if key == nil {
157 continue
158 }
159 if !reflect.TypeOf(key).Comparable() {
160 return nil, fmt.Errorf("image cache: image identity must be comparable")
161 }
162 if seen[key] {
163 continue
164 }
165 seen[key] = true
166 count++
167 b := key.Bounds()
168 if b.Dx() <= 0 || b.Dy() <= 0 || b.Dx() > maxImageDimension || b.Dy() > maxImageDimension {
169 return nil, fmt.Errorf("image cache: invalid dimensions %dx%d (maximum %d)", b.Dx(), b.Dy(), maxImageDimension)
170 }
171 if e := c.policy.entries[key]; e != nil {
172 bytes += e.bytes
173 } else {
174 bytes += b.Dx() * b.Dy() * 4
175 }
176 }
177 if count > maxImageTextures || bytes > maxImageBytes {
178 return nil, fmt.Errorf("image cache: frame exceeds 64 textures/64 MiB budget")
179 }
180 for i := range result {
181 if result[i].Source == nil {
182 continue
183 }
184 key := result[i].Source
185 e, retired, err := c.policy.reserve(key)
186 c.dispose(retired)
187 if err != nil {
188 return nil, err
189 }
190 t := c.textures[e]
191 if t == nil {
192 // Track the entry before creating GPU objects so abort can drop it.
193 f.imageCopies = append(f.imageCopies, imageCopy{entry: e})
194 t, err = c.create(key, e.slot)
195 if err != nil {
196 return nil, err
197 }
198 c.textures[e] = t
199 n := image.NewNRGBA(image.Rect(0, 0, key.Bounds().Dx(), key.Bounds().Dy()))
200 draw.Draw(n, n.Bounds(), key, key.Bounds().Min, draw.Src)
201 f.imageCopies[len(f.imageCopies)-1] = imageCopy{entry: e, texture: t, offset: vulkan.DeviceSize(len(pixels)), width: uint32(n.Rect.Dx()), height: uint32(n.Rect.Dy())}
202 pixels = append(pixels, n.Pix...)
203 }
204 result[i].Texture = &t.Texture
205 }
206 if len(pixels) > 0 {
207 if err := f.stageImages(pixels); err != nil {
208 return nil, err
209 }
210 }
211 return result, nil
212}
213
214func (c *imageCache) pollInflight(readyFn func(*Frame) (bool, error)) error {
215 for g := range c.inflight {
216 ready, err := readyFn(g)
217 if err != nil {
218 return err
219 }
220 if ready {
221 g.settleImages()
222 }
223 }
224 return nil
225}
226
227// abort undoes resolve for a frame that will not be submitted: textures
228// created for it were never uploaded, so they are destroyed and their slots
229// freed at once (no GPU work references them), and its serial is completed so
230// later fences can still release retired textures.
231func (c *imageCache) abort(f *Frame) {
232 for _, cp := range f.imageCopies {
233 if t := c.textures[cp.entry]; t != nil {
234 c.destroy(t)
235 delete(c.textures, cp.entry)
236 }
237 c.policy.drop(cp.entry)
238 }
239 f.imageCopies = f.imageCopies[:0]
240 c.complete(f.imageSerial)
241 f.imageSerial = 0
242}
243
244type imageCopy struct {
245 entry *imageEntry
246 texture *gpuImage
247 offset vulkan.DeviceSize
248 width, height uint32
249}
250
251func (c *imageCache) create(source image.Image, slot int) (t *gpuImage, err error) {
252 d := c.device
253 t = &gpuImage{Texture: ImageTexture{Set: c.sets[slot]}}
254 defer func() {
255 if err != nil {
256 c.destroy(t)
257 }
258 }()
259 b := source.Bounds()
260 ci := vulkan.ImageCreateInfo{SType: vulkan.StructureTypeImageCreateInfo, ImageType: vulkan.ImageType2d, Format: vulkan.FormatR8g8b8a8Unorm, Extent: vulkan.Extent3D{Width: uint32(b.Dx()), Height: uint32(b.Dy()), Depth: 1}, MipLevels: 1, ArrayLayers: 1, Samples: vulkan.SampleCount1Bit, Tiling: vulkan.ImageTilingOptimal, Usage: vulkan.ImageUsageSampledBit | vulkan.ImageUsageTransferDstBit, SharingMode: vulkan.SharingModeExclusive, InitialLayout: vulkan.ImageLayoutUndefined}
261 if err = vulkan.Check(d.Dispatch.CreateImage(d.Logical, &ci, nil, &t.Image)); err != nil {
262 return nil, err
263 }
264 var req vulkan.MemoryRequirements
265 d.Dispatch.GetImageMemoryRequirements(d.Logical, t.Image, &req)
266 var mem vulkan.PhysicalDeviceMemoryProperties
267 d.InstanceDispatch.GetPhysicalDeviceMemoryProperties(d.Physical, &mem)
268 index := uint32(math.MaxUint32)
269 for i := uint32(0); i < mem.MemoryTypeCount; i++ {
270 if req.MemoryTypeBits&(1<<i) != 0 && mem.MemoryTypes[i].PropertyFlags&vulkan.MemoryPropertyDeviceLocalBit != 0 {
271 index = i
272 break
273 }
274 }
275 if index == math.MaxUint32 {
276 return nil, fmt.Errorf("no device-local image memory")
277 }
278 alloc := vulkan.MemoryAllocateInfo{SType: vulkan.StructureTypeMemoryAllocateInfo, AllocationSize: req.Size, MemoryTypeIndex: index}
279 if err = vulkan.Check(d.Dispatch.AllocateMemory(d.Logical, &alloc, nil, &t.Memory)); err != nil {
280 return nil, err
281 }
282 if err = vulkan.Check(d.Dispatch.BindImageMemory(d.Logical, t.Image, t.Memory, 0)); err != nil {
283 return nil, err
284 }
285 view := vulkan.ImageViewCreateInfo{SType: vulkan.StructureTypeImageViewCreateInfo, Image: t.Image, ViewType: vulkan.ImageViewType2d, Format: vulkan.FormatR8g8b8a8Unorm, SubresourceRange: vulkan.ImageSubresourceRange{AspectMask: vulkan.ImageAspectColorBit, LevelCount: 1, LayerCount: 1}}
286 if err = vulkan.Check(d.Dispatch.CreateImageView(d.Logical, &view, nil, &t.View)); err != nil {
287 return nil, err
288 }
289 info := vulkan.DescriptorImageInfo{Sampler: c.Sampler, ImageView: t.View, ImageLayout: vulkan.ImageLayoutShaderReadOnlyOptimal}
290 write := vulkan.WriteDescriptorSet{SType: vulkan.StructureTypeWriteDescriptorSet, DstSet: t.Texture.Set, DescriptorCount: 1, DescriptorType: vulkan.DescriptorTypeCombinedImageSampler, ImageInfo: &info}
291 d.Dispatch.UpdateDescriptorSets(d.Logical, 1, &write, 0, nil)
292 return t, nil
293}
294
295func (f *Frame) stageImages(pixels []byte) error {
296 if f.imageStaging == nil || len(pixels) > f.imageStaging.capacity {
297 if f.imageStaging != nil {
298 f.imageStaging.Close()
299 }
300 f.imageStaging = &instanceBuffer{device: f.device}
301 b := f.imageStaging
302 ci := vulkan.BufferCreateInfo{SType: vulkan.StructureTypeBufferCreateInfo, Size: vulkan.DeviceSize(len(pixels)), Usage: vulkan.BufferUsageTransferSrcBit, SharingMode: vulkan.SharingModeExclusive}
303 if err := vulkan.Check(f.device.Dispatch.CreateBuffer(f.device.Logical, &ci, nil, &b.Buffer)); err != nil {
304 return err
305 }
306 var req vulkan.MemoryRequirements
307 f.device.Dispatch.GetBufferMemoryRequirements(f.device.Logical, b.Buffer, &req)
308 var mem vulkan.PhysicalDeviceMemoryProperties
309 f.device.InstanceDispatch.GetPhysicalDeviceMemoryProperties(f.device.Physical, &mem)
310 want := vulkan.MemoryPropertyFlags(vulkan.MemoryPropertyHostVisibleBit | vulkan.MemoryPropertyHostCoherentBit)
311 index := uint32(math.MaxUint32)
312 for i := uint32(0); i < mem.MemoryTypeCount; i++ {
313 if req.MemoryTypeBits&(1<<i) != 0 && mem.MemoryTypes[i].PropertyFlags&want == want {
314 index = i
315 break
316 }
317 }
318 if index == math.MaxUint32 {
319 return fmt.Errorf("no host-coherent image staging memory")
320 }
321 ai := vulkan.MemoryAllocateInfo{SType: vulkan.StructureTypeMemoryAllocateInfo, AllocationSize: req.Size, MemoryTypeIndex: index}
322 if err := vulkan.Check(f.device.Dispatch.AllocateMemory(f.device.Logical, &ai, nil, &b.Memory)); err != nil {
323 return err
324 }
325 if err := vulkan.Check(f.device.Dispatch.BindBufferMemory(f.device.Logical, b.Buffer, b.Memory, 0)); err != nil {
326 return err
327 }
328 b.capacity = len(pixels)
329 }
330 var ptr unsafe.Pointer
331 if err := vulkan.Check(f.device.Dispatch.MapMemory(f.device.Logical, f.imageStaging.Memory, 0, vulkan.DeviceSize(len(pixels)), 0, &ptr)); err != nil {
332 return err
333 }
334 copy(unsafe.Slice((*byte)(ptr), len(pixels)), pixels)
335 f.device.Dispatch.UnmapMemory(f.device.Logical, f.imageStaging.Memory)
336 return nil
337}
338
339// recordFallback transitions a dedicated, never sampled fallback until the
340// first list submission. The shader statically requires set 1 on every draw.
341func (f *Frame) recordFallback() {
342 c := f.device.images
343 f.fallbackRecorded = true
344 barrier := vulkan.ImageMemoryBarrier2{SType: vulkan.StructureTypeImageMemoryBarrier2, SrcStageMask: vulkan.PipelineStage2None, DstStageMask: vulkan.PipelineStage2FragmentShaderBit, DstAccessMask: vulkan.Access2ShaderSampledReadBit, OldLayout: vulkan.ImageLayoutUndefined, NewLayout: vulkan.ImageLayoutShaderReadOnlyOptimal, SrcQueueFamilyIndex: ^uint32(0), DstQueueFamilyIndex: ^uint32(0), Image: c.fallback.Image, SubresourceRange: vulkan.ImageSubresourceRange{AspectMask: vulkan.ImageAspectColorBit, LevelCount: 1, LayerCount: 1}}
345 dep := vulkan.DependencyInfo{SType: vulkan.StructureTypeDependencyInfo, ImageMemoryBarrierCount: 1, ImageMemoryBarriers: &barrier}
346 f.device.Dispatch.CmdPipelineBarrier2(f.Commands, &dep)
347}
348
349func (f *Frame) recordImages() {
350 d := f.device.Dispatch
351 for _, copyInfo := range f.imageCopies {
352 t := copyInfo.texture
353 barrier := vulkan.ImageMemoryBarrier2{SType: vulkan.StructureTypeImageMemoryBarrier2, SrcStageMask: vulkan.PipelineStage2None, DstStageMask: vulkan.PipelineStage2TransferBit, DstAccessMask: vulkan.Access2TransferWriteBit, OldLayout: vulkan.ImageLayoutUndefined, NewLayout: vulkan.ImageLayoutTransferDstOptimal, SrcQueueFamilyIndex: ^uint32(0), DstQueueFamilyIndex: ^uint32(0), Image: t.Image, SubresourceRange: vulkan.ImageSubresourceRange{AspectMask: vulkan.ImageAspectColorBit, LevelCount: 1, LayerCount: 1}}
354 dep := vulkan.DependencyInfo{SType: vulkan.StructureTypeDependencyInfo, ImageMemoryBarrierCount: 1, ImageMemoryBarriers: &barrier}
355 d.CmdPipelineBarrier2(f.Commands, &dep)
356 region := vulkan.BufferImageCopy{BufferOffset: copyInfo.offset, ImageSubresource: vulkan.ImageSubresourceLayers{AspectMask: vulkan.ImageAspectColorBit, LayerCount: 1}, ImageExtent: vulkan.Extent3D{Width: copyInfo.width, Height: copyInfo.height, Depth: 1}}
357 d.CmdCopyBufferToImage(f.Commands, f.imageStaging.Buffer, t.Image, vulkan.ImageLayoutTransferDstOptimal, 1, ®ion)
358 barrier.OldLayout = vulkan.ImageLayoutTransferDstOptimal
359 barrier.NewLayout = vulkan.ImageLayoutShaderReadOnlyOptimal
360 barrier.SrcStageMask = vulkan.PipelineStage2TransferBit
361 barrier.SrcAccessMask = vulkan.Access2TransferWriteBit
362 barrier.DstStageMask = vulkan.PipelineStage2FragmentShaderBit
363 barrier.DstAccessMask = vulkan.Access2ShaderSampledReadBit
364 d.CmdPipelineBarrier2(f.Commands, &dep)
365 }
366}