Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // SurfaceVk.cpp: |
| 7 | // Implements the class methods for SurfaceVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/SurfaceVk.h" |
| 11 | |
| 12 | #include "common/debug.h" |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 13 | #include "libANGLE/Surface.h" |
| 14 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 15 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
| 16 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | abaab23 | 2017-01-10 12:37:37 -0500 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/formatutilsvk.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 18 | |
| 19 | namespace rx |
| 20 | { |
| 21 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 22 | namespace |
| 23 | { |
| 24 | |
Jamie Madill | abaab23 | 2017-01-10 12:37:37 -0500 | [diff] [blame] | 25 | const vk::Format &GetVkFormatFromConfig(const egl::Config &config) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 26 | { |
| 27 | // TODO(jmadill): Properly handle format interpretation. |
Jamie Madill | abaab23 | 2017-01-10 12:37:37 -0500 | [diff] [blame] | 28 | return vk::Format::Get(GL_BGRA8_EXT); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 29 | } |
| 30 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 31 | VkPresentModeKHR GetDesiredPresentMode(const std::vector<VkPresentModeKHR> &presentModes, |
| 32 | EGLint minSwapInterval, |
| 33 | EGLint maxSwapInterval) |
| 34 | { |
| 35 | ASSERT(!presentModes.empty()); |
| 36 | |
| 37 | // Use FIFO mode for v-sync, since it throttles you to the display rate. Mailbox is more |
| 38 | // similar to triple-buffering. For now we hard-code Mailbox for perf tseting. |
| 39 | // TODO(jmadill): Properly select present mode and re-create display if changed. |
| 40 | VkPresentModeKHR bestChoice = VK_PRESENT_MODE_MAILBOX_KHR; |
| 41 | |
| 42 | for (auto presentMode : presentModes) |
| 43 | { |
| 44 | if (presentMode == bestChoice) |
| 45 | { |
| 46 | return bestChoice; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | ERR() << "Desired present mode not available. Falling back to " << presentModes[0]; |
| 51 | return presentModes[0]; |
| 52 | } |
| 53 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 54 | } // namespace |
| 55 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 56 | OffscreenSurfaceVk::OffscreenSurfaceVk(const egl::SurfaceState &surfaceState, |
| 57 | EGLint width, |
| 58 | EGLint height) |
| 59 | : SurfaceImpl(surfaceState), mWidth(width), mHeight(height) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 63 | OffscreenSurfaceVk::~OffscreenSurfaceVk() |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 67 | egl::Error OffscreenSurfaceVk::initialize(const DisplayImpl *displayImpl) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 68 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 69 | return egl::Error(EGL_SUCCESS); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 72 | FramebufferImpl *OffscreenSurfaceVk::createDefaultFramebuffer(const gl::FramebufferState &state) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 73 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 74 | // Use a user FBO for an offscreen RT. |
| 75 | return FramebufferVk::CreateUserFBO(state); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 78 | egl::Error OffscreenSurfaceVk::swap(const DisplayImpl *displayImpl) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 79 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 80 | return egl::Error(EGL_SUCCESS); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 83 | egl::Error OffscreenSurfaceVk::postSubBuffer(EGLint /*x*/, |
| 84 | EGLint /*y*/, |
| 85 | EGLint /*width*/, |
| 86 | EGLint /*height*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 87 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 88 | return egl::Error(EGL_SUCCESS); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 91 | egl::Error OffscreenSurfaceVk::querySurfacePointerANGLE(EGLint /*attribute*/, void ** /*value*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 92 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 93 | UNREACHABLE(); |
| 94 | return egl::Error(EGL_BAD_CURRENT_SURFACE); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 97 | egl::Error OffscreenSurfaceVk::bindTexImage(gl::Texture * /*texture*/, EGLint /*buffer*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 98 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 99 | return egl::Error(EGL_SUCCESS); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 102 | egl::Error OffscreenSurfaceVk::releaseTexImage(EGLint /*buffer*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 103 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 104 | return egl::Error(EGL_SUCCESS); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 107 | egl::Error OffscreenSurfaceVk::getSyncValues(EGLuint64KHR * /*ust*/, |
| 108 | EGLuint64KHR * /*msc*/, |
| 109 | EGLuint64KHR * /*sbc*/) |
| 110 | { |
| 111 | UNIMPLEMENTED(); |
| 112 | return egl::Error(EGL_BAD_ACCESS); |
| 113 | } |
| 114 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 115 | void OffscreenSurfaceVk::setSwapInterval(EGLint /*interval*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 116 | { |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 117 | } |
| 118 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 119 | EGLint OffscreenSurfaceVk::getWidth() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 120 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 121 | return mWidth; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 124 | EGLint OffscreenSurfaceVk::getHeight() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 125 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 126 | return mHeight; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 129 | EGLint OffscreenSurfaceVk::isPostSubBufferSupported() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 130 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 131 | return EGL_FALSE; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 134 | EGLint OffscreenSurfaceVk::getSwapBehavior() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 135 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 136 | return EGL_BUFFER_PRESERVED; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 139 | gl::Error OffscreenSurfaceVk::getAttachmentRenderTarget( |
| 140 | const gl::FramebufferAttachment::Target & /*target*/, |
| 141 | FramebufferAttachmentRenderTarget ** /*rtOut*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 142 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 143 | UNREACHABLE(); |
| 144 | return gl::Error(GL_INVALID_OPERATION); |
| 145 | } |
| 146 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 147 | WindowSurfaceVk::WindowSurfaceVk(const egl::SurfaceState &surfaceState, |
| 148 | EGLNativeWindowType window, |
| 149 | EGLint width, |
| 150 | EGLint height) |
| 151 | : SurfaceImpl(surfaceState), |
| 152 | mNativeWindowType(window), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 153 | mSurface(VK_NULL_HANDLE), |
| 154 | mSwapchain(VK_NULL_HANDLE), |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 155 | mRenderTarget(), |
| 156 | mCurrentSwapchainImageIndex(0) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 157 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 158 | mRenderTarget.extents.width = static_cast<GLint>(width); |
| 159 | mRenderTarget.extents.height = static_cast<GLint>(height); |
| 160 | mRenderTarget.extents.depth = 1; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 161 | mRenderTarget.resource = this; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | WindowSurfaceVk::~WindowSurfaceVk() |
| 165 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 166 | ASSERT(mSurface == VK_NULL_HANDLE); |
| 167 | ASSERT(mSwapchain == VK_NULL_HANDLE); |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void WindowSurfaceVk::destroy(const DisplayImpl *displayImpl) |
| 171 | { |
| 172 | const DisplayVk *displayVk = GetAs<DisplayVk>(displayImpl); |
| 173 | RendererVk *rendererVk = displayVk->getRenderer(); |
| 174 | VkDevice device = rendererVk->getDevice(); |
| 175 | VkInstance instance = rendererVk->getInstance(); |
| 176 | |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame^] | 177 | rendererVk->finish(); |
| 178 | |
| 179 | mImageAvailableSemaphore.destroy(device); |
| 180 | mRenderingCompleteSemaphore.destroy(device); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 181 | |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 182 | for (auto &imageView : mSwapchainImageViews) |
| 183 | { |
| 184 | imageView.destroy(device); |
| 185 | } |
| 186 | |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 187 | // Although we don't own the swapchain image handles, we need to keep our shutdown clean. |
| 188 | for (auto &image : mSwapchainImages) |
| 189 | { |
| 190 | image.reset(); |
| 191 | } |
| 192 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 193 | for (auto &framebuffer : mSwapchainFramebuffers) |
| 194 | { |
| 195 | framebuffer.destroy(device); |
| 196 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 197 | |
| 198 | if (mSwapchain) |
| 199 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 200 | vkDestroySwapchainKHR(device, mSwapchain, nullptr); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 201 | mSwapchain = VK_NULL_HANDLE; |
| 202 | } |
| 203 | |
| 204 | if (mSurface) |
| 205 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 206 | vkDestroySurfaceKHR(instance, mSurface, nullptr); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 207 | mSurface = VK_NULL_HANDLE; |
| 208 | } |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | egl::Error WindowSurfaceVk::initialize(const DisplayImpl *displayImpl) |
| 212 | { |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 213 | const DisplayVk *displayVk = GetAs<DisplayVk>(displayImpl); |
| 214 | return initializeImpl(displayVk->getRenderer()).toEGL(EGL_BAD_SURFACE); |
| 215 | } |
| 216 | |
| 217 | vk::Error WindowSurfaceVk::initializeImpl(RendererVk *renderer) |
| 218 | { |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 219 | // TODO(jmadill): Make this platform-specific. |
| 220 | VkWin32SurfaceCreateInfoKHR createInfo; |
| 221 | |
| 222 | createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; |
| 223 | createInfo.pNext = nullptr; |
| 224 | createInfo.flags = 0; |
| 225 | createInfo.hinstance = GetModuleHandle(nullptr); |
| 226 | createInfo.hwnd = mNativeWindowType; |
| 227 | ANGLE_VK_TRY(vkCreateWin32SurfaceKHR(renderer->getInstance(), &createInfo, nullptr, &mSurface)); |
| 228 | |
| 229 | uint32_t presentQueue = 0; |
| 230 | ANGLE_TRY_RESULT(renderer->selectPresentQueueForSurface(mSurface), presentQueue); |
| 231 | |
| 232 | const auto &physicalDevice = renderer->getPhysicalDevice(); |
| 233 | |
| 234 | VkSurfaceCapabilitiesKHR surfaceCaps; |
| 235 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, mSurface, &surfaceCaps)); |
| 236 | |
| 237 | // Adjust width and height to the swapchain if necessary. |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 238 | uint32_t width = surfaceCaps.currentExtent.width; |
| 239 | uint32_t height = surfaceCaps.currentExtent.height; |
| 240 | |
| 241 | // TODO(jmadill): Support devices which don't support copy. We use this for ReadPixels. |
| 242 | ANGLE_VK_CHECK((surfaceCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) != 0, |
| 243 | VK_ERROR_INITIALIZATION_FAILED); |
| 244 | |
| 245 | if (surfaceCaps.currentExtent.width == 0xFFFFFFFFu) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 246 | { |
| 247 | ASSERT(surfaceCaps.currentExtent.height == 0xFFFFFFFFu); |
| 248 | |
| 249 | RECT rect; |
| 250 | ANGLE_VK_CHECK(GetClientRect(mNativeWindowType, &rect) == TRUE, |
| 251 | VK_ERROR_INITIALIZATION_FAILED); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 252 | if (mRenderTarget.extents.width == 0) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 253 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 254 | width = static_cast<uint32_t>(rect.right - rect.left); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 255 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 256 | if (mRenderTarget.extents.height == 0) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 257 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 258 | height = static_cast<uint32_t>(rect.bottom - rect.top); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 262 | mRenderTarget.extents.width = static_cast<int>(width); |
| 263 | mRenderTarget.extents.height = static_cast<int>(height); |
| 264 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 265 | uint32_t presentModeCount = 0; |
| 266 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, mSurface, |
| 267 | &presentModeCount, nullptr)); |
| 268 | ASSERT(presentModeCount > 0); |
| 269 | |
| 270 | std::vector<VkPresentModeKHR> presentModes(presentModeCount); |
| 271 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, mSurface, |
| 272 | &presentModeCount, presentModes.data())); |
| 273 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 274 | // Select appropriate present mode based on vsync parameter. |
| 275 | // TODO(jmadill): More complete implementation, which allows for changing and more values. |
| 276 | const EGLint minSwapInterval = mState.config->minSwapInterval; |
| 277 | const EGLint maxSwapInterval = mState.config->maxSwapInterval; |
| 278 | ASSERT(minSwapInterval == 0 || minSwapInterval == 1); |
| 279 | ASSERT(maxSwapInterval == 0 || maxSwapInterval == 1); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 280 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 281 | VkPresentModeKHR swapchainPresentMode = |
| 282 | GetDesiredPresentMode(presentModes, minSwapInterval, maxSwapInterval); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 283 | |
| 284 | // Determine number of swapchain images. Aim for one more than the minimum. |
| 285 | uint32_t minImageCount = surfaceCaps.minImageCount + 1; |
| 286 | if (surfaceCaps.maxImageCount > 0 && minImageCount > surfaceCaps.maxImageCount) |
| 287 | { |
| 288 | minImageCount = surfaceCaps.maxImageCount; |
| 289 | } |
| 290 | |
| 291 | // Default to identity transform. |
| 292 | VkSurfaceTransformFlagBitsKHR preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 293 | if ((surfaceCaps.supportedTransforms & preTransform) == 0) |
| 294 | { |
| 295 | preTransform = surfaceCaps.currentTransform; |
| 296 | } |
| 297 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 298 | uint32_t surfaceFormatCount = 0; |
| 299 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &surfaceFormatCount, |
| 300 | nullptr)); |
| 301 | |
| 302 | std::vector<VkSurfaceFormatKHR> surfaceFormats(surfaceFormatCount); |
| 303 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &surfaceFormatCount, |
| 304 | surfaceFormats.data())); |
| 305 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 306 | mRenderTarget.format = &GetVkFormatFromConfig(*mState.config); |
| 307 | auto nativeFormat = mRenderTarget.format->native; |
| 308 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 309 | if (surfaceFormatCount == 1u && surfaceFormats[0].format == VK_FORMAT_UNDEFINED) |
| 310 | { |
| 311 | // This is fine. |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | bool foundFormat = false; |
| 316 | for (const auto &surfaceFormat : surfaceFormats) |
| 317 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 318 | if (surfaceFormat.format == nativeFormat) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 319 | { |
| 320 | foundFormat = true; |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | ANGLE_VK_CHECK(foundFormat, VK_ERROR_INITIALIZATION_FAILED); |
| 326 | } |
| 327 | |
| 328 | VkSwapchainCreateInfoKHR swapchainInfo; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 329 | swapchainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 330 | swapchainInfo.pNext = nullptr; |
| 331 | swapchainInfo.flags = 0; |
| 332 | swapchainInfo.surface = mSurface; |
| 333 | swapchainInfo.minImageCount = minImageCount; |
| 334 | swapchainInfo.imageFormat = nativeFormat; |
| 335 | swapchainInfo.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
| 336 | swapchainInfo.imageExtent.width = width; |
| 337 | swapchainInfo.imageExtent.height = height; |
| 338 | swapchainInfo.imageArrayLayers = 1; |
| 339 | swapchainInfo.imageUsage = (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 340 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 341 | swapchainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 342 | swapchainInfo.queueFamilyIndexCount = 0; |
| 343 | swapchainInfo.pQueueFamilyIndices = nullptr; |
| 344 | swapchainInfo.preTransform = preTransform; |
| 345 | swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
| 346 | swapchainInfo.presentMode = swapchainPresentMode; |
| 347 | swapchainInfo.clipped = VK_TRUE; |
| 348 | swapchainInfo.oldSwapchain = VK_NULL_HANDLE; |
| 349 | |
| 350 | const auto &device = renderer->getDevice(); |
| 351 | ANGLE_VK_TRY(vkCreateSwapchainKHR(device, &swapchainInfo, nullptr, &mSwapchain)); |
| 352 | |
| 353 | // Intialize the swapchain image views. |
| 354 | uint32_t imageCount = 0; |
| 355 | ANGLE_VK_TRY(vkGetSwapchainImagesKHR(device, mSwapchain, &imageCount, nullptr)); |
| 356 | |
| 357 | std::vector<VkImage> swapchainImages(imageCount); |
| 358 | ANGLE_VK_TRY(vkGetSwapchainImagesKHR(device, mSwapchain, &imageCount, swapchainImages.data())); |
| 359 | |
| 360 | // CommandBuffer is a singleton in the Renderer. |
| 361 | vk::CommandBuffer *commandBuffer = renderer->getCommandBuffer(); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 362 | ANGLE_TRY(commandBuffer->begin(device)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 363 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 364 | VkClearColorValue transparentBlack; |
| 365 | transparentBlack.float32[0] = 0.0f; |
| 366 | transparentBlack.float32[1] = 0.0f; |
| 367 | transparentBlack.float32[2] = 0.0f; |
| 368 | transparentBlack.float32[3] = 0.0f; |
| 369 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 370 | mSwapchainImages.resize(imageCount); |
| 371 | mSwapchainImageViews.resize(imageCount); |
| 372 | |
| 373 | for (uint32_t imageIndex = 0; imageIndex < imageCount; ++imageIndex) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 374 | { |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 375 | VkImage swapchainImage = swapchainImages[imageIndex]; |
| 376 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 377 | VkImageViewCreateInfo imageViewInfo; |
| 378 | imageViewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 379 | imageViewInfo.pNext = nullptr; |
| 380 | imageViewInfo.flags = 0; |
| 381 | imageViewInfo.image = swapchainImage; |
| 382 | imageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 383 | imageViewInfo.format = nativeFormat; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 384 | imageViewInfo.components.r = VK_COMPONENT_SWIZZLE_R; |
| 385 | imageViewInfo.components.g = VK_COMPONENT_SWIZZLE_G; |
| 386 | imageViewInfo.components.b = VK_COMPONENT_SWIZZLE_B; |
| 387 | imageViewInfo.components.a = VK_COMPONENT_SWIZZLE_A; |
| 388 | imageViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 389 | imageViewInfo.subresourceRange.baseMipLevel = 0; |
| 390 | imageViewInfo.subresourceRange.levelCount = 1; |
| 391 | imageViewInfo.subresourceRange.baseArrayLayer = 0; |
| 392 | imageViewInfo.subresourceRange.layerCount = 1; |
| 393 | |
| 394 | vk::Image image(swapchainImage); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 395 | vk::ImageView imageView; |
| 396 | ANGLE_TRY(imageView.init(device, imageViewInfo)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 397 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 398 | // Set transfer dest layout, and clear the image to black. |
| 399 | image.changeLayoutTop(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 400 | commandBuffer); |
| 401 | commandBuffer->clearSingleColorImage(image, transparentBlack); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 402 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 403 | mSwapchainImages[imageIndex].retain(device, std::move(image)); |
| 404 | mSwapchainImageViews[imageIndex].retain(device, std::move(imageView)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | ANGLE_TRY(commandBuffer->end()); |
| 408 | ANGLE_TRY(renderer->submitAndFinishCommandBuffer(*commandBuffer)); |
| 409 | |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame^] | 410 | ANGLE_TRY(mImageAvailableSemaphore.init(device)); |
| 411 | ANGLE_TRY(mRenderingCompleteSemaphore.init(device)); |
| 412 | |
| 413 | // Get the first available swapchain iamge. |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 414 | ANGLE_TRY(nextSwapchainImage(renderer)); |
| 415 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 416 | return vk::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | FramebufferImpl *WindowSurfaceVk::createDefaultFramebuffer(const gl::FramebufferState &state) |
| 420 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 421 | return FramebufferVk::CreateDefaultFBO(state, this); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | egl::Error WindowSurfaceVk::swap(const DisplayImpl *displayImpl) |
| 425 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 426 | const DisplayVk *displayVk = GetAs<DisplayVk>(displayImpl); |
| 427 | return swapImpl(displayVk->getRenderer()).toEGL(EGL_BAD_ALLOC); |
| 428 | } |
| 429 | |
| 430 | vk::Error WindowSurfaceVk::swapImpl(RendererVk *renderer) |
| 431 | { |
| 432 | vk::CommandBuffer *currentCB = renderer->getCommandBuffer(); |
| 433 | |
| 434 | auto *image = &mSwapchainImages[mCurrentSwapchainImageIndex]; |
| 435 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 436 | currentCB->begin(renderer->getDevice()); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 437 | image->changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
| 438 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 439 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, currentCB); |
| 440 | currentCB->end(); |
| 441 | |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame^] | 442 | ANGLE_TRY(renderer->submitCommandsWithSync(*currentCB, mImageAvailableSemaphore, |
| 443 | mRenderingCompleteSemaphore)); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 444 | |
| 445 | VkPresentInfoKHR presentInfo; |
| 446 | presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; |
| 447 | presentInfo.pNext = nullptr; |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame^] | 448 | presentInfo.waitSemaphoreCount = 1; |
| 449 | presentInfo.pWaitSemaphores = mRenderingCompleteSemaphore.ptr(); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 450 | presentInfo.swapchainCount = 1; |
| 451 | presentInfo.pSwapchains = &mSwapchain; |
| 452 | presentInfo.pImageIndices = &mCurrentSwapchainImageIndex; |
| 453 | presentInfo.pResults = nullptr; |
| 454 | |
| 455 | ANGLE_VK_TRY(vkQueuePresentKHR(renderer->getQueue(), &presentInfo)); |
| 456 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 457 | // Get the next available swapchain image. |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 458 | ANGLE_TRY(nextSwapchainImage(renderer)); |
| 459 | |
| 460 | return vk::NoError(); |
| 461 | } |
| 462 | |
| 463 | vk::Error WindowSurfaceVk::nextSwapchainImage(RendererVk *renderer) |
| 464 | { |
| 465 | VkDevice device = renderer->getDevice(); |
| 466 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 467 | ANGLE_VK_TRY(vkAcquireNextImageKHR(device, mSwapchain, std::numeric_limits<uint64_t>::max(), |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame^] | 468 | mImageAvailableSemaphore.getHandle(), VK_NULL_HANDLE, |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 469 | &mCurrentSwapchainImageIndex)); |
| 470 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 471 | // Update RenderTarget pointers. |
| 472 | mRenderTarget.image = &mSwapchainImages[mCurrentSwapchainImageIndex]; |
| 473 | mRenderTarget.imageView = &mSwapchainImageViews[mCurrentSwapchainImageIndex]; |
| 474 | |
| 475 | return vk::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | egl::Error WindowSurfaceVk::postSubBuffer(EGLint x, EGLint y, EGLint width, EGLint height) |
| 479 | { |
| 480 | // TODO(jmadill) |
| 481 | return egl::Error(EGL_SUCCESS); |
| 482 | } |
| 483 | |
| 484 | egl::Error WindowSurfaceVk::querySurfacePointerANGLE(EGLint attribute, void **value) |
| 485 | { |
| 486 | UNREACHABLE(); |
| 487 | return egl::Error(EGL_BAD_CURRENT_SURFACE); |
| 488 | } |
| 489 | |
| 490 | egl::Error WindowSurfaceVk::bindTexImage(gl::Texture *texture, EGLint buffer) |
| 491 | { |
| 492 | return egl::Error(EGL_SUCCESS); |
| 493 | } |
| 494 | |
| 495 | egl::Error WindowSurfaceVk::releaseTexImage(EGLint buffer) |
| 496 | { |
| 497 | return egl::Error(EGL_SUCCESS); |
| 498 | } |
| 499 | |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 500 | egl::Error WindowSurfaceVk::getSyncValues(EGLuint64KHR * /*ust*/, |
| 501 | EGLuint64KHR * /*msc*/, |
| 502 | EGLuint64KHR * /*sbc*/) |
| 503 | { |
| 504 | UNIMPLEMENTED(); |
| 505 | return egl::Error(EGL_BAD_ACCESS); |
| 506 | } |
| 507 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 508 | void WindowSurfaceVk::setSwapInterval(EGLint interval) |
| 509 | { |
| 510 | } |
| 511 | |
| 512 | EGLint WindowSurfaceVk::getWidth() const |
| 513 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 514 | return static_cast<EGLint>(mRenderTarget.extents.width); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | EGLint WindowSurfaceVk::getHeight() const |
| 518 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 519 | return static_cast<EGLint>(mRenderTarget.extents.height); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | EGLint WindowSurfaceVk::isPostSubBufferSupported() const |
| 523 | { |
| 524 | // TODO(jmadill) |
| 525 | return EGL_FALSE; |
| 526 | } |
| 527 | |
| 528 | EGLint WindowSurfaceVk::getSwapBehavior() const |
| 529 | { |
| 530 | // TODO(jmadill) |
| 531 | return EGL_BUFFER_DESTROYED; |
| 532 | } |
| 533 | |
| 534 | gl::Error WindowSurfaceVk::getAttachmentRenderTarget( |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 535 | const gl::FramebufferAttachment::Target & /*target*/, |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 536 | FramebufferAttachmentRenderTarget **rtOut) |
| 537 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 538 | *rtOut = &mRenderTarget; |
| 539 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 542 | gl::ErrorOrResult<vk::Framebuffer *> WindowSurfaceVk::getCurrentFramebuffer( |
| 543 | VkDevice device, |
| 544 | const vk::RenderPass &compatibleRenderPass) |
| 545 | { |
| 546 | if (!mSwapchainFramebuffers.empty()) |
| 547 | { |
| 548 | // Validation layers should detect if the render pass is really compatible. |
| 549 | return &mSwapchainFramebuffers[mCurrentSwapchainImageIndex]; |
| 550 | } |
| 551 | |
| 552 | VkFramebufferCreateInfo framebufferInfo; |
| 553 | |
| 554 | // TODO(jmadill): Depth/Stencil attachments. |
| 555 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 556 | framebufferInfo.pNext = nullptr; |
| 557 | framebufferInfo.flags = 0; |
| 558 | framebufferInfo.renderPass = compatibleRenderPass.getHandle(); |
| 559 | framebufferInfo.attachmentCount = 1u; |
| 560 | framebufferInfo.pAttachments = nullptr; |
| 561 | framebufferInfo.width = static_cast<uint32_t>(mRenderTarget.extents.width); |
| 562 | framebufferInfo.height = static_cast<uint32_t>(mRenderTarget.extents.height); |
| 563 | framebufferInfo.layers = 1; |
| 564 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 565 | mSwapchainFramebuffers.resize(mSwapchainImageViews.size()); |
| 566 | for (size_t imageIndex = 0; imageIndex < mSwapchainFramebuffers.size(); ++imageIndex) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 567 | { |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 568 | const auto &imageView = mSwapchainImageViews[imageIndex]; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 569 | VkImageView imageViewHandle = imageView.getHandle(); |
| 570 | framebufferInfo.pAttachments = &imageViewHandle; |
| 571 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 572 | vk::Framebuffer framebuffer; |
| 573 | ANGLE_TRY(framebuffer.init(device, framebufferInfo)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 574 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 575 | mSwapchainFramebuffers[imageIndex].retain(device, std::move(framebuffer)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // We should only initialize framebuffers on the first swap. |
| 579 | ASSERT(mCurrentSwapchainImageIndex == 0u); |
| 580 | return &mSwapchainFramebuffers[mCurrentSwapchainImageIndex]; |
| 581 | } |
| 582 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 583 | } // namespace rx |