-
Notifications
You must be signed in to change notification settings - Fork 4
/
platform.go
386 lines (347 loc) · 12 KB
/
platform.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
package asche
import (
"errors"
"log"
"unsafe"
vk "github.com/vulkan-go/vulkan"
)
type Platform interface {
// MemoryProperties gets the current Vulkan physical device memory properties.
MemoryProperties() vk.PhysicalDeviceMemoryProperties
// PhysicalDeviceProperies gets the current Vulkan physical device properties.
PhysicalDeviceProperies() vk.PhysicalDeviceProperties
// GraphicsQueueFamilyIndex gets the current Vulkan graphics queue family index.
GraphicsQueueFamilyIndex() uint32
// PresentQueueFamilyIndex gets the current Vulkan present queue family index.
PresentQueueFamilyIndex() uint32
// HasSeparatePresentQueue is true when PresentQueueFamilyIndex differs from GraphicsQueueFamilyIndex.
HasSeparatePresentQueue() bool
// GraphicsQueue gets the current Vulkan graphics queue.
GraphicsQueue() vk.Queue
// PresentQueue gets the current Vulkan present queue.
PresentQueue() vk.Queue
// Instance gets the current Vulkan instance.
Instance() vk.Instance
// Device gets the current Vulkan device.
Device() vk.Device
// PhysicalDevice gets the current Vulkan physical device.
PhysicalDevice() vk.PhysicalDevice
// Surface gets the current Vulkan surface.
Surface() vk.Surface
// Destroy is the destructor for the Platform instance.
Destroy()
}
func NewPlatform(app Application) (pFace Platform, err error) {
// defer checkErr(&err)
p := &platform{
basePlatform: basePlatform{
context: &context{
// TODO: make configurable
// defines count of slots allocated in swapchain
frameLag: 3,
},
},
}
p.context.platform = p
// Select instance extensions
requiredInstanceExtensions := safeStrings(app.VulkanInstanceExtensions())
actualInstanceExtensions, err := InstanceExtensions()
orPanic(err)
instanceExtensions, missing := checkExisting(actualInstanceExtensions, requiredInstanceExtensions)
if missing > 0 {
log.Println("vulkan warning: missing", missing, "required instance extensions during init")
}
log.Printf("vulkan: enabling %d instance extensions", len(instanceExtensions))
// Select instance layers
var validationLayers []string
if iface, ok := app.(ApplicationVulkanLayers); ok {
requiredValidationLayers := safeStrings(iface.VulkanLayers())
actualValidationLayers, err := ValidationLayers()
orPanic(err)
validationLayers, missing = checkExisting(actualValidationLayers, requiredValidationLayers)
if missing > 0 {
log.Println("vulkan warning: missing", missing, "required validation layers during init")
}
}
// Create instance
var instance vk.Instance
ret := vk.CreateInstance(&vk.InstanceCreateInfo{
SType: vk.StructureTypeInstanceCreateInfo,
PApplicationInfo: &vk.ApplicationInfo{
SType: vk.StructureTypeApplicationInfo,
ApiVersion: uint32(app.VulkanAPIVersion()),
ApplicationVersion: uint32(app.VulkanAppVersion()),
PApplicationName: safeString(app.VulkanAppName()),
PEngineName: "vulkango.com\x00",
},
EnabledExtensionCount: uint32(len(instanceExtensions)),
PpEnabledExtensionNames: instanceExtensions,
EnabledLayerCount: uint32(len(validationLayers)),
PpEnabledLayerNames: validationLayers,
}, nil, &instance)
orPanic(NewError(ret))
p.instance = instance
vk.InitInstance(instance)
if app.VulkanDebug() {
// Register a debug callback
ret := vk.CreateDebugReportCallback(instance, &vk.DebugReportCallbackCreateInfo{
SType: vk.StructureTypeDebugReportCallbackCreateInfo,
Flags: vk.DebugReportFlags(vk.DebugReportErrorBit | vk.DebugReportWarningBit),
PfnCallback: dbgCallbackFunc,
}, nil, &p.debugCallback)
orPanic(NewError(ret))
log.Println("vulkan: DebugReportCallback enabled by application")
}
// Find a suitable GPU
var gpuCount uint32
ret = vk.EnumeratePhysicalDevices(p.instance, &gpuCount, nil)
orPanic(NewError(ret))
if gpuCount == 0 {
return nil, errors.New("vulkan error: no GPU devices found")
}
gpus := make([]vk.PhysicalDevice, gpuCount)
ret = vk.EnumeratePhysicalDevices(p.instance, &gpuCount, gpus)
orPanic(NewError(ret))
// get the first one, multiple GPUs not supported yet
p.gpu = gpus[0]
vk.GetPhysicalDeviceProperties(p.gpu, &p.gpuProperties)
p.gpuProperties.Deref()
vk.GetPhysicalDeviceMemoryProperties(p.gpu, &p.memoryProperties)
p.memoryProperties.Deref()
// Select device extensions
requiredDeviceExtensions := safeStrings(app.VulkanDeviceExtensions())
actualDeviceExtensions, err := DeviceExtensions(p.gpu)
orPanic(err)
deviceExtensions, missing := checkExisting(actualDeviceExtensions, requiredDeviceExtensions)
if missing > 0 {
log.Println("vulkan warning: missing", missing, "required device extensions during init")
}
log.Printf("vulkan: enabling %d device extensions", len(deviceExtensions))
// Make sure the surface is here if required
mode := app.VulkanMode()
if mode.Has(VulkanPresent) { // so, a surface is required and provided
p.surface = app.VulkanSurface(p.instance)
if p.surface == vk.NullSurface {
return nil, errors.New("vulkan error: surface required but not provided")
}
}
// Get queue family properties
var queueCount uint32
vk.GetPhysicalDeviceQueueFamilyProperties(p.gpu, &queueCount, nil)
queueProperties := make([]vk.QueueFamilyProperties, queueCount)
vk.GetPhysicalDeviceQueueFamilyProperties(p.gpu, &queueCount, queueProperties)
if queueCount == 0 { // probably should try another GPU
return nil, errors.New("vulkan error: no queue families found on GPU 0")
}
// Find a suitable queue family for the target Vulkan mode
var graphicsFound bool
var presentFound bool
var separateQueue bool
for i := uint32(0); i < queueCount; i++ {
var (
required vk.QueueFlags
supportsPresent vk.Bool32
needsPresent bool
)
if graphicsFound {
// looking for separate present queue
separateQueue = true
vk.GetPhysicalDeviceSurfaceSupport(p.gpu, i, p.surface, &supportsPresent)
if supportsPresent.B() {
p.presentQueueIndex = i
presentFound = true
break
}
}
if mode.Has(VulkanCompute) {
required |= vk.QueueFlags(vk.QueueComputeBit)
}
if mode.Has(VulkanGraphics) {
required |= vk.QueueFlags(vk.QueueGraphicsBit)
}
if mode.Has(VulkanPresent) {
needsPresent = true
vk.GetPhysicalDeviceSurfaceSupport(p.gpu, i, p.surface, &supportsPresent)
}
queueProperties[i].Deref()
if queueProperties[i].QueueFlags&required != 0 {
if !needsPresent || (needsPresent && supportsPresent.B()) {
p.graphicsQueueIndex = i
graphicsFound = true
break
} else if needsPresent {
p.graphicsQueueIndex = i
graphicsFound = true
// need present, but this one doesn't support
// continue lookup
}
}
}
if separateQueue && !presentFound {
err := errors.New("vulkan error: could not found separate queue with present capabilities")
return nil, err
}
if !graphicsFound {
err := errors.New("vulkan error: could not find a suitable queue family for the target Vulkan mode")
return nil, err
}
// Create a Vulkan device
queueInfos := []vk.DeviceQueueCreateInfo{{
SType: vk.StructureTypeDeviceQueueCreateInfo,
QueueFamilyIndex: p.graphicsQueueIndex,
QueueCount: 1,
PQueuePriorities: []float32{1.0},
}}
if separateQueue {
queueInfos = append(queueInfos, vk.DeviceQueueCreateInfo{
SType: vk.StructureTypeDeviceQueueCreateInfo,
QueueFamilyIndex: p.presentQueueIndex,
QueueCount: 1,
PQueuePriorities: []float32{1.0},
})
}
var device vk.Device
ret = vk.CreateDevice(p.gpu, &vk.DeviceCreateInfo{
SType: vk.StructureTypeDeviceCreateInfo,
QueueCreateInfoCount: uint32(len(queueInfos)),
PQueueCreateInfos: queueInfos,
EnabledExtensionCount: uint32(len(deviceExtensions)),
PpEnabledExtensionNames: deviceExtensions,
EnabledLayerCount: uint32(len(validationLayers)),
PpEnabledLayerNames: validationLayers,
}, nil, &device)
orPanic(NewError(ret))
p.device = device
p.context.device = device
app.VulkanInit(p.context)
var queue vk.Queue
vk.GetDeviceQueue(p.device, p.graphicsQueueIndex, 0, &queue)
p.graphicsQueue = queue
if mode.Has(VulkanPresent) { // init a swapchain for surface
if separateQueue {
var presentQueue vk.Queue
vk.GetDeviceQueue(p.device, p.presentQueueIndex, 0, &presentQueue)
p.presentQueue = presentQueue
}
p.context.preparePresent()
dimensions := &SwapchainDimensions{
// some default preferences here
Width: 640, Height: 480,
Format: vk.FormatB8g8r8a8Unorm,
}
if iface, ok := app.(ApplicationSwapchainDimensions); ok {
dimensions = iface.VulkanSwapchainDimensions()
}
p.context.prepareSwapchain(p.gpu, p.surface, dimensions)
}
if iface, ok := app.(ApplicationContextPrepare); ok {
p.context.SetOnPrepare(iface.VulkanContextPrepare)
}
if iface, ok := app.(ApplicationContextCleanup); ok {
p.context.SetOnCleanup(iface.VulkanContextCleanup)
}
if iface, ok := app.(ApplicationContextInvalidate); ok {
p.context.SetOnInvalidate(iface.VulkanContextInvalidate)
}
if mode.Has(VulkanPresent) {
p.context.prepare(false)
}
return p, nil
}
type basePlatform struct {
context *context
instance vk.Instance
gpu vk.PhysicalDevice
device vk.Device
graphicsQueueIndex uint32
presentQueueIndex uint32
presentQueue vk.Queue
graphicsQueue vk.Queue
gpuProperties vk.PhysicalDeviceProperties
memoryProperties vk.PhysicalDeviceMemoryProperties
}
func (p *basePlatform) MemoryProperties() vk.PhysicalDeviceMemoryProperties {
return p.memoryProperties
}
func (p *basePlatform) PhysicalDeviceProperies() vk.PhysicalDeviceProperties {
return p.gpuProperties
}
func (p *basePlatform) PhysicalDevice() vk.PhysicalDevice {
return p.gpu
}
func (p *basePlatform) Surface() vk.Surface {
return vk.NullSurface
}
func (p *basePlatform) GraphicsQueueFamilyIndex() uint32 {
return p.graphicsQueueIndex
}
func (p *basePlatform) PresentQueueFamilyIndex() uint32 {
return p.presentQueueIndex
}
func (p *basePlatform) HasSeparatePresentQueue() bool {
return p.presentQueueIndex != p.graphicsQueueIndex
}
func (p *basePlatform) GraphicsQueue() vk.Queue {
return p.graphicsQueue
}
func (p *basePlatform) PresentQueue() vk.Queue {
if p.graphicsQueueIndex != p.presentQueueIndex {
return p.presentQueue
}
return p.graphicsQueue
}
func (p *basePlatform) Instance() vk.Instance {
return p.instance
}
func (p *basePlatform) Device() vk.Device {
return p.device
}
type platform struct {
basePlatform
surface vk.Surface
debugCallback vk.DebugReportCallback
}
func (p *platform) Surface() vk.Surface {
return p.surface
}
func (p *platform) Destroy() {
if p.device != nil {
vk.DeviceWaitIdle(p.device)
}
p.context.destroy()
p.context = nil
if p.surface != vk.NullSurface {
vk.DestroySurface(p.instance, p.surface, nil)
p.surface = vk.NullSurface
}
if p.device != nil {
vk.DestroyDevice(p.device, nil)
p.device = nil
}
if p.debugCallback != vk.NullDebugReportCallback {
vk.DestroyDebugReportCallback(p.instance, p.debugCallback, nil)
}
if p.instance != nil {
vk.DestroyInstance(p.instance, nil)
p.instance = nil
}
}
func dbgCallbackFunc(flags vk.DebugReportFlags, objectType vk.DebugReportObjectType,
object uint64, location uint, messageCode int32, pLayerPrefix string,
pMessage string, pUserData unsafe.Pointer) vk.Bool32 {
switch {
case flags&vk.DebugReportFlags(vk.DebugReportInformationBit) != 0:
log.Printf("INFORMATION: [%s] Code %d : %s", pLayerPrefix, messageCode, pMessage)
case flags&vk.DebugReportFlags(vk.DebugReportWarningBit) != 0:
log.Printf("WARNING: [%s] Code %d : %s", pLayerPrefix, messageCode, pMessage)
case flags&vk.DebugReportFlags(vk.DebugReportPerformanceWarningBit) != 0:
log.Printf("PERFORMANCE WARNING: [%s] Code %d : %s", pLayerPrefix, messageCode, pMessage)
case flags&vk.DebugReportFlags(vk.DebugReportErrorBit) != 0:
log.Printf("ERROR: [%s] Code %d : %s", pLayerPrefix, messageCode, pMessage)
case flags&vk.DebugReportFlags(vk.DebugReportDebugBit) != 0:
log.Printf("DEBUG: [%s] Code %d : %s", pLayerPrefix, messageCode, pMessage)
default:
log.Printf("INFORMATION: [%s] Code %d : %s", pLayerPrefix, messageCode, pMessage)
}
return vk.Bool32(vk.False)
}