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