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