diff --git a/shaders/shader.vert b/shaders/shader.vert index 4509126..93f3fa7 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -1,6 +1,6 @@ #version 450 -layout (location = 0) in vec2 inPosition; +layout (location = 0) in vec3 inPosition; layout (location = 1) in vec3 inColor; layout (location = 0) out vec3 fragColor; @@ -12,6 +12,6 @@ layout (binding = 0) uniform UniformBufferObject { } ubo; void main() { - gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); + gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPosition, 1.0); fragColor = inColor; } \ No newline at end of file diff --git a/src/glm.h b/src/glm.h new file mode 100644 index 0000000..2626495 --- /dev/null +++ b/src/glm.h @@ -0,0 +1,6 @@ +#pragma once + +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include +#include \ No newline at end of file diff --git a/src/vulkan/application.cpp b/src/vulkan/application.cpp index d0e8ba9..69783b8 100644 --- a/src/vulkan/application.cpp +++ b/src/vulkan/application.cpp @@ -5,33 +5,37 @@ #include "buffer.hpp" #include "command_pool.hpp" + + Application::Application() { - instance = new Instance; - swapchain = new Swapchain(instance); - pipeline = new Pipeline(instance, swapchain->renderPass); - auto stagedVertexBuffer = Buffer::createStagedVertexBuffer(instance); - vertexBuffer = new Buffer(instance, stagedVertexBuffer->size, + new Instance; + swapchain = new Swapchain(); + pipeline = new Pipeline(swapchain->renderPass); + auto stagedVertexBuffer = Buffer::createStagedVertexBuffer(); + vertexBuffer = new Buffer(stagedVertexBuffer->size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); - auto stagedIndexBuffer = Buffer::createStagedIndexBuffer(instance); - indexBuffer = new Buffer(instance, stagedIndexBuffer->size, + auto stagedIndexBuffer = Buffer::createStagedIndexBuffer(); + indexBuffer = new Buffer(stagedIndexBuffer->size, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); VkDeviceSize bufferSize = sizeof(UniformBufferObject); - uniformBuffer = new Buffer(instance, bufferSize, + uniformBuffer = new Buffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); - vkMapMemory(instance->device, uniformBuffer->memory, 0, bufferSize, 0, &uniformBufferMapped); + vkMapMemory(Instance::instance->device, uniformBuffer->memory, 0, bufferSize, 0, &uniformBufferMapped); pipeline->createDescriptorSet(uniformBuffer); - commandPool = new CommandPool(instance); + commandPool = new CommandPool(); stagedVertexBuffer->copyTo(vertexBuffer, commandPool); stagedIndexBuffer->copyTo(indexBuffer, commandPool); + createDepthResources(); + delete stagedVertexBuffer; delete stagedIndexBuffer; @@ -101,16 +105,16 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im } void Application::drawFrame() { - vkWaitForFences(instance->device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX); + vkWaitForFences(Instance::instance->device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX); uint32_t imageIndex; - VkResult result = vkAcquireNextImageKHR(instance->device, swapchain->handle, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); + VkResult result = vkAcquireNextImageKHR(Instance::instance->device, swapchain->handle, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); if (result == VK_ERROR_OUT_OF_DATE_KHR){ swapchain->recreateSwapchain(); return; } - vkResetFences(instance->device, 1, &inFlightFences[currentFrame]); + vkResetFences(Instance::instance->device, 1, &inFlightFences[currentFrame]); vkResetCommandBuffer(commandPool->buffers[currentFrame], 0); recordCommandBuffer(commandPool->buffers[currentFrame], imageIndex); @@ -132,7 +136,7 @@ void Application::drawFrame() { submitInfo.signalSemaphoreCount = 1; submitInfo.pSignalSemaphores = signalSemaphores; - vkQueueSubmit(instance->graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]); + vkQueueSubmit(Instance::instance->graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]); VkPresentInfoKHR presentInfo {}; presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; @@ -144,7 +148,7 @@ void Application::drawFrame() { presentInfo.pSwapchains = swapchains; presentInfo.pImageIndices = &imageIndex; - result = vkQueuePresentKHR(instance->presentQueue, &presentInfo); + result = vkQueuePresentKHR(Instance::instance->presentQueue, &presentInfo); if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR){ swapchain->recreateSwapchain(); return; @@ -166,33 +170,37 @@ void Application::createSyncObjects() { fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++){ - vkCreateSemaphore(instance->device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]); - vkCreateSemaphore(instance->device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]); - vkCreateFence(instance->device, &fenceInfo, nullptr, &inFlightFences[i]); + vkCreateSemaphore(Instance::instance->device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]); + vkCreateSemaphore(Instance::instance->device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]); + vkCreateFence(Instance::instance->device, &fenceInfo, nullptr, &inFlightFences[i]); } } void Application::mainLoop() { - while (!glfwWindowShouldClose(instance->window)){ + while (!glfwWindowShouldClose(Instance::instance->window)){ glfwPollEvents(); drawFrame(); } - vkDeviceWaitIdle(instance->device); + vkDeviceWaitIdle(Instance::instance->device); } Application::~Application() { delete swapchain; for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++){ - vkDestroySemaphore(instance->device, imageAvailableSemaphores[i], nullptr); - vkDestroySemaphore(instance->device, renderFinishedSemaphores[i], nullptr); - vkDestroyFence(instance->device, inFlightFences[i], nullptr); + vkDestroySemaphore(Instance::instance->device, imageAvailableSemaphores[i], nullptr); + vkDestroySemaphore(Instance::instance->device, renderFinishedSemaphores[i], nullptr); + vkDestroyFence(Instance::instance->device, inFlightFences[i], nullptr); } delete commandPool; delete vertexBuffer; delete indexBuffer; delete uniformBuffer; delete pipeline; - delete instance; + delete Instance::instance; +} + +void Application::createDepthResources() { + } diff --git a/src/vulkan/application.hpp b/src/vulkan/application.hpp index a617824..f4ab997 100644 --- a/src/vulkan/application.hpp +++ b/src/vulkan/application.hpp @@ -2,8 +2,6 @@ #include -#include - #include #include #include @@ -43,7 +41,6 @@ public: void mainLoop(); ~Application(); private: - Instance* instance = nullptr; Swapchain* swapchain = nullptr; Pipeline* pipeline = nullptr; Buffer* vertexBuffer = nullptr; @@ -53,6 +50,11 @@ private: void* uniformBufferMapped = nullptr; + VkImage depthImage = VK_NULL_HANDLE; + VkDeviceMemory depthImageMemory = VK_NULL_HANDLE; + VkImageView depthImageView = VK_NULL_HANDLE; + void createDepthResources(); + void updateUniformBuffer(); void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex); diff --git a/src/vulkan/buffer.cpp b/src/vulkan/buffer.cpp index 87c77ef..ec48bce 100644 --- a/src/vulkan/buffer.cpp +++ b/src/vulkan/buffer.cpp @@ -5,31 +5,31 @@ #include "instance.hpp" #include "command_pool.hpp" -Buffer::Buffer(Instance* instance, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : instance(instance), size(size){ +Buffer::Buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : size(size){ VkBufferCreateInfo bufferInfo{}; bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; bufferInfo.size = size; bufferInfo.usage = usage; bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - vkCreateBuffer(instance->device, &bufferInfo, nullptr, &handle); + vkCreateBuffer(Instance::instance->device, &bufferInfo, nullptr, &handle); VkMemoryRequirements memoryRequirements; - vkGetBufferMemoryRequirements(instance->device, handle, &memoryRequirements); + vkGetBufferMemoryRequirements(Instance::instance->device, handle, &memoryRequirements); VkMemoryAllocateInfo allocateInfo {}; allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocateInfo.allocationSize = memoryRequirements.size; allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, properties); - vkAllocateMemory(instance->device, &allocateInfo, nullptr, &memory); + vkAllocateMemory(Instance::instance->device, &allocateInfo, nullptr, &memory); - vkBindBufferMemory(instance->device, handle, memory, 0); + vkBindBufferMemory(Instance::instance->device, handle, memory, 0); } uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags) { VkPhysicalDeviceMemoryProperties memoryProperties; - vkGetPhysicalDeviceMemoryProperties(instance->physicalDevice, &memoryProperties); + vkGetPhysicalDeviceMemoryProperties(Instance::instance->physicalDevice, &memoryProperties); for (uint32_t type = 0; type < memoryProperties.memoryTypeCount; type++){ if ((typeFilter & (1 << type)) && (memoryProperties.memoryTypes[type].propertyFlags & propertyFlags)){ @@ -41,35 +41,48 @@ uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags prope } Buffer::~Buffer() { - vkFreeMemory(instance->device, memory, nullptr); - vkDestroyBuffer(instance->device, handle, nullptr); + vkFreeMemory(Instance::instance->device, memory, nullptr); + vkDestroyBuffer(Instance::instance->device, handle, nullptr); } -Buffer* Buffer::createStagedVertexBuffer(Instance* instance) { +Buffer* Buffer::createStagedVertexBuffer() { + std::vector vertices = { + {{0.0, -0.5, 0}, {1, 0, 0}}, + {{0.5, 0.5, 0}, {0, 1, 0}}, + {{-0.5, 0.5, 0}, {0, 0, 1}}, + {{0.8, -0.5, 0}, {1, 1, 1}} + }; + + vertices.insert(vertices.end(), vertices.begin(), vertices.end()); + + for (size_t i = 4; i < vertices.size(); i++){ + vertices[i].pos.z -= 0.5f; + } + VkDeviceSize size = vertices.size() * sizeof(Vertex); VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; - auto stagingBuffer = new Buffer(instance, size, usage, properties); + auto stagingBuffer = new Buffer(size, usage, properties); void* data; - vkMapMemory(instance->device, stagingBuffer->memory, 0, size, 0, &data); + vkMapMemory(Instance::instance->device, stagingBuffer->memory, 0, size, 0, &data); memcpy(data, vertices.data(), size); - vkUnmapMemory(instance->device, stagingBuffer->memory); + vkUnmapMemory(Instance::instance->device, stagingBuffer->memory); return stagingBuffer; } -Buffer *Buffer::createStagedIndexBuffer(Instance *instance) { +Buffer *Buffer::createStagedIndexBuffer() { VkDeviceSize size = indices.size() * sizeof(uint32_t); VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; - auto stagingBuffer = new Buffer(instance, size, usage, properties); + auto stagingBuffer = new Buffer(size, usage, properties); uint32_t *data; - vkMapMemory(instance->device, stagingBuffer->memory, 0, size, 0, reinterpret_cast(&data)); + vkMapMemory(Instance::instance->device, stagingBuffer->memory, 0, size, 0, reinterpret_cast(&data)); memcpy(data, indices.data(), sizeof(uint32_t) * indices.size()); - vkUnmapMemory(instance->device, stagingBuffer->memory); + vkUnmapMemory(Instance::instance->device, stagingBuffer->memory); return stagingBuffer; } @@ -82,7 +95,7 @@ void Buffer::copyTo(Buffer *dst, CommandPool* commandPool) { allocateInfo.commandBufferCount = 1; VkCommandBuffer commandBuffer; - vkAllocateCommandBuffers(instance->device, &allocateInfo, &commandBuffer); + vkAllocateCommandBuffers(Instance::instance->device, &allocateInfo, &commandBuffer); VkCommandBufferBeginInfo beginInfo {}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -99,8 +112,8 @@ void Buffer::copyTo(Buffer *dst, CommandPool* commandPool) { submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &commandBuffer; - vkQueueSubmit(instance->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); - vkDeviceWaitIdle(instance->device); + vkQueueSubmit(Instance::instance->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); + vkDeviceWaitIdle(Instance::instance->device); - vkFreeCommandBuffers(instance->device, commandPool->handle, 1, &commandBuffer); + vkFreeCommandBuffers(Instance::instance->device, commandPool->handle, 1, &commandBuffer); } diff --git a/src/vulkan/buffer.hpp b/src/vulkan/buffer.hpp index d45226b..979388b 100644 --- a/src/vulkan/buffer.hpp +++ b/src/vulkan/buffer.hpp @@ -4,15 +4,9 @@ #include #include "vertex.hpp" -const std::vector vertices = { - {{0.0, -0.5}, {1, 0, 0}}, - {{0.5, 0.5}, {0, 1, 0}}, - {{-0.5, 0.5}, {0, 0, 1}}, - {{0.8, -0.5}, {1, 1, 1}} -}; - const std::vector indices = { - 0, 1, 2, 0, 3, 1 + 0, 1, 2, 0, 3, 1, + 4, 5, 6, 4, 7, 5 }; class Instance; @@ -20,19 +14,18 @@ class CommandPool; class Buffer { public: - explicit Buffer(Instance* instance, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties); + explicit Buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties); ~Buffer(); VkBuffer handle = VK_NULL_HANDLE; VkDeviceMemory memory = VK_NULL_HANDLE; VkDeviceSize size; - static Buffer* createStagedVertexBuffer(Instance* instance); - static Buffer* createStagedIndexBuffer(Instance* instance); + static Buffer* createStagedVertexBuffer(); + static Buffer* createStagedIndexBuffer(); void copyTo(Buffer* dst, CommandPool* commandPool); private: - Instance* instance = nullptr; uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags); }; \ No newline at end of file diff --git a/src/vulkan/command_pool.cpp b/src/vulkan/command_pool.cpp index 64494a1..8e103cf 100644 --- a/src/vulkan/command_pool.cpp +++ b/src/vulkan/command_pool.cpp @@ -2,15 +2,15 @@ #include "application.hpp" #include "instance.hpp" -CommandPool::CommandPool(Instance *instance) : instance(instance) { - Instance::QueueFamilyIndices indices = Instance::findQueueFamilies(instance->physicalDevice, instance->surface); +CommandPool::CommandPool() { + Instance::QueueFamilyIndices indices = Instance::findQueueFamilies(Instance::instance->physicalDevice, Instance::instance->surface); VkCommandPoolCreateInfo poolInfo {}; poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; poolInfo.queueFamilyIndex = indices.graphicsFamily.value(); - vkCreateCommandPool(instance->device, &poolInfo, nullptr, &handle); + vkCreateCommandPool(Instance::instance->device, &poolInfo, nullptr, &handle); createBuffers(); } @@ -24,10 +24,10 @@ void CommandPool::createBuffers() { allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocateInfo.commandBufferCount = buffers.size(); - vkAllocateCommandBuffers(instance->device, &allocateInfo, buffers.data()); + vkAllocateCommandBuffers(Instance::instance->device, &allocateInfo, buffers.data()); } CommandPool::~CommandPool() { - vkFreeCommandBuffers(instance->device, handle, buffers.size(), buffers.data()); - vkDestroyCommandPool(instance->device, handle, nullptr); + vkFreeCommandBuffers(Instance::instance->device, handle, buffers.size(), buffers.data()); + vkDestroyCommandPool(Instance::instance->device, handle, nullptr); } diff --git a/src/vulkan/command_pool.hpp b/src/vulkan/command_pool.hpp index f80c350..932c93a 100644 --- a/src/vulkan/command_pool.hpp +++ b/src/vulkan/command_pool.hpp @@ -7,12 +7,11 @@ class Instance; class CommandPool { public: - explicit CommandPool(Instance* instance); + explicit CommandPool(); ~CommandPool(); std::vector buffers; VkCommandPool handle = VK_NULL_HANDLE; private: - Instance* instance; void createBuffers(); }; \ No newline at end of file diff --git a/src/vulkan/instance.cpp b/src/vulkan/instance.cpp index 1151fc8..d1e47d6 100644 --- a/src/vulkan/instance.cpp +++ b/src/vulkan/instance.cpp @@ -46,7 +46,11 @@ bool checkValidationLayerSupport(){ } #endif +Instance* Instance::instance = nullptr; + Instance::Instance() { + instance = this; + initWindow(); createInstance(); diff --git a/src/vulkan/instance.hpp b/src/vulkan/instance.hpp index d807976..7eb1036 100644 --- a/src/vulkan/instance.hpp +++ b/src/vulkan/instance.hpp @@ -30,6 +30,7 @@ public: }; static QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface); + static Instance* instance; private: VkInstance handle = VK_NULL_HANDLE; diff --git a/src/vulkan/pipeline.cpp b/src/vulkan/pipeline.cpp index e82296a..83f3400 100644 --- a/src/vulkan/pipeline.cpp +++ b/src/vulkan/pipeline.cpp @@ -22,7 +22,7 @@ std::vector readFile(const std::string& fileName){ return buffer; } -Pipeline::Pipeline(Instance* instance, VkRenderPass renderPass) : instance(instance) { +Pipeline::Pipeline(VkRenderPass renderPass) { createDescriptorSetLayout(); createDescriptorPool(); @@ -107,7 +107,7 @@ Pipeline::Pipeline(Instance* instance, VkRenderPass renderPass) : instance(insta pipelineLayoutInfo.setLayoutCount = 1; pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout; - vkCreatePipelineLayout(instance->device, &pipelineLayoutInfo, nullptr, &layout); + vkCreatePipelineLayout(Instance::instance->device, &pipelineLayoutInfo, nullptr, &layout); VkGraphicsPipelineCreateInfo pipelineInfo {}; { @@ -130,10 +130,10 @@ Pipeline::Pipeline(Instance* instance, VkRenderPass renderPass) : instance(insta pipelineInfo.subpass = 0; } - vkCreateGraphicsPipelines(instance->device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &handle); + vkCreateGraphicsPipelines(Instance::instance->device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &handle); - vkDestroyShaderModule(instance->device, vertShaderModule, nullptr); - vkDestroyShaderModule(instance->device, fragShaderModule, nullptr); + vkDestroyShaderModule(Instance::instance->device, vertShaderModule, nullptr); + vkDestroyShaderModule(Instance::instance->device, fragShaderModule, nullptr); } VkShaderModule Pipeline::createShaderModule(const std::vector &code) { @@ -143,16 +143,16 @@ VkShaderModule Pipeline::createShaderModule(const std::vector &code) { createInfo.pCode = reinterpret_cast(code.data()); VkShaderModule shaderModule; - vkCreateShaderModule(instance->device, &createInfo, nullptr, &shaderModule); + vkCreateShaderModule(Instance::instance->device, &createInfo, nullptr, &shaderModule); return shaderModule; } Pipeline::~Pipeline() { - vkDestroyDescriptorPool(instance->device, descriptorPool, nullptr); - vkDestroyDescriptorSetLayout(instance->device, descriptorSetLayout, nullptr); - vkDestroyPipeline(instance->device, handle, nullptr); - vkDestroyPipelineLayout(instance->device, layout, nullptr); + vkDestroyDescriptorPool(Instance::instance->device, descriptorPool, nullptr); + vkDestroyDescriptorSetLayout(Instance::instance->device, descriptorSetLayout, nullptr); + vkDestroyPipeline(Instance::instance->device, handle, nullptr); + vkDestroyPipelineLayout(Instance::instance->device, layout, nullptr); } void Pipeline::createDescriptorSetLayout() { @@ -167,7 +167,7 @@ void Pipeline::createDescriptorSetLayout() { layoutCreateInfo.bindingCount = 1; layoutCreateInfo.pBindings = &uboLayoutBinding; - vkCreateDescriptorSetLayout(instance->device, &layoutCreateInfo, nullptr, &descriptorSetLayout); + vkCreateDescriptorSetLayout(Instance::instance->device, &layoutCreateInfo, nullptr, &descriptorSetLayout); } void Pipeline::createDescriptorPool() { @@ -181,7 +181,7 @@ void Pipeline::createDescriptorPool() { poolInfo.poolSizeCount = 1; poolInfo.maxSets = 1; - vkCreateDescriptorPool(instance->device, &poolInfo, nullptr, &descriptorPool); + vkCreateDescriptorPool(Instance::instance->device, &poolInfo, nullptr, &descriptorPool); } void Pipeline::createDescriptorSet(Buffer *buffer) { @@ -191,7 +191,7 @@ void Pipeline::createDescriptorSet(Buffer *buffer) { allocateInfo.descriptorSetCount = 1; allocateInfo.pSetLayouts = &descriptorSetLayout; - vkAllocateDescriptorSets(instance->device, &allocateInfo, &descriptorSet); + vkAllocateDescriptorSets(Instance::instance->device, &allocateInfo, &descriptorSet); VkDescriptorBufferInfo bufferInfo {}; bufferInfo.buffer = buffer->handle; @@ -207,6 +207,6 @@ void Pipeline::createDescriptorSet(Buffer *buffer) { descriptorWrite.descriptorCount = 1; descriptorWrite.pBufferInfo = &bufferInfo; - vkUpdateDescriptorSets(instance->device, 1, &descriptorWrite, 0, nullptr); + vkUpdateDescriptorSets(Instance::instance->device, 1, &descriptorWrite, 0, nullptr); } diff --git a/src/vulkan/pipeline.hpp b/src/vulkan/pipeline.hpp index 4fecaf9..8e80778 100644 --- a/src/vulkan/pipeline.hpp +++ b/src/vulkan/pipeline.hpp @@ -1,7 +1,7 @@ #pragma once +#include "../glm.h" #include -#include class Instance; class Buffer; @@ -15,7 +15,7 @@ struct UniformBufferObject { class Pipeline { public: - explicit Pipeline(Instance* instance, VkRenderPass renderPass); + explicit Pipeline(VkRenderPass renderPass); ~Pipeline(); VkPipeline handle = VK_NULL_HANDLE; VkPipelineLayout layout = VK_NULL_HANDLE; @@ -27,7 +27,6 @@ private: VkDescriptorPool descriptorPool = VK_NULL_HANDLE; VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE; - Instance* instance = nullptr; void createDescriptorSetLayout(); void createDescriptorPool(); diff --git a/src/vulkan/swapchain.cpp b/src/vulkan/swapchain.cpp index d5ecf13..d5e52de 100644 --- a/src/vulkan/swapchain.cpp +++ b/src/vulkan/swapchain.cpp @@ -25,8 +25,7 @@ SwapchainSupportDetails querySwapchainSupport(VkPhysicalDevice device, VkSurface return details; } -Swapchain::Swapchain(Instance* instance) -: instance(instance) { +Swapchain::Swapchain() { createSwapchain(); createImageViews(); createRenderpass(); @@ -35,7 +34,7 @@ Swapchain::Swapchain(Instance* instance) Swapchain::~Swapchain() { cleanupSwapchain(); - vkDestroyRenderPass(instance->device, renderPass, nullptr); + vkDestroyRenderPass(Instance::instance->device, renderPass, nullptr); } VkExtent2D Swapchain::chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities) { @@ -43,7 +42,7 @@ VkExtent2D Swapchain::chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilit return capabilities.currentExtent; } else { int width, height; - glfwGetFramebufferSize(instance->window, &width, &height); + glfwGetFramebufferSize(Instance::instance->window, &width, &height); VkExtent2D actualExtent = { static_cast(width), @@ -58,7 +57,7 @@ VkExtent2D Swapchain::chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilit } void Swapchain::createSwapchain() { - SwapchainSupportDetails swapchainSupport = querySwapchainSupport(instance->physicalDevice, instance->surface); + SwapchainSupportDetails swapchainSupport = querySwapchainSupport(Instance::instance->physicalDevice, Instance::instance->surface); VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapchainSupport.formats); VkPresentModeKHR presentMode = chooseSwapPresentMode(swapchainSupport.presentModes); @@ -71,7 +70,7 @@ void Swapchain::createSwapchain() { VkSwapchainCreateInfoKHR createInfo {}; createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; - createInfo.surface = instance->surface; + createInfo.surface = Instance::instance->surface; createInfo.minImageCount = imageCount; createInfo.imageFormat = surfaceFormat.format; createInfo.imageColorSpace = surfaceFormat.colorSpace; @@ -79,7 +78,7 @@ void Swapchain::createSwapchain() { createInfo.imageArrayLayers = 1; createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - Instance::QueueFamilyIndices indices = Instance::findQueueFamilies(instance->physicalDevice, instance->surface); + Instance::QueueFamilyIndices indices = Instance::findQueueFamilies(Instance::instance->physicalDevice, Instance::instance->surface); uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()}; if (indices.graphicsFamily != indices.presentFamily){ @@ -96,27 +95,27 @@ void Swapchain::createSwapchain() { createInfo.clipped = VK_TRUE; createInfo.oldSwapchain = VK_NULL_HANDLE; - vkCreateSwapchainKHR(instance->device, &createInfo, nullptr, &handle); + vkCreateSwapchainKHR(Instance::instance->device, &createInfo, nullptr, &handle); - vkGetSwapchainImagesKHR(instance->device, handle, &imageCount, nullptr); + vkGetSwapchainImagesKHR(Instance::instance->device, handle, &imageCount, nullptr); images.resize(imageCount); - vkGetSwapchainImagesKHR(instance->device, handle, &imageCount, images.data()); + vkGetSwapchainImagesKHR(Instance::instance->device, handle, &imageCount, images.data()); imageFormat = surfaceFormat.format; } void Swapchain::cleanupSwapchain() { for (auto framebuffer : frameBuffers){ - vkDestroyFramebuffer(instance->device, framebuffer, nullptr); + vkDestroyFramebuffer(Instance::instance->device, framebuffer, nullptr); } for (auto imageView : imageViews){ - vkDestroyImageView(instance->device, imageView, nullptr); + vkDestroyImageView(Instance::instance->device, imageView, nullptr); } - vkDestroySwapchainKHR(instance->device, handle, nullptr); + vkDestroySwapchainKHR(Instance::instance->device, handle, nullptr); } void Swapchain::recreateSwapchain() { - vkDeviceWaitIdle(instance->device); + vkDeviceWaitIdle(Instance::instance->device); cleanupSwapchain(); @@ -140,7 +139,7 @@ void Swapchain::createImageViews() { VK_COMPONENT_SWIZZLE_IDENTITY, }; createInfo.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; - vkCreateImageView(instance->device, &createInfo, nullptr, &imageViews[i]); + vkCreateImageView(Instance::instance->device, &createInfo, nullptr, &imageViews[i]); } } @@ -158,7 +157,7 @@ void Swapchain::createFramebuffers() { framebufferInfo.height = extent.height; framebufferInfo.layers = 1; - vkCreateFramebuffer(instance->device, &framebufferInfo, nullptr, &frameBuffers[i]); + vkCreateFramebuffer(Instance::instance->device, &framebufferInfo, nullptr, &frameBuffers[i]); } } @@ -212,5 +211,5 @@ void Swapchain::createRenderpass() { renderPassInfo.dependencyCount = 1; renderPassInfo.pDependencies = &dependency; - vkCreateRenderPass(instance->device, &renderPassInfo, nullptr, &renderPass); + vkCreateRenderPass(Instance::instance->device, &renderPassInfo, nullptr, &renderPass); } diff --git a/src/vulkan/swapchain.hpp b/src/vulkan/swapchain.hpp index 6804d8d..7de23dc 100644 --- a/src/vulkan/swapchain.hpp +++ b/src/vulkan/swapchain.hpp @@ -14,7 +14,7 @@ SwapchainSupportDetails querySwapchainSupport(VkPhysicalDevice device, VkSurface class Swapchain { public: - explicit Swapchain(Instance* instance); + explicit Swapchain(); ~Swapchain(); void recreateSwapchain(); @@ -25,7 +25,6 @@ public: std::vector frameBuffers; VkSwapchainKHR handle = VK_NULL_HANDLE; private: - Instance* instance = nullptr; std::vector images; VkFormat imageFormat {}; diff --git a/src/vulkan/vertex.hpp b/src/vulkan/vertex.hpp index 4459995..ef809d4 100644 --- a/src/vulkan/vertex.hpp +++ b/src/vulkan/vertex.hpp @@ -1,12 +1,11 @@ #pragma once -#define GLM_FORCE_RADIANS -#include +#include "../glm.h" #include #include struct Vertex { - glm::vec2 pos; + glm::vec3 pos; glm::vec3 color; static VkVertexInputBindingDescription getBindingDescription();