Skip to content

Commit

Permalink
Fix: Crash when vulkan not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Jun 24, 2024
1 parent d881747 commit c8de137
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/atta/graphics/apis/vulkan/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace atta::graphics::vk {

Instance::Instance() {
Instance::Instance() : _instance(VK_NULL_HANDLE) {
// Application info
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Expand Down Expand Up @@ -46,7 +46,8 @@ Instance::Instance() {

// Create instance
VkResult result = vkCreateInstance(&createInfo, nullptr, &_instance);
if (result != VK_SUCCESS)
_wasCreated = result == VK_SUCCESS;
if (!_wasCreated)
LOG_ERROR("gfx::vk::Instance", "Failed to create vulkan instance! Code: $0", common::toString(result));
}

Expand All @@ -55,6 +56,8 @@ Instance::~Instance() {
vkDestroyInstance(_instance, nullptr);
}

bool Instance::wasCreated() const { return _wasCreated; }

VkInstance Instance::getHandle() const { return _instance; }

void Instance::printAvailableExtensions() {
Expand Down
3 changes: 3 additions & 0 deletions src/atta/graphics/apis/vulkan/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Instance {
Instance();
~Instance();

bool wasCreated() const;

VkInstance getHandle() const;

private:
Expand All @@ -29,6 +31,7 @@ class Instance {
std::vector<const char*> getEnabledLayers();

VkInstance _instance;
bool _wasCreated;
VkDebugUtilsMessengerEXT debugMessenger;
};

Expand Down
5 changes: 5 additions & 0 deletions src/atta/graphics/apis/vulkan/vulkanAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ void VulkanAPI::generateProcessedTexture(GenerateProcessedTextureInfo gptInfo) {

void* VulkanAPI::getImGuiImage(StringId sid) const { return nullptr; }

bool VulkanAPI::isSupported() {
vk::Instance instance;
return instance.wasCreated();
}

std::shared_ptr<vk::Instance> VulkanAPI::getInstance() const { return _instance; }

std::shared_ptr<vk::PhysicalDevice> VulkanAPI::getPhysicalDevice() const { return _physicalDevice; }
Expand Down
2 changes: 2 additions & 0 deletions src/atta/graphics/apis/vulkan/vulkanAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class VulkanAPI final : public GraphicsAPI {

void* getImGuiImage(StringId sid) const override;

static bool isSupported();

std::shared_ptr<vk::Instance> getInstance() const;
std::shared_ptr<vk::PhysicalDevice> getPhysicalDevice() const;
std::shared_ptr<vk::Device> getDevice() const;
Expand Down
15 changes: 11 additions & 4 deletions src/atta/graphics/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@
namespace atta::graphics {

Manager::Manager() {
setGraphicsAPIImpl(GraphicsAPI::OPENGL);
#if ATTA_VULKAN_SUPPORT
_desiredGraphicsAPI = GraphicsAPI::VULKAN;
#else
_desiredGraphicsAPI = GraphicsAPI::OPENGL;
setGraphicsAPIImpl(GraphicsAPI::VULKAN);
#endif
}

Expand Down Expand Up @@ -153,7 +152,15 @@ std::shared_ptr<GraphicsAPI> Manager::getGraphicsAPIImpl() const { return _graph

std::shared_ptr<Window> Manager::getWindowImpl() const { return _window; }

void Manager::setGraphicsAPIImpl(GraphicsAPI::Type type) { _desiredGraphicsAPI = type; }
void Manager::setGraphicsAPIImpl(GraphicsAPI::Type type) {
#if ATTA_VULKAN_SUPPORT
if (type == GraphicsAPI::VULKAN && !VulkanAPI::isSupported()) {
LOG_WARN("gfx::Manager", "Failed to set graphics API to Vulkan");
return;
}
#endif
_desiredGraphicsAPI = type;
}

void Manager::recreateGraphicsAPI() {
if (_uiShutDownFunc)
Expand Down
2 changes: 1 addition & 1 deletion src/extern/solveResources.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#FetchContent_Declare(resources
# URL "http://storage.googleapis.com/atta-resources/atta/resources-0.3.1.zip"
# URL "https://atta-resources.s3.amazonaws.com/v0.3.1-ui-temp.zip"
# SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/resources
#)
#
Expand Down

0 comments on commit c8de137

Please sign in to comment.