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" |
| 16 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
| 18 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 19 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 20 | |
| 21 | namespace rx |
| 22 | { |
| 23 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 24 | namespace |
| 25 | { |
| 26 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 27 | VkPresentModeKHR GetDesiredPresentMode(const std::vector<VkPresentModeKHR> &presentModes, |
| 28 | EGLint minSwapInterval, |
| 29 | EGLint maxSwapInterval) |
| 30 | { |
| 31 | ASSERT(!presentModes.empty()); |
| 32 | |
| 33 | // Use FIFO mode for v-sync, since it throttles you to the display rate. Mailbox is more |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 34 | // similar to triple-buffering. For now we hard-code Mailbox for perf testing. |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 35 | // TODO(jmadill): Properly select present mode and re-create display if changed. |
| 36 | VkPresentModeKHR bestChoice = VK_PRESENT_MODE_MAILBOX_KHR; |
| 37 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 38 | for (VkPresentModeKHR presentMode : presentModes) |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 39 | { |
| 40 | if (presentMode == bestChoice) |
| 41 | { |
| 42 | return bestChoice; |
| 43 | } |
| 44 | } |
| 45 | |
Jamie Madill | 98de826 | 2017-05-29 13:01:02 -0400 | [diff] [blame] | 46 | WARN() << "Present mode " << bestChoice << " not available. Falling back to " |
| 47 | << presentModes[0]; |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 48 | return presentModes[0]; |
| 49 | } |
| 50 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 51 | constexpr VkImageUsageFlags kSurfaceVKImageUsageFlags = |
| 52 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 53 | constexpr VkImageUsageFlags kSurfaceVKColorImageUsageFlags = |
| 54 | kSurfaceVKImageUsageFlags | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 55 | constexpr VkImageUsageFlags kSurfaceVKDepthStencilImageUsageFlags = |
| 56 | kSurfaceVKImageUsageFlags | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; |
| 57 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 58 | } // namespace |
| 59 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 60 | OffscreenSurfaceVk::AttachmentImage::AttachmentImage(vk::CommandGraphResource *commandGraphResource) |
| 61 | : renderTarget(&image, &imageView, commandGraphResource) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | OffscreenSurfaceVk::AttachmentImage::~AttachmentImage() = default; |
| 66 | |
| 67 | egl::Error OffscreenSurfaceVk::AttachmentImage::initialize(const egl::Display *display, |
| 68 | EGLint width, |
| 69 | EGLint height, |
| 70 | const vk::Format &vkFormat) |
| 71 | { |
| 72 | const DisplayVk *displayVk = vk::GetImpl(display); |
| 73 | RendererVk *renderer = displayVk->getRenderer(); |
| 74 | VkDevice device = renderer->getDevice(); |
| 75 | |
| 76 | const angle::Format &textureFormat = vkFormat.textureFormat(); |
| 77 | bool isDepthOrStencilFormat = textureFormat.depthBits > 0 || textureFormat.stencilBits > 0; |
| 78 | const VkImageUsageFlags usage = isDepthOrStencilFormat ? kSurfaceVKDepthStencilImageUsageFlags |
| 79 | : kSurfaceVKColorImageUsageFlags; |
| 80 | |
| 81 | gl::Extents extents(static_cast<int>(width), static_cast<int>(height), 1); |
| 82 | ANGLE_TRY(image.init(device, gl::TextureType::_2D, extents, vkFormat, 1, usage, 1)); |
| 83 | |
| 84 | VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 85 | ANGLE_TRY(image.initMemory(device, renderer->getMemoryProperties(), flags)); |
| 86 | |
| 87 | VkImageAspectFlags aspect = vk::GetFormatAspectFlags(textureFormat); |
| 88 | |
| 89 | ANGLE_TRY(image.initImageView(device, gl::TextureType::_2D, aspect, gl::SwizzleState(), |
| 90 | &imageView, 1)); |
| 91 | |
| 92 | return egl::NoError(); |
| 93 | } |
| 94 | |
| 95 | void OffscreenSurfaceVk::AttachmentImage::destroy(const egl::Display *display, |
| 96 | Serial storedQueueSerial) |
| 97 | { |
| 98 | const DisplayVk *displayVk = vk::GetImpl(display); |
| 99 | RendererVk *renderer = displayVk->getRenderer(); |
| 100 | |
| 101 | image.release(renderer->getCurrentQueueSerial(), renderer); |
| 102 | renderer->releaseObject(storedQueueSerial, &imageView); |
| 103 | } |
| 104 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 105 | OffscreenSurfaceVk::OffscreenSurfaceVk(const egl::SurfaceState &surfaceState, |
| 106 | EGLint width, |
| 107 | EGLint height) |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 108 | : SurfaceImpl(surfaceState), |
| 109 | mWidth(width), |
| 110 | mHeight(height), |
| 111 | mColorAttachment(this), |
| 112 | mDepthStencilAttachment(this) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 113 | { |
| 114 | } |
| 115 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 116 | OffscreenSurfaceVk::~OffscreenSurfaceVk() |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 117 | { |
| 118 | } |
| 119 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 120 | egl::Error OffscreenSurfaceVk::initialize(const egl::Display *display) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 121 | { |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 122 | const DisplayVk *displayVk = vk::GetImpl(display); |
| 123 | RendererVk *renderer = displayVk->getRenderer(); |
| 124 | |
| 125 | const egl::Config *config = mState.config; |
| 126 | |
| 127 | if (config->renderTargetFormat != GL_NONE) |
| 128 | { |
| 129 | ANGLE_TRY(mColorAttachment.initialize(display, mWidth, mHeight, |
| 130 | renderer->getFormat(config->renderTargetFormat))); |
| 131 | } |
| 132 | |
| 133 | if (config->depthStencilFormat != GL_NONE) |
| 134 | { |
| 135 | ANGLE_TRY(mDepthStencilAttachment.initialize( |
| 136 | display, mWidth, mHeight, renderer->getFormat(config->depthStencilFormat))); |
| 137 | } |
| 138 | |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 139 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 142 | void OffscreenSurfaceVk::destroy(const egl::Display *display) |
| 143 | { |
| 144 | mColorAttachment.destroy(display, getStoredQueueSerial()); |
| 145 | mDepthStencilAttachment.destroy(display, getStoredQueueSerial()); |
| 146 | } |
| 147 | |
Geoff Lang | bf7b95d | 2018-05-01 16:48:21 -0400 | [diff] [blame] | 148 | FramebufferImpl *OffscreenSurfaceVk::createDefaultFramebuffer(const gl::Context *context, |
| 149 | const gl::FramebufferState &state) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 150 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 151 | // Use a user FBO for an offscreen RT. |
| 152 | return FramebufferVk::CreateUserFBO(state); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 155 | egl::Error OffscreenSurfaceVk::swap(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 156 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 157 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 160 | egl::Error OffscreenSurfaceVk::postSubBuffer(const gl::Context * /*context*/, |
| 161 | EGLint /*x*/, |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 162 | EGLint /*y*/, |
| 163 | EGLint /*width*/, |
| 164 | EGLint /*height*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 165 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 166 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 169 | egl::Error OffscreenSurfaceVk::querySurfacePointerANGLE(EGLint /*attribute*/, void ** /*value*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 170 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 171 | UNREACHABLE(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 172 | return egl::EglBadCurrentSurface(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Geoff Lang | ccafa62 | 2018-05-02 13:07:53 -0400 | [diff] [blame] | 175 | egl::Error OffscreenSurfaceVk::bindTexImage(const gl::Context * /*context*/, |
| 176 | gl::Texture * /*texture*/, |
| 177 | EGLint /*buffer*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 178 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 179 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Geoff Lang | ccafa62 | 2018-05-02 13:07:53 -0400 | [diff] [blame] | 182 | egl::Error OffscreenSurfaceVk::releaseTexImage(const gl::Context * /*context*/, EGLint /*buffer*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 183 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 184 | return egl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 187 | egl::Error OffscreenSurfaceVk::getSyncValues(EGLuint64KHR * /*ust*/, |
| 188 | EGLuint64KHR * /*msc*/, |
| 189 | EGLuint64KHR * /*sbc*/) |
| 190 | { |
| 191 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 192 | return egl::EglBadAccess(); |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 195 | void OffscreenSurfaceVk::setSwapInterval(EGLint /*interval*/) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 196 | { |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 199 | EGLint OffscreenSurfaceVk::getWidth() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 200 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 201 | return mWidth; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 204 | EGLint OffscreenSurfaceVk::getHeight() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 205 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 206 | return mHeight; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 209 | EGLint OffscreenSurfaceVk::isPostSubBufferSupported() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 210 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 211 | return EGL_FALSE; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 214 | EGLint OffscreenSurfaceVk::getSwapBehavior() const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 215 | { |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 216 | return EGL_BUFFER_PRESERVED; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 219 | gl::Error OffscreenSurfaceVk::getAttachmentRenderTarget(const gl::Context * /*context*/, |
| 220 | GLenum binding, |
| 221 | const gl::ImageIndex & /*imageIndex*/, |
| 222 | FramebufferAttachmentRenderTarget **rtOut) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 223 | { |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 224 | if (binding == GL_BACK) |
| 225 | { |
| 226 | *rtOut = &mColorAttachment.renderTarget; |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | ASSERT(binding == GL_DEPTH || binding == GL_STENCIL || binding == GL_DEPTH_STENCIL); |
| 231 | *rtOut = &mDepthStencilAttachment.renderTarget; |
| 232 | } |
| 233 | |
| 234 | return gl::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 237 | gl::Error OffscreenSurfaceVk::initializeContents(const gl::Context *context, |
| 238 | const gl::ImageIndex &imageIndex) |
| 239 | { |
| 240 | UNIMPLEMENTED(); |
| 241 | return gl::NoError(); |
| 242 | } |
| 243 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 244 | WindowSurfaceVk::SwapchainImage::SwapchainImage() = default; |
| 245 | WindowSurfaceVk::SwapchainImage::~SwapchainImage() = default; |
| 246 | |
| 247 | WindowSurfaceVk::SwapchainImage::SwapchainImage(SwapchainImage &&other) |
| 248 | : image(std::move(other.image)), |
| 249 | imageView(std::move(other.imageView)), |
| 250 | framebuffer(std::move(other.framebuffer)), |
| 251 | imageAcquiredSemaphore(std::move(other.imageAcquiredSemaphore)), |
| 252 | commandsCompleteSemaphore(std::move(other.commandsCompleteSemaphore)) |
| 253 | { |
| 254 | } |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 255 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 256 | WindowSurfaceVk::WindowSurfaceVk(const egl::SurfaceState &surfaceState, |
| 257 | EGLNativeWindowType window, |
| 258 | EGLint width, |
| 259 | EGLint height) |
| 260 | : SurfaceImpl(surfaceState), |
| 261 | mNativeWindowType(window), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 262 | mSurface(VK_NULL_HANDLE), |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 263 | mInstance(VK_NULL_HANDLE), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 264 | mSwapchain(VK_NULL_HANDLE), |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 265 | mColorRenderTarget(nullptr, nullptr, this), |
| 266 | mDepthStencilRenderTarget(&mDepthStencilImage, &mDepthStencilImageView, this), |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 267 | mCurrentSwapchainImageIndex(0) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 268 | { |
| 269 | } |
| 270 | |
| 271 | WindowSurfaceVk::~WindowSurfaceVk() |
| 272 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 273 | ASSERT(mSurface == VK_NULL_HANDLE); |
| 274 | ASSERT(mSwapchain == VK_NULL_HANDLE); |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 277 | void WindowSurfaceVk::destroy(const egl::Display *display) |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 278 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 279 | const DisplayVk *displayVk = vk::GetImpl(display); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 280 | RendererVk *renderer = displayVk->getRenderer(); |
| 281 | VkDevice device = renderer->getDevice(); |
| 282 | VkInstance instance = renderer->getInstance(); |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 283 | |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 284 | // We might not need to flush the pipe here. |
| 285 | renderer->finish(display->getProxyContext()); |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame] | 286 | |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 287 | mAcquireNextImageSemaphore.destroy(device); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 288 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 289 | mDepthStencilImage.release(renderer->getCurrentQueueSerial(), renderer); |
| 290 | mDepthStencilImageView.destroy(device); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 291 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 292 | for (SwapchainImage &swapchainImage : mSwapchainImages) |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 293 | { |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 294 | // Although we don't own the swapchain image handles, we need to keep our shutdown clean. |
Jamie Madill | 858c1cc | 2018-03-31 14:19:13 -0400 | [diff] [blame] | 295 | swapchainImage.image.resetImageWeakReference(); |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 296 | swapchainImage.image.destroy(device); |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 297 | swapchainImage.imageView.destroy(device); |
| 298 | swapchainImage.framebuffer.destroy(device); |
| 299 | swapchainImage.imageAcquiredSemaphore.destroy(device); |
| 300 | swapchainImage.commandsCompleteSemaphore.destroy(device); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 301 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 302 | |
| 303 | if (mSwapchain) |
| 304 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 305 | vkDestroySwapchainKHR(device, mSwapchain, nullptr); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 306 | mSwapchain = VK_NULL_HANDLE; |
| 307 | } |
| 308 | |
| 309 | if (mSurface) |
| 310 | { |
Jamie Madill | 70ee0f6 | 2017-02-06 16:04:20 -0500 | [diff] [blame] | 311 | vkDestroySurfaceKHR(instance, mSurface, nullptr); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 312 | mSurface = VK_NULL_HANDLE; |
| 313 | } |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 316 | egl::Error WindowSurfaceVk::initialize(const egl::Display *display) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 317 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 318 | const DisplayVk *displayVk = vk::GetImpl(display); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 319 | return initializeImpl(displayVk->getRenderer()).toEGL(EGL_BAD_SURFACE); |
| 320 | } |
| 321 | |
| 322 | vk::Error WindowSurfaceVk::initializeImpl(RendererVk *renderer) |
| 323 | { |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 324 | gl::Extents windowSize; |
| 325 | ANGLE_TRY_RESULT(createSurfaceVk(renderer), windowSize); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 326 | |
| 327 | uint32_t presentQueue = 0; |
| 328 | ANGLE_TRY_RESULT(renderer->selectPresentQueueForSurface(mSurface), presentQueue); |
Jamie Madill | c6dbc25 | 2018-04-30 19:07:56 -0400 | [diff] [blame] | 329 | ANGLE_UNUSED_VARIABLE(presentQueue); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 330 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 331 | const VkPhysicalDevice &physicalDevice = renderer->getPhysicalDevice(); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 332 | |
| 333 | VkSurfaceCapabilitiesKHR surfaceCaps; |
| 334 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, mSurface, &surfaceCaps)); |
| 335 | |
| 336 | // Adjust width and height to the swapchain if necessary. |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 337 | uint32_t width = surfaceCaps.currentExtent.width; |
| 338 | uint32_t height = surfaceCaps.currentExtent.height; |
| 339 | |
| 340 | // TODO(jmadill): Support devices which don't support copy. We use this for ReadPixels. |
| 341 | ANGLE_VK_CHECK((surfaceCaps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) != 0, |
| 342 | VK_ERROR_INITIALIZATION_FAILED); |
| 343 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 344 | EGLAttrib attribWidth = mState.attributes.get(EGL_WIDTH, 0); |
| 345 | EGLAttrib attribHeight = mState.attributes.get(EGL_HEIGHT, 0); |
| 346 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 347 | if (surfaceCaps.currentExtent.width == 0xFFFFFFFFu) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 348 | { |
| 349 | ASSERT(surfaceCaps.currentExtent.height == 0xFFFFFFFFu); |
| 350 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 351 | if (attribWidth == 0) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 352 | { |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 353 | width = windowSize.width; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 354 | } |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 355 | if (attribHeight == 0) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 356 | { |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 357 | height = windowSize.height; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 361 | gl::Extents extents(static_cast<int>(width), static_cast<int>(height), 1); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 362 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 363 | uint32_t presentModeCount = 0; |
| 364 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, mSurface, |
| 365 | &presentModeCount, nullptr)); |
| 366 | ASSERT(presentModeCount > 0); |
| 367 | |
| 368 | std::vector<VkPresentModeKHR> presentModes(presentModeCount); |
| 369 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, mSurface, |
| 370 | &presentModeCount, presentModes.data())); |
| 371 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 372 | // Select appropriate present mode based on vsync parameter. |
| 373 | // TODO(jmadill): More complete implementation, which allows for changing and more values. |
| 374 | const EGLint minSwapInterval = mState.config->minSwapInterval; |
| 375 | const EGLint maxSwapInterval = mState.config->maxSwapInterval; |
| 376 | ASSERT(minSwapInterval == 0 || minSwapInterval == 1); |
| 377 | ASSERT(maxSwapInterval == 0 || maxSwapInterval == 1); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 378 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 379 | VkPresentModeKHR swapchainPresentMode = |
| 380 | GetDesiredPresentMode(presentModes, minSwapInterval, maxSwapInterval); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 381 | |
| 382 | // Determine number of swapchain images. Aim for one more than the minimum. |
| 383 | uint32_t minImageCount = surfaceCaps.minImageCount + 1; |
| 384 | if (surfaceCaps.maxImageCount > 0 && minImageCount > surfaceCaps.maxImageCount) |
| 385 | { |
| 386 | minImageCount = surfaceCaps.maxImageCount; |
| 387 | } |
| 388 | |
| 389 | // Default to identity transform. |
| 390 | VkSurfaceTransformFlagBitsKHR preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 391 | if ((surfaceCaps.supportedTransforms & preTransform) == 0) |
| 392 | { |
| 393 | preTransform = surfaceCaps.currentTransform; |
| 394 | } |
| 395 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 396 | uint32_t surfaceFormatCount = 0; |
| 397 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &surfaceFormatCount, |
| 398 | nullptr)); |
| 399 | |
| 400 | std::vector<VkSurfaceFormatKHR> surfaceFormats(surfaceFormatCount); |
| 401 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &surfaceFormatCount, |
| 402 | surfaceFormats.data())); |
| 403 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 404 | const vk::Format &format = renderer->getFormat(mState.config->renderTargetFormat); |
| 405 | VkFormat nativeFormat = format.vkTextureFormat; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 406 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 407 | if (surfaceFormatCount == 1u && surfaceFormats[0].format == VK_FORMAT_UNDEFINED) |
| 408 | { |
| 409 | // This is fine. |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | bool foundFormat = false; |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 414 | for (const VkSurfaceFormatKHR &surfaceFormat : surfaceFormats) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 415 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 416 | if (surfaceFormat.format == nativeFormat) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 417 | { |
| 418 | foundFormat = true; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | ANGLE_VK_CHECK(foundFormat, VK_ERROR_INITIALIZATION_FAILED); |
| 424 | } |
| 425 | |
Yuly Novikov | 12da5e7 | 2018-01-23 18:34:53 -0500 | [diff] [blame] | 426 | VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
| 427 | if ((surfaceCaps.supportedCompositeAlpha & compositeAlpha) == 0) |
| 428 | { |
| 429 | compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; |
| 430 | } |
| 431 | ANGLE_VK_CHECK((surfaceCaps.supportedCompositeAlpha & compositeAlpha) != 0, |
| 432 | VK_ERROR_INITIALIZATION_FAILED); |
| 433 | |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 434 | // We need transfer src for reading back from the backbuffer. |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 435 | VkImageUsageFlags imageUsageFlags = kSurfaceVKColorImageUsageFlags; |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 436 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 437 | VkSwapchainCreateInfoKHR swapchainInfo; |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 438 | swapchainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 439 | swapchainInfo.pNext = nullptr; |
| 440 | swapchainInfo.flags = 0; |
| 441 | swapchainInfo.surface = mSurface; |
| 442 | swapchainInfo.minImageCount = minImageCount; |
| 443 | swapchainInfo.imageFormat = nativeFormat; |
| 444 | swapchainInfo.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
| 445 | swapchainInfo.imageExtent.width = width; |
| 446 | swapchainInfo.imageExtent.height = height; |
| 447 | swapchainInfo.imageArrayLayers = 1; |
| 448 | swapchainInfo.imageUsage = imageUsageFlags; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 449 | swapchainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 450 | swapchainInfo.queueFamilyIndexCount = 0; |
| 451 | swapchainInfo.pQueueFamilyIndices = nullptr; |
| 452 | swapchainInfo.preTransform = preTransform; |
Yuly Novikov | 12da5e7 | 2018-01-23 18:34:53 -0500 | [diff] [blame] | 453 | swapchainInfo.compositeAlpha = compositeAlpha; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 454 | swapchainInfo.presentMode = swapchainPresentMode; |
| 455 | swapchainInfo.clipped = VK_TRUE; |
| 456 | swapchainInfo.oldSwapchain = VK_NULL_HANDLE; |
| 457 | |
Jamie Madill | 6a89d22 | 2017-11-02 11:59:51 -0400 | [diff] [blame] | 458 | VkDevice device = renderer->getDevice(); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 459 | ANGLE_VK_TRY(vkCreateSwapchainKHR(device, &swapchainInfo, nullptr, &mSwapchain)); |
| 460 | |
| 461 | // Intialize the swapchain image views. |
| 462 | uint32_t imageCount = 0; |
| 463 | ANGLE_VK_TRY(vkGetSwapchainImagesKHR(device, mSwapchain, &imageCount, nullptr)); |
| 464 | |
| 465 | std::vector<VkImage> swapchainImages(imageCount); |
| 466 | ANGLE_VK_TRY(vkGetSwapchainImagesKHR(device, mSwapchain, &imageCount, swapchainImages.data())); |
| 467 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 468 | // Allocate a command buffer for clearing our images to black. |
| 469 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 470 | ANGLE_TRY(beginWriteResource(renderer, &commandBuffer)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 471 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 472 | VkClearColorValue transparentBlack; |
| 473 | transparentBlack.float32[0] = 0.0f; |
| 474 | transparentBlack.float32[1] = 0.0f; |
| 475 | transparentBlack.float32[2] = 0.0f; |
| 476 | transparentBlack.float32[3] = 0.0f; |
| 477 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 478 | mSwapchainImages.resize(imageCount); |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 479 | |
| 480 | ANGLE_TRY(mAcquireNextImageSemaphore.init(device)); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 481 | |
| 482 | for (uint32_t imageIndex = 0; imageIndex < imageCount; ++imageIndex) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 483 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 484 | SwapchainImage &member = mSwapchainImages[imageIndex]; |
| 485 | member.image.init2DWeakReference(swapchainImages[imageIndex], extents, format, 1); |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 486 | member.image.initImageView(device, gl::TextureType::_2D, VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 487 | gl::SwizzleState(), &member.imageView, 1); |
Jamie Madill | 25301b6 | 2017-10-28 20:59:31 -0400 | [diff] [blame] | 488 | |
| 489 | // Set transfer dest layout, and clear the image to black. |
Luc Ferron | c20b950 | 2018-05-24 09:30:17 -0400 | [diff] [blame] | 490 | member.image.clearColor(transparentBlack, 0, 1, commandBuffer); |
Jamie Madill | 25301b6 | 2017-10-28 20:59:31 -0400 | [diff] [blame] | 491 | |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 492 | ANGLE_TRY(member.imageAcquiredSemaphore.init(device)); |
| 493 | ANGLE_TRY(member.commandsCompleteSemaphore.init(device)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 494 | } |
| 495 | |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame] | 496 | // Get the first available swapchain iamge. |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 497 | ANGLE_TRY(nextSwapchainImage(renderer)); |
| 498 | |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 499 | // Initialize depth/stencil if requested. |
| 500 | if (mState.config->depthStencilFormat != GL_NONE) |
| 501 | { |
| 502 | const vk::Format &dsFormat = renderer->getFormat(mState.config->depthStencilFormat); |
| 503 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 504 | const VkImageUsageFlags dsUsage = kSurfaceVKDepthStencilImageUsageFlags; |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 505 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame^] | 506 | ANGLE_TRY(mDepthStencilImage.init(device, gl::TextureType::_2D, extents, dsFormat, 1, |
| 507 | dsUsage, 1)); |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 508 | ANGLE_TRY(mDepthStencilImage.initMemory(device, renderer->getMemoryProperties(), |
| 509 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 510 | |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 511 | const VkImageAspectFlags aspect = vk::GetDepthStencilAspectFlags(dsFormat.textureFormat()); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 512 | VkClearDepthStencilValue depthStencilClearValue = {1.0f, 0}; |
| 513 | |
| 514 | // Set transfer dest layout, and clear the image. |
Jamie Madill | 858c1cc | 2018-03-31 14:19:13 -0400 | [diff] [blame] | 515 | mDepthStencilImage.clearDepthStencil(aspect, depthStencilClearValue, commandBuffer); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 516 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 517 | ANGLE_TRY(mDepthStencilImage.initImageView(device, gl::TextureType::_2D, aspect, |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 518 | gl::SwizzleState(), &mDepthStencilImageView, 1)); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 519 | |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 520 | // TODO(jmadill): Figure out how to pass depth/stencil image views to the RenderTargetVk. |
| 521 | } |
| 522 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 523 | return vk::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 524 | } |
| 525 | |
Geoff Lang | bf7b95d | 2018-05-01 16:48:21 -0400 | [diff] [blame] | 526 | FramebufferImpl *WindowSurfaceVk::createDefaultFramebuffer(const gl::Context *context, |
| 527 | const gl::FramebufferState &state) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 528 | { |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 529 | return FramebufferVk::CreateDefaultFBO(state, this); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 530 | } |
| 531 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 532 | egl::Error WindowSurfaceVk::swap(const gl::Context *context) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 533 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 534 | const DisplayVk *displayVk = vk::GetImpl(context->getCurrentDisplay()); |
Jamie Madill | d482615 | 2017-09-21 11:18:59 -0400 | [diff] [blame] | 535 | RendererVk *renderer = displayVk->getRenderer(); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 536 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 537 | vk::CommandBuffer *swapCommands = nullptr; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 538 | ANGLE_TRY(beginWriteResource(renderer, &swapCommands)); |
Jamie Madill | d482615 | 2017-09-21 11:18:59 -0400 | [diff] [blame] | 539 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 540 | SwapchainImage &image = mSwapchainImages[mCurrentSwapchainImageIndex]; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 541 | |
Jamie Madill | 858c1cc | 2018-03-31 14:19:13 -0400 | [diff] [blame] | 542 | image.image.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
| 543 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 544 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, swapCommands); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 545 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 546 | ANGLE_TRY( |
| 547 | renderer->flush(context, image.imageAcquiredSemaphore, image.commandsCompleteSemaphore)); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 548 | |
| 549 | VkPresentInfoKHR presentInfo; |
| 550 | presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; |
| 551 | presentInfo.pNext = nullptr; |
Jamie Madill | e918de2 | 2017-04-12 10:21:11 -0400 | [diff] [blame] | 552 | presentInfo.waitSemaphoreCount = 1; |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 553 | presentInfo.pWaitSemaphores = image.commandsCompleteSemaphore.ptr(); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 554 | presentInfo.swapchainCount = 1; |
| 555 | presentInfo.pSwapchains = &mSwapchain; |
| 556 | presentInfo.pImageIndices = &mCurrentSwapchainImageIndex; |
| 557 | presentInfo.pResults = nullptr; |
| 558 | |
| 559 | ANGLE_VK_TRY(vkQueuePresentKHR(renderer->getQueue(), &presentInfo)); |
| 560 | |
Jamie Madill | f0eafe1 | 2017-02-21 15:03:50 -0500 | [diff] [blame] | 561 | // Get the next available swapchain image. |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 562 | ANGLE_TRY(nextSwapchainImage(renderer)); |
| 563 | |
| 564 | return vk::NoError(); |
| 565 | } |
| 566 | |
| 567 | vk::Error WindowSurfaceVk::nextSwapchainImage(RendererVk *renderer) |
| 568 | { |
| 569 | VkDevice device = renderer->getDevice(); |
| 570 | |
jchen10 | 191a84a | 2018-01-31 16:09:45 +0800 | [diff] [blame] | 571 | ANGLE_VK_TRY(vkAcquireNextImageKHR(device, mSwapchain, UINT64_MAX, |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 572 | mAcquireNextImageSemaphore.getHandle(), VK_NULL_HANDLE, |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 573 | &mCurrentSwapchainImageIndex)); |
| 574 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 575 | SwapchainImage &image = mSwapchainImages[mCurrentSwapchainImageIndex]; |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 576 | |
| 577 | // Swap the unused swapchain semaphore and the now active spare semaphore. |
| 578 | std::swap(image.imageAcquiredSemaphore, mAcquireNextImageSemaphore); |
| 579 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 580 | // Update RenderTarget pointers. |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 581 | mColorRenderTarget.updateSwapchainImage(&image.image, &image.imageView); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 582 | |
| 583 | return vk::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 584 | } |
| 585 | |
Jamie Madill | fe54834 | 2017-06-19 11:13:24 -0400 | [diff] [blame] | 586 | egl::Error WindowSurfaceVk::postSubBuffer(const gl::Context *context, |
| 587 | EGLint x, |
| 588 | EGLint y, |
| 589 | EGLint width, |
| 590 | EGLint height) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 591 | { |
| 592 | // TODO(jmadill) |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 593 | return egl::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | egl::Error WindowSurfaceVk::querySurfacePointerANGLE(EGLint attribute, void **value) |
| 597 | { |
| 598 | UNREACHABLE(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 599 | return egl::EglBadCurrentSurface(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 600 | } |
| 601 | |
Geoff Lang | ccafa62 | 2018-05-02 13:07:53 -0400 | [diff] [blame] | 602 | egl::Error WindowSurfaceVk::bindTexImage(const gl::Context *context, |
| 603 | gl::Texture *texture, |
| 604 | EGLint buffer) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 605 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 606 | return egl::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 607 | } |
| 608 | |
Geoff Lang | ccafa62 | 2018-05-02 13:07:53 -0400 | [diff] [blame] | 609 | egl::Error WindowSurfaceVk::releaseTexImage(const gl::Context *context, EGLint buffer) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 610 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 611 | return egl::NoError(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 612 | } |
| 613 | |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 614 | egl::Error WindowSurfaceVk::getSyncValues(EGLuint64KHR * /*ust*/, |
| 615 | EGLuint64KHR * /*msc*/, |
| 616 | EGLuint64KHR * /*sbc*/) |
| 617 | { |
| 618 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 619 | return egl::EglBadAccess(); |
Stanislav Chiknavaryan | ee218f2 | 2017-03-22 15:39:13 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 622 | void WindowSurfaceVk::setSwapInterval(EGLint interval) |
| 623 | { |
| 624 | } |
| 625 | |
| 626 | EGLint WindowSurfaceVk::getWidth() const |
| 627 | { |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 628 | return static_cast<EGLint>(mColorRenderTarget.getImageExtents().width); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | EGLint WindowSurfaceVk::getHeight() const |
| 632 | { |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 633 | return static_cast<EGLint>(mColorRenderTarget.getImageExtents().height); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | EGLint WindowSurfaceVk::isPostSubBufferSupported() const |
| 637 | { |
| 638 | // TODO(jmadill) |
| 639 | return EGL_FALSE; |
| 640 | } |
| 641 | |
| 642 | EGLint WindowSurfaceVk::getSwapBehavior() const |
| 643 | { |
| 644 | // TODO(jmadill) |
| 645 | return EGL_BUFFER_DESTROYED; |
| 646 | } |
| 647 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 648 | gl::Error WindowSurfaceVk::getAttachmentRenderTarget(const gl::Context * /*context*/, |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 649 | GLenum binding, |
Jamie Madill | 4fd95d5 | 2017-04-05 11:22:18 -0400 | [diff] [blame] | 650 | const gl::ImageIndex & /*target*/, |
| 651 | FramebufferAttachmentRenderTarget **rtOut) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 652 | { |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 653 | if (binding == GL_BACK) |
| 654 | { |
| 655 | *rtOut = &mColorRenderTarget; |
| 656 | } |
| 657 | else |
| 658 | { |
| 659 | ASSERT(binding == GL_DEPTH || binding == GL_STENCIL || binding == GL_DEPTH_STENCIL); |
| 660 | *rtOut = &mDepthStencilRenderTarget; |
| 661 | } |
| 662 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 663 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 664 | } |
| 665 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 666 | gl::ErrorOrResult<vk::Framebuffer *> WindowSurfaceVk::getCurrentFramebuffer( |
| 667 | VkDevice device, |
| 668 | const vk::RenderPass &compatibleRenderPass) |
| 669 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 670 | vk::Framebuffer ¤tFramebuffer = mSwapchainImages[mCurrentSwapchainImageIndex].framebuffer; |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 671 | |
| 672 | if (currentFramebuffer.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 673 | { |
| 674 | // Validation layers should detect if the render pass is really compatible. |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 675 | return ¤tFramebuffer; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | VkFramebufferCreateInfo framebufferInfo; |
| 679 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 680 | const gl::Extents &extents = mColorRenderTarget.getImageExtents(); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 681 | std::array<VkImageView, 2> imageViews = {{VK_NULL_HANDLE, mDepthStencilImageView.getHandle()}}; |
| 682 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 683 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 684 | framebufferInfo.pNext = nullptr; |
| 685 | framebufferInfo.flags = 0; |
| 686 | framebufferInfo.renderPass = compatibleRenderPass.getHandle(); |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 687 | framebufferInfo.attachmentCount = (mDepthStencilImage.valid() ? 2u : 1u); |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 688 | framebufferInfo.pAttachments = imageViews.data(); |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 689 | framebufferInfo.width = static_cast<uint32_t>(extents.width); |
| 690 | framebufferInfo.height = static_cast<uint32_t>(extents.height); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 691 | framebufferInfo.layers = 1; |
| 692 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 693 | for (SwapchainImage &swapchainImage : mSwapchainImages) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 694 | { |
Jamie Madill | f618c9e | 2018-02-15 14:45:40 -0500 | [diff] [blame] | 695 | imageViews[0] = swapchainImage.imageView.getHandle(); |
Jamie Madill | 25301b6 | 2017-10-28 20:59:31 -0400 | [diff] [blame] | 696 | ANGLE_TRY(swapchainImage.framebuffer.init(device, framebufferInfo)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 697 | } |
| 698 | |
Jamie Madill | a9c60e9 | 2017-09-28 19:06:39 -0400 | [diff] [blame] | 699 | ASSERT(currentFramebuffer.valid()); |
| 700 | return ¤tFramebuffer; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 701 | } |
| 702 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 703 | gl::Error WindowSurfaceVk::initializeContents(const gl::Context *context, |
| 704 | const gl::ImageIndex &imageIndex) |
| 705 | { |
| 706 | UNIMPLEMENTED(); |
| 707 | return gl::NoError(); |
| 708 | } |
| 709 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 710 | } // namespace rx |