From 577d273419ab295735fe2dc7e15dc2868ff4a77f Mon Sep 17 00:00:00 2001 From: Tim Stullich Date: Wed, 21 Oct 2020 10:27:17 +0200 Subject: [PATCH] Added the blitz namespace to all necessary files --- src/buffer.cpp | 18 +++--- src/buffer.h | 4 +- src/camera.h | 4 +- src/gltfloader.cpp | 2 +- src/gltfloader.h | 2 + src/image.cpp | 10 ++-- src/image.h | 4 +- src/main.cpp | 2 +- src/renderer.cpp | 141 +++++++++++++++++++++++---------------------- src/renderer.h | 60 ++++++++++--------- src/shaderloader.h | 4 +- src/swapchain.cpp | 23 ++++---- src/swapchain.h | 13 +++-- src/texture.cpp | 8 +-- src/texture.h | 2 + src/ui.cpp | 28 ++++----- src/ui.h | 4 +- src/vertex.h | 5 +- 18 files changed, 181 insertions(+), 153 deletions(-) diff --git a/src/buffer.cpp b/src/buffer.cpp index 3b3d00a..e9b0fb6 100644 --- a/src/buffer.cpp +++ b/src/buffer.cpp @@ -2,7 +2,7 @@ // TODO Create a separate command pool for copying buffers and use VK_COMMAND_POOL_CREATE_TRANSIENT_BIT // during command pool creation -void Buffer::copyBuffer(const BufferContext &ctx, VkBuffer src, VkBuffer dst, VkDeviceSize size) { +void blitz::Buffer::copyBuffer(const BufferContext &ctx, VkBuffer src, VkBuffer dst, VkDeviceSize size) { VkCommandBufferAllocateInfo allocateInfo = {}; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.commandPool = ctx.commandPool; @@ -43,13 +43,13 @@ void Buffer::copyBuffer(const BufferContext &ctx, VkBuffer src, VkBuffer dst, Vk } -void Buffer::cleanup(VkDevice logicalDevice) { +void blitz::Buffer::cleanup(VkDevice logicalDevice) { vkDestroyBuffer(logicalDevice, buffer, nullptr); vkFreeMemory(logicalDevice, deviceMemory, nullptr); } -void Buffer::allocateMemory(const BufferContext &ctx, VkDeviceSize size, VkBufferUsageFlags usage, - VkMemoryPropertyFlags properties, VkBuffer &buf, VkDeviceMemory &devMem) { +void blitz::Buffer::allocateMemory(const Buffer::BufferContext &ctx, VkDeviceSize size, VkBufferUsageFlags usage, + VkMemoryPropertyFlags properties, VkBuffer &buf, VkDeviceMemory &devMem) { VkBufferCreateInfo bufferInfo = {}; bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; bufferInfo.size = size; @@ -76,7 +76,7 @@ void Buffer::allocateMemory(const BufferContext &ctx, VkDeviceSize size, VkBuffe vkBindBufferMemory(ctx.logicalDevice, buf, devMem, 0); } -uint32_t Buffer::pickMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties) { +uint32_t blitz::Buffer::pickMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties) { VkPhysicalDeviceMemoryProperties memProperties; vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties); @@ -89,7 +89,7 @@ uint32_t Buffer::pickMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFi throw std::runtime_error("Unable to find proper memory type on the GPU!"); } -void VertexBuffer::create(const BufferContext &ctx, const std::vector &vertices) { +void blitz::VertexBuffer::create(const BufferContext &ctx, const std::vector &vertices) { VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size(); // We use a staging buffer to transfer the host buffer data to the GPU @@ -114,7 +114,7 @@ void VertexBuffer::create(const BufferContext &ctx, const std::vector &v vkFreeMemory(ctx.logicalDevice, stagingMemory, nullptr); } -void IndexBuffer::create(const BufferContext &ctx, const std::vector &vertIndices) { +void blitz::IndexBuffer::create(const BufferContext &ctx, const std::vector &vertIndices) { VkDeviceSize bufferSize = sizeof(vertIndices[0]) * vertIndices.size(); VkBuffer stagingBuffer; @@ -137,14 +137,14 @@ void IndexBuffer::create(const BufferContext &ctx, const std::vector & vkFreeMemory(ctx.logicalDevice, stagingMemory, nullptr); } -void UniformBuffer::create(const BufferContext &ctx, const Camera &camera) { +void blitz::UniformBuffer::create(const BufferContext &ctx, const Camera &camera) { VkDeviceSize bufferSize = sizeof(Camera); allocateMemory(ctx, bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, buffer, deviceMemory); } -void TextureBuffer::create(const Buffer::BufferContext &ctx, const tinygltf::Image &textureImage) { +void blitz::TextureBuffer::create(const Buffer::BufferContext &ctx, const tinygltf::Image &textureImage) { VkDeviceSize bufferSize = textureImage.image.size(); allocateMemory(ctx, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | diff --git a/src/buffer.h b/src/buffer.h index 0849bfc..246b332 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -11,6 +11,7 @@ #include "vertex.h" // TODO Templatize this logic since vertex and index buffer creation is almost identical +namespace blitz { class Buffer { public: struct BufferContext { @@ -67,4 +68,5 @@ class TextureBuffer : public Buffer { TextureBuffer() = default; void create(const BufferContext &ctx, const tinygltf::Image &textureImage); -}; \ No newline at end of file +}; +} // namespace blitz \ No newline at end of file diff --git a/src/camera.h b/src/camera.h index eb060a4..577bb57 100644 --- a/src/camera.h +++ b/src/camera.h @@ -5,6 +5,7 @@ #include #include +namespace blitz { struct Camera { Camera() = default; @@ -18,4 +19,5 @@ struct Camera { glm::mat4 model = {}; glm::mat4 view = {}; glm::mat4 proj = {}; -}; \ No newline at end of file +}; +} // namespace blitz \ No newline at end of file diff --git a/src/gltfloader.cpp b/src/gltfloader.cpp index acb5efa..d148674 100644 --- a/src/gltfloader.cpp +++ b/src/gltfloader.cpp @@ -5,7 +5,7 @@ #define STB_IMAGE_WRITE_IMPLEMENTATION #include "tiny_gltf.h" -tinygltf::Model GLTFLoader::load(const std::string &filePath) { +tinygltf::Model blitz::GLTFLoader::load(const std::string &filePath) { tinygltf::Model model; tinygltf::TinyGLTF loader; std::string error; diff --git a/src/gltfloader.h b/src/gltfloader.h index b06ece3..984d90f 100644 --- a/src/gltfloader.h +++ b/src/gltfloader.h @@ -5,9 +5,11 @@ #include "tiny_gltf.h" +namespace blitz { class GLTFLoader { public: GLTFLoader() = delete; static tinygltf::Model load(const std::string &filePath); }; +} // namespace blitz \ No newline at end of file diff --git a/src/image.cpp b/src/image.cpp index b010535..6e55120 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1,10 +1,10 @@ #include "image.h" -Image::Image(VkDevice logicalDevice, const VkImageCreateInfo &imageInfo) { +blitz::Image::Image(VkDevice logicalDevice, const VkImageCreateInfo &imageInfo) { createImage(logicalDevice, imageInfo); } -void Image::bindImage(VkDevice logicalDevice, uint32_t memoryTypeIndex, VkImageAspectFlagBits aspectFlags) { +void blitz::Image::bindImage(VkDevice logicalDevice, uint32_t memoryTypeIndex, VkImageAspectFlagBits aspectFlags) { VkMemoryRequirements memRequirements; vkGetImageMemoryRequirements(logicalDevice, image, &memRequirements); @@ -23,13 +23,13 @@ void Image::bindImage(VkDevice logicalDevice, uint32_t memoryTypeIndex, VkImageA createImageView(logicalDevice, aspectFlags); } -void Image::cleanup(VkDevice logicalDevice) { +void blitz::Image::cleanup(VkDevice logicalDevice) { vkDestroyImageView(logicalDevice, imageView, nullptr); vkDestroyImage(logicalDevice, image, nullptr); vkFreeMemory(logicalDevice, imageMemory, nullptr); } -void Image::createImage(VkDevice logicalDevice, const VkImageCreateInfo &imageCreateInfo) { +void blitz::Image::createImage(VkDevice logicalDevice, const VkImageCreateInfo &imageCreateInfo) { // Store the image format and mip levels for later use format = imageCreateInfo.format; mipLevels = imageCreateInfo.mipLevels; @@ -40,7 +40,7 @@ void Image::createImage(VkDevice logicalDevice, const VkImageCreateInfo &imageCr } } -void Image::createImageView(VkDevice logicalDevice, VkImageAspectFlagBits aspectFlags) { +void blitz::Image::createImageView(VkDevice logicalDevice, VkImageAspectFlagBits aspectFlags) { VkImageViewCreateInfo viewInfo = {}; viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; viewInfo.image = image; diff --git a/src/image.h b/src/image.h index 8de1a8e..f189ac1 100644 --- a/src/image.h +++ b/src/image.h @@ -4,6 +4,7 @@ #include +namespace blitz { class Image { public: Image() = default; @@ -37,4 +38,5 @@ class Image { VkDeviceMemory imageMemory = {}; VkExtent3D extent = {}; uint32_t mipLevels = 1; -}; \ No newline at end of file +}; +} // namespace blitz \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index eef4263..1cdf1fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include "renderer.h" int main() { - Renderer app; + blitz::Renderer app; try { app.run(); } catch (std::exception &e) { diff --git a/src/renderer.cpp b/src/renderer.cpp index fab3d47..1ad6a17 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,13 +1,13 @@ #include "renderer.h" -Renderer::Renderer() { +blitz::Renderer::Renderer() { loadScene(); initWindow(); initVulkan(); initUI(); } -Renderer::~Renderer() { +blitz::Renderer::~Renderer() { std::cout << "Cleaning up" << std::endl; if (enableValidationLayers) { DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr); @@ -56,7 +56,7 @@ Renderer::~Renderer() { glfwTerminate(); } -VkCommandBuffer Renderer::beginSingleTimeCommands() { +VkCommandBuffer blitz::Renderer::beginSingleTimeCommands() { VkCommandBufferAllocateInfo allocInfo = {}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; @@ -75,7 +75,7 @@ VkCommandBuffer Renderer::beginSingleTimeCommands() { return commandBuffer; } -bool Renderer::checkDeviceExtensions(VkPhysicalDevice device) { +bool blitz::Renderer::checkDeviceExtensions(VkPhysicalDevice device) { uint32_t extensionsCount = 0; if (vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionsCount, nullptr) != VK_SUCCESS) { throw std::runtime_error("Unable to enumerate device extensions!"); @@ -92,7 +92,7 @@ bool Renderer::checkDeviceExtensions(VkPhysicalDevice device) { return requiredDeviceExtensions.empty(); } -bool Renderer::checkValidationLayerSupport() { +bool blitz::Renderer::checkValidationLayerSupport() { uint32_t layerCount; vkEnumerateInstanceLayerProperties(&layerCount, nullptr); @@ -116,7 +116,7 @@ bool Renderer::checkValidationLayerSupport() { } // Destroys all the resources associated with swapchain recreation -void Renderer::cleanupSwapchain() { +void blitz::Renderer::cleanupSwapchain() { msaaImage.cleanup(logicalDevice); depthImage.cleanup(logicalDevice); @@ -135,7 +135,7 @@ void Renderer::cleanupSwapchain() { vkDestroyDescriptorPool(logicalDevice, descriptorPool, nullptr); } -void Renderer::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) { +void blitz::Renderer::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) { VkCommandBuffer commandBuffer = beginSingleTimeCommands(); VkBufferImageCopy region = {}; @@ -156,7 +156,7 @@ void Renderer::copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, endSingleTimeCommands(commandBuffer); } -Buffer::BufferContext Renderer::createBufferContext() { +blitz::Buffer::BufferContext blitz::Renderer::createBufferContext() { Buffer::BufferContext ctx = {}; ctx.physicalDevice = physicalDevice; ctx.logicalDevice = logicalDevice; @@ -166,7 +166,7 @@ Buffer::BufferContext Renderer::createBufferContext() { return ctx; } -void Renderer::createCommandBuffers() { +void blitz::Renderer::createCommandBuffers() { commandBuffers.resize(swapchain.getFramebufferSize()); VkCommandBufferAllocateInfo allocInfo = {}; @@ -225,7 +225,7 @@ void Renderer::createCommandBuffers() { } } -void Renderer::createCommandPool() { +void blitz::Renderer::createCommandPool() { VkCommandPoolCreateInfo createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; createInfo.queueFamilyIndex = queueIndices.graphicsFamilyIndex; @@ -235,7 +235,7 @@ void Renderer::createCommandPool() { } } -void Renderer::createDepthResources() { +void blitz::Renderer::createDepthResources() { VkImageCreateInfo imageInfo = {}; imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageInfo.imageType = VK_IMAGE_TYPE_2D; @@ -262,7 +262,7 @@ void Renderer::createDepthResources() { } // TODO Check if this is used by Dear IMGUI and how to move it -void Renderer::createDescriptorPool() { +void blitz::Renderer::createDescriptorPool() { VkDescriptorPoolSize uboPoolSize = {}; uboPoolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; uboPoolSize.descriptorCount = static_cast(swapchain.getImagesSize()); @@ -284,7 +284,7 @@ void Renderer::createDescriptorPool() { } } -void Renderer::createDescriptorSetLayout() { +void blitz::Renderer::createDescriptorSetLayout() { VkDescriptorSetLayoutBinding uboBinding = {}; uboBinding.binding = 0; uboBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; @@ -310,7 +310,7 @@ void Renderer::createDescriptorSetLayout() { } } -void Renderer::createDescriptorSets() { +void blitz::Renderer::createDescriptorSets() { std::vector layouts(swapchain.getImagesSize(), descriptorSetLayout); VkDescriptorSetAllocateInfo allocInfo = {}; allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; @@ -356,7 +356,7 @@ void Renderer::createDescriptorSets() { } } -void Renderer::createGraphicsPipeline() { +void blitz::Renderer::createGraphicsPipeline() { // Load our shader modules in from disk auto vertShaderCode = ShaderLoader::load("shaders/vert.spv"); auto fragShaderCode = ShaderLoader::load("shaders/frag.spv"); @@ -511,9 +511,9 @@ void Renderer::createGraphicsPipeline() { vkDestroyShaderModule(logicalDevice, fragShaderModule, nullptr); } -void Renderer::createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, - VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image, - VkDeviceMemory &imageMemory) { +void blitz::Renderer::createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, + VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image, + VkDeviceMemory &imageMemory) { VkImageCreateInfo imageInfo = {}; imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageInfo.imageType = VK_IMAGE_TYPE_2D; @@ -548,12 +548,12 @@ void Renderer::createImage(uint32_t width, uint32_t height, VkFormat format, VkI vkBindImageMemory(logicalDevice, image, imageMemory, 0); } -void Renderer::createIndexBuffer() { +void blitz::Renderer::createIndexBuffer() { auto ctx = createBufferContext(); vertIndexBuffer.create(ctx, vertIndices); } -void Renderer::createInstance() { +void blitz::Renderer::createInstance() { if (enableValidationLayers && !checkValidationLayerSupport()) { throw std::runtime_error("Unable to establish validation layer support!"); } @@ -592,7 +592,7 @@ void Renderer::createInstance() { } } -void Renderer::createLogicalDevice() { +void blitz::Renderer::createLogicalDevice() { // Setup our Command Queues std::set uniqueQueueIndices = {queueIndices.graphicsFamilyIndex, queueIndices.presentFamilyIndex}; std::vector queueCreateInfos; @@ -636,7 +636,7 @@ void Renderer::createLogicalDevice() { vkGetDeviceQueue(logicalDevice, queueIndices.presentFamilyIndex, 0, &presentQueue); } -void Renderer::createMsaaResources() { +void blitz::Renderer::createMsaaResources() { auto extent = swapchain.getExtent(); VkImageCreateInfo imageCreateInfo = {}; @@ -662,7 +662,7 @@ void Renderer::createMsaaResources() { msaaImage.bindImage(logicalDevice, memoryTypeIndex, VK_IMAGE_ASPECT_COLOR_BIT); } -void Renderer::createRenderPass() { +void blitz::Renderer::createRenderPass() { // Configure a color attachment that will determine how the framebuffer is used VkAttachmentDescription colorAttachment = {}; colorAttachment.format = swapchain.getImageFormat(); @@ -739,7 +739,7 @@ void Renderer::createRenderPass() { } } -VkShaderModule Renderer::createShaderModule(const std::vector &shaderCode) { +VkShaderModule blitz::Renderer::createShaderModule(const std::vector &shaderCode) { VkShaderModuleCreateInfo createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; createInfo.codeSize = shaderCode.size(); @@ -754,17 +754,17 @@ VkShaderModule Renderer::createShaderModule(const std::vector &shaderCode) } // For cross-platform compatibility we let GLFW take care of the surface creation -void Renderer::createSurface() { +void blitz::Renderer::createSurface() { if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) { throw std::runtime_error("Unable to create window surface!"); } } -void Renderer::createSwapchain() { +void blitz::Renderer::createSwapchain() { swapchain.init(createSwapchainContext()); } -Swapchain::SwapchainContext Renderer::createSwapchainContext() { +blitz::Swapchain::SwapchainContext blitz::Renderer::createSwapchainContext() { Swapchain::SwapchainContext ctx = {}; ctx.physicalDevice = physicalDevice; ctx.logicalDevice = logicalDevice; @@ -777,7 +777,7 @@ Swapchain::SwapchainContext Renderer::createSwapchainContext() { return ctx; } -void Renderer::createSyncObjects() { +void blitz::Renderer::createSyncObjects() { // Create our semaphores and fences for synchronizing the GPU and CPU imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT); renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT); @@ -810,7 +810,7 @@ void Renderer::createSyncObjects() { } // TODO Look into recording a buffer that uploads all texture asynchronously -void Renderer::createTextureImage() { +void blitz::Renderer::createTextureImage() { // Allocate a host-side staging buffer auto ctx = createBufferContext(); auto stagingBuffer = TextureBuffer(); @@ -841,7 +841,7 @@ void Renderer::createTextureImage() { generateMipMaps(texture); } -void Renderer::createTextureSampler() { +void blitz::Renderer::createTextureSampler() { VkSamplerCreateInfo samplerInfo = {}; samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; samplerInfo.magFilter = VK_FILTER_LINEAR; // TODO Grab this from glTF @@ -865,7 +865,7 @@ void Renderer::createTextureSampler() { } } -UserInterface::UIContext Renderer::createUIContext() { +blitz::UserInterface::UIContext blitz::Renderer::createUIContext() { UserInterface::UIContext context = {}; context.window = window; context.instance = instance; @@ -879,7 +879,7 @@ UserInterface::UIContext Renderer::createUIContext() { return context; } -void Renderer::createUniformBuffers() { +void blitz::Renderer::createUniformBuffers() { // Create a uniform buffer for our camera parameters uniformBuffers.resize(swapchain.getImagesSize()); for (size_t i = 0; i < swapchain.getImagesSize(); ++i) { @@ -887,12 +887,12 @@ void Renderer::createUniformBuffers() { } } -void Renderer::createVertexBuffer() { +void blitz::Renderer::createVertexBuffer() { auto ctx = createBufferContext(); vertexBuffer.create(ctx, vertices); } -void Renderer::drawFrame() { +void blitz::Renderer::drawFrame() { // Sync for next frame. Fences also need to be manually reset unlike semaphores, which is done here vkWaitForFences(logicalDevice, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX); @@ -969,7 +969,7 @@ void Renderer::drawFrame() { currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; } -void Renderer::endSingleTimeCommands(VkCommandBuffer commandBuffer) { +void blitz::Renderer::endSingleTimeCommands(VkCommandBuffer commandBuffer) { vkEndCommandBuffer(commandBuffer); VkSubmitInfo submitInfo{}; @@ -983,10 +983,10 @@ void Renderer::endSingleTimeCommands(VkCommandBuffer commandBuffer) { vkFreeCommandBuffers(logicalDevice, commandPool, 1, &commandBuffer); } -void Renderer::generateMipMaps(const Texture &texture) { +void blitz::Renderer::generateMipMaps(const Texture &tex) { // Check if the blit command is available on the physical device VkFormatProperties properties; - vkGetPhysicalDeviceFormatProperties(physicalDevice, texture.getTexImage().getFormat(), &properties); + vkGetPhysicalDeviceFormatProperties(physicalDevice, tex.getTexImage().getFormat(), &properties); if (!(properties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)) { throw std::runtime_error("Texture image format does not support linear blitting!"); } @@ -995,7 +995,7 @@ void Renderer::generateMipMaps(const Texture &texture) { VkImageMemoryBarrier memBarrier = {}; memBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - memBarrier.image = texture.getTexImage().getImage(); + memBarrier.image = tex.getTexImage().getImage(); memBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; memBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; memBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; @@ -1003,9 +1003,9 @@ void Renderer::generateMipMaps(const Texture &texture) { memBarrier.subresourceRange.layerCount = 1; memBarrier.subresourceRange.levelCount = 1; - int32_t mipWidth = texture.getWidth(); - int32_t mipHeight = texture.getHeight(); - for (uint32_t i = 1; i < texture.getMipLevels(); ++i) { + int32_t mipWidth = tex.getWidth(); + int32_t mipHeight = tex.getHeight(); + for (uint32_t i = 1; i < tex.getMipLevels(); ++i) { memBarrier.subresourceRange.baseMipLevel = i - 1; memBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; memBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; @@ -1034,8 +1034,8 @@ void Renderer::generateMipMaps(const Texture &texture) { blit.dstSubresource.layerCount = 1; vkCmdBlitImage(commandBuffer, - texture.getTexImage().getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, - texture.getTexImage().getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + tex.getTexImage().getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, + tex.getTexImage().getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, VK_FILTER_LINEAR); @@ -1056,7 +1056,7 @@ void Renderer::generateMipMaps(const Texture &texture) { } // Put in a final barrier for transitioning - memBarrier.subresourceRange.baseMipLevel = texture.getMipLevels() - 1; + memBarrier.subresourceRange.baseMipLevel = tex.getMipLevels() - 1; memBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; memBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; memBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; @@ -1071,7 +1071,7 @@ void Renderer::generateMipMaps(const Texture &texture) { endSingleTimeCommands(commandBuffer); } -void Renderer::getDeviceQueueIndices() { +void blitz::Renderer::getDeviceQueueIndices() { uint32_t queueFamilyCount = 0; vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr); @@ -1096,7 +1096,7 @@ void Renderer::getDeviceQueueIndices() { } } -std::vector Renderer::getRequiredExtensions() const { +std::vector blitz::Renderer::getRequiredExtensions() const { uint32_t glfwExtensionCount = 0; const char** glfwRequiredExtensions; glfwRequiredExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); @@ -1112,11 +1112,12 @@ std::vector Renderer::getRequiredExtensions() const { return extensions; } -void Renderer::initUI() { - ui.init(createUIContext()); +void blitz::Renderer::initUI() { + auto ctx = createUIContext(); + ui.init(ctx); } -void Renderer::initVulkan() { +void blitz::Renderer::initVulkan() { createInstance(); setupDebugMessenger(); createSurface(); @@ -1142,7 +1143,7 @@ void Renderer::initVulkan() { createSyncObjects(); } -void Renderer::initWindow() { +void blitz::Renderer::initWindow() { if (!glfwInit()) { throw std::runtime_error("Unable to initialize GLFW!"); } @@ -1163,7 +1164,7 @@ void Renderer::initWindow() { } } -bool Renderer::isDeviceSuitable(VkPhysicalDevice device) { +bool blitz::Renderer::isDeviceSuitable(VkPhysicalDevice device) { VkPhysicalDeviceFeatures supportedFeatures; vkGetPhysicalDeviceFeatures(device, &supportedFeatures); @@ -1182,13 +1183,13 @@ bool Renderer::isDeviceSuitable(VkPhysicalDevice device) { supportedFeatures.samplerAnisotropy; } -void Renderer::keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { +void blitz::Renderer::keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) { glfwSetWindowShouldClose(window, GLFW_TRUE); } } -void Renderer::loadCamera(tinygltf::Model &model, tinygltf::Node &node) { +void blitz::Renderer::loadCamera(tinygltf::Model &model, tinygltf::Node &node) { // TODO Figure out why this does not work! auto translation = glm::mat4(1.0f); if (!node.translation.empty()) { @@ -1213,7 +1214,7 @@ void Renderer::loadCamera(tinygltf::Model &model, tinygltf::Node &node) { cam.model = translation * rotation * scale; } -void Renderer::loadMesh(tinygltf::Model &model, tinygltf::Mesh &mesh) { +void blitz::Renderer::loadMesh(tinygltf::Model &model, tinygltf::Mesh &mesh) { for (auto primitive : mesh.primitives) { if (primitive.indices >= 0) { // Mesh supports indexing. Load the indices @@ -1228,7 +1229,7 @@ void Renderer::loadMesh(tinygltf::Model &model, tinygltf::Mesh &mesh) { } } -void Renderer::loadMeshIndices(tinygltf::Model &model, tinygltf::Primitive &primitive) { +void blitz::Renderer::loadMeshIndices(tinygltf::Model &model, tinygltf::Primitive &primitive) { auto indicesAccessor = model.accessors[primitive.indices]; auto bufferView = model.bufferViews[indicesAccessor.bufferView]; auto buffer = model.buffers[bufferView.buffer]; @@ -1249,7 +1250,7 @@ void Renderer::loadMeshIndices(tinygltf::Model &model, tinygltf::Primitive &prim } } -void Renderer::loadMeshMaterial(tinygltf::Model &model, tinygltf::Primitive &primitive) { +void blitz::Renderer::loadMeshMaterial(tinygltf::Model &model, tinygltf::Primitive &primitive) { // TODO Expand the logic here later to properly handle PBR-related things auto material = model.materials[primitive.material]; if (material.pbrMetallicRoughness.baseColorTexture.index >= 0) { @@ -1259,7 +1260,7 @@ void Renderer::loadMeshMaterial(tinygltf::Model &model, tinygltf::Primitive &pri } } -void Renderer::loadVertexAttributes(tinygltf::Model &model, tinygltf::Mesh &mesh, tinygltf::Primitive &primitive) { +void blitz::Renderer::loadVertexAttributes(tinygltf::Model &model, tinygltf::Mesh &mesh, tinygltf::Primitive &primitive) { auto positionIndex = primitive.attributes.find("POSITION")->second; auto normalIndex = primitive.attributes.find("NORMAL")->second; auto texIndex = primitive.attributes.find("TEXCOORD_0")->second; @@ -1321,7 +1322,7 @@ void Renderer::loadVertexAttributes(tinygltf::Model &model, tinygltf::Mesh &mesh } } -void Renderer::loadNode(tinygltf::Model &model, tinygltf::Node &node) { +void blitz::Renderer::loadNode(tinygltf::Model &model, tinygltf::Node &node) { if (node.mesh >= 0 && node.mesh < model.meshes.size()) { loadMesh(model, model.meshes[node.mesh]); } @@ -1337,7 +1338,7 @@ void Renderer::loadNode(tinygltf::Model &model, tinygltf::Node &node) { } } -void Renderer::loadScene() { +void blitz::Renderer::loadScene() { auto model = GLTFLoader::load("models/helmet/SciFiHelmet.gltf"); auto scene = model.scenes[model.defaultScene]; cam = Camera(static_cast(windowWidth) / windowHeight); @@ -1346,7 +1347,7 @@ void Renderer::loadScene() { } } -VkFormat Renderer::pickDepthFormat() { +VkFormat blitz::Renderer::pickDepthFormat() { return pickSupportedFormat( {VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT}, VK_IMAGE_TILING_OPTIMAL, @@ -1354,7 +1355,7 @@ VkFormat Renderer::pickDepthFormat() { ); } -VkSampleCountFlagBits Renderer::pickMaxUsableSampleCount(VkPhysicalDevice device) { +VkSampleCountFlagBits blitz::Renderer::pickMaxUsableSampleCount(VkPhysicalDevice device) { VkPhysicalDeviceProperties physicalDeviceProperties; vkGetPhysicalDeviceProperties(device, &physicalDeviceProperties); @@ -1370,7 +1371,7 @@ VkSampleCountFlagBits Renderer::pickMaxUsableSampleCount(VkPhysicalDevice device return VK_SAMPLE_COUNT_1_BIT; } -uint32_t Renderer::pickMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) { +uint32_t blitz::Renderer::pickMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) { VkPhysicalDeviceMemoryProperties memProperties; vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties); @@ -1383,7 +1384,7 @@ uint32_t Renderer::pickMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags pro throw std::runtime_error("Unable to find proper memory type on the GPU!"); } -VkPhysicalDevice Renderer::pickPhysicalDevice() { +VkPhysicalDevice blitz::Renderer::pickPhysicalDevice() { uint32_t physicalDeviceCount = 0; if (vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr) != VK_SUCCESS) { throw std::runtime_error("Unable to enumerate physical devices!"); @@ -1420,8 +1421,8 @@ VkPhysicalDevice Renderer::pickPhysicalDevice() { return physicalDevices[0]; } -VkFormat Renderer::pickSupportedFormat(const std::vector &candidates, VkImageTiling tiling, - VkFormatFeatureFlags features) { +VkFormat blitz::Renderer::pickSupportedFormat(const std::vector &candidates, VkImageTiling tiling, + VkFormatFeatureFlags features) { for (const auto& format : candidates) { VkFormatProperties properties; vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &properties); @@ -1436,7 +1437,7 @@ VkFormat Renderer::pickSupportedFormat(const std::vector &candidates, // In case the swapchain is invalidated, i.e. during window resizing, // we need to implement a mechanism to recreate it -void Renderer::recreateSwapchain() { +void blitz::Renderer::recreateSwapchain() { int width = 0, height = 0; glfwGetFramebufferSize(window, &width, &height); while (width == 0 || height == 0) { @@ -1470,7 +1471,7 @@ void Renderer::recreateSwapchain() { ui.recreate(createUIContext()); } -void Renderer::run() { +void blitz::Renderer::run() { while (!glfwWindowShouldClose(window)) { glfwPollEvents(); ui.draw(); @@ -1481,7 +1482,7 @@ void Renderer::run() { vkDeviceWaitIdle(logicalDevice); } -void Renderer::setupDebugMessenger() { +void blitz::Renderer::setupDebugMessenger() { if (!enableValidationLayers) { return; } @@ -1494,7 +1495,7 @@ void Renderer::setupDebugMessenger() { } } -void Renderer::transitionImageLayout(const Image &image, VkImageLayout oldLayout, VkImageLayout newLayout) { +void blitz::Renderer::transitionImageLayout(const Image &image, VkImageLayout oldLayout, VkImageLayout newLayout) { VkCommandBuffer commandBuffer = beginSingleTimeCommands(); VkImageMemoryBarrier barrier = {}; @@ -1551,7 +1552,7 @@ void Renderer::transitionImageLayout(const Image &image, VkImageLayout oldLayout endSingleTimeCommands(commandBuffer); } -void Renderer::updateUniformBuffer(size_t bufferIdx) { +void blitz::Renderer::updateUniformBuffer(size_t bufferIdx) { static auto startTime = std::chrono::high_resolution_clock::now(); auto currentTime = std::chrono::high_resolution_clock::now(); diff --git a/src/renderer.h b/src/renderer.h index 2fb9830..dcee132 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -26,6 +26,7 @@ #include "ui.h" #include "vertex.h" +namespace blitz { class Renderer { public: Renderer(); @@ -41,9 +42,12 @@ class Renderer { uint32_t presentFamilyIndex; }; - static VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, - const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger) { - auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); + static VkResult + CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo, + const VkAllocationCallbacks *pAllocator, + VkDebugUtilsMessengerEXT *pDebugMessenger) { + auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, + "vkCreateDebugUtilsMessengerEXT"); if (func != nullptr) { return func(instance, pCreateInfo, pAllocator, pDebugMessenger); } else { @@ -54,8 +58,8 @@ class Renderer { static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, - const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, - void* pUserData) { + const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, + void *pUserData) { std::cerr << "Validation layer: " << pCallbackData->pMessage << std::endl; @@ -63,17 +67,18 @@ class Renderer { } static void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, - const VkAllocationCallbacks* pAllocator) { - auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT"); + const VkAllocationCallbacks *pAllocator) { + auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, + "vkDestroyDebugUtilsMessengerEXT"); if (func != nullptr) { func(instance, debugMessenger, pAllocator); } } - static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); + static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); - static void framebufferResizeCallback(GLFWwindow* window, int width, int height) { - auto app = reinterpret_cast(glfwGetWindowUserPointer(window)); + static void framebufferResizeCallback(GLFWwindow *window, int width, int height) { + auto app = reinterpret_cast(glfwGetWindowUserPointer(window)); app->framebufferResized = true; } @@ -116,8 +121,8 @@ class Renderer { void createGraphicsPipeline(); void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, - VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, - VkDeviceMemory& imageMemory); + VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image, + VkDeviceMemory &imageMemory); VkImageView createImageView(VkImage image, VkFormat format); @@ -155,14 +160,14 @@ class Renderer { void endSingleTimeCommands(VkCommandBuffer commandBuffer); - void generateMipMaps(const Texture &texture); + void generateMipMaps(const Texture &tex); void getDeviceQueueIndices(); - std::vector getRequiredExtensions() const; + std::vector getRequiredExtensions() const; inline bool hasStencilComponent(VkFormat format) { - return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT; + return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT; } void initUI(); @@ -189,7 +194,7 @@ class Renderer { VkFormat pickDepthFormat(); - VkSampleCountFlagBits pickMaxUsableSampleCount(VkPhysicalDevice device); + static VkSampleCountFlagBits pickMaxUsableSampleCount(VkPhysicalDevice device); uint32_t pickMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties); @@ -262,14 +267,14 @@ class Renderer { bool framebufferResized = false; - std::vector requiredExtensions; + std::vector requiredExtensions; - const std::array validationLayers = { - "VK_LAYER_KHRONOS_validation", + const std::array validationLayers = { + "VK_LAYER_KHRONOS_validation", }; - const std::array deviceExtensions = { - VK_KHR_SWAPCHAIN_EXTENSION_NAME + const std::array deviceExtensions = { + VK_KHR_SWAPCHAIN_EXTENSION_NAME }; std::vector vertices = {}; @@ -279,9 +284,10 @@ class Renderer { // Debug utilities VkDebugUtilsMessengerEXT debugMessenger = {}; - #ifdef NDEBUG - const bool enableValidationLayers = false; - #else - const bool enableValidationLayers = true; - #endif -}; \ No newline at end of file +#ifdef NDEBUG + const bool enableValidationLayers = false; +#else + const bool enableValidationLayers = true; +#endif +}; +} // namespace blitz \ No newline at end of file diff --git a/src/shaderloader.h b/src/shaderloader.h index 7e05d51..b6800ed 100644 --- a/src/shaderloader.h +++ b/src/shaderloader.h @@ -5,6 +5,7 @@ #include #include +namespace blitz { class ShaderLoader { public: ShaderLoader() = delete; @@ -26,4 +27,5 @@ class ShaderLoader { return buffer; } -}; \ No newline at end of file +}; +} \ No newline at end of file diff --git a/src/swapchain.cpp b/src/swapchain.cpp index 3556b67..00d9017 100644 --- a/src/swapchain.cpp +++ b/src/swapchain.cpp @@ -4,7 +4,7 @@ #include "swapchain.h" -void Swapchain::cleanup() { +void blitz::Swapchain::cleanup() { for (auto framebuffer : swapchainFramebuffers) { vkDestroyFramebuffer(ctx.logicalDevice, framebuffer, nullptr); } @@ -16,14 +16,14 @@ void Swapchain::cleanup() { vkDestroySwapchainKHR(ctx.logicalDevice, swapchain, nullptr); } -void Swapchain::init(const SwapchainContext &context) { +void blitz::Swapchain::init(const SwapchainContext &context) { ctx = context; createSwapchain(); createImageViews(); } -void Swapchain::initFramebuffers(const VkRenderPass &renderPass, const VkImageView &depthImageView, - const VkImageView &msaaImageView) { +void blitz::Swapchain::initFramebuffers(const VkRenderPass &renderPass, const VkImageView &depthImageView, + const VkImageView &msaaImageView) { swapchainFramebuffers.resize(swapchainImageViews.size()); for (size_t i = 0; i < swapchainImageViews.size(); ++i) { // We need to attach an image view to the frame buffer for presentation purposes @@ -47,7 +47,7 @@ void Swapchain::initFramebuffers(const VkRenderPass &renderPass, const VkImageVi } } -void Swapchain::createImageViews() { +void blitz::Swapchain::createImageViews() { swapchainImageViews.resize(swapchainImages.size()); for (size_t i = 0; i < swapchainImages.size(); ++i) { VkImageViewCreateInfo createInfo = {}; @@ -59,7 +59,7 @@ void Swapchain::createImageViews() { createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; - createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; // We do not enable mip-mapping + createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; createInfo.subresourceRange.baseArrayLayer = 0; createInfo.subresourceRange.baseMipLevel = 0; createInfo.subresourceRange.levelCount = 1; @@ -72,7 +72,7 @@ void Swapchain::createImageViews() { } } -void Swapchain::createSwapchain() { +void blitz::Swapchain::createSwapchain() { SwapchainConfiguration configuration = querySwapchainSupport(ctx.physicalDevice, ctx.surface); VkSurfaceFormatKHR surfaceFormat = pickSwapchainSurfaceFormat(configuration.surfaceFormats); @@ -125,7 +125,7 @@ void Swapchain::createSwapchain() { vkGetSwapchainImagesKHR(ctx.logicalDevice, swapchain, &imageCount, swapchainImages.data()); } -VkExtent2D Swapchain::pickSwapchainExtent(const VkSurfaceCapabilitiesKHR &surfaceCapabilities, uint32_t width, uint32_t height) { +VkExtent2D blitz::Swapchain::pickSwapchainExtent(const VkSurfaceCapabilitiesKHR &surfaceCapabilities, uint32_t width, uint32_t height) { if (surfaceCapabilities.currentExtent.width != UINT32_MAX) { return surfaceCapabilities.currentExtent; } else { @@ -138,7 +138,7 @@ VkExtent2D Swapchain::pickSwapchainExtent(const VkSurfaceCapabilitiesKHR &surfac } } -VkPresentModeKHR Swapchain::pickSwapchainPresentMode(const std::vector &presentModes) { +VkPresentModeKHR blitz::Swapchain::pickSwapchainPresentMode(const std::vector &presentModes) { // Look for triple-buffering present mode if available for (VkPresentModeKHR availableMode : presentModes) { if (availableMode == VK_PRESENT_MODE_MAILBOX_KHR) { @@ -150,7 +150,7 @@ VkPresentModeKHR Swapchain::pickSwapchainPresentMode(const std::vector &formats) { +VkSurfaceFormatKHR blitz::Swapchain::pickSwapchainSurfaceFormat(const std::vector &formats) { for (VkSurfaceFormatKHR availableFormat : formats) { if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB && availableFormat.colorSpace == VK_COLORSPACE_SRGB_NONLINEAR_KHR) { @@ -163,7 +163,8 @@ VkSurfaceFormatKHR Swapchain::pickSwapchainSurfaceFormat(const std::vector +namespace blitz { class Swapchain { public: struct SwapchainContext { @@ -31,7 +32,8 @@ class Swapchain { void init(const SwapchainContext &context); - void initFramebuffers(const VkRenderPass &renderPass, const VkImageView &depthImageView, const VkImageView &msaaImageView); + void initFramebuffers(const VkRenderPass &renderPass, const VkImageView &depthImageView, + const VkImageView &msaaImageView); inline size_t getFramebufferSize() const { return swapchainFramebuffers.size(); } @@ -51,14 +53,16 @@ class Swapchain { inline VkSwapchainKHR getSwapchain() const { return swapchain; } - static SwapchainConfiguration querySwapchainSupport(const VkPhysicalDevice &physicalDevice, const VkSurfaceKHR &surface); + static SwapchainConfiguration + querySwapchainSupport(const VkPhysicalDevice &physicalDevice, const VkSurfaceKHR &surface); private: void createImageViews(); void createSwapchain(); - static VkExtent2D pickSwapchainExtent(const VkSurfaceCapabilitiesKHR &surfaceCapabilities, uint32_t width, uint32_t height); + static VkExtent2D + pickSwapchainExtent(const VkSurfaceCapabilitiesKHR &surfaceCapabilities, uint32_t width, uint32_t height); static VkPresentModeKHR pickSwapchainPresentMode(const std::vector &presentModes); @@ -74,4 +78,5 @@ class Swapchain { std::vector swapchainFramebuffers; uint32_t imageCount = 0; -}; \ No newline at end of file +}; +} // namespace blitz \ No newline at end of file diff --git a/src/texture.cpp b/src/texture.cpp index 3895c99..de15ca3 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,6 +1,6 @@ #include "texture.h" -Texture::Texture(VkDevice logicalDevice, const tinygltf::Image &image, bool createMips) { +blitz::Texture::Texture(VkDevice logicalDevice, const tinygltf::Image &image, bool createMips) { auto imageWidth = static_cast(image.width); auto imageHeight = static_cast(image.height); auto mipLevels = createMips ? calculateMipLevels(imageWidth, imageHeight) : 1; @@ -29,14 +29,14 @@ Texture::Texture(VkDevice logicalDevice, const tinygltf::Image &image, bool crea textureImage = Image(logicalDevice, imageInfo); } -void Texture::bind(VkDevice logicalDevice, uint32_t memoryTypeIndex) { +void blitz::Texture::bind(VkDevice logicalDevice, uint32_t memoryTypeIndex) { textureImage.bindImage(logicalDevice, memoryTypeIndex, VK_IMAGE_ASPECT_COLOR_BIT); } -void Texture::cleanup(VkDevice logicalDevice) { +void blitz::Texture::cleanup(VkDevice logicalDevice) { textureImage.cleanup(logicalDevice); } -uint32_t Texture::calculateMipLevels(const uint32_t &texWidth, const uint32_t &texHeight) { +uint32_t blitz::Texture::calculateMipLevels(const uint32_t &texWidth, const uint32_t &texHeight) { return static_cast(std::floor(std::log2(std::max(texWidth, texHeight)))) + 1; } \ No newline at end of file diff --git a/src/texture.h b/src/texture.h index df09f9e..703fb73 100644 --- a/src/texture.h +++ b/src/texture.h @@ -7,6 +7,7 @@ #include "buffer.h" #include "image.h" +namespace blitz { class Texture { public: Texture() = default; @@ -32,3 +33,4 @@ class Texture { Image textureImage = {}; }; +} // namespace blitz diff --git a/src/ui.cpp b/src/ui.cpp index 1d527bb..f8d64c3 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1,6 +1,6 @@ #include "ui.h" -void UserInterface::cleanupResources() { +void blitz::UserInterface::cleanupResources() { for (auto framebuffer : framebuffers) { vkDestroyFramebuffer(context.logicalDevice, framebuffer, nullptr); } @@ -9,7 +9,7 @@ void UserInterface::cleanupResources() { commandBuffers.data()); } -void UserInterface::cleanup() { +void blitz::UserInterface::cleanup() { ImGui_ImplVulkan_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); @@ -22,7 +22,7 @@ void UserInterface::cleanup() { vkDestroyRenderPass(context.logicalDevice, renderPass, nullptr); } -void UserInterface::draw() { +void blitz::UserInterface::draw() { // Start the Dear ImGui frame ImGui_ImplVulkan_NewFrame(); ImGui_ImplGlfw_NewFrame(); @@ -35,12 +35,12 @@ void UserInterface::draw() { ImGui::Checkbox("Rotate Object", &options.rotate); - ImGui::ShowDemoWindow(); + //ImGui::ShowDemoWindow(); ImGui::Render(); } -void UserInterface::init(const UIContext &ctx) { +void blitz::UserInterface::init(const UIContext &ctx) { context = ctx; IMGUI_CHECKVERSION(); @@ -74,7 +74,7 @@ void UserInterface::init(const UIContext &ctx) { ImGui_ImplVulkan_DestroyFontUploadObjects(); } -VkCommandBuffer UserInterface::beginSingleTimeCommands(VkCommandPool cmdPool) const { +VkCommandBuffer blitz::UserInterface::beginSingleTimeCommands(VkCommandPool cmdPool) const { VkCommandBufferAllocateInfo allocInfo = {}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; @@ -95,7 +95,7 @@ VkCommandBuffer UserInterface::beginSingleTimeCommands(VkCommandPool cmdPool) co return commandBuffer; } -void UserInterface::createCommandBuffers() { +void blitz::UserInterface::createCommandBuffers() { commandBuffers.resize(context.imageCount); VkCommandBufferAllocateInfo commandBufferAllocateInfo = {}; @@ -109,7 +109,7 @@ void UserInterface::createCommandBuffers() { } } -void UserInterface::createCommandPool(VkCommandPoolCreateFlags flags) { +void blitz::UserInterface::createCommandPool(VkCommandPoolCreateFlags flags) { VkCommandPoolCreateInfo commandPoolCreateInfo = {}; commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; commandPoolCreateInfo.queueFamilyIndex = context.graphicsFamilyIndex; @@ -122,7 +122,7 @@ void UserInterface::createCommandPool(VkCommandPoolCreateFlags flags) { // Copied this code from DearImgui's setup: // https://github.com/ocornut/imgui/blob/master/examples/example_glfw_vulkan/main.cpp -void UserInterface::createDescriptorPool() { +void blitz::UserInterface::createDescriptorPool() { VkDescriptorPoolSize pool_sizes[] = { { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 }, { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 }, @@ -148,7 +148,7 @@ void UserInterface::createDescriptorPool() { } } -void UserInterface::createFramebuffers() { +void blitz::UserInterface::createFramebuffers() { // Create some UI framebuffers. These will be used in the render pass for the UI framebuffers.resize(context.imageCount); VkImageView attachment[1]; @@ -169,7 +169,7 @@ void UserInterface::createFramebuffers() { } } -void UserInterface::createRenderPass() { +void blitz::UserInterface::createRenderPass() { // Create an attachment description for the render pass VkAttachmentDescription attachmentDescription = {}; attachmentDescription.format = context.swapchain.getImageFormat(); @@ -218,7 +218,7 @@ void UserInterface::createRenderPass() { } } -void UserInterface::endSingleTimeCommands(VkCommandBuffer commandBuffer, VkCommandPool cmdPool) const { +void blitz::UserInterface::endSingleTimeCommands(VkCommandBuffer commandBuffer, VkCommandPool cmdPool) const { vkEndCommandBuffer(commandBuffer); VkSubmitInfo submitInfo = {}; @@ -232,7 +232,7 @@ void UserInterface::endSingleTimeCommands(VkCommandBuffer commandBuffer, VkComma vkFreeCommandBuffers(context.logicalDevice, cmdPool, 1, &commandBuffer); } -VkCommandBuffer UserInterface::recordCommands(uint32_t bufferIdx, VkExtent2D swapchainExtent) { +VkCommandBuffer blitz::UserInterface::recordCommands(uint32_t bufferIdx, VkExtent2D swapchainExtent) { VkCommandBufferBeginInfo cmdBufferBegin = {}; cmdBufferBegin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; cmdBufferBegin.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; @@ -266,7 +266,7 @@ VkCommandBuffer UserInterface::recordCommands(uint32_t bufferIdx, VkExtent2D swa return commandBuffers[bufferIdx]; } -void UserInterface::recreate(const UIContext &ctx) { +void blitz::UserInterface::recreate(const UIContext &ctx) { cleanupResources(); context = ctx; diff --git a/src/ui.h b/src/ui.h index 0405613..124018f 100644 --- a/src/ui.h +++ b/src/ui.h @@ -15,6 +15,7 @@ #include "camera.h" #include "swapchain.h" +namespace blitz { class UserInterface { public: struct UIContext { @@ -75,4 +76,5 @@ class UserInterface { std::vector commandBuffers; std::vector framebuffers; -}; \ No newline at end of file +}; +} // namespace blitz \ No newline at end of file diff --git a/src/vertex.h b/src/vertex.h index 4d0d72c..7a7db06 100644 --- a/src/vertex.h +++ b/src/vertex.h @@ -5,8 +5,8 @@ #include #include +namespace blitz { struct Vertex { - // Just have the position + color for now. Will expand to more attributes later glm::vec3 position; glm::vec3 normal; glm::vec2 texCoord; @@ -41,4 +41,5 @@ struct Vertex { return descriptions; } -}; \ No newline at end of file +}; +} // namespace blitz \ No newline at end of file