From d83c4ac4074fe436551ef042a92aa95c142140b5 Mon Sep 17 00:00:00 2001 From: Tim Sylvester Date: Mon, 7 Oct 2024 04:12:01 -0700 Subject: [PATCH] Fix errors with Vulkan Xcode build (#2906) --- platform/glfw/glfw_view.cpp | 88 +++++++++++++++------- platform/glfw/glfw_view.hpp | 3 + src/mbgl/shaders/vulkan/shader_program.cpp | 2 +- src/mbgl/vulkan/context.cpp | 20 +++-- src/mbgl/vulkan/drawable.cpp | 25 +++--- src/mbgl/vulkan/texture2d.cpp | 2 +- 6 files changed, 94 insertions(+), 46 deletions(-) diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 36a0e01fa..b20e11e14 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -209,6 +209,13 @@ GLFWView::GLFWView(bool fullscreen_, glfwSetScrollCallback(window, onScroll); glfwSetKeyCallback(window, onKey); glfwSetWindowFocusCallback(window, onWindowFocus); +#if defined(__APPLE__) && defined(MLN_RENDER_BACKEND_VULKAN) + glfwSetWindowRefreshCallback(window, onWindowRefresh); +#endif + + // "... applications will typically want to set the swap interval to one" + // https://www.glfw.org/docs/latest/quick.html#quick_swap_buffers + glfwSwapInterval(1); glfwGetWindowSize(window, &width, &height); @@ -915,6 +922,16 @@ void GLFWView::onFramebufferResize(GLFWwindow *window, int width, int height) { // different pixel ratio. We are forcing a repaint my invalidating the view, // which triggers a rerender with the new framebuffer dimensions. view->invalidate(); + +#if defined(__APPLE__) && defined(MLN_RENDER_BACKEND_VULKAN) + // Render continuously while resizing + // Untested elsewhere + view->render(); + +#if defined(MLN_RENDER_BACKEND_OPENGL) + glfwSwapBuffers(window); +#endif +#endif } void GLFWView::onMouseClick(GLFWwindow *window, int button, int action, int modifiers) { @@ -1021,6 +1038,49 @@ void GLFWView::onWindowFocus(GLFWwindow *window, int focused) { } } +#if defined(__APPLE__) && defined(MLN_RENDER_BACKEND_VULKAN) +void GLFWView::onWindowRefresh(GLFWwindow *window) { + // Untested elsewhere + if (auto *view = reinterpret_cast(glfwGetWindowUserPointer(window))) { + view->invalidate(); + view->render(); +#if defined(MLN_RENDER_BACKEND_OPENGL) + glfwSwapBuffers(window); +#endif + } +} +#endif + +void GLFWView::render() { + if (dirty && rendererFrontend) { + MLN_TRACE_ZONE(ReRender); + + dirty = false; + const double started = glfwGetTime(); + + if (animateRouteCallback) { + MLN_TRACE_ZONE(animateRouteCallback); + + animateRouteCallback(map); + } + + updateAnimatedAnnotations(); + + mbgl::gfx::BackendScope scope{backend->getRendererBackend()}; + + rendererFrontend->render(); + + if (freeCameraDemoPhase >= 0.0) { + updateFreeCameraDemo(); + } + + report(static_cast(1000 * (glfwGetTime() - started))); + if (benchmark) { + invalidate(); + } + } +} + void GLFWView::run() { MLN_TRACE_FUNC(); @@ -1040,33 +1100,7 @@ void GLFWView::run() { glfwPollEvents(); } - if (dirty && rendererFrontend) { - MLN_TRACE_ZONE(ReRender); - - dirty = false; - const double started = glfwGetTime(); - - if (animateRouteCallback) { - MLN_TRACE_ZONE(animateRouteCallback); - - animateRouteCallback(map); - } - - updateAnimatedAnnotations(); - - mbgl::gfx::BackendScope scope{backend->getRendererBackend()}; - - rendererFrontend->render(); - - if (freeCameraDemoPhase >= 0.0) { - updateFreeCameraDemo(); - } - - report(static_cast(1000 * (glfwGetTime() - started))); - if (benchmark) { - invalidate(); - } - } + render(); }; // Cap frame rate to 60hz if benchmark mode is disabled diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp index a4912d1c1..c97042937 100644 --- a/platform/glfw/glfw_view.hpp +++ b/platform/glfw/glfw_view.hpp @@ -77,10 +77,13 @@ private: static void onMouseClick(GLFWwindow *window, int button, int action, int modifiers); static void onMouseMove(GLFWwindow *window, double x, double y); static void onWindowFocus(GLFWwindow *window, int focused); + static void onWindowRefresh(GLFWwindow *window); // Internal void report(float duration); + void render(); + mbgl::Color makeRandomColor() const; mbgl::Point makeRandomPoint() const; static std::unique_ptr makeImage(const std::string &id, diff --git a/src/mbgl/shaders/vulkan/shader_program.cpp b/src/mbgl/shaders/vulkan/shader_program.cpp index ca5971833..2f7793bb9 100644 --- a/src/mbgl/shaders/vulkan/shader_program.cpp +++ b/src/mbgl/shaders/vulkan/shader_program.cpp @@ -54,7 +54,7 @@ ShaderProgram::ShaderProgram(shaders::BuiltIn shaderID, const auto preamble = defineStr + "\n" + prelude; const char* shaderData = data.data(); - const int shaderDataSize = data.size(); + const int shaderDataSize = static_cast(data.size()); glslShader.setPreamble(preamble.c_str()); glslShader.setStringsWithLengths(&shaderData, &shaderDataSize, 1); diff --git a/src/mbgl/vulkan/context.cpp b/src/mbgl/vulkan/context.cpp index 25c0d322b..4f07c48ea 100644 --- a/src/mbgl/vulkan/context.cpp +++ b/src/mbgl/vulkan/context.cpp @@ -207,9 +207,11 @@ void Context::beginFrame() { } else if (acquireImageResult.result == vk::Result::eSuboptimalKHR) { renderableResource.setAcquiredImageIndex(acquireImageResult.value); // TODO implement pre-rotation transform for surface orientation - // requestSurfaceUpdate(); - // beginFrame(); - // return; +#if defined(__APPLE__) + requestSurfaceUpdate(); + beginFrame(); + return; +#endif } } catch (const vk::OutOfDateKHRError& e) { @@ -271,7 +273,9 @@ void Context::submitFrame() { const vk::Result presentResult = presentQueue.presentKHR(presentInfo); if (presentResult == vk::Result::eSuboptimalKHR) { // TODO implement pre-rotation transform for surface orientation - // requestSurfaceUpdate(); +#if defined(__APPLE__) + requestSurfaceUpdate(); +#endif } } catch (const vk::OutOfDateKHRError& e) { requestSurfaceUpdate(); @@ -438,13 +442,13 @@ bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass, clipping.pipelineInfo.inputBindings.push_back( vk::VertexInputBindingDescription() .setBinding(0) - .setStride(VertexAttribute::getStrideOf(ShaderClass::attributes[0].dataType)) + .setStride(static_cast(VertexAttribute::getStrideOf(ShaderClass::attributes[0].dataType))) .setInputRate(vk::VertexInputRate::eVertex)); clipping.pipelineInfo.inputAttributes.push_back( vk::VertexInputAttributeDescription() .setBinding(0) - .setLocation(ShaderClass::attributes[0].index) + .setLocation(static_cast(ShaderClass::attributes[0].index)) .setFormat(PipelineInfo::vulkanFormat(ShaderClass::attributes[0].dataType))); } @@ -521,7 +525,7 @@ const vk::UniqueDescriptorSetLayout& Context::getUniformDescriptorSetLayout() { for (size_t i = 0; i < shaders::maxUBOCountPerShader; ++i) { bindings.push_back(vk::DescriptorSetLayoutBinding() - .setBinding(i) + .setBinding(static_cast(i)) .setStageFlags(stageFlags) .setDescriptorType(vk::DescriptorType::eUniformBuffer) .setDescriptorCount(1)); @@ -542,7 +546,7 @@ const vk::UniqueDescriptorSetLayout& Context::getImageDescriptorSetLayout() { for (size_t i = 0; i < shaders::maxTextureCountPerShader; ++i) { bindings.push_back(vk::DescriptorSetLayoutBinding() - .setBinding(i) + .setBinding(static_cast(i)) .setStageFlags(vk::ShaderStageFlagBits::eFragment) .setDescriptorType(vk::DescriptorType::eCombinedImageSampler) .setDescriptorCount(1)); diff --git a/src/mbgl/vulkan/drawable.cpp b/src/mbgl/vulkan/drawable.cpp index b310edf8f..9593496e7 100644 --- a/src/mbgl/vulkan/drawable.cpp +++ b/src/mbgl/vulkan/drawable.cpp @@ -269,7 +269,7 @@ void Drawable::draw(PaintParameters& parameters) const { impl->pipelineInfo.setRenderable(renderPass_.getDescriptor().renderable); - const uint32_t instances = instanceAttributes ? instanceAttributes->getMaxCount() : 1; + const auto instances = instanceAttributes ? instanceAttributes->getMaxCount() : 1; for (const auto& seg : impl->segments) { const auto& segment = seg->getSegment(); @@ -283,9 +283,16 @@ void Drawable::draw(PaintParameters& parameters) const { commandBuffer->bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline.get()); if (segment.indexLength) { - commandBuffer->drawIndexed(segment.indexLength, instances, segment.indexOffset, segment.vertexOffset, 0); + commandBuffer->drawIndexed(static_cast(segment.indexLength), + static_cast(instances), + static_cast(segment.indexOffset), + static_cast(segment.vertexOffset), + 0); } else { - commandBuffer->draw(segment.vertexLength, instances, segment.vertexOffset, 0); + commandBuffer->draw(static_cast(segment.vertexLength), + static_cast(instances), + static_cast(segment.vertexOffset), + 0); } context.renderingStats().numDrawCalls++; @@ -344,7 +351,7 @@ void Drawable::buildVulkanInputBindings() noexcept { const auto& buffer = vertexBuffer->get(); const auto& buffIt = std::find(uniqueBuffers.begin(), uniqueBuffers.end(), binding->vertexBufferResource); - uint32_t bindingIndex = 0; + std::size_t bindingIndex = 0; if (buffIt == uniqueBuffers.end()) { bindingIndex = impl->pipelineInfo.inputBindings.size(); @@ -353,7 +360,7 @@ void Drawable::buildVulkanInputBindings() noexcept { // add new buffer binding impl->pipelineInfo.inputBindings.push_back(vk::VertexInputBindingDescription() - .setBinding(bindingIndex) + .setBinding(static_cast(bindingIndex)) .setStride(binding->vertexStride) .setInputRate(inputRate)); @@ -365,8 +372,8 @@ void Drawable::buildVulkanInputBindings() noexcept { impl->pipelineInfo.inputAttributes.push_back( vk::VertexInputAttributeDescription() - .setBinding(bindingIndex) - .setLocation(i) + .setBinding(static_cast(bindingIndex)) + .setLocation(static_cast(i)) .setFormat(PipelineInfo::vulkanFormat(binding->attribute.dataType)) .setOffset(binding->attribute.offset)); } @@ -431,7 +438,7 @@ bool Drawable::bindDescriptors(CommandEncoder& encoder) const noexcept { .setBufferInfo(descriptorBufferInfo) .setDescriptorCount(1) .setDescriptorType(vk::DescriptorType::eUniformBuffer) - .setDstBinding(id) + .setDstBinding(static_cast(id)) .setDstSet(uniformDescriptorSet); device->updateDescriptorSets(writeDescriptorSet, nullptr); @@ -460,7 +467,7 @@ bool Drawable::bindDescriptors(CommandEncoder& encoder) const noexcept { .setImageInfo(descriptorImageInfo) .setDescriptorCount(1) .setDescriptorType(vk::DescriptorType::eCombinedImageSampler) - .setDstBinding(id) + .setDstBinding(static_cast(id)) .setDstSet(imageDescriptorSet); device->updateDescriptorSets(writeDescriptorSet, nullptr); diff --git a/src/mbgl/vulkan/texture2d.cpp b/src/mbgl/vulkan/texture2d.cpp index a83351338..bd7210645 100644 --- a/src/mbgl/vulkan/texture2d.cpp +++ b/src/mbgl/vulkan/texture2d.cpp @@ -496,7 +496,7 @@ std::shared_ptr Texture2D::readImage() { if (imageSize == layout.arrayPitch) { memcpy(imageData->data.get(), mappedData, imageSize); } else { - uint32_t rowSize = size.width * getPixelStride(); + auto rowSize = static_cast(size.width * getPixelStride()); for (uint32_t i = 0; i < size.height; ++i) { memcpy(imageData->data.get() + rowSize * i, mappedData + layout.rowPitch * i, rowSize); }