Skip to content

Commit 5d22baa

Browse files
committed
Create Surface
1 parent a2df2dd commit 5d22baa

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

guide/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
- [Initialization](initialization/README.md)
1212
- [GLFW Window](initialization/glfw_window.md)
1313
- [Vulkan Instance](initialization/instance.md)
14+
- [Vulkan Surface](initialization/surface.md)
1415

guide/src/initialization/surface.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Vulkan Surface
2+
3+
Being platform agnostic, Vulkan interfaces with the WSI via the [`VK_KHR_surface` extension](https://registry.khronos.org/vulkan/specs/latest/man/html/VK_KHR_surface.html).
4+
5+
Add another helper function in `window.hpp/cpp`:
6+
7+
```cpp
8+
auto glfw::create_surface(GLFWwindow* window, vk::Instance const instance)
9+
-> vk::UniqueSurfaceKHR {
10+
VkSurfaceKHR ret{};
11+
auto const result =
12+
glfwCreateWindowSurface(instance, window, nullptr, &ret);
13+
if (result != VK_SUCCESS || ret == VkSurfaceKHR{}) {
14+
throw std::runtime_error{"Failed to create Vulkan Surface"};
15+
}
16+
return vk::UniqueSurfaceKHR{ret, instance};
17+
}
18+
```
19+
20+
Add corrseponding members to `App` and create the surface:
21+
22+
```cpp
23+
void App::create_surface() {
24+
m_surface = glfw::create_surface(m_window.get(), *m_instance);
25+
}
26+
```

src/app.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ constexpr auto vk_version_v = VK_MAKE_VERSION(1, 3, 0);
1111
void App::run() {
1212
create_window();
1313
create_instance();
14+
create_surface();
1415

1516
main_loop();
1617
}
@@ -38,6 +39,10 @@ void App::create_instance() {
3839
VULKAN_HPP_DEFAULT_DISPATCHER.init(*m_instance);
3940
}
4041

42+
void App::create_surface() {
43+
m_surface = glfw::create_surface(m_window.get(), *m_instance);
44+
}
45+
4146
void App::main_loop() {
4247
while (glfwWindowShouldClose(m_window.get()) == GLFW_FALSE) {
4348
glfwPollEvents();

src/app.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ class App {
1010
private:
1111
void create_window();
1212
void create_instance();
13+
void create_surface();
1314

1415
void main_loop();
1516

1617
glfw::Window m_window{};
1718
vk::UniqueInstance m_instance{};
19+
vk::UniqueSurfaceKHR m_surface{};
1820
};
1921
} // namespace lvk

src/window.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ auto glfw::create_window(glm::ivec2 const size, char const* title) -> Window {
2424
throw std::runtime_error{"Vulkan not supported"};
2525
}
2626
auto ret = Window{};
27-
// tell GLFW we don't want an OpenGL context
27+
// tell GLFW that we don't want an OpenGL context.
2828
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
2929
ret.reset(glfwCreateWindow(size.x, size.y, title, nullptr, nullptr));
3030
if (!ret) { throw std::runtime_error{"Failed to create GLFW Window"}; }
@@ -36,4 +36,15 @@ auto glfw::instance_extensions() -> std::span<char const* const> {
3636
auto const* extensions = glfwGetRequiredInstanceExtensions(&count);
3737
return {extensions, static_cast<std::size_t>(count)};
3838
}
39+
40+
auto glfw::create_surface(GLFWwindow* window, vk::Instance const instance)
41+
-> vk::UniqueSurfaceKHR {
42+
VkSurfaceKHR ret{};
43+
auto const result =
44+
glfwCreateWindowSurface(instance, window, nullptr, &ret);
45+
if (result != VK_SUCCESS || ret == VkSurfaceKHR{}) {
46+
throw std::runtime_error{"Failed to create Vulkan Surface"};
47+
}
48+
return vk::UniqueSurfaceKHR{ret, instance};
49+
}
3950
} // namespace lvk

src/window.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <GLFW/glfw3.h>
33
#include <glm/vec2.hpp>
4+
#include <vulkan/vulkan.hpp>
45
#include <memory>
56
#include <span>
67

@@ -15,4 +16,7 @@ using Window = std::unique_ptr<GLFWwindow, Deleter>;
1516
[[nodiscard]] auto create_window(glm::ivec2 size, char const* title) -> Window;
1617

1718
[[nodiscard]] auto instance_extensions() -> std::span<char const* const>;
19+
20+
[[nodiscard]] auto create_surface(GLFWwindow* window, vk::Instance instance)
21+
-> vk::UniqueSurfaceKHR;
1822
} // namespace lvk::glfw

0 commit comments

Comments
 (0)