blob: 83b609aab81b9cd9450acd72c35e61520762557e [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
Jamie Madill316c6062018-05-29 10:49:45 -040062 mImage->changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT,
63 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
64 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
65 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, commandBuffer);
Jamie Madillbcf467f2018-05-23 09:46:00 -040066
Jamie Madill316c6062018-05-29 10:49:45 -040067 // Set up dependencies between the RT resource and the Framebuffer.
Jamie Madill2d03ff42018-09-27 15:04:26 -040068 mImage->addWriteDependency(framebufferVk);
Jamie Madillbcf467f2018-05-23 09:46:00 -040069}
70
Jamie Madill193a2842018-10-30 17:28:41 -040071void RenderTargetVk::onDepthStencilDraw(vk::FramebufferHelper *framebufferVk,
Jamie Madill316c6062018-05-29 10:49:45 -040072 vk::CommandBuffer *commandBuffer,
Jamie Madillbcf467f2018-05-23 09:46:00 -040073 vk::RenderPassDesc *renderPassDesc)
74{
Jamie Madill316c6062018-05-29 10:49:45 -040075 ASSERT(commandBuffer->valid());
Jamie Madillbcf467f2018-05-23 09:46:00 -040076 ASSERT(mImage->getFormat().textureFormat().hasDepthOrStencilBits());
77
78 // Store the attachment info in the renderPassDesc.
Jamie Madill502d2e22018-11-01 11:06:23 -040079 renderPassDesc->packAttachment(mImage->getFormat());
Jamie Madillbcf467f2018-05-23 09:46:00 -040080
81 // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361
82 const angle::Format &format = mImage->getFormat().textureFormat();
Luc Ferron5bdf8bd2018-06-20 09:51:37 -040083 VkImageAspectFlags aspectFlags = vk::GetDepthStencilAspectFlags(format);
Jamie Madillbcf467f2018-05-23 09:46:00 -040084
85 mImage->changeLayoutWithStages(aspectFlags, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
86 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
Jamie Madill316c6062018-05-29 10:49:45 -040087 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, 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,
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400137 VkImageLayout 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
Jamie Madill2d03ff42018-09-27 15:04:26 -0400143 mImage->addWriteDependency(readingResource);
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400144
Luc Ferron1617e692018-07-11 11:08:19 -0400145 mImage->changeLayoutWithStages(mImage->getAspectFlags(), layout,
146 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
Jamie Madille4e2d0c2018-06-22 08:25:05 -0400147 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer);
148
149 return mImage;
150}
151
Shahbaz Youssefic81e7bf2019-01-18 15:35:55 -0500152vk::ImageHelper *RenderTargetVk::getImageForWrite(vk::CommandGraphResource *writingResource) const
Jamie Madillbcf467f2018-05-23 09:46:00 -0400153{
154 ASSERT(mImage && mImage->valid());
Jamie Madill2d03ff42018-09-27 15:04:26 -0400155 mImage->addWriteDependency(writingResource);
Jamie Madillbcf467f2018-05-23 09:46:00 -0400156 return mImage;
157}
158
Shahbaz Youssefi29b49412019-01-07 14:03:06 -0500159angle::Result RenderTargetVk::ensureImageInitialized(ContextVk *contextVk)
160{
161 if (mOwner)
162 {
163 return mOwner->ensureImageInitialized(contextVk);
164 }
165 return angle::Result::Continue;
166}
167
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500168} // namespace rx