blob: 508c82754fff8304b972818ffe23a41b56f840cc [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{
Geoff Langf3e823d2019-01-14 12:40:34 -050019RenderTargetVk::RenderTargetVk()
20 : mImage(nullptr), mImageView(nullptr), mLayerIndex(0), mOwner(nullptr)
Jamie Madillb980c562018-11-27 11:34:27 -050021{}
Jamie Madillbcf467f2018-05-23 09:46:00 -040022
Jamie Madillb980c562018-11-27 11:34:27 -050023RenderTargetVk::~RenderTargetVk() {}
Jamie Madillbcf467f2018-05-23 09:46:00 -040024
Jamie Madill71bb0262018-09-12 11:09:42 -040025RenderTargetVk::RenderTargetVk(RenderTargetVk &&other)
Shahbaz Youssefi29b49412019-01-07 14:03:06 -050026 : mImage(other.mImage),
27 mImageView(other.mImageView),
28 mLayerIndex(other.mLayerIndex),
29 mOwner(other.mOwner)
Jamie Madillb980c562018-11-27 11:34:27 -050030{}
Jamie Madill71bb0262018-09-12 11:09:42 -040031
Geoff Langf3e823d2019-01-14 12:40:34 -050032void RenderTargetVk::init(vk::ImageHelper *image,
33 vk::ImageView *imageView,
34 size_t layerIndex,
35 TextureVk *owner)
36{
37 mImage = image;
38 mImageView = imageView;
39 mLayerIndex = layerIndex;
40 mOwner = owner;
41}
42
43void RenderTargetVk::reset()
44{
45 mImage = nullptr;
46 mImageView = nullptr;
47 mLayerIndex = 0;
48 mOwner = nullptr;
49}
50
Jamie Madill193a2842018-10-30 17:28:41 -040051void RenderTargetVk::onColorDraw(vk::FramebufferHelper *framebufferVk,
Jamie Madill316c6062018-05-29 10:49:45 -040052 vk::CommandBuffer *commandBuffer,
Jamie Madillbcf467f2018-05-23 09:46:00 -040053 vk::RenderPassDesc *renderPassDesc)
54{
Jamie Madill316c6062018-05-29 10:49:45 -040055 ASSERT(commandBuffer->valid());
56 ASSERT(!mImage->getFormat().textureFormat().hasDepthOrStencilBits());
Jamie Madillbcf467f2018-05-23 09:46:00 -040057
58 // Store the attachment info in the renderPassDesc.
Jamie Madill502d2e22018-11-01 11:06:23 -040059 renderPassDesc->packAttachment(mImage->getFormat());
Jamie Madillbcf467f2018-05-23 09:46:00 -040060
61 // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -050062 mImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::ColorAttachment,
63 commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -040064
Jamie Madill316c6062018-05-29 10:49:45 -040065 // Set up dependencies between the RT resource and the Framebuffer.
Jamie Madill2d03ff42018-09-27 15:04:26 -040066 mImage->addWriteDependency(framebufferVk);
Jamie Madillbcf467f2018-05-23 09:46:00 -040067}
68
Jamie Madill193a2842018-10-30 17:28:41 -040069void RenderTargetVk::onDepthStencilDraw(vk::FramebufferHelper *framebufferVk,
Jamie Madill316c6062018-05-29 10:49:45 -040070 vk::CommandBuffer *commandBuffer,
Jamie Madillbcf467f2018-05-23 09:46:00 -040071 vk::RenderPassDesc *renderPassDesc)
72{
Jamie Madill316c6062018-05-29 10:49:45 -040073 ASSERT(commandBuffer->valid());
Jamie Madillbcf467f2018-05-23 09:46:00 -040074 ASSERT(mImage->getFormat().textureFormat().hasDepthOrStencilBits());
75
76 // Store the attachment info in the renderPassDesc.
Jamie Madill502d2e22018-11-01 11:06:23 -040077 renderPassDesc->packAttachment(mImage->getFormat());
Jamie Madillbcf467f2018-05-23 09:46:00 -040078
79 // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361
80 const angle::Format &format = mImage->getFormat().textureFormat();
Luc Ferron5bdf8bd2018-06-20 09:51:37 -040081 VkImageAspectFlags aspectFlags = vk::GetDepthStencilAspectFlags(format);
Jamie Madillbcf467f2018-05-23 09:46:00 -040082
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -050083 mImage->changeLayout(aspectFlags, vk::ImageLayout::DepthStencilAttachment, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -040084
Jamie Madill316c6062018-05-29 10:49:45 -040085 // Set up dependencies between the RT resource and the Framebuffer.
Jamie Madill2d03ff42018-09-27 15:04:26 -040086 mImage->addWriteDependency(framebufferVk);
Jamie Madillbcf467f2018-05-23 09:46:00 -040087}
88
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +010089vk::ImageHelper &RenderTargetVk::getImage()
90{
91 ASSERT(mImage && mImage->valid());
92 return *mImage;
93}
94
Jamie Madillbcf467f2018-05-23 09:46:00 -040095const vk::ImageHelper &RenderTargetVk::getImage() const
96{
97 ASSERT(mImage && mImage->valid());
98 return *mImage;
99}
100
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100101vk::ImageView *RenderTargetVk::getDrawImageView() const
Jamie Madillbcf467f2018-05-23 09:46:00 -0400102{
103 ASSERT(mImageView && mImageView->valid());
104 return mImageView;
105}
106
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100107vk::ImageView *RenderTargetVk::getReadImageView() const
108{
109 return getDrawImageView();
110}
111
Jamie Madillbcf467f2018-05-23 09:46:00 -0400112const vk::Format &RenderTargetVk::getImageFormat() const
113{
114 ASSERT(mImage && mImage->valid());
115 return mImage->getFormat();
116}
117
118const gl::Extents &RenderTargetVk::getImageExtents() const
119{
120 ASSERT(mImage && mImage->valid());
121 return mImage->getExtents();
122}
123
124void RenderTargetVk::updateSwapchainImage(vk::ImageHelper *image, vk::ImageView *imageView)
125{
126 ASSERT(image && image->valid() && imageView && imageView->valid());
127 mImage = image;
128 mImageView = imageView;
Shahbaz Youssefi29b49412019-01-07 14:03:06 -0500129 mOwner = nullptr;
Jamie Madillbcf467f2018-05-23 09:46:00 -0400130}
131
Shahbaz Youssefic81e7bf2019-01-18 15:35:55 -0500132vk::ImageHelper *RenderTargetVk::getImageForRead(vk::CommandGraphResource *readingResource,
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500133 vk::ImageLayout layout,
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400134 vk::CommandBuffer *commandBuffer)
135{
136 ASSERT(mImage && mImage->valid());
137
138 // TODO(jmadill): Better simultaneous resource access. http://anglebug.com/2679
Jamie Madill2d03ff42018-09-27 15:04:26 -0400139 mImage->addWriteDependency(readingResource);
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400140
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500141 mImage->changeLayout(mImage->getAspectFlags(), layout, commandBuffer);
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400142
143 return mImage;
144}
145
Shahbaz Youssefic81e7bf2019-01-18 15:35:55 -0500146vk::ImageHelper *RenderTargetVk::getImageForWrite(vk::CommandGraphResource *writingResource) const
Jamie Madillbcf467f2018-05-23 09:46:00 -0400147{
148 ASSERT(mImage && mImage->valid());
Jamie Madill2d03ff42018-09-27 15:04:26 -0400149 mImage->addWriteDependency(writingResource);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400150 return mImage;
151}
152
Shahbaz Youssefi29b49412019-01-07 14:03:06 -0500153angle::Result RenderTargetVk::ensureImageInitialized(ContextVk *contextVk)
154{
155 if (mOwner)
156 {
157 return mOwner->ensureImageInitialized(contextVk);
158 }
159 return angle::Result::Continue;
160}
161
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500162} // namespace rx