Skip to content

Commit

Permalink
Streamlined Android initialization code
Browse files Browse the repository at this point in the history
  • Loading branch information
corporateshark committed Jan 18, 2025
1 parent 0b97b2d commit e4e3136
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 75 deletions.
87 changes: 33 additions & 54 deletions lvk/LVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,44 +88,6 @@ static constexpr TextureFormatProperties properties[] = {
PROPS(YUV_420p, 24, .blockWidth = 4, .blockHeight = 4, .compressed = true, .numPlanes = 3), // Subsampled 420
};

bool initVulkanContextWithSwapchain(std::unique_ptr<lvk::VulkanContext>& ctx,
uint32_t width,
uint32_t height,
lvk::HWDeviceType preferredDeviceType) {
using namespace lvk;
HWDeviceDesc device;
uint32_t numDevices = ctx->queryDevices(preferredDeviceType, &device, 1);

if (!numDevices) {
if (preferredDeviceType == HWDeviceType_Discrete) {
numDevices = ctx->queryDevices(HWDeviceType_Integrated, &device);
} else if (preferredDeviceType == HWDeviceType_Integrated) {
numDevices = ctx->queryDevices(HWDeviceType_Discrete, &device);
}
}

if (!numDevices) {
LVK_ASSERT_MSG(false, "GPU is not found");
return false;
}

Result res = ctx->initContext(device);

if (!res.isOk()) {
LVK_ASSERT_MSG(false, "Failed initContext()");
return false;
}

if (width > 0 && height > 0) {
res = ctx->initSwapchain(width, height);
if (!res.isOk()) {
LVK_ASSERT_MSG(false, "Failed to create swapchain");
return false;
}
}
return true;
}

} // namespace

#if __APPLE__ && LVK_WITH_GLFW
Expand Down Expand Up @@ -393,8 +355,10 @@ GLFWwindow* lvk::initWindow(const char* windowTitle, int& outWidth, int& outHeig

return window;
}
#endif // LVK_WITH_GLFW

