Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // FramebufferVk.cpp: |
| 7 | // Implements the class methods for FramebufferVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
| 11 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 12 | #include <vulkan/vulkan.h> |
Jamie Madill | 231c7f5 | 2017-04-26 13:45:37 -0400 | [diff] [blame] | 13 | #include <array> |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 14 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 15 | #include "common/debug.h" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 16 | #include "libANGLE/Context.h" |
| 17 | #include "libANGLE/Display.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 18 | #include "libANGLE/formatutils.h" |
| 19 | #include "libANGLE/renderer/renderer_utils.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 21 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 22 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 23 | #include "libANGLE/renderer/vulkan/RenderTargetVk.h" |
| 24 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
| 25 | #include "libANGLE/renderer/vulkan/SurfaceVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 26 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 27 | |
| 28 | namespace rx |
| 29 | { |
| 30 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 31 | namespace |
| 32 | { |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 33 | constexpr size_t kMinReadPixelsBufferSize = 128000; |
| 34 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 35 | const gl::InternalFormat &GetReadAttachmentInfo(const gl::Context *context, |
| 36 | RenderTargetVk *renderTarget) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 37 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 38 | GLenum implFormat = |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 39 | renderTarget->getImageFormat().textureFormat().fboImplementationInternalFormat; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 40 | return gl::GetSizedInternalFormatInfo(implFormat); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 41 | } |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 42 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 43 | bool ClipToRenderTarget(const gl::Rectangle &area, |
| 44 | RenderTargetVk *renderTarget, |
| 45 | gl::Rectangle *rectOut) |
| 46 | { |
| 47 | const gl::Extents &renderTargetExtents = renderTarget->getImageExtents(); |
| 48 | gl::Rectangle renderTargetRect(0, 0, renderTargetExtents.width, renderTargetExtents.height); |
| 49 | return ClipRectangle(area, renderTargetRect, rectOut); |
| 50 | } |
| 51 | |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 52 | bool HasSrcAndDstBlitProperties(RendererVk *renderer, |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 53 | RenderTargetVk *srcRenderTarget, |
| 54 | RenderTargetVk *dstRenderTarget) |
| 55 | { |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 56 | const VkFormat srcFormat = srcRenderTarget->getImageFormat().vkTextureFormat; |
| 57 | const VkFormat dstFormat = dstRenderTarget->getImageFormat().vkTextureFormat; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 58 | |
| 59 | // 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] | 60 | return renderer->hasTextureFormatFeatureBits(srcFormat, VK_FORMAT_FEATURE_BLIT_SRC_BIT) && |
| 61 | renderer->hasTextureFormatFeatureBits(dstFormat, VK_FORMAT_FEATURE_BLIT_DST_BIT); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 62 | } |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 63 | |
| 64 | // Special rules apply to VkBufferImageCopy with depth/stencil. The components are tightly packed |
| 65 | // into a depth or stencil section of the destination buffer. See the spec: |
| 66 | // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkBufferImageCopy.html |
| 67 | const angle::Format &GetDepthStencilImageToBufferFormat(const angle::Format &imageFormat, |
| 68 | VkImageAspectFlagBits copyAspect) |
| 69 | { |
| 70 | if (copyAspect == VK_IMAGE_ASPECT_STENCIL_BIT) |
| 71 | { |
| 72 | ASSERT(imageFormat.id == angle::FormatID::D24_UNORM_S8_UINT || |
| 73 | imageFormat.id == angle::FormatID::D32_FLOAT_S8X24_UINT || |
| 74 | imageFormat.id == angle::FormatID::S8_UINT); |
| 75 | return angle::Format::Get(angle::FormatID::S8_UINT); |
| 76 | } |
| 77 | |
| 78 | ASSERT(copyAspect == VK_IMAGE_ASPECT_DEPTH_BIT); |
| 79 | |
| 80 | switch (imageFormat.id) |
| 81 | { |
| 82 | case angle::FormatID::D16_UNORM: |
| 83 | return imageFormat; |
| 84 | case angle::FormatID::D24_UNORM_X8_UINT: |
| 85 | return imageFormat; |
| 86 | case angle::FormatID::D24_UNORM_S8_UINT: |
| 87 | return angle::Format::Get(angle::FormatID::D24_UNORM_X8_UINT); |
| 88 | case angle::FormatID::D32_FLOAT: |
| 89 | return imageFormat; |
| 90 | case angle::FormatID::D32_FLOAT_S8X24_UINT: |
| 91 | return angle::Format::Get(angle::FormatID::D32_FLOAT); |
| 92 | default: |
| 93 | UNREACHABLE(); |
| 94 | return imageFormat; |
| 95 | } |
| 96 | } |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 97 | } // anonymous namespace |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 98 | |
| 99 | // static |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 100 | FramebufferVk *FramebufferVk::CreateUserFBO(RendererVk *renderer, const gl::FramebufferState &state) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 101 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 102 | return new FramebufferVk(renderer, state, nullptr); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // static |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 106 | FramebufferVk *FramebufferVk::CreateDefaultFBO(RendererVk *renderer, |
| 107 | const gl::FramebufferState &state, |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 108 | WindowSurfaceVk *backbuffer) |
| 109 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 110 | return new FramebufferVk(renderer, state, backbuffer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 113 | FramebufferVk::FramebufferVk(RendererVk *renderer, |
| 114 | const gl::FramebufferState &state, |
| 115 | WindowSurfaceVk *backbuffer) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 116 | : FramebufferImpl(state), |
| 117 | mBackbuffer(backbuffer), |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 118 | mActiveColorComponents(0), |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 119 | mReadPixelBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT, kMinReadPixelsBufferSize, true), |
| 120 | mBlitPixelBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, kMinReadPixelsBufferSize, true) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 121 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 122 | mBlitPixelBuffer.init(1, renderer); |
Frank Henigman | 49b0f6e | 2018-10-03 23:15:29 -0400 | [diff] [blame] | 123 | mReadPixelBuffer.init(4, renderer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 126 | FramebufferVk::~FramebufferVk() = default; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 127 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 128 | void FramebufferVk::destroy(const gl::Context *context) |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 129 | { |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 130 | ContextVk *contextVk = vk::GetImpl(context); |
| 131 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 132 | mFramebuffer.release(renderer); |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 133 | |
Shahbaz Youssefi | fca8fd6 | 2018-11-13 13:55:48 -0500 | [diff] [blame] | 134 | mReadPixelBuffer.release(renderer); |
| 135 | mBlitPixelBuffer.release(renderer); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 138 | angle::Result FramebufferVk::discard(const gl::Context *context, |
| 139 | size_t count, |
| 140 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 141 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 142 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 143 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 146 | angle::Result FramebufferVk::invalidate(const gl::Context *context, |
| 147 | size_t count, |
| 148 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 149 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 150 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 151 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 154 | angle::Result FramebufferVk::invalidateSub(const gl::Context *context, |
| 155 | size_t count, |
| 156 | const GLenum *attachments, |
| 157 | const gl::Rectangle &area) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 158 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 159 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 160 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 163 | angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 164 | { |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 165 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 166 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 167 | // This command buffer is only started once. |
Jamie Madill | 50cf2be | 2018-06-15 09:46:57 -0400 | [diff] [blame] | 168 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 169 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 170 | const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment(); |
| 171 | bool clearDepth = (depthAttachment && (mask & GL_DEPTH_BUFFER_BIT) != 0); |
| 172 | ASSERT(!clearDepth || depthAttachment->isAttached()); |
| 173 | |
Shahbaz Youssefi | 2197dc5 | 2018-10-30 11:29:58 -0400 | [diff] [blame] | 174 | // If depth write is disabled, pretend that GL_DEPTH_BUFFER_BIT is not specified altogether. |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 175 | clearDepth = clearDepth && contextVk->getState().getDepthStencilState().depthMask; |
Shahbaz Youssefi | 2197dc5 | 2018-10-30 11:29:58 -0400 | [diff] [blame] | 176 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 177 | const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment(); |
| 178 | bool clearStencil = (stencilAttachment && (mask & GL_STENCIL_BUFFER_BIT) != 0); |
| 179 | ASSERT(!clearStencil || stencilAttachment->isAttached()); |
| 180 | |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 181 | bool clearColor = IsMaskFlagSet(static_cast<int>(mask), GL_COLOR_BUFFER_BIT); |
| 182 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 183 | const gl::State &glState = context->getState(); |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 184 | |
| 185 | VkClearDepthStencilValue clearDepthStencilValue = |
| 186 | contextVk->getClearDepthStencilValue().depthStencil; |
| 187 | |
| 188 | // Apply the stencil mask to the clear value. |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 189 | clearDepthStencilValue.stencil &= contextVk->getState().getDepthStencilState().stencilWritemask; |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 190 | |
| 191 | // If the depth or stencil is being cleared, and the image was originally requested to have a |
| 192 | // single aspect, but it's emulated with a depth/stencil format, clear both aspects, setting the |
| 193 | // other aspect to 0. |
| 194 | if (clearStencil || clearDepth) |
| 195 | { |
| 196 | RenderTargetVk *depthStencil = mRenderTargetCache.getDepthStencil(); |
| 197 | const vk::Format &format = depthStencil->getImageFormat(); |
| 198 | |
| 199 | // GL_DEPTH_COMPONENT24 is always emulated with a format that has stencil. |
| 200 | if (format.angleFormat().stencilBits == 0) |
| 201 | { |
| 202 | clearStencil = true; |
| 203 | clearDepthStencilValue.stencil = 0; |
| 204 | } |
| 205 | // GL_STENCIL_INDEX8 may or may not be emulated. |
| 206 | else if (format.angleFormat().depthBits == 0 && format.vkTextureFormat != VK_FORMAT_S8_UINT) |
| 207 | { |
| 208 | clearDepth = true; |
| 209 | clearDepthStencilValue.depth = 0; |
| 210 | } |
| 211 | } |
Luc Ferron | 8836f63 | 2018-04-05 07:26:53 -0400 | [diff] [blame] | 212 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 213 | // The most costly clear mode is when we need to mask out specific color channels. This can |
| 214 | // only be done with a draw call. The scissor region however can easily be integrated with |
| 215 | // this method. Similarly for depth/stencil clear. |
| 216 | VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask(); |
| 217 | if (clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents) |
| 218 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 219 | ANGLE_TRY(clearWithDraw(contextVk, colorMaskFlags)); |
Jamie Madill | 78cd940 | 2018-05-10 16:49:58 -0400 | [diff] [blame] | 220 | |
| 221 | // Stencil clears must be handled separately. The only way to write out a stencil value from |
| 222 | // a fragment shader in Vulkan is with VK_EXT_shader_stencil_export. Support for this |
| 223 | // extension is sparse. Hence, we call into the RenderPass clear path. We similarly clear |
| 224 | // depth to keep the code simple, but depth clears could be combined with the masked color |
| 225 | // clears as an optimization. |
| 226 | |
| 227 | if (clearDepth || clearStencil) |
| 228 | { |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 229 | ANGLE_TRY(clearWithClearAttachments(contextVk, false, clearDepth, clearStencil, |
| 230 | clearDepthStencilValue)); |
Jamie Madill | 78cd940 | 2018-05-10 16:49:58 -0400 | [diff] [blame] | 231 | } |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 232 | return angle::Result::Continue; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 235 | if (glState.isScissorTestEnabled()) |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 236 | { |
| 237 | // With scissor test enabled, we clear very differently and we don't need to access |
| 238 | // the image inside each attachment we can just use clearCmdAttachments with our |
| 239 | // scissor region instead. |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 240 | ANGLE_TRY(clearWithClearAttachments(contextVk, clearColor, clearDepth, clearStencil, |
| 241 | clearDepthStencilValue)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 242 | return angle::Result::Continue; |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Standard Depth/stencil clear without scissor. |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 246 | if (clearDepth || clearStencil) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 247 | { |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 248 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 249 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 250 | RenderTargetVk *renderTarget = mRenderTargetCache.getDepthStencil(); |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 251 | const angle::Format &format = renderTarget->getImageFormat().textureFormat(); |
| 252 | const VkImageAspectFlags aspectFlags = vk::GetDepthStencilAspectFlags(format); |
| 253 | |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 254 | VkImageAspectFlags clearAspects = aspectFlags; |
| 255 | if (!clearDepth) |
| 256 | { |
| 257 | clearAspects &= ~VK_IMAGE_ASPECT_DEPTH_BIT; |
| 258 | } |
| 259 | if (!clearStencil) |
| 260 | { |
| 261 | clearAspects &= ~VK_IMAGE_ASPECT_STENCIL_BIT; |
| 262 | } |
| 263 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 264 | vk::ImageHelper *image = renderTarget->getImageForWrite(&mFramebuffer); |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 265 | image->clearDepthStencil(aspectFlags, clearAspects, clearDepthStencilValue, commandBuffer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 266 | } |
| 267 | |
Luc Ferron | d3ab307 | 2018-06-19 09:55:04 -0400 | [diff] [blame] | 268 | if (!clearColor) |
| 269 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 270 | return angle::Result::Continue; |
Luc Ferron | d3ab307 | 2018-06-19 09:55:04 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 273 | const auto *attachment = mState.getFirstNonNullAttachment(); |
| 274 | ASSERT(attachment && attachment->isAttached()); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 275 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 276 | if (!commandBuffer) |
| 277 | { |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 278 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 279 | } |
Jamie Madill | efb5a5c | 2018-01-29 15:56:59 -0500 | [diff] [blame] | 280 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 281 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
Jamie Madill | 50cf2be | 2018-06-15 09:46:57 -0400 | [diff] [blame] | 282 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 283 | const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 284 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 285 | { |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 286 | VkClearColorValue modifiedClearColorValue = clearColorValue; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 287 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex]; |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 288 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 289 | // It's possible we're clearing a render target that has no alpha channel but we represent |
| 290 | // 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] | 291 | // clear value is in that case. |
| 292 | if (mEmulatedAlphaAttachmentMask[colorIndex]) |
| 293 | { |
| 294 | modifiedClearColorValue.float32[3] = 1.0; |
| 295 | } |
| 296 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 297 | ASSERT(colorRenderTarget); |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 298 | vk::ImageHelper *image = colorRenderTarget->getImageForWrite(&mFramebuffer); |
Luc Ferron | c20b950 | 2018-05-24 09:30:17 -0400 | [diff] [blame] | 299 | GLint mipLevelToClear = (attachment->type() == GL_TEXTURE) ? attachment->mipLevel() : 0; |
Jamie Madill | 3f3b358 | 2018-09-14 10:38:44 -0400 | [diff] [blame] | 300 | |
| 301 | // If we're clearing a cube map face ensure we only clear the selected layer. |
| 302 | image->clearColorLayer(modifiedClearColorValue, mipLevelToClear, 1, |
| 303 | colorRenderTarget->getLayerIndex(), 1, commandBuffer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 304 | } |
| 305 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 306 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 307 | } |
| 308 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 309 | angle::Result FramebufferVk::clearBufferfv(const gl::Context *context, |
| 310 | GLenum buffer, |
| 311 | GLint drawbuffer, |
| 312 | const GLfloat *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 313 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 314 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 315 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 318 | angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context, |
| 319 | GLenum buffer, |
| 320 | GLint drawbuffer, |
| 321 | const GLuint *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 322 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 323 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 324 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 327 | angle::Result FramebufferVk::clearBufferiv(const gl::Context *context, |
| 328 | GLenum buffer, |
| 329 | GLint drawbuffer, |
| 330 | const GLint *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 331 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 332 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 333 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 336 | angle::Result FramebufferVk::clearBufferfi(const gl::Context *context, |
| 337 | GLenum buffer, |
| 338 | GLint drawbuffer, |
| 339 | GLfloat depth, |
| 340 | GLint stencil) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 341 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 342 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 343 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 346 | GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 347 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 348 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 351 | GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 352 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 353 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 356 | angle::Result FramebufferVk::readPixels(const gl::Context *context, |
| 357 | const gl::Rectangle &area, |
| 358 | GLenum format, |
| 359 | GLenum type, |
| 360 | void *pixels) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 361 | { |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 362 | // Clip read area to framebuffer. |
| 363 | const gl::Extents &fbSize = getState().getReadAttachment()->getSize(); |
| 364 | const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 365 | ContextVk *contextVk = vk::GetImpl(context); |
| 366 | RendererVk *renderer = contextVk->getRenderer(); |
| 367 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 368 | gl::Rectangle clippedArea; |
| 369 | if (!ClipRectangle(area, fbRect, &clippedArea)) |
| 370 | { |
| 371 | // nothing to read |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 372 | return angle::Result::Continue; |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 373 | } |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 374 | gl::Rectangle flippedArea = clippedArea; |
Geoff Lang | e076a23 | 2018-07-16 15:34:05 -0400 | [diff] [blame] | 375 | if (contextVk->isViewportFlipEnabledForReadFBO()) |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 376 | { |
| 377 | flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height; |
| 378 | } |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 379 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 380 | const gl::State &glState = context->getState(); |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 381 | const gl::PixelPackState &packState = glState.getPackState(); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 382 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 383 | const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type); |
| 384 | |
| 385 | GLuint outputPitch = 0; |
Jamie Madill | abfbc0f | 2018-10-09 12:48:52 -0400 | [diff] [blame] | 386 | ANGLE_VK_CHECK_MATH(contextVk, |
| 387 | sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment, |
| 388 | packState.rowLength, &outputPitch)); |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 389 | GLuint outputSkipBytes = 0; |
Jamie Madill | abfbc0f | 2018-10-09 12:48:52 -0400 | [diff] [blame] | 390 | ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState, |
| 391 | false, &outputSkipBytes)); |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 392 | |
| 393 | outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes + |
| 394 | (clippedArea.y - area.y) * outputPitch; |
Luc Ferron | 6028422 | 2018-03-20 16:01:44 -0400 | [diff] [blame] | 395 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 396 | const angle::Format &angleFormat = GetFormatFromFormatType(format, type); |
| 397 | |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 398 | PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder, |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 399 | glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0); |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 400 | if (contextVk->isViewportFlipEnabledForReadFBO()) |
| 401 | { |
| 402 | params.reverseRowOrder = !params.reverseRowOrder; |
| 403 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 404 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 405 | ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 406 | getColorReadRenderTarget(), |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 407 | static_cast<uint8_t *>(pixels) + outputSkipBytes)); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 408 | mReadPixelBuffer.releaseRetainedBuffers(renderer); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 409 | return angle::Result::Continue; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 410 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 411 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 412 | RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const |
| 413 | { |
| 414 | return mRenderTargetCache.getDepthStencil(); |
| 415 | } |
| 416 | |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 417 | angle::Result FramebufferVk::blitWithCopy(ContextVk *contextVk, |
| 418 | const gl::Rectangle ©Area, |
| 419 | RenderTargetVk *readRenderTarget, |
| 420 | RenderTargetVk *drawRenderTarget, |
| 421 | bool blitDepthBuffer, |
| 422 | bool blitStencilBuffer) |
Luc Ferron | be30c4f | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 423 | { |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 424 | vk::ImageHelper *writeImage = drawRenderTarget->getImageForWrite(&mFramebuffer); |
| 425 | |
| 426 | vk::CommandBuffer *commandBuffer; |
| 427 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
| 428 | |
Luc Ferron | be30c4f | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 429 | vk::ImageHelper *readImage = readRenderTarget->getImageForRead( |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 430 | &mFramebuffer, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, commandBuffer); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 431 | |
| 432 | VkImageAspectFlags aspectMask = |
| 433 | vk::GetDepthStencilAspectFlagsForCopy(blitDepthBuffer, blitStencilBuffer); |
Luc Ferron | be30c4f | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 434 | vk::ImageHelper::Copy(readImage, writeImage, gl::Offset(), gl::Offset(), |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 435 | gl::Extents(copyArea.width, copyArea.height, 1), aspectMask, |
| 436 | commandBuffer); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 437 | return angle::Result::Continue; |
Luc Ferron | be30c4f | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 440 | RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 441 | { |
| 442 | RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 443 | ASSERT(renderTarget && renderTarget->getImage().valid()); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 444 | return renderTarget; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 447 | angle::Result FramebufferVk::blitWithReadback(ContextVk *contextVk, |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 448 | const gl::Rectangle ©Area, |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 449 | VkImageAspectFlagBits aspect, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 450 | RenderTargetVk *readRenderTarget, |
| 451 | RenderTargetVk *drawRenderTarget) |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 452 | { |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 453 | RendererVk *renderer = contextVk->getRenderer(); |
| 454 | const angle::Format &readFormat = readRenderTarget->getImageFormat().textureFormat(); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 455 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 456 | 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] | 457 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 458 | // This path is only currently used for y-flipping depth/stencil blits. |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 459 | PackPixelsParams packPixelsParams; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 460 | packPixelsParams.reverseRowOrder = true; |
| 461 | packPixelsParams.area.width = copyArea.width; |
| 462 | packPixelsParams.area.height = copyArea.height; |
| 463 | packPixelsParams.area.x = copyArea.x; |
| 464 | packPixelsParams.area.y = copyArea.y; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 465 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 466 | // Read back depth values into the destination buffer. |
| 467 | const angle::Format ©Format = GetDepthStencilImageToBufferFormat(readFormat, aspect); |
| 468 | packPixelsParams.destFormat = ©Format; |
| 469 | packPixelsParams.outputPitch = copyFormat.pixelBytes * copyArea.width; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 470 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 471 | // Allocate a space in the destination buffer to write to. |
| 472 | size_t blitAllocationSize = copyFormat.pixelBytes * copyArea.width * copyArea.height; |
| 473 | uint8_t *destPtr = nullptr; |
| 474 | VkBuffer destBufferHandle = VK_NULL_HANDLE; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 475 | VkDeviceSize destOffset = 0; |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 476 | ANGLE_TRY(mBlitPixelBuffer.allocate(contextVk, blitAllocationSize, &destPtr, &destBufferHandle, |
| 477 | &destOffset, nullptr)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 478 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 479 | ANGLE_TRY( |
| 480 | readPixelsImpl(contextVk, copyArea, packPixelsParams, aspect, readRenderTarget, destPtr)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 481 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 482 | VkBufferImageCopy copyRegion = {}; |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 483 | copyRegion.bufferOffset = destOffset; |
| 484 | copyRegion.bufferImageHeight = copyArea.height; |
| 485 | copyRegion.bufferRowLength = copyArea.width; |
| 486 | copyRegion.imageExtent.width = copyArea.width; |
| 487 | copyRegion.imageExtent.height = copyArea.height; |
| 488 | copyRegion.imageExtent.depth = 1; |
| 489 | copyRegion.imageSubresource.mipLevel = 0; |
| 490 | copyRegion.imageSubresource.aspectMask = aspect; |
| 491 | copyRegion.imageSubresource.baseArrayLayer = 0; |
| 492 | copyRegion.imageSubresource.layerCount = 1; |
| 493 | copyRegion.imageOffset.x = copyArea.x; |
| 494 | copyRegion.imageOffset.y = copyArea.y; |
| 495 | copyRegion.imageOffset.z = 0; |
| 496 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 497 | ANGLE_TRY(mBlitPixelBuffer.flush(contextVk)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 498 | |
| 499 | // Reinitialize the commandBuffer after a read pixels because it calls |
| 500 | // renderer->finish which makes command buffers obsolete. |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 501 | vk::CommandBuffer *commandBuffer; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 502 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 503 | |
| 504 | // We read the bytes of the image in a buffer, now we have to copy them into the |
| 505 | // destination target. |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 506 | vk::ImageHelper *imageForWrite = drawRenderTarget->getImageForWrite(&mFramebuffer); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 507 | |
| 508 | imageForWrite->changeLayoutWithStages( |
| 509 | imageForWrite->getAspectFlags(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 510 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer); |
| 511 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 512 | commandBuffer->copyBufferToImage(destBufferHandle, imageForWrite->getImage(), |
| 513 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Region); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 514 | |
| 515 | mBlitPixelBuffer.releaseRetainedBuffers(renderer); |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 516 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 517 | return angle::Result::Continue; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 520 | angle::Result FramebufferVk::blit(const gl::Context *context, |
| 521 | const gl::Rectangle &sourceArea, |
| 522 | const gl::Rectangle &destArea, |
| 523 | GLbitfield mask, |
| 524 | GLenum filter) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 525 | { |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 526 | ContextVk *contextVk = vk::GetImpl(context); |
| 527 | RendererVk *renderer = contextVk->getRenderer(); |
| 528 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 529 | const gl::State &glState = context->getState(); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 530 | const gl::Framebuffer *sourceFramebuffer = glState.getReadFramebuffer(); |
Jamie Madill | ba36593 | 2018-07-18 17:23:46 -0400 | [diff] [blame] | 531 | bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0; |
| 532 | bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0; |
| 533 | bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 534 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 535 | FramebufferVk *sourceFramebufferVk = vk::GetImpl(sourceFramebuffer); |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 536 | bool flipSource = contextVk->isViewportFlipEnabledForReadFBO(); |
| 537 | bool flipDest = contextVk->isViewportFlipEnabledForDrawFBO(); |
| 538 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 539 | gl::Rectangle readRect = sourceArea; |
| 540 | gl::Rectangle drawRect = destArea; |
| 541 | |
| 542 | if (glState.isScissorTestEnabled()) |
| 543 | { |
| 544 | const gl::Rectangle scissorRect = glState.getScissor(); |
| 545 | if (!ClipRectangle(sourceArea, scissorRect, &readRect)) |
| 546 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 547 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (!ClipRectangle(destArea, scissorRect, &drawRect)) |
| 551 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 552 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | // After cropping for the scissor, we also want to crop for the size of the buffers. |
| 557 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 558 | if (blitColorBuffer) |
| 559 | { |
| 560 | RenderTargetVk *readRenderTarget = sourceFramebufferVk->getColorReadRenderTarget(); |
| 561 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 562 | gl::Rectangle readRenderTargetRect; |
| 563 | if (!ClipToRenderTarget(readRect, readRenderTarget, &readRenderTargetRect)) |
| 564 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 565 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 566 | } |
| 567 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 568 | for (size_t colorAttachment : mState.getEnabledDrawBuffers()) |
| 569 | { |
| 570 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorAttachment]; |
| 571 | ASSERT(drawRenderTarget); |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 572 | ASSERT(HasSrcAndDstBlitProperties(renderer, readRenderTarget, drawRenderTarget)); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 573 | |
| 574 | gl::Rectangle drawRenderTargetRect; |
| 575 | if (!ClipToRenderTarget(drawRect, drawRenderTarget, &drawRenderTargetRect)) |
| 576 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 577 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 580 | ANGLE_TRY(blitWithCommand(contextVk, readRenderTargetRect, drawRenderTargetRect, |
| 581 | readRenderTarget, drawRenderTarget, filter, true, false, |
| 582 | false, flipSource, flipDest)); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | if (blitDepthBuffer || blitStencilBuffer) |
| 587 | { |
| 588 | RenderTargetVk *readRenderTarget = sourceFramebufferVk->getDepthStencilRenderTarget(); |
| 589 | ASSERT(readRenderTarget); |
| 590 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 591 | gl::Rectangle readRenderTargetRect; |
| 592 | if (!ClipToRenderTarget(readRect, readRenderTarget, &readRenderTargetRect)) |
| 593 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 594 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 595 | } |
| 596 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 597 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil(); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 598 | ASSERT(drawRenderTarget); |
| 599 | |
| 600 | gl::Rectangle drawRenderTargetRect; |
| 601 | if (!ClipToRenderTarget(drawRect, drawRenderTarget, &drawRenderTargetRect)) |
| 602 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 603 | return angle::Result::Continue; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | ASSERT(readRenderTargetRect == drawRenderTargetRect); |
| 607 | ASSERT(filter == GL_NEAREST); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 608 | |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 609 | if (HasSrcAndDstBlitProperties(renderer, readRenderTarget, drawRenderTarget)) |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 610 | { |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 611 | ANGLE_TRY(blitWithCommand(contextVk, readRenderTargetRect, drawRenderTargetRect, |
| 612 | readRenderTarget, drawRenderTarget, filter, false, |
| 613 | blitDepthBuffer, blitStencilBuffer, flipSource, flipDest)); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 614 | } |
| 615 | else |
| 616 | { |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 617 | if (flipSource || flipDest) |
| 618 | { |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 619 | if (blitDepthBuffer) |
| 620 | { |
| 621 | ANGLE_TRY(blitWithReadback(contextVk, readRenderTargetRect, |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 622 | VK_IMAGE_ASPECT_DEPTH_BIT, readRenderTarget, |
| 623 | drawRenderTarget)); |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 624 | } |
| 625 | if (blitStencilBuffer) |
| 626 | { |
| 627 | ANGLE_TRY(blitWithReadback(contextVk, readRenderTargetRect, |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 628 | VK_IMAGE_ASPECT_STENCIL_BIT, readRenderTarget, |
| 629 | drawRenderTarget)); |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 630 | } |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 631 | } |
| 632 | else |
| 633 | { |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 634 | ANGLE_TRY(blitWithCopy(contextVk, readRenderTargetRect, readRenderTarget, |
| 635 | drawRenderTarget, blitDepthBuffer, blitStencilBuffer)); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 636 | } |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 640 | return angle::Result::Continue; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 641 | } |
| 642 | |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 643 | angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk, |
| 644 | const gl::Rectangle &readRectIn, |
| 645 | const gl::Rectangle &drawRectIn, |
| 646 | RenderTargetVk *readRenderTarget, |
| 647 | RenderTargetVk *drawRenderTarget, |
| 648 | GLenum filter, |
| 649 | bool colorBlit, |
| 650 | bool depthBlit, |
| 651 | bool stencilBlit, |
| 652 | bool flipSource, |
| 653 | bool flipDest) |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 654 | { |
| 655 | // Since blitRenderbufferRect is called for each render buffer that needs to be blitted, |
| 656 | // it should never be the case that both color and depth/stencil need to be blitted at |
| 657 | // at the same time. |
| 658 | ASSERT(colorBlit != (depthBlit || stencilBlit)); |
| 659 | |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 660 | vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer); |
| 661 | |
| 662 | vk::CommandBuffer *commandBuffer; |
| 663 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
| 664 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 665 | const vk::Format &readImageFormat = readRenderTarget->getImageFormat(); |
| 666 | VkImageAspectFlags aspectMask = |
| 667 | colorBlit ? VK_IMAGE_ASPECT_COLOR_BIT |
| 668 | : vk::GetDepthStencilAspectFlags(readImageFormat.textureFormat()); |
| 669 | vk::ImageHelper *srcImage = readRenderTarget->getImageForRead( |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 670 | &mFramebuffer, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, commandBuffer); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 671 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 672 | const gl::Extents sourceFrameBufferExtents = readRenderTarget->getImageExtents(); |
| 673 | gl::Rectangle readRect = readRectIn; |
| 674 | |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 675 | if (flipSource) |
| 676 | { |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 677 | readRect.y = sourceFrameBufferExtents.height - readRect.y - readRect.height; |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 678 | } |
| 679 | |
Jamie Madill | ba36593 | 2018-07-18 17:23:46 -0400 | [diff] [blame] | 680 | VkImageBlit blit = {}; |
| 681 | blit.srcOffsets[0] = {readRect.x0(), flipSource ? readRect.y1() : readRect.y0(), 0}; |
| 682 | blit.srcOffsets[1] = {readRect.x1(), flipSource ? readRect.y0() : readRect.y1(), 1}; |
| 683 | blit.srcSubresource.aspectMask = aspectMask; |
| 684 | blit.srcSubresource.mipLevel = 0; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 685 | blit.srcSubresource.baseArrayLayer = 0; |
| 686 | blit.srcSubresource.layerCount = 1; |
| 687 | blit.dstSubresource.aspectMask = aspectMask; |
| 688 | blit.dstSubresource.mipLevel = 0; |
| 689 | blit.dstSubresource.baseArrayLayer = 0; |
| 690 | blit.dstSubresource.layerCount = 1; |
| 691 | |
| 692 | const gl::Extents &drawFrameBufferExtents = drawRenderTarget->getImageExtents(); |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 693 | gl::Rectangle drawRect = drawRectIn; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 694 | |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 695 | if (flipDest) |
| 696 | { |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 697 | drawRect.y = drawFrameBufferExtents.height - drawRect.y - drawRect.height; |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 698 | } |
| 699 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 700 | blit.dstOffsets[0] = {drawRect.x0(), flipDest ? drawRect.y1() : drawRect.y0(), 0}; |
| 701 | blit.dstOffsets[1] = {drawRect.x1(), flipDest ? drawRect.y0() : drawRect.y1(), 1}; |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 702 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 703 | // Requirement of the copyImageToBuffer, the dst image must be in |
| 704 | // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout. |
| 705 | dstImage->changeLayoutWithStages(aspectMask, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 706 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 707 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer); |
| 708 | |
| 709 | commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 710 | dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, |
| 711 | gl_vk::GetFilter(filter)); |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 712 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 713 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 714 | } |
| 715 | |
Kenneth Russell | ce8602a | 2017-10-03 18:23:08 -0700 | [diff] [blame] | 716 | bool FramebufferVk::checkStatus(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 717 | { |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 718 | // if we have both a depth and stencil buffer, they must refer to the same object |
| 719 | // since we only support packed_depth_stencil and not separate depth and stencil |
| 720 | if (mState.hasSeparateDepthAndStencilAttachments()) |
| 721 | { |
| 722 | return false; |
| 723 | } |
| 724 | |
Jamie Madill | b79e7bb | 2017-10-24 13:55:50 -0400 | [diff] [blame] | 725 | return true; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 726 | } |
| 727 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 728 | angle::Result FramebufferVk::syncState(const gl::Context *context, |
| 729 | const gl::Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 730 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 731 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 7bd1666 | 2017-10-28 19:40:50 -0400 | [diff] [blame] | 732 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 733 | |
| 734 | ASSERT(dirtyBits.any()); |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 735 | for (size_t dirtyBit : dirtyBits) |
| 736 | { |
| 737 | switch (dirtyBit) |
| 738 | { |
| 739 | case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
| 740 | case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
| 741 | ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState)); |
| 742 | break; |
| 743 | case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
| 744 | case gl::Framebuffer::DIRTY_BIT_READ_BUFFER: |
| 745 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH: |
| 746 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT: |
| 747 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES: |
| 748 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS: |
| 749 | break; |
| 750 | default: |
| 751 | { |
| 752 | ASSERT(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 && |
| 753 | dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX); |
| 754 | size_t colorIndex = |
| 755 | static_cast<size_t>(dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 756 | ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndex)); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 757 | |
| 758 | // Update cached masks for masked clears. |
| 759 | RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndex]; |
| 760 | if (renderTarget) |
| 761 | { |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 762 | const angle::Format &emulatedFormat = |
| 763 | renderTarget->getImageFormat().textureFormat(); |
| 764 | updateActiveColorMasks( |
| 765 | colorIndex, emulatedFormat.redBits > 0, emulatedFormat.greenBits > 0, |
| 766 | emulatedFormat.blueBits > 0, emulatedFormat.alphaBits > 0); |
| 767 | |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 768 | const angle::Format &sourceFormat = |
| 769 | renderTarget->getImageFormat().angleFormat(); |
| 770 | mEmulatedAlphaAttachmentMask.set( |
| 771 | colorIndex, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0); |
Luc Ferron | 0bb940a | 2018-06-22 09:59:34 -0400 | [diff] [blame] | 772 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 773 | contextVk->updateColorMask(context->getState().getBlendState()); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 774 | } |
| 775 | else |
| 776 | { |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 777 | updateActiveColorMasks(colorIndex, false, false, false, false); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 778 | } |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 779 | break; |
| 780 | } |
| 781 | } |
| 782 | } |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 783 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 784 | mActiveColorComponents = gl_vk::GetColorComponentFlags( |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 785 | mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(), |
| 786 | mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any()); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 787 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 788 | mFramebuffer.release(renderer); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 789 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 790 | // 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] | 791 | // create a new entry in the command graph. |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 792 | mFramebuffer.finishCurrentCommands(renderer); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 793 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame^] | 794 | // Notify the ContextVk to update the pipeline desc. |
| 795 | updateRenderPassDesc(); |
| 796 | contextVk->onFramebufferChange(mRenderPassDesc); |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 797 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 798 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 799 | } |
| 800 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame^] | 801 | void FramebufferVk::updateRenderPassDesc() |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 802 | { |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame^] | 803 | mRenderPassDesc = {}; |
| 804 | mRenderPassDesc.setSamples(getSamples()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 805 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 806 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 807 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 808 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 809 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 810 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex]; |
| 811 | ASSERT(colorRenderTarget); |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame^] | 812 | mRenderPassDesc.packAttachment(colorRenderTarget->getImage().getFormat()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 813 | } |
| 814 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 815 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 816 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 817 | { |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame^] | 818 | mRenderPassDesc.packAttachment(depthStencilRenderTarget->getImage().getFormat()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 819 | } |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 820 | } |
| 821 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 822 | angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 823 | { |
| 824 | // If we've already created our cached Framebuffer, return it. |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 825 | if (mFramebuffer.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 826 | { |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 827 | *framebufferOut = &mFramebuffer.getFramebuffer(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 828 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | vk::RenderPass *renderPass = nullptr; |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame^] | 832 | ANGLE_TRY( |
| 833 | contextVk->getRenderer()->getCompatibleRenderPass(contextVk, mRenderPassDesc, &renderPass)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 834 | |
| 835 | // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it. |
| 836 | if (mBackbuffer) |
| 837 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 838 | return mBackbuffer->getCurrentFramebuffer(contextVk, *renderPass, framebufferOut); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // Gather VkImageViews over all FBO attachments, also size of attached region. |
| 842 | std::vector<VkImageView> attachments; |
| 843 | gl::Extents attachmentsSize; |
| 844 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 845 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 846 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 847 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 848 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 849 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex]; |
| 850 | ASSERT(colorRenderTarget); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 851 | attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 852 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 853 | ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getImageExtents()); |
| 854 | attachmentsSize = colorRenderTarget->getImageExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 855 | } |
| 856 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 857 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 858 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 859 | { |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 860 | attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 861 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 862 | ASSERT(attachmentsSize.empty() || |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 863 | attachmentsSize == depthStencilRenderTarget->getImageExtents()); |
| 864 | attachmentsSize = depthStencilRenderTarget->getImageExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | ASSERT(!attachments.empty()); |
| 868 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 869 | VkFramebufferCreateInfo framebufferInfo = {}; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 870 | |
| 871 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 872 | framebufferInfo.flags = 0; |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 873 | framebufferInfo.renderPass = renderPass->getHandle(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 874 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); |
| 875 | framebufferInfo.pAttachments = attachments.data(); |
| 876 | framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width); |
| 877 | framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height); |
| 878 | framebufferInfo.layers = 1; |
| 879 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 880 | ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo)); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 881 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 882 | *framebufferOut = &mFramebuffer.getFramebuffer(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 883 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 884 | } |
| 885 | |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 886 | angle::Result FramebufferVk::clearWithClearAttachments( |
| 887 | ContextVk *contextVk, |
| 888 | bool clearColor, |
| 889 | bool clearDepth, |
| 890 | bool clearStencil, |
| 891 | const VkClearDepthStencilValue &clearDepthStencilValue) |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 892 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 893 | // Trigger a new command node to ensure overlapping writes happen sequentially. |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 894 | mFramebuffer.finishCurrentCommands(contextVk->getRenderer()); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 895 | |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 896 | // This command can only happen inside a render pass, so obtain one if its already happening |
| 897 | // or create a new one if not. |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 898 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 899 | vk::RecordingMode mode = vk::RecordingMode::Start; |
| 900 | ANGLE_TRY(getCommandBufferForDraw(contextVk, &commandBuffer, &mode)); |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 901 | |
Jamie Madill | 3f3b358 | 2018-09-14 10:38:44 -0400 | [diff] [blame] | 902 | // 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] | 903 | VkClearRect clearRect = {}; |
Luc Ferron | e4c38be | 2018-04-17 11:09:16 -0400 | [diff] [blame] | 904 | clearRect.baseArrayLayer = 0; |
| 905 | clearRect.layerCount = 1; |
| 906 | |
| 907 | // When clearing, the scissor region must be clipped to the renderArea per the validation rules |
| 908 | // in Vulkan. |
| 909 | gl::Rectangle intersection; |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 910 | if (!gl::ClipRectangle(contextVk->getState().getScissor(), |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 911 | mFramebuffer.getRenderPassRenderArea(), &intersection)) |
Luc Ferron | e4c38be | 2018-04-17 11:09:16 -0400 | [diff] [blame] | 912 | { |
| 913 | // 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] | 914 | return angle::Result::Continue; |
Luc Ferron | e4c38be | 2018-04-17 11:09:16 -0400 | [diff] [blame] | 915 | } |
Luc Ferron | 1a135ad | 2018-07-04 10:35:31 -0400 | [diff] [blame] | 916 | |
Luc Ferron | e4c38be | 2018-04-17 11:09:16 -0400 | [diff] [blame] | 917 | clearRect.rect = gl_vk::GetRect(intersection); |
| 918 | |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 919 | if (contextVk->isViewportFlipEnabledForDrawFBO()) |
Luc Ferron | 1a135ad | 2018-07-04 10:35:31 -0400 | [diff] [blame] | 920 | { |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 921 | clearRect.rect.offset.y = mFramebuffer.getRenderPassRenderArea().height - |
| 922 | clearRect.rect.offset.y - clearRect.rect.extent.height; |
Luc Ferron | 1a135ad | 2018-07-04 10:35:31 -0400 | [diff] [blame] | 923 | } |
| 924 | |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 925 | gl::AttachmentArray<VkClearAttachment> clearAttachments; |
| 926 | int clearAttachmentIndex = 0; |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 927 | |
| 928 | if (clearColor) |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 929 | { |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 930 | RenderTargetVk *renderTarget = getColorReadRenderTarget(); |
| 931 | const vk::Format &format = renderTarget->getImageFormat(); |
| 932 | VkClearValue modifiedClear = contextVk->getClearColorValue(); |
| 933 | |
| 934 | // We need to make sure we are not clearing the alpha channel if we are using a buffer |
| 935 | // format that doesn't have an alpha channel. |
| 936 | if (format.angleFormat().alphaBits == 0) |
| 937 | { |
| 938 | modifiedClear.color.float32[3] = 1.0; |
| 939 | } |
| 940 | |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 941 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 942 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
| 943 | { |
| 944 | VkClearAttachment &clearAttachment = clearAttachments[clearAttachmentIndex]; |
| 945 | clearAttachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 946 | clearAttachment.colorAttachment = static_cast<uint32_t>(colorIndex); |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 947 | clearAttachment.clearValue = modifiedClear; |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 948 | ++clearAttachmentIndex; |
| 949 | } |
| 950 | } |
| 951 | |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 952 | VkClearValue depthStencilClearValue = {}; |
| 953 | depthStencilClearValue.depthStencil = clearDepthStencilValue; |
Shahbaz Youssefi | 2197dc5 | 2018-10-30 11:29:58 -0400 | [diff] [blame] | 954 | |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 955 | if (clearDepth && clearStencil && mState.getDepthStencilAttachment() != nullptr) |
| 956 | { |
| 957 | // When we have a packed depth/stencil attachment we can do 1 clear for both when it |
| 958 | // applies. |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 959 | VkClearAttachment &clearAttachment = clearAttachments[clearAttachmentIndex]; |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 960 | clearAttachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 961 | clearAttachment.colorAttachment = VK_ATTACHMENT_UNUSED; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 962 | clearAttachment.clearValue = depthStencilClearValue; |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 963 | ++clearAttachmentIndex; |
| 964 | } |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 965 | else |
| 966 | { |
| 967 | if (clearDepth) |
| 968 | { |
| 969 | VkClearAttachment &clearAttachment = clearAttachments[clearAttachmentIndex]; |
| 970 | clearAttachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 971 | clearAttachment.colorAttachment = VK_ATTACHMENT_UNUSED; |
Shahbaz Youssefi | 2197dc5 | 2018-10-30 11:29:58 -0400 | [diff] [blame] | 972 | clearAttachment.clearValue = depthStencilClearValue; |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 973 | ++clearAttachmentIndex; |
| 974 | } |
| 975 | |
| 976 | if (clearStencil) |
| 977 | { |
| 978 | VkClearAttachment &clearAttachment = clearAttachments[clearAttachmentIndex]; |
| 979 | clearAttachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 980 | clearAttachment.colorAttachment = VK_ATTACHMENT_UNUSED; |
Shahbaz Youssefi | 2197dc5 | 2018-10-30 11:29:58 -0400 | [diff] [blame] | 981 | clearAttachment.clearValue = depthStencilClearValue; |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 982 | ++clearAttachmentIndex; |
| 983 | } |
| 984 | } |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 985 | |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 986 | commandBuffer->clearAttachments(static_cast<uint32_t>(clearAttachmentIndex), |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 987 | clearAttachments.data(), 1, &clearRect); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 988 | return angle::Result::Continue; |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 989 | } |
| 990 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 991 | angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk, |
| 992 | VkColorComponentFlags colorMaskFlags) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 993 | { |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 994 | RendererVk *renderer = contextVk->getRenderer(); |
| 995 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 996 | // If the format of the framebuffer does not have an alpha channel, we need to make sure we do |
Luc Ferron | 66c2f4a | 2018-06-19 10:27:57 -0400 | [diff] [blame] | 997 | // not affect the alpha channel of the type we're using to emulate the format. |
| 998 | // TODO(jmadill): Implement EXT_draw_buffers http://anglebug.com/2394 |
| 999 | RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[0]; |
| 1000 | ASSERT(renderTarget); |
| 1001 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1002 | UtilsVk::ClearImageParameters params = {}; |
| 1003 | params.colorMaskFlags = colorMaskFlags; |
| 1004 | params.alphaMask = &getEmulatedAlphaAttachmentMask(); |
| 1005 | params.renderPassDesc = &getRenderPassDesc(); |
| 1006 | |
| 1007 | const vk::Format &imageFormat = renderTarget->getImageFormat(); |
| 1008 | params.clearValue = contextVk->getClearColorValue().color; |
Luc Ferron | 66c2f4a | 2018-06-19 10:27:57 -0400 | [diff] [blame] | 1009 | bool overrideAlphaWithOne = |
| 1010 | imageFormat.textureFormat().alphaBits > 0 && imageFormat.angleFormat().alphaBits == 0; |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1011 | params.clearValue.float32[3] = overrideAlphaWithOne ? 1.0f : params.clearValue.float32[3]; |
Luc Ferron | 66c2f4a | 2018-06-19 10:27:57 -0400 | [diff] [blame] | 1012 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1013 | params.renderAreaHeight = mState.getDimensions().height; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1014 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1015 | return renderer->getUtils().clearImage(contextVk, this, params); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 1018 | angle::Result FramebufferVk::getSamplePosition(const gl::Context *context, |
| 1019 | size_t index, |
| 1020 | GLfloat *xy) const |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1021 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 1022 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1023 | return angle::Result::Stop; |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1026 | angle::Result FramebufferVk::getCommandBufferForDraw(ContextVk *contextVk, |
| 1027 | vk::CommandBuffer **commandBufferOut, |
| 1028 | vk::RecordingMode *modeOut) |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1029 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1030 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | bef918c | 2017-12-13 13:11:30 -0500 | [diff] [blame] | 1031 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 1032 | // This will clear the current write operation if it is complete. |
Jamie Madill | 5dca651 | 2018-05-30 10:53:51 -0400 | [diff] [blame] | 1033 | if (appendToStartedRenderPass(renderer, commandBufferOut)) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1034 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 1035 | *modeOut = vk::RecordingMode::Append; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1036 | return angle::Result::Continue; |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 1037 | } |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1038 | |
Jamie Madill | d1249de | 2018-08-28 16:58:53 -0400 | [diff] [blame] | 1039 | return startNewRenderPass(contextVk, commandBufferOut); |
| 1040 | } |
| 1041 | |
| 1042 | angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk, |
| 1043 | vk::CommandBuffer **commandBufferOut) |
| 1044 | { |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1045 | vk::Framebuffer *framebuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1046 | ANGLE_TRY(getFramebuffer(contextVk, &framebuffer)); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1047 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 1048 | // TODO(jmadill): Proper clear value implementation. http://anglebug.com/2361 |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1049 | std::vector<VkClearValue> attachmentClearValues; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1050 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 1051 | vk::CommandBuffer *writeCommands = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1052 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands)); |
Jamie Madill | e4c5a23 | 2018-03-02 21:00:31 -0500 | [diff] [blame] | 1053 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1054 | vk::RenderPassDesc renderPassDesc; |
| 1055 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1056 | // Initialize RenderPass info. |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1057 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 1058 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 1059 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1060 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1061 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex]; |
| 1062 | ASSERT(colorRenderTarget); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1063 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1064 | colorRenderTarget->onColorDraw(&mFramebuffer, writeCommands, &renderPassDesc); |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1065 | attachmentClearValues.emplace_back(contextVk->getClearColorValue()); |
| 1066 | } |
| 1067 | |
| 1068 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1069 | if (depthStencilRenderTarget) |
| 1070 | { |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1071 | depthStencilRenderTarget->onDepthStencilDraw(&mFramebuffer, writeCommands, &renderPassDesc); |
Jamie Madill | f4d693c | 2018-02-14 16:38:16 -0500 | [diff] [blame] | 1072 | attachmentClearValues.emplace_back(contextVk->getClearDepthStencilValue()); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1073 | } |
| 1074 | |
Luc Ferron | d17bdfe | 2018-04-05 13:50:10 -0400 | [diff] [blame] | 1075 | gl::Rectangle renderArea = |
| 1076 | gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1077 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame^] | 1078 | return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc, |
| 1079 | attachmentClearValues, commandBufferOut); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1080 | } |
| 1081 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1082 | void FramebufferVk::updateActiveColorMasks(size_t colorIndex, bool r, bool g, bool b, bool a) |
| 1083 | { |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1084 | mActiveColorComponentMasksForClear[0].set(colorIndex, r); |
| 1085 | mActiveColorComponentMasksForClear[1].set(colorIndex, g); |
| 1086 | mActiveColorComponentMasksForClear[2].set(colorIndex, b); |
| 1087 | mActiveColorComponentMasksForClear[3].set(colorIndex, a); |
| 1088 | } |
| 1089 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1090 | const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1091 | { |
| 1092 | return mEmulatedAlphaAttachmentMask; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1093 | } |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1094 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1095 | angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk, |
| 1096 | const gl::Rectangle &area, |
| 1097 | const PackPixelsParams &packPixelsParams, |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1098 | VkImageAspectFlagBits copyAspectFlags, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1099 | RenderTargetVk *renderTarget, |
| 1100 | void *pixels) |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1101 | { |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1102 | RendererVk *renderer = contextVk->getRenderer(); |
| 1103 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1104 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1105 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1106 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1107 | // 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] | 1108 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1109 | vk::ImageHelper *srcImage = renderTarget->getImageForRead( |
| 1110 | &mFramebuffer, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, commandBuffer); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1111 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1112 | const angle::Format *readFormat = &srcImage->getFormat().textureFormat(); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1113 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1114 | if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT) |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1115 | { |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1116 | readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1117 | } |
| 1118 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1119 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 1120 | uint8_t *readPixelBuffer = nullptr; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 1121 | VkDeviceSize stagingOffset = 0; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1122 | size_t allocationSize = readFormat->pixelBytes * area.width * area.height; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1123 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 1124 | ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle, |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 1125 | &stagingOffset, nullptr)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1126 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1127 | VkBufferImageCopy region = {}; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1128 | region.bufferImageHeight = area.height; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 1129 | region.bufferOffset = stagingOffset; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1130 | region.bufferRowLength = area.width; |
| 1131 | region.imageExtent.width = area.width; |
| 1132 | region.imageExtent.height = area.height; |
| 1133 | region.imageExtent.depth = 1; |
| 1134 | region.imageOffset.x = area.x; |
| 1135 | region.imageOffset.y = area.y; |
| 1136 | region.imageOffset.z = 0; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1137 | region.imageSubresource.aspectMask = copyAspectFlags; |
Jamie Madill | 3f3b358 | 2018-09-14 10:38:44 -0400 | [diff] [blame] | 1138 | region.imageSubresource.baseArrayLayer = renderTarget->getLayerIndex(); |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1139 | region.imageSubresource.layerCount = 1; |
| 1140 | region.imageSubresource.mipLevel = 0; |
| 1141 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1142 | commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(), |
| 1143 | bufferHandle, 1, ®ion); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1144 | |
| 1145 | // Triggers a full finish. |
| 1146 | // TODO(jmadill): Don't block on asynchronous readback. |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1147 | ANGLE_TRY(renderer->finish(contextVk)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1148 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1149 | // The buffer we copied to needs to be invalidated before we read from it because its not been |
| 1150 | // created with the host coherent bit. |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 1151 | ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk)); |
Yuly Novikov | 6c6c76c | 2018-05-17 18:45:06 +0000 | [diff] [blame] | 1152 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1153 | PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, readPixelBuffer, |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 1154 | static_cast<uint8_t *>(pixels)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1155 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1156 | return angle::Result::Continue; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1157 | } |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1158 | |
| 1159 | const gl::Extents &FramebufferVk::getReadImageExtents() const |
| 1160 | { |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1161 | return getColorReadRenderTarget()->getImageExtents(); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1162 | } |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 1163 | |
| 1164 | RenderTargetVk *FramebufferVk::getFirstRenderTarget() const |
| 1165 | { |
| 1166 | for (auto *renderTarget : mRenderTargetCache.getColors()) |
| 1167 | { |
| 1168 | if (renderTarget) |
| 1169 | { |
| 1170 | return renderTarget; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return mRenderTargetCache.getDepthStencil(); |
| 1175 | } |
| 1176 | |
| 1177 | GLint FramebufferVk::getSamples() const |
| 1178 | { |
| 1179 | RenderTargetVk *firstRT = getFirstRenderTarget(); |
| 1180 | return firstRT ? firstRT->getImage().getSamples() : 0; |
| 1181 | } |
| 1182 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1183 | } // namespace rx |