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