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