Skip to content

Commit 899f0fa

Browse files
committed
app: [Android] check for physical devices before creating Vulkan surface
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]>
1 parent 53fe2e5 commit 899f0fa

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

app/vulkan_android.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package app
77

88
import (
9+
"errors"
910
"unsafe"
1011

1112
"gioui.org/gpu"
@@ -24,6 +25,16 @@ func init() {
2425
if err != nil {
2526
return nil, err
2627
}
28+
// Some devices (e.g. LG K20) will crash if we create a surface. They
29+
// also don't report any physical devices, so check for that before
30+
// proceeding.
31+
devs, err := vk.EnumeratePhysicalDevices(inst)
32+
if err != nil {
33+
return nil, err
34+
}
35+
if len(devs) == 0 {
36+
return nil, errors.New("vulkan: no physical devices available")
37+
}
2738
window, _, _ := w.nativeWindow()
2839
surf, err := vk.CreateAndroidSurface(inst, unsafe.Pointer(window))
2940
if err != nil {

internal/vk/vulkan.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -816,12 +816,12 @@ func CreateInstance(exts ...string) (Instance, error) {
816816
return nil, err
817817
}
818818
inf := C.VkInstanceCreateInfo{
819-
sType: C.VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
820-
enabledExtensionCount: C.uint32_t(len(exts)),
819+
sType: C.VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
821820
}
822821
if len(exts) > 0 {
823822
cexts := mallocCStringArr(exts)
824823
defer freeCStringArr(cexts)
824+
inf.enabledExtensionCount = C.uint32_t(len(exts))
825825
inf.ppEnabledExtensionNames = &cexts[0]
826826
}
827827
var inst Instance
@@ -861,17 +861,25 @@ func GetPhysicalDeviceQueueFamilyProperties(pd PhysicalDevice) []QueueFamilyProp
861861
return queues
862862
}
863863

864-
func ChoosePhysicalDevice(inst Instance, surf Surface) (PhysicalDevice, int, error) {
864+
func EnumeratePhysicalDevices(inst Instance) ([]PhysicalDevice, error) {
865865
var count C.uint32_t
866866
if err := vkErr(C.vkEnumeratePhysicalDevices(funcs.vkEnumeratePhysicalDevices, inst, &count, nil)); err != nil {
867-
return nil, 0, fmt.Errorf("vulkan: vkEnumeratePhysicalDevices: %w", err)
867+
return nil, fmt.Errorf("vulkan: vkEnumeratePhysicalDevices: %w", err)
868868
}
869869
if count == 0 {
870-
return nil, 0, errors.New("vulkan: no devices available")
870+
return nil, nil
871871
}
872872
devs := make([]C.VkPhysicalDevice, count)
873873
if err := vkErr(C.vkEnumeratePhysicalDevices(funcs.vkEnumeratePhysicalDevices, inst, &count, &devs[0])); err != nil {
874-
return nil, 0, fmt.Errorf("vulkan: vkEnumeratePhysicalDevices: %w", err)
874+
return nil, fmt.Errorf("vulkan: vkEnumeratePhysicalDevices: %w", err)
875+
}
876+
return devs, nil
877+
}
878+
879+
func ChoosePhysicalDevice(inst Instance, surf Surface) (PhysicalDevice, int, error) {
880+
devs, err := EnumeratePhysicalDevices(inst)
881+
if err != nil {
882+
return nil, 0, err
875883
}
876884
for _, pd := range devs {
877885
const caps = C.VK_QUEUE_GRAPHICS_BIT | C.VK_QUEUE_COMPUTE_BIT

0 commit comments

Comments
 (0)