Compare commits

..

2 Commits

Author SHA1 Message Date
Alexandre
7ba0db3e0f Added models 2026-04-13 17:27:35 +02:00
Alexandre
4a49e774ec Fixed debug message and added 3D model loading 2026-04-08 13:56:36 +02:00
2 changed files with 120 additions and 76 deletions

View File

@ -177,7 +177,7 @@ function (add_chapter CHAPTER_NAME)
target_link_libraries (${CHAPTER_NAME} ${CHAPTER_LIBS}) target_link_libraries (${CHAPTER_NAME} ${CHAPTER_LIBS})
endif () endif ()
if (DEFINED CHAPTER_MODELS) if (DEFINED CHAPTER_MODELS)
list(TRANSFORM CHAPTER_MODELS PREPEND "${CMAKE_SOURCE_DIR}/assets/") list(TRANSFORM CHAPTER_MODELS PREPEND "${CMAKE_SOURCE_DIR}/models/")
file (COPY ${CHAPTER_MODELS} DESTINATION ${CMAKE_BINARY_DIR}/${CHAPTER_NAME}/models) file (COPY ${CHAPTER_MODELS} DESTINATION ${CMAKE_BINARY_DIR}/${CHAPTER_NAME}/models)
endif () endif ()
if (DEFINED CHAPTER_TEXTURES) if (DEFINED CHAPTER_TEXTURES)

View File

