Changeset c324d6a in opengl-game


Ignore:
Timestamp:
Jan 2, 2021, 4:07:45 PM (3 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl, master
Children:
3b7d497, ca188cc
Parents:
a2f62d7
Message:

Make some minor updates to VulkanGame

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • game-gui-sdl.cpp

    ra2f62d7 rc324d6a  
    33#include <map>
    44#include <queue>
     5
     6#include <SDL2/SDL_ttf.h>
    57
    68#include "compiler.hpp"
     
    911using namespace std;
    1012
    11 string GameGui_SDL::s_errorMessage;
    12 
    1313GameGui_SDL::GameGui_SDL() : keyState(SDL_GetKeyboardState(NULL)) {
     14   window = nullptr;
    1415}
    1516
    1617string& GameGui_SDL::getError() {
    17    GameGui_SDL::s_errorMessage = SDL_GetError();
     18   s_errorMessage = SDL_GetError();
    1819
    19    return GameGui_SDL::s_errorMessage;
     20   return s_errorMessage;
    2021}
    2122
     
    2425   // prevents SDL from setting up its own handlers for SIG_SEGV and stuff like that
    2526
    26    GameGui_SDL::s_errorMessage = "No error";
     27   s_errorMessage = "No error";
    2728
    2829   if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
     
    5657
    5758#ifdef WINDOWS
    58     uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE);
     59   uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI |
     60      (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE);
    5961#else
    60     uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
     62   uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI |
     63      (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
    6164#endif
    6265
    63    window = SDL_CreateWindow(title.c_str(),
    64                SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
    65                width, height, flags);
     66   window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
    6667
    6768   refreshWindowSize();
     
    148149
    149150void GameGui_SDL::refreshWindowSize() {
     151   // TODO: Make sure this works on a mac (the analogous glfw function had issues on Mac retina displays)
    150152   SDL_GetWindowSize(window, &windowWidth, &windowHeight);
    151153}
  • game-gui-sdl.hpp

    ra2f62d7 rc324d6a  
    55
    66#include <SDL2/SDL.h>
    7 #include <SDL2/SDL_ttf.h>
    87
    98#ifdef GAMEGUI_INCLUDE_VULKAN
     
    4140      const Uint8* keyState;
    4241
    43       static string s_errorMessage;
     42      string s_errorMessage;
    4443};
    4544
  • new-game.cpp

    ra2f62d7 rc324d6a  
    88#include <glm/gtc/type_ptr.hpp>
    99
     10#include <GL/glew.h>
     11#include <GLFW/glfw3.h>
     12
    1013#include "IMGUI/imgui.h"
    1114#include "IMGUI/imgui_impl_glfw.h"
    1215#include "IMGUI/imgui_impl_opengl3.h"
    13 
    14 #include <GL/glew.h>
    15 #include <GLFW/glfw3.h>
    1616
    1717#include <cstdio>
  • vulkan-game.cpp

    ra2f62d7 rc324d6a  
    55#include <numeric>
    66#include <set>
    7 
    8 #include "consts.hpp"
     7#include <stdexcept>
     8
    99#include "logger.hpp"
    1010
     
    1616using namespace std;
    1717
    18 // TODO: Update all occurances of instance variables to use this->
     18// TODO: Update all occurances of instance variables to use this-> (Actually, not sure if I really want to do this)
     19
     20/* TODO: Try doing the following tasks based on the Vulkan implementation of IMGUI (Also maybe looks at Sascha Willems' code to see how he does these things)
     21 *
     22 * - When recreating the swapchain, pass the old one in and destroy the old one after the new one is created
     23 * - Recreate semaphores when recreating the swapchain
     24 *     - imgui uses one image acquired and one render complete sem  and once fence per frame\
     25 * - IMGUI creates one command pool per framebuffer
     26 */
    1927
    2028VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) {
    2129   this->gui = nullptr;
    2230   this->window = nullptr;
     31
     32   this->debugMessenger = VK_NULL_HANDLE;
    2333
    2434   this->currentFrame = 0;
     
    8595
    8696   // TODO: Refactor the logger api to be more flexible,
    87    // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
     97   // esp. since gl_log() and gl_log_err() have issues printing anything besides strings
    8898   restart_gl_log();
    8999   gl_log("starting SDL\n%s.%s.%s",
     
    93103
    94104   // TODO: Use open_Log() and related functions instead of gl_log ones
     105   // TODO: In addition, delete the gl_log functions
    95106   open_log();
    96107   get_log() << "starting SDL" << endl;
     
    807818   }
    808819
     820   // TODO: Should probably check the returned result
    809821   vkDeviceWaitIdle(device);
    810822}
     
    11311143}
    11321144
    1133 void VulkanGame::createVulkanInstance(const vector<const char*> &validationLayers) {
     1145void VulkanGame::createVulkanInstance(const vector<const char*>& validationLayers) {
    11341146   if (ENABLE_VALIDATION_LAYERS && !VulkanUtils::checkValidationLayerSupport(validationLayers)) {
    11351147      throw runtime_error("validation layers requested, but not available!");
     
    11811193
    11821194void VulkanGame::setupDebugMessenger() {
    1183    if (!ENABLE_VALIDATION_LAYERS) return;
     1195   if (!ENABLE_VALIDATION_LAYERS) {
     1196      return;
     1197   }
    11841198
    11851199   VkDebugUtilsMessengerCreateInfoEXT createInfo;
     
    12171231void VulkanGame::pickPhysicalDevice(const vector<const char*>& deviceExtensions) {
    12181232   uint32_t deviceCount = 0;
     1233   // TODO: Check VkResult
    12191234   vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
    12201235
     
    12241239
    12251240   vector<VkPhysicalDevice> devices(deviceCount);
     1241   // TODO: Check VkResult
    12261242   vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
    12271243
     
    12621278}
    12631279
    1264 void VulkanGame::createLogicalDevice(
    1265       const vector<const char*> validationLayers, const vector<const char*>& deviceExtensions) {
     1280void VulkanGame::createLogicalDevice(const vector<const char*>& validationLayers,
     1281      const vector<const char*>& deviceExtensions) {
    12661282   QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface);
    12671283
     
    12851301   VkDeviceCreateInfo createInfo = {};
    12861302   createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
     1303
    12871304   createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfoList.size());
    12881305   createInfo.pQueueCreateInfos = queueCreateInfoList.data();
     
    13171334   VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight());
    13181335
    1319    uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
    1320    if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
    1321       imageCount = swapChainSupport.capabilities.maxImageCount;
     1336   swapChainImageCount = swapChainSupport.capabilities.minImageCount + 1;
     1337   if (swapChainSupport.capabilities.maxImageCount > 0 && swapChainImageCount > swapChainSupport.capabilities.maxImageCount) {
     1338      swapChainImageCount = swapChainSupport.capabilities.maxImageCount;
    13221339   }
    13231340
     
    13251342   createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    13261343   createInfo.surface = surface;
    1327    createInfo.minImageCount = imageCount;
     1344   createInfo.minImageCount = swapChainImageCount;
    13281345   createInfo.imageFormat = surfaceFormat.format;
    13291346   createInfo.imageColorSpace = surfaceFormat.colorSpace;
     
    13551372   }
    13561373
    1357    vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
    1358    swapChainImages.resize(imageCount);
    1359    vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
     1374   vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, nullptr);
     1375   swapChainImages.resize(swapChainImageCount);
     1376   vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, swapChainImages.data());
    13601377
    13611378   swapChainImageFormat = surfaceFormat.format;
  • vulkan-game.hpp

    ra2f62d7 rc324d6a  
    1515#include <vulkan/vulkan.h>
    1616
     17#include <SDL2/SDL.h>
    1718#include <SDL2/SDL_ttf.h>
    1819
     
    227228
    228229   private:
     230      static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
     231         VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
     232         VkDebugUtilsMessageTypeFlagsEXT messageType,
     233         const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
     234         void* pUserData);
     235
    229236      // TODO: Make these consts static
    230237      // Also, maybe move them into consts.hpp
     
    261268      VkQueue presentQueue;
    262269
     270      uint32_t swapChainImageCount;
    263271      VkSwapchainKHR swapChain;
    264272      vector<VkImage> swapChainImages;
     
    374382      void cleanup();
    375383
    376       void createVulkanInstance(const vector<const char*> &validationLayers);
     384      void createVulkanInstance(const vector<const char*>& validationLayers);
    377385      void setupDebugMessenger();
    378386      void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
     
    380388      void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
    381389      bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
    382       void createLogicalDevice(
    383          const vector<const char*> validationLayers,
     390      void createLogicalDevice(const vector<const char*>& validationLayers,
    384391         const vector<const char*>& deviceExtensions);
    385392      void createSwapChain();
     
    436443
    437444      void cleanupSwapChain();
    438 
    439       static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
    440             VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
    441             VkDebugUtilsMessageTypeFlagsEXT messageType,
    442             const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
    443             void* pUserData);
    444445};
    445446
  • vulkan-utils.cpp

    ra2f62d7 rc324d6a  
    145145   VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
    146146
     147   /* This functions effectively selects present modes in this order, which allows for unlimited framerate:
     148    * { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR }
     149    *
     150    * To cap the framerate (I assume to the monitor refresh rate), just use:
     151    * { VK_PRESENT_MODE_FIFO_KHR }
     152    *
     153    * Would be better to make a more generic function that takes a list of prefered modes ordered by preference.
     154    * Example code:
     155    *
     156    * for (int request_i = 0; request_i < request_modes_count; request_i++)
     157    *    for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
     158    *       if (request_modes[request_i] == avail_modes[avail_i])
     159    *          return request_modes[request_i];
     160    */
     161
    147162   for (const auto& availablePresentMode : availablePresentModes) {
    148163      if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
    149164         return availablePresentMode;
    150       }
    151       else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
     165      } else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
    152166         bestMode = availablePresentMode;
    153167      }
Note: See TracChangeset for help on using the changeset viewer.