Skip to content

Commit df17b20

Browse files
Update chap02/chap02.md
1 parent b9f4a5f commit df17b20

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

chap02/chap02.md

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
# Creating an Instance
22

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:
44

55
```cpp
6-
#ifndef VULKAN_EXAMPLE_HPP
7-
#define VULKAN_EXAMPLE_HPP
8-
9-
#include <cassert>
106
#include <stdio.h>
11-
#include <stdlib.h>
127
#include <vector>
13-
148
#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:
1512

13+
```cpp
1614
class VulkanExample {
1715
private:
1816
void exitOnError(const char* msg);
1917
void initInstance();
20-
18+
2119
const char* applicationName = "Vulkan Example";
2220
const char* engineName = "Vulkan Engine";
23-
21+
2422
VkInstance instance;
25-
2623
public:
2724
VulkanExample();
2825
virtual ~VulkanExample();
2926
};
30-
31-
#endif // VULKAN_EXAMPLE_HPP
3227
```
3328
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.
3530
36-
## `VkApplicationInfo`
31+
## Application Information
3732
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.
3934
4035
**Definition for `VkApplicationInfo`**:
4136
@@ -74,9 +69,9 @@ appInfo.apiVersion = VK_MAKE_VERSION(1, 0, 3);
7469

7570
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`.
7671

77-
## `VkInstanceCreateInfo`
72+
## Instance Creation Information
7873

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.
8075

8176
**Definition for `VkInstanceCreateInfo`**:
8277

@@ -135,9 +130,9 @@ createInfo.enabledExtensionCount = enabledExtensions.size();
135130
createInfo.ppEnabledExtensionNames = enabledExtensions.data();
136131
```
137132

138-
## `vkCreateInstance`
133+
## Creating an Instance
139134

140-
Finally we're ready to create our instance.
135+
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.
141136

142137
**Definition for `vkCreateInstance`**:
143138

@@ -154,20 +149,15 @@ VkResult vkCreateInstance(
154149
- `pAllocator` controls host memory allocation.
155150
- `pInstance` points a `VkInstance` handle in which the resulting instance is returned.
156151
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.
158153
159154
**Usage for `vkCreateInstance`**:
160155
161156
```cpp
162157
VkResult res = vkCreateInstance(&createInfo, NULL, &instance);
163158
```
164159

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:
171161

172162
```cpp
173163
if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
@@ -183,9 +173,9 @@ if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
183173
}
184174
```
185175

186-
## `exitOnError`
176+
## Termination on Error
187177

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:
189179

190180
```cpp
191181
void VulkanExample::exitOnError(const char* msg) {
@@ -194,12 +184,12 @@ void VulkanExample::exitOnError(const char* msg) {
194184
}
195185
```
196186
197-
We'll make some minor changes to that when we start working with windows.
198-
199-
## Destructor
187+
## Cleaning Up (Destructor)
200188
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:
202190
203191
```cpp
204-
VulkanExample::~VulkanExample() { vkDestroyInstance(instance, NULL); }
192+
VulkanExample::~VulkanExample() {
193+
vkDestroyInstance(instance, NULL);
194+
}
205195
```

0 commit comments

Comments
 (0)