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