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() |
| 20 | : mImage(nullptr), mImageView(nullptr), 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), |
| 28 | mLayerIndex(other.mLayerIndex), |
| 29 | mOwner(other.mOwner) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 30 | {} |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 31 | |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame^] | 32 | void 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 | |
| 43 | void RenderTargetVk::reset() |
| 44 | { |
| 45 | mImage = nullptr; |
| 46 | mImageView = nullptr; |
| 47 | mLayerIndex = 0; |
| 48 | mOwner = nullptr; |
| 49 | } |
| 50 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 51 | void RenderTargetVk::onColorDraw(vk::FramebufferHelper *framebufferVk, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 52 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 53 | vk::RenderPassDesc *renderPassDesc) |
| 54 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 55 | ASSERT(commandBuffer->valid()); |
| 56 | ASSERT(!mImage->getFormat().textureFormat().hasDepthOrStencilBits()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 57 | |
| 58 | // Store the attachment info in the renderPassDesc. |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 59 | renderPassDesc->packAttachment(mImage->getFormat()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 60 | |
| 61 | // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361 |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 62 | 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 Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 66 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 67 | // Set up dependencies between the RT resource and the Framebuffer. |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 68 | mImage->addWriteDependency(framebufferVk); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 71 | void RenderTargetVk::onDepthStencilDraw(vk::FramebufferHelper *framebufferVk, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 72 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 73 | vk::RenderPassDesc *renderPassDesc) |
| 74 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 75 | ASSERT(commandBuffer->valid()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 76 | ASSERT(mImage->getFormat().textureFormat().hasDepthOrStencilBits()); |
| 77 | |
| 78 | // Store the attachment info in the renderPassDesc. |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 79 | renderPassDesc->packAttachment(mImage->getFormat()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 80 | |
| 81 | // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361 |
| 82 | const angle::Format &format = mImage->getFormat().textureFormat(); |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 83 | VkImageAspectFlags aspectFlags = vk::GetDepthStencilAspectFlags(format); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 84 | |
| 85 | mImage->changeLayoutWithStages(aspectFlags, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 86 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 87 | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, 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, |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 137 | VkImageLayout 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 |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 143 | mImage->addWriteDependency(readingResource); |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 144 | |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 145 | mImage->changeLayoutWithStages(mImage->getAspectFlags(), layout, |
| 146 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 147 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer); |
| 148 | |
| 149 | return mImage; |
| 150 | } |
| 151 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 152 | vk::ImageHelper *RenderTargetVk::getImageForWrite(vk::CommandGraphResource *writingResource) const |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 153 | { |
| 154 | ASSERT(mImage && mImage->valid()); |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 155 | mImage->addWriteDependency(writingResource); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 156 | return mImage; |
| 157 | } |
| 158 | |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame] | 159 | angle::Result RenderTargetVk::ensureImageInitialized(ContextVk *contextVk) |
| 160 | { |
| 161 | if (mOwner) |
| 162 | { |
| 163 | return mOwner->ensureImageInitialized(contextVk); |
| 164 | } |
| 165 | return angle::Result::Continue; |
| 166 | } |
| 167 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 168 | } // namespace rx |