blob: d73e2f2c7a100fc0300734329ee018da274a5845 [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"
Tobin Ehlis4a419142019-02-05 08:50:30 -070027#include "third_party/trace_event/trace_event.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 Madill9aef3672018-04-27 11:45:06 -0400165 : FramebufferImpl(state),
166 mBackbuffer(backbuffer),
Jamie Madill9aef3672018-04-27 11:45:06 -0400167 mActiveColorComponents(0),
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500168 mReadPixelBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT, kMinReadPixelsBufferSize, true),
169 mBlitPixelBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, kMinReadPixelsBufferSize, true)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500170{
Jamie Madill639bc902018-07-18 17:08:27 -0400171 mBlitPixelBuffer.init(1, renderer);
Frank Henigman49b0f6e2018-10-03 23:15:29 -0400172 mReadPixelBuffer.init(4, renderer);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500173}
174
Jamie Madill58675012018-05-22 14:54:07 -0400175FramebufferVk::~FramebufferVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400176
Jamie Madillc564c072017-06-01 12:45:42 -0400177void FramebufferVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500178{
Luc Ferron534b00d2018-05-18 08:16:53 -0400179 ContextVk *contextVk = vk::GetImpl(context);
Geoff Langee244c72019-05-06 10:30:18 -0400180 mFramebuffer.release(contextVk);
Luc Ferron534b00d2018-05-18 08:16:53 -0400181
Geoff Langee244c72019-05-06 10:30:18 -0400182 mReadPixelBuffer.release(contextVk);
183 mBlitPixelBuffer.release(contextVk);
Jamie Madill5deea722017-02-16 10:44:46 -0500184}
185
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400186angle::Result FramebufferVk::discard(const gl::Context *context,
187 size_t count,
188 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400189{
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400190 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -0500191 return angle::Result::Stop;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400192}
193
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400194angle::Result FramebufferVk::invalidate(const gl::Context *context,
195 size_t count,
196 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400197{
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400198 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -0500199 return angle::Result::Stop;
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{
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400207 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -0500208 return angle::Result::Stop;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400209}
210
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400211angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400212{
Jamie Madill0cec82a2018-03-14 09:21:07 -0400213 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500214
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400215 bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT));
216 bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT));
217 bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT));
218 gl::DrawBufferMask clearColorBuffers;
219 if (clearColor)
220 {
221 clearColorBuffers = mState.getEnabledDrawBuffers();
222 }
223
224 const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color;
225 const VkClearDepthStencilValue &clearDepthStencilValue =
226 contextVk->getClearDepthStencilValue().depthStencil;
227
228 return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue,
229 clearDepthStencilValue);
230}
231
232angle::Result FramebufferVk::clearImpl(const gl::Context *context,
233 gl::DrawBufferMask clearColorBuffers,
234 bool clearDepth,
235 bool clearStencil,
236 const VkClearColorValue &clearColorValue,
237 const VkClearDepthStencilValue &clearDepthStencilValue)
238{
239 ContextVk *contextVk = vk::GetImpl(context);
240
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400241 const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
242
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400243 // Discard clear altogether if scissor has 0 width or height.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400244 if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400245 {
246 return angle::Result::Continue;
247 }
248
Geoff Langee244c72019-05-06 10:30:18 -0400249 mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400250
251 // This function assumes that only enabled attachments are asked to be cleared.
252 ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers);
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400253
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400254 // Adjust clear behavior based on whether the respective attachments are present; if asked to
255 // clear a non-existent attachment, don't attempt to clear it.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400256
257 VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask();
258 bool clearColor = clearColorBuffers.any();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400259
Jamie Madill0cec82a2018-03-14 09:21:07 -0400260 const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400261 clearDepth = clearDepth && depthAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400262 ASSERT(!clearDepth || depthAttachment->isAttached());
263
264 const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400265 clearStencil = clearStencil && stencilAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400266 ASSERT(!clearStencil || stencilAttachment->isAttached());
267
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400268 uint8_t stencilMask =
269 static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask);
270
271 // The front-end should ensure we don't attempt to clear color if all channels are masked.
272 ASSERT(!clearColor || colorMaskFlags != 0);
273 // The front-end should ensure we don't attempt to clear depth if depth write is disabled.
274 ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask);
275 // The front-end should ensure we don't attempt to clear stencil if all bits are masked.
276 ASSERT(!clearStencil || stencilMask != 0);
277
278 // If there is nothing to clear, return right away (for example, if asked to clear depth, but
279 // there is no depth attachment).
Shahbaz Youssefi02a579e2019-03-27 14:21:20 -0400280 if (!clearColor && !clearDepth && !clearStencil)
281 {
282 return angle::Result::Continue;
283 }
284
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400285 VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue;
Shahbaz Youssefid856ca42018-10-31 16:55:12 -0400286
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400287 // We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If
288 // there's a depth mask, depth clearing is already disabled.
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400289 bool maskedClearColor =
290 clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400291 bool maskedClearStencil = stencilMask != 0xFF;
292
293 bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor;
294 bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400295
296 // At least one of color, depth or stencil should be clearable with render pass loadOp for us
297 // to use this clear path.
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400298 bool clearAnyWithRenderPassLoadOp =
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400299 clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400300
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400301 if (clearAnyWithRenderPassLoadOp)
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400302 {
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400303 // Clearing color is indicated by the set bits in this mask. If not clearing colors with
304 // render pass loadOp, the default value of all-zeros means the clear is not done in
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400305 // clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass
306 // loadOp.
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400307 gl::DrawBufferMask clearBuffersWithRenderPassLoadOp;
308 if (clearColorWithRenderPassLoadOp)
309 {
310 clearBuffersWithRenderPassLoadOp = clearColorBuffers;
311 }
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400312 ANGLE_TRY(clearWithRenderPassOp(
313 contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth,
314 clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue));
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400315
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400316 // On some hardware, having inline commands at this point results in corrupted output. In
317 // that case, end the render pass immediately. http://anglebug.com/2361
Jonah Ryan-Davis776694c2019-05-08 10:28:55 -0400318 if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled)
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400319 {
Geoff Langee244c72019-05-06 10:30:18 -0400320 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400321 }
322
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400323 // Fallback to other methods for whatever isn't cleared here.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400324 clearDepth = false;
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400325 if (clearColorWithRenderPassLoadOp)
326 {
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400327 clearColorBuffers.reset();
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400328 clearColor = false;
329 }
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400330 if (clearStencilWithRenderPassLoadOp)
331 {
332 clearStencil = false;
333 }
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400334
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400335 // If nothing left to clear, early out.
336 if (!clearColor && !clearStencil)
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400337 {
338 return angle::Result::Continue;
339 }
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400340 }
341
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400342 // Note: depth clear is always done through render pass loadOp.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400343 ASSERT(clearDepth == false);
344
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400345 // The most costly clear mode is when we need to mask out specific color channels or stencil
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400346 // bits. This can only be done with a draw call.
347 return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil,
348 colorMaskFlags, stencilMask, clearColorValue,
349 static_cast<uint8_t>(modifiedDepthStencilValue.stencil));
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400350}
351
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400352angle::Result FramebufferVk::clearBufferfv(const gl::Context *context,
353 GLenum buffer,
354 GLint drawbuffer,
355 const GLfloat *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400356{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400357 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400358
359 bool clearDepth = false;
360 gl::DrawBufferMask clearColorBuffers;
361
362 if (buffer == GL_DEPTH)
363 {
364 clearDepth = true;
365 clearValue.depthStencil.depth = values[0];
366 }
367 else
368 {
369 clearColorBuffers.set(drawbuffer);
370 clearValue.color.float32[0] = values[0];
371 clearValue.color.float32[1] = values[1];
372 clearValue.color.float32[2] = values[2];
373 clearValue.color.float32[3] = values[3];
374 }
375
376 return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color,
377 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400378}
379
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400380angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context,
381 GLenum buffer,
382 GLint drawbuffer,
383 const GLuint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400384{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400385 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400386
387 gl::DrawBufferMask clearColorBuffers;
388 clearColorBuffers.set(drawbuffer);
389
390 clearValue.color.uint32[0] = values[0];
391 clearValue.color.uint32[1] = values[1];
392 clearValue.color.uint32[2] = values[2];
393 clearValue.color.uint32[3] = values[3];
394
395 return clearImpl(context, clearColorBuffers, false, false, clearValue.color,
396 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400397}
398
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400399angle::Result FramebufferVk::clearBufferiv(const gl::Context *context,
400 GLenum buffer,
401 GLint drawbuffer,
402 const GLint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400403{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400404 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400405
406 bool clearStencil = false;
407 gl::DrawBufferMask clearColorBuffers;
408
409 if (buffer == GL_STENCIL)
410 {
411 clearStencil = true;
412 clearValue.depthStencil.stencil =
413 gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max());
414 }
415 else
416 {
417 clearColorBuffers.set(drawbuffer);
418 clearValue.color.int32[0] = values[0];
419 clearValue.color.int32[1] = values[1];
420 clearValue.color.int32[2] = values[2];
421 clearValue.color.int32[3] = values[3];
422 }
423
424 return clearImpl(context, clearColorBuffers, false, clearStencil, clearValue.color,
425 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400426}
427
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400428angle::Result FramebufferVk::clearBufferfi(const gl::Context *context,
429 GLenum buffer,
430 GLint drawbuffer,
431 GLfloat depth,
432 GLint stencil)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400433{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400434 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400435
436 clearValue.depthStencil.depth = depth;
437 clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max());
438
439 return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color,
440 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400441}
442
Jamie Madill4928b7c2017-06-20 12:57:39 -0400443GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400444{
Jamie Madill66546be2018-03-08 09:47:20 -0500445 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400446}
447
Jamie Madill4928b7c2017-06-20 12:57:39 -0400448GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400449{
Jamie Madill66546be2018-03-08 09:47:20 -0500450 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400451}
452
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400453angle::Result FramebufferVk::readPixels(const gl::Context *context,
454 const gl::Rectangle &area,
455 GLenum format,
456 GLenum type,
457 void *pixels)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400458{
Luc Ferrona1c72422018-05-14 15:58:28 -0400459 // Clip read area to framebuffer.
460 const gl::Extents &fbSize = getState().getReadAttachment()->getSize();
461 const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400462 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400463
Luc Ferrona1c72422018-05-14 15:58:28 -0400464 gl::Rectangle clippedArea;
465 if (!ClipRectangle(area, fbRect, &clippedArea))
466 {
467 // nothing to read
Jamie Madill7c985f52018-11-29 18:16:17 -0500468 return angle::Result::Continue;
Luc Ferrona1c72422018-05-14 15:58:28 -0400469 }
Luc Ferronbf6dc372018-06-28 15:24:19 -0400470 gl::Rectangle flippedArea = clippedArea;
Geoff Lange076a232018-07-16 15:34:05 -0400471 if (contextVk->isViewportFlipEnabledForReadFBO())
Luc Ferronbf6dc372018-06-28 15:24:19 -0400472 {
473 flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height;
474 }
Luc Ferrona1c72422018-05-14 15:58:28 -0400475
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500476 const gl::State &glState = context->getState();
Frank Henigman1ffad842018-09-24 23:40:45 -0400477 const gl::PixelPackState &packState = glState.getPackState();
Luc Ferronbf6dc372018-06-28 15:24:19 -0400478
Luc Ferrona1c72422018-05-14 15:58:28 -0400479 const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type);
480
481 GLuint outputPitch = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400482 ANGLE_VK_CHECK_MATH(contextVk,
483 sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment,
484 packState.rowLength, &outputPitch));
Luc Ferrona1c72422018-05-14 15:58:28 -0400485 GLuint outputSkipBytes = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400486 ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState,
487 false, &outputSkipBytes));
Luc Ferrona1c72422018-05-14 15:58:28 -0400488
489 outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes +
490 (clippedArea.y - area.y) * outputPitch;
Luc Ferron60284222018-03-20 16:01:44 -0400491
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400492 const angle::Format &angleFormat = GetFormatFromFormatType(format, type);
493
Frank Henigman1ffad842018-09-24 23:40:45 -0400494 PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder,
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400495 glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0);
Frank Henigman1ffad842018-09-24 23:40:45 -0400496 if (contextVk->isViewportFlipEnabledForReadFBO())
497 {
498 params.reverseRowOrder = !params.reverseRowOrder;
499 }
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500500
Jamie Madill21061022018-07-12 23:56:30 -0400501 ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400502 getColorReadRenderTarget(),
Rafael Cintron05a449a2018-06-20 18:08:04 -0700503 static_cast<uint8_t *>(pixels) + outputSkipBytes));
Geoff Langee244c72019-05-06 10:30:18 -0400504 mReadPixelBuffer.releaseRetainedBuffers(contextVk);
Jamie Madill7c985f52018-11-29 18:16:17 -0500505 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -0400506}
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500507
Luc Ferron26581112018-06-21 09:43:08 -0400508RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const
509{
510 return mRenderTargetCache.getDepthStencil();
511}
512
Jamie Madill58675012018-05-22 14:54:07 -0400513RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const
Luc Ferron018709f2018-05-10 13:53:11 -0400514{
515 RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400516 ASSERT(renderTarget && renderTarget->getImage().valid());
Luc Ferron018709f2018-05-10 13:53:11 -0400517 return renderTarget;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400518}
519
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400520angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400521 const gl::Rectangle &sourceArea,
522 const gl::Rectangle &destArea,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400523 RenderTargetVk *readRenderTarget,
524 RenderTargetVk *drawRenderTarget,
525 GLenum filter,
526 bool colorBlit,
527 bool depthBlit,
528 bool stencilBlit,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400529 bool flipX,
530 bool flipY)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400531{
532 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
533 // it should never be the case that both color and depth/stencil need to be blitted at
534 // at the same time.
535 ASSERT(colorBlit != (depthBlit || stencilBlit));
536
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400537 vk::ImageHelper *srcImage = &readRenderTarget->getImage();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400538 vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
539
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400540 VkImageAspectFlags imageAspectMask = srcImage->getAspectFlags();
541 VkImageAspectFlags blitAspectMask = imageAspectMask;
542
543 // Remove depth or stencil aspects if they are not requested to be blitted.
544 if (!depthBlit)
545 {
546 blitAspectMask &= ~VK_IMAGE_ASPECT_DEPTH_BIT;
547 }
548 if (!stencilBlit)
549 {
550 blitAspectMask &= ~VK_IMAGE_ASPECT_STENCIL_BIT;
551 }
552
553 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
554 {
555 vk::CommandBuffer *srcLayoutChange;
556 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
557 srcImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferSrc, srcLayoutChange);
558 }
559
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400560 vk::CommandBuffer *commandBuffer = nullptr;
561 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
562
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400563 srcImage->addReadDependency(&mFramebuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400564
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400565 VkImageBlit blit = {};
566 blit.srcSubresource.aspectMask = blitAspectMask;
567 blit.srcSubresource.mipLevel = readRenderTarget->getLevelIndex();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400568 blit.srcSubresource.baseArrayLayer = readRenderTarget->getLayerIndex();
569 blit.srcSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400570 blit.srcOffsets[0] = {sourceArea.x0(), sourceArea.y0(), 0};
571 blit.srcOffsets[1] = {sourceArea.x1(), sourceArea.y1(), 1};
572 blit.dstSubresource.aspectMask = blitAspectMask;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400573 blit.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
574 blit.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
575 blit.dstSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400576 blit.dstOffsets[0] = {destArea.x0(), destArea.y0(), 0};
577 blit.dstOffsets[1] = {destArea.x1(), destArea.y1(), 1};
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400578
579 // Requirement of the copyImageToBuffer, the dst image must be in
580 // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400581 dstImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferDst, commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400582
583 commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
584 dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
585 gl_vk::GetFilter(filter));
586
587 return angle::Result::Continue;
588}
589
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400590angle::Result FramebufferVk::blit(const gl::Context *context,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400591 const gl::Rectangle &sourceAreaIn,
592 const gl::Rectangle &destAreaIn,
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400593 GLbitfield mask,
594 GLenum filter)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400595{
Luc Ferron26581112018-06-21 09:43:08 -0400596 ContextVk *contextVk = vk::GetImpl(context);
597 RendererVk *renderer = contextVk->getRenderer();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400598 UtilsVk &utilsVk = contextVk->getUtils();
Luc Ferron26581112018-06-21 09:43:08 -0400599
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400600 const gl::State &glState = contextVk->getState();
601 const gl::Framebuffer *srcFramebuffer = glState.getReadFramebuffer();
602
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400603 const bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0;
604 const bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0;
605 const bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400606
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400607 const bool isResolve = srcFramebuffer->getCachedSamples(context) > 1;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400608
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400609 FramebufferVk *srcFramebufferVk = vk::GetImpl(srcFramebuffer);
610 const bool srcFramebufferFlippedY = contextVk->isViewportFlipEnabledForReadFBO();
611 const bool destFramebufferFlippedY = contextVk->isViewportFlipEnabledForDrawFBO();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400612
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400613 gl::Rectangle sourceArea = sourceAreaIn;
614 gl::Rectangle destArea = destAreaIn;
615
616 // Note: GLES (all 3.x versions) require source and dest area to be identical when
617 // resolving.
618 ASSERT(!isResolve ||
619 (sourceArea.x == destArea.x && sourceArea.y == destArea.y &&
620 sourceArea.width == destArea.width && sourceArea.height == destArea.height));
621
622 const gl::Rectangle srcFramebufferDimensions =
623 srcFramebufferVk->mState.getDimensions().toRect();
624
625 // If the destination is flipped in either direction, we will flip the source instead so that
626 // the destination area is always unflipped.
627 sourceArea = sourceArea.flip(destArea.isReversedX(), destArea.isReversedY());
628 destArea = destArea.removeReversal();
629
630 // Calculate the stretch factor prior to any clipping, as it needs to remain constant.
631 const float stretch[2] = {
632 std::abs(sourceArea.width / static_cast<float>(destArea.width)),
633 std::abs(sourceArea.height / static_cast<float>(destArea.height)),
634 };
635
636 // First, clip the source area to framebuffer. That requires transforming the dest area to
637 // match the clipped source.
638 gl::Rectangle absSourceArea = sourceArea.removeReversal();
639 gl::Rectangle clippedSourceArea;
640 if (!gl::ClipRectangle(srcFramebufferDimensions, absSourceArea, &clippedSourceArea))
641 {
642 return angle::Result::Continue;
643 }
644
645 // Resize the destination area based on the new size of source. Note again that stretch is
646 // calculated as SrcDimension/DestDimension.
647 gl::Rectangle srcClippedDestArea;
648 if (isResolve)
649 {
650 // Source and dest areas are identical in resolve.
651 srcClippedDestArea = clippedSourceArea;
652 }
653 else if (clippedSourceArea == absSourceArea)
654 {
655 // If there was no clipping, keep dest area as is.
656 srcClippedDestArea = destArea;
657 }
658 else
659 {
660 // Shift dest area's x0,y0,x1,y1 by as much as the source area's got shifted (taking
661 // stretching into account)
662 float x0Shift = std::round((clippedSourceArea.x - absSourceArea.x) / stretch[0]);
663 float y0Shift = std::round((clippedSourceArea.y - absSourceArea.y) / stretch[1]);
664 float x1Shift = std::round((absSourceArea.x1() - clippedSourceArea.x1()) / stretch[0]);
665 float y1Shift = std::round((absSourceArea.y1() - clippedSourceArea.y1()) / stretch[1]);
666
667 // If the source area was reversed in any direction, the shift should be applied in the
668 // opposite direction as well.
669 if (sourceArea.isReversedX())
670 {
671 std::swap(x0Shift, x1Shift);
672 }
673
674 if (sourceArea.isReversedY())
675 {
676 std::swap(y0Shift, y1Shift);
677 }
678
679 srcClippedDestArea.x = destArea.x0() + static_cast<int>(x0Shift);
680 srcClippedDestArea.y = destArea.y0() + static_cast<int>(y0Shift);
681 int x1 = destArea.x1() - static_cast<int>(x1Shift);
682 int y1 = destArea.y1() - static_cast<int>(y1Shift);
683
684 srcClippedDestArea.width = x1 - srcClippedDestArea.x;
685 srcClippedDestArea.height = y1 - srcClippedDestArea.y;
686 }
687
688 // If framebuffers are flipped in Y, flip the source and dest area (which define the
689 // transformation regardless of clipping), as well as the blit area (which is the clipped
690 // dest area).
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400691 if (srcFramebufferFlippedY)
692 {
693 sourceArea.y = srcFramebufferDimensions.height - sourceArea.y;
694 sourceArea.height = -sourceArea.height;
695 }
696 if (destFramebufferFlippedY)
697 {
698 destArea.y = mState.getDimensions().height - destArea.y;
699 destArea.height = -destArea.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400700
701 srcClippedDestArea.y =
702 mState.getDimensions().height - srcClippedDestArea.y - srcClippedDestArea.height;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400703 }
704
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400705 const bool flipX = sourceArea.isReversedX() != destArea.isReversedX();
706 const bool flipY = sourceArea.isReversedY() != destArea.isReversedY();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400707
708 // GLES doesn't allow flipping the parameters of glBlitFramebuffer if performing a resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400709 ASSERT(!isResolve ||
710 (flipX == false && flipY == (srcFramebufferFlippedY != destFramebufferFlippedY)));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400711
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400712 // Again, transfer the destination flip to source, so dest is unflipped. Note that destArea
713 // was not reversed until the final possible Y-flip.
714 ASSERT(!destArea.isReversedX());
715 sourceArea = sourceArea.flip(false, destArea.isReversedY());
716 destArea = destArea.removeReversal();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400717
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400718 // Clip the destination area to the framebuffer size and scissor. Note that we don't care
719 // about the source area anymore. The offset translation is done based on the original source
720 // and destination rectangles. The stretch factor is already calculated as well.
721 gl::Rectangle blitArea;
722 if (!gl::ClipRectangle(getScissoredRenderArea(contextVk), srcClippedDestArea, &blitArea))
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400723 {
724 return angle::Result::Continue;
725 }
726
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400727 bool noClip = blitArea == destArea && stretch[0] == 1.0f && stretch[1] == 1.0f;
728 bool noFlip = !flipX && !flipY;
729 bool disableFlippingBlitWithCommand =
730 contextVk->getRenderer()->getFeatures().disableFlippingBlitWithCommand.enabled;
731
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400732 UtilsVk::BlitResolveParameters params;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400733 params.srcOffset[0] = sourceArea.x;
734 params.srcOffset[1] = sourceArea.y;
735 params.destOffset[0] = destArea.x;
736 params.destOffset[1] = destArea.y;
737 params.stretch[0] = stretch[0];
738 params.stretch[1] = stretch[1];
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400739 params.srcExtents[0] = srcFramebufferDimensions.width;
740 params.srcExtents[1] = srcFramebufferDimensions.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400741 params.blitArea = blitArea;
742 params.linear = filter == GL_LINEAR;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400743 params.flipX = flipX;
744 params.flipY = flipY;
745
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400746 if (blitColorBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400747 {
748 RenderTargetVk *readRenderTarget = srcFramebufferVk->getColorReadRenderTarget();
749 params.srcLayer = readRenderTarget->getLayerIndex();
750
751 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400752 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400753
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400754 // If there was no clipping and the format capabilities allow us, use Vulkan's builtin blit.
755 // The reason clipping is prohibited in this path is that due to rounding errors, it would
756 // be hard to guarantee the image stretching remains perfect. That also allows us not to
757 // have to transform back the dest clipping to source.
758 //
759 // For simplicity, we either blit all render targets with a Vulkan command, or none.
760 bool canBlitWithCommand = !isResolve && noClip &&
761 (noFlip || !disableFlippingBlitWithCommand) &&
762 HasSrcBlitFeature(renderer, readRenderTarget);
763 bool areChannelsBlitCompatible = true;
764 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
765 {
766 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
767 canBlitWithCommand =
768 canBlitWithCommand && HasDstBlitFeature(renderer, drawRenderTarget);
769 areChannelsBlitCompatible =
770 areChannelsBlitCompatible &&
771 areSrcAndDstColorChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
772 }
773
774 if (canBlitWithCommand && areChannelsBlitCompatible)
775 {
776 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
777 {
778 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
779 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
780 drawRenderTarget, filter, true, false, false, flipX,
781 flipY));
782 }
783 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400784 // If we're not flipping, use Vulkan's builtin resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400785 else if (isResolve && !flipX && !flipY && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400786 {
787 ANGLE_TRY(resolveColorWithCommand(contextVk, params, &readRenderTarget->getImage()));
788 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400789 // Otherwise use a shader to do blit or resolve.
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400790 else
791 {
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400792 ANGLE_TRY(utilsVk.colorBlitResolve(contextVk, this, &readRenderTarget->getImage(),
793 readRenderTarget->getFetchImageView(), params));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400794 }
795 }
796
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400797 if (blitDepthBuffer || blitStencilBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400798 {
799 RenderTargetVk *readRenderTarget = srcFramebufferVk->getDepthStencilRenderTarget();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400800 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400801 params.srcLayer = readRenderTarget->getLayerIndex();
802
803 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400804 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400805
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400806 // Similarly, only blit if there's been no clipping.
807 bool canBlitWithCommand = !isResolve && noClip &&
808 (noFlip || !disableFlippingBlitWithCommand) &&
809 HasSrcBlitFeature(renderer, readRenderTarget) &&
810 HasDstBlitFeature(renderer, drawRenderTarget);
811 bool areChannelsBlitCompatible =
812 areSrcAndDstDepthStencilChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400813
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400814 if (canBlitWithCommand && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400815 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400816 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
817 drawRenderTarget, filter, false, blitDepthBuffer,
818 blitStencilBuffer, flipX, flipY));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400819 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400820 else
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400821 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400822 // Create depth- and stencil-only views for reading.
823 vk::Scoped<vk::ImageView> depthView(contextVk->getDevice());
824 vk::Scoped<vk::ImageView> stencilView(contextVk->getDevice());
825
826 vk::ImageHelper *depthStencilImage = &readRenderTarget->getImage();
827 uint32_t levelIndex = readRenderTarget->getLevelIndex();
828 uint32_t layerIndex = readRenderTarget->getLayerIndex();
829 gl::TextureType textureType = vk::Get2DTextureType(depthStencilImage->getLayerCount(),
830 depthStencilImage->getSamples());
831
832 if (blitDepthBuffer)
833 {
834 ANGLE_TRY(depthStencilImage->initLayerImageView(
835 contextVk, textureType, VK_IMAGE_ASPECT_DEPTH_BIT, gl::SwizzleState(),
836 &depthView.get(), levelIndex, 1, layerIndex, 1));
837 }
838
839 if (blitStencilBuffer)
840 {
841 ANGLE_TRY(depthStencilImage->initLayerImageView(
842 contextVk, textureType, VK_IMAGE_ASPECT_STENCIL_BIT, gl::SwizzleState(),
843 &stencilView.get(), levelIndex, 1, layerIndex, 1));
844 }
845
846 // If shader stencil export is not possible, defer stencil blit/stencil to another pass.
847 bool hasShaderStencilExport =
848 contextVk->getRenderer()->getFeatures().supportsShaderStencilExport.enabled;
849
850 // Blit depth. If shader stencil export is present, blit stencil as well.
851 if (blitDepthBuffer || (blitStencilBuffer && hasShaderStencilExport))
852 {
853 vk::ImageView *depth = blitDepthBuffer ? &depthView.get() : nullptr;
854 vk::ImageView *stencil =
855 blitStencilBuffer && hasShaderStencilExport ? &stencilView.get() : nullptr;
856
857 ANGLE_TRY(utilsVk.depthStencilBlitResolve(contextVk, this, depthStencilImage, depth,
858 stencil, params));
859 }
860
861 // If shader stencil export is not present, blit stencil through a different path.
862 if (blitStencilBuffer && !hasShaderStencilExport)
863 {
864 ANGLE_TRY(utilsVk.stencilBlitResolveNoShaderExport(
865 contextVk, this, depthStencilImage, &stencilView.get(), params));
866 }
867
868 vk::ImageView depthViewObject = depthView.release();
869 vk::ImageView stencilViewObject = stencilView.release();
870
871 contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &depthViewObject);
872 contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &stencilViewObject);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400873 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400874 }
875
876 return angle::Result::Continue;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400877} // namespace rx
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400878
879angle::Result FramebufferVk::resolveColorWithCommand(ContextVk *contextVk,
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400880 const UtilsVk::BlitResolveParameters &params,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400881 vk::ImageHelper *srcImage)
882{
883 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
884 {
885 vk::CommandBuffer *srcLayoutChange;
886 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
887 srcImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferSrc,
888 srcLayoutChange);
889 }
Jamie Madill16c20142018-10-01 13:58:19 -0400890
Shahbaz Youssefi2660b502019-03-21 12:08:40 -0400891 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill16c20142018-10-01 13:58:19 -0400892 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
893
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400894 // Source's layout change should happen before rendering
895 srcImage->addReadDependency(&mFramebuffer);
Luc Ferron26581112018-06-21 09:43:08 -0400896
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400897 VkImageResolve resolveRegion = {};
898 resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
899 resolveRegion.srcSubresource.mipLevel = 0;
900 resolveRegion.srcSubresource.baseArrayLayer = params.srcLayer;
901 resolveRegion.srcSubresource.layerCount = 1;
902 resolveRegion.srcOffset.x = params.srcOffset[0];
903 resolveRegion.srcOffset.y = params.srcOffset[1];
904 resolveRegion.srcOffset.z = 0;
905 resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
906 resolveRegion.dstSubresource.layerCount = 1;
907 resolveRegion.dstOffset.x = params.destOffset[0];
908 resolveRegion.dstOffset.y = params.destOffset[1];
909 resolveRegion.dstOffset.z = 0;
910 resolveRegion.extent.width = params.srcExtents[0];
911 resolveRegion.extent.height = params.srcExtents[1];
912 resolveRegion.extent.depth = 1;
Jamie Madilld754eb52018-07-19 14:55:03 -0400913
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400914 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Luc Ferron82eda932018-07-09 15:10:22 -0400915 {
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400916 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400917 vk::ImageHelper *drawImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
918 drawImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferDst,
919 commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400920
921 resolveRegion.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
922 resolveRegion.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
923
924 srcImage->resolve(&drawRenderTarget->getImage(), resolveRegion, commandBuffer);
Luc Ferron82eda932018-07-09 15:10:22 -0400925 }
926
Jamie Madill7c985f52018-11-29 18:16:17 -0500927 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400928}
929
Kenneth Russellce8602a2017-10-03 18:23:08 -0700930bool FramebufferVk::checkStatus(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400931{
Luc Ferron5bdf8bd2018-06-20 09:51:37 -0400932 // if we have both a depth and stencil buffer, they must refer to the same object
933 // since we only support packed_depth_stencil and not separate depth and stencil
934 if (mState.hasSeparateDepthAndStencilAttachments())
935 {
936 return false;
937 }
938
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400939 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400940}
941
Jamie Madill67220092019-05-20 11:12:53 -0400942angle::Result FramebufferVk::updateColorAttachment(const gl::Context *context, size_t colorIndexGL)
943{
944 ContextVk *contextVk = vk::GetImpl(context);
945
946 ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL));
947
948 // Update cached masks for masked clears.
949 RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL];
950 if (renderTarget)
951 {
952 const angle::Format &emulatedFormat = renderTarget->getImageFormat().imageFormat();
953 updateActiveColorMasks(colorIndexGL, emulatedFormat.redBits > 0,
954 emulatedFormat.greenBits > 0, emulatedFormat.blueBits > 0,
955 emulatedFormat.alphaBits > 0);
956
957 const angle::Format &sourceFormat = renderTarget->getImageFormat().angleFormat();
958 mEmulatedAlphaAttachmentMask.set(
959 colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0);
960
961 contextVk->updateColorMask(context->getState().getBlendState());
962 }
963 else
964 {
965 updateActiveColorMasks(colorIndexGL, false, false, false, false);
966 }
967
968 return angle::Result::Continue;
969}
970
Jamie Madill6f755b22018-10-09 12:48:54 -0400971angle::Result FramebufferVk::syncState(const gl::Context *context,
972 const gl::Framebuffer::DirtyBits &dirtyBits)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400973{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400974 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldd43e6c2017-03-24 14:18:49 -0400975
976 ASSERT(dirtyBits.any());
Jamie Madill57d9cbb2018-04-27 11:45:04 -0400977 for (size_t dirtyBit : dirtyBits)
978 {
979 switch (dirtyBit)
980 {
981 case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
982 case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
983 ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState));
984 break;
Jamie Madill67220092019-05-20 11:12:53 -0400985 case gl::Framebuffer::DIRTY_BIT_DEPTH_BUFFER_CONTENTS:
986 case gl::Framebuffer::DIRTY_BIT_STENCIL_BUFFER_CONTENTS:
987 ANGLE_TRY(mRenderTargetCache.getDepthStencil()->flushStagedUpdates(contextVk));
988 break;
Jamie Madill57d9cbb2018-04-27 11:45:04 -0400989 case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
990 case gl::Framebuffer::DIRTY_BIT_READ_BUFFER:
991 case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
992 case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
993 case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
994 case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
995 break;
996 default:
997 {
Jamie Madill67220092019-05-20 11:12:53 -0400998 static_assert(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits");
999 if (dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX)
Jamie Madill9aef3672018-04-27 11:45:06 -04001000 {
Jamie Madill67220092019-05-20 11:12:53 -04001001 size_t colorIndexGL = static_cast<size_t>(
1002 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
1003 ANGLE_TRY(updateColorAttachment(context, colorIndexGL));
Jamie Madill9aef3672018-04-27 11:45:06 -04001004 }
1005 else
1006 {
Jamie Madill67220092019-05-20 11:12:53 -04001007 ASSERT(dirtyBit >= gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 &&
1008 dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_MAX);
1009 size_t colorIndexGL = static_cast<size_t>(
1010 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0);
1011 ANGLE_TRY(mRenderTargetCache.getColors()[colorIndexGL]->flushStagedUpdates(
1012 contextVk));
Jamie Madill9aef3672018-04-27 11:45:06 -04001013 }
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001014 break;
1015 }
1016 }
1017 }
Jamie Madill66546be2018-03-08 09:47:20 -05001018
Jamie Madill9aef3672018-04-27 11:45:06 -04001019 mActiveColorComponents = gl_vk::GetColorComponentFlags(
Luc Ferron5fd36932018-06-19 14:55:50 -04001020 mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(),
1021 mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any());
Jamie Madill9aef3672018-04-27 11:45:06 -04001022
Geoff Langee244c72019-05-06 10:30:18 -04001023 mFramebuffer.release(contextVk);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001024
Jamie Madill316c6062018-05-29 10:49:45 -04001025 // Will freeze the current set of dependencies on this FBO. The next time we render we will
Jamie Madilla5e06072018-05-18 14:36:05 -04001026 // create a new entry in the command graph.
Geoff Langee244c72019-05-06 10:30:18 -04001027 mFramebuffer.finishCurrentCommands(contextVk);
Jamie Madill72106562017-03-24 14:18:50 -04001028
Jamie Madilldbc605c2019-01-04 16:39:14 -05001029 // Notify the ContextVk to update the pipeline desc.
1030 updateRenderPassDesc();
1031 contextVk->onFramebufferChange(mRenderPassDesc);
Jamie Madill19fa1c62018-03-08 09:47:21 -05001032
Jamie Madill7c985f52018-11-29 18:16:17 -05001033 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001034}
1035
Jamie Madilldbc605c2019-01-04 16:39:14 -05001036void FramebufferVk::updateRenderPassDesc()
Jamie Madillab9f9c32017-01-17 17:47:34 -05001037{
Jamie Madilldbc605c2019-01-04 16:39:14 -05001038 mRenderPassDesc = {};
1039 mRenderPassDesc.setSamples(getSamples());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001040
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001041 const auto &colorRenderTargets = mRenderTargetCache.getColors();
1042 const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers();
1043 for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001044 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001045 if (enabledDrawBuffers[colorIndexGL])
1046 {
1047 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
1048 ASSERT(colorRenderTarget);
1049 mRenderPassDesc.packColorAttachment(
1050 colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID);
1051 }
1052 else
1053 {
1054 mRenderPassDesc.packColorAttachmentGap(colorIndexGL);
1055 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001056 }
1057
Jamie Madill66546be2018-03-08 09:47:20 -05001058 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1059 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001060 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001061 mRenderPassDesc.packDepthStencilAttachment(
1062 depthStencilRenderTarget->getImage().getFormat().angleFormatID);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001063 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001064}
1065
Jamie Madill21061022018-07-12 23:56:30 -04001066angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001067{
1068 // If we've already created our cached Framebuffer, return it.
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001069 if (mFramebuffer.valid())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001070 {
Jamie Madille8dd0792018-09-27 15:04:27 -04001071 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001072 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001073 }
1074
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001075 vk::RenderPass *compatibleRenderPass = nullptr;
Geoff Langee244c72019-05-06 10:30:18 -04001076 ANGLE_TRY(contextVk->getCompatibleRenderPass(mRenderPassDesc, &compatibleRenderPass));
Jamie Madillab9f9c32017-01-17 17:47:34 -05001077
1078 // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it.
1079 if (mBackbuffer)
1080 {
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001081 return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001082 }
1083
1084 // Gather VkImageViews over all FBO attachments, also size of attached region.
1085 std::vector<VkImageView> attachments;
1086 gl::Extents attachmentsSize;
1087
Jamie Madill66546be2018-03-08 09:47:20 -05001088 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001089 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001090 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001091 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001092 ASSERT(colorRenderTarget);
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001093 attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001094
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001095 ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents());
1096 attachmentsSize = colorRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001097 }
1098
Jamie Madill66546be2018-03-08 09:47:20 -05001099 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1100 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001101 {
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001102 attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001103
Jamie Madillbc543422018-03-30 10:43:19 -04001104 ASSERT(attachmentsSize.empty() ||
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001105 attachmentsSize == depthStencilRenderTarget->getExtents());
1106 attachmentsSize = depthStencilRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001107 }
1108
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001109 VkFramebufferCreateInfo framebufferInfo = {};
Jamie Madillab9f9c32017-01-17 17:47:34 -05001110
1111 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001112 framebufferInfo.flags = 0;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001113 framebufferInfo.renderPass = compatibleRenderPass->getHandle();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001114 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
1115 framebufferInfo.pAttachments = attachments.data();
1116 framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width);
1117 framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height);
1118 framebufferInfo.layers = 1;
1119
Jamie Madill21061022018-07-12 23:56:30 -04001120 ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo));
Jamie Madill5deea722017-02-16 10:44:46 -05001121
Jamie Madille8dd0792018-09-27 15:04:27 -04001122 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001123 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001124}
1125
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001126angle::Result FramebufferVk::clearWithRenderPassOp(
1127 ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001128 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001129 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001130 bool clearDepth,
1131 bool clearStencil,
1132 const VkClearColorValue &clearColorValue,
1133 const VkClearDepthStencilValue &clearDepthStencilValue)
1134{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001135 // Start a new render pass if:
1136 //
1137 // - no render pass has started,
1138 // - there is a render pass started but it contains commands; we cannot modify its ops, so new
1139 // render pass is needed,
1140 // - the current render area doesn't match the clear area. We need the render area to be
1141 // exactly as specified by the scissor for the loadOp to clear only that area. See
1142 // onScissorChange for more information.
1143
1144 if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() ||
1145 mFramebuffer.getRenderPassRenderArea() != clearArea)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001146 {
1147 vk::CommandBuffer *commandBuffer;
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001148 ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer));
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001149 }
1150
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001151 size_t attachmentIndexVk = 0;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001152
1153 // Go through clearColorBuffers and set the appropriate loadOp and clear values.
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001154 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001155 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001156 if (clearColorBuffers.test(colorIndexGL))
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001157 {
1158 RenderTargetVk *renderTarget = getColorReadRenderTarget();
1159
1160 // If the render target doesn't have alpha, but its emulated format has it, clear the
1161 // alpha to 1.
1162 VkClearColorValue value = clearColorValue;
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001163 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001164 {
1165 SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value);
1166 }
1167
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001168 mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value);
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001169 }
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001170 ++attachmentIndexVk;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001171 }
1172
1173 // Set the appropriate loadOp and clear values for depth and stencil.
1174 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1175 if (depthStencilRenderTarget)
1176 {
1177 if (clearDepth)
1178 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001179 mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001180 clearDepthStencilValue.depth);
1181 }
1182
1183 if (clearStencil)
1184 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001185 mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001186 clearDepthStencilValue.stencil);
1187 }
1188 }
1189
1190 return angle::Result::Continue;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001191}
1192
Jamie Madill21061022018-07-12 23:56:30 -04001193angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001194 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001195 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001196 bool clearStencil,
1197 VkColorComponentFlags colorMaskFlags,
1198 uint8_t stencilMask,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001199 const VkClearColorValue &clearColorValue,
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001200 uint8_t clearStencilValue)
Jamie Madill9aef3672018-04-27 11:45:06 -04001201{
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001202 UtilsVk::ClearFramebufferParameters params = {};
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001203 params.clearArea = clearArea;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001204 params.colorClearValue = clearColorValue;
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001205 params.stencilClearValue = clearStencilValue;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001206 params.stencilMask = stencilMask;
1207
1208 params.clearColor = true;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001209 params.clearStencil = clearStencil;
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001210
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001211 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001212 for (size_t colorIndexGL : clearColorBuffers)
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001213 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001214 const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001215 ASSERT(colorRenderTarget);
1216
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001217 params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat();
1218 params.colorAttachmentIndexGL = colorIndexGL;
1219 params.colorMaskFlags = colorMaskFlags;
1220 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001221 {
1222 params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT;
1223 }
1224
Geoff Langee244c72019-05-06 10:30:18 -04001225 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001226
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001227 // Clear stencil only once!
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001228 params.clearStencil = false;
1229 }
1230
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001231 // If there was no color clear, clear stencil alone.
1232 if (params.clearStencil)
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001233 {
1234 params.clearColor = false;
Geoff Langee244c72019-05-06 10:30:18 -04001235 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001236 }
1237
1238 return angle::Result::Continue;
Jamie Madill9aef3672018-04-27 11:45:06 -04001239}
1240
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001241angle::Result FramebufferVk::getSamplePosition(const gl::Context *context,
1242 size_t index,
1243 GLfloat *xy) const
JiangYizhoubddc46b2016-12-09 09:50:51 +08001244{
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001245 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -05001246 return angle::Result::Stop;
JiangYizhoubddc46b2016-12-09 09:50:51 +08001247}
1248
Jamie Madilld1249de2018-08-28 16:58:53 -04001249angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001250 const gl::Rectangle &renderArea,
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001251 vk::CommandBuffer **commandBufferOut)
Jamie Madilld1249de2018-08-28 16:58:53 -04001252{
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001253 vk::Framebuffer *framebuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001254 ANGLE_TRY(getFramebuffer(contextVk, &framebuffer));
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001255
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001256 vk::AttachmentOpsArray renderPassAttachmentOps;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001257 std::vector<VkClearValue> attachmentClearValues;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001258
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001259 vk::CommandBuffer *writeCommands = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001260 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands));
Jamie Madille4c5a232018-03-02 21:00:31 -05001261
Jamie Madill49ac74b2017-12-21 14:42:33 -05001262 // Initialize RenderPass info.
Jamie Madill66546be2018-03-08 09:47:20 -05001263 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001264 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madill4c26fc22017-02-24 11:04:10 -05001265 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001266 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001267 ASSERT(colorRenderTarget);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001268
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001269 ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001270
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001271 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1272 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1273 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
1274 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill66546be2018-03-08 09:47:20 -05001275 }
1276
1277 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1278 if (depthStencilRenderTarget)
1279 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001280 ANGLE_TRY(
1281 depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001282
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001283 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1284 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1285 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
1286 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001287 }
1288
Jamie Madilldbc605c2019-01-04 16:39:14 -05001289 return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001290 renderPassAttachmentOps, attachmentClearValues,
1291 commandBufferOut);
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001292}
1293
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001294void FramebufferVk::updateActiveColorMasks(size_t colorIndexGL, bool r, bool g, bool b, bool a)
Jamie Madill9aef3672018-04-27 11:45:06 -04001295{
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001296 mActiveColorComponentMasksForClear[0].set(colorIndexGL, r);
1297 mActiveColorComponentMasksForClear[1].set(colorIndexGL, g);
1298 mActiveColorComponentMasksForClear[2].set(colorIndexGL, b);
1299 mActiveColorComponentMasksForClear[3].set(colorIndexGL, a);
Luc Ferron5fd36932018-06-19 14:55:50 -04001300}
1301
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001302const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const
Luc Ferron5fd36932018-06-19 14:55:50 -04001303{
1304 return mEmulatedAlphaAttachmentMask;
Jamie Madill9aef3672018-04-27 11:45:06 -04001305}
Luc Ferron018709f2018-05-10 13:53:11 -04001306
Jamie Madill21061022018-07-12 23:56:30 -04001307angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk,
1308 const gl::Rectangle &area,
1309 const PackPixelsParams &packPixelsParams,
Jamie Madillb436aac2018-07-18 17:23:48 -04001310 VkImageAspectFlagBits copyAspectFlags,
Jamie Madill21061022018-07-12 23:56:30 -04001311 RenderTargetVk *renderTarget,
1312 void *pixels)
Luc Ferron018709f2018-05-10 13:53:11 -04001313{
Tobin Ehlis4a419142019-02-05 08:50:30 -07001314 TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl");
Jamie Madill58675012018-05-22 14:54:07 -04001315
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001316 RendererVk *renderer = contextVk->getRenderer();
1317
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001318 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001319 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
Jamie Madill58675012018-05-22 14:54:07 -04001320
Jamie Madillbcf467f2018-05-23 09:46:00 -04001321 // Note that although we're reading from the image, we need to update the layout below.
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -05001322 vk::ImageHelper *srcImage =
1323 renderTarget->getImageForRead(&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -04001324
Jamie Madill0631e192019-04-18 16:09:12 -04001325 const angle::Format *readFormat = &srcImage->getFormat().imageFormat();
Luc Ferron1617e692018-07-11 11:08:19 -04001326
Jamie Madillb436aac2018-07-18 17:23:48 -04001327 if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT)
Luc Ferron1617e692018-07-11 11:08:19 -04001328 {
Jamie Madillb436aac2018-07-18 17:23:48 -04001329 readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags);
Luc Ferron1617e692018-07-11 11:08:19 -04001330 }
1331
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001332 size_t level = renderTarget->getLevelIndex();
1333 size_t layer = renderTarget->getLayerIndex();
1334 VkOffset3D srcOffset = {area.x, area.y, 0};
1335 VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height),
1336 1};
1337
1338 // If the source image is multisampled, we need to resolve it into a temporary image before
1339 // performing a readback.
1340 bool isMultisampled = srcImage->getSamples() > 1;
1341 vk::Scoped<vk::ImageHelper> resolvedImage(contextVk->getDevice());
1342 if (isMultisampled)
1343 {
1344 ANGLE_TRY(resolvedImage.get().init2DStaging(
1345 contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1),
1346 srcImage->getFormat(),
1347 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1));
Geoff Langee244c72019-05-06 10:30:18 -04001348 resolvedImage.get().updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001349
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -04001350 // Note: resolve only works on color images (not depth/stencil).
1351 //
1352 // TODO: Currently, depth/stencil blit can perform a depth/stencil readback, but that code
1353 // path will be optimized away. http://anglebug.com/3200
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001354 ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT);
1355
1356 VkImageResolve resolveRegion = {};
1357 resolveRegion.srcSubresource.aspectMask = copyAspectFlags;
1358 resolveRegion.srcSubresource.mipLevel = level;
1359 resolveRegion.srcSubresource.baseArrayLayer = layer;
1360 resolveRegion.srcSubresource.layerCount = 1;
1361 resolveRegion.srcOffset = srcOffset;
1362 resolveRegion.dstSubresource.aspectMask = copyAspectFlags;
1363 resolveRegion.dstSubresource.mipLevel = 0;
1364 resolveRegion.dstSubresource.baseArrayLayer = 0;
1365 resolveRegion.dstSubresource.layerCount = 1;
1366 resolveRegion.dstOffset = {};
1367 resolveRegion.extent = srcExtent;
1368
1369 srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer);
1370
1371 resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc,
1372 commandBuffer);
1373
1374 // Make the resolved image the target of buffer copy.
1375 srcImage = &resolvedImage.get();
1376 level = 0;
1377 layer = 0;
1378 srcOffset = {0, 0, 0};
1379 }
1380
Jamie Madillb980c562018-11-27 11:34:27 -05001381 VkBuffer bufferHandle = VK_NULL_HANDLE;
1382 uint8_t *readPixelBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -04001383 VkDeviceSize stagingOffset = 0;
Jamie Madillb980c562018-11-27 11:34:27 -05001384 size_t allocationSize = readFormat->pixelBytes * area.width * area.height;
Luc Ferron018709f2018-05-10 13:53:11 -04001385
Jamie Madilld754eb52018-07-19 14:55:03 -04001386 ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -05001387 &stagingOffset, nullptr));
Luc Ferron018709f2018-05-10 13:53:11 -04001388
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001389 VkBufferImageCopy region = {};
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001390 region.bufferImageHeight = srcExtent.height;
Jamie Madill4c310832018-08-29 13:43:17 -04001391 region.bufferOffset = stagingOffset;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001392 region.bufferRowLength = srcExtent.width;
1393 region.imageExtent = srcExtent;
1394 region.imageOffset = srcOffset;
Luc Ferron1617e692018-07-11 11:08:19 -04001395 region.imageSubresource.aspectMask = copyAspectFlags;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001396 region.imageSubresource.baseArrayLayer = layer;
Luc Ferron534b00d2018-05-18 08:16:53 -04001397 region.imageSubresource.layerCount = 1;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001398 region.imageSubresource.mipLevel = level;
Luc Ferron534b00d2018-05-18 08:16:53 -04001399
Jamie Madillbcf467f2018-05-23 09:46:00 -04001400 commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(),
1401 bufferHandle, 1, &region);
Luc Ferron018709f2018-05-10 13:53:11 -04001402
1403 // Triggers a full finish.
1404 // TODO(jmadill): Don't block on asynchronous readback.
Geoff Lang892d1802019-03-27 14:21:34 -04001405 ANGLE_TRY(contextVk->finishImpl());
Luc Ferron018709f2018-05-10 13:53:11 -04001406
Luc Ferron534b00d2018-05-18 08:16:53 -04001407 // The buffer we copied to needs to be invalidated before we read from it because its not been
1408 // created with the host coherent bit.
Jamie Madilld754eb52018-07-19 14:55:03 -04001409 ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk));
Yuly Novikov6c6c76c2018-05-17 18:45:06 +00001410
Jamie Madillb436aac2018-07-18 17:23:48 -04001411 PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, readPixelBuffer,
Rafael Cintron05a449a2018-06-20 18:08:04 -07001412 static_cast<uint8_t *>(pixels));
Luc Ferron018709f2018-05-10 13:53:11 -04001413
Jamie Madill7c985f52018-11-29 18:16:17 -05001414 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -04001415}
Jamie Madill58675012018-05-22 14:54:07 -04001416
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001417gl::Extents FramebufferVk::getReadImageExtents() const
Jamie Madill58675012018-05-22 14:54:07 -04001418{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001419 ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width);
1420 ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height);
1421
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001422 return getColorReadRenderTarget()->getExtents();
Jamie Madill58675012018-05-22 14:54:07 -04001423}
Jamie Madill502d2e22018-11-01 11:06:23 -04001424
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001425gl::Rectangle FramebufferVk::getCompleteRenderArea() const
1426{
1427 return gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height);
1428}
1429
1430gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const
1431{
1432 const gl::Rectangle renderArea(0, 0, mState.getDimensions().width,
1433 mState.getDimensions().height);
1434 bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO();
1435
1436 return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport);
1437}
1438
1439void FramebufferVk::onScissorChange(ContextVk *contextVk)
1440{
1441 gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
1442
1443 // If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass
1444 // is restarted. Otherwise, we can continue using the same renderpass area.
1445 //
1446 // Without a scissor, the render pass area covers the whole of the framebuffer. With a
1447 // scissored clear, the render pass area could be smaller than the framebuffer size. When the
1448 // scissor changes, if the scissor area is completely encompassed by the render pass area, it's
1449 // possible to continue using the same render pass. However, if the current render pass area
1450 // is too small, we need to start a new one. The latter can happen if a scissored clear starts
1451 // a render pass, the scissor is disabled and a draw call is issued to affect the whole
1452 // framebuffer.
Geoff Langee244c72019-05-06 10:30:18 -04001453 mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001454 if (mFramebuffer.hasStartedRenderPass() &&
1455 !mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea))
1456 {
Geoff Langee244c72019-05-06 10:30:18 -04001457 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001458 }
1459}
1460
Jamie Madill502d2e22018-11-01 11:06:23 -04001461RenderTargetVk *FramebufferVk::getFirstRenderTarget() const
1462{
1463 for (auto *renderTarget : mRenderTargetCache.getColors())
1464 {
1465 if (renderTarget)
1466 {
1467 return renderTarget;
1468 }
1469 }
1470
1471 return mRenderTargetCache.getDepthStencil();
1472}
1473
1474GLint FramebufferVk::getSamples() const
1475{
1476 RenderTargetVk *firstRT = getFirstRenderTarget();
1477 return firstRT ? firstRT->getImage().getSamples() : 0;
1478}
1479
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001480} // namespace rx