Skip to content

Commit 6f39a73

Browse files
committed
First attempt at a rewrite with C++ modules and raii and SLang.
1 parent 9520fc2 commit 6f39a73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+16643
-302
lines changed

code/00_base_code.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import vulkan_hpp;
2+
#include <GLFW/glfw3.h>
3+
4+
#include <iostream>
5+
#include <stdexcept>
6+
#include <cstdlib>
7+
8+
const uint32_t WIDTH = 800;
9+
const uint32_t HEIGHT = 600;
10+
11+
class HelloTriangleApplication {
12+
public:
13+
void run() {
14+
initWindow();
15+
initVulkan();
16+
mainLoop();
17+
cleanup();
18+
}
19+
20+
private:
21+
GLFWwindow* window;
22+
23+
void initWindow() {
24+
glfwInit();
25+
26+
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
27+
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
28+
29+
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
30+
}
31+
32+
void initVulkan() {
33+
34+
}
35+
36+
void mainLoop() {
37+
while (!glfwWindowShouldClose(window)) {
38+
glfwPollEvents();
39+
}
40+
}
41+
42+
void cleanup() {
43+
glfwDestroyWindow(window);
44+
45+
glfwTerminate();
46+
}
47+
};
48+
49+
int main() {
50+
HelloTriangleApplication app;
51+
52+
try {
53+
app.run();
54+
} catch (const std::exception& e) {
55+
std::cerr << e.what() << std::endl;
56+
return EXIT_FAILURE;
57+
}
58+
59+
return EXIT_SUCCESS;
60+
}

code/01_instance_creation.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
#include <stdexcept>
3+
#include <cstdlib>
4+
#include <memory>
5+
6+
import vulkan_hpp;
7+
#include <vulkan/vk_platform.h>
8+
9+
#define GLFW_INCLUDE_VULKAN // REQUIRED only for GLFW CreateWindowSurface.
10+
#include <GLFW/glfw3.h>
11+
12+
constexpr uint32_t WIDTH = 800;
13+
constexpr uint32_t HEIGHT = 600;
14+
15+
class HelloTriangleApplication {
16+
public:
17+
void run() {
18+
initWindow();
19+
initVulkan();
20+
mainLoop();
21+
cleanup();
22+
}
23+
24+
private:
25+
GLFWwindow* window = nullptr;
26+
27+
vk::raii::Context context;
28+
std::unique_ptr<vk::raii::Instance> instance;
29+
30+
void initWindow() {
31+
glfwInit();
32+
33+
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
34+
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
35+
36+
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
37+
}
38+
39+
void initVulkan() {
40+
createInstance();
41+
}
42+
43+
void mainLoop() {
44+
while (!glfwWindowShouldClose(window)) {
45+
glfwPollEvents();
46+
}
47+
}
48+
49+
void cleanup() {
50+
glfwDestroyWindow(window);
51+
52+
glfwTerminate();
53+
}
54+
55+
void createInstance() {
56+
constexpr auto appInfo = vk::ApplicationInfo("Hello Triangle", 1, "No Engine", 1, vk::ApiVersion14);
57+
uint32_t glfwExtensionCount = 0;
58+
auto glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
59+
vk::InstanceCreateInfo createInfo({}, &appInfo, {}, glfwExtensions);
60+
instance = std::make_unique<vk::raii::Instance>(context, createInfo);
61+
}
62+
};
63+
64+
int main() {
65+
try {
66+
HelloTriangleApplication app;
67+
app.run();
68+
} catch (const std::exception& e) {
69+
std::cerr << e.what() << std::endl;
70+
return EXIT_FAILURE;
71+
}
72+
73+
return EXIT_SUCCESS;
74+
}

