Added android support for shadow mapping (projected and omni) and texture (array and cubemap ) example (#97)

This commit is contained in:
saschawillems 2016-03-25 15:29:38 +01:00
parent c6bdca128a
commit 08fc2feecc
33 changed files with 1363 additions and 77 deletions

View file

@ -110,12 +110,25 @@ public:
delete[] uboVS.instance;
}
void loadTextureArray(const char* filename, VkFormat format)
void loadTextureArray(std::string filename, VkFormat format)
{
VkFormatProperties formatProperties;
VkResult err;
#if defined(__ANDROID__)
// Textures are stored inside the apk on Android (compressed)
// So they need to be loaded via the asset manager
AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
assert(asset);
size_t size = AAsset_getLength(asset);
assert(size > 0);
void *textureData = malloc(size);
AAsset_read(asset, textureData, size);
AAsset_close(asset);
gli::texture2DArray tex2DArray(gli::load((const char*)textureData, size));
#else
gli::texture2DArray tex2DArray(gli::load(filename));
#endif
assert(!tex2DArray.empty());
textureArray.width = tex2DArray.dimensions().x;
@ -123,6 +136,7 @@ public:
layerCount = tex2DArray.layers();
// Get device properites for the requested texture format
VkFormatProperties formatProperties;
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties);
VkImageCreateInfo imageCreateInfo = vkTools::initializers::imageCreateInfo();
@ -155,7 +169,7 @@ public:
cmdPool,
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
1);
err = vkAllocateCommandBuffers(device, &cmdBufAlllocatInfo, &cmdBuffer);
VkResult err = vkAllocateCommandBuffers(device, &cmdBufAlllocatInfo, &cmdBuffer);
assert(!err);
VkCommandBufferBeginInfo cmdBufInfo =
@ -332,7 +346,9 @@ public:
void loadTextures()
{
loadTextureArray("./../data/textures/texturearray_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK);
loadTextureArray(
getAssetPath() + "textures/texturearray_bc3.ktx",
VK_FORMAT_BC3_UNORM_BLOCK);
}
void buildCommandBuffers()
@ -629,8 +645,8 @@ public:
// Load shaders
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
shaderStages[0] = loadShader("./../data/shaders/texturearray/instancing.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader("./../data/shaders/texturearray/instancing.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
shaderStages[0] = loadShader(getAssetPath() + "shaders/texturearray/instancing.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/texturearray/instancing.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
vkTools::initializers::pipelineCreateInfo(
@ -747,8 +763,7 @@ public:
VulkanExample *vulkanExample;
#ifdef _WIN32
#if defined(_WIN32)
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (vulkanExample != NULL)
@ -757,9 +772,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
}
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}
#else
#elif defined(__linux__) && !defined(__ANDROID__)
static void handleEvent(const xcb_generic_event_t *event)
{
if (vulkanExample != NULL)
@ -769,21 +782,42 @@ static void handleEvent(const xcb_generic_event_t *event)
}
#endif
#ifdef _WIN32
// Main entry point
#if defined(_WIN32)
// Windows entry point
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
#else
#elif defined(__ANDROID__)
// Android entry point
void android_main(android_app* state)
#elif defined(__linux__)
// Linux entry point
int main(const int argc, const char *argv[])
#endif
{
#if defined(__ANDROID__)
// Removing this may cause the compiler to omit the main entry point
// which would make the application crash at start
app_dummy();
#endif
vulkanExample = new VulkanExample();
#ifdef _WIN32
#if defined(_WIN32)
vulkanExample->setupWindow(hInstance, WndProc);
#else
#elif defined(__ANDROID__)
// Attach vulkan example to global android application state
state->userData = vulkanExample;
state->onAppCmd = VulkanExample::handleAppCommand;
state->onInputEvent = VulkanExample::handleAppInput;
vulkanExample->androidApp = state;
#elif defined(__linux__)
vulkanExample->setupWindow();
#endif
#if !defined(__ANDROID__)
vulkanExample->initSwapchain();
vulkanExample->prepare();
#endif
vulkanExample->renderLoop();
#if !defined(__ANDROID__)
delete(vulkanExample);
return 0;
}
#endif
}