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