blob: 49247b618cb0ff4aa8d9312b4741eb74c0dca4da [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
2// Copyright 2016 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// FramebufferVk.cpp:
7// Implements the class methods for FramebufferVk.
8//
9
10#include "libANGLE/renderer/vulkan/FramebufferVk.h"
11
Jamie Madill7b57b9d2017-01-13 09:33:38 -050012#include <vulkan/vulkan.h>
Jamie Madill231c7f52017-04-26 13:45:37 -040013#include <array>
Jamie Madill7b57b9d2017-01-13 09:33:38 -050014
Jamie Madill9e54b5a2016-05-25 12:57:39 -040015#include "common/debug.h"
Jamie Madillc564c072017-06-01 12:45:42 -040016#include "libANGLE/Context.h"
17#include "libANGLE/Display.h"
Jamie Madill7b57b9d2017-01-13 09:33:38 -050018#include "libANGLE/formatutils.h"
19#include "libANGLE/renderer/renderer_utils.h"
Jamie Madill1f46bc12018-02-20 16:09:43 -050020#include "libANGLE/renderer/vulkan/CommandGraph.h"
Jamie Madill7b57b9d2017-01-13 09:33:38 -050021#include "libANGLE/renderer/vulkan/ContextVk.h"
Jamie Madill5deea722017-02-16 10:44:46 -050022#include "libANGLE/renderer/vulkan/DisplayVk.h"
Jamie Madill7b57b9d2017-01-13 09:33:38 -050023#include "libANGLE/renderer/vulkan/RenderTargetVk.h"
24#include "libANGLE/renderer/vulkan/RendererVk.h"
25#include "libANGLE/renderer/vulkan/SurfaceVk.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050026#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madill3ea463b2019-06-19 14:21:33 -040027#include "libANGLE/trace.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040028
29namespace rx
30{
31
Jamie Madill7b57b9d2017-01-13 09:33:38 -050032namespace
33{
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -040034// The value to assign an alpha channel that's emulated. The type is unsigned int, though it will
35// automatically convert to the actual data type.
36constexpr unsigned int kEmulatedAlphaValue = 1;
37
Luc Ferron534b00d2018-05-18 08:16:53 -040038constexpr size_t kMinReadPixelsBufferSize = 128000;
Mohan Maiya8a43b8c2019-08-02 08:47:13 -070039
40// Alignment value to accommodate the largest known, for now, uncompressed Vulkan format
Shahbaz Youssefic9724352019-08-20 14:55:43 -040041// VK_FORMAT_R64G64B64A64_SFLOAT, while supporting 3-component types such as
42// VK_FORMAT_R16G16B16_SFLOAT.
43constexpr size_t kReadPixelsBufferAlignment = 32 * 3;
Mohan Maiya8a43b8c2019-08-02 08:47:13 -070044
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -040045// Clear values are only used when loadOp=Clear is set in clearWithRenderPassOp. When starting a
46// new render pass, the clear value is set to an unlikely value (bright pink) to stand out better
47// in case of a bug.
48constexpr VkClearValue kUninitializedClearValue = {{{0.95, 0.05, 0.95, 0.95}}};
Luc Ferron534b00d2018-05-18 08:16:53 -040049
Jamie Madill66546be2018-03-08 09:47:20 -050050const gl::InternalFormat &GetReadAttachmentInfo(const gl::Context *context,
51 RenderTargetVk *renderTarget)
Jamie Madill7b57b9d2017-01-13 09:33:38 -050052{
Jamie Madillbc543422018-03-30 10:43:19 -040053 GLenum implFormat =
Jamie Madill0631e192019-04-18 16:09:12 -040054 renderTarget->getImageFormat().imageFormat().fboImplementationInternalFormat;
Jamie Madill66546be2018-03-08 09:47:20 -050055 return gl::GetSizedInternalFormatInfo(implFormat);
Jamie Madill7b57b9d2017-01-13 09:33:38 -050056}
Luc Ferron26581112018-06-21 09:43:08 -040057
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040058bool HasSrcBlitFeature(RendererVk *renderer, RenderTargetVk *srcRenderTarget)
Luc Ferron26581112018-06-21 09:43:08 -040059{
Jamie Madill0631e192019-04-18 16:09:12 -040060 const VkFormat srcFormat = srcRenderTarget->getImageFormat().vkImageFormat;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040061 return renderer->hasImageFormatFeatureBits(srcFormat, VK_FORMAT_FEATURE_BLIT_SRC_BIT);
62}
Luc Ferron26581112018-06-21 09:43:08 -040063
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -040064bool HasDstBlitFeature(RendererVk *renderer, RenderTargetVk *dstRenderTarget)
65{
66 const VkFormat dstFormat = dstRenderTarget->getImageFormat().vkImageFormat;
67 return renderer->hasImageFormatFeatureBits(dstFormat, VK_FORMAT_FEATURE_BLIT_DST_BIT);
68}
69
70// Returns false if destination has any channel the source doesn't. This means that channel was
71// emulated and using the Vulkan blit command would overwrite that emulated channel.
72bool areSrcAndDstColorChannelsBlitCompatible(RenderTargetVk *srcRenderTarget,
73 RenderTargetVk *dstRenderTarget)
74{
75 const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat();
76 const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat();
77
78 // Luminance/alpha formats are not renderable, so they can't have ended up in a framebuffer to
79 // participate in a blit.
80 ASSERT(!dstFormat.isLUMA() && !srcFormat.isLUMA());
81
82 // All color formats have the red channel.
83 ASSERT(dstFormat.redBits > 0 && srcFormat.redBits > 0);
84
85 return (dstFormat.greenBits > 0 || srcFormat.greenBits == 0) &&
86 (dstFormat.blueBits > 0 || srcFormat.blueBits == 0) &&
87 (dstFormat.alphaBits > 0 || srcFormat.alphaBits == 0);
88}
89
90bool areSrcAndDstDepthStencilChannelsBlitCompatible(RenderTargetVk *srcRenderTarget,
91 RenderTargetVk *dstRenderTarget)
92{
93 const angle::Format &srcFormat = srcRenderTarget->getImageFormat().angleFormat();
94 const angle::Format &dstFormat = dstRenderTarget->getImageFormat().angleFormat();
95
96 return (dstFormat.depthBits > 0 || srcFormat.depthBits == 0) &&
97 (dstFormat.stencilBits > 0 || srcFormat.stencilBits == 0);
Luc Ferron26581112018-06-21 09:43:08 -040098}
Jamie Madillb436aac2018-07-18 17:23:48 -040099
100// Special rules apply to VkBufferImageCopy with depth/stencil. The components are tightly packed
101// into a depth or stencil section of the destination buffer. See the spec:
102// https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkBufferImageCopy.html
103const angle::Format &GetDepthStencilImageToBufferFormat(const angle::Format &imageFormat,
104 VkImageAspectFlagBits copyAspect)
105{
106 if (copyAspect == VK_IMAGE_ASPECT_STENCIL_BIT)
107 {
108 ASSERT(imageFormat.id == angle::FormatID::D24_UNORM_S8_UINT ||
109 imageFormat.id == angle::FormatID::D32_FLOAT_S8X24_UINT ||
110 imageFormat.id == angle::FormatID::S8_UINT);
111 return angle::Format::Get(angle::FormatID::S8_UINT);
112 }
113
114 ASSERT(copyAspect == VK_IMAGE_ASPECT_DEPTH_BIT);
115
116 switch (imageFormat.id)
117 {
118 case angle::FormatID::D16_UNORM:
119 return imageFormat;
120 case angle::FormatID::D24_UNORM_X8_UINT:
121 return imageFormat;
122 case angle::FormatID::D24_UNORM_S8_UINT:
123 return angle::Format::Get(angle::FormatID::D24_UNORM_X8_UINT);
124 case angle::FormatID::D32_FLOAT:
125 return imageFormat;
126 case angle::FormatID::D32_FLOAT_S8X24_UINT:
127 return angle::Format::Get(angle::FormatID::D32_FLOAT);
128 default:
129 UNREACHABLE();
130 return imageFormat;
131 }
132}
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400133
134void SetEmulatedAlphaValue(const vk::Format &format, VkClearColorValue *value)
135{
136 if (format.vkFormatIsInt)
137 {
138 if (format.vkFormatIsUnsigned)
139 {
140 value->uint32[3] = kEmulatedAlphaValue;
141 }
142 else
143 {
144 value->int32[3] = kEmulatedAlphaValue;
145 }
146 }
147 else
148 {
149 value->float32[3] = kEmulatedAlphaValue;
150 }
151}
Jamie Madillbcf467f2018-05-23 09:46:00 -0400152} // anonymous namespace
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500153
154// static
Jamie Madill639bc902018-07-18 17:08:27 -0400155FramebufferVk *FramebufferVk::CreateUserFBO(RendererVk *renderer, const gl::FramebufferState &state)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500156{
Jamie Madill639bc902018-07-18 17:08:27 -0400157 return new FramebufferVk(renderer, state, nullptr);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500158}
159
160// static
Jamie Madill639bc902018-07-18 17:08:27 -0400161FramebufferVk *FramebufferVk::CreateDefaultFBO(RendererVk *renderer,
162 const gl::FramebufferState &state,
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500163 WindowSurfaceVk *backbuffer)
164{
Jamie Madill639bc902018-07-18 17:08:27 -0400165 return new FramebufferVk(renderer, state, backbuffer);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500166}
167
Jamie Madill639bc902018-07-18 17:08:27 -0400168FramebufferVk::FramebufferVk(RendererVk *renderer,
169 const gl::FramebufferState &state,
170 WindowSurfaceVk *backbuffer)
Jamie Madill7f2520f2019-06-26 11:18:33 -0400171 : FramebufferImpl(state), mBackbuffer(backbuffer), mActiveColorComponents(0)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500172{
Mohan Maiya8a43b8c2019-08-02 08:47:13 -0700173 mReadPixelBuffer.init(renderer, VK_BUFFER_USAGE_TRANSFER_DST_BIT, kReadPixelsBufferAlignment,
174 kMinReadPixelsBufferSize, true);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500175}
176
Jamie Madill58675012018-05-22 14:54:07 -0400177FramebufferVk::~FramebufferVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400178
Jamie Madillc564c072017-06-01 12:45:42 -0400179void FramebufferVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500180{
Luc Ferron534b00d2018-05-18 08:16:53 -0400181 ContextVk *contextVk = vk::GetImpl(context);
Geoff Langee244c72019-05-06 10:30:18 -0400182 mFramebuffer.release(contextVk);
Luc Ferron534b00d2018-05-18 08:16:53 -0400183
Geoff Langee244c72019-05-06 10:30:18 -0400184 mReadPixelBuffer.release(contextVk);
Jamie Madill5deea722017-02-16 10:44:46 -0500185}
186
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400187angle::Result FramebufferVk::discard(const gl::Context *context,
188 size_t count,
189 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400190{
Shahbaz Youssefi97123e32019-07-08 15:11:16 -0400191 return invalidate(context, count, attachments);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400192}
193
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400194angle::Result FramebufferVk::invalidate(const gl::Context *context,
195 size_t count,
196 const GLenum *attachments)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400197{
Shahbaz Youssefida904482019-07-02 10:49:14 -0400198 mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial());
199
200 if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass())
201 {
202 invalidateImpl(vk::GetImpl(context), count, attachments);
203 }
204
205 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400206}
207
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400208angle::Result FramebufferVk::invalidateSub(const gl::Context *context,
209 size_t count,
210 const GLenum *attachments,
211 const gl::Rectangle &area)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400212{
Shahbaz Youssefida904482019-07-02 10:49:14 -0400213 mFramebuffer.updateQueueSerial(vk::GetImpl(context)->getCurrentQueueSerial());
214
215 // RenderPass' storeOp cannot be made conditional to a specific region, so we only apply this
216 // hint if the requested area encompasses the render area.
217 if (mFramebuffer.valid() && mFramebuffer.hasStartedRenderPass() &&
218 area.encloses(mFramebuffer.getRenderPassRenderArea()))
219 {
220 invalidateImpl(vk::GetImpl(context), count, attachments);
221 }
222
223 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400224}
225
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400226angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400227{
Jamie Madill0cec82a2018-03-14 09:21:07 -0400228 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500229
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400230 bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT));
231 bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT));
232 bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT));
233 gl::DrawBufferMask clearColorBuffers;
234 if (clearColor)
235 {
236 clearColorBuffers = mState.getEnabledDrawBuffers();
237 }
238
239 const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color;
240 const VkClearDepthStencilValue &clearDepthStencilValue =
241 contextVk->getClearDepthStencilValue().depthStencil;
242
243 return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue,
244 clearDepthStencilValue);
245}
246
247angle::Result FramebufferVk::clearImpl(const gl::Context *context,
248 gl::DrawBufferMask clearColorBuffers,
249 bool clearDepth,
250 bool clearStencil,
251 const VkClearColorValue &clearColorValue,
252 const VkClearDepthStencilValue &clearDepthStencilValue)
253{
254 ContextVk *contextVk = vk::GetImpl(context);
255
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400256 const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
257
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400258 // Discard clear altogether if scissor has 0 width or height.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400259 if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400260 {
261 return angle::Result::Continue;
262 }
263
Geoff Langee244c72019-05-06 10:30:18 -0400264 mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400265
266 // This function assumes that only enabled attachments are asked to be cleared.
267 ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers);
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400268
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400269 // Adjust clear behavior based on whether the respective attachments are present; if asked to
270 // clear a non-existent attachment, don't attempt to clear it.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400271
272 VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask();
273 bool clearColor = clearColorBuffers.any();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400274
Jamie Madill0cec82a2018-03-14 09:21:07 -0400275 const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400276 clearDepth = clearDepth && depthAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400277 ASSERT(!clearDepth || depthAttachment->isAttached());
278
279 const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment();
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400280 clearStencil = clearStencil && stencilAttachment;
Jamie Madill0cec82a2018-03-14 09:21:07 -0400281 ASSERT(!clearStencil || stencilAttachment->isAttached());
282
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400283 uint8_t stencilMask =
284 static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask);
285
286 // The front-end should ensure we don't attempt to clear color if all channels are masked.
287 ASSERT(!clearColor || colorMaskFlags != 0);
288 // The front-end should ensure we don't attempt to clear depth if depth write is disabled.
289 ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask);
290 // The front-end should ensure we don't attempt to clear stencil if all bits are masked.
291 ASSERT(!clearStencil || stencilMask != 0);
292
293 // If there is nothing to clear, return right away (for example, if asked to clear depth, but
294 // there is no depth attachment).
Shahbaz Youssefi02a579e2019-03-27 14:21:20 -0400295 if (!clearColor && !clearDepth && !clearStencil)
296 {
297 return angle::Result::Continue;
298 }
299
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400300 VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue;
Shahbaz Youssefid856ca42018-10-31 16:55:12 -0400301
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400302 // We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If
303 // there's a depth mask, depth clearing is already disabled.
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400304 bool maskedClearColor =
305 clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400306 bool maskedClearStencil = stencilMask != 0xFF;
307
308 bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor;
309 bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400310
311 // At least one of color, depth or stencil should be clearable with render pass loadOp for us
312 // to use this clear path.
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400313 bool clearAnyWithRenderPassLoadOp =
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400314 clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400315
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400316 if (clearAnyWithRenderPassLoadOp)
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400317 {
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400318 // Clearing color is indicated by the set bits in this mask. If not clearing colors with
319 // render pass loadOp, the default value of all-zeros means the clear is not done in
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400320 // clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass
321 // loadOp.
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400322 gl::DrawBufferMask clearBuffersWithRenderPassLoadOp;
323 if (clearColorWithRenderPassLoadOp)
324 {
325 clearBuffersWithRenderPassLoadOp = clearColorBuffers;
326 }
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400327 ANGLE_TRY(clearWithRenderPassOp(
328 contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth,
329 clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue));
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400330
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400331 // On some hardware, having inline commands at this point results in corrupted output. In
332 // that case, end the render pass immediately. http://anglebug.com/2361
Jonah Ryan-Davis776694c2019-05-08 10:28:55 -0400333 if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled)
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400334 {
Geoff Langee244c72019-05-06 10:30:18 -0400335 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi27f115a2019-04-01 10:33:21 -0400336 }
337
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400338 // Fallback to other methods for whatever isn't cleared here.
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400339 clearDepth = false;
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400340 if (clearColorWithRenderPassLoadOp)
341 {
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400342 clearColorBuffers.reset();
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400343 clearColor = false;
344 }
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400345 if (clearStencilWithRenderPassLoadOp)
346 {
347 clearStencil = false;
348 }
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400349
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400350 // If nothing left to clear, early out.
351 if (!clearColor && !clearStencil)
Shahbaz Youssefif1153b02019-03-27 11:22:54 -0400352 {
353 return angle::Result::Continue;
354 }
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -0400355 }
356
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400357 // Note: depth clear is always done through render pass loadOp.
Shahbaz Youssefi127990f2019-04-04 13:52:04 -0400358 ASSERT(clearDepth == false);
359
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -0400360 // The most costly clear mode is when we need to mask out specific color channels or stencil
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -0400361 // bits. This can only be done with a draw call.
362 return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil,
363 colorMaskFlags, stencilMask, clearColorValue,
364 static_cast<uint8_t>(modifiedDepthStencilValue.stencil));
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400365}
366
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400367angle::Result FramebufferVk::clearBufferfv(const gl::Context *context,
368 GLenum buffer,
369 GLint drawbuffer,
370 const GLfloat *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400371{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400372 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400373
374 bool clearDepth = false;
375 gl::DrawBufferMask clearColorBuffers;
376
377 if (buffer == GL_DEPTH)
378 {
379 clearDepth = true;
380 clearValue.depthStencil.depth = values[0];
381 }
382 else
383 {
384 clearColorBuffers.set(drawbuffer);
385 clearValue.color.float32[0] = values[0];
386 clearValue.color.float32[1] = values[1];
387 clearValue.color.float32[2] = values[2];
388 clearValue.color.float32[3] = values[3];
389 }
390
391 return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color,
392 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400393}
394
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400395angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context,
396 GLenum buffer,
397 GLint drawbuffer,
398 const GLuint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400399{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400400 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400401
402 gl::DrawBufferMask clearColorBuffers;
403 clearColorBuffers.set(drawbuffer);
404
405 clearValue.color.uint32[0] = values[0];
406 clearValue.color.uint32[1] = values[1];
407 clearValue.color.uint32[2] = values[2];
408 clearValue.color.uint32[3] = values[3];
409
410 return clearImpl(context, clearColorBuffers, false, false, clearValue.color,
411 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400412}
413
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400414angle::Result FramebufferVk::clearBufferiv(const gl::Context *context,
415 GLenum buffer,
416 GLint drawbuffer,
417 const GLint *values)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400418{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400419 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400420
421 bool clearStencil = false;
422 gl::DrawBufferMask clearColorBuffers;
423
424 if (buffer == GL_STENCIL)
425 {
426 clearStencil = true;
427 clearValue.depthStencil.stencil =
428 gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max());
429 }
430 else
431 {
432 clearColorBuffers.set(drawbuffer);
433 clearValue.color.int32[0] = values[0];
434 clearValue.color.int32[1] = values[1];
435 clearValue.color.int32[2] = values[2];
436 clearValue.color.int32[3] = values[3];
437 }
438
439 return clearImpl(context, clearColorBuffers, false, clearStencil, clearValue.color,
440 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400441}
442
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400443angle::Result FramebufferVk::clearBufferfi(const gl::Context *context,
444 GLenum buffer,
445 GLint drawbuffer,
446 GLfloat depth,
447 GLint stencil)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400448{
Shahbaz Youssefiedef8952019-04-04 10:03:09 -0400449 VkClearValue clearValue = {};
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -0400450
451 clearValue.depthStencil.depth = depth;
452 clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max());
453
454 return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color,
455 clearValue.depthStencil);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400456}
457
Jamie Madill4928b7c2017-06-20 12:57:39 -0400458GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400459{
Jamie Madill66546be2018-03-08 09:47:20 -0500460 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400461}
462
Jamie Madill4928b7c2017-06-20 12:57:39 -0400463GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400464{
Jamie Madill66546be2018-03-08 09:47:20 -0500465 return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400466}
467
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400468angle::Result FramebufferVk::readPixels(const gl::Context *context,
469 const gl::Rectangle &area,
470 GLenum format,
471 GLenum type,
472 void *pixels)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400473{
Luc Ferrona1c72422018-05-14 15:58:28 -0400474 // Clip read area to framebuffer.
475 const gl::Extents &fbSize = getState().getReadAttachment()->getSize();
476 const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400477 ContextVk *contextVk = vk::GetImpl(context);
Luc Ferronbf6dc372018-06-28 15:24:19 -0400478
Luc Ferrona1c72422018-05-14 15:58:28 -0400479 gl::Rectangle clippedArea;
480 if (!ClipRectangle(area, fbRect, &clippedArea))
481 {
482 // nothing to read
Jamie Madill7c985f52018-11-29 18:16:17 -0500483 return angle::Result::Continue;
Luc Ferrona1c72422018-05-14 15:58:28 -0400484 }
Luc Ferronbf6dc372018-06-28 15:24:19 -0400485 gl::Rectangle flippedArea = clippedArea;
Geoff Lange076a232018-07-16 15:34:05 -0400486 if (contextVk->isViewportFlipEnabledForReadFBO())
Luc Ferronbf6dc372018-06-28 15:24:19 -0400487 {
488 flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height;
489 }
Luc Ferrona1c72422018-05-14 15:58:28 -0400490
Jamie Madillc3dc5d42018-12-30 12:12:04 -0500491 const gl::State &glState = context->getState();
Frank Henigman1ffad842018-09-24 23:40:45 -0400492 const gl::PixelPackState &packState = glState.getPackState();
Luc Ferronbf6dc372018-06-28 15:24:19 -0400493
Luc Ferrona1c72422018-05-14 15:58:28 -0400494 const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type);
495
496 GLuint outputPitch = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400497 ANGLE_VK_CHECK_MATH(contextVk,
498 sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment,
499 packState.rowLength, &outputPitch));
Luc Ferrona1c72422018-05-14 15:58:28 -0400500 GLuint outputSkipBytes = 0;
Jamie Madillabfbc0f2018-10-09 12:48:52 -0400501 ANGLE_VK_CHECK_MATH(contextVk, sizedFormatInfo.computeSkipBytes(type, outputPitch, 0, packState,
502 false, &outputSkipBytes));
Luc Ferrona1c72422018-05-14 15:58:28 -0400503
504 outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes +
505 (clippedArea.y - area.y) * outputPitch;
Luc Ferron60284222018-03-20 16:01:44 -0400506
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400507 const angle::Format &angleFormat = GetFormatFromFormatType(format, type);
508
Frank Henigman1ffad842018-09-24 23:40:45 -0400509 PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder,
Jamie Madilldb9c69e2018-07-18 17:23:47 -0400510 glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0);
Frank Henigman1ffad842018-09-24 23:40:45 -0400511 if (contextVk->isViewportFlipEnabledForReadFBO())
512 {
513 params.reverseRowOrder = !params.reverseRowOrder;
514 }
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500515
Jamie Madill21061022018-07-12 23:56:30 -0400516 ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT,
Luc Ferron1617e692018-07-11 11:08:19 -0400517 getColorReadRenderTarget(),
Rafael Cintron05a449a2018-06-20 18:08:04 -0700518 static_cast<uint8_t *>(pixels) + outputSkipBytes));
Jamie Madillc773ab92019-06-25 17:11:58 -0400519 mReadPixelBuffer.releaseInFlightBuffers(contextVk);
Jamie Madill7c985f52018-11-29 18:16:17 -0500520 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -0400521}
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500522
Luc Ferron26581112018-06-21 09:43:08 -0400523RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const
524{
525 return mRenderTargetCache.getDepthStencil();
526}
527
Tim Van Patten56ba54c2019-08-08 13:03:34 -0600528RenderTargetVk *FramebufferVk::getColorDrawRenderTarget(size_t colorIndex) const
529{
530 RenderTargetVk *renderTarget = mRenderTargetCache.getColorDraw(mState, colorIndex);
531 ASSERT(renderTarget && renderTarget->getImage().valid());
532 return renderTarget;
533}
534
Jamie Madill58675012018-05-22 14:54:07 -0400535RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const
Luc Ferron018709f2018-05-10 13:53:11 -0400536{
537 RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400538 ASSERT(renderTarget && renderTarget->getImage().valid());
Luc Ferron018709f2018-05-10 13:53:11 -0400539 return renderTarget;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400540}
541
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400542angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400543 const gl::Rectangle &sourceArea,
544 const gl::Rectangle &destArea,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400545 RenderTargetVk *readRenderTarget,
546 RenderTargetVk *drawRenderTarget,
547 GLenum filter,
548 bool colorBlit,
549 bool depthBlit,
550 bool stencilBlit,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400551 bool flipX,
552 bool flipY)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400553{
554 // Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
555 // it should never be the case that both color and depth/stencil need to be blitted at
556 // at the same time.
557 ASSERT(colorBlit != (depthBlit || stencilBlit));
558
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400559 vk::ImageHelper *srcImage = &readRenderTarget->getImage();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400560 vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
561
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400562 VkImageAspectFlags imageAspectMask = srcImage->getAspectFlags();
563 VkImageAspectFlags blitAspectMask = imageAspectMask;
564
565 // Remove depth or stencil aspects if they are not requested to be blitted.
566 if (!depthBlit)
567 {
568 blitAspectMask &= ~VK_IMAGE_ASPECT_DEPTH_BIT;
569 }
570 if (!stencilBlit)
571 {
572 blitAspectMask &= ~VK_IMAGE_ASPECT_STENCIL_BIT;
573 }
574
575 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
576 {
577 vk::CommandBuffer *srcLayoutChange;
578 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
579 srcImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferSrc, srcLayoutChange);
580 }
581
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400582 vk::CommandBuffer *commandBuffer = nullptr;
583 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
584
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400585 srcImage->addReadDependency(&mFramebuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400586
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400587 VkImageBlit blit = {};
588 blit.srcSubresource.aspectMask = blitAspectMask;
589 blit.srcSubresource.mipLevel = readRenderTarget->getLevelIndex();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400590 blit.srcSubresource.baseArrayLayer = readRenderTarget->getLayerIndex();
591 blit.srcSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400592 blit.srcOffsets[0] = {sourceArea.x0(), sourceArea.y0(), 0};
593 blit.srcOffsets[1] = {sourceArea.x1(), sourceArea.y1(), 1};
594 blit.dstSubresource.aspectMask = blitAspectMask;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400595 blit.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
596 blit.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
597 blit.dstSubresource.layerCount = 1;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400598 blit.dstOffsets[0] = {destArea.x0(), destArea.y0(), 0};
599 blit.dstOffsets[1] = {destArea.x1(), destArea.y1(), 1};
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400600
601 // Requirement of the copyImageToBuffer, the dst image must be in
602 // VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400603 dstImage->changeLayout(imageAspectMask, vk::ImageLayout::TransferDst, commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400604
605 commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
606 dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
607 gl_vk::GetFilter(filter));
608
609 return angle::Result::Continue;
610}
611
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400612angle::Result FramebufferVk::blit(const gl::Context *context,
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400613 const gl::Rectangle &sourceAreaIn,
614 const gl::Rectangle &destAreaIn,
Jamie Madill64b7c4f2018-10-19 11:38:04 -0400615 GLbitfield mask,
616 GLenum filter)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400617{
Luc Ferron26581112018-06-21 09:43:08 -0400618 ContextVk *contextVk = vk::GetImpl(context);
619 RendererVk *renderer = contextVk->getRenderer();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400620 UtilsVk &utilsVk = contextVk->getUtils();
Luc Ferron26581112018-06-21 09:43:08 -0400621
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400622 const gl::State &glState = contextVk->getState();
623 const gl::Framebuffer *srcFramebuffer = glState.getReadFramebuffer();
624
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400625 const bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0;
626 const bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0;
627 const bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400628
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400629 const bool isResolve = srcFramebuffer->getCachedSamples(context) > 1;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400630
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400631 FramebufferVk *srcFramebufferVk = vk::GetImpl(srcFramebuffer);
632 const bool srcFramebufferFlippedY = contextVk->isViewportFlipEnabledForReadFBO();
633 const bool destFramebufferFlippedY = contextVk->isViewportFlipEnabledForDrawFBO();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400634
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400635 gl::Rectangle sourceArea = sourceAreaIn;
636 gl::Rectangle destArea = destAreaIn;
637
638 // Note: GLES (all 3.x versions) require source and dest area to be identical when
639 // resolving.
640 ASSERT(!isResolve ||
641 (sourceArea.x == destArea.x && sourceArea.y == destArea.y &&
642 sourceArea.width == destArea.width && sourceArea.height == destArea.height));
643
644 const gl::Rectangle srcFramebufferDimensions =
645 srcFramebufferVk->mState.getDimensions().toRect();
646
647 // If the destination is flipped in either direction, we will flip the source instead so that
648 // the destination area is always unflipped.
649 sourceArea = sourceArea.flip(destArea.isReversedX(), destArea.isReversedY());
650 destArea = destArea.removeReversal();
651
652 // Calculate the stretch factor prior to any clipping, as it needs to remain constant.
653 const float stretch[2] = {
654 std::abs(sourceArea.width / static_cast<float>(destArea.width)),
655 std::abs(sourceArea.height / static_cast<float>(destArea.height)),
656 };
657
658 // First, clip the source area to framebuffer. That requires transforming the dest area to
659 // match the clipped source.
660 gl::Rectangle absSourceArea = sourceArea.removeReversal();
661 gl::Rectangle clippedSourceArea;
662 if (!gl::ClipRectangle(srcFramebufferDimensions, absSourceArea, &clippedSourceArea))
663 {
664 return angle::Result::Continue;
665 }
666
667 // Resize the destination area based on the new size of source. Note again that stretch is
668 // calculated as SrcDimension/DestDimension.
669 gl::Rectangle srcClippedDestArea;
670 if (isResolve)
671 {
672 // Source and dest areas are identical in resolve.
673 srcClippedDestArea = clippedSourceArea;
674 }
675 else if (clippedSourceArea == absSourceArea)
676 {
677 // If there was no clipping, keep dest area as is.
678 srcClippedDestArea = destArea;
679 }
680 else
681 {
682 // Shift dest area's x0,y0,x1,y1 by as much as the source area's got shifted (taking
683 // stretching into account)
684 float x0Shift = std::round((clippedSourceArea.x - absSourceArea.x) / stretch[0]);
685 float y0Shift = std::round((clippedSourceArea.y - absSourceArea.y) / stretch[1]);
686 float x1Shift = std::round((absSourceArea.x1() - clippedSourceArea.x1()) / stretch[0]);
687 float y1Shift = std::round((absSourceArea.y1() - clippedSourceArea.y1()) / stretch[1]);
688
689 // If the source area was reversed in any direction, the shift should be applied in the
690 // opposite direction as well.
691 if (sourceArea.isReversedX())
692 {
693 std::swap(x0Shift, x1Shift);
694 }
695
696 if (sourceArea.isReversedY())
697 {
698 std::swap(y0Shift, y1Shift);
699 }
700
701 srcClippedDestArea.x = destArea.x0() + static_cast<int>(x0Shift);
702 srcClippedDestArea.y = destArea.y0() + static_cast<int>(y0Shift);
703 int x1 = destArea.x1() - static_cast<int>(x1Shift);
704 int y1 = destArea.y1() - static_cast<int>(y1Shift);
705
706 srcClippedDestArea.width = x1 - srcClippedDestArea.x;
707 srcClippedDestArea.height = y1 - srcClippedDestArea.y;
708 }
709
710 // If framebuffers are flipped in Y, flip the source and dest area (which define the
711 // transformation regardless of clipping), as well as the blit area (which is the clipped
712 // dest area).
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400713 if (srcFramebufferFlippedY)
714 {
715 sourceArea.y = srcFramebufferDimensions.height - sourceArea.y;
716 sourceArea.height = -sourceArea.height;
717 }
718 if (destFramebufferFlippedY)
719 {
720 destArea.y = mState.getDimensions().height - destArea.y;
721 destArea.height = -destArea.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400722
723 srcClippedDestArea.y =
724 mState.getDimensions().height - srcClippedDestArea.y - srcClippedDestArea.height;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400725 }
726
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400727 const bool flipX = sourceArea.isReversedX() != destArea.isReversedX();
728 const bool flipY = sourceArea.isReversedY() != destArea.isReversedY();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400729
730 // GLES doesn't allow flipping the parameters of glBlitFramebuffer if performing a resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400731 ASSERT(!isResolve ||
732 (flipX == false && flipY == (srcFramebufferFlippedY != destFramebufferFlippedY)));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400733
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400734 // Again, transfer the destination flip to source, so dest is unflipped. Note that destArea
735 // was not reversed until the final possible Y-flip.
736 ASSERT(!destArea.isReversedX());
737 sourceArea = sourceArea.flip(false, destArea.isReversedY());
738 destArea = destArea.removeReversal();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400739
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400740 // Clip the destination area to the framebuffer size and scissor. Note that we don't care
741 // about the source area anymore. The offset translation is done based on the original source
742 // and destination rectangles. The stretch factor is already calculated as well.
743 gl::Rectangle blitArea;
744 if (!gl::ClipRectangle(getScissoredRenderArea(contextVk), srcClippedDestArea, &blitArea))
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400745 {
746 return angle::Result::Continue;
747 }
748
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400749 bool noClip = blitArea == destArea && stretch[0] == 1.0f && stretch[1] == 1.0f;
750 bool noFlip = !flipX && !flipY;
751 bool disableFlippingBlitWithCommand =
752 contextVk->getRenderer()->getFeatures().disableFlippingBlitWithCommand.enabled;
753
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400754 UtilsVk::BlitResolveParameters params;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400755 params.srcOffset[0] = sourceArea.x;
756 params.srcOffset[1] = sourceArea.y;
757 params.destOffset[0] = destArea.x;
758 params.destOffset[1] = destArea.y;
759 params.stretch[0] = stretch[0];
760 params.stretch[1] = stretch[1];
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400761 params.srcExtents[0] = srcFramebufferDimensions.width;
762 params.srcExtents[1] = srcFramebufferDimensions.height;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400763 params.blitArea = blitArea;
764 params.linear = filter == GL_LINEAR;
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400765 params.flipX = flipX;
766 params.flipY = flipY;
767
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400768 if (blitColorBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400769 {
770 RenderTargetVk *readRenderTarget = srcFramebufferVk->getColorReadRenderTarget();
771 params.srcLayer = readRenderTarget->getLayerIndex();
772
773 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400774 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400775
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400776 // If there was no clipping and the format capabilities allow us, use Vulkan's builtin blit.
777 // The reason clipping is prohibited in this path is that due to rounding errors, it would
778 // be hard to guarantee the image stretching remains perfect. That also allows us not to
779 // have to transform back the dest clipping to source.
780 //
781 // For simplicity, we either blit all render targets with a Vulkan command, or none.
782 bool canBlitWithCommand = !isResolve && noClip &&
783 (noFlip || !disableFlippingBlitWithCommand) &&
784 HasSrcBlitFeature(renderer, readRenderTarget);
785 bool areChannelsBlitCompatible = true;
786 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
787 {
788 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
789 canBlitWithCommand =
790 canBlitWithCommand && HasDstBlitFeature(renderer, drawRenderTarget);
791 areChannelsBlitCompatible =
792 areChannelsBlitCompatible &&
793 areSrcAndDstColorChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
794 }
795
796 if (canBlitWithCommand && areChannelsBlitCompatible)
797 {
798 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
799 {
800 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
801 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
802 drawRenderTarget, filter, true, false, false, flipX,
803 flipY));
804 }
805 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400806 // If we're not flipping, use Vulkan's builtin resolve.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400807 else if (isResolve && !flipX && !flipY && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400808 {
809 ANGLE_TRY(resolveColorWithCommand(contextVk, params, &readRenderTarget->getImage()));
810 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400811 // Otherwise use a shader to do blit or resolve.
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400812 else
813 {
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400814 ANGLE_TRY(utilsVk.colorBlitResolve(contextVk, this, &readRenderTarget->getImage(),
815 readRenderTarget->getFetchImageView(), params));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400816 }
817 }
818
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400819 if (blitDepthBuffer || blitStencilBuffer)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400820 {
821 RenderTargetVk *readRenderTarget = srcFramebufferVk->getDepthStencilRenderTarget();
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400822 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil();
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400823 params.srcLayer = readRenderTarget->getLayerIndex();
824
825 // Multisampled images are not allowed to have mips.
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400826 ASSERT(!isResolve || readRenderTarget->getLevelIndex() == 0);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400827
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400828 // Similarly, only blit if there's been no clipping.
829 bool canBlitWithCommand = !isResolve && noClip &&
830 (noFlip || !disableFlippingBlitWithCommand) &&
831 HasSrcBlitFeature(renderer, readRenderTarget) &&
832 HasDstBlitFeature(renderer, drawRenderTarget);
833 bool areChannelsBlitCompatible =
834 areSrcAndDstDepthStencilChannelsBlitCompatible(readRenderTarget, drawRenderTarget);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400835
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400836 if (canBlitWithCommand && areChannelsBlitCompatible)
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400837 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400838 ANGLE_TRY(blitWithCommand(contextVk, sourceArea, destArea, readRenderTarget,
839 drawRenderTarget, filter, false, blitDepthBuffer,
840 blitStencilBuffer, flipX, flipY));
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400841 }
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400842 else
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400843 {
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400844 // Create depth- and stencil-only views for reading.
Shahbaz Youssefi57ad1e12019-08-23 14:53:26 -0400845 vk::DeviceScoped<vk::ImageView> depthView(contextVk->getDevice());
846 vk::DeviceScoped<vk::ImageView> stencilView(contextVk->getDevice());
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400847
848 vk::ImageHelper *depthStencilImage = &readRenderTarget->getImage();
849 uint32_t levelIndex = readRenderTarget->getLevelIndex();
850 uint32_t layerIndex = readRenderTarget->getLayerIndex();
851 gl::TextureType textureType = vk::Get2DTextureType(depthStencilImage->getLayerCount(),
852 depthStencilImage->getSamples());
853
854 if (blitDepthBuffer)
855 {
856 ANGLE_TRY(depthStencilImage->initLayerImageView(
857 contextVk, textureType, VK_IMAGE_ASPECT_DEPTH_BIT, gl::SwizzleState(),
858 &depthView.get(), levelIndex, 1, layerIndex, 1));
859 }
860
861 if (blitStencilBuffer)
862 {
863 ANGLE_TRY(depthStencilImage->initLayerImageView(
864 contextVk, textureType, VK_IMAGE_ASPECT_STENCIL_BIT, gl::SwizzleState(),
865 &stencilView.get(), levelIndex, 1, layerIndex, 1));
866 }
867
868 // If shader stencil export is not possible, defer stencil blit/stencil to another pass.
869 bool hasShaderStencilExport =
870 contextVk->getRenderer()->getFeatures().supportsShaderStencilExport.enabled;
871
872 // Blit depth. If shader stencil export is present, blit stencil as well.
873 if (blitDepthBuffer || (blitStencilBuffer && hasShaderStencilExport))
874 {
Shahbaz Youssefi0b1fbcf2019-08-30 15:05:12 -0400875 const vk::ImageView *depth = blitDepthBuffer ? &depthView.get() : nullptr;
876 const vk::ImageView *stencil =
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400877 blitStencilBuffer && hasShaderStencilExport ? &stencilView.get() : nullptr;
878
879 ANGLE_TRY(utilsVk.depthStencilBlitResolve(contextVk, this, depthStencilImage, depth,
880 stencil, params));
881 }
882
883 // If shader stencil export is not present, blit stencil through a different path.
884 if (blitStencilBuffer && !hasShaderStencilExport)
885 {
886 ANGLE_TRY(utilsVk.stencilBlitResolveNoShaderExport(
887 contextVk, this, depthStencilImage, &stencilView.get(), params));
888 }
889
890 vk::ImageView depthViewObject = depthView.release();
891 vk::ImageView stencilViewObject = stencilView.release();
892
893 contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &depthViewObject);
894 contextVk->releaseObject(contextVk->getCurrentQueueSerial(), &stencilViewObject);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400895 }
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400896 }
897
898 return angle::Result::Continue;
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400899} // namespace rx
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400900
901angle::Result FramebufferVk::resolveColorWithCommand(ContextVk *contextVk,
Shahbaz Youssefide70a712019-06-03 17:05:16 -0400902 const UtilsVk::BlitResolveParameters &params,
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400903 vk::ImageHelper *srcImage)
904{
905 if (srcImage->isLayoutChangeNecessary(vk::ImageLayout::TransferSrc))
906 {
907 vk::CommandBuffer *srcLayoutChange;
908 ANGLE_TRY(srcImage->recordCommands(contextVk, &srcLayoutChange));
909 srcImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferSrc,
910 srcLayoutChange);
911 }
Jamie Madill16c20142018-10-01 13:58:19 -0400912
Shahbaz Youssefi2660b502019-03-21 12:08:40 -0400913 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madill16c20142018-10-01 13:58:19 -0400914 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
915
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400916 // Source's layout change should happen before rendering
917 srcImage->addReadDependency(&mFramebuffer);
Luc Ferron26581112018-06-21 09:43:08 -0400918
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400919 VkImageResolve resolveRegion = {};
920 resolveRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
921 resolveRegion.srcSubresource.mipLevel = 0;
922 resolveRegion.srcSubresource.baseArrayLayer = params.srcLayer;
923 resolveRegion.srcSubresource.layerCount = 1;
924 resolveRegion.srcOffset.x = params.srcOffset[0];
925 resolveRegion.srcOffset.y = params.srcOffset[1];
926 resolveRegion.srcOffset.z = 0;
927 resolveRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
928 resolveRegion.dstSubresource.layerCount = 1;
929 resolveRegion.dstOffset.x = params.destOffset[0];
930 resolveRegion.dstOffset.y = params.destOffset[1];
931 resolveRegion.dstOffset.z = 0;
932 resolveRegion.extent.width = params.srcExtents[0];
933 resolveRegion.extent.height = params.srcExtents[1];
934 resolveRegion.extent.depth = 1;
Jamie Madilld754eb52018-07-19 14:55:03 -0400935
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400936 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Luc Ferron82eda932018-07-09 15:10:22 -0400937 {
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400938 RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
Shahbaz Youssefib407e1a2019-06-03 17:15:51 -0400939 vk::ImageHelper *drawImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
940 drawImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::TransferDst,
941 commandBuffer);
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -0400942
943 resolveRegion.dstSubresource.mipLevel = drawRenderTarget->getLevelIndex();
944 resolveRegion.dstSubresource.baseArrayLayer = drawRenderTarget->getLayerIndex();
945
946 srcImage->resolve(&drawRenderTarget->getImage(), resolveRegion, commandBuffer);
Luc Ferron82eda932018-07-09 15:10:22 -0400947 }
948
Jamie Madill7c985f52018-11-29 18:16:17 -0500949 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400950}
951
Kenneth Russellce8602a2017-10-03 18:23:08 -0700952bool FramebufferVk::checkStatus(const gl::Context *context) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400953{
Luc Ferron5bdf8bd2018-06-20 09:51:37 -0400954 // if we have both a depth and stencil buffer, they must refer to the same object
955 // since we only support packed_depth_stencil and not separate depth and stencil
956 if (mState.hasSeparateDepthAndStencilAttachments())
957 {
958 return false;
959 }
960
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400961 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400962}
963
Jamie Madill67220092019-05-20 11:12:53 -0400964angle::Result FramebufferVk::updateColorAttachment(const gl::Context *context, size_t colorIndexGL)
965{
966 ContextVk *contextVk = vk::GetImpl(context);
967
968 ANGLE_TRY(mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL));
969
970 // Update cached masks for masked clears.
971 RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL];
972 if (renderTarget)
973 {
974 const angle::Format &emulatedFormat = renderTarget->getImageFormat().imageFormat();
975 updateActiveColorMasks(colorIndexGL, emulatedFormat.redBits > 0,
976 emulatedFormat.greenBits > 0, emulatedFormat.blueBits > 0,
977 emulatedFormat.alphaBits > 0);
978
979 const angle::Format &sourceFormat = renderTarget->getImageFormat().angleFormat();
980 mEmulatedAlphaAttachmentMask.set(
981 colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0);
982
983 contextVk->updateColorMask(context->getState().getBlendState());
984 }
985 else
986 {
987 updateActiveColorMasks(colorIndexGL, false, false, false, false);
988 }
989
990 return angle::Result::Continue;
991}
992
Shahbaz Youssefida904482019-07-02 10:49:14 -0400993void FramebufferVk::invalidateImpl(ContextVk *contextVk, size_t count, const GLenum *attachments)
994{
995 ASSERT(mFramebuffer.hasStartedRenderPass());
996
997 gl::DrawBufferMask invalidateColorBuffers;
998 bool invalidateDepthBuffer = false;
999 bool invalidateStencilBuffer = false;
1000
1001 for (size_t i = 0; i < count; ++i)
1002 {
1003 const GLenum attachment = attachments[i];
1004
1005 switch (attachment)
1006 {
1007 case GL_DEPTH:
1008 case GL_DEPTH_ATTACHMENT:
1009 invalidateDepthBuffer = true;
1010 break;
1011 case GL_STENCIL:
1012 case GL_STENCIL_ATTACHMENT:
1013 invalidateStencilBuffer = true;
1014 break;
1015 case GL_DEPTH_STENCIL_ATTACHMENT:
1016 invalidateDepthBuffer = true;
1017 invalidateStencilBuffer = true;
1018 break;
1019 default:
1020 ASSERT(
1021 (attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) ||
1022 (attachment == GL_COLOR));
1023
1024 invalidateColorBuffers.set(
1025 attachment == GL_COLOR ? 0u : (attachment - GL_COLOR_ATTACHMENT0));
1026 }
1027 }
1028
1029 // Set the appropriate storeOp for attachments.
1030 size_t attachmentIndexVk = 0;
1031 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
1032 {
1033 if (invalidateColorBuffers.test(colorIndexGL))
1034 {
1035 mFramebuffer.invalidateRenderPassColorAttachment(attachmentIndexVk);
1036 }
1037 ++attachmentIndexVk;
1038 }
1039
1040 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1041 if (depthStencilRenderTarget)
1042 {
1043 if (invalidateDepthBuffer)
1044 {
1045 mFramebuffer.invalidateRenderPassDepthAttachment(attachmentIndexVk);
1046 }
1047
1048 if (invalidateStencilBuffer)
1049 {
1050 mFramebuffer.invalidateRenderPassStencilAttachment(attachmentIndexVk);
1051 }
1052 }
1053
1054 // NOTE: Possible future optimization is to delay setting the storeOp and only do so if the
1055 // render pass is closed by itself before another draw call. Otherwise, in a situation like
1056 // this:
1057 //
1058 // draw()
1059 // invalidate()
1060 // draw()
1061 //
1062 // We would be discarding the attachments only to load them for the next draw (which is less
1063 // efficient than keeping the render pass open and not do the discard at all). While dEQP tests
1064 // this pattern, this optimization may not be necessary if no application does this. It is
1065 // expected that an application would invalidate() when it's done with the framebuffer, so the
1066 // render pass would have closed either way.
1067 mFramebuffer.finishCurrentCommands(contextVk);
1068}
1069
Jamie Madill6f755b22018-10-09 12:48:54 -04001070angle::Result FramebufferVk::syncState(const gl::Context *context,
1071 const gl::Framebuffer::DirtyBits &dirtyBits)
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001072{
Jamie Madille1f3ad42017-10-28 23:00:42 -04001073 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001074
1075 ASSERT(dirtyBits.any());
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001076 for (size_t dirtyBit : dirtyBits)
1077 {
1078 switch (dirtyBit)
1079 {
1080 case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
1081 case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
1082 ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState));
1083 break;
Jamie Madill67220092019-05-20 11:12:53 -04001084 case gl::Framebuffer::DIRTY_BIT_DEPTH_BUFFER_CONTENTS:
1085 case gl::Framebuffer::DIRTY_BIT_STENCIL_BUFFER_CONTENTS:
1086 ANGLE_TRY(mRenderTargetCache.getDepthStencil()->flushStagedUpdates(contextVk));
1087 break;
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001088 case gl::Framebuffer::DIRTY_BIT_READ_BUFFER:
Tim Van Patten56ba54c2019-08-08 13:03:34 -06001089 ANGLE_TRY(mRenderTargetCache.update(context, mState, dirtyBits));
1090 break;
1091 case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001092 case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
1093 case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
1094 case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
1095 case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1096 break;
1097 default:
1098 {
Jamie Madill67220092019-05-20 11:12:53 -04001099 static_assert(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits");
1100 if (dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX)
Jamie Madill9aef3672018-04-27 11:45:06 -04001101 {
Jamie Madill67220092019-05-20 11:12:53 -04001102 size_t colorIndexGL = static_cast<size_t>(
1103 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
1104 ANGLE_TRY(updateColorAttachment(context, colorIndexGL));
Jamie Madill9aef3672018-04-27 11:45:06 -04001105 }
1106 else
1107 {
Jamie Madill67220092019-05-20 11:12:53 -04001108 ASSERT(dirtyBit >= gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 &&
1109 dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_MAX);
1110 size_t colorIndexGL = static_cast<size_t>(
1111 dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0);
1112 ANGLE_TRY(mRenderTargetCache.getColors()[colorIndexGL]->flushStagedUpdates(
1113 contextVk));
Jamie Madill9aef3672018-04-27 11:45:06 -04001114 }
Jamie Madill57d9cbb2018-04-27 11:45:04 -04001115 }
1116 }
1117 }
Jamie Madill66546be2018-03-08 09:47:20 -05001118
Tim Van Patten626a7282019-07-08 15:11:59 -06001119 // The FBOs new attachment may have changed the renderable area
1120 const gl::State &glState = context->getState();
1121 contextVk->updateScissor(glState);
1122
Jamie Madill9aef3672018-04-27 11:45:06 -04001123 mActiveColorComponents = gl_vk::GetColorComponentFlags(
Luc Ferron5fd36932018-06-19 14:55:50 -04001124 mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(),
1125 mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any());
Jamie Madill9aef3672018-04-27 11:45:06 -04001126
Geoff Langee244c72019-05-06 10:30:18 -04001127 mFramebuffer.release(contextVk);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001128
Jamie Madill316c6062018-05-29 10:49:45 -04001129 // Will freeze the current set of dependencies on this FBO. The next time we render we will
Jamie Madilla5e06072018-05-18 14:36:05 -04001130 // create a new entry in the command graph.
Geoff Langee244c72019-05-06 10:30:18 -04001131 mFramebuffer.finishCurrentCommands(contextVk);
Jamie Madill72106562017-03-24 14:18:50 -04001132
Jamie Madilldbc605c2019-01-04 16:39:14 -05001133 // Notify the ContextVk to update the pipeline desc.
1134 updateRenderPassDesc();
Shahbaz Youssefida904482019-07-02 10:49:14 -04001135
1136 FramebufferVk *currentDrawFramebuffer = vk::GetImpl(context->getState().getDrawFramebuffer());
1137 if (currentDrawFramebuffer == this)
1138 {
1139 contextVk->onDrawFramebufferChange(this);
1140 }
Jamie Madill19fa1c62018-03-08 09:47:21 -05001141
Jamie Madill7c985f52018-11-29 18:16:17 -05001142 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001143}
1144
Jamie Madilldbc605c2019-01-04 16:39:14 -05001145void FramebufferVk::updateRenderPassDesc()
Jamie Madillab9f9c32017-01-17 17:47:34 -05001146{
Jamie Madilldbc605c2019-01-04 16:39:14 -05001147 mRenderPassDesc = {};
1148 mRenderPassDesc.setSamples(getSamples());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001149
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001150 const auto &colorRenderTargets = mRenderTargetCache.getColors();
1151 const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers();
1152 for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001153 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001154 if (enabledDrawBuffers[colorIndexGL])
1155 {
1156 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
1157 ASSERT(colorRenderTarget);
1158 mRenderPassDesc.packColorAttachment(
1159 colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID);
1160 }
1161 else
1162 {
1163 mRenderPassDesc.packColorAttachmentGap(colorIndexGL);
1164 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001165 }
1166
Jamie Madill66546be2018-03-08 09:47:20 -05001167 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1168 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001169 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001170 mRenderPassDesc.packDepthStencilAttachment(
1171 depthStencilRenderTarget->getImage().getFormat().angleFormatID);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001172 }
Jamie Madillab9f9c32017-01-17 17:47:34 -05001173}
1174
Jamie Madill21061022018-07-12 23:56:30 -04001175angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001176{
1177 // If we've already created our cached Framebuffer, return it.
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001178 if (mFramebuffer.valid())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001179 {
Jamie Madille8dd0792018-09-27 15:04:27 -04001180 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001181 return angle::Result::Continue;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001182 }
1183
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001184 vk::RenderPass *compatibleRenderPass = nullptr;
Geoff Langee244c72019-05-06 10:30:18 -04001185 ANGLE_TRY(contextVk->getCompatibleRenderPass(mRenderPassDesc, &compatibleRenderPass));
Jamie Madillab9f9c32017-01-17 17:47:34 -05001186
1187 // If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it.
1188 if (mBackbuffer)
1189 {
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001190 return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut);
Jamie Madillab9f9c32017-01-17 17:47:34 -05001191 }
1192
1193 // Gather VkImageViews over all FBO attachments, also size of attached region.
1194 std::vector<VkImageView> attachments;
1195 gl::Extents attachmentsSize;
1196
Jamie Madill66546be2018-03-08 09:47:20 -05001197 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001198 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madillab9f9c32017-01-17 17:47:34 -05001199 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001200 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001201 ASSERT(colorRenderTarget);
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001202 attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001203
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001204 ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents());
1205 attachmentsSize = colorRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001206 }
1207
Jamie Madill66546be2018-03-08 09:47:20 -05001208 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1209 if (depthStencilRenderTarget)
Jamie Madillab9f9c32017-01-17 17:47:34 -05001210 {
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001211 attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle());
Jamie Madillab9f9c32017-01-17 17:47:34 -05001212
Jamie Madillbc543422018-03-30 10:43:19 -04001213 ASSERT(attachmentsSize.empty() ||
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001214 attachmentsSize == depthStencilRenderTarget->getExtents());
1215 attachmentsSize = depthStencilRenderTarget->getExtents();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001216 }
1217
Tim Van Patten626a7282019-07-08 15:11:59 -06001218 if (attachmentsSize.empty())
1219 {
1220 // No attachments, so use the default values.
1221 attachmentsSize.height = mState.getDefaultHeight();
1222 attachmentsSize.width = mState.getDefaultWidth();
1223 attachmentsSize.depth = 0;
1224 }
1225
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001226 VkFramebufferCreateInfo framebufferInfo = {};
Jamie Madillab9f9c32017-01-17 17:47:34 -05001227
1228 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
Jamie Madillab9f9c32017-01-17 17:47:34 -05001229 framebufferInfo.flags = 0;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001230 framebufferInfo.renderPass = compatibleRenderPass->getHandle();
Jamie Madillab9f9c32017-01-17 17:47:34 -05001231 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
1232 framebufferInfo.pAttachments = attachments.data();
1233 framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width);
1234 framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height);
1235 framebufferInfo.layers = 1;
1236
Jamie Madill21061022018-07-12 23:56:30 -04001237 ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo));
Jamie Madill5deea722017-02-16 10:44:46 -05001238
Jamie Madille8dd0792018-09-27 15:04:27 -04001239 *framebufferOut = &mFramebuffer.getFramebuffer();
Jamie Madill7c985f52018-11-29 18:16:17 -05001240 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001241}
1242
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001243angle::Result FramebufferVk::clearWithRenderPassOp(
1244 ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001245 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001246 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001247 bool clearDepth,
1248 bool clearStencil,
1249 const VkClearColorValue &clearColorValue,
1250 const VkClearDepthStencilValue &clearDepthStencilValue)
1251{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001252 // Start a new render pass if:
1253 //
1254 // - no render pass has started,
1255 // - there is a render pass started but it contains commands; we cannot modify its ops, so new
1256 // render pass is needed,
1257 // - the current render area doesn't match the clear area. We need the render area to be
1258 // exactly as specified by the scissor for the loadOp to clear only that area. See
1259 // onScissorChange for more information.
1260
1261 if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() ||
1262 mFramebuffer.getRenderPassRenderArea() != clearArea)
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001263 {
1264 vk::CommandBuffer *commandBuffer;
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001265 ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer));
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001266 }
1267
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001268 size_t attachmentIndexVk = 0;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001269
1270 // Go through clearColorBuffers and set the appropriate loadOp and clear values.
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001271 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001272 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001273 if (clearColorBuffers.test(colorIndexGL))
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001274 {
Tim Van Patten56ba54c2019-08-08 13:03:34 -06001275 RenderTargetVk *renderTarget = getColorDrawRenderTarget(colorIndexGL);
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001276
1277 // If the render target doesn't have alpha, but its emulated format has it, clear the
1278 // alpha to 1.
1279 VkClearColorValue value = clearColorValue;
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001280 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001281 {
1282 SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value);
1283 }
1284
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001285 mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value);
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001286 }
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001287 ++attachmentIndexVk;
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001288 }
1289
1290 // Set the appropriate loadOp and clear values for depth and stencil.
1291 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1292 if (depthStencilRenderTarget)
1293 {
1294 if (clearDepth)
1295 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001296 mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001297 clearDepthStencilValue.depth);
1298 }
1299
1300 if (clearStencil)
1301 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001302 mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001303 clearDepthStencilValue.stencil);
1304 }
1305 }
1306
1307 return angle::Result::Continue;
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001308}
1309
Jamie Madill21061022018-07-12 23:56:30 -04001310angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001311 const gl::Rectangle &clearArea,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001312 gl::DrawBufferMask clearColorBuffers,
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001313 bool clearStencil,
1314 VkColorComponentFlags colorMaskFlags,
1315 uint8_t stencilMask,
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001316 const VkClearColorValue &clearColorValue,
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001317 uint8_t clearStencilValue)
Jamie Madill9aef3672018-04-27 11:45:06 -04001318{
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001319 UtilsVk::ClearFramebufferParameters params = {};
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001320 params.clearArea = clearArea;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001321 params.colorClearValue = clearColorValue;
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001322 params.stencilClearValue = clearStencilValue;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001323 params.stencilMask = stencilMask;
1324
1325 params.clearColor = true;
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001326 params.clearStencil = clearStencil;
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001327
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001328 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001329 for (size_t colorIndexGL : clearColorBuffers)
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001330 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001331 const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001332 ASSERT(colorRenderTarget);
1333
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001334 params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat();
James Darpinian7e48c9e2019-08-06 17:17:19 -07001335 params.colorAttachmentIndexGL = static_cast<uint32_t>(colorIndexGL);
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001336 params.colorMaskFlags = colorMaskFlags;
1337 if (mEmulatedAlphaAttachmentMask[colorIndexGL])
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001338 {
1339 params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT;
1340 }
1341
Geoff Langee244c72019-05-06 10:30:18 -04001342 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001343
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001344 // Clear stencil only once!
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001345 params.clearStencil = false;
1346 }
1347
Shahbaz Youssefi2249d4a2019-04-05 16:48:55 -04001348 // If there was no color clear, clear stencil alone.
1349 if (params.clearStencil)
Shahbaz Youssefif6c937f2019-04-02 17:04:08 -04001350 {
1351 params.clearColor = false;
Geoff Langee244c72019-05-06 10:30:18 -04001352 ANGLE_TRY(contextVk->getUtils().clearFramebuffer(contextVk, this, params));
Shahbaz Youssefi43997012019-03-30 23:24:01 -04001353 }
1354
1355 return angle::Result::Continue;
Jamie Madill9aef3672018-04-27 11:45:06 -04001356}
1357
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001358angle::Result FramebufferVk::getSamplePosition(const gl::Context *context,
1359 size_t index,
1360 GLfloat *xy) const
JiangYizhoubddc46b2016-12-09 09:50:51 +08001361{
Jamie Madill64b7c4f2018-10-19 11:38:04 -04001362 ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
Jamie Madill7c985f52018-11-29 18:16:17 -05001363 return angle::Result::Stop;
JiangYizhoubddc46b2016-12-09 09:50:51 +08001364}
1365
Jamie Madilld1249de2018-08-28 16:58:53 -04001366angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk,
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001367 const gl::Rectangle &renderArea,
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001368 vk::CommandBuffer **commandBufferOut)
Jamie Madilld1249de2018-08-28 16:58:53 -04001369{
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001370 vk::Framebuffer *framebuffer = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001371 ANGLE_TRY(getFramebuffer(contextVk, &framebuffer));
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001372
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001373 vk::AttachmentOpsArray renderPassAttachmentOps;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001374 std::vector<VkClearValue> attachmentClearValues;
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001375
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001376 vk::CommandBuffer *writeCommands = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001377 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands));
Jamie Madille4c5a232018-03-02 21:00:31 -05001378
Jamie Madill49ac74b2017-12-21 14:42:33 -05001379 // Initialize RenderPass info.
Jamie Madill66546be2018-03-08 09:47:20 -05001380 const auto &colorRenderTargets = mRenderTargetCache.getColors();
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001381 for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
Jamie Madill4c26fc22017-02-24 11:04:10 -05001382 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001383 RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
Jamie Madill66546be2018-03-08 09:47:20 -05001384 ASSERT(colorRenderTarget);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001385
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001386 ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001387
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001388 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1389 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1390 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
1391 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill66546be2018-03-08 09:47:20 -05001392 }
1393
1394 RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
1395 if (depthStencilRenderTarget)
1396 {
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001397 ANGLE_TRY(
1398 depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands));
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001399
Shahbaz Youssefidb4ed312019-03-29 00:32:45 -04001400 renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
1401 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1402 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
1403 attachmentClearValues.emplace_back(kUninitializedClearValue);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001404 }
1405
Jamie Madilldbc605c2019-01-04 16:39:14 -05001406 return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc,
Shahbaz Youssefi0c128e12019-03-25 23:50:14 -04001407 renderPassAttachmentOps, attachmentClearValues,
1408 commandBufferOut);
Jamie Madilldf68a6f2017-01-13 17:29:53 -05001409}
1410
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001411void FramebufferVk::updateActiveColorMasks(size_t colorIndexGL, bool r, bool g, bool b, bool a)
Jamie Madill9aef3672018-04-27 11:45:06 -04001412{
Shahbaz Youssefi9fa248e2019-05-06 14:55:18 -04001413 mActiveColorComponentMasksForClear[0].set(colorIndexGL, r);
1414 mActiveColorComponentMasksForClear[1].set(colorIndexGL, g);
1415 mActiveColorComponentMasksForClear[2].set(colorIndexGL, b);
1416 mActiveColorComponentMasksForClear[3].set(colorIndexGL, a);
Luc Ferron5fd36932018-06-19 14:55:50 -04001417}
1418
Shahbaz Youssefie3219402018-12-08 16:54:14 +01001419const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const
Luc Ferron5fd36932018-06-19 14:55:50 -04001420{
1421 return mEmulatedAlphaAttachmentMask;
Jamie Madill9aef3672018-04-27 11:45:06 -04001422}
Luc Ferron018709f2018-05-10 13:53:11 -04001423
Jamie Madill21061022018-07-12 23:56:30 -04001424angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk,
1425 const gl::Rectangle &area,
1426 const PackPixelsParams &packPixelsParams,
Jamie Madillb436aac2018-07-18 17:23:48 -04001427 VkImageAspectFlagBits copyAspectFlags,
Jamie Madill21061022018-07-12 23:56:30 -04001428 RenderTargetVk *renderTarget,
1429 void *pixels)
Luc Ferron018709f2018-05-10 13:53:11 -04001430{
Jamie Madill3ea463b2019-06-19 14:21:33 -04001431 ANGLE_TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl");
Jamie Madill58675012018-05-22 14:54:07 -04001432
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001433 RendererVk *renderer = contextVk->getRenderer();
1434
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001435 vk::CommandBuffer *commandBuffer = nullptr;
Jamie Madille8dd0792018-09-27 15:04:27 -04001436 ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
Jamie Madill58675012018-05-22 14:54:07 -04001437
Jamie Madillbcf467f2018-05-23 09:46:00 -04001438 // Note that although we're reading from the image, we need to update the layout below.
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -05001439 vk::ImageHelper *srcImage =
1440 renderTarget->getImageForRead(&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -04001441
Jamie Madill0631e192019-04-18 16:09:12 -04001442 const angle::Format *readFormat = &srcImage->getFormat().imageFormat();
Luc Ferron1617e692018-07-11 11:08:19 -04001443
Jamie Madillb436aac2018-07-18 17:23:48 -04001444 if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT)
Luc Ferron1617e692018-07-11 11:08:19 -04001445 {
Jamie Madillb436aac2018-07-18 17:23:48 -04001446 readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags);
Luc Ferron1617e692018-07-11 11:08:19 -04001447 }
1448
James Darpinian7e48c9e2019-08-06 17:17:19 -07001449 uint32_t level = renderTarget->getLevelIndex();
1450 uint32_t layer = renderTarget->getLayerIndex();
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001451 VkOffset3D srcOffset = {area.x, area.y, 0};
Cody Northropeb0479e2019-07-24 14:15:56 -06001452
1453 VkImageSubresourceLayers srcSubresource = {};
1454 srcSubresource.aspectMask = copyAspectFlags;
1455 srcSubresource.mipLevel = level;
1456 srcSubresource.baseArrayLayer = layer;
1457 srcSubresource.layerCount = 1;
1458
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001459 VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height),
1460 1};
1461
Cody Northropeb0479e2019-07-24 14:15:56 -06001462 if (srcImage->getExtents().depth > 1)
1463 {
1464 // Depth > 1 means this is a 3D texture and we need special handling
1465 srcOffset.z = layer;
1466 srcSubresource.baseArrayLayer = 0;
1467 }
1468
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001469 // If the source image is multisampled, we need to resolve it into a temporary image before
1470 // performing a readback.
1471 bool isMultisampled = srcImage->getSamples() > 1;
Shahbaz Youssefi57ad1e12019-08-23 14:53:26 -04001472 vk::DeviceScoped<vk::ImageHelper> resolvedImage(contextVk->getDevice());
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001473 if (isMultisampled)
1474 {
1475 ANGLE_TRY(resolvedImage.get().init2DStaging(
1476 contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1),
1477 srcImage->getFormat(),
1478 VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1));
Geoff Langee244c72019-05-06 10:30:18 -04001479 resolvedImage.get().updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001480
Shahbaz Youssefif2a1c382019-05-21 16:32:49 -04001481 // Note: resolve only works on color images (not depth/stencil).
1482 //
1483 // TODO: Currently, depth/stencil blit can perform a depth/stencil readback, but that code
1484 // path will be optimized away. http://anglebug.com/3200
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001485 ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT);
1486
1487 VkImageResolve resolveRegion = {};
Cody Northropeb0479e2019-07-24 14:15:56 -06001488 resolveRegion.srcSubresource = srcSubresource;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001489 resolveRegion.srcOffset = srcOffset;
1490 resolveRegion.dstSubresource.aspectMask = copyAspectFlags;
1491 resolveRegion.dstSubresource.mipLevel = 0;
1492 resolveRegion.dstSubresource.baseArrayLayer = 0;
1493 resolveRegion.dstSubresource.layerCount = 1;
1494 resolveRegion.dstOffset = {};
1495 resolveRegion.extent = srcExtent;
1496
1497 srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer);
1498
1499 resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc,
1500 commandBuffer);
1501
1502 // Make the resolved image the target of buffer copy.
Mingyu Hu7e44ec22019-08-26 15:59:48 -07001503 srcImage = &resolvedImage.get();
1504 level = 0;
1505 layer = 0;
1506 srcOffset = {0, 0, 0};
Cody Northropeb0479e2019-07-24 14:15:56 -06001507 srcSubresource.baseArrayLayer = 0;
1508 srcSubresource.layerCount = 1;
1509 srcSubresource.mipLevel = 0;
Shahbaz Youssefib16d69c2019-05-13 16:28:27 -04001510 }
1511
Jamie Madillb980c562018-11-27 11:34:27 -05001512 VkBuffer bufferHandle = VK_NULL_HANDLE;
1513 uint8_t *readPixelBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -04001514 VkDeviceSize stagingOffset = 0;
Jamie Madillb980c562018-11-27 11:34:27 -05001515 size_t allocationSize = readFormat->pixelBytes * area.width * area.height;
Luc Ferron018709f2018-05-10 13:53:11 -04001516
Jamie Madilld754eb52018-07-19 14:55:03 -04001517 ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -05001518 &stagingOffset, nullptr));
Luc Ferron018709f2018-05-10 13:53:11 -04001519
Cody Northropeb0479e2019-07-24 14:15:56 -06001520 VkBufferImageCopy region = {};
1521 region.bufferImageHeight = srcExtent.height;
1522 region.bufferOffset = stagingOffset;
1523 region.bufferRowLength = srcExtent.width;
1524 region.imageExtent = srcExtent;
1525 region.imageOffset = srcOffset;
1526 region.imageSubresource = srcSubresource;
Luc Ferron534b00d2018-05-18 08:16:53 -04001527
Jamie Madillbcf467f2018-05-23 09:46:00 -04001528 commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(),
1529 bufferHandle, 1, &region);
Luc Ferron018709f2018-05-10 13:53:11 -04001530
1531 // Triggers a full finish.
1532 // TODO(jmadill): Don't block on asynchronous readback.
Geoff Lang892d1802019-03-27 14:21:34 -04001533 ANGLE_TRY(contextVk->finishImpl());
Luc Ferron018709f2018-05-10 13:53:11 -04001534
Luc Ferron534b00d2018-05-18 08:16:53 -04001535 // The buffer we copied to needs to be invalidated before we read from it because its not been
1536 // created with the host coherent bit.
Jamie Madilld754eb52018-07-19 14:55:03 -04001537 ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk));
Yuly Novikov6c6c76c2018-05-17 18:45:06 +00001538
Ian Elliottdc2c5c52019-08-02 09:32:18 -06001539 const gl::State &glState = contextVk->getState();
1540 gl::Buffer *packBuffer = glState.getTargetBuffer(gl::BufferBinding::PixelPack);
1541 if (packBuffer != nullptr)
1542 {
1543 // Must map the PBO in order to read its contents (and then unmap it later)
1544 BufferVk *packBufferVk = vk::GetImpl(packBuffer);
1545 void *mapPtr = nullptr;
1546 ANGLE_TRY(packBufferVk->mapImpl(contextVk, &mapPtr));
1547 uint8_t *dest = static_cast<uint8_t *>(mapPtr) + reinterpret_cast<ptrdiff_t>(pixels);
1548 PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes,
1549 readPixelBuffer, static_cast<uint8_t *>(dest));
1550 packBufferVk->unmapImpl(contextVk);
1551 }
1552 else
1553 {
1554 PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes,
1555 readPixelBuffer, static_cast<uint8_t *>(pixels));
1556 }
Luc Ferron018709f2018-05-10 13:53:11 -04001557
Jamie Madill7c985f52018-11-29 18:16:17 -05001558 return angle::Result::Continue;
Luc Ferron018709f2018-05-10 13:53:11 -04001559}
Jamie Madill58675012018-05-22 14:54:07 -04001560
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001561gl::Extents FramebufferVk::getReadImageExtents() const
Jamie Madill58675012018-05-22 14:54:07 -04001562{
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001563 ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width);
1564 ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height);
1565
Shahbaz Youssefi68549402019-03-25 23:30:49 -04001566 return getColorReadRenderTarget()->getExtents();
Jamie Madill58675012018-05-22 14:54:07 -04001567}
Jamie Madill502d2e22018-11-01 11:06:23 -04001568
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001569gl::Rectangle FramebufferVk::getCompleteRenderArea() const
1570{
Tim Van Patten626a7282019-07-08 15:11:59 -06001571 const gl::Box &dimensions = mState.getDimensions();
1572 return gl::Rectangle(0, 0, dimensions.width, dimensions.height);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001573}
1574
1575gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const
1576{
Tim Van Patten626a7282019-07-08 15:11:59 -06001577 const gl::Box &dimensions = mState.getDimensions();
1578 const gl::Rectangle renderArea(0, 0, dimensions.width, dimensions.height);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001579 bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO();
1580
1581 return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport);
1582}
1583
1584void FramebufferVk::onScissorChange(ContextVk *contextVk)
1585{
1586 gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
1587
1588 // If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass
1589 // is restarted. Otherwise, we can continue using the same renderpass area.
1590 //
1591 // Without a scissor, the render pass area covers the whole of the framebuffer. With a
1592 // scissored clear, the render pass area could be smaller than the framebuffer size. When the
1593 // scissor changes, if the scissor area is completely encompassed by the render pass area, it's
1594 // possible to continue using the same render pass. However, if the current render pass area
1595 // is too small, we need to start a new one. The latter can happen if a scissored clear starts
1596 // a render pass, the scissor is disabled and a draw call is issued to affect the whole
1597 // framebuffer.
Geoff Langee244c72019-05-06 10:30:18 -04001598 mFramebuffer.updateQueueSerial(contextVk->getCurrentQueueSerial());
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001599 if (mFramebuffer.hasStartedRenderPass() &&
1600 !mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea))
1601 {
Geoff Langee244c72019-05-06 10:30:18 -04001602 mFramebuffer.finishCurrentCommands(contextVk);
Shahbaz Youssefi127990f2019-04-04 13:52:04 -04001603 }
1604}
1605
Jamie Madill502d2e22018-11-01 11:06:23 -04001606RenderTargetVk *FramebufferVk::getFirstRenderTarget() const
1607{
1608 for (auto *renderTarget : mRenderTargetCache.getColors())
1609 {
1610 if (renderTarget)
1611 {
1612 return renderTarget;
1613 }
1614 }
1615
1616 return mRenderTargetCache.getDepthStencil();
1617}
1618
1619GLint FramebufferVk::getSamples() const
1620{
1621 RenderTargetVk *firstRT = getFirstRenderTarget();
1622 return firstRT ? firstRT->getImage().getSamples() : 0;
1623}
1624
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001625} // namespace rx