Skip to content

Commit

Permalink
app: [Android] check for physical devices before creating Vulkan surface
Browse files Browse the repository at this point in the history
The LG K20 doesn't support Vulkan, but has a stub implementation that
lets us get so far as to creating a VkSurfaceKHR. However, creating a
surface leads to crashes later.

This change checks whether any device is available before proceeding.

Signed-off-by: Elias Naur <[email protected]>
  • Loading branch information
eliasnaur committed Sep 23, 2021
1 parent 53fe2e5 commit 899f0fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 11 additions & 0 deletions app/vulkan_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package app

import (
"errors"
"unsafe"

"gioui.org/gpu"
Expand All @@ -24,6 +25,16 @@ func init() {
if err != nil {
return nil, err
}
// Some devices (e.g. LG K20) will crash if we create a surface. They
// also don't report any physical devices, so check for that before
// proceeding.
devs, err := vk.EnumeratePhysicalDevices(inst)
if err != nil {
return nil, err
}
if len(devs) == 0 {
return nil, errors.New("vulkan: no physical devices available")
}
window, _, _ := w.nativeWindow()
surf, err := vk.CreateAndroidSurface(inst, unsafe.Pointer(window))
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions internal/vk/vulkan.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,12 +816,12 @@ func CreateInstance(exts ...string) (Instance, error) {
return nil, err
}
inf := C.VkInstanceCreateInfo{
sType: C.VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
enabledExtensionCount: C.uint32_t(len(exts)),
sType: C.VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
}
if len(exts) > 0 {
cexts := mallocCStringArr(exts)
defer freeCStringArr(cexts)
inf.enabledExtensionCount = C.uint32_t(len(exts))
inf.ppEnabledExtensionNames = &cexts[0]
}
var inst Instance
Expand Down Expand Up @@ -861,17 +861,25 @@ func GetPhysicalDeviceQueueFamilyProperties(pd PhysicalDevice) []QueueFamilyProp
return queues
}

func ChoosePhysicalDevice(inst Instance, surf Surface) (PhysicalDevice, int, error) {
func EnumeratePhysicalDevices(inst Instance) ([]PhysicalDevice, error) {
var count C.uint32_t
if err := vkErr(C.vkEnumeratePhysicalDevices(funcs.vkEnumeratePhysicalDevices, inst, &count, nil)); err != nil {
return nil, 0, fmt.Errorf("vulkan: vkEnumeratePhysicalDevices: %w", err)
return nil, fmt.Errorf("vulkan: vkEnumeratePhysicalDevices: %w", err)
}
if count == 0 {
return nil, 0, errors.New("vulkan: no devices available")
return nil, nil
}
devs := make([]C.VkPhysicalDevice, count)
if err := vkErr(C.vkEnumeratePhysicalDevices(funcs.vkEnumeratePhysicalDevices, inst, &count, &devs[0])); err != nil {
return nil, 0, fmt.Errorf("vulkan: vkEnumeratePhysicalDevices: %w", err)
return nil, fmt.Errorf("vulkan: vkEnumeratePhysicalDevices: %w", err)
}
return devs, nil
}

func ChoosePhysicalDevice(inst Instance, surf Surface) (PhysicalDevice, int, error) {
devs, err := EnumeratePhysicalDevices(inst)
if err != nil {
return nil, 0, err
}
for _, pd := range devs {
const caps = C.VK_QUEUE_GRAPHICS_BIT | C.VK_QUEUE_COMPUTE_BIT
Expand Down

0 comments on commit 899f0fa

Please sign in to comment.