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 | // TextureVk.cpp: |
| 7 | // Implements the class methods for TextureVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/TextureVk.h" |
| 11 | |
| 12 | #include "common/debug.h" |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 13 | #include "image_util/generatemip.inl" |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 14 | #include "libANGLE/Context.h" |
| 15 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 18 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 19 | |
| 20 | namespace rx |
| 21 | { |
Luc Ferron | 5164b79 | 2018-03-06 09:10:12 -0500 | [diff] [blame] | 22 | namespace |
| 23 | { |
Jamie Madill | 93edca1 | 2018-03-30 10:43:18 -0400 | [diff] [blame] | 24 | void MapSwizzleState(GLenum internalFormat, |
| 25 | const gl::SwizzleState &swizzleState, |
| 26 | gl::SwizzleState *swizzleStateOut) |
Luc Ferron | 5164b79 | 2018-03-06 09:10:12 -0500 | [diff] [blame] | 27 | { |
| 28 | switch (internalFormat) |
| 29 | { |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 30 | case GL_LUMINANCE8_OES: |
Jamie Madill | 93edca1 | 2018-03-30 10:43:18 -0400 | [diff] [blame] | 31 | swizzleStateOut->swizzleRed = swizzleState.swizzleRed; |
| 32 | swizzleStateOut->swizzleGreen = swizzleState.swizzleRed; |
| 33 | swizzleStateOut->swizzleBlue = swizzleState.swizzleRed; |
| 34 | swizzleStateOut->swizzleAlpha = GL_ONE; |
Luc Ferron | 5164b79 | 2018-03-06 09:10:12 -0500 | [diff] [blame] | 35 | break; |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 36 | case GL_LUMINANCE8_ALPHA8_OES: |
Jamie Madill | 93edca1 | 2018-03-30 10:43:18 -0400 | [diff] [blame] | 37 | swizzleStateOut->swizzleRed = swizzleState.swizzleRed; |
| 38 | swizzleStateOut->swizzleGreen = swizzleState.swizzleRed; |
| 39 | swizzleStateOut->swizzleBlue = swizzleState.swizzleRed; |
| 40 | swizzleStateOut->swizzleAlpha = swizzleState.swizzleGreen; |
Luc Ferron | 5164b79 | 2018-03-06 09:10:12 -0500 | [diff] [blame] | 41 | break; |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 42 | case GL_ALPHA8_OES: |
Jamie Madill | 93edca1 | 2018-03-30 10:43:18 -0400 | [diff] [blame] | 43 | swizzleStateOut->swizzleRed = GL_ZERO; |
| 44 | swizzleStateOut->swizzleGreen = GL_ZERO; |
| 45 | swizzleStateOut->swizzleBlue = GL_ZERO; |
| 46 | swizzleStateOut->swizzleAlpha = swizzleState.swizzleRed; |
Luc Ferron | 49cef9a | 2018-03-21 17:28:53 -0400 | [diff] [blame] | 47 | break; |
Luc Ferron | 5164b79 | 2018-03-06 09:10:12 -0500 | [diff] [blame] | 48 | default: |
Jamie Madill | 93edca1 | 2018-03-30 10:43:18 -0400 | [diff] [blame] | 49 | *swizzleStateOut = swizzleState; |
Luc Ferron | 5164b79 | 2018-03-06 09:10:12 -0500 | [diff] [blame] | 50 | break; |
| 51 | } |
| 52 | } |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 53 | |
| 54 | constexpr VkBufferUsageFlags kStagingBufferFlags = |
| 55 | (VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT); |
| 56 | constexpr size_t kStagingBufferSize = 1024 * 16; |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 57 | |
| 58 | constexpr VkFormatFeatureFlags kBlitFeatureFlags = |
| 59 | VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT; |
Luc Ferron | 5164b79 | 2018-03-06 09:10:12 -0500 | [diff] [blame] | 60 | } // anonymous namespace |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 61 | |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 62 | // StagingStorage implementation. |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 63 | PixelBuffer::PixelBuffer(RendererVk *renderer) |
| 64 | : mStagingBuffer(kStagingBufferFlags, kStagingBufferSize) |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 65 | { |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 66 | // vkCmdCopyBufferToImage must have an offset that is a multiple of 4. |
| 67 | // https://www.khronos.org/registry/vulkan/specs/1.0/man/html/VkBufferImageCopy.html |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 68 | mStagingBuffer.init(4, renderer); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 71 | PixelBuffer::~PixelBuffer() |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 75 | void PixelBuffer::release(RendererVk *renderer) |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 76 | { |
| 77 | mStagingBuffer.release(renderer); |
| 78 | } |
| 79 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 80 | void PixelBuffer::removeStagedUpdates(const gl::ImageIndex &index) |
| 81 | { |
| 82 | // Find any staged updates for this index and removes them from the pending list. |
| 83 | uint32_t levelIndex = static_cast<uint32_t>(index.getLevelIndex()); |
| 84 | uint32_t layerIndex = static_cast<uint32_t>(index.getLayerIndex()); |
| 85 | auto removeIfStatement = [levelIndex, layerIndex](SubresourceUpdate &update) { |
| 86 | return update.copyRegion.imageSubresource.mipLevel == levelIndex && |
| 87 | update.copyRegion.imageSubresource.baseArrayLayer == layerIndex; |
| 88 | }; |
| 89 | mSubresourceUpdates.erase( |
| 90 | std::remove_if(mSubresourceUpdates.begin(), mSubresourceUpdates.end(), removeIfStatement), |
| 91 | mSubresourceUpdates.end()); |
| 92 | } |
| 93 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 94 | angle::Result PixelBuffer::stageSubresourceUpdate(ContextVk *contextVk, |
| 95 | const gl::ImageIndex &index, |
| 96 | const gl::Extents &extents, |
| 97 | const gl::Offset &offset, |
| 98 | const gl::InternalFormat &formatInfo, |
| 99 | const gl::PixelUnpackState &unpack, |
| 100 | GLenum type, |
| 101 | const uint8_t *pixels) |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 102 | { |
| 103 | GLuint inputRowPitch = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 104 | ANGLE_VK_CHECK_MATH(contextVk, formatInfo.computeRowPitch(type, extents.width, unpack.alignment, |
| 105 | unpack.rowLength, &inputRowPitch)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 106 | |
| 107 | GLuint inputDepthPitch = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 108 | ANGLE_VK_CHECK_MATH(contextVk, formatInfo.computeDepthPitch(extents.height, unpack.imageHeight, |
| 109 | inputRowPitch, &inputDepthPitch)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 110 | |
| 111 | // TODO(jmadill): skip images for 3D Textures. |
| 112 | bool applySkipImages = false; |
| 113 | |
| 114 | GLuint inputSkipBytes = 0; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 115 | ANGLE_VK_CHECK_MATH(contextVk, |
| 116 | formatInfo.computeSkipBytes(type, inputRowPitch, inputDepthPitch, unpack, |
| 117 | applySkipImages, &inputSkipBytes)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 118 | |
| 119 | RendererVk *renderer = contextVk->getRenderer(); |
| 120 | |
| 121 | const vk::Format &vkFormat = renderer->getFormat(formatInfo.sizedInternalFormat); |
| 122 | const angle::Format &storageFormat = vkFormat.textureFormat(); |
| 123 | |
| 124 | size_t outputRowPitch = storageFormat.pixelBytes * extents.width; |
| 125 | size_t outputDepthPitch = outputRowPitch * extents.height; |
| 126 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 127 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 128 | |
Jamie Madill | 7f23293 | 2018-09-12 11:03:06 -0400 | [diff] [blame] | 129 | uint8_t *stagingPointer = nullptr; |
| 130 | bool newBufferAllocated = false; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 131 | VkDeviceSize stagingOffset = 0; |
Jamie Madill | 7f23293 | 2018-09-12 11:03:06 -0400 | [diff] [blame] | 132 | size_t allocationSize = outputDepthPitch * extents.depth; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 133 | ANGLE_TRY(mStagingBuffer.allocate(contextVk, allocationSize, &stagingPointer, &bufferHandle, |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame] | 134 | &stagingOffset, &newBufferAllocated)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 135 | |
| 136 | const uint8_t *source = pixels + inputSkipBytes; |
| 137 | |
Frank Henigman | d9618bf | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 138 | LoadImageFunctionInfo loadFunction = vkFormat.textureLoadFunctions(type); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 139 | |
| 140 | loadFunction.loadFunction(extents.width, extents.height, extents.depth, source, inputRowPitch, |
| 141 | inputDepthPitch, stagingPointer, outputRowPitch, outputDepthPitch); |
| 142 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 143 | VkBufferImageCopy copy; |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 144 | |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 145 | copy.bufferOffset = stagingOffset; |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 146 | copy.bufferRowLength = extents.width; |
| 147 | copy.bufferImageHeight = extents.height; |
| 148 | copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 149 | copy.imageSubresource.mipLevel = index.getLevelIndex(); |
| 150 | copy.imageSubresource.baseArrayLayer = index.hasLayer() ? index.getLayerIndex() : 0; |
| 151 | copy.imageSubresource.layerCount = index.getLayerCount(); |
| 152 | |
Luc Ferron | 33e05ba | 2018-04-23 15:12:34 -0400 | [diff] [blame] | 153 | gl_vk::GetOffset(offset, ©.imageOffset); |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 154 | gl_vk::GetExtent(extents, ©.imageExtent); |
| 155 | |
| 156 | mSubresourceUpdates.emplace_back(bufferHandle, copy); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 157 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 158 | return angle::Result::Continue(); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 161 | angle::Result PixelBuffer::stageSubresourceUpdateFromFramebuffer( |
| 162 | const gl::Context *context, |
| 163 | const gl::ImageIndex &index, |
| 164 | const gl::Rectangle &sourceArea, |
| 165 | const gl::Offset &dstOffset, |
| 166 | const gl::Extents &dstExtent, |
| 167 | const gl::InternalFormat &formatInfo, |
| 168 | FramebufferVk *framebufferVk) |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 169 | { |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 170 | ContextVk *contextVk = vk::GetImpl(context); |
| 171 | |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 172 | // If the extents and offset is outside the source image, we need to clip. |
| 173 | gl::Rectangle clippedRectangle; |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 174 | const gl::Extents readExtents = framebufferVk->getReadImageExtents(); |
| 175 | if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, readExtents.width, readExtents.height), |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 176 | &clippedRectangle)) |
| 177 | { |
| 178 | // Empty source area, nothing to do. |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 179 | return angle::Result::Continue(); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 182 | bool isViewportFlipEnabled = contextVk->isViewportFlipEnabledForDrawFBO(); |
| 183 | if (isViewportFlipEnabled) |
| 184 | { |
| 185 | clippedRectangle.y = readExtents.height - clippedRectangle.y - clippedRectangle.height; |
| 186 | } |
| 187 | |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 188 | // 1- obtain a buffer handle to copy to |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 189 | RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 190 | |
| 191 | const vk::Format &vkFormat = renderer->getFormat(formatInfo.sizedInternalFormat); |
| 192 | const angle::Format &storageFormat = vkFormat.textureFormat(); |
Frank Henigman | d9618bf | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 193 | LoadImageFunctionInfo loadFunction = vkFormat.textureLoadFunctions(formatInfo.type); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 194 | |
| 195 | size_t outputRowPitch = storageFormat.pixelBytes * clippedRectangle.width; |
| 196 | size_t outputDepthPitch = outputRowPitch * clippedRectangle.height; |
| 197 | |
| 198 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 199 | |
Jamie Madill | 7f23293 | 2018-09-12 11:03:06 -0400 | [diff] [blame] | 200 | uint8_t *stagingPointer = nullptr; |
| 201 | bool newBufferAllocated = false; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 202 | VkDeviceSize stagingOffset = 0; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 203 | |
| 204 | // The destination is only one layer deep. |
| 205 | size_t allocationSize = outputDepthPitch; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 206 | ANGLE_TRY(mStagingBuffer.allocate(contextVk, allocationSize, &stagingPointer, &bufferHandle, |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame] | 207 | &stagingOffset, &newBufferAllocated)); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 208 | |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 209 | gl::PixelPackState pixelPackState = gl::PixelPackState(); |
| 210 | // TODO(lucferron): The pixel pack state alignment should probably be 1 instead of 4. |
| 211 | // http://anglebug.com/2718 |
| 212 | |
| 213 | if (isViewportFlipEnabled) |
| 214 | { |
| 215 | pixelPackState.reverseRowOrder = !pixelPackState.reverseRowOrder; |
| 216 | } |
| 217 | |
Jamie Madill | db9c69e | 2018-07-18 17:23:47 -0400 | [diff] [blame] | 218 | const angle::Format ©Format = |
| 219 | GetFormatFromFormatType(formatInfo.internalFormat, formatInfo.type); |
| 220 | PackPixelsParams params(clippedRectangle, copyFormat, static_cast<GLuint>(outputRowPitch), |
| 221 | pixelPackState, nullptr, 0); |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 222 | |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 223 | // 2- copy the source image region to the pixel buffer using a cpu readback |
| 224 | if (loadFunction.requiresConversion) |
| 225 | { |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 226 | // When a conversion is required, we need to use the loadFunction to read from a temporary |
| 227 | // buffer instead so its an even slower path. |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 228 | size_t bufferSize = |
| 229 | storageFormat.pixelBytes * clippedRectangle.width * clippedRectangle.height; |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 230 | angle::MemoryBuffer *memoryBuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 231 | ANGLE_VK_CHECK_ALLOC(contextVk, context->getScratchBuffer(bufferSize, &memoryBuffer)); |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 232 | |
| 233 | // Read into the scratch buffer |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 234 | ANGLE_TRY(framebufferVk->readPixelsImpl( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 235 | contextVk, clippedRectangle, params, VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 236 | framebufferVk->getColorReadRenderTarget(), memoryBuffer->data())); |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 237 | |
| 238 | // Load from scratch buffer to our pixel buffer |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 239 | loadFunction.loadFunction(clippedRectangle.width, clippedRectangle.height, 1, |
| 240 | memoryBuffer->data(), outputRowPitch, 0, stagingPointer, |
| 241 | outputRowPitch, 0); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 242 | } |
| 243 | else |
| 244 | { |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 245 | // We read directly from the framebuffer into our pixel buffer. |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 246 | ANGLE_TRY(framebufferVk->readPixelsImpl( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 247 | contextVk, clippedRectangle, params, VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 248 | framebufferVk->getColorReadRenderTarget(), stagingPointer)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 249 | } |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 250 | |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 251 | // 3- enqueue the destination image subresource update |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 252 | VkBufferImageCopy copyToImage; |
| 253 | copyToImage.bufferOffset = static_cast<VkDeviceSize>(stagingOffset); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 254 | copyToImage.bufferRowLength = 0; // Tightly packed data can be specified as 0. |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 255 | copyToImage.bufferImageHeight = clippedRectangle.height; |
| 256 | copyToImage.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 257 | copyToImage.imageSubresource.mipLevel = index.getLevelIndex(); |
| 258 | copyToImage.imageSubresource.baseArrayLayer = index.hasLayer() ? index.getLayerIndex() : 0; |
| 259 | copyToImage.imageSubresource.layerCount = index.getLayerCount(); |
| 260 | gl_vk::GetOffset(dstOffset, ©ToImage.imageOffset); |
| 261 | gl_vk::GetExtent(dstExtent, ©ToImage.imageExtent); |
| 262 | |
| 263 | // 3- enqueue the destination image subresource update |
| 264 | mSubresourceUpdates.emplace_back(bufferHandle, copyToImage); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 265 | return angle::Result::Continue(); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 268 | angle::Result PixelBuffer::allocate(ContextVk *contextVk, |
| 269 | size_t sizeInBytes, |
| 270 | uint8_t **ptrOut, |
| 271 | VkBuffer *handleOut, |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 272 | VkDeviceSize *offsetOut, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 273 | bool *newBufferAllocatedOut) |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 274 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 275 | return mStagingBuffer.allocate(contextVk, sizeInBytes, ptrOut, handleOut, offsetOut, |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 276 | newBufferAllocatedOut); |
| 277 | } |
| 278 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 279 | angle::Result PixelBuffer::flushUpdatesToImage(ContextVk *contextVk, |
| 280 | uint32_t levelCount, |
| 281 | vk::ImageHelper *image, |
| 282 | vk::CommandBuffer *commandBuffer) |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 283 | { |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 284 | if (mSubresourceUpdates.empty()) |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 285 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 286 | return angle::Result::Continue(); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 289 | ANGLE_TRY(mStagingBuffer.flush(contextVk)); |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 290 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 291 | std::vector<SubresourceUpdate> updatesToKeep; |
| 292 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 293 | for (const SubresourceUpdate &update : mSubresourceUpdates) |
| 294 | { |
| 295 | ASSERT(update.bufferHandle != VK_NULL_HANDLE); |
Luc Ferron | 1a186b1 | 2018-04-24 15:25:35 -0400 | [diff] [blame] | 296 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 297 | const uint32_t updateMipLevel = update.copyRegion.imageSubresource.mipLevel; |
| 298 | // It's possible we've accumulated updates that are no longer applicable if the image has |
| 299 | // never been flushed but the image description has changed. Check if this level exist for |
| 300 | // this image. |
| 301 | if (updateMipLevel >= levelCount) |
| 302 | { |
| 303 | updatesToKeep.emplace_back(update); |
| 304 | continue; |
| 305 | } |
| 306 | |
Luc Ferron | 1a186b1 | 2018-04-24 15:25:35 -0400 | [diff] [blame] | 307 | // Conservatively flush all writes to the image. We could use a more restricted barrier. |
| 308 | // Do not move this above the for loop, otherwise multiple updates can have race conditions |
| 309 | // and not be applied correctly as seen i: |
| 310 | // dEQP-gles2.functional_texture_specification_texsubimage2d_align_2d* tests on Windows AMD |
| 311 | image->changeLayoutWithStages( |
| 312 | VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 313 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, commandBuffer); |
| 314 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 315 | commandBuffer->copyBufferToImage(update.bufferHandle, image->getImage(), |
| 316 | image->getCurrentLayout(), 1, &update.copyRegion); |
| 317 | } |
| 318 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 319 | // Only remove the updates that were actually applied to the image. |
| 320 | mSubresourceUpdates = std::move(updatesToKeep); |
| 321 | |
| 322 | if (mSubresourceUpdates.empty()) |
| 323 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 324 | mStagingBuffer.releaseRetainedBuffers(contextVk->getRenderer()); |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | { |
| 328 | WARN() << "Internal Vulkan bufffer could not be released. This is likely due to having " |
| 329 | "extra images defined in the Texture."; |
| 330 | } |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 331 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 332 | return angle::Result::Continue(); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Luc Ferron | 10434f6 | 2018-04-24 10:06:37 -0400 | [diff] [blame] | 335 | bool PixelBuffer::empty() const |
| 336 | { |
| 337 | return mSubresourceUpdates.empty(); |
| 338 | } |
| 339 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 340 | angle::Result PixelBuffer::stageSubresourceUpdateAndGetData(ContextVk *contextVk, |
| 341 | size_t allocationSize, |
| 342 | const gl::ImageIndex &imageIndex, |
| 343 | const gl::Extents &extents, |
| 344 | const gl::Offset &offset, |
| 345 | uint8_t **destData) |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 346 | { |
| 347 | VkBuffer bufferHandle; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 348 | VkDeviceSize stagingOffset = 0; |
Jamie Madill | 7f23293 | 2018-09-12 11:03:06 -0400 | [diff] [blame] | 349 | bool newBufferAllocated = false; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 350 | ANGLE_TRY(mStagingBuffer.allocate(contextVk, allocationSize, destData, &bufferHandle, |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 351 | &stagingOffset, &newBufferAllocated)); |
| 352 | |
| 353 | VkBufferImageCopy copy; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 354 | copy.bufferOffset = stagingOffset; |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 355 | copy.bufferRowLength = extents.width; |
| 356 | copy.bufferImageHeight = extents.height; |
| 357 | copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 358 | copy.imageSubresource.mipLevel = imageIndex.getLevelIndex(); |
| 359 | copy.imageSubresource.baseArrayLayer = imageIndex.hasLayer() ? imageIndex.getLayerIndex() : 0; |
| 360 | copy.imageSubresource.layerCount = imageIndex.getLayerCount(); |
| 361 | |
| 362 | gl_vk::GetOffset(offset, ©.imageOffset); |
| 363 | gl_vk::GetExtent(extents, ©.imageExtent); |
| 364 | |
| 365 | mSubresourceUpdates.emplace_back(bufferHandle, copy); |
| 366 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 367 | return angle::Result::Continue(); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 368 | } |
| 369 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 370 | angle::Result TextureVk::generateMipmapLevelsWithCPU(ContextVk *contextVk, |
| 371 | const angle::Format &sourceFormat, |
| 372 | GLuint layer, |
| 373 | GLuint firstMipLevel, |
| 374 | GLuint maxMipLevel, |
| 375 | const size_t sourceWidth, |
| 376 | const size_t sourceHeight, |
| 377 | const size_t sourceRowPitch, |
| 378 | uint8_t *sourceData) |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 379 | { |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 380 | size_t previousLevelWidth = sourceWidth; |
| 381 | size_t previousLevelHeight = sourceHeight; |
| 382 | uint8_t *previousLevelData = sourceData; |
| 383 | size_t previousLevelRowPitch = sourceRowPitch; |
| 384 | |
| 385 | for (GLuint currentMipLevel = firstMipLevel; currentMipLevel <= maxMipLevel; currentMipLevel++) |
| 386 | { |
| 387 | // Compute next level width and height. |
| 388 | size_t mipWidth = std::max<size_t>(1, previousLevelWidth >> 1); |
| 389 | size_t mipHeight = std::max<size_t>(1, previousLevelHeight >> 1); |
| 390 | |
| 391 | // With the width and height of the next mip, we can allocate the next buffer we need. |
| 392 | uint8_t *destData = nullptr; |
| 393 | size_t destRowPitch = mipWidth * sourceFormat.pixelBytes; |
| 394 | |
| 395 | size_t mipAllocationSize = destRowPitch * mipHeight; |
| 396 | gl::Extents mipLevelExtents(static_cast<int>(mipWidth), static_cast<int>(mipHeight), 1); |
| 397 | |
| 398 | ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateAndGetData( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 399 | contextVk, mipAllocationSize, |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 400 | gl::ImageIndex::MakeFromType(mState.getType(), currentMipLevel, layer), mipLevelExtents, |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 401 | gl::Offset(), &destData)); |
| 402 | |
| 403 | // Generate the mipmap into that new buffer |
| 404 | sourceFormat.mipGenerationFunction(previousLevelWidth, previousLevelHeight, 1, |
| 405 | previousLevelData, previousLevelRowPitch, 0, destData, |
| 406 | destRowPitch, 0); |
| 407 | |
| 408 | // Swap for the next iteration |
| 409 | previousLevelWidth = mipWidth; |
| 410 | previousLevelHeight = mipHeight; |
| 411 | previousLevelData = destData; |
| 412 | previousLevelRowPitch = destRowPitch; |
| 413 | } |
| 414 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 415 | return angle::Result::Continue(); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 416 | } |
| 417 | |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 418 | PixelBuffer::SubresourceUpdate::SubresourceUpdate() : bufferHandle(VK_NULL_HANDLE) |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 419 | { |
| 420 | } |
| 421 | |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 422 | PixelBuffer::SubresourceUpdate::SubresourceUpdate(VkBuffer bufferHandleIn, |
| 423 | const VkBufferImageCopy ©RegionIn) |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 424 | : bufferHandle(bufferHandleIn), copyRegion(copyRegionIn) |
| 425 | { |
| 426 | } |
| 427 | |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 428 | PixelBuffer::SubresourceUpdate::SubresourceUpdate(const SubresourceUpdate &other) = default; |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 429 | |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 430 | // TextureVk implementation. |
Luc Ferron | a9ab0f3 | 2018-05-17 17:03:55 -0400 | [diff] [blame] | 431 | TextureVk::TextureVk(const gl::TextureState &state, RendererVk *renderer) |
Jamie Madill | 3f3b358 | 2018-09-14 10:38:44 -0400 | [diff] [blame] | 432 | : TextureImpl(state), |
| 433 | mRenderTarget(&mImage, &mBaseLevelImageView, this, 0), |
| 434 | mPixelBuffer(renderer) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 435 | { |
| 436 | } |
| 437 | |
| 438 | TextureVk::~TextureVk() |
| 439 | { |
| 440 | } |
| 441 | |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 442 | gl::Error TextureVk::onDestroy(const gl::Context *context) |
| 443 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 444 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 445 | RendererVk *renderer = contextVk->getRenderer(); |
| 446 | |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 447 | releaseImage(context, renderer); |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 448 | renderer->releaseObject(getStoredQueueSerial(), &mSampler); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 449 | |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 450 | mPixelBuffer.release(renderer); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 451 | return gl::NoError(); |
| 452 | } |
| 453 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 454 | gl::Error TextureVk::setImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 455 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 456 | GLenum internalFormat, |
| 457 | const gl::Extents &size, |
| 458 | GLenum format, |
| 459 | GLenum type, |
| 460 | const gl::PixelUnpackState &unpack, |
| 461 | const uint8_t *pixels) |
| 462 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 463 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 1b03824 | 2017-11-01 15:14:36 -0400 | [diff] [blame] | 464 | RendererVk *renderer = contextVk->getRenderer(); |
| 465 | |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 466 | // Convert internalFormat to sized internal format. |
| 467 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat, type); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 468 | |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 469 | ANGLE_TRY(redefineImage(context, index, formatInfo, size)); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 470 | |
Geoff Lang | bd6ae4a | 2018-01-29 15:51:18 -0500 | [diff] [blame] | 471 | // Early-out on empty textures, don't create a zero-sized storage. |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 472 | if (size.empty()) |
Geoff Lang | bd6ae4a | 2018-01-29 15:51:18 -0500 | [diff] [blame] | 473 | { |
| 474 | return gl::NoError(); |
| 475 | } |
| 476 | |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 477 | // Create a new graph node to store image initialization commands. |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame^] | 478 | finishCurrentCommands(renderer); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 479 | |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 480 | // Handle initial data. |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 481 | if (pixels) |
| 482 | { |
Luc Ferron | 33e05ba | 2018-04-23 15:12:34 -0400 | [diff] [blame] | 483 | ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate(contextVk, index, size, gl::Offset(), |
| 484 | formatInfo, unpack, type, pixels)); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 490 | gl::Error TextureVk::setSubImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 491 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 492 | const gl::Box &area, |
| 493 | GLenum format, |
| 494 | GLenum type, |
| 495 | const gl::PixelUnpackState &unpack, |
Jamie Madill | 0d0fb43 | 2018-09-07 17:43:32 -0400 | [diff] [blame] | 496 | gl::Buffer *unpackBuffer, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 497 | const uint8_t *pixels) |
| 498 | { |
Jamie Madill | 5b18f48 | 2017-11-30 17:24:22 -0500 | [diff] [blame] | 499 | ContextVk *contextVk = vk::GetImpl(context); |
| 500 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type); |
Luc Ferron | 33e05ba | 2018-04-23 15:12:34 -0400 | [diff] [blame] | 501 | ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate( |
| 502 | contextVk, index, gl::Extents(area.width, area.height, area.depth), |
| 503 | gl::Offset(area.x, area.y, area.z), formatInfo, unpack, type, pixels)); |
Jamie Madill | b221486 | 2018-04-26 07:25:48 -0400 | [diff] [blame] | 504 | |
| 505 | // Create a new graph node to store image initialization commands. |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame^] | 506 | finishCurrentCommands(contextVk->getRenderer()); |
Jamie Madill | b221486 | 2018-04-26 07:25:48 -0400 | [diff] [blame] | 507 | |
Jamie Madill | 5b18f48 | 2017-11-30 17:24:22 -0500 | [diff] [blame] | 508 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 509 | } |
| 510 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 511 | gl::Error TextureVk::setCompressedImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 512 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 513 | GLenum internalFormat, |
| 514 | const gl::Extents &size, |
| 515 | const gl::PixelUnpackState &unpack, |
| 516 | size_t imageSize, |
| 517 | const uint8_t *pixels) |
| 518 | { |
| 519 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 520 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 521 | } |
| 522 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 523 | gl::Error TextureVk::setCompressedSubImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 524 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 525 | const gl::Box &area, |
| 526 | GLenum format, |
| 527 | const gl::PixelUnpackState &unpack, |
| 528 | size_t imageSize, |
| 529 | const uint8_t *pixels) |
| 530 | { |
| 531 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 532 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 533 | } |
| 534 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 535 | gl::Error TextureVk::copyImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 536 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 537 | const gl::Rectangle &sourceArea, |
| 538 | GLenum internalFormat, |
Jamie Madill | 690c8eb | 2018-03-12 15:20:03 -0400 | [diff] [blame] | 539 | gl::Framebuffer *source) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 540 | { |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 541 | gl::Extents newImageSize(sourceArea.width, sourceArea.height, 1); |
| 542 | const gl::InternalFormat &internalFormatInfo = |
| 543 | gl::GetInternalFormatInfo(internalFormat, GL_UNSIGNED_BYTE); |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 544 | ANGLE_TRY(redefineImage(context, index, internalFormatInfo, newImageSize)); |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 545 | return copySubImageImpl(context, index, gl::Offset(0, 0, 0), sourceArea, internalFormatInfo, |
| 546 | source); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 549 | gl::Error TextureVk::copySubImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 550 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 551 | const gl::Offset &destOffset, |
| 552 | const gl::Rectangle &sourceArea, |
Jamie Madill | 690c8eb | 2018-03-12 15:20:03 -0400 | [diff] [blame] | 553 | gl::Framebuffer *source) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 554 | { |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 555 | const gl::InternalFormat ¤tFormat = *mState.getBaseLevelDesc().format.info; |
| 556 | return copySubImageImpl(context, index, destOffset, sourceArea, currentFormat, source); |
| 557 | } |
| 558 | |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 559 | gl::Error TextureVk::copyTexture(const gl::Context *context, |
| 560 | const gl::ImageIndex &index, |
| 561 | GLenum internalFormat, |
| 562 | GLenum type, |
| 563 | size_t sourceLevel, |
| 564 | bool unpackFlipY, |
| 565 | bool unpackPremultiplyAlpha, |
| 566 | bool unpackUnmultiplyAlpha, |
| 567 | const gl::Texture *source) |
| 568 | { |
| 569 | TextureVk *sourceVk = vk::GetImpl(source); |
| 570 | const gl::ImageDesc &sourceImageDesc = |
| 571 | sourceVk->mState.getImageDesc(NonCubeTextureTypeToTarget(source->getType()), sourceLevel); |
| 572 | gl::Rectangle sourceArea(0, 0, sourceImageDesc.size.width, sourceImageDesc.size.height); |
| 573 | |
| 574 | const gl::InternalFormat &destFormatInfo = gl::GetInternalFormatInfo(internalFormat, type); |
| 575 | |
| 576 | ANGLE_TRY(redefineImage(context, index, destFormatInfo, sourceImageDesc.size)); |
| 577 | |
| 578 | return copySubTextureImpl(vk::GetImpl(context), index, gl::kOffsetZero, destFormatInfo, |
| 579 | sourceLevel, sourceArea, unpackFlipY, unpackPremultiplyAlpha, |
| 580 | unpackUnmultiplyAlpha, sourceVk); |
| 581 | } |
| 582 | |
| 583 | gl::Error TextureVk::copySubTexture(const gl::Context *context, |
| 584 | const gl::ImageIndex &index, |
| 585 | const gl::Offset &destOffset, |
| 586 | size_t sourceLevel, |
| 587 | const gl::Rectangle &sourceArea, |
| 588 | bool unpackFlipY, |
| 589 | bool unpackPremultiplyAlpha, |
| 590 | bool unpackUnmultiplyAlpha, |
| 591 | const gl::Texture *source) |
| 592 | { |
| 593 | gl::TextureTarget target = index.getTarget(); |
| 594 | size_t level = static_cast<size_t>(index.getLevelIndex()); |
| 595 | const gl::InternalFormat &destFormatInfo = *mState.getImageDesc(target, level).format.info; |
| 596 | return copySubTextureImpl(vk::GetImpl(context), index, destOffset, destFormatInfo, sourceLevel, |
| 597 | sourceArea, unpackFlipY, unpackPremultiplyAlpha, |
| 598 | unpackUnmultiplyAlpha, vk::GetImpl(source)); |
| 599 | } |
| 600 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 601 | angle::Result TextureVk::copySubImageImpl(const gl::Context *context, |
| 602 | const gl::ImageIndex &index, |
| 603 | const gl::Offset &destOffset, |
| 604 | const gl::Rectangle &sourceArea, |
| 605 | const gl::InternalFormat &internalFormat, |
| 606 | gl::Framebuffer *source) |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 607 | { |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 608 | gl::Extents fbSize = source->getReadColorbuffer()->getSize(); |
| 609 | gl::Rectangle clippedSourceArea; |
| 610 | if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, fbSize.width, fbSize.height), |
| 611 | &clippedSourceArea)) |
| 612 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 613 | return angle::Result::Continue(); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | const gl::Offset modifiedDestOffset(destOffset.x + sourceArea.x - sourceArea.x, |
| 617 | destOffset.y + sourceArea.y - sourceArea.y, 0); |
| 618 | |
Frank Henigman | d9618bf | 2018-06-24 19:57:31 -0400 | [diff] [blame] | 619 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 620 | RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 621 | FramebufferVk *framebufferVk = vk::GetImpl(source); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 622 | |
| 623 | // For now, favor conformance. We do a CPU readback that does the conversion, and then stage the |
| 624 | // change to the pixel buffer. |
| 625 | // Eventually we can improve this easily by implementing vkCmdBlitImage to do the conversion |
| 626 | // when its supported. |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 627 | ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateFromFramebuffer( |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 628 | context, index, clippedSourceArea, modifiedDestOffset, |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 629 | gl::Extents(clippedSourceArea.width, clippedSourceArea.height, 1), internalFormat, |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 630 | framebufferVk)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 631 | |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame^] | 632 | finishCurrentCommands(renderer); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 633 | framebufferVk->addReadDependency(this); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 634 | return angle::Result::Continue(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 635 | } |
| 636 | |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 637 | gl::Error TextureVk::copySubTextureImpl(ContextVk *contextVk, |
| 638 | const gl::ImageIndex &index, |
| 639 | const gl::Offset &destOffset, |
| 640 | const gl::InternalFormat &destFormat, |
| 641 | size_t sourceLevel, |
| 642 | const gl::Rectangle &sourceArea, |
| 643 | bool unpackFlipY, |
| 644 | bool unpackPremultiplyAlpha, |
| 645 | bool unpackUnmultiplyAlpha, |
| 646 | TextureVk *source) |
| 647 | { |
| 648 | RendererVk *renderer = contextVk->getRenderer(); |
| 649 | |
| 650 | // Read back the requested region of the source texture |
| 651 | uint8_t *sourceData = nullptr; |
| 652 | ANGLE_TRY(source->copyImageDataToBuffer(contextVk, sourceLevel, sourceArea, &sourceData)); |
| 653 | |
| 654 | ANGLE_TRY(renderer->finish(contextVk)); |
| 655 | |
| 656 | // Using the front-end ANGLE format for the colorRead and colorWrite functions. Otherwise |
| 657 | // emulated formats like luminance-alpha would not know how to interpret the data. |
| 658 | const angle::Format &sourceAngleFormat = source->getImage().getFormat().angleFormat(); |
| 659 | const angle::Format &destAngleFormat = |
| 660 | renderer->getFormat(destFormat.sizedInternalFormat).angleFormat(); |
| 661 | size_t destinationAllocationSize = |
| 662 | sourceArea.width * sourceArea.height * destAngleFormat.pixelBytes; |
| 663 | |
| 664 | // Allocate memory in the destination texture for the copy/conversion |
| 665 | uint8_t *destData = nullptr; |
| 666 | ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateAndGetData( |
| 667 | contextVk, destinationAllocationSize, index, |
| 668 | gl::Extents(sourceArea.width, sourceArea.height, 1), destOffset, &destData)); |
| 669 | |
| 670 | // Source and dest data is tightly packed |
| 671 | GLuint sourceDataRowPitch = sourceArea.width * sourceAngleFormat.pixelBytes; |
| 672 | GLuint destDataRowPitch = sourceArea.width * destAngleFormat.pixelBytes; |
| 673 | |
| 674 | CopyImageCHROMIUM(sourceData, sourceDataRowPitch, sourceAngleFormat.pixelBytes, |
Jamie Madill | 522095f | 2018-07-23 14:59:41 -0400 | [diff] [blame] | 675 | sourceAngleFormat.pixelReadFunction, destData, destDataRowPitch, |
| 676 | destAngleFormat.pixelBytes, destAngleFormat.pixelWriteFunction, |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 677 | destFormat.format, destFormat.componentType, sourceArea.width, |
| 678 | sourceArea.height, unpackFlipY, unpackPremultiplyAlpha, |
| 679 | unpackUnmultiplyAlpha); |
| 680 | |
| 681 | // Create a new graph node to store image initialization commands. |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame^] | 682 | finishCurrentCommands(contextVk->getRenderer()); |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 683 | |
| 684 | return angle::Result::Continue(); |
| 685 | } |
| 686 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 687 | angle::Result TextureVk::getCommandBufferForWrite(ContextVk *contextVk, |
| 688 | vk::CommandBuffer **commandBufferOut) |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 689 | { |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame^] | 690 | ANGLE_TRY(recordCommands(contextVk, commandBufferOut)); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 691 | return angle::Result::Continue(); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 692 | } |
| 693 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 694 | gl::Error TextureVk::setStorage(const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 695 | gl::TextureType type, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 696 | size_t levels, |
| 697 | GLenum internalFormat, |
| 698 | const gl::Extents &size) |
| 699 | { |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 700 | ContextVk *contextVk = GetAs<ContextVk>(context->getImplementation()); |
| 701 | RendererVk *renderer = contextVk->getRenderer(); |
| 702 | const vk::Format &format = renderer->getFormat(internalFormat); |
| 703 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 704 | ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer)); |
Jamie Madill | 3e29cf3 | 2018-08-31 17:19:17 -0400 | [diff] [blame] | 705 | |
| 706 | if (mImage.valid()) |
| 707 | { |
| 708 | releaseImage(context, renderer); |
| 709 | } |
| 710 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 711 | ANGLE_TRY(initImage(contextVk, format, size, static_cast<uint32_t>(levels), commandBuffer)); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 712 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 715 | gl::Error TextureVk::setEGLImageTarget(const gl::Context *context, |
| 716 | gl::TextureType type, |
| 717 | egl::Image *image) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 718 | { |
| 719 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 720 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 721 | } |
| 722 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 723 | gl::Error TextureVk::setImageExternal(const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 724 | gl::TextureType type, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 725 | egl::Stream *stream, |
| 726 | const egl::Stream::GLTextureDescription &desc) |
| 727 | { |
| 728 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 729 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 730 | } |
| 731 | |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 732 | angle::Result TextureVk::redefineImage(const gl::Context *context, |
| 733 | const gl::ImageIndex &index, |
| 734 | const gl::InternalFormat &internalFormat, |
| 735 | const gl::Extents &size) |
| 736 | { |
| 737 | ContextVk *contextVk = vk::GetImpl(context); |
| 738 | RendererVk *renderer = contextVk->getRenderer(); |
| 739 | |
| 740 | // If there is any staged changes for this index, we can remove them since we're going to |
| 741 | // override them with this call. |
| 742 | mPixelBuffer.removeStagedUpdates(index); |
| 743 | |
| 744 | if (mImage.valid()) |
| 745 | { |
| 746 | const vk::Format &vkFormat = renderer->getFormat(internalFormat.sizedInternalFormat); |
| 747 | |
| 748 | // Calculate the expected size for the index we are defining. If the size is different from |
| 749 | // the given size, or the format is different, we are redefining the image so we must |
| 750 | // release it. |
| 751 | if (mImage.getFormat() != vkFormat || size != mImage.getSize(index)) |
| 752 | { |
| 753 | releaseImage(context, renderer); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | return angle::Result::Continue(); |
| 758 | } |
| 759 | |
| 760 | angle::Result TextureVk::copyImageDataToBuffer(ContextVk *contextVk, |
| 761 | size_t sourceLevel, |
| 762 | const gl::Rectangle &sourceArea, |
| 763 | uint8_t **outDataPtr) |
| 764 | { |
| 765 | if (sourceLevel != 0) |
| 766 | { |
| 767 | WARN() << "glCopyTextureCHROMIUM with sourceLevel != 0 not implemented."; |
| 768 | return angle::Result::Stop(); |
| 769 | } |
| 770 | |
| 771 | // Make sure the source is initialized and it's images are flushed. |
| 772 | ANGLE_TRY(ensureImageInitialized(contextVk)); |
| 773 | |
| 774 | const angle::Format &angleFormat = getImage().getFormat().textureFormat(); |
| 775 | const gl::Extents imageSize = |
| 776 | mState.getImageDesc(NonCubeTextureTypeToTarget(mState.getType()), sourceLevel).size; |
| 777 | size_t sourceCopyAllocationSize = sourceArea.width * sourceArea.height * angleFormat.pixelBytes; |
| 778 | |
| 779 | vk::CommandBuffer *commandBuffer = nullptr; |
| 780 | ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer)); |
| 781 | |
| 782 | // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout. |
| 783 | bool newBufferAllocated = false; |
| 784 | mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 785 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 786 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer); |
| 787 | |
| 788 | // Allocate enough memory to copy the sourceArea region of the source texture into its pixel |
| 789 | // buffer. |
| 790 | VkBuffer copyBufferHandle; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 791 | VkDeviceSize sourceCopyOffset = 0; |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 792 | ANGLE_TRY(mPixelBuffer.allocate(contextVk, sourceCopyAllocationSize, outDataPtr, |
| 793 | ©BufferHandle, &sourceCopyOffset, &newBufferAllocated)); |
| 794 | |
| 795 | VkBufferImageCopy region; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 796 | region.bufferOffset = sourceCopyOffset; |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 797 | region.bufferRowLength = imageSize.width; |
| 798 | region.bufferImageHeight = imageSize.height; |
| 799 | region.imageExtent.width = sourceArea.width; |
| 800 | region.imageExtent.height = sourceArea.height; |
| 801 | region.imageExtent.depth = 1; |
| 802 | region.imageOffset.x = sourceArea.x; |
| 803 | region.imageOffset.y = sourceArea.y; |
| 804 | region.imageOffset.z = 0; |
| 805 | region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 806 | region.imageSubresource.baseArrayLayer = 0; |
| 807 | region.imageSubresource.layerCount = 1; |
Jamie Madill | c685586 | 2018-07-18 15:06:54 -0400 | [diff] [blame] | 808 | region.imageSubresource.mipLevel = static_cast<uint32_t>(sourceLevel); |
Geoff Lang | d691aee | 2018-07-11 16:32:06 -0400 | [diff] [blame] | 809 | |
| 810 | commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle, |
| 811 | 1, ®ion); |
| 812 | |
| 813 | return angle::Result::Continue(); |
| 814 | } |
| 815 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 816 | angle::Result TextureVk::generateMipmapWithBlit(ContextVk *contextVk) |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 817 | { |
| 818 | uint32_t imageLayerCount = GetImageLayerCount(mState.getType()); |
| 819 | const gl::Extents baseLevelExtents = mImage.getExtents(); |
| 820 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 821 | ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer)); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 822 | |
| 823 | // We are able to use blitImage since the image format we are using supports it. This |
| 824 | // is a faster way we can generate the mips. |
| 825 | int32_t mipWidth = baseLevelExtents.width; |
| 826 | int32_t mipHeight = baseLevelExtents.height; |
| 827 | |
| 828 | // Manually manage the image memory barrier because it uses a lot more parameters than our |
| 829 | // usual one. |
| 830 | VkImageMemoryBarrier barrier; |
| 831 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 832 | barrier.image = mImage.getImage().getHandle(); |
| 833 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 834 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 835 | barrier.pNext = nullptr; |
| 836 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 837 | barrier.subresourceRange.baseArrayLayer = 0; |
| 838 | barrier.subresourceRange.layerCount = imageLayerCount; |
| 839 | barrier.subresourceRange.levelCount = 1; |
| 840 | |
| 841 | for (uint32_t mipLevel = 1; mipLevel <= mState.getMipmapMaxLevel(); mipLevel++) |
| 842 | { |
| 843 | int32_t nextMipWidth = std::max<int32_t>(1, mipWidth >> 1); |
| 844 | int32_t nextMipHeight = std::max<int32_t>(1, mipHeight >> 1); |
| 845 | |
| 846 | barrier.subresourceRange.baseMipLevel = mipLevel - 1; |
| 847 | barrier.oldLayout = mImage.getCurrentLayout(); |
| 848 | barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 849 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 850 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 851 | |
| 852 | // We can do it for all layers at once. |
| 853 | commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 854 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier); |
| 855 | |
| 856 | VkImageBlit blit = {}; |
| 857 | blit.srcOffsets[0] = {0, 0, 0}; |
| 858 | blit.srcOffsets[1] = {mipWidth, mipHeight, 1}; |
| 859 | blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 860 | blit.srcSubresource.mipLevel = mipLevel - 1; |
| 861 | blit.srcSubresource.baseArrayLayer = 0; |
| 862 | blit.srcSubresource.layerCount = imageLayerCount; |
| 863 | blit.dstOffsets[0] = {0, 0, 0}; |
| 864 | blit.dstOffsets[1] = {nextMipWidth, nextMipHeight, 1}; |
| 865 | blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 866 | blit.dstSubresource.mipLevel = mipLevel; |
| 867 | blit.dstSubresource.baseArrayLayer = 0; |
| 868 | blit.dstSubresource.layerCount = imageLayerCount; |
| 869 | |
| 870 | mipWidth = nextMipWidth; |
| 871 | mipHeight = nextMipHeight; |
| 872 | |
| 873 | commandBuffer->blitImage(mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 874 | mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, |
| 875 | VK_FILTER_LINEAR); |
| 876 | } |
| 877 | |
| 878 | // Transition the last mip level to the same layout as all the other ones, so we can declare |
| 879 | // our whole image layout to be SRC_OPTIMAL. |
| 880 | barrier.subresourceRange.baseMipLevel = mState.getMipmapMaxLevel(); |
| 881 | barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 882 | barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 883 | |
| 884 | // We can do it for all layers at once. |
| 885 | commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 886 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier); |
| 887 | |
| 888 | // This is just changing the internal state of the image helper so that the next call |
| 889 | // to changeLayoutWithStages will use this layout as the "oldLayout" argument. |
| 890 | mImage.updateLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame] | 891 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 892 | return angle::Result::Continue(); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 893 | } |
| 894 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 895 | angle::Result TextureVk::generateMipmapWithCPU(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 896 | { |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 897 | ContextVk *contextVk = vk::GetImpl(context); |
| 898 | RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 899 | |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 900 | bool newBufferAllocated = false; |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 901 | const gl::Extents baseLevelExtents = mImage.getExtents(); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 902 | uint32_t imageLayerCount = GetImageLayerCount(mState.getType()); |
| 903 | const angle::Format &angleFormat = mImage.getFormat().textureFormat(); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 904 | GLuint sourceRowPitch = baseLevelExtents.width * angleFormat.pixelBytes; |
| 905 | size_t baseLevelAllocationSize = sourceRowPitch * baseLevelExtents.height; |
| 906 | |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 907 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 908 | ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer)); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 909 | |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 910 | // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout. |
| 911 | mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 912 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 913 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer); |
| 914 | |
| 915 | size_t totalAllocationSize = baseLevelAllocationSize * imageLayerCount; |
| 916 | |
| 917 | VkBuffer copyBufferHandle; |
| 918 | uint8_t *baseLevelBuffers; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 919 | VkDeviceSize copyBaseOffset; |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 920 | |
| 921 | // Allocate enough memory to copy every level 0 image (one for each layer of the texture). |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 922 | ANGLE_TRY(mPixelBuffer.allocate(contextVk, totalAllocationSize, &baseLevelBuffers, |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 923 | ©BufferHandle, ©BaseOffset, &newBufferAllocated)); |
| 924 | |
| 925 | // Do only one copy for all layers at once. |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 926 | VkBufferImageCopy region; |
| 927 | region.bufferImageHeight = baseLevelExtents.height; |
Jamie Madill | 4c31083 | 2018-08-29 13:43:17 -0400 | [diff] [blame] | 928 | region.bufferOffset = copyBaseOffset; |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 929 | region.bufferRowLength = baseLevelExtents.width; |
| 930 | region.imageExtent.width = baseLevelExtents.width; |
| 931 | region.imageExtent.height = baseLevelExtents.height; |
| 932 | region.imageExtent.depth = 1; |
| 933 | region.imageOffset.x = 0; |
| 934 | region.imageOffset.y = 0; |
| 935 | region.imageOffset.z = 0; |
| 936 | region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 937 | region.imageSubresource.baseArrayLayer = 0; |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 938 | region.imageSubresource.layerCount = imageLayerCount; |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 939 | region.imageSubresource.mipLevel = mState.getEffectiveBaseLevel(); |
| 940 | |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 941 | commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle, |
| 942 | 1, ®ion); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 943 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 944 | ANGLE_TRY(renderer->finish(contextVk)); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 945 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 946 | const uint32_t levelCount = getLevelCount(); |
| 947 | |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 948 | // We now have the base level available to be manipulated in the baseLevelBuffer pointer. |
| 949 | // Generate all the missing mipmaps with the slow path. We can optimize with vkCmdBlitImage |
| 950 | // later. |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 951 | // For each layer, use the copied data to generate all the mips. |
| 952 | for (GLuint layer = 0; layer < imageLayerCount; layer++) |
| 953 | { |
| 954 | size_t bufferOffset = layer * baseLevelAllocationSize; |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 955 | |
| 956 | ANGLE_TRY(generateMipmapLevelsWithCPU( |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 957 | contextVk, angleFormat, layer, mState.getEffectiveBaseLevel() + 1, |
| 958 | mState.getMipmapMaxLevel(), baseLevelExtents.width, baseLevelExtents.height, |
| 959 | sourceRowPitch, baseLevelBuffers + bufferOffset)); |
| 960 | } |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 961 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 962 | return mPixelBuffer.flushUpdatesToImage(contextVk, levelCount, &mImage, commandBuffer); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | gl::Error TextureVk::generateMipmap(const gl::Context *context) |
| 966 | { |
| 967 | ContextVk *contextVk = vk::GetImpl(context); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 968 | |
| 969 | // Some data is pending, or the image has not been defined at all yet |
| 970 | if (!mImage.valid()) |
| 971 | { |
| 972 | // lets initialize the image so we can generate the next levels. |
| 973 | if (!mPixelBuffer.empty()) |
| 974 | { |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 975 | ANGLE_TRY(ensureImageInitialized(contextVk)); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 976 | ASSERT(mImage.valid()); |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | // There is nothing to generate if there is nothing uploaded so far. |
| 981 | return gl::NoError(); |
| 982 | } |
| 983 | } |
| 984 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 985 | RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 986 | VkFormatProperties imageProperties; |
| 987 | vk::GetFormatProperties(renderer->getPhysicalDevice(), mImage.getFormat().vkTextureFormat, |
| 988 | &imageProperties); |
| 989 | |
| 990 | // Check if the image supports blit. If it does, we can do the mipmap generation on the gpu |
| 991 | // only. |
| 992 | if (IsMaskFlagSet(kBlitFeatureFlags, imageProperties.linearTilingFeatures)) |
| 993 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 994 | ANGLE_TRY(generateMipmapWithBlit(contextVk)); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 995 | } |
| 996 | else |
| 997 | { |
| 998 | ANGLE_TRY(generateMipmapWithCPU(context)); |
| 999 | } |
| 1000 | |
| 1001 | // We're changing this textureVk content, make sure we let the graph know. |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame^] | 1002 | finishCurrentCommands(renderer); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 1003 | |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 1004 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1007 | gl::Error TextureVk::setBaseLevel(const gl::Context *context, GLuint baseLevel) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1008 | { |
| 1009 | UNIMPLEMENTED(); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1010 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1011 | } |
| 1012 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1013 | gl::Error TextureVk::bindTexImage(const gl::Context *context, egl::Surface *surface) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1014 | { |
| 1015 | UNIMPLEMENTED(); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1016 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1017 | } |
| 1018 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1019 | gl::Error TextureVk::releaseTexImage(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1020 | { |
| 1021 | UNIMPLEMENTED(); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1022 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 1025 | gl::Error TextureVk::getAttachmentRenderTarget(const gl::Context *context, |
| 1026 | GLenum binding, |
Jamie Madill | 4fd95d5 | 2017-04-05 11:22:18 -0400 | [diff] [blame] | 1027 | const gl::ImageIndex &imageIndex, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1028 | FramebufferAttachmentRenderTarget **rtOut) |
| 1029 | { |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1030 | // Non-zero mip level attachments are an ES 3.0 feature. |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 1031 | ASSERT(imageIndex.getLevelIndex() == 0); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1032 | |
| 1033 | ContextVk *contextVk = vk::GetImpl(context); |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 1034 | ANGLE_TRY(ensureImageInitialized(contextVk)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1035 | |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 1036 | switch (imageIndex.getType()) |
| 1037 | { |
| 1038 | case gl::TextureType::_2D: |
| 1039 | *rtOut = &mRenderTarget; |
| 1040 | break; |
| 1041 | case gl::TextureType::CubeMap: |
| 1042 | ANGLE_TRY(initCubeMapRenderTargets(contextVk)); |
| 1043 | *rtOut = &mCubeMapRenderTargets[imageIndex.cubeMapFaceIndex()]; |
| 1044 | break; |
| 1045 | default: |
| 1046 | UNREACHABLE(); |
| 1047 | } |
| 1048 | |
Jamie Madill | b79e7bb | 2017-10-24 13:55:50 -0400 | [diff] [blame] | 1049 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1052 | angle::Result TextureVk::ensureImageInitialized(ContextVk *contextVk) |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1053 | { |
Luc Ferron | 10434f6 | 2018-04-24 10:06:37 -0400 | [diff] [blame] | 1054 | if (mImage.valid() && mPixelBuffer.empty()) |
| 1055 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1056 | return angle::Result::Continue(); |
Luc Ferron | 10434f6 | 2018-04-24 10:06:37 -0400 | [diff] [blame] | 1057 | } |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 1058 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1059 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1060 | ANGLE_TRY(getCommandBufferForWrite(contextVk, &commandBuffer)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1061 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 1062 | const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc(); |
| 1063 | const gl::Extents &baseLevelExtents = baseLevelDesc.size; |
| 1064 | const uint32_t levelCount = getLevelCount(); |
| 1065 | |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1066 | if (!mImage.valid()) |
| 1067 | { |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1068 | const vk::Format &format = |
| 1069 | renderer->getFormat(baseLevelDesc.format.info->sizedInternalFormat); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1070 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 1071 | ANGLE_TRY(initImage(contextVk, format, baseLevelExtents, levelCount, commandBuffer)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1072 | } |
| 1073 | |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 1074 | return mPixelBuffer.flushUpdatesToImage(contextVk, levelCount, &mImage, commandBuffer); |
| 1075 | } |
| 1076 | |
| 1077 | angle::Result TextureVk::initCubeMapRenderTargets(ContextVk *contextVk) |
| 1078 | { |
| 1079 | // Lazy init. Check if already initialized. |
| 1080 | if (!mCubeMapFaceImageViews.empty()) |
| 1081 | return angle::Result::Continue(); |
| 1082 | |
| 1083 | mCubeMapFaceImageViews.resize(gl::kCubeFaceCount); |
| 1084 | |
| 1085 | for (size_t cubeMapFaceIndex = 0; cubeMapFaceIndex < gl::kCubeFaceCount; ++cubeMapFaceIndex) |
| 1086 | { |
| 1087 | vk::ImageView &imageView = mCubeMapFaceImageViews[cubeMapFaceIndex]; |
| 1088 | ANGLE_TRY(mImage.initLayerImageView(contextVk, gl::TextureType::CubeMap, |
| 1089 | VK_IMAGE_ASPECT_COLOR_BIT, gl::SwizzleState(), |
| 1090 | &imageView, 1, cubeMapFaceIndex, 1)); |
Jamie Madill | 3f3b358 | 2018-09-14 10:38:44 -0400 | [diff] [blame] | 1091 | mCubeMapRenderTargets.emplace_back(&mImage, &imageView, this, cubeMapFaceIndex); |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 1092 | } |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1093 | return angle::Result::Continue(); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
Luc Ferron | 4bba74f | 2018-04-19 14:40:45 -0400 | [diff] [blame] | 1096 | gl::Error TextureVk::syncState(const gl::Context *context, const gl::Texture::DirtyBits &dirtyBits) |
Geoff Lang | 2241686 | 2016-06-08 16:14:36 -0700 | [diff] [blame] | 1097 | { |
Luc Ferron | 2061090 | 2018-04-19 14:41:13 -0400 | [diff] [blame] | 1098 | if (dirtyBits.none() && mSampler.valid()) |
| 1099 | { |
| 1100 | return gl::NoError(); |
| 1101 | } |
| 1102 | |
| 1103 | ContextVk *contextVk = vk::GetImpl(context); |
| 1104 | if (mSampler.valid()) |
| 1105 | { |
| 1106 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 1107 | renderer->releaseObject(getStoredQueueSerial(), &mSampler); |
Luc Ferron | 2061090 | 2018-04-19 14:41:13 -0400 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | const gl::SamplerState &samplerState = mState.getSamplerState(); |
| 1111 | |
| 1112 | // Create a simple sampler. Force basic parameter settings. |
| 1113 | VkSamplerCreateInfo samplerInfo; |
| 1114 | samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; |
| 1115 | samplerInfo.pNext = nullptr; |
| 1116 | samplerInfo.flags = 0; |
Jamie Madill | 097d3c0 | 2018-09-12 11:03:05 -0400 | [diff] [blame] | 1117 | samplerInfo.magFilter = gl_vk::GetFilter(samplerState.getMagFilter()); |
| 1118 | samplerInfo.minFilter = gl_vk::GetFilter(samplerState.getMinFilter()); |
| 1119 | samplerInfo.mipmapMode = gl_vk::GetSamplerMipmapMode(samplerState.getMinFilter()); |
| 1120 | samplerInfo.addressModeU = gl_vk::GetSamplerAddressMode(samplerState.getWrapS()); |
| 1121 | samplerInfo.addressModeV = gl_vk::GetSamplerAddressMode(samplerState.getWrapT()); |
| 1122 | samplerInfo.addressModeW = gl_vk::GetSamplerAddressMode(samplerState.getWrapR()); |
Luc Ferron | 2061090 | 2018-04-19 14:41:13 -0400 | [diff] [blame] | 1123 | samplerInfo.mipLodBias = 0.0f; |
| 1124 | samplerInfo.anisotropyEnable = VK_FALSE; |
| 1125 | samplerInfo.maxAnisotropy = 1.0f; |
| 1126 | samplerInfo.compareEnable = VK_FALSE; |
| 1127 | samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS; |
Jamie Madill | 097d3c0 | 2018-09-12 11:03:05 -0400 | [diff] [blame] | 1128 | samplerInfo.minLod = samplerState.getMinLod(); |
| 1129 | samplerInfo.maxLod = samplerState.getMaxLod(); |
Luc Ferron | 2061090 | 2018-04-19 14:41:13 -0400 | [diff] [blame] | 1130 | samplerInfo.borderColor = VK_BORDER_COLOR_INT_TRANSPARENT_BLACK; |
| 1131 | samplerInfo.unnormalizedCoordinates = VK_FALSE; |
| 1132 | |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 1133 | return mSampler.init(contextVk, samplerInfo); |
Geoff Lang | 2241686 | 2016-06-08 16:14:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 1136 | gl::Error TextureVk::setStorageMultisample(const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 1137 | gl::TextureType type, |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1138 | GLsizei samples, |
| 1139 | GLint internalformat, |
| 1140 | const gl::Extents &size, |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 1141 | bool fixedSampleLocations) |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 1142 | { |
| 1143 | UNIMPLEMENTED(); |
| 1144 | return gl::InternalError() << "setStorageMultisample is unimplemented."; |
| 1145 | } |
| 1146 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 1147 | gl::Error TextureVk::initializeContents(const gl::Context *context, |
| 1148 | const gl::ImageIndex &imageIndex) |
| 1149 | { |
| 1150 | UNIMPLEMENTED(); |
| 1151 | return gl::NoError(); |
| 1152 | } |
| 1153 | |
Jamie Madill | 858c1cc | 2018-03-31 14:19:13 -0400 | [diff] [blame] | 1154 | const vk::ImageHelper &TextureVk::getImage() const |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 1155 | { |
| 1156 | ASSERT(mImage.valid()); |
Jamie Madill | 858c1cc | 2018-03-31 14:19:13 -0400 | [diff] [blame] | 1157 | return mImage; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | const vk::ImageView &TextureVk::getImageView() const |
| 1161 | { |
Jamie Madill | 93edca1 | 2018-03-30 10:43:18 -0400 | [diff] [blame] | 1162 | ASSERT(mImage.valid()); |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 1163 | |
Jamie Madill | 097d3c0 | 2018-09-12 11:03:05 -0400 | [diff] [blame] | 1164 | const GLenum minFilter = mState.getSamplerState().getMinFilter(); |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 1165 | if (minFilter == GL_LINEAR || minFilter == GL_NEAREST) |
| 1166 | { |
| 1167 | return mBaseLevelImageView; |
| 1168 | } |
| 1169 | |
| 1170 | return mMipmapImageView; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | const vk::Sampler &TextureVk::getSampler() const |
| 1174 | { |
| 1175 | ASSERT(mSampler.valid()); |
| 1176 | return mSampler; |
| 1177 | } |
| 1178 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1179 | angle::Result TextureVk::initImage(ContextVk *contextVk, |
| 1180 | const vk::Format &format, |
| 1181 | const gl::Extents &extents, |
| 1182 | const uint32_t levelCount, |
| 1183 | vk::CommandBuffer *commandBuffer) |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1184 | { |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 1185 | const RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1186 | |
| 1187 | const VkImageUsageFlags usage = |
| 1188 | (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 1189 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT); |
| 1190 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1191 | ANGLE_TRY(mImage.init(contextVk, mState.getType(), extents, format, 1, usage, levelCount)); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1192 | |
| 1193 | const VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 1194 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1195 | ANGLE_TRY(mImage.initMemory(contextVk, renderer->getMemoryProperties(), flags)); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1196 | |
| 1197 | gl::SwizzleState mappedSwizzle; |
| 1198 | MapSwizzleState(format.internalFormat, mState.getSwizzleState(), &mappedSwizzle); |
| 1199 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 1200 | // Renderable textures cannot have a swizzle. |
| 1201 | ASSERT(!contextVk->getTextureCaps().get(format.internalFormat).textureAttachment || |
| 1202 | !mappedSwizzle.swizzleRequired()); |
| 1203 | |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1204 | // TODO(jmadill): Separate imageviews for RenderTargets and Sampling. |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1205 | ANGLE_TRY(mImage.initImageView(contextVk, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1206 | mappedSwizzle, &mMipmapImageView, levelCount)); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1207 | ANGLE_TRY(mImage.initImageView(contextVk, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT, |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1208 | mappedSwizzle, &mBaseLevelImageView, 1)); |
| 1209 | |
| 1210 | // TODO(jmadill): Fold this into the RenderPass load/store ops. http://anglebug.com/2361 |
Luc Ferron | 7348fc5 | 2018-05-09 07:17:16 -0400 | [diff] [blame] | 1211 | VkClearColorValue black = {{0, 0, 0, 1.0f}}; |
Luc Ferron | c20b950 | 2018-05-24 09:30:17 -0400 | [diff] [blame] | 1212 | mImage.clearColor(black, 0, levelCount, commandBuffer); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1213 | return angle::Result::Continue(); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1214 | } |
| 1215 | |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 1216 | void TextureVk::releaseImage(const gl::Context *context, RendererVk *renderer) |
| 1217 | { |
| 1218 | mImage.release(renderer->getCurrentQueueSerial(), renderer); |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 1219 | renderer->releaseObject(getStoredQueueSerial(), &mBaseLevelImageView); |
| 1220 | renderer->releaseObject(getStoredQueueSerial(), &mMipmapImageView); |
Jamie Madill | 71bb026 | 2018-09-12 11:09:42 -0400 | [diff] [blame] | 1221 | |
| 1222 | for (vk::ImageView &imageView : mCubeMapFaceImageViews) |
| 1223 | { |
| 1224 | renderer->releaseObject(getStoredQueueSerial(), &imageView); |
| 1225 | } |
| 1226 | mCubeMapFaceImageViews.clear(); |
| 1227 | mCubeMapRenderTargets.clear(); |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 1228 | } |
| 1229 | |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 1230 | uint32_t TextureVk::getLevelCount() const |
| 1231 | { |
| 1232 | ASSERT(mState.getEffectiveBaseLevel() == 0); |
| 1233 | |
| 1234 | // getMipmapMaxLevel will be 0 here if mipmaps are not used, so the levelCount is always +1. |
| 1235 | return mState.getMipmapMaxLevel() + 1; |
| 1236 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1237 | } // namespace rx |