blob: 42e7446a707194d9ad99769721297adbd4e40cbf [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
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 Madill7b57b9d2017-01-13 09:33:38 -050012#include <vulkan/vulkan.h>
Jamie Madill231c7f52017-04-26 13:45:37 -040013#include <array>
Jamie Madill7b57b9d2017-01-13 09:33:38 -050014
Jamie Madill9e54b5a2016-05-25 12:57:39 -040015#include "common/debug.h"
Jamie Madillc564c072017-06-01 12:45:42 -040016#include "libANGLE/Context.h"
17#include "libANGLE/Display.h"
Jamie Madill7b57b9d2017-01-13 09:33:38 -050018#include "libANGLE/formatutils.h"
19#include "libANGLE/renderer/renderer_utils.h"
Jamie Madill1f46bc12018-02-20 16:09:43 -050020#include "libANGLE/renderer/vulkan/CommandGraph.h"
Jamie Madill7b57b9d2017-01-13 09:33:38 -050021#include "libANGLE/renderer/vulkan/ContextVk.h"
Jamie Madill5deea722017-02-16 10:44:46 -050022#include "libANGLE/renderer/vulkan/DisplayVk.h"
Jamie Madill7b57b9d2017-01-13 09:33:38 -050023#include "libANGLE/renderer/vulkan/RenderTargetVk.h"
24#include "libANGLE/renderer/vulkan/RendererVk.h"
25#include "libANGLE/renderer/vulkan/SurfaceVk.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050026#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madill3ea463b2019-06-19 14:21:33 -040027#include "libANGLE/trace.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040028
29namespace rx
30{
31
Jamie Madill7b57b9d2017-01-13 09:33:38 -050032namespace
33{
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -040034// The value to assign an alpha channel that's emulated. The type is unsigned int, though it will
35// automatically convert to the actual data type.
36constexpr unsigned int kEmulatedAlphaValue = 1;
37
Luc Ferron534b00d2018-05-18 08:16:53 -040038constexpr size_t kMinReadPixelsBufferSize = 128000;
Mohan Maiya8a43b8c2019-08-02 08:47:13 -070039
40// Alignment value to accommodate the largest known, for now, uncompressed Vulkan format
Shahbaz Youssefic9724352019-08-20 14:55:43 -040041// VK_FORMAT_R64G64B64A64_SFLOAT, while supporting 3-component types such as
42// VK_FORMAT_R16G16B16_SFLOAT.
43constexpr size_t kReadPixelsBufferAlignment = 32 * 3;
Mohan Maiya8a43b8c2019-08-02 08:47:13 -070044
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -040045// Clear values are only used when loadOp=Clear is set in clearWithRenderPassOp. When starting a
46// new render pass, the clear value is set to an unlikely value (bright pink) to stand out better
47// in case of a bug.
48constexpr VkClearValue kUninitializedClearValue = {{{0.95, 0.05, 0.95, 0.95}}};
Luc Ferron534b00d2018-05-18 08:16:53 -040049
Jamie Madill66546be2018-03-08 09:47:20 -050050const gl::InternalFormat &GetReadAttachmentInfo(const gl::Context *context,
51 RenderTargetVk *renderTarget)
Jamie Madill7b57b9d2017-01-13 09:33:38 -050052{
Jamie Madillbc543422018-03-30 10:43:19 -040053 GLenum implFormat =
Jamie Madill0631e192019-04-18 16:09:12 -040054 renderTarget->getImageFormat().imageFormat().fboImplementationInternalFormat;
Jamie Madill66546be2018-03-08 09:47:20 -050055 return gl::GetSizedInternalFormatInfo(implFormat);
Jamie Madill7b57b9d2017-01-13 09:33:38 -050056}
Luc Ferron26581112018-06-21 09:43:08 -040057
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040058bool HasSrcBlitFeature(RendererVk *renderer, RenderTargetVk *srcRenderTarget)
Luc Ferron26581112018-06-21 09:43:08 -040059{
Jamie Madill0631e192019-04-18 16:09:12 -040060 const VkFormat srcFormat = srcRenderTarget->getImageFormat().vkImageFormat;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040061 return renderer->hasImageFormatFeatureBits(srcFormat, VK_FORMAT_FEATURE_BLIT_SRC_BIT);
62}
Luc Ferron26581112018-06-21 09:43:08 -040063
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040064bool HasDstBlitFeature(RendererVk *renderer, RenderTargetVk *dstRenderTarget)
65{
66 const VkFormat dstFormat = dstRenderTarget->getImageFormat().vkImageFormat;
67 return renderer->hasImageFormatFeatureBits(dstFormat, VK_FORMAT_FEATURE_BLIT_DST_BIT);
68}
69
70// Returns false if destination has any channel the source doesn't. This means that channel was
71// emulated and using the Vulkan blit command would overwrite that emulated channel.
72bool areSrcAndDstColorChannelsBlitCompatible(RenderTargetVk *srcRenderTarget,
73 RenderTargetVk *dstRenderTarget)
74{
75 const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat();
76 const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat();
77
78 // Luminance/alpha formats are not renderable, so they can't have ended up in a framebuffer to
79 // participate in a blit.
80 ASSERT(!dstFormat.isLUMA() && !srcFormat.isLUMA());
81
82 // All color formats have the red channel.
83 ASSERT(dstFormat.redBits > 0 && srcFormat.redBits > 0);
84
85 return (dstFormat.greenBits > 0 || srcFormat.greenBits == 0) &&
86 (dstFormat.blueBits > 0 || srcFormat.blueBits == 0) &&
87 (dstFormat.alphaBits > 0 || srcFormat.alphaBits == 0);
88}
89
90bool areSrcAndDstDepthStencilChannelsBlitCompatible(RenderTargetVk *srcRenderTarget,
91 RenderTargetVk *dstRenderTarget)
92{
93 const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat();
94 const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat();
95
96 return (dstFormat.depthBits > 0 || srcFormat.depthBits == 0) &&
97 (dstFormat.stencilBits > 0 || srcFormat.stencilBits == 0);
Luc Ferron26581112018-06-21 09:43:08 -040098}
Jamie Madillb436aac2018-07-18 17:23:48 -040099
100// Special rules apply to VkBufferImageCopy with depth/stencil. The components are tightly packed
101// into a depth or stencil section of the destination buffer. See the spec:
102// https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkBufferImageCopy.html
103const angle::Format &GetDepthStencilImageToBufferFormat(const angle::Format &imageFormat,
104 VkImageAspectFlagBits copyAspect)
105{
106 if (copyAspect == VK_IMAGE_ASPECT_STENCIL_BIT)
107 {
108 ASSERT(imageFormat.id == angle::FormatID::D24_UNORM_S8_UINT ||
109 imageFormat.id == angle::FormatID::D32_FLOAT_S8X24_UINT ||
110 imageFormat.id == angle::FormatID::S8_UINT);
111 return angle::Format::Get(angle::FormatID::S8_UINT);
112 }
113
114 ASSERT(copyAspect == VK_IMAGE_ASPECT_DEPTH_BIT);
115
116 switch (imageFormat.id)
117 {
118 case angle::FormatID::D16_UNORM:
119 return imageFormat;
120 case angle::FormatID::D24_UNORM_X8_UINT:
121 return imageFormat;
122 case angle::FormatID::D24_UNORM_S8_UINT:
123 return angle::Format::Get(angle::FormatID::D24_UNORM_X8_UINT);
124 case angle::FormatID::D32_FLOAT:
125 return imageFormat;
126 case angle::FormatID::D32_FLOAT_S8X24_UINT:
127 return angle::Format::Get(angle::FormatID::D32_FLOAT);
128 default:
129 UNREACHABLE();
130 return imageFormat;
131 }
132}
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400133
134void SetEmulatedAlphaValue(const vk::Format &format, VkClearColorValue *value)
135{
136 if (format.vkFormatIsInt)
137 {
138 if (format.vkFormatIsUnsigned)
139 {
140 value->uint32[3] = kEmulatedAlphaValue;
141 }
142 else
143 {
144 value->int32[3] = kEmulatedAlphaValue;
145 }
146 }
147 else
148 {
149 value->float32[3] = kEmulatedAlphaValue;
150 }
151}
Jamie Madillbcf467f2018-05-23 09:46:00 -0400152} // anonymous namespace
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500153
154// static
Jamie Madill639bc902018-07-18 17:08:27 -0400155FramebufferVk *FramebufferVk::CreateUserFBO(RendererVk *renderer, const gl::FramebufferState &state)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500156{
Jamie Madill639bc902018-07-18 17:08:27 -0400157 return new FramebufferVk(renderer, state, nullptr);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500158}
159
160// static
Jamie Madill639bc902018-07-18 17:08:27 -0400161FramebufferVk *FramebufferVk::CreateDefaultFBO(RendererVk *renderer,
162 const gl::FramebufferState &state,
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500163 WindowSurfaceVk *backbuffer)
164{
Jamie Madill639bc902018-07-18 17:08:27 -0400165 return new FramebufferVk(renderer, state, backbuffer);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500166}
167
Jamie Madill639bc902018-07-18 17:08:27 -0400168FramebufferVk::FramebufferVk(RendererVk *renderer,
169 const gl::FramebufferState &state,
170 WindowSurfaceVk *backbuffer)
Jamie Madill7f2520f2019-06-26 11:18:33 -0400171 : FramebufferImpl(state), mBackbuffer(backbuffer), mActiveColorComponents(0)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500172{
Mohan Maiya8a43b8c2019-08-02 08:47:13 -0700173 mReadPixelBuffer.init(renderer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, kReadPixelsBufferAlignment,
174 kMinReadPixelsBufferSize, true);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500175}
176
Jamie Madill58675012018-05-22 14:54:07 -0400177FramebufferVk::~FramebufferVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400178
Jamie Madillc564c072017-06-01 12:45:42 -0400179void FramebufferVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500180{
Luc Ferron534b00d2018-05-18 08:16:53 -0400181 ContextVk *contextVk = vk::GetImpl(context);
Geoff Langee244c72019-05-06 10:30:18 -0400182 mFramebuffer.release(contextVk);
Luc Ferron534b00d2018-05-18 08:16:53 -0400183
Jamie Madillf10bf6b2019-09-26 10:27:18 -0400184 mReadPixelBuffer.release(contextVk->getRenderer());
Jamie Madill5deea722017-02-16 10:44:46 -0500185}
186
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400187angle::Result FramebufferVk::discard(const gl::Context *context,
188 size_t count,
189 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400190{
Shahbaz Youssefi97123e32019-07-08 15:11:16 -0400191 return invalidate(context, count, attachments);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400192}
193
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400194angle::Result FramebufferVk::invalidate(const gl::Context *context,
195 size_t count,
196 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400197{
Jamie Madilld7f28aa2019-09-19 14:19:10 -0400198 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillb540da82019-09-19 14:19:12 -0400199 mFramebuffer.onGraphAccess(contextVk->getCommandGraph());
Shahbaz Youssefida904482019-07-02 10:49:14 -0400200
201 if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass())
202 {
Jamie Madilld7f28aa2019-09-19 14:19:10 -0400203 invalidateImpl(contextVk, count, attachments);
Shahbaz Youssefida904482019-07-02 10:49:14 -0400204 }
205
206 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400207}
208
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400209angle::Result FramebufferVk::invalidateSub(const gl::Context *context,
210 size_t count,
211 const GLenum *attachments,
212 const gl::Rectangle &area)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400213{
Jamie Madilld7f28aa2019-09-19 14:19:10 -0400214 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillb540da82019-09-19 14:19:12 -0400215 mFramebuffer.onGraphAccess(contextVk->getCommandGraph());
Shahbaz Youssefida904482019-07-02 10:49:14 -0400216
217 // RenderPass' storeOp cannot be made conditional to a specific region, so we only apply this
218 // hint if the requested area encompasses the render area.
219 if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass() &&
220 area.encloses(mFramebuffer.getRenderPassRenderArea()))
221 {
Jamie Madilld7f28aa2019-09-19 14:19:10 -0400222 invalidateImpl(contextVk, count, attachments);
Shahbaz Youssefida904482019-07-02 10:49:14 -0400223 }
224
225 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400226}
227
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400228angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400229{
Jamie Madill0cec82a2018-03-14 09:21:07 -0400230 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500231
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400232 bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT));
233 bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT));
234 bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT));
235 gl::DrawBufferMask clearColorBuffers;
236 if (clearColor)
237 {
238 clearColorBuffers = mState.getEnabledDrawBuffers();
239 }
240
241 const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color;
242 const VkClearDepthStencilValue &clearDepthStencilValue =
243 contextVk->getClearDepthStencilValue().depthStencil;
244
245 return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue,
246 clearDepthStencilValue);
247}
248
249angle::Result FramebufferVk::clearImpl(const gl::Context *context,
250 gl::DrawBufferMask clearColorBuffers,
251 bool clearDepth,
252 bool clearStencil,
253 const VkClearColorValue &clearColorValue,
254 const VkClearDepthStencilValue &clearDepthStencilValue)
255{
256 ContextVk *contextVk = vk::GetImpl(context);
257
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400258 const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
259
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400260 // Discard clear altogether if scissor has 0 width or height.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400261 if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400262 {
263 return angle::Result::Continue;
264 }
265
Jamie Madillf10bf6b2019-09-26 10:27:18 -0400266 mFramebuffer.updateCurrentAccessNodes();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400267
268 // This function assumes that only enabled attachments are asked to be cleared.
269 ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers);
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400270
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400271 // Adjust clear behavior based on whether the respective attachments are present; if asked to
272 // clear a non-existent attachment, don't attempt to clear it.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400273
274 VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask();
275 bool clearColor = clearColorBuffers.any();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400276
Jamie Madill0cec82a2018-03-14 09:21:07 -0400277 const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400278 clearDepth = clearDepth && depthAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400279 ASSERT(!clearDepth || depthAttachment->isAttached());
280
281 const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400282 clearStencil = clearStencil && stencilAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400283 ASSERT(!clearStencil || stencilAttachment->isAttached());
284
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400285 uint8_t stencilMask =
286 static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask);
287
288 // The front-end should ensure we don't attempt to clear color if all channels are masked.
289 ASSERT(!clearColor || colorMaskFlags != 0);
290 // The front-end should ensure we don't attempt to clear depth if depth write is disabled.
291 ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask);
292 // The front-end should ensure we don't attempt to clear stencil if all bits are masked.
293 ASSERT(!clearStencil || stencilMask != 0);
294
295 // If there is nothing to clear, return right away (for example, if asked to clear depth, but
296 // there is no depth attachment).
Shahbaz Youssefi02a579e2019-03-27 14:21:20 -0400297 if (!clearColor && !clearDepth && !clearStencil)
298 {
299 return angle::Result::Continue;
300 }
301
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400302 VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue;
Shahbaz Youssefid856ca42018-10-31 16:55:12 -0400303
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400304 // We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If
305 // there's a depth mask, depth clearing is already disabled.
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400306 bool maskedClearColor =
307 clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400308 bool maskedClearStencil = stencilMask != 0xFF;
309
310 bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor;
311 bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400312
313 // At least one of color, depth or stencil should be clearable with render pass loadOp for us
314 // to use this clear path.
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400315 bool clearAnyWithRenderPassLoadOp =
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400316 clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400317
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400318 if (clearAnyWithRenderPassLoadOp)
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400319 {
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400320 // Clearing color is indicated by the set bits in this mask. If not clearing colors with
321 // render pass loadOp, the default value of all-zeros means the clear is not done in
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400322 // clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass
323 // loadOp.
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400324 gl::DrawBufferMask clearBuffersWithRenderPassLoadOp;
325 if (clearColorWithRenderPassLoadOp)
326 {
327 clearBuffersWithRenderPassLoadOp = clearColorBuffers;
328 }
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400329 ANGLE_TRY(clearWithRenderPassOp(
330 contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth,
331 clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue));
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400332
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400333 // On some hardware, having inline commands at this point results in corrupted output. In
334 // that case, end the render pass immediately. http://anglebug.com/2361
Jonah Ryan-Davis776694c2019-05-08 10:28:55 -0400335 if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled)
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400336 {
Geoff Langee244c72019-05-06 10:30:18 -0400337 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400338 }
339
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400340 // Fallback to other methods for whatever isn't cleared here.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400341 clearDepth = false;
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400342 if (clearColorWithRenderPassLoadOp)
343 {
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400344 clearColorBuffers.reset();
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400345 clearColor = false;
346 }
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400347 if (clearStencilWithRenderPassLoadOp)
348 {
349 clearStencil = false;
350 }
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400351
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400352 // If nothing left to clear, early out.
353 if (!clearColor && !clearStencil)
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400354 {
355 return angle::Result::Continue;
356 }
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400357 }
358
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400359 // Note: depth clear is always done through render pass loadOp.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400360 ASSERT(clearDepth == false);
361
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400362 // The most costly clear mode is when we need to mask out specific color channels or stencil
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400363 // bits. This can only be done with a draw call.
364 return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil,
365 colorMaskFlags, stencilMask, clearColorValue,
366 static_cast<uint8_t>(modifiedDepthStencilValue.stencil));
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400367}
368
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400369angle::Result FramebufferVk::clearBufferfv(const gl::Context *context,
370 GLenum buffer,
371 GLint drawbuffer,
372 const GLfloat *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400373{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400374 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400375
376 bool clearDepth = false;
377 gl::DrawBufferMask clearColorBuffers;
378
379 if (buffer == GL_DEPTH)
380 {
381 clearDepth = true;
382 clearValue.depthStencil.depth = values[0];
383 }
384 else
385 {
386 clearColorBuffers.set(drawbuffer);
387 clearValue.color.float32[0] = values[0];
388 clearValue.color.float32[1] = values[1];
389 clearValue.color.float32[2] = values[2];
390 clearValue.color.float32[3] = values[3];
391 }
392
393 return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color,
394 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400395}
396
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400397angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context,
398 GLenum buffer,
399 GLint drawbuffer,
400 const GLuint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400401{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400402 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400403
404 gl::DrawBufferMask clearColorBuffers;
405 clearColorBuffers.set(drawbuffer);
406
407 clearValue.color.uint32[0] = values[0];
408 clearValue.color.uint32[1] = values[1];
409 clearValue.color.uint32[2] = values[2];
410 clearValue.color.uint32[3] = values[3];
411
412 return clearImpl(context, clearColorBuffers, false, false, clearValue.color,
413 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400414}
415
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400416angle::Result FramebufferVk::clearBufferiv(const gl::Context *context,
417 GLenum buffer,
418 GLint drawbuffer,
419 const GLint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400420{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400421 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400422
423 bool clearStencil = false;
424 gl::DrawBufferMask clearColorBuffers;
425
426 if (buffer == GL_STENCIL)
427 {
428 clearStencil = true;
429 clearValue.depthStencil.stencil =
430 gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max());
431 }
432 else
433 {
434 clearColorBuffers.set(drawbuffer);
435 clearValue.color.int32[0] = values[0];
436 clearValue.color.int32[1] = values[1];
437 clearValue.color.int32[2] = values[2];
438 clearValue.color.int32[3] = values[3];
439 }
440
441 return clearImpl(context, clearColorBuffers, false, clearStencil, clearValue.color,
442 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400443}
444
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400445angle::Result FramebufferVk::clearBufferfi(const gl::Context *context,
446 GLenum buffer,
447 GLint drawbuffer,
448 GLfloat depth,
449 GLint stencil)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400450{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400451 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400452
453 clearValue.depthStencil.depth = depth;
454 clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max());
455
456 return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color,
457 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400458}
459
Jamie Madill4928b7c2017-06-20 12:57:39 -0400460GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400461{
Jamie Madill66546be2018-03-08 09:47:20 -0500462 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400463}
464
Jamie Madill4928b7c2017-06-20 12:57:39 -0400465GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400466{
Mohan Maiya6caa2652019-09-11 08:06:13 -0700467 GLenum readType = GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type;
468 if (context->getClientMajorVersion() < 3 && readType == GL_HALF_FLOAT)
469 {
470 // GL_HALF_FLOAT was not introduced until GLES 3.0, and has a different value from
471 // GL_HALF_FLOAT_OES
472 readType = GL_HALF_FLOAT_OES;
473 }
474 return readType;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400475}
476
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400477angle::Result FramebufferVk::readPixels(const gl::Context *context,
478 const gl::Rectangle &area,
479 GLenum format,
480 GLenum type,
481 void *pixels)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400482{
Luc Ferrona1c72422018-05-14 15:58:28 -0400483 // Clip read area to framebuffer.
484 const gl::Extents &fbSize = getState().getReadAttachment()->getSize();
485 const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400486 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400487
Luc Ferrona1c72422018-05-14 15:58:28 -0400488 gl::Rectangle clippedArea;
489 if (!ClipRectangle(area, fbRect, &clippedArea))
490 {
491 // nothing to read
Jamie Madill7c985f52018-11-29 18:16:17 -0500492 return angle::Result::Continue;
Luc Ferrona1c72422018-05-14 15:58:28 -0400493 }
Luc Ferronbf6dc372018-06-28 15:24:19 -0400494 gl::Rectangle flippedArea = clippedArea;
Geoff Lange076a232018-07-16 15:34:05 -0400495 if (contextVk->isViewportFlipEnabledForReadFBO())
Luc Ferronbf6dc372018-06-28 15:24:19 -0400496 {
497 flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height;
498 }
Luc Ferrona1c72422018-05-14 15:58:28 -0400499
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500500 const gl::State &glState = context->getState();
Frank Henigman1ffad842018-09-24 23:40:45 -0400501 const gl::PixelPackState &packState = glState.getPackState();
Luc Ferronbf6dc372018-06-28 15:24:19 -0400502
Luc Ferrona1c72422018-05-14 15:58:28 -0400503 const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type);
504
505 GLuint outputPitch = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400506 ANGLE_VK_CHECK_MATH(contextVk,
507 sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment,
508 packState.rowLength, &outputPitch));
Luc Ferrona1c72422018-05-14 15:58:28 -0400509 GLuint outputSkipBytes = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400510 ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState,
511 false, &outputSkipBytes));
Luc Ferrona1c72422018-05-14 15:58:28 -0400512
513 outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes +
514 (clippedArea.y - area.y) * outputPitch;
Luc Ferron60284222018-03-20 16:01:44 -0400515
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400516 const angle::Format &angleFormat = GetFormatFromFormatType(format, type);
517
Frank Henigman1ffad842018-09-24 23:40:45 -0400518 PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder,
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400519 glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0);
Frank Henigman1ffad842018-09-24 23:40:45 -0400520 if (contextVk->isViewportFlipEnabledForReadFBO())
521 {
522 params.reverseRowOrder = !params.reverseRowOrder;
523 }
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500524
Jamie Madill21061022018-07-12 23:56:30 -0400525 ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400526 getColorReadRenderTarget(),
Rafael Cintron05a449a2018-06-20 18:08:04 -0700527 static_cast<uint8_t *>(pixels) + outputSkipBytes));
Jamie Madillc773ab92019-06-25 17:11:58 -0400528 mReadPixelBuffer.releaseInFlightBuffers(contextVk);
Jamie Madill7c985f52018-11-29 18:16:17 -0500529 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -0400530}
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500531
Luc Ferron26581112018-06-21 09:43:08 -0400532RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const
533{
534 return mRenderTargetCache.getDepthStencil();
535}
536
Tim Van Patten56ba54c2019-08-08 13:03:34 -0600537RenderTargetVk *FramebufferVk::getColorDrawRenderTarget(size_t colorIndex) const
538{
539 RenderTargetVk *renderTarget = mRenderTargetCache.getColorDraw(mState, colorIndex);
540 ASSERT(renderTarget && renderTarget->getImage().valid());
541 return renderTarget;
542}
543
Jamie Madill58675012018-05-22 14:54:07 -0400544RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const
Luc Ferron018709f2018-05-10 13:53:11 -0400545{
546 RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400547 ASSERT(renderTarget && renderTarget->getImage().valid());
Luc Ferron018709f2018-05-10 13:53:11 -0400548 return renderTarget;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400549}
550
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400551angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400552 const gl::Rectangle &sourceArea,
553 const gl::Rectangle &destArea,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400554 RenderTargetVk *readRenderTarget,
555 RenderTargetVk *drawRenderTarget,
556 GLenum filter,
557 bool colorBlit,
558 bool depthBlit,
559 bool stencilBlit,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400560 bool flipX,
561 bool flipY)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400562{
563 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
564 // it should never be the case that both color and depth/stencil need to be blitted at
565 // at the same time.
566 ASSERT(colorBlit != (depthBlit || stencilBlit));
567
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400568 vk::ImageHelper *srcImage = &readRenderTarget->getImage();
Jamie Madilld7f28aa2019-09-19 14:19:10 -0400569 vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(contextVk, &mFramebuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400570
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400571 VkImageAspectFlags imageAspectMask = srcImage->getAspectFlags();
572 VkImageAspectFlags blitAspectMask = imageAspectMask;
573
574 // Remove depth or stencil aspects if they are not requested to be blitted.
575 if (!depthBlit)
576 {
577 blitAspectMask &= ~VK_IMAGE_ASPECT_DEPTH_BIT;
578 }
579 if (!stencilBlit)
580 {
581 blitAspectMask &= ~VK_IMAGE_ASPECT_STENCIL_BIT;
582 }
583
584 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
585 {
586 vk::CommandBuffer *srcLayoutChange;
587 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
588 srcImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferSrc, srcLayoutChange);
589 }
590
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400591 vk::CommandBuffer *commandBuffer = nullptr;
592 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
593
Jamie Madilld7f28aa2019-09-19 14:19:10 -0400594 srcImage->addReadDependency(contextVk, &mFramebuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400595
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400596 VkImageBlit blit = {};
597 blit.srcSubresource.aspectMask = blitAspectMask;
598 blit.srcSubresource.mipLevel = readRenderTarget->getLevelIndex();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400599 blit.srcSubresource.baseArrayLayer = readRenderTarget->getLayerIndex();
600 blit.srcSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400601 blit.srcOffsets[0] = {sourceArea.x0(), sourceArea.y0(), 0};
602 blit.srcOffsets[1] = {sourceArea.x1(), sourceArea.y1(), 1};
603 blit.dstSubresource.aspectMask = blitAspectMask;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400604 blit.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
605 blit.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
606 blit.dstSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400607 blit.dstOffsets[0] = {destArea.x0(), destArea.y0(), 0};
608 blit.dstOffsets[1] = {destArea.x1(), destArea.y1(), 1};
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400609
610 // Requirement of the copyImageToBuffer, the dst image must be in
611 // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400612 dstImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferDst, commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400613
614 commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
615 dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
616 gl_vk::GetFilter(filter));
617
618 return angle::Result::Continue;
619}
620
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400621angle::Result FramebufferVk::blit(const gl::Context *context,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400622 const gl::Rectangle &sourceAreaIn,
623 const gl::Rectangle &destAreaIn,
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400624 GLbitfield mask,
625 GLenum filter)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400626{
Luc Ferron26581112018-06-21 09:43:08 -0400627 ContextVk *contextVk = vk::GetImpl(context);
628 RendererVk *renderer = contextVk->getRenderer();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400629 UtilsVk &utilsVk = contextVk->getUtils();
Luc Ferron26581112018-06-21 09:43:08 -0400630
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400631 const gl::State &glState = contextVk->getState();
632 const gl::Framebuffer *srcFramebuffer = glState.getReadFramebuffer();
633
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400634 const bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0;
635 const bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0;
636 const bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400637
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400638 const bool isResolve = srcFramebuffer->getCachedSamples(context) > 1;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400639
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400640 FramebufferVk *srcFramebufferVk = vk::GetImpl(srcFramebuffer);
641 const bool srcFramebufferFlippedY = contextVk->isViewportFlipEnabledForReadFBO();
642 const bool destFramebufferFlippedY = contextVk->isViewportFlipEnabledForDrawFBO();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400643
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400644 gl::Rectangle sourceArea = sourceAreaIn;
645 gl::Rectangle destArea = destAreaIn;
646
647 // Note: GLES (all 3.x versions) require source and dest area to be identical when
648 // resolving.
649 ASSERT(!isResolve ||
650 (sourceArea.x == destArea.x && sourceArea.y == destArea.y &&
651 sourceArea.width == destArea.width && sourceArea.height == destArea.height));
652
653 const gl::Rectangle srcFramebufferDimensions =
654 srcFramebufferVk->mState.getDimensions().toRect();
655
656 // If the destination is flipped in either direction, we will flip the source instead so that
657 // the destination area is always unflipped.
658 sourceArea = sourceArea.flip(destArea.isReversedX(), destArea.isReversedY());
659 destArea = destArea.removeReversal();
660
661 // Calculate the stretch factor prior to any clipping, as it needs to remain constant.
662 const float stretch[2] = {
663 std::abs(sourceArea.width / static_cast<float>(destArea.width)),
664 std::abs(sourceArea.height / static_cast<float>(destArea.height)),
665 };
666
667 // First, clip the source area to framebuffer. That requires transforming the dest area to
668 // match the clipped source.
669 gl::Rectangle absSourceArea = sourceArea.removeReversal();
670 gl::Rectangle clippedSourceArea;
671 if (!gl::ClipRectangle(srcFramebufferDimensions, absSourceArea, &clippedSourceArea))
672 {
673 return angle::Result::Continue;
674 }
675
676 // Resize the destination area based on the new size of source. Note again that stretch is
677 // calculated as SrcDimension/DestDimension.
678 gl::Rectangle srcClippedDestArea;
679 if (isResolve)
680 {
681 // Source and dest areas are identical in resolve.
682 srcClippedDestArea = clippedSourceArea;
683 }
684 else if (clippedSourceArea == absSourceArea)
685 {
686 // If there was no clipping, keep dest area as is.
687 srcClippedDestArea = destArea;
688 }
689 else
690 {
691 // Shift dest area's x0,y0,x1,y1 by as much as the source area's got shifted (taking
692 // stretching into account)
693 float x0Shift = std::round((clippedSourceArea.x - absSourceArea.x) / stretch[0]);
694 float y0Shift = std::round((clippedSourceArea.y - absSourceArea.y) / stretch[1]);
695 float x1Shift = std::round((absSourceArea.x1() - clippedSourceArea.x1()) / stretch[0]);
696 float y1Shift = std::round((absSourceArea.y1() - clippedSourceArea.y1()) / stretch[1]);
697
698 // If the source area was reversed in any direction, the shift should be applied in the
699 // opposite direction as well.
700 if (sourceArea.isReversedX())
701 {
702 std::swap(x0Shift, x1Shift);
703 }
704
705 if (sourceArea.isReversedY())
706 {
707 std::swap(y0Shift, y1Shift);
708 }
709
710 srcClippedDestArea.x = destArea.x0() + static_cast<int>(x0Shift);
711 srcClippedDestArea.y = destArea.y0() + static_cast<int>(y0Shift);
712 int x1 = destArea.x1() - static_cast<int>(x1Shift);
713 int y1 = destArea.y1() - static_cast<int>(y1Shift);
714
715 srcClippedDestArea.width = x1 - srcClippedDestArea.x;
716 srcClippedDestArea.height = y1 - srcClippedDestArea.y;
717 }
718
719 // If framebuffers are flipped in Y, flip the source and dest area (which define the
720 // transformation regardless of clipping), as well as the blit area (which is the clipped
721 // dest area).
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400722 if (srcFramebufferFlippedY)
723 {
724 sourceArea.y = srcFramebufferDimensions.height - sourceArea.y;
725 sourceArea.height = -sourceArea.height;
726 }
727 if (destFramebufferFlippedY)
728 {
729 destArea.y = mState.getDimensions().height - destArea.y;
730 destArea.height = -destArea.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400731
732 srcClippedDestArea.y =
733 mState.getDimensions().height - srcClippedDestArea.y - srcClippedDestArea.height;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400734 }
735
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400736 const bool flipX = sourceArea.isReversedX() != destArea.isReversedX();
737 const bool flipY = sourceArea.isReversedY() != destArea.isReversedY();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400738
739 // GLES doesn't allow flipping the parameters of glBlitFramebuffer if performing a resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400740 ASSERT(!isResolve ||
741 (flipX == false && flipY == (srcFramebufferFlippedY != destFramebufferFlippedY)));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400742
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400743 // Again, transfer the destination flip to source, so dest is unflipped. Note that destArea
744 // was not reversed until the final possible Y-flip.
745 ASSERT(!destArea.isReversedX());
746 sourceArea = sourceArea.flip(false, destArea.isReversedY());
747 destArea = destArea.removeReversal();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400748
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400749 // Clip the destination area to the framebuffer size and scissor. Note that we don't care
750 // about the source area anymore. The offset translation is done based on the original source
751 // and destination rectangles. The stretch factor is already calculated as well.
752 gl::Rectangle blitArea;
753 if (!gl::ClipRectangle(getScissoredRenderArea(contextVk), srcClippedDestArea, &blitArea))
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400754 {
755 return angle::Result::Continue;
756 }
757
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400758 bool noClip = blitArea == destArea && stretch[0] == 1.0f && stretch[1] == 1.0f;
759 bool noFlip = !flipX && !flipY;
760 bool disableFlippingBlitWithCommand =
761 contextVk->getRenderer()->getFeatures().disableFlippingBlitWithCommand.enabled;
762
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400763 UtilsVk::BlitResolveParameters params;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400764 params.srcOffset[0] = sourceArea.x;
765 params.srcOffset[1] = sourceArea.y;
766 params.destOffset[0] = destArea.x;
767 params.destOffset[1] = destArea.y;
768 params.stretch[0] = stretch[0];
769 params.stretch[1] = stretch[1];
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400770 params.srcExtents[0] = srcFramebufferDimensions.width;
771 params.srcExtents[1] = srcFramebufferDimensions.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400772 params.blitArea = blitArea;
773 params.linear = filter == GL_LINEAR;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400774 params.flipX = flipX;
775 params.flipY = flipY;
776
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400777 if (blitColorBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400778 {
779 RenderTargetVk *readRenderTarget = srcFramebufferVk->getColorReadRenderTarget();
780 params.srcLayer = readRenderTarget->getLayerIndex();
781
782 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400783 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400784
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400785 // If there was no clipping and the format capabilities allow us, use Vulkan's builtin blit.
786 // The reason clipping is prohibited in this path is that due to rounding errors, it would
787 // be hard to guarantee the image stretching remains perfect. That also allows us not to
788 // have to transform back the dest clipping to source.
789 //
790 // For simplicity, we either blit all render targets with a Vulkan command, or none.
791 bool canBlitWithCommand = !isResolve && noClip &&
792 (noFlip || !disableFlippingBlitWithCommand) &&
793 HasSrcBlitFeature(renderer, readRenderTarget);
794 bool areChannelsBlitCompatible = true;
795 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
796 {
797 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
798 canBlitWithCommand =
799 canBlitWithCommand && HasDstBlitFeature(renderer, drawRenderTarget);
800 areChannelsBlitCompatible =
801 areChannelsBlitCompatible &&
802 areSrcAndDstColorChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
803 }
804
805 if (canBlitWithCommand && areChannelsBlitCompatible)
806 {
807 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
808 {
809 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
810 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
811 drawRenderTarget, filter, true, false, false, flipX,
812 flipY));
813 }
814 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400815 // If we're not flipping, use Vulkan's builtin resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400816 else if (isResolve && !flipX && !flipY && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400817 {
818 ANGLE_TRY(resolveColorWithCommand(contextVk, params, &readRenderTarget->getImage()));
819 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400820 // Otherwise use a shader to do blit or resolve.
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400821 else
822 {
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400823 ANGLE_TRY(utilsVk.colorBlitResolve(contextVk, this, &readRenderTarget->getImage(),
Jamie Madill88bc4d32019-10-04 20:41:24 -0400824 readRenderTarget->getReadImageView(), params));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400825 }
826 }
827
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400828 if (blitDepthBuffer || blitStencilBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400829 {
830 RenderTargetVk *readRenderTarget = srcFramebufferVk->getDepthStencilRenderTarget();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400831 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400832 params.srcLayer = readRenderTarget->getLayerIndex();
833
834 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400835 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400836
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400837 // Similarly, only blit if there's been no clipping.
838 bool canBlitWithCommand = !isResolve && noClip &&
839 (noFlip || !disableFlippingBlitWithCommand) &&
840 HasSrcBlitFeature(renderer, readRenderTarget) &&
841 HasDstBlitFeature(renderer, drawRenderTarget);
842 bool areChannelsBlitCompatible =
843 areSrcAndDstDepthStencilChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400844
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400845 if (canBlitWithCommand && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400846 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400847 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
848 drawRenderTarget, filter, false, blitDepthBuffer,
849 blitStencilBuffer, flipX, flipY));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400850 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400851 else
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400852 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400853 // Create depth- and stencil-only views for reading.
Shahbaz Youssefi57ad1e12019-08-23 14:53:26 -0400854 vk::DeviceScoped<vk::ImageView> depthView(contextVk->getDevice());
855 vk::DeviceScoped<vk::ImageView> stencilView(contextVk->getDevice());
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400856
857 vk::ImageHelper *depthStencilImage = &readRenderTarget->getImage();
858 uint32_t levelIndex = readRenderTarget->getLevelIndex();
859 uint32_t layerIndex = readRenderTarget->getLayerIndex();
860 gl::TextureType textureType = vk::Get2DTextureType(depthStencilImage->getLayerCount(),
861 depthStencilImage->getSamples());
862
863 if (blitDepthBuffer)
864 {
865 ANGLE_TRY(depthStencilImage->initLayerImageView(
866 contextVk, textureType, VK_IMAGE_ASPECT_DEPTH_BIT, gl::SwizzleState(),
867 &depthView.get(), levelIndex, 1, layerIndex, 1));
868 }
869
870 if (blitStencilBuffer)
871 {
872 ANGLE_TRY(depthStencilImage->initLayerImageView(
873 contextVk, textureType, VK_IMAGE_ASPECT_STENCIL_BIT, gl::SwizzleState(),
874 &stencilView.get(), levelIndex, 1, layerIndex, 1));
875 }
876
877 // If shader stencil export is not possible, defer stencil blit/stencil to another pass.
878 bool hasShaderStencilExport =
879 contextVk->getRenderer()->getFeatures().supportsShaderStencilExport.enabled;
880
881 // Blit depth. If shader stencil export is present, blit stencil as well.
882 if (blitDepthBuffer || (blitStencilBuffer && hasShaderStencilExport))
883 {
Shahbaz Youssefi0b1fbcf2019-08-30 15:05:12 -0400884 const vk::ImageView *depth = blitDepthBuffer ? &depthView.get() : nullptr;
885 const vk::ImageView *stencil =
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400886 blitStencilBuffer && hasShaderStencilExport ? &stencilView.get() : nullptr;
887
888 ANGLE_TRY(utilsVk.depthStencilBlitResolve(contextVk, this, depthStencilImage, depth,
889 stencil, params));
890 }
891
892 // If shader stencil export is not present, blit stencil through a different path.
893 if (blitStencilBuffer && !hasShaderStencilExport)
894 {
895 ANGLE_TRY(utilsVk.stencilBlitResolveNoShaderExport(
896 contextVk, this, depthStencilImage, &stencilView.get(), params));
897 }
898
899 vk::ImageView depthViewObject = depthView.release();
900 vk::ImageView stencilViewObject = stencilView.release();
901
Jamie Madillb540da82019-09-19 14:19:12 -0400902 contextVk->addGarbage(&depthViewObject);
903 contextVk->addGarbage(&stencilViewObject);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400904 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400905 }
906
907 return angle::Result::Continue;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400908} // namespace rx
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400909
910angle::Result FramebufferVk::resolveColorWithCommand(ContextVk *contextVk,
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400911 const UtilsVk::BlitResolveParameters &params,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400912 vk::ImageHelper *srcImage)
913{
914 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
915 {
916 vk::CommandBuffer *srcLayoutChange;
917 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
918 srcImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferSrc,
919 srcLayoutChange);
920 }
Jamie Madill16c20142018-10-01 13:58:19 -0400921
Shahbaz Youssefi2660b502019-03-21 12:08:40 -0400922 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill16c20142018-10-01 13:58:19 -0400923 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
924
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400925 // Source's layout change should happen before rendering
Jamie Madilld7f28aa2019-09-19 14:19:10 -0400926 srcImage->addReadDependency(contextVk, &mFramebuffer);
Luc Ferron26581112018-06-21 09:43:08 -0400927
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400928 VkImageResolve resolveRegion = {};
929 resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
930 resolveRegion.srcSubresource.mipLevel = 0;
931 resolveRegion.srcSubresource.baseArrayLayer = params.srcLayer;
932 resolveRegion.srcSubresource.layerCount = 1;
933 resolveRegion.srcOffset.x = params.srcOffset[0];
934 resolveRegion.srcOffset.y = params.srcOffset[1];
935 resolveRegion.srcOffset.z = 0;
936 resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
937 resolveRegion.dstSubresource.layerCount = 1;
938 resolveRegion.dstOffset.x = params.destOffset[0];
939 resolveRegion.dstOffset.y = params.destOffset[1];
940 resolveRegion.dstOffset.z = 0;
941 resolveRegion.extent.width = params.srcExtents[0];
942 resolveRegion.extent.height = params.srcExtents[1];
943 resolveRegion.extent.depth = 1;
Jamie Madilld754eb52018-07-19 14:55:03 -0400944
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400945 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Luc Ferron82eda932018-07-09 15:10:22 -0400946 {
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400947 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
Jamie Madilld7f28aa2019-09-19 14:19:10 -0400948 vk::ImageHelper *drawImage = drawRenderTarget->getImageForWrite(contextVk, &mFramebuffer);
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400949 drawImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferDst,
950 commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400951
952 resolveRegion.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
953 resolveRegion.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
954
955 srcImage->resolve(&drawRenderTarget->getImage(), resolveRegion, commandBuffer);
Luc Ferron82eda932018-07-09 15:10:22 -0400956 }
957
Jamie Madill7c985f52018-11-29 18:16:17 -0500958 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400959}
960
Kenneth Russellce8602a2017-10-03 18:23:08 -0700961bool FramebufferVk::checkStatus(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400962{
Luc Ferron5bdf8bd2018-06-20 09:51:37 -0400963 // if we have both a depth and stencil buffer, they must refer to the same object
964 // since we only support packed_depth_stencil and not separate depth and stencil
965 if (mState.hasSeparateDepthAndStencilAttachments())
966 {
967 return false;
968 }
969
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400970 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400971}
972
Jamie Madill67220092019-05-20 11:12:53 -0400973angle::Result FramebufferVk::updateColorAttachment(const gl::Context *context, size_t colorIndexGL)
974{
975 ContextVk *contextVk = vk::GetImpl(context);
976
977 ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL));
978
979 // Update cached masks for masked clears.
980 RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL];
981 if (renderTarget)
982 {
983 const angle::Format &emulatedFormat = renderTarget->getImageFormat().imageFormat();
984 updateActiveColorMasks(colorIndexGL, emulatedFormat.redBits > 0,
985 emulatedFormat.greenBits > 0, emulatedFormat.blueBits > 0,
986 emulatedFormat.alphaBits > 0);
987
988 const angle::Format &sourceFormat = renderTarget->getImageFormat().angleFormat();
989 mEmulatedAlphaAttachmentMask.set(
990 colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0);
991
992 contextVk->updateColorMask(context->getState().getBlendState());
993 }
994 else
995 {
996 updateActiveColorMasks(colorIndexGL, false, false, false, false);
997 }
998
999 return angle::Result::Continue;
1000}
1001
Shahbaz Youssefida904482019-07-02 10:49:14 -04001002void FramebufferVk::invalidateImpl(ContextVk *contextVk, size_t count, const GLenum *attachments)
1003{
1004 ASSERT(mFramebuffer.hasStartedRenderPass());
1005
1006 gl::DrawBufferMask invalidateColorBuffers;
1007 bool invalidateDepthBuffer = false;
1008 bool invalidateStencilBuffer = false;
1009
1010 for (size_t i = 0; i < count; ++i)
1011 {
1012 const GLenum attachment = attachments[i];
1013
1014 switch (attachment)
1015 {
1016 case GL_DEPTH:
1017 case GL_DEPTH_ATTACHMENT:
1018 invalidateDepthBuffer = true;
1019 break;
1020 case GL_STENCIL:
1021 case GL_STENCIL_ATTACHMENT:
1022 invalidateStencilBuffer = true;
1023 break;
1024 case GL_DEPTH_STENCIL_ATTACHMENT:
1025 invalidateDepthBuffer = true;
1026 invalidateStencilBuffer = true;
1027 break;
1028 default:
1029 ASSERT(
1030 (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) ||
1031 (attachment == GL_COLOR));
1032
1033 invalidateColorBuffers.set(
1034 attachment == GL_COLOR ? 0u : (attachment - GL_COLOR_ATTACHMENT0));
1035 }
1036 }
1037
1038 // Set the appropriate storeOp for attachments.
1039 size_t attachmentIndexVk = 0;
1040 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
1041 {
1042 if (invalidateColorBuffers.test(colorIndexGL))
1043 {
1044 mFramebuffer.invalidateRenderPassColorAttachment(attachmentIndexVk);
1045 }
1046 ++attachmentIndexVk;
1047 }
1048
1049 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1050 if (depthStencilRenderTarget)
1051 {
1052 if (invalidateDepthBuffer)
1053 {
1054 mFramebuffer.invalidateRenderPassDepthAttachment(attachmentIndexVk);
1055 }
1056
1057 if (invalidateStencilBuffer)
1058 {
1059 mFramebuffer.invalidateRenderPassStencilAttachment(attachmentIndexVk);
1060 }
1061 }
1062
1063 // NOTE: Possible future optimization is to delay setting the storeOp and only do so if the
1064 // render pass is closed by itself before another draw call. Otherwise, in a situation like
1065 // this:
1066 //
1067 // draw()
1068 // invalidate()
1069 // draw()
1070 //
1071 // We would be discarding the attachments only to load them for the next draw (which is less
1072 // efficient than keeping the render pass open and not do the discard at all). While dEQP tests
1073 // this pattern, this optimization may not be necessary if no application does this. It is
1074 // expected that an application would invalidate() when it's done with the framebuffer, so the
1075 // render pass would have closed either way.
1076 mFramebuffer.finishCurrentCommands(contextVk);
1077}
1078
Jamie Madill6f755b22018-10-09 12:48:54 -04001079angle::Result FramebufferVk::syncState(const gl::Context *context,
1080 const gl::Framebuffer::DirtyBits &dirtyBits)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001081{
Jamie Madille1f3ad42017-10-28 23:00:42 -04001082 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001083
1084 ASSERT(dirtyBits.any());
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001085 for (size_t dirtyBit : dirtyBits)
1086 {
1087 switch (dirtyBit)
1088 {
1089 case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
1090 case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
1091 ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState));
1092 break;
Jamie Madill67220092019-05-20 11:12:53 -04001093 case gl::Framebuffer::DIRTY_BIT_DEPTH_BUFFER_CONTENTS:
1094 case gl::Framebuffer::DIRTY_BIT_STENCIL_BUFFER_CONTENTS:
1095 ANGLE_TRY(mRenderTargetCache.getDepthStencil()->flushStagedUpdates(contextVk));
1096 break;
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001097 case gl::Framebuffer::DIRTY_BIT_READ_BUFFER:
Tim Van Patten56ba54c2019-08-08 13:03:34 -06001098 ANGLE_TRY(mRenderTargetCache.update(context, mState, dirtyBits));
1099 break;
1100 case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001101 case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
1102 case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
1103 case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
1104 case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1105 break;
1106 default:
1107 {
Jamie Madill67220092019-05-20 11:12:53 -04001108 static_assert(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits");
1109 if (dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX)
Jamie Madill9aef3672018-04-27 11:45:06 -04001110 {
Jamie Madill67220092019-05-20 11:12:53 -04001111 size_t colorIndexGL = static_cast<size_t>(
1112 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
1113 ANGLE_TRY(updateColorAttachment(context, colorIndexGL));
Jamie Madill9aef3672018-04-27 11:45:06 -04001114 }
1115 else
1116 {
Jamie Madill67220092019-05-20 11:12:53 -04001117 ASSERT(dirtyBit >= gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 &&
1118 dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_MAX);
1119 size_t colorIndexGL = static_cast<size_t>(
1120 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0);
1121 ANGLE_TRY(mRenderTargetCache.getColors()[colorIndexGL]->flushStagedUpdates(
1122 contextVk));
Jamie Madill9aef3672018-04-27 11:45:06 -04001123 }
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001124 }
1125 }
1126 }
Jamie Madill66546be2018-03-08 09:47:20 -05001127
Tim Van Patten626a7282019-07-08 15:11:59 -06001128 // The FBOs new attachment may have changed the renderable area
1129 const gl::State &glState = context->getState();
1130 contextVk->updateScissor(glState);
1131
Jamie Madill9aef3672018-04-27 11:45:06 -04001132 mActiveColorComponents = gl_vk::GetColorComponentFlags(
Luc Ferron5fd36932018-06-19 14:55:50 -04001133 mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(),
1134 mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any());
Jamie Madill9aef3672018-04-27 11:45:06 -04001135
Geoff Langee244c72019-05-06 10:30:18 -04001136 mFramebuffer.release(contextVk);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001137
Jamie Madill316c6062018-05-29 10:49:45 -04001138 // Will freeze the current set of dependencies on this FBO. The next time we render we will
Jamie Madilla5e06072018-05-18 14:36:05 -04001139 // create a new entry in the command graph.
Geoff Langee244c72019-05-06 10:30:18 -04001140 mFramebuffer.finishCurrentCommands(contextVk);
Jamie Madill72106562017-03-24 14:18:50 -04001141
Jamie Madilldbc605c2019-01-04 16:39:14 -05001142 // Notify the ContextVk to update the pipeline desc.
1143 updateRenderPassDesc();
Shahbaz Youssefida904482019-07-02 10:49:14 -04001144
1145 FramebufferVk *currentDrawFramebuffer = vk::GetImpl(context->getState().getDrawFramebuffer());
1146 if (currentDrawFramebuffer == this)
1147 {
1148 contextVk->onDrawFramebufferChange(this);
1149 }
Jamie Madill19fa1c62018-03-08 09:47:21 -05001150
Jamie Madill7c985f52018-11-29 18:16:17 -05001151 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001152}
1153
Jamie Madilldbc605c2019-01-04 16:39:14 -05001154void FramebufferVk::updateRenderPassDesc()
Jamie Madillab9f9c32017-01-17 17:47:34 -05001155{
Jamie Madilldbc605c2019-01-04 16:39:14 -05001156 mRenderPassDesc = {};
1157 mRenderPassDesc.setSamples(getSamples());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001158
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001159 const auto &colorRenderTargets = mRenderTargetCache.getColors();
1160 const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers();
1161 for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001162 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001163 if (enabledDrawBuffers[colorIndexGL])
1164 {
1165 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
1166 ASSERT(colorRenderTarget);
1167 mRenderPassDesc.packColorAttachment(
1168 colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID);
1169 }
1170 else
1171 {
1172 mRenderPassDesc.packColorAttachmentGap(colorIndexGL);
1173 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001174 }
1175
Jamie Madill66546be2018-03-08 09:47:20 -05001176 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1177 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001178 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001179 mRenderPassDesc.packDepthStencilAttachment(
1180 depthStencilRenderTarget->getImage().getFormat().angleFormatID);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001181 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001182}
1183
Jamie Madill21061022018-07-12 23:56:30 -04001184angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001185{
1186 // If we've already created our cached Framebuffer, return it.
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001187 if (mFramebuffer.valid())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001188 {
Jamie Madille8dd0792018-09-27 15:04:27 -04001189 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001190 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001191 }
1192
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001193 vk::RenderPass *compatibleRenderPass = nullptr;
Geoff Langee244c72019-05-06 10:30:18 -04001194 ANGLE_TRY(contextVk->getCompatibleRenderPass(mRenderPassDesc, &compatibleRenderPass));
Jamie Madillab9f9c32017-01-17 17:47:34 -05001195
1196 // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it.
1197 if (mBackbuffer)
1198 {
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001199 return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001200 }
1201
1202 // Gather VkImageViews over all FBO attachments, also size of attached region.
1203 std::vector<VkImageView> attachments;
1204 gl::Extents attachmentsSize;
1205
Jamie Madill66546be2018-03-08 09:47:20 -05001206 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001207 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001208 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001209 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001210 ASSERT(colorRenderTarget);
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001211 attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001212
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001213 ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents());
1214 attachmentsSize = colorRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001215 }
1216
Jamie Madill66546be2018-03-08 09:47:20 -05001217 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1218 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001219 {
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001220 attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001221
Jamie Madillbc543422018-03-30 10:43:19 -04001222 ASSERT(attachmentsSize.empty() ||
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001223 attachmentsSize == depthStencilRenderTarget->getExtents());
1224 attachmentsSize = depthStencilRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001225 }
1226
Tim Van Patten626a7282019-07-08 15:11:59 -06001227 if (attachmentsSize.empty())
1228 {
1229 // No attachments, so use the default values.
1230 attachmentsSize.height = mState.getDefaultHeight();
1231 attachmentsSize.width = mState.getDefaultWidth();
1232 attachmentsSize.depth = 0;
1233 }
1234
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001235 VkFramebufferCreateInfo framebufferInfo = {};
Jamie Madillab9f9c32017-01-17 17:47:34 -05001236
1237 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001238 framebufferInfo.flags = 0;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001239 framebufferInfo.renderPass = compatibleRenderPass->getHandle();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001240 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
1241 framebufferInfo.pAttachments = attachments.data();
1242 framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width);
1243 framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height);
1244 framebufferInfo.layers = 1;
1245
Jamie Madill21061022018-07-12 23:56:30 -04001246 ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo));
Jamie Madill5deea722017-02-16 10:44:46 -05001247
Jamie Madille8dd0792018-09-27 15:04:27 -04001248 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001249 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001250}
1251
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001252angle::Result FramebufferVk::clearWithRenderPassOp(
1253 ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001254 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001255 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001256 bool clearDepth,
1257 bool clearStencil,
1258 const VkClearColorValue &clearColorValue,
1259 const VkClearDepthStencilValue &clearDepthStencilValue)
1260{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001261 // Start a new render pass if:
1262 //
1263 // - no render pass has started,
1264 // - there is a render pass started but it contains commands; we cannot modify its ops, so new
1265 // render pass is needed,
1266 // - the current render area doesn't match the clear area. We need the render area to be
1267 // exactly as specified by the scissor for the loadOp to clear only that area. See
1268 // onScissorChange for more information.
1269
1270 if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() ||
1271 mFramebuffer.getRenderPassRenderArea() != clearArea)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001272 {
1273 vk::CommandBuffer *commandBuffer;
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001274 ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer));
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001275 }
1276
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001277 size_t attachmentIndexVk = 0;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001278
1279 // Go through clearColorBuffers and set the appropriate loadOp and clear values.
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001280 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001281 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001282 if (clearColorBuffers.test(colorIndexGL))
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001283 {
Tim Van Patten56ba54c2019-08-08 13:03:34 -06001284 RenderTargetVk *renderTarget = getColorDrawRenderTarget(colorIndexGL);
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001285
1286 // If the render target doesn't have alpha, but its emulated format has it, clear the
1287 // alpha to 1.
1288 VkClearColorValue value = clearColorValue;
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001289 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001290 {
1291 SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value);
1292 }
1293
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001294 mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value);
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001295 }
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001296 ++attachmentIndexVk;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001297 }
1298
1299 // Set the appropriate loadOp and clear values for depth and stencil.
1300 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1301 if (depthStencilRenderTarget)
1302 {
1303 if (clearDepth)
1304 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001305 mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001306 clearDepthStencilValue.depth);
1307 }
1308
1309 if (clearStencil)
1310 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001311 mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001312 clearDepthStencilValue.stencil);
1313 }
1314 }
1315
1316 return angle::Result::Continue;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001317}
1318
Jamie Madill21061022018-07-12 23:56:30 -04001319angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001320 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001321 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001322 bool clearStencil,
1323 VkColorComponentFlags colorMaskFlags,
1324 uint8_t stencilMask,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001325 const VkClearColorValue &clearColorValue,
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001326 uint8_t clearStencilValue)
Jamie Madill9aef3672018-04-27 11:45:06 -04001327{
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001328 UtilsVk::ClearFramebufferParameters params = {};
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001329 params.clearArea = clearArea;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001330 params.colorClearValue = clearColorValue;
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001331 params.stencilClearValue = clearStencilValue;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001332 params.stencilMask = stencilMask;
1333
1334 params.clearColor = true;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001335 params.clearStencil = clearStencil;
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001336
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001337 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001338 for (size_t colorIndexGL : clearColorBuffers)
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001339 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001340 const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001341 ASSERT(colorRenderTarget);
1342
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001343 params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat();
James Darpinian7e48c9e2019-08-06 17:17:19 -07001344 params.colorAttachmentIndexGL = static_cast<uint32_t>(colorIndexGL);
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001345 params.colorMaskFlags = colorMaskFlags;
1346 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001347 {
1348 params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT;
1349 }
1350
Geoff Langee244c72019-05-06 10:30:18 -04001351 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001352
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001353 // Clear stencil only once!
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001354 params.clearStencil = false;
1355 }
1356
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001357 // If there was no color clear, clear stencil alone.
1358 if (params.clearStencil)
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001359 {
1360 params.clearColor = false;
Geoff Langee244c72019-05-06 10:30:18 -04001361 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001362 }
1363
1364 return angle::Result::Continue;
Jamie Madill9aef3672018-04-27 11:45:06 -04001365}
1366
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001367angle::Result FramebufferVk::getSamplePosition(const gl::Context *context,
1368 size_t index,
1369 GLfloat *xy) const
JiangYizhoubddc46b2016-12-09 09:50:51 +08001370{
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001371 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -05001372 return angle::Result::Stop;
JiangYizhoubddc46b2016-12-09 09:50:51 +08001373}
1374
Jamie Madilld1249de2018-08-28 16:58:53 -04001375angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001376 const gl::Rectangle &renderArea,
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001377 vk::CommandBuffer **commandBufferOut)
Jamie Madilld1249de2018-08-28 16:58:53 -04001378{
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001379 vk::Framebuffer *framebuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001380 ANGLE_TRY(getFramebuffer(contextVk, &framebuffer));
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001381
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001382 vk::AttachmentOpsArray renderPassAttachmentOps;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001383 std::vector<VkClearValue> attachmentClearValues;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001384
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001385 vk::CommandBuffer *writeCommands = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001386 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands));
Jamie Madille4c5a232018-03-02 21:00:31 -05001387
Jamie Madill49ac74b2017-12-21 14:42:33 -05001388 // Initialize RenderPass info.
Jamie Madill66546be2018-03-08 09:47:20 -05001389 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001390 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madill4c26fc22017-02-24 11:04:10 -05001391 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001392 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001393 ASSERT(colorRenderTarget);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001394
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001395 ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001396
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001397 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1398 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1399 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
1400 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill66546be2018-03-08 09:47:20 -05001401 }
1402
1403 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1404 if (depthStencilRenderTarget)
1405 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001406 ANGLE_TRY(
1407 depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001408
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001409 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1410 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1411 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
1412 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001413 }
1414
Jamie Madilldbc605c2019-01-04 16:39:14 -05001415 return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001416 renderPassAttachmentOps, attachmentClearValues,
1417 commandBufferOut);
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001418}
1419
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001420void FramebufferVk::updateActiveColorMasks(size_t colorIndexGL, bool r, bool g, bool b, bool a)
Jamie Madill9aef3672018-04-27 11:45:06 -04001421{
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001422 mActiveColorComponentMasksForClear[0].set(colorIndexGL, r);
1423 mActiveColorComponentMasksForClear[1].set(colorIndexGL, g);
1424 mActiveColorComponentMasksForClear[2].set(colorIndexGL, b);
1425 mActiveColorComponentMasksForClear[3].set(colorIndexGL, a);
Luc Ferron5fd36932018-06-19 14:55:50 -04001426}
1427
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001428const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const
Luc Ferron5fd36932018-06-19 14:55:50 -04001429{
1430 return mEmulatedAlphaAttachmentMask;
Jamie Madill9aef3672018-04-27 11:45:06 -04001431}
Luc Ferron018709f2018-05-10 13:53:11 -04001432
Jamie Madill21061022018-07-12 23:56:30 -04001433angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk,
1434 const gl::Rectangle &area,
1435 const PackPixelsParams &packPixelsParams,
Jamie Madillb436aac2018-07-18 17:23:48 -04001436 VkImageAspectFlagBits copyAspectFlags,
Jamie Madill21061022018-07-12 23:56:30 -04001437 RenderTargetVk *renderTarget,
1438 void *pixels)
Luc Ferron018709f2018-05-10 13:53:11 -04001439{
Jamie Madill3ea463b2019-06-19 14:21:33 -04001440 ANGLE_TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl");
Jamie Madill58675012018-05-22 14:54:07 -04001441
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001442 RendererVk *renderer = contextVk->getRenderer();
1443
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001444 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001445 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
Jamie Madill58675012018-05-22 14:54:07 -04001446
Jamie Madillbcf467f2018-05-23 09:46:00 -04001447 // Note that although we're reading from the image, we need to update the layout below.
Jamie Madilld7f28aa2019-09-19 14:19:10 -04001448 vk::ImageHelper *srcImage = renderTarget->getImageForRead(
1449 contextVk, &mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -04001450
Jamie Madill0631e192019-04-18 16:09:12 -04001451 const angle::Format *readFormat = &srcImage->getFormat().imageFormat();
Luc Ferron1617e692018-07-11 11:08:19 -04001452
Jamie Madillb436aac2018-07-18 17:23:48 -04001453 if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT)
Luc Ferron1617e692018-07-11 11:08:19 -04001454 {
Jamie Madillb436aac2018-07-18 17:23:48 -04001455 readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags);
Luc Ferron1617e692018-07-11 11:08:19 -04001456 }
1457
James Darpinian7e48c9e2019-08-06 17:17:19 -07001458 uint32_t level = renderTarget->getLevelIndex();
1459 uint32_t layer = renderTarget->getLayerIndex();
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001460 VkOffset3D srcOffset = {area.x, area.y, 0};
Cody Northropeb0479e2019-07-24 14:15:56 -06001461
1462 VkImageSubresourceLayers srcSubresource = {};
1463 srcSubresource.aspectMask = copyAspectFlags;
1464 srcSubresource.mipLevel = level;
1465 srcSubresource.baseArrayLayer = layer;
1466 srcSubresource.layerCount = 1;
1467
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001468 VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height),
1469 1};
1470
Cody Northropeb0479e2019-07-24 14:15:56 -06001471 if (srcImage->getExtents().depth > 1)
1472 {
1473 // Depth > 1 means this is a 3D texture and we need special handling
1474 srcOffset.z = layer;
1475 srcSubresource.baseArrayLayer = 0;
1476 }
1477
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001478 // If the source image is multisampled, we need to resolve it into a temporary image before
1479 // performing a readback.
1480 bool isMultisampled = srcImage->getSamples() > 1;
Shahbaz Youssefi57ad1e12019-08-23 14:53:26 -04001481 vk::DeviceScoped<vk::ImageHelper> resolvedImage(contextVk->getDevice());
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001482 if (isMultisampled)
1483 {
1484 ANGLE_TRY(resolvedImage.get().init2DStaging(
1485 contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1),
1486 srcImage->getFormat(),
1487 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1));
Jamie Madillb540da82019-09-19 14:19:12 -04001488 resolvedImage.get().onGraphAccess(contextVk->getCommandGraph());
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001489
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -04001490 // Note: resolve only works on color images (not depth/stencil).
1491 //
1492 // TODO: Currently, depth/stencil blit can perform a depth/stencil readback, but that code
1493 // path will be optimized away. http://anglebug.com/3200
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001494 ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT);
1495
1496 VkImageResolve resolveRegion = {};
Cody Northropeb0479e2019-07-24 14:15:56 -06001497 resolveRegion.srcSubresource = srcSubresource;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001498 resolveRegion.srcOffset = srcOffset;
1499 resolveRegion.dstSubresource.aspectMask = copyAspectFlags;
1500 resolveRegion.dstSubresource.mipLevel = 0;
1501 resolveRegion.dstSubresource.baseArrayLayer = 0;
1502 resolveRegion.dstSubresource.layerCount = 1;
1503 resolveRegion.dstOffset = {};
1504 resolveRegion.extent = srcExtent;
1505
1506 srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer);
1507
1508 resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc,
1509 commandBuffer);
1510
1511 // Make the resolved image the target of buffer copy.
Mingyu Hu7e44ec22019-08-26 15:59:48 -07001512 srcImage = &resolvedImage.get();
1513 level = 0;
1514 layer = 0;
1515 srcOffset = {0, 0, 0};
Cody Northropeb0479e2019-07-24 14:15:56 -06001516 srcSubresource.baseArrayLayer = 0;
1517 srcSubresource.layerCount = 1;
1518 srcSubresource.mipLevel = 0;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001519 }
1520
Jamie Madillb980c562018-11-27 11:34:27 -05001521 VkBuffer bufferHandle = VK_NULL_HANDLE;
1522 uint8_t *readPixelBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -04001523 VkDeviceSize stagingOffset = 0;
Jamie Madillb980c562018-11-27 11:34:27 -05001524 size_t allocationSize = readFormat->pixelBytes * area.width * area.height;
Luc Ferron018709f2018-05-10 13:53:11 -04001525
Jamie Madilld754eb52018-07-19 14:55:03 -04001526 ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -05001527 &stagingOffset, nullptr));
Luc Ferron018709f2018-05-10 13:53:11 -04001528
Cody Northropeb0479e2019-07-24 14:15:56 -06001529 VkBufferImageCopy region = {};
1530 region.bufferImageHeight = srcExtent.height;
1531 region.bufferOffset = stagingOffset;
1532 region.bufferRowLength = srcExtent.width;
1533 region.imageExtent = srcExtent;
1534 region.imageOffset = srcOffset;
1535 region.imageSubresource = srcSubresource;
Luc Ferron534b00d2018-05-18 08:16:53 -04001536
Jamie Madillbcf467f2018-05-23 09:46:00 -04001537 commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(),
1538 bufferHandle, 1, &region);
Luc Ferron018709f2018-05-10 13:53:11 -04001539
1540 // Triggers a full finish.
1541 // TODO(jmadill): Don't block on asynchronous readback.
Geoff Lang892d1802019-03-27 14:21:34 -04001542 ANGLE_TRY(contextVk->finishImpl());
Luc Ferron018709f2018-05-10 13:53:11 -04001543
Luc Ferron534b00d2018-05-18 08:16:53 -04001544 // The buffer we copied to needs to be invalidated before we read from it because its not been
1545 // created with the host coherent bit.
Jamie Madilld754eb52018-07-19 14:55:03 -04001546 ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk));
Yuly Novikov6c6c76c2018-05-17 18:45:06 +00001547
Ian Elliottdc2c5c52019-08-02 09:32:18 -06001548 const gl::State &glState = contextVk->getState();
1549 gl::Buffer *packBuffer = glState.getTargetBuffer(gl::BufferBinding::PixelPack);
1550 if (packBuffer != nullptr)
1551 {
1552 // Must map the PBO in order to read its contents (and then unmap it later)
1553 BufferVk *packBufferVk = vk::GetImpl(packBuffer);
1554 void *mapPtr = nullptr;
1555 ANGLE_TRY(packBufferVk->mapImpl(contextVk, &mapPtr));
1556 uint8_t *dest = static_cast<uint8_t *>(mapPtr) + reinterpret_cast<ptrdiff_t>(pixels);
1557 PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes,
1558 readPixelBuffer, static_cast<uint8_t *>(dest));
1559 packBufferVk->unmapImpl(contextVk);
1560 }
1561 else
1562 {
1563 PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes,
1564 readPixelBuffer, static_cast<uint8_t *>(pixels));
1565 }
Luc Ferron018709f2018-05-10 13:53:11 -04001566
Jamie Madill7c985f52018-11-29 18:16:17 -05001567 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -04001568}
Jamie Madill58675012018-05-22 14:54:07 -04001569
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001570gl::Extents FramebufferVk::getReadImageExtents() const
Jamie Madill58675012018-05-22 14:54:07 -04001571{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001572 ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width);
1573 ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height);
1574
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001575 return getColorReadRenderTarget()->getExtents();
Jamie Madill58675012018-05-22 14:54:07 -04001576}
Jamie Madill502d2e22018-11-01 11:06:23 -04001577
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001578gl::Rectangle FramebufferVk::getCompleteRenderArea() const
1579{
Tim Van Patten626a7282019-07-08 15:11:59 -06001580 const gl::Box &dimensions = mState.getDimensions();
1581 return gl::Rectangle(0, 0, dimensions.width, dimensions.height);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001582}
1583
1584gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const
1585{
Tim Van Patten626a7282019-07-08 15:11:59 -06001586 const gl::Box &dimensions = mState.getDimensions();
1587 const gl::Rectangle renderArea(0, 0, dimensions.width, dimensions.height);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001588 bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO();
1589
1590 return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport);
1591}
1592
1593void FramebufferVk::onScissorChange(ContextVk *contextVk)
1594{
1595 gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
1596
1597 // If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass
1598 // is restarted. Otherwise, we can continue using the same renderpass area.
1599 //
1600 // Without a scissor, the render pass area covers the whole of the framebuffer. With a
1601 // scissored clear, the render pass area could be smaller than the framebuffer size. When the
1602 // scissor changes, if the scissor area is completely encompassed by the render pass area, it's
1603 // possible to continue using the same render pass. However, if the current render pass area
1604 // is too small, we need to start a new one. The latter can happen if a scissored clear starts
1605 // a render pass, the scissor is disabled and a draw call is issued to affect the whole
1606 // framebuffer.
Jamie Madillf10bf6b2019-09-26 10:27:18 -04001607 mFramebuffer.updateCurrentAccessNodes();
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001608 if (mFramebuffer.hasStartedRenderPass() &&
1609 !mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea))
1610 {
Geoff Langee244c72019-05-06 10:30:18 -04001611 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001612 }
1613}
1614
Jamie Madill502d2e22018-11-01 11:06:23 -04001615RenderTargetVk *FramebufferVk::getFirstRenderTarget() const
1616{
1617 for (auto *renderTarget : mRenderTargetCache.getColors())
1618 {
1619 if (renderTarget)
1620 {
1621 return renderTarget;
1622 }
1623 }
1624
1625 return mRenderTargetCache.getDepthStencil();
1626}
1627
1628GLint FramebufferVk::getSamples() const
1629{
1630 RenderTargetVk *firstRT = getFirstRenderTarget();
1631 return firstRT ? firstRT->getImage().getSamples() : 0;
1632}
1633
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001634} // namespace rx