-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04_logical_device.diff
More file actions
112 lines (101 loc) · 3.02 KB
/
04_logical_device.diff
File metadata and controls
112 lines (101 loc) · 3.02 KB
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
diff --git a/../steps/03_physical_device_selection/main.go b/../steps/04_logical_device/main.go
index d3ac8e6..297c8d3 100644
--- a/../steps/03_physical_device_selection/main.go
+++ b/../steps/04_logical_device/main.go
@@ -8,6 +8,7 @@ import (
"github.com/vkngwrapper/core/v2/core1_0"
"github.com/vkngwrapper/extensions/v2/ext_debug_utils"
"github.com/vkngwrapper/extensions/v2/khr_portability_enumeration"
+ "github.com/vkngwrapper/extensions/v2/khr_portability_subset"
"log"
)
@@ -31,6 +32,9 @@ type HelloTriangleApplication struct {
debugMessenger ext_debug_utils.DebugUtilsMessenger
physicalDevice core1_0.PhysicalDevice
+ device core1_0.Device
+
+ graphicsQueue core1_0.Queue
}
func (app *HelloTriangleApplication) Run() error {
@@ -78,7 +82,12 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
- return app.pickPhysicalDevice()
+ err = app.pickPhysicalDevice()
+ if err != nil {
+ return err
+ }
+
+ return app.createLogicalDevice()
}
func (app *HelloTriangleApplication) mainLoop() error {
@@ -96,6 +105,10 @@ appLoop:
}
func (app *HelloTriangleApplication) cleanup() {
+ if app.device != nil {
+ app.device.Destroy(nil)
+ }
+
if app.debugMessenger != nil {
app.debugMessenger.Destroy(nil)
}
@@ -208,9 +221,52 @@ func (app *HelloTriangleApplication) pickPhysicalDevice() error {
}
if app.physicalDevice == nil {
- return errors.New("failed to find a suitable GPU!")
+ return errors.Errorf("failed to find a suitable GPU!")
+ }
+
+ return nil
+}
+
+func (app *HelloTriangleApplication) createLogicalDevice() error {
+ indices, err := app.findQueueFamilies(app.physicalDevice)
+ if err != nil {
+ return err
+ }
+
+ uniqueQueueFamilies := []int{*indices.GraphicsFamily}
+
+ var queueFamilyOptions []core1_0.DeviceQueueCreateInfo
+ queuePriority := float32(1.0)
+ for _, queueFamily := range uniqueQueueFamilies {
+ queueFamilyOptions = append(queueFamilyOptions, core1_0.DeviceQueueCreateInfo{
+ QueueFamilyIndex: queueFamily,
+ QueuePriorities: []float32{queuePriority},
+ })
+ }
+
+ var extensionNames []string
+
+ // Makes this example compatible with vulkan portability, necessary to run on mobile & mac
+ extensions, _, err := app.physicalDevice.EnumerateDeviceExtensionProperties()
+ if err != nil {
+ return err
}
+ _, supported := extensions[khr_portability_subset.ExtensionName]
+ if supported {
+ extensionNames = append(extensionNames, khr_portability_subset.ExtensionName)
+ }
+
+ app.device, _, err = app.physicalDevice.CreateDevice(nil, core1_0.DeviceCreateInfo{
+ QueueCreateInfos: queueFamilyOptions,
+ EnabledFeatures: &core1_0.PhysicalDeviceFeatures{},
+ EnabledExtensionNames: extensionNames,
+ })
+ if err != nil {
+ return err
+ }
+
+ app.graphicsQueue = app.device.GetQueue(*indices.GraphicsFamily, 0)
return nil
}
@@ -246,10 +302,6 @@ func (app *HelloTriangleApplication) logDebug(msgType ext_debug_utils.DebugUtils
return false
}
-func fail(val any) {
- log.Fatalf("%+v\n", val)
-}
-
func main() {
app := &HelloTriangleApplication{}