// // Vulkan termination. // void Example::terminate(const vkts::IUpdateThreadContext& updateContext) { if (initialResources.get()) { if (initialResources->getDevice().get()) { terminateResources(updateContext); // if (sceneContext.get()) { sceneContext->destroy(); sceneContext.reset(); } if (scene.get()) { scene->destroy(); } if (swapchain.get()) { swapchain->destroy(); } if (pipelineLayout.get()) { pipelineLayout->destroy(); } if (vertexShaderModule.get()) { vertexShaderModule->destroy(); } if (fragmentShaderModule.get()) { fragmentShaderModule->destroy(); } if (vertexViewProjectionUniformBuffer.get()) { vertexViewProjectionUniformBuffer->destroy(); } if (fragmentUniformBuffer.get()) { fragmentUniformBuffer->destroy(); } if (descriptorSetLayout.get()) { descriptorSetLayout->destroy(); } if (renderingCompleteSemaphore.get()) { renderingCompleteSemaphore->destroy(); } if (imageAcquiredSemaphore.get()) { imageAcquiredSemaphore->destroy(); } if (commandPool.get()) { commandPool->destroy(); } } } }
// // Vulkan update. // VkBool32 Example::update(const vkts::IUpdateThreadContext& updateContext) { for (size_t i = 0; i < allUpdateables.size(); i++) { allUpdateables[i]->update(updateContext.getDeltaTime(), updateContext.getDeltaTicks()); } // VkResult result = VK_SUCCESS; // if (windowDimension != updateContext.getWindowDimension(windowIndex)) { windowDimension = updateContext.getWindowDimension(windowIndex); result = VK_ERROR_OUT_OF_DATE_KHR; } // uint32_t currentBuffer; if (result == VK_SUCCESS) { result = swapchain->acquireNextImage(UINT64_MAX, imageAcquiredSemaphore->getSemaphore(), VK_NULL_HANDLE, currentBuffer); } if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) { glm::mat4 projectionMatrix(1.0f); glm::mat4 viewMatrix(1.0f); const auto& dimension = updateContext.getWindowDimension(windowIndex); projectionMatrix = vkts::perspectiveMat4(45.0f, (float) dimension.x / (float) dimension.y, 1.0f, 100.0f); viewMatrix = camera->getViewMatrix(); glm::vec3 lightDirection = glm::mat3(viewMatrix) * glm::vec3(0.0f, 1.0f, 2.0f); lightDirection = glm::normalize(lightDirection); if (!fragmentUniformBuffer->upload(0, 0, lightDirection)) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not upload light direction."); return VK_FALSE; } if (!vertexViewProjectionUniformBuffer->upload(0 * sizeof(float) * 16, 0, projectionMatrix)) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not upload matrices."); return VK_FALSE; } if (!vertexViewProjectionUniformBuffer->upload(1 * sizeof(float) * 16, 0, viewMatrix)) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not upload matrices."); return VK_FALSE; } if (scene.get()) { scene->updateRecursive(updateContext); } // VkSemaphore waitSemaphores = imageAcquiredSemaphore->getSemaphore(); VkSemaphore signalSemaphores = renderingCompleteSemaphore->getSemaphore(); VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; VkSubmitInfo submitInfo; memset(&submitInfo, 0, sizeof(VkSubmitInfo)); submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.waitSemaphoreCount = 1; submitInfo.pWaitSemaphores = &waitSemaphores; submitInfo.pWaitDstStageMask = &waitDstStageMask; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = cmdBuffer[currentBuffer]->getCommandBuffers(); submitInfo.signalSemaphoreCount = 1; submitInfo.pSignalSemaphores = &signalSemaphores; result = initialResources->getQueue()->submit(1, &submitInfo, VK_NULL_HANDLE); if (result != VK_SUCCESS) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not submit queue."); return VK_FALSE; } waitSemaphores = renderingCompleteSemaphore->getSemaphore(); VkSwapchainKHR swapchains = swapchain->getSwapchain(); result = swapchain->queuePresent(initialResources->getQueue()->getQueue(), 1, &waitSemaphores, 1, &swapchains, ¤tBuffer, nullptr); if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) { result = initialResources->getQueue()->waitIdle(); if (result != VK_SUCCESS) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not wait for idle queue."); return VK_FALSE; } } else { if (result == VK_ERROR_OUT_OF_DATE_KHR) { terminateResources(updateContext); if (!buildResources(updateContext)) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not build resources."); return VK_FALSE; } } else { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not present queue."); return VK_FALSE; } } } else { if (result == VK_ERROR_OUT_OF_DATE_KHR) { terminateResources(updateContext); if (!buildResources(updateContext)) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not build resources."); return VK_FALSE; } } else { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not acquire next image."); return VK_FALSE; } } // result = imageAcquiredSemaphore->reset(); if (result != VK_SUCCESS) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not reset semaphore."); return VK_FALSE; } result = renderingCompleteSemaphore->reset(); if (result != VK_SUCCESS) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not reset semaphore."); return VK_FALSE; } return VK_TRUE; }
// // Vulkan update. // VkBool32 Example::update(const vkts::IUpdateThreadContext& updateContext) { VkResult result = VK_SUCCESS; // if (windowDimension != updateContext.getWindowDimension(windowIndex)) { windowDimension = updateContext.getWindowDimension(windowIndex); result = VK_ERROR_OUT_OF_DATE_KHR; } // uint32_t currentBuffer; if (result == VK_SUCCESS) { result = swapchain->acquireNextImage(UINT64_MAX, imageAcquiredSemaphore->getSemaphore(), VK_NULL_HANDLE, currentBuffer); } if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) { VkSemaphore waitSemaphores = imageAcquiredSemaphore->getSemaphore(); VkSemaphore signalSemaphores = renderingCompleteSemaphore->getSemaphore(); VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; VkSubmitInfo submitInfo; memset(&submitInfo, 0, sizeof(VkSubmitInfo)); submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.waitSemaphoreCount = 1; submitInfo.pWaitSemaphores = &waitSemaphores; submitInfo.pWaitDstStageMask = &waitDstStageMask; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = cmdBuffer[currentBuffer]->getCommandBuffers(); submitInfo.signalSemaphoreCount = 1; submitInfo.pSignalSemaphores = &signalSemaphores; result = queue->submit(1, &submitInfo, VK_NULL_HANDLE); if (result != VK_SUCCESS) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not submit queue."); return VK_FALSE; } waitSemaphores = renderingCompleteSemaphore->getSemaphore(); VkSwapchainKHR swapchains = swapchain->getSwapchain(); result = swapchain->queuePresent(queue->getQueue(), 1, &waitSemaphores, 1, &swapchains, ¤tBuffer, nullptr); if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) { result = queue->waitIdle(); if (result != VK_SUCCESS) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not wait for idle queue."); return VK_FALSE; } } else { if (result == VK_ERROR_OUT_OF_DATE_KHR) { terminateResources(updateContext); if (!buildResources(updateContext)) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not build resources."); return VK_FALSE; } } else { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not present queue."); return VK_FALSE; } } } else { if (result == VK_ERROR_OUT_OF_DATE_KHR) { terminateResources(updateContext); if (!buildResources(updateContext)) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not build resources."); return VK_FALSE; } } else { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not acquire next image."); return VK_FALSE; } } // result = imageAcquiredSemaphore->reset(); if (result != VK_SUCCESS) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not reset semaphore."); return VK_FALSE; } result = renderingCompleteSemaphore->reset(); if (result != VK_SUCCESS) { vkts::logPrint(VKTS_LOG_ERROR, "Example: Could not reset semaphore."); return VK_FALSE; } return VK_TRUE; }
// // Vulkan termination. // void Example::terminate(const vkts::IUpdateThreadContext& updateContext) { if (device.get()) { terminateResources(updateContext); // if (swapchain.get()) { swapchain->destroy(); } if (pipelineLayout != VK_NULL_HANDLE) { vkDestroyPipelineLayout(device->getDevice(), pipelineLayout, nullptr); pipelineLayout = VK_NULL_HANDLE; } if (pipelineCache != VK_NULL_HANDLE) { vkDestroyPipelineCache(device->getDevice(), pipelineCache, nullptr); pipelineCache = VK_NULL_HANDLE; } if (vertexShaderModule != VK_NULL_HANDLE) { vkDestroyShaderModule(device->getDevice(), vertexShaderModule, nullptr); vertexShaderModule = VK_NULL_HANDLE; } if (fragmentShaderModule != VK_NULL_HANDLE) { vkDestroyShaderModule(device->getDevice(), fragmentShaderModule, nullptr); fragmentShaderModule = VK_NULL_HANDLE; } if (vertexBuffer != VK_NULL_HANDLE) { vkDestroyBuffer(device->getDevice(), vertexBuffer, nullptr); vertexBuffer = VK_NULL_HANDLE; } if (deviceMemoryVertexBuffer != VK_NULL_HANDLE) { vkFreeMemory(device->getDevice(), deviceMemoryVertexBuffer, nullptr); deviceMemoryVertexBuffer = VK_NULL_HANDLE; } if (renderingCompleteSemaphore.get()) { renderingCompleteSemaphore->destroy(); } if (imageAcquiredSemaphore.get()) { imageAcquiredSemaphore->destroy(); } if (commandPool.get()) { commandPool->destroy(); } if (surface.get()) { surface->destroy(); } device->destroy(); } if (instance.get()) { instance->destroy(); } }
// // Vulkan termination. // void Example::terminate(const vkts::IUpdateThreadContext& updateContext) { if (device.get()) { terminateResources(updateContext); // if (swapchain.get()) { swapchain->destroy(); } if (pipelineLayout.get()) { pipelineLayout->destroy(); } if (pipelineCache.get()) { pipelineCache->destroy(); } if (vertexShaderModule.get()) { vertexShaderModule->destroy(); } if (fragmentShaderModule.get()) { fragmentShaderModule->destroy(); } if (vertexBuffer.get()) { vertexBuffer->destroy(); } if (deviceMemoryVertexBuffer.get()) { deviceMemoryVertexBuffer->destroy(); } if (descriptorSet != VK_NULL_HANDLE) { vkFreeDescriptorSets(device->getDevice(), descriptorPool, 1, &descriptorSet); descriptorSet = VK_NULL_HANDLE; } if (descriptorPool != VK_NULL_HANDLE) { vkDestroyDescriptorPool(device->getDevice(), descriptorPool, nullptr); descriptorPool = VK_NULL_HANDLE; } if (descriptorSetLayout != VK_NULL_HANDLE) { vkDestroyDescriptorSetLayout(device->getDevice(), descriptorSetLayout, nullptr); descriptorSetLayout = VK_NULL_HANDLE; } if (imageView.get()) { imageView->destroy(); } if (sampler.get()) { sampler->destroy(); } destroyTexture(image, deviceMemoryImage); if (renderingCompleteSemaphore.get()) { renderingCompleteSemaphore->destroy(); } if (imageAcquiredSemaphore.get()) { imageAcquiredSemaphore->destroy(); } if (commandPool.get()) { commandPool->destroy(); } if (surface.get()) { surface->destroy(); } device->destroy(); } if (instance.get()) { instance->destroy(); } }