-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsurface.go
443 lines (396 loc) · 13.8 KB
/
surface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright (c) 2022, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This is initially adapted from https://github.com/vulkan-go/asche
// Copyright © 2017 Maxim Kupriianov <[email protected]>, under the MIT License
package vgpu
import (
"errors"
"fmt"
vk "github.com/goki/vulkan"
)
// Surface manages the physical device for the visible image
// of a window surface, and the swapchain for presenting images.
type Surface struct {
// pointer to gpu device, for convenience
GPU *GPU
// device for this surface -- each window surface has its own device, configured for that surface
Device Device
// the Render for this Surface, typically from a System
Render *Render
// has the current swapchain image format and dimensions
Format ImageFormat
// ordered list of surface formats to select
DesiredFormats []vk.Format
// number of frames to maintain in the swapchain -- e.g., 2 = double-buffering, 3 = triple-buffering -- initially set to a requested amount, and after Init reflects actual number
NFrames int
// Framebuffers representing the visible Image owned by the Surface -- we iterate through these in rendering subsequent frames
Frames []*Framebuffer
// vulkan handle for surface
Surface vk.Surface `display:"-"`
// vulkan handle for swapchain
Swapchain vk.Swapchain `display:"-"`
// semaphore used internally for waiting on acquisition of next frame
ImageAcquired vk.Semaphore `display:"-"`
// semaphore that surface user can wait on, will be activated when image has been acquired in AcquireNextFrame method
RenderDone vk.Semaphore `display:"-"`
// fence for rendering command running
RenderFence vk.Fence `display:"-"`
// NeedsConfig is whether the surface needs to be configured again without freeing the swapchain.
// This is set internally to allow for correct recovery from sudden minimization events that are
// only detected at the point of swapchain reconfiguration.
NeedsConfig bool
}
// NewSurface returns a new surface initialized for given GPU and vulkan
// Surface handle, obtained from a valid window.
func NewSurface(gp *GPU, vsurf vk.Surface) *Surface {
sf := &Surface{}
sf.Defaults()
sf.Init(gp, vsurf)
return sf
}
func (sf *Surface) Defaults() {
sf.NFrames = 3 // requested, will be updated with actual
sf.Format.Defaults()
sf.Format.Set(1024, 768, vk.FormatR8g8b8a8Srgb)
sf.Format.SetMultisample(4) // good default
sf.DesiredFormats = []vk.Format{
vk.FormatR8g8b8a8Srgb,
vk.FormatB8g8r8a8Srgb,
// vk.FormatR8g8b8a8Unorm, // these def too dark
// vk.FormatB8g8r8a8Unorm,
}
}
// Init initializes the device and all other resources for the surface
// based on the vulkan surface handle which must be obtained from the
// OS-specific window, created first (e.g., via glfw)
func (sf *Surface) Init(gp *GPU, vs vk.Surface) error {
sf.GPU = gp
sf.Surface = vs
// Get queue family properties
var queueCount uint32
vk.GetPhysicalDeviceQueueFamilyProperties(sf.GPU.GPU, &queueCount, nil)
queueProperties := make([]vk.QueueFamilyProperties, queueCount)
vk.GetPhysicalDeviceQueueFamilyProperties(sf.GPU.GPU, &queueCount, queueProperties)
if queueCount == 0 { // probably should try another GPU
return errors.New("vulkan error: no queue families found on GPU 0")
}
// Find a suitable queue family for the target Vulkan mode
// note: this differs from generic Device.FindQueue in
// specifying the surface.
found := false
for i := uint32(0); i < queueCount; i++ {
var supportsPresent vk.Bool32
vk.GetPhysicalDeviceSurfaceSupport(sf.GPU.GPU, i, sf.Surface, &supportsPresent)
if supportsPresent.B() {
sf.Device.QueueIndex = i
found = true
break
}
}
if !found {
err := errors.New("Surface vulkan error: could not found queue with present capabilities")
return err
}
sf.Device.MakeDevice(gp)
sf.ConfigSwapchain()
return nil
}
// ConfigSwapchain configures the swapchain for surface.
// This assumes that all existing items have been destroyed.
// It returns false if the swapchain size is zero.
func (sf *Surface) ConfigSwapchain() bool {
dev := sf.Device.Device
// Read sf.Surface capabilities
var surfaceCapabilities vk.SurfaceCapabilities
ret := vk.GetPhysicalDeviceSurfaceCapabilities(sf.GPU.GPU, sf.Surface, &surfaceCapabilities)
IfPanic(NewError(ret))
surfaceCapabilities.Deref()
// Get available surface pixel formats
var formatCount uint32
vk.GetPhysicalDeviceSurfaceFormats(sf.GPU.GPU, sf.Surface, &formatCount, nil)
formats := make([]vk.SurfaceFormat, formatCount)
vk.GetPhysicalDeviceSurfaceFormats(sf.GPU.GPU, sf.Surface, &formatCount, formats)
// Select a proper surface format
var format vk.SurfaceFormat
if formatCount == 1 {
formats[0].Deref()
if formats[0].Format == vk.FormatUndefined {
format = formats[0]
format.Format = sf.Format.Format
} else {
format = formats[0]
}
} else if formatCount == 0 {
IfPanic(errors.New("vulkan error: surface has no pixel formats"))
} else {
got := false
for _, df := range sf.DesiredFormats {
for _, ft := range formats {
ft.Deref()
if ft.Format == df {
format = ft
got = true
break
}
}
if got {
break
}
}
if !got {
formats[0].Deref()
format = formats[0]
if Debug {
dfs := make([]string, len(sf.DesiredFormats))
for i, df := range sf.DesiredFormats {
dfs[i] = ImageFormatNames[df]
}
fmt.Printf("vgpu.Surface:Init unable to find desired format: %v, using first one: %s\n", dfs, ImageFormatNames[format.Format])
}
}
}
// Setup swapchain parameters
var swapchainSize vk.Extent2D
surfaceCapabilities.CurrentExtent.Deref()
if surfaceCapabilities.CurrentExtent.Width == vk.MaxUint32 {
w, h := sf.Format.Size32()
swapchainSize.Width = w
swapchainSize.Height = h
} else {
swapchainSize = surfaceCapabilities.CurrentExtent
}
if swapchainSize.Width == 0 || swapchainSize.Height == 0 {
return false
}
// The FIFO present mode is guaranteed by the spec to be supported
// and to have no tearing. It's a great default present mode to use.
swapchainPresentMode := vk.PresentModeFifo
// Determine the number of VkImage's to use in the swapchain.
// Ideally, we desire to own 1 image at a time, the rest of the images can either be rendered to and/or
// being queued up for display.
desiredSwapchainImages := uint32(sf.NFrames)
if surfaceCapabilities.MaxImageCount > 0 && desiredSwapchainImages > surfaceCapabilities.MaxImageCount {
// App must settle for fewer images than desired.
desiredSwapchainImages = surfaceCapabilities.MaxImageCount
}
// Figure out a suitable surface transform.
var preTransform vk.SurfaceTransformFlagBits
requiredTransforms := vk.SurfaceTransformIdentityBit
supportedTransforms := surfaceCapabilities.SupportedTransforms
if vk.SurfaceTransformFlagBits(supportedTransforms)&requiredTransforms != 0 {
preTransform = requiredTransforms
} else {
preTransform = surfaceCapabilities.CurrentTransform
}
// Find a supported composite alpha mode - one of these is guaranteed to be set
compositeAlpha := vk.CompositeAlphaOpaqueBit
compositeAlphaFlags := []vk.CompositeAlphaFlagBits{
vk.CompositeAlphaPreMultipliedBit,
vk.CompositeAlphaOpaqueBit, // this only affects blending with other windows in OS
vk.CompositeAlphaPostMultipliedBit,
vk.CompositeAlphaInheritBit,
}
// goti := -1
for i := 0; i < len(compositeAlphaFlags); i++ {
alphaFlags := vk.CompositeAlphaFlags(compositeAlphaFlags[i])
flagSupported := surfaceCapabilities.SupportedCompositeAlpha&alphaFlags != 0
if flagSupported {
// goti = i
compositeAlpha = compositeAlphaFlags[i]
break
}
}
// fmt.Printf("Got alpha: %d\n", goti)
// Create a swapchain
var swapchain vk.Swapchain
oldSwapchain := sf.Swapchain
swci := &vk.SwapchainCreateInfo{
SType: vk.StructureTypeSwapchainCreateInfo,
Surface: sf.Surface,
MinImageCount: desiredSwapchainImages,
ImageFormat: format.Format,
ImageColorSpace: format.ColorSpace,
ImageExtent: vk.Extent2D{
Width: swapchainSize.Width,
Height: swapchainSize.Height,
},
ImageUsage: vk.ImageUsageFlags(vk.ImageUsageColorAttachmentBit),
PreTransform: preTransform,
CompositeAlpha: compositeAlpha,
ImageArrayLayers: 1,
ImageSharingMode: vk.SharingModeExclusive,
PresentMode: swapchainPresentMode,
OldSwapchain: oldSwapchain,
Clipped: vk.True,
}
ret = vk.CreateSwapchain(dev, swci, nil, &swapchain)
IfPanic(NewError(ret))
if oldSwapchain != vk.NullSwapchain {
vk.DestroySwapchain(dev, oldSwapchain, nil)
}
sf.Swapchain = swapchain
sf.Format.Set(int(swapchainSize.Width), int(swapchainSize.Height), format.Format)
var imageCount uint32
ret = vk.GetSwapchainImages(dev, sf.Swapchain, &imageCount, nil)
IfPanic(NewError(ret))
sf.NFrames = int(imageCount)
swapchainImages := make([]vk.Image, imageCount)
ret = vk.GetSwapchainImages(dev, sf.Swapchain, &imageCount, swapchainImages)
IfPanic(NewError(ret))
sf.ImageAcquired = NewSemaphore(dev)
sf.RenderDone = NewSemaphore(dev)
sf.RenderFence = NewFence(dev)
sf.Frames = make([]*Framebuffer, sf.NFrames)
for i := 0; i < sf.NFrames; i++ {
fr := &Framebuffer{}
fr.ConfigSurfaceImage(sf.GPU, dev, sf.Format, swapchainImages[i])
sf.Frames[i] = fr
}
return true
}
// FreeSwapchain frees any existing swawpchain (for ReInit or Destroy)
func (sf *Surface) FreeSwapchain() {
dev := sf.Device.Device
vk.DeviceWaitIdle(dev)
vk.DestroySemaphore(dev, sf.ImageAcquired, nil)
vk.DestroySemaphore(dev, sf.RenderDone, nil)
vk.DestroyFence(dev, sf.RenderFence, nil)
for _, fr := range sf.Frames {
fr.Destroy()
}
sf.Frames = nil
if sf.Swapchain != vk.NullSwapchain {
vk.DestroySwapchain(dev, sf.Swapchain, nil)
sf.Swapchain = vk.NullSwapchain
}
}
// ReConfigSwapchain does a re-initialize of swapchain, freeing existing.
// This must be called when the window is resized.
// It returns false if the swapchain size is zero.
func (sf *Surface) ReConfigSwapchain() bool {
sf.FreeSwapchain()
if !sf.ConfigSwapchain() {
return false
}
sf.Render.SetSize(sf.Format.Size)
sf.ReConfigFrames()
return true
}
// SetRender sets the Render and updates frames accordingly
func (sf *Surface) SetRender(rp *Render) {
sf.Render = rp
for _, fr := range sf.Frames {
fr.ConfigRender(rp)
}
}
// ReConfigFrames re-configures the Famebuffers
// using exiting settings. Assumes ConfigSwapchain has been called.
func (sf *Surface) ReConfigFrames() {
for _, fr := range sf.Frames {
fr.ConfigRender(sf.Render)
}
}
func (sf *Surface) Destroy() {
sf.FreeSwapchain()
if sf.Surface != vk.NullSurface {
vk.DestroySurface(sf.GPU.Instance, sf.Surface, nil)
sf.Surface = vk.NullSurface
}
sf.Device.Destroy()
sf.GPU = nil
}
// AcquireNextImage gets the next frame index to render to.
// It automatically handles any issues with out-of-date swapchain.
// It triggers the ImageAcquired semaphore when image actually acquired.
// Must call SubmitRender with command to launch command contingent
// on that semaphore. It returns false if the swapchain size is zero.
func (sf *Surface) AcquireNextImage() (uint32, bool) {
dev := sf.Device.Device
for {
vk.WaitForFences(dev, 1, []vk.Fence{sf.RenderFence}, vk.True, vk.MaxUint64)
vk.ResetFences(dev, 1, []vk.Fence{sf.RenderFence})
var idx uint32
if sf.NeedsConfig {
// we must skip FreeSwapchain for NeedsConfig
if !sf.ConfigSwapchain() {
if Debug {
fmt.Println("vgpu.Surface.AcquireNextImage: bailing on ConfigSwapchain caused by NeedsConfig (somewhat unexpected)")
}
return idx, false
}
sf.Render.SetSize(sf.Format.Size)
sf.ReConfigFrames()
sf.NeedsConfig = false
if Debug {
fmt.Println("vgpu.Surface.AcquireNextImage: did NeedsConfig update")
}
continue
}
ret := vk.AcquireNextImage(dev, sf.Swapchain, vk.MaxUint64, sf.ImageAcquired, vk.NullFence, &idx)
switch ret {
case vk.ErrorOutOfDate, vk.Suboptimal:
if !sf.ReConfigSwapchain() {
if Debug {
fmt.Println("vgpu.Surface.AcquireNextImage: bailing on zero swapchain size")
}
sf.NeedsConfig = true
return idx, false
}
if Debug {
fmt.Printf("vgpu.Surface.AcquireNextImage: new format: %#v\n", sf.Format)
}
continue // try again
case vk.Success:
return idx, true
default:
IfPanic(NewError(ret))
return idx, true
}
}
}
// SubmitRender submits a rendering command that must have been added
// to the given command buffer, calling CmdEnd on the buffer first.
// This buffer triggers the associated Fence logic to control the
// sequencing of render commands over time.
// The ImageAcquired semaphore before the command is run.
func (sf *Surface) SubmitRender(cmd vk.CommandBuffer) {
CmdEnd(cmd)
ret := vk.QueueSubmit(sf.Device.Queue, 1, []vk.SubmitInfo{{
SType: vk.StructureTypeSubmitInfo,
PWaitDstStageMask: []vk.PipelineStageFlags{
vk.PipelineStageFlags(vk.PipelineStageColorAttachmentOutputBit),
},
WaitSemaphoreCount: 1,
PWaitSemaphores: []vk.Semaphore{sf.ImageAcquired},
CommandBufferCount: 1,
PCommandBuffers: []vk.CommandBuffer{cmd},
SignalSemaphoreCount: 1,
PSignalSemaphores: []vk.Semaphore{sf.RenderDone},
}}, sf.RenderFence)
IfPanic(NewError(ret))
}
// PresentImage waits on the RenderDone semaphore to present the
// rendered image to the surface, for the given frame index,
// as returned by AcquireNextImage.
func (sf *Surface) PresentImage(frameIndex uint32) error {
ret := vk.QueuePresent(sf.Device.Queue, &vk.PresentInfo{
SType: vk.StructureTypePresentInfo,
WaitSemaphoreCount: 1,
PWaitSemaphores: []vk.Semaphore{sf.RenderDone},
SwapchainCount: 1,
PSwapchains: []vk.Swapchain{sf.Swapchain},
PImageIndices: []uint32{frameIndex},
})
switch ret {
case vk.ErrorOutOfDate, vk.Suboptimal:
if Debug {
fmt.Println("vgpu.Surface.PresentImage: did not render due to out of date or suboptimal swapchain")
}
return nil
case vk.Success:
return nil
default:
return NewError(ret)
}
}