Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31ffdb0

Browse files
authored
Merge pull request #198 from abnercoimbre/master
Dev env: fix #189; setting up Vulkan on Linux needs updates
2 parents d01c592 + db29bea commit 31ffdb0

1 file changed

Lines changed: 30 additions & 120 deletions

File tree

en/02_Development_environment.md

Lines changed: 30 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -196,54 +196,18 @@ The number of extensions should be non-zero. Congratulations, you're all set for
196196
## Linux
197197

198198
These instructions will be aimed at Ubuntu users, but you may be able to follow
199-
along by compiling the LunarG SDK yourself and changing the `apt` commands to
200-
the package manager commands that are appropriate for you. You should have a
201-
compiler that supports C++17 (GCC 7+ or Clang 5+). You also need both CMake and
202-
make.
199+
along by changing the `apt` commands to the package manager commands that are appropriate for you. You should have a compiler that supports C++17 (GCC 7+ or Clang 5+). You'll also need make.
203200

204-
### Vulkan SDK
205-
206-
The most important component you'll need for developing Vulkan applications is
207-
the SDK. It includes the headers, standard validation layers, debugging tools
208-
and a loader for the Vulkan functions. The loader looks up the functions in the
209-
driver at runtime, similarly to GLEW for OpenGL - if you're familiar with that.
210-
211-
The SDK can be downloaded from [the LunarG website](https://vulkan.lunarg.com/)
212-
using the buttons at the bottom of the page. You don't have to create an
213-
account, but it will give you access to some additional documentation that may
214-
be useful to you.
215-
216-
![](/images/vulkan_sdk_download_buttons.png)
217-
218-
Open a terminal in the directory where you've downloaded the `.tar.gz` archive and extract it:
201+
### Vulkan Packages
219202

220-
```bash
221-
tar -xzf vulkansdk-linux-x86_64-xxx.tar.gz
222-
```
223-
224-
It will extract all of the files in the SDK to a subdirectory with the SDK version as name in the working directory. Move the directory to a convenient place and take
225-
note of its path. Open a terminal in the root directory of the SDK, which will
226-
contain files like `build_examples.sh`.
227-
228-
The samples in the SDK and one of the libraries that you will later use for your
229-
program depend on the XCB library. This is a C library that is used to interface
230-
with the X Window System. It can be installed in Ubuntu from the `libxcb1-dev`
231-
package. You also need the generic X development files that come with the
232-
`xorg-dev` package.
233-
234-
```bash
235-
sudo apt install libxcb1-dev xorg-dev
236-
```
203+
The most important components you'll need for developing Vulkan applications on Linux are the Vulkan loader, validation layers, and a couple of command-line utilities to test whether your machine is Vulkan-capable:
237204

238-
You can now build the Vulkan examples in the SDK by running:
205+
* `sudo apt install vulkan-tools`: Command-line utilities, most importantly `vulkaninfo` and `vkcube`. Run these to confirm your machine supports Vulkan.
206+
* `sudo apt install libvulkan-dev`: Installs Vulkan loader. The loader looks up the functions in the driver at runtime, similarly to GLEW for OpenGL - if you're familiar with that.
207+
* `sudo apt install vulkan-validationlayers-dev`: Installs the standard validation layers. These are crucial when debugging Vulkan applications, and we'll discuss them in the upcoming chapter.
239208

240-
```bash
241-
./build_examples.sh
242-
```
243-
244-
If compilation was successful, then you should now have a
245-
`./examples/build/vkcube` executable. Run it from the `examples/build` directory
246-
with `./vkcube` and ensure that you see the following pop up in a window:
209+
If installation was successful, you should be all set with the Vulkan portion. Remember to run
210+
`vkcube` and ensure you see the following pop up in a window:
247211

248212
![](/images/cube_demo_nowindow.png)
249213

@@ -263,24 +227,10 @@ purpose, like [SDL](https://www.libsdl.org/), but the advantage of GLFW is that
263227
it also abstracts away some of the other platform-specific things in Vulkan
264228
besides just window creation.
265229

266-
We'll be installing GLFW from source instead of using a package, because the
267-
Vulkan support requires a recent version. You can find the sources on the [official website](http://www.glfw.org/).
268-
Extract the source code to a convenient directory and open a terminal in the
269-
directory with files like `CMakeLists.txt`.
270-
271-
Run the following commands to generate a makefile and compile GLFW:
230+
We'll be installing GLFW from the following command:
272231

273232
```bash
274-
cmake .
275-
make
276-
```
277-
278-
You may see a warning stating `Could NOT find Vulkan`, but you can safely ignore
279-
this message. If compilation was successful, then you can install GLFW into the
280-
system libraries by running:
281-
282-
```bash
283-
sudo make install
233+
sudo apt install libglfw3-dev
284234
```
285235

286236
### GLM
@@ -296,6 +246,16 @@ It is a header-only library that can be installed from the `libglm-dev` package:
296246
sudo apt install libglm-dev
297247
```
298248

249+
### Shader Compiler
250+
251+
We have just about all we need, except we'll want a program to compile shaders from the human-readable [GLSL](https://en.wikipedia.org/wiki/OpenGL_Shading_Language) to bytecode.
252+
253+
Two popular shader compilers are Khronos Group's `glslangValidator` and Google's `glslc`. The latter has a familiar GCC- and Clang-like usage, so we'll go with that: download Google's [unofficial binaries](https://github.com/google/shaderc/blob/main/downloads.md) and copy `glslc` to your `/usr/local/bin`. Note you may need to `sudo` depending on your permissions. To test, run `glslc` and it should rightfully complain we didn't pass any shaders to compile:
254+
255+
`glslc: error: no input files`
256+
257+
We'll cover `glslc` in depth in the [shader modules](!en/Drawing_a_triangle/Graphics_pipeline_basics/Shader_modules) chapter.
258+
299259
### Setting up a makefile project
300260

301261
Now that you have installed all of the dependencies, we can set up a basic
@@ -352,33 +312,21 @@ experience with makefiles, like how variables and rules work. If not, you can
352312
get up to speed very quickly with [this tutorial](https://makefiletutorial.com/).
353313

354314
We'll first define a couple of variables to simplify the remainder of the file.
355-
Define a `VULKAN_SDK_PATH` variable that refers to the location of the `x86_64`
356-
directory in the LunarG SDK, for example:
357-
358-
```make
359-
VULKAN_SDK_PATH = /home/user/VulkanSDK/x.x.x.x/x86_64
360-
```
361-
362-
Make sure to replace `user` with your own username and `x.x.x.x` with the right version. Next, define a `CFLAGS` variable that will specify the basic compiler flags:
315+
Define a `CFLAGS` variable that will specify the basic compiler flags:
363316

364317
```make
365-
CFLAGS = -std=c++17 -I$(VULKAN_SDK_PATH)/include
318+
CFLAGS = -std=c++17 -O2
366319
```
367320

368-
We're going to use modern C++ (`-std=c++17`), and we need to be
369-
able to locate `vulkan.h` in the LunarG SDK.
321+
We're going to use modern C++ (`-std=c++17`), and we'll set optimization level to O2. We can remove -O2 to compile programs faster, but we should remember to place it back for release builds.
370322

371323
Similarly, define the linker flags in a `LDFLAGS` variable:
372324

373325
```make
374-
LDFLAGS = -L$(VULKAN_SDK_PATH)/lib `pkg-config --static --libs glfw3` -lvulkan
326+
LDFLAGS = -lglfw -lvulkan -ldl -lpthread
375327
```
376328

377-
The first flag specifies that we want to be able to find libraries like
378-
`libvulkan.so` in the LunarG SDK's `x86_64/lib` directory. The second component
379-
invokes `pkg-config` to automatically retrieve all of the linker flags necessary
380-
to build an application with GLFW. Finally, `-lvulkan` links with the Vulkan
381-
function loader that comes with the LunarG SDK.
329+
The flag `-lglfw` is for GLFW, `-lvulkan` links with the Vulkan function loader and the remaining flags are low-level system libraries that GLFW needs.
382330

383331
Specifying the rule to compile `VulkanTest` is straightforward now. Make sure to
384332
use tabs for indentation instead of spaces.
@@ -405,63 +353,25 @@ clean:
405353
rm -f VulkanTest
406354
```
407355

408-
You will find that `make clean` works perfectly fine, but `make test` will most
409-
likely fail with the following error message:
410-
411-
```text
412-
./VulkanTest: error while loading shared libraries: libvulkan.so.1: cannot open shared object file: No such file or directory
413-
```
414-
415-
That's because `libvulkan.so` is not installed as system library. To alleviate
416-
this problem, explicitly specify the library loading path using the
417-
`LD_LIBRARY_PATH` environment variable:
356+
Running `make test` should show the program running successfully, and displaying the number of Vulkan extensions. The application should exit with the success return code (`0`) when you close the empty window. You should now have a complete makefile that resembles the following:
418357

419358
```make
420-
test: VulkanTest
421-
LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib ./VulkanTest
422-
```
423-
424-
The program should now run successfully, and display the number of Vulkan
425-
extensions. The application should exit with the success return code (`0`) when
426-
you close the empty window. However, there is one more variable that you need to
427-
set. We will start using validation layers in Vulkan and you need to tell the
428-
Vulkan library where to load these from using the `VK_LAYER_PATH` variable:
429-
430-
```make
431-
test: VulkanTest
432-
LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib VK_LAYER_PATH=$(VULKAN_SDK_PATH)/etc/vulkan/explicit_layer.d ./VulkanTest
433-
```
434-
435-
You should now have a complete makefile that resembles the following:
436-
437-
```make
438-
VULKAN_SDK_PATH = /home/user/VulkanSDK/x.x.x.x/x86_64
439-
440-
CFLAGS = -std=c++17 -I$(VULKAN_SDK_PATH)/include
441-
LDFLAGS = -L$(VULKAN_SDK_PATH)/lib `pkg-config --static --libs glfw3` -lvulkan
359+
CFLAGS = -std=c++17 -O2
360+
LDFLAGS = -lglfw -lvulkan -ldl -lpthread
442361

443362
VulkanTest: main.cpp
444363
g++ $(CFLAGS) -o VulkanTest main.cpp $(LDFLAGS)
445364

446365
.PHONY: test clean
447366

448367
test: VulkanTest
449-
LD_LIBRARY_PATH=$(VULKAN_SDK_PATH)/lib VK_LAYER_PATH=$(VULKAN_SDK_PATH)/etc/vulkan/explicit_layer.d ./VulkanTest
368+
./VulkanTest
450369

451370
clean:
452371
rm -f VulkanTest
453372
```
454373

455-
You can now use this directory as a template for your Vulkan projects. Make a
456-
copy, rename it to something like `HelloTriangle` and remove all of the code
457-
in `main.cpp`.
458-
459-
Before we move on, let's explore the Vulkan SDK a bit more. There is another program in it that will be very useful for development. The `x86_64/bin/glslangValidator` and `x86_64/bin/glslc` programs will be used to compile shaders from
460-
the human-readable [GLSL](https://en.wikipedia.org/wiki/OpenGL_Shading_Language)
461-
to bytecode. We'll cover this in depth in the [shader modules](!en/Drawing_a_triangle/Graphics_pipeline_basics/Shader_modules)
462-
chapter.
463-
464-
Feel free to explore the other files, but we won't need them for this tutorial.
374+
You can now use this directory as a template for your Vulkan projects. Make a copy, rename it to something like `HelloTriangle` and remove all of the code in `main.cpp`.
465375

466376
You are now all set for [the real adventure](!en/Drawing_a_triangle/Setup/Base_code).
467377

0 commit comments

Comments
 (0)