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

Skip to content

Commit 74128e2

Browse files
authored
Merge pull request #80 from fvcaputo/master
Add MacOS Instructions
2 parents ce24784 + 26b4e8b commit 74128e2

9 files changed

Lines changed: 132 additions & 3 deletions

02_Development_environment.md

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
In this chapter we'll set up your environment for developing Vulkan applications
22
and install some useful libraries. All of the tools we'll use, with the
3-
exception of the compiler, are compatible with both Windows and Linux, but the
3+
exception of the compiler, are compatible with Windows, Linux and MacOS, but the
44
steps for installing them differ a bit, which is why they're described
55
separately here.
66

@@ -62,7 +62,7 @@ As mentioned before, Vulkan by itself is a platform agnostic API and does not
6262
include tools for creating a window to display the rendered results. To benefit
6363
from the cross-platform advantages of Vulkan and to avoid the horrors of Win32,
6464
we'll use the [GLFW library](http://www.glfw.org/) to create a window, which
65-
supports both Windows and Linux. There are other libraries available for this
65+
supports Windows, Linux and MacOS. There are other libraries available for this
6666
purpose, like [SDL](https://www.libsdl.org/), but the advantage of GLFW is that
6767
it also abstracts away some of the other platform-specific things in Vulkan
6868
besides just window creation.
@@ -266,7 +266,7 @@ As mentioned before, Vulkan by itself is a platform agnostic API and does not
266266
include tools for creation a window to display the rendered results. To benefit
267267
from the cross-platform advantages of Vulkan and to avoid the horrors of X11,
268268
we'll use the [GLFW library](http://www.glfw.org/) to create a window, which
269-
supports both Windows and Linux. There are other libraries available for this
269+
supports Windows, Linux and MacOS. There are other libraries available for this
270270
purpose, like [SDL](https://www.libsdl.org/), but the advantage of GLFW is that
271271
it also abstracts away some of the other platform-specific things in Vulkan
272272
besides just window creation.
@@ -487,3 +487,132 @@ offline version of the entire Vulkan specification. Feel free to explore the
487487
other files, but we won't need them for this tutorial.
488488

489489
You are now all set for [the real adventure](!Drawing_a_triangle/Setup/Base_code).
490+
491+
## MacOS
492+
493+
These instructions will assume you are using Xcode and the [Homebrew package manager](https://brew.sh/). Also, keep in mind that you will need at least MacOS version 10.11, and your device needs to support the [Metal API](https://en.wikipedia.org/wiki/Metal_(API)#Supported_GPUs).
494+
495+
### Vulkan SDK
496+
497+
The most important component you'll need for developing Vulkan applications is the SDK. It includes the headers, standard validation layers, debugging tools and a loader for the Vulkan functions. The loader looks up the functions in the driver at runtime, similarly to GLEW for OpenGL - if you're familiar with that.
498+
499+
The SDK can be downloaded from [the LunarG website](https://vulkan.lunarg.com/) using the buttons at the bottom of the page. You don't have to create an account, but it will give you access to some additional documentation that may be useful to you.
500+
501+
![](/images/vulkan_sdk_download_buttons.png)
502+
503+
The SDK version for MacOS internally uses [MoltenVK](https://moltengl.com/). There is no native support for Vulkan on MacOS, so what MoltenVK does is actually act as a layer that translates Vulkan API calls to Apple's Metal graphics framework. With this you can take advantage of debugging and performance benefits of Apple's Metal framework.
504+
505+
After downloading it, simply extract the contents to a folder of your choice (keep in mind you will need to reference it when creating your projects on Xcode). Inside the extracted folder, in the `Applications` folder you should have some executable files that will run a few demos using the SDK. Run the `cube` executable and you will see the following:
506+
507+
![](/images/cube_demo_mac.png)
508+
509+
### GLFW
510+
511+
As mentioned before, Vulkan by itself is a platform agnostic API and does not include tools for creation a window to display the rendered results. We'll use the [GLFW library](http://www.glfw.org/) to create a window, which supports Windows, Linux and MacOS. There are other libraries available for this purpose, like [SDL](https://www.libsdl.org/), but the advantage of GLFW is that it also abstracts away some of the other platform-specific things in Vulkan besides just window creation.
512+
513+
To install GLFW on MacOS we will use the Homebrew package manager. Vulkan support for MacOS is still not fully available on the current (at the time of this writing) stable version 3.2.1. Therefore we will install the latest version of the `glfw3` package using:
514+
515+
```bash
516+
brew install glfw3 --HEAD
517+
```
518+
519+
### GLM
520+
521+
Vulkan does not include a library for linear algebra operations, so we'll have to download one. [GLM](http://glm.g-truc.net/) is a nice library that is designed for use with graphics APIs and is also commonly used with OpenGL.
522+
523+
It is a header-only library that can be installed from the `glm` package:
524+
525+
```bash
526+
brew install glm
527+
```
528+
529+
### Setting up Xcode
530+
531+
Now that all the dependencies are installed we can set up a basic Xcode project for Vulkan. Most of the instructions here are essentially a lot of "plumbing" so we can get all the dependencies linked to the project. Also, keep in mind that during the following instructions whenever we mention the folder `vulkansdk` we are refering to the folder where you extracted the Vulkan SDK.
532+
533+
Start Xcode and create a new Xcode project. On the window that will open select Application > Command Line Tool.
534+
535+
![](/images/xcode_new_project.png)
536+
537+
Select `Next`, write a name for the project and for `Language` select `C++`.
538+
539+
![](/images/xcode_new_project_2.png)
540+
541+
Press `Next` and the project should have been created. Now, let's change the code in the generated `main.cpp` file to the following code:
542+
543+
```c++
544+
#define GLFW_INCLUDE_VULKAN
545+
#include <GLFW/glfw3.h>
546+
547+
#define GLM_FORCE_RADIANS
548+
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
549+
#include <glm/vec4.hpp>
550+
#include <glm/mat4x4.hpp>
551+
552+
#include <iostream>
553+
554+
int main() {
555+
glfwInit();
556+
557+
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
558+
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
559+
560+
uint32_t extensionCount = 0;
561+
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
562+
563+
std::cout << extensionCount << " extensions supported" << std::endl;
564+
565+
glm::mat4 matrix;
566+
glm::vec4 vec;
567+
auto test = matrix * vec;
568+
569+
while(!glfwWindowShouldClose(window)) {
570+
glfwPollEvents();
571+
}
572+
573+
glfwDestroyWindow(window);
574+
575+
glfwTerminate();
576+
577+
return 0;
578+
}
579+
```
580+
581+
Keep in mind you are not required to understand all this code is doing yet, we are just setting up some API calls to make sure everything is working.
582+
583+
Xcode should already be showing some errors such as libraries it cannot find. We will now start configuring the project to get rid of those errors. On the *Project Navigator* panel select your project. Open the *Build Settings* tab and then:
584+
585+
* Find the **Header Search Paths** field and add a link to `/usr/local/include` (this is where Homebrew installs headers, so the glm and glfw3 header files should be there) and a link to `vulkansdk/macOS/include` for the Vulkan headers.
586+
* Find the **Library Search Paths** field and add a link to `/usr/local/lib` (again, this is where Homebrew installs libraries, so the glm and glfw3 lib files should be there) and a link to `vulkansdk/macOS/lib`.
587+
588+
It should look like so (obviously, paths will be different depending on where you placed on your files):
589+
590+
![](/images/xcode_paths.png)
591+
592+
Now, in the *Build Phases* tab, on **Link Binary With Libraries** we will add both the `glfw3` and the `vulkan` frameworks. To make things easier we will be adding he dynamic libraries in the project (you can check the documentation of these libraries if you want to use the static frameworks).
593+
594+
* For glfw open the folder `/usr/local/lib` and there you will find a file name like `libglfw.3.x.dylib` ("x" is the library's version number, it might be different depending on when you downloaded the package from Homebrew). Simply drag that file to the Linked Frameworks and Libraries tab on Xcode.
595+
* For vulkan, go to `vulkansdk/macOS/lib`. Do the same for the file both files `libvulkan.1.dylib` and `libvulkan.1.x.xx.dylib` (where "x" will be the version number of the the SDK you downloaded).
596+
597+
After adding those libraries, in the same tab on **Copy Files** change `Destination` to "Frameworks", clear the subpath and deselect "Copy only when installing". Click on the "+" sign and add all those three frameworks here aswell.
598+
599+
Your Xcode configuration should look like:
600+
601+
![](/images/xcode_frameworks.png)
602+
603+
The last thing you need to setup are a couple of environment variables. On Xcode toolbar go to `Product` > `Scheme` > `Edit Scheme...`, and in the `Arguments` tab add the two following environment variables:
604+
605+
* VK_ICD_FILENAMES = `vulkansdk/macOS/etc/vulkan/icd.d/MoltenVK_icd.json`
606+
* VK_LAYER_PATH = `vulkansdk/macOS/etc/vulkan/explicit_layer.d`
607+
608+
It should look like so:
609+
610+
![](/images/xcode_variables.png)
611+
612+
Finally, you should be all set! Now if you run the project (remembering to setting the build configuration to Debug or Release depending on the configuration you chose) you should see the following:
613+
614+
![](/images/xcode_output.png)
615+
616+
The number of extensions should be non-zero. The other logs are from the libraries, you might get different messages from those depending on your configuration.
617+
618+
You are now all set for [the real thing](!Drawing_a_triangle/Setup/Base_code).

images/cube_demo_mac.png

65.7 KB
Loading
30.9 KB
Loading

images/xcode_frameworks.png

76.5 KB
Loading

images/xcode_new_project.png

70.2 KB
Loading

images/xcode_new_project_2.png

44 KB
Loading

images/xcode_output.png

44.7 KB
Loading

images/xcode_paths.png

72.6 KB
Loading

images/xcode_variables.png

72.3 KB
Loading

0 commit comments

Comments
 (0)