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