forked from vkngwrapper/vulkan-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
260 lines (209 loc) · 6.25 KB
/
main.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
package main
import (
"github.com/cockroachdb/errors"
"github.com/veandco/go-sdl2/sdl"
"github.com/vkngwrapper/core/v2"
"github.com/vkngwrapper/core/v2/common"
"github.com/vkngwrapper/core/v2/core1_0"
"github.com/vkngwrapper/extensions/v2/ext_debug_utils"
"github.com/vkngwrapper/extensions/v2/khr_portability_enumeration"
"log"
)
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 {
err := app.initWindow()
if err != nil {
return err
}
err = app.initVulkan()
if err != nil {
return err
}
defer app.cleanup()
return app.mainLoop()
}
func (app *HelloTriangleApplication) initWindow() error {
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return err
}
window, err := sdl.CreateWindow("Vulkan", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN|sdl.WINDOW_VULKAN)
if err != nil {
return err
}
app.window = window
app.loader, err = core.CreateLoaderFromProcAddr(sdl.VulkanGetVkGetInstanceProcAddr())
if err != nil {
return err
}
return nil
}
func (app *HelloTriangleApplication) initVulkan() error {
err := app.createInstance()
if err != nil {
return err
}
err = app.setupDebugMessenger()
if err != nil {
return err
}
return app.pickPhysicalDevice()
}
func (app *HelloTriangleApplication) mainLoop() error {
appLoop:
for true {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
break appLoop
}
}
}
return nil
}
func (app *HelloTriangleApplication) cleanup() {
if app.debugMessenger != nil {
app.debugMessenger.Destroy(nil)
}
if app.instance != nil {
app.instance.Destroy(nil)
}
if app.window != nil {
app.window.Destroy()
}
sdl.Quit()
}
func (app *HelloTriangleApplication) createInstance() error {
instanceOptions := core1_0.InstanceCreateInfo{
ApplicationName: "Hello Triangle",
ApplicationVersion: common.CreateVersion(1, 0, 0),
EngineName: "No Engine",
EngineVersion: common.CreateVersion(1, 0, 0),
APIVersion: common.Vulkan1_2,
}
// Add extensions
sdlExtensions := app.window.VulkanGetInstanceExtensions()
extensions, _, err := app.loader.AvailableExtensions()
if err != nil {
return err
}
for _, ext := range sdlExtensions {
_, hasExt := extensions[ext]
if !hasExt {
return errors.Newf("createinstance: cannot initialize sdl: missing extension %s", ext)
}
instanceOptions.EnabledExtensionNames = append(instanceOptions.EnabledExtensionNames, ext)
}
if enableValidationLayers {
instanceOptions.EnabledExtensionNames = append(instanceOptions.EnabledExtensionNames, ext_debug_utils.ExtensionName)
}
_, enumerationSupported := extensions[khr_portability_enumeration.ExtensionName]
if enumerationSupported {
instanceOptions.EnabledExtensionNames = append(instanceOptions.EnabledExtensionNames, khr_portability_enumeration.ExtensionName)
instanceOptions.Flags |= khr_portability_enumeration.InstanceCreateEnumeratePortability
}
// Add layers
layers, _, err := app.loader.AvailableLayers()
if err != nil {
return err
}
if enableValidationLayers {
for _, layer := range validationLayers {
_, hasValidation := layers[layer]
if !hasValidation {
return errors.Newf("createInstance: cannot add validation- layer %s not available- install LunarG Vulkan SDK", layer)
}
instanceOptions.EnabledLayerNames = append(instanceOptions.EnabledLayerNames, layer)
}
// Add debug messenger
instanceOptions.Next = app.debugMessengerOptions()
}
app.instance, _, err = app.loader.CreateInstance(nil, instanceOptions)
if err != nil {
return err
}
return nil
}
func (app *HelloTriangleApplication) debugMessengerOptions() ext_debug_utils.DebugUtilsMessengerCreateInfo {
return ext_debug_utils.DebugUtilsMessengerCreateInfo{
MessageSeverity: ext_debug_utils.SeverityError | ext_debug_utils.SeverityWarning,
MessageType: ext_debug_utils.TypeGeneral | ext_debug_utils.TypeValidation | ext_debug_utils.TypePerformance,
UserCallback: app.logDebug,
}
}
func (app *HelloTriangleApplication) setupDebugMessenger() error {
if !enableValidationLayers {
return nil
}
var err error
debugLoader := ext_debug_utils.CreateExtensionFromInstance(app.instance)
app.debugMessenger, _, err = debugLoader.CreateDebugUtilsMessenger(app.instance, nil, app.debugMessengerOptions())
if err != nil {
return err
}
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{}
err := app.Run()
if err != nil {
log.Fatalf("%+v\n", err)
}
}