Moved android_app object to global scope

This commit is contained in:
saschawillems 2017-01-18 19:21:40 +01:00
parent 3dfb17c89a
commit 839fc84d69
7 changed files with 40 additions and 51 deletions

View file

@ -87,10 +87,6 @@ namespace vks
uint32_t indexCount = 0; uint32_t indexCount = 0;
uint32_t vertexCount = 0; uint32_t vertexCount = 0;
#if defined(__ANDROID__)
AAssetManager* assetManager = nullptr;
#endif
/** @brief Stores vertex and index base and counts for each part of a model */ /** @brief Stores vertex and index base and counts for each part of a model */
struct ModelPart { struct ModelPart {
uint32_t vertexBase; uint32_t vertexBase;
@ -121,7 +117,7 @@ namespace vks
} }
} }
boolean loadFromFile( bool loadFromFile(
vk::VulkanDevice *device, vk::VulkanDevice *device,
const std::string& filename, const std::string& filename,
vks::VertexLayout layout, vks::VertexLayout layout,
@ -139,7 +135,7 @@ namespace vks
// Meshes are stored inside the apk on Android (compressed) // Meshes are stored inside the apk on Android (compressed)
// So they need to be loaded via the asset manager // So they need to be loaded via the asset manager
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_STREAMING); AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
assert(asset); assert(asset);
size_t size = AAsset_getLength(asset); size_t size = AAsset_getLength(asset);

View file

@ -67,10 +67,6 @@ namespace vkTools
VkCommandBuffer cmdBuffer; VkCommandBuffer cmdBuffer;
VkCommandPool cmdPool; VkCommandPool cmdPool;
public: public:
#if defined(__ANDROID__)
AAssetManager* assetManager = nullptr;
#endif
/** /**
* Default constructor * Default constructor
* *
@ -126,11 +122,9 @@ namespace vkTools
VkImageLayout imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) VkImageLayout imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
{ {
#if defined(__ANDROID__) #if defined(__ANDROID__)
assert(assetManager != nullptr);
// Textures are stored inside the apk on Android (compressed) // Textures are stored inside the apk on Android (compressed)
// So they need to be loaded via the asset manager // So they need to be loaded via the asset manager
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_STREAMING); AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
assert(asset); assert(asset);
size_t size = AAsset_getLength(asset); size_t size = AAsset_getLength(asset);
assert(size > 0); assert(size > 0);
@ -457,11 +451,9 @@ namespace vkTools
VkImageLayout imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) VkImageLayout imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
{ {
#if defined(__ANDROID__) #if defined(__ANDROID__)
assert(assetManager != nullptr);
// Textures are stored inside the apk on Android (compressed) // Textures are stored inside the apk on Android (compressed)
// So they need to be loaded via the asset manager // So they need to be loaded via the asset manager
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_STREAMING); AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
assert(asset); assert(asset);
size_t size = AAsset_getLength(asset); size_t size = AAsset_getLength(asset);
assert(size > 0); assert(size > 0);
@ -680,11 +672,9 @@ namespace vkTools
VkImageLayout imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) VkImageLayout imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
{ {
#if defined(__ANDROID__) #if defined(__ANDROID__)
assert(assetManager != nullptr);
// Textures are stored inside the apk on Android (compressed) // Textures are stored inside the apk on Android (compressed)
// So they need to be loaded via the asset manager // So they need to be loaded via the asset manager
AAsset* asset = AAssetManager_open(assetManager, filename.c_str(), AASSET_MODE_STREAMING); AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
assert(asset); assert(asset);
size_t size = AAsset_getLength(asset); size_t size = AAsset_getLength(asset);
assert(size > 0); assert(size > 0);

View file

@ -9,8 +9,10 @@
#include "vulkanandroid.h" #include "vulkanandroid.h"
#if defined(__ANDROID__) #if defined(__ANDROID__)
#include <android/log.h> #include <android/log.h>
#include <dlfcn.h> #include <dlfcn.h>
android_app* androidApp;
PFN_vkCreateInstance vkCreateInstance; PFN_vkCreateInstance vkCreateInstance;
PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr; PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr;
@ -122,40 +124,40 @@ void *libVulkan;
// Dynamically load Vulkan library and base function pointers // Dynamically load Vulkan library and base function pointers
bool loadVulkanLibrary() bool loadVulkanLibrary()
{ {
__android_log_print(ANDROID_LOG_INFO, "vulkanandroid", "Loading libvulkan.so...\n"); __android_log_print(ANDROID_LOG_INFO, "vulkanandroid", "Loading libvulkan.so...\n");
// Load vulkan library // Load vulkan library
libVulkan = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL); libVulkan = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL);
if (!libVulkan) if (!libVulkan)
{ {
__android_log_print(ANDROID_LOG_INFO, "vulkanandroid", "Could not load vulkan library : %s!\n", dlerror()); __android_log_print(ANDROID_LOG_INFO, "vulkanandroid", "Could not load vulkan library : %s!\n", dlerror());
return false; return false;
} }
// Load base function pointers // Load base function pointers
vkEnumerateInstanceExtensionProperties = reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(dlsym(libVulkan, "vkEnumerateInstanceExtensionProperties")); vkEnumerateInstanceExtensionProperties = reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(dlsym(libVulkan, "vkEnumerateInstanceExtensionProperties"));
vkEnumerateInstanceLayerProperties = reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(dlsym(libVulkan, "vkEnumerateInstanceLayerProperties")); vkEnumerateInstanceLayerProperties = reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(dlsym(libVulkan, "vkEnumerateInstanceLayerProperties"));
vkCreateInstance = reinterpret_cast<PFN_vkCreateInstance>(dlsym(libVulkan, "vkCreateInstance")); vkCreateInstance = reinterpret_cast<PFN_vkCreateInstance>(dlsym(libVulkan, "vkCreateInstance"));
vkGetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(dlsym(libVulkan, "vkGetInstanceProcAddr")); vkGetInstanceProcAddr = reinterpret_cast<PFN_vkGetInstanceProcAddr>(dlsym(libVulkan, "vkGetInstanceProcAddr"));
vkGetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(dlsym(libVulkan, "vkGetDeviceProcAddr")); vkGetDeviceProcAddr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(dlsym(libVulkan, "vkGetDeviceProcAddr"));
return true; return true;
} }
// Load instance based Vulkan function pointers // Load instance based Vulkan function pointers
void loadVulkanFunctions(VkInstance instance) void loadVulkanFunctions(VkInstance instance)
{ {
__android_log_print(ANDROID_LOG_INFO, "vulkanandroid", "Loading instance based function pointers...\n"); __android_log_print(ANDROID_LOG_INFO, "vulkanandroid", "Loading instance based function pointers...\n");
vkEnumeratePhysicalDevices = reinterpret_cast<PFN_vkEnumeratePhysicalDevices>(vkGetInstanceProcAddr(instance, "vkEnumeratePhysicalDevices")); vkEnumeratePhysicalDevices = reinterpret_cast<PFN_vkEnumeratePhysicalDevices>(vkGetInstanceProcAddr(instance, "vkEnumeratePhysicalDevices"));
vkGetPhysicalDeviceProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties")); vkGetPhysicalDeviceProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceProperties"));
vkEnumerateDeviceLayerProperties = reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>(vkGetInstanceProcAddr(instance, "vkEnumerateDeviceLayerProperties")); vkEnumerateDeviceLayerProperties = reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>(vkGetInstanceProcAddr(instance, "vkEnumerateDeviceLayerProperties"));
vkEnumerateDeviceExtensionProperties = reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(vkGetInstanceProcAddr(instance, "vkEnumerateDeviceExtensionProperties")); vkEnumerateDeviceExtensionProperties = reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(vkGetInstanceProcAddr(instance, "vkEnumerateDeviceExtensionProperties"));
vkGetPhysicalDeviceQueueFamilyProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceQueueFamilyProperties>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceQueueFamilyProperties")); vkGetPhysicalDeviceQueueFamilyProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceQueueFamilyProperties>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceQueueFamilyProperties"));
vkGetPhysicalDeviceFeatures = reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures")); vkGetPhysicalDeviceFeatures = reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFeatures"));
vkCreateDevice = reinterpret_cast<PFN_vkCreateDevice>(vkGetInstanceProcAddr(instance, "vkCreateDevice")); vkCreateDevice = reinterpret_cast<PFN_vkCreateDevice>(vkGetInstanceProcAddr(instance, "vkCreateDevice"));
vkGetPhysicalDeviceFormatProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceFormatProperties>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFormatProperties")); vkGetPhysicalDeviceFormatProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceFormatProperties>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceFormatProperties"));
vkGetPhysicalDeviceMemoryProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceMemoryProperties>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceMemoryProperties")); vkGetPhysicalDeviceMemoryProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceMemoryProperties>(vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceMemoryProperties"));
vkCmdPipelineBarrier = reinterpret_cast<PFN_vkCmdPipelineBarrier>(vkGetInstanceProcAddr(instance, "vkCmdPipelineBarrier")); vkCmdPipelineBarrier = reinterpret_cast<PFN_vkCmdPipelineBarrier>(vkGetInstanceProcAddr(instance, "vkCmdPipelineBarrier"));
vkCreateShaderModule = reinterpret_cast<PFN_vkCreateShaderModule>(vkGetInstanceProcAddr(instance, "vkCreateShaderModule")); vkCreateShaderModule = reinterpret_cast<PFN_vkCreateShaderModule>(vkGetInstanceProcAddr(instance, "vkCreateShaderModule"));

View file

@ -23,6 +23,7 @@
#if defined(__ANDROID__) #if defined(__ANDROID__)
#include <android/log.h> #include <android/log.h>
#include <android_native_app_glue.h>
#include <memory> #include <memory>
// Missing from the NDK // Missing from the NDK
@ -35,6 +36,9 @@ namespace std
} }
} }
// Global reference to android application object
extern android_app* androidApp;
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "vulkanExample", __VA_ARGS__)) #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "vulkanExample", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "vulkanExample", __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "vulkanExample", __VA_ARGS__))
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "vulkanExample", __VA_ARGS__)) #define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "vulkanExample", __VA_ARGS__))

View file

@ -173,9 +173,6 @@ void VulkanExampleBase::prepare()
setupFrameBuffer(); setupFrameBuffer();
// Create a simple texture loader class // Create a simple texture loader class
textureLoader = new vkTools::VulkanTextureLoader(vulkanDevice, queue, cmdPool); textureLoader = new vkTools::VulkanTextureLoader(vulkanDevice, queue, cmdPool);
#if defined(__ANDROID__)
textureLoader->assetManager = androidApp->activity->assetManager;
#endif
if (enableTextOverlay) if (enableTextOverlay)
{ {
// Load the text rendering shaders // Load the text rendering shaders
@ -1101,7 +1098,7 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd)
break; break;
case APP_CMD_INIT_WINDOW: case APP_CMD_INIT_WINDOW:
LOGD("APP_CMD_INIT_WINDOW"); LOGD("APP_CMD_INIT_WINDOW");
if (vulkanExample->androidApp->window != NULL) if (androidApp->window != NULL)
{ {
vulkanExample->initVulkan(); vulkanExample->initVulkan();
vulkanExample->initSwapchain(); vulkanExample->initSwapchain();

View file

@ -188,7 +188,6 @@ public:
HWND window; HWND window;
HINSTANCE windowInstance; HINSTANCE windowInstance;
#elif defined(__ANDROID__) #elif defined(__ANDROID__)
android_app* androidApp;
// true if application has focused, false if moved to background // true if application has focused, false if moved to background
bool focused = false; bool focused = false;
#elif defined(__linux__) #elif defined(__linux__)
@ -359,7 +358,7 @@ void android_main(android_app* state) \
state->userData = vulkanExample; \ state->userData = vulkanExample; \
state->onAppCmd = VulkanExample::handleAppCommand; \ state->onAppCmd = VulkanExample::handleAppCommand; \
state->onInputEvent = VulkanExample::handleAppInput; \ state->onInputEvent = VulkanExample::handleAppInput; \
vulkanExample->androidApp = state; \ androidApp = state; \
vulkanExample->renderLoop(); \ vulkanExample->renderLoop(); \
delete(vulkanExample); \ delete(vulkanExample); \
} }

View file

@ -90,6 +90,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Base", "Base", "{09B9A54B-F
base\vulkanheightmap.hpp = base\vulkanheightmap.hpp base\vulkanheightmap.hpp = base\vulkanheightmap.hpp
base\VulkanInitializers.hpp = base\VulkanInitializers.hpp base\VulkanInitializers.hpp = base\VulkanInitializers.hpp
base\vulkanMeshLoader.hpp = base\vulkanMeshLoader.hpp base\vulkanMeshLoader.hpp = base\vulkanMeshLoader.hpp
base\VulkanModelLoader.hpp = base\VulkanModelLoader.hpp
base\vulkanscene.hpp = base\vulkanscene.hpp base\vulkanscene.hpp = base\vulkanscene.hpp
base\vulkanswapchain.hpp = base\vulkanswapchain.hpp base\vulkanswapchain.hpp = base\vulkanswapchain.hpp
base\vulkantextoverlay.hpp = base\vulkantextoverlay.hpp base\vulkantextoverlay.hpp = base\vulkantextoverlay.hpp