-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03_physical_device_selection.diff
More file actions
113 lines (103 loc) · 2.91 KB
/
03_physical_device_selection.diff
File metadata and controls
113 lines (103 loc) · 2.91 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
113
diff --git a/../steps/02_validation_layers/main.go b/../steps/03_physical_device_selection/main.go
index a9bf827..d3ac8e6 100644
--- a/../steps/02_validation_layers/main.go
+++ b/../steps/03_physical_device_selection/main.go
@@ -15,12 +15,22 @@ var validationLayers = []string{"VK_LAYER_KHRONOS_validation"}
const enableValidationLayers = true
+type QueueFamilyIndices struct {
+ GraphicsFamily *int
+}
+
+func (i *QueueFamilyIndices) IsComplete() bool {
+ return i.GraphicsFamily != nil
+}
+
type HelloTriangleApplication struct {
window *sdl.Window
loader core.Loader
instance core1_0.Instance
debugMessenger ext_debug_utils.DebugUtilsMessenger
+
+ physicalDevice core1_0.PhysicalDevice
}
func (app *HelloTriangleApplication) Run() error {
@@ -49,7 +59,7 @@ func (app *HelloTriangleApplication) initWindow() error {
}
app.window = window
- app.loader, err = core.CreateSystemLoader()
+ app.loader, err = core.CreateLoaderFromProcAddr(sdl.VulkanGetVkGetInstanceProcAddr())
if err != nil {
return err
}
@@ -63,7 +73,12 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
- return app.setupDebugMessenger()
+ err = app.setupDebugMessenger()
+ if err != nil {
+ return err
+ }
+
+ return app.pickPhysicalDevice()
}
func (app *HelloTriangleApplication) mainLoop() error {
@@ -179,11 +194,62 @@ func (app *HelloTriangleApplication) setupDebugMessenger() error {
return nil
}
+func (app *HelloTriangleApplication) pickPhysicalDevice() error {
+ physicalDevices, _, err := app.instance.EnumeratePhysicalDevices()
+ if err != nil {
+ return err
+ }
+
+ for _, device := range physicalDevices {
+ if app.isDeviceSuitable(device) {
+ app.physicalDevice = device
+ break
+ }
+ }
+
+ if app.physicalDevice == nil {
+ return errors.New("failed to find a suitable GPU!")
+ }
+
+ return nil
+}
+
+func (app *HelloTriangleApplication) isDeviceSuitable(device core1_0.PhysicalDevice) bool {
+ indices, err := app.findQueueFamilies(device)
+ if err != nil {
+ return false
+ }
+
+ return indices.IsComplete()
+}
+
+func (app *HelloTriangleApplication) findQueueFamilies(device core1_0.PhysicalDevice) (QueueFamilyIndices, error) {
+ indices := QueueFamilyIndices{}
+ queueFamilies := device.QueueFamilyProperties()
+
+ for queueFamilyIdx, queueFamily := range queueFamilies {
+ if (queueFamily.QueueFlags & core1_0.QueueGraphics) != 0 {
+ indices.GraphicsFamily = new(int)
+ *indices.GraphicsFamily = queueFamilyIdx
+ }
+
+ if indices.IsComplete() {
+ break
+ }
+ }
+
+ return indices, nil
+}
+
func (app *HelloTriangleApplication) logDebug(msgType ext_debug_utils.DebugUtilsMessageTypeFlags, severity ext_debug_utils.DebugUtilsMessageSeverityFlags, data *ext_debug_utils.DebugUtilsMessengerCallbackData) bool {
log.Printf("[%s %s] - %s", severity, msgType, data.Message)
return false
}
+func fail(val any) {
+ log.Fatalf("%+v\n", val)
+}
+
func main() {
app := &HelloTriangleApplication{}