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})
endif ()
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)
endif ()
if (DEFINED CHAPTER_TEXTURES)

View File

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