@ -1,6 +1,7 @@
//========== Includes ========== //========== Includes ==========
// General includes // General includes
#include <cstring> #include <cstring>
#include <functional>
#include <iostream> #include <iostream>
#include <array> #include <array>
#include <algorithm> // std::clamp #include <algorithm> // std::clamp
@ -16,6 +17,7 @@
#include <vector> #include <vector>
#include <stdexcept> #include <stdexcept>
#include <cstdlib> #include <cstdlib>
#include <unordered_map>
// Vulkan Includes // Vulkan Includes
#include "vulkan/vulkan.hpp" #include "vulkan/vulkan.hpp"
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
@ -32,6 +34,8 @@ import vulkan_hpp;
#define GLM_FORCE_DEPTH_ZERO_TO_ONE #define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
#include <chrono> #include <chrono>
// Images // Images
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
@ -74,6 +78,18 @@ struct Vertex {
vk::VertexInputAttributeDescription( 2, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, texCoord) ) vk::VertexInputAttributeDescription( 2, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, texCoord) )
}; };
} }
bool operator==(const Vertex& other) const {
return pos == other.pos && color == other.color && texCoord == other.texCoord;
}
};
template <> struct std::hash<Vertex> {
size_t operator() (Vertex const& vertex) const noexcept {
return ((hash<glm::vec3>() (vertex.pos) ^
(hash<glm::vec3>() (vertex.color) << 1 )) >> 1) ^
(hash<glm::vec2>() (vertex.texCoord) << 1 );
}
}; };
/* /*
@ -95,8 +111,6 @@ const std::vector<uint16_t> indices = {
}; };
*/ */
//Uniform buffer //Uniform buffer
struct UniformBufferObject { struct UniformBufferObject {
glm::mat4 model; glm::mat4 model;
@ -107,14 +121,14 @@ struct UniformBufferObject {
class HelloTriangleApplication { class HelloTriangleApplication {
public: public:
void run() { void run() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Global initialisation...\n"; std::cout << "Global initialisation...\n";
#endif #endif
initWindow(); initWindow();
initVulkan(); initVulkan();
mainLoop(); mainLoop();
cleanup(); cleanup();
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Bye Bye !\n"; std::cout << "Bye Bye !\n";
#endif #endif
@ -151,7 +165,7 @@ class HelloTriangleApplication {
vk::raii::DeviceMemory vertexBufferMemory = nullptr; vk::raii::DeviceMemory vertexBufferMemory = nullptr;
// Index - Buffers // Index - Buffers
vk::raii::Buffer indexBuffer = nullptr; vk::raii::Buffer indexBuffer = nullptr;
vk::raii::DeviceMemory indexBufferMemory = nullptr;s vk::raii::DeviceMemory indexBufferMemory = nullptr;
// Uniform Buffers - Buffers // Uniform Buffers - Buffers
std::vector<vk::raii::Buffer> uniformBuffers; std::vector<vk::raii::Buffer> uniformBuffers;
std::vector<vk::raii::DeviceMemory> uniformBuffersMemory; std::vector<vk::raii::DeviceMemory> uniformBuffersMemory;
@ -186,7 +200,7 @@ class HelloTriangleApplication {
//========== Inits ========== //========== Inits ==========
void initWindow() { void initWindow() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating GLFW window...\n"; std::cout << "Creating GLFW window...\n";
#endif #endif
glfwInit(); glfwInit();
@ -198,13 +212,13 @@ class HelloTriangleApplication {
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
glfwSetWindowUserPointer(window, this); glfwSetWindowUserPointer(window, this);
glfwSetFramebufferSizeCallback(window, framebufferResizeCallback); glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Window created !\n"; std::cout << "Window created !\n";
#endif #endif
} }
void initVulkan() { void initVulkan() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "\tInitialing Vulkan...\n"; std::cout << "\tInitialing Vulkan...\n";
#endif #endif
createInstance(); createInstance();
@ -221,6 +235,7 @@ class HelloTriangleApplication {
createTextureImage(); createTextureImage();
createTextureImageView(); createTextureImageView();
createTextureSampler(); createTextureSampler();
loadModel();
createVertexBuffer(); createVertexBuffer();
createIndexBuffer(); createIndexBuffer();
createUniformBuffers(); createUniformBuffers();
@ -228,13 +243,13 @@ class HelloTriangleApplication {
createDescriptorSets(); createDescriptorSets();
createCommandBuffers(); createCommandBuffers();
createSyncObjects(); createSyncObjects();
#ifdef DEBUG #ifndef NDEBUG
std::cout << "\tVulkan is initialised !\n"; std::cout << "\tVulkan is initialised !\n";
#endif #endif
} }
void mainLoop() { void mainLoop() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Main Loop...\n"; std::cout << "Main Loop...\n";
#endif #endif
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
@ -250,14 +265,14 @@ class HelloTriangleApplication {
} }
void cleanup() { void cleanup() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Cleaning up...\n"; std::cout << "Cleaning up...\n";
#endif #endif
cleanupSwapChain(); cleanupSwapChain();
surface = nullptr; surface = nullptr;
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Done !\n"; std::cout << "Done !\n";
#endif #endif
} }
@ -265,7 +280,7 @@ class HelloTriangleApplication {
//========== UTILS ========== //========== UTILS ==========
void createInstance() { void createInstance() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a Vulkan Instance...\n"; std::cout << "Creating a Vulkan Instance...\n";
#endif #endif
constexpr vk::ApplicationInfo appInfo{ constexpr vk::ApplicationInfo appInfo{
@ -315,7 +330,7 @@ class HelloTriangleApplication {
.ppEnabledExtensionNames = requiredExtensions.data()}; .ppEnabledExtensionNames = requiredExtensions.data()};
instance = vk::raii::Instance(context, createInfo); instance = vk::raii::Instance(context, createInfo);
#ifdef DEBUG #ifndef NDEBUG
std::cout << requiredExtensions.size() << " extensions loaded !\n"; std::cout << requiredExtensions.size() << " extensions loaded !\n";
std::cout << requiredLayers.size() << " layers loaded !\n"; std::cout << requiredLayers.size() << " layers loaded !\n";
@ -324,7 +339,7 @@ class HelloTriangleApplication {
} }
void setupDebugMessenger () { void setupDebugMessenger () {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Setting up the messenger !\n"; std::cout << "Setting up the messenger !\n";
#endif #endif
if (!enableValidationLayers) { if (!enableValidationLayers) {
@ -339,13 +354,13 @@ class HelloTriangleApplication {
.pfnUserCallback = &debugCallback }; .pfnUserCallback = &debugCallback };
debugMessenger = instance.createDebugUtilsMessengerEXT(debugUtilsMessengerCreateInfoEXT); debugMessenger = instance.createDebugUtilsMessengerEXT(debugUtilsMessengerCreateInfoEXT);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Messenger set up !\n"; std::cout << "Messenger set up !\n";
#endif #endif
} }
void pickPhysicalDevice() { void pickPhysicalDevice() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Selecting the GPU...\n"; std::cout << "Selecting the GPU...\n";
#endif #endif
std::vector<vk::raii::PhysicalDevice> devices = instance.enumeratePhysicalDevices(); std::vector<vk::raii::PhysicalDevice> devices = instance.enumeratePhysicalDevices();
@ -362,7 +377,7 @@ class HelloTriangleApplication {
}); });
if (devIter != devices.end()) { if (devIter != devices.end()) {
physicalDevice = *devIter; physicalDevice = *devIter;
#ifdef DEBUG #ifndef NDEBUG
std::cout << "GPU selected !\n"; std::cout << "GPU selected !\n";
#endif #endif
} }
@ -373,7 +388,7 @@ class HelloTriangleApplication {
void createLogicalDevice() { void createLogicalDevice() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a logical device !\n"; std::cout << "Creating a logical device !\n";
#endif #endif
@ -412,13 +427,13 @@ class HelloTriangleApplication {
device = vk::raii::Device( physicalDevice, deviceCreateInfo ); device = vk::raii::Device( physicalDevice, deviceCreateInfo );
queue = vk::raii::Queue( device, queueIndex, 0 ); queue = vk::raii::Queue( device, queueIndex, 0 );
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Logical device created\n"; std::cout << "Logical device created\n";
#endif #endif
} }
void createSurface() { void createSurface() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a surface !\n"; std::cout << "Creating a surface !\n";
#endif #endif
VkSurfaceKHR _surface; VkSurfaceKHR _surface;
@ -426,13 +441,13 @@ class HelloTriangleApplication {
throw std::runtime_error("Failed to create Window surface !\nAborting..."); throw std::runtime_error("Failed to create Window surface !\nAborting...");
} }
surface = vk::raii::SurfaceKHR(instance, _surface); surface = vk::raii::SurfaceKHR(instance, _surface);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Surface created !\n"; std::cout << "Surface created !\n";
#endif #endif
} }
void createSwapChain() { void createSwapChain() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating Swap Chain...\n"; std::cout << "Creating Swap Chain...\n";
#endif #endif
auto surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR(*surface); auto surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR(*surface);
@ -460,13 +475,13 @@ class HelloTriangleApplication {
swapChain = vk::raii::SwapchainKHR(device, swapChainCreateInfo); swapChain = vk::raii::SwapchainKHR(device, swapChainCreateInfo);
swapChainImages = swapChain.getImages(); swapChainImages = swapChain.getImages();
swapChainImageFormat = swapChainSurfaceFormat.format; swapChainImageFormat = swapChainSurfaceFormat.format;
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Swap Chain Created !\n"; std::cout << "Swap Chain Created !\n";
#endif #endif
} }
void createImageViews() { void createImageViews() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating Image View...\n"; std::cout << "Creating Image View...\n";
#endif #endif
swapChainImageViews.clear(); swapChainImageViews.clear();
@ -478,13 +493,13 @@ class HelloTriangleApplication {
vk::ImageAspectFlagBits::eColor vk::ImageAspectFlagBits::eColor
)); ));
} }
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Image View created !\n"; std::cout << "Image View created !\n";
#endif #endif
} }
void createDescriptorSetLayout() { void createDescriptorSetLayout() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating the descriptor set...\n"; std::cout << "Creating the descriptor set...\n";
#endif #endif
std::array bindings = { std::array bindings = {
@ -510,13 +525,13 @@ class HelloTriangleApplication {
descriptorSetLayout = vk::raii::DescriptorSetLayout(device, layoutInfo); descriptorSetLayout = vk::raii::DescriptorSetLayout(device, layoutInfo);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Descriptor set created !\n"; std::cout << "Descriptor set created !\n";
#endif #endif
} }
void createGraphicsPipeline() { void createGraphicsPipeline() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating the Graphics Pipeline...\n"; std::cout << "Creating the Graphics Pipeline...\n";
#endif #endif
vk::raii::ShaderModule shaderModule = createShaderModule(readFile("shaders/slang.spv")); vk::raii::ShaderModule shaderModule = createShaderModule(readFile("shaders/slang.spv"));
@ -640,13 +655,13 @@ class HelloTriangleApplication {
graphicsPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo); graphicsPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Pipeline created !\n"; std::cout << "Pipeline created !\n";
#endif #endif
} }
void createCommandPool() { void createCommandPool() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a command pool\n"; std::cout << "Creating a command pool\n";
#endif #endif
vk::CommandPoolCreateInfo poolInfo { vk::CommandPoolCreateInfo poolInfo {
@ -655,13 +670,13 @@ class HelloTriangleApplication {
}; };
commandPool = vk::raii::CommandPool(device, poolInfo); commandPool = vk::raii::CommandPool(device, poolInfo);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Command pool created\n"; std::cout << "Command pool created\n";
#endif #endif
} }
void createDepthResources() { void createDepthResources() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a depth ressouce...\n"; std::cout << "Creating a depth ressouce...\n";
#endif #endif
vk::Format depthFormat = findDepthFormat(); vk::Format depthFormat = findDepthFormat();
@ -681,13 +696,13 @@ class HelloTriangleApplication {
depthFormat, depthFormat,
vk::ImageAspectFlagBits::eDepth vk::ImageAspectFlagBits::eDepth
); );
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Depth ressouce created !\n"; std::cout << "Depth ressouce created !\n";
#endif #endif
} }
void createTextureImage(){ void createTextureImage(){
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a texture image...\n"; std::cout << "Creating a texture image...\n";
#endif #endif
int texWidth, texHeight, texChannels; int texWidth, texHeight, texChannels;
@ -728,13 +743,13 @@ class HelloTriangleApplication {
copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight)); copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
transitionImageLayout(textureImage, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal); transitionImageLayout(textureImage, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Texture image created !\n"; std::cout << "Texture image created !\n";
#endif #endif
} }
void createTextureImageView() { void createTextureImageView() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating the texture image...\n"; std::cout << "Creating the texture image...\n";
#endif #endif
textureImageView = createImageView( textureImageView = createImageView(
@ -742,7 +757,7 @@ class HelloTriangleApplication {
vk::Format::eR8G8B8A8Srgb, vk::Format::eR8G8B8A8Srgb,
vk::ImageAspectFlagBits::eColor vk::ImageAspectFlagBits::eColor
); );
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Texture Image created !\n"; std::cout << "Texture Image created !\n";
#endif #endif
} }
@ -757,7 +772,7 @@ class HelloTriangleApplication {
vk::raii::Image &image, vk::raii::Image &image,
vk::raii::DeviceMemory &imageMemory vk::raii::DeviceMemory &imageMemory
) { ) {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating an image with usage " << vk::to_string(usage) << '\n'; std::cout << "Creating an image with usage " << vk::to_string(usage) << '\n';
#endif #endif
vk::ImageCreateInfo imageInfo { vk::ImageCreateInfo imageInfo {
@ -782,13 +797,13 @@ class HelloTriangleApplication {
imageMemory = vk::raii::DeviceMemory(device, allocInfo); imageMemory = vk::raii::DeviceMemory(device, allocInfo);
image.bindMemory(imageMemory, 0); image.bindMemory(imageMemory, 0);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Created image with usage " << vk::to_string(usage) << '\n'; std::cout << "Created image with usage " << vk::to_string(usage) << '\n';
#endif #endif
} }
void createTextureSampler() { void createTextureSampler() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a texture sampler...\n"; std::cout << "Creating a texture sampler...\n";
#endif #endif
vk::PhysicalDeviceProperties properties = physicalDevice.getProperties(); vk::PhysicalDeviceProperties properties = physicalDevice.getProperties();
@ -812,13 +827,13 @@ class HelloTriangleApplication {
textureSampler = vk::raii::Sampler(device, samplerInfo); textureSampler = vk::raii::Sampler(device, samplerInfo);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Texture sampler created !\n"; std::cout << "Texture sampler created !\n";
#endif #endif
} }
void loadModel() { void loadModel() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Loading the 3D model...\n"; std::cout << "Loading the 3D model...\n";
#endif #endif
@ -827,17 +842,46 @@ class HelloTriangleApplication {
std::vector<tinyobj::material_t> materials; std::vector<tinyobj::material_t> materials;
std::string warn, err; std::string warn, err;
if (!tinyobj::LoadObj(&attrib, &matrials, &warn, &err, MODEL_PATH.c_str())) { if (!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, MODEL_PATH.c_str())) {
throw std::runtime_error(warn + err); throw std::runtime_error(warn + err);
} }
#ifdef DEBUG std::unordered_map<Vertex, uint32_t> uniqueVertices{};
for (const auto& shape : shapes) {
for (const auto& index : shape.mesh.indices) {
Vertex vertex{};
vertex.pos = {
attrib.vertices[3 * index.vertex_index + 0],
attrib.vertices[3 * index.vertex_index + 1],
attrib.vertices[3 * index.vertex_index + 2]
};
vertex.texCoord = {
attrib.texcoords[2 * index.texcoord_index + 0],
1.0f - attrib.texcoords[2 * index.texcoord_index + 1]
};
vertex.color = {1.0f, 1.0f, 1.0f};
if (uniqueVertices.count(vertex) == 0) {
uniqueVertices[vertex] = static_cast<uint32_t>(vertices.size());
vertices.push_back(vertex);
}
indices.push_back(uniqueVertices[vertex]);
}
}
#ifndef NDEBUG
std::cout << "3D model loaded !\n"; std::cout << "3D model loaded !\n";
std::cout << "3D model has " << vertices.size() << " vertices and " << indices.size() << " index \n";
#endif #endif
} }
void createVertexBuffer() { void createVertexBuffer() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a vertex buffer...\n"; std::cout << "Creating a vertex buffer...\n";
#endif #endif
vk::raii::Buffer stagingBuffer = nullptr; vk::raii::Buffer stagingBuffer = nullptr;
@ -864,7 +908,7 @@ class HelloTriangleApplication {
vertexBufferMemory vertexBufferMemory
); );
copyBuffer(stagingBuffer, vertexBuffer, bufferSize); copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Vertex buffer created !\n"; std::cout << "Vertex buffer created !\n";
#endif #endif
} }
@ -873,8 +917,8 @@ class HelloTriangleApplication {
vk::raii::Buffer & srcBuffer, vk::raii::Buffer & srcBuffer,
vk::raii::Buffer & dstBuffer, vk::raii::Buffer & dstBuffer,
vk::DeviceSize size vk::DeviceSize size
) { ){
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Copying buffer " << vk::to_string(srcBuffer.objectType) << " to " << vk::to_string(dstBuffer.objectType) << "...\n"; std::cout << "Copying buffer " << vk::to_string(srcBuffer.objectType) << " to " << vk::to_string(dstBuffer.objectType) << "...\n";
#endif #endif
vk::raii::CommandBuffer commandCopyBuffer = beginSingleTimeCommands(); vk::raii::CommandBuffer commandCopyBuffer = beginSingleTimeCommands();
@ -883,13 +927,13 @@ class HelloTriangleApplication {
endSingleTimeCommands(commandCopyBuffer); endSingleTimeCommands(commandCopyBuffer);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Copied buffer " << vk::to_string(srcBuffer.objectType) << " to " << vk::to_string(dstBuffer.objectType) << "!\n"; std::cout << "Copied buffer " << vk::to_string(srcBuffer.objectType) << " to " << vk::to_string(dstBuffer.objectType) << "!\n";
#endif #endif
} }
void createIndexBuffer() { void createIndexBuffer() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating an index buffer...\n"; std::cout << "Creating an index buffer...\n";
#endif #endif
vk::DeviceSize bufferSize = sizeof( indices[0] ) * indices.size(); vk::DeviceSize bufferSize = sizeof( indices[0] ) * indices.size();
@ -917,13 +961,13 @@ class HelloTriangleApplication {
); );
copyBuffer(stagingBuffer, indexBuffer, bufferSize); copyBuffer(stagingBuffer, indexBuffer, bufferSize);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Index buffer created !\n"; std::cout << "Index buffer created !\n";
#endif #endif
} }
void createUniformBuffers() { void createUniformBuffers() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating uniform buffers...\n"; std::cout << "Creating uniform buffers...\n";
#endif #endif
uniformBuffers.clear(); uniformBuffers.clear();
@ -946,13 +990,13 @@ class HelloTriangleApplication {
uniformBuffersMemory.emplace_back(std::move(bufferMem)); uniformBuffersMemory.emplace_back(std::move(bufferMem));
uniformBuffersMapped.emplace_back( uniformBuffersMemory[i].mapMemory(0, bufferSize)); uniformBuffersMapped.emplace_back( uniformBuffersMemory[i].mapMemory(0, bufferSize));
} }
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Uniform buffers created !\n"; std::cout << "Uniform buffers created !\n";
#endif #endif
} }
void createDescriptorPool() { void createDescriptorPool() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a descriptor pool...\n"; std::cout << "Creating a descriptor pool...\n";
#endif #endif
std::array poolSize { std::array poolSize {
@ -974,13 +1018,13 @@ class HelloTriangleApplication {
descriptorPool = vk::raii::DescriptorPool(device, poolInfo); descriptorPool = vk::raii::DescriptorPool(device, poolInfo);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Descriptor pool created !\n"; std::cout << "Descriptor pool created !\n";
#endif #endif
} }
void createDescriptorSets() { void createDescriptorSets() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a descriptor set...\n"; std::cout << "Creating a descriptor set...\n";
#endif #endif
std::vector<vk::DescriptorSetLayout> layouts( MAX_FRAMES_IN_FLIGHT, *descriptorSetLayout ); std::vector<vk::DescriptorSetLayout> layouts( MAX_FRAMES_IN_FLIGHT, *descriptorSetLayout );
@ -1026,13 +1070,13 @@ class HelloTriangleApplication {
device.updateDescriptorSets(descriptorWrites, {}); device.updateDescriptorSets(descriptorWrites, {});
} }
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Descriptor sets created !\n"; std::cout << "Descriptor sets created !\n";
#endif #endif
} }
void createCommandBuffers() { void createCommandBuffers() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a command buffers\n"; std::cout << "Creating a command buffers\n";
#endif #endif
@ -1043,7 +1087,7 @@ class HelloTriangleApplication {
}; };
commandBuffers = vk::raii::CommandBuffers(device, allocInfo); commandBuffers = vk::raii::CommandBuffers(device, allocInfo);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Command buffer created\n"; std::cout << "Command buffer created\n";
#endif #endif
} }
@ -1251,7 +1295,7 @@ class HelloTriangleApplication {
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count(); float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
UniformBufferObject ubo {}; UniformBufferObject ubo {};
ubo.model = rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.model = rotate(glm::mat4(1.0f), time/10 * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
ubo.view = lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.view = lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
ubo.proj = glm::perspective(glm::radians(45.0f), static_cast<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height), 0.1f, 10.0f); ubo.proj = glm::perspective(glm::radians(45.0f), static_cast<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height), 0.1f, 10.0f);
ubo.proj[1][1] *= -1; ubo.proj[1][1] *= -1;
@ -1269,11 +1313,11 @@ class HelloTriangleApplication {
auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr); auto [result, imageIndex] = swapChain.acquireNextImage(UINT64_MAX, *presentCompleteSemaphores[frameIndex], nullptr);
if (result == vk::Result::eErrorOutOfDateKHR) { if (result == vk::Result::eErrorOutOfDateKHR) {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "SwapChain out of date ! \n"; std::cout << "SwapChain out of date ! \n";
#endif #endif
recreateSwapChain(); recreateSwapChain();
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Fixed the swapchain !\n"; std::cout << "Fixed the swapchain !\n";
#endif #endif
return; return;
@ -1338,7 +1382,7 @@ class HelloTriangleApplication {
} }
void recreateSwapChain() { void recreateSwapChain() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Re-creating the SwapChain...\n"; std::cout << "Re-creating the SwapChain...\n";
#endif #endif
@ -1358,7 +1402,7 @@ class HelloTriangleApplication {
createImageViews(); createImageViews();
createDepthResources(); createDepthResources();
#ifdef DEBUG #ifndef NDEBUG
std::cout << "SwapChain re-created sucessfully !\n"; std::cout << "SwapChain re-created sucessfully !\n";
#endif #endif
} }
@ -1370,7 +1414,7 @@ class HelloTriangleApplication {
vk::raii::Buffer& buffer, vk::raii::Buffer& buffer,
vk::raii::DeviceMemory& bufferMemory vk::raii::DeviceMemory& bufferMemory
) { ) {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating a buffer of type: " << vk::to_string(usage) << '\n'; std::cout << "Creating a buffer of type: " << vk::to_string(usage) << '\n';
#endif #endif
@ -1389,7 +1433,7 @@ class HelloTriangleApplication {
bufferMemory = vk::raii::DeviceMemory(device, allocInfo); bufferMemory = vk::raii::DeviceMemory(device, allocInfo);
buffer.bindMemory(*bufferMemory, 0); buffer.bindMemory(*bufferMemory, 0);
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Buffer of type: " << vk::to_string(usage) << " sucessfully created !\n"; std::cout << "Buffer of type: " << vk::to_string(usage) << " sucessfully created !\n";
#endif #endif
} }
@ -1401,7 +1445,7 @@ class HelloTriangleApplication {
void * void *
){ ){
if (severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eError || severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning) { if (severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eError || severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning) {
#ifdef DEBUG #ifndef NDEBUG
std::cerr << "Validation layer: type " << to_string(type) << " msg: " << pCallbackData->pMessage << std::endl; std::cerr << "Validation layer: type " << to_string(type) << " msg: " << pCallbackData->pMessage << std::endl;
#endif #endif
} }
@ -1437,7 +1481,7 @@ class HelloTriangleApplication {
} }
uint32_t findQueueFamilies(vk::raii::PhysicalDevice physicalDevice) { uint32_t findQueueFamilies(vk::raii::PhysicalDevice physicalDevice) {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Selecting a queue...\n"; std::cout << "Selecting a queue...\n";
#endif #endif
// Get the index of the first queue that support graphics // Get the index of the first queue that support graphics
@ -1446,7 +1490,7 @@ class HelloTriangleApplication {
for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) { for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++) {
if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) { if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) && physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface)) {
// The queue has both graphics and present // The queue has both graphics and present
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Found a graphics and present queue !\n"; std::cout << "Found a graphics and present queue !\n";
#endif #endif
queueIndex = qfpIndex; queueIndex = qfpIndex;
@ -1489,7 +1533,7 @@ class HelloTriangleApplication {
} }
static std::vector<char> readFile(const std::string& filename) { static std::vector<char> readFile(const std::string& filename) {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Loading " << filename << " from disk...\n"; std::cout << "Loading " << filename << " from disk...\n";
#endif #endif
std::ifstream file(filename, std::ios::ate | std::ios::binary); std::ifstream file(filename, std::ios::ate | std::ios::binary);
@ -1502,14 +1546,14 @@ class HelloTriangleApplication {
file.seekg(0, std::ios::beg); file.seekg(0, std::ios::beg);
file.read(buffer.data(), static_cast<std::streamsize>(buffer.size())); file.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
file.close(); file.close();
#ifdef DEBUG #ifndef NDEBUG
std::cout << "File loaded with size of " << buffer.size() << '\n'; std::cout << "File loaded with size of " << buffer.size() << '\n';
#endif #endif
return buffer; return buffer;
} }
[[nodiscard]] vk::raii::ShaderModule createShaderModule(const std::vector<char>& code) const { [[nodiscard]] vk::raii::ShaderModule createShaderModule(const std::vector<char>& code) const {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Creating shader module\n"; std::cout << "Creating shader module\n";
#endif #endif
vk::ShaderModuleCreateInfo createInfo{ vk::ShaderModuleCreateInfo createInfo{
@ -1517,7 +1561,7 @@ class HelloTriangleApplication {
.pCode = reinterpret_cast<const uint32_t*>(code.data()) .pCode = reinterpret_cast<const uint32_t*>(code.data())
}; };
vk::raii::ShaderModule shaderModule { device, createInfo }; vk::raii::ShaderModule shaderModule { device, createInfo };
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Shader module created\n"; std::cout << "Shader module created\n";
#endif #endif
return shaderModule; return shaderModule;
@ -1608,7 +1652,7 @@ class HelloTriangleApplication {
}; };
int main() { int main() {
#ifdef DEBUG #ifndef NDEBUG
std::cout << "Starting the app...\n"; std::cout << "Starting the app...\n";
#endif #endif
HelloTriangleApplication app; HelloTriangleApplication app;