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 | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 13 | #include "libANGLE/Context.h" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 14 | #include "libANGLE/Display.h" |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 15 | #include "libANGLE/Surface.h" |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 18 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
| 19 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 21 | #include "third_party/trace_event/trace_event.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 22 | |
| 23 | namespace rx |
| 24 | { |
| 25 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 26 | namespace |
| 27 | { |
| 28 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 29 | VkPresentModeKHR GetDesiredPresentMode(const std::vector<VkPresentModeKHR> &presentModes, |
| 30 | EGLint minSwapInterval, |
| 31 | EGLint maxSwapInterval) |
| 32 | { |
| 33 | ASSERT(!presentModes.empty()); |
| 34 | |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 35 | // Use FIFO mode for v-sync, since it throttles you to the display rate. |
| 36 | // |
| 37 | // However, for performance testing (for now), we want to issue draws as fast as possible so we |
| 38 | // use either of the following, if available, in order specified here: |
| 39 | // |
| 40 | // - Mailbox is similar to triple-buffering. |
| 41 | // - Immediate is similar to single-buffering. |
| 42 | // |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 43 | // TODO(jmadill): Properly select present mode and re-create display if changed. |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 44 | bool mailboxAvailable = false; |
| 45 | bool immediateAvailable = false; |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 46 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 47 | for (VkPresentModeKHR presentMode : presentModes) |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 48 | { |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 49 | switch (presentMode) |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 50 | { |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 51 | case VK_PRESENT_MODE_MAILBOX_KHR: |
| 52 | mailboxAvailable = true; |
| 53 | break; |
| 54 | case VK_PRESENT_MODE_IMMEDIATE_KHR: |
| 55 | immediateAvailable = true; |
| 56 | break; |
| 57 | default: |
| 58 | break; |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 62 | // Note that VK_PRESENT_MODE_FIFO_KHR is guaranteed to be available. |
| 63 | return mailboxAvailable |
| 64 | ? VK_PRESENT_MODE_MAILBOX_KHR |
| 65 | : immediateAvailable ? VK_PRESENT_MODE_IMMEDIATE_KHR : VK_PRESENT_MODE_FIFO_KHR; |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 68 | constexpr VkImageUsageFlags kSurfaceVKImageUsageFlags = |
| 69 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 70 | constexpr VkImageUsageFlags kSurfaceVKColorImageUsageFlags = |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 71 | kSurfaceVKImageUsageFlags | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 72 | constexpr VkImageUsageFlags kSurfaceVKDepthStencilImageUsageFlags = |
| 73 | kSurfaceVKImageUsageFlags | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 74 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 75 | } // namespace |
| 76 | |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame] | 77 | OffscreenSurfaceVk::AttachmentImage::AttachmentImage() |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 78 | { |
| 79 | renderTarget.init(&image, &imageView, 0, nullptr); |
| 80 | } |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 81 | |
| 82 | OffscreenSurfaceVk::AttachmentImage::~AttachmentImage() = default; |
| 83 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 84 | angle::Result OffscreenSurfaceVk::AttachmentImage::initialize(DisplayVk *displayVk, |
| 85 | EGLint width, |
| 86 | EGLint height, |
| 87 | const vk::Format &vkFormat) |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 88 | { |
Geoff Lang | 38971fd | 2018-06-28 15:19:18 -0400 | [diff] [blame] | 89 | RendererVk *renderer = displayVk->getRenderer(); |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 90 | |
| 91 | const angle::Format &textureFormat = vkFormat.textureFormat(); |
| 92 | bool isDepthOrStencilFormat = textureFormat.depthBits > 0 || textureFormat.stencilBits > 0; |
| 93 | const VkImageUsageFlags usage = isDepthOrStencilFormat ? kSurfaceVKDepthStencilImageUsageFlags |
| 94 | : kSurfaceVKColorImageUsageFlags; |
| 95 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 96 | gl::Extents extents(std::max(static_cast<int>(width), 1), std::max(static_cast<int>(height), 1), |
| 97 | 1); |
Shahbaz Youssefi | b5ba549 | 2019-01-02 15:19:22 -0500 | [diff] [blame] | 98 | ANGLE_TRY(image.init(displayVk, gl::TextureType::_2D, extents, vkFormat, 1, usage, 1, 1)); |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 99 | |
| 100 | VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 101 | ANGLE_TRY(image.initMemory(displayVk, renderer->getMemoryProperties(), flags)); |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 102 | |
| 103 | VkImageAspectFlags aspect = vk::GetFormatAspectFlags(textureFormat); |
| 104 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 105 | ANGLE_TRY(image.initImageView(displayVk, gl::TextureType::_2D, aspect, gl::SwizzleState(), |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 106 | &imageView, 1)); |
| 107 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 108 | return angle::Result::Continue; |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 111 | void OffscreenSurfaceVk::AttachmentImage::destroy(const egl::Display *display) |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 112 | { |
| 113 | const DisplayVk *displayVk = vk::GetImpl(display); |
| 114 | RendererVk *renderer = displayVk->getRenderer(); |
| 115 | |
Geoff Lang | 08f8fa6 | 2019-01-10 14:25:47 -0500 | [diff] [blame] | 116 | image.releaseImage(renderer); |
| 117 | image.releaseStagingBuffer(renderer); |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 118 | renderer->releaseObject(renderer->getCurrentQueueSerial(), &imageView); |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 121 | OffscreenSurfaceVk::OffscreenSurfaceVk(const egl::SurfaceState &surfaceState, |
| 122 | EGLint width, |
| 123 | EGLint height) |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 124 | : SurfaceImpl(surfaceState), mWidth(width), mHeight(height) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 125 | {} |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 126 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 127 | OffscreenSurfaceVk::~OffscreenSurfaceVk() {} |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 128 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 129 | egl::Error OffscreenSurfaceVk::initialize(const egl::Display *display) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 130 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 131 | DisplayVk *displayVk = vk::GetImpl(display); |
| 132 | angle::Result result = initializeImpl(displayVk); |
| 133 | return angle::ToEGL(result, displayVk, EGL_BAD_SURFACE); |
| 134 | } |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 135 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 136 | angle::Result OffscreenSurfaceVk::initializeImpl(DisplayVk *displayVk) |
| 137 | { |
| 138 | RendererVk *renderer = displayVk->getRenderer(); |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 139 | const egl::Config *config = mState.config; |
| 140 | |
| 141 | if (config->renderTargetFormat != GL_NONE) |
| 142 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 143 | ANGLE_TRY(mColorAttachment.initialize(displayVk, mWidth, mHeight, |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 144 | renderer->getFormat(config->renderTargetFormat))); |
| 145 | } |
| 146 | |
| 147 | if (config->depthStencilFormat != GL_NONE) |
| 148 | { |
| 149 | ANGLE_TRY(mDepthStencilAttachment.initialize( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 150 | displayVk, mWidth, mHeight, renderer->getFormat(config->depthStencilFormat))); |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 153 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 156 | void OffscreenSurfaceVk::destroy(const egl::Display *display) |
| 157 | { |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 158 | mColorAttachment.destroy(display); |
| 159 | mDepthStencilAttachment.destroy(display); |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Geoff Lang | bf7b95d | 2018-05-01 16:48:21 -0400 | [diff] [blame] | 162 | FramebufferImpl *OffscreenSurfaceVk::createDefaultFramebuffer(const gl::Context *context, |
| 163 | const gl::FramebufferState &state) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 164 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 165 | RendererVk *renderer = vk::GetImpl(context)->getRenderer(); |
| 166 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 167 | // Use a user FBO for an offscreen RT. |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 168 | return FramebufferVk::CreateUserFBO(renderer, state); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 171 | egl::Error OffscreenSurfaceVk::swap(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 172 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 173 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 176 | egl::Error OffscreenSurfaceVk::postSubBuffer(const gl::Context * /*context*/, |
| 177 | EGLint /*x*/, |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 178 | EGLint /*y*/, |
| 179 | EGLint /*width*/, |
| 180 | EGLint /*height*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 181 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 182 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 185 | egl::Error OffscreenSurfaceVk::querySurfacePointerANGLE(EGLint /*attribute*/, void ** /*value*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 186 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 187 | UNREACHABLE(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 188 | return egl::EglBadCurrentSurface(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Geoff Lang | ccafa62 | 2018-05-02 13:07:53 -0400 | [diff] [blame] | 191 | egl::Error OffscreenSurfaceVk::bindTexImage(const gl::Context * /*context*/, |
| 192 | gl::Texture * /*texture*/, |
| 193 | EGLint /*buffer*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 194 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 195 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Geoff Lang | ccafa62 | 2018-05-02 13:07:53 -0400 | [diff] [blame] | 198 | egl::Error OffscreenSurfaceVk::releaseTexImage(const gl::Context * /*context*/, EGLint /*buffer*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 199 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 200 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 203 | egl::Error OffscreenSurfaceVk::getSyncValues(EGLuint64KHR * /*ust*/, |
| 204 | EGLuint64KHR * /*msc*/, |
| 205 | EGLuint64KHR * /*sbc*/) |
| 206 | { |
| 207 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 208 | return egl::EglBadAccess(); |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 211 | void OffscreenSurfaceVk::setSwapInterval(EGLint /*interval*/) {} |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 212 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 213 | EGLint OffscreenSurfaceVk::getWidth() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 214 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 215 | return mWidth; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 218 | EGLint OffscreenSurfaceVk::getHeight() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 219 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 220 | return mHeight; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 223 | EGLint OffscreenSurfaceVk::isPostSubBufferSupported() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 224 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 225 | return EGL_FALSE; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 228 | EGLint OffscreenSurfaceVk::getSwapBehavior() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 229 | { |
Tim Van Patten | c2ee2cc | 2019-01-11 10:39:15 -0700 | [diff] [blame] | 230 | return EGL_BUFFER_DESTROYED; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 233 | angle::Result OffscreenSurfaceVk::getAttachmentRenderTarget( |
| 234 | const gl::Context *context, |
| 235 | GLenum binding, |
| 236 | const gl::ImageIndex &imageIndex, |
| 237 | FramebufferAttachmentRenderTarget **rtOut) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 238 | { |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 239 | if (binding == GL_BACK) |
| 240 | { |
| 241 | *rtOut = &mColorAttachment.renderTarget; |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | ASSERT(binding == GL_DEPTH || binding == GL_STENCIL || binding == GL_DEPTH_STENCIL); |
| 246 | *rtOut = &mDepthStencilAttachment.renderTarget; |
| 247 | } |
| 248 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 249 | return angle::Result::Continue; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 252 | angle::Result OffscreenSurfaceVk::initializeContents(const gl::Context *context, |
| 253 | const gl::ImageIndex &imageIndex) |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 254 | { |
| 255 | UNIMPLEMENTED(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 256 | return angle::Result::Continue; |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 259 | WindowSurfaceVk::SwapchainImage::SwapchainImage() = default; |
| 260 | WindowSurfaceVk::SwapchainImage::~SwapchainImage() = default; |
| 261 | |
| 262 | WindowSurfaceVk::SwapchainImage::SwapchainImage(SwapchainImage &&other) |
| 263 | : image(std::move(other.image)), |
| 264 | imageView(std::move(other.imageView)), |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 265 | framebuffer(std::move(other.framebuffer)) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 266 | {} |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 267 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 268 | WindowSurfaceVk::WindowSurfaceVk(const egl::SurfaceState &surfaceState, |
| 269 | EGLNativeWindowType window, |
| 270 | EGLint width, |
| 271 | EGLint height) |
| 272 | : SurfaceImpl(surfaceState), |
| 273 | mNativeWindowType(window), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 274 | mSurface(VK_NULL_HANDLE), |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 275 | mInstance(VK_NULL_HANDLE), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 276 | mSwapchain(VK_NULL_HANDLE), |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 277 | mSwapchainPresentMode(VK_PRESENT_MODE_FIFO_KHR), |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 278 | mMinImageCount(0), |
| 279 | mPreTransform(VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR), |
| 280 | mCompositeAlpha(VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR), |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 281 | mCurrentSwapchainImageIndex(0), |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 282 | mCurrentSwapHistoryIndex(0) |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 283 | { |
| 284 | mDepthStencilRenderTarget.init(&mDepthStencilImage, &mDepthStencilImageView, 0, nullptr); |
| 285 | } |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 286 | |
| 287 | WindowSurfaceVk::~WindowSurfaceVk() |
| 288 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 289 | ASSERT(mSurface == VK_NULL_HANDLE); |
| 290 | ASSERT(mSwapchain == VK_NULL_HANDLE); |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 293 | void WindowSurfaceVk::destroy(const egl::Display *display) |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 294 | { |
Geoff Lang | 38971fd | 2018-06-28 15:19:18 -0400 | [diff] [blame] | 295 | DisplayVk *displayVk = vk::GetImpl(display); |
| 296 | RendererVk *renderer = displayVk->getRenderer(); |
| 297 | VkDevice device = renderer->getDevice(); |
| 298 | VkInstance instance = renderer->getInstance(); |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 299 | |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 300 | // We might not need to flush the pipe here. |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 301 | (void)renderer->finish(displayVk); |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame] | 302 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 303 | releaseSwapchainImages(renderer); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 304 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 305 | for (SwapHistory &swap : mSwapHistory) |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 306 | { |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 307 | if (swap.swapchain != VK_NULL_HANDLE) |
| 308 | { |
| 309 | vkDestroySwapchainKHR(device, swap.swapchain, nullptr); |
| 310 | swap.swapchain = VK_NULL_HANDLE; |
| 311 | } |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 312 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 313 | |
| 314 | if (mSwapchain) |
| 315 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 316 | vkDestroySwapchainKHR(device, mSwapchain, nullptr); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 317 | mSwapchain = VK_NULL_HANDLE; |
| 318 | } |
| 319 | |
| 320 | if (mSurface) |
| 321 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 322 | vkDestroySurfaceKHR(instance, mSurface, nullptr); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 323 | mSurface = VK_NULL_HANDLE; |
| 324 | } |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 325 | } |
| 326 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 327 | egl::Error WindowSurfaceVk::initialize(const egl::Display *display) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 328 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 329 | DisplayVk *displayVk = vk::GetImpl(display); |
| 330 | angle::Result result = initializeImpl(displayVk); |
| 331 | return angle::ToEGL(result, displayVk, EGL_BAD_SURFACE); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 332 | } |
| 333 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 334 | angle::Result WindowSurfaceVk::initializeImpl(DisplayVk *displayVk) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 335 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 336 | RendererVk *renderer = displayVk->getRenderer(); |
| 337 | |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 338 | gl::Extents windowSize; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 339 | ANGLE_TRY(createSurfaceVk(displayVk, &windowSize)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 340 | |
| 341 | uint32_t presentQueue = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 342 | ANGLE_TRY(renderer->selectPresentQueueForSurface(displayVk, mSurface, &presentQueue)); |
Jamie Madill | c6dbc25 | 2018-04-30 19:07:56 -0400 | [diff] [blame] | 343 | ANGLE_UNUSED_VARIABLE(presentQueue); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 344 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 345 | const VkPhysicalDevice &physicalDevice = renderer->getPhysicalDevice(); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 346 | |
| 347 | VkSurfaceCapabilitiesKHR surfaceCaps; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 348 | ANGLE_VK_TRY(displayVk, |
| 349 | vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, mSurface, &surfaceCaps)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 350 | |
| 351 | // Adjust width and height to the swapchain if necessary. |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 352 | uint32_t width = surfaceCaps.currentExtent.width; |
| 353 | uint32_t height = surfaceCaps.currentExtent.height; |
| 354 | |
| 355 | // TODO(jmadill): Support devices which don't support copy. We use this for ReadPixels. |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 356 | ANGLE_VK_CHECK(displayVk, |
| 357 | (surfaceCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) != 0, |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 358 | VK_ERROR_INITIALIZATION_FAILED); |
| 359 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 360 | EGLAttrib attribWidth = mState.attributes.get(EGL_WIDTH, 0); |
| 361 | EGLAttrib attribHeight = mState.attributes.get(EGL_HEIGHT, 0); |
| 362 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 363 | if (surfaceCaps.currentExtent.width == 0xFFFFFFFFu) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 364 | { |
| 365 | ASSERT(surfaceCaps.currentExtent.height == 0xFFFFFFFFu); |
| 366 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 367 | if (attribWidth == 0) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 368 | { |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 369 | width = windowSize.width; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 370 | } |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 371 | if (attribHeight == 0) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 372 | { |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 373 | height = windowSize.height; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 377 | gl::Extents extents(static_cast<int>(width), static_cast<int>(height), 1); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 378 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 379 | uint32_t presentModeCount = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 380 | ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, mSurface, |
| 381 | &presentModeCount, nullptr)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 382 | ASSERT(presentModeCount > 0); |
| 383 | |
| 384 | std::vector<VkPresentModeKHR> presentModes(presentModeCount); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 385 | ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfacePresentModesKHR( |
| 386 | physicalDevice, mSurface, &presentModeCount, presentModes.data())); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 387 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 388 | // Select appropriate present mode based on vsync parameter. |
| 389 | // TODO(jmadill): More complete implementation, which allows for changing and more values. |
| 390 | const EGLint minSwapInterval = mState.config->minSwapInterval; |
| 391 | const EGLint maxSwapInterval = mState.config->maxSwapInterval; |
| 392 | ASSERT(minSwapInterval == 0 || minSwapInterval == 1); |
| 393 | ASSERT(maxSwapInterval == 0 || maxSwapInterval == 1); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 394 | |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 395 | mSwapchainPresentMode = GetDesiredPresentMode(presentModes, minSwapInterval, maxSwapInterval); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 396 | |
Shahbaz Youssefi | eb47e2c | 2019-02-05 16:26:41 -0500 | [diff] [blame] | 397 | // Determine the number of swapchain images: |
| 398 | // |
| 399 | // - On mailbox, we use minImageCount. The drivers may increase the number so that non-blocking |
| 400 | // mailbox actually makes sense. |
| 401 | // - On immediate, we use max(2, minImageCount). The vkQueuePresentKHR call immediately frees |
| 402 | // up the other image, so there is no point in having any more images. |
| 403 | // - On fifo, we use max(3, minImageCount). Triple-buffering allows us to present an image, |
| 404 | // have one in the queue and record in another. Note: on certain configurations (windows + |
| 405 | // nvidia + windowed mode), we could get away with a smaller number. |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 406 | mMinImageCount = surfaceCaps.minImageCount; |
Shahbaz Youssefi | eb47e2c | 2019-02-05 16:26:41 -0500 | [diff] [blame] | 407 | if (mSwapchainPresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) |
| 408 | { |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 409 | mMinImageCount = std::max<uint32_t>(2, mMinImageCount); |
Shahbaz Youssefi | eb47e2c | 2019-02-05 16:26:41 -0500 | [diff] [blame] | 410 | } |
| 411 | else if (mSwapchainPresentMode == VK_PRESENT_MODE_FIFO_KHR) |
| 412 | { |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 413 | mMinImageCount = std::max<uint32_t>(3, mMinImageCount); |
Shahbaz Youssefi | eb47e2c | 2019-02-05 16:26:41 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // Make sure we don't exceed maxImageCount. |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 417 | if (surfaceCaps.maxImageCount > 0 && mMinImageCount > surfaceCaps.maxImageCount) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 418 | { |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 419 | mMinImageCount = surfaceCaps.maxImageCount; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // Default to identity transform. |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 423 | mPreTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 424 | if ((surfaceCaps.supportedTransforms & mPreTransform) == 0) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 425 | { |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 426 | mPreTransform = surfaceCaps.currentTransform; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 429 | uint32_t surfaceFormatCount = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 430 | ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, |
| 431 | &surfaceFormatCount, nullptr)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 432 | |
| 433 | std::vector<VkSurfaceFormatKHR> surfaceFormats(surfaceFormatCount); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 434 | ANGLE_VK_TRY(displayVk, |
| 435 | vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &surfaceFormatCount, |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 436 | surfaceFormats.data())); |
| 437 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 438 | const vk::Format &format = renderer->getFormat(mState.config->renderTargetFormat); |
| 439 | VkFormat nativeFormat = format.vkTextureFormat; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 440 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 441 | if (surfaceFormatCount == 1u && surfaceFormats[0].format == VK_FORMAT_UNDEFINED) |
| 442 | { |
| 443 | // This is fine. |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | bool foundFormat = false; |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 448 | for (const VkSurfaceFormatKHR &surfaceFormat : surfaceFormats) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 449 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 450 | if (surfaceFormat.format == nativeFormat) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 451 | { |
| 452 | foundFormat = true; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 457 | ANGLE_VK_CHECK(displayVk, foundFormat, VK_ERROR_INITIALIZATION_FAILED); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 458 | } |
| 459 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 460 | mCompositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
| 461 | if ((surfaceCaps.supportedCompositeAlpha & mCompositeAlpha) == 0) |
Yuly Novikov | 12da5e7 | 2018-01-23 18:34:53 -0500 | [diff] [blame] | 462 | { |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 463 | mCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; |
Yuly Novikov | 12da5e7 | 2018-01-23 18:34:53 -0500 | [diff] [blame] | 464 | } |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 465 | ANGLE_VK_CHECK(displayVk, (surfaceCaps.supportedCompositeAlpha & mCompositeAlpha) != 0, |
Yuly Novikov | 12da5e7 | 2018-01-23 18:34:53 -0500 | [diff] [blame] | 466 | VK_ERROR_INITIALIZATION_FAILED); |
| 467 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 468 | ANGLE_TRY(recreateSwapchain(displayVk, extents, mCurrentSwapHistoryIndex)); |
| 469 | |
| 470 | // Get the first available swapchain iamge. |
| 471 | return nextSwapchainImage(displayVk); |
| 472 | } |
| 473 | |
| 474 | angle::Result WindowSurfaceVk::recreateSwapchain(DisplayVk *displayVk, |
| 475 | const gl::Extents &extents, |
| 476 | uint32_t swapHistoryIndex) |
| 477 | { |
| 478 | RendererVk *renderer = displayVk->getRenderer(); |
| 479 | VkDevice device = renderer->getDevice(); |
| 480 | |
| 481 | VkSwapchainKHR oldSwapchain = mSwapchain; |
| 482 | mSwapchain = VK_NULL_HANDLE; |
| 483 | |
| 484 | if (oldSwapchain) |
| 485 | { |
| 486 | // Note: the old swapchain must be destroyed regardless of whether creating the new |
| 487 | // swapchain succeeds. We can only destroy the swapchain once rendering to all its images |
| 488 | // have finished. We therefore store the handle to the swapchain being destroyed in the |
| 489 | // swap history (alongside the serial of the last submission) so it can be destroyed once we |
| 490 | // wait on that serial as part of the CPU throttling. |
| 491 | // |
| 492 | // TODO(syoussefi): the spec specifically allows multiple retired swapchains to exist: |
| 493 | // |
| 494 | // > Multiple retired swapchains can be associated with the same VkSurfaceKHR through |
| 495 | // > multiple uses of oldSwapchain that outnumber calls to vkDestroySwapchainKHR. |
| 496 | // |
| 497 | // However, a bug in the validation layers currently forces us to limit this to one retired |
| 498 | // swapchain. Once the issue is resolved, the following for loop can be removed. |
| 499 | // http://anglebug.com/3095 |
| 500 | for (SwapHistory &swap : mSwapHistory) |
| 501 | { |
| 502 | if (swap.swapchain != VK_NULL_HANDLE) |
| 503 | { |
| 504 | ANGLE_TRY(renderer->finishToSerial(displayVk, swap.serial)); |
| 505 | vkDestroySwapchainKHR(renderer->getDevice(), swap.swapchain, nullptr); |
| 506 | swap.swapchain = VK_NULL_HANDLE; |
| 507 | } |
| 508 | } |
| 509 | mSwapHistory[swapHistoryIndex].swapchain = oldSwapchain; |
| 510 | } |
| 511 | |
| 512 | releaseSwapchainImages(renderer); |
| 513 | |
| 514 | const vk::Format &format = renderer->getFormat(mState.config->renderTargetFormat); |
| 515 | VkFormat nativeFormat = format.vkTextureFormat; |
| 516 | |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 517 | // We need transfer src for reading back from the backbuffer. |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 518 | constexpr VkImageUsageFlags kImageUsageFlags = kSurfaceVKColorImageUsageFlags; |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 519 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 520 | VkSwapchainCreateInfoKHR swapchainInfo = {}; |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 521 | swapchainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 522 | swapchainInfo.flags = 0; |
| 523 | swapchainInfo.surface = mSurface; |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 524 | swapchainInfo.minImageCount = mMinImageCount; |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 525 | swapchainInfo.imageFormat = nativeFormat; |
| 526 | swapchainInfo.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 527 | // Note: Vulkan doesn't allow 0-width/height swapchains. |
| 528 | swapchainInfo.imageExtent.width = std::max(extents.width, 1); |
| 529 | swapchainInfo.imageExtent.height = std::max(extents.height, 1); |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 530 | swapchainInfo.imageArrayLayers = 1; |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 531 | swapchainInfo.imageUsage = kImageUsageFlags; |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 532 | swapchainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 533 | swapchainInfo.queueFamilyIndexCount = 0; |
| 534 | swapchainInfo.pQueueFamilyIndices = nullptr; |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 535 | swapchainInfo.preTransform = mPreTransform; |
| 536 | swapchainInfo.compositeAlpha = mCompositeAlpha; |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 537 | swapchainInfo.presentMode = mSwapchainPresentMode; |
| 538 | swapchainInfo.clipped = VK_TRUE; |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 539 | swapchainInfo.oldSwapchain = oldSwapchain; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 540 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 541 | // TODO(syoussefi): Once EGL_SWAP_BEHAVIOR_PRESERVED_BIT is supported, the contents of the old |
| 542 | // swapchain need to carry over to the new one. http://anglebug.com/2942 |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 543 | ANGLE_VK_TRY(displayVk, vkCreateSwapchainKHR(device, &swapchainInfo, nullptr, &mSwapchain)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 544 | |
| 545 | // Intialize the swapchain image views. |
| 546 | uint32_t imageCount = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 547 | ANGLE_VK_TRY(displayVk, vkGetSwapchainImagesKHR(device, mSwapchain, &imageCount, nullptr)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 548 | |
| 549 | std::vector<VkImage> swapchainImages(imageCount); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 550 | ANGLE_VK_TRY(displayVk, |
| 551 | vkGetSwapchainImagesKHR(device, mSwapchain, &imageCount, swapchainImages.data())); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 552 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 553 | VkClearColorValue transparentBlack = {}; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 554 | transparentBlack.float32[0] = 0.0f; |
| 555 | transparentBlack.float32[1] = 0.0f; |
| 556 | transparentBlack.float32[2] = 0.0f; |
| 557 | transparentBlack.float32[3] = 0.0f; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 558 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 559 | // Note: we expect subsequent calls to vkGetSwapchainImagesKHR to return the same number of |
| 560 | // images, given that the same value for minImageCount was provided. If that's not true, then |
| 561 | // the swap history would need to be preserved correctly; if there are more images now, the |
| 562 | // circular history buffer needs to be rearranged to remain continuous, and if there are fewer |
| 563 | // images now, we need to finishToSerial for the part of history that doesn't fit and clean up |
| 564 | // the respective old swapchains. |
| 565 | ASSERT(mSwapHistory.size() == 0 || mSwapHistory.size() == imageCount); |
| 566 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 567 | mSwapchainImages.resize(imageCount); |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 568 | mSwapHistory.resize(imageCount); |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 569 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 570 | for (uint32_t imageIndex = 0; imageIndex < imageCount; ++imageIndex) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 571 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 572 | SwapchainImage &member = mSwapchainImages[imageIndex]; |
| 573 | member.image.init2DWeakReference(swapchainImages[imageIndex], extents, format, 1); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 574 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 575 | ANGLE_TRY(member.image.initImageView(displayVk, gl::TextureType::_2D, |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame] | 576 | VK_IMAGE_ASPECT_COLOR_BIT, gl::SwizzleState(), |
| 577 | &member.imageView, 1)); |
Jamie Madill | 25301b6 | 2017-10-28 20:59:31 -0400 | [diff] [blame] | 578 | |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 579 | // Allocate a command buffer for clearing our images to black. |
| 580 | vk::CommandBuffer *commandBuffer = nullptr; |
| 581 | ANGLE_TRY(member.image.recordCommands(displayVk, &commandBuffer)); |
| 582 | |
Jamie Madill | 25301b6 | 2017-10-28 20:59:31 -0400 | [diff] [blame] | 583 | // Set transfer dest layout, and clear the image to black. |
Luc Ferron | c20b950 | 2018-05-24 09:30:17 -0400 | [diff] [blame] | 584 | member.image.clearColor(transparentBlack, 0, 1, commandBuffer); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 585 | } |
| 586 | |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 587 | // Initialize depth/stencil if requested. |
| 588 | if (mState.config->depthStencilFormat != GL_NONE) |
| 589 | { |
| 590 | const vk::Format &dsFormat = renderer->getFormat(mState.config->depthStencilFormat); |
| 591 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 592 | const VkImageUsageFlags dsUsage = kSurfaceVKDepthStencilImageUsageFlags; |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 593 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 594 | ANGLE_TRY(mDepthStencilImage.init(displayVk, gl::TextureType::_2D, extents, dsFormat, 1, |
Shahbaz Youssefi | b5ba549 | 2019-01-02 15:19:22 -0500 | [diff] [blame] | 595 | dsUsage, 1, 1)); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 596 | ANGLE_TRY(mDepthStencilImage.initMemory(displayVk, renderer->getMemoryProperties(), |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 597 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 598 | |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 599 | const VkImageAspectFlags aspect = vk::GetDepthStencilAspectFlags(dsFormat.textureFormat()); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 600 | VkClearDepthStencilValue depthStencilClearValue = {1.0f, 0}; |
| 601 | |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 602 | // Clear the image. |
| 603 | vk::CommandBuffer *commandBuffer = nullptr; |
| 604 | ANGLE_TRY(mDepthStencilImage.recordCommands(displayVk, &commandBuffer)); |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 605 | mDepthStencilImage.clearDepthStencil(aspect, aspect, depthStencilClearValue, commandBuffer); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 606 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 607 | ANGLE_TRY(mDepthStencilImage.initImageView(displayVk, gl::TextureType::_2D, aspect, |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 608 | gl::SwizzleState(), &mDepthStencilImageView, 1)); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 609 | |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 610 | // We will need to pass depth/stencil image views to the RenderTargetVk in the future. |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 611 | } |
| 612 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 613 | return angle::Result::Continue; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 614 | } |
| 615 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 616 | angle::Result WindowSurfaceVk::checkForOutOfDateSwapchain(DisplayVk *displayVk, |
| 617 | uint32_t swapHistoryIndex, |
| 618 | bool presentOutOfDate) |
| 619 | { |
| 620 | // Check for window resize and recreate swapchain if necessary. |
| 621 | gl::Extents currentExtents; |
| 622 | ANGLE_TRY(getCurrentWindowSize(displayVk, ¤tExtents)); |
| 623 | if (!presentOutOfDate && currentExtents.width == getWidth() && |
| 624 | currentExtents.height == getHeight()) |
| 625 | { |
| 626 | return angle::Result::Continue; |
| 627 | } |
| 628 | |
| 629 | VkSurfaceCapabilitiesKHR surfaceCaps; |
| 630 | const VkPhysicalDevice &physicalDevice = displayVk->getRenderer()->getPhysicalDevice(); |
| 631 | ANGLE_VK_TRY(displayVk, |
| 632 | vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, mSurface, &surfaceCaps)); |
| 633 | |
| 634 | uint32_t width = surfaceCaps.currentExtent.width; |
| 635 | uint32_t height = surfaceCaps.currentExtent.height; |
| 636 | |
| 637 | if (width != 0xFFFFFFFFu) |
| 638 | { |
| 639 | ASSERT(height != 0xFFFFFFFFu); |
| 640 | currentExtents.width = width; |
| 641 | currentExtents.height = height; |
| 642 | } |
| 643 | |
| 644 | return recreateSwapchain(displayVk, currentExtents, swapHistoryIndex); |
| 645 | } |
| 646 | |
| 647 | void WindowSurfaceVk::releaseSwapchainImages(RendererVk *renderer) |
| 648 | { |
| 649 | if (mDepthStencilImage.valid()) |
| 650 | { |
| 651 | Serial depthStencilSerial = mDepthStencilImage.getStoredQueueSerial(); |
| 652 | mDepthStencilImage.releaseImage(renderer); |
| 653 | mDepthStencilImage.releaseStagingBuffer(renderer); |
| 654 | |
| 655 | if (mDepthStencilImageView.valid()) |
| 656 | { |
| 657 | renderer->releaseObject(depthStencilSerial, &mDepthStencilImageView); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | for (SwapchainImage &swapchainImage : mSwapchainImages) |
| 662 | { |
| 663 | Serial imageSerial = swapchainImage.image.getStoredQueueSerial(); |
| 664 | |
| 665 | // We don't own the swapchain image handles, so we just remove our reference to it. |
| 666 | swapchainImage.image.resetImageWeakReference(); |
| 667 | swapchainImage.image.destroy(renderer->getDevice()); |
| 668 | |
| 669 | if (swapchainImage.imageView.valid()) |
| 670 | { |
| 671 | renderer->releaseObject(imageSerial, &swapchainImage.imageView); |
| 672 | } |
| 673 | |
| 674 | if (swapchainImage.framebuffer.valid()) |
| 675 | { |
| 676 | renderer->releaseObject(imageSerial, &swapchainImage.framebuffer); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | mSwapchainImages.clear(); |
| 681 | } |
| 682 | |
Geoff Lang | bf7b95d | 2018-05-01 16:48:21 -0400 | [diff] [blame] | 683 | FramebufferImpl *WindowSurfaceVk::createDefaultFramebuffer(const gl::Context *context, |
| 684 | const gl::FramebufferState &state) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 685 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 686 | RendererVk *renderer = vk::GetImpl(context)->getRenderer(); |
| 687 | return FramebufferVk::CreateDefaultFBO(renderer, state, this); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 688 | } |
| 689 | |
Courtney Goeltzenleuchter | f0d258c | 2018-09-11 09:37:48 -0600 | [diff] [blame] | 690 | egl::Error WindowSurfaceVk::swapWithDamage(const gl::Context *context, |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 691 | EGLint *rects, |
| 692 | EGLint n_rects) |
Courtney Goeltzenleuchter | f0d258c | 2018-09-11 09:37:48 -0600 | [diff] [blame] | 693 | { |
| 694 | DisplayVk *displayVk = vk::GetImpl(context->getCurrentDisplay()); |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 695 | angle::Result result = swapImpl(displayVk, rects, n_rects); |
Courtney Goeltzenleuchter | f0d258c | 2018-09-11 09:37:48 -0600 | [diff] [blame] | 696 | return angle::ToEGL(result, displayVk, EGL_BAD_SURFACE); |
| 697 | } |
| 698 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 699 | egl::Error WindowSurfaceVk::swap(const gl::Context *context) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 700 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 701 | DisplayVk *displayVk = vk::GetImpl(context->getCurrentDisplay()); |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 702 | angle::Result result = swapImpl(displayVk, nullptr, 0); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 703 | return angle::ToEGL(result, displayVk, EGL_BAD_SURFACE); |
| 704 | } |
| 705 | |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 706 | angle::Result WindowSurfaceVk::swapImpl(DisplayVk *displayVk, EGLint *rects, EGLint n_rects) |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 707 | { |
| 708 | RendererVk *renderer = displayVk->getRenderer(); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 709 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 710 | // Throttle the submissions to avoid getting too far ahead of the GPU. |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 711 | { |
| 712 | TRACE_EVENT0("gpu.angle", "WindowSurfaceVk::swapImpl: Throttle CPU"); |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 713 | SwapHistory &swap = mSwapHistory[mCurrentSwapHistoryIndex]; |
| 714 | ANGLE_TRY(renderer->finishToSerial(displayVk, swap.serial)); |
| 715 | if (swap.swapchain != VK_NULL_HANDLE) |
| 716 | { |
| 717 | vkDestroySwapchainKHR(renderer->getDevice(), swap.swapchain, nullptr); |
| 718 | swap.swapchain = VK_NULL_HANDLE; |
| 719 | } |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 720 | } |
| 721 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 722 | SwapchainImage &image = mSwapchainImages[mCurrentSwapchainImageIndex]; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 723 | |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 724 | vk::CommandBuffer *swapCommands = nullptr; |
| 725 | ANGLE_TRY(image.image.recordCommands(displayVk, &swapCommands)); |
| 726 | |
| 727 | image.image.changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::Present, swapCommands); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 728 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 729 | ANGLE_TRY(renderer->flush(displayVk)); |
| 730 | |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 731 | // Remember the serial of the last submission. |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 732 | mSwapHistory[mCurrentSwapHistoryIndex].serial = renderer->getLastSubmittedQueueSerial(); |
| 733 | uint32_t currentSwapHistoryIndex = mCurrentSwapHistoryIndex; |
| 734 | ++mCurrentSwapHistoryIndex; |
| 735 | mCurrentSwapHistoryIndex = |
| 736 | mCurrentSwapHistoryIndex == mSwapHistory.size() ? 0 : mCurrentSwapHistoryIndex; |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 737 | |
| 738 | // Ask the renderer what semaphore it signaled in the last flush. |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 739 | const vk::Semaphore *commandsCompleteSemaphore = |
| 740 | renderer->getSubmitLastSignaledSemaphore(displayVk); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 741 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 742 | VkPresentInfoKHR presentInfo = {}; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 743 | presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 744 | presentInfo.waitSemaphoreCount = commandsCompleteSemaphore ? 1 : 0; |
| 745 | presentInfo.pWaitSemaphores = |
| 746 | commandsCompleteSemaphore ? commandsCompleteSemaphore->ptr() : nullptr; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 747 | presentInfo.swapchainCount = 1; |
| 748 | presentInfo.pSwapchains = &mSwapchain; |
| 749 | presentInfo.pImageIndices = &mCurrentSwapchainImageIndex; |
| 750 | presentInfo.pResults = nullptr; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 751 | |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 752 | VkPresentRegionsKHR presentRegions = {}; |
| 753 | if (renderer->getFeatures().supportsIncrementalPresent && (n_rects > 0)) |
| 754 | { |
| 755 | VkPresentRegionKHR presentRegion = {}; |
| 756 | std::vector<VkRectLayerKHR> vk_rects(n_rects); |
| 757 | EGLint *egl_rects = rects; |
| 758 | presentRegion.rectangleCount = n_rects; |
| 759 | for (EGLint rect = 0; rect < n_rects; rect++) |
| 760 | { |
| 761 | vk_rects[rect].offset.x = *egl_rects++; |
| 762 | vk_rects[rect].offset.y = *egl_rects++; |
| 763 | vk_rects[rect].extent.width = *egl_rects++; |
| 764 | vk_rects[rect].extent.height = *egl_rects++; |
| 765 | vk_rects[rect].layer = 0; |
| 766 | } |
| 767 | presentRegion.pRectangles = vk_rects.data(); |
| 768 | |
| 769 | presentRegions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR; |
| 770 | presentRegions.pNext = nullptr; |
| 771 | presentRegions.swapchainCount = 1; |
| 772 | presentRegions.pRegions = &presentRegion; |
| 773 | |
| 774 | presentInfo.pNext = &presentRegions; |
| 775 | } |
| 776 | |
Shahbaz Youssefi | ab5acbd | 2019-01-23 13:58:09 -0500 | [diff] [blame^] | 777 | VkResult result = vkQueuePresentKHR(renderer->getQueue(), &presentInfo); |
| 778 | |
| 779 | // If SUBOPTIMAL/OUT_OF_DATE is returned, it's ok, we just need to recreate the swapchain before |
| 780 | // continuing. |
| 781 | bool swapchainOutOfDate = result == VK_SUBOPTIMAL_KHR || result == VK_ERROR_OUT_OF_DATE_KHR; |
| 782 | if (!swapchainOutOfDate) |
| 783 | { |
| 784 | ANGLE_VK_TRY(displayVk, result); |
| 785 | } |
| 786 | |
| 787 | ANGLE_TRY(checkForOutOfDateSwapchain(displayVk, currentSwapHistoryIndex, swapchainOutOfDate)); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 788 | |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 789 | { |
| 790 | // Note: TRACE_EVENT0 is put here instead of inside the function to workaround this issue: |
| 791 | // http://anglebug.com/2927 |
| 792 | TRACE_EVENT0("gpu.angle", "nextSwapchainImage"); |
| 793 | // Get the next available swapchain image. |
| 794 | ANGLE_TRY(nextSwapchainImage(displayVk)); |
| 795 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 796 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 797 | ANGLE_TRY(renderer->syncPipelineCacheVk(displayVk)); |
| 798 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 799 | return angle::Result::Continue; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 800 | } |
| 801 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 802 | angle::Result WindowSurfaceVk::nextSwapchainImage(DisplayVk *displayVk) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 803 | { |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 804 | VkDevice device = displayVk->getDevice(); |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 805 | RendererVk *renderer = displayVk->getRenderer(); |
| 806 | |
| 807 | const vk::Semaphore *acquireNextImageSemaphore = nullptr; |
| 808 | ANGLE_TRY(renderer->allocateSubmitWaitSemaphore(displayVk, &acquireNextImageSemaphore)); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 809 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 810 | ANGLE_VK_TRY(displayVk, vkAcquireNextImageKHR(device, mSwapchain, UINT64_MAX, |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 811 | acquireNextImageSemaphore->getHandle(), |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 812 | VK_NULL_HANDLE, &mCurrentSwapchainImageIndex)); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 813 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 814 | SwapchainImage &image = mSwapchainImages[mCurrentSwapchainImageIndex]; |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 815 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 816 | // Update RenderTarget pointers. |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 817 | mColorRenderTarget.updateSwapchainImage(&image.image, &image.imageView); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 818 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 819 | return angle::Result::Continue; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 820 | } |
| 821 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 822 | egl::Error WindowSurfaceVk::postSubBuffer(const gl::Context *context, |
| 823 | EGLint x, |
| 824 | EGLint y, |
| 825 | EGLint width, |
| 826 | EGLint height) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 827 | { |
| 828 | // TODO(jmadill) |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 829 | return egl::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | egl::Error WindowSurfaceVk::querySurfacePointerANGLE(EGLint attribute, void **value) |
| 833 | { |
| 834 | UNREACHABLE(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 835 | return egl::EglBadCurrentSurface(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 836 | } |
| 837 | |
Geoff Lang | ccafa62 | 2018-05-02 13:07:53 -0400 | [diff] [blame] | 838 | egl::Error WindowSurfaceVk::bindTexImage(const gl::Context *context, |
| 839 | gl::Texture *texture, |
| 840 | EGLint buffer) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 841 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 842 | return egl::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 843 | } |
| 844 | |
Geoff Lang | ccafa62 | 2018-05-02 13:07:53 -0400 | [diff] [blame] | 845 | egl::Error WindowSurfaceVk::releaseTexImage(const gl::Context *context, EGLint buffer) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 846 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 847 | return egl::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 848 | } |
| 849 | |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 850 | egl::Error WindowSurfaceVk::getSyncValues(EGLuint64KHR * /*ust*/, |
| 851 | EGLuint64KHR * /*msc*/, |
| 852 | EGLuint64KHR * /*sbc*/) |
| 853 | { |
| 854 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 855 | return egl::EglBadAccess(); |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 858 | void WindowSurfaceVk::setSwapInterval(EGLint interval) {} |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 859 | |
| 860 | EGLint WindowSurfaceVk::getWidth() const |
| 861 | { |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 862 | return static_cast<EGLint>(mColorRenderTarget.getImageExtents().width); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | EGLint WindowSurfaceVk::getHeight() const |
| 866 | { |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 867 | return static_cast<EGLint>(mColorRenderTarget.getImageExtents().height); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | EGLint WindowSurfaceVk::isPostSubBufferSupported() const |
| 871 | { |
| 872 | // TODO(jmadill) |
| 873 | return EGL_FALSE; |
| 874 | } |
| 875 | |
| 876 | EGLint WindowSurfaceVk::getSwapBehavior() const |
| 877 | { |
| 878 | // TODO(jmadill) |
| 879 | return EGL_BUFFER_DESTROYED; |
| 880 | } |
| 881 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 882 | angle::Result WindowSurfaceVk::getAttachmentRenderTarget(const gl::Context *context, |
| 883 | GLenum binding, |
| 884 | const gl::ImageIndex &imageIndex, |
| 885 | FramebufferAttachmentRenderTarget **rtOut) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 886 | { |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 887 | if (binding == GL_BACK) |
| 888 | { |
| 889 | *rtOut = &mColorRenderTarget; |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | ASSERT(binding == GL_DEPTH || binding == GL_STENCIL || binding == GL_DEPTH_STENCIL); |
| 894 | *rtOut = &mDepthStencilRenderTarget; |
| 895 | } |
| 896 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 897 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 898 | } |
| 899 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 900 | angle::Result WindowSurfaceVk::getCurrentFramebuffer(vk::Context *context, |
| 901 | const vk::RenderPass &compatibleRenderPass, |
| 902 | vk::Framebuffer **framebufferOut) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 903 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 904 | vk::Framebuffer ¤tFramebuffer = mSwapchainImages[mCurrentSwapchainImageIndex].framebuffer; |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 905 | |
| 906 | if (currentFramebuffer.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 907 | { |
| 908 | // Validation layers should detect if the render pass is really compatible. |
Jamie Madill | 5598148 | 2018-07-11 09:01:18 -0400 | [diff] [blame] | 909 | *framebufferOut = ¤tFramebuffer; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 910 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 911 | } |
| 912 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 913 | VkFramebufferCreateInfo framebufferInfo = {}; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 914 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 915 | const gl::Extents &extents = mColorRenderTarget.getImageExtents(); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 916 | std::array<VkImageView, 2> imageViews = {{VK_NULL_HANDLE, mDepthStencilImageView.getHandle()}}; |
| 917 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 918 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 919 | framebufferInfo.flags = 0; |
| 920 | framebufferInfo.renderPass = compatibleRenderPass.getHandle(); |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 921 | framebufferInfo.attachmentCount = (mDepthStencilImage.valid() ? 2u : 1u); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 922 | framebufferInfo.pAttachments = imageViews.data(); |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 923 | framebufferInfo.width = static_cast<uint32_t>(extents.width); |
| 924 | framebufferInfo.height = static_cast<uint32_t>(extents.height); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 925 | framebufferInfo.layers = 1; |
| 926 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 927 | for (SwapchainImage &swapchainImage : mSwapchainImages) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 928 | { |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 929 | imageViews[0] = swapchainImage.imageView.getHandle(); |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 930 | ANGLE_VK_TRY(context, |
| 931 | swapchainImage.framebuffer.init(context->getDevice(), framebufferInfo)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 932 | } |
| 933 | |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 934 | ASSERT(currentFramebuffer.valid()); |
Jamie Madill | 5598148 | 2018-07-11 09:01:18 -0400 | [diff] [blame] | 935 | *framebufferOut = ¤tFramebuffer; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 936 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 937 | } |
| 938 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 939 | angle::Result WindowSurfaceVk::initializeContents(const gl::Context *context, |
| 940 | const gl::ImageIndex &imageIndex) |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 941 | { |
| 942 | UNIMPLEMENTED(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 943 | return angle::Result::Continue; |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 944 | } |
| 945 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 946 | } // namespace rx |