std::unique_ptr<lvk::IContext> lvk::createVulkanContextWithSwapchain(GLFWwindow* window,
#if LVK_WITH_GLFW || defined(ANDROID)
std::unique_ptr<lvk::IContext> lvk::createVulkanContextWithSwapchain(LVKwindow* window,
uint32_t width,
uint32_t height,
const lvk::ContextConfig& cfg,
Expand All @@ -405,9 +369,11 @@ std::unique_ptr<lvk::IContext> lvk::createVulkanContextWithSwapchain(GLFWwindow*

#if defined(_WIN32)
ctx = std::make_unique<VulkanContext>(cfg, (void*)glfwGetWin32Window(window));
#elif defined(ANDROID)
ctx = std::make_unique<VulkanContext>(cfg, (void*)window);
#elif defined(__linux__)
#if defined(LVK_WITH_WAYLAND)
auto waylandWindow = glfwGetWaylandWindow(window);
wl_surface* waylandWindow = glfwGetWaylandWindow(window);
if (!waylandWindow) {
LVK_ASSERT_MSG(false, "Wayland window not found");
return nullptr;
Expand All @@ -421,25 +387,38 @@ std::unique_ptr<lvk::IContext> lvk::createVulkanContextWithSwapchain(GLFWwindow*
#else
#error Unsupported OS
#endif
if (!initVulkanContextWithSwapchain(ctx, width, height, preferredDeviceType)) {

HWDeviceDesc device;
uint32_t numDevices = ctx->queryDevices(preferredDeviceType, &device, 1);

if (!numDevices) {
if (preferredDeviceType == HWDeviceType_Discrete) {
numDevices = ctx->queryDevices(HWDeviceType_Integrated, &device);
} else if (preferredDeviceType == HWDeviceType_Integrated) {
numDevices = ctx->queryDevices(HWDeviceType_Discrete, &device);
}
}

if (!numDevices) {
LVK_ASSERT_MSG(false, "GPU is not found");
return nullptr;
}

return std::move(ctx);
}
#endif // LVK_WITH_GLFW
Result res = ctx->initContext(device);

#if defined(ANDROID)
std::unique_ptr<lvk::IContext> lvk::createVulkanContextWithSwapchain(ANativeWindow* window,
uint32_t width,
uint32_t height,
const lvk::ContextConfig& cfg,
lvk::HWDeviceType preferredDeviceType) {
using namespace lvk;
std::unique_ptr<VulkanContext> ctx = std::make_unique<VulkanContext>(cfg, (void*)window);
if (!initVulkanContextWithSwapchain(ctx, width, height, preferredDeviceType)) {
if (!res.isOk()) {
LVK_ASSERT_MSG(false, "createVulkanContextWithSwapchain() failed");
return nullptr;
}

if (width > 0 && height > 0) {
res = ctx->initSwapchain(width, height);
if (!res.isOk()) {
LVK_ASSERT_MSG(false, "initSwapchain() failed");
return nullptr;
}
}

return std::move(ctx);
}
#endif // ANDROID
#endif // LVK_WITH_GLFW || defined(ANDROID)
16 changes: 8 additions & 8 deletions lvk/LVK.h
Original file line number Diff line number Diff line change
Expand Up @@ -1151,19 +1151,19 @@ constexpr uint32_t calcNumMipLevels(uint32_t width, uint32_t height) {
* The actual values in pixels are returned in parameters.
*/
GLFWwindow* initWindow(const char* windowTitle, int& outWidth, int& outHeight, bool resizable = false);
std::unique_ptr<lvk::IContext> createVulkanContextWithSwapchain(GLFWwindow* window,
uint32_t width,
uint32_t height,
const lvk::ContextConfig& cfg,
lvk::HWDeviceType preferredDeviceType = lvk::HWDeviceType_Discrete);
#endif
#endif // LVK_WITH_GLFW

#if LVK_WITH_GLFW || defined(ANDROID)
#if defined(ANDROID)
std::unique_ptr<lvk::IContext> createVulkanContextWithSwapchain(ANativeWindow* window,
using LVKwindow = ANativeWindow;
#else
using LVKwindow = GLFWwindow;
#endif
std::unique_ptr<lvk::IContext> createVulkanContextWithSwapchain(LVKwindow* window,
uint32_t width,
uint32_t height,
const lvk::ContextConfig& cfg,
lvk::HWDeviceType preferredDeviceType = lvk::HWDeviceType_Discrete);
#endif
#endif // LVK_WITH_GLFW || defined(ANDROID)

} // namespace lvk
25 changes: 12 additions & 13 deletions lvk/vulkan/VulkanClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5996,7 +5996,7 @@ uint32_t lvk::VulkanContext::queryDevices(HWDeviceType deviceType, HWDeviceDesc*
std::vector<VkPhysicalDevice> vkDevices(deviceCount);
VK_ASSERT(vkEnumeratePhysicalDevices(vkInstance_, &deviceCount, vkDevices.data()));

auto convertVulkanDeviceTypeToIGL = [](VkPhysicalDeviceType vkDeviceType) -> HWDeviceType {
auto convertVulkanDeviceTypeToLVK = [](VkPhysicalDeviceType vkDeviceType) -> HWDeviceType {
switch (vkDeviceType) {
case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
return HWDeviceType_Integrated;
Expand All @@ -6020,7 +6020,7 @@ uint32_t lvk::VulkanContext::queryDevices(HWDeviceType deviceType, HWDeviceDesc*
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);

const HWDeviceType deviceType = convertVulkanDeviceTypeToIGL(deviceProperties.deviceType);
const HWDeviceType deviceType = convertVulkanDeviceTypeToLVK(deviceProperties.deviceType);

// filter non-suitable hardware devices
if (desiredDeviceType != HWDeviceType_Software && desiredDeviceType != deviceType) {
Expand Down Expand Up @@ -6301,16 +6301,6 @@ lvk::Result lvk::VulkanContext::initContext(const HWDeviceDesc& desc) {
addOptionalExtension(VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME, has8BitIndices_, &indexTypeUint8Features);
}

const VkDeviceCreateInfo ci = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = createInfoNext,
.queueCreateInfoCount = numQueues,
.pQueueCreateInfos = ciQueue,
.enabledExtensionCount = (uint32_t)deviceExtensionNames.size(),
.ppEnabledExtensionNames = deviceExtensionNames.data(),
.pEnabledFeatures = &deviceFeatures10,
};

// check extensions
{
std::string missingExtensions;
Expand Down Expand Up @@ -6485,6 +6475,15 @@ lvk::Result lvk::VulkanContext::initContext(const HWDeviceDesc& desc) {
}
}

const VkDeviceCreateInfo ci = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = createInfoNext,
.queueCreateInfoCount = numQueues,
.pQueueCreateInfos = ciQueue,
.enabledExtensionCount = (uint32_t)deviceExtensionNames.size(),
.ppEnabledExtensionNames = deviceExtensionNames.data(),
.pEnabledFeatures = &deviceFeatures10,
};
VK_ASSERT_RETURN(vkCreateDevice(vkPhysicalDevice_, &ci, nullptr, &vkDevice_));

volkLoadDevice(vkDevice_);
Expand Down Expand Up @@ -6624,7 +6623,7 @@ lvk::Result lvk::VulkanContext::initContext(const HWDeviceDesc& desc) {
};
}
LVK_ASSERT(pimpl_->tracyVkCtx_);
#endif // IGL_WITH_TRACY_GPU
#endif // LVK_WITH_TRACY_GPU

return Result();
}
Expand Down

0 comments on commit e4e3136

Please sign in to comment.