blob: c6158a249107e28f6d86985e435519ada60c4dc0 [file] [log] [blame]
Jamie Madill7b57b9d2017-01-13 09:33:38 -05001//
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// RenderTargetVk:
7// Wrapper around a Vulkan renderable resource, using an ImageView.
8//
9
10#include "libANGLE/renderer/vulkan/RenderTargetVk.h"
11
Jamie Madillbcf467f2018-05-23 09:46:00 -040012#include "libANGLE/renderer/vulkan/CommandGraph.h"
Shahbaz Youssefi29b49412019-01-07 14:03:06 -050013#include "libANGLE/renderer/vulkan/TextureVk.h"
Jamie Madillbcf467f2018-05-23 09:46:00 -040014#include "libANGLE/renderer/vulkan/vk_format_utils.h"
15#include "libANGLE/renderer/vulkan/vk_helpers.h"
16
Jamie Madill7b57b9d2017-01-13 09:33:38 -050017namespace rx
18{
Shahbaz Youssefi29b49412019-01-07 14:03:06 -050019RenderTargetVk::RenderTargetVk(vk::ImageHelper *image,
20 vk::ImageView *imageView,
21 size_t layerIndex,
22 TextureVk *owner)
23 : mImage(image), mImageView(imageView), mLayerIndex(layerIndex), mOwner(owner)
Jamie Madillb980c562018-11-27 11:34:27 -050024{}
Jamie Madillbcf467f2018-05-23 09:46:00 -040025
Jamie Madillb980c562018-11-27 11:34:27 -050026RenderTargetVk::~RenderTargetVk() {}
Jamie Madillbcf467f2018-05-23 09:46:00 -040027
Jamie Madill71bb0262018-09-12 11:09:42 -040028RenderTargetVk::RenderTargetVk(RenderTargetVk &&other)
Shahbaz Youssefi29b49412019-01-07 14:03:06 -050029 : mImage(other.mImage),
30 mImageView(other.mImageView),
31 mLayerIndex(other.mLayerIndex),
32 mOwner(other.mOwner)
Jamie Madillb980c562018-11-27 11:34:27 -050033{}
Jamie Madill71bb0262018-09-12 11:09:42 -040034
Jamie Madill193a2842018-10-30 17:28:41 -040035void RenderTargetVk::onColorDraw(vk::FramebufferHelper *framebufferVk,
Jamie Madill316c6062018-05-29 10:49:45 -040036 vk::CommandBuffer *commandBuffer,
Jamie Madillbcf467f2018-05-23 09:46:00 -040037 vk::RenderPassDesc *renderPassDesc)
38{
Jamie Madill316c6062018-05-29 10:49:45 -040039 ASSERT(commandBuffer->valid());
40 ASSERT(!mImage->getFormat().textureFormat().hasDepthOrStencilBits());
Jamie Madillbcf467f2018-05-23 09:46:00 -040041
42 // Store the attachment info in the renderPassDesc.
Jamie Madill502d2e22018-11-01 11:06:23 -040043 renderPassDesc->packAttachment(mImage->getFormat());
Jamie Madillbcf467f2018-05-23 09:46:00 -040044
45 // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361
Jamie Madill316c6062018-05-29 10:49:45 -040046 mImage->changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT,
47 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
48 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
49 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -040050
Jamie Madill316c6062018-05-29 10:49:45 -040051 // Set up dependencies between the RT resource and the Framebuffer.
Jamie Madill2d03ff42018-09-27 15:04:26 -040052 mImage->addWriteDependency(framebufferVk);
Jamie Madillbcf467f2018-05-23 09:46:00 -040053}
54
Jamie Madill193a2842018-10-30 17:28:41 -040055void RenderTargetVk::onDepthStencilDraw(vk::FramebufferHelper *framebufferVk,
Jamie Madill316c6062018-05-29 10:49:45 -040056 vk::CommandBuffer *commandBuffer,
Jamie Madillbcf467f2018-05-23 09:46:00 -040057 vk::RenderPassDesc *renderPassDesc)
58{
Jamie Madill316c6062018-05-29 10:49:45 -040059 ASSERT(commandBuffer->valid());
Jamie Madillbcf467f2018-05-23 09:46:00 -040060 ASSERT(mImage->getFormat().textureFormat().hasDepthOrStencilBits());
61
62 // Store the attachment info in the renderPassDesc.
Jamie Madill502d2e22018-11-01 11:06:23 -040063 renderPassDesc->packAttachment(mImage->getFormat());
Jamie Madillbcf467f2018-05-23 09:46:00 -040064
65 // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361
66 const angle::Format &format = mImage->getFormat().textureFormat();
Luc Ferron5bdf8bd2018-06-20 09:51:37 -040067 VkImageAspectFlags aspectFlags = vk::GetDepthStencilAspectFlags(format);
Jamie Madillbcf467f2018-05-23 09:46:00 -040068
69 mImage->changeLayoutWithStages(aspectFlags, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
70 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
Jamie Madill316c6062018-05-29 10:49:45 -040071 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -040072
Jamie Madill316c6062018-05-29 10:49:45 -040073 // Set up dependencies between the RT resource and the Framebuffer.
Jamie Madill2d03ff42018-09-27 15:04:26 -040074 mImage->addWriteDependency(framebufferVk);
Jamie Madillbcf467f2018-05-23 09:46:00 -040075}
76
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +010077vk::ImageHelper &RenderTargetVk::getImage()
78{
79 ASSERT(mImage && mImage->valid());
80 return *mImage;
81}
82
Jamie Madillbcf467f2018-05-23 09:46:00 -040083const vk::ImageHelper &RenderTargetVk::getImage() const
84{
85 ASSERT(mImage && mImage->valid());
86 return *mImage;
87}
88
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +010089vk::ImageView *RenderTargetVk::getDrawImageView() const
Jamie Madillbcf467f2018-05-23 09:46:00 -040090{
91 ASSERT(mImageView && mImageView->valid());
92 return mImageView;
93}
94
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +010095vk::ImageView *RenderTargetVk::getReadImageView() const
96{
97 return getDrawImageView();
98}
99
Jamie Madillbcf467f2018-05-23 09:46:00 -0400100const vk::Format &RenderTargetVk::getImageFormat() const
101{
102 ASSERT(mImage && mImage->valid());
103 return mImage->getFormat();
104}
105
106const gl::Extents &RenderTargetVk::getImageExtents() const
107{
108 ASSERT(mImage && mImage->valid());
109 return mImage->getExtents();
110}
111
112void RenderTargetVk::updateSwapchainImage(vk::ImageHelper *image, vk::ImageView *imageView)
113{
114 ASSERT(image && image->valid() && imageView && imageView->valid());
115 mImage = image;
116 mImageView = imageView;
Shahbaz Youssefi29b49412019-01-07 14:03:06 -0500117 mOwner = nullptr;
Jamie Madillbcf467f2018-05-23 09:46:00 -0400118}
119
Jamie Madill193a2842018-10-30 17:28:41 -0400120vk::ImageHelper *RenderTargetVk::getImageForRead(vk::RecordableGraphResource *readingResource,
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400121 VkImageLayout layout,
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400122 vk::CommandBuffer *commandBuffer)
123{
124 ASSERT(mImage && mImage->valid());
125
126 // TODO(jmadill): Better simultaneous resource access. http://anglebug.com/2679
Jamie Madill2d03ff42018-09-27 15:04:26 -0400127 mImage->addWriteDependency(readingResource);
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400128
Luc Ferron1617e692018-07-11 11:08:19 -0400129 mImage->changeLayoutWithStages(mImage->getAspectFlags(), layout,
130 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400131 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
132
133 return mImage;
134}
135
Jamie Madill193a2842018-10-30 17:28:41 -0400136vk::ImageHelper *RenderTargetVk::getImageForWrite(
137 vk::RecordableGraphResource *writingResource) const
Jamie Madillbcf467f2018-05-23 09:46:00 -0400138{
139 ASSERT(mImage && mImage->valid());
Jamie Madill2d03ff42018-09-27 15:04:26 -0400140 mImage->addWriteDependency(writingResource);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400141 return mImage;
142}
143
Shahbaz Youssefi29b49412019-01-07 14:03:06 -0500144angle::Result RenderTargetVk::ensureImageInitialized(ContextVk *contextVk)
145{
146 if (mOwner)
147 {
148 return mOwner->ensureImageInitialized(contextVk);
149 }
150 return angle::Result::Continue;
151}
152
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500153} // namespace rx