code/02_validation_layers.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <iostream>
2+
#include <stdexcept>
3+
#include <vector>
4+
#include <cstring>
5+
#include <cstdlib>
6+
#include <memory>
7+
#include <algorithm>
8+
9+
import vulkan_hpp;
10+
#include <vulkan/vk_platform.h>
11+
12+
#define GLFW_INCLUDE_VULKAN // REQUIRED only for GLFW CreateWindowSurface.
13+
#include <GLFW/glfw3.h>
14+
15+
constexpr uint32_t WIDTH = 800;
16+
constexpr uint32_t HEIGHT = 600;
17+
18+
const std::vector validationLayers = {
19+
"VK_LAYER_KHRONOS_validation"
20+
};
21+
22+
#ifdef NDEBUG
23+
constexpr bool enableValidationLayers = false;
24+
#else
25+
constexpr bool enableValidationLayers = true;
26+
#endif
27+
28+
class HelloTriangleApplication {
29+
public:
30+
void run() {
31+
initWindow();
32+
initVulkan();
33+
mainLoop();
34+
cleanup();
35+
}
36+
37+
private:
38+
GLFWwindow* window = nullptr;
39+
40+
vk::raii::Context context;
41+
std::unique_ptr<vk::raii::Instance> instance;
42+
std::unique_ptr<vk::raii::DebugUtilsMessengerEXT> debugMessenger;
43+
44+
void initWindow() {
45+
glfwInit();
46+
47+
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
48+
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
49+
50+
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
51+
}
52+
53+
void initVulkan() {
54+
createInstance();
55+
setupDebugMessenger();
56+
}
57+
58+
void mainLoop() {
59+
while (!glfwWindowShouldClose(window)) {
60+
glfwPollEvents();
61+
}
62+
}
63+
64+
void cleanup() {
65+
glfwDestroyWindow(window);
66+
67+
glfwTerminate();
68+
}
69+
70+
void createInstance() {
71+
if (enableValidationLayers && !checkValidationLayerSupport()) {
72+
throw std::runtime_error("validation layers requested, but not available!");
73+
}
74+
75+
constexpr auto appInfo = vk::ApplicationInfo("Hello Triangle", 1, "No Engine", 1, vk::ApiVersion14);
76+
auto extensions = getRequiredExtensions();
77+
std::vector<char const *> enabledLayers;
78+
if (enableValidationLayers) {
79+
enabledLayers.assign(validationLayers.begin(), validationLayers.end());
80+
}
81+
vk::InstanceCreateInfo createInfo({}, &appInfo, enabledLayers.size(), enabledLayers.data(), extensions.size(), extensions.data());
82+
instance = std::make_unique<vk::raii::Instance>(context, createInfo);
83+
}
84+
85+
void setupDebugMessenger() {
86+
if (!enableValidationLayers) return;
87+
88+
vk::DebugUtilsMessageSeverityFlagsEXT severityFlags( vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose | vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning | vk::DebugUtilsMessageSeverityFlagBitsEXT::eError );
89+
vk::DebugUtilsMessageTypeFlagsEXT messageTypeFlags( vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance | vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation );
90+
vk::DebugUtilsMessengerCreateInfoEXT debugUtilsMessengerCreateInfoEXT({}, severityFlags, messageTypeFlags, &debugCallback);
91+
debugMessenger = std::make_unique<vk::raii::DebugUtilsMessengerEXT>( *instance, debugUtilsMessengerCreateInfoEXT );
92+
}
93+
94+
std::vector<const char*> getRequiredExtensions() {
95+
uint32_t glfwExtensionCount = 0;
96+
auto glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
97+
98+
std::vector<vk::ExtensionProperties> props = context.enumerateInstanceExtensionProperties();
99+
if (const auto propsIterator = std::ranges::find_if(props, []( vk::ExtensionProperties const & ep ) { return strcmp( ep.extensionName, vk::EXTDebugUtilsExtensionName ) == 0; } ); propsIterator == props.end() )
100+
{
101+
std::cout << "Something went very wrong, cannot find VK_EXT_debug_utils extension" << std::endl;
102+
exit( 1 );
103+
}
104+
std::vector extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
105+
if (enableValidationLayers) {
106+
extensions.push_back(vk::EXTDebugUtilsExtensionName );
107+
}
108+
109+
return extensions;
110+
}
111+
112+
bool checkValidationLayerSupport() {
113+
return (std::ranges::any_of(context.enumerateInstanceLayerProperties(),
114+
[]( vk::LayerProperties const & lp ) { return ( strcmp( "VK_LAYER_KHRONOS_validation", lp.layerName ) == 0 ); } ) );
115+
}
116+
117+
static VKAPI_ATTR vk::Bool32 VKAPI_CALL debugCallback(vk::DebugUtilsMessageSeverityFlagBitsEXT severity, vk::DebugUtilsMessageTypeFlagsEXT type, const vk::DebugUtilsMessengerCallbackDataEXT* pCallbackData, void*) {
118+
if (severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eError || severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning) {
119+
std::cerr << "validation layer: type " << to_string(type) << " msg: " << pCallbackData->pMessage << std::endl;
120+
}
121+
122+
return vk::False;
123+
}
124+
};
125+
126+
int main() {
127+
try {
128+
HelloTriangleApplication app;
129+
app.run();
130+
} catch (const std::exception& e) {
131+
std::cerr << e.what() << std::endl;
132+
return EXIT_FAILURE;
133+
}
134+
135+
return EXIT_SUCCESS;
136+
}

0 commit comments

Comments
 (0)