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