Compare commits

..

No commits in common. "7ba0db3e0f8bd9195efc16a3f2286b55e02b57f1" and "8f4ff25734688b2c2e5f0a02a61fd83872b7c5d8" have entirely different histories.

2 changed files with 76 additions and 120 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}/models/") list(TRANSFORM CHAPTER_MODELS PREPEND "${CMAKE_SOURCE_DIR}/assets/")
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,7 +1,6 @@
//========== 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
@ -17,7 +16,6 @@
#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>
@ -34,8 +32,6 @@ 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
@ -78,18 +74,6 @@ 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 );
}
}; };
/* /*
@ -111,6 +95,8 @@ const std::vector<uint16_t> indices = {
}; };
*/ */
//Uniform buffer //Uniform buffer
struct UniformBufferObject { struct UniformBufferObject {
glm::mat4 model; glm::mat4 model;
@ -121,14 +107,14 @@ struct UniformBufferObject {
class HelloTriangleApplication { class HelloTriangleApplication {
public: public:
void run() { void run() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Global initialisation...\n"; std::cout << "Global initialisation...\n";
#endif #endif
initWindow(); initWindow();
initVulkan(); initVulkan();
mainLoop(); mainLoop();
cleanup(); cleanup();
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Bye Bye !\n"; std::cout << "Bye Bye !\n";
#endif #endif
@ -165,7 +151,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; vk::raii::DeviceMemory indexBufferMemory = nullptr;s
// 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;
@ -200,7 +186,7 @@ class HelloTriangleApplication {
//========== Inits ========== //========== Inits ==========
void initWindow() { void initWindow() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating GLFW window...\n"; std::cout << "Creating GLFW window...\n";
#endif #endif
glfwInit(); glfwInit();
@ -212,13 +198,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);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Window created !\n"; std::cout << "Window created !\n";
#endif #endif
} }
void initVulkan() { void initVulkan() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "\tInitialing Vulkan...\n"; std::cout << "\tInitialing Vulkan...\n";
#endif #endif
createInstance(); createInstance();
@ -235,7 +221,6 @@ class HelloTriangleApplication {
createTextureImage(); createTextureImage();
createTextureImageView(); createTextureImageView();
createTextureSampler(); createTextureSampler();
loadModel();
createVertexBuffer(); createVertexBuffer();
createIndexBuffer(); createIndexBuffer();
createUniformBuffers(); createUniformBuffers();
@ -243,13 +228,13 @@ class HelloTriangleApplication {
createDescriptorSets(); createDescriptorSets();
createCommandBuffers(); createCommandBuffers();
createSyncObjects(); createSyncObjects();
#ifndef NDEBUG #ifdef DEBUG
std::cout << "\tVulkan is initialised !\n"; std::cout << "\tVulkan is initialised !\n";
#endif #endif
} }
void mainLoop() { void mainLoop() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Main Loop...\n"; std::cout << "Main Loop...\n";
#endif #endif
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
@ -265,14 +250,14 @@ class HelloTriangleApplication {
} }
void cleanup() { void cleanup() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Cleaning up...\n"; std::cout << "Cleaning up...\n";
#endif #endif
cleanupSwapChain(); cleanupSwapChain();
surface = nullptr; surface = nullptr;
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Done !\n"; std::cout << "Done !\n";
#endif #endif
} }
@ -280,7 +265,7 @@ class HelloTriangleApplication {
//========== UTILS ========== //========== UTILS ==========
void createInstance() { void createInstance() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating a Vulkan Instance...\n"; std::cout << "Creating a Vulkan Instance...\n";
#endif #endif
constexpr vk::ApplicationInfo appInfo{ constexpr vk::ApplicationInfo appInfo{
@ -330,7 +315,7 @@ class HelloTriangleApplication {
.ppEnabledExtensionNames = requiredExtensions.data()}; .ppEnabledExtensionNames = requiredExtensions.data()};
instance = vk::raii::Instance(context, createInfo); instance = vk::raii::Instance(context, createInfo);
#ifndef NDEBUG #ifdef DEBUG
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";
@ -339,7 +324,7 @@ class HelloTriangleApplication {
} }
void setupDebugMessenger () { void setupDebugMessenger () {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Setting up the messenger !\n"; std::cout << "Setting up the messenger !\n";
#endif #endif
if (!enableValidationLayers) { if (!enableValidationLayers) {
@ -354,13 +339,13 @@ class HelloTriangleApplication {
.pfnUserCallback = &debugCallback }; .pfnUserCallback = &debugCallback };
debugMessenger = instance.createDebugUtilsMessengerEXT(debugUtilsMessengerCreateInfoEXT); debugMessenger = instance.createDebugUtilsMessengerEXT(debugUtilsMessengerCreateInfoEXT);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Messenger set up !\n"; std::cout << "Messenger set up !\n";
#endif #endif
} }
void pickPhysicalDevice() { void pickPhysicalDevice() {
#ifndef NDEBUG #ifdef DEBUG
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();
@ -377,7 +362,7 @@ class HelloTriangleApplication {
}); });
if (devIter != devices.end()) { if (devIter != devices.end()) {
physicalDevice = *devIter; physicalDevice = *devIter;
#ifndef NDEBUG #ifdef DEBUG
std::cout << "GPU selected !\n"; std::cout << "GPU selected !\n";
#endif #endif
} }
@ -388,7 +373,7 @@ class HelloTriangleApplication {
void createLogicalDevice() { void createLogicalDevice() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating a logical device !\n"; std::cout << "Creating a logical device !\n";
#endif #endif
@ -427,13 +412,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 );
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Logical device created\n"; std::cout << "Logical device created\n";
#endif #endif
} }
void createSurface() { void createSurface() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating a surface !\n"; std::cout << "Creating a surface !\n";
#endif #endif
VkSurfaceKHR _surface; VkSurfaceKHR _surface;
@ -441,13 +426,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);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Surface created !\n"; std::cout << "Surface created !\n";
#endif #endif
} }
void createSwapChain() { void createSwapChain() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating Swap Chain...\n"; std::cout << "Creating Swap Chain...\n";
#endif #endif
auto surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR(*surface); auto surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR(*surface);
@ -475,13 +460,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;
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Swap Chain Created !\n"; std::cout << "Swap Chain Created !\n";
#endif #endif
} }
void createImageViews() { void createImageViews() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating Image View...\n"; std::cout << "Creating Image View...\n";
#endif #endif
swapChainImageViews.clear(); swapChainImageViews.clear();
@ -493,13 +478,13 @@ class HelloTriangleApplication {
vk::ImageAspectFlagBits::eColor vk::ImageAspectFlagBits::eColor
)); ));
} }
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Image View created !\n"; std::cout << "Image View created !\n";
#endif #endif
} }
void createDescriptorSetLayout() { void createDescriptorSetLayout() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating the descriptor set...\n"; std::cout << "Creating the descriptor set...\n";
#endif #endif
std::array bindings = { std::array bindings = {
@ -525,13 +510,13 @@ class HelloTriangleApplication {
descriptorSetLayout = vk::raii::DescriptorSetLayout(device, layoutInfo); descriptorSetLayout = vk::raii::DescriptorSetLayout(device, layoutInfo);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Descriptor set created !\n"; std::cout << "Descriptor set created !\n";
#endif #endif
} }
void createGraphicsPipeline() { void createGraphicsPipeline() {
#ifndef NDEBUG #ifdef DEBUG
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"));
@ -655,13 +640,13 @@ class HelloTriangleApplication {
graphicsPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo); graphicsPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Pipeline created !\n"; std::cout << "Pipeline created !\n";
#endif #endif
} }
void createCommandPool() { void createCommandPool() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating a command pool\n"; std::cout << "Creating a command pool\n";
#endif #endif
vk::CommandPoolCreateInfo poolInfo { vk::CommandPoolCreateInfo poolInfo {
@ -670,13 +655,13 @@ class HelloTriangleApplication {
}; };
commandPool = vk::raii::CommandPool(device, poolInfo); commandPool = vk::raii::CommandPool(device, poolInfo);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Command pool created\n"; std::cout << "Command pool created\n";
#endif #endif
} }
void createDepthResources() { void createDepthResources() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating a depth ressouce...\n"; std::cout << "Creating a depth ressouce...\n";
#endif #endif
vk::Format depthFormat = findDepthFormat(); vk::Format depthFormat = findDepthFormat();
@ -696,13 +681,13 @@ class HelloTriangleApplication {
depthFormat, depthFormat,
vk::ImageAspectFlagBits::eDepth vk::ImageAspectFlagBits::eDepth
); );
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Depth ressouce created !\n"; std::cout << "Depth ressouce created !\n";
#endif #endif
} }
void createTextureImage(){ void createTextureImage(){
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating a texture image...\n"; std::cout << "Creating a texture image...\n";
#endif #endif
int texWidth, texHeight, texChannels; int texWidth, texHeight, texChannels;
@ -743,13 +728,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);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Texture image created !\n"; std::cout << "Texture image created !\n";
#endif #endif
} }
void createTextureImageView() { void createTextureImageView() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating the texture image...\n"; std::cout << "Creating the texture image...\n";
#endif #endif
textureImageView = createImageView( textureImageView = createImageView(
@ -757,7 +742,7 @@ class HelloTriangleApplication {
vk::Format::eR8G8B8A8Srgb, vk::Format::eR8G8B8A8Srgb,
vk::ImageAspectFlagBits::eColor vk::ImageAspectFlagBits::eColor
); );
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Texture Image created !\n"; std::cout << "Texture Image created !\n";
#endif #endif
} }
@ -772,7 +757,7 @@ class HelloTriangleApplication {
vk::raii::Image &image, vk::raii::Image &image,
vk::raii::DeviceMemory &imageMemory vk::raii::DeviceMemory &imageMemory
) { ) {
#ifndef NDEBUG #ifdef DEBUG
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 {
@ -797,13 +782,13 @@ class HelloTriangleApplication {
imageMemory = vk::raii::DeviceMemory(device, allocInfo); imageMemory = vk::raii::DeviceMemory(device, allocInfo);
image.bindMemory(imageMemory, 0); image.bindMemory(imageMemory, 0);
#ifndef NDEBUG #ifdef DEBUG
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() {
#ifndef NDEBUG #ifdef DEBUG
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();
@ -827,13 +812,13 @@ class HelloTriangleApplication {
textureSampler = vk::raii::Sampler(device, samplerInfo); textureSampler = vk::raii::Sampler(device, samplerInfo);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Texture sampler created !\n"; std::cout << "Texture sampler created !\n";
#endif #endif
} }
void loadModel() { void loadModel() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Loading the 3D model...\n"; std::cout << "Loading the 3D model...\n";
#endif #endif
@ -842,46 +827,17 @@ 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, &shapes, &materials, &warn, &err, MODEL_PATH.c_str())) { if (!tinyobj::LoadObj(&attrib, &matrials, &warn, &err, MODEL_PATH.c_str())) {
throw std::runtime_error(warn + err); throw std::runtime_error(warn + err);
} }
std::unordered_map<Vertex, uint32_t> uniqueVertices{}; #ifdef DEBUG
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() {
#ifndef NDEBUG #ifdef DEBUG
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;
@ -908,7 +864,7 @@ class HelloTriangleApplication {
vertexBufferMemory vertexBufferMemory
); );
copyBuffer(stagingBuffer, vertexBuffer, bufferSize); copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Vertex buffer created !\n"; std::cout << "Vertex buffer created !\n";
#endif #endif
} }
@ -918,7 +874,7 @@ class HelloTriangleApplication {
vk::raii::Buffer & dstBuffer, vk::raii::Buffer & dstBuffer,
vk::DeviceSize size vk::DeviceSize size
) { ) {
#ifndef NDEBUG #ifdef DEBUG
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();
@ -927,13 +883,13 @@ class HelloTriangleApplication {
endSingleTimeCommands(commandCopyBuffer); endSingleTimeCommands(commandCopyBuffer);
#ifndef NDEBUG #ifdef DEBUG
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() {
#ifndef NDEBUG #ifdef DEBUG
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();
@ -961,13 +917,13 @@ class HelloTriangleApplication {
); );
copyBuffer(stagingBuffer, indexBuffer, bufferSize); copyBuffer(stagingBuffer, indexBuffer, bufferSize);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Index buffer created !\n"; std::cout << "Index buffer created !\n";
#endif #endif
} }
void createUniformBuffers() { void createUniformBuffers() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating uniform buffers...\n"; std::cout << "Creating uniform buffers...\n";
#endif #endif
uniformBuffers.clear(); uniformBuffers.clear();
@ -990,13 +946,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));
} }
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Uniform buffers created !\n"; std::cout << "Uniform buffers created !\n";
#endif #endif
} }
void createDescriptorPool() { void createDescriptorPool() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating a descriptor pool...\n"; std::cout << "Creating a descriptor pool...\n";
#endif #endif
std::array poolSize { std::array poolSize {
@ -1018,13 +974,13 @@ class HelloTriangleApplication {
descriptorPool = vk::raii::DescriptorPool(device, poolInfo); descriptorPool = vk::raii::DescriptorPool(device, poolInfo);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Descriptor pool created !\n"; std::cout << "Descriptor pool created !\n";
#endif #endif
} }
void createDescriptorSets() { void createDescriptorSets() {
#ifndef NDEBUG #ifdef DEBUG
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 );
@ -1070,13 +1026,13 @@ class HelloTriangleApplication {
device.updateDescriptorSets(descriptorWrites, {}); device.updateDescriptorSets(descriptorWrites, {});
} }
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Descriptor sets created !\n"; std::cout << "Descriptor sets created !\n";
#endif #endif
} }
void createCommandBuffers() { void createCommandBuffers() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating a command buffers\n"; std::cout << "Creating a command buffers\n";
#endif #endif
@ -1087,7 +1043,7 @@ class HelloTriangleApplication {
}; };
commandBuffers = vk::raii::CommandBuffers(device, allocInfo); commandBuffers = vk::raii::CommandBuffers(device, allocInfo);
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Command buffer created\n"; std::cout << "Command buffer created\n";
#endif #endif
} }
@ -1295,7 +1251,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/10 * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.model = rotate(glm::mat4(1.0f), time * 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;
@ -1313,11 +1269,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) {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "SwapChain out of date ! \n"; std::cout << "SwapChain out of date ! \n";
#endif #endif
recreateSwapChain(); recreateSwapChain();
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Fixed the swapchain !\n"; std::cout << "Fixed the swapchain !\n";
#endif #endif
return; return;
@ -1382,7 +1338,7 @@ class HelloTriangleApplication {
} }
void recreateSwapChain() { void recreateSwapChain() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Re-creating the SwapChain...\n"; std::cout << "Re-creating the SwapChain...\n";
#endif #endif
@ -1402,7 +1358,7 @@ class HelloTriangleApplication {
createImageViews(); createImageViews();
createDepthResources(); createDepthResources();
#ifndef NDEBUG #ifdef DEBUG
std::cout << "SwapChain re-created sucessfully !\n"; std::cout << "SwapChain re-created sucessfully !\n";
#endif #endif
} }
@ -1414,7 +1370,7 @@ class HelloTriangleApplication {
vk::raii::Buffer& buffer, vk::raii::Buffer& buffer,
vk::raii::DeviceMemory& bufferMemory vk::raii::DeviceMemory& bufferMemory
) { ) {
#ifndef NDEBUG #ifdef DEBUG
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
@ -1433,7 +1389,7 @@ class HelloTriangleApplication {
bufferMemory = vk::raii::DeviceMemory(device, allocInfo); bufferMemory = vk::raii::DeviceMemory(device, allocInfo);
buffer.bindMemory(*bufferMemory, 0); buffer.bindMemory(*bufferMemory, 0);
#ifndef NDEBUG #ifdef DEBUG
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
} }
@ -1445,7 +1401,7 @@ class HelloTriangleApplication {
void * void *
){ ){
if (severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eError || severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning) { if (severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eError || severity == vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning) {
#ifndef NDEBUG #ifdef DEBUG
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
} }
@ -1481,7 +1437,7 @@ class HelloTriangleApplication {
} }
uint32_t findQueueFamilies(vk::raii::PhysicalDevice physicalDevice) { uint32_t findQueueFamilies(vk::raii::PhysicalDevice physicalDevice) {
#ifndef NDEBUG #ifdef DEBUG
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
@ -1490,7 +1446,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
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Found a graphics and present queue !\n"; std::cout << "Found a graphics and present queue !\n";
#endif #endif
queueIndex = qfpIndex; queueIndex = qfpIndex;
@ -1533,7 +1489,7 @@ class HelloTriangleApplication {
} }
static std::vector<char> readFile(const std::string& filename) { static std::vector<char> readFile(const std::string& filename) {
#ifndef NDEBUG #ifdef DEBUG
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);
@ -1546,14 +1502,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();
#ifndef NDEBUG #ifdef DEBUG
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 {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Creating shader module\n"; std::cout << "Creating shader module\n";
#endif #endif
vk::ShaderModuleCreateInfo createInfo{ vk::ShaderModuleCreateInfo createInfo{
@ -1561,7 +1517,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 };
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Shader module created\n"; std::cout << "Shader module created\n";
#endif #endif
return shaderModule; return shaderModule;
@ -1652,7 +1608,7 @@ class HelloTriangleApplication {
}; };
int main() { int main() {
#ifndef NDEBUG #ifdef DEBUG
std::cout << "Starting the app...\n"; std::cout << "Starting the app...\n";
#endif #endif
HelloTriangleApplication app; HelloTriangleApplication app;