Compare commits

..

2 Commits

  1. 41
      src/vulkan/application.cpp
  2. 17
      src/vulkan/application.hpp
  3. 73
      src/vulkan/buffer.cpp
  4. 14
      src/vulkan/buffer.hpp
  5. 1
      src/vulkan/command_pool.cpp
  6. 6
      src/vulkan/command_pool.hpp
  7. 1
      src/vulkan/pipeline.cpp
  8. 3
      src/vulkan/pipeline.hpp
  9. 1
      src/vulkan/swapchain.cpp
  10. 2
      src/vulkan/swapchain.hpp

@ -1,11 +1,21 @@
#include "application.hpp"
#include "swapchain.hpp"
#include "pipeline.hpp"
#include "instance.hpp"
#include "buffer.hpp"
#include "command_pool.hpp"
Application::Application() {
instance = new Instance;
swapchain = new Swapchain(instance);
pipeline = new Pipeline(instance, swapchain->renderPass);
buffer = new Buffer(instance);
stagingBuffer = Buffer::createStagingBuffer(instance);
vertexBuffer = new Buffer(instance, stagingBuffer->size,
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
commandPool = new CommandPool(instance);
stagingBuffer->copyTo(vertexBuffer, commandPool);
createSyncObjects();
mainLoop();
@ -89,19 +99,6 @@ void Application::createSyncObjects() {
}
}
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);
}
delete commandPool;
delete buffer;
delete pipeline;
delete instance;
}
void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) {
VkCommandBufferBeginInfo beginInfo {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
@ -136,7 +133,7 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im
scissor.extent = swapchain->extent;
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
VkBuffer buffers[] = {buffer->vertexHandle};
VkBuffer buffers[] = {vertexBuffer->handle};
VkDeviceSize offsets[] = {0};
vkCmdBindVertexBuffers(commandBuffer, 0, 1, buffers, offsets);
@ -146,6 +143,20 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im
vkEndCommandBuffer(commandBuffer);
}
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);
}
delete commandPool;
delete stagingBuffer;
delete vertexBuffer;
delete pipeline;
delete instance;
}

@ -13,12 +13,14 @@
#include <numeric>
#include <chrono>
#include <array>
#include "vertex.hpp"
#include "swapchain.hpp"
#include "pipeline.hpp"
#include "instance.hpp"
#include "buffer.hpp"
#include "command_pool.hpp"
#include <memory>
#include <vulkan/vulkan_core.h>
class Instance;
class Swapchain;
class Pipeline;
class Buffer;
class CommandPool;
constexpr int MAX_FRAMES_IN_FLIGHT = 2;
@ -43,7 +45,8 @@ private:
Instance* instance = nullptr;
Swapchain* swapchain = nullptr;
Pipeline* pipeline = nullptr;
Buffer* buffer = nullptr;
Buffer* stagingBuffer = nullptr;
Buffer* vertexBuffer = nullptr;
CommandPool* commandPool = nullptr;
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);

