Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 1 | // |
| 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 Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 12 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame] | 13 | #include "libANGLE/renderer/vulkan/TextureVk.h" |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 14 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
| 15 | #include "libANGLE/renderer/vulkan/vk_helpers.h" |
| 16 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 17 | namespace rx |
| 18 | { |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 19 | RenderTargetVk::RenderTargetVk() |
Geoff Lang | 4a29870 | 2019-01-18 10:49:36 -0500 | [diff] [blame] | 20 | : mImage(nullptr), mImageView(nullptr), mLevelIndex(0), mLayerIndex(0), mOwner(nullptr) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 21 | {} |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 22 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 23 | RenderTargetVk::~RenderTargetVk() {} |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 24 | |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 25 | RenderTargetVk::RenderTargetVk(RenderTargetVk &&other) |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame] | 26 | : mImage(other.mImage), |
| 27 | mImageView(other.mImageView), |
Geoff Lang | 4a29870 | 2019-01-18 10:49:36 -0500 | [diff] [blame] | 28 | mLevelIndex(other.mLevelIndex), |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame] | 29 | mLayerIndex(other.mLayerIndex), |
| 30 | mOwner(other.mOwner) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 31 | {} |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 32 | |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 33 | void RenderTargetVk::init(vk::ImageHelper *image, |
| 34 | vk::ImageView *imageView, |
Geoff Lang | 4a29870 | 2019-01-18 10:49:36 -0500 | [diff] [blame] | 35 | size_t levelIndex, |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 36 | size_t layerIndex, |
| 37 | TextureVk *owner) |
| 38 | { |
| 39 | mImage = image; |
| 40 | mImageView = imageView; |
Geoff Lang | 4a29870 | 2019-01-18 10:49:36 -0500 | [diff] [blame] | 41 | mLevelIndex = levelIndex; |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 42 | mLayerIndex = layerIndex; |
| 43 | mOwner = owner; |
| 44 | } |
| 45 | |
| 46 | void RenderTargetVk::reset() |
| 47 | { |
| 48 | mImage = nullptr; |
| 49 | mImageView = nullptr; |
Geoff Lang | 4a29870 | 2019-01-18 10:49:36 -0500 | [diff] [blame] | 50 | mLevelIndex = 0; |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 51 | mLayerIndex = 0; |
| 52 | mOwner = nullptr; |
| 53 | } |
| 54 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 55 | void RenderTargetVk::onColorDraw(vk::FramebufferHelper *framebufferVk, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 56 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 57 | vk::RenderPassDesc *renderPassDesc) |
| 58 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 59 | ASSERT(commandBuffer->valid()); |
| 60 | ASSERT(!mImage->getFormat().textureFormat().hasDepthOrStencilBits()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 61 | |
| 62 | // Store the attachment info in the renderPassDesc. |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 63 | renderPassDesc->packAttachment(mImage->getFormat()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 64 | |
| 65 | // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361 |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 66 | mImage->changeLayout(VK_IMAGE_ASPECT_COLOR_BIT, vk::ImageLayout::ColorAttachment, |
| 67 | commandBuffer); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 68 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 69 | // Set up dependencies between the RT resource and the Framebuffer. |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 70 | mImage->addWriteDependency(framebufferVk); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 73 | void RenderTargetVk::onDepthStencilDraw(vk::FramebufferHelper *framebufferVk, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 74 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 75 | vk::RenderPassDesc *renderPassDesc) |
| 76 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 77 | ASSERT(commandBuffer->valid()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 78 | ASSERT(mImage->getFormat().textureFormat().hasDepthOrStencilBits()); |
| 79 | |
| 80 | // Store the attachment info in the renderPassDesc. |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 81 | renderPassDesc->packAttachment(mImage->getFormat()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 82 | |
| 83 | // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361 |
| 84 | const angle::Format &format = mImage->getFormat().textureFormat(); |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 85 | VkImageAspectFlags aspectFlags = vk::GetDepthStencilAspectFlags(format); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 86 | |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 87 | mImage->changeLayout(aspectFlags, vk::ImageLayout::DepthStencilAttachment, commandBuffer); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 88 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 89 | // Set up dependencies between the RT resource and the Framebuffer. |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 90 | mImage->addWriteDependency(framebufferVk); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 93 | vk::ImageHelper &RenderTargetVk::getImage() |
| 94 | { |
| 95 | ASSERT(mImage && mImage->valid()); |
| 96 | return *mImage; |
| 97 | } |
| 98 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 99 | const vk::ImageHelper &RenderTargetVk::getImage() const |
| 100 | { |
| 101 | ASSERT(mImage && mImage->valid()); |
| 102 | return *mImage; |
| 103 | } |
| 104 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 105 | vk::ImageView *RenderTargetVk::getDrawImageView() const |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 106 | { |
| 107 | ASSERT(mImageView && mImageView->valid()); |
| 108 | return mImageView; |
| 109 | } |
| 110 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 111 | vk::ImageView *RenderTargetVk::getReadImageView() const |
| 112 | { |
| 113 | return getDrawImageView(); |
| 114 | } |
| 115 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 116 | const vk::Format &RenderTargetVk::getImageFormat() const |
| 117 | { |
| 118 | ASSERT(mImage && mImage->valid()); |
| 119 | return mImage->getFormat(); |
| 120 | } |
| 121 | |
| 122 | const gl::Extents &RenderTargetVk::getImageExtents() const |
| 123 | { |
| 124 | ASSERT(mImage && mImage->valid()); |
| 125 | return mImage->getExtents(); |
| 126 | } |
| 127 | |
| 128 | void RenderTargetVk::updateSwapchainImage(vk::ImageHelper *image, vk::ImageView *imageView) |
| 129 | { |
| 130 | ASSERT(image && image->valid() && imageView && imageView->valid()); |
| 131 | mImage = image; |
| 132 | mImageView = imageView; |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame] | 133 | mOwner = nullptr; |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 136 | vk::ImageHelper *RenderTargetVk::getImageForRead(vk::CommandGraphResource *readingResource, |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 137 | vk::ImageLayout layout, |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 138 | vk::CommandBuffer *commandBuffer) |
| 139 | { |
| 140 | ASSERT(mImage && mImage->valid()); |
| 141 | |
| 142 | // TODO(jmadill): Better simultaneous resource access. http://anglebug.com/2679 |
Shahbaz Youssefi | 028df5f | 2019-02-13 12:57:10 -0500 | [diff] [blame] | 143 | // |
| 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 Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 157 | mImage->addWriteDependency(readingResource); |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 158 | |
Shahbaz Youssefi | 7dafe3e | 2019-01-28 11:39:15 -0500 | [diff] [blame] | 159 | mImage->changeLayout(mImage->getAspectFlags(), layout, commandBuffer); |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 160 | |
| 161 | return mImage; |
| 162 | } |
| 163 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 164 | vk::ImageHelper *RenderTargetVk::getImageForWrite(vk::CommandGraphResource *writingResource) const |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 165 | { |
| 166 | ASSERT(mImage && mImage->valid()); |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 167 | mImage->addWriteDependency(writingResource); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 168 | return mImage; |
| 169 | } |
| 170 | |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame] | 171 | angle::Result RenderTargetVk::ensureImageInitialized(ContextVk *contextVk) |
| 172 | { |
| 173 | if (mOwner) |
| 174 | { |
| 175 | return mOwner->ensureImageInitialized(contextVk); |
| 176 | } |
| 177 | return angle::Result::Continue; |
| 178 | } |
| 179 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 180 | } // namespace rx |