Skip to content

Commit 0ae859b

Browse files
committed
Refactor
1 parent c27a971 commit 0ae859b

35 files changed

+654
-1407
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
.idea/
44

55
build/
6+
shaders_copy/
67
shaders/*.spv
78
site/

docs/codes/0100_base/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cmake_minimum_required(VERSION 3.30)
2+
3+
#################################################################################
4+
#### Set up the vcpkg toolchain, which must be done before the project() function.
5+
# You can also set it up by default through the CMakePresets.json file
6+
# requires the VCPKG_ROOT environment variable
7+
# The TO_CMAKE_PATH here is used to convert back slashes to forward slashes
8+
file(TO_CMAKE_PATH "$ENV{VCPKG_ROOT}" VCPKG_CMAKE_PATH)
9+
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_CMAKE_PATH}/scripts/buildsystems/vcpkg.cmake")
10+
#################################################################################
11+
12+
project(HelloVulkan LANGUAGES CXX)
13+
14+
set(CMAKE_CXX_STANDARD 20)
15+
16+
#################################################################################
17+
#### Search for Vulkan package.
18+
# CMake provides FindVulkan support, but requires the VULKAN_SDK environment variable
19+
# Setting by default during Vulkan SDK installation, such as E: \ Vulkan \ 1.4.309.0
20+
find_package(Vulkan REQUIRED)
21+
#################################################################################
22+
23+
#################################################################################
24+
#### add_executable
25+
# Import third-party libraries through vcpkg
26+
find_package(glfw3 CONFIG REQUIRED)
27+
find_package(glm CONFIG REQUIRED)
28+
29+
# Add executable program targets
30+
add_executable(${PROJECT_NAME} src/main.cpp)
31+
32+
target_link_libraries(${PROJECT_NAME} PRIVATE Vulkan::Vulkan )
33+
target_link_libraries(${PROJECT_NAME} PRIVATE glm::glm )
34+
target_link_libraries(${PROJECT_NAME} PRIVATE glfw )
35+
#################################################################################

docs/codes/0100_base/main.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <memory>
88
#include <stdexcept>
99

10-
1110
class HelloTriangleApplication {
1211
public:
1312
void run() {
@@ -20,27 +19,21 @@ class HelloTriangleApplication {
2019
private:
2120
/////////////////////////////////////////////////////////////////
2221
/// static values
23-
static const uint32_t WIDTH = 800;
24-
static const uint32_t HEIGHT = 600;
22+
static constexpr uint32_t WIDTH = 800;
23+
static constexpr uint32_t HEIGHT = 600;
2524
/////////////////////////////////////////////////////////////////
2625

2726
/////////////////////////////////////////////////////////////////
2827
/// class member
2928
GLFWwindow* m_window{ nullptr };
3029
/////////////////////////////////////////////////////////////////
31-
32-
/////////////////////////////////////////////////////////////////
33-
/// run()
30+
3431
void initWindow() {
35-
// initialize glfw lib
3632
glfwInit();
37-
38-
// Configure GLFW to not use OpenGL
33+
3934
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
40-
// Temporarily disable window resizing to simplify operations
4135
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
42-
43-
// Create window
36+
4437
m_window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
4538
}
4639

@@ -58,7 +51,8 @@ class HelloTriangleApplication {
5851
glfwDestroyWindow( m_window );
5952
glfwTerminate();
6053
}
61-
/////////////////////////////////////////////////////////////////
54+
55+
6256
};
6357

6458
int main() {

docs/codes/0101_instance/main.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <memory>
88
#include <stdexcept>
99

10-
1110
class HelloTriangleApplication {
1211
public:
1312
void run() {
@@ -20,8 +19,8 @@ class HelloTriangleApplication {
2019
private:
2120
/////////////////////////////////////////////////////////////////
2221
/// static values
23-
static const uint32_t WIDTH = 800;
24-
static const uint32_t HEIGHT = 600;
22+
static constexpr uint32_t WIDTH = 800;
23+
static constexpr uint32_t HEIGHT = 600;
2524
/////////////////////////////////////////////////////////////////
2625

2726
/////////////////////////////////////////////////////////////////
@@ -30,19 +29,15 @@ class HelloTriangleApplication {
3029
vk::raii::Context m_context;
3130
vk::raii::Instance m_instance{ nullptr };
3231
/////////////////////////////////////////////////////////////////
33-
32+
3433
/////////////////////////////////////////////////////////////////
3534
/// run()
3635
void initWindow() {
37-
// initialize glfw lib
3836
glfwInit();
39-
40-
// Configure GLFW to not use OpenGL
37+
4138
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
42-
// Temporarily disable window resizing to simplify operations
4339
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
44-
45-
// Create window
40+
4641
m_window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
4742
}
4843

@@ -63,7 +58,7 @@ class HelloTriangleApplication {
6358
/////////////////////////////////////////////////////////////////
6459

6560
/////////////////////////////////////////////////////////////////
66-
/// create instance
61+
/// instance creation
6762
void createInstance(){
6863
vk::ApplicationInfo applicationInfo(
6964
"Hello Triangle", // pApplicationName
@@ -72,31 +67,29 @@ class HelloTriangleApplication {
7267
1, // engineVersion
7368
VK_API_VERSION_1_1 // apiVersion
7469
);
75-
70+
7671
vk::InstanceCreateInfo createInfo(
7772
{}, // vk::InstanceCreateFlags
7873
&applicationInfo // vk::ApplicationInfo*
7974
);
8075

81-
// std::vector<vk::ExtensionProperties>
82-
auto extensions = m_context.enumerateInstanceExtensionProperties();
83-
std::cout << "available extensions:\n";
84-
85-
for (const auto& extension : extensions) {
86-
std::cout << '\t' << extension.extensionName << std::endl;
87-
}
88-
8976
uint32_t glfwExtensionCount = 0;
9077
const char** glfwExtensions;
9178
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
92-
9379
std::vector<const char*> requiredExtensions( glfwExtensions, glfwExtensions + glfwExtensionCount );
80+
9481
requiredExtensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
9582

96-
// special setter
9783
createInfo.setPEnabledExtensionNames( requiredExtensions );
9884
createInfo.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
9985

86+
auto extensions = m_context.enumerateInstanceExtensionProperties();
87+
std::cout << "available extensions:\n";
88+
89+
for (const auto& extension : extensions) {
90+
std::cout << '\t' << extension.extensionName << std::endl;
91+
}
92+
10093
m_instance = m_context.createInstance( createInfo );
10194
}
10295
/////////////////////////////////////////////////////////////////

docs/codes/0101_instance/main.diff

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
diff --git a/src/main.cpp b/src/main.cpp
2-
index 91f3142..5f58927 100644
2+
index 28b69ae..334c6ed 100644
33
--- a/src/main.cpp
44
+++ b/src/main.cpp
5-
@@ -27,6 +27,8 @@ private:
5+
@@ -26,8 +26,12 @@ private:
66
/////////////////////////////////////////////////////////////////
77
/// class member
88
GLFWwindow* m_window{ nullptr };
99
+ vk::raii::Context m_context;
1010
+ vk::raii::Instance m_instance{ nullptr };
1111
/////////////////////////////////////////////////////////////////
12-
13-
/////////////////////////////////////////////////////////////////
14-
@@ -45,7 +47,7 @@ private:
12+
13+
+ /////////////////////////////////////////////////////////////////
14+
+ /// run()
15+
void initWindow() {
16+
glfwInit();
17+
18+
@@ -38,7 +42,7 @@ private:
1519
}
1620

1721
void initVulkan() {
@@ -20,13 +24,14 @@ index 91f3142..5f58927 100644
2024
}
2125

2226
void mainLoop() {
23-
@@ -59,6 +61,45 @@ private:
27+
@@ -51,8 +55,44 @@ private:
28+
glfwDestroyWindow( m_window );
2429
glfwTerminate();
2530
}
26-
/////////////////////////////////////////////////////////////////
31+
+ /////////////////////////////////////////////////////////////////
2732
+
2833
+ /////////////////////////////////////////////////////////////////
29-
+ /// create instance
34+
+ /// instance creation
3035
+ void createInstance(){
3136
+ vk::ApplicationInfo applicationInfo(
3237
+ "Hello Triangle", // pApplicationName
@@ -35,31 +40,30 @@ index 91f3142..5f58927 100644
3540
+ 1, // engineVersion
3641
+ VK_API_VERSION_1_1 // apiVersion
3742
+ );
38-
+
43+
+
3944
+ vk::InstanceCreateInfo createInfo(
4045
+ {}, // vk::InstanceCreateFlags
4146
+ &applicationInfo // vk::ApplicationInfo*
4247
+ );
4348
+
44-
+ // std::vector<vk::ExtensionProperties>
45-
+ auto extensions = m_context.enumerateInstanceExtensionProperties();
46-
+ std::cout << "available extensions:\n";
47-
+
48-
+ for (const auto& extension : extensions) {
49-
+ std::cout << '\t' << extension.extensionName << std::endl;
50-
+ }
51-
+
5249
+ uint32_t glfwExtensionCount = 0;
5350
+ const char** glfwExtensions;
5451
+ glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
55-
+
5652
+ std::vector<const char*> requiredExtensions( glfwExtensions, glfwExtensions + glfwExtensionCount );
57-
+ requiredExtensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
5853
+
59-
+ // special setter
54+
+ requiredExtensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
55+
56+
-
6057
+ createInfo.setPEnabledExtensionNames( requiredExtensions );
6158
+ createInfo.flags |= vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR;
6259
+
60+
+ auto extensions = m_context.enumerateInstanceExtensionProperties();
61+
+ std::cout << "available extensions:\n";
62+
+
63+
+ for (const auto& extension : extensions) {
64+
+ std::cout << '\t' << extension.extensionName << std::endl;
65+
+ }
66+
+
6367
+ m_instance = m_context.createInstance( createInfo );
6468
+ }
6569
+ /////////////////////////////////////////////////////////////////

docs/codes/0102_validation/main.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <memory>
1010
#include <stdexcept>
1111

12-
1312
class HelloTriangleApplication {
1413
public:
1514
void run() {
@@ -22,17 +21,17 @@ class HelloTriangleApplication {
2221
private:
2322
/////////////////////////////////////////////////////////////////
2423
/// static values
25-
static const uint32_t WIDTH = 800;
26-
static const uint32_t HEIGHT = 600;
24+
static constexpr uint32_t WIDTH = 800;
25+
static constexpr uint32_t HEIGHT = 600;
2726

28-
static constexpr std::array<const char*,1> validationLayers {
27+
inline static const std::vector<const char*> validationLayers {
2928
"VK_LAYER_KHRONOS_validation"
3029
};
3130

3231
#ifdef NDEBUG
33-
static const bool enableValidationLayers = false;
32+
static constexpr bool enableValidationLayers = false;
3433
#else
35-
static const bool enableValidationLayers = true;
34+
static constexpr bool enableValidationLayers = true;
3635
#endif
3736
/////////////////////////////////////////////////////////////////
3837

@@ -43,19 +42,15 @@ class HelloTriangleApplication {
4342
vk::raii::Instance m_instance{ nullptr };
4443
vk::raii::DebugUtilsMessengerEXT m_debugMessenger{ nullptr };
4544
/////////////////////////////////////////////////////////////////
46-
45+
4746
/////////////////////////////////////////////////////////////////
4847
/// run()
4948
void initWindow() {
50-
// initialize glfw lib
5149
glfwInit();
52-
53-
// Configure GLFW to not use OpenGL
50+
5451
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
55-
// Temporarily disable window resizing to simplify operations
5652
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
57-
58-
// Create window
53+
5954
m_window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
6055
}
6156

@@ -77,7 +72,7 @@ class HelloTriangleApplication {
7772
/////////////////////////////////////////////////////////////////
7873

7974
/////////////////////////////////////////////////////////////////
80-
/// create instance
75+
/// instance creation
8176
std::vector<const char*> getRequiredExtensions() {
8277
uint32_t glfwExtensionCount = 0;
8378
const char** glfwExtensions;
@@ -92,7 +87,6 @@ class HelloTriangleApplication {
9287

9388
return extensions;
9489
}
95-
9690
void createInstance(){
9791
if (enableValidationLayers && !checkValidationLayerSupport()) {
9892
throw std::runtime_error("validation layers requested, but not available!");
@@ -105,20 +99,12 @@ class HelloTriangleApplication {
10599
1, // engineVersion
106100
VK_API_VERSION_1_1 // apiVersion
107101
);
108-
102+
109103
vk::InstanceCreateInfo createInfo(
110104
{}, // vk::InstanceCreateFlags
111105
&applicationInfo // vk::ApplicationInfo*
112106
);
113107

114-
// std::vector<vk::ExtensionProperties>
115-
auto extensions = m_context.enumerateInstanceExtensionProperties();
116-
std::cout << "available extensions:\n";
117-
118-
for (const auto& extension : extensions) {
119-
std::cout << '\t' << extension.extensionName << std::endl;
120-
}
121-
122108
std::vector<const char*> requiredExtensions = getRequiredExtensions();
123109
// special setter
124110
createInfo.setPEnabledExtensionNames( requiredExtensions );
@@ -131,6 +117,13 @@ class HelloTriangleApplication {
131117
createInfo.pNext = &debugMessengerCreateInfo;
132118
}
133119

120+
auto extensions = m_context.enumerateInstanceExtensionProperties();
121+
std::cout << "available extensions:\n";
122+
123+
for (const auto& extension : extensions) {
124+
std::cout << '\t' << extension.extensionName << std::endl;
125+
}
126+
134127
m_instance = m_context.createInstance( createInfo );
135128
}
136129
/////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)