|
1 | 1 | # Chapter 00 - Introduction
|
2 | 2 |
|
3 |
| -TBD |
| 3 | +So what is Vulkan? Here's the definition for the Vulkan standard landing page: |
| 4 | + |
| 5 | +> Vulkan is a new generation graphics and compute API that provides high-efficiency, |
| 6 | +> cross-platform access to modern GPUs used in a wide variety of devices from PCs and consoles to mobile phones and embedded platforms. |
| 7 | +
|
| 8 | +Vulkan is a standard developed by the [Khronos group](https://www.khronos.org/), |
| 9 | +which is an open industry consortium behind many other well-known standards such as [OpenGL](https://www.khronos.org/opengl/) and [OpenCL](https://www.khronos.org/opencl/). |
| 10 | + |
| 11 | +## Why Vulkan? |
| 12 | + |
| 13 | +The first questions to come to mind are why Vulkan? Why adopt another cross-platform graphics API? Why not just stick with OpenGL, which is also cross-platform? |
| 14 | + |
| 15 | +[](https://xkcd.com/927/) |
| 16 | + |
| 17 | +- It is as modern of an API as you can get, designed without the constraints of having to maintain backwards compatibility or legacy hardware. |
| 18 | + Take, for instance OpenGL, |
| 19 | + it is an aging API that has been evolving over the years and needs to support all parts of a graphics pipeline, |
| 20 | + from immediate modes to programmable pipelines. |
| 21 | + |
| 22 | +- As a modern API, it has been designed with modern hardware capabilities in mind (GPUs and CPUs). |
| 23 | + For example, [concurrency support](https://en.wikipedia.org/wiki/Concurrency_(computer_science)) is part one of the strongest points of Vulkan. |
| 24 | + This dramatically improves the performance of applications that may now be CPU constrained by the single-threaded nature of some other APIs (such us OpenGL). |
| 25 | + |
| 26 | +- It is a lower overhead API, in the sense that the most part of the work shall be explicitly done in the application. |
| 27 | + Hence, developers need to be very direct and precisely control every aspect. |
| 28 | + This simplifies the Vulkan drivers which provide a very thin layer on top of the hardware. |
| 29 | + |
| 30 | +- Due to its lower overhead and its explicit nature, you will have direct control. |
| 31 | + You will get what you ask for, the drivers will not have to guess or assume about the next steps in your application, nor will it hold your hand. |
| 32 | + This will mean the differences between implementations and drivers may be much lower than in other APIs, |
| 33 | + resulting in more predictable and portable applications. |
| 34 | + |
| 35 | +- It is indeed a platform-agnostic API not only for desktop computing but also for mobile platforms. |
| 36 | + |
| 37 | +All that has been said above comes at a cost. |
| 38 | +It is an API that imposes a lot of responsibility onto the developers. |
| 39 | +And with great power, comes big responsibility. |
| 40 | +You will have to properly control everything, from memory allocation, to resource management, |
| 41 | +and to guarantee proper synchronization between your CPU and graphics card. |
| 42 | +As a result, you will have a more direct view about the GPU working inner details, |
| 43 | +which combined with the knowledge on how your application works can lead to great performance improvements. |
| 44 | + |
| 45 | +The next question that may come to your mind may be, Is it Vulkan the right tool for you? |
| 46 | +The answer to this question depends on your skills and interests. |
| 47 | +If you are new to programming or want to obtain a rapid product, Vulkan is not the most adequate solution for you. |
| 48 | +As it has been already said, Vulkan is complex--you will have to invest lots of time understanding all the concepts, |
| 49 | +which can be challenging to understand for even moderate programmers. |
| 50 | +It is hard, but there will be a moment where all this complexity will start to fit in your mind and make sense. |
| 51 | +As a result, you will have a deeper knowledge of (and appreciation for) modern graphics applications and how GPUs work. |
| 52 | + |
| 53 | +Besides complexity, other drawbacks of Vulkan may be: |
| 54 | + |
| 55 | +- Verbosity. |
| 56 | + It is a very explicit API; |
| 57 | + you will have to specify every single detail, from available features, memory layouts, |
| 58 | + detailed pipeline execution, etc. This means that you will have to write a lot of code to manage it all. |
| 59 | +- You may not directly translate concepts from other APIs to fully exploit Vulkan capabilities. |
| 60 | + This implies additional effort, specially if you have an existing codebase or assets. |
| 61 | +- Its relatively new, so it is not so easy to find information about some specific topics. |
| 62 | + |
| 63 | +Therefore, if you are new to programming, |
| 64 | +it is much better to start with some existing game engines such as [Unity](https://unity.com) or [Godot](https://godotengine.org/) or even to start with OpenGL. |
| 65 | +You can check my other book about OpenGL using LWJGL [here](https://ahbejarano.gitbook.io/lwjglgamedev/). |
| 66 | + |
| 67 | +And the final question, why code in Java? |
| 68 | +Vulkan is a C based API, so usage of C/C++ is the ideal approach. |
| 69 | +I do not want to enter into a language war, but in my opinion, Java provides a good balance between easy of use, readability and maintainability. |
| 70 | +Having said that, I must admit that integration with native code in Java is quite verbose, |
| 71 | +and using libraries that require lots of pointers to `struct`'s, such as Vulkan, will require a bit of extra work. |
| 72 | + |
| 73 | +## Prerequisites |
| 74 | + |
| 75 | +This book assumes that you have a good understanding of Java language, and some previous knowledge of 3D graphics, such as OpenGL. |
| 76 | + |
| 77 | +We will use the following libraries: |
| 78 | + |
| 79 | +- [LWJGL](https://www.lwjgl.org/) will provide the bindings to Vulkan and other required libraries |
| 80 | + (such as [GLFW](https://www.glfw.org/) for window and input management, and [STB](https://github.com/nothings/stb) for image loading). |
| 81 | + |
| 82 | +- [JOML](https://github.com/JOML-CI/JOML) as the math library. |
| 83 | + |
| 84 | +- [Maven](http://maven.apache.org/) to build the samples. |
| 85 | + |
| 86 | +- [Tiny Log](https://tinylog.org/) for logging. |
| 87 | + |
| 88 | +- [RenderDoc](https://renderdoc.org/) for graphics debugging. |
| 89 | + |
| 90 | +- [GraalVM](https://www.graalvm.org) to generate native images (optional) |
| 91 | + |
| 92 | +You will also need the following: |
| 93 | + |
| 94 | +- The Vulkan [SDK](https://vulkan.lunarg.com/). |
| 95 | + |
| 96 | +Bring your own IDE (Although I use [IntelliJ](https://www.jetbrains.com/es-es/idea/download/)) |
| 97 | + |
| 98 | +> [!WARNING] |
| 99 | +> Source code is structured as a multi-module project. The recommended approach is to open each chapter independently. Resources (3D models, shaders, etc.) are loaded using relative > paths to the root folder, which is the folder associated to each chapter (chapter-01, chapter02, ... etc.). If you still want to work with the root project, |
| 100 | +> please be aware that you will need to set the working directory to the root of each of the sub-projects you want to run. |
| 101 | +
|
| 102 | +The maven files support native image generation, through GraalVM, by activating the native profile. |
| 103 | +To activate this, you need to properly set up GraalVM in your environment and to install native building tools. |
| 104 | +Please check GraalVM documentation for that. |
| 105 | +To generate native images, you need to execute `mvn -Pnative clean package`. |
| 106 | + |
| 107 | +## Resources used for writing this book |
| 108 | + |
| 109 | +This book is the result of my self-learning process. |
| 110 | +I do not work on this domain professionally, rather I'm just a hobbyist with an interest in learning new technologies. |
| 111 | +As a result, you may find mistakes/bugs or even explanations that may be plain wrong. |
| 112 | +Please feel free to contact me about that. |
| 113 | +My aim is that this book may help others in learning Vulkan. |
| 114 | + |
| 115 | +There are multiple materials that I've consulted to write this book. |
| 116 | +The following list collects the ones that I've found more useful, and that I've consulted many times while leaning the Vulkan path: |
| 117 | + |
| 118 | +- [The Vulkan tutorial](https://vulkan-tutorial.com/). This is a C based tutorial for Vulkan which describes in great detail the core concepts of the API. |
| 119 | +- [Sascha Willems](https://github.com/SaschaWillems/Vulkan) Vulkan samples. This is an awesome collection of C++ samples which cover a huge set of Vulkan features. |
| 120 | +- [Khronos Vulkan samples](https://github.com/KhronosGroup/Vulkan-Samples). |
| 121 | +- LWJGL Vulkan [demos](https://github.com/LWJGL/lwjgl3-demos/tree/master/src/org/lwjgl/demo/vulkan). |
| 122 | + |
| 123 | +## LWJGL Debugging |
| 124 | + |
| 125 | +> [!NOTE] |
| 126 | +> If you have troubles with LWJGL in your machine you can try the following JVM arguments: `-Dorg.lwjgl.util.Debug=true` to enable LWJGL debug mode, |
| 127 | +> `-Dorg.lwjgl.util.DebugAllocator=true` to debug `MemoryUtil` memory allocation and `-Dorg.lwjgl.util.DebugStack=true` to debug `MemoryStack` related operations |
| 128 | +> (push and pop) thattmay cause issues. |
| 129 | +> Remember that these are JVM arguments, not program arguments. If you set them in a IDE you may need to set this in the correct place. |
| 130 | +
|
| 131 | +[Next chapter](../chapter-01/chapter-01.md) |
0 commit comments