Skip to content

Commit d990430

Browse files
committed
Add C language examples alongside C++ in Vulkan tutorials
- Include equivalent C code snippets for all Vulkan-related examples to enhance tutorial compatibility. - Annotate source blocks to specify programming languages for clarity (`[source,multilang,c++,c]`). - Improve cross-language consistency in method signatures, helper functions, and setup instructions.
1 parent 298a423 commit d990430

File tree

6 files changed

+369
-54
lines changed

6 files changed

+369
-54
lines changed

en/01_Overview.adoc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ with the existing APIs somehow. This resulted in less than ideal abstractions
2828
Aside from these new features, the past decade also saw an influx of mobile
2929
and embedded devices with powerful graphics hardware. These mobile GPUs have
3030
different architectures based on their energy and space requirements.
31-
One such example is https://en.wikipedia.org/wiki/Tiled_rendering[tiled rendering],
31+
One such example is https://en.wikipedia.org/wiki/Tiled_rendering[tiled rendering],
3232
which would benefit from improved performance by offering the
3333
programmer more control over this functionality.
3434
Another limitation originating from the age of these APIs is limited
@@ -261,10 +261,12 @@ developed by LunarG. We'll look into installing this SDK in the next chapter.
261261
Functions have a lower case `vk` prefix, types like enumerations and structs
262262
have a `Vk` prefix and enumeration values have a `VK_` prefix.
263263
The API heavily uses structs to provide parameters to functions.
264-
For example, object creation generally follows this pattern:
264+
For example, object creation generally follows this pattern in both the C API and the C++ RAII wrapper:
265265

266-
[,c++]
266+
[source,multilang,c++,c]
267+
.Object creation pattern
267268
----
269+
// START c
268270
VkXXXCreateInfo createInfo{};
269271
createInfo.sType = VK_STRUCTURE_TYPE_XXX_CREATE_INFO;
270272
createInfo.pNext = nullptr;
@@ -276,6 +278,12 @@ if (vkCreateXXX(&createInfo, nullptr, &object) != VK_SUCCESS) {
276278
std::cerr << "failed to create object" << std::endl;
277279
return false;
278280
}
281+
// END c
282+
283+
// START c++
284+
auto createInfo = vk::xxx();
285+
auto object = vk::raii::XXX(context, createInfo);
286+
// END c++
279287
----
280288

281289
Many structures in Vulkan require you to explicitly specify the type of
@@ -291,15 +299,6 @@ error code.
291299
The specification describes which error codes each function can return and
292300
what they mean.
293301

294-
To help illustrate the utility of using the RAII C++ Vulkan abstraction; this
295-
is the same code written with our modern API:
296-
297-
[,c++]
298-
----
299-
auto createInfo = vk::xxx();
300-
auto object = vk::raii::XXX(context, createInfo);
301-
----
302-
303302
Failure of such calls is reported by C++ exceptions. The exception will
304303
respond with more information about the error including the aforementioned
305304
vkResult, this enables us to check multiple commands from one call and keep

en/03_Drawing_a_triangle/01_Presentation/00_Window_surface.adoc

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,16 @@ works in this tutorial.
3737

3838
Start by adding a `surface` class member right below the debug callback.
3939

40-
[,c++]
40+
[source,multilang,c++,c]
41+
.Surface member
4142
----
43+
// START c++
4244
vk::raii::SurfaceKHR surface = nullptr;
45+
// END c++
46+
47+
// START c
48+
VkSurfaceKHR surface = VK_NULL_HANDLE;
49+
// END c
4350
----
4451

4552
Although the `VkSurfaceKHR` object and its usage is platform-agnostic, its
@@ -72,12 +79,22 @@ Because a window surface is a Vulkan object, it comes with a
7279
important parameters: `hwnd` and `hinstance`. These are the handles to the
7380
window and the process.
7481

75-
[,c++]
82+
[source,multilang,c++,c]
83+
.Win32 surface create info
7684
----
85+
// START c++
86+
vk::Win32SurfaceCreateInfoKHR createInfo{
87+
.hwnd = glfwGetWin32Window(window),
88+
.hinstance = GetModuleHandle(nullptr)
89+
};
90+
// END c++
91+
92+
// START c
7793
VkWin32SurfaceCreateInfoKHR createInfo{};
7894
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
7995
createInfo.hwnd = glfwGetWin32Window(window);
8096
createInfo.hinstance = GetModuleHandle(nullptr);
97+
// END c
8198
----
8299

83100
The `glfwGetWin32Window` function is used to get the raw `HWND` from the GLFW
@@ -91,11 +108,18 @@ Technically, this is a WSI extension function, but it is so commonly used
91108
that the standard Vulkan loader includes it, so unlike other extensions, you
92109
don't need to explicitly load it.
93110

94-
[,c++]
111+
[source,multilang,c++,c]
112+
.Create the Win32 surface
95113
----
114+
// START c++
115+
surface = vk::raii::SurfaceKHR(instance, createInfo);
116+
// END c++
117+
118+
// START c
96119
if (vkCreateWin32SurfaceKHR(instance, &createInfo, nullptr, &surface) != VK_SUCCESS) {
97120
throw std::runtime_error("failed to create window surface!");
98121
}
122+
// END c
99123
----
100124

101125
The process is similar for other platforms like Linux, where
@@ -125,15 +149,26 @@ void createSurface() {
125149
The GLFW call takes simple parameters instead of a struct which makes the
126150
implementation of the function very straightforward:
127151

128-
[,c++]
152+
[source,multilang,c++,c]
153+
.GLFW window surface creation
129154
----
155+
// START c++
130156
void createSurface() {
131-
VkSurfaceKHR _surface;
157+
VkSurfaceKHR _surface;
132158
if (glfwCreateWindowSurface(*instance, window, nullptr, &_surface) != 0) {
133159
throw std::runtime_error("failed to create window surface!");
134160
}
135161
surface = vk::raii::SurfaceKHR(instance, _surface);
136162
}
163+
// END c++
164+
165+
// START c
166+
void createSurface() {
167+
if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
168+
throw std::runtime_error("failed to create window surface!");
169+
}
170+
}
171+
// END c
137172
----
138173

139174
However, as you see in the above, GLFW only deals with the Vulkan C API.
@@ -165,9 +200,17 @@ to our window surface. The function to check for that is
165200
queue family index and surface as parameters. Add a call to it
166201
in the same loop as the `VK_QUEUE_GRAPHICS_BIT`:
167202

168-
[,c++]
203+
[source,multilang,c++,c]
204+
.Presentation support query
169205
----
170-
VkBool32 presentSupport = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface );
206+
// START c++
207+
VkBool32 presentSupport = physicalDevice.getSurfaceSupportKHR(graphicsIndex, *surface);
208+
// END c++
209+
210+
// START c
211+
VkBool32 presentSupport = VK_FALSE;
212+
vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, graphicsIndex, surface, &presentSupport);
213+
// END c
171214
----
172215

173216
Then check the value of the boolean and store the presentation family

0 commit comments

Comments
 (0)