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