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