Use wait and signal semaphores, renamed main for android triangle example

This commit is contained in:
saschawillems 2016-03-09 21:37:46 +01:00
parent ae185d8f2e
commit 774d1f37bd
3 changed files with 28 additions and 16 deletions

View file

@ -149,7 +149,7 @@
<CompileAs>CompileAsCpp</CompileAs> <CompileAs>CompileAsCpp</CompileAs>
<PreprocessorDefinitions>VK_NO_PROTOTYPES;VK_USE_PLATFORM_ANDROID_KHR;__STDINT_LIMITS</PreprocessorDefinitions> <PreprocessorDefinitions>VK_NO_PROTOTYPES;VK_USE_PLATFORM_ANDROID_KHR;__STDINT_LIMITS</PreprocessorDefinitions>
<CppLanguageStandard>c++11</CppLanguageStandard> <CppLanguageStandard>c++11</CppLanguageStandard>
<AdditionalIncludeDirectories>../../../external;../../../base;../../../external/glm;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../../../external;../../../base;../../../external/glm;../../base;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<LibraryDependencies>%(LibraryDependencies);GLESv1_CM;EGL;</LibraryDependencies> <LibraryDependencies>%(LibraryDependencies);GLESv1_CM;EGL;</LibraryDependencies>
@ -215,7 +215,7 @@
<ClCompile Include="..\..\..\base\vulkanandroid.cpp" /> <ClCompile Include="..\..\..\base\vulkanandroid.cpp" />
<ClCompile Include="..\..\..\base\vulkantools.cpp" /> <ClCompile Include="..\..\..\base\vulkantools.cpp" />
<ClCompile Include="android_native_app_glue.c" /> <ClCompile Include="android_native_app_glue.c" />
<ClCompile Include="main.cpp" /> <ClCompile Include="triangle.cpp" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View file

@ -7,8 +7,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="android_native_app_glue.c" /> <ClCompile Include="android_native_app_glue.c" />
<ClCompile Include="main.cpp" />
<ClCompile Include="..\..\..\base\vulkanandroid.cpp" /> <ClCompile Include="..\..\..\base\vulkanandroid.cpp" />
<ClCompile Include="..\..\..\base\vulkantools.cpp" /> <ClCompile Include="..\..\..\base\vulkantools.cpp" />
<ClCompile Include="triangle.cpp" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -17,7 +17,7 @@
#include <android/asset_manager.h> #include <android/asset_manager.h>
#define GLM_FORCE_RADIANS #define GLM_FORCE_RADIANS
#define GLM_DEPTH_ZERO_TO_ONE #define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include "glm/glm.hpp" #include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp" #include "glm/gtc/matrix_transform.hpp"
@ -59,6 +59,11 @@ struct VulkanExample
VkPhysicalDeviceMemoryProperties deviceMemoryProperties; VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
std::vector<VkShaderModule> shaderModules; std::vector<VkShaderModule> shaderModules;
struct {
VkSemaphore presentComplete;
VkSemaphore submitSignal;
} semaphores;
struct { struct {
VkBuffer buf; VkBuffer buf;
VkDeviceMemory mem; VkDeviceMemory mem;
@ -251,6 +256,15 @@ struct VulkanExample
VkResult err = vkCreatePipelineCache(device, &pipelineCacheCreateInfo, nullptr, &pipelineCache); VkResult err = vkCreatePipelineCache(device, &pipelineCacheCreateInfo, nullptr, &pipelineCache);
assert(!err); assert(!err);
// Create semaphores for synchronization
VkSemaphoreCreateInfo semaphoreCreateInfo = {};
semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.presentComplete);
assert(!err);
err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.submitSignal);
assert(!err);
createSetupCommandBuffer(); createSetupCommandBuffer();
startSetupCommandBuffer(); startSetupCommandBuffer();
@ -318,6 +332,9 @@ struct VulkanExample
vkDestroyImage(device, depthStencil.image, nullptr); vkDestroyImage(device, depthStencil.image, nullptr);
vkFreeMemory(device, depthStencil.mem, nullptr); vkFreeMemory(device, depthStencil.mem, nullptr);
vkDestroySemaphore(device, semaphores.presentComplete, nullptr);
vkDestroySemaphore(device, semaphores.submitSignal, nullptr);
vkDestroyPipelineCache(device, pipelineCache, nullptr); vkDestroyPipelineCache(device, pipelineCache, nullptr);
vkDestroyDevice(device, nullptr); vkDestroyDevice(device, nullptr);
vkDestroyInstance(instance, nullptr); vkDestroyInstance(instance, nullptr);
@ -983,27 +1000,24 @@ struct VulkanExample
void draw() void draw()
{ {
VkResult err; VkResult err;
VkSemaphore presentCompleteSemaphore;
VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = {};
presentCompleteSemaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
presentCompleteSemaphoreCreateInfo.pNext = NULL;
err = vkCreateSemaphore(device, &presentCompleteSemaphoreCreateInfo, nullptr, &presentCompleteSemaphore);
assert(!err);
// Get next image in the swap chain (back/front buffer) // Get next image in the swap chain (back/front buffer)
err = swapChain.acquireNextImage(presentCompleteSemaphore, &currentBuffer); err = swapChain.acquireNextImage(semaphores.presentComplete, &currentBuffer);
assert(!err); assert(!err);
// The submit infor strcuture contains a list of // The submit infor strcuture contains a list of
// command buffers and semaphores to be submitted to a queue // command buffers and semaphores to be submitted to a queue
// If you want to submit multiple command buffers, pass an array // If you want to submit multiple command buffers, pass an array
VkPipelineStageFlags pipelineStages = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
VkSubmitInfo submitInfo = {}; VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.waitSemaphoreCount = 1; submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = &presentCompleteSemaphore; submitInfo.pWaitSemaphores = &semaphores.presentComplete;
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer]; submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
submitInfo.pWaitDstStageMask = &pipelineStages;
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &semaphores.submitSignal;
// Submit to the graphics queue // Submit to the graphics queue
err = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE); err = vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE);
@ -1011,11 +1025,9 @@ struct VulkanExample
// Present the current buffer to the swap chain // Present the current buffer to the swap chain
// This will display the image // This will display the image
err = swapChain.queuePresent(queue, currentBuffer); err = swapChain.queuePresent(queue, currentBuffer, semaphores.submitSignal);
assert(!err); assert(!err);
vkDestroySemaphore(device, presentCompleteSemaphore, nullptr);
// Add a post present image memory barrier // Add a post present image memory barrier
// This will transform the frame buffer color attachment back // This will transform the frame buffer color attachment back
// to it's initial layout after it has been presented to the // to it's initial layout after it has been presented to the