You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chap02/chap02.md
+23-33Lines changed: 23 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,41 +1,36 @@
1
1
# Creating an Instance
2
2
3
-
Before we are able to start using Vulkan, we must first create an instance. A `VkInstance` is an object that contains all the information the implementation needs to work. Unlike OpenGL, Vulkan does not have a global state. Because of this, we must instead store our states in this object. In this chapter, we'll be beginning a class we'll use for the rest of the book. Here is what it the skeleton code looks like:
3
+
Before we are able to start using Vulkan, we must first create an instance. A `VkInstance` is an object that contains all the information the implementation needs to work. Unlike OpenGL, Vulkan does not have a global state. Because of this, we must instead store our states in this object. In this chapter, we'll be beginning a class we'll use for the rest of the book. Here are the headers we will need to include:
4
4
5
5
```cpp
6
-
#ifndef VULKAN_EXAMPLE_HPP
7
-
#define VULKAN_EXAMPLE_HPP
8
-
9
-
#include <cassert>
10
6
#include<stdio.h>
11
-
#include <stdlib.h>
12
7
#include<vector>
13
-
14
8
#include<vulkan/vulkan.h>
9
+
```
10
+
11
+
We'll be storing all of our variables in a class we'll call `VulkanExample` for now. The code will start out looking like:
15
12
13
+
```cpp
16
14
classVulkanExample {
17
15
private:
18
16
void exitOnError(const char* msg);
19
17
void initInstance();
20
-
18
+
21
19
const char* applicationName = "Vulkan Example";
22
20
const char* engineName = "Vulkan Engine";
23
-
21
+
24
22
VkInstance instance;
25
-
26
23
public:
27
24
VulkanExample();
28
25
virtual ~VulkanExample();
29
26
};
30
-
31
-
#endif// VULKAN_EXAMPLE_HPP
32
27
```
33
28
34
-
We'll be writing the contents of the constructor and the `initInstance` method.
29
+
In this chapter, we'll be focusing on the constructor and the `initInstance` method.
35
30
36
-
## `VkApplicationInfo`
31
+
## Application Information
37
32
38
-
This object, while not required, is pretty standard in most applications.
33
+
Before we can use `VkInstanceCreateInfo`, we need to use another type to describe our application. The `VkApplicationInfo` contains the name of the application, application version, engine name, engine version, and API version. Like OpenGL, if the driver does not support the version we request, we most likely will not be able to recover.
You'll notice that for `apiVersion`, I am using `VK_MAKE_VERSION`. This allows the developer to specify a targeted Vulkan version. We'll see later that if the version we try to get is unsupported, we'll get an error called `VK_ERROR_INCOMPATIBLE_DRIVER`.
76
71
77
-
## `VkInstanceCreateInfo`
72
+
## Instance Creation Information
78
73
79
-
This object, **is** required unlike `VkApplicationInfo`. This will be used to inform the instance of our application info, layers we'll be using, and extensions we'll be using.
74
+
`VkInstanceCreateInfo`will be used to inform Vulkan of our application info, layers we'll be using, and extensions we want.
Finally we're ready to create our instance. A Vulkan instance is similar, in concept, to an OpenGL rendering context. Like I alluded to before, we'll store the engine state here including layers and extensions. To create an instance, we'll be using the `vkCreateInstance` function.
141
136
142
137
**Definition for `vkCreateInstance`**:
143
138
@@ -154,20 +149,15 @@ VkResult vkCreateInstance(
154
149
- `pAllocator` controls host memory allocation.
155
150
- `pInstance` points a `VkInstance` handle in which the resulting instance is returned.
156
151
157
-
Notice this returns a `VkResult`. This value is used for many function calls in Vulkan to indicate failure during execution of the function body. In this case, the value will tell us if the instance creation was successful or if it failed.
152
+
You'll want to note two things. First, we're not going to use `pAllocator`. Instead, we'll pass in `NULL` for this argument. Second, the call to this function returns a `VkResult`. This value is used for many function calls in Vulkan. It will tell us if we were successful, failed, or if something else happened. In this case, the value will tell us if the instance creation was successful or if it failed.
158
153
159
154
**Usage for `vkCreateInstance`**:
160
155
161
156
```cpp
162
157
VkResult res = vkCreateInstance(&createInfo, NULL, &instance);
163
158
```
164
159
165
-
We should check:
166
-
167
-
- Is our driver compatible?
168
-
- Was our call to `vkCreateInstance()` succcessful?
169
-
170
-
We can do this with the following code:
160
+
After we've made the call, we should first check if our driver compatible. If it is not, we need to exit. We cannot recover. However, we should provide some sort of useful error. This error is extremely common in my experience. Second, we'll check if our call to `vkCreateInstance` successful. Again, if it was not, we need to exit. We cannot continue. These checks can be made with the following code:
171
161
172
162
```cpp
173
163
if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
@@ -183,9 +173,9 @@ if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
183
173
}
184
174
```
185
175
186
-
## `exitOnError`
176
+
## Termination on Error
187
177
188
-
Our `exitOnError` method is very simple at the moment. It looks like this:
178
+
Our `exitOnError` method is simple at the moment. We'll make some minor changes to it when we start working with windows, but for now, this will fulfill our needs:
We'll make some minor changes to that when we start working with windows.
198
-
199
-
## Destructor
187
+
## Cleaning Up (Destructor)
200
188
201
-
For now, we will simply destroy the instance we created and then exit. The destructor looks like this:
189
+
Exiting should be graceful if possible. In the case that our destructor is called, we will destroy the instance we created. Afterwards, the program will exit. The destructor looks like this:
0 commit comments