Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [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 | // RenderbufferVk.cpp: |
| 7 | // Implements the class methods for RenderbufferVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/RenderbufferVk.h" |
| 11 | |
Jamie Madill | 12eb3d7 | 2018-02-14 12:34:45 -0500 | [diff] [blame] | 12 | #include "libANGLE/Context.h" |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 13 | #include "libANGLE/Image.h" |
Jamie Madill | 12eb3d7 | 2018-02-14 12:34:45 -0500 | [diff] [blame] | 14 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 15 | #include "libANGLE/renderer/vulkan/ImageVk.h" |
Jamie Madill | 12eb3d7 | 2018-02-14 12:34:45 -0500 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 17 | |
| 18 | namespace rx |
| 19 | { |
| 20 | |
Luc Ferron | e6a40d0 | 2018-03-22 10:30:57 -0400 | [diff] [blame] | 21 | namespace |
| 22 | { |
| 23 | constexpr VkClearDepthStencilValue kDefaultClearDepthStencilValue = {0.0f, 1}; |
| 24 | constexpr VkClearColorValue kBlackClearColorValue = {{0}}; |
| 25 | |
| 26 | } // anonymous namespace |
| 27 | |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 28 | RenderbufferVk::RenderbufferVk(const gl::RenderbufferState &state) |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 29 | : RenderbufferImpl(state), mOwnsImage(false), mImage(nullptr) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 30 | {} |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 31 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 32 | RenderbufferVk::~RenderbufferVk() {} |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 33 | |
Jamie Madill | c2328a1 | 2018-10-18 15:00:29 -0400 | [diff] [blame] | 34 | void RenderbufferVk::onDestroy(const gl::Context *context) |
Jamie Madill | 12eb3d7 | 2018-02-14 12:34:45 -0500 | [diff] [blame] | 35 | { |
| 36 | ContextVk *contextVk = vk::GetImpl(context); |
| 37 | RendererVk *renderer = contextVk->getRenderer(); |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 38 | releaseAndDeleteImage(context, renderer); |
Jamie Madill | 12eb3d7 | 2018-02-14 12:34:45 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Jamie Madill | c2328a1 | 2018-10-18 15:00:29 -0400 | [diff] [blame] | 41 | angle::Result RenderbufferVk::setStorage(const gl::Context *context, |
| 42 | GLenum internalformat, |
| 43 | size_t width, |
| 44 | size_t height) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 45 | { |
Jamie Madill | 12eb3d7 | 2018-02-14 12:34:45 -0500 | [diff] [blame] | 46 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 57fbfd8 | 2018-02-14 12:45:34 -0500 | [diff] [blame] | 47 | RendererVk *renderer = contextVk->getRenderer(); |
| 48 | const vk::Format &vkFormat = renderer->getFormat(internalformat); |
Jamie Madill | 12eb3d7 | 2018-02-14 12:34:45 -0500 | [diff] [blame] | 49 | |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 50 | if (!mOwnsImage) |
| 51 | { |
| 52 | releaseAndDeleteImage(context, renderer); |
| 53 | } |
| 54 | |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 55 | if (mImage != nullptr && mImage->valid()) |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 56 | { |
| 57 | // Check against the state if we need to recreate the storage. |
| 58 | if (internalformat != mState.getFormat().info->internalFormat || |
| 59 | static_cast<GLsizei>(width) != mState.getWidth() || |
| 60 | static_cast<GLsizei>(height) != mState.getHeight()) |
| 61 | { |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 62 | releaseImage(context, renderer); |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 63 | } |
| 64 | } |
Jamie Madill | 0cbfa58 | 2018-02-15 14:45:41 -0500 | [diff] [blame] | 65 | |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 66 | if ((mImage == nullptr || !mImage->valid()) && (width != 0 && height != 0)) |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 67 | { |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 68 | if (mImage == nullptr) |
| 69 | { |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 70 | mImage = new vk::ImageHelper(); |
| 71 | mOwnsImage = true; |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Luc Ferron | e6a40d0 | 2018-03-22 10:30:57 -0400 | [diff] [blame] | 74 | const angle::Format &textureFormat = vkFormat.textureFormat(); |
| 75 | bool isDepthOrStencilFormat = textureFormat.depthBits > 0 || textureFormat.stencilBits > 0; |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 76 | const VkImageUsageFlags usage = |
Luc Ferron | e6a40d0 | 2018-03-22 10:30:57 -0400 | [diff] [blame] | 77 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 78 | VK_IMAGE_USAGE_SAMPLED_BIT | |
| 79 | (textureFormat.redBits > 0 ? VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT : 0) | |
| 80 | (isDepthOrStencilFormat ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT : 0); |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 81 | |
Jamie Madill | bc54342 | 2018-03-30 10:43:19 -0400 | [diff] [blame] | 82 | gl::Extents extents(static_cast<int>(width), static_cast<int>(height), 1); |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 83 | ANGLE_TRY(mImage->init(contextVk, gl::TextureType::_2D, extents, vkFormat, 1, usage, 1, 1)); |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 84 | |
| 85 | VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 86 | ANGLE_TRY(mImage->initMemory(contextVk, renderer->getMemoryProperties(), flags)); |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 87 | |
Geoff Lang | 9e14164 | 2018-06-27 11:43:18 -0400 | [diff] [blame] | 88 | VkImageAspectFlags aspect = vk::GetFormatAspectFlags(textureFormat); |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 89 | |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 90 | // Note that LUMA textures are not color-renderable, so a read-view with swizzle is not |
| 91 | // needed. |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 92 | ANGLE_TRY(mImage->initImageView(contextVk, gl::TextureType::_2D, aspect, gl::SwizzleState(), |
| 93 | &mImageView, 1)); |
Jamie Madill | e4c5a23 | 2018-03-02 21:00:31 -0500 | [diff] [blame] | 94 | |
| 95 | // TODO(jmadill): Fold this into the RenderPass load/store ops. http://anglebug.com/2361 |
| 96 | vk::CommandBuffer *commandBuffer = nullptr; |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 97 | ANGLE_TRY(mImage->recordCommands(contextVk, &commandBuffer)); |
Jamie Madill | 93edca1 | 2018-03-30 10:43:18 -0400 | [diff] [blame] | 98 | |
Luc Ferron | e6a40d0 | 2018-03-22 10:30:57 -0400 | [diff] [blame] | 99 | if (isDepthOrStencilFormat) |
| 100 | { |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 101 | mImage->clearDepthStencil(aspect, aspect, kDefaultClearDepthStencilValue, |
| 102 | commandBuffer); |
Luc Ferron | e6a40d0 | 2018-03-22 10:30:57 -0400 | [diff] [blame] | 103 | } |
| 104 | else |
| 105 | { |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 106 | mImage->clearColor(kBlackClearColorValue, 0, 1, commandBuffer); |
Luc Ferron | e6a40d0 | 2018-03-22 10:30:57 -0400 | [diff] [blame] | 107 | } |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 108 | |
| 109 | mRenderTarget.init(mImage, &mImageView, 0, nullptr); |
Jamie Madill | 7b21381 | 2018-03-06 10:13:13 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 112 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Jamie Madill | c2328a1 | 2018-10-18 15:00:29 -0400 | [diff] [blame] | 115 | angle::Result RenderbufferVk::setStorageMultisample(const gl::Context *context, |
| 116 | size_t samples, |
| 117 | GLenum internalformat, |
| 118 | size_t width, |
| 119 | size_t height) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 120 | { |
Jamie Madill | c2328a1 | 2018-10-18 15:00:29 -0400 | [diff] [blame] | 121 | ANGLE_VK_UNREACHABLE(vk::GetImpl(context)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 122 | return angle::Result::Stop; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Jamie Madill | c2328a1 | 2018-10-18 15:00:29 -0400 | [diff] [blame] | 125 | angle::Result RenderbufferVk::setStorageEGLImageTarget(const gl::Context *context, |
| 126 | egl::Image *image) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 127 | { |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 128 | ContextVk *contextVk = vk::GetImpl(context); |
| 129 | RendererVk *renderer = contextVk->getRenderer(); |
| 130 | |
| 131 | releaseAndDeleteImage(context, renderer); |
| 132 | |
| 133 | ImageVk *imageVk = vk::GetImpl(image); |
| 134 | mImage = imageVk->getImage(); |
| 135 | mOwnsImage = false; |
| 136 | |
| 137 | const vk::Format &vkFormat = renderer->getFormat(image->getFormat().info->sizedInternalFormat); |
| 138 | const angle::Format &textureFormat = vkFormat.textureFormat(); |
| 139 | |
| 140 | VkImageAspectFlags aspect = vk::GetFormatAspectFlags(textureFormat); |
| 141 | |
| 142 | ANGLE_TRY(mImage->initImageView(contextVk, gl::TextureType::_2D, aspect, gl::SwizzleState(), |
| 143 | &mImageView, 1)); |
| 144 | |
| 145 | mRenderTarget.init(mImage, &mImageView, 0, nullptr); |
| 146 | |
| 147 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 150 | angle::Result RenderbufferVk::getAttachmentRenderTarget(const gl::Context *context, |
| 151 | GLenum binding, |
| 152 | const gl::ImageIndex &imageIndex, |
| 153 | FramebufferAttachmentRenderTarget **rtOut) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 154 | { |
Geoff Lang | f3e823d | 2019-01-14 12:40:34 -0500 | [diff] [blame] | 155 | ASSERT(mImage && mImage->valid()); |
Jamie Madill | 0cbfa58 | 2018-02-15 14:45:41 -0500 | [diff] [blame] | 156 | *rtOut = &mRenderTarget; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 157 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 160 | angle::Result RenderbufferVk::initializeContents(const gl::Context *context, |
| 161 | const gl::ImageIndex &imageIndex) |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 162 | { |
| 163 | UNIMPLEMENTED(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 164 | return angle::Result::Continue; |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Geoff Lang | fe59f6b | 2019-01-16 09:34:30 -0500 | [diff] [blame^] | 167 | void RenderbufferVk::releaseOwnershipOfImage(const gl::Context *context) |
| 168 | { |
| 169 | ContextVk *contextVk = vk::GetImpl(context); |
| 170 | RendererVk *renderer = contextVk->getRenderer(); |
| 171 | |
| 172 | mOwnsImage = false; |
| 173 | releaseAndDeleteImage(context, renderer); |
| 174 | } |
| 175 | |
| 176 | void RenderbufferVk::releaseAndDeleteImage(const gl::Context *context, RendererVk *renderer) |
| 177 | { |
| 178 | releaseImage(context, renderer); |
| 179 | SafeDelete(mImage); |
| 180 | } |
| 181 | |
| 182 | void RenderbufferVk::releaseImage(const gl::Context *context, RendererVk *renderer) |
| 183 | { |
| 184 | if (mImage && mOwnsImage) |
| 185 | { |
| 186 | mImage->releaseImage(renderer); |
| 187 | mImage->releaseStagingBuffer(renderer); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | mImage = nullptr; |
| 192 | } |
| 193 | |
| 194 | renderer->releaseObject(renderer->getCurrentQueueSerial(), &mImageView); |
| 195 | } |
| 196 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 197 | } // namespace rx |