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 | // FramebufferVk.cpp: |
| 7 | // Implements the class methods for FramebufferVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
| 11 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 12 | #include <vulkan/vulkan.h> |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 13 | #include <array> |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 14 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 15 | #include "common/debug.h" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 16 | #include "libANGLE/Context.h" |
| 17 | #include "libANGLE/Display.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 18 | #include "libANGLE/formatutils.h" |
| 19 | #include "libANGLE/renderer/renderer_utils.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 21 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 22 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 23 | #include "libANGLE/renderer/vulkan/RenderTargetVk.h" |
| 24 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
| 25 | #include "libANGLE/renderer/vulkan/SurfaceVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 26 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | 3ea463b | 2019-06-19 14:21:33 -0400 | [diff] [blame] | 27 | #include "libANGLE/trace.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 28 | |
| 29 | namespace rx |
| 30 | { |
| 31 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 32 | namespace |
| 33 | { |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 34 | // The value to assign an alpha channel that's emulated. The type is unsigned int, though it will |
| 35 | // automatically convert to the actual data type. |
| 36 | constexpr unsigned int kEmulatedAlphaValue = 1; |
| 37 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 38 | constexpr size_t kMinReadPixelsBufferSize = 128000; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 39 | // Clear values are only used when loadOp=Clear is set in clearWithRenderPassOp. When starting a |
| 40 | // new render pass, the clear value is set to an unlikely value (bright pink) to stand out better |
| 41 | // in case of a bug. |
| 42 | constexpr VkClearValue kUninitializedClearValue = {{{0.95, 0.05, 0.95, 0.95}}}; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 43 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 44 | const gl::InternalFormat &GetReadAttachmentInfo(const gl::Context *context, |
| 45 | RenderTargetVk *renderTarget) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 46 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 47 | GLenum implFormat = |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 48 | renderTarget->getImageFormat().imageFormat().fboImplementationInternalFormat; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 49 | return gl::GetSizedInternalFormatInfo(implFormat); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 50 | } |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 51 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 52 | bool HasSrcBlitFeature(RendererVk *renderer, RenderTargetVk *srcRenderTarget) |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 53 | { |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 54 | const VkFormat srcFormat = srcRenderTarget->getImageFormat().vkImageFormat; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 55 | return renderer->hasImageFormatFeatureBits(srcFormat, VK_FORMAT_FEATURE_BLIT_SRC_BIT); |
| 56 | } |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 57 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 58 | bool HasDstBlitFeature(RendererVk *renderer, RenderTargetVk *dstRenderTarget) |
| 59 | { |
| 60 | const VkFormat dstFormat = dstRenderTarget->getImageFormat().vkImageFormat; |
| 61 | return renderer->hasImageFormatFeatureBits(dstFormat, VK_FORMAT_FEATURE_BLIT_DST_BIT); |
| 62 | } |
| 63 | |
| 64 | // Returns false if destination has any channel the source doesn't. This means that channel was |
| 65 | // emulated and using the Vulkan blit command would overwrite that emulated channel. |
| 66 | bool areSrcAndDstColorChannelsBlitCompatible(RenderTargetVk *srcRenderTarget, |
| 67 | RenderTargetVk *dstRenderTarget) |
| 68 | { |
| 69 | const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat(); |
| 70 | const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat(); |
| 71 | |
| 72 | // Luminance/alpha formats are not renderable, so they can't have ended up in a framebuffer to |
| 73 | // participate in a blit. |
| 74 | ASSERT(!dstFormat.isLUMA() && !srcFormat.isLUMA()); |
| 75 | |
| 76 | // All color formats have the red channel. |
| 77 | ASSERT(dstFormat.redBits > 0 && srcFormat.redBits > 0); |
| 78 | |
| 79 | return (dstFormat.greenBits > 0 || srcFormat.greenBits == 0) && |
| 80 | (dstFormat.blueBits > 0 || srcFormat.blueBits == 0) && |
| 81 | (dstFormat.alphaBits > 0 || srcFormat.alphaBits == 0); |
| 82 | } |
| 83 | |
| 84 | bool areSrcAndDstDepthStencilChannelsBlitCompatible(RenderTargetVk *srcRenderTarget, |
| 85 | RenderTargetVk *dstRenderTarget) |
| 86 | { |
| 87 | const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat(); |
| 88 | const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat(); |
| 89 | |
| 90 | return (dstFormat.depthBits > 0 || srcFormat.depthBits == 0) && |
| 91 | (dstFormat.stencilBits > 0 || srcFormat.stencilBits == 0); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 92 | } |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 93 | |
| 94 | // Special rules apply to VkBufferImageCopy with depth/stencil. The components are tightly packed |
| 95 | // into a depth or stencil section of the destination buffer. See the spec: |
| 96 | // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkBufferImageCopy.html |
| 97 | const angle::Format &GetDepthStencilImageToBufferFormat(const angle::Format &imageFormat, |
| 98 | VkImageAspectFlagBits copyAspect) |
| 99 | { |
| 100 | if (copyAspect == VK_IMAGE_ASPECT_STENCIL_BIT) |
| 101 | { |
| 102 | ASSERT(imageFormat.id == angle::FormatID::D24_UNORM_S8_UINT || |
| 103 | imageFormat.id == angle::FormatID::D32_FLOAT_S8X24_UINT || |
| 104 | imageFormat.id == angle::FormatID::S8_UINT); |
| 105 | return angle::Format::Get(angle::FormatID::S8_UINT); |
| 106 | } |
| 107 | |
| 108 | ASSERT(copyAspect == VK_IMAGE_ASPECT_DEPTH_BIT); |
| 109 | |
| 110 | switch (imageFormat.id) |
| 111 | { |
| 112 | case angle::FormatID::D16_UNORM: |
| 113 | return imageFormat; |
| 114 | case angle::FormatID::D24_UNORM_X8_UINT: |
| 115 | return imageFormat; |
| 116 | case angle::FormatID::D24_UNORM_S8_UINT: |
| 117 | return angle::Format::Get(angle::FormatID::D24_UNORM_X8_UINT); |
| 118 | case angle::FormatID::D32_FLOAT: |
| 119 | return imageFormat; |
| 120 | case angle::FormatID::D32_FLOAT_S8X24_UINT: |
| 121 | return angle::Format::Get(angle::FormatID::D32_FLOAT); |
| 122 | default: |
| 123 | UNREACHABLE(); |
| 124 | return imageFormat; |
| 125 | } |
| 126 | } |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 127 | |
| 128 | void SetEmulatedAlphaValue(const vk::Format &format, VkClearColorValue *value) |
| 129 | { |
| 130 | if (format.vkFormatIsInt) |
| 131 | { |
| 132 | if (format.vkFormatIsUnsigned) |
| 133 | { |
| 134 | value->uint32[3] = kEmulatedAlphaValue; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | value->int32[3] = kEmulatedAlphaValue; |
| 139 | } |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | value->float32[3] = kEmulatedAlphaValue; |
| 144 | } |
| 145 | } |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 146 | } // anonymous namespace |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 147 | |
| 148 | // static |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 149 | FramebufferVk *FramebufferVk::CreateUserFBO(RendererVk *renderer, const gl::FramebufferState &state) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 150 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 151 | return new FramebufferVk(renderer, state, nullptr); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // static |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 155 | FramebufferVk *FramebufferVk::CreateDefaultFBO(RendererVk *renderer, |
| 156 | const gl::FramebufferState &state, |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 157 | WindowSurfaceVk *backbuffer) |
| 158 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 159 | return new FramebufferVk(renderer, state, backbuffer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 162 | FramebufferVk::FramebufferVk(RendererVk *renderer, |
| 163 | const gl::FramebufferState &state, |
| 164 | WindowSurfaceVk *backbuffer) |
Jamie Madill | 7f2520f | 2019-06-26 11:18:33 -0400 | [diff] [blame] | 165 | : FramebufferImpl(state), mBackbuffer(backbuffer), mActiveColorComponents(0) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 166 | { |
Jamie Madill | 7f2520f | 2019-06-26 11:18:33 -0400 | [diff] [blame] | 167 | mReadPixelBuffer.init(renderer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, 4, kMinReadPixelsBufferSize, |
| 168 | true); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 171 | FramebufferVk::~FramebufferVk() = default; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 172 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 173 | void FramebufferVk::destroy(const gl::Context *context) |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 174 | { |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 175 | ContextVk *contextVk = vk::GetImpl(context); |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 176 | mFramebuffer.release(contextVk); |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 177 | |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 178 | mReadPixelBuffer.release(contextVk); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 181 | angle::Result FramebufferVk::discard(const gl::Context *context, |
| 182 | size_t count, |
| 183 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 184 | { |
Shahbaz Youssefi | 97123e3 | 2019-07-08 15:11:16 -0400 | [diff] [blame] | 185 | return invalidate(context, count, attachments); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 188 | angle::Result FramebufferVk::invalidate(const gl::Context *context, |
| 189 | size_t count, |
| 190 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 191 | { |
Shahbaz Youssefi | da90448 | 2019-07-02 10:49:14 -0400 | [diff] [blame] | 192 | mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial()); |
| 193 | |
| 194 | if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass()) |
| 195 | { |
| 196 | invalidateImpl(vk::GetImpl(context), count, attachments); |
| 197 | } |
| 198 | |
| 199 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 202 | angle::Result FramebufferVk::invalidateSub(const gl::Context *context, |
| 203 | size_t count, |
| 204 | const GLenum *attachments, |
| 205 | const gl::Rectangle &area) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 206 | { |
Shahbaz Youssefi | da90448 | 2019-07-02 10:49:14 -0400 | [diff] [blame] | 207 | mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial()); |
| 208 | |
| 209 | // RenderPass' storeOp cannot be made conditional to a specific region, so we only apply this |
| 210 | // hint if the requested area encompasses the render area. |
| 211 | if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass() && |
| 212 | area.encloses(mFramebuffer.getRenderPassRenderArea())) |
| 213 | { |
| 214 | invalidateImpl(vk::GetImpl(context), count, attachments); |
| 215 | } |
| 216 | |
| 217 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 220 | angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 221 | { |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 222 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 223 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 224 | bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT)); |
| 225 | bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT)); |
| 226 | bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT)); |
| 227 | gl::DrawBufferMask clearColorBuffers; |
| 228 | if (clearColor) |
| 229 | { |
| 230 | clearColorBuffers = mState.getEnabledDrawBuffers(); |
| 231 | } |
| 232 | |
| 233 | const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color; |
| 234 | const VkClearDepthStencilValue &clearDepthStencilValue = |
| 235 | contextVk->getClearDepthStencilValue().depthStencil; |
| 236 | |
| 237 | return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue, |
| 238 | clearDepthStencilValue); |
| 239 | } |
| 240 | |
| 241 | angle::Result FramebufferVk::clearImpl(const gl::Context *context, |
| 242 | gl::DrawBufferMask clearColorBuffers, |
| 243 | bool clearDepth, |
| 244 | bool clearStencil, |
| 245 | const VkClearColorValue &clearColorValue, |
| 246 | const VkClearDepthStencilValue &clearDepthStencilValue) |
| 247 | { |
| 248 | ContextVk *contextVk = vk::GetImpl(context); |
| 249 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 250 | const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk); |
| 251 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 252 | // Discard clear altogether if scissor has 0 width or height. |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 253 | if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 254 | { |
| 255 | return angle::Result::Continue; |
| 256 | } |
| 257 | |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 258 | mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial()); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 259 | |
| 260 | // This function assumes that only enabled attachments are asked to be cleared. |
| 261 | ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers); |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 262 | |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 263 | // Adjust clear behavior based on whether the respective attachments are present; if asked to |
| 264 | // clear a non-existent attachment, don't attempt to clear it. |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 265 | |
| 266 | VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask(); |
| 267 | bool clearColor = clearColorBuffers.any(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 268 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 269 | const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 270 | clearDepth = clearDepth && depthAttachment; |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 271 | ASSERT(!clearDepth || depthAttachment->isAttached()); |
| 272 | |
| 273 | const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 274 | clearStencil = clearStencil && stencilAttachment; |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 275 | ASSERT(!clearStencil || stencilAttachment->isAttached()); |
| 276 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 277 | uint8_t stencilMask = |
| 278 | static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask); |
| 279 | |
| 280 | // The front-end should ensure we don't attempt to clear color if all channels are masked. |
| 281 | ASSERT(!clearColor || colorMaskFlags != 0); |
| 282 | // The front-end should ensure we don't attempt to clear depth if depth write is disabled. |
| 283 | ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask); |
| 284 | // The front-end should ensure we don't attempt to clear stencil if all bits are masked. |
| 285 | ASSERT(!clearStencil || stencilMask != 0); |
| 286 | |
| 287 | // If there is nothing to clear, return right away (for example, if asked to clear depth, but |
| 288 | // there is no depth attachment). |
Shahbaz Youssefi | 02a579e | 2019-03-27 14:21:20 -0400 | [diff] [blame] | 289 | if (!clearColor && !clearDepth && !clearStencil) |
| 290 | { |
| 291 | return angle::Result::Continue; |
| 292 | } |
| 293 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 294 | VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue; |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 295 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 296 | // We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If |
| 297 | // there's a depth mask, depth clearing is already disabled. |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 298 | bool maskedClearColor = |
| 299 | clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 300 | bool maskedClearStencil = stencilMask != 0xFF; |
| 301 | |
| 302 | bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor; |
| 303 | bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 304 | |
| 305 | // At least one of color, depth or stencil should be clearable with render pass loadOp for us |
| 306 | // to use this clear path. |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 307 | bool clearAnyWithRenderPassLoadOp = |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 308 | clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 309 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 310 | if (clearAnyWithRenderPassLoadOp) |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 311 | { |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 312 | // Clearing color is indicated by the set bits in this mask. If not clearing colors with |
| 313 | // render pass loadOp, the default value of all-zeros means the clear is not done in |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 314 | // clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass |
| 315 | // loadOp. |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 316 | gl::DrawBufferMask clearBuffersWithRenderPassLoadOp; |
| 317 | if (clearColorWithRenderPassLoadOp) |
| 318 | { |
| 319 | clearBuffersWithRenderPassLoadOp = clearColorBuffers; |
| 320 | } |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 321 | ANGLE_TRY(clearWithRenderPassOp( |
| 322 | contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth, |
| 323 | clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue)); |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 324 | |
Shahbaz Youssefi | 27f115a | 2019-04-01 10:33:21 -0400 | [diff] [blame] | 325 | // On some hardware, having inline commands at this point results in corrupted output. In |
| 326 | // that case, end the render pass immediately. http://anglebug.com/2361 |
Jonah Ryan-Davis | 776694c | 2019-05-08 10:28:55 -0400 | [diff] [blame] | 327 | if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled) |
Shahbaz Youssefi | 27f115a | 2019-04-01 10:33:21 -0400 | [diff] [blame] | 328 | { |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 329 | mFramebuffer.finishCurrentCommands(contextVk); |
Shahbaz Youssefi | 27f115a | 2019-04-01 10:33:21 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 332 | // Fallback to other methods for whatever isn't cleared here. |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 333 | clearDepth = false; |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 334 | if (clearColorWithRenderPassLoadOp) |
| 335 | { |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 336 | clearColorBuffers.reset(); |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 337 | clearColor = false; |
| 338 | } |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 339 | if (clearStencilWithRenderPassLoadOp) |
| 340 | { |
| 341 | clearStencil = false; |
| 342 | } |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 343 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 344 | // If nothing left to clear, early out. |
| 345 | if (!clearColor && !clearStencil) |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 346 | { |
| 347 | return angle::Result::Continue; |
| 348 | } |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 351 | // Note: depth clear is always done through render pass loadOp. |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 352 | ASSERT(clearDepth == false); |
| 353 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 354 | // The most costly clear mode is when we need to mask out specific color channels or stencil |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 355 | // bits. This can only be done with a draw call. |
| 356 | return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil, |
| 357 | colorMaskFlags, stencilMask, clearColorValue, |
| 358 | static_cast<uint8_t>(modifiedDepthStencilValue.stencil)); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 359 | } |
| 360 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 361 | angle::Result FramebufferVk::clearBufferfv(const gl::Context *context, |
| 362 | GLenum buffer, |
| 363 | GLint drawbuffer, |
| 364 | const GLfloat *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 365 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 366 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 367 | |
| 368 | bool clearDepth = false; |
| 369 | gl::DrawBufferMask clearColorBuffers; |
| 370 | |
| 371 | if (buffer == GL_DEPTH) |
| 372 | { |
| 373 | clearDepth = true; |
| 374 | clearValue.depthStencil.depth = values[0]; |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | clearColorBuffers.set(drawbuffer); |
| 379 | clearValue.color.float32[0] = values[0]; |
| 380 | clearValue.color.float32[1] = values[1]; |
| 381 | clearValue.color.float32[2] = values[2]; |
| 382 | clearValue.color.float32[3] = values[3]; |
| 383 | } |
| 384 | |
| 385 | return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color, |
| 386 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 389 | angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context, |
| 390 | GLenum buffer, |
| 391 | GLint drawbuffer, |
| 392 | const GLuint *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 393 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 394 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 395 | |
| 396 | gl::DrawBufferMask clearColorBuffers; |
| 397 | clearColorBuffers.set(drawbuffer); |
| 398 | |
| 399 | clearValue.color.uint32[0] = values[0]; |
| 400 | clearValue.color.uint32[1] = values[1]; |
| 401 | clearValue.color.uint32[2] = values[2]; |
| 402 | clearValue.color.uint32[3] = values[3]; |
| 403 | |
| 404 | return clearImpl(context, clearColorBuffers, false, false, clearValue.color, |
| 405 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 408 | angle::Result FramebufferVk::clearBufferiv(const gl::Context *context, |
| 409 | GLenum buffer, |
| 410 | GLint drawbuffer, |
| 411 | const GLint *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 412 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 413 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 414 | |
| 415 | bool clearStencil = false; |
| 416 | gl::DrawBufferMask clearColorBuffers; |
| 417 | |
| 418 | if (buffer == GL_STENCIL) |
| 419 | { |
| 420 | clearStencil = true; |
| 421 | clearValue.depthStencil.stencil = |
| 422 | gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max()); |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | clearColorBuffers.set(drawbuffer); |
| 427 | clearValue.color.int32[0] = values[0]; |
| 428 | clearValue.color.int32[1] = values[1]; |
| 429 | clearValue.color.int32[2] = values[2]; |
| 430 | clearValue.color.int32[3] = values[3]; |
| 431 | } |
| 432 | |
| 433 | return clearImpl(context, clearColorBuffers, false, clearStencil, clearValue.color, |
| 434 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 435 | } |
| 436 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 437 | angle::Result FramebufferVk::clearBufferfi(const gl::Context *context, |
| 438 | GLenum buffer, |
| 439 | GLint drawbuffer, |
| 440 | GLfloat depth, |
| 441 | GLint stencil) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 442 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 443 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 444 | |
| 445 | clearValue.depthStencil.depth = depth; |
| 446 | clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max()); |
| 447 | |
| 448 | return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color, |
| 449 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 450 | } |
| 451 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 452 | GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 453 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 454 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 457 | GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 458 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 459 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 460 | } |
| 461 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 462 | angle::Result FramebufferVk::readPixels(const gl::Context *context, |
| 463 | const gl::Rectangle &area, |
| 464 | GLenum format, |
| 465 | GLenum type, |
| 466 | void *pixels) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 467 | { |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 468 | // Clip read area to framebuffer. |
| 469 | const gl::Extents &fbSize = getState().getReadAttachment()->getSize(); |
| 470 | const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 471 | ContextVk *contextVk = vk::GetImpl(context); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 472 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 473 | gl::Rectangle clippedArea; |
| 474 | if (!ClipRectangle(area, fbRect, &clippedArea)) |
| 475 | { |
| 476 | // nothing to read |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 477 | return angle::Result::Continue; |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 478 | } |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 479 | gl::Rectangle flippedArea = clippedArea; |
Geoff Lang | e076a23 | 2018-07-16 15:34:05 -0400 | [diff] [blame] | 480 | if (contextVk->isViewportFlipEnabledForReadFBO()) |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 481 | { |
| 482 | flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height; |
| 483 | } |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 484 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 485 | const gl::State &glState = context->getState(); |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 486 | const gl::PixelPackState &packState = glState.getPackState(); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 487 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 488 | const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type); |
| 489 | |
| 490 | GLuint outputPitch = 0; |
Jamie Madill | abfbc0f | 2018-10-09 12:48:52 -0400 | [diff] [blame] | 491 | ANGLE_VK_CHECK_MATH(contextVk, |
| 492 | sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment, |
| 493 | packState.rowLength, &outputPitch)); |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 494 | GLuint outputSkipBytes = 0; |
Jamie Madill | abfbc0f | 2018-10-09 12:48:52 -0400 | [diff] [blame] | 495 | ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState, |
| 496 | false, &outputSkipBytes)); |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 497 | |
| 498 | outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes + |
| 499 | (clippedArea.y - area.y) * outputPitch; |
Luc Ferron | 6028422 | 2018-03-20 16:01:44 -0400 | [diff] [blame] | 500 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 501 | const angle::Format &angleFormat = GetFormatFromFormatType(format, type); |
| 502 | |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 503 | PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder, |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 504 | glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0); |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 505 | if (contextVk->isViewportFlipEnabledForReadFBO()) |
| 506 | { |
| 507 | params.reverseRowOrder = !params.reverseRowOrder; |
| 508 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 509 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 510 | ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 511 | getColorReadRenderTarget(), |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 512 | static_cast<uint8_t *>(pixels) + outputSkipBytes)); |
Jamie Madill | c773ab9 | 2019-06-25 17:11:58 -0400 | [diff] [blame] | 513 | mReadPixelBuffer.releaseInFlightBuffers(contextVk); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 514 | return angle::Result::Continue; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 515 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 516 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 517 | RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const |
| 518 | { |
| 519 | return mRenderTargetCache.getDepthStencil(); |
| 520 | } |
| 521 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 522 | RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 523 | { |
| 524 | RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 525 | ASSERT(renderTarget && renderTarget->getImage().valid()); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 526 | return renderTarget; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 529 | angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk, |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 530 | const gl::Rectangle &sourceArea, |
| 531 | const gl::Rectangle &destArea, |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 532 | RenderTargetVk *readRenderTarget, |
| 533 | RenderTargetVk *drawRenderTarget, |
| 534 | GLenum filter, |
| 535 | bool colorBlit, |
| 536 | bool depthBlit, |
| 537 | bool stencilBlit, |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 538 | bool flipX, |
| 539 | bool flipY) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 540 | { |
| 541 | // Since blitRenderbufferRect is called for each render buffer that needs to be blitted, |
| 542 | // it should never be the case that both color and depth/stencil need to be blitted at |
| 543 | // at the same time. |
| 544 | ASSERT(colorBlit != (depthBlit || stencilBlit)); |
| 545 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 546 | vk::ImageHelper *srcImage = &readRenderTarget->getImage(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 547 | vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer); |
| 548 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 549 | VkImageAspectFlags imageAspectMask = srcImage->getAspectFlags(); |
| 550 | VkImageAspectFlags blitAspectMask = imageAspectMask; |
| 551 | |
| 552 | // Remove depth or stencil aspects if they are not requested to be blitted. |
| 553 | if (!depthBlit) |
| 554 | { |
| 555 | blitAspectMask &= ~VK_IMAGE_ASPECT_DEPTH_BIT; |
| 556 | } |
| 557 | if (!stencilBlit) |
| 558 | { |
| 559 | blitAspectMask &= ~VK_IMAGE_ASPECT_STENCIL_BIT; |
| 560 | } |
| 561 | |
| 562 | if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc)) |
| 563 | { |
| 564 | vk::CommandBuffer *srcLayoutChange; |
| 565 | ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange)); |
| 566 | srcImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferSrc, srcLayoutChange); |
| 567 | } |
| 568 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 569 | vk::CommandBuffer *commandBuffer = nullptr; |
| 570 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
| 571 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 572 | srcImage->addReadDependency(&mFramebuffer); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 573 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 574 | VkImageBlit blit = {}; |
| 575 | blit.srcSubresource.aspectMask = blitAspectMask; |
| 576 | blit.srcSubresource.mipLevel = readRenderTarget->getLevelIndex(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 577 | blit.srcSubresource.baseArrayLayer = readRenderTarget->getLayerIndex(); |
| 578 | blit.srcSubresource.layerCount = 1; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 579 | blit.srcOffsets[0] = {sourceArea.x0(), sourceArea.y0(), 0}; |
| 580 | blit.srcOffsets[1] = {sourceArea.x1(), sourceArea.y1(), 1}; |
| 581 | blit.dstSubresource.aspectMask = blitAspectMask; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 582 | blit.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex(); |
| 583 | blit.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex(); |
| 584 | blit.dstSubresource.layerCount = 1; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 585 | blit.dstOffsets[0] = {destArea.x0(), destArea.y0(), 0}; |
| 586 | blit.dstOffsets[1] = {destArea.x1(), destArea.y1(), 1}; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 587 | |
| 588 | // Requirement of the copyImageToBuffer, the dst image must be in |
| 589 | // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 590 | dstImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferDst, commandBuffer); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 591 | |
| 592 | commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 593 | dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, |
| 594 | gl_vk::GetFilter(filter)); |
| 595 | |
| 596 | return angle::Result::Continue; |
| 597 | } |
| 598 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 599 | angle::Result FramebufferVk::blit(const gl::Context *context, |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 600 | const gl::Rectangle &sourceAreaIn, |
| 601 | const gl::Rectangle &destAreaIn, |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 602 | GLbitfield mask, |
| 603 | GLenum filter) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 604 | { |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 605 | ContextVk *contextVk = vk::GetImpl(context); |
| 606 | RendererVk *renderer = contextVk->getRenderer(); |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 607 | UtilsVk &utilsVk = contextVk->getUtils(); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 608 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 609 | const gl::State &glState = contextVk->getState(); |
| 610 | const gl::Framebuffer *srcFramebuffer = glState.getReadFramebuffer(); |
| 611 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 612 | const bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0; |
| 613 | const bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0; |
| 614 | const bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 615 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 616 | const bool isResolve = srcFramebuffer->getCachedSamples(context) > 1; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 617 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 618 | FramebufferVk *srcFramebufferVk = vk::GetImpl(srcFramebuffer); |
| 619 | const bool srcFramebufferFlippedY = contextVk->isViewportFlipEnabledForReadFBO(); |
| 620 | const bool destFramebufferFlippedY = contextVk->isViewportFlipEnabledForDrawFBO(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 621 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 622 | gl::Rectangle sourceArea = sourceAreaIn; |
| 623 | gl::Rectangle destArea = destAreaIn; |
| 624 | |
| 625 | // Note: GLES (all 3.x versions) require source and dest area to be identical when |
| 626 | // resolving. |
| 627 | ASSERT(!isResolve || |
| 628 | (sourceArea.x == destArea.x && sourceArea.y == destArea.y && |
| 629 | sourceArea.width == destArea.width && sourceArea.height == destArea.height)); |
| 630 | |
| 631 | const gl::Rectangle srcFramebufferDimensions = |
| 632 | srcFramebufferVk->mState.getDimensions().toRect(); |
| 633 | |
| 634 | // If the destination is flipped in either direction, we will flip the source instead so that |
| 635 | // the destination area is always unflipped. |
| 636 | sourceArea = sourceArea.flip(destArea.isReversedX(), destArea.isReversedY()); |
| 637 | destArea = destArea.removeReversal(); |
| 638 | |
| 639 | // Calculate the stretch factor prior to any clipping, as it needs to remain constant. |
| 640 | const float stretch[2] = { |
| 641 | std::abs(sourceArea.width / static_cast<float>(destArea.width)), |
| 642 | std::abs(sourceArea.height / static_cast<float>(destArea.height)), |
| 643 | }; |
| 644 | |
| 645 | // First, clip the source area to framebuffer. That requires transforming the dest area to |
| 646 | // match the clipped source. |
| 647 | gl::Rectangle absSourceArea = sourceArea.removeReversal(); |
| 648 | gl::Rectangle clippedSourceArea; |
| 649 | if (!gl::ClipRectangle(srcFramebufferDimensions, absSourceArea, &clippedSourceArea)) |
| 650 | { |
| 651 | return angle::Result::Continue; |
| 652 | } |
| 653 | |
| 654 | // Resize the destination area based on the new size of source. Note again that stretch is |
| 655 | // calculated as SrcDimension/DestDimension. |
| 656 | gl::Rectangle srcClippedDestArea; |
| 657 | if (isResolve) |
| 658 | { |
| 659 | // Source and dest areas are identical in resolve. |
| 660 | srcClippedDestArea = clippedSourceArea; |
| 661 | } |
| 662 | else if (clippedSourceArea == absSourceArea) |
| 663 | { |
| 664 | // If there was no clipping, keep dest area as is. |
| 665 | srcClippedDestArea = destArea; |
| 666 | } |
| 667 | else |
| 668 | { |
| 669 | // Shift dest area's x0,y0,x1,y1 by as much as the source area's got shifted (taking |
| 670 | // stretching into account) |
| 671 | float x0Shift = std::round((clippedSourceArea.x - absSourceArea.x) / stretch[0]); |
| 672 | float y0Shift = std::round((clippedSourceArea.y - absSourceArea.y) / stretch[1]); |
| 673 | float x1Shift = std::round((absSourceArea.x1() - clippedSourceArea.x1()) / stretch[0]); |
| 674 | float y1Shift = std::round((absSourceArea.y1() - clippedSourceArea.y1()) / stretch[1]); |
| 675 | |
| 676 | // If the source area was reversed in any direction, the shift should be applied in the |
| 677 | // opposite direction as well. |
| 678 | if (sourceArea.isReversedX()) |
| 679 | { |
| 680 | std::swap(x0Shift, x1Shift); |
| 681 | } |
| 682 | |
| 683 | if (sourceArea.isReversedY()) |
| 684 | { |
| 685 | std::swap(y0Shift, y1Shift); |
| 686 | } |
| 687 | |
| 688 | srcClippedDestArea.x = destArea.x0() + static_cast<int>(x0Shift); |
| 689 | srcClippedDestArea.y = destArea.y0() + static_cast<int>(y0Shift); |
| 690 | int x1 = destArea.x1() - static_cast<int>(x1Shift); |
| 691 | int y1 = destArea.y1() - static_cast<int>(y1Shift); |
| 692 | |
| 693 | srcClippedDestArea.width = x1 - srcClippedDestArea.x; |
| 694 | srcClippedDestArea.height = y1 - srcClippedDestArea.y; |
| 695 | } |
| 696 | |
| 697 | // If framebuffers are flipped in Y, flip the source and dest area (which define the |
| 698 | // transformation regardless of clipping), as well as the blit area (which is the clipped |
| 699 | // dest area). |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 700 | if (srcFramebufferFlippedY) |
| 701 | { |
| 702 | sourceArea.y = srcFramebufferDimensions.height - sourceArea.y; |
| 703 | sourceArea.height = -sourceArea.height; |
| 704 | } |
| 705 | if (destFramebufferFlippedY) |
| 706 | { |
| 707 | destArea.y = mState.getDimensions().height - destArea.y; |
| 708 | destArea.height = -destArea.height; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 709 | |
| 710 | srcClippedDestArea.y = |
| 711 | mState.getDimensions().height - srcClippedDestArea.y - srcClippedDestArea.height; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 712 | } |
| 713 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 714 | const bool flipX = sourceArea.isReversedX() != destArea.isReversedX(); |
| 715 | const bool flipY = sourceArea.isReversedY() != destArea.isReversedY(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 716 | |
| 717 | // GLES doesn't allow flipping the parameters of glBlitFramebuffer if performing a resolve. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 718 | ASSERT(!isResolve || |
| 719 | (flipX == false && flipY == (srcFramebufferFlippedY != destFramebufferFlippedY))); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 720 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 721 | // Again, transfer the destination flip to source, so dest is unflipped. Note that destArea |
| 722 | // was not reversed until the final possible Y-flip. |
| 723 | ASSERT(!destArea.isReversedX()); |
| 724 | sourceArea = sourceArea.flip(false, destArea.isReversedY()); |
| 725 | destArea = destArea.removeReversal(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 726 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 727 | // Clip the destination area to the framebuffer size and scissor. Note that we don't care |
| 728 | // about the source area anymore. The offset translation is done based on the original source |
| 729 | // and destination rectangles. The stretch factor is already calculated as well. |
| 730 | gl::Rectangle blitArea; |
| 731 | if (!gl::ClipRectangle(getScissoredRenderArea(contextVk), srcClippedDestArea, &blitArea)) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 732 | { |
| 733 | return angle::Result::Continue; |
| 734 | } |
| 735 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 736 | bool noClip = blitArea == destArea && stretch[0] == 1.0f && stretch[1] == 1.0f; |
| 737 | bool noFlip = !flipX && !flipY; |
| 738 | bool disableFlippingBlitWithCommand = |
| 739 | contextVk->getRenderer()->getFeatures().disableFlippingBlitWithCommand.enabled; |
| 740 | |
Shahbaz Youssefi | de70a71 | 2019-06-03 17:05:16 -0400 | [diff] [blame] | 741 | UtilsVk::BlitResolveParameters params; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 742 | params.srcOffset[0] = sourceArea.x; |
| 743 | params.srcOffset[1] = sourceArea.y; |
| 744 | params.destOffset[0] = destArea.x; |
| 745 | params.destOffset[1] = destArea.y; |
| 746 | params.stretch[0] = stretch[0]; |
| 747 | params.stretch[1] = stretch[1]; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 748 | params.srcExtents[0] = srcFramebufferDimensions.width; |
| 749 | params.srcExtents[1] = srcFramebufferDimensions.height; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 750 | params.blitArea = blitArea; |
| 751 | params.linear = filter == GL_LINEAR; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 752 | params.flipX = flipX; |
| 753 | params.flipY = flipY; |
| 754 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 755 | if (blitColorBuffer) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 756 | { |
| 757 | RenderTargetVk *readRenderTarget = srcFramebufferVk->getColorReadRenderTarget(); |
| 758 | params.srcLayer = readRenderTarget->getLayerIndex(); |
| 759 | |
| 760 | // Multisampled images are not allowed to have mips. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 761 | ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 762 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 763 | // If there was no clipping and the format capabilities allow us, use Vulkan's builtin blit. |
| 764 | // The reason clipping is prohibited in this path is that due to rounding errors, it would |
| 765 | // be hard to guarantee the image stretching remains perfect. That also allows us not to |
| 766 | // have to transform back the dest clipping to source. |
| 767 | // |
| 768 | // For simplicity, we either blit all render targets with a Vulkan command, or none. |
| 769 | bool canBlitWithCommand = !isResolve && noClip && |
| 770 | (noFlip || !disableFlippingBlitWithCommand) && |
| 771 | HasSrcBlitFeature(renderer, readRenderTarget); |
| 772 | bool areChannelsBlitCompatible = true; |
| 773 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
| 774 | { |
| 775 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
| 776 | canBlitWithCommand = |
| 777 | canBlitWithCommand && HasDstBlitFeature(renderer, drawRenderTarget); |
| 778 | areChannelsBlitCompatible = |
| 779 | areChannelsBlitCompatible && |
| 780 | areSrcAndDstColorChannelsBlitCompatible(readRenderTarget, drawRenderTarget); |
| 781 | } |
| 782 | |
| 783 | if (canBlitWithCommand && areChannelsBlitCompatible) |
| 784 | { |
| 785 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
| 786 | { |
| 787 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
| 788 | ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget, |
| 789 | drawRenderTarget, filter, true, false, false, flipX, |
| 790 | flipY)); |
| 791 | } |
| 792 | } |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 793 | // If we're not flipping, use Vulkan's builtin resolve. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 794 | else if (isResolve && !flipX && !flipY && areChannelsBlitCompatible) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 795 | { |
| 796 | ANGLE_TRY(resolveColorWithCommand(contextVk, params, &readRenderTarget->getImage())); |
| 797 | } |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 798 | // Otherwise use a shader to do blit or resolve. |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 799 | else |
| 800 | { |
Shahbaz Youssefi | de70a71 | 2019-06-03 17:05:16 -0400 | [diff] [blame] | 801 | ANGLE_TRY(utilsVk.colorBlitResolve(contextVk, this, &readRenderTarget->getImage(), |
| 802 | readRenderTarget->getFetchImageView(), params)); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 806 | if (blitDepthBuffer || blitStencilBuffer) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 807 | { |
| 808 | RenderTargetVk *readRenderTarget = srcFramebufferVk->getDepthStencilRenderTarget(); |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 809 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 810 | params.srcLayer = readRenderTarget->getLayerIndex(); |
| 811 | |
| 812 | // Multisampled images are not allowed to have mips. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 813 | ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 814 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 815 | // Similarly, only blit if there's been no clipping. |
| 816 | bool canBlitWithCommand = !isResolve && noClip && |
| 817 | (noFlip || !disableFlippingBlitWithCommand) && |
| 818 | HasSrcBlitFeature(renderer, readRenderTarget) && |
| 819 | HasDstBlitFeature(renderer, drawRenderTarget); |
| 820 | bool areChannelsBlitCompatible = |
| 821 | areSrcAndDstDepthStencilChannelsBlitCompatible(readRenderTarget, drawRenderTarget); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 822 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 823 | if (canBlitWithCommand && areChannelsBlitCompatible) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 824 | { |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 825 | ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget, |
| 826 | drawRenderTarget, filter, false, blitDepthBuffer, |
| 827 | blitStencilBuffer, flipX, flipY)); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 828 | } |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 829 | else |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 830 | { |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 831 | // Create depth- and stencil-only views for reading. |
| 832 | vk::Scoped<vk::ImageView> depthView(contextVk->getDevice()); |
| 833 | vk::Scoped<vk::ImageView> stencilView(contextVk->getDevice()); |
| 834 | |
| 835 | vk::ImageHelper *depthStencilImage = &readRenderTarget->getImage(); |
| 836 | uint32_t levelIndex = readRenderTarget->getLevelIndex(); |
| 837 | uint32_t layerIndex = readRenderTarget->getLayerIndex(); |
| 838 | gl::TextureType textureType = vk::Get2DTextureType(depthStencilImage->getLayerCount(), |
| 839 | depthStencilImage->getSamples()); |
| 840 | |
| 841 | if (blitDepthBuffer) |
| 842 | { |
| 843 | ANGLE_TRY(depthStencilImage->initLayerImageView( |
| 844 | contextVk, textureType, VK_IMAGE_ASPECT_DEPTH_BIT, gl::SwizzleState(), |
| 845 | &depthView.get(), levelIndex, 1, layerIndex, 1)); |
| 846 | } |
| 847 | |
| 848 | if (blitStencilBuffer) |
| 849 | { |
| 850 | ANGLE_TRY(depthStencilImage->initLayerImageView( |
| 851 | contextVk, textureType, VK_IMAGE_ASPECT_STENCIL_BIT, gl::SwizzleState(), |
| 852 | &stencilView.get(), levelIndex, 1, layerIndex, 1)); |
| 853 | } |
| 854 | |
| 855 | // If shader stencil export is not possible, defer stencil blit/stencil to another pass. |
| 856 | bool hasShaderStencilExport = |
| 857 | contextVk->getRenderer()->getFeatures().supportsShaderStencilExport.enabled; |
| 858 | |
| 859 | // Blit depth. If shader stencil export is present, blit stencil as well. |
| 860 | if (blitDepthBuffer || (blitStencilBuffer && hasShaderStencilExport)) |
| 861 | { |
| 862 | vk::ImageView *depth = blitDepthBuffer ? &depthView.get() : nullptr; |
| 863 | vk::ImageView *stencil = |
| 864 | blitStencilBuffer && hasShaderStencilExport ? &stencilView.get() : nullptr; |
| 865 | |
| 866 | ANGLE_TRY(utilsVk.depthStencilBlitResolve(contextVk, this, depthStencilImage, depth, |
| 867 | stencil, params)); |
| 868 | } |
| 869 | |
| 870 | // If shader stencil export is not present, blit stencil through a different path. |
| 871 | if (blitStencilBuffer && !hasShaderStencilExport) |
| 872 | { |
| 873 | ANGLE_TRY(utilsVk.stencilBlitResolveNoShaderExport( |
| 874 | contextVk, this, depthStencilImage, &stencilView.get(), params)); |
| 875 | } |
| 876 | |
| 877 | vk::ImageView depthViewObject = depthView.release(); |
| 878 | vk::ImageView stencilViewObject = stencilView.release(); |
| 879 | |
| 880 | contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &depthViewObject); |
| 881 | contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &stencilViewObject); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 882 | } |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | return angle::Result::Continue; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 886 | } // namespace rx |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 887 | |
| 888 | angle::Result FramebufferVk::resolveColorWithCommand(ContextVk *contextVk, |
Shahbaz Youssefi | de70a71 | 2019-06-03 17:05:16 -0400 | [diff] [blame] | 889 | const UtilsVk::BlitResolveParameters ¶ms, |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 890 | vk::ImageHelper *srcImage) |
| 891 | { |
| 892 | if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc)) |
| 893 | { |
| 894 | vk::CommandBuffer *srcLayoutChange; |
| 895 | ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange)); |
| 896 | srcImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferSrc, |
| 897 | srcLayoutChange); |
| 898 | } |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 899 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 900 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 901 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
| 902 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 903 | // Source's layout change should happen before rendering |
| 904 | srcImage->addReadDependency(&mFramebuffer); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 905 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 906 | VkImageResolve resolveRegion = {}; |
| 907 | resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 908 | resolveRegion.srcSubresource.mipLevel = 0; |
| 909 | resolveRegion.srcSubresource.baseArrayLayer = params.srcLayer; |
| 910 | resolveRegion.srcSubresource.layerCount = 1; |
| 911 | resolveRegion.srcOffset.x = params.srcOffset[0]; |
| 912 | resolveRegion.srcOffset.y = params.srcOffset[1]; |
| 913 | resolveRegion.srcOffset.z = 0; |
| 914 | resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 915 | resolveRegion.dstSubresource.layerCount = 1; |
| 916 | resolveRegion.dstOffset.x = params.destOffset[0]; |
| 917 | resolveRegion.dstOffset.y = params.destOffset[1]; |
| 918 | resolveRegion.dstOffset.z = 0; |
| 919 | resolveRegion.extent.width = params.srcExtents[0]; |
| 920 | resolveRegion.extent.height = params.srcExtents[1]; |
| 921 | resolveRegion.extent.depth = 1; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 922 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 923 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 924 | { |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 925 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 926 | vk::ImageHelper *drawImage = drawRenderTarget->getImageForWrite(&mFramebuffer); |
| 927 | drawImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferDst, |
| 928 | commandBuffer); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 929 | |
| 930 | resolveRegion.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex(); |
| 931 | resolveRegion.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex(); |
| 932 | |
| 933 | srcImage->resolve(&drawRenderTarget->getImage(), resolveRegion, commandBuffer); |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 934 | } |
| 935 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 936 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 937 | } |
| 938 | |
Kenneth Russell | ce8602a | 2017-10-03 18:23:08 -0700 | [diff] [blame] | 939 | bool FramebufferVk::checkStatus(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 940 | { |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 941 | // if we have both a depth and stencil buffer, they must refer to the same object |
| 942 | // since we only support packed_depth_stencil and not separate depth and stencil |
| 943 | if (mState.hasSeparateDepthAndStencilAttachments()) |
| 944 | { |
| 945 | return false; |
| 946 | } |
| 947 | |
Jamie Madill | b79e7bb | 2017-10-24 13:55:50 -0400 | [diff] [blame] | 948 | return true; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 949 | } |
| 950 | |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 951 | angle::Result FramebufferVk::updateColorAttachment(const gl::Context *context, size_t colorIndexGL) |
| 952 | { |
| 953 | ContextVk *contextVk = vk::GetImpl(context); |
| 954 | |
| 955 | ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL)); |
| 956 | |
| 957 | // Update cached masks for masked clears. |
| 958 | RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
| 959 | if (renderTarget) |
| 960 | { |
| 961 | const angle::Format &emulatedFormat = renderTarget->getImageFormat().imageFormat(); |
| 962 | updateActiveColorMasks(colorIndexGL, emulatedFormat.redBits > 0, |
| 963 | emulatedFormat.greenBits > 0, emulatedFormat.blueBits > 0, |
| 964 | emulatedFormat.alphaBits > 0); |
| 965 | |
| 966 | const angle::Format &sourceFormat = renderTarget->getImageFormat().angleFormat(); |
| 967 | mEmulatedAlphaAttachmentMask.set( |
| 968 | colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0); |
| 969 | |
| 970 | contextVk->updateColorMask(context->getState().getBlendState()); |
| 971 | } |
| 972 | else |
| 973 | { |
| 974 | updateActiveColorMasks(colorIndexGL, false, false, false, false); |
| 975 | } |
| 976 | |
| 977 | return angle::Result::Continue; |
| 978 | } |
| 979 | |
Shahbaz Youssefi | da90448 | 2019-07-02 10:49:14 -0400 | [diff] [blame] | 980 | void FramebufferVk::invalidateImpl(ContextVk *contextVk, size_t count, const GLenum *attachments) |
| 981 | { |
| 982 | ASSERT(mFramebuffer.hasStartedRenderPass()); |
| 983 | |
| 984 | gl::DrawBufferMask invalidateColorBuffers; |
| 985 | bool invalidateDepthBuffer = false; |
| 986 | bool invalidateStencilBuffer = false; |
| 987 | |
| 988 | for (size_t i = 0; i < count; ++i) |
| 989 | { |
| 990 | const GLenum attachment = attachments[i]; |
| 991 | |
| 992 | switch (attachment) |
| 993 | { |
| 994 | case GL_DEPTH: |
| 995 | case GL_DEPTH_ATTACHMENT: |
| 996 | invalidateDepthBuffer = true; |
| 997 | break; |
| 998 | case GL_STENCIL: |
| 999 | case GL_STENCIL_ATTACHMENT: |
| 1000 | invalidateStencilBuffer = true; |
| 1001 | break; |
| 1002 | case GL_DEPTH_STENCIL_ATTACHMENT: |
| 1003 | invalidateDepthBuffer = true; |
| 1004 | invalidateStencilBuffer = true; |
| 1005 | break; |
| 1006 | default: |
| 1007 | ASSERT( |
| 1008 | (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) || |
| 1009 | (attachment == GL_COLOR)); |
| 1010 | |
| 1011 | invalidateColorBuffers.set( |
| 1012 | attachment == GL_COLOR ? 0u : (attachment - GL_COLOR_ATTACHMENT0)); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | // Set the appropriate storeOp for attachments. |
| 1017 | size_t attachmentIndexVk = 0; |
| 1018 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
| 1019 | { |
| 1020 | if (invalidateColorBuffers.test(colorIndexGL)) |
| 1021 | { |
| 1022 | mFramebuffer.invalidateRenderPassColorAttachment(attachmentIndexVk); |
| 1023 | } |
| 1024 | ++attachmentIndexVk; |
| 1025 | } |
| 1026 | |
| 1027 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1028 | if (depthStencilRenderTarget) |
| 1029 | { |
| 1030 | if (invalidateDepthBuffer) |
| 1031 | { |
| 1032 | mFramebuffer.invalidateRenderPassDepthAttachment(attachmentIndexVk); |
| 1033 | } |
| 1034 | |
| 1035 | if (invalidateStencilBuffer) |
| 1036 | { |
| 1037 | mFramebuffer.invalidateRenderPassStencilAttachment(attachmentIndexVk); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // NOTE: Possible future optimization is to delay setting the storeOp and only do so if the |
| 1042 | // render pass is closed by itself before another draw call. Otherwise, in a situation like |
| 1043 | // this: |
| 1044 | // |
| 1045 | // draw() |
| 1046 | // invalidate() |
| 1047 | // draw() |
| 1048 | // |
| 1049 | // We would be discarding the attachments only to load them for the next draw (which is less |
| 1050 | // efficient than keeping the render pass open and not do the discard at all). While dEQP tests |
| 1051 | // this pattern, this optimization may not be necessary if no application does this. It is |
| 1052 | // expected that an application would invalidate() when it's done with the framebuffer, so the |
| 1053 | // render pass would have closed either way. |
| 1054 | mFramebuffer.finishCurrentCommands(contextVk); |
| 1055 | } |
| 1056 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 1057 | angle::Result FramebufferVk::syncState(const gl::Context *context, |
| 1058 | const gl::Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1059 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 1060 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1061 | |
| 1062 | ASSERT(dirtyBits.any()); |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 1063 | for (size_t dirtyBit : dirtyBits) |
| 1064 | { |
| 1065 | switch (dirtyBit) |
| 1066 | { |
| 1067 | case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
| 1068 | case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
| 1069 | ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState)); |
| 1070 | break; |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1071 | case gl::Framebuffer::DIRTY_BIT_DEPTH_BUFFER_CONTENTS: |
| 1072 | case gl::Framebuffer::DIRTY_BIT_STENCIL_BUFFER_CONTENTS: |
| 1073 | ANGLE_TRY(mRenderTargetCache.getDepthStencil()->flushStagedUpdates(contextVk)); |
| 1074 | break; |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 1075 | case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
| 1076 | case gl::Framebuffer::DIRTY_BIT_READ_BUFFER: |
| 1077 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH: |
| 1078 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT: |
| 1079 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES: |
| 1080 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS: |
| 1081 | break; |
| 1082 | default: |
| 1083 | { |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1084 | static_assert(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits"); |
| 1085 | if (dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1086 | { |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1087 | size_t colorIndexGL = static_cast<size_t>( |
| 1088 | dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 1089 | ANGLE_TRY(updateColorAttachment(context, colorIndexGL)); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1090 | } |
| 1091 | else |
| 1092 | { |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1093 | ASSERT(dirtyBit >= gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 && |
| 1094 | dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_MAX); |
| 1095 | size_t colorIndexGL = static_cast<size_t>( |
| 1096 | dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0); |
| 1097 | ANGLE_TRY(mRenderTargetCache.getColors()[colorIndexGL]->flushStagedUpdates( |
| 1098 | contextVk)); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1099 | } |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | } |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1103 | |
Tim Van Patten | 626a728 | 2019-07-08 15:11:59 -0600 | [diff] [blame^] | 1104 | // The FBOs new attachment may have changed the renderable area |
| 1105 | const gl::State &glState = context->getState(); |
| 1106 | contextVk->updateScissor(glState); |
| 1107 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1108 | mActiveColorComponents = gl_vk::GetColorComponentFlags( |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1109 | mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(), |
| 1110 | mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any()); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1111 | |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1112 | mFramebuffer.release(contextVk); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1113 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 1114 | // Will freeze the current set of dependencies on this FBO. The next time we render we will |
Jamie Madill | a5e0607 | 2018-05-18 14:36:05 -0400 | [diff] [blame] | 1115 | // create a new entry in the command graph. |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1116 | mFramebuffer.finishCurrentCommands(contextVk); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 1117 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1118 | // Notify the ContextVk to update the pipeline desc. |
| 1119 | updateRenderPassDesc(); |
Shahbaz Youssefi | da90448 | 2019-07-02 10:49:14 -0400 | [diff] [blame] | 1120 | |
| 1121 | FramebufferVk *currentDrawFramebuffer = vk::GetImpl(context->getState().getDrawFramebuffer()); |
| 1122 | if (currentDrawFramebuffer == this) |
| 1123 | { |
| 1124 | contextVk->onDrawFramebufferChange(this); |
| 1125 | } |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 1126 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1127 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1128 | } |
| 1129 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1130 | void FramebufferVk::updateRenderPassDesc() |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1131 | { |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1132 | mRenderPassDesc = {}; |
| 1133 | mRenderPassDesc.setSamples(getSamples()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1134 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1135 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 1136 | const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers(); |
| 1137 | for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1138 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1139 | if (enabledDrawBuffers[colorIndexGL]) |
| 1140 | { |
| 1141 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
| 1142 | ASSERT(colorRenderTarget); |
| 1143 | mRenderPassDesc.packColorAttachment( |
| 1144 | colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID); |
| 1145 | } |
| 1146 | else |
| 1147 | { |
| 1148 | mRenderPassDesc.packColorAttachmentGap(colorIndexGL); |
| 1149 | } |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1152 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1153 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1154 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1155 | mRenderPassDesc.packDepthStencilAttachment( |
| 1156 | depthStencilRenderTarget->getImage().getFormat().angleFormatID); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1157 | } |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1158 | } |
| 1159 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1160 | angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1161 | { |
| 1162 | // If we've already created our cached Framebuffer, return it. |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1163 | if (mFramebuffer.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1164 | { |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1165 | *framebufferOut = &mFramebuffer.getFramebuffer(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1166 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1167 | } |
| 1168 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1169 | vk::RenderPass *compatibleRenderPass = nullptr; |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1170 | ANGLE_TRY(contextVk->getCompatibleRenderPass(mRenderPassDesc, &compatibleRenderPass)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1171 | |
| 1172 | // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it. |
| 1173 | if (mBackbuffer) |
| 1174 | { |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1175 | return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | // Gather VkImageViews over all FBO attachments, also size of attached region. |
| 1179 | std::vector<VkImageView> attachments; |
| 1180 | gl::Extents attachmentsSize; |
| 1181 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1182 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1183 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1184 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1185 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1186 | ASSERT(colorRenderTarget); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 1187 | attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1188 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1189 | ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents()); |
| 1190 | attachmentsSize = colorRenderTarget->getExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1191 | } |
| 1192 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1193 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1194 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1195 | { |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 1196 | attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1197 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 1198 | ASSERT(attachmentsSize.empty() || |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1199 | attachmentsSize == depthStencilRenderTarget->getExtents()); |
| 1200 | attachmentsSize = depthStencilRenderTarget->getExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1201 | } |
| 1202 | |
Tim Van Patten | 626a728 | 2019-07-08 15:11:59 -0600 | [diff] [blame^] | 1203 | if (attachmentsSize.empty()) |
| 1204 | { |
| 1205 | // No attachments, so use the default values. |
| 1206 | attachmentsSize.height = mState.getDefaultHeight(); |
| 1207 | attachmentsSize.width = mState.getDefaultWidth(); |
| 1208 | attachmentsSize.depth = 0; |
| 1209 | } |
| 1210 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1211 | VkFramebufferCreateInfo framebufferInfo = {}; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1212 | |
| 1213 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1214 | framebufferInfo.flags = 0; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1215 | framebufferInfo.renderPass = compatibleRenderPass->getHandle(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1216 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); |
| 1217 | framebufferInfo.pAttachments = attachments.data(); |
| 1218 | framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width); |
| 1219 | framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height); |
| 1220 | framebufferInfo.layers = 1; |
| 1221 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1222 | ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo)); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 1223 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1224 | *framebufferOut = &mFramebuffer.getFramebuffer(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1225 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1228 | angle::Result FramebufferVk::clearWithRenderPassOp( |
| 1229 | ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1230 | const gl::Rectangle &clearArea, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1231 | gl::DrawBufferMask clearColorBuffers, |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1232 | bool clearDepth, |
| 1233 | bool clearStencil, |
| 1234 | const VkClearColorValue &clearColorValue, |
| 1235 | const VkClearDepthStencilValue &clearDepthStencilValue) |
| 1236 | { |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1237 | // Start a new render pass if: |
| 1238 | // |
| 1239 | // - no render pass has started, |
| 1240 | // - there is a render pass started but it contains commands; we cannot modify its ops, so new |
| 1241 | // render pass is needed, |
| 1242 | // - the current render area doesn't match the clear area. We need the render area to be |
| 1243 | // exactly as specified by the scissor for the loadOp to clear only that area. See |
| 1244 | // onScissorChange for more information. |
| 1245 | |
| 1246 | if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() || |
| 1247 | mFramebuffer.getRenderPassRenderArea() != clearArea) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1248 | { |
| 1249 | vk::CommandBuffer *commandBuffer; |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1250 | ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer)); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1253 | size_t attachmentIndexVk = 0; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1254 | |
| 1255 | // Go through clearColorBuffers and set the appropriate loadOp and clear values. |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1256 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1257 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1258 | if (clearColorBuffers.test(colorIndexGL)) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1259 | { |
| 1260 | RenderTargetVk *renderTarget = getColorReadRenderTarget(); |
| 1261 | |
| 1262 | // If the render target doesn't have alpha, but its emulated format has it, clear the |
| 1263 | // alpha to 1. |
| 1264 | VkClearColorValue value = clearColorValue; |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1265 | if (mEmulatedAlphaAttachmentMask[colorIndexGL]) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1266 | { |
| 1267 | SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value); |
| 1268 | } |
| 1269 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1270 | mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1271 | } |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1272 | ++attachmentIndexVk; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | // Set the appropriate loadOp and clear values for depth and stencil. |
| 1276 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1277 | if (depthStencilRenderTarget) |
| 1278 | { |
| 1279 | if (clearDepth) |
| 1280 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1281 | mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1282 | clearDepthStencilValue.depth); |
| 1283 | } |
| 1284 | |
| 1285 | if (clearStencil) |
| 1286 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1287 | mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1288 | clearDepthStencilValue.stencil); |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | return angle::Result::Continue; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1293 | } |
| 1294 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1295 | angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1296 | const gl::Rectangle &clearArea, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1297 | gl::DrawBufferMask clearColorBuffers, |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1298 | bool clearStencil, |
| 1299 | VkColorComponentFlags colorMaskFlags, |
| 1300 | uint8_t stencilMask, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1301 | const VkClearColorValue &clearColorValue, |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1302 | uint8_t clearStencilValue) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1303 | { |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1304 | UtilsVk::ClearFramebufferParameters params = {}; |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1305 | params.clearArea = clearArea; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1306 | params.colorClearValue = clearColorValue; |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1307 | params.stencilClearValue = clearStencilValue; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1308 | params.stencilMask = stencilMask; |
| 1309 | |
| 1310 | params.clearColor = true; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1311 | params.clearStencil = clearStencil; |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1312 | |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1313 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1314 | for (size_t colorIndexGL : clearColorBuffers) |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1315 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1316 | const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1317 | ASSERT(colorRenderTarget); |
| 1318 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1319 | params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat(); |
| 1320 | params.colorAttachmentIndexGL = colorIndexGL; |
| 1321 | params.colorMaskFlags = colorMaskFlags; |
| 1322 | if (mEmulatedAlphaAttachmentMask[colorIndexGL]) |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1323 | { |
| 1324 | params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT; |
| 1325 | } |
| 1326 | |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1327 | ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params)); |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1328 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1329 | // Clear stencil only once! |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1330 | params.clearStencil = false; |
| 1331 | } |
| 1332 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1333 | // If there was no color clear, clear stencil alone. |
| 1334 | if (params.clearStencil) |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1335 | { |
| 1336 | params.clearColor = false; |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1337 | ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params)); |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | return angle::Result::Continue; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1341 | } |
| 1342 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 1343 | angle::Result FramebufferVk::getSamplePosition(const gl::Context *context, |
| 1344 | size_t index, |
| 1345 | GLfloat *xy) const |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1346 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 1347 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1348 | return angle::Result::Stop; |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Jamie Madill | d1249de | 2018-08-28 16:58:53 -0400 | [diff] [blame] | 1351 | angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1352 | const gl::Rectangle &renderArea, |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1353 | vk::CommandBuffer **commandBufferOut) |
Jamie Madill | d1249de | 2018-08-28 16:58:53 -0400 | [diff] [blame] | 1354 | { |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1355 | vk::Framebuffer *framebuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1356 | ANGLE_TRY(getFramebuffer(contextVk, &framebuffer)); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1357 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1358 | vk::AttachmentOpsArray renderPassAttachmentOps; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1359 | std::vector<VkClearValue> attachmentClearValues; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1360 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1361 | vk::CommandBuffer *writeCommands = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1362 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands)); |
Jamie Madill | e4c5a23 | 2018-03-02 21:00:31 -0500 | [diff] [blame] | 1363 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1364 | // Initialize RenderPass info. |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1365 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1366 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1367 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1368 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1369 | ASSERT(colorRenderTarget); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1370 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1371 | ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands)); |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1372 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1373 | renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(), |
| 1374 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1375 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 1376 | attachmentClearValues.emplace_back(kUninitializedClearValue); |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1380 | if (depthStencilRenderTarget) |
| 1381 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1382 | ANGLE_TRY( |
| 1383 | depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands)); |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1384 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1385 | renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(), |
| 1386 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1387 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
| 1388 | attachmentClearValues.emplace_back(kUninitializedClearValue); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1389 | } |
| 1390 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1391 | return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc, |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1392 | renderPassAttachmentOps, attachmentClearValues, |
| 1393 | commandBufferOut); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1394 | } |
| 1395 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1396 | void FramebufferVk::updateActiveColorMasks(size_t colorIndexGL, bool r, bool g, bool b, bool a) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1397 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1398 | mActiveColorComponentMasksForClear[0].set(colorIndexGL, r); |
| 1399 | mActiveColorComponentMasksForClear[1].set(colorIndexGL, g); |
| 1400 | mActiveColorComponentMasksForClear[2].set(colorIndexGL, b); |
| 1401 | mActiveColorComponentMasksForClear[3].set(colorIndexGL, a); |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1402 | } |
| 1403 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1404 | const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1405 | { |
| 1406 | return mEmulatedAlphaAttachmentMask; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1407 | } |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1408 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1409 | angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk, |
| 1410 | const gl::Rectangle &area, |
| 1411 | const PackPixelsParams &packPixelsParams, |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1412 | VkImageAspectFlagBits copyAspectFlags, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1413 | RenderTargetVk *renderTarget, |
| 1414 | void *pixels) |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1415 | { |
Jamie Madill | 3ea463b | 2019-06-19 14:21:33 -0400 | [diff] [blame] | 1416 | ANGLE_TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl"); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1417 | |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1418 | RendererVk *renderer = contextVk->getRenderer(); |
| 1419 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1420 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1421 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1422 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1423 | // Note that although we're reading from the image, we need to update the layout below. |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 1424 | vk::ImageHelper *srcImage = |
| 1425 | renderTarget->getImageForRead(&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1426 | |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 1427 | const angle::Format *readFormat = &srcImage->getFormat().imageFormat(); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1428 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1429 | if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT) |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1430 | { |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1431 | readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1432 | } |
| 1433 | |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1434 | size_t level = renderTarget->getLevelIndex(); |
| 1435 | size_t layer = renderTarget->getLayerIndex(); |
| 1436 | VkOffset3D srcOffset = {area.x, area.y, 0}; |
| 1437 | VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height), |
| 1438 | 1}; |
| 1439 | |
| 1440 | // If the source image is multisampled, we need to resolve it into a temporary image before |
| 1441 | // performing a readback. |
| 1442 | bool isMultisampled = srcImage->getSamples() > 1; |
| 1443 | vk::Scoped<vk::ImageHelper> resolvedImage(contextVk->getDevice()); |
| 1444 | if (isMultisampled) |
| 1445 | { |
| 1446 | ANGLE_TRY(resolvedImage.get().init2DStaging( |
| 1447 | contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1), |
| 1448 | srcImage->getFormat(), |
| 1449 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1)); |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1450 | resolvedImage.get().updateQueueSerial(contextVk->getCurrentQueueSerial()); |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1451 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 1452 | // Note: resolve only works on color images (not depth/stencil). |
| 1453 | // |
| 1454 | // TODO: Currently, depth/stencil blit can perform a depth/stencil readback, but that code |
| 1455 | // path will be optimized away. http://anglebug.com/3200 |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1456 | ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT); |
| 1457 | |
| 1458 | VkImageResolve resolveRegion = {}; |
| 1459 | resolveRegion.srcSubresource.aspectMask = copyAspectFlags; |
| 1460 | resolveRegion.srcSubresource.mipLevel = level; |
| 1461 | resolveRegion.srcSubresource.baseArrayLayer = layer; |
| 1462 | resolveRegion.srcSubresource.layerCount = 1; |
| 1463 | resolveRegion.srcOffset = srcOffset; |
| 1464 | resolveRegion.dstSubresource.aspectMask = copyAspectFlags; |
| 1465 | resolveRegion.dstSubresource.mipLevel = 0; |
| 1466 | resolveRegion.dstSubresource.baseArrayLayer = 0; |
| 1467 | resolveRegion.dstSubresource.layerCount = 1; |
| 1468 | resolveRegion.dstOffset = {}; |
| 1469 | resolveRegion.extent = srcExtent; |
| 1470 | |
| 1471 | srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer); |
| 1472 | |
| 1473 | resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc, |
| 1474 | commandBuffer); |
| 1475 | |
| 1476 | // Make the resolved image the target of buffer copy. |
| 1477 | srcImage = &resolvedImage.get(); |
| 1478 | level = 0; |
| 1479 | layer = 0; |
| 1480 | srcOffset = {0, 0, 0}; |
| 1481 | } |
| 1482 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1483 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 1484 | uint8_t *readPixelBuffer = nullptr; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 1485 | VkDeviceSize stagingOffset = 0; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1486 | size_t allocationSize = readFormat->pixelBytes * area.width * area.height; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1487 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 1488 | ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle, |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 1489 | &stagingOffset, nullptr)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1490 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1491 | VkBufferImageCopy region = {}; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1492 | region.bufferImageHeight = srcExtent.height; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 1493 | region.bufferOffset = stagingOffset; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1494 | region.bufferRowLength = srcExtent.width; |
| 1495 | region.imageExtent = srcExtent; |
| 1496 | region.imageOffset = srcOffset; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1497 | region.imageSubresource.aspectMask = copyAspectFlags; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1498 | region.imageSubresource.baseArrayLayer = layer; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1499 | region.imageSubresource.layerCount = 1; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1500 | region.imageSubresource.mipLevel = level; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1501 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1502 | commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(), |
| 1503 | bufferHandle, 1, ®ion); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1504 | |
| 1505 | // Triggers a full finish. |
| 1506 | // TODO(jmadill): Don't block on asynchronous readback. |
Geoff Lang | 892d180 | 2019-03-27 14:21:34 -0400 | [diff] [blame] | 1507 | ANGLE_TRY(contextVk->finishImpl()); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1508 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1509 | // The buffer we copied to needs to be invalidated before we read from it because its not been |
| 1510 | // created with the host coherent bit. |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 1511 | ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk)); |
Yuly Novikov | 6c6c76c | 2018-05-17 18:45:06 +0000 | [diff] [blame] | 1512 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1513 | PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, readPixelBuffer, |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 1514 | static_cast<uint8_t *>(pixels)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1515 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1516 | return angle::Result::Continue; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1517 | } |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1518 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1519 | gl::Extents FramebufferVk::getReadImageExtents() const |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1520 | { |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1521 | ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width); |
| 1522 | ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height); |
| 1523 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1524 | return getColorReadRenderTarget()->getExtents(); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1525 | } |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 1526 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1527 | gl::Rectangle FramebufferVk::getCompleteRenderArea() const |
| 1528 | { |
Tim Van Patten | 626a728 | 2019-07-08 15:11:59 -0600 | [diff] [blame^] | 1529 | const gl::Box &dimensions = mState.getDimensions(); |
| 1530 | return gl::Rectangle(0, 0, dimensions.width, dimensions.height); |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const |
| 1534 | { |
Tim Van Patten | 626a728 | 2019-07-08 15:11:59 -0600 | [diff] [blame^] | 1535 | const gl::Box &dimensions = mState.getDimensions(); |
| 1536 | const gl::Rectangle renderArea(0, 0, dimensions.width, dimensions.height); |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1537 | bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO(); |
| 1538 | |
| 1539 | return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport); |
| 1540 | } |
| 1541 | |
| 1542 | void FramebufferVk::onScissorChange(ContextVk *contextVk) |
| 1543 | { |
| 1544 | gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk); |
| 1545 | |
| 1546 | // If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass |
| 1547 | // is restarted. Otherwise, we can continue using the same renderpass area. |
| 1548 | // |
| 1549 | // Without a scissor, the render pass area covers the whole of the framebuffer. With a |
| 1550 | // scissored clear, the render pass area could be smaller than the framebuffer size. When the |
| 1551 | // scissor changes, if the scissor area is completely encompassed by the render pass area, it's |
| 1552 | // possible to continue using the same render pass. However, if the current render pass area |
| 1553 | // is too small, we need to start a new one. The latter can happen if a scissored clear starts |
| 1554 | // a render pass, the scissor is disabled and a draw call is issued to affect the whole |
| 1555 | // framebuffer. |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1556 | mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial()); |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1557 | if (mFramebuffer.hasStartedRenderPass() && |
| 1558 | !mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea)) |
| 1559 | { |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1560 | mFramebuffer.finishCurrentCommands(contextVk); |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 1564 | RenderTargetVk *FramebufferVk::getFirstRenderTarget() const |
| 1565 | { |
| 1566 | for (auto *renderTarget : mRenderTargetCache.getColors()) |
| 1567 | { |
| 1568 | if (renderTarget) |
| 1569 | { |
| 1570 | return renderTarget; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | return mRenderTargetCache.getDepthStencil(); |
| 1575 | } |
| 1576 | |
| 1577 | GLint FramebufferVk::getSamples() const |
| 1578 | { |
| 1579 | RenderTargetVk *firstRT = getFirstRenderTarget(); |
| 1580 | return firstRT ? firstRT->getImage().getSamples() : 0; |
| 1581 | } |
| 1582 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1583 | } // namespace rx |