blob: 7aeb3efaedc761806c48a996a89e105809708d45 [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{
Shahbaz Youssefi97123e32019-07-08 15:11:16 -0400185 return invalidate(context, count, attachments);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400186}
187
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400188angle::Result FramebufferVk::invalidate(const gl::Context *context,
189 size_t count,
190 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400191{
Shahbaz Youssefida904482019-07-02 10:49:14 -0400192 mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial());
193
194 if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass())
195 {
196 invalidateImpl(vk::GetImpl(context), count, attachments);
197 }
198
199 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400200}
201
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400202angle::Result FramebufferVk::invalidateSub(const gl::Context *context,
203 size_t count,
204 const GLenum *attachments,
205 const gl::Rectangle &area)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400206{
Shahbaz Youssefida904482019-07-02 10:49:14 -0400207 mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial());
208
209 // RenderPass' storeOp cannot be made conditional to a specific region, so we only apply this
210 // hint if the requested area encompasses the render area.
211 if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass() &&
212 area.encloses(mFramebuffer.getRenderPassRenderArea()))
213 {
214 invalidateImpl(vk::GetImpl(context), count, attachments);
215 }
216
217 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400218}
219
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400220angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400221{
Jamie Madill0cec82a2018-03-14 09:21:07 -0400222 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500223
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400224 bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT));
225 bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT));
226 bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT));
227 gl::DrawBufferMask clearColorBuffers;
228 if (clearColor)
229 {
230 clearColorBuffers = mState.getEnabledDrawBuffers();
231 }
232
233 const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color;
234 const VkClearDepthStencilValue &clearDepthStencilValue =
235 contextVk->getClearDepthStencilValue().depthStencil;
236
237 return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue,
238 clearDepthStencilValue);
239}
240
241angle::Result FramebufferVk::clearImpl(const gl::Context *context,
242 gl::DrawBufferMask clearColorBuffers,
243 bool clearDepth,
244 bool clearStencil,
245 const VkClearColorValue &clearColorValue,
246 const VkClearDepthStencilValue &clearDepthStencilValue)
247{
248 ContextVk *contextVk = vk::GetImpl(context);
249
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400250 const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
251
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400252 // Discard clear altogether if scissor has 0 width or height.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400253 if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400254 {
255 return angle::Result::Continue;
256 }
257
Geoff Langee244c72019-05-06 10:30:18 -0400258 mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400259
260 // This function assumes that only enabled attachments are asked to be cleared.
261 ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers);
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400262
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400263 // Adjust clear behavior based on whether the respective attachments are present; if asked to
264 // clear a non-existent attachment, don't attempt to clear it.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400265
266 VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask();
267 bool clearColor = clearColorBuffers.any();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400268
Jamie Madill0cec82a2018-03-14 09:21:07 -0400269 const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400270 clearDepth = clearDepth && depthAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400271 ASSERT(!clearDepth || depthAttachment->isAttached());
272
273 const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400274 clearStencil = clearStencil && stencilAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400275 ASSERT(!clearStencil || stencilAttachment->isAttached());
276
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400277 uint8_t stencilMask =
278 static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask);
279
280 // The front-end should ensure we don't attempt to clear color if all channels are masked.
281 ASSERT(!clearColor || colorMaskFlags != 0);
282 // The front-end should ensure we don't attempt to clear depth if depth write is disabled.
283 ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask);
284 // The front-end should ensure we don't attempt to clear stencil if all bits are masked.
285 ASSERT(!clearStencil || stencilMask != 0);
286
287 // If there is nothing to clear, return right away (for example, if asked to clear depth, but
288 // there is no depth attachment).
Shahbaz Youssefi02a579e2019-03-27 14:21:20 -0400289 if (!clearColor && !clearDepth && !clearStencil)
290 {
291 return angle::Result::Continue;
292 }
293
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400294 VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue;
Shahbaz Youssefid856ca42018-10-31 16:55:12 -0400295
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400296 // We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If
297 // there's a depth mask, depth clearing is already disabled.
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400298 bool maskedClearColor =
299 clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400300 bool maskedClearStencil = stencilMask != 0xFF;
301
302 bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor;
303 bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400304
305 // At least one of color, depth or stencil should be clearable with render pass loadOp for us
306 // to use this clear path.
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400307 bool clearAnyWithRenderPassLoadOp =
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400308 clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400309
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400310 if (clearAnyWithRenderPassLoadOp)
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400311 {
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400312 // Clearing color is indicated by the set bits in this mask. If not clearing colors with
313 // render pass loadOp, the default value of all-zeros means the clear is not done in
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400314 // clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass
315 // loadOp.
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400316 gl::DrawBufferMask clearBuffersWithRenderPassLoadOp;
317 if (clearColorWithRenderPassLoadOp)
318 {
319 clearBuffersWithRenderPassLoadOp = clearColorBuffers;
320 }
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400321 ANGLE_TRY(clearWithRenderPassOp(
322 contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth,
323 clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue));
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400324
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400325 // On some hardware, having inline commands at this point results in corrupted output. In
326 // that case, end the render pass immediately. http://anglebug.com/2361
Jonah Ryan-Davis776694c2019-05-08 10:28:55 -0400327 if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled)
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400328 {
Geoff Langee244c72019-05-06 10:30:18 -0400329 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400330 }
331
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400332 // Fallback to other methods for whatever isn't cleared here.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400333 clearDepth = false;
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400334 if (clearColorWithRenderPassLoadOp)
335 {
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400336 clearColorBuffers.reset();
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400337 clearColor = false;
338 }
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400339 if (clearStencilWithRenderPassLoadOp)
340 {
341 clearStencil = false;
342 }
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400343
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400344 // If nothing left to clear, early out.
345 if (!clearColor && !clearStencil)
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400346 {
347 return angle::Result::Continue;
348 }
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400349 }
350
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400351 // Note: depth clear is always done through render pass loadOp.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400352 ASSERT(clearDepth == false);
353
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400354 // The most costly clear mode is when we need to mask out specific color channels or stencil
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400355 // bits. This can only be done with a draw call.
356 return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil,
357 colorMaskFlags, stencilMask, clearColorValue,
358 static_cast<uint8_t>(modifiedDepthStencilValue.stencil));
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400359}
360
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400361angle::Result FramebufferVk::clearBufferfv(const gl::Context *context,
362 GLenum buffer,
363 GLint drawbuffer,
364 const GLfloat *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400365{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400366 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400367
368 bool clearDepth = false;
369 gl::DrawBufferMask clearColorBuffers;
370
371 if (buffer == GL_DEPTH)
372 {
373 clearDepth = true;
374 clearValue.depthStencil.depth = values[0];
375 }
376 else
377 {
378 clearColorBuffers.set(drawbuffer);
379 clearValue.color.float32[0] = values[0];
380 clearValue.color.float32[1] = values[1];
381 clearValue.color.float32[2] = values[2];
382 clearValue.color.float32[3] = values[3];
383 }
384
385 return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color,
386 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400387}
388
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400389angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context,
390 GLenum buffer,
391 GLint drawbuffer,
392 const GLuint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400393{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400394 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400395
396 gl::DrawBufferMask clearColorBuffers;
397 clearColorBuffers.set(drawbuffer);
398
399 clearValue.color.uint32[0] = values[0];
400 clearValue.color.uint32[1] = values[1];
401 clearValue.color.uint32[2] = values[2];
402 clearValue.color.uint32[3] = values[3];
403
404 return clearImpl(context, clearColorBuffers, false, false, clearValue.color,
405 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400406}
407
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400408angle::Result FramebufferVk::clearBufferiv(const gl::Context *context,
409 GLenum buffer,
410 GLint drawbuffer,
411 const GLint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400412{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400413 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400414
415 bool clearStencil = false;
416 gl::DrawBufferMask clearColorBuffers;
417
418 if (buffer == GL_STENCIL)
419 {
420 clearStencil = true;
421 clearValue.depthStencil.stencil =
422 gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max());
423 }
424 else
425 {
426 clearColorBuffers.set(drawbuffer);
427 clearValue.color.int32[0] = values[0];
428 clearValue.color.int32[1] = values[1];
429 clearValue.color.int32[2] = values[2];
430 clearValue.color.int32[3] = values[3];
431 }
432
433 return clearImpl(context, clearColorBuffers, false, clearStencil, clearValue.color,
434 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400435}
436
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400437angle::Result FramebufferVk::clearBufferfi(const gl::Context *context,
438 GLenum buffer,
439 GLint drawbuffer,
440 GLfloat depth,
441 GLint stencil)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400442{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400443 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400444
445 clearValue.depthStencil.depth = depth;
446 clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max());
447
448 return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color,
449 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400450}
451
Jamie Madill4928b7c2017-06-20 12:57:39 -0400452GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400453{
Jamie Madill66546be2018-03-08 09:47:20 -0500454 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400455}
456
Jamie Madill4928b7c2017-06-20 12:57:39 -0400457GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400458{
Jamie Madill66546be2018-03-08 09:47:20 -0500459 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400460}
461
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400462angle::Result FramebufferVk::readPixels(const gl::Context *context,
463 const gl::Rectangle &area,
464 GLenum format,
465 GLenum type,
466 void *pixels)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400467{
Luc Ferrona1c72422018-05-14 15:58:28 -0400468 // Clip read area to framebuffer.
469 const gl::Extents &fbSize = getState().getReadAttachment()->getSize();
470 const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400471 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400472
Luc Ferrona1c72422018-05-14 15:58:28 -0400473 gl::Rectangle clippedArea;
474 if (!ClipRectangle(area, fbRect, &clippedArea))
475 {
476 // nothing to read
Jamie Madill7c985f52018-11-29 18:16:17 -0500477 return angle::Result::Continue;
Luc Ferrona1c72422018-05-14 15:58:28 -0400478 }
Luc Ferronbf6dc372018-06-28 15:24:19 -0400479 gl::Rectangle flippedArea = clippedArea;
Geoff Lange076a232018-07-16 15:34:05 -0400480 if (contextVk->isViewportFlipEnabledForReadFBO())
Luc Ferronbf6dc372018-06-28 15:24:19 -0400481 {
482 flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height;
483 }
Luc Ferrona1c72422018-05-14 15:58:28 -0400484
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500485 const gl::State &glState = context->getState();
Frank Henigman1ffad842018-09-24 23:40:45 -0400486 const gl::PixelPackState &packState = glState.getPackState();
Luc Ferronbf6dc372018-06-28 15:24:19 -0400487
Luc Ferrona1c72422018-05-14 15:58:28 -0400488 const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type);
489
490 GLuint outputPitch = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400491 ANGLE_VK_CHECK_MATH(contextVk,
492 sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment,
493 packState.rowLength, &outputPitch));
Luc Ferrona1c72422018-05-14 15:58:28 -0400494 GLuint outputSkipBytes = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400495 ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState,
496 false, &outputSkipBytes));
Luc Ferrona1c72422018-05-14 15:58:28 -0400497
498 outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes +
499 (clippedArea.y - area.y) * outputPitch;
Luc Ferron60284222018-03-20 16:01:44 -0400500
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400501 const angle::Format &angleFormat = GetFormatFromFormatType(format, type);
502
Frank Henigman1ffad842018-09-24 23:40:45 -0400503 PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder,
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400504 glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0);
Frank Henigman1ffad842018-09-24 23:40:45 -0400505 if (contextVk->isViewportFlipEnabledForReadFBO())
506 {
507 params.reverseRowOrder = !params.reverseRowOrder;
508 }
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500509
Jamie Madill21061022018-07-12 23:56:30 -0400510 ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400511 getColorReadRenderTarget(),
Rafael Cintron05a449a2018-06-20 18:08:04 -0700512 static_cast<uint8_t *>(pixels) + outputSkipBytes));
Jamie Madillc773ab92019-06-25 17:11:58 -0400513 mReadPixelBuffer.releaseInFlightBuffers(contextVk);
Jamie Madill7c985f52018-11-29 18:16:17 -0500514 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -0400515}
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500516
Luc Ferron26581112018-06-21 09:43:08 -0400517RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const
518{
519 return mRenderTargetCache.getDepthStencil();
520}
521
Jamie Madill58675012018-05-22 14:54:07 -0400522RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const
Luc Ferron018709f2018-05-10 13:53:11 -0400523{
524 RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400525 ASSERT(renderTarget && renderTarget->getImage().valid());
Luc Ferron018709f2018-05-10 13:53:11 -0400526 return renderTarget;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400527}
528
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400529angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400530 const gl::Rectangle &sourceArea,
531 const gl::Rectangle &destArea,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400532 RenderTargetVk *readRenderTarget,
533 RenderTargetVk *drawRenderTarget,
534 GLenum filter,
535 bool colorBlit,
536 bool depthBlit,
537 bool stencilBlit,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400538 bool flipX,
539 bool flipY)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400540{
541 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
542 // it should never be the case that both color and depth/stencil need to be blitted at
543 // at the same time.
544 ASSERT(colorBlit != (depthBlit || stencilBlit));
545
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400546 vk::ImageHelper *srcImage = &readRenderTarget->getImage();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400547 vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
548
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400549 VkImageAspectFlags imageAspectMask = srcImage->getAspectFlags();
550 VkImageAspectFlags blitAspectMask = imageAspectMask;
551
552 // Remove depth or stencil aspects if they are not requested to be blitted.
553 if (!depthBlit)
554 {
555 blitAspectMask &= ~VK_IMAGE_ASPECT_DEPTH_BIT;
556 }
557 if (!stencilBlit)
558 {
559 blitAspectMask &= ~VK_IMAGE_ASPECT_STENCIL_BIT;
560 }
561
562 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
563 {
564 vk::CommandBuffer *srcLayoutChange;
565 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
566 srcImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferSrc, srcLayoutChange);
567 }
568
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400569 vk::CommandBuffer *commandBuffer = nullptr;
570 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
571
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400572 srcImage->addReadDependency(&mFramebuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400573
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400574 VkImageBlit blit = {};
575 blit.srcSubresource.aspectMask = blitAspectMask;
576 blit.srcSubresource.mipLevel = readRenderTarget->getLevelIndex();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400577 blit.srcSubresource.baseArrayLayer = readRenderTarget->getLayerIndex();
578 blit.srcSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400579 blit.srcOffsets[0] = {sourceArea.x0(), sourceArea.y0(), 0};
580 blit.srcOffsets[1] = {sourceArea.x1(), sourceArea.y1(), 1};
581 blit.dstSubresource.aspectMask = blitAspectMask;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400582 blit.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
583 blit.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
584 blit.dstSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400585 blit.dstOffsets[0] = {destArea.x0(), destArea.y0(), 0};
586 blit.dstOffsets[1] = {destArea.x1(), destArea.y1(), 1};
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400587
588 // Requirement of the copyImageToBuffer, the dst image must be in
589 // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400590 dstImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferDst, commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400591
592 commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
593 dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
594 gl_vk::GetFilter(filter));
595
596 return angle::Result::Continue;
597}
598
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400599angle::Result FramebufferVk::blit(const gl::Context *context,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400600 const gl::Rectangle &sourceAreaIn,
601 const gl::Rectangle &destAreaIn,
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400602 GLbitfield mask,
603 GLenum filter)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400604{
Luc Ferron26581112018-06-21 09:43:08 -0400605 ContextVk *contextVk = vk::GetImpl(context);
606 RendererVk *renderer = contextVk->getRenderer();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400607 UtilsVk &utilsVk = contextVk->getUtils();
Luc Ferron26581112018-06-21 09:43:08 -0400608
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400609 const gl::State &glState = contextVk->getState();
610 const gl::Framebuffer *srcFramebuffer = glState.getReadFramebuffer();
611
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400612 const bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0;
613 const bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0;
614 const bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400615
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400616 const bool isResolve = srcFramebuffer->getCachedSamples(context) > 1;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400617
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400618 FramebufferVk *srcFramebufferVk = vk::GetImpl(srcFramebuffer);
619 const bool srcFramebufferFlippedY = contextVk->isViewportFlipEnabledForReadFBO();
620 const bool destFramebufferFlippedY = contextVk->isViewportFlipEnabledForDrawFBO();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400621
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400622 gl::Rectangle sourceArea = sourceAreaIn;
623 gl::Rectangle destArea = destAreaIn;
624
625 // Note: GLES (all 3.x versions) require source and dest area to be identical when
626 // resolving.
627 ASSERT(!isResolve ||
628 (sourceArea.x == destArea.x && sourceArea.y == destArea.y &&
629 sourceArea.width == destArea.width && sourceArea.height == destArea.height));
630
631 const gl::Rectangle srcFramebufferDimensions =
632 srcFramebufferVk->mState.getDimensions().toRect();
633
634 // If the destination is flipped in either direction, we will flip the source instead so that
635 // the destination area is always unflipped.
636 sourceArea = sourceArea.flip(destArea.isReversedX(), destArea.isReversedY());
637 destArea = destArea.removeReversal();
638
639 // Calculate the stretch factor prior to any clipping, as it needs to remain constant.
640 const float stretch[2] = {
641 std::abs(sourceArea.width / static_cast<float>(destArea.width)),
642 std::abs(sourceArea.height / static_cast<float>(destArea.height)),
643 };
644
645 // First, clip the source area to framebuffer. That requires transforming the dest area to
646 // match the clipped source.
647 gl::Rectangle absSourceArea = sourceArea.removeReversal();
648 gl::Rectangle clippedSourceArea;
649 if (!gl::ClipRectangle(srcFramebufferDimensions, absSourceArea, &clippedSourceArea))
650 {
651 return angle::Result::Continue;
652 }
653
654 // Resize the destination area based on the new size of source. Note again that stretch is
655 // calculated as SrcDimension/DestDimension.
656 gl::Rectangle srcClippedDestArea;
657 if (isResolve)
658 {
659 // Source and dest areas are identical in resolve.
660 srcClippedDestArea = clippedSourceArea;
661 }
662 else if (clippedSourceArea == absSourceArea)
663 {
664 // If there was no clipping, keep dest area as is.
665 srcClippedDestArea = destArea;
666 }
667 else
668 {
669 // Shift dest area's x0,y0,x1,y1 by as much as the source area's got shifted (taking
670 // stretching into account)
671 float x0Shift = std::round((clippedSourceArea.x - absSourceArea.x) / stretch[0]);
672 float y0Shift = std::round((clippedSourceArea.y - absSourceArea.y) / stretch[1]);
673 float x1Shift = std::round((absSourceArea.x1() - clippedSourceArea.x1()) / stretch[0]);
674 float y1Shift = std::round((absSourceArea.y1() - clippedSourceArea.y1()) / stretch[1]);
675
676 // If the source area was reversed in any direction, the shift should be applied in the
677 // opposite direction as well.
678 if (sourceArea.isReversedX())
679 {
680 std::swap(x0Shift, x1Shift);
681 }
682
683 if (sourceArea.isReversedY())
684 {
685 std::swap(y0Shift, y1Shift);
686 }
687
688 srcClippedDestArea.x = destArea.x0() + static_cast<int>(x0Shift);
689 srcClippedDestArea.y = destArea.y0() + static_cast<int>(y0Shift);
690 int x1 = destArea.x1() - static_cast<int>(x1Shift);
691 int y1 = destArea.y1() - static_cast<int>(y1Shift);
692
693 srcClippedDestArea.width = x1 - srcClippedDestArea.x;
694 srcClippedDestArea.height = y1 - srcClippedDestArea.y;
695 }
696
697 // If framebuffers are flipped in Y, flip the source and dest area (which define the
698 // transformation regardless of clipping), as well as the blit area (which is the clipped
699 // dest area).
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400700 if (srcFramebufferFlippedY)
701 {
702 sourceArea.y = srcFramebufferDimensions.height - sourceArea.y;
703 sourceArea.height = -sourceArea.height;
704 }
705 if (destFramebufferFlippedY)
706 {
707 destArea.y = mState.getDimensions().height - destArea.y;
708 destArea.height = -destArea.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400709
710 srcClippedDestArea.y =
711 mState.getDimensions().height - srcClippedDestArea.y - srcClippedDestArea.height;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400712 }
713
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400714 const bool flipX = sourceArea.isReversedX() != destArea.isReversedX();
715 const bool flipY = sourceArea.isReversedY() != destArea.isReversedY();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400716
717 // GLES doesn't allow flipping the parameters of glBlitFramebuffer if performing a resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400718 ASSERT(!isResolve ||
719 (flipX == false && flipY == (srcFramebufferFlippedY != destFramebufferFlippedY)));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400720
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400721 // Again, transfer the destination flip to source, so dest is unflipped. Note that destArea
722 // was not reversed until the final possible Y-flip.
723 ASSERT(!destArea.isReversedX());
724 sourceArea = sourceArea.flip(false, destArea.isReversedY());
725 destArea = destArea.removeReversal();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400726
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400727 // Clip the destination area to the framebuffer size and scissor. Note that we don't care
728 // about the source area anymore. The offset translation is done based on the original source
729 // and destination rectangles. The stretch factor is already calculated as well.
730 gl::Rectangle blitArea;
731 if (!gl::ClipRectangle(getScissoredRenderArea(contextVk), srcClippedDestArea, &blitArea))
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400732 {
733 return angle::Result::Continue;
734 }
735
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400736 bool noClip = blitArea == destArea && stretch[0] == 1.0f && stretch[1] == 1.0f;
737 bool noFlip = !flipX && !flipY;
738 bool disableFlippingBlitWithCommand =
739 contextVk->getRenderer()->getFeatures().disableFlippingBlitWithCommand.enabled;
740
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400741 UtilsVk::BlitResolveParameters params;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400742 params.srcOffset[0] = sourceArea.x;
743 params.srcOffset[1] = sourceArea.y;
744 params.destOffset[0] = destArea.x;
745 params.destOffset[1] = destArea.y;
746 params.stretch[0] = stretch[0];
747 params.stretch[1] = stretch[1];
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400748 params.srcExtents[0] = srcFramebufferDimensions.width;
749 params.srcExtents[1] = srcFramebufferDimensions.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400750 params.blitArea = blitArea;
751 params.linear = filter == GL_LINEAR;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400752 params.flipX = flipX;
753 params.flipY = flipY;
754
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400755 if (blitColorBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400756 {
757 RenderTargetVk *readRenderTarget = srcFramebufferVk->getColorReadRenderTarget();
758 params.srcLayer = readRenderTarget->getLayerIndex();
759
760 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400761 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400762
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400763 // If there was no clipping and the format capabilities allow us, use Vulkan's builtin blit.
764 // The reason clipping is prohibited in this path is that due to rounding errors, it would
765 // be hard to guarantee the image stretching remains perfect. That also allows us not to
766 // have to transform back the dest clipping to source.
767 //
768 // For simplicity, we either blit all render targets with a Vulkan command, or none.
769 bool canBlitWithCommand = !isResolve && noClip &&
770 (noFlip || !disableFlippingBlitWithCommand) &&
771 HasSrcBlitFeature(renderer, readRenderTarget);
772 bool areChannelsBlitCompatible = true;
773 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
774 {
775 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
776 canBlitWithCommand =
777 canBlitWithCommand && HasDstBlitFeature(renderer, drawRenderTarget);
778 areChannelsBlitCompatible =
779 areChannelsBlitCompatible &&
780 areSrcAndDstColorChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
781 }
782
783 if (canBlitWithCommand && areChannelsBlitCompatible)
784 {
785 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
786 {
787 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
788 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
789 drawRenderTarget, filter, true, false, false, flipX,
790 flipY));
791 }
792 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400793 // If we're not flipping, use Vulkan's builtin resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400794 else if (isResolve && !flipX && !flipY && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400795 {
796 ANGLE_TRY(resolveColorWithCommand(contextVk, params, &readRenderTarget->getImage()));
797 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400798 // Otherwise use a shader to do blit or resolve.
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400799 else
800 {
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400801 ANGLE_TRY(utilsVk.colorBlitResolve(contextVk, this, &readRenderTarget->getImage(),
802 readRenderTarget->getFetchImageView(), params));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400803 }
804 }
805
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400806 if (blitDepthBuffer || blitStencilBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400807 {
808 RenderTargetVk *readRenderTarget = srcFramebufferVk->getDepthStencilRenderTarget();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400809 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400810 params.srcLayer = readRenderTarget->getLayerIndex();
811
812 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400813 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400814
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400815 // Similarly, only blit if there's been no clipping.
816 bool canBlitWithCommand = !isResolve && noClip &&
817 (noFlip || !disableFlippingBlitWithCommand) &&
818 HasSrcBlitFeature(renderer, readRenderTarget) &&
819 HasDstBlitFeature(renderer, drawRenderTarget);
820 bool areChannelsBlitCompatible =
821 areSrcAndDstDepthStencilChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400822
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400823 if (canBlitWithCommand && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400824 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400825 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
826 drawRenderTarget, filter, false, blitDepthBuffer,
827 blitStencilBuffer, flipX, flipY));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400828 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400829 else
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400830 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400831 // Create depth- and stencil-only views for reading.
832 vk::Scoped<vk::ImageView> depthView(contextVk->getDevice());
833 vk::Scoped<vk::ImageView> stencilView(contextVk->getDevice());
834
835 vk::ImageHelper *depthStencilImage = &readRenderTarget->getImage();
836 uint32_t levelIndex = readRenderTarget->getLevelIndex();
837 uint32_t layerIndex = readRenderTarget->getLayerIndex();
838 gl::TextureType textureType = vk::Get2DTextureType(depthStencilImage->getLayerCount(),
839 depthStencilImage->getSamples());
840
841 if (blitDepthBuffer)
842 {
843 ANGLE_TRY(depthStencilImage->initLayerImageView(
844 contextVk, textureType, VK_IMAGE_ASPECT_DEPTH_BIT, gl::SwizzleState(),
845 &depthView.get(), levelIndex, 1, layerIndex, 1));
846 }
847
848 if (blitStencilBuffer)
849 {
850 ANGLE_TRY(depthStencilImage->initLayerImageView(
851 contextVk, textureType, VK_IMAGE_ASPECT_STENCIL_BIT, gl::SwizzleState(),
852 &stencilView.get(), levelIndex, 1, layerIndex, 1));
853 }
854
855 // If shader stencil export is not possible, defer stencil blit/stencil to another pass.
856 bool hasShaderStencilExport =
857 contextVk->getRenderer()->getFeatures().supportsShaderStencilExport.enabled;
858
859 // Blit depth. If shader stencil export is present, blit stencil as well.
860 if (blitDepthBuffer || (blitStencilBuffer && hasShaderStencilExport))
861 {
862 vk::ImageView *depth = blitDepthBuffer ? &depthView.get() : nullptr;
863 vk::ImageView *stencil =
864 blitStencilBuffer && hasShaderStencilExport ? &stencilView.get() : nullptr;
865
866 ANGLE_TRY(utilsVk.depthStencilBlitResolve(contextVk, this, depthStencilImage, depth,
867 stencil, params));
868 }
869
870 // If shader stencil export is not present, blit stencil through a different path.
871 if (blitStencilBuffer && !hasShaderStencilExport)
872 {
873 ANGLE_TRY(utilsVk.stencilBlitResolveNoShaderExport(
874 contextVk, this, depthStencilImage, &stencilView.get(), params));
875 }
876
877 vk::ImageView depthViewObject = depthView.release();
878 vk::ImageView stencilViewObject = stencilView.release();
879
880 contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &depthViewObject);
881 contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &stencilViewObject);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400882 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400883 }
884
885 return angle::Result::Continue;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400886} // namespace rx
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400887
888angle::Result FramebufferVk::resolveColorWithCommand(ContextVk *contextVk,
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400889 const UtilsVk::BlitResolveParameters &params,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400890 vk::ImageHelper *srcImage)
891{
892 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
893 {
894 vk::CommandBuffer *srcLayoutChange;
895 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
896 srcImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferSrc,
897 srcLayoutChange);
898 }
Jamie Madill16c20142018-10-01 13:58:19 -0400899
Shahbaz Youssefi2660b502019-03-21 12:08:40 -0400900 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill16c20142018-10-01 13:58:19 -0400901 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
902
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400903 // Source's layout change should happen before rendering
904 srcImage->addReadDependency(&mFramebuffer);
Luc Ferron26581112018-06-21 09:43:08 -0400905
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400906 VkImageResolve resolveRegion = {};
907 resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
908 resolveRegion.srcSubresource.mipLevel = 0;
909 resolveRegion.srcSubresource.baseArrayLayer = params.srcLayer;
910 resolveRegion.srcSubresource.layerCount = 1;
911 resolveRegion.srcOffset.x = params.srcOffset[0];
912 resolveRegion.srcOffset.y = params.srcOffset[1];
913 resolveRegion.srcOffset.z = 0;
914 resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
915 resolveRegion.dstSubresource.layerCount = 1;
916 resolveRegion.dstOffset.x = params.destOffset[0];
917 resolveRegion.dstOffset.y = params.destOffset[1];
918 resolveRegion.dstOffset.z = 0;
919 resolveRegion.extent.width = params.srcExtents[0];
920 resolveRegion.extent.height = params.srcExtents[1];
921 resolveRegion.extent.depth = 1;
Jamie Madilld754eb52018-07-19 14:55:03 -0400922
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400923 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Luc Ferron82eda932018-07-09 15:10:22 -0400924 {
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400925 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400926 vk::ImageHelper *drawImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
927 drawImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferDst,
928 commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400929
930 resolveRegion.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
931 resolveRegion.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
932
933 srcImage->resolve(&drawRenderTarget->getImage(), resolveRegion, commandBuffer);
Luc Ferron82eda932018-07-09 15:10:22 -0400934 }
935
Jamie Madill7c985f52018-11-29 18:16:17 -0500936 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400937}
938
Kenneth Russellce8602a2017-10-03 18:23:08 -0700939bool FramebufferVk::checkStatus(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400940{
Luc Ferron5bdf8bd2018-06-20 09:51:37 -0400941 // if we have both a depth and stencil buffer, they must refer to the same object
942 // since we only support packed_depth_stencil and not separate depth and stencil
943 if (mState.hasSeparateDepthAndStencilAttachments())
944 {
945 return false;
946 }
947
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400948 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400949}
950
Jamie Madill67220092019-05-20 11:12:53 -0400951angle::Result FramebufferVk::updateColorAttachment(const gl::Context *context, size_t colorIndexGL)
952{
953 ContextVk *contextVk = vk::GetImpl(context);
954
955 ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL));
956
957 // Update cached masks for masked clears.
958 RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL];
959 if (renderTarget)
960 {
961 const angle::Format &emulatedFormat = renderTarget->getImageFormat().imageFormat();
962 updateActiveColorMasks(colorIndexGL, emulatedFormat.redBits > 0,
963 emulatedFormat.greenBits > 0, emulatedFormat.blueBits > 0,
964 emulatedFormat.alphaBits > 0);
965
966 const angle::Format &sourceFormat = renderTarget->getImageFormat().angleFormat();
967 mEmulatedAlphaAttachmentMask.set(
968 colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0);
969
970 contextVk->updateColorMask(context->getState().getBlendState());
971 }
972 else
973 {
974 updateActiveColorMasks(colorIndexGL, false, false, false, false);
975 }
976
977 return angle::Result::Continue;
978}
979
Shahbaz Youssefida904482019-07-02 10:49:14 -0400980void FramebufferVk::invalidateImpl(ContextVk *contextVk, size_t count, const GLenum *attachments)
981{
982 ASSERT(mFramebuffer.hasStartedRenderPass());
983
984 gl::DrawBufferMask invalidateColorBuffers;
985 bool invalidateDepthBuffer = false;
986 bool invalidateStencilBuffer = false;
987
988 for (size_t i = 0; i < count; ++i)
989 {
990 const GLenum attachment = attachments[i];
991
992 switch (attachment)
993 {
994 case GL_DEPTH:
995 case GL_DEPTH_ATTACHMENT:
996 invalidateDepthBuffer = true;
997 break;
998 case GL_STENCIL:
999 case GL_STENCIL_ATTACHMENT:
1000 invalidateStencilBuffer = true;
1001 break;
1002 case GL_DEPTH_STENCIL_ATTACHMENT:
1003 invalidateDepthBuffer = true;
1004 invalidateStencilBuffer = true;
1005 break;
1006 default:
1007 ASSERT(
1008 (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) ||
1009 (attachment == GL_COLOR));
1010
1011 invalidateColorBuffers.set(
1012 attachment == GL_COLOR ? 0u : (attachment - GL_COLOR_ATTACHMENT0));
1013 }
1014 }
1015
1016 // Set the appropriate storeOp for attachments.
1017 size_t attachmentIndexVk = 0;
1018 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
1019 {
1020 if (invalidateColorBuffers.test(colorIndexGL))
1021 {
1022 mFramebuffer.invalidateRenderPassColorAttachment(attachmentIndexVk);
1023 }
1024 ++attachmentIndexVk;
1025 }
1026
1027 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1028 if (depthStencilRenderTarget)
1029 {
1030 if (invalidateDepthBuffer)
1031 {
1032 mFramebuffer.invalidateRenderPassDepthAttachment(attachmentIndexVk);
1033 }
1034
1035 if (invalidateStencilBuffer)
1036 {
1037 mFramebuffer.invalidateRenderPassStencilAttachment(attachmentIndexVk);
1038 }
1039 }
1040
1041 // NOTE: Possible future optimization is to delay setting the storeOp and only do so if the
1042 // render pass is closed by itself before another draw call. Otherwise, in a situation like
1043 // this:
1044 //
1045 // draw()
1046 // invalidate()
1047 // draw()
1048 //
1049 // We would be discarding the attachments only to load them for the next draw (which is less
1050 // efficient than keeping the render pass open and not do the discard at all). While dEQP tests
1051 // this pattern, this optimization may not be necessary if no application does this. It is
1052 // expected that an application would invalidate() when it's done with the framebuffer, so the
1053 // render pass would have closed either way.
1054 mFramebuffer.finishCurrentCommands(contextVk);
1055}
1056
Jamie Madill6f755b22018-10-09 12:48:54 -04001057angle::Result FramebufferVk::syncState(const gl::Context *context,
1058 const gl::Framebuffer::DirtyBits &dirtyBits)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001059{
Jamie Madille1f3ad42017-10-28 23:00:42 -04001060 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001061
1062 ASSERT(dirtyBits.any());
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001063 for (size_t dirtyBit : dirtyBits)
1064 {
1065 switch (dirtyBit)
1066 {
1067 case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
1068 case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
1069 ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState));
1070 break;
Jamie Madill67220092019-05-20 11:12:53 -04001071 case gl::Framebuffer::DIRTY_BIT_DEPTH_BUFFER_CONTENTS:
1072 case gl::Framebuffer::DIRTY_BIT_STENCIL_BUFFER_CONTENTS:
1073 ANGLE_TRY(mRenderTargetCache.getDepthStencil()->flushStagedUpdates(contextVk));
1074 break;
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001075 case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
1076 case gl::Framebuffer::DIRTY_BIT_READ_BUFFER:
1077 case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
1078 case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
1079 case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
1080 case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1081 break;
1082 default:
1083 {
Jamie Madill67220092019-05-20 11:12:53 -04001084 static_assert(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits");
1085 if (dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX)
Jamie Madill9aef3672018-04-27 11:45:06 -04001086 {
Jamie Madill67220092019-05-20 11:12:53 -04001087 size_t colorIndexGL = static_cast<size_t>(
1088 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
1089 ANGLE_TRY(updateColorAttachment(context, colorIndexGL));
Jamie Madill9aef3672018-04-27 11:45:06 -04001090 }
1091 else
1092 {
Jamie Madill67220092019-05-20 11:12:53 -04001093 ASSERT(dirtyBit >= gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 &&
1094 dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_MAX);
1095 size_t colorIndexGL = static_cast<size_t>(
1096 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0);
1097 ANGLE_TRY(mRenderTargetCache.getColors()[colorIndexGL]->flushStagedUpdates(
1098 contextVk));
Jamie Madill9aef3672018-04-27 11:45:06 -04001099 }
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001100 break;
1101 }
1102 }
1103 }
Jamie Madill66546be2018-03-08 09:47:20 -05001104
Jamie Madill9aef3672018-04-27 11:45:06 -04001105 mActiveColorComponents = gl_vk::GetColorComponentFlags(
Luc Ferron5fd36932018-06-19 14:55:50 -04001106 mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(),
1107 mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any());
Jamie Madill9aef3672018-04-27 11:45:06 -04001108
Geoff Langee244c72019-05-06 10:30:18 -04001109 mFramebuffer.release(contextVk);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001110
Jamie Madill316c6062018-05-29 10:49:45 -04001111 // Will freeze the current set of dependencies on this FBO. The next time we render we will
Jamie Madilla5e06072018-05-18 14:36:05 -04001112 // create a new entry in the command graph.
Geoff Langee244c72019-05-06 10:30:18 -04001113 mFramebuffer.finishCurrentCommands(contextVk);
Jamie Madill72106562017-03-24 14:18:50 -04001114
Jamie Madilldbc605c2019-01-04 16:39:14 -05001115 // Notify the ContextVk to update the pipeline desc.
1116 updateRenderPassDesc();
Shahbaz Youssefida904482019-07-02 10:49:14 -04001117
1118 FramebufferVk *currentDrawFramebuffer = vk::GetImpl(context->getState().getDrawFramebuffer());
1119 if (currentDrawFramebuffer == this)
1120 {
1121 contextVk->onDrawFramebufferChange(this);
1122 }
Jamie Madill19fa1c62018-03-08 09:47:21 -05001123
Jamie Madill7c985f52018-11-29 18:16:17 -05001124 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001125}
1126
Jamie Madilldbc605c2019-01-04 16:39:14 -05001127void FramebufferVk::updateRenderPassDesc()
Jamie Madillab9f9c32017-01-17 17:47:34 -05001128{
Jamie Madilldbc605c2019-01-04 16:39:14 -05001129 mRenderPassDesc = {};
1130 mRenderPassDesc.setSamples(getSamples());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001131
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001132 const auto &colorRenderTargets = mRenderTargetCache.getColors();
1133 const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers();
1134 for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001135 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001136 if (enabledDrawBuffers[colorIndexGL])
1137 {
1138 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
1139 ASSERT(colorRenderTarget);
1140 mRenderPassDesc.packColorAttachment(
1141 colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID);
1142 }
1143 else
1144 {
1145 mRenderPassDesc.packColorAttachmentGap(colorIndexGL);
1146 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001147 }
1148
Jamie Madill66546be2018-03-08 09:47:20 -05001149 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1150 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001151 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001152 mRenderPassDesc.packDepthStencilAttachment(
1153 depthStencilRenderTarget->getImage().getFormat().angleFormatID);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001154 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001155}
1156
Jamie Madill21061022018-07-12 23:56:30 -04001157angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001158{
1159 // If we've already created our cached Framebuffer, return it.
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001160 if (mFramebuffer.valid())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001161 {
Jamie Madille8dd0792018-09-27 15:04:27 -04001162 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001163 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001164 }
1165
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001166 vk::RenderPass *compatibleRenderPass = nullptr;
Geoff Langee244c72019-05-06 10:30:18 -04001167 ANGLE_TRY(contextVk->getCompatibleRenderPass(mRenderPassDesc, &compatibleRenderPass));
Jamie Madillab9f9c32017-01-17 17:47:34 -05001168
1169 // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it.
1170 if (mBackbuffer)
1171 {
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001172 return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001173 }
1174
1175 // Gather VkImageViews over all FBO attachments, also size of attached region.
1176 std::vector<VkImageView> attachments;
1177 gl::Extents attachmentsSize;
1178
Jamie Madill66546be2018-03-08 09:47:20 -05001179 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001180 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001181 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001182 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001183 ASSERT(colorRenderTarget);
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001184 attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001185
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001186 ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents());
1187 attachmentsSize = colorRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001188 }
1189
Jamie Madill66546be2018-03-08 09:47:20 -05001190 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1191 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001192 {
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001193 attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001194
Jamie Madillbc543422018-03-30 10:43:19 -04001195 ASSERT(attachmentsSize.empty() ||
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001196 attachmentsSize == depthStencilRenderTarget->getExtents());
1197 attachmentsSize = depthStencilRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001198 }
1199
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001200 VkFramebufferCreateInfo framebufferInfo = {};
Jamie Madillab9f9c32017-01-17 17:47:34 -05001201
1202 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001203 framebufferInfo.flags = 0;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001204 framebufferInfo.renderPass = compatibleRenderPass->getHandle();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001205 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
1206 framebufferInfo.pAttachments = attachments.data();
1207 framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width);
1208 framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height);
1209 framebufferInfo.layers = 1;
1210
Jamie Madill21061022018-07-12 23:56:30 -04001211 ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo));
Jamie Madill5deea722017-02-16 10:44:46 -05001212
Jamie Madille8dd0792018-09-27 15:04:27 -04001213 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001214 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001215}
1216
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001217angle::Result FramebufferVk::clearWithRenderPassOp(
1218 ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001219 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001220 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001221 bool clearDepth,
1222 bool clearStencil,
1223 const VkClearColorValue &clearColorValue,
1224 const VkClearDepthStencilValue &clearDepthStencilValue)
1225{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001226 // Start a new render pass if:
1227 //
1228 // - no render pass has started,
1229 // - there is a render pass started but it contains commands; we cannot modify its ops, so new
1230 // render pass is needed,
1231 // - the current render area doesn't match the clear area. We need the render area to be
1232 // exactly as specified by the scissor for the loadOp to clear only that area. See
1233 // onScissorChange for more information.
1234
1235 if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() ||
1236 mFramebuffer.getRenderPassRenderArea() != clearArea)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001237 {
1238 vk::CommandBuffer *commandBuffer;
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001239 ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer));
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001240 }
1241
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001242 size_t attachmentIndexVk = 0;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001243
1244 // Go through clearColorBuffers and set the appropriate loadOp and clear values.
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001245 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001246 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001247 if (clearColorBuffers.test(colorIndexGL))
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001248 {
1249 RenderTargetVk *renderTarget = getColorReadRenderTarget();
1250
1251 // If the render target doesn't have alpha, but its emulated format has it, clear the
1252 // alpha to 1.
1253 VkClearColorValue value = clearColorValue;
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001254 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001255 {
1256 SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value);
1257 }
1258
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001259 mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value);
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001260 }
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001261 ++attachmentIndexVk;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001262 }
1263
1264 // Set the appropriate loadOp and clear values for depth and stencil.
1265 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1266 if (depthStencilRenderTarget)
1267 {
1268 if (clearDepth)
1269 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001270 mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001271 clearDepthStencilValue.depth);
1272 }
1273
1274 if (clearStencil)
1275 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001276 mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001277 clearDepthStencilValue.stencil);
1278 }
1279 }
1280
1281 return angle::Result::Continue;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001282}
1283
Jamie Madill21061022018-07-12 23:56:30 -04001284angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001285 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001286 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001287 bool clearStencil,
1288 VkColorComponentFlags colorMaskFlags,
1289 uint8_t stencilMask,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001290 const VkClearColorValue &clearColorValue,
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001291 uint8_t clearStencilValue)
Jamie Madill9aef3672018-04-27 11:45:06 -04001292{
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001293 UtilsVk::ClearFramebufferParameters params = {};
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001294 params.clearArea = clearArea;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001295 params.colorClearValue = clearColorValue;
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001296 params.stencilClearValue = clearStencilValue;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001297 params.stencilMask = stencilMask;
1298
1299 params.clearColor = true;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001300 params.clearStencil = clearStencil;
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001301
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001302 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001303 for (size_t colorIndexGL : clearColorBuffers)
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001304 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001305 const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001306 ASSERT(colorRenderTarget);
1307
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001308 params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat();
1309 params.colorAttachmentIndexGL = colorIndexGL;
1310 params.colorMaskFlags = colorMaskFlags;
1311 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001312 {
1313 params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT;
1314 }
1315
Geoff Langee244c72019-05-06 10:30:18 -04001316 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001317
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001318 // Clear stencil only once!
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001319 params.clearStencil = false;
1320 }
1321
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001322 // If there was no color clear, clear stencil alone.
1323 if (params.clearStencil)
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001324 {
1325 params.clearColor = false;
Geoff Langee244c72019-05-06 10:30:18 -04001326 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001327 }
1328
1329 return angle::Result::Continue;
Jamie Madill9aef3672018-04-27 11:45:06 -04001330}
1331
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001332angle::Result FramebufferVk::getSamplePosition(const gl::Context *context,
1333 size_t index,
1334 GLfloat *xy) const
JiangYizhoubddc46b2016-12-09 09:50:51 +08001335{
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001336 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -05001337 return angle::Result::Stop;
JiangYizhoubddc46b2016-12-09 09:50:51 +08001338}
1339
Jamie Madilld1249de2018-08-28 16:58:53 -04001340angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001341 const gl::Rectangle &renderArea,
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001342 vk::CommandBuffer **commandBufferOut)
Jamie Madilld1249de2018-08-28 16:58:53 -04001343{
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001344 vk::Framebuffer *framebuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001345 ANGLE_TRY(getFramebuffer(contextVk, &framebuffer));
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001346
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001347 vk::AttachmentOpsArray renderPassAttachmentOps;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001348 std::vector<VkClearValue> attachmentClearValues;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001349
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001350 vk::CommandBuffer *writeCommands = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001351 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands));
Jamie Madille4c5a232018-03-02 21:00:31 -05001352
Jamie Madill49ac74b2017-12-21 14:42:33 -05001353 // Initialize RenderPass info.
Jamie Madill66546be2018-03-08 09:47:20 -05001354 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001355 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madill4c26fc22017-02-24 11:04:10 -05001356 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001357 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001358 ASSERT(colorRenderTarget);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001359
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001360 ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001361
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001362 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1363 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1364 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
1365 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill66546be2018-03-08 09:47:20 -05001366 }
1367
1368 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1369 if (depthStencilRenderTarget)
1370 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001371 ANGLE_TRY(
1372 depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001373
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001374 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1375 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1376 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
1377 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001378 }
1379
Jamie Madilldbc605c2019-01-04 16:39:14 -05001380 return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001381 renderPassAttachmentOps, attachmentClearValues,
1382 commandBufferOut);
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001383}
1384
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001385void FramebufferVk::updateActiveColorMasks(size_t colorIndexGL, bool r, bool g, bool b, bool a)
Jamie Madill9aef3672018-04-27 11:45:06 -04001386{
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001387 mActiveColorComponentMasksForClear[0].set(colorIndexGL, r);
1388 mActiveColorComponentMasksForClear[1].set(colorIndexGL, g);
1389 mActiveColorComponentMasksForClear[2].set(colorIndexGL, b);
1390 mActiveColorComponentMasksForClear[3].set(colorIndexGL, a);
Luc Ferron5fd36932018-06-19 14:55:50 -04001391}
1392
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001393const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const
Luc Ferron5fd36932018-06-19 14:55:50 -04001394{
1395 return mEmulatedAlphaAttachmentMask;
Jamie Madill9aef3672018-04-27 11:45:06 -04001396}
Luc Ferron018709f2018-05-10 13:53:11 -04001397
Jamie Madill21061022018-07-12 23:56:30 -04001398angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk,
1399 const gl::Rectangle &area,
1400 const PackPixelsParams &packPixelsParams,
Jamie Madillb436aac2018-07-18 17:23:48 -04001401 VkImageAspectFlagBits copyAspectFlags,
Jamie Madill21061022018-07-12 23:56:30 -04001402 RenderTargetVk *renderTarget,
1403 void *pixels)
Luc Ferron018709f2018-05-10 13:53:11 -04001404{
Jamie Madill3ea463b2019-06-19 14:21:33 -04001405 ANGLE_TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl");
Jamie Madill58675012018-05-22 14:54:07 -04001406
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001407 RendererVk *renderer = contextVk->getRenderer();
1408
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001409 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001410 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
Jamie Madill58675012018-05-22 14:54:07 -04001411
Jamie Madillbcf467f2018-05-23 09:46:00 -04001412 // Note that although we're reading from the image, we need to update the layout below.
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -05001413 vk::ImageHelper *srcImage =
1414 renderTarget->getImageForRead(&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -04001415
Jamie Madill0631e192019-04-18 16:09:12 -04001416 const angle::Format *readFormat = &srcImage->getFormat().imageFormat();
Luc Ferron1617e692018-07-11 11:08:19 -04001417
Jamie Madillb436aac2018-07-18 17:23:48 -04001418 if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT)
Luc Ferron1617e692018-07-11 11:08:19 -04001419 {
Jamie Madillb436aac2018-07-18 17:23:48 -04001420 readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags);
Luc Ferron1617e692018-07-11 11:08:19 -04001421 }
1422
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001423 size_t level = renderTarget->getLevelIndex();
1424 size_t layer = renderTarget->getLayerIndex();
1425 VkOffset3D srcOffset = {area.x, area.y, 0};
1426 VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height),
1427 1};
1428
1429 // If the source image is multisampled, we need to resolve it into a temporary image before
1430 // performing a readback.
1431 bool isMultisampled = srcImage->getSamples() > 1;
1432 vk::Scoped<vk::ImageHelper> resolvedImage(contextVk->getDevice());
1433 if (isMultisampled)
1434 {
1435 ANGLE_TRY(resolvedImage.get().init2DStaging(
1436 contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1),
1437 srcImage->getFormat(),
1438 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1));
Geoff Langee244c72019-05-06 10:30:18 -04001439 resolvedImage.get().updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001440
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -04001441 // Note: resolve only works on color images (not depth/stencil).
1442 //
1443 // TODO: Currently, depth/stencil blit can perform a depth/stencil readback, but that code
1444 // path will be optimized away. http://anglebug.com/3200
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001445 ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT);
1446
1447 VkImageResolve resolveRegion = {};
1448 resolveRegion.srcSubresource.aspectMask = copyAspectFlags;
1449 resolveRegion.srcSubresource.mipLevel = level;
1450 resolveRegion.srcSubresource.baseArrayLayer = layer;
1451 resolveRegion.srcSubresource.layerCount = 1;
1452 resolveRegion.srcOffset = srcOffset;
1453 resolveRegion.dstSubresource.aspectMask = copyAspectFlags;
1454 resolveRegion.dstSubresource.mipLevel = 0;
1455 resolveRegion.dstSubresource.baseArrayLayer = 0;
1456 resolveRegion.dstSubresource.layerCount = 1;
1457 resolveRegion.dstOffset = {};
1458 resolveRegion.extent = srcExtent;
1459
1460 srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer);
1461
1462 resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc,
1463 commandBuffer);
1464
1465 // Make the resolved image the target of buffer copy.
1466 srcImage = &resolvedImage.get();
1467 level = 0;
1468 layer = 0;
1469 srcOffset = {0, 0, 0};
1470 }
1471
Jamie Madillb980c562018-11-27 11:34:27 -05001472 VkBuffer bufferHandle = VK_NULL_HANDLE;
1473 uint8_t *readPixelBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -04001474 VkDeviceSize stagingOffset = 0;
Jamie Madillb980c562018-11-27 11:34:27 -05001475 size_t allocationSize = readFormat->pixelBytes * area.width * area.height;
Luc Ferron018709f2018-05-10 13:53:11 -04001476
Jamie Madilld754eb52018-07-19 14:55:03 -04001477 ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -05001478 &stagingOffset, nullptr));
Luc Ferron018709f2018-05-10 13:53:11 -04001479
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001480 VkBufferImageCopy region = {};
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001481 region.bufferImageHeight = srcExtent.height;
Jamie Madill4c310832018-08-29 13:43:17 -04001482 region.bufferOffset = stagingOffset;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001483 region.bufferRowLength = srcExtent.width;
1484 region.imageExtent = srcExtent;
1485 region.imageOffset = srcOffset;
Luc Ferron1617e692018-07-11 11:08:19 -04001486 region.imageSubresource.aspectMask = copyAspectFlags;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001487 region.imageSubresource.baseArrayLayer = layer;
Luc Ferron534b00d2018-05-18 08:16:53 -04001488 region.imageSubresource.layerCount = 1;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001489 region.imageSubresource.mipLevel = level;
Luc Ferron534b00d2018-05-18 08:16:53 -04001490
Jamie Madillbcf467f2018-05-23 09:46:00 -04001491 commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(),
1492 bufferHandle, 1, &region);
Luc Ferron018709f2018-05-10 13:53:11 -04001493
1494 // Triggers a full finish.
1495 // TODO(jmadill): Don't block on asynchronous readback.
Geoff Lang892d1802019-03-27 14:21:34 -04001496 ANGLE_TRY(contextVk->finishImpl());
Luc Ferron018709f2018-05-10 13:53:11 -04001497
Luc Ferron534b00d2018-05-18 08:16:53 -04001498 // The buffer we copied to needs to be invalidated before we read from it because its not been
1499 // created with the host coherent bit.
Jamie Madilld754eb52018-07-19 14:55:03 -04001500 ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk));
Yuly Novikov6c6c76c2018-05-17 18:45:06 +00001501
Jamie Madillb436aac2018-07-18 17:23:48 -04001502 PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, readPixelBuffer,
Rafael Cintron05a449a2018-06-20 18:08:04 -07001503 static_cast<uint8_t *>(pixels));
Luc Ferron018709f2018-05-10 13:53:11 -04001504
Jamie Madill7c985f52018-11-29 18:16:17 -05001505 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -04001506}
Jamie Madill58675012018-05-22 14:54:07 -04001507
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001508gl::Extents FramebufferVk::getReadImageExtents() const
Jamie Madill58675012018-05-22 14:54:07 -04001509{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001510 ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width);
1511 ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height);
1512
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001513 return getColorReadRenderTarget()->getExtents();
Jamie Madill58675012018-05-22 14:54:07 -04001514}
Jamie Madill502d2e22018-11-01 11:06:23 -04001515
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001516gl::Rectangle FramebufferVk::getCompleteRenderArea() const
1517{
1518 return gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height);
1519}
1520
1521gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const
1522{
1523 const gl::Rectangle renderArea(0, 0, mState.getDimensions().width,
1524 mState.getDimensions().height);
1525 bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO();
1526
1527 return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport);
1528}
1529
1530void FramebufferVk::onScissorChange(ContextVk *contextVk)
1531{
1532 gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
1533
1534 // If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass
1535 // is restarted. Otherwise, we can continue using the same renderpass area.
1536 //
1537 // Without a scissor, the render pass area covers the whole of the framebuffer. With a
1538 // scissored clear, the render pass area could be smaller than the framebuffer size. When the
1539 // scissor changes, if the scissor area is completely encompassed by the render pass area, it's
1540 // possible to continue using the same render pass. However, if the current render pass area
1541 // is too small, we need to start a new one. The latter can happen if a scissored clear starts
1542 // a render pass, the scissor is disabled and a draw call is issued to affect the whole
1543 // framebuffer.
Geoff Langee244c72019-05-06 10:30:18 -04001544 mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001545 if (mFramebuffer.hasStartedRenderPass() &&
1546 !mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea))
1547 {
Geoff Langee244c72019-05-06 10:30:18 -04001548 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001549 }
1550}
1551
Jamie Madill502d2e22018-11-01 11:06:23 -04001552RenderTargetVk *FramebufferVk::getFirstRenderTarget() const
1553{
1554 for (auto *renderTarget : mRenderTargetCache.getColors())
1555 {
1556 if (renderTarget)
1557 {
1558 return renderTarget;
1559 }
1560 }
1561
1562 return mRenderTargetCache.getDepthStencil();
1563}
1564
1565GLint FramebufferVk::getSamples() const
1566{
1567 RenderTargetVk *firstRT = getFirstRenderTarget();
1568 return firstRT ? firstRT->getImage().getSamples() : 0;
1569}
1570
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001571} // namespace rx