blob: fed8f52378af32816e1d6276931fd2229105853c [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;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -040039// Clear values are only used when loadOp=Clear is set in clearWithRenderPassOp. When starting a
40// new render pass, the clear value is set to an unlikely value (bright pink) to stand out better
41// in case of a bug.
42constexpr VkClearValue kUninitializedClearValue = {{{0.95, 0.05, 0.95, 0.95}}};
Luc Ferron534b00d2018-05-18 08:16:53 -040043
Jamie Madill66546be2018-03-08 09:47:20 -050044const gl::InternalFormat &GetReadAttachmentInfo(const gl::Context *context,
45 RenderTargetVk *renderTarget)
Jamie Madill7b57b9d2017-01-13 09:33:38 -050046{
Jamie Madillbc543422018-03-30 10:43:19 -040047 GLenum implFormat =
Jamie Madill0631e192019-04-18 16:09:12 -040048 renderTarget->getImageFormat().imageFormat().fboImplementationInternalFormat;
Jamie Madill66546be2018-03-08 09:47:20 -050049 return gl::GetSizedInternalFormatInfo(implFormat);
Jamie Madill7b57b9d2017-01-13 09:33:38 -050050}
Luc Ferron26581112018-06-21 09:43:08 -040051
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040052bool HasSrcBlitFeature(RendererVk *renderer, RenderTargetVk *srcRenderTarget)
Luc Ferron26581112018-06-21 09:43:08 -040053{
Jamie Madill0631e192019-04-18 16:09:12 -040054 const VkFormat srcFormat = srcRenderTarget->getImageFormat().vkImageFormat;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040055 return renderer->hasImageFormatFeatureBits(srcFormat, VK_FORMAT_FEATURE_BLIT_SRC_BIT);
56}
Luc Ferron26581112018-06-21 09:43:08 -040057
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040058bool HasDstBlitFeature(RendererVk *renderer, RenderTargetVk *dstRenderTarget)
59{
60 const VkFormat dstFormat = dstRenderTarget->getImageFormat().vkImageFormat;
61 return renderer->hasImageFormatFeatureBits(dstFormat, VK_FORMAT_FEATURE_BLIT_DST_BIT);
62}
63
64// Returns false if destination has any channel the source doesn't. This means that channel was
65// emulated and using the Vulkan blit command would overwrite that emulated channel.
66bool areSrcAndDstColorChannelsBlitCompatible(RenderTargetVk *srcRenderTarget,
67 RenderTargetVk *dstRenderTarget)
68{
69 const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat();
70 const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat();
71
72 // Luminance/alpha formats are not renderable, so they can't have ended up in a framebuffer to
73 // participate in a blit.
74 ASSERT(!dstFormat.isLUMA() && !srcFormat.isLUMA());
75
76 // All color formats have the red channel.
77 ASSERT(dstFormat.redBits > 0 && srcFormat.redBits > 0);
78
79 return (dstFormat.greenBits > 0 || srcFormat.greenBits == 0) &&
80 (dstFormat.blueBits > 0 || srcFormat.blueBits == 0) &&
81 (dstFormat.alphaBits > 0 || srcFormat.alphaBits == 0);
82}
83
84bool areSrcAndDstDepthStencilChannelsBlitCompatible(RenderTargetVk *srcRenderTarget,
85 RenderTargetVk *dstRenderTarget)
86{
87 const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat();
88 const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat();
89
90 return (dstFormat.depthBits > 0 || srcFormat.depthBits == 0) &&
91 (dstFormat.stencilBits > 0 || srcFormat.stencilBits == 0);
Luc Ferron26581112018-06-21 09:43:08 -040092}
Jamie Madillb436aac2018-07-18 17:23:48 -040093
94// Special rules apply to VkBufferImageCopy with depth/stencil. The components are tightly packed
95// into a depth or stencil section of the destination buffer. See the spec:
96// https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkBufferImageCopy.html
97const angle::Format &GetDepthStencilImageToBufferFormat(const angle::Format &imageFormat,
98 VkImageAspectFlagBits copyAspect)
99{
100 if (copyAspect == VK_IMAGE_ASPECT_STENCIL_BIT)
101 {
102 ASSERT(imageFormat.id == angle::FormatID::D24_UNORM_S8_UINT ||
103 imageFormat.id == angle::FormatID::D32_FLOAT_S8X24_UINT ||
104 imageFormat.id == angle::FormatID::S8_UINT);
105 return angle::Format::Get(angle::FormatID::S8_UINT);
106 }
107
108 ASSERT(copyAspect == VK_IMAGE_ASPECT_DEPTH_BIT);
109
110 switch (imageFormat.id)
111 {
112 case angle::FormatID::D16_UNORM:
113 return imageFormat;
114 case angle::FormatID::D24_UNORM_X8_UINT:
115 return imageFormat;
116 case angle::FormatID::D24_UNORM_S8_UINT:
117 return angle::Format::Get(angle::FormatID::D24_UNORM_X8_UINT);
118 case angle::FormatID::D32_FLOAT:
119 return imageFormat;
120 case angle::FormatID::D32_FLOAT_S8X24_UINT:
121 return angle::Format::Get(angle::FormatID::D32_FLOAT);
122 default:
123 UNREACHABLE();
124 return imageFormat;
125 }
126}
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400127
128void SetEmulatedAlphaValue(const vk::Format &format, VkClearColorValue *value)
129{
130 if (format.vkFormatIsInt)
131 {
132 if (format.vkFormatIsUnsigned)
133 {
134 value->uint32[3] = kEmulatedAlphaValue;
135 }
136 else
137 {
138 value->int32[3] = kEmulatedAlphaValue;
139 }
140 }
141 else
142 {
143 value->float32[3] = kEmulatedAlphaValue;
144 }
145}
Jamie Madillbcf467f2018-05-23 09:46:00 -0400146} // anonymous namespace
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500147
148// static
Jamie Madill639bc902018-07-18 17:08:27 -0400149FramebufferVk *FramebufferVk::CreateUserFBO(RendererVk *renderer, const gl::FramebufferState &state)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500150{
Jamie Madill639bc902018-07-18 17:08:27 -0400151 return new FramebufferVk(renderer, state, nullptr);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500152}
153
154// static
Jamie Madill639bc902018-07-18 17:08:27 -0400155FramebufferVk *FramebufferVk::CreateDefaultFBO(RendererVk *renderer,
156 const gl::FramebufferState &state,
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500157 WindowSurfaceVk *backbuffer)
158{
Jamie Madill639bc902018-07-18 17:08:27 -0400159 return new FramebufferVk(renderer, state, backbuffer);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500160}
161
Jamie Madill639bc902018-07-18 17:08:27 -0400162FramebufferVk::FramebufferVk(RendererVk *renderer,
163 const gl::FramebufferState &state,
164 WindowSurfaceVk *backbuffer)
Jamie Madill7f2520f2019-06-26 11:18:33 -0400165 : FramebufferImpl(state), mBackbuffer(backbuffer), mActiveColorComponents(0)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500166{
Jamie Madill7f2520f2019-06-26 11:18:33 -0400167 mReadPixelBuffer.init(renderer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, 4, kMinReadPixelsBufferSize,
168 true);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500169}
170
Jamie Madill58675012018-05-22 14:54:07 -0400171FramebufferVk::~FramebufferVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400172
Jamie Madillc564c072017-06-01 12:45:42 -0400173void FramebufferVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500174{
Luc Ferron534b00d2018-05-18 08:16:53 -0400175 ContextVk *contextVk = vk::GetImpl(context);
Geoff Langee244c72019-05-06 10:30:18 -0400176 mFramebuffer.release(contextVk);
Luc Ferron534b00d2018-05-18 08:16:53 -0400177
Geoff Langee244c72019-05-06 10:30:18 -0400178 mReadPixelBuffer.release(contextVk);
Jamie Madill5deea722017-02-16 10:44:46 -0500179}
180
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400181angle::Result FramebufferVk::discard(const gl::Context *context,
182 size_t count,
183 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400184{
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400185 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -0500186 return angle::Result::Stop;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400187}
188
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400189angle::Result FramebufferVk::invalidate(const gl::Context *context,
190 size_t count,
191 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400192{
Shahbaz Youssefida904482019-07-02 10:49:14 -0400193 mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial());
194
195 if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass())
196 {
197 invalidateImpl(vk::GetImpl(context), count, attachments);
198 }
199
200 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400201}
202
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400203angle::Result FramebufferVk::invalidateSub(const gl::Context *context,
204 size_t count,
205 const GLenum *attachments,
206 const gl::Rectangle &area)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400207{
Shahbaz Youssefida904482019-07-02 10:49:14 -0400208 mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial());
209
210 // RenderPass' storeOp cannot be made conditional to a specific region, so we only apply this
211 // hint if the requested area encompasses the render area.
212 if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass() &&
213 area.encloses(mFramebuffer.getRenderPassRenderArea()))
214 {
215 invalidateImpl(vk::GetImpl(context), count, attachments);
216 }
217
218 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400219}
220
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400221angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400222{
Jamie Madill0cec82a2018-03-14 09:21:07 -0400223 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500224
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400225 bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT));
226 bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT));
227 bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT));
228 gl::DrawBufferMask clearColorBuffers;
229 if (clearColor)
230 {
231 clearColorBuffers = mState.getEnabledDrawBuffers();
232 }
233
234 const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color;
235 const VkClearDepthStencilValue &clearDepthStencilValue =
236 contextVk->getClearDepthStencilValue().depthStencil;
237
238 return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue,
239 clearDepthStencilValue);
240}
241
242angle::Result FramebufferVk::clearImpl(const gl::Context *context,
243 gl::DrawBufferMask clearColorBuffers,
244 bool clearDepth,
245 bool clearStencil,
246 const VkClearColorValue &clearColorValue,
247 const VkClearDepthStencilValue &clearDepthStencilValue)
248{
249 ContextVk *contextVk = vk::GetImpl(context);
250
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400251 const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
252
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400253 // Discard clear altogether if scissor has 0 width or height.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400254 if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400255 {
256 return angle::Result::Continue;
257 }
258
Geoff Langee244c72019-05-06 10:30:18 -0400259 mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400260
261 // This function assumes that only enabled attachments are asked to be cleared.
262 ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers);
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400263
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400264 // Adjust clear behavior based on whether the respective attachments are present; if asked to
265 // clear a non-existent attachment, don't attempt to clear it.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400266
267 VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask();
268 bool clearColor = clearColorBuffers.any();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400269
Jamie Madill0cec82a2018-03-14 09:21:07 -0400270 const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400271 clearDepth = clearDepth && depthAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400272 ASSERT(!clearDepth || depthAttachment->isAttached());
273
274 const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400275 clearStencil = clearStencil && stencilAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400276 ASSERT(!clearStencil || stencilAttachment->isAttached());
277
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400278 uint8_t stencilMask =
279 static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask);
280
281 // The front-end should ensure we don't attempt to clear color if all channels are masked.
282 ASSERT(!clearColor || colorMaskFlags != 0);
283 // The front-end should ensure we don't attempt to clear depth if depth write is disabled.
284 ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask);
285 // The front-end should ensure we don't attempt to clear stencil if all bits are masked.
286 ASSERT(!clearStencil || stencilMask != 0);
287
288 // If there is nothing to clear, return right away (for example, if asked to clear depth, but
289 // there is no depth attachment).
Shahbaz Youssefi02a579e2019-03-27 14:21:20 -0400290 if (!clearColor && !clearDepth && !clearStencil)
291 {
292 return angle::Result::Continue;
293 }
294
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400295 VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue;
Shahbaz Youssefid856ca42018-10-31 16:55:12 -0400296
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400297 // We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If
298 // there's a depth mask, depth clearing is already disabled.
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400299 bool maskedClearColor =
300 clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400301 bool maskedClearStencil = stencilMask != 0xFF;
302
303 bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor;
304 bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400305
306 // At least one of color, depth or stencil should be clearable with render pass loadOp for us
307 // to use this clear path.
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400308 bool clearAnyWithRenderPassLoadOp =
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400309 clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400310
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400311 if (clearAnyWithRenderPassLoadOp)
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400312 {
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400313 // Clearing color is indicated by the set bits in this mask. If not clearing colors with
314 // render pass loadOp, the default value of all-zeros means the clear is not done in
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400315 // clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass
316 // loadOp.
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400317 gl::DrawBufferMask clearBuffersWithRenderPassLoadOp;
318 if (clearColorWithRenderPassLoadOp)
319 {
320 clearBuffersWithRenderPassLoadOp = clearColorBuffers;
321 }
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400322 ANGLE_TRY(clearWithRenderPassOp(
323 contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth,
324 clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue));
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400325
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400326 // On some hardware, having inline commands at this point results in corrupted output. In
327 // that case, end the render pass immediately. http://anglebug.com/2361
Jonah Ryan-Davis776694c2019-05-08 10:28:55 -0400328 if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled)
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400329 {
Geoff Langee244c72019-05-06 10:30:18 -0400330 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400331 }
332
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400333 // Fallback to other methods for whatever isn't cleared here.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400334 clearDepth = false;
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400335 if (clearColorWithRenderPassLoadOp)
336 {
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400337 clearColorBuffers.reset();
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400338 clearColor = false;
339 }
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400340 if (clearStencilWithRenderPassLoadOp)
341 {
342 clearStencil = false;
343 }
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400344
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400345 // If nothing left to clear, early out.
346 if (!clearColor && !clearStencil)
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400347 {
348 return angle::Result::Continue;
349 }
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400350 }
351
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400352 // Note: depth clear is always done through render pass loadOp.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400353 ASSERT(clearDepth == false);
354
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400355 // The most costly clear mode is when we need to mask out specific color channels or stencil
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400356 // bits. This can only be done with a draw call.
357 return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil,
358 colorMaskFlags, stencilMask, clearColorValue,
359 static_cast<uint8_t>(modifiedDepthStencilValue.stencil));
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400360}
361
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400362angle::Result FramebufferVk::clearBufferfv(const gl::Context *context,
363 GLenum buffer,
364 GLint drawbuffer,
365 const GLfloat *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400366{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400367 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400368
369 bool clearDepth = false;
370 gl::DrawBufferMask clearColorBuffers;
371
372 if (buffer == GL_DEPTH)
373 {
374 clearDepth = true;
375 clearValue.depthStencil.depth = values[0];
376 }
377 else
378 {
379 clearColorBuffers.set(drawbuffer);
380 clearValue.color.float32[0] = values[0];
381 clearValue.color.float32[1] = values[1];
382 clearValue.color.float32[2] = values[2];
383 clearValue.color.float32[3] = values[3];
384 }
385
386 return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color,
387 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400388}
389
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400390angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context,
391 GLenum buffer,
392 GLint drawbuffer,
393 const GLuint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400394{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400395 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400396
397 gl::DrawBufferMask clearColorBuffers;
398 clearColorBuffers.set(drawbuffer);
399
400 clearValue.color.uint32[0] = values[0];
401 clearValue.color.uint32[1] = values[1];
402 clearValue.color.uint32[2] = values[2];
403 clearValue.color.uint32[3] = values[3];
404
405 return clearImpl(context, clearColorBuffers, false, false, clearValue.color,
406 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400407}
408
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400409angle::Result FramebufferVk::clearBufferiv(const gl::Context *context,
410 GLenum buffer,
411 GLint drawbuffer,
412 const GLint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400413{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400414 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400415
416 bool clearStencil = false;
417 gl::DrawBufferMask clearColorBuffers;
418
419 if (buffer == GL_STENCIL)
420 {
421 clearStencil = true;
422 clearValue.depthStencil.stencil =
423 gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max());
424 }
425 else
426 {
427 clearColorBuffers.set(drawbuffer);
428 clearValue.color.int32[0] = values[0];
429 clearValue.color.int32[1] = values[1];
430 clearValue.color.int32[2] = values[2];
431 clearValue.color.int32[3] = values[3];
432 }
433
434 return clearImpl(context, clearColorBuffers, false, clearStencil, clearValue.color,
435 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400436}
437
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400438angle::Result FramebufferVk::clearBufferfi(const gl::Context *context,
439 GLenum buffer,
440 GLint drawbuffer,
441 GLfloat depth,
442 GLint stencil)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400443{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400444 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400445
446 clearValue.depthStencil.depth = depth;
447 clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max());
448
449 return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color,
450 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400451}
452
Jamie Madill4928b7c2017-06-20 12:57:39 -0400453GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400454{
Jamie Madill66546be2018-03-08 09:47:20 -0500455 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400456}
457
Jamie Madill4928b7c2017-06-20 12:57:39 -0400458GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400459{
Jamie Madill66546be2018-03-08 09:47:20 -0500460 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400461}
462
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400463angle::Result FramebufferVk::readPixels(const gl::Context *context,
464 const gl::Rectangle &area,
465 GLenum format,
466 GLenum type,
467 void *pixels)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400468{
Luc Ferrona1c72422018-05-14 15:58:28 -0400469 // Clip read area to framebuffer.
470 const gl::Extents &fbSize = getState().getReadAttachment()->getSize();
471 const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400472 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400473
Luc Ferrona1c72422018-05-14 15:58:28 -0400474 gl::Rectangle clippedArea;
475 if (!ClipRectangle(area, fbRect, &clippedArea))
476 {
477 // nothing to read
Jamie Madill7c985f52018-11-29 18:16:17 -0500478 return angle::Result::Continue;
Luc Ferrona1c72422018-05-14 15:58:28 -0400479 }
Luc Ferronbf6dc372018-06-28 15:24:19 -0400480 gl::Rectangle flippedArea = clippedArea;
Geoff Lange076a232018-07-16 15:34:05 -0400481 if (contextVk->isViewportFlipEnabledForReadFBO())
Luc Ferronbf6dc372018-06-28 15:24:19 -0400482 {
483 flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height;
484 }
Luc Ferrona1c72422018-05-14 15:58:28 -0400485
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500486 const gl::State &glState = context->getState();
Frank Henigman1ffad842018-09-24 23:40:45 -0400487 const gl::PixelPackState &packState = glState.getPackState();
Luc Ferronbf6dc372018-06-28 15:24:19 -0400488
Luc Ferrona1c72422018-05-14 15:58:28 -0400489 const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type);
490
491 GLuint outputPitch = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400492 ANGLE_VK_CHECK_MATH(contextVk,
493 sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment,
494 packState.rowLength, &outputPitch));
Luc Ferrona1c72422018-05-14 15:58:28 -0400495 GLuint outputSkipBytes = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400496 ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState,
497 false, &outputSkipBytes));
Luc Ferrona1c72422018-05-14 15:58:28 -0400498
499 outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes +
500 (clippedArea.y - area.y) * outputPitch;
Luc Ferron60284222018-03-20 16:01:44 -0400501
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400502 const angle::Format &angleFormat = GetFormatFromFormatType(format, type);
503
Frank Henigman1ffad842018-09-24 23:40:45 -0400504 PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder,
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400505 glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0);
Frank Henigman1ffad842018-09-24 23:40:45 -0400506 if (contextVk->isViewportFlipEnabledForReadFBO())
507 {
508 params.reverseRowOrder = !params.reverseRowOrder;
509 }
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500510
Jamie Madill21061022018-07-12 23:56:30 -0400511 ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400512 getColorReadRenderTarget(),
Rafael Cintron05a449a2018-06-20 18:08:04 -0700513 static_cast<uint8_t *>(pixels) + outputSkipBytes));
Jamie Madillc773ab92019-06-25 17:11:58 -0400514 mReadPixelBuffer.releaseInFlightBuffers(contextVk);
Jamie Madill7c985f52018-11-29 18:16:17 -0500515 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -0400516}
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500517
Luc Ferron26581112018-06-21 09:43:08 -0400518RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const
519{
520 return mRenderTargetCache.getDepthStencil();
521}
522
Jamie Madill58675012018-05-22 14:54:07 -0400523RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const
Luc Ferron018709f2018-05-10 13:53:11 -0400524{
525 RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400526 ASSERT(renderTarget && renderTarget->getImage().valid());
Luc Ferron018709f2018-05-10 13:53:11 -0400527 return renderTarget;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400528}
529
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400530angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400531 const gl::Rectangle &sourceArea,
532 const gl::Rectangle &destArea,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400533 RenderTargetVk *readRenderTarget,
534 RenderTargetVk *drawRenderTarget,
535 GLenum filter,
536 bool colorBlit,
537 bool depthBlit,
538 bool stencilBlit,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400539 bool flipX,
540 bool flipY)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400541{
542 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
543 // it should never be the case that both color and depth/stencil need to be blitted at
544 // at the same time.
545 ASSERT(colorBlit != (depthBlit || stencilBlit));
546
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400547 vk::ImageHelper *srcImage = &readRenderTarget->getImage();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400548 vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
549
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400550 VkImageAspectFlags imageAspectMask = srcImage->getAspectFlags();
551 VkImageAspectFlags blitAspectMask = imageAspectMask;
552
553 // Remove depth or stencil aspects if they are not requested to be blitted.
554 if (!depthBlit)
555 {
556 blitAspectMask &= ~VK_IMAGE_ASPECT_DEPTH_BIT;
557 }
558 if (!stencilBlit)
559 {
560 blitAspectMask &= ~VK_IMAGE_ASPECT_STENCIL_BIT;
561 }
562
563 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
564 {
565 vk::CommandBuffer *srcLayoutChange;
566 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
567 srcImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferSrc, srcLayoutChange);
568 }
569
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400570 vk::CommandBuffer *commandBuffer = nullptr;
571 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
572
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400573 srcImage->addReadDependency(&mFramebuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400574
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400575 VkImageBlit blit = {};
576 blit.srcSubresource.aspectMask = blitAspectMask;
577 blit.srcSubresource.mipLevel = readRenderTarget->getLevelIndex();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400578 blit.srcSubresource.baseArrayLayer = readRenderTarget->getLayerIndex();
579 blit.srcSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400580 blit.srcOffsets[0] = {sourceArea.x0(), sourceArea.y0(), 0};
581 blit.srcOffsets[1] = {sourceArea.x1(), sourceArea.y1(), 1};
582 blit.dstSubresource.aspectMask = blitAspectMask;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400583 blit.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
584 blit.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
585 blit.dstSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400586 blit.dstOffsets[0] = {destArea.x0(), destArea.y0(), 0};
587 blit.dstOffsets[1] = {destArea.x1(), destArea.y1(), 1};
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400588
589 // Requirement of the copyImageToBuffer, the dst image must be in
590 // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400591 dstImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferDst, commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400592
593 commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
594 dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
595 gl_vk::GetFilter(filter));
596
597 return angle::Result::Continue;
598}
599
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400600angle::Result FramebufferVk::blit(const gl::Context *context,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400601 const gl::Rectangle &sourceAreaIn,
602 const gl::Rectangle &destAreaIn,
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400603 GLbitfield mask,
604 GLenum filter)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400605{
Luc Ferron26581112018-06-21 09:43:08 -0400606 ContextVk *contextVk = vk::GetImpl(context);
607 RendererVk *renderer = contextVk->getRenderer();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400608 UtilsVk &utilsVk = contextVk->getUtils();
Luc Ferron26581112018-06-21 09:43:08 -0400609
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400610 const gl::State &glState = contextVk->getState();
611 const gl::Framebuffer *srcFramebuffer = glState.getReadFramebuffer();
612
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400613 const bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0;
614 const bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0;
615 const bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400616
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400617 const bool isResolve = srcFramebuffer->getCachedSamples(context) > 1;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400618
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400619 FramebufferVk *srcFramebufferVk = vk::GetImpl(srcFramebuffer);
620 const bool srcFramebufferFlippedY = contextVk->isViewportFlipEnabledForReadFBO();
621 const bool destFramebufferFlippedY = contextVk->isViewportFlipEnabledForDrawFBO();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400622
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400623 gl::Rectangle sourceArea = sourceAreaIn;
624 gl::Rectangle destArea = destAreaIn;
625
626 // Note: GLES (all 3.x versions) require source and dest area to be identical when
627 // resolving.
628 ASSERT(!isResolve ||
629 (sourceArea.x == destArea.x && sourceArea.y == destArea.y &&
630 sourceArea.width == destArea.width && sourceArea.height == destArea.height));
631
632 const gl::Rectangle srcFramebufferDimensions =
633 srcFramebufferVk->mState.getDimensions().toRect();
634
635 // If the destination is flipped in either direction, we will flip the source instead so that
636 // the destination area is always unflipped.
637 sourceArea = sourceArea.flip(destArea.isReversedX(), destArea.isReversedY());
638 destArea = destArea.removeReversal();
639
640 // Calculate the stretch factor prior to any clipping, as it needs to remain constant.
641 const float stretch[2] = {
642 std::abs(sourceArea.width / static_cast<float>(destArea.width)),
643 std::abs(sourceArea.height / static_cast<float>(destArea.height)),
644 };
645
646 // First, clip the source area to framebuffer. That requires transforming the dest area to
647 // match the clipped source.
648 gl::Rectangle absSourceArea = sourceArea.removeReversal();
649 gl::Rectangle clippedSourceArea;
650 if (!gl::ClipRectangle(srcFramebufferDimensions, absSourceArea, &clippedSourceArea))
651 {
652 return angle::Result::Continue;
653 }
654
655 // Resize the destination area based on the new size of source. Note again that stretch is
656 // calculated as SrcDimension/DestDimension.
657 gl::Rectangle srcClippedDestArea;
658 if (isResolve)
659 {
660 // Source and dest areas are identical in resolve.
661 srcClippedDestArea = clippedSourceArea;
662 }
663 else if (clippedSourceArea == absSourceArea)
664 {
665 // If there was no clipping, keep dest area as is.
666 srcClippedDestArea = destArea;
667 }
668 else
669 {
670 // Shift dest area's x0,y0,x1,y1 by as much as the source area's got shifted (taking
671 // stretching into account)
672 float x0Shift = std::round((clippedSourceArea.x - absSourceArea.x) / stretch[0]);
673 float y0Shift = std::round((clippedSourceArea.y - absSourceArea.y) / stretch[1]);
674 float x1Shift = std::round((absSourceArea.x1() - clippedSourceArea.x1()) / stretch[0]);
675 float y1Shift = std::round((absSourceArea.y1() - clippedSourceArea.y1()) / stretch[1]);
676
677 // If the source area was reversed in any direction, the shift should be applied in the
678 // opposite direction as well.
679 if (sourceArea.isReversedX())
680 {
681 std::swap(x0Shift, x1Shift);
682 }
683
684 if (sourceArea.isReversedY())
685 {
686 std::swap(y0Shift, y1Shift);
687 }
688
689 srcClippedDestArea.x = destArea.x0() + static_cast<int>(x0Shift);
690 srcClippedDestArea.y = destArea.y0() + static_cast<int>(y0Shift);
691 int x1 = destArea.x1() - static_cast<int>(x1Shift);
692 int y1 = destArea.y1() - static_cast<int>(y1Shift);
693
694 srcClippedDestArea.width = x1 - srcClippedDestArea.x;
695 srcClippedDestArea.height = y1 - srcClippedDestArea.y;
696 }
697
698 // If framebuffers are flipped in Y, flip the source and dest area (which define the
699 // transformation regardless of clipping), as well as the blit area (which is the clipped
700 // dest area).
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400701 if (srcFramebufferFlippedY)
702 {
703 sourceArea.y = srcFramebufferDimensions.height - sourceArea.y;
704 sourceArea.height = -sourceArea.height;
705 }
706 if (destFramebufferFlippedY)
707 {
708 destArea.y = mState.getDimensions().height - destArea.y;
709 destArea.height = -destArea.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400710
711 srcClippedDestArea.y =
712 mState.getDimensions().height - srcClippedDestArea.y - srcClippedDestArea.height;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400713 }
714
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400715 const bool flipX = sourceArea.isReversedX() != destArea.isReversedX();
716 const bool flipY = sourceArea.isReversedY() != destArea.isReversedY();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400717
718 // GLES doesn't allow flipping the parameters of glBlitFramebuffer if performing a resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400719 ASSERT(!isResolve ||
720 (flipX == false && flipY == (srcFramebufferFlippedY != destFramebufferFlippedY)));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400721
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400722 // Again, transfer the destination flip to source, so dest is unflipped. Note that destArea
723 // was not reversed until the final possible Y-flip.
724 ASSERT(!destArea.isReversedX());
725 sourceArea = sourceArea.flip(false, destArea.isReversedY());
726 destArea = destArea.removeReversal();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400727
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400728 // Clip the destination area to the framebuffer size and scissor. Note that we don't care
729 // about the source area anymore. The offset translation is done based on the original source
730 // and destination rectangles. The stretch factor is already calculated as well.
731 gl::Rectangle blitArea;
732 if (!gl::ClipRectangle(getScissoredRenderArea(contextVk), srcClippedDestArea, &blitArea))
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400733 {
734 return angle::Result::Continue;
735 }
736
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400737 bool noClip = blitArea == destArea && stretch[0] == 1.0f && stretch[1] == 1.0f;
738 bool noFlip = !flipX && !flipY;
739 bool disableFlippingBlitWithCommand =
740 contextVk->getRenderer()->getFeatures().disableFlippingBlitWithCommand.enabled;
741
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400742 UtilsVk::BlitResolveParameters params;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400743 params.srcOffset[0] = sourceArea.x;
744 params.srcOffset[1] = sourceArea.y;
745 params.destOffset[0] = destArea.x;
746 params.destOffset[1] = destArea.y;
747 params.stretch[0] = stretch[0];
748 params.stretch[1] = stretch[1];
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400749 params.srcExtents[0] = srcFramebufferDimensions.width;
750 params.srcExtents[1] = srcFramebufferDimensions.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400751 params.blitArea = blitArea;
752 params.linear = filter == GL_LINEAR;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400753 params.flipX = flipX;
754 params.flipY = flipY;
755
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400756 if (blitColorBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400757 {
758 RenderTargetVk *readRenderTarget = srcFramebufferVk->getColorReadRenderTarget();
759 params.srcLayer = readRenderTarget->getLayerIndex();
760
761 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400762 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400763
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400764 // If there was no clipping and the format capabilities allow us, use Vulkan's builtin blit.
765 // The reason clipping is prohibited in this path is that due to rounding errors, it would
766 // be hard to guarantee the image stretching remains perfect. That also allows us not to
767 // have to transform back the dest clipping to source.
768 //
769 // For simplicity, we either blit all render targets with a Vulkan command, or none.
770 bool canBlitWithCommand = !isResolve && noClip &&
771 (noFlip || !disableFlippingBlitWithCommand) &&
772 HasSrcBlitFeature(renderer, readRenderTarget);
773 bool areChannelsBlitCompatible = true;
774 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
775 {
776 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
777 canBlitWithCommand =
778 canBlitWithCommand && HasDstBlitFeature(renderer, drawRenderTarget);
779 areChannelsBlitCompatible =
780 areChannelsBlitCompatible &&
781 areSrcAndDstColorChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
782 }
783
784 if (canBlitWithCommand && areChannelsBlitCompatible)
785 {
786 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
787 {
788 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
789 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
790 drawRenderTarget, filter, true, false, false, flipX,
791 flipY));
792 }
793 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400794 // If we're not flipping, use Vulkan's builtin resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400795 else if (isResolve && !flipX && !flipY && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400796 {
797 ANGLE_TRY(resolveColorWithCommand(contextVk, params, &readRenderTarget->getImage()));
798 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400799 // Otherwise use a shader to do blit or resolve.
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400800 else
801 {
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400802 ANGLE_TRY(utilsVk.colorBlitResolve(contextVk, this, &readRenderTarget->getImage(),
803 readRenderTarget->getFetchImageView(), params));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400804 }
805 }
806
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400807 if (blitDepthBuffer || blitStencilBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400808 {
809 RenderTargetVk *readRenderTarget = srcFramebufferVk->getDepthStencilRenderTarget();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400810 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400811 params.srcLayer = readRenderTarget->getLayerIndex();
812
813 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400814 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400815
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400816 // Similarly, only blit if there's been no clipping.
817 bool canBlitWithCommand = !isResolve && noClip &&
818 (noFlip || !disableFlippingBlitWithCommand) &&
819 HasSrcBlitFeature(renderer, readRenderTarget) &&
820 HasDstBlitFeature(renderer, drawRenderTarget);
821 bool areChannelsBlitCompatible =
822 areSrcAndDstDepthStencilChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400823
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400824 if (canBlitWithCommand && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400825 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400826 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
827 drawRenderTarget, filter, false, blitDepthBuffer,
828 blitStencilBuffer, flipX, flipY));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400829 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400830 else
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400831 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400832 // Create depth- and stencil-only views for reading.
833 vk::Scoped<vk::ImageView> depthView(contextVk->getDevice());
834 vk::Scoped<vk::ImageView> stencilView(contextVk->getDevice());
835
836 vk::ImageHelper *depthStencilImage = &readRenderTarget->getImage();
837 uint32_t levelIndex = readRenderTarget->getLevelIndex();
838 uint32_t layerIndex = readRenderTarget->getLayerIndex();
839 gl::TextureType textureType = vk::Get2DTextureType(depthStencilImage->getLayerCount(),
840 depthStencilImage->getSamples());
841
842 if (blitDepthBuffer)
843 {
844 ANGLE_TRY(depthStencilImage->initLayerImageView(
845 contextVk, textureType, VK_IMAGE_ASPECT_DEPTH_BIT, gl::SwizzleState(),
846 &depthView.get(), levelIndex, 1, layerIndex, 1));
847 }
848
849 if (blitStencilBuffer)
850 {
851 ANGLE_TRY(depthStencilImage->initLayerImageView(
852 contextVk, textureType, VK_IMAGE_ASPECT_STENCIL_BIT, gl::SwizzleState(),
853 &stencilView.get(), levelIndex, 1, layerIndex, 1));
854 }
855
856 // If shader stencil export is not possible, defer stencil blit/stencil to another pass.
857 bool hasShaderStencilExport =
858 contextVk->getRenderer()->getFeatures().supportsShaderStencilExport.enabled;
859
860 // Blit depth. If shader stencil export is present, blit stencil as well.
861 if (blitDepthBuffer || (blitStencilBuffer && hasShaderStencilExport))
862 {
863 vk::ImageView *depth = blitDepthBuffer ? &depthView.get() : nullptr;
864 vk::ImageView *stencil =
865 blitStencilBuffer && hasShaderStencilExport ? &stencilView.get() : nullptr;
866
867 ANGLE_TRY(utilsVk.depthStencilBlitResolve(contextVk, this, depthStencilImage, depth,
868 stencil, params));
869 }
870
871 // If shader stencil export is not present, blit stencil through a different path.
872 if (blitStencilBuffer && !hasShaderStencilExport)
873 {
874 ANGLE_TRY(utilsVk.stencilBlitResolveNoShaderExport(
875 contextVk, this, depthStencilImage, &stencilView.get(), params));
876 }
877
878 vk::ImageView depthViewObject = depthView.release();
879 vk::ImageView stencilViewObject = stencilView.release();
880
881 contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &depthViewObject);
882 contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &stencilViewObject);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400883 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400884 }
885
886 return angle::Result::Continue;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400887} // namespace rx
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400888
889angle::Result FramebufferVk::resolveColorWithCommand(ContextVk *contextVk,
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400890 const UtilsVk::BlitResolveParameters &params,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400891 vk::ImageHelper *srcImage)
892{
893 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
894 {
895 vk::CommandBuffer *srcLayoutChange;
896 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
897 srcImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferSrc,
898 srcLayoutChange);
899 }
Jamie Madill16c20142018-10-01 13:58:19 -0400900
Shahbaz Youssefi2660b502019-03-21 12:08:40 -0400901 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill16c20142018-10-01 13:58:19 -0400902 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
903
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400904 // Source's layout change should happen before rendering
905 srcImage->addReadDependency(&mFramebuffer);
Luc Ferron26581112018-06-21 09:43:08 -0400906
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400907 VkImageResolve resolveRegion = {};
908 resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
909 resolveRegion.srcSubresource.mipLevel = 0;
910 resolveRegion.srcSubresource.baseArrayLayer = params.srcLayer;
911 resolveRegion.srcSubresource.layerCount = 1;
912 resolveRegion.srcOffset.x = params.srcOffset[0];
913 resolveRegion.srcOffset.y = params.srcOffset[1];
914 resolveRegion.srcOffset.z = 0;
915 resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
916 resolveRegion.dstSubresource.layerCount = 1;
917 resolveRegion.dstOffset.x = params.destOffset[0];
918 resolveRegion.dstOffset.y = params.destOffset[1];
919 resolveRegion.dstOffset.z = 0;
920 resolveRegion.extent.width = params.srcExtents[0];
921 resolveRegion.extent.height = params.srcExtents[1];
922 resolveRegion.extent.depth = 1;
Jamie Madilld754eb52018-07-19 14:55:03 -0400923
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400924 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Luc Ferron82eda932018-07-09 15:10:22 -0400925 {
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400926 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400927 vk::ImageHelper *drawImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
928 drawImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferDst,
929 commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400930
931 resolveRegion.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
932 resolveRegion.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
933
934 srcImage->resolve(&drawRenderTarget->getImage(), resolveRegion, commandBuffer);
Luc Ferron82eda932018-07-09 15:10:22 -0400935 }
936
Jamie Madill7c985f52018-11-29 18:16:17 -0500937 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400938}
939
Kenneth Russellce8602a2017-10-03 18:23:08 -0700940bool FramebufferVk::checkStatus(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400941{
Luc Ferron5bdf8bd2018-06-20 09:51:37 -0400942 // if we have both a depth and stencil buffer, they must refer to the same object
943 // since we only support packed_depth_stencil and not separate depth and stencil
944 if (mState.hasSeparateDepthAndStencilAttachments())
945 {
946 return false;
947 }
948
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400949 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400950}
951
Jamie Madill67220092019-05-20 11:12:53 -0400952angle::Result FramebufferVk::updateColorAttachment(const gl::Context *context, size_t colorIndexGL)
953{
954 ContextVk *contextVk = vk::GetImpl(context);
955
956 ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL));
957
958 // Update cached masks for masked clears.
959 RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL];
960 if (renderTarget)
961 {
962 const angle::Format &emulatedFormat = renderTarget->getImageFormat().imageFormat();
963 updateActiveColorMasks(colorIndexGL, emulatedFormat.redBits > 0,
964 emulatedFormat.greenBits > 0, emulatedFormat.blueBits > 0,
965 emulatedFormat.alphaBits > 0);
966
967 const angle::Format &sourceFormat = renderTarget->getImageFormat().angleFormat();
968 mEmulatedAlphaAttachmentMask.set(
969 colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0);
970
971 contextVk->updateColorMask(context->getState().getBlendState());
972 }
973 else
974 {
975 updateActiveColorMasks(colorIndexGL, false, false, false, false);
976 }
977
978 return angle::Result::Continue;
979}
980
Shahbaz Youssefida904482019-07-02 10:49:14 -0400981void FramebufferVk::invalidateImpl(ContextVk *contextVk, size_t count, const GLenum *attachments)
982{
983 ASSERT(mFramebuffer.hasStartedRenderPass());
984
985 gl::DrawBufferMask invalidateColorBuffers;
986 bool invalidateDepthBuffer = false;
987 bool invalidateStencilBuffer = false;
988
989 for (size_t i = 0; i < count; ++i)
990 {
991 const GLenum attachment = attachments[i];
992
993 switch (attachment)
994 {
995 case GL_DEPTH:
996 case GL_DEPTH_ATTACHMENT:
997 invalidateDepthBuffer = true;
998 break;
999 case GL_STENCIL:
1000 case GL_STENCIL_ATTACHMENT:
1001 invalidateStencilBuffer = true;
1002 break;
1003 case GL_DEPTH_STENCIL_ATTACHMENT:
1004 invalidateDepthBuffer = true;
1005 invalidateStencilBuffer = true;
1006 break;
1007 default:
1008 ASSERT(
1009 (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) ||
1010 (attachment == GL_COLOR));
1011
1012 invalidateColorBuffers.set(
1013 attachment == GL_COLOR ? 0u : (attachment - GL_COLOR_ATTACHMENT0));
1014 }
1015 }
1016
1017 // Set the appropriate storeOp for attachments.
1018 size_t attachmentIndexVk = 0;
1019 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
1020 {
1021 if (invalidateColorBuffers.test(colorIndexGL))
1022 {
1023 mFramebuffer.invalidateRenderPassColorAttachment(attachmentIndexVk);
1024 }
1025 ++attachmentIndexVk;
1026 }
1027
1028 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1029 if (depthStencilRenderTarget)
1030 {
1031 if (invalidateDepthBuffer)
1032 {
1033 mFramebuffer.invalidateRenderPassDepthAttachment(attachmentIndexVk);
1034 }
1035
1036 if (invalidateStencilBuffer)
1037 {
1038 mFramebuffer.invalidateRenderPassStencilAttachment(attachmentIndexVk);
1039 }
1040 }
1041
1042 // NOTE: Possible future optimization is to delay setting the storeOp and only do so if the
1043 // render pass is closed by itself before another draw call. Otherwise, in a situation like
1044 // this:
1045 //
1046 // draw()
1047 // invalidate()
1048 // draw()
1049 //
1050 // We would be discarding the attachments only to load them for the next draw (which is less
1051 // efficient than keeping the render pass open and not do the discard at all). While dEQP tests
1052 // this pattern, this optimization may not be necessary if no application does this. It is
1053 // expected that an application would invalidate() when it's done with the framebuffer, so the
1054 // render pass would have closed either way.
1055 mFramebuffer.finishCurrentCommands(contextVk);
1056}
1057
Jamie Madill6f755b22018-10-09 12:48:54 -04001058angle::Result FramebufferVk::syncState(const gl::Context *context,
1059 const gl::Framebuffer::DirtyBits &dirtyBits)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001060{
Jamie Madille1f3ad42017-10-28 23:00:42 -04001061 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001062
1063 ASSERT(dirtyBits.any());
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001064 for (size_t dirtyBit : dirtyBits)
1065 {
1066 switch (dirtyBit)
1067 {
1068 case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
1069 case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
1070 ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState));
1071 break;
Jamie Madill67220092019-05-20 11:12:53 -04001072 case gl::Framebuffer::DIRTY_BIT_DEPTH_BUFFER_CONTENTS:
1073 case gl::Framebuffer::DIRTY_BIT_STENCIL_BUFFER_CONTENTS:
1074 ANGLE_TRY(mRenderTargetCache.getDepthStencil()->flushStagedUpdates(contextVk));
1075 break;
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001076 case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
1077 case gl::Framebuffer::DIRTY_BIT_READ_BUFFER:
1078 case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
1079 case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
1080 case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
1081 case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1082 break;
1083 default:
1084 {
Jamie Madill67220092019-05-20 11:12:53 -04001085 static_assert(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits");
1086 if (dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX)
Jamie Madill9aef3672018-04-27 11:45:06 -04001087 {
Jamie Madill67220092019-05-20 11:12:53 -04001088 size_t colorIndexGL = static_cast<size_t>(
1089 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
1090 ANGLE_TRY(updateColorAttachment(context, colorIndexGL));
Jamie Madill9aef3672018-04-27 11:45:06 -04001091 }
1092 else
1093 {
Jamie Madill67220092019-05-20 11:12:53 -04001094 ASSERT(dirtyBit >= gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 &&
1095 dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_MAX);
1096 size_t colorIndexGL = static_cast<size_t>(
1097 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0);
1098 ANGLE_TRY(mRenderTargetCache.getColors()[colorIndexGL]->flushStagedUpdates(
1099 contextVk));
Jamie Madill9aef3672018-04-27 11:45:06 -04001100 }
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001101 break;
1102 }
1103 }
1104 }
Jamie Madill66546be2018-03-08 09:47:20 -05001105
Jamie Madill9aef3672018-04-27 11:45:06 -04001106 mActiveColorComponents = gl_vk::GetColorComponentFlags(
Luc Ferron5fd36932018-06-19 14:55:50 -04001107 mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(),
1108 mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any());
Jamie Madill9aef3672018-04-27 11:45:06 -04001109
Geoff Langee244c72019-05-06 10:30:18 -04001110 mFramebuffer.release(contextVk);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001111
Jamie Madill316c6062018-05-29 10:49:45 -04001112 // Will freeze the current set of dependencies on this FBO. The next time we render we will
Jamie Madilla5e06072018-05-18 14:36:05 -04001113 // create a new entry in the command graph.
Geoff Langee244c72019-05-06 10:30:18 -04001114 mFramebuffer.finishCurrentCommands(contextVk);
Jamie Madill72106562017-03-24 14:18:50 -04001115
Jamie Madilldbc605c2019-01-04 16:39:14 -05001116 // Notify the ContextVk to update the pipeline desc.
1117 updateRenderPassDesc();
Shahbaz Youssefida904482019-07-02 10:49:14 -04001118
1119 FramebufferVk *currentDrawFramebuffer = vk::GetImpl(context->getState().getDrawFramebuffer());
1120 if (currentDrawFramebuffer == this)
1121 {
1122 contextVk->onDrawFramebufferChange(this);
1123 }
Jamie Madill19fa1c62018-03-08 09:47:21 -05001124
Jamie Madill7c985f52018-11-29 18:16:17 -05001125 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001126}
1127
Jamie Madilldbc605c2019-01-04 16:39:14 -05001128void FramebufferVk::updateRenderPassDesc()
Jamie Madillab9f9c32017-01-17 17:47:34 -05001129{
Jamie Madilldbc605c2019-01-04 16:39:14 -05001130 mRenderPassDesc = {};
1131 mRenderPassDesc.setSamples(getSamples());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001132
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001133 const auto &colorRenderTargets = mRenderTargetCache.getColors();
1134 const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers();
1135 for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001136 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001137 if (enabledDrawBuffers[colorIndexGL])
1138 {
1139 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
1140 ASSERT(colorRenderTarget);
1141 mRenderPassDesc.packColorAttachment(
1142 colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID);
1143 }
1144 else
1145 {
1146 mRenderPassDesc.packColorAttachmentGap(colorIndexGL);
1147 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001148 }
1149
Jamie Madill66546be2018-03-08 09:47:20 -05001150 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1151 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001152 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001153 mRenderPassDesc.packDepthStencilAttachment(
1154 depthStencilRenderTarget->getImage().getFormat().angleFormatID);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001155 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001156}
1157
Jamie Madill21061022018-07-12 23:56:30 -04001158angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001159{
1160 // If we've already created our cached Framebuffer, return it.
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001161 if (mFramebuffer.valid())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001162 {
Jamie Madille8dd0792018-09-27 15:04:27 -04001163 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001164 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001165 }
1166
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001167 vk::RenderPass *compatibleRenderPass = nullptr;
Geoff Langee244c72019-05-06 10:30:18 -04001168 ANGLE_TRY(contextVk->getCompatibleRenderPass(mRenderPassDesc, &compatibleRenderPass));
Jamie Madillab9f9c32017-01-17 17:47:34 -05001169
1170 // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it.
1171 if (mBackbuffer)
1172 {
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001173 return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001174 }
1175
1176 // Gather VkImageViews over all FBO attachments, also size of attached region.
1177 std::vector<VkImageView> attachments;
1178 gl::Extents attachmentsSize;
1179
Jamie Madill66546be2018-03-08 09:47:20 -05001180 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001181 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001182 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001183 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001184 ASSERT(colorRenderTarget);
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001185 attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001186
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001187 ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents());
1188 attachmentsSize = colorRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001189 }
1190
Jamie Madill66546be2018-03-08 09:47:20 -05001191 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1192 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001193 {
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001194 attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001195
Jamie Madillbc543422018-03-30 10:43:19 -04001196 ASSERT(attachmentsSize.empty() ||
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001197 attachmentsSize == depthStencilRenderTarget->getExtents());
1198 attachmentsSize = depthStencilRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001199 }
1200
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001201 VkFramebufferCreateInfo framebufferInfo = {};
Jamie Madillab9f9c32017-01-17 17:47:34 -05001202
1203 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001204 framebufferInfo.flags = 0;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001205 framebufferInfo.renderPass = compatibleRenderPass->getHandle();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001206 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
1207 framebufferInfo.pAttachments = attachments.data();
1208 framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width);
1209 framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height);
1210 framebufferInfo.layers = 1;
1211
Jamie Madill21061022018-07-12 23:56:30 -04001212 ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo));
Jamie Madill5deea722017-02-16 10:44:46 -05001213
Jamie Madille8dd0792018-09-27 15:04:27 -04001214 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001215 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001216}
1217
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001218angle::Result FramebufferVk::clearWithRenderPassOp(
1219 ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001220 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001221 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001222 bool clearDepth,
1223 bool clearStencil,
1224 const VkClearColorValue &clearColorValue,
1225 const VkClearDepthStencilValue &clearDepthStencilValue)
1226{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001227 // Start a new render pass if:
1228 //
1229 // - no render pass has started,
1230 // - there is a render pass started but it contains commands; we cannot modify its ops, so new
1231 // render pass is needed,
1232 // - the current render area doesn't match the clear area. We need the render area to be
1233 // exactly as specified by the scissor for the loadOp to clear only that area. See
1234 // onScissorChange for more information.
1235
1236 if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() ||
1237 mFramebuffer.getRenderPassRenderArea() != clearArea)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001238 {
1239 vk::CommandBuffer *commandBuffer;
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001240 ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer));
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001241 }
1242
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001243 size_t attachmentIndexVk = 0;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001244
1245 // Go through clearColorBuffers and set the appropriate loadOp and clear values.
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001246 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001247 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001248 if (clearColorBuffers.test(colorIndexGL))
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001249 {
1250 RenderTargetVk *renderTarget = getColorReadRenderTarget();
1251
1252 // If the render target doesn't have alpha, but its emulated format has it, clear the
1253 // alpha to 1.
1254 VkClearColorValue value = clearColorValue;
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001255 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001256 {
1257 SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value);
1258 }
1259
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001260 mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value);
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001261 }
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001262 ++attachmentIndexVk;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001263 }
1264
1265 // Set the appropriate loadOp and clear values for depth and stencil.
1266 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1267 if (depthStencilRenderTarget)
1268 {
1269 if (clearDepth)
1270 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001271 mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001272 clearDepthStencilValue.depth);
1273 }
1274
1275 if (clearStencil)
1276 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001277 mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001278 clearDepthStencilValue.stencil);
1279 }
1280 }
1281
1282 return angle::Result::Continue;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001283}
1284
Jamie Madill21061022018-07-12 23:56:30 -04001285angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001286 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001287 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001288 bool clearStencil,
1289 VkColorComponentFlags colorMaskFlags,
1290 uint8_t stencilMask,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001291 const VkClearColorValue &clearColorValue,
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001292 uint8_t clearStencilValue)
Jamie Madill9aef3672018-04-27 11:45:06 -04001293{
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001294 UtilsVk::ClearFramebufferParameters params = {};
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001295 params.clearArea = clearArea;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001296 params.colorClearValue = clearColorValue;
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001297 params.stencilClearValue = clearStencilValue;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001298 params.stencilMask = stencilMask;
1299
1300 params.clearColor = true;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001301 params.clearStencil = clearStencil;
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001302
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001303 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001304 for (size_t colorIndexGL : clearColorBuffers)
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001305 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001306 const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001307 ASSERT(colorRenderTarget);
1308
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001309 params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat();
1310 params.colorAttachmentIndexGL = colorIndexGL;
1311 params.colorMaskFlags = colorMaskFlags;
1312 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001313 {
1314 params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT;
1315 }
1316
Geoff Langee244c72019-05-06 10:30:18 -04001317 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001318
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001319 // Clear stencil only once!
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001320 params.clearStencil = false;
1321 }
1322
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001323 // If there was no color clear, clear stencil alone.
1324 if (params.clearStencil)
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001325 {
1326 params.clearColor = false;
Geoff Langee244c72019-05-06 10:30:18 -04001327 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001328 }
1329
1330 return angle::Result::Continue;
Jamie Madill9aef3672018-04-27 11:45:06 -04001331}
1332
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001333angle::Result FramebufferVk::getSamplePosition(const gl::Context *context,
1334 size_t index,
1335 GLfloat *xy) const
JiangYizhoubddc46b2016-12-09 09:50:51 +08001336{
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001337 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -05001338 return angle::Result::Stop;
JiangYizhoubddc46b2016-12-09 09:50:51 +08001339}
1340
Jamie Madilld1249de2018-08-28 16:58:53 -04001341angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001342 const gl::Rectangle &renderArea,
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001343 vk::CommandBuffer **commandBufferOut)
Jamie Madilld1249de2018-08-28 16:58:53 -04001344{
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001345 vk::Framebuffer *framebuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001346 ANGLE_TRY(getFramebuffer(contextVk, &framebuffer));
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001347
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001348 vk::AttachmentOpsArray renderPassAttachmentOps;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001349 std::vector<VkClearValue> attachmentClearValues;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001350
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001351 vk::CommandBuffer *writeCommands = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001352 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands));
Jamie Madille4c5a232018-03-02 21:00:31 -05001353
Jamie Madill49ac74b2017-12-21 14:42:33 -05001354 // Initialize RenderPass info.
Jamie Madill66546be2018-03-08 09:47:20 -05001355 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001356 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madill4c26fc22017-02-24 11:04:10 -05001357 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001358 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001359 ASSERT(colorRenderTarget);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001360
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001361 ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001362
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001363 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1364 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1365 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
1366 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill66546be2018-03-08 09:47:20 -05001367 }
1368
1369 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1370 if (depthStencilRenderTarget)
1371 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001372 ANGLE_TRY(
1373 depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001374
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001375 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1376 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1377 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
1378 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001379 }
1380
Jamie Madilldbc605c2019-01-04 16:39:14 -05001381 return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001382 renderPassAttachmentOps, attachmentClearValues,
1383 commandBufferOut);
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001384}
1385
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001386void FramebufferVk::updateActiveColorMasks(size_t colorIndexGL, bool r, bool g, bool b, bool a)
Jamie Madill9aef3672018-04-27 11:45:06 -04001387{
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001388 mActiveColorComponentMasksForClear[0].set(colorIndexGL, r);
1389 mActiveColorComponentMasksForClear[1].set(colorIndexGL, g);
1390 mActiveColorComponentMasksForClear[2].set(colorIndexGL, b);
1391 mActiveColorComponentMasksForClear[3].set(colorIndexGL, a);
Luc Ferron5fd36932018-06-19 14:55:50 -04001392}
1393
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001394const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const
Luc Ferron5fd36932018-06-19 14:55:50 -04001395{
1396 return mEmulatedAlphaAttachmentMask;
Jamie Madill9aef3672018-04-27 11:45:06 -04001397}
Luc Ferron018709f2018-05-10 13:53:11 -04001398
Jamie Madill21061022018-07-12 23:56:30 -04001399angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk,
1400 const gl::Rectangle &area,
1401 const PackPixelsParams &packPixelsParams,
Jamie Madillb436aac2018-07-18 17:23:48 -04001402 VkImageAspectFlagBits copyAspectFlags,
Jamie Madill21061022018-07-12 23:56:30 -04001403 RenderTargetVk *renderTarget,
1404 void *pixels)
Luc Ferron018709f2018-05-10 13:53:11 -04001405{
Jamie Madill3ea463b2019-06-19 14:21:33 -04001406 ANGLE_TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl");
Jamie Madill58675012018-05-22 14:54:07 -04001407
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001408 RendererVk *renderer = contextVk->getRenderer();
1409
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001410 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001411 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
Jamie Madill58675012018-05-22 14:54:07 -04001412
Jamie Madillbcf467f2018-05-23 09:46:00 -04001413 // Note that although we're reading from the image, we need to update the layout below.
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -05001414 vk::ImageHelper *srcImage =
1415 renderTarget->getImageForRead(&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -04001416
Jamie Madill0631e192019-04-18 16:09:12 -04001417 const angle::Format *readFormat = &srcImage->getFormat().imageFormat();
Luc Ferron1617e692018-07-11 11:08:19 -04001418
Jamie Madillb436aac2018-07-18 17:23:48 -04001419 if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT)
Luc Ferron1617e692018-07-11 11:08:19 -04001420 {
Jamie Madillb436aac2018-07-18 17:23:48 -04001421 readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags);
Luc Ferron1617e692018-07-11 11:08:19 -04001422 }
1423
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001424 size_t level = renderTarget->getLevelIndex();
1425 size_t layer = renderTarget->getLayerIndex();
1426 VkOffset3D srcOffset = {area.x, area.y, 0};
1427 VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height),
1428 1};
1429
1430 // If the source image is multisampled, we need to resolve it into a temporary image before
1431 // performing a readback.
1432 bool isMultisampled = srcImage->getSamples() > 1;
1433 vk::Scoped<vk::ImageHelper> resolvedImage(contextVk->getDevice());
1434 if (isMultisampled)
1435 {
1436 ANGLE_TRY(resolvedImage.get().init2DStaging(
1437 contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1),
1438 srcImage->getFormat(),
1439 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1));
Geoff Langee244c72019-05-06 10:30:18 -04001440 resolvedImage.get().updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001441
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -04001442 // Note: resolve only works on color images (not depth/stencil).
1443 //
1444 // TODO: Currently, depth/stencil blit can perform a depth/stencil readback, but that code
1445 // path will be optimized away. http://anglebug.com/3200
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001446 ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT);
1447
1448 VkImageResolve resolveRegion = {};
1449 resolveRegion.srcSubresource.aspectMask = copyAspectFlags;
1450 resolveRegion.srcSubresource.mipLevel = level;
1451 resolveRegion.srcSubresource.baseArrayLayer = layer;
1452 resolveRegion.srcSubresource.layerCount = 1;
1453 resolveRegion.srcOffset = srcOffset;
1454 resolveRegion.dstSubresource.aspectMask = copyAspectFlags;
1455 resolveRegion.dstSubresource.mipLevel = 0;
1456 resolveRegion.dstSubresource.baseArrayLayer = 0;
1457 resolveRegion.dstSubresource.layerCount = 1;
1458 resolveRegion.dstOffset = {};
1459 resolveRegion.extent = srcExtent;
1460
1461 srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer);
1462
1463 resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc,
1464 commandBuffer);
1465
1466 // Make the resolved image the target of buffer copy.
1467 srcImage = &resolvedImage.get();
1468 level = 0;
1469 layer = 0;
1470 srcOffset = {0, 0, 0};
1471 }
1472
Jamie Madillb980c562018-11-27 11:34:27 -05001473 VkBuffer bufferHandle = VK_NULL_HANDLE;
1474 uint8_t *readPixelBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -04001475 VkDeviceSize stagingOffset = 0;
Jamie Madillb980c562018-11-27 11:34:27 -05001476 size_t allocationSize = readFormat->pixelBytes * area.width * area.height;
Luc Ferron018709f2018-05-10 13:53:11 -04001477
Jamie Madilld754eb52018-07-19 14:55:03 -04001478 ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -05001479 &stagingOffset, nullptr));
Luc Ferron018709f2018-05-10 13:53:11 -04001480
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001481 VkBufferImageCopy region = {};
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001482 region.bufferImageHeight = srcExtent.height;
Jamie Madill4c310832018-08-29 13:43:17 -04001483 region.bufferOffset = stagingOffset;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001484 region.bufferRowLength = srcExtent.width;
1485 region.imageExtent = srcExtent;
1486 region.imageOffset = srcOffset;
Luc Ferron1617e692018-07-11 11:08:19 -04001487 region.imageSubresource.aspectMask = copyAspectFlags;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001488 region.imageSubresource.baseArrayLayer = layer;
Luc Ferron534b00d2018-05-18 08:16:53 -04001489 region.imageSubresource.layerCount = 1;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001490 region.imageSubresource.mipLevel = level;
Luc Ferron534b00d2018-05-18 08:16:53 -04001491
Jamie Madillbcf467f2018-05-23 09:46:00 -04001492 commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(),
1493 bufferHandle, 1, &region);
Luc Ferron018709f2018-05-10 13:53:11 -04001494
1495 // Triggers a full finish.
1496 // TODO(jmadill): Don't block on asynchronous readback.
Geoff Lang892d1802019-03-27 14:21:34 -04001497 ANGLE_TRY(contextVk->finishImpl());
Luc Ferron018709f2018-05-10 13:53:11 -04001498
Luc Ferron534b00d2018-05-18 08:16:53 -04001499 // The buffer we copied to needs to be invalidated before we read from it because its not been
1500 // created with the host coherent bit.
Jamie Madilld754eb52018-07-19 14:55:03 -04001501 ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk));
Yuly Novikov6c6c76c2018-05-17 18:45:06 +00001502
Jamie Madillb436aac2018-07-18 17:23:48 -04001503 PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, readPixelBuffer,
Rafael Cintron05a449a2018-06-20 18:08:04 -07001504 static_cast<uint8_t *>(pixels));
Luc Ferron018709f2018-05-10 13:53:11 -04001505
Jamie Madill7c985f52018-11-29 18:16:17 -05001506 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -04001507}
Jamie Madill58675012018-05-22 14:54:07 -04001508
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001509gl::Extents FramebufferVk::getReadImageExtents() const
Jamie Madill58675012018-05-22 14:54:07 -04001510{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001511 ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width);
1512 ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height);
1513
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001514 return getColorReadRenderTarget()->getExtents();
Jamie Madill58675012018-05-22 14:54:07 -04001515}
Jamie Madill502d2e22018-11-01 11:06:23 -04001516
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001517gl::Rectangle FramebufferVk::getCompleteRenderArea() const
1518{
1519 return gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height);
1520}
1521
1522gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const
1523{
1524 const gl::Rectangle renderArea(0, 0, mState.getDimensions().width,
1525 mState.getDimensions().height);
1526 bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO();
1527
1528 return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport);
1529}
1530
1531void FramebufferVk::onScissorChange(ContextVk *contextVk)
1532{
1533 gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
1534
1535 // If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass
1536 // is restarted. Otherwise, we can continue using the same renderpass area.
1537 //
1538 // Without a scissor, the render pass area covers the whole of the framebuffer. With a
1539 // scissored clear, the render pass area could be smaller than the framebuffer size. When the
1540 // scissor changes, if the scissor area is completely encompassed by the render pass area, it's
1541 // possible to continue using the same render pass. However, if the current render pass area
1542 // is too small, we need to start a new one. The latter can happen if a scissored clear starts
1543 // a render pass, the scissor is disabled and a draw call is issued to affect the whole
1544 // framebuffer.
Geoff Langee244c72019-05-06 10:30:18 -04001545 mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001546 if (mFramebuffer.hasStartedRenderPass() &&
1547 !mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea))
1548 {
Geoff Langee244c72019-05-06 10:30:18 -04001549 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001550 }
1551}
1552
Jamie Madill502d2e22018-11-01 11:06:23 -04001553RenderTargetVk *FramebufferVk::getFirstRenderTarget() const
1554{
1555 for (auto *renderTarget : mRenderTargetCache.getColors())
1556 {
1557 if (renderTarget)
1558 {
1559 return renderTarget;
1560 }
1561 }
1562
1563 return mRenderTargetCache.getDepthStencil();
1564}
1565
1566GLint FramebufferVk::getSamples() const
1567{
1568 RenderTargetVk *firstRT = getFirstRenderTarget();
1569 return firstRT ? firstRT->getImage().getSamples() : 0;
1570}
1571
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001572} // namespace rx