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 | 3ea463b | 2019-06-19 14:21:33 -0400 | [diff] [blame] | 27 | #include "libANGLE/trace.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; |
Mohan Maiya | 8a43b8c | 2019-08-02 08:47:13 -0700 | [diff] [blame] | 39 | |
| 40 | // Alignment value to accommodate the largest known, for now, uncompressed Vulkan format |
Shahbaz Youssefi | c972435 | 2019-08-20 14:55:43 -0400 | [diff] [blame] | 41 | // VK_FORMAT_R64G64B64A64_SFLOAT, while supporting 3-component types such as |
| 42 | // VK_FORMAT_R16G16B16_SFLOAT. |
| 43 | constexpr size_t kReadPixelsBufferAlignment = 32 * 3; |
Mohan Maiya | 8a43b8c | 2019-08-02 08:47:13 -0700 | [diff] [blame] | 44 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 45 | // Clear values are only used when loadOp=Clear is set in clearWithRenderPassOp. When starting a |
| 46 | // new render pass, the clear value is set to an unlikely value (bright pink) to stand out better |
| 47 | // in case of a bug. |
| 48 | constexpr VkClearValue kUninitializedClearValue = {{{0.95, 0.05, 0.95, 0.95}}}; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 49 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 50 | const gl::InternalFormat &GetReadAttachmentInfo(const gl::Context *context, |
| 51 | RenderTargetVk *renderTarget) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 52 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 53 | GLenum implFormat = |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 54 | renderTarget->getImageFormat().imageFormat().fboImplementationInternalFormat; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 55 | return gl::GetSizedInternalFormatInfo(implFormat); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 56 | } |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 57 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 58 | bool HasSrcBlitFeature(RendererVk *renderer, RenderTargetVk *srcRenderTarget) |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 59 | { |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 60 | const VkFormat srcFormat = srcRenderTarget->getImageFormat().vkImageFormat; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 61 | return renderer->hasImageFormatFeatureBits(srcFormat, VK_FORMAT_FEATURE_BLIT_SRC_BIT); |
| 62 | } |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 63 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 64 | bool HasDstBlitFeature(RendererVk *renderer, RenderTargetVk *dstRenderTarget) |
| 65 | { |
| 66 | const VkFormat dstFormat = dstRenderTarget->getImageFormat().vkImageFormat; |
| 67 | return renderer->hasImageFormatFeatureBits(dstFormat, VK_FORMAT_FEATURE_BLIT_DST_BIT); |
| 68 | } |
| 69 | |
| 70 | // Returns false if destination has any channel the source doesn't. This means that channel was |
| 71 | // emulated and using the Vulkan blit command would overwrite that emulated channel. |
| 72 | bool areSrcAndDstColorChannelsBlitCompatible(RenderTargetVk *srcRenderTarget, |
| 73 | RenderTargetVk *dstRenderTarget) |
| 74 | { |
| 75 | const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat(); |
| 76 | const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat(); |
| 77 | |
| 78 | // Luminance/alpha formats are not renderable, so they can't have ended up in a framebuffer to |
| 79 | // participate in a blit. |
| 80 | ASSERT(!dstFormat.isLUMA() && !srcFormat.isLUMA()); |
| 81 | |
| 82 | // All color formats have the red channel. |
| 83 | ASSERT(dstFormat.redBits > 0 && srcFormat.redBits > 0); |
| 84 | |
| 85 | return (dstFormat.greenBits > 0 || srcFormat.greenBits == 0) && |
| 86 | (dstFormat.blueBits > 0 || srcFormat.blueBits == 0) && |
| 87 | (dstFormat.alphaBits > 0 || srcFormat.alphaBits == 0); |
| 88 | } |
| 89 | |
| 90 | bool areSrcAndDstDepthStencilChannelsBlitCompatible(RenderTargetVk *srcRenderTarget, |
| 91 | RenderTargetVk *dstRenderTarget) |
| 92 | { |
| 93 | const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat(); |
| 94 | const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat(); |
| 95 | |
| 96 | return (dstFormat.depthBits > 0 || srcFormat.depthBits == 0) && |
| 97 | (dstFormat.stencilBits > 0 || srcFormat.stencilBits == 0); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 98 | } |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 99 | |
| 100 | // Special rules apply to VkBufferImageCopy with depth/stencil. The components are tightly packed |
| 101 | // into a depth or stencil section of the destination buffer. See the spec: |
| 102 | // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkBufferImageCopy.html |
| 103 | const angle::Format &GetDepthStencilImageToBufferFormat(const angle::Format &imageFormat, |
| 104 | VkImageAspectFlagBits copyAspect) |
| 105 | { |
| 106 | if (copyAspect == VK_IMAGE_ASPECT_STENCIL_BIT) |
| 107 | { |
| 108 | ASSERT(imageFormat.id == angle::FormatID::D24_UNORM_S8_UINT || |
| 109 | imageFormat.id == angle::FormatID::D32_FLOAT_S8X24_UINT || |
| 110 | imageFormat.id == angle::FormatID::S8_UINT); |
| 111 | return angle::Format::Get(angle::FormatID::S8_UINT); |
| 112 | } |
| 113 | |
| 114 | ASSERT(copyAspect == VK_IMAGE_ASPECT_DEPTH_BIT); |
| 115 | |
| 116 | switch (imageFormat.id) |
| 117 | { |
| 118 | case angle::FormatID::D16_UNORM: |
| 119 | return imageFormat; |
| 120 | case angle::FormatID::D24_UNORM_X8_UINT: |
| 121 | return imageFormat; |
| 122 | case angle::FormatID::D24_UNORM_S8_UINT: |
| 123 | return angle::Format::Get(angle::FormatID::D24_UNORM_X8_UINT); |
| 124 | case angle::FormatID::D32_FLOAT: |
| 125 | return imageFormat; |
| 126 | case angle::FormatID::D32_FLOAT_S8X24_UINT: |
| 127 | return angle::Format::Get(angle::FormatID::D32_FLOAT); |
| 128 | default: |
| 129 | UNREACHABLE(); |
| 130 | return imageFormat; |
| 131 | } |
| 132 | } |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 133 | |
| 134 | void SetEmulatedAlphaValue(const vk::Format &format, VkClearColorValue *value) |
| 135 | { |
| 136 | if (format.vkFormatIsInt) |
| 137 | { |
| 138 | if (format.vkFormatIsUnsigned) |
| 139 | { |
| 140 | value->uint32[3] = kEmulatedAlphaValue; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | value->int32[3] = kEmulatedAlphaValue; |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | value->float32[3] = kEmulatedAlphaValue; |
| 150 | } |
| 151 | } |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 152 | } // anonymous namespace |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 153 | |
| 154 | // static |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 155 | FramebufferVk *FramebufferVk::CreateUserFBO(RendererVk *renderer, const gl::FramebufferState &state) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 156 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 157 | return new FramebufferVk(renderer, state, nullptr); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // static |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 161 | FramebufferVk *FramebufferVk::CreateDefaultFBO(RendererVk *renderer, |
| 162 | const gl::FramebufferState &state, |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 163 | WindowSurfaceVk *backbuffer) |
| 164 | { |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 165 | return new FramebufferVk(renderer, state, backbuffer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Jamie Madill | 639bc90 | 2018-07-18 17:08:27 -0400 | [diff] [blame] | 168 | FramebufferVk::FramebufferVk(RendererVk *renderer, |
| 169 | const gl::FramebufferState &state, |
| 170 | WindowSurfaceVk *backbuffer) |
Jamie Madill | 7f2520f | 2019-06-26 11:18:33 -0400 | [diff] [blame] | 171 | : FramebufferImpl(state), mBackbuffer(backbuffer), mActiveColorComponents(0) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 172 | { |
Mohan Maiya | 8a43b8c | 2019-08-02 08:47:13 -0700 | [diff] [blame] | 173 | mReadPixelBuffer.init(renderer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, kReadPixelsBufferAlignment, |
| 174 | kMinReadPixelsBufferSize, true); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 177 | FramebufferVk::~FramebufferVk() = default; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 178 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 179 | void FramebufferVk::destroy(const gl::Context *context) |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 180 | { |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 181 | ContextVk *contextVk = vk::GetImpl(context); |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 182 | mFramebuffer.release(contextVk); |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 183 | |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 184 | mReadPixelBuffer.release(contextVk); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 187 | angle::Result FramebufferVk::discard(const gl::Context *context, |
| 188 | size_t count, |
| 189 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 190 | { |
Shahbaz Youssefi | 97123e3 | 2019-07-08 15:11:16 -0400 | [diff] [blame] | 191 | return invalidate(context, count, attachments); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 194 | angle::Result FramebufferVk::invalidate(const gl::Context *context, |
| 195 | size_t count, |
| 196 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 197 | { |
Shahbaz Youssefi | da90448 | 2019-07-02 10:49:14 -0400 | [diff] [blame] | 198 | mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial()); |
| 199 | |
| 200 | if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass()) |
| 201 | { |
| 202 | invalidateImpl(vk::GetImpl(context), count, attachments); |
| 203 | } |
| 204 | |
| 205 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 208 | angle::Result FramebufferVk::invalidateSub(const gl::Context *context, |
| 209 | size_t count, |
| 210 | const GLenum *attachments, |
| 211 | const gl::Rectangle &area) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 212 | { |
Shahbaz Youssefi | da90448 | 2019-07-02 10:49:14 -0400 | [diff] [blame] | 213 | mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial()); |
| 214 | |
| 215 | // RenderPass' storeOp cannot be made conditional to a specific region, so we only apply this |
| 216 | // hint if the requested area encompasses the render area. |
| 217 | if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass() && |
| 218 | area.encloses(mFramebuffer.getRenderPassRenderArea())) |
| 219 | { |
| 220 | invalidateImpl(vk::GetImpl(context), count, attachments); |
| 221 | } |
| 222 | |
| 223 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 226 | angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 227 | { |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 228 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 229 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 230 | bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT)); |
| 231 | bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT)); |
| 232 | bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT)); |
| 233 | gl::DrawBufferMask clearColorBuffers; |
| 234 | if (clearColor) |
| 235 | { |
| 236 | clearColorBuffers = mState.getEnabledDrawBuffers(); |
| 237 | } |
| 238 | |
| 239 | const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color; |
| 240 | const VkClearDepthStencilValue &clearDepthStencilValue = |
| 241 | contextVk->getClearDepthStencilValue().depthStencil; |
| 242 | |
| 243 | return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue, |
| 244 | clearDepthStencilValue); |
| 245 | } |
| 246 | |
| 247 | angle::Result FramebufferVk::clearImpl(const gl::Context *context, |
| 248 | gl::DrawBufferMask clearColorBuffers, |
| 249 | bool clearDepth, |
| 250 | bool clearStencil, |
| 251 | const VkClearColorValue &clearColorValue, |
| 252 | const VkClearDepthStencilValue &clearDepthStencilValue) |
| 253 | { |
| 254 | ContextVk *contextVk = vk::GetImpl(context); |
| 255 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 256 | const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk); |
| 257 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 258 | // Discard clear altogether if scissor has 0 width or height. |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 259 | if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 260 | { |
| 261 | return angle::Result::Continue; |
| 262 | } |
| 263 | |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 264 | mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial()); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 265 | |
| 266 | // This function assumes that only enabled attachments are asked to be cleared. |
| 267 | ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers); |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 268 | |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 269 | // Adjust clear behavior based on whether the respective attachments are present; if asked to |
| 270 | // clear a non-existent attachment, don't attempt to clear it. |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 271 | |
| 272 | VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask(); |
| 273 | bool clearColor = clearColorBuffers.any(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 274 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 275 | const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 276 | clearDepth = clearDepth && depthAttachment; |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 277 | ASSERT(!clearDepth || depthAttachment->isAttached()); |
| 278 | |
| 279 | const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment(); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 280 | clearStencil = clearStencil && stencilAttachment; |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 281 | ASSERT(!clearStencil || stencilAttachment->isAttached()); |
| 282 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 283 | uint8_t stencilMask = |
| 284 | static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask); |
| 285 | |
| 286 | // The front-end should ensure we don't attempt to clear color if all channels are masked. |
| 287 | ASSERT(!clearColor || colorMaskFlags != 0); |
| 288 | // The front-end should ensure we don't attempt to clear depth if depth write is disabled. |
| 289 | ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask); |
| 290 | // The front-end should ensure we don't attempt to clear stencil if all bits are masked. |
| 291 | ASSERT(!clearStencil || stencilMask != 0); |
| 292 | |
| 293 | // If there is nothing to clear, return right away (for example, if asked to clear depth, but |
| 294 | // there is no depth attachment). |
Shahbaz Youssefi | 02a579e | 2019-03-27 14:21:20 -0400 | [diff] [blame] | 295 | if (!clearColor && !clearDepth && !clearStencil) |
| 296 | { |
| 297 | return angle::Result::Continue; |
| 298 | } |
| 299 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 300 | VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue; |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 301 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 302 | // We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If |
| 303 | // there's a depth mask, depth clearing is already disabled. |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 304 | bool maskedClearColor = |
| 305 | clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 306 | bool maskedClearStencil = stencilMask != 0xFF; |
| 307 | |
| 308 | bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor; |
| 309 | bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 310 | |
| 311 | // At least one of color, depth or stencil should be clearable with render pass loadOp for us |
| 312 | // to use this clear path. |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 313 | bool clearAnyWithRenderPassLoadOp = |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 314 | clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 315 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 316 | if (clearAnyWithRenderPassLoadOp) |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 317 | { |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 318 | // Clearing color is indicated by the set bits in this mask. If not clearing colors with |
| 319 | // render pass loadOp, the default value of all-zeros means the clear is not done in |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 320 | // clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass |
| 321 | // loadOp. |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 322 | gl::DrawBufferMask clearBuffersWithRenderPassLoadOp; |
| 323 | if (clearColorWithRenderPassLoadOp) |
| 324 | { |
| 325 | clearBuffersWithRenderPassLoadOp = clearColorBuffers; |
| 326 | } |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 327 | ANGLE_TRY(clearWithRenderPassOp( |
| 328 | contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth, |
| 329 | clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue)); |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 330 | |
Shahbaz Youssefi | 27f115a | 2019-04-01 10:33:21 -0400 | [diff] [blame] | 331 | // On some hardware, having inline commands at this point results in corrupted output. In |
| 332 | // that case, end the render pass immediately. http://anglebug.com/2361 |
Jonah Ryan-Davis | 776694c | 2019-05-08 10:28:55 -0400 | [diff] [blame] | 333 | if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled) |
Shahbaz Youssefi | 27f115a | 2019-04-01 10:33:21 -0400 | [diff] [blame] | 334 | { |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 335 | mFramebuffer.finishCurrentCommands(contextVk); |
Shahbaz Youssefi | 27f115a | 2019-04-01 10:33:21 -0400 | [diff] [blame] | 336 | } |
| 337 | |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 338 | // Fallback to other methods for whatever isn't cleared here. |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 339 | clearDepth = false; |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 340 | if (clearColorWithRenderPassLoadOp) |
| 341 | { |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 342 | clearColorBuffers.reset(); |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 343 | clearColor = false; |
| 344 | } |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 345 | if (clearStencilWithRenderPassLoadOp) |
| 346 | { |
| 347 | clearStencil = false; |
| 348 | } |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 349 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 350 | // If nothing left to clear, early out. |
| 351 | if (!clearColor && !clearStencil) |
Shahbaz Youssefi | f1153b0 | 2019-03-27 11:22:54 -0400 | [diff] [blame] | 352 | { |
| 353 | return angle::Result::Continue; |
| 354 | } |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 355 | } |
| 356 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 357 | // Note: depth clear is always done through render pass loadOp. |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 358 | ASSERT(clearDepth == false); |
| 359 | |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 360 | // The most costly clear mode is when we need to mask out specific color channels or stencil |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 361 | // bits. This can only be done with a draw call. |
| 362 | return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil, |
| 363 | colorMaskFlags, stencilMask, clearColorValue, |
| 364 | static_cast<uint8_t>(modifiedDepthStencilValue.stencil)); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 365 | } |
| 366 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 367 | angle::Result FramebufferVk::clearBufferfv(const gl::Context *context, |
| 368 | GLenum buffer, |
| 369 | GLint drawbuffer, |
| 370 | const GLfloat *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 371 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 372 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 373 | |
| 374 | bool clearDepth = false; |
| 375 | gl::DrawBufferMask clearColorBuffers; |
| 376 | |
| 377 | if (buffer == GL_DEPTH) |
| 378 | { |
| 379 | clearDepth = true; |
| 380 | clearValue.depthStencil.depth = values[0]; |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | clearColorBuffers.set(drawbuffer); |
| 385 | clearValue.color.float32[0] = values[0]; |
| 386 | clearValue.color.float32[1] = values[1]; |
| 387 | clearValue.color.float32[2] = values[2]; |
| 388 | clearValue.color.float32[3] = values[3]; |
| 389 | } |
| 390 | |
| 391 | return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color, |
| 392 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 395 | angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context, |
| 396 | GLenum buffer, |
| 397 | GLint drawbuffer, |
| 398 | const GLuint *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 399 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 400 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 401 | |
| 402 | gl::DrawBufferMask clearColorBuffers; |
| 403 | clearColorBuffers.set(drawbuffer); |
| 404 | |
| 405 | clearValue.color.uint32[0] = values[0]; |
| 406 | clearValue.color.uint32[1] = values[1]; |
| 407 | clearValue.color.uint32[2] = values[2]; |
| 408 | clearValue.color.uint32[3] = values[3]; |
| 409 | |
| 410 | return clearImpl(context, clearColorBuffers, false, false, clearValue.color, |
| 411 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 412 | } |
| 413 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 414 | angle::Result FramebufferVk::clearBufferiv(const gl::Context *context, |
| 415 | GLenum buffer, |
| 416 | GLint drawbuffer, |
| 417 | const GLint *values) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 418 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 419 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 420 | |
| 421 | bool clearStencil = false; |
| 422 | gl::DrawBufferMask clearColorBuffers; |
| 423 | |
| 424 | if (buffer == GL_STENCIL) |
| 425 | { |
| 426 | clearStencil = true; |
| 427 | clearValue.depthStencil.stencil = |
| 428 | gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max()); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | clearColorBuffers.set(drawbuffer); |
| 433 | clearValue.color.int32[0] = values[0]; |
| 434 | clearValue.color.int32[1] = values[1]; |
| 435 | clearValue.color.int32[2] = values[2]; |
| 436 | clearValue.color.int32[3] = values[3]; |
| 437 | } |
| 438 | |
| 439 | return clearImpl(context, clearColorBuffers, false, clearStencil, clearValue.color, |
| 440 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 441 | } |
| 442 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 443 | angle::Result FramebufferVk::clearBufferfi(const gl::Context *context, |
| 444 | GLenum buffer, |
| 445 | GLint drawbuffer, |
| 446 | GLfloat depth, |
| 447 | GLint stencil) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 448 | { |
Shahbaz Youssefi | edef895 | 2019-04-04 10:03:09 -0400 | [diff] [blame] | 449 | VkClearValue clearValue = {}; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 450 | |
| 451 | clearValue.depthStencil.depth = depth; |
| 452 | clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max()); |
| 453 | |
| 454 | return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color, |
| 455 | clearValue.depthStencil); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 458 | GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 459 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 460 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 461 | } |
| 462 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 463 | GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 464 | { |
Mohan Maiya | 6caa265 | 2019-09-11 08:06:13 -0700 | [diff] [blame^] | 465 | GLenum readType = GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type; |
| 466 | if (context->getClientMajorVersion() < 3 && readType == GL_HALF_FLOAT) |
| 467 | { |
| 468 | // GL_HALF_FLOAT was not introduced until GLES 3.0, and has a different value from |
| 469 | // GL_HALF_FLOAT_OES |
| 470 | readType = GL_HALF_FLOAT_OES; |
| 471 | } |
| 472 | return readType; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 475 | angle::Result FramebufferVk::readPixels(const gl::Context *context, |
| 476 | const gl::Rectangle &area, |
| 477 | GLenum format, |
| 478 | GLenum type, |
| 479 | void *pixels) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 480 | { |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 481 | // Clip read area to framebuffer. |
| 482 | const gl::Extents &fbSize = getState().getReadAttachment()->getSize(); |
| 483 | const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 484 | ContextVk *contextVk = vk::GetImpl(context); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 485 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 486 | gl::Rectangle clippedArea; |
| 487 | if (!ClipRectangle(area, fbRect, &clippedArea)) |
| 488 | { |
| 489 | // nothing to read |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 490 | return angle::Result::Continue; |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 491 | } |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 492 | gl::Rectangle flippedArea = clippedArea; |
Geoff Lang | e076a23 | 2018-07-16 15:34:05 -0400 | [diff] [blame] | 493 | if (contextVk->isViewportFlipEnabledForReadFBO()) |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 494 | { |
| 495 | flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height; |
| 496 | } |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 497 | |
Jamie Madill | c3dc5d4 | 2018-12-30 12:12:04 -0500 | [diff] [blame] | 498 | const gl::State &glState = context->getState(); |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 499 | const gl::PixelPackState &packState = glState.getPackState(); |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 500 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 501 | const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type); |
| 502 | |
| 503 | GLuint outputPitch = 0; |
Jamie Madill | abfbc0f | 2018-10-09 12:48:52 -0400 | [diff] [blame] | 504 | ANGLE_VK_CHECK_MATH(contextVk, |
| 505 | sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment, |
| 506 | packState.rowLength, &outputPitch)); |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 507 | GLuint outputSkipBytes = 0; |
Jamie Madill | abfbc0f | 2018-10-09 12:48:52 -0400 | [diff] [blame] | 508 | ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState, |
| 509 | false, &outputSkipBytes)); |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 510 | |
| 511 | outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes + |
| 512 | (clippedArea.y - area.y) * outputPitch; |
Luc Ferron | 6028422 | 2018-03-20 16:01:44 -0400 | [diff] [blame] | 513 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 514 | const angle::Format &angleFormat = GetFormatFromFormatType(format, type); |
| 515 | |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 516 | PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder, |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 517 | glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0); |
Frank Henigman | 1ffad84 | 2018-09-24 23:40:45 -0400 | [diff] [blame] | 518 | if (contextVk->isViewportFlipEnabledForReadFBO()) |
| 519 | { |
| 520 | params.reverseRowOrder = !params.reverseRowOrder; |
| 521 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 522 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 523 | ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 524 | getColorReadRenderTarget(), |
Rafael Cintron | 05a449a | 2018-06-20 18:08:04 -0700 | [diff] [blame] | 525 | static_cast<uint8_t *>(pixels) + outputSkipBytes)); |
Jamie Madill | c773ab9 | 2019-06-25 17:11:58 -0400 | [diff] [blame] | 526 | mReadPixelBuffer.releaseInFlightBuffers(contextVk); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 527 | return angle::Result::Continue; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 528 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 529 | |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 530 | RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const |
| 531 | { |
| 532 | return mRenderTargetCache.getDepthStencil(); |
| 533 | } |
| 534 | |
Tim Van Patten | 56ba54c | 2019-08-08 13:03:34 -0600 | [diff] [blame] | 535 | RenderTargetVk *FramebufferVk::getColorDrawRenderTarget(size_t colorIndex) const |
| 536 | { |
| 537 | RenderTargetVk *renderTarget = mRenderTargetCache.getColorDraw(mState, colorIndex); |
| 538 | ASSERT(renderTarget && renderTarget->getImage().valid()); |
| 539 | return renderTarget; |
| 540 | } |
| 541 | |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 542 | RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 543 | { |
| 544 | RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 545 | ASSERT(renderTarget && renderTarget->getImage().valid()); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 546 | return renderTarget; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 549 | angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk, |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 550 | const gl::Rectangle &sourceArea, |
| 551 | const gl::Rectangle &destArea, |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 552 | RenderTargetVk *readRenderTarget, |
| 553 | RenderTargetVk *drawRenderTarget, |
| 554 | GLenum filter, |
| 555 | bool colorBlit, |
| 556 | bool depthBlit, |
| 557 | bool stencilBlit, |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 558 | bool flipX, |
| 559 | bool flipY) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 560 | { |
| 561 | // Since blitRenderbufferRect is called for each render buffer that needs to be blitted, |
| 562 | // it should never be the case that both color and depth/stencil need to be blitted at |
| 563 | // at the same time. |
| 564 | ASSERT(colorBlit != (depthBlit || stencilBlit)); |
| 565 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 566 | vk::ImageHelper *srcImage = &readRenderTarget->getImage(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 567 | vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer); |
| 568 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 569 | VkImageAspectFlags imageAspectMask = srcImage->getAspectFlags(); |
| 570 | VkImageAspectFlags blitAspectMask = imageAspectMask; |
| 571 | |
| 572 | // Remove depth or stencil aspects if they are not requested to be blitted. |
| 573 | if (!depthBlit) |
| 574 | { |
| 575 | blitAspectMask &= ~VK_IMAGE_ASPECT_DEPTH_BIT; |
| 576 | } |
| 577 | if (!stencilBlit) |
| 578 | { |
| 579 | blitAspectMask &= ~VK_IMAGE_ASPECT_STENCIL_BIT; |
| 580 | } |
| 581 | |
| 582 | if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc)) |
| 583 | { |
| 584 | vk::CommandBuffer *srcLayoutChange; |
| 585 | ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange)); |
| 586 | srcImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferSrc, srcLayoutChange); |
| 587 | } |
| 588 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 589 | vk::CommandBuffer *commandBuffer = nullptr; |
| 590 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
| 591 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 592 | srcImage->addReadDependency(&mFramebuffer); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 593 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 594 | VkImageBlit blit = {}; |
| 595 | blit.srcSubresource.aspectMask = blitAspectMask; |
| 596 | blit.srcSubresource.mipLevel = readRenderTarget->getLevelIndex(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 597 | blit.srcSubresource.baseArrayLayer = readRenderTarget->getLayerIndex(); |
| 598 | blit.srcSubresource.layerCount = 1; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 599 | blit.srcOffsets[0] = {sourceArea.x0(), sourceArea.y0(), 0}; |
| 600 | blit.srcOffsets[1] = {sourceArea.x1(), sourceArea.y1(), 1}; |
| 601 | blit.dstSubresource.aspectMask = blitAspectMask; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 602 | blit.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex(); |
| 603 | blit.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex(); |
| 604 | blit.dstSubresource.layerCount = 1; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 605 | blit.dstOffsets[0] = {destArea.x0(), destArea.y0(), 0}; |
| 606 | blit.dstOffsets[1] = {destArea.x1(), destArea.y1(), 1}; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 607 | |
| 608 | // Requirement of the copyImageToBuffer, the dst image must be in |
| 609 | // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 610 | dstImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferDst, commandBuffer); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 611 | |
| 612 | commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 613 | dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, |
| 614 | gl_vk::GetFilter(filter)); |
| 615 | |
| 616 | return angle::Result::Continue; |
| 617 | } |
| 618 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 619 | angle::Result FramebufferVk::blit(const gl::Context *context, |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 620 | const gl::Rectangle &sourceAreaIn, |
| 621 | const gl::Rectangle &destAreaIn, |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 622 | GLbitfield mask, |
| 623 | GLenum filter) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 624 | { |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 625 | ContextVk *contextVk = vk::GetImpl(context); |
| 626 | RendererVk *renderer = contextVk->getRenderer(); |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 627 | UtilsVk &utilsVk = contextVk->getUtils(); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 628 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 629 | const gl::State &glState = contextVk->getState(); |
| 630 | const gl::Framebuffer *srcFramebuffer = glState.getReadFramebuffer(); |
| 631 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 632 | const bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0; |
| 633 | const bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0; |
| 634 | const bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 635 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 636 | const bool isResolve = srcFramebuffer->getCachedSamples(context) > 1; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 637 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 638 | FramebufferVk *srcFramebufferVk = vk::GetImpl(srcFramebuffer); |
| 639 | const bool srcFramebufferFlippedY = contextVk->isViewportFlipEnabledForReadFBO(); |
| 640 | const bool destFramebufferFlippedY = contextVk->isViewportFlipEnabledForDrawFBO(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 641 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 642 | gl::Rectangle sourceArea = sourceAreaIn; |
| 643 | gl::Rectangle destArea = destAreaIn; |
| 644 | |
| 645 | // Note: GLES (all 3.x versions) require source and dest area to be identical when |
| 646 | // resolving. |
| 647 | ASSERT(!isResolve || |
| 648 | (sourceArea.x == destArea.x && sourceArea.y == destArea.y && |
| 649 | sourceArea.width == destArea.width && sourceArea.height == destArea.height)); |
| 650 | |
| 651 | const gl::Rectangle srcFramebufferDimensions = |
| 652 | srcFramebufferVk->mState.getDimensions().toRect(); |
| 653 | |
| 654 | // If the destination is flipped in either direction, we will flip the source instead so that |
| 655 | // the destination area is always unflipped. |
| 656 | sourceArea = sourceArea.flip(destArea.isReversedX(), destArea.isReversedY()); |
| 657 | destArea = destArea.removeReversal(); |
| 658 | |
| 659 | // Calculate the stretch factor prior to any clipping, as it needs to remain constant. |
| 660 | const float stretch[2] = { |
| 661 | std::abs(sourceArea.width / static_cast<float>(destArea.width)), |
| 662 | std::abs(sourceArea.height / static_cast<float>(destArea.height)), |
| 663 | }; |
| 664 | |
| 665 | // First, clip the source area to framebuffer. That requires transforming the dest area to |
| 666 | // match the clipped source. |
| 667 | gl::Rectangle absSourceArea = sourceArea.removeReversal(); |
| 668 | gl::Rectangle clippedSourceArea; |
| 669 | if (!gl::ClipRectangle(srcFramebufferDimensions, absSourceArea, &clippedSourceArea)) |
| 670 | { |
| 671 | return angle::Result::Continue; |
| 672 | } |
| 673 | |
| 674 | // Resize the destination area based on the new size of source. Note again that stretch is |
| 675 | // calculated as SrcDimension/DestDimension. |
| 676 | gl::Rectangle srcClippedDestArea; |
| 677 | if (isResolve) |
| 678 | { |
| 679 | // Source and dest areas are identical in resolve. |
| 680 | srcClippedDestArea = clippedSourceArea; |
| 681 | } |
| 682 | else if (clippedSourceArea == absSourceArea) |
| 683 | { |
| 684 | // If there was no clipping, keep dest area as is. |
| 685 | srcClippedDestArea = destArea; |
| 686 | } |
| 687 | else |
| 688 | { |
| 689 | // Shift dest area's x0,y0,x1,y1 by as much as the source area's got shifted (taking |
| 690 | // stretching into account) |
| 691 | float x0Shift = std::round((clippedSourceArea.x - absSourceArea.x) / stretch[0]); |
| 692 | float y0Shift = std::round((clippedSourceArea.y - absSourceArea.y) / stretch[1]); |
| 693 | float x1Shift = std::round((absSourceArea.x1() - clippedSourceArea.x1()) / stretch[0]); |
| 694 | float y1Shift = std::round((absSourceArea.y1() - clippedSourceArea.y1()) / stretch[1]); |
| 695 | |
| 696 | // If the source area was reversed in any direction, the shift should be applied in the |
| 697 | // opposite direction as well. |
| 698 | if (sourceArea.isReversedX()) |
| 699 | { |
| 700 | std::swap(x0Shift, x1Shift); |
| 701 | } |
| 702 | |
| 703 | if (sourceArea.isReversedY()) |
| 704 | { |
| 705 | std::swap(y0Shift, y1Shift); |
| 706 | } |
| 707 | |
| 708 | srcClippedDestArea.x = destArea.x0() + static_cast<int>(x0Shift); |
| 709 | srcClippedDestArea.y = destArea.y0() + static_cast<int>(y0Shift); |
| 710 | int x1 = destArea.x1() - static_cast<int>(x1Shift); |
| 711 | int y1 = destArea.y1() - static_cast<int>(y1Shift); |
| 712 | |
| 713 | srcClippedDestArea.width = x1 - srcClippedDestArea.x; |
| 714 | srcClippedDestArea.height = y1 - srcClippedDestArea.y; |
| 715 | } |
| 716 | |
| 717 | // If framebuffers are flipped in Y, flip the source and dest area (which define the |
| 718 | // transformation regardless of clipping), as well as the blit area (which is the clipped |
| 719 | // dest area). |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 720 | if (srcFramebufferFlippedY) |
| 721 | { |
| 722 | sourceArea.y = srcFramebufferDimensions.height - sourceArea.y; |
| 723 | sourceArea.height = -sourceArea.height; |
| 724 | } |
| 725 | if (destFramebufferFlippedY) |
| 726 | { |
| 727 | destArea.y = mState.getDimensions().height - destArea.y; |
| 728 | destArea.height = -destArea.height; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 729 | |
| 730 | srcClippedDestArea.y = |
| 731 | mState.getDimensions().height - srcClippedDestArea.y - srcClippedDestArea.height; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 732 | } |
| 733 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 734 | const bool flipX = sourceArea.isReversedX() != destArea.isReversedX(); |
| 735 | const bool flipY = sourceArea.isReversedY() != destArea.isReversedY(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 736 | |
| 737 | // GLES doesn't allow flipping the parameters of glBlitFramebuffer if performing a resolve. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 738 | ASSERT(!isResolve || |
| 739 | (flipX == false && flipY == (srcFramebufferFlippedY != destFramebufferFlippedY))); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 740 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 741 | // Again, transfer the destination flip to source, so dest is unflipped. Note that destArea |
| 742 | // was not reversed until the final possible Y-flip. |
| 743 | ASSERT(!destArea.isReversedX()); |
| 744 | sourceArea = sourceArea.flip(false, destArea.isReversedY()); |
| 745 | destArea = destArea.removeReversal(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 746 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 747 | // Clip the destination area to the framebuffer size and scissor. Note that we don't care |
| 748 | // about the source area anymore. The offset translation is done based on the original source |
| 749 | // and destination rectangles. The stretch factor is already calculated as well. |
| 750 | gl::Rectangle blitArea; |
| 751 | if (!gl::ClipRectangle(getScissoredRenderArea(contextVk), srcClippedDestArea, &blitArea)) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 752 | { |
| 753 | return angle::Result::Continue; |
| 754 | } |
| 755 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 756 | bool noClip = blitArea == destArea && stretch[0] == 1.0f && stretch[1] == 1.0f; |
| 757 | bool noFlip = !flipX && !flipY; |
| 758 | bool disableFlippingBlitWithCommand = |
| 759 | contextVk->getRenderer()->getFeatures().disableFlippingBlitWithCommand.enabled; |
| 760 | |
Shahbaz Youssefi | de70a71 | 2019-06-03 17:05:16 -0400 | [diff] [blame] | 761 | UtilsVk::BlitResolveParameters params; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 762 | params.srcOffset[0] = sourceArea.x; |
| 763 | params.srcOffset[1] = sourceArea.y; |
| 764 | params.destOffset[0] = destArea.x; |
| 765 | params.destOffset[1] = destArea.y; |
| 766 | params.stretch[0] = stretch[0]; |
| 767 | params.stretch[1] = stretch[1]; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 768 | params.srcExtents[0] = srcFramebufferDimensions.width; |
| 769 | params.srcExtents[1] = srcFramebufferDimensions.height; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 770 | params.blitArea = blitArea; |
| 771 | params.linear = filter == GL_LINEAR; |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 772 | params.flipX = flipX; |
| 773 | params.flipY = flipY; |
| 774 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 775 | if (blitColorBuffer) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 776 | { |
| 777 | RenderTargetVk *readRenderTarget = srcFramebufferVk->getColorReadRenderTarget(); |
| 778 | params.srcLayer = readRenderTarget->getLayerIndex(); |
| 779 | |
| 780 | // Multisampled images are not allowed to have mips. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 781 | ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 782 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 783 | // If there was no clipping and the format capabilities allow us, use Vulkan's builtin blit. |
| 784 | // The reason clipping is prohibited in this path is that due to rounding errors, it would |
| 785 | // be hard to guarantee the image stretching remains perfect. That also allows us not to |
| 786 | // have to transform back the dest clipping to source. |
| 787 | // |
| 788 | // For simplicity, we either blit all render targets with a Vulkan command, or none. |
| 789 | bool canBlitWithCommand = !isResolve && noClip && |
| 790 | (noFlip || !disableFlippingBlitWithCommand) && |
| 791 | HasSrcBlitFeature(renderer, readRenderTarget); |
| 792 | bool areChannelsBlitCompatible = true; |
| 793 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
| 794 | { |
| 795 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
| 796 | canBlitWithCommand = |
| 797 | canBlitWithCommand && HasDstBlitFeature(renderer, drawRenderTarget); |
| 798 | areChannelsBlitCompatible = |
| 799 | areChannelsBlitCompatible && |
| 800 | areSrcAndDstColorChannelsBlitCompatible(readRenderTarget, drawRenderTarget); |
| 801 | } |
| 802 | |
| 803 | if (canBlitWithCommand && areChannelsBlitCompatible) |
| 804 | { |
| 805 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
| 806 | { |
| 807 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
| 808 | ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget, |
| 809 | drawRenderTarget, filter, true, false, false, flipX, |
| 810 | flipY)); |
| 811 | } |
| 812 | } |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 813 | // If we're not flipping, use Vulkan's builtin resolve. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 814 | else if (isResolve && !flipX && !flipY && areChannelsBlitCompatible) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 815 | { |
| 816 | ANGLE_TRY(resolveColorWithCommand(contextVk, params, &readRenderTarget->getImage())); |
| 817 | } |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 818 | // Otherwise use a shader to do blit or resolve. |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 819 | else |
| 820 | { |
Shahbaz Youssefi | de70a71 | 2019-06-03 17:05:16 -0400 | [diff] [blame] | 821 | ANGLE_TRY(utilsVk.colorBlitResolve(contextVk, this, &readRenderTarget->getImage(), |
| 822 | readRenderTarget->getFetchImageView(), params)); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 826 | if (blitDepthBuffer || blitStencilBuffer) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 827 | { |
| 828 | RenderTargetVk *readRenderTarget = srcFramebufferVk->getDepthStencilRenderTarget(); |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 829 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil(); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 830 | params.srcLayer = readRenderTarget->getLayerIndex(); |
| 831 | |
| 832 | // Multisampled images are not allowed to have mips. |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 833 | ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 834 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 835 | // Similarly, only blit if there's been no clipping. |
| 836 | bool canBlitWithCommand = !isResolve && noClip && |
| 837 | (noFlip || !disableFlippingBlitWithCommand) && |
| 838 | HasSrcBlitFeature(renderer, readRenderTarget) && |
| 839 | HasDstBlitFeature(renderer, drawRenderTarget); |
| 840 | bool areChannelsBlitCompatible = |
| 841 | areSrcAndDstDepthStencilChannelsBlitCompatible(readRenderTarget, drawRenderTarget); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 842 | |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 843 | if (canBlitWithCommand && areChannelsBlitCompatible) |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 844 | { |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 845 | ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget, |
| 846 | drawRenderTarget, filter, false, blitDepthBuffer, |
| 847 | blitStencilBuffer, flipX, flipY)); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 848 | } |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 849 | else |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 850 | { |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 851 | // Create depth- and stencil-only views for reading. |
Shahbaz Youssefi | 57ad1e1 | 2019-08-23 14:53:26 -0400 | [diff] [blame] | 852 | vk::DeviceScoped<vk::ImageView> depthView(contextVk->getDevice()); |
| 853 | vk::DeviceScoped<vk::ImageView> stencilView(contextVk->getDevice()); |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 854 | |
| 855 | vk::ImageHelper *depthStencilImage = &readRenderTarget->getImage(); |
| 856 | uint32_t levelIndex = readRenderTarget->getLevelIndex(); |
| 857 | uint32_t layerIndex = readRenderTarget->getLayerIndex(); |
| 858 | gl::TextureType textureType = vk::Get2DTextureType(depthStencilImage->getLayerCount(), |
| 859 | depthStencilImage->getSamples()); |
| 860 | |
| 861 | if (blitDepthBuffer) |
| 862 | { |
| 863 | ANGLE_TRY(depthStencilImage->initLayerImageView( |
| 864 | contextVk, textureType, VK_IMAGE_ASPECT_DEPTH_BIT, gl::SwizzleState(), |
| 865 | &depthView.get(), levelIndex, 1, layerIndex, 1)); |
| 866 | } |
| 867 | |
| 868 | if (blitStencilBuffer) |
| 869 | { |
| 870 | ANGLE_TRY(depthStencilImage->initLayerImageView( |
| 871 | contextVk, textureType, VK_IMAGE_ASPECT_STENCIL_BIT, gl::SwizzleState(), |
| 872 | &stencilView.get(), levelIndex, 1, layerIndex, 1)); |
| 873 | } |
| 874 | |
| 875 | // If shader stencil export is not possible, defer stencil blit/stencil to another pass. |
| 876 | bool hasShaderStencilExport = |
| 877 | contextVk->getRenderer()->getFeatures().supportsShaderStencilExport.enabled; |
| 878 | |
| 879 | // Blit depth. If shader stencil export is present, blit stencil as well. |
| 880 | if (blitDepthBuffer || (blitStencilBuffer && hasShaderStencilExport)) |
| 881 | { |
Shahbaz Youssefi | 0b1fbcf | 2019-08-30 15:05:12 -0400 | [diff] [blame] | 882 | const vk::ImageView *depth = blitDepthBuffer ? &depthView.get() : nullptr; |
| 883 | const vk::ImageView *stencil = |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 884 | blitStencilBuffer && hasShaderStencilExport ? &stencilView.get() : nullptr; |
| 885 | |
| 886 | ANGLE_TRY(utilsVk.depthStencilBlitResolve(contextVk, this, depthStencilImage, depth, |
| 887 | stencil, params)); |
| 888 | } |
| 889 | |
| 890 | // If shader stencil export is not present, blit stencil through a different path. |
| 891 | if (blitStencilBuffer && !hasShaderStencilExport) |
| 892 | { |
| 893 | ANGLE_TRY(utilsVk.stencilBlitResolveNoShaderExport( |
| 894 | contextVk, this, depthStencilImage, &stencilView.get(), params)); |
| 895 | } |
| 896 | |
| 897 | vk::ImageView depthViewObject = depthView.release(); |
| 898 | vk::ImageView stencilViewObject = stencilView.release(); |
| 899 | |
| 900 | contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &depthViewObject); |
| 901 | contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &stencilViewObject); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 902 | } |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | return angle::Result::Continue; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 906 | } // namespace rx |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 907 | |
| 908 | angle::Result FramebufferVk::resolveColorWithCommand(ContextVk *contextVk, |
Shahbaz Youssefi | de70a71 | 2019-06-03 17:05:16 -0400 | [diff] [blame] | 909 | const UtilsVk::BlitResolveParameters ¶ms, |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 910 | vk::ImageHelper *srcImage) |
| 911 | { |
| 912 | if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc)) |
| 913 | { |
| 914 | vk::CommandBuffer *srcLayoutChange; |
| 915 | ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange)); |
| 916 | srcImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferSrc, |
| 917 | srcLayoutChange); |
| 918 | } |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 919 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 920 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 16c2014 | 2018-10-01 13:58:19 -0400 | [diff] [blame] | 921 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
| 922 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 923 | // Source's layout change should happen before rendering |
| 924 | srcImage->addReadDependency(&mFramebuffer); |
Luc Ferron | 2658111 | 2018-06-21 09:43:08 -0400 | [diff] [blame] | 925 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 926 | VkImageResolve resolveRegion = {}; |
| 927 | resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 928 | resolveRegion.srcSubresource.mipLevel = 0; |
| 929 | resolveRegion.srcSubresource.baseArrayLayer = params.srcLayer; |
| 930 | resolveRegion.srcSubresource.layerCount = 1; |
| 931 | resolveRegion.srcOffset.x = params.srcOffset[0]; |
| 932 | resolveRegion.srcOffset.y = params.srcOffset[1]; |
| 933 | resolveRegion.srcOffset.z = 0; |
| 934 | resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 935 | resolveRegion.dstSubresource.layerCount = 1; |
| 936 | resolveRegion.dstOffset.x = params.destOffset[0]; |
| 937 | resolveRegion.dstOffset.y = params.destOffset[1]; |
| 938 | resolveRegion.dstOffset.z = 0; |
| 939 | resolveRegion.extent.width = params.srcExtents[0]; |
| 940 | resolveRegion.extent.height = params.srcExtents[1]; |
| 941 | resolveRegion.extent.depth = 1; |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 942 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 943 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 944 | { |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 945 | RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
Shahbaz Youssefi | b407e1a | 2019-06-03 17:15:51 -0400 | [diff] [blame] | 946 | vk::ImageHelper *drawImage = drawRenderTarget->getImageForWrite(&mFramebuffer); |
| 947 | drawImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferDst, |
| 948 | commandBuffer); |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 949 | |
| 950 | resolveRegion.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex(); |
| 951 | resolveRegion.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex(); |
| 952 | |
| 953 | srcImage->resolve(&drawRenderTarget->getImage(), resolveRegion, commandBuffer); |
Luc Ferron | 82eda93 | 2018-07-09 15:10:22 -0400 | [diff] [blame] | 954 | } |
| 955 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 956 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 957 | } |
| 958 | |
Kenneth Russell | ce8602a | 2017-10-03 18:23:08 -0700 | [diff] [blame] | 959 | bool FramebufferVk::checkStatus(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 960 | { |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 961 | // if we have both a depth and stencil buffer, they must refer to the same object |
| 962 | // since we only support packed_depth_stencil and not separate depth and stencil |
| 963 | if (mState.hasSeparateDepthAndStencilAttachments()) |
| 964 | { |
| 965 | return false; |
| 966 | } |
| 967 | |
Jamie Madill | b79e7bb | 2017-10-24 13:55:50 -0400 | [diff] [blame] | 968 | return true; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 969 | } |
| 970 | |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 971 | angle::Result FramebufferVk::updateColorAttachment(const gl::Context *context, size_t colorIndexGL) |
| 972 | { |
| 973 | ContextVk *contextVk = vk::GetImpl(context); |
| 974 | |
| 975 | ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL)); |
| 976 | |
| 977 | // Update cached masks for masked clears. |
| 978 | RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL]; |
| 979 | if (renderTarget) |
| 980 | { |
| 981 | const angle::Format &emulatedFormat = renderTarget->getImageFormat().imageFormat(); |
| 982 | updateActiveColorMasks(colorIndexGL, emulatedFormat.redBits > 0, |
| 983 | emulatedFormat.greenBits > 0, emulatedFormat.blueBits > 0, |
| 984 | emulatedFormat.alphaBits > 0); |
| 985 | |
| 986 | const angle::Format &sourceFormat = renderTarget->getImageFormat().angleFormat(); |
| 987 | mEmulatedAlphaAttachmentMask.set( |
| 988 | colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0); |
| 989 | |
| 990 | contextVk->updateColorMask(context->getState().getBlendState()); |
| 991 | } |
| 992 | else |
| 993 | { |
| 994 | updateActiveColorMasks(colorIndexGL, false, false, false, false); |
| 995 | } |
| 996 | |
| 997 | return angle::Result::Continue; |
| 998 | } |
| 999 | |
Shahbaz Youssefi | da90448 | 2019-07-02 10:49:14 -0400 | [diff] [blame] | 1000 | void FramebufferVk::invalidateImpl(ContextVk *contextVk, size_t count, const GLenum *attachments) |
| 1001 | { |
| 1002 | ASSERT(mFramebuffer.hasStartedRenderPass()); |
| 1003 | |
| 1004 | gl::DrawBufferMask invalidateColorBuffers; |
| 1005 | bool invalidateDepthBuffer = false; |
| 1006 | bool invalidateStencilBuffer = false; |
| 1007 | |
| 1008 | for (size_t i = 0; i < count; ++i) |
| 1009 | { |
| 1010 | const GLenum attachment = attachments[i]; |
| 1011 | |
| 1012 | switch (attachment) |
| 1013 | { |
| 1014 | case GL_DEPTH: |
| 1015 | case GL_DEPTH_ATTACHMENT: |
| 1016 | invalidateDepthBuffer = true; |
| 1017 | break; |
| 1018 | case GL_STENCIL: |
| 1019 | case GL_STENCIL_ATTACHMENT: |
| 1020 | invalidateStencilBuffer = true; |
| 1021 | break; |
| 1022 | case GL_DEPTH_STENCIL_ATTACHMENT: |
| 1023 | invalidateDepthBuffer = true; |
| 1024 | invalidateStencilBuffer = true; |
| 1025 | break; |
| 1026 | default: |
| 1027 | ASSERT( |
| 1028 | (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) || |
| 1029 | (attachment == GL_COLOR)); |
| 1030 | |
| 1031 | invalidateColorBuffers.set( |
| 1032 | attachment == GL_COLOR ? 0u : (attachment - GL_COLOR_ATTACHMENT0)); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | // Set the appropriate storeOp for attachments. |
| 1037 | size_t attachmentIndexVk = 0; |
| 1038 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
| 1039 | { |
| 1040 | if (invalidateColorBuffers.test(colorIndexGL)) |
| 1041 | { |
| 1042 | mFramebuffer.invalidateRenderPassColorAttachment(attachmentIndexVk); |
| 1043 | } |
| 1044 | ++attachmentIndexVk; |
| 1045 | } |
| 1046 | |
| 1047 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1048 | if (depthStencilRenderTarget) |
| 1049 | { |
| 1050 | if (invalidateDepthBuffer) |
| 1051 | { |
| 1052 | mFramebuffer.invalidateRenderPassDepthAttachment(attachmentIndexVk); |
| 1053 | } |
| 1054 | |
| 1055 | if (invalidateStencilBuffer) |
| 1056 | { |
| 1057 | mFramebuffer.invalidateRenderPassStencilAttachment(attachmentIndexVk); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | // NOTE: Possible future optimization is to delay setting the storeOp and only do so if the |
| 1062 | // render pass is closed by itself before another draw call. Otherwise, in a situation like |
| 1063 | // this: |
| 1064 | // |
| 1065 | // draw() |
| 1066 | // invalidate() |
| 1067 | // draw() |
| 1068 | // |
| 1069 | // We would be discarding the attachments only to load them for the next draw (which is less |
| 1070 | // efficient than keeping the render pass open and not do the discard at all). While dEQP tests |
| 1071 | // this pattern, this optimization may not be necessary if no application does this. It is |
| 1072 | // expected that an application would invalidate() when it's done with the framebuffer, so the |
| 1073 | // render pass would have closed either way. |
| 1074 | mFramebuffer.finishCurrentCommands(contextVk); |
| 1075 | } |
| 1076 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 1077 | angle::Result FramebufferVk::syncState(const gl::Context *context, |
| 1078 | const gl::Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1079 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 1080 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1081 | |
| 1082 | ASSERT(dirtyBits.any()); |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 1083 | for (size_t dirtyBit : dirtyBits) |
| 1084 | { |
| 1085 | switch (dirtyBit) |
| 1086 | { |
| 1087 | case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
| 1088 | case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
| 1089 | ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState)); |
| 1090 | break; |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1091 | case gl::Framebuffer::DIRTY_BIT_DEPTH_BUFFER_CONTENTS: |
| 1092 | case gl::Framebuffer::DIRTY_BIT_STENCIL_BUFFER_CONTENTS: |
| 1093 | ANGLE_TRY(mRenderTargetCache.getDepthStencil()->flushStagedUpdates(contextVk)); |
| 1094 | break; |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 1095 | case gl::Framebuffer::DIRTY_BIT_READ_BUFFER: |
Tim Van Patten | 56ba54c | 2019-08-08 13:03:34 -0600 | [diff] [blame] | 1096 | ANGLE_TRY(mRenderTargetCache.update(context, mState, dirtyBits)); |
| 1097 | break; |
| 1098 | case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 1099 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH: |
| 1100 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT: |
| 1101 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES: |
| 1102 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS: |
| 1103 | break; |
| 1104 | default: |
| 1105 | { |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1106 | static_assert(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits"); |
| 1107 | if (dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1108 | { |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1109 | size_t colorIndexGL = static_cast<size_t>( |
| 1110 | dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 1111 | ANGLE_TRY(updateColorAttachment(context, colorIndexGL)); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1112 | } |
| 1113 | else |
| 1114 | { |
Jamie Madill | 6722009 | 2019-05-20 11:12:53 -0400 | [diff] [blame] | 1115 | ASSERT(dirtyBit >= gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 && |
| 1116 | dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_MAX); |
| 1117 | size_t colorIndexGL = static_cast<size_t>( |
| 1118 | dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0); |
| 1119 | ANGLE_TRY(mRenderTargetCache.getColors()[colorIndexGL]->flushStagedUpdates( |
| 1120 | contextVk)); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1121 | } |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | } |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1125 | |
Tim Van Patten | 626a728 | 2019-07-08 15:11:59 -0600 | [diff] [blame] | 1126 | // The FBOs new attachment may have changed the renderable area |
| 1127 | const gl::State &glState = context->getState(); |
| 1128 | contextVk->updateScissor(glState); |
| 1129 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1130 | mActiveColorComponents = gl_vk::GetColorComponentFlags( |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1131 | mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(), |
| 1132 | mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any()); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1133 | |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1134 | mFramebuffer.release(contextVk); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1135 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 1136 | // 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] | 1137 | // create a new entry in the command graph. |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1138 | mFramebuffer.finishCurrentCommands(contextVk); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 1139 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1140 | // Notify the ContextVk to update the pipeline desc. |
| 1141 | updateRenderPassDesc(); |
Shahbaz Youssefi | da90448 | 2019-07-02 10:49:14 -0400 | [diff] [blame] | 1142 | |
| 1143 | FramebufferVk *currentDrawFramebuffer = vk::GetImpl(context->getState().getDrawFramebuffer()); |
| 1144 | if (currentDrawFramebuffer == this) |
| 1145 | { |
| 1146 | contextVk->onDrawFramebufferChange(this); |
| 1147 | } |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 1148 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1149 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1152 | void FramebufferVk::updateRenderPassDesc() |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1153 | { |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1154 | mRenderPassDesc = {}; |
| 1155 | mRenderPassDesc.setSamples(getSamples()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1156 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1157 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 1158 | const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers(); |
| 1159 | for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1160 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1161 | if (enabledDrawBuffers[colorIndexGL]) |
| 1162 | { |
| 1163 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
| 1164 | ASSERT(colorRenderTarget); |
| 1165 | mRenderPassDesc.packColorAttachment( |
| 1166 | colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID); |
| 1167 | } |
| 1168 | else |
| 1169 | { |
| 1170 | mRenderPassDesc.packColorAttachmentGap(colorIndexGL); |
| 1171 | } |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1172 | } |
| 1173 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1174 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1175 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1176 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1177 | mRenderPassDesc.packDepthStencilAttachment( |
| 1178 | depthStencilRenderTarget->getImage().getFormat().angleFormatID); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1179 | } |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1180 | } |
| 1181 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1182 | angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1183 | { |
| 1184 | // If we've already created our cached Framebuffer, return it. |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 1185 | if (mFramebuffer.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1186 | { |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1187 | *framebufferOut = &mFramebuffer.getFramebuffer(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1188 | return angle::Result::Continue; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1189 | } |
| 1190 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1191 | vk::RenderPass *compatibleRenderPass = nullptr; |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1192 | ANGLE_TRY(contextVk->getCompatibleRenderPass(mRenderPassDesc, &compatibleRenderPass)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1193 | |
| 1194 | // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it. |
| 1195 | if (mBackbuffer) |
| 1196 | { |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1197 | return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | // Gather VkImageViews over all FBO attachments, also size of attached region. |
| 1201 | std::vector<VkImageView> attachments; |
| 1202 | gl::Extents attachmentsSize; |
| 1203 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1204 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1205 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1206 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1207 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1208 | ASSERT(colorRenderTarget); |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 1209 | attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1210 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1211 | ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents()); |
| 1212 | attachmentsSize = colorRenderTarget->getExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1213 | } |
| 1214 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1215 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1216 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1217 | { |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 1218 | attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1219 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 1220 | ASSERT(attachmentsSize.empty() || |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1221 | attachmentsSize == depthStencilRenderTarget->getExtents()); |
| 1222 | attachmentsSize = depthStencilRenderTarget->getExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1223 | } |
| 1224 | |
Tim Van Patten | 626a728 | 2019-07-08 15:11:59 -0600 | [diff] [blame] | 1225 | if (attachmentsSize.empty()) |
| 1226 | { |
| 1227 | // No attachments, so use the default values. |
| 1228 | attachmentsSize.height = mState.getDefaultHeight(); |
| 1229 | attachmentsSize.width = mState.getDefaultWidth(); |
| 1230 | attachmentsSize.depth = 0; |
| 1231 | } |
| 1232 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1233 | VkFramebufferCreateInfo framebufferInfo = {}; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1234 | |
| 1235 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1236 | framebufferInfo.flags = 0; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1237 | framebufferInfo.renderPass = compatibleRenderPass->getHandle(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 1238 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); |
| 1239 | framebufferInfo.pAttachments = attachments.data(); |
| 1240 | framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width); |
| 1241 | framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height); |
| 1242 | framebufferInfo.layers = 1; |
| 1243 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1244 | ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo)); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 1245 | |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1246 | *framebufferOut = &mFramebuffer.getFramebuffer(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1247 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1248 | } |
| 1249 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1250 | angle::Result FramebufferVk::clearWithRenderPassOp( |
| 1251 | ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1252 | const gl::Rectangle &clearArea, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1253 | gl::DrawBufferMask clearColorBuffers, |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1254 | bool clearDepth, |
| 1255 | bool clearStencil, |
| 1256 | const VkClearColorValue &clearColorValue, |
| 1257 | const VkClearDepthStencilValue &clearDepthStencilValue) |
| 1258 | { |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1259 | // Start a new render pass if: |
| 1260 | // |
| 1261 | // - no render pass has started, |
| 1262 | // - there is a render pass started but it contains commands; we cannot modify its ops, so new |
| 1263 | // render pass is needed, |
| 1264 | // - the current render area doesn't match the clear area. We need the render area to be |
| 1265 | // exactly as specified by the scissor for the loadOp to clear only that area. See |
| 1266 | // onScissorChange for more information. |
| 1267 | |
| 1268 | if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() || |
| 1269 | mFramebuffer.getRenderPassRenderArea() != clearArea) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1270 | { |
| 1271 | vk::CommandBuffer *commandBuffer; |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1272 | ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer)); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1273 | } |
| 1274 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1275 | size_t attachmentIndexVk = 0; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1276 | |
| 1277 | // Go through clearColorBuffers and set the appropriate loadOp and clear values. |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1278 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1279 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1280 | if (clearColorBuffers.test(colorIndexGL)) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1281 | { |
Tim Van Patten | 56ba54c | 2019-08-08 13:03:34 -0600 | [diff] [blame] | 1282 | RenderTargetVk *renderTarget = getColorDrawRenderTarget(colorIndexGL); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1283 | |
| 1284 | // If the render target doesn't have alpha, but its emulated format has it, clear the |
| 1285 | // alpha to 1. |
| 1286 | VkClearColorValue value = clearColorValue; |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1287 | if (mEmulatedAlphaAttachmentMask[colorIndexGL]) |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1288 | { |
| 1289 | SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value); |
| 1290 | } |
| 1291 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1292 | mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value); |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1293 | } |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1294 | ++attachmentIndexVk; |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | // Set the appropriate loadOp and clear values for depth and stencil. |
| 1298 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1299 | if (depthStencilRenderTarget) |
| 1300 | { |
| 1301 | if (clearDepth) |
| 1302 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1303 | mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1304 | clearDepthStencilValue.depth); |
| 1305 | } |
| 1306 | |
| 1307 | if (clearStencil) |
| 1308 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1309 | mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1310 | clearDepthStencilValue.stencil); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | return angle::Result::Continue; |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1315 | } |
| 1316 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1317 | angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1318 | const gl::Rectangle &clearArea, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1319 | gl::DrawBufferMask clearColorBuffers, |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1320 | bool clearStencil, |
| 1321 | VkColorComponentFlags colorMaskFlags, |
| 1322 | uint8_t stencilMask, |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1323 | const VkClearColorValue &clearColorValue, |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1324 | uint8_t clearStencilValue) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1325 | { |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1326 | UtilsVk::ClearFramebufferParameters params = {}; |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1327 | params.clearArea = clearArea; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1328 | params.colorClearValue = clearColorValue; |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1329 | params.stencilClearValue = clearStencilValue; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1330 | params.stencilMask = stencilMask; |
| 1331 | |
| 1332 | params.clearColor = true; |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1333 | params.clearStencil = clearStencil; |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1334 | |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1335 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1336 | for (size_t colorIndexGL : clearColorBuffers) |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1337 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1338 | const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1339 | ASSERT(colorRenderTarget); |
| 1340 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1341 | params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat(); |
James Darpinian | 7e48c9e | 2019-08-06 17:17:19 -0700 | [diff] [blame] | 1342 | params.colorAttachmentIndexGL = static_cast<uint32_t>(colorIndexGL); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1343 | params.colorMaskFlags = colorMaskFlags; |
| 1344 | if (mEmulatedAlphaAttachmentMask[colorIndexGL]) |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1345 | { |
| 1346 | params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT; |
| 1347 | } |
| 1348 | |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1349 | ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params)); |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1350 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1351 | // Clear stencil only once! |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1352 | params.clearStencil = false; |
| 1353 | } |
| 1354 | |
Shahbaz Youssefi | 2249d4a | 2019-04-05 16:48:55 -0400 | [diff] [blame] | 1355 | // If there was no color clear, clear stencil alone. |
| 1356 | if (params.clearStencil) |
Shahbaz Youssefi | f6c937f | 2019-04-02 17:04:08 -0400 | [diff] [blame] | 1357 | { |
| 1358 | params.clearColor = false; |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1359 | ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params)); |
Shahbaz Youssefi | 4399701 | 2019-03-30 23:24:01 -0400 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | return angle::Result::Continue; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1363 | } |
| 1364 | |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 1365 | angle::Result FramebufferVk::getSamplePosition(const gl::Context *context, |
| 1366 | size_t index, |
| 1367 | GLfloat *xy) const |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1368 | { |
Jamie Madill | 64b7c4f | 2018-10-19 11:38:04 -0400 | [diff] [blame] | 1369 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1370 | return angle::Result::Stop; |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1371 | } |
| 1372 | |
Jamie Madill | d1249de | 2018-08-28 16:58:53 -0400 | [diff] [blame] | 1373 | angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk, |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1374 | const gl::Rectangle &renderArea, |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1375 | vk::CommandBuffer **commandBufferOut) |
Jamie Madill | d1249de | 2018-08-28 16:58:53 -0400 | [diff] [blame] | 1376 | { |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1377 | vk::Framebuffer *framebuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1378 | ANGLE_TRY(getFramebuffer(contextVk, &framebuffer)); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1379 | |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1380 | vk::AttachmentOpsArray renderPassAttachmentOps; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1381 | std::vector<VkClearValue> attachmentClearValues; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1382 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1383 | vk::CommandBuffer *writeCommands = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1384 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands)); |
Jamie Madill | e4c5a23 | 2018-03-02 21:00:31 -0500 | [diff] [blame] | 1385 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1386 | // Initialize RenderPass info. |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1387 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1388 | for (size_t colorIndexGL : mState.getEnabledDrawBuffers()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1389 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1390 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL]; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1391 | ASSERT(colorRenderTarget); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1392 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1393 | ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands)); |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1394 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1395 | renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(), |
| 1396 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1397 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 1398 | attachmentClearValues.emplace_back(kUninitializedClearValue); |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 1402 | if (depthStencilRenderTarget) |
| 1403 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1404 | ANGLE_TRY( |
| 1405 | depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands)); |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1406 | |
Shahbaz Youssefi | db4ed31 | 2019-03-29 00:32:45 -0400 | [diff] [blame] | 1407 | renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(), |
| 1408 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1409 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
| 1410 | attachmentClearValues.emplace_back(kUninitializedClearValue); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1411 | } |
| 1412 | |
Jamie Madill | dbc605c | 2019-01-04 16:39:14 -0500 | [diff] [blame] | 1413 | return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc, |
Shahbaz Youssefi | 0c128e1 | 2019-03-25 23:50:14 -0400 | [diff] [blame] | 1414 | renderPassAttachmentOps, attachmentClearValues, |
| 1415 | commandBufferOut); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 1416 | } |
| 1417 | |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1418 | void FramebufferVk::updateActiveColorMasks(size_t colorIndexGL, bool r, bool g, bool b, bool a) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1419 | { |
Shahbaz Youssefi | 9fa248e | 2019-05-06 14:55:18 -0400 | [diff] [blame] | 1420 | mActiveColorComponentMasksForClear[0].set(colorIndexGL, r); |
| 1421 | mActiveColorComponentMasksForClear[1].set(colorIndexGL, g); |
| 1422 | mActiveColorComponentMasksForClear[2].set(colorIndexGL, b); |
| 1423 | mActiveColorComponentMasksForClear[3].set(colorIndexGL, a); |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1424 | } |
| 1425 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 1426 | const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const |
Luc Ferron | 5fd3693 | 2018-06-19 14:55:50 -0400 | [diff] [blame] | 1427 | { |
| 1428 | return mEmulatedAlphaAttachmentMask; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1429 | } |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1430 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1431 | angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk, |
| 1432 | const gl::Rectangle &area, |
| 1433 | const PackPixelsParams &packPixelsParams, |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1434 | VkImageAspectFlagBits copyAspectFlags, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1435 | RenderTargetVk *renderTarget, |
| 1436 | void *pixels) |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1437 | { |
Jamie Madill | 3ea463b | 2019-06-19 14:21:33 -0400 | [diff] [blame] | 1438 | ANGLE_TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl"); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1439 | |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1440 | RendererVk *renderer = contextVk->getRenderer(); |
| 1441 | |
Shahbaz Youssefi | 2660b50 | 2019-03-21 12:08:40 -0400 | [diff] [blame] | 1442 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | e8dd079 | 2018-09-27 15:04:27 -0400 | [diff] [blame] | 1443 | ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer)); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1444 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1445 | // Note that although we're reading from the image, we need to update the layout below. |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 1446 | vk::ImageHelper *srcImage = |
| 1447 | renderTarget->getImageForRead(&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1448 | |
Jamie Madill | 0631e19 | 2019-04-18 16:09:12 -0400 | [diff] [blame] | 1449 | const angle::Format *readFormat = &srcImage->getFormat().imageFormat(); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1450 | |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1451 | if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT) |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1452 | { |
Jamie Madill | b436aac | 2018-07-18 17:23:48 -0400 | [diff] [blame] | 1453 | readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags); |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1454 | } |
| 1455 | |
James Darpinian | 7e48c9e | 2019-08-06 17:17:19 -0700 | [diff] [blame] | 1456 | uint32_t level = renderTarget->getLevelIndex(); |
| 1457 | uint32_t layer = renderTarget->getLayerIndex(); |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1458 | VkOffset3D srcOffset = {area.x, area.y, 0}; |
Cody Northrop | eb0479e | 2019-07-24 14:15:56 -0600 | [diff] [blame] | 1459 | |
| 1460 | VkImageSubresourceLayers srcSubresource = {}; |
| 1461 | srcSubresource.aspectMask = copyAspectFlags; |
| 1462 | srcSubresource.mipLevel = level; |
| 1463 | srcSubresource.baseArrayLayer = layer; |
| 1464 | srcSubresource.layerCount = 1; |
| 1465 | |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1466 | VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height), |
| 1467 | 1}; |
| 1468 | |
Cody Northrop | eb0479e | 2019-07-24 14:15:56 -0600 | [diff] [blame] | 1469 | if (srcImage->getExtents().depth > 1) |
| 1470 | { |
| 1471 | // Depth > 1 means this is a 3D texture and we need special handling |
| 1472 | srcOffset.z = layer; |
| 1473 | srcSubresource.baseArrayLayer = 0; |
| 1474 | } |
| 1475 | |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1476 | // If the source image is multisampled, we need to resolve it into a temporary image before |
| 1477 | // performing a readback. |
| 1478 | bool isMultisampled = srcImage->getSamples() > 1; |
Shahbaz Youssefi | 57ad1e1 | 2019-08-23 14:53:26 -0400 | [diff] [blame] | 1479 | vk::DeviceScoped<vk::ImageHelper> resolvedImage(contextVk->getDevice()); |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1480 | if (isMultisampled) |
| 1481 | { |
| 1482 | ANGLE_TRY(resolvedImage.get().init2DStaging( |
| 1483 | contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1), |
| 1484 | srcImage->getFormat(), |
| 1485 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1)); |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1486 | resolvedImage.get().updateQueueSerial(contextVk->getCurrentQueueSerial()); |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1487 | |
Shahbaz Youssefi | f2a1c38 | 2019-05-21 16:32:49 -0400 | [diff] [blame] | 1488 | // Note: resolve only works on color images (not depth/stencil). |
| 1489 | // |
| 1490 | // TODO: Currently, depth/stencil blit can perform a depth/stencil readback, but that code |
| 1491 | // path will be optimized away. http://anglebug.com/3200 |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1492 | ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT); |
| 1493 | |
| 1494 | VkImageResolve resolveRegion = {}; |
Cody Northrop | eb0479e | 2019-07-24 14:15:56 -0600 | [diff] [blame] | 1495 | resolveRegion.srcSubresource = srcSubresource; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1496 | resolveRegion.srcOffset = srcOffset; |
| 1497 | resolveRegion.dstSubresource.aspectMask = copyAspectFlags; |
| 1498 | resolveRegion.dstSubresource.mipLevel = 0; |
| 1499 | resolveRegion.dstSubresource.baseArrayLayer = 0; |
| 1500 | resolveRegion.dstSubresource.layerCount = 1; |
| 1501 | resolveRegion.dstOffset = {}; |
| 1502 | resolveRegion.extent = srcExtent; |
| 1503 | |
| 1504 | srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer); |
| 1505 | |
| 1506 | resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc, |
| 1507 | commandBuffer); |
| 1508 | |
| 1509 | // Make the resolved image the target of buffer copy. |
Mingyu Hu | 7e44ec2 | 2019-08-26 15:59:48 -0700 | [diff] [blame] | 1510 | srcImage = &resolvedImage.get(); |
| 1511 | level = 0; |
| 1512 | layer = 0; |
| 1513 | srcOffset = {0, 0, 0}; |
Cody Northrop | eb0479e | 2019-07-24 14:15:56 -0600 | [diff] [blame] | 1514 | srcSubresource.baseArrayLayer = 0; |
| 1515 | srcSubresource.layerCount = 1; |
| 1516 | srcSubresource.mipLevel = 0; |
Shahbaz Youssefi | b16d69c | 2019-05-13 16:28:27 -0400 | [diff] [blame] | 1517 | } |
| 1518 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1519 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 1520 | uint8_t *readPixelBuffer = nullptr; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 1521 | VkDeviceSize stagingOffset = 0; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1522 | size_t allocationSize = readFormat->pixelBytes * area.width * area.height; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1523 | |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 1524 | ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle, |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 1525 | &stagingOffset, nullptr)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1526 | |
Cody Northrop | eb0479e | 2019-07-24 14:15:56 -0600 | [diff] [blame] | 1527 | VkBufferImageCopy region = {}; |
| 1528 | region.bufferImageHeight = srcExtent.height; |
| 1529 | region.bufferOffset = stagingOffset; |
| 1530 | region.bufferRowLength = srcExtent.width; |
| 1531 | region.imageExtent = srcExtent; |
| 1532 | region.imageOffset = srcOffset; |
| 1533 | region.imageSubresource = srcSubresource; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1534 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 1535 | commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(), |
| 1536 | bufferHandle, 1, ®ion); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1537 | |
| 1538 | // Triggers a full finish. |
| 1539 | // TODO(jmadill): Don't block on asynchronous readback. |
Geoff Lang | 892d180 | 2019-03-27 14:21:34 -0400 | [diff] [blame] | 1540 | ANGLE_TRY(contextVk->finishImpl()); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1541 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 1542 | // The buffer we copied to needs to be invalidated before we read from it because its not been |
| 1543 | // created with the host coherent bit. |
Jamie Madill | d754eb5 | 2018-07-19 14:55:03 -0400 | [diff] [blame] | 1544 | ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk)); |
Yuly Novikov | 6c6c76c | 2018-05-17 18:45:06 +0000 | [diff] [blame] | 1545 | |
Ian Elliott | dc2c5c5 | 2019-08-02 09:32:18 -0600 | [diff] [blame] | 1546 | const gl::State &glState = contextVk->getState(); |
| 1547 | gl::Buffer *packBuffer = glState.getTargetBuffer(gl::BufferBinding::PixelPack); |
| 1548 | if (packBuffer != nullptr) |
| 1549 | { |
| 1550 | // Must map the PBO in order to read its contents (and then unmap it later) |
| 1551 | BufferVk *packBufferVk = vk::GetImpl(packBuffer); |
| 1552 | void *mapPtr = nullptr; |
| 1553 | ANGLE_TRY(packBufferVk->mapImpl(contextVk, &mapPtr)); |
| 1554 | uint8_t *dest = static_cast<uint8_t *>(mapPtr) + reinterpret_cast<ptrdiff_t>(pixels); |
| 1555 | PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, |
| 1556 | readPixelBuffer, static_cast<uint8_t *>(dest)); |
| 1557 | packBufferVk->unmapImpl(contextVk); |
| 1558 | } |
| 1559 | else |
| 1560 | { |
| 1561 | PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, |
| 1562 | readPixelBuffer, static_cast<uint8_t *>(pixels)); |
| 1563 | } |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1564 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1565 | return angle::Result::Continue; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 1566 | } |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1567 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1568 | gl::Extents FramebufferVk::getReadImageExtents() const |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1569 | { |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1570 | ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width); |
| 1571 | ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height); |
| 1572 | |
Shahbaz Youssefi | 6854940 | 2019-03-25 23:30:49 -0400 | [diff] [blame] | 1573 | return getColorReadRenderTarget()->getExtents(); |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 1574 | } |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 1575 | |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1576 | gl::Rectangle FramebufferVk::getCompleteRenderArea() const |
| 1577 | { |
Tim Van Patten | 626a728 | 2019-07-08 15:11:59 -0600 | [diff] [blame] | 1578 | const gl::Box &dimensions = mState.getDimensions(); |
| 1579 | return gl::Rectangle(0, 0, dimensions.width, dimensions.height); |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const |
| 1583 | { |
Tim Van Patten | 626a728 | 2019-07-08 15:11:59 -0600 | [diff] [blame] | 1584 | const gl::Box &dimensions = mState.getDimensions(); |
| 1585 | const gl::Rectangle renderArea(0, 0, dimensions.width, dimensions.height); |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1586 | bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO(); |
| 1587 | |
| 1588 | return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport); |
| 1589 | } |
| 1590 | |
| 1591 | void FramebufferVk::onScissorChange(ContextVk *contextVk) |
| 1592 | { |
| 1593 | gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk); |
| 1594 | |
| 1595 | // If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass |
| 1596 | // is restarted. Otherwise, we can continue using the same renderpass area. |
| 1597 | // |
| 1598 | // Without a scissor, the render pass area covers the whole of the framebuffer. With a |
| 1599 | // scissored clear, the render pass area could be smaller than the framebuffer size. When the |
| 1600 | // scissor changes, if the scissor area is completely encompassed by the render pass area, it's |
| 1601 | // possible to continue using the same render pass. However, if the current render pass area |
| 1602 | // is too small, we need to start a new one. The latter can happen if a scissored clear starts |
| 1603 | // a render pass, the scissor is disabled and a draw call is issued to affect the whole |
| 1604 | // framebuffer. |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1605 | mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial()); |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1606 | if (mFramebuffer.hasStartedRenderPass() && |
| 1607 | !mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea)) |
| 1608 | { |
Geoff Lang | ee244c7 | 2019-05-06 10:30:18 -0400 | [diff] [blame] | 1609 | mFramebuffer.finishCurrentCommands(contextVk); |
Shahbaz Youssefi | 127990f | 2019-04-04 13:52:04 -0400 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 1613 | RenderTargetVk *FramebufferVk::getFirstRenderTarget() const |
| 1614 | { |
| 1615 | for (auto *renderTarget : mRenderTargetCache.getColors()) |
| 1616 | { |
| 1617 | if (renderTarget) |
| 1618 | { |
| 1619 | return renderTarget; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | return mRenderTargetCache.getDepthStencil(); |
| 1624 | } |
| 1625 | |
| 1626 | GLint FramebufferVk::getSamples() const |
| 1627 | { |
| 1628 | RenderTargetVk *firstRT = getFirstRenderTarget(); |
| 1629 | return firstRT ? firstRT->getImage().getSamples() : 0; |
| 1630 | } |
| 1631 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1632 | } // namespace rx |