@ -2,6 +2,8 @@
#include <cstring>
#include "buffer.hpp"
#include "vertex.hpp"
#include "instance.hpp"
#include "command_pool.hpp"
const std::vector<Vertex> vertices = {
{{0.0, -0.5}, {1, 0, 0}},
@ -9,31 +11,26 @@ const std::vector<Vertex> vertices = {
{{-0.5, 0.5}, {0, 0, 1}}
};
Buffer::Buffer(Instance* instance) : instance(instance) {
Buffer::Buffer(Instance* instance, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : instance(instance), size(size){
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = vertices.size() * sizeof(Vertex);
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vkCreateBuffer(instance->device, &bufferInfo, nullptr, &vertexHandle);
vkCreateBuffer(instance->device, &bufferInfo, nullptr, &handle);
VkMemoryRequirements memoryRequirements;
vkGetBufferMemoryRequirements(instance->device, vertexHandle, &memoryRequirements);
vkGetBufferMemoryRequirements(instance->device, handle, &memoryRequirements);
VkMemoryAllocateInfo allocateInfo {};
allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocateInfo.allocationSize = memoryRequirements.size;
allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, properties);
vkAllocateMemory(instance->device, &allocateInfo, nullptr, &vertexMemory);
vkAllocateMemory(instance->device, &allocateInfo, nullptr, &memory);
vkBindBufferMemory(instance->device, vertexHandle, vertexMemory, 0);
void* data;
vkMapMemory(instance->device, vertexMemory, 0, bufferInfo.size, 0, &data);
memcpy(data, vertices.data(), bufferInfo.size);
vkUnmapMemory(instance->device, vertexMemory);
vkBindBufferMemory(instance->device, handle, memory, 0);
}
uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags) {
@ -50,6 +47,52 @@ uint32_t Buffer::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags prope
}
Buffer::~Buffer() {
vkFreeMemory(instance->device, vertexMemory, nullptr);
vkDestroyBuffer(instance->device, vertexHandle, nullptr);
vkFreeMemory(instance->device, memory, nullptr);
vkDestroyBuffer(instance->device, handle, nullptr);
}
Buffer* Buffer::createStagingBuffer(Instance* instance) {
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);
void* data;
vkMapMemory(instance->device, stagingBuffer->memory, 0, size, 0, &data);
memcpy(data, vertices.data(), size);
vkUnmapMemory(instance->device, stagingBuffer->memory);
return stagingBuffer;
}
void Buffer::copyTo(Buffer *dst, CommandPool* commandPool) {
VkCommandBufferAllocateInfo allocateInfo {};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandPool = commandPool->handle;
allocateInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer;
vkAllocateCommandBuffers(instance->device, &allocateInfo, &commandBuffer);
VkCommandBufferBeginInfo beginInfo {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(commandBuffer, &beginInfo);
VkBufferCopy copyRegion {};
copyRegion.size = size;
vkCmdCopyBuffer(commandBuffer, handle, dst->handle, 1, &copyRegion);
vkEndCommandBuffer(commandBuffer);
VkSubmitInfo submitInfo {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(instance->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkDeviceWaitIdle(instance->device);
vkFreeCommandBuffers(instance->device, commandPool->handle, 1, &commandBuffer);
}

@ -2,18 +2,24 @@
#include <vulkan/vulkan_core.h>
#include <stdexcept>
#include "instance.hpp"
class Instance;
class CommandPool;
class Buffer {
public:
explicit Buffer(Instance* instance);
explicit Buffer(Instance* instance, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties);
~Buffer();
VkBuffer vertexHandle = VK_NULL_HANDLE;
VkBuffer handle = VK_NULL_HANDLE;
VkDeviceMemory memory = VK_NULL_HANDLE;
VkDeviceSize size;
static Buffer* createStagingBuffer(Instance* instance);
void copyTo(Buffer* dst, CommandPool* commandPool);
private:
Instance* instance = nullptr;
VkDeviceMemory vertexMemory = VK_NULL_HANDLE;
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags propertyFlags);
};

@ -1,5 +1,6 @@
#include "command_pool.hpp"
#include "application.hpp"
#include "instance.hpp"
CommandPool::CommandPool(Instance *instance) : instance(instance) {
Instance::QueueFamilyIndices indices = Instance::findQueueFamilies(instance->physicalDevice, instance->surface);

@ -1,16 +1,18 @@
#pragma once
#include <vector>
#include "instance.hpp"
#include <vulkan/vulkan_core.h>
class Instance;
class CommandPool {
public:
explicit CommandPool(Instance* instance);
~CommandPool();
std::vector<VkCommandBuffer> buffers;
VkCommandPool handle = VK_NULL_HANDLE;
private:
Instance* instance;
VkCommandPool handle = VK_NULL_HANDLE;
void createBuffers();
};

@ -2,6 +2,7 @@
#include <fstream>
#include "pipeline.hpp"
#include "vertex.hpp"
#include "instance.hpp"
std::vector<char> readFile(const std::string& fileName){
std::ifstream file(fileName, std::ios::ate | std::ios::binary);

@ -1,7 +1,8 @@
#pragma once
#include <vulkan/vulkan.h>
#include "instance.hpp"
class Instance;
class Pipeline {
public:

@ -1,6 +1,7 @@
#include <limits>
#include <stdexcept>
#include "swapchain.hpp"
#include "instance.hpp"
SwapchainSupportDetails querySwapchainSupport(VkPhysicalDevice device, VkSurfaceKHR surface){
SwapchainSupportDetails details;

@ -3,7 +3,7 @@
#include <vulkan/vulkan.h>
#include <vector>
#include "instance.hpp"
class Instance;
struct SwapchainSupportDetails {
VkSurfaceCapabilitiesKHR capabilities {};

Loading…
Cancel
Save