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