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