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 | { |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame^] | 19 | RenderTargetVk::RenderTargetVk(vk::ImageHelper *image, |
| 20 | vk::ImageView *imageView, |
| 21 | size_t layerIndex, |
| 22 | TextureVk *owner) |
| 23 | : mImage(image), mImageView(imageView), mLayerIndex(layerIndex), mOwner(owner) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 24 | {} |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 25 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 26 | RenderTargetVk::~RenderTargetVk() {} |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 27 | |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 28 | RenderTargetVk::RenderTargetVk(RenderTargetVk &&other) |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame^] | 29 | : mImage(other.mImage), |
| 30 | mImageView(other.mImageView), |
| 31 | mLayerIndex(other.mLayerIndex), |
| 32 | mOwner(other.mOwner) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 33 | {} |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 34 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 35 | void RenderTargetVk::onColorDraw(vk::FramebufferHelper *framebufferVk, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 36 | vk::CommandBuffer *commandBuffer, |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 37 | vk::RenderPassDesc *renderPassDesc) |
| 38 | { |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 39 | ASSERT(commandBuffer->valid()); |
| 40 | ASSERT(!mImage->getFormat().textureFormat().hasDepthOrStencilBits()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 41 | |
| 42 | // Store the attachment info in the renderPassDesc. |
Jamie Madill | 502d2e2 | 2018-11-01 11:06:23 -0400 | [diff] [blame] | 43 | renderPassDesc->packAttachment(mImage->getFormat()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 44 | |
| 45 | // TODO(jmadill): Use automatic layout transition. http://anglebug.com/2361 |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 46 | mImage->changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, |
| 47 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 48 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 49 | VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, commandBuffer); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 50 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 51 | // Set up dependencies between the RT resource and the Framebuffer. |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 52 | mImage->addWriteDependency(framebufferVk); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 55 | void RenderTargetVk::onDepthStencilDraw(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()); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 60 | ASSERT(mImage->getFormat().textureFormat().hasDepthOrStencilBits()); |
| 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 |
| 66 | const angle::Format &format = mImage->getFormat().textureFormat(); |
Luc Ferron | 5bdf8bd | 2018-06-20 09:51:37 -0400 | [diff] [blame] | 67 | VkImageAspectFlags aspectFlags = vk::GetDepthStencilAspectFlags(format); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 68 | |
| 69 | mImage->changeLayoutWithStages(aspectFlags, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 70 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 71 | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, commandBuffer); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 72 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 73 | // Set up dependencies between the RT resource and the Framebuffer. |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 74 | mImage->addWriteDependency(framebufferVk); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 77 | vk::ImageHelper &RenderTargetVk::getImage() |
| 78 | { |
| 79 | ASSERT(mImage && mImage->valid()); |
| 80 | return *mImage; |
| 81 | } |
| 82 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 83 | const vk::ImageHelper &RenderTargetVk::getImage() const |
| 84 | { |
| 85 | ASSERT(mImage && mImage->valid()); |
| 86 | return *mImage; |
| 87 | } |
| 88 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 89 | vk::ImageView *RenderTargetVk::getDrawImageView() const |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 90 | { |
| 91 | ASSERT(mImageView && mImageView->valid()); |
| 92 | return mImageView; |
| 93 | } |
| 94 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 95 | vk::ImageView *RenderTargetVk::getReadImageView() const |
| 96 | { |
| 97 | return getDrawImageView(); |
| 98 | } |
| 99 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 100 | const vk::Format &RenderTargetVk::getImageFormat() const |
| 101 | { |
| 102 | ASSERT(mImage && mImage->valid()); |
| 103 | return mImage->getFormat(); |
| 104 | } |
| 105 | |
| 106 | const gl::Extents &RenderTargetVk::getImageExtents() const |
| 107 | { |
| 108 | ASSERT(mImage && mImage->valid()); |
| 109 | return mImage->getExtents(); |
| 110 | } |
| 111 | |
| 112 | void RenderTargetVk::updateSwapchainImage(vk::ImageHelper *image, vk::ImageView *imageView) |
| 113 | { |
| 114 | ASSERT(image && image->valid() && imageView && imageView->valid()); |
| 115 | mImage = image; |
| 116 | mImageView = imageView; |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame^] | 117 | mOwner = nullptr; |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 120 | vk::ImageHelper *RenderTargetVk::getImageForRead(vk::RecordableGraphResource *readingResource, |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 121 | VkImageLayout layout, |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 122 | vk::CommandBuffer *commandBuffer) |
| 123 | { |
| 124 | ASSERT(mImage && mImage->valid()); |
| 125 | |
| 126 | // TODO(jmadill): Better simultaneous resource access. http://anglebug.com/2679 |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 127 | mImage->addWriteDependency(readingResource); |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 128 | |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 129 | mImage->changeLayoutWithStages(mImage->getAspectFlags(), layout, |
| 130 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
Jamie Madill | e4e2d0c | 2018-06-22 08:25:05 -0400 | [diff] [blame] | 131 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer); |
| 132 | |
| 133 | return mImage; |
| 134 | } |
| 135 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 136 | vk::ImageHelper *RenderTargetVk::getImageForWrite( |
| 137 | vk::RecordableGraphResource *writingResource) const |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 138 | { |
| 139 | ASSERT(mImage && mImage->valid()); |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 140 | mImage->addWriteDependency(writingResource); |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 141 | return mImage; |
| 142 | } |
| 143 | |
Shahbaz Youssefi | 29b4941 | 2019-01-07 14:03:06 -0500 | [diff] [blame^] | 144 | angle::Result RenderTargetVk::ensureImageInitialized(ContextVk *contextVk) |
| 145 | { |
| 146 | if (mOwner) |
| 147 | { |
| 148 | return mOwner->ensureImageInitialized(contextVk); |
| 149 | } |
| 150 | return angle::Result::Continue; |
| 151 | } |
| 152 | |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 153 | } // namespace rx |