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