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