blob: 8035c8ee23c639677ba56aa7eceaed88fe1e72f8 [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()
Geoff Lang4a298702019-01-18 10:49:36 -050020 : mImage(nullptr), mImageView(nullptr), mLevelIndex(0), 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),
Geoff Lang4a298702019-01-18 10:49:36 -050028 mLevelIndex(other.mLevelIndex),
Shahbaz Youssefi29b49412019-01-07 14:03:06 -050029 mLayerIndex(other.mLayerIndex),
30 mOwner(other.mOwner)
Jamie Madillb980c562018-11-27 11:34:27 -050031{}
Jamie Madill71bb0262018-09-12 11:09:42 -040032
Geoff Langf3e823d2019-01-14 12:40:34 -050033void RenderTargetVk::init(vk::ImageHelper *image,
34 vk::ImageView *imageView,
Geoff Lang4a298702019-01-18 10:49:36 -050035 size_t levelIndex,
Geoff Langf3e823d2019-01-14 12:40:34 -050036 size_t layerIndex,
37 TextureVk *owner)
38{
39 mImage = image;
40 mImageView = imageView;
Geoff Lang4a298702019-01-18 10:49:36 -050041 mLevelIndex = levelIndex;
Geoff Langf3e823d2019-01-14 12:40:34 -050042 mLayerIndex = layerIndex;
43 mOwner = owner;
44}
45
46void RenderTargetVk::reset()
47{
48 mImage = nullptr;
49 mImageView = nullptr;
Geoff Lang4a298702019-01-18 10:49:36 -050050 mLevelIndex = 0;
Geoff Langf3e823d2019-01-14 12:40:34 -050051 mLayerIndex = 0;
52 mOwner = nullptr;
53}
54
Jamie Madill193a2842018-10-30 17:28:41 -040055void RenderTargetVk::onColorDraw(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());
60 ASSERT(!mImage->getFormat().textureFormat().hasDepthOrStencilBits());
Jamie Madillbcf467f2018-05-23 09:46:00 -040061
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
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -050066 mImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::ColorAttachment,
67 commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -040068
Jamie Madill316c6062018-05-29 10:49:45 -040069 // Set up dependencies between the RT resource and the Framebuffer.
Jamie Madill2d03ff42018-09-27 15:04:26 -040070 mImage->addWriteDependency(framebufferVk);
Jamie Madillbcf467f2018-05-23 09:46:00 -040071}
72
Jamie Madill193a2842018-10-30 17:28:41 -040073void RenderTargetVk::onDepthStencilDraw(vk::FramebufferHelper *framebufferVk,
Jamie Madill316c6062018-05-29 10:49:45 -040074 vk::CommandBuffer *commandBuffer,
Jamie Madillbcf467f2018-05-23 09:46:00 -040075 vk::RenderPassDesc *renderPassDesc)
76{
Jamie Madill316c6062018-05-29 10:49:45 -040077 ASSERT(commandBuffer->valid());
Jamie Madillbcf467f2018-05-23 09:46:00 -040078 ASSERT(mImage->getFormat().textureFormat().hasDepthOrStencilBits());
79
80 // Store the attachment info in the renderPassDesc.
Jamie Madill502d2e22018-11-01 11:06:23 -040081 renderPassDesc->packAttachment(mImage->getFormat());
Jamie Madillbcf467f2018-05-23 09:46:00 -040082
83 // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361
84 const angle::Format &format = mImage->getFormat().textureFormat();
Luc Ferron5bdf8bd2018-06-20 09:51:37 -040085 VkImageAspectFlags aspectFlags = vk::GetDepthStencilAspectFlags(format);
Jamie Madillbcf467f2018-05-23 09:46:00 -040086
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -050087 mImage->changeLayout(aspectFlags, vk::ImageLayout::DepthStencilAttachment, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -040088
Jamie Madill316c6062018-05-29 10:49:45 -040089 // Set up dependencies between the RT resource and the Framebuffer.
Jamie Madill2d03ff42018-09-27 15:04:26 -040090 mImage->addWriteDependency(framebufferVk);
Jamie Madillbcf467f2018-05-23 09:46:00 -040091}
92
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +010093vk::ImageHelper &RenderTargetVk::getImage()
94{
95 ASSERT(mImage && mImage->valid());
96 return *mImage;
97}
98
Jamie Madillbcf467f2018-05-23 09:46:00 -040099const vk::ImageHelper &RenderTargetVk::getImage() const
100{
101 ASSERT(mImage && mImage->valid());
102 return *mImage;
103}
104
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100105vk::ImageView *RenderTargetVk::getDrawImageView() const
Jamie Madillbcf467f2018-05-23 09:46:00 -0400106{
107 ASSERT(mImageView && mImageView->valid());
108 return mImageView;
109}
110
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100111vk::ImageView *RenderTargetVk::getReadImageView() const
112{
113 return getDrawImageView();
114}
115
Jamie Madillbcf467f2018-05-23 09:46:00 -0400116const vk::Format &RenderTargetVk::getImageFormat() const
117{
118 ASSERT(mImage && mImage->valid());
119 return mImage->getFormat();
120}
121
122const gl::Extents &RenderTargetVk::getImageExtents() const
123{
124 ASSERT(mImage && mImage->valid());
125 return mImage->getExtents();
126}
127
128void RenderTargetVk::updateSwapchainImage(vk::ImageHelper *image, vk::ImageView *imageView)
129{
130 ASSERT(image && image->valid() && imageView && imageView->valid());
131 mImage = image;
132 mImageView = imageView;
Shahbaz Youssefi29b49412019-01-07 14:03:06 -0500133 mOwner = nullptr;
Jamie Madillbcf467f2018-05-23 09:46:00 -0400134}
135
Shahbaz Youssefic81e7bf2019-01-18 15:35:55 -0500136vk::ImageHelper *RenderTargetVk::getImageForRead(vk::CommandGraphResource *readingResource,
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500137 vk::ImageLayout layout,
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400138 vk::CommandBuffer *commandBuffer)
139{
140 ASSERT(mImage && mImage->valid());
141
142 // TODO(jmadill): Better simultaneous resource access. http://anglebug.com/2679
Shahbaz Youssefi028df5f2019-02-13 12:57:10 -0500143 //
144 // A better alternative would be:
145 //
146 // if (mImage->isLayoutChangeNecessary(layout)
147 // {
148 // vk::CommandBuffer *srcLayoutChange;
149 // ANGLE_TRY(mImage->recordCommands(contextVk, &srcLayoutChange));
150 // mImage->changeLayout(mImage->getAspectFlags(), layout, srcLayoutChange);
151 // }
152 // mImage->addReadDependency(readingResource);
153 //
154 // I.e. the transition should happen on a node generated from mImage itself.
155 // However, this needs context to be available here, or all call sites changed
156 // to perform the layout transition and set the dependency.
Jamie Madill2d03ff42018-09-27 15:04:26 -0400157 mImage->addWriteDependency(readingResource);
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400158
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500159 mImage->changeLayout(mImage->getAspectFlags(), layout, commandBuffer);
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400160
161 return mImage;
162}
163
Shahbaz Youssefic81e7bf2019-01-18 15:35:55 -0500164vk::ImageHelper *RenderTargetVk::getImageForWrite(vk::CommandGraphResource *writingResource) const
Jamie Madillbcf467f2018-05-23 09:46:00 -0400165{
166 ASSERT(mImage && mImage->valid());
Jamie Madill2d03ff42018-09-27 15:04:26 -0400167 mImage->addWriteDependency(writingResource);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400168 return mImage;
169}
170
Shahbaz Youssefi29b49412019-01-07 14:03:06 -0500171angle::Result RenderTargetVk::ensureImageInitialized(ContextVk *contextVk)
172{
173 if (mOwner)
174 {
175 return mOwner->ensureImageInitialized(contextVk);
176 }
177 return angle::Result::Continue;
178}
179
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500180} // namespace rx