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