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" |
Tobin Ehlis | 4a41914 | 2019-02-05 08:50:30 -0700 | [diff] [blame] | 27 | #include "third_party/trace_event/trace_event.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 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 52 | bool ClipToRenderTarget(const gl::Rectangle &area, |
| 53 | RenderTargetVk *renderTarget, |
| 54 | gl::Rectangle *rectOut) |
| 55 | { |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 56 | const gl::Extents renderTargetExtents = renderTarget->getExtents(); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 57 | gl::Rectangle renderTargetRect(0, 0, renderTargetExtents.width, renderTargetExtents.height); |
| 58 | return ClipRectangle(area, renderTargetRect, rectOut); |
| 59 | } |
| 60 | |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 61 | bool HasSrcAndDstBlitProperties(RendererVk *renderer, |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 62 | RenderTargetVk *srcRenderTarget, |
| 63 | RenderTargetVk *dstRenderTarget) |
| 64 | { |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 65 | const VkFormat srcFormat = srcRenderTarget->getImageFormat().vkImageFormat; |
| 66 | const VkFormat dstFormat = dstRenderTarget->getImageFormat().vkImageFormat; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 67 | |
| 68 | // Verifies if the draw and read images have the necessary prerequisites for blitting. |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 69 | return renderer->hasImageFormatFeatureBits(srcFormat, VK_FORMAT_FEATURE_BLIT_SRC_BIT) && |
| 70 | renderer->hasImageFormatFeatureBits(dstFormat, VK_FORMAT_FEATURE_BLIT_DST_BIT); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 71 | } |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 72 | |
| 73 | // Special rules apply to VkBufferImageCopy with depth/stencil. The components are tightly packed |
| 74 | // into a depth or stencil section of the destination buffer. See the spec: |
| 75 | // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkBufferImageCopy.html |
| 76 | const angle::Format &GetDepthStencilImageToBufferFormat(const angle::Format &imageFormat, |
| 77 | VkImageAspectFlagBits copyAspect) |
| 78 | { |
| 79 | if (copyAspect == VK_IMAGE_ASPECT_STENCIL_BIT) |
| 80 | { |
| 81 | ASSERT(imageFormat.id == angle::FormatID::D24_UNORM_S8_UINT || |
| 82 | imageFormat.id == angle::FormatID::D32_FLOAT_S8X24_UINT || |
| 83 | imageFormat.id == angle::FormatID::S8_UINT); |
| 84 | return angle::Format::Get(angle::FormatID::S8_UINT); |
| 85 | } |
| 86 | |
| 87 | ASSERT(copyAspect == VK_IMAGE_ASPECT_DEPTH_BIT); |
| 88 | |
| 89 | switch (imageFormat.id) |
| 90 | { |
| 91 | case angle::FormatID::D16_UNORM: |
| 92 | return imageFormat; |
| 93 | case angle::FormatID::D24_UNORM_X8_UINT: |
| 94 | return imageFormat; |
| 95 | case angle::FormatID::D24_UNORM_S8_UINT: |
| 96 | return angle::Format::Get(angle::FormatID::D24_UNORM_X8_UINT); |
| 97 | case angle::FormatID::D32_FLOAT: |
| 98 | return imageFormat; |
| 99 | case angle::FormatID::D32_FLOAT_S8X24_UINT: |
| 100 | return angle::Format::Get(angle::FormatID::D32_FLOAT); |
| 101 | default: |
| 102 | UNREACHABLE(); |
| 103 | return imageFormat; |
| 104 | } |
| 105 | } |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 106 | |
| 107 | void SetEmulatedAlphaValue(const vk::Format &format, VkClearColorValue *value) |
| 108 | { |
| 109 | if (format.vkFormatIsInt) |
| 110 | { |
| 111 | if (format.vkFormatIsUnsigned) |
| 112 | { |
| 113 | value->uint32[3] = kEmulatedAlphaValue; |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | value->int32[3] = kEmulatedAlphaValue; |
| 118 | } |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | value->float32[3] = kEmulatedAlphaValue; |
| 123 | } |
| 124 | } |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 125 | } // anonymous namespace |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 126 | |
| 127 | // static |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 128 | FramebufferVk *FramebufferVk::CreateUserFBO(RendererVk *renderer, const gl::FramebufferState &state) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 129 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 130 | return new FramebufferVk(renderer, state, nullptr); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // static |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 134 | FramebufferVk *FramebufferVk::CreateDefaultFBO(RendererVk *renderer, |
| 135 | const gl::FramebufferState &state, |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 136 | WindowSurfaceVk *backbuffer) |
| 137 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 138 | return new FramebufferVk(renderer, state, backbuffer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 139 | } |
| 140 | |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 141 | FramebufferVk::FramebufferVk(RendererVk *renderer, |
| 142 | const gl::FramebufferState &state, |
| 143 | WindowSurfaceVk *backbuffer) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 144 | : FramebufferImpl(state), |
| 145 | mBackbuffer(backbuffer), |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 146 | mActiveColorComponents(0), |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 147 | mReadPixelBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT, kMinReadPixelsBufferSize, true), |
| 148 | mBlitPixelBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, kMinReadPixelsBufferSize, true) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 149 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 150 | mBlitPixelBuffer.init(1, renderer); |
Frank Henigman | 49b0f6e | 2018-10-03 23:15:29 -0400 | [diff] [blame] | 151 | mReadPixelBuffer.init(4, renderer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 154 | FramebufferVk::~FramebufferVk() = default; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 155 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 156 | void FramebufferVk::destroy(const gl::Context *context) |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 157 | { |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 158 | ContextVk *contextVk = vk::GetImpl(context); |
| 159 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 160 | mFramebuffer.release(renderer); |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 161 | |
Shahbaz Youssefi | fca8fd6 | 2018-11-13 13:55:48 -0500 | [diff] [blame] | 162 | mReadPixelBuffer.release(renderer); |
| 163 | mBlitPixelBuffer.release(renderer); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 164 | } |
| 165 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 166 | angle::Result FramebufferVk::discard(const gl::Context *context, |
| 167 | size_t count, |
| 168 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 169 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 170 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 171 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 174 | angle::Result FramebufferVk::invalidate(const gl::Context *context, |
| 175 | size_t count, |
| 176 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 177 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 178 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 179 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 182 | angle::Result FramebufferVk::invalidateSub(const gl::Context *context, |
| 183 | size_t count, |
| 184 | const GLenum *attachments, |
| 185 | const gl::Rectangle &area) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 186 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 187 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 188 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 191 | angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 192 | { |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 193 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 194 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 195 | bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT)); |
| 196 | bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT)); |
| 197 | bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT)); |
| 198 | gl::DrawBufferMask clearColorBuffers; |
| 199 | if (clearColor) |
| 200 | { |
| 201 | clearColorBuffers = mState.getEnabledDrawBuffers(); |
| 202 | } |
| 203 | |
| 204 | const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color; |
| 205 | const VkClearDepthStencilValue &clearDepthStencilValue = |
| 206 | contextVk->getClearDepthStencilValue().depthStencil; |
| 207 | |
| 208 | return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue, |
| 209 | clearDepthStencilValue); |
| 210 | } |
| 211 | |
| 212 | angle::Result FramebufferVk::clearImpl(const gl::Context *context, |
| 213 | gl::DrawBufferMask clearColorBuffers, |
| 214 | bool clearDepth, |
| 215 | bool clearStencil, |
| 216 | const VkClearColorValue &clearColorValue, |
| 217 | const VkClearDepthStencilValue &clearDepthStencilValue) |
| 218 | { |
| 219 | ContextVk *contextVk = vk::GetImpl(context); |
| 220 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 221 | const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk); |
| 222 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 223 | // Discard clear altogether if scissor has 0 width or height. |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 224 | if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 225 | { |
| 226 | return angle::Result::Continue; |
| 227 | } |
| 228 | |
| 229 | mFramebuffer.updateQueueSerial(contextVk->getRenderer()->getCurrentQueueSerial()); |
| 230 | |
| 231 | // This function assumes that only enabled attachments are asked to be cleared. |
| 232 | ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers); |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 233 | |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 234 | // Adjust clear behavior based on whether the respective attachments are present; if asked to |
| 235 | // clear a non-existent attachment, don't attempt to clear it. |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 236 | |
| 237 | VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask(); |
| 238 | bool clearColor = clearColorBuffers.any(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 239 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 240 | const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 241 | clearDepth = clearDepth && depthAttachment; |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 242 | ASSERT(!clearDepth || depthAttachment->isAttached()); |
| 243 | |
| 244 | const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 245 | clearStencil = clearStencil && stencilAttachment; |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 246 | ASSERT(!clearStencil || stencilAttachment->isAttached()); |
| 247 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 248 | uint8_t stencilMask = |
| 249 | static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask); |
| 250 | |
| 251 | // The front-end should ensure we don't attempt to clear color if all channels are masked. |
| 252 | ASSERT(!clearColor || colorMaskFlags != 0); |
| 253 | // The front-end should ensure we don't attempt to clear depth if depth write is disabled. |
| 254 | ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask); |
| 255 | // The front-end should ensure we don't attempt to clear stencil if all bits are masked. |
| 256 | ASSERT(!clearStencil || stencilMask != 0); |
| 257 | |
| 258 | // If there is nothing to clear, return right away (for example, if asked to clear depth, but |
| 259 | // there is no depth attachment). |
Shahbaz Youssefi | 02a579e | 2019-03-27 14:21:20 -0400 | [diff] [blame] | 260 | if (!clearColor && !clearDepth && !clearStencil) |
| 261 | { |
| 262 | return angle::Result::Continue; |
| 263 | } |
| 264 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 265 | VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue; |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 266 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 267 | // We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If |
| 268 | // there's a depth mask, depth clearing is already disabled. |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 269 | bool maskedClearColor = |
| 270 | clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 271 | bool maskedClearStencil = stencilMask != 0xFF; |
| 272 | |
| 273 | bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor; |
| 274 | bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 275 | |
| 276 | // At least one of color, depth or stencil should be clearable with render pass loadOp for us |
| 277 | // to use this clear path. |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 278 | bool clearAnyWithRenderPassLoadOp = |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 279 | clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 280 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 281 | if (clearAnyWithRenderPassLoadOp) |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 282 | { |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 283 | // Clearing color is indicated by the set bits in this mask. If not clearing colors with |
| 284 | // 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] | 285 | // clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass |
| 286 | // loadOp. |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 287 | gl::DrawBufferMask clearBuffersWithRenderPassLoadOp; |
| 288 | if (clearColorWithRenderPassLoadOp) |
| 289 | { |
| 290 | clearBuffersWithRenderPassLoadOp = clearColorBuffers; |
| 291 | } |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 292 | ANGLE_TRY(clearWithRenderPassOp( |
| 293 | contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth, |
| 294 | clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue)); |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 295 | |
Shahbaz Youssefi | 27f115a | 2019-04-01 10:33:21 -0400 | [diff] [blame] | 296 | // On some hardware, having inline commands at this point results in corrupted output. In |
| 297 | // that case, end the render pass immediately. http://anglebug.com/2361 |
Jonah Ryan-Davis | 776694c | 2019-05-08 10:28:55 -0400 | [diff] [blame] | 298 | if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled) |
Shahbaz Youssefi | 27f115a | 2019-04-01 10:33:21 -0400 | [diff] [blame] | 299 | { |
| 300 | mFramebuffer.finishCurrentCommands(contextVk->getRenderer()); |
| 301 | } |
| 302 | |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 303 | // Fallback to other methods for whatever isn't cleared here. |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 304 | clearDepth = false; |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 305 | if (clearColorWithRenderPassLoadOp) |
| 306 | { |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 307 | clearColorBuffers.reset(); |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 308 | clearColor = false; |
| 309 | } |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 310 | if (clearStencilWithRenderPassLoadOp) |
| 311 | { |
| 312 | clearStencil = false; |
| 313 | } |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 314 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 315 | // If nothing left to clear, early out. |
| 316 | if (!clearColor && !clearStencil) |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 317 | { |
| 318 | return angle::Result::Continue; |
| 319 | } |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 322 | // Note: depth clear is always done through render pass loadOp. |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 323 | ASSERT(clearDepth == false); |
| 324 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 325 | // 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] | 326 | // bits. This can only be done with a draw call. |
| 327 | return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil, |
| 328 | colorMaskFlags, stencilMask, clearColorValue, |
| 329 | static_cast<uint8_t>(modifiedDepthStencilValue.stencil)); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 330 | } |
| 331 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 332 | angle::Result FramebufferVk::clearBufferfv(const gl::Context *context, |
| 333 | GLenum buffer, |
| 334 | GLint drawbuffer, |
| 335 | const GLfloat *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 336 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 337 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 338 | |
| 339 | bool clearDepth = false; |
| 340 | gl::DrawBufferMask clearColorBuffers; |
| 341 | |
| 342 | if (buffer == GL_DEPTH) |
| 343 | { |
| 344 | clearDepth = true; |
| 345 | clearValue.depthStencil.depth = values[0]; |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | clearColorBuffers.set(drawbuffer); |
| 350 | clearValue.color.float32[0] = values[0]; |
| 351 | clearValue.color.float32[1] = values[1]; |
| 352 | clearValue.color.float32[2] = values[2]; |
| 353 | clearValue.color.float32[3] = values[3]; |
| 354 | } |
| 355 | |
| 356 | return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color, |
| 357 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 358 | } |
| 359 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 360 | angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context, |
| 361 | GLenum buffer, |
| 362 | GLint drawbuffer, |
| 363 | const GLuint *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 364 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 365 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 366 | |
| 367 | gl::DrawBufferMask clearColorBuffers; |
| 368 | clearColorBuffers.set(drawbuffer); |
| 369 | |
| 370 | clearValue.color.uint32[0] = values[0]; |
| 371 | clearValue.color.uint32[1] = values[1]; |
| 372 | clearValue.color.uint32[2] = values[2]; |
| 373 | clearValue.color.uint32[3] = values[3]; |
| 374 | |
| 375 | return clearImpl(context, clearColorBuffers, false, false, clearValue.color, |
| 376 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 377 | } |
| 378 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 379 | angle::Result FramebufferVk::clearBufferiv(const gl::Context *context, |
| 380 | GLenum buffer, |
| 381 | GLint drawbuffer, |
| 382 | const GLint *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 383 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 384 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 385 | |
| 386 | bool clearStencil = false; |
| 387 | gl::DrawBufferMask clearColorBuffers; |
| 388 | |
| 389 | if (buffer == GL_STENCIL) |
| 390 | { |
| 391 | clearStencil = true; |
| 392 | clearValue.depthStencil.stencil = |
| 393 | gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max()); |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | clearColorBuffers.set(drawbuffer); |
| 398 | clearValue.color.int32[0] = values[0]; |
| 399 | clearValue.color.int32[1] = values[1]; |
| 400 | clearValue.color.int32[2] = values[2]; |
| 401 | clearValue.color.int32[3] = values[3]; |
| 402 | } |
| 403 | |
| 404 | return clearImpl(context, clearColorBuffers, false, clearStencil, 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::clearBufferfi(const gl::Context *context, |
| 409 | GLenum buffer, |
| 410 | GLint drawbuffer, |
| 411 | GLfloat depth, |
| 412 | GLint stencil) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 413 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 414 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 415 | |
| 416 | clearValue.depthStencil.depth = depth; |
| 417 | clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max()); |
| 418 | |
| 419 | return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color, |
| 420 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 423 | GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 424 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 425 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 426 | } |
| 427 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 428 | GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 429 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 430 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 433 | angle::Result FramebufferVk::readPixels(const gl::Context *context, |
| 434 | const gl::Rectangle &area, |
| 435 | GLenum format, |
| 436 | GLenum type, |
| 437 | void *pixels) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 438 | { |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 439 | // Clip read area to framebuffer. |
| 440 | const gl::Extents &fbSize = getState().getReadAttachment()->getSize(); |
| 441 | const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 442 | ContextVk *contextVk = vk::GetImpl(context); |
| 443 | RendererVk *renderer = contextVk->getRenderer(); |
| 444 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 445 | gl::Rectangle clippedArea; |
| 446 | if (!ClipRectangle(area, fbRect, &clippedArea)) |
| 447 | { |
| 448 | // nothing to read |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 449 | return angle::Result::Continue; |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 450 | } |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 451 | gl::Rectangle flippedArea = clippedArea; |
Geoff Lang | e076a23 | 2018-07-16 15:34:05 -0400 | [diff] [blame] | 452 | if (contextVk->isViewportFlipEnabledForReadFBO()) |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 453 | { |
| 454 | flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height; |
| 455 | } |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 456 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 457 | const gl::State &glState = context->getState(); |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 458 | const gl::PixelPackState &packState = glState.getPackState(); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 459 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 460 | const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type); |
| 461 | |
| 462 | GLuint outputPitch = 0; |
Jamie Madill | abfbc0f | 2018-10-09 12:48:52 -0400 | [diff] [blame] | 463 | ANGLE_VK_CHECK_MATH(contextVk, |
| 464 | sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment, |
| 465 | packState.rowLength, &outputPitch)); |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 466 | GLuint outputSkipBytes = 0; |
Jamie Madill | abfbc0f | 2018-10-09 12:48:52 -0400 | [diff] [blame] | 467 | ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState, |
| 468 | false, &outputSkipBytes)); |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 469 | |
| 470 | outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes + |
| 471 | (clippedArea.y - area.y) * outputPitch; |
Luc Ferron | 6028422 | 2018-03-20 16:01:44 -0400 | [diff] [blame] | 472 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 473 | const angle::Format &angleFormat = GetFormatFromFormatType(format, type); |
| 474 | |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 475 | PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder, |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 476 | glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0); |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 477 | if (contextVk->isViewportFlipEnabledForReadFBO()) |
| 478 | { |
| 479 | params.reverseRowOrder = !params.reverseRowOrder; |
| 480 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 481 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 482 | ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 483 | getColorReadRenderTarget(), |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 484 | static_cast<uint8_t *>(pixels) + outputSkipBytes)); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 485 | mReadPixelBuffer.releaseRetainedBuffers(renderer); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 486 | return angle::Result::Continue; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 487 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 488 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 489 | RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const |
| 490 | { |
| 491 | return mRenderTargetCache.getDepthStencil(); |
| 492 | } |
| 493 | |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 494 | angle::Result FramebufferVk::blitWithCopy(ContextVk *contextVk, |
| 495 | const gl::Rectangle ©Area, |
| 496 | RenderTargetVk *readRenderTarget, |
| 497 | RenderTargetVk *drawRenderTarget, |
| 498 | bool blitDepthBuffer, |
| 499 | bool blitStencilBuffer) |
Luc Ferron | be30c4f | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 500 | { |
Shahbaz Youssefi | 028df5f | 2019-02-13 12:57:10 -0500 | [diff] [blame] | 501 | VkImageAspectFlags aspectMask = |
| 502 | vk::GetDepthStencilAspectFlagsForCopy(blitDepthBuffer, blitStencilBuffer); |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 503 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 504 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 505 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
| 506 | |
Shahbaz Youssefi | 028df5f | 2019-02-13 12:57:10 -0500 | [diff] [blame] | 507 | vk::ImageHelper *writeImage = drawRenderTarget->getImageForWrite(&mFramebuffer); |
| 508 | writeImage->changeLayout(writeImage->getAspectFlags(), vk::ImageLayout::TransferDst, |
| 509 | commandBuffer); |
| 510 | |
Luc Ferron | be30c4f | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 511 | vk::ImageHelper *readImage = readRenderTarget->getImageForRead( |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 512 | &mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 513 | |
Shahbaz Youssefi | 028df5f | 2019-02-13 12:57:10 -0500 | [diff] [blame] | 514 | VkImageSubresourceLayers readSubresource = {}; |
| 515 | readSubresource.aspectMask = aspectMask; |
| 516 | readSubresource.mipLevel = 0; |
| 517 | readSubresource.baseArrayLayer = 0; |
| 518 | readSubresource.layerCount = 1; |
| 519 | |
| 520 | VkImageSubresourceLayers writeSubresource = readSubresource; |
| 521 | |
Luc Ferron | be30c4f | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 522 | vk::ImageHelper::Copy(readImage, writeImage, gl::Offset(), gl::Offset(), |
Shahbaz Youssefi | 028df5f | 2019-02-13 12:57:10 -0500 | [diff] [blame] | 523 | gl::Extents(copyArea.width, copyArea.height, 1), readSubresource, |
| 524 | writeSubresource, commandBuffer); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 525 | return angle::Result::Continue; |
Luc Ferron | be30c4f | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 526 | } |
| 527 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 528 | RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 529 | { |
| 530 | RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 531 | ASSERT(renderTarget && renderTarget->getImage().valid()); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 532 | return renderTarget; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 533 | } |
| 534 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 535 | angle::Result FramebufferVk::blitWithReadback(ContextVk *contextVk, |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 536 | const gl::Rectangle ©Area, |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 537 | VkImageAspectFlagBits aspect, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 538 | RenderTargetVk *readRenderTarget, |
| 539 | RenderTargetVk *drawRenderTarget) |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 540 | { |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 541 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 542 | const angle::Format &readFormat = readRenderTarget->getImageFormat().imageFormat(); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 543 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 544 | ASSERT(aspect == VK_IMAGE_ASPECT_DEPTH_BIT || aspect == VK_IMAGE_ASPECT_STENCIL_BIT); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 545 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 546 | // This path is only currently used for y-flipping depth/stencil blits. |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 547 | PackPixelsParams packPixelsParams; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 548 | packPixelsParams.reverseRowOrder = true; |
| 549 | packPixelsParams.area.width = copyArea.width; |
| 550 | packPixelsParams.area.height = copyArea.height; |
| 551 | packPixelsParams.area.x = copyArea.x; |
| 552 | packPixelsParams.area.y = copyArea.y; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 553 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 554 | // Read back depth values into the destination buffer. |
| 555 | const angle::Format ©Format = GetDepthStencilImageToBufferFormat(readFormat, aspect); |
| 556 | packPixelsParams.destFormat = ©Format; |
| 557 | packPixelsParams.outputPitch = copyFormat.pixelBytes * copyArea.width; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 558 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 559 | // Allocate a space in the destination buffer to write to. |
| 560 | size_t blitAllocationSize = copyFormat.pixelBytes * copyArea.width * copyArea.height; |
| 561 | uint8_t *destPtr = nullptr; |
| 562 | VkBuffer destBufferHandle = VK_NULL_HANDLE; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 563 | VkDeviceSize destOffset = 0; |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 564 | ANGLE_TRY(mBlitPixelBuffer.allocate(contextVk, blitAllocationSize, &destPtr, &destBufferHandle, |
| 565 | &destOffset, nullptr)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 566 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 567 | ANGLE_TRY( |
| 568 | readPixelsImpl(contextVk, copyArea, packPixelsParams, aspect, readRenderTarget, destPtr)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 569 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 570 | VkBufferImageCopy copyRegion = {}; |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 571 | copyRegion.bufferOffset = destOffset; |
| 572 | copyRegion.bufferImageHeight = copyArea.height; |
| 573 | copyRegion.bufferRowLength = copyArea.width; |
| 574 | copyRegion.imageExtent.width = copyArea.width; |
| 575 | copyRegion.imageExtent.height = copyArea.height; |
| 576 | copyRegion.imageExtent.depth = 1; |
| 577 | copyRegion.imageSubresource.mipLevel = 0; |
| 578 | copyRegion.imageSubresource.aspectMask = aspect; |
| 579 | copyRegion.imageSubresource.baseArrayLayer = 0; |
| 580 | copyRegion.imageSubresource.layerCount = 1; |
| 581 | copyRegion.imageOffset.x = copyArea.x; |
| 582 | copyRegion.imageOffset.y = copyArea.y; |
| 583 | copyRegion.imageOffset.z = 0; |
| 584 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 585 | ANGLE_TRY(mBlitPixelBuffer.flush(contextVk)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 586 | |
| 587 | // Reinitialize the commandBuffer after a read pixels because it calls |
| 588 | // renderer->finish which makes command buffers obsolete. |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 589 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 590 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 591 | |
| 592 | // We read the bytes of the image in a buffer, now we have to copy them into the |
| 593 | // destination target. |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 594 | vk::ImageHelper *imageForWrite = drawRenderTarget->getImageForWrite(&mFramebuffer); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 595 | |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 596 | imageForWrite->changeLayout(imageForWrite->getAspectFlags(), vk::ImageLayout::TransferDst, |
| 597 | commandBuffer); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 598 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 599 | commandBuffer->copyBufferToImage(destBufferHandle, imageForWrite->getImage(), |
| 600 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Region); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 601 | |
| 602 | mBlitPixelBuffer.releaseRetainedBuffers(renderer); |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 603 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 604 | return angle::Result::Continue; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 607 | angle::Result FramebufferVk::blit(const gl::Context *context, |
| 608 | const gl::Rectangle &sourceArea, |
| 609 | const gl::Rectangle &destArea, |
| 610 | GLbitfield mask, |
| 611 | GLenum filter) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 612 | { |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 613 | ContextVk *contextVk = vk::GetImpl(context); |
| 614 | RendererVk *renderer = contextVk->getRenderer(); |
| 615 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 616 | const gl::State &glState = context->getState(); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 617 | const gl::Framebuffer *sourceFramebuffer = glState.getReadFramebuffer(); |
Jamie Madill | ba36593 | 2018-07-18 17:23:46 -0400 | [diff] [blame] | 618 | bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0; |
| 619 | bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0; |
| 620 | bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 621 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 622 | FramebufferVk *sourceFramebufferVk = vk::GetImpl(sourceFramebuffer); |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 623 | bool flipSource = contextVk->isViewportFlipEnabledForReadFBO(); |
| 624 | bool flipDest = contextVk->isViewportFlipEnabledForDrawFBO(); |
| 625 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 626 | gl::Rectangle readRect = sourceArea; |
| 627 | gl::Rectangle drawRect = destArea; |
| 628 | |
| 629 | if (glState.isScissorTestEnabled()) |
| 630 | { |
| 631 | const gl::Rectangle scissorRect = glState.getScissor(); |
| 632 | if (!ClipRectangle(sourceArea, scissorRect, &readRect)) |
| 633 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 634 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | if (!ClipRectangle(destArea, scissorRect, &drawRect)) |
| 638 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 639 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | // After cropping for the scissor, we also want to crop for the size of the buffers. |
| 644 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 645 | if (blitColorBuffer) |
| 646 | { |
| 647 | RenderTargetVk *readRenderTarget = sourceFramebufferVk->getColorReadRenderTarget(); |
| 648 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 649 | gl::Rectangle readRenderTargetRect; |
| 650 | if (!ClipToRenderTarget(readRect, readRenderTarget, &readRenderTargetRect)) |
| 651 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 652 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 653 | } |
| 654 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 655 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 656 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 657 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 658 | ASSERT(drawRenderTarget); |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 659 | ASSERT(HasSrcAndDstBlitProperties(renderer, readRenderTarget, drawRenderTarget)); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 660 | |
| 661 | gl::Rectangle drawRenderTargetRect; |
| 662 | if (!ClipToRenderTarget(drawRect, drawRenderTarget, &drawRenderTargetRect)) |
| 663 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 664 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 665 | } |
| 666 | |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 667 | ANGLE_TRY(blitWithCommand(contextVk, readRenderTargetRect, drawRenderTargetRect, |
| 668 | readRenderTarget, drawRenderTarget, filter, true, false, |
| 669 | false, flipSource, flipDest)); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | if (blitDepthBuffer || blitStencilBuffer) |
| 674 | { |
| 675 | RenderTargetVk *readRenderTarget = sourceFramebufferVk->getDepthStencilRenderTarget(); |
| 676 | ASSERT(readRenderTarget); |
| 677 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 678 | gl::Rectangle readRenderTargetRect; |
| 679 | if (!ClipToRenderTarget(readRect, readRenderTarget, &readRenderTargetRect)) |
| 680 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 681 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 682 | } |
| 683 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 684 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil(); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 685 | ASSERT(drawRenderTarget); |
| 686 | |
| 687 | gl::Rectangle drawRenderTargetRect; |
| 688 | if (!ClipToRenderTarget(drawRect, drawRenderTarget, &drawRenderTargetRect)) |
| 689 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 690 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | ASSERT(readRenderTargetRect == drawRenderTargetRect); |
| 694 | ASSERT(filter == GL_NEAREST); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 695 | |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 696 | if (HasSrcAndDstBlitProperties(renderer, readRenderTarget, drawRenderTarget)) |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 697 | { |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 698 | ANGLE_TRY(blitWithCommand(contextVk, readRenderTargetRect, drawRenderTargetRect, |
| 699 | readRenderTarget, drawRenderTarget, filter, false, |
| 700 | blitDepthBuffer, blitStencilBuffer, flipSource, flipDest)); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 701 | } |
| 702 | else |
| 703 | { |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 704 | if (flipSource || flipDest) |
| 705 | { |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 706 | if (blitDepthBuffer) |
| 707 | { |
| 708 | ANGLE_TRY(blitWithReadback(contextVk, readRenderTargetRect, |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 709 | VK_IMAGE_ASPECT_DEPTH_BIT, readRenderTarget, |
| 710 | drawRenderTarget)); |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 711 | } |
| 712 | if (blitStencilBuffer) |
| 713 | { |
| 714 | ANGLE_TRY(blitWithReadback(contextVk, readRenderTargetRect, |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 715 | VK_IMAGE_ASPECT_STENCIL_BIT, readRenderTarget, |
| 716 | drawRenderTarget)); |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 717 | } |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 718 | } |
| 719 | else |
| 720 | { |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 721 | ANGLE_TRY(blitWithCopy(contextVk, readRenderTargetRect, readRenderTarget, |
| 722 | drawRenderTarget, blitDepthBuffer, blitStencilBuffer)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 723 | } |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 727 | return angle::Result::Continue; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 728 | } |
| 729 | |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 730 | angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk, |
| 731 | const gl::Rectangle &readRectIn, |
| 732 | const gl::Rectangle &drawRectIn, |
| 733 | RenderTargetVk *readRenderTarget, |
| 734 | RenderTargetVk *drawRenderTarget, |
| 735 | GLenum filter, |
| 736 | bool colorBlit, |
| 737 | bool depthBlit, |
| 738 | bool stencilBlit, |
| 739 | bool flipSource, |
| 740 | bool flipDest) |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 741 | { |
| 742 | // Since blitRenderbufferRect is called for each render buffer that needs to be blitted, |
| 743 | // it should never be the case that both color and depth/stencil need to be blitted at |
| 744 | // at the same time. |
| 745 | ASSERT(colorBlit != (depthBlit || stencilBlit)); |
| 746 | |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 747 | vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer); |
| 748 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 749 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 750 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
| 751 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 752 | const vk::Format &readImageFormat = readRenderTarget->getImageFormat(); |
| 753 | VkImageAspectFlags aspectMask = |
| 754 | colorBlit ? VK_IMAGE_ASPECT_COLOR_BIT |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 755 | : vk::GetDepthStencilAspectFlags(readImageFormat.imageFormat()); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 756 | vk::ImageHelper *srcImage = readRenderTarget->getImageForRead( |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 757 | &mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 758 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 759 | const gl::Extents sourceFrameBufferExtents = readRenderTarget->getExtents(); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 760 | gl::Rectangle readRect = readRectIn; |
| 761 | |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 762 | if (flipSource) |
| 763 | { |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 764 | readRect.y = sourceFrameBufferExtents.height - readRect.y - readRect.height; |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 765 | } |
| 766 | |
Jamie Madill | ba36593 | 2018-07-18 17:23:46 -0400 | [diff] [blame] | 767 | VkImageBlit blit = {}; |
| 768 | blit.srcOffsets[0] = {readRect.x0(), flipSource ? readRect.y1() : readRect.y0(), 0}; |
| 769 | blit.srcOffsets[1] = {readRect.x1(), flipSource ? readRect.y0() : readRect.y1(), 1}; |
| 770 | blit.srcSubresource.aspectMask = aspectMask; |
| 771 | blit.srcSubresource.mipLevel = 0; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 772 | blit.srcSubresource.baseArrayLayer = 0; |
| 773 | blit.srcSubresource.layerCount = 1; |
| 774 | blit.dstSubresource.aspectMask = aspectMask; |
| 775 | blit.dstSubresource.mipLevel = 0; |
| 776 | blit.dstSubresource.baseArrayLayer = 0; |
| 777 | blit.dstSubresource.layerCount = 1; |
| 778 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 779 | const gl::Extents drawFrameBufferExtents = drawRenderTarget->getExtents(); |
| 780 | gl::Rectangle drawRect = drawRectIn; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 781 | |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 782 | if (flipDest) |
| 783 | { |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 784 | drawRect.y = drawFrameBufferExtents.height - drawRect.y - drawRect.height; |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 785 | } |
| 786 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 787 | blit.dstOffsets[0] = {drawRect.x0(), flipDest ? drawRect.y1() : drawRect.y0(), 0}; |
| 788 | blit.dstOffsets[1] = {drawRect.x1(), flipDest ? drawRect.y0() : drawRect.y1(), 1}; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 789 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 790 | // Requirement of the copyImageToBuffer, the dst image must be in |
| 791 | // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout. |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 792 | dstImage->changeLayout(aspectMask, vk::ImageLayout::TransferDst, commandBuffer); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 793 | |
| 794 | commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 795 | dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, |
| 796 | gl_vk::GetFilter(filter)); |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 797 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 798 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 799 | } |
| 800 | |
Kenneth Russell | ce8602a | 2017-10-03 18:23:08 -0700 | [diff] [blame] | 801 | bool FramebufferVk::checkStatus(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 802 | { |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 803 | // if we have both a depth and stencil buffer, they must refer to the same object |
| 804 | // since we only support packed_depth_stencil and not separate depth and stencil |
| 805 | if (mState.hasSeparateDepthAndStencilAttachments()) |
| 806 | { |
| 807 | return false; |
| 808 | } |
| 809 | |
Jamie Madill | b79e7bb | 2017-10-24 13:55:50 -0400 | [diff] [blame] | 810 | return true; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 811 | } |
| 812 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 813 | angle::Result FramebufferVk::syncState(const gl::Context *context, |
| 814 | const gl::Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 815 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 816 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 7bd1666 | 2017-10-28 19:40:50 -0400 | [diff] [blame] | 817 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 818 | |
| 819 | ASSERT(dirtyBits.any()); |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 820 | for (size_t dirtyBit : dirtyBits) |
| 821 | { |
| 822 | switch (dirtyBit) |
| 823 | { |
| 824 | case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
| 825 | case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
| 826 | ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState)); |
| 827 | break; |
| 828 | case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
| 829 | case gl::Framebuffer::DIRTY_BIT_READ_BUFFER: |
| 830 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH: |
| 831 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT: |
| 832 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES: |
| 833 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS: |
| 834 | break; |
| 835 | default: |
| 836 | { |
| 837 | ASSERT(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 && |
| 838 | dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 839 | size_t colorIndexGL = |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 840 | static_cast<size_t>(dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 841 | ANGLE_TRY( |
| 842 | mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL)); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 843 | |
| 844 | // Update cached masks for masked clears. |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 845 | RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 846 | if (renderTarget) |
| 847 | { |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 848 | const angle::Format &emulatedFormat = |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 849 | renderTarget->getImageFormat().imageFormat(); |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 850 | updateActiveColorMasks( |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 851 | colorIndexGL, emulatedFormat.redBits > 0, emulatedFormat.greenBits > 0, |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 852 | emulatedFormat.blueBits > 0, emulatedFormat.alphaBits > 0); |
| 853 | |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 854 | const angle::Format &sourceFormat = |
| 855 | renderTarget->getImageFormat().angleFormat(); |
| 856 | mEmulatedAlphaAttachmentMask.set( |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 857 | colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0); |
Luc Ferron | 0bb940a | 2018-06-22 09:59:34 -0400 | [diff] [blame] | 858 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 859 | contextVk->updateColorMask(context->getState().getBlendState()); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 860 | } |
| 861 | else |
| 862 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 863 | updateActiveColorMasks(colorIndexGL, false, false, false, false); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 864 | } |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 865 | break; |
| 866 | } |
| 867 | } |
| 868 | } |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 869 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 870 | mActiveColorComponents = gl_vk::GetColorComponentFlags( |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 871 | mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(), |
| 872 | mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any()); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 873 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 874 | mFramebuffer.release(renderer); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 875 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 876 | // 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] | 877 | // create a new entry in the command graph. |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 878 | mFramebuffer.finishCurrentCommands(renderer); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 879 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 880 | // Notify the ContextVk to update the pipeline desc. |
| 881 | updateRenderPassDesc(); |
| 882 | contextVk->onFramebufferChange(mRenderPassDesc); |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 883 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 884 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 885 | } |
| 886 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 887 | void FramebufferVk::updateRenderPassDesc() |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 888 | { |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 889 | mRenderPassDesc = {}; |
| 890 | mRenderPassDesc.setSamples(getSamples()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 891 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 892 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 893 | const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers(); |
| 894 | for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 895 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 896 | if (enabledDrawBuffers[colorIndexGL]) |
| 897 | { |
| 898 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
| 899 | ASSERT(colorRenderTarget); |
| 900 | mRenderPassDesc.packColorAttachment( |
| 901 | colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID); |
| 902 | } |
| 903 | else |
| 904 | { |
| 905 | mRenderPassDesc.packColorAttachmentGap(colorIndexGL); |
| 906 | } |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 907 | } |
| 908 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 909 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 910 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 911 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 912 | mRenderPassDesc.packDepthStencilAttachment( |
| 913 | depthStencilRenderTarget->getImage().getFormat().angleFormatID); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 914 | } |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 915 | } |
| 916 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 917 | angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 918 | { |
| 919 | // If we've already created our cached Framebuffer, return it. |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 920 | if (mFramebuffer.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 921 | { |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 922 | *framebufferOut = &mFramebuffer.getFramebuffer(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 923 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 924 | } |
| 925 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 926 | vk::RenderPass *compatibleRenderPass = nullptr; |
| 927 | ANGLE_TRY(contextVk->getRenderer()->getCompatibleRenderPass(contextVk, mRenderPassDesc, |
| 928 | &compatibleRenderPass)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 929 | |
| 930 | // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it. |
| 931 | if (mBackbuffer) |
| 932 | { |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 933 | return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | // Gather VkImageViews over all FBO attachments, also size of attached region. |
| 937 | std::vector<VkImageView> attachments; |
| 938 | gl::Extents attachmentsSize; |
| 939 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 940 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 941 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 942 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 943 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 944 | ASSERT(colorRenderTarget); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 945 | attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 946 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 947 | ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents()); |
| 948 | attachmentsSize = colorRenderTarget->getExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 949 | } |
| 950 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 951 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 952 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 953 | { |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 954 | attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 955 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 956 | ASSERT(attachmentsSize.empty() || |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 957 | attachmentsSize == depthStencilRenderTarget->getExtents()); |
| 958 | attachmentsSize = depthStencilRenderTarget->getExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 959 | } |
| 960 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 961 | VkFramebufferCreateInfo framebufferInfo = {}; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 962 | |
| 963 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 964 | framebufferInfo.flags = 0; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 965 | framebufferInfo.renderPass = compatibleRenderPass->getHandle(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 966 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); |
| 967 | framebufferInfo.pAttachments = attachments.data(); |
| 968 | framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width); |
| 969 | framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height); |
| 970 | framebufferInfo.layers = 1; |
| 971 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 972 | ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo)); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 973 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 974 | *framebufferOut = &mFramebuffer.getFramebuffer(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 975 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 976 | } |
| 977 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 978 | angle::Result FramebufferVk::clearWithRenderPassOp( |
| 979 | ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 980 | const gl::Rectangle &clearArea, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 981 | gl::DrawBufferMask clearColorBuffers, |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 982 | bool clearDepth, |
| 983 | bool clearStencil, |
| 984 | const VkClearColorValue &clearColorValue, |
| 985 | const VkClearDepthStencilValue &clearDepthStencilValue) |
| 986 | { |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 987 | // Start a new render pass if: |
| 988 | // |
| 989 | // - no render pass has started, |
| 990 | // - there is a render pass started but it contains commands; we cannot modify its ops, so new |
| 991 | // render pass is needed, |
| 992 | // - the current render area doesn't match the clear area. We need the render area to be |
| 993 | // exactly as specified by the scissor for the loadOp to clear only that area. See |
| 994 | // onScissorChange for more information. |
| 995 | |
| 996 | if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() || |
| 997 | mFramebuffer.getRenderPassRenderArea() != clearArea) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 998 | { |
| 999 | vk::CommandBuffer *commandBuffer; |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1000 | ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer)); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1001 | } |
| 1002 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1003 | size_t attachmentIndexVk = 0; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1004 | |
| 1005 | // Go through clearColorBuffers and set the appropriate loadOp and clear values. |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1006 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1007 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1008 | if (clearColorBuffers.test(colorIndexGL)) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1009 | { |
| 1010 | RenderTargetVk *renderTarget = getColorReadRenderTarget(); |
| 1011 | |
| 1012 | // If the render target doesn't have alpha, but its emulated format has it, clear the |
| 1013 | // alpha to 1. |
| 1014 | VkClearColorValue value = clearColorValue; |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1015 | if (mEmulatedAlphaAttachmentMask[colorIndexGL]) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1016 | { |
| 1017 | SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value); |
| 1018 | } |
| 1019 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1020 | mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1021 | } |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1022 | ++attachmentIndexVk; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | // Set the appropriate loadOp and clear values for depth and stencil. |
| 1026 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1027 | if (depthStencilRenderTarget) |
| 1028 | { |
| 1029 | if (clearDepth) |
| 1030 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1031 | mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1032 | clearDepthStencilValue.depth); |
| 1033 | } |
| 1034 | |
| 1035 | if (clearStencil) |
| 1036 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1037 | mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1038 | clearDepthStencilValue.stencil); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | return angle::Result::Continue; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1043 | } |
| 1044 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1045 | angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1046 | const gl::Rectangle &clearArea, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1047 | gl::DrawBufferMask clearColorBuffers, |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1048 | bool clearStencil, |
| 1049 | VkColorComponentFlags colorMaskFlags, |
| 1050 | uint8_t stencilMask, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1051 | const VkClearColorValue &clearColorValue, |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1052 | uint8_t clearStencilValue) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1053 | { |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 1054 | RendererVk *renderer = contextVk->getRenderer(); |
| 1055 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1056 | UtilsVk::ClearFramebufferParameters params = {}; |
| 1057 | params.renderPassDesc = &getRenderPassDesc(); |
| 1058 | params.renderAreaHeight = mState.getDimensions().height; |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1059 | params.clearArea = clearArea; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1060 | params.colorClearValue = clearColorValue; |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1061 | params.stencilClearValue = clearStencilValue; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1062 | params.stencilMask = stencilMask; |
| 1063 | |
| 1064 | params.clearColor = true; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1065 | params.clearStencil = clearStencil; |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1066 | |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1067 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1068 | for (size_t colorIndexGL : clearColorBuffers) |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1069 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1070 | const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1071 | ASSERT(colorRenderTarget); |
| 1072 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1073 | params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat(); |
| 1074 | params.colorAttachmentIndexGL = colorIndexGL; |
| 1075 | params.colorMaskFlags = colorMaskFlags; |
| 1076 | if (mEmulatedAlphaAttachmentMask[colorIndexGL]) |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1077 | { |
| 1078 | params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT; |
| 1079 | } |
| 1080 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1081 | ANGLE_TRY(renderer->getUtils().clearFramebuffer(contextVk, this, params)); |
| 1082 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1083 | // Clear stencil only once! |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1084 | params.clearStencil = false; |
| 1085 | } |
| 1086 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1087 | // If there was no color clear, clear stencil alone. |
| 1088 | if (params.clearStencil) |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1089 | { |
| 1090 | params.clearColor = false; |
| 1091 | ANGLE_TRY(renderer->getUtils().clearFramebuffer(contextVk, this, params)); |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | return angle::Result::Continue; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1095 | } |
| 1096 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 1097 | angle::Result FramebufferVk::getSamplePosition(const gl::Context *context, |
| 1098 | size_t index, |
| 1099 | GLfloat *xy) const |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1100 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 1101 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1102 | return angle::Result::Stop; |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Jamie Madill | d1249de | 2018-08-28 16:58:53 -0400 | [diff] [blame] | 1105 | angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1106 | const gl::Rectangle &renderArea, |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1107 | vk::CommandBuffer **commandBufferOut) |
Jamie Madill | d1249de | 2018-08-28 16:58:53 -0400 | [diff] [blame] | 1108 | { |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1109 | vk::Framebuffer *framebuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1110 | ANGLE_TRY(getFramebuffer(contextVk, &framebuffer)); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1111 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1112 | vk::AttachmentOpsArray renderPassAttachmentOps; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1113 | std::vector<VkClearValue> attachmentClearValues; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1114 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1115 | vk::CommandBuffer *writeCommands = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1116 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands)); |
Jamie Madill | e4c5a23 | 2018-03-02 21:00:31 -0500 | [diff] [blame] | 1117 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1118 | // Initialize RenderPass info. |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1119 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1120 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1121 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1122 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1123 | ASSERT(colorRenderTarget); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1124 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1125 | ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands)); |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1126 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1127 | renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(), |
| 1128 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1129 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 1130 | attachmentClearValues.emplace_back(kUninitializedClearValue); |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1134 | if (depthStencilRenderTarget) |
| 1135 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1136 | ANGLE_TRY( |
| 1137 | depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands)); |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1138 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1139 | renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(), |
| 1140 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1141 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
| 1142 | attachmentClearValues.emplace_back(kUninitializedClearValue); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1143 | } |
| 1144 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1145 | return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc, |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1146 | renderPassAttachmentOps, attachmentClearValues, |
| 1147 | commandBufferOut); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1150 | 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] | 1151 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1152 | mActiveColorComponentMasksForClear[0].set(colorIndexGL, r); |
| 1153 | mActiveColorComponentMasksForClear[1].set(colorIndexGL, g); |
| 1154 | mActiveColorComponentMasksForClear[2].set(colorIndexGL, b); |
| 1155 | mActiveColorComponentMasksForClear[3].set(colorIndexGL, a); |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1156 | } |
| 1157 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1158 | const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1159 | { |
| 1160 | return mEmulatedAlphaAttachmentMask; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1161 | } |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1162 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1163 | angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk, |
| 1164 | const gl::Rectangle &area, |
| 1165 | const PackPixelsParams &packPixelsParams, |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1166 | VkImageAspectFlagBits copyAspectFlags, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1167 | RenderTargetVk *renderTarget, |
| 1168 | void *pixels) |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1169 | { |
Tobin Ehlis | 4a41914 | 2019-02-05 08:50:30 -0700 | [diff] [blame] | 1170 | TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl"); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1171 | |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame^] | 1172 | RendererVk *renderer = contextVk->getRenderer(); |
| 1173 | |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame] | 1174 | ANGLE_TRY(renderTarget->ensureImageInitialized(contextVk)); |
| 1175 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1176 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1177 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1178 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1179 | // Note that although we're reading from the image, we need to update the layout below. |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1180 | |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 1181 | vk::ImageHelper *srcImage = |
| 1182 | renderTarget->getImageForRead(&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1183 | |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 1184 | const angle::Format *readFormat = &srcImage->getFormat().imageFormat(); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1185 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1186 | if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT) |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1187 | { |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1188 | readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1189 | } |
| 1190 | |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame^] | 1191 | size_t level = renderTarget->getLevelIndex(); |
| 1192 | size_t layer = renderTarget->getLayerIndex(); |
| 1193 | VkOffset3D srcOffset = {area.x, area.y, 0}; |
| 1194 | VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height), |
| 1195 | 1}; |
| 1196 | |
| 1197 | // If the source image is multisampled, we need to resolve it into a temporary image before |
| 1198 | // performing a readback. |
| 1199 | bool isMultisampled = srcImage->getSamples() > 1; |
| 1200 | vk::Scoped<vk::ImageHelper> resolvedImage(contextVk->getDevice()); |
| 1201 | if (isMultisampled) |
| 1202 | { |
| 1203 | ANGLE_TRY(resolvedImage.get().init2DStaging( |
| 1204 | contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1), |
| 1205 | srcImage->getFormat(), |
| 1206 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1)); |
| 1207 | resolvedImage.get().updateQueueSerial(renderer->getCurrentQueueSerial()); |
| 1208 | |
| 1209 | // TODO(syoussefi): resolve only works on color images (not depth/stencil). If readback |
| 1210 | // on multisampled depth/stencil image is done, we would need a different path. One |
| 1211 | // possible solution would be a compute shader that directly reads from the multisampled |
| 1212 | // image, performs the resolve and outputs to the buffer in one go. |
| 1213 | // http://anglebug.com/3200 |
| 1214 | ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT); |
| 1215 | |
| 1216 | VkImageResolve resolveRegion = {}; |
| 1217 | resolveRegion.srcSubresource.aspectMask = copyAspectFlags; |
| 1218 | resolveRegion.srcSubresource.mipLevel = level; |
| 1219 | resolveRegion.srcSubresource.baseArrayLayer = layer; |
| 1220 | resolveRegion.srcSubresource.layerCount = 1; |
| 1221 | resolveRegion.srcOffset = srcOffset; |
| 1222 | resolveRegion.dstSubresource.aspectMask = copyAspectFlags; |
| 1223 | resolveRegion.dstSubresource.mipLevel = 0; |
| 1224 | resolveRegion.dstSubresource.baseArrayLayer = 0; |
| 1225 | resolveRegion.dstSubresource.layerCount = 1; |
| 1226 | resolveRegion.dstOffset = {}; |
| 1227 | resolveRegion.extent = srcExtent; |
| 1228 | |
| 1229 | srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer); |
| 1230 | |
| 1231 | resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc, |
| 1232 | commandBuffer); |
| 1233 | |
| 1234 | // Make the resolved image the target of buffer copy. |
| 1235 | srcImage = &resolvedImage.get(); |
| 1236 | level = 0; |
| 1237 | layer = 0; |
| 1238 | srcOffset = {0, 0, 0}; |
| 1239 | } |
| 1240 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1241 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 1242 | uint8_t *readPixelBuffer = nullptr; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 1243 | VkDeviceSize stagingOffset = 0; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1244 | size_t allocationSize = readFormat->pixelBytes * area.width * area.height; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1245 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 1246 | ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle, |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 1247 | &stagingOffset, nullptr)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1248 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1249 | VkBufferImageCopy region = {}; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame^] | 1250 | region.bufferImageHeight = srcExtent.height; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 1251 | region.bufferOffset = stagingOffset; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame^] | 1252 | region.bufferRowLength = srcExtent.width; |
| 1253 | region.imageExtent = srcExtent; |
| 1254 | region.imageOffset = srcOffset; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1255 | region.imageSubresource.aspectMask = copyAspectFlags; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame^] | 1256 | region.imageSubresource.baseArrayLayer = layer; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1257 | region.imageSubresource.layerCount = 1; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame^] | 1258 | region.imageSubresource.mipLevel = level; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1259 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1260 | commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(), |
| 1261 | bufferHandle, 1, ®ion); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1262 | |
| 1263 | // Triggers a full finish. |
| 1264 | // TODO(jmadill): Don't block on asynchronous readback. |
Geoff Lang | 892d180 | 2019-03-27 14:21:34 -0400 | [diff] [blame] | 1265 | ANGLE_TRY(contextVk->finishImpl()); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1266 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1267 | // The buffer we copied to needs to be invalidated before we read from it because its not been |
| 1268 | // created with the host coherent bit. |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 1269 | ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk)); |
Yuly Novikov | 6c6c76c | 2018-05-17 18:45:06 +0000 | [diff] [blame] | 1270 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1271 | PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, readPixelBuffer, |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 1272 | static_cast<uint8_t *>(pixels)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1273 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1274 | return angle::Result::Continue; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1275 | } |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1276 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1277 | gl::Extents FramebufferVk::getReadImageExtents() const |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1278 | { |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1279 | ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width); |
| 1280 | ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height); |
| 1281 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1282 | return getColorReadRenderTarget()->getExtents(); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1283 | } |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 1284 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1285 | gl::Rectangle FramebufferVk::getCompleteRenderArea() const |
| 1286 | { |
| 1287 | return gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height); |
| 1288 | } |
| 1289 | |
| 1290 | gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const |
| 1291 | { |
| 1292 | const gl::Rectangle renderArea(0, 0, mState.getDimensions().width, |
| 1293 | mState.getDimensions().height); |
| 1294 | bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO(); |
| 1295 | |
| 1296 | return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport); |
| 1297 | } |
| 1298 | |
| 1299 | void FramebufferVk::onScissorChange(ContextVk *contextVk) |
| 1300 | { |
| 1301 | gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk); |
| 1302 | |
| 1303 | // If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass |
| 1304 | // is restarted. Otherwise, we can continue using the same renderpass area. |
| 1305 | // |
| 1306 | // Without a scissor, the render pass area covers the whole of the framebuffer. With a |
| 1307 | // scissored clear, the render pass area could be smaller than the framebuffer size. When the |
| 1308 | // scissor changes, if the scissor area is completely encompassed by the render pass area, it's |
| 1309 | // possible to continue using the same render pass. However, if the current render pass area |
| 1310 | // is too small, we need to start a new one. The latter can happen if a scissored clear starts |
| 1311 | // a render pass, the scissor is disabled and a draw call is issued to affect the whole |
| 1312 | // framebuffer. |
| 1313 | mFramebuffer.updateQueueSerial(contextVk->getRenderer()->getCurrentQueueSerial()); |
| 1314 | if (mFramebuffer.hasStartedRenderPass() && |
| 1315 | !mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea)) |
| 1316 | { |
| 1317 | mFramebuffer.finishCurrentCommands(contextVk->getRenderer()); |
| 1318 | } |
| 1319 | } |
| 1320 | |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 1321 | RenderTargetVk *FramebufferVk::getFirstRenderTarget() const |
| 1322 | { |
| 1323 | for (auto *renderTarget : mRenderTargetCache.getColors()) |
| 1324 | { |
| 1325 | if (renderTarget) |
| 1326 | { |
| 1327 | return renderTarget; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | return mRenderTargetCache.getDepthStencil(); |
| 1332 | } |
| 1333 | |
| 1334 | GLint FramebufferVk::getSamples() const |
| 1335 | { |
| 1336 | RenderTargetVk *firstRT = getFirstRenderTarget(); |
| 1337 | return firstRT ? firstRT->getImage().getSamples() : 0; |
| 1338 | } |
| 1339 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1340 | } // namespace rx |