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