vulkan_guide.html 15.7 KB
Newer Older
Nick Sharp's avatar
Nick Sharp committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GLFW: Vulkan guide</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="extra.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
	<div class="glfwheader">
		<a href="http://www.glfw.org/" id="glfwhome">GLFW</a>
		<ul class="glfwnavbar">
			<li><a href="http://www.glfw.org/documentation.html">Documentation</a></li>
			<li><a href="http://www.glfw.org/download.html">Download</a></li>
			<li><a href="http://www.glfw.org/media.html">Media</a></li>
			<li><a href="http://www.glfw.org/community.html">Community</a></li>
		</ul>
	</div>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Vulkan guide </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#vulkan_include">Including the Vulkan and GLFW header files</a></li>
<li class="level1"><a href="#vulkan_support">Querying for Vulkan support</a><ul><li class="level2"><a href="#vulkan_proc">Querying Vulkan function pointers</a></li>
</ul>
</li>
<li class="level1"><a href="#vulkan_ext">Querying required Vulkan extensions</a></li>
<li class="level1"><a href="#vulkan_present">Querying for Vulkan presentation support</a></li>
<li class="level1"><a href="#vulkan_window">Creating the window</a></li>
<li class="level1"><a href="#vulkan_surface">Creating a Vulkan window surface</a></li>
</ul>
</div>
<div class="textblock"><p>This guide is intended to fill the gaps between the <a href="https://www.khronos.org/vulkan/">Vulkan documentation</a> and the rest of the GLFW documentation and is not a replacement for either. It assumes some familiarity with Vulkan concepts like loaders, devices, queues and surfaces and leaves it to the Vulkan documentation to explain the details of Vulkan functions.</p>
<p>To develop for Vulkan you should install an SDK for your platform, for example the <a href="https://vulkan.lunarg.com/">LunarG Vulkan SDK</a>. Apart from the headers and libraries, it also provides the validation layers necessary for development.</p>
<p>The GLFW library does not need the Vulkan SDK to enable support for Vulkan. However, any Vulkan-specific test and example programs are built only if the CMake files find a Vulkan SDK.</p>
<p>For details on a specific function in this category, see the <a class="el" href="group__vulkan.html">Vulkan reference</a>. There are also guides for the other areas of the GLFW API.</p>
<ul>
<li><a class="el" href="intro_guide.html">Introduction to the API</a></li>
<li><a class="el" href="window_guide.html">Window guide</a></li>
<li><a class="el" href="context_guide.html">Context guide</a></li>
<li><a class="el" href="monitor_guide.html">Monitor guide</a></li>
<li><a class="el" href="input_guide.html">Input guide</a></li>
</ul>
<h1><a class="anchor" id="vulkan_include"></a>
Including the Vulkan and GLFW header files</h1>
<p>To include the Vulkan header, define <a class="el" href="build_guide.html#build_macros">GLFW_INCLUDE_VULKAN</a> before including the GLFW header.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#define GLFW_INCLUDE_VULKAN</span></div><div class="line"><span class="preprocessor">#include &lt;GLFW/glfw3.h&gt;</span></div></div><!-- fragment --><p>If you instead want to include the Vulkan header from a custom location or use your own custom Vulkan header then do this before the GLFW header.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;path/to/vulkan.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="glfw3_8h.html">GLFW/glfw3.h</a>&gt;</span></div></div><!-- fragment --><p>Unless a Vulkan header is included, either by the GLFW header or above it, any GLFW functions that take or return Vulkan types will not be declared.</p>
<p>The <code>VK_USE_PLATFORM_*_KHR</code> macros do not need to be defined for the Vulkan part of GLFW to work. Define them only if you are using these extensions directly.</p>
<h1><a class="anchor" id="vulkan_support"></a>
Querying for Vulkan support</h1>
<p>If you are linking directly against the Vulkan loader then you can skip this section. The canonical desktop loader library exports all Vulkan core and Khronos extension functions, allowing them to be called directly.</p>
<p>If you are loading the Vulkan loader dynamically instead of linking directly against it, you can check for the availability of a loader with <a class="el" href="group__vulkan.html#ga2e7f30931e02464b5bc8d0d4b6f9fe2b">glfwVulkanSupported</a>.</p>
<div class="fragment"><div class="line"><span class="keywordflow">if</span> (<a class="code" href="group__vulkan.html#ga2e7f30931e02464b5bc8d0d4b6f9fe2b">glfwVulkanSupported</a>())</div><div class="line">{</div><div class="line">    <span class="comment">// Vulkan is available, at least for compute</span></div><div class="line">}</div></div><!-- fragment --><p>This function returns <code>GLFW_TRUE</code> if the Vulkan loader was found. This check is performed by <a class="el" href="group__init.html#ga317aac130a235ab08c6db0834907d85e">glfwInit</a>.</p>
<p>If no loader was found, calling any other Vulkan related GLFW function will generate a <a class="el" href="group__errors.html#ga56882b290db23261cc6c053c40c2d08e">GLFW_API_UNAVAILABLE</a> error.</p>
<h2><a class="anchor" id="vulkan_proc"></a>
Querying Vulkan function pointers</h2>
<p>To load any Vulkan core or extension function from the found loader, call <a class="el" href="group__vulkan.html#gadf228fac94c5fd8f12423ec9af9ff1e9">glfwGetInstanceProcAddress</a>. To load functions needed for instance creation, pass <code>NULL</code> as the instance.</p>
<div class="fragment"><div class="line">PFN_vkCreateInstance pfnCreateInstance = (PFN_vkCreateInstance)</div><div class="line">    <a class="code" href="group__vulkan.html#gadf228fac94c5fd8f12423ec9af9ff1e9">glfwGetInstanceProcAddress</a>(NULL, <span class="stringliteral">&quot;vkCreateInstance&quot;</span>);</div></div><!-- fragment --><p>Once you have created an instance, you can load from it all other Vulkan core functions and functions from any instance extensions you enabled.</p>
<div class="fragment"><div class="line">PFN_vkCreateDevice pfnCreateDevice = (PFN_vkCreateDevice)</div><div class="line">    <a class="code" href="group__vulkan.html#gadf228fac94c5fd8f12423ec9af9ff1e9">glfwGetInstanceProcAddress</a>(instance, <span class="stringliteral">&quot;vkCreateDevice&quot;</span>);</div></div><!-- fragment --><p>This function in turn calls <code>vkGetInstanceProcAddr</code>. If that fails, the function falls back to a platform-specific query of the Vulkan loader (i.e. <code>dlsym</code> or <code>GetProcAddress</code>). If that also fails, the function returns <code>NULL</code>. For more information about <code>vkGetInstanceProcAddr</code>, see the Vulkan documentation.</p>
<p>Vulkan also provides <code>vkGetDeviceProcAddr</code> for loading device-specific versions of Vulkan function. This function can be retrieved from an instance with <a class="el" href="group__vulkan.html#gadf228fac94c5fd8f12423ec9af9ff1e9">glfwGetInstanceProcAddress</a>.</p>
<div class="fragment"><div class="line">PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr = (PFN_vkGetDeviceProcAddr)</div><div class="line">    <a class="code" href="group__vulkan.html#gadf228fac94c5fd8f12423ec9af9ff1e9">glfwGetInstanceProcAddress</a>(instance, <span class="stringliteral">&quot;vkGetDeviceProcAddr&quot;</span>);</div></div><!-- fragment --><p>Device-specific functions may execute a little bit faster, due to not having to dispatch internally based on the device passed to them. For more information about <code>vkGetDeviceProcAddr</code>, see the Vulkan documentation.</p>
<h1><a class="anchor" id="vulkan_ext"></a>
Querying required Vulkan extensions</h1>
<p>To do anything useful with Vulkan you need to create an instance. If you want to use Vulkan to render to a window, you must enable the instance extensions GLFW requires to create Vulkan surfaces.</p>
<p>To query the instance extensions required, call <a class="el" href="group__vulkan.html#ga1abcbe61033958f22f63ef82008874b1">glfwGetRequiredInstanceExtensions</a>.</p>
<div class="fragment"><div class="line">uint32_t count;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span>** extensions = <a class="code" href="group__vulkan.html#ga1abcbe61033958f22f63ef82008874b1">glfwGetRequiredInstanceExtensions</a>(&amp;count);</div></div><!-- fragment --><p>These extensions must all be enabled when creating instances that are going to be passed to <a class="el" href="group__vulkan.html#gaff3823355cdd7e2f3f9f4d9ea9518d92">glfwGetPhysicalDevicePresentationSupport</a> and <a class="el" href="group__vulkan.html#ga1a24536bec3f80b08ead18e28e6ae965">glfwCreateWindowSurface</a>. The set of extensions will vary depending on platform and may also vary depending on graphics drivers and other factors.</p>
<p>If it fails it will return <code>NULL</code> and GLFW will not be able to create Vulkan window surfaces. You can still use Vulkan for off-screen rendering and compute work.</p>
<p>The returned array will always contain <code>VK_KHR_surface</code>, so if you don't require any additional extensions you can pass this list directly to the <code>VkInstanceCreateInfo</code> struct.</p>
<div class="fragment"><div class="line">VkInstanceCreateInfo ici;</div><div class="line"></div><div class="line">memset(&amp;ici, 0, <span class="keyword">sizeof</span>(ici));</div><div class="line">ici.enabledExtensionCount = count;</div><div class="line">ici.ppEnabledExtensionNames = extensions;</div><div class="line">...</div></div><!-- fragment --><p>Additional extensions may be required by future versions of GLFW. You should check whether any extensions you wish to enable are already in the returned array, as it is an error to specify an extension more than once in the <code>VkInstanceCreateInfo</code> struct.</p>
<h1><a class="anchor" id="vulkan_present"></a>
Querying for Vulkan presentation support</h1>
<p>Not every queue family of every Vulkan device can present images to surfaces. To check whether a specific queue family of a physical device supports image presentation without first having to create a window and surface, call <a class="el" href="group__vulkan.html#gaff3823355cdd7e2f3f9f4d9ea9518d92">glfwGetPhysicalDevicePresentationSupport</a>.</p>
<div class="fragment"><div class="line"><span class="keywordflow">if</span> (<a class="code" href="group__vulkan.html#gaff3823355cdd7e2f3f9f4d9ea9518d92">glfwGetPhysicalDevicePresentationSupport</a>(instance, physical_device, queue_family_index))</div><div class="line">{</div><div class="line">    <span class="comment">// Queue family supports image presentation</span></div><div class="line">}</div></div><!-- fragment --><p>The <code>VK_KHR_surface</code> extension additionally provides the <code>vkGetPhysicalDeviceSurfaceSupportKHR</code> function, which performs the same test on an existing Vulkan surface.</p>
<h1><a class="anchor" id="vulkan_window"></a>
Creating the window</h1>
<p>Unless you will be using OpenGL or OpenGL ES with the same window as Vulkan, there is no need to create a context. You can disable context creation with the <a class="el" href="window_guide.html#window_hints_ctx">GLFW_CLIENT_API</a> hint.</p>
<div class="fragment"><div class="line"><a class="code" href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<a class="code" href="glfw3_8h.html#a649309cf72a3d3de5b1348ca7936c95b">GLFW_CLIENT_API</a>, <a class="code" href="glfw3_8h.html#a8f6dcdc968d214ff14779564f1389264">GLFW_NO_API</a>);</div><div class="line"><a class="code" href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window = <a class="code" href="group__window.html#ga5c336fddf2cbb5b92f65f10fb6043344">glfwCreateWindow</a>(640, 480, <span class="stringliteral">&quot;Window Title&quot;</span>, NULL, NULL);</div></div><!-- fragment --><p>See <a class="el" href="context_guide.html#context_less">Windows without contexts</a> for more information.</p>
<h1><a class="anchor" id="vulkan_surface"></a>
Creating a Vulkan window surface</h1>
<p>You can create a Vulkan surface (as defined by the <code>VK_KHR_surface</code> extension) for a GLFW window with <a class="el" href="group__vulkan.html#ga1a24536bec3f80b08ead18e28e6ae965">glfwCreateWindowSurface</a>.</p>
<div class="fragment"><div class="line">VkSurfaceKHR surface;</div><div class="line">VkResult err = <a class="code" href="group__vulkan.html#ga1a24536bec3f80b08ead18e28e6ae965">glfwCreateWindowSurface</a>(instance, window, NULL, &amp;surface);</div><div class="line"><span class="keywordflow">if</span> (err)</div><div class="line">{</div><div class="line">    <span class="comment">// Window surface creation failed</span></div><div class="line">}</div></div><!-- fragment --><p>It is your responsibility to destroy the surface. GLFW does not destroy it for you. Call <code>vkDestroySurfaceKHR</code> function from the same extension to destroy it. </p>
</div></div><!-- contents -->
<address class="footer">
<p>
Last update on Thu Aug 18 2016 for GLFW 3.2.1
</p>
</address>
</body>
</html>