Files
SDL3CPlusPlus/src/app/vulkan_cube_app.cpp
Richard Ward 3ca9ce773d code
2025-12-18 19:13:31 +00:00

900 lines
36 KiB
C++

#include "app/vulkan_cube_app.hpp"
#include <algorithm>
#include <chrono>
#include <cstring>
#include <fstream>
#include <limits>
#include <set>
#include <stdexcept>
namespace sdl3cpp::app {
std::vector<char> ReadFile(const std::string& path) {
std::ifstream file(path, std::ios::ate | std::ios::binary);
if (!file) {
throw std::runtime_error("failed to open file: " + path);
}
size_t size = static_cast<size_t>(file.tellg());
std::vector<char> buffer(size);
file.seekg(0);
file.read(buffer.data(), buffer.size());
return buffer;
}
VulkanCubeApp::VulkanCubeApp(const std::filesystem::path& scriptPath) : cubeScript_(scriptPath) {}
void VulkanCubeApp::Run() {
InitSDL();
InitVulkan();
MainLoop();
Cleanup();
}
void VulkanCubeApp::InitSDL() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
throw std::runtime_error(std::string("SDL_Init failed: ") + SDL_GetError());
}
SDL_Vulkan_LoadLibrary(nullptr);
window_ = SDL_CreateWindow("SDL3 Vulkan Cube Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
kWidth, kHeight, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
if (!window_) {
throw std::runtime_error(std::string("SDL_CreateWindow failed: ") + SDL_GetError());
}
}
void VulkanCubeApp::InitVulkan() {
CreateInstance();
CreateSurface();
PickPhysicalDevice();
CreateLogicalDevice();
CreateSwapChain();
CreateImageViews();
CreateRenderPass();
LoadCubeData();
CreateGraphicsPipeline();
CreateFramebuffers();
CreateCommandPool();
CreateVertexBuffer();
CreateIndexBuffer();
CreateCommandBuffers();
CreateSyncObjects();
}
void VulkanCubeApp::MainLoop() {
bool running = true;
auto start = std::chrono::steady_clock::now();
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
running = false;
} else if (event.type == SDL_EVENT_WINDOW_SIZE_CHANGED) {
framebufferResized_ = true;
}
}
auto now = std::chrono::steady_clock::now();
float time = std::chrono::duration<float>(now - start).count();
DrawFrame(time);
}
vkDeviceWaitIdle(device_);
}
void VulkanCubeApp::CleanupSwapChain() {
for (auto framebuffer : swapChainFramebuffers_) {
vkDestroyFramebuffer(device_, framebuffer, nullptr);
}
vkFreeCommandBuffers(device_, commandPool_,
static_cast<uint32_t>(commandBuffers_.size()), commandBuffers_.data());
for (auto& entry : graphicsPipelines_) {
vkDestroyPipeline(device_, entry.second, nullptr);
}
graphicsPipelines_.clear();
if (pipelineLayout_ != VK_NULL_HANDLE) {
vkDestroyPipelineLayout(device_, pipelineLayout_, nullptr);
pipelineLayout_ = VK_NULL_HANDLE;
}
vkDestroyRenderPass(device_, renderPass_, nullptr);
for (auto imageView : swapChainImageViews_) {
vkDestroyImageView(device_, imageView, nullptr);
}
vkDestroySwapchainKHR(device_, swapChain_, nullptr);
}
void VulkanCubeApp::Cleanup() {
CleanupSwapChain();
vkDestroyBuffer(device_, vertexBuffer_, nullptr);
vkFreeMemory(device_, vertexBufferMemory_, nullptr);
vkDestroyBuffer(device_, indexBuffer_, nullptr);
vkFreeMemory(device_, indexBufferMemory_, nullptr);
vkDestroySemaphore(device_, renderFinishedSemaphore_, nullptr);
vkDestroySemaphore(device_, imageAvailableSemaphore_, nullptr);
vkDestroyFence(device_, inFlightFence_, nullptr);
vkDestroyCommandPool(device_, commandPool_, nullptr);
vkDestroyDevice(device_, nullptr);
vkDestroySurfaceKHR(instance_, surface_, nullptr);
vkDestroyInstance(instance_, nullptr);
if (window_) {
SDL_DestroyWindow(window_);
window_ = nullptr;
}
SDL_Vulkan_UnloadLibrary();
SDL_Quit();
}
void VulkanCubeApp::RecreateSwapChain() {
int width = 0;
int height = 0;
while (width == 0 || height == 0) {
SDL_Vulkan_GetDrawableSize(window_, &width, &height);
SDL_Event event;
SDL_WaitEvent(&event);
}
vkDeviceWaitIdle(device_);
CleanupSwapChain();
CreateSwapChain();
CreateImageViews();
CreateRenderPass();
CreateGraphicsPipeline();
CreateFramebuffers();
CreateCommandBuffers();
framebufferResized_ = false;
}
void VulkanCubeApp::CreateInstance() {
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "SDL3 Vulkan Cube";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_2;
uint32_t extensionCount = 0;
if (!SDL_Vulkan_GetInstanceExtensions(window_, &extensionCount, nullptr)) {
throw std::runtime_error("Failed to query Vulkan extensions from SDL");
}
std::vector<const char*> extensions(extensionCount);
if (!SDL_Vulkan_GetInstanceExtensions(window_, &extensionCount, extensions.data())) {
throw std::runtime_error("Failed to store Vulkan extensions from SDL");
}
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
createInfo.ppEnabledExtensionNames = extensions.data();
if (vkCreateInstance(&createInfo, nullptr, &instance_) != VK_SUCCESS) {
throw std::runtime_error("Failed to create Vulkan instance");
}
}
void VulkanCubeApp::CreateSurface() {
if (!SDL_Vulkan_CreateSurface(window_, instance_, &surface_)) {
throw std::runtime_error("Failed to create Vulkan surface");
}
}
void VulkanCubeApp::PickPhysicalDevice() {
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(instance_, &deviceCount, nullptr);
if (deviceCount == 0) {
throw std::runtime_error("Failed to find GPUs with Vulkan support");
}
std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(instance_, &deviceCount, devices.data());
for (const auto& device : devices) {
if (IsDeviceSuitable(device)) {
physicalDevice_ = device;
break;
}
}
if (physicalDevice_ == VK_NULL_HANDLE) {
throw std::runtime_error("Failed to find a suitable GPU");
}
}
void VulkanCubeApp::CreateLogicalDevice() {
QueueFamilyIndices indices = FindQueueFamilies(physicalDevice_);
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
std::set<uint32_t> uniqueQueueFamilies = {*indices.graphicsFamily, *indices.presentFamily};
float queuePriority = 1.0f;
for (uint32_t queueFamily : uniqueQueueFamilies) {
VkDeviceQueueCreateInfo queueCreateInfo{};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.queueFamilyIndex = queueFamily;
queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = &queuePriority;
queueCreateInfos.push_back(queueCreateInfo);
}
VkPhysicalDeviceFeatures deviceFeatures{};
VkDeviceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
createInfo.pQueueCreateInfos = queueCreateInfos.data();
createInfo.pEnabledFeatures = &deviceFeatures;
createInfo.enabledExtensionCount = static_cast<uint32_t>(kDeviceExtensions.size());
createInfo.ppEnabledExtensionNames = kDeviceExtensions.data();
if (vkCreateDevice(physicalDevice_, &createInfo, nullptr, &device_) != VK_SUCCESS) {
throw std::runtime_error("Failed to create logical device");
}
vkGetDeviceQueue(device_, *indices.graphicsFamily, 0, &graphicsQueue_);
vkGetDeviceQueue(device_, *indices.presentFamily, 0, &presentQueue_);
}
void VulkanCubeApp::CreateSwapChain() {
SwapChainSupportDetails support = QuerySwapChainSupport(physicalDevice_);
VkSurfaceFormatKHR surfaceFormat = ChooseSwapSurfaceFormat(support.formats);
VkPresentModeKHR presentMode = ChooseSwapPresentMode(support.presentModes);
VkExtent2D extent = ChooseSwapExtent(support.capabilities);
uint32_t imageCount = support.capabilities.minImageCount + 1;
if (support.capabilities.maxImageCount > 0 && imageCount > support.capabilities.maxImageCount) {
imageCount = support.capabilities.maxImageCount;
}
VkSwapchainCreateInfoKHR createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = surface_;
createInfo.minImageCount = imageCount;
createInfo.imageFormat = surfaceFormat.format;
createInfo.imageColorSpace = surfaceFormat.colorSpace;
createInfo.imageExtent = extent;
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
QueueFamilyIndices indices = FindQueueFamilies(physicalDevice_);
uint32_t queueFamilyIndices[] = {*indices.graphicsFamily, *indices.presentFamily};
if (indices.graphicsFamily != indices.presentFamily) {
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
} else {
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
}
createInfo.preTransform = support.capabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = presentMode;
createInfo.clipped = VK_TRUE;
if (vkCreateSwapchainKHR(device_, &createInfo, nullptr, &swapChain_) != VK_SUCCESS) {
throw std::runtime_error("Failed to create swap chain");
}
vkGetSwapchainImagesKHR(device_, swapChain_, &imageCount, nullptr);
swapChainImages_.resize(imageCount);
vkGetSwapchainImagesKHR(device_, swapChain_, &imageCount, swapChainImages_.data());
swapChainImageFormat_ = surfaceFormat.format;
swapChainExtent_ = extent;
}
void VulkanCubeApp::CreateImageViews() {
swapChainImageViews_.resize(swapChainImages_.size());
for (size_t i = 0; i < swapChainImages_.size(); ++i) {
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = swapChainImages_[i];
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = swapChainImageFormat_;
viewInfo.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY};
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(device_, &viewInfo, nullptr, &swapChainImageViews_[i]) != VK_SUCCESS) {
throw std::runtime_error("Failed to create image views");
}
}
}
void VulkanCubeApp::CreateRenderPass() {
VkAttachmentDescription colorAttachment{};
colorAttachment.format = swapChainImageFormat_;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference colorAttachmentRef{};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
VkSubpassDependency dependency{};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
VkRenderPassCreateInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = 1;
renderPassInfo.pAttachments = &colorAttachment;
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;
renderPassInfo.dependencyCount = 1;
renderPassInfo.pDependencies = &dependency;
if (vkCreateRenderPass(device_, &renderPassInfo, nullptr, &renderPass_) != VK_SUCCESS) {
throw std::runtime_error("Failed to create render pass");
}
}
VkShaderModule VulkanCubeApp::CreateShaderModule(const std::vector<char>& code) {
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
VkShaderModule shaderModule;
if (vkCreateShaderModule(device_, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
throw std::runtime_error("Failed to create shader module");
}
return shaderModule;
}
void VulkanCubeApp::CreateGraphicsPipeline() {
if (shaderPathMap_.empty()) {
throw std::runtime_error("No shader paths were loaded before pipeline creation");
}
for (auto& entry : graphicsPipelines_) {
vkDestroyPipeline(device_, entry.second, nullptr);
}
graphicsPipelines_.clear();
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
VkVertexInputBindingDescription bindingDescription{};
bindingDescription.binding = 0;
bindingDescription.stride = sizeof(core::Vertex);
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
std::array<VkVertexInputAttributeDescription, 2> attributeDescriptions{};
attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[0].offset = offsetof(core::Vertex, position);
attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[1].offset = offsetof(core::Vertex, color);
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.vertexBindingDescriptionCount = 1;
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssembly.primitiveRestartEnable = VK_FALSE;
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(swapChainExtent_.width);
viewport.height = static_cast<float>(swapChainExtent_.height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{};
scissor.offset = {0, 0};
scissor.extent = swapChainExtent_;
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.pViewports = &viewport;
viewportState.scissorCount = 1;
viewportState.pScissors = &scissor;
VkPipelineRasterizationStateCreateInfo rasterizer{};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.depthClampEnable = VK_FALSE;
rasterizer.rasterizerDiscardEnable = VK_FALSE;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.lineWidth = 1.0f;
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
rasterizer.depthBiasEnable = VK_FALSE;
VkPipelineMultisampleStateCreateInfo multisampling{};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_FALSE;
VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.logicOpEnable = VK_FALSE;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
VkPushConstantRange pushRange{};
pushRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
pushRange.offset = 0;
pushRange.size = sizeof(core::PushConstants);
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushRange;
if (pipelineLayout_ != VK_NULL_HANDLE) {
vkDestroyPipelineLayout(device_, pipelineLayout_, nullptr);
pipelineLayout_ = VK_NULL_HANDLE;
}
if (vkCreatePipelineLayout(device_, &pipelineLayoutInfo, nullptr, &pipelineLayout_) != VK_SUCCESS) {
throw std::runtime_error("Failed to create pipeline layout");
}
VkGraphicsPipelineCreateInfo pipelineInfo{};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.layout = pipelineLayout_;
pipelineInfo.renderPass = renderPass_;
pipelineInfo.subpass = 0;
for (const auto& [key, paths] : shaderPathMap_) {
auto vertShaderCode = ReadFile(paths.vertex);
auto fragShaderCode = ReadFile(paths.fragment);
VkShaderModule vertShaderModule = CreateShaderModule(vertShaderCode);
VkShaderModule fragShaderModule = CreateShaderModule(fragShaderCode);
VkPipelineShaderStageCreateInfo vertStageInfo{};
vertStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertStageInfo.module = vertShaderModule;
vertStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo fragStageInfo{};
fragStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragStageInfo.module = fragShaderModule;
fragStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo shaderStages[] = {vertStageInfo, fragStageInfo};
VkGraphicsPipelineCreateInfo pipelineCreateInfo = pipelineInfo;
pipelineCreateInfo.stageCount = 2;
pipelineCreateInfo.pStages = shaderStages;
VkPipeline pipeline;
if (vkCreateGraphicsPipelines(device_, VK_NULL_HANDLE, 1, &pipelineCreateInfo, nullptr,
&pipeline) != VK_SUCCESS) {
vkDestroyShaderModule(device_, fragShaderModule, nullptr);
vkDestroyShaderModule(device_, vertShaderModule, nullptr);
throw std::runtime_error("Failed to create graphics pipeline");
}
graphicsPipelines_.emplace(key, pipeline);
vkDestroyShaderModule(device_, fragShaderModule, nullptr);
vkDestroyShaderModule(device_, vertShaderModule, nullptr);
}
}
void VulkanCubeApp::CreateFramebuffers() {
swapChainFramebuffers_.resize(swapChainImageViews_.size());
for (size_t i = 0; i < swapChainImageViews_.size(); ++i) {
VkImageView attachments[] = {swapChainImageViews_[i]};
VkFramebufferCreateInfo framebufferInfo{};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = renderPass_;
framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = attachments;
framebufferInfo.width = swapChainExtent_.width;
framebufferInfo.height = swapChainExtent_.height;
framebufferInfo.layers = 1;
if (vkCreateFramebuffer(device_, &framebufferInfo, nullptr, &swapChainFramebuffers_[i]) !=
VK_SUCCESS) {
throw std::runtime_error("Failed to create framebuffer");
}
}
}
void VulkanCubeApp::CreateCommandPool() {
QueueFamilyIndices indices = FindQueueFamilies(physicalDevice_);
VkCommandPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
poolInfo.queueFamilyIndex = *indices.graphicsFamily;
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
if (vkCreateCommandPool(device_, &poolInfo, nullptr, &commandPool_) != VK_SUCCESS) {
throw std::runtime_error("Failed to create command pool");
}
}
void VulkanCubeApp::LoadCubeData() {
shaderPathMap_ = cubeScript_.LoadShaderPathsMap();
if (shaderPathMap_.empty()) {
throw std::runtime_error("Lua script did not provide shader paths");
}
defaultShaderKey_ = shaderPathMap_.count("default") ? "default" : shaderPathMap_.begin()->first;
auto sceneObjects = cubeScript_.LoadSceneObjects();
if (sceneObjects.empty()) {
throw std::runtime_error("Lua script did not provide any scene objects");
}
vertices_.clear();
indices_.clear();
renderObjects_.clear();
size_t vertexOffset = 0;
size_t indexOffset = 0;
for (const auto& sceneObject : sceneObjects) {
RenderObject renderObject{};
renderObject.vertexOffset = static_cast<int32_t>(vertexOffset);
renderObject.indexOffset = static_cast<uint32_t>(indexOffset);
renderObject.indexCount = static_cast<uint32_t>(sceneObject.indices.size());
renderObject.computeModelMatrixRef = sceneObject.computeModelMatrixRef;
renderObject.shaderKey = sceneObject.shaderKey;
if (shaderPathMap_.find(renderObject.shaderKey) == shaderPathMap_.end()) {
renderObject.shaderKey = defaultShaderKey_;
}
renderObjects_.push_back(renderObject);
vertices_.insert(vertices_.end(), sceneObject.vertices.begin(), sceneObject.vertices.end());
for (uint16_t index : sceneObject.indices) {
indices_.push_back(static_cast<uint16_t>(index + vertexOffset));
}
vertexOffset += sceneObject.vertices.size();
indexOffset += sceneObject.indices.size();
}
if (vertices_.empty() || indices_.empty()) {
throw std::runtime_error("Aggregated scene geometry is empty");
}
}
void VulkanCubeApp::CreateVertexBuffer() {
VkDeviceSize bufferSize = sizeof(vertices_[0]) * vertices_.size();
CreateBuffer(bufferSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, vertexBuffer_,
vertexBufferMemory_);
void* data;
vkMapMemory(device_, vertexBufferMemory_, 0, bufferSize, 0, &data);
std::memcpy(data, vertices_.data(), static_cast<size_t>(bufferSize));
vkUnmapMemory(device_, vertexBufferMemory_);
}
void VulkanCubeApp::CreateIndexBuffer() {
VkDeviceSize bufferSize = sizeof(indices_[0]) * indices_.size();
CreateBuffer(bufferSize, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, indexBuffer_,
indexBufferMemory_);
void* data;
vkMapMemory(device_, indexBufferMemory_, 0, bufferSize, 0, &data);
std::memcpy(data, indices_.data(), static_cast<size_t>(bufferSize));
vkUnmapMemory(device_, indexBufferMemory_);
}
void VulkanCubeApp::CreateCommandBuffers() {
commandBuffers_.resize(swapChainFramebuffers_.size());
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = commandPool_;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers_.size());
if (vkAllocateCommandBuffers(device_, &allocInfo, commandBuffers_.data()) != VK_SUCCESS) {
throw std::runtime_error("Failed to allocate command buffers");
}
}
void VulkanCubeApp::RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex, float time,
const std::array<float, 16>& viewProj) {
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
vkBeginCommandBuffer(commandBuffer, &beginInfo);
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = renderPass_;
renderPassInfo.framebuffer = swapChainFramebuffers_[imageIndex];
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = swapChainExtent_;
VkClearValue clearColor = {{{0.1f, 0.1f, 0.15f, 1.0f}}};
renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = &clearColor;
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
VkBuffer vertexBuffers[] = {vertexBuffer_};
VkDeviceSize offsets[] = {0};
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
vkCmdBindIndexBuffer(commandBuffer, indexBuffer_, 0, VK_INDEX_TYPE_UINT16);
core::PushConstants pushConstants{};
pushConstants.viewProj = viewProj;
for (const auto& object : renderObjects_) {
auto pipelineIt = graphicsPipelines_.find(object.shaderKey);
if (pipelineIt == graphicsPipelines_.end()) {
pipelineIt = graphicsPipelines_.find(defaultShaderKey_);
if (pipelineIt == graphicsPipelines_.end()) {
throw std::runtime_error("Missing pipeline for shader key");
}
}
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineIt->second);
pushConstants.model = cubeScript_.ComputeModelMatrix(object.computeModelMatrixRef, time);
vkCmdPushConstants(commandBuffer, pipelineLayout_, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(core::PushConstants),
&pushConstants);
vkCmdDrawIndexed(commandBuffer, object.indexCount, 1, object.indexOffset, object.vertexOffset, 0);
}
vkCmdEndRenderPass(commandBuffer);
vkEndCommandBuffer(commandBuffer);
}
void VulkanCubeApp::CreateSyncObjects() {
VkSemaphoreCreateInfo semaphoreInfo{};
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
VkFenceCreateInfo fenceInfo{};
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
if (vkCreateSemaphore(device_, &semaphoreInfo, nullptr, &imageAvailableSemaphore_) != VK_SUCCESS ||
vkCreateSemaphore(device_, &semaphoreInfo, nullptr, &renderFinishedSemaphore_) != VK_SUCCESS ||
vkCreateFence(device_, &fenceInfo, nullptr, &inFlightFence_) != VK_SUCCESS) {
throw std::runtime_error("Failed to create semaphores");
}
}
void VulkanCubeApp::DrawFrame(float time) {
vkWaitForFences(device_, 1, &inFlightFence_, VK_TRUE, std::numeric_limits<uint64_t>::max());
vkResetFences(device_, 1, &inFlightFence_);
uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR(device_, swapChain_, std::numeric_limits<uint64_t>::max(),
imageAvailableSemaphore_, VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized_) {
RecreateSwapChain();
return;
} else if (result != VK_SUCCESS) {
throw std::runtime_error("Failed to acquire swap chain image");
}
float aspect = static_cast<float>(swapChainExtent_.width) / static_cast<float>(swapChainExtent_.height);
auto viewProj = cubeScript_.GetViewProjectionMatrix(aspect);
vkResetCommandBuffer(commandBuffers_[imageIndex], 0);
RecordCommandBuffer(commandBuffers_[imageIndex], imageIndex, time, viewProj);
VkSemaphore waitSemaphores[] = {imageAvailableSemaphore_};
VkSemaphore signalSemaphores[] = {renderFinishedSemaphore_};
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffers_[imageIndex];
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
if (vkQueueSubmit(graphicsQueue_, 1, &submitInfo, inFlightFence_) != VK_SUCCESS) {
throw std::runtime_error("Failed to submit draw command buffer");
}
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &swapChain_;
presentInfo.pImageIndices = &imageIndex;
result = vkQueuePresentKHR(presentQueue_, &presentInfo);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized_) {
RecreateSwapChain();
} else if (result != VK_SUCCESS) {
throw std::runtime_error("Failed to present swap chain image");
}
}
QueueFamilyIndices VulkanCubeApp::FindQueueFamilies(VkPhysicalDevice device) {
QueueFamilyIndices indices;
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
int i = 0;
for (const auto& queueFamily : queueFamilies) {
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
indices.graphicsFamily = i;
}
VkBool32 presentSupport = VK_FALSE;
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface_, &presentSupport);
if (presentSupport) {
indices.presentFamily = i;
}
if (indices.isComplete()) {
break;
}
++i;
}
return indices;
}
bool VulkanCubeApp::CheckDeviceExtensionSupport(VkPhysicalDevice device) {
uint32_t extensionCount = 0;
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
std::vector<VkExtensionProperties> availableExtensions(extensionCount);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
std::set<std::string> requiredExtensions(kDeviceExtensions.begin(), kDeviceExtensions.end());
for (const auto& extension : availableExtensions) {
requiredExtensions.erase(extension.extensionName);
}
return requiredExtensions.empty();
}
SwapChainSupportDetails VulkanCubeApp::QuerySwapChainSupport(VkPhysicalDevice device) {
SwapChainSupportDetails details;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface_, &details.capabilities);
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface_, &formatCount, nullptr);
if (formatCount != 0) {
details.formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface_, &formatCount, details.formats.data());
}
uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface_, &presentModeCount, nullptr);
if (presentModeCount != 0) {
details.presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface_, &presentModeCount,
details.presentModes.data());
}
return details;
}
bool VulkanCubeApp::IsDeviceSuitable(VkPhysicalDevice device) {
QueueFamilyIndices indices = FindQueueFamilies(device);
bool extensionsSupported = CheckDeviceExtensionSupport(device);
bool swapChainAdequate = false;
if (extensionsSupported) {
auto details = QuerySwapChainSupport(device);
swapChainAdequate = !details.formats.empty() && !details.presentModes.empty();
}
return indices.isComplete() && extensionsSupported && swapChainAdequate;
}
VkSurfaceFormatKHR VulkanCubeApp::ChooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats) {
for (const auto& availableFormat : availableFormats) {
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB &&
availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
return availableFormat;
}
}
return availableFormats[0];
}
VkPresentModeKHR VulkanCubeApp::ChooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes) {
for (const auto& availablePresentMode : availablePresentModes) {
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
return availablePresentMode;
}
}
return VK_PRESENT_MODE_FIFO_KHR;
}
VkExtent2D VulkanCubeApp::ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
int width, height;
SDL_Vulkan_GetDrawableSize(window_, &width, &height);
VkExtent2D actualExtent = {
static_cast<uint32_t>(std::clamp(width, static_cast<int>(capabilities.minImageExtent.width),
static_cast<int>(capabilities.maxImageExtent.width))),
static_cast<uint32_t>(std::clamp(height, static_cast<int>(capabilities.minImageExtent.height),
static_cast<int>(capabilities.maxImageExtent.height)))
};
return actualExtent;
}
void VulkanCubeApp::CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties,
VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size;
bufferInfo.usage = usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateBuffer(device_, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
throw std::runtime_error("Failed to create buffer");
}
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device_, buffer, &memRequirements);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex =
FindMemoryType(memRequirements.memoryTypeBits, properties);
if (vkAllocateMemory(device_, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
throw std::runtime_error("Failed to allocate buffer memory");
}
vkBindBufferMemory(device_, buffer, bufferMemory, 0);
}
uint32_t VulkanCubeApp::FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice_, &memProperties);
for (uint32_t i = 0; i < memProperties.memoryTypeCount; ++i) {
if ((typeFilter & (1 << i)) &&
(memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
return i;
}
}
throw std::runtime_error("Failed to find suitable memory type");
}
} // namespace sdl3cpp::app