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 | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 16 | #include "image_util/imageformats.h" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 17 | #include "libANGLE/Context.h" |
| 18 | #include "libANGLE/Display.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 19 | #include "libANGLE/formatutils.h" |
| 20 | #include "libANGLE/renderer/renderer_utils.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 21 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 22 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 23 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 24 | #include "libANGLE/renderer/vulkan/RenderTargetVk.h" |
| 25 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
| 26 | #include "libANGLE/renderer/vulkan/SurfaceVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 27 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 28 | |
| 29 | namespace rx |
| 30 | { |
| 31 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 32 | namespace |
| 33 | { |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 34 | constexpr size_t kMinReadPixelsBufferSize = 128000; |
| 35 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 36 | const gl::InternalFormat &GetReadAttachmentInfo(const gl::Context *context, |
| 37 | RenderTargetVk *renderTarget) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 38 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 39 | GLenum implFormat = |
| 40 | renderTarget->image->getFormat().textureFormat().fboImplementationInternalFormat; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 41 | return gl::GetSizedInternalFormatInfo(implFormat); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 42 | } |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 43 | } // anonymous namespace< |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 44 | |
| 45 | // static |
| 46 | FramebufferVk *FramebufferVk::CreateUserFBO(const gl::FramebufferState &state) |
| 47 | { |
| 48 | return new FramebufferVk(state); |
| 49 | } |
| 50 | |
| 51 | // static |
| 52 | FramebufferVk *FramebufferVk::CreateDefaultFBO(const gl::FramebufferState &state, |
| 53 | WindowSurfaceVk *backbuffer) |
| 54 | { |
| 55 | return new FramebufferVk(state, backbuffer); |
| 56 | } |
| 57 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 58 | FramebufferVk::FramebufferVk(const gl::FramebufferState &state) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 59 | : FramebufferImpl(state), |
| 60 | mBackbuffer(nullptr), |
| 61 | mRenderPassDesc(), |
| 62 | mFramebuffer(), |
| 63 | mActiveColorComponents(0), |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 64 | mMaskedClearDescriptorSet(VK_NULL_HANDLE), |
| 65 | mReadPixelsBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT, kMinReadPixelsBufferSize) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 66 | { |
| 67 | } |
| 68 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 69 | FramebufferVk::FramebufferVk(const gl::FramebufferState &state, WindowSurfaceVk *backbuffer) |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 70 | : FramebufferImpl(state), |
| 71 | mBackbuffer(backbuffer), |
| 72 | mRenderPassDesc(), |
| 73 | mFramebuffer(), |
| 74 | mActiveColorComponents(0), |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 75 | mMaskedClearDescriptorSet(VK_NULL_HANDLE), |
| 76 | mReadPixelsBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT, kMinReadPixelsBufferSize) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 77 | { |
| 78 | } |
| 79 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 80 | FramebufferVk::~FramebufferVk() |
| 81 | { |
| 82 | } |
| 83 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 84 | void FramebufferVk::destroy(const gl::Context *context) |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 85 | { |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 86 | ContextVk *contextVk = vk::GetImpl(context); |
| 87 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 526543c | 2017-10-28 10:59:16 -0400 | [diff] [blame] | 88 | renderer->releaseResource(*this, &mFramebuffer); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 89 | renderer->releaseResource(*this, &mMaskedClearUniformBuffer.buffer); |
| 90 | renderer->releaseResource(*this, &mMaskedClearUniformBuffer.memory); |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 91 | |
| 92 | mReadPixelsBuffer.destroy(contextVk->getDevice()); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 95 | gl::Error FramebufferVk::discard(const gl::Context *context, |
| 96 | size_t count, |
| 97 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 98 | { |
| 99 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 100 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 103 | gl::Error FramebufferVk::invalidate(const gl::Context *context, |
| 104 | size_t count, |
| 105 | const GLenum *attachments) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 106 | { |
| 107 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 108 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 111 | gl::Error FramebufferVk::invalidateSub(const gl::Context *context, |
| 112 | size_t count, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 113 | const GLenum *attachments, |
| 114 | const gl::Rectangle &area) |
| 115 | { |
| 116 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 117 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 120 | gl::Error FramebufferVk::clear(const gl::Context *context, GLbitfield mask) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 121 | { |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 122 | ContextVk *contextVk = vk::GetImpl(context); |
| 123 | RendererVk *renderer = contextVk->getRenderer(); |
| 124 | Serial currentSerial = renderer->getCurrentQueueSerial(); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 125 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 126 | // This command buffer is only started once. |
| 127 | vk::CommandBuffer *commandBuffer = nullptr; |
| 128 | vk::CommandGraphNode *writingNode = nullptr; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 129 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 130 | const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment(); |
| 131 | bool clearDepth = (depthAttachment && (mask & GL_DEPTH_BUFFER_BIT) != 0); |
| 132 | ASSERT(!clearDepth || depthAttachment->isAttached()); |
| 133 | |
| 134 | const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment(); |
| 135 | bool clearStencil = (stencilAttachment && (mask & GL_STENCIL_BUFFER_BIT) != 0); |
| 136 | ASSERT(!clearStencil || stencilAttachment->isAttached()); |
| 137 | |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 138 | bool clearColor = IsMaskFlagSet(static_cast<int>(mask), GL_COLOR_BUFFER_BIT); |
| 139 | |
Luc Ferron | 8836f63 | 2018-04-05 07:26:53 -0400 | [diff] [blame] | 140 | const gl::FramebufferAttachment *depthStencilAttachment = mState.getDepthStencilAttachment(); |
Jamie Madill | d47044a | 2018-04-27 11:45:03 -0400 | [diff] [blame] | 141 | const gl::State &glState = context->getGLState(); |
Luc Ferron | 8836f63 | 2018-04-05 07:26:53 -0400 | [diff] [blame] | 142 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 143 | // The most costly clear mode is when we need to mask out specific color channels. This can |
| 144 | // only be done with a draw call. The scissor region however can easily be integrated with |
| 145 | // this method. Similarly for depth/stencil clear. |
| 146 | VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask(); |
| 147 | if (clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents) |
| 148 | { |
Jamie Madill | 78cd940 | 2018-05-10 16:49:58 -0400 | [diff] [blame] | 149 | ANGLE_TRY(clearWithDraw(contextVk, colorMaskFlags)); |
| 150 | |
| 151 | // Stencil clears must be handled separately. The only way to write out a stencil value from |
| 152 | // a fragment shader in Vulkan is with VK_EXT_shader_stencil_export. Support for this |
| 153 | // extension is sparse. Hence, we call into the RenderPass clear path. We similarly clear |
| 154 | // depth to keep the code simple, but depth clears could be combined with the masked color |
| 155 | // clears as an optimization. |
| 156 | |
| 157 | if (clearDepth || clearStencil) |
| 158 | { |
| 159 | // Masked stencil clears are currently not implemented. |
| 160 | // TODO(jmadill): Masked stencil clear. http://anglebug.com/2540 |
| 161 | ANGLE_TRY(clearWithClearAttachments(contextVk, false, clearDepth, clearStencil)); |
| 162 | } |
| 163 | return gl::NoError(); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Luc Ferron | 8836f63 | 2018-04-05 07:26:53 -0400 | [diff] [blame] | 166 | // If we clear the depth OR the stencil but not both, and we have a packed depth stencil |
| 167 | // attachment, we need to use clearAttachment instead of clearDepthStencil since Vulkan won't |
| 168 | // allow us to clear one or the other separately. |
| 169 | bool isSingleClearOnPackedDepthStencilAttachment = |
| 170 | depthStencilAttachment && (clearDepth != clearStencil); |
Jamie Madill | d47044a | 2018-04-27 11:45:03 -0400 | [diff] [blame] | 171 | if (glState.isScissorTestEnabled() || isSingleClearOnPackedDepthStencilAttachment) |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 172 | { |
| 173 | // With scissor test enabled, we clear very differently and we don't need to access |
| 174 | // the image inside each attachment we can just use clearCmdAttachments with our |
| 175 | // scissor region instead. |
Jamie Madill | 78cd940 | 2018-05-10 16:49:58 -0400 | [diff] [blame] | 176 | |
| 177 | // Masked stencil clears are currently not implemented. |
| 178 | // TODO(jmadill): Masked stencil clear. http://anglebug.com/2540 |
Jamie Madill | b90779e | 2018-04-27 11:45:01 -0400 | [diff] [blame] | 179 | ANGLE_TRY(clearWithClearAttachments(contextVk, clearColor, clearDepth, clearStencil)); |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 180 | return gl::NoError(); |
| 181 | } |
| 182 | |
| 183 | // Standard Depth/stencil clear without scissor. |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 184 | if (clearDepth || clearStencil) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 185 | { |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 186 | ANGLE_TRY(beginWriteResource(renderer, &commandBuffer)); |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 187 | writingNode = getCurrentWritingNode(); |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 188 | |
| 189 | const VkClearDepthStencilValue &clearDepthStencilValue = |
| 190 | contextVk->getClearDepthStencilValue().depthStencil; |
| 191 | |
| 192 | // We only support packed depth/stencil, not separate. |
Luc Ferron | 8836f63 | 2018-04-05 07:26:53 -0400 | [diff] [blame] | 193 | ASSERT(!(clearDepth && clearStencil) || depthStencilAttachment); |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 194 | |
Luc Ferron | e6a40d0 | 2018-03-22 10:30:57 -0400 | [diff] [blame] | 195 | const VkImageAspectFlags aspectFlags = |
| 196 | (depthAttachment ? VK_IMAGE_ASPECT_DEPTH_BIT : 0) | |
| 197 | (stencilAttachment ? VK_IMAGE_ASPECT_STENCIL_BIT : 0); |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 198 | |
| 199 | RenderTargetVk *renderTarget = mRenderTargetCache.getDepthStencil(); |
| 200 | renderTarget->resource->onWriteResource(writingNode, currentSerial); |
Jamie Madill | 858c1cc | 2018-03-31 14:19:13 -0400 | [diff] [blame] | 201 | renderTarget->image->clearDepthStencil(aspectFlags, clearDepthStencilValue, commandBuffer); |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 202 | |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 203 | if (!clearColor) |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 204 | { |
| 205 | return gl::NoError(); |
| 206 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 209 | ASSERT(clearColor); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 210 | const auto *attachment = mState.getFirstNonNullAttachment(); |
| 211 | ASSERT(attachment && attachment->isAttached()); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 212 | |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 213 | if (!commandBuffer) |
| 214 | { |
| 215 | ANGLE_TRY(beginWriteResource(renderer, &commandBuffer)); |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 216 | writingNode = getCurrentWritingNode(); |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 217 | } |
Jamie Madill | efb5a5c | 2018-01-29 15:56:59 -0500 | [diff] [blame] | 218 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 219 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 220 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 221 | const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color; |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 222 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 223 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 224 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex]; |
| 225 | ASSERT(colorRenderTarget); |
Jamie Madill | 0cec82a | 2018-03-14 09:21:07 -0400 | [diff] [blame] | 226 | colorRenderTarget->resource->onWriteResource(writingNode, currentSerial); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 227 | colorRenderTarget->image->clearColor(clearColorValue, commandBuffer); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 230 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 233 | gl::Error FramebufferVk::clearBufferfv(const gl::Context *context, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 234 | GLenum buffer, |
| 235 | GLint drawbuffer, |
| 236 | const GLfloat *values) |
| 237 | { |
| 238 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 239 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 242 | gl::Error FramebufferVk::clearBufferuiv(const gl::Context *context, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 243 | GLenum buffer, |
| 244 | GLint drawbuffer, |
| 245 | const GLuint *values) |
| 246 | { |
| 247 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 248 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 251 | gl::Error FramebufferVk::clearBufferiv(const gl::Context *context, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 252 | GLenum buffer, |
| 253 | GLint drawbuffer, |
| 254 | const GLint *values) |
| 255 | { |
| 256 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 257 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 260 | gl::Error FramebufferVk::clearBufferfi(const gl::Context *context, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 261 | GLenum buffer, |
| 262 | GLint drawbuffer, |
| 263 | GLfloat depth, |
| 264 | GLint stencil) |
| 265 | { |
| 266 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 267 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 270 | GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 271 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 272 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 275 | GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 276 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 277 | return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 280 | gl::Error FramebufferVk::readPixels(const gl::Context *context, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 281 | const gl::Rectangle &area, |
| 282 | GLenum format, |
| 283 | GLenum type, |
Jamie Madill | d482615 | 2017-09-21 11:18:59 -0400 | [diff] [blame] | 284 | void *pixels) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 285 | { |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 286 | // Clip read area to framebuffer. |
| 287 | const gl::Extents &fbSize = getState().getReadAttachment()->getSize(); |
| 288 | const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height); |
| 289 | gl::Rectangle clippedArea; |
| 290 | if (!ClipRectangle(area, fbRect, &clippedArea)) |
| 291 | { |
| 292 | // nothing to read |
| 293 | return gl::NoError(); |
| 294 | } |
| 295 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 296 | const gl::State &glState = context->getGLState(); |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 297 | RendererVk *renderer = vk::GetImpl(context)->getRenderer(); |
| 298 | |
| 299 | vk::CommandBuffer *commandBuffer = nullptr; |
| 300 | ANGLE_TRY(beginWriteResource(renderer, &commandBuffer)); |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 301 | |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 302 | const gl::PixelPackState &packState = context->getGLState().getPackState(); |
| 303 | const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type); |
| 304 | |
| 305 | GLuint outputPitch = 0; |
| 306 | ANGLE_TRY_RESULT( |
| 307 | sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment, packState.rowLength), |
| 308 | outputPitch); |
| 309 | GLuint outputSkipBytes = 0; |
Jeff Gilbert | 31d3deb | 2018-05-18 18:32:16 -0700 | [diff] [blame^] | 310 | ANGLE_TRY_RESULT(sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState, false), |
Luc Ferron | a1c7242 | 2018-05-14 15:58:28 -0400 | [diff] [blame] | 311 | outputSkipBytes); |
| 312 | |
| 313 | outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes + |
| 314 | (clippedArea.y - area.y) * outputPitch; |
Luc Ferron | 6028422 | 2018-03-20 16:01:44 -0400 | [diff] [blame] | 315 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 316 | PackPixelsParams params; |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 317 | params.area = clippedArea; |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 318 | params.format = format; |
| 319 | params.type = type; |
Luc Ferron | 6028422 | 2018-03-20 16:01:44 -0400 | [diff] [blame] | 320 | params.outputPitch = outputPitch; |
Corentin Wallez | 336129f | 2017-10-17 15:55:40 -0400 | [diff] [blame] | 321 | params.packBuffer = glState.getTargetBuffer(gl::BufferBinding::PixelPack); |
Corentin Wallez | cda6af1 | 2017-10-30 19:20:37 -0400 | [diff] [blame] | 322 | params.pack = glState.getPackState(); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 323 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 324 | if (!mReadPixelsBuffer.valid()) |
| 325 | { |
| 326 | mReadPixelsBuffer.init(1, renderer); |
| 327 | ASSERT(mReadPixelsBuffer.valid()); |
| 328 | } |
| 329 | |
| 330 | ANGLE_TRY(ReadPixelsFromRenderTarget(context, clippedArea, params, mReadPixelsBuffer, |
| 331 | getColorReadRenderTarget(), commandBuffer, |
| 332 | reinterpret_cast<uint8_t *>(pixels) + outputSkipBytes)); |
| 333 | mReadPixelsBuffer.releaseRetainedBuffers(renderer); |
| 334 | return gl::NoError(); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 335 | } |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 336 | |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 337 | RenderTargetVk *FramebufferVk::getColorReadRenderTarget() |
| 338 | { |
| 339 | RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState); |
| 340 | ASSERT(renderTarget && renderTarget->image->valid()); |
| 341 | return renderTarget; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 344 | gl::Error FramebufferVk::blit(const gl::Context *context, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 345 | const gl::Rectangle &sourceArea, |
| 346 | const gl::Rectangle &destArea, |
| 347 | GLbitfield mask, |
| 348 | GLenum filter) |
| 349 | { |
| 350 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 351 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Kenneth Russell | ce8602a | 2017-10-03 18:23:08 -0700 | [diff] [blame] | 354 | bool FramebufferVk::checkStatus(const gl::Context *context) const |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 355 | { |
Jamie Madill | b79e7bb | 2017-10-24 13:55:50 -0400 | [diff] [blame] | 356 | return true; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 359 | gl::Error FramebufferVk::syncState(const gl::Context *context, |
| 360 | const gl::Framebuffer::DirtyBits &dirtyBits) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 361 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 362 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 7bd1666 | 2017-10-28 19:40:50 -0400 | [diff] [blame] | 363 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 364 | |
| 365 | ASSERT(dirtyBits.any()); |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 366 | for (size_t dirtyBit : dirtyBits) |
| 367 | { |
| 368 | switch (dirtyBit) |
| 369 | { |
| 370 | case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT: |
| 371 | case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT: |
| 372 | ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState)); |
| 373 | break; |
| 374 | case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS: |
| 375 | case gl::Framebuffer::DIRTY_BIT_READ_BUFFER: |
| 376 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH: |
| 377 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT: |
| 378 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES: |
| 379 | case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS: |
| 380 | break; |
| 381 | default: |
| 382 | { |
| 383 | ASSERT(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 && |
| 384 | dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX); |
| 385 | size_t colorIndex = |
| 386 | static_cast<size_t>(dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0); |
| 387 | ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndex)); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 388 | |
| 389 | // Update cached masks for masked clears. |
| 390 | RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndex]; |
| 391 | if (renderTarget) |
| 392 | { |
| 393 | const angle::Format &format = renderTarget->image->getFormat().textureFormat(); |
| 394 | updateActiveColorMasks(colorIndex, format.redBits > 0, format.greenBits > 0, |
| 395 | format.blueBits > 0, format.alphaBits > 0); |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | updateActiveColorMasks(colorIndex, 0, 0, 0, 0); |
| 400 | } |
Jamie Madill | 57d9cbb | 2018-04-27 11:45:04 -0400 | [diff] [blame] | 401 | break; |
| 402 | } |
| 403 | } |
| 404 | } |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 405 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 406 | mActiveColorComponents = gl_vk::GetColorComponentFlags( |
| 407 | mActiveColorComponentMasks[0].any(), mActiveColorComponentMasks[1].any(), |
| 408 | mActiveColorComponentMasks[2].any(), mActiveColorComponentMasks[3].any()); |
| 409 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 410 | mRenderPassDesc.reset(); |
Jamie Madill | 7bd1666 | 2017-10-28 19:40:50 -0400 | [diff] [blame] | 411 | renderer->releaseResource(*this, &mFramebuffer); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 412 | |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 413 | // Trigger a new set of secondary commands next time we render to this FBO. |
| 414 | getNewWritingNode(renderer); |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 415 | |
Jamie Madill | 7210656 | 2017-03-24 14:18:50 -0400 | [diff] [blame] | 416 | contextVk->invalidateCurrentPipeline(); |
Jamie Madill | 19fa1c6 | 2018-03-08 09:47:21 -0500 | [diff] [blame] | 417 | |
| 418 | return gl::NoError(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 419 | } |
| 420 | |
Jamie Madill | b90779e | 2018-04-27 11:45:01 -0400 | [diff] [blame] | 421 | const vk::RenderPassDesc &FramebufferVk::getRenderPassDesc() |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 422 | { |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 423 | if (mRenderPassDesc.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 424 | { |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 425 | return mRenderPassDesc.value(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Jamie Madill | 0b684ce | 2017-11-23 12:57:39 -0500 | [diff] [blame] | 428 | vk::RenderPassDesc desc; |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 429 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 430 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 431 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 432 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 433 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 434 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex]; |
| 435 | ASSERT(colorRenderTarget); |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 436 | desc.packColorAttachment(*colorRenderTarget->image); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 437 | } |
| 438 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 439 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 440 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 441 | { |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 442 | desc.packDepthStencilAttachment(*depthStencilRenderTarget->image); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 443 | } |
| 444 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 445 | mRenderPassDesc = desc; |
| 446 | return mRenderPassDesc.value(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Jamie Madill | b90779e | 2018-04-27 11:45:01 -0400 | [diff] [blame] | 449 | gl::ErrorOrResult<vk::Framebuffer *> FramebufferVk::getFramebuffer(RendererVk *rendererVk) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 450 | { |
| 451 | // If we've already created our cached Framebuffer, return it. |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 452 | if (mFramebuffer.valid()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 453 | { |
| 454 | return &mFramebuffer; |
| 455 | } |
| 456 | |
Jamie Madill | b90779e | 2018-04-27 11:45:01 -0400 | [diff] [blame] | 457 | const vk::RenderPassDesc &desc = getRenderPassDesc(); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 458 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 459 | vk::RenderPass *renderPass = nullptr; |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 460 | ANGLE_TRY(rendererVk->getCompatibleRenderPass(desc, &renderPass)); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 461 | |
| 462 | // 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] | 463 | VkDevice device = rendererVk->getDevice(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 464 | if (mBackbuffer) |
| 465 | { |
| 466 | return mBackbuffer->getCurrentFramebuffer(device, *renderPass); |
| 467 | } |
| 468 | |
| 469 | // Gather VkImageViews over all FBO attachments, also size of attached region. |
| 470 | std::vector<VkImageView> attachments; |
| 471 | gl::Extents attachmentsSize; |
| 472 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 473 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 474 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 475 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 476 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 477 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex]; |
| 478 | ASSERT(colorRenderTarget); |
| 479 | attachments.push_back(colorRenderTarget->imageView->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 480 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 481 | ASSERT(attachmentsSize.empty() || |
| 482 | attachmentsSize == colorRenderTarget->image->getExtents()); |
| 483 | attachmentsSize = colorRenderTarget->image->getExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 486 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 487 | if (depthStencilRenderTarget) |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 488 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 489 | attachments.push_back(depthStencilRenderTarget->imageView->getHandle()); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 490 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 491 | ASSERT(attachmentsSize.empty() || |
| 492 | attachmentsSize == depthStencilRenderTarget->image->getExtents()); |
| 493 | attachmentsSize = depthStencilRenderTarget->image->getExtents(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | ASSERT(!attachments.empty()); |
| 497 | |
| 498 | VkFramebufferCreateInfo framebufferInfo; |
| 499 | |
| 500 | framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; |
| 501 | framebufferInfo.pNext = nullptr; |
| 502 | framebufferInfo.flags = 0; |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 503 | framebufferInfo.renderPass = renderPass->getHandle(); |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 504 | framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); |
| 505 | framebufferInfo.pAttachments = attachments.data(); |
| 506 | framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width); |
| 507 | framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height); |
| 508 | framebufferInfo.layers = 1; |
| 509 | |
Jamie Madill | 25301b6 | 2017-10-28 20:59:31 -0400 | [diff] [blame] | 510 | ANGLE_TRY(mFramebuffer.init(device, framebufferInfo)); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 511 | |
Jamie Madill | ab9f9c3 | 2017-01-17 17:47:34 -0500 | [diff] [blame] | 512 | return &mFramebuffer; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 513 | } |
| 514 | |
Jamie Madill | b90779e | 2018-04-27 11:45:01 -0400 | [diff] [blame] | 515 | gl::Error FramebufferVk::clearWithClearAttachments(ContextVk *contextVk, |
| 516 | bool clearColor, |
| 517 | bool clearDepth, |
| 518 | bool clearStencil) |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 519 | { |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 520 | RendererVk *renderer = contextVk->getRenderer(); |
| 521 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 522 | getNewWritingNode(renderer); |
| 523 | |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 524 | // This command can only happen inside a render pass, so obtain one if its already happening |
| 525 | // or create a new one if not. |
| 526 | vk::CommandGraphNode *node = nullptr; |
| 527 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | b90779e | 2018-04-27 11:45:01 -0400 | [diff] [blame] | 528 | ANGLE_TRY(getCommandGraphNodeForDraw(contextVk, &node)); |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 529 | if (node->getInsideRenderPassCommands()->valid()) |
| 530 | { |
| 531 | commandBuffer = node->getInsideRenderPassCommands(); |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | ANGLE_TRY(node->beginInsideRenderPassRecording(renderer, &commandBuffer)); |
| 536 | } |
| 537 | |
Luc Ferron | e4c38be | 2018-04-17 11:09:16 -0400 | [diff] [blame] | 538 | // TODO(jmadill): Cube map attachments. http://anglebug.com/2470 |
| 539 | // We assume for now that we always need to clear only 1 layer starting at the |
| 540 | // baseArrayLayer 0, this might need to change depending how we'll implement |
| 541 | // cube maps, 3d textures and array textures. |
| 542 | VkClearRect clearRect; |
| 543 | clearRect.baseArrayLayer = 0; |
| 544 | clearRect.layerCount = 1; |
| 545 | |
| 546 | // When clearing, the scissor region must be clipped to the renderArea per the validation rules |
| 547 | // in Vulkan. |
| 548 | gl::Rectangle intersection; |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 549 | if (!gl::ClipRectangle(contextVk->getGLState().getScissor(), node->getRenderPassRenderArea(), |
| 550 | &intersection)) |
Luc Ferron | e4c38be | 2018-04-17 11:09:16 -0400 | [diff] [blame] | 551 | { |
| 552 | // There is nothing to clear since the scissor is outside of the render area. |
| 553 | return gl::NoError(); |
| 554 | } |
Luc Ferron | e4c38be | 2018-04-17 11:09:16 -0400 | [diff] [blame] | 555 | clearRect.rect = gl_vk::GetRect(intersection); |
| 556 | |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 557 | gl::AttachmentArray<VkClearAttachment> clearAttachments; |
| 558 | int clearAttachmentIndex = 0; |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 559 | |
| 560 | if (clearColor) |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 561 | { |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 562 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 563 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
| 564 | { |
| 565 | VkClearAttachment &clearAttachment = clearAttachments[clearAttachmentIndex]; |
| 566 | clearAttachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 567 | clearAttachment.colorAttachment = static_cast<uint32_t>(colorIndex); |
| 568 | clearAttachment.clearValue = contextVk->getClearColorValue(); |
| 569 | ++clearAttachmentIndex; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if (clearDepth && clearStencil && mState.getDepthStencilAttachment() != nullptr) |
| 574 | { |
| 575 | // When we have a packed depth/stencil attachment we can do 1 clear for both when it |
| 576 | // applies. |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 577 | VkClearAttachment &clearAttachment = clearAttachments[clearAttachmentIndex]; |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 578 | clearAttachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 579 | clearAttachment.colorAttachment = VK_ATTACHMENT_UNUSED; |
| 580 | clearAttachment.clearValue = contextVk->getClearDepthStencilValue(); |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 581 | ++clearAttachmentIndex; |
| 582 | } |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 583 | else |
| 584 | { |
| 585 | if (clearDepth) |
| 586 | { |
| 587 | VkClearAttachment &clearAttachment = clearAttachments[clearAttachmentIndex]; |
| 588 | clearAttachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; |
| 589 | clearAttachment.colorAttachment = VK_ATTACHMENT_UNUSED; |
| 590 | clearAttachment.clearValue = contextVk->getClearDepthStencilValue(); |
| 591 | ++clearAttachmentIndex; |
| 592 | } |
| 593 | |
| 594 | if (clearStencil) |
| 595 | { |
| 596 | VkClearAttachment &clearAttachment = clearAttachments[clearAttachmentIndex]; |
| 597 | clearAttachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 598 | clearAttachment.colorAttachment = VK_ATTACHMENT_UNUSED; |
| 599 | clearAttachment.clearValue = contextVk->getClearDepthStencilValue(); |
| 600 | ++clearAttachmentIndex; |
| 601 | } |
| 602 | } |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 603 | |
Luc Ferron | a8af3a6 | 2018-03-29 14:44:24 -0400 | [diff] [blame] | 604 | commandBuffer->clearAttachments(static_cast<uint32_t>(clearAttachmentIndex), |
Luc Ferron | 5242d5b | 2018-02-15 07:14:35 -0500 | [diff] [blame] | 605 | clearAttachments.data(), 1, &clearRect); |
| 606 | return gl::NoError(); |
| 607 | } |
| 608 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 609 | gl::Error FramebufferVk::clearWithDraw(ContextVk *contextVk, VkColorComponentFlags colorMaskFlags) |
| 610 | { |
| 611 | RendererVk *renderer = contextVk->getRenderer(); |
| 612 | vk::ShaderLibrary *shaderLibrary = renderer->getShaderLibrary(); |
| 613 | |
| 614 | const vk::ShaderAndSerial *fullScreenQuad = nullptr; |
| 615 | ANGLE_TRY(shaderLibrary->getShader(renderer, vk::InternalShaderID::FullScreenQuad_vert, |
| 616 | &fullScreenQuad)); |
| 617 | |
| 618 | const vk::ShaderAndSerial *uniformColor = nullptr; |
| 619 | ANGLE_TRY( |
| 620 | shaderLibrary->getShader(renderer, vk::InternalShaderID::UniformColor_frag, &uniformColor)); |
| 621 | |
| 622 | const vk::PipelineLayout *pipelineLayout = nullptr; |
| 623 | ANGLE_TRY(renderer->getInternalUniformPipelineLayout(&pipelineLayout)); |
| 624 | |
| 625 | vk::CommandBuffer *commandBuffer = nullptr; |
| 626 | ANGLE_TRY(beginWriteResource(renderer, &commandBuffer)); |
| 627 | |
| 628 | vk::CommandGraphNode *node = nullptr; |
| 629 | ANGLE_TRY(getCommandGraphNodeForDraw(contextVk, &node)); |
| 630 | |
| 631 | // This pipeline desc could be cached. |
| 632 | vk::PipelineDesc pipelineDesc; |
| 633 | pipelineDesc.initDefaults(); |
| 634 | pipelineDesc.updateColorWriteMask(colorMaskFlags); |
| 635 | pipelineDesc.updateRenderPassDesc(getRenderPassDesc()); |
| 636 | pipelineDesc.updateShaders(fullScreenQuad->queueSerial(), uniformColor->queueSerial()); |
| 637 | pipelineDesc.updateViewport(node->getRenderPassRenderArea(), 0.0f, 1.0f); |
| 638 | |
| 639 | const gl::State &glState = contextVk->getGLState(); |
| 640 | if (glState.isScissorTestEnabled()) |
| 641 | { |
| 642 | gl::Rectangle intersection; |
| 643 | if (!gl::ClipRectangle(glState.getScissor(), node->getRenderPassRenderArea(), |
| 644 | &intersection)) |
| 645 | { |
| 646 | return gl::NoError(); |
| 647 | } |
| 648 | |
| 649 | pipelineDesc.updateScissor(intersection); |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | pipelineDesc.updateScissor(node->getRenderPassRenderArea()); |
| 654 | } |
| 655 | |
| 656 | vk::PipelineAndSerial *pipeline = nullptr; |
| 657 | ANGLE_TRY(renderer->getInternalPipeline(*fullScreenQuad, *uniformColor, *pipelineLayout, |
| 658 | pipelineDesc, gl::AttributesMask(), &pipeline)); |
| 659 | pipeline->updateSerial(renderer->getCurrentQueueSerial()); |
| 660 | |
| 661 | VkDevice device = renderer->getDevice(); |
| 662 | |
| 663 | if (!mMaskedClearUniformBuffer.buffer.valid()) |
| 664 | { |
| 665 | ASSERT(mMaskedClearDescriptorSet == VK_NULL_HANDLE); |
| 666 | |
| 667 | VkBufferUsageFlags bufferUsage = |
| 668 | (VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT); |
| 669 | |
| 670 | VkBufferCreateInfo uniformBufferInfo; |
| 671 | uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 672 | uniformBufferInfo.pNext = nullptr; |
| 673 | uniformBufferInfo.flags = 0; |
| 674 | uniformBufferInfo.size = sizeof(VkClearColorValue); |
| 675 | uniformBufferInfo.usage = bufferUsage; |
| 676 | uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 677 | uniformBufferInfo.queueFamilyIndexCount = 0; |
| 678 | uniformBufferInfo.pQueueFamilyIndices = nullptr; |
| 679 | |
| 680 | ANGLE_TRY(mMaskedClearUniformBuffer.buffer.init(device, uniformBufferInfo)); |
| 681 | |
| 682 | VkMemoryPropertyFlags memoryFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 683 | size_t requiredSize = 0; |
| 684 | ANGLE_TRY(vk::AllocateBufferMemory(renderer, memoryFlags, &mMaskedClearUniformBuffer.buffer, |
| 685 | &mMaskedClearUniformBuffer.memory, &requiredSize)); |
| 686 | |
| 687 | const vk::DescriptorSetLayout &descriptorSetLayout = |
| 688 | renderer->getInternalUniformDescriptorSetLayout(); |
| 689 | |
| 690 | // This might confuse the dynamic descriptor pool's counting, but it shouldn't cause |
| 691 | // overflow. |
| 692 | vk::DynamicDescriptorPool *descriptorPool = contextVk->getDynamicDescriptorPool(); |
| 693 | descriptorPool->allocateDescriptorSets(contextVk, descriptorSetLayout.ptr(), 1, |
| 694 | &mMaskedClearDescriptorSet); |
| 695 | |
| 696 | VkDescriptorBufferInfo bufferInfo; |
| 697 | bufferInfo.buffer = mMaskedClearUniformBuffer.buffer.getHandle(); |
| 698 | bufferInfo.offset = 0; |
| 699 | bufferInfo.range = VK_WHOLE_SIZE; |
| 700 | |
| 701 | VkWriteDescriptorSet writeSet; |
| 702 | writeSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 703 | writeSet.pNext = nullptr; |
| 704 | writeSet.dstSet = mMaskedClearDescriptorSet; |
| 705 | writeSet.dstBinding = 1; |
| 706 | writeSet.dstArrayElement = 0; |
| 707 | writeSet.descriptorCount = 1; |
| 708 | writeSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC; |
| 709 | writeSet.pImageInfo = nullptr; |
| 710 | writeSet.pBufferInfo = &bufferInfo; |
| 711 | writeSet.pTexelBufferView = nullptr; |
| 712 | |
| 713 | vkUpdateDescriptorSets(device, 1, &writeSet, 0, nullptr); |
| 714 | } |
| 715 | |
| 716 | VkClearColorValue clearColorValue = contextVk->getClearColorValue().color; |
| 717 | commandBuffer->updateBuffer(mMaskedClearUniformBuffer.buffer, 0, sizeof(VkClearColorValue), |
| 718 | clearColorValue.float32); |
| 719 | |
| 720 | vk::CommandBuffer *drawCommandBuffer = nullptr; |
| 721 | ANGLE_TRY(node->beginInsideRenderPassRecording(renderer, &drawCommandBuffer)); |
| 722 | |
| 723 | std::array<uint32_t, 2> dynamicOffsets = {{0, 0}}; |
| 724 | drawCommandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, *pipelineLayout, 0, 1, |
| 725 | &mMaskedClearDescriptorSet, 2, dynamicOffsets.data()); |
| 726 | |
| 727 | // TODO(jmadill): Masked combined color and depth/stencil clear. http://anglebug.com/2455 |
| 728 | // Any active queries submitted by the user should also be paused here. |
| 729 | drawCommandBuffer->bindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->get()); |
| 730 | drawCommandBuffer->draw(6, 1, 0, 0); |
| 731 | |
| 732 | return gl::NoError(); |
| 733 | } |
| 734 | |
Geoff Lang | 1345507 | 2018-05-09 11:24:43 -0400 | [diff] [blame] | 735 | gl::Error FramebufferVk::getSamplePosition(const gl::Context *context, |
| 736 | size_t index, |
| 737 | GLfloat *xy) const |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 738 | { |
| 739 | UNIMPLEMENTED(); |
| 740 | return gl::InternalError() << "getSamplePosition is unimplemented."; |
| 741 | } |
| 742 | |
Jamie Madill | b90779e | 2018-04-27 11:45:01 -0400 | [diff] [blame] | 743 | gl::Error FramebufferVk::getCommandGraphNodeForDraw(ContextVk *contextVk, |
Jamie Madill | e4c5a23 | 2018-03-02 21:00:31 -0500 | [diff] [blame] | 744 | vk::CommandGraphNode **nodeOut) |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 745 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 746 | RendererVk *renderer = contextVk->getRenderer(); |
| 747 | Serial currentSerial = renderer->getCurrentQueueSerial(); |
Jamie Madill | bef918c | 2017-12-13 13:11:30 -0500 | [diff] [blame] | 748 | |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 749 | // This will reset the current writing node if it has been completed. |
| 750 | updateQueueSerial(currentSerial); |
| 751 | |
| 752 | if (hasChildlessWritingNode()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 753 | { |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 754 | *nodeOut = getCurrentWritingNode(); |
| 755 | } |
| 756 | else |
| 757 | { |
| 758 | *nodeOut = getNewWritingNode(renderer); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 759 | } |
| 760 | |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 761 | if ((*nodeOut)->getInsideRenderPassCommands()->valid()) |
| 762 | { |
| 763 | return gl::NoError(); |
| 764 | } |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 765 | |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 766 | vk::Framebuffer *framebuffer = nullptr; |
Jamie Madill | b90779e | 2018-04-27 11:45:01 -0400 | [diff] [blame] | 767 | ANGLE_TRY_RESULT(getFramebuffer(renderer), framebuffer); |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 768 | |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 769 | std::vector<VkClearValue> attachmentClearValues; |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 770 | |
Jamie Madill | e4c5a23 | 2018-03-02 21:00:31 -0500 | [diff] [blame] | 771 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 772 | if (!(*nodeOut)->getOutsideRenderPassCommands()->valid()) |
| 773 | { |
| 774 | ANGLE_TRY((*nodeOut)->beginOutsideRenderPassRecording( |
| 775 | renderer->getDevice(), renderer->getCommandPool(), &commandBuffer)); |
| 776 | } |
| 777 | else |
| 778 | { |
| 779 | commandBuffer = (*nodeOut)->getOutsideRenderPassCommands(); |
| 780 | } |
Jamie Madill | e4c5a23 | 2018-03-02 21:00:31 -0500 | [diff] [blame] | 781 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 782 | // Initialize RenderPass info. |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 783 | // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394 |
| 784 | const auto &colorRenderTargets = mRenderTargetCache.getColors(); |
| 785 | for (size_t colorIndex : mState.getEnabledDrawBuffers()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 786 | { |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 787 | RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndex]; |
| 788 | ASSERT(colorRenderTarget); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 789 | |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 790 | (*nodeOut)->appendColorRenderTarget(currentSerial, colorRenderTarget); |
Jamie Madill | 66546be | 2018-03-08 09:47:20 -0500 | [diff] [blame] | 791 | attachmentClearValues.emplace_back(contextVk->getClearColorValue()); |
| 792 | } |
| 793 | |
| 794 | RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil(); |
| 795 | if (depthStencilRenderTarget) |
| 796 | { |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 797 | (*nodeOut)->appendDepthStencilRenderTarget(currentSerial, depthStencilRenderTarget); |
Jamie Madill | f4d693c | 2018-02-14 16:38:16 -0500 | [diff] [blame] | 798 | attachmentClearValues.emplace_back(contextVk->getClearDepthStencilValue()); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 799 | } |
| 800 | |
Luc Ferron | d17bdfe | 2018-04-05 13:50:10 -0400 | [diff] [blame] | 801 | gl::Rectangle renderArea = |
| 802 | gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height); |
Jamie Madill | f4d693c | 2018-02-14 16:38:16 -0500 | [diff] [blame] | 803 | // Hard-code RenderPass to clear the first render target to the current clear value. |
| 804 | // TODO(jmadill): Proper clear value implementation. http://anglebug.com/2361 |
Luc Ferron | d17bdfe | 2018-04-05 13:50:10 -0400 | [diff] [blame] | 805 | (*nodeOut)->storeRenderPassInfo(*framebuffer, renderArea, attachmentClearValues); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 806 | |
Jamie Madill | df68a6f | 2017-01-13 17:29:53 -0500 | [diff] [blame] | 807 | return gl::NoError(); |
| 808 | } |
| 809 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 810 | void FramebufferVk::updateActiveColorMasks(size_t colorIndex, bool r, bool g, bool b, bool a) |
| 811 | { |
| 812 | mActiveColorComponentMasks[0].set(colorIndex, r); |
| 813 | mActiveColorComponentMasks[1].set(colorIndex, g); |
| 814 | mActiveColorComponentMasks[2].set(colorIndex, b); |
| 815 | mActiveColorComponentMasks[3].set(colorIndex, a); |
| 816 | } |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 817 | |
| 818 | gl::Error ReadPixelsFromRenderTarget(const gl::Context *context, |
| 819 | const gl::Rectangle &area, |
| 820 | const PackPixelsParams &packPixelsParams, |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 821 | vk::DynamicBuffer &dynamicBuffer, |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 822 | RenderTargetVk *renderTarget, |
| 823 | vk::CommandBuffer *commandBuffer, |
| 824 | void *pixels) |
| 825 | { |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 826 | RendererVk *renderer = vk::GetImpl(context)->getRenderer(); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 827 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 828 | vk::ImageHelper *renderTargetImage = renderTarget->image; |
| 829 | const angle::Format &angleFormat = renderTargetImage->getFormat().textureFormat(); |
| 830 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 831 | uint8_t *readPixelBuffer = nullptr; |
| 832 | bool newBufferAllocated = false; |
| 833 | uint32_t stagingOffset = 0; |
| 834 | size_t allocationSize = area.width * angleFormat.pixelBytes * area.height; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 835 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 836 | dynamicBuffer.allocate(renderer, allocationSize, &readPixelBuffer, &bufferHandle, |
| 837 | &stagingOffset, &newBufferAllocated); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 838 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 839 | VkBufferImageCopy region; |
| 840 | region.bufferImageHeight = area.height; |
| 841 | region.bufferOffset = static_cast<VkDeviceSize>(stagingOffset); |
| 842 | region.bufferRowLength = area.width; |
| 843 | region.imageExtent.width = area.width; |
| 844 | region.imageExtent.height = area.height; |
| 845 | region.imageExtent.depth = 1; |
| 846 | region.imageOffset.x = area.x; |
| 847 | region.imageOffset.y = area.y; |
| 848 | region.imageOffset.z = 0; |
| 849 | region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 850 | region.imageSubresource.baseArrayLayer = 0; |
| 851 | region.imageSubresource.layerCount = 1; |
| 852 | region.imageSubresource.mipLevel = 0; |
| 853 | |
| 854 | renderTargetImage->changeLayoutWithStages( |
| 855 | VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 856 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer); |
| 857 | |
| 858 | commandBuffer->copyImageToBuffer(renderTargetImage->getImage(), |
| 859 | renderTargetImage->getCurrentLayout(), bufferHandle, 1, |
| 860 | ®ion); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 861 | |
| 862 | // Triggers a full finish. |
| 863 | // TODO(jmadill): Don't block on asynchronous readback. |
| 864 | ANGLE_TRY(renderer->finish(context)); |
| 865 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 866 | // The buffer we copied to needs to be invalidated before we read from it because its not been |
| 867 | // created with the host coherent bit. |
| 868 | ANGLE_TRY(dynamicBuffer.invalidate(renderer->getDevice())); |
Yuly Novikov | 6c6c76c | 2018-05-17 18:45:06 +0000 | [diff] [blame] | 869 | |
Luc Ferron | 534b00d | 2018-05-18 08:16:53 -0400 | [diff] [blame] | 870 | PackPixels(packPixelsParams, angleFormat, area.width * angleFormat.pixelBytes, readPixelBuffer, |
| 871 | reinterpret_cast<uint8_t *>(pixels)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 872 | |
| 873 | return vk::NoError(); |
| 874 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 875 | } // namespace rx |