forked from vkngwrapper/vulkan-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_validation_layers.diff
124 lines (114 loc) · 3.92 KB
/
02_validation_layers.diff
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
diff --git a/../steps/01_instance_creation/main.go b/../steps/02_validation_layers/main.go
index fc30f18..6d77127 100644
--- a/../steps/01_instance_creation/main.go
+++ b/../steps/02_validation_layers/main.go
@@ -6,15 +6,21 @@ import (
"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 HelloTriangleApplication struct {
- loader core.Loader
window *sdl.Window
+ loader core.Loader
- instance core1_0.Instance
+ instance core1_0.Instance
+ debugMessenger ext_debug_utils.DebugUtilsMessenger
}
func (app *HelloTriangleApplication) Run() error {
@@ -52,7 +58,12 @@ func (app *HelloTriangleApplication) initWindow() error {
}
func (app *HelloTriangleApplication) initVulkan() error {
- return app.createInstance()
+ err := app.createInstance()
+ if err != nil {
+ return err
+ }
+
+ return app.setupDebugMessenger()
}
func (app *HelloTriangleApplication) mainLoop() error {
@@ -70,6 +81,10 @@ appLoop:
}
func (app *HelloTriangleApplication) cleanup() {
+ if app.debugMessenger != nil {
+ app.debugMessenger.Destroy(nil)
+ }
+
if app.instance != nil {
app.instance.Destroy(nil)
}
@@ -104,12 +119,35 @@ func (app *HelloTriangleApplication) createInstance() error {
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
@@ -118,6 +156,34 @@ func (app *HelloTriangleApplication) createInstance() error {
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) 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 main() {
app := &HelloTriangleApplication{}