Changeset c205c3a in opengl-game


Ignore:
Timestamp:
Nov 15, 2020, 3:18:33 PM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl, master
Children:
78c3045
Parents:
57d43d0
Message:

In VulkanSFMLReference, use the Vulkan SDK version of vulkan.h instead of the one from the SFML repo, switch to the newer debugUtilsMessengerEXT for debugging, and add resources the example code needs for rendering

Files:
5 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • Vulkan.cpp

    r57d43d0 rc205c3a  
    1 
    2 ////////////////////////////////////////////////////////////
    3 // Headers
    4 ////////////////////////////////////////////////////////////
    5 #define GLAD_VULKAN_IMPLEMENTATION
    6 #include "vulkan.h"
    7 
    8 // Include graphics because we use sf::Image for loading images
    9 #include <SFML/Graphics.hpp>
    10 
    11 #include <SFML/Window.hpp>
     1#include <vulkan/vulkan.h>
     2
     3#include <iostream>
    124#include <vector>
    135#include <limits>
     
    157#include <cmath>
    168
    17 
    18 ////////////////////////////////////////////////////////////
    19 // Helper functions
    20 ////////////////////////////////////////////////////////////
    21 namespace
    22 {
     9#include <SFML/Window.hpp>
     10#include <SFML/Graphics.hpp> // Include graphics because we use sf::Image for loading images
     11
     12#include "vulkan-utils-new.hpp"
     13
     14using namespace std;
     15
     16namespace {
     17
     18   #ifdef NDEBUG
     19      const bool ENABLE_VALIDATION_LAYERS = false;
     20   #else
     21      const bool ENABLE_VALIDATION_LAYERS = true;
     22   #endif
     23
    2324   typedef float Vec3[3];
    2425   typedef float Matrix[4][4];
    2526
    2627   // Multiply 2 matrices
    27    void matrixMultiply(Matrix& result, const Matrix& left, const Matrix& right)
    28    {
     28   void matrixMultiply(Matrix& result, const Matrix& left, const Matrix& right) {
    2929      Matrix temp;
    3030
     
    3939
    4040   // Rotate a matrix around the x-axis
    41    void matrixRotateX(Matrix& result, float angle)
    42    {
     41   void matrixRotateX(Matrix& result, float angle) {
    4342      Matrix matrix = {
    4443          {1.f,   0.f,             0.f,             0.f},
     
    5251
    5352   // Rotate a matrix around the y-axis
    54    void matrixRotateY(Matrix& result, float angle)
    55    {
     53   void matrixRotateY(Matrix& result, float angle) {
    5654      Matrix matrix = {
    5755          { std::cos(angle), 0.f, std::sin(angle), 0.f},
     
    6563
    6664   // Rotate a matrix around the z-axis
    67    void matrixRotateZ(Matrix& result, float angle)
    68    {
     65   void matrixRotateZ(Matrix& result, float angle) {
    6966      Matrix matrix = {
    7067          { std::cos(angle), std::sin(angle), 0.f, 0.f},
     
    7875
    7976   // Construct a lookat view matrix
    80    void matrixLookAt(Matrix& result, const Vec3& eye, const Vec3& center, const Vec3& up)
    81    {
     77   void matrixLookAt(Matrix& result, const Vec3& eye, const Vec3& center, const Vec3& up) {
    8278      // Forward-looking vector
    8379      Vec3 forward = {
     
    9086      float factor = 1.0f / std::sqrt(forward[0] * forward[0] + forward[1] * forward[1] + forward[2] * forward[2]);
    9187
    92       for (int i = 0; i < 3; i++)
     88      for (int i = 0; i < 3; i++) {
    9389         forward[i] = forward[i] * factor;
     90      }
    9491
    9592      // Side vector (Forward cross product Up)
     
    128125
    129126   // Construct a perspective projection matrix
    130    void matrixPerspective(Matrix& result, float fov, float aspect, float nearPlane, float farPlane)
    131    {
     127   void matrixPerspective(Matrix& result, float fov, float aspect, float nearPlane, float farPlane) {
    132128      const float a = 1.f / std::tan(fov / 2.f);
    133129
     
    155151   // Clamp a value between low and high values
    156152   template<typename T>
    157    T clamp(T value, T low, T high)
    158    {
     153   T clamp(T value, T low, T high) {
    159154      return (value <= low) ? low : ((value >= high) ? high : value);
    160155   }
    161156
    162    // Helper function we pass to GLAD to load Vulkan functions via SFML
    163    GLADapiproc getVulkanFunction(const char* name)
    164    {
    165       return reinterpret_cast<GLADapiproc>(sf::Vulkan::getFunction(name));
    166    }
    167 
    168    // Debug we pass to Vulkan to call when it detects warnings or errors
    169    VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugReportFlagsEXT, VkDebugReportObjectTypeEXT, uint64_t, size_t, int32_t, const char*, const char* pMessage, void*)
    170    {
    171       sf::err() << pMessage << std::endl;
     157   VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
     158         VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
     159         VkDebugUtilsMessageTypeFlagsEXT messageType,
     160         const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
     161         void* pUserData) {
     162      cerr << "validation layer: " << pCallbackData->pMessage << endl;
    172163
    173164      return VK_FALSE;
     
    176167
    177168
    178 ////////////////////////////////////////////////////////////
    179 // VulkanExample class
    180 ////////////////////////////////////////////////////////////
    181 class VulkanExample
    182 {
     169class VulkanExample {
    183170public:
    184171   // Constructor
     
    190177      swapchainOutOfDate(false),
    191178      instance(0),
    192       debugReportCallback(0),
    193179      surface(0),
    194180      gpu(0),
     
    220206      descriptorPool(0)
    221207   {
     208      const vector<const char*> validationLayers = {
     209         "VK_LAYER_KHRONOS_validation",
     210         "VK_LAYER_LUNARG_monitor"                    // This should show the FPS in the title bar
     211      };
     212
    222213      // Vulkan setup procedure
    223       if (vulkanAvailable) setupInstance();
    224       if (vulkanAvailable) setupDebugReportCallback();
     214      if (vulkanAvailable) setupInstance(validationLayers);
     215      if (vulkanAvailable) setupDebugMessenger();
    225216      if (vulkanAvailable) setupSurface();
    226217      if (vulkanAvailable) setupPhysicalDevice();
     
    257248
    258249   // Destructor
    259    ~VulkanExample()
    260    {
     250   ~VulkanExample() {
    261251      // Wait until there are no pending frames
    262       if (device)
     252      if (device) {
    263253         vkDeviceWaitIdle(device);
     254      }
    264255
    265256      // Teardown swapchain
     
    327318         vkDestroySurfaceKHR(instance, surface, 0);
    328319
    329       if (debugReportCallback)
    330          vkDestroyDebugReportCallbackEXT(instance, debugReportCallback, 0);
     320      if (ENABLE_VALIDATION_LAYERS) {
     321         VulkanUtilsNew::destroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
     322      }
    331323
    332324      if (instance)
     
    335327
    336328   // Cleanup swapchain
    337    void cleanupSwapchain()
    338    {
     329   void cleanupSwapchain() {
    339330      // Swapchain teardown procedure
    340331      for (std::size_t i = 0; i < fences.size(); i++)
     
    351342      swapchainFramebuffers.clear();
    352343
    353       if (graphicsPipeline)
     344      if (graphicsPipeline) {
    354345         vkDestroyPipeline(device, graphicsPipeline, 0);
     346      }
    355347
    356348      if (renderPass)
     
    374366      swapchainImageViews.clear();
    375367
    376       if (swapchain)
     368      if (swapchain) {
    377369         vkDestroySwapchainKHR(device, swapchain, 0);
     370      }
    378371   }
    379372
    380373   // Cleanup and recreate swapchain
    381    void recreateSwapchain()
    382    {
     374   void recreateSwapchain() {
     375      cout << "Recreating pipeline..." << endl;
     376
    383377      // Wait until there are no pending frames
    384378      vkDeviceWaitIdle(device);
     
    401395
    402396   // Setup Vulkan instance
    403    void setupInstance()
    404    {
    405       // Load bootstrap entry points
    406       gladLoadVulkan(0, getVulkanFunction);
    407 
    408       if (!vkCreateInstance)
    409       {
    410          vulkanAvailable = false;
    411          return;
    412       }
    413 
    414       // Retrieve the available instance layers
    415       uint32_t objectCount = 0;
    416 
    417       std::vector<VkLayerProperties> layers;
    418 
    419       if (vkEnumerateInstanceLayerProperties(&objectCount, 0) != VK_SUCCESS)
    420       {
    421          vulkanAvailable = false;
    422          return;
    423       }
    424 
    425       layers.resize(objectCount);
    426 
    427       if (vkEnumerateInstanceLayerProperties(&objectCount, &layers[0]) != VK_SUCCESS)
    428       {
    429          vulkanAvailable = false;
    430          return;
    431       }
    432 
    433       // Activate the layers we are interested in
    434       std::vector<const char*> validationLayers;
    435 
    436       for (std::size_t i = 0; i < layers.size(); i++)
    437       {
    438          // VK_LAYER_LUNARG_standard_validation, meta-layer for the following layers:
    439          // -- VK_LAYER_GOOGLE_threading
    440          // -- VK_LAYER_LUNARG_parameter_validation
    441          // -- VK_LAYER_LUNARG_device_limits
    442          // -- VK_LAYER_LUNARG_object_tracker
    443          // -- VK_LAYER_LUNARG_image
    444          // -- VK_LAYER_LUNARG_core_validation
    445          // -- VK_LAYER_LUNARG_swapchain
    446          // -- VK_LAYER_GOOGLE_unique_objects
    447          // These layers perform error checking and warn about bad or sub-optimal Vulkan API usage
    448          // VK_LAYER_LUNARG_monitor appends an FPS counter to the window title
    449          if (!std::strcmp(layers[i].layerName, "VK_LAYER_LUNARG_standard_validation"))
    450          {
    451             validationLayers.push_back("VK_LAYER_LUNARG_standard_validation");
    452          }
    453          else if (!std::strcmp(layers[i].layerName, "VK_LAYER_LUNARG_monitor"))
    454          {
    455             validationLayers.push_back("VK_LAYER_LUNARG_monitor");
    456          }
     397   void setupInstance(const vector<const char*>& validationLayers) {
     398      if (ENABLE_VALIDATION_LAYERS && !VulkanUtilsNew::checkValidationLayerSupport(validationLayers)) {
     399         throw runtime_error("validation layers requested, but not available!");
    457400      }
    458401
    459402      // Retrieve the extensions we need to enable in order to use Vulkan with SFML
    460403      std::vector<const char*> requiredExtentions = sf::Vulkan::getGraphicsRequiredInstanceExtensions();
    461       requiredExtentions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
     404      if (ENABLE_VALIDATION_LAYERS) {
     405         requiredExtentions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
     406      }
    462407
    463408      // Register our application information
     
    473418      instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    474419      instanceCreateInfo.pApplicationInfo = &applicationInfo;
    475       instanceCreateInfo.enabledLayerCount = validationLayers.size();
    476       instanceCreateInfo.ppEnabledLayerNames = &validationLayers[0];
     420
    477421      instanceCreateInfo.enabledExtensionCount = requiredExtentions.size();
    478       instanceCreateInfo.ppEnabledExtensionNames = &requiredExtentions[0];
    479 
    480       // Try to create a Vulkan instance with debug report enabled
    481       VkResult result = vkCreateInstance(&instanceCreateInfo, 0, &instance);
    482 
    483       // If an extension is missing, try disabling debug report
    484       if (result == VK_ERROR_EXTENSION_NOT_PRESENT)
    485       {
    486          requiredExtentions.pop_back();
    487 
    488          instanceCreateInfo.enabledExtensionCount = requiredExtentions.size();
    489          instanceCreateInfo.ppEnabledExtensionNames = &requiredExtentions[0];
    490 
    491          result = vkCreateInstance(&instanceCreateInfo, 0, &instance);
    492       }
    493 
    494       // If instance creation still fails, give up
    495       if (result != VK_SUCCESS)
    496       {
    497          vulkanAvailable = false;
    498          return;
    499       }
    500 
    501       // Load instance entry points
    502       gladLoadVulkan(0, getVulkanFunction);
     422      instanceCreateInfo.ppEnabledExtensionNames = requiredExtentions.data();
     423
     424      VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
     425      if (ENABLE_VALIDATION_LAYERS) {
     426         instanceCreateInfo.enabledLayerCount = validationLayers.size();
     427         instanceCreateInfo.ppEnabledLayerNames = validationLayers.data();
     428
     429         populateDebugMessengerCreateInfo(debugCreateInfo);
     430         instanceCreateInfo.pNext = &debugCreateInfo;
     431      } else {
     432         instanceCreateInfo.enabledLayerCount = 0;
     433
     434         instanceCreateInfo.pNext = nullptr;
     435      }
     436
     437      if (vkCreateInstance(&instanceCreateInfo, nullptr, &instance) != VK_SUCCESS) {
     438         throw runtime_error("failed to create instance!");
     439      }
     440   }
     441
     442   void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
     443      createInfo = {};
     444      createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
     445      // createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
     446      createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
     447      createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
     448      createInfo.pfnUserCallback = debugCallback;
    503449   }
    504450
    505451   // Setup our debug callback function to be called by Vulkan
    506    void setupDebugReportCallback()
    507    {
    508       // Don't try to register the callback if the extension is not available
    509       if (!vkCreateDebugReportCallbackEXT)
    510          return;
    511 
    512       // Register for warnings and errors
    513       VkDebugReportCallbackCreateInfoEXT debugReportCallbackCreateInfo = VkDebugReportCallbackCreateInfoEXT();
    514       debugReportCallbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
    515       debugReportCallbackCreateInfo.flags = VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | VK_DEBUG_REPORT_ERROR_BIT_EXT;
    516       debugReportCallbackCreateInfo.pfnCallback = debugCallback;
    517 
    518       // Create the debug callback
    519       if (vkCreateDebugReportCallbackEXT(instance, &debugReportCallbackCreateInfo, 0, &debugReportCallback) != VK_SUCCESS)
    520       {
    521          vulkanAvailable = false;
    522          return;
     452   void setupDebugMessenger() {
     453      if (!ENABLE_VALIDATION_LAYERS) return;
     454
     455      VkDebugUtilsMessengerCreateInfoEXT createInfo;
     456      populateDebugMessengerCreateInfo(createInfo);
     457
     458      if (VulkanUtilsNew::createDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
     459         throw runtime_error("failed to set up debug messenger!");
    523460      }
    524461   }
     
    613550         return;
    614551      }
    615 
    616       // Load physical device entry points
    617       gladLoadVulkan(gpu, getVulkanFunction);
    618552
    619553      // Check what depth formats are available and select one
     
    716650
    717651   // Query surface formats and set up swapchain
    718    void setupSwapchain()
    719    {
     652   void setupSwapchain() {
     653      cout << "STARTED CALL" << endl;
    720654      // Select a surface format that supports RGBA color format
    721655      uint32_t objectCount = 0;
     
    723657      std::vector<VkSurfaceFormatKHR> surfaceFormats;
    724658
    725       if (vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &objectCount, 0) != VK_SUCCESS)
    726       {
     659      if (vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &objectCount, 0) != VK_SUCCESS) {
    727660         vulkanAvailable = false;
    728661         return;
     
    731664      surfaceFormats.resize(objectCount);
    732665
    733       if (vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &objectCount, &surfaceFormats[0]) != VK_SUCCESS)
    734       {
    735          vulkanAvailable = false;
    736          return;
    737       }
    738 
    739       if ((surfaceFormats.size() == 1) && (surfaceFormats[0].format == VK_FORMAT_UNDEFINED))
    740       {
     666      if (vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &objectCount, &surfaceFormats[0]) != VK_SUCCESS) {
     667         vulkanAvailable = false;
     668         return;
     669      }
     670
     671      if ((surfaceFormats.size() == 1) && (surfaceFormats[0].format == VK_FORMAT_UNDEFINED)) {
    741672         swapchainFormat.format = VK_FORMAT_B8G8R8A8_UNORM;
    742673         swapchainFormat.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
    743       }
    744       else if (!surfaceFormats.empty())
    745       {
    746          for (std::size_t i = 0; i < surfaceFormats.size(); i++)
    747          {
    748             if ((surfaceFormats[i].format == VK_FORMAT_B8G8R8A8_UNORM) && (surfaceFormats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR))
    749             {
     674      } else if (!surfaceFormats.empty()) {
     675         for (std::size_t i = 0; i < surfaceFormats.size(); i++) {
     676            if ((surfaceFormats[i].format == VK_FORMAT_B8G8R8A8_UNORM) && (surfaceFormats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)) {
    750677               swapchainFormat.format = VK_FORMAT_B8G8R8A8_UNORM;
    751678               swapchainFormat.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
     
    755682         }
    756683
    757          if (swapchainFormat.format == VK_FORMAT_UNDEFINED)
     684         if (swapchainFormat.format == VK_FORMAT_UNDEFINED) {
    758685            swapchainFormat = surfaceFormats[0];
    759       }
    760       else
    761       {
     686         }
     687      } else {
    762688         vulkanAvailable = false;
    763689         return;
     
    767693      std::vector<VkPresentModeKHR> presentModes;
    768694
    769       if (vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, &objectCount, 0) != VK_SUCCESS)
    770       {
     695      if (vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, &objectCount, 0) != VK_SUCCESS) {
    771696         vulkanAvailable = false;
    772697         return;
     
    775700      presentModes.resize(objectCount);
    776701
    777       if (vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, &objectCount, &presentModes[0]) != VK_SUCCESS)
    778       {
     702      if (vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, &objectCount, &presentModes[0]) != VK_SUCCESS) {
    779703         vulkanAvailable = false;
    780704         return;
     
    784708      VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
    785709
    786       for (std::size_t i = 0; i < presentModes.size(); i++)
    787       {
    788          if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR)
    789          {
     710      for (std::size_t i = 0; i < presentModes.size(); i++) {
     711         if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
    790712            presentMode = presentModes[i];
    791713            break;
     
    796718      VkSurfaceCapabilitiesKHR surfaceCapabilities;
    797719
    798       if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, surface, &surfaceCapabilities) != VK_SUCCESS)
    799       {
     720      if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, surface, &surfaceCapabilities) != VK_SUCCESS) {
    800721         vulkanAvailable = false;
    801722         return;
     
    824745
    825746      // Create the swapchain
    826       if (vkCreateSwapchainKHR(device, &swapchainCreateInfo, 0, &swapchain) != VK_SUCCESS)
    827       {
     747      if (vkCreateSwapchainKHR(device, &swapchainCreateInfo, 0, &swapchain) != VK_SUCCESS) {
    828748         vulkanAvailable = false;
    829749         return;
     
    1057977
    1058978   // Set up pipeline layout
    1059    void setupPipelineLayout()
    1060    {
     979   void setupPipelineLayout() {
    1061980      VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = VkPipelineLayoutCreateInfo();
    1062981      pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
     
    1065984
    1066985      // Create pipeline layout
    1067       if (vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, 0, &pipelineLayout) != VK_SUCCESS)
    1068       {
     986      if (vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, 0, &pipelineLayout) != VK_SUCCESS) {
    1069987         vulkanAvailable = false;
    1070988         return;
     
    1073991
    1074992   // Set up rendering pipeline
    1075    void setupPipeline()
    1076    {
     993   void setupPipeline() {
    1077994      // Set up how the vertex shader pulls data out of our vertex buffer
    1078995      VkVertexInputBindingDescription vertexInputBindingDescription = VkVertexInputBindingDescription();
     
    11991116
    12001117      // Create our graphics pipeline
    1201       if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, 0, &graphicsPipeline) != VK_SUCCESS)
    1202       {
    1203          vulkanAvailable = false;
    1204          return;
     1118      if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphicsPipelineCreateInfo, 0, &graphicsPipeline) != VK_SUCCESS) {
     1119         vulkanAvailable = false;
    12051120      }
    12061121   }
    12071122
    12081123   // Use our renderpass and swapchain images to create the corresponding framebuffers
    1209    void setupFramebuffers()
    1210    {
     1124   void setupFramebuffers() {
    12111125      swapchainFramebuffers.resize(swapchainImageViews.size());
    12121126
     
    12191133      framebufferCreateInfo.layers = 1;
    12201134
    1221       for (std::size_t i = 0; i < swapchainFramebuffers.size(); i++)
    1222       {
     1135      for (std::size_t i = 0; i < swapchainFramebuffers.size(); i++) {
    12231136         // Each framebuffer consists of a corresponding swapchain image and the shared depth image
    12241137         VkImageView attachments[] = { swapchainImageViews[i], depthImageView };
     
    12271140
    12281141         // Create the framebuffer
    1229          if (vkCreateFramebuffer(device, &framebufferCreateInfo, 0, &swapchainFramebuffers[i]) != VK_SUCCESS)
    1230          {
     1142         if (vkCreateFramebuffer(device, &framebufferCreateInfo, 0, &swapchainFramebuffers[i]) != VK_SUCCESS) {
    12311143            vulkanAvailable = false;
    12321144            return;
     
    12361148
    12371149   // Set up our command pool
    1238    void setupCommandPool()
    1239    {
     1150   void setupCommandPool() {
    12401151      // We want to be able to reset command buffers after submitting them
    12411152      VkCommandPoolCreateInfo commandPoolCreateInfo = VkCommandPoolCreateInfo();
     
    12451156
    12461157      // Create our command pool
    1247       if (vkCreateCommandPool(device, &commandPoolCreateInfo, 0, &commandPool) != VK_SUCCESS)
    1248       {
     1158      if (vkCreateCommandPool(device, &commandPoolCreateInfo, 0, &commandPool) != VK_SUCCESS) {
    12491159         vulkanAvailable = false;
    12501160         return;
     
    12531163
    12541164   // Helper to create a generic buffer with the specified size, usage and memory flags
    1255    bool createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& memory)
    1256    {
     1165   bool createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& memory) {
    12571166      // We only have a single queue so we can request exclusive access
    12581167      VkBufferCreateInfo bufferCreateInfo = VkBufferCreateInfo();
     
    12631172
    12641173      // Create the buffer, this does not allocate any memory for it yet
    1265       if (vkCreateBuffer(device, &bufferCreateInfo, 0, &buffer) != VK_SUCCESS)
     1174      if (vkCreateBuffer(device, &bufferCreateInfo, 0, &buffer) != VK_SUCCESS) {
    12661175         return false;
     1176      }
    12671177
    12681178      // Check what kind of memory we need to request from the GPU
     
    12761186      uint32_t memoryType = 0;
    12771187
    1278       for (; memoryType < memoryProperties.memoryTypeCount; memoryType++)
    1279       {
     1188      for (; memoryType < memoryProperties.memoryTypeCount; memoryType++) {
    12801189         if ((memoryRequirements.memoryTypeBits & (1 << memoryType)) &&
    1281             ((memoryProperties.memoryTypes[memoryType].propertyFlags & properties) == properties))
     1190               ((memoryProperties.memoryTypes[memoryType].propertyFlags & properties) == properties)) {
    12821191            break;
    1283       }
    1284 
    1285       if (memoryType == memoryProperties.memoryTypeCount)
     1192         }
     1193      }
     1194
     1195      if (memoryType == memoryProperties.memoryTypeCount) {
    12861196         return false;
     1197      }
    12871198
    12881199      VkMemoryAllocateInfo memoryAllocateInfo = VkMemoryAllocateInfo();
     
    12921203
    12931204      // Allocate the memory out of the GPU pool for the required memory type
    1294       if (vkAllocateMemory(device, &memoryAllocateInfo, 0, &memory) != VK_SUCCESS)
     1205      if (vkAllocateMemory(device, &memoryAllocateInfo, 0, &memory) != VK_SUCCESS) {
    12951206         return false;
     1207      }
    12961208
    12971209      // Bind the allocated memory to our buffer object
    1298       if (vkBindBufferMemory(device, buffer, memory, 0) != VK_SUCCESS)
     1210      if (vkBindBufferMemory(device, buffer, memory, 0) != VK_SUCCESS) {
    12991211         return false;
     1212      }
    13001213
    13011214      return true;
     
    13031216
    13041217   // Helper to copy the contents of one buffer to another buffer
    1305    bool copyBuffer(VkBuffer dst, VkBuffer src, VkDeviceSize size)
    1306    {
     1218   bool copyBuffer(VkBuffer dst, VkBuffer src, VkDeviceSize size) {
    13071219      // Allocate a primary command buffer out of our command pool
    13081220      VkCommandBufferAllocateInfo commandBufferAllocateInfo = VkCommandBufferAllocateInfo();
     
    13141226      VkCommandBuffer commandBuffer;
    13151227
    1316       if (vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, &commandBuffer) != VK_SUCCESS)
     1228      if (vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, &commandBuffer) != VK_SUCCESS) {
    13171229         return false;
     1230      }
    13181231
    13191232      // Begin the command buffer
     
    13221235      commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
    13231236
    1324       if (vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo) != VK_SUCCESS)
    1325       {
     1237      if (vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo) != VK_SUCCESS) {
    13261238         vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
    13271239
     
    13451257      submitInfo.pCommandBuffers = &commandBuffer;
    13461258
    1347       if (vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS)
    1348       {
     1259      if (vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS) {
    13491260         vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
    13501261
     
    13531264
    13541265      // Ensure the command buffer has been processed
    1355       if (vkQueueWaitIdle(queue) != VK_SUCCESS)
    1356       {
     1266      if (vkQueueWaitIdle(queue) != VK_SUCCESS) {
    13571267         vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
    13581268
     
    13671277
    13681278   // Create our vertex buffer and upload its data
    1369    void setupVertexBuffer()
    1370    {
     1279   void setupVertexBuffer() {
    13711280      float vertexData[] = {
    13721281         // X      Y      Z     R     G     B     A     U     V
     
    23762285   }
    23772286
    2378    void draw()
    2379    {
     2287   void draw() {
    23802288      uint32_t imageIndex = 0;
    23812289
     
    23832291      vkWaitForFences(device, 1, &fences[currentFrame], VK_TRUE, std::numeric_limits<uint64_t>::max());
    23842292
    2385       {
    2386          // Get the next image in the swapchain
    2387          VkResult result = vkAcquireNextImageKHR(device, swapchain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
    2388 
    2389          // Check if we need to re-create the swapchain (e.g. if the window was resized)
    2390          if (result == VK_ERROR_OUT_OF_DATE_KHR)
    2391          {
    2392             recreateSwapchain();
    2393             swapchainOutOfDate = false;
    2394             return;
    2395          }
    2396 
    2397          if ((result != VK_SUCCESS) && (result != VK_TIMEOUT) && (result != VK_NOT_READY) && (result != VK_SUBOPTIMAL_KHR))
    2398          {
    2399             vulkanAvailable = false;
    2400             return;
    2401          }
     2293      // Get the next image in the swapchain
     2294      VkResult result = vkAcquireNextImageKHR(device, swapchain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
     2295
     2296      // Check if we need to re-create the swapchain (e.g. if the window was resized)
     2297      if (result == VK_ERROR_OUT_OF_DATE_KHR) {
     2298         recreateSwapchain();
     2299         swapchainOutOfDate = false;
     2300         return;
     2301      }
     2302
     2303      // && result != VK_TIMEOUT && result != VK_NOT_READY
     2304      if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
     2305         throw runtime_error("failed to acquire swap chain image!");
     2306         vulkanAvailable = false;
     2307         return;
    24022308      }
    24032309
     
    24182324      vkResetFences(device, 1, &fences[currentFrame]);
    24192325
    2420       if (vkQueueSubmit(queue, 1, &submitInfo, fences[currentFrame]) != VK_SUCCESS)
    2421       {
     2326      if (vkQueueSubmit(queue, 1, &submitInfo, fences[currentFrame]) != VK_SUCCESS) {
    24222327         vulkanAvailable = false;
    24232328         return;
     
    24332338      presentInfo.pImageIndices = &imageIndex;
    24342339
    2435       {
    2436          // Queue presentation
    2437          VkResult result = vkQueuePresentKHR(queue, &presentInfo);
    2438 
    2439          // Check if we need to re-create the swapchain (e.g. if the window was resized)
    2440          if ((result == VK_ERROR_OUT_OF_DATE_KHR) || (result == VK_SUBOPTIMAL_KHR) || swapchainOutOfDate)
    2441          {
    2442             recreateSwapchain();
    2443             swapchainOutOfDate = false;
    2444          }
    2445          else if (result != VK_SUCCESS)
    2446          {
    2447             vulkanAvailable = false;
    2448             return;
    2449          }
     2340      // Queue presentation
     2341      result = vkQueuePresentKHR(queue, &presentInfo);
     2342
     2343      // Check if we need to re-create the swapchain (e.g. if the window was resized)
     2344      if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || swapchainOutOfDate) {
     2345         recreateSwapchain();
     2346         swapchainOutOfDate = false;
     2347      } else if (result != VK_SUCCESS) {
     2348         throw runtime_error("failed to present swap chain image!");
     2349         vulkanAvailable = false;
     2350         return;
    24502351      }
    24512352
     
    24632364         // Process events
    24642365         sf::Event event;
    2465          while (window.pollEvent(event))
    2466          {
     2366         while (window.pollEvent(event)) {
    24672367            // Close window: exit
    24682368            if (event.type == sf::Event::Closed)
     
    24782378         }
    24792379
    2480          if (vulkanAvailable)
    2481          {
     2380         if (vulkanAvailable) {
    24822381            // Update the uniform buffer (matrices)
    24832382            updateUniformBuffer(clock.getElapsedTime().asSeconds());
     
    24992398
    25002399   VkInstance instance;
    2501    VkDebugReportCallbackEXT debugReportCallback;
     2400   VkDebugUtilsMessengerEXT debugMessenger;
    25022401   VkSurfaceKHR surface;
    25032402   VkPhysicalDevice gpu;
  • VulkanSFMLReference.vcxproj

    r57d43d0 rc205c3a  
    2020  </ItemGroup>
    2121  <ItemGroup>
    22     <ClInclude Include="vulkan.h" />
     22    <ClCompile Include="vulkan-utils-new.cpp" />
     23    <ClCompile Include="Vulkan.cpp" />
    2324  </ItemGroup>
    2425  <ItemGroup>
    25     <ClCompile Include="Vulkan.cpp" />
     26    <ClInclude Include="vulkan-utils-new.hpp" />
    2627  </ItemGroup>
    2728  <PropertyGroup Label="Globals">
     
    9394    <OutDir>$(SolutionDir)$(ProjectName).build\$(Platform)\$(Configuration)\</OutDir>
    9495    <IntDir>$(ProjectName).build\$(Platform)\$(Configuration)\</IntDir>
    95     <IncludePath>../include;$(IncludePath)</IncludePath>
    96     <LibraryPath>../lib;$(LibraryPath)</LibraryPath>
     96    <IncludePath>../include;$(VULKAN_SDK)/Include;$(VULKAN_SDK)/Third-Party/Include;$(IncludePath)</IncludePath>
     97    <LibraryPath>../lib;$(VULKAN_SDK)/Lib;$(LibraryPath)</LibraryPath>
    9798  </PropertyGroup>
    9899  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
     
    100101    <OutDir>$(SolutionDir)$(ProjectName).build\$(Platform)\$(Configuration)\</OutDir>
    101102    <IntDir>$(ProjectName).build\$(Platform)\$(Configuration)\</IntDir>
    102     <IncludePath>../include;$(IncludePath)</IncludePath>
    103     <LibraryPath>../lib;$(LibraryPath)</LibraryPath>
     103    <IncludePath>../include;$(VULKAN_SDK)/Include;$(VULKAN_SDK)/Third-Party/Include;$(IncludePath)</IncludePath>
     104    <LibraryPath>../lib;$(VULKAN_SDK)/Lib;$(LibraryPath)</LibraryPath>
    104105  </PropertyGroup>
    105106  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     
    141142      <SubSystem>Console</SubSystem>
    142143      <GenerateDebugInformation>true</GenerateDebugInformation>
    143       <AdditionalDependencies>sfml-graphics-s-d.lib;sfml-window-s-d.lib;sfml-system-s-d.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
     144      <AdditionalDependencies>sfml-graphics-s-d.lib;sfml-window-s-d.lib;sfml-system-s-d.lib;vulkan-1.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
    144145    </Link>
    145146  </ItemDefinitionGroup>
     
    158159      <OptimizeReferences>true</OptimizeReferences>
    159160      <GenerateDebugInformation>true</GenerateDebugInformation>
    160       <AdditionalDependencies>sfml-graphics-s.lib;sfml-window-s.lib;sfml-system-s.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
     161      <AdditionalDependencies>sfml-graphics-s.lib;sfml-window-s.lib;sfml-system-s.lib;vulkan-1.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
    161162    </Link>
    162163  </ItemDefinitionGroup>
  • vulkan-utils.cpp

    r57d43d0 rc205c3a  
    3939      const VkAllocationCallbacks* pAllocator,
    4040      VkDebugUtilsMessengerEXT* pDebugMessenger) {
    41    auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
     41   PFN_vkCreateDebugUtilsMessengerEXT func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance,
     42      "vkCreateDebugUtilsMessengerEXT");
    4243
    4344   if (func != nullptr) {
     
    5152      VkDebugUtilsMessengerEXT debugMessenger,
    5253      const VkAllocationCallbacks* pAllocator) {
    53    auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
     54   PFN_vkDestroyDebugUtilsMessengerEXT func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance,
     55      "vkDestroyDebugUtilsMessengerEXT");
    5456
    5557   if (func != nullptr) {
Note: See TracChangeset for help on using the changeset viewer.