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 | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 94 | vk::Error PixelBuffer::stageSubresourceUpdate(ContextVk *contextVk, |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 95 | const gl::ImageIndex &index, |
| 96 | const gl::Extents &extents, |
Luc Ferron | 33e05ba | 2018-04-23 15:12:34 -0400 | [diff] [blame] | 97 | const gl::Offset &offset, |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 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 | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 104 | ANGLE_TRY_VK_CHECKED_MATH(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 | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 108 | ANGLE_TRY_VK_CHECKED_MATH(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 | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 115 | ANGLE_TRY_VK_CHECKED_MATH(formatInfo.computeSkipBytes( |
| 116 | type, inputRowPitch, inputDepthPitch, unpack, applySkipImages, &inputSkipBytes)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 117 | |
| 118 | RendererVk *renderer = contextVk->getRenderer(); |
| 119 | |
| 120 | const vk::Format &vkFormat = renderer->getFormat(formatInfo.sizedInternalFormat); |
| 121 | const angle::Format &storageFormat = vkFormat.textureFormat(); |
| 122 | |
| 123 | size_t outputRowPitch = storageFormat.pixelBytes * extents.width; |
| 124 | size_t outputDepthPitch = outputRowPitch * extents.height; |
| 125 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 126 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 127 | |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 128 | uint8_t *stagingPointer = nullptr; |
| 129 | bool newBufferAllocated = false; |
| 130 | uint32_t stagingOffset = 0; |
| 131 | size_t allocationSize = outputDepthPitch * extents.depth; |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 132 | ANGLE_TRY(mStagingBuffer.allocate(renderer, allocationSize, &stagingPointer, &bufferHandle, |
| 133 | &stagingOffset, &newBufferAllocated)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 134 | |
| 135 | const uint8_t *source = pixels + inputSkipBytes; |
| 136 | |
| 137 | LoadImageFunctionInfo loadFunction = vkFormat.loadFunctions(type); |
| 138 | |
| 139 | loadFunction.loadFunction(extents.width, extents.height, extents.depth, source, inputRowPitch, |
| 140 | inputDepthPitch, stagingPointer, outputRowPitch, outputDepthPitch); |
| 141 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 142 | VkBufferImageCopy copy; |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 143 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 144 | copy.bufferOffset = static_cast<VkDeviceSize>(stagingOffset); |
| 145 | copy.bufferRowLength = extents.width; |
| 146 | copy.bufferImageHeight = extents.height; |
| 147 | copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 148 | copy.imageSubresource.mipLevel = index.getLevelIndex(); |
| 149 | copy.imageSubresource.baseArrayLayer = index.hasLayer() ? index.getLayerIndex() : 0; |
| 150 | copy.imageSubresource.layerCount = index.getLayerCount(); |
| 151 | |
Luc Ferron | 33e05ba | 2018-04-23 15:12:34 -0400 | [diff] [blame] | 152 | gl_vk::GetOffset(offset, ©.imageOffset); |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 153 | gl_vk::GetExtent(extents, ©.imageExtent); |
| 154 | |
| 155 | mSubresourceUpdates.emplace_back(bufferHandle, copy); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 156 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 157 | return vk::NoError(); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 160 | vk::Error PixelBuffer::stageSubresourceUpdateFromFramebuffer(const gl::Context *context, |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 161 | const gl::ImageIndex &index, |
| 162 | const gl::Rectangle &sourceArea, |
| 163 | const gl::Offset &dstOffset, |
| 164 | const gl::Extents &dstExtent, |
| 165 | const gl::InternalFormat &formatInfo, |
| 166 | FramebufferVk *framebufferVk) |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 167 | { |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 168 | ContextVk *contextVk = vk::GetImpl(context); |
| 169 | |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 170 | // If the extents and offset is outside the source image, we need to clip. |
| 171 | gl::Rectangle clippedRectangle; |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 172 | const gl::Extents readExtents = framebufferVk->getReadImageExtents(); |
| 173 | if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, readExtents.width, readExtents.height), |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 174 | &clippedRectangle)) |
| 175 | { |
| 176 | // Empty source area, nothing to do. |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 177 | return vk::NoError(); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 180 | bool isViewportFlipEnabled = contextVk->isViewportFlipEnabledForDrawFBO(); |
| 181 | if (isViewportFlipEnabled) |
| 182 | { |
| 183 | clippedRectangle.y = readExtents.height - clippedRectangle.y - clippedRectangle.height; |
| 184 | } |
| 185 | |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 186 | // 1- obtain a buffer handle to copy to |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 187 | RendererVk *renderer = GetImplAs<ContextVk>(context)->getRenderer(); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 188 | |
| 189 | const vk::Format &vkFormat = renderer->getFormat(formatInfo.sizedInternalFormat); |
| 190 | const angle::Format &storageFormat = vkFormat.textureFormat(); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 191 | LoadImageFunctionInfo loadFunction = vkFormat.loadFunctions(formatInfo.type); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 192 | |
| 193 | size_t outputRowPitch = storageFormat.pixelBytes * clippedRectangle.width; |
| 194 | size_t outputDepthPitch = outputRowPitch * clippedRectangle.height; |
| 195 | |
| 196 | VkBuffer bufferHandle = VK_NULL_HANDLE; |
| 197 | |
| 198 | uint8_t *stagingPointer = nullptr; |
| 199 | bool newBufferAllocated = false; |
| 200 | uint32_t stagingOffset = 0; |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 201 | |
| 202 | // The destination is only one layer deep. |
| 203 | size_t allocationSize = outputDepthPitch; |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 204 | ANGLE_TRY(mStagingBuffer.allocate(renderer, allocationSize, &stagingPointer, &bufferHandle, |
| 205 | &stagingOffset, &newBufferAllocated)); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 206 | |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 207 | gl::PixelPackState pixelPackState = gl::PixelPackState(); |
| 208 | // TODO(lucferron): The pixel pack state alignment should probably be 1 instead of 4. |
| 209 | // http://anglebug.com/2718 |
| 210 | |
| 211 | if (isViewportFlipEnabled) |
| 212 | { |
| 213 | pixelPackState.reverseRowOrder = !pixelPackState.reverseRowOrder; |
| 214 | } |
| 215 | |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 216 | PackPixelsParams params; |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 217 | params.area = clippedRectangle; |
Luc Ferron | c94ba1d | 2018-06-18 11:26:28 -0400 | [diff] [blame] | 218 | params.format = formatInfo.format; |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 219 | params.type = formatInfo.type; |
| 220 | params.outputPitch = static_cast<GLuint>(outputRowPitch); |
| 221 | params.packBuffer = nullptr; |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 222 | params.pack = pixelPackState; |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 223 | |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 224 | // 2- copy the source image region to the pixel buffer using a cpu readback |
| 225 | if (loadFunction.requiresConversion) |
| 226 | { |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 227 | // When a conversion is required, we need to use the loadFunction to read from a temporary |
| 228 | // buffer instead so its an even slower path. |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 229 | size_t bufferSize = |
| 230 | storageFormat.pixelBytes * clippedRectangle.width * clippedRectangle.height; |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 231 | angle::MemoryBuffer *memoryBuffer = nullptr; |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 232 | ANGLE_TRY_VK_ALLOCATION(context->getScratchBuffer(bufferSize, &memoryBuffer)); |
Luc Ferron | daf7ace | 2018-05-14 13:44:15 -0400 | [diff] [blame] | 233 | |
| 234 | // Read into the scratch buffer |
Luc Ferron | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 235 | ANGLE_TRY( |
| 236 | framebufferVk->readPixelsImpl(context, clippedRectangle, params, 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 | aa2126c | 2018-07-09 15:36:36 -0400 | [diff] [blame] | 246 | ANGLE_TRY(framebufferVk->readPixelsImpl(context, clippedRectangle, params, stagingPointer)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 247 | } |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 248 | |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 249 | // 3- enqueue the destination image subresource update |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 250 | VkBufferImageCopy copyToImage; |
| 251 | copyToImage.bufferOffset = static_cast<VkDeviceSize>(stagingOffset); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 252 | copyToImage.bufferRowLength = 0; // Tightly packed data can be specified as 0. |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 253 | copyToImage.bufferImageHeight = clippedRectangle.height; |
| 254 | copyToImage.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 255 | copyToImage.imageSubresource.mipLevel = index.getLevelIndex(); |
| 256 | copyToImage.imageSubresource.baseArrayLayer = index.hasLayer() ? index.getLayerIndex() : 0; |
| 257 | copyToImage.imageSubresource.layerCount = index.getLayerCount(); |
| 258 | gl_vk::GetOffset(dstOffset, ©ToImage.imageOffset); |
| 259 | gl_vk::GetExtent(dstExtent, ©ToImage.imageExtent); |
| 260 | |
| 261 | // 3- enqueue the destination image subresource update |
| 262 | mSubresourceUpdates.emplace_back(bufferHandle, copyToImage); |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 263 | return vk::NoError(); |
Luc Ferron | 2a849bf | 2018-05-10 13:19:11 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 266 | vk::Error PixelBuffer::allocate(RendererVk *renderer, |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 267 | size_t sizeInBytes, |
| 268 | uint8_t **ptrOut, |
| 269 | VkBuffer *handleOut, |
| 270 | uint32_t *offsetOut, |
| 271 | bool *newBufferAllocatedOut) |
| 272 | { |
| 273 | return mStagingBuffer.allocate(renderer, sizeInBytes, ptrOut, handleOut, offsetOut, |
| 274 | newBufferAllocatedOut); |
| 275 | } |
| 276 | |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 277 | vk::Error PixelBuffer::flushUpdatesToImage(RendererVk *renderer, |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 278 | uint32_t levelCount, |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 279 | vk::ImageHelper *image, |
| 280 | vk::CommandBuffer *commandBuffer) |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 281 | { |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 282 | if (mSubresourceUpdates.empty()) |
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 | return vk::NoError(); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 287 | ANGLE_TRY(mStagingBuffer.flush(renderer->getDevice())); |
| 288 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 289 | std::vector<SubresourceUpdate> updatesToKeep; |
| 290 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 291 | for (const SubresourceUpdate &update : mSubresourceUpdates) |
| 292 | { |
| 293 | ASSERT(update.bufferHandle != VK_NULL_HANDLE); |
Luc Ferron | 1a186b1 | 2018-04-24 15:25:35 -0400 | [diff] [blame] | 294 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 295 | const uint32_t updateMipLevel = update.copyRegion.imageSubresource.mipLevel; |
| 296 | // It's possible we've accumulated updates that are no longer applicable if the image has |
| 297 | // never been flushed but the image description has changed. Check if this level exist for |
| 298 | // this image. |
| 299 | if (updateMipLevel >= levelCount) |
| 300 | { |
| 301 | updatesToKeep.emplace_back(update); |
| 302 | continue; |
| 303 | } |
| 304 | |
Luc Ferron | 1a186b1 | 2018-04-24 15:25:35 -0400 | [diff] [blame] | 305 | // Conservatively flush all writes to the image. We could use a more restricted barrier. |
| 306 | // Do not move this above the for loop, otherwise multiple updates can have race conditions |
| 307 | // and not be applied correctly as seen i: |
| 308 | // dEQP-gles2.functional_texture_specification_texsubimage2d_align_2d* tests on Windows AMD |
| 309 | image->changeLayoutWithStages( |
| 310 | VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 311 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, commandBuffer); |
| 312 | |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 313 | commandBuffer->copyBufferToImage(update.bufferHandle, image->getImage(), |
| 314 | image->getCurrentLayout(), 1, &update.copyRegion); |
| 315 | } |
| 316 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 317 | // Only remove the updates that were actually applied to the image. |
| 318 | mSubresourceUpdates = std::move(updatesToKeep); |
| 319 | |
| 320 | if (mSubresourceUpdates.empty()) |
| 321 | { |
| 322 | mStagingBuffer.releaseRetainedBuffers(renderer); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | WARN() << "Internal Vulkan bufffer could not be released. This is likely due to having " |
| 327 | "extra images defined in the Texture."; |
| 328 | } |
Jamie Madill | 20fa8d5 | 2018-04-15 10:09:32 -0400 | [diff] [blame] | 329 | |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 330 | return vk::NoError(); |
| 331 | } |
| 332 | |
Luc Ferron | 10434f6 | 2018-04-24 10:06:37 -0400 | [diff] [blame] | 333 | bool PixelBuffer::empty() const |
| 334 | { |
| 335 | return mSubresourceUpdates.empty(); |
| 336 | } |
| 337 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 338 | vk::Error PixelBuffer::stageSubresourceUpdateAndGetData(RendererVk *renderer, |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 339 | size_t allocationSize, |
| 340 | const gl::ImageIndex &imageIndex, |
| 341 | const gl::Extents &extents, |
| 342 | const gl::Offset &offset, |
| 343 | uint8_t **destData) |
| 344 | { |
| 345 | VkBuffer bufferHandle; |
| 346 | uint32_t stagingOffset = 0; |
| 347 | bool newBufferAllocated = false; |
| 348 | ANGLE_TRY(mStagingBuffer.allocate(renderer, allocationSize, destData, &bufferHandle, |
| 349 | &stagingOffset, &newBufferAllocated)); |
| 350 | |
| 351 | VkBufferImageCopy copy; |
| 352 | copy.bufferOffset = static_cast<VkDeviceSize>(stagingOffset); |
| 353 | copy.bufferRowLength = extents.width; |
| 354 | copy.bufferImageHeight = extents.height; |
| 355 | copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 356 | copy.imageSubresource.mipLevel = imageIndex.getLevelIndex(); |
| 357 | copy.imageSubresource.baseArrayLayer = imageIndex.hasLayer() ? imageIndex.getLayerIndex() : 0; |
| 358 | copy.imageSubresource.layerCount = imageIndex.getLayerCount(); |
| 359 | |
| 360 | gl_vk::GetOffset(offset, ©.imageOffset); |
| 361 | gl_vk::GetExtent(extents, ©.imageExtent); |
| 362 | |
| 363 | mSubresourceUpdates.emplace_back(bufferHandle, copy); |
| 364 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 365 | return vk::NoError(); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 368 | vk::Error TextureVk::generateMipmapLevelsWithCPU(ContextVk *contextVk, |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 369 | const angle::Format &sourceFormat, |
| 370 | GLuint layer, |
| 371 | GLuint firstMipLevel, |
| 372 | GLuint maxMipLevel, |
| 373 | const size_t sourceWidth, |
| 374 | const size_t sourceHeight, |
| 375 | const size_t sourceRowPitch, |
| 376 | uint8_t *sourceData) |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 377 | { |
| 378 | RendererVk *renderer = contextVk->getRenderer(); |
| 379 | |
| 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( |
| 399 | renderer, 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 | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 415 | return vk::NoError(); |
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 | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 432 | : TextureImpl(state), mRenderTarget(&mImage, &mBaseLevelImageView, this), mPixelBuffer(renderer) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 433 | { |
| 434 | } |
| 435 | |
| 436 | TextureVk::~TextureVk() |
| 437 | { |
| 438 | } |
| 439 | |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 440 | gl::Error TextureVk::onDestroy(const gl::Context *context) |
| 441 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 442 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 443 | RendererVk *renderer = contextVk->getRenderer(); |
| 444 | |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 445 | releaseImage(context, renderer); |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 446 | renderer->releaseObject(getStoredQueueSerial(), &mSampler); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 447 | |
Jamie Madill | a7be1f7 | 2018-04-13 15:16:26 -0400 | [diff] [blame] | 448 | mPixelBuffer.release(renderer); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 449 | return gl::NoError(); |
| 450 | } |
| 451 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 452 | gl::Error TextureVk::setImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 453 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 454 | GLenum internalFormat, |
| 455 | const gl::Extents &size, |
| 456 | GLenum format, |
| 457 | GLenum type, |
| 458 | const gl::PixelUnpackState &unpack, |
| 459 | const uint8_t *pixels) |
| 460 | { |
Jamie Madill | e1f3ad4 | 2017-10-28 23:00:42 -0400 | [diff] [blame] | 461 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 1b03824 | 2017-11-01 15:14:36 -0400 | [diff] [blame] | 462 | RendererVk *renderer = contextVk->getRenderer(); |
| 463 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 464 | // If there is any staged changes for this index, we can remove them since we're going to |
| 465 | // override them with this call. |
| 466 | mPixelBuffer.removeStagedUpdates(index); |
| 467 | |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 468 | // Convert internalFormat to sized internal format. |
| 469 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat, type); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 470 | |
Jamie Madill | 1b03824 | 2017-11-01 15:14:36 -0400 | [diff] [blame] | 471 | if (mImage.valid()) |
| 472 | { |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 473 | const vk::Format &vkFormat = renderer->getFormat(formatInfo.sizedInternalFormat); |
Luc Ferron | 9096836 | 2018-05-04 08:47:22 -0400 | [diff] [blame] | 474 | |
| 475 | // Calculate the expected size for the index we are defining. If the size is different from |
| 476 | // the given size, or the format is different, we are redefining the image so we must |
| 477 | // release it. |
| 478 | if (mImage.getFormat() != vkFormat || size != mImage.getSize(index)) |
Jamie Madill | 1b03824 | 2017-11-01 15:14:36 -0400 | [diff] [blame] | 479 | { |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 480 | releaseImage(context, renderer); |
Jamie Madill | 1b03824 | 2017-11-01 15:14:36 -0400 | [diff] [blame] | 481 | } |
| 482 | } |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 483 | |
Geoff Lang | bd6ae4a | 2018-01-29 15:51:18 -0500 | [diff] [blame] | 484 | // Early-out on empty textures, don't create a zero-sized storage. |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 485 | if (size.empty()) |
Geoff Lang | bd6ae4a | 2018-01-29 15:51:18 -0500 | [diff] [blame] | 486 | { |
| 487 | return gl::NoError(); |
| 488 | } |
| 489 | |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 490 | // Create a new graph node to store image initialization commands. |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 491 | onResourceChanged(renderer); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 492 | |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 493 | // Handle initial data. |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 494 | if (pixels) |
| 495 | { |
Luc Ferron | 33e05ba | 2018-04-23 15:12:34 -0400 | [diff] [blame] | 496 | ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate(contextVk, index, size, gl::Offset(), |
| 497 | formatInfo, unpack, type, pixels)); |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 501 | } |
| 502 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 503 | gl::Error TextureVk::setSubImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 504 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 505 | const gl::Box &area, |
| 506 | GLenum format, |
| 507 | GLenum type, |
| 508 | const gl::PixelUnpackState &unpack, |
| 509 | const uint8_t *pixels) |
| 510 | { |
Jamie Madill | 5b18f48 | 2017-11-30 17:24:22 -0500 | [diff] [blame] | 511 | ContextVk *contextVk = vk::GetImpl(context); |
| 512 | const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type); |
Luc Ferron | 33e05ba | 2018-04-23 15:12:34 -0400 | [diff] [blame] | 513 | ANGLE_TRY(mPixelBuffer.stageSubresourceUpdate( |
| 514 | contextVk, index, gl::Extents(area.width, area.height, area.depth), |
| 515 | gl::Offset(area.x, area.y, area.z), formatInfo, unpack, type, pixels)); |
Jamie Madill | b221486 | 2018-04-26 07:25:48 -0400 | [diff] [blame] | 516 | |
| 517 | // Create a new graph node to store image initialization commands. |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 518 | onResourceChanged(contextVk->getRenderer()); |
Jamie Madill | b221486 | 2018-04-26 07:25:48 -0400 | [diff] [blame] | 519 | |
Jamie Madill | 5b18f48 | 2017-11-30 17:24:22 -0500 | [diff] [blame] | 520 | return gl::NoError(); |
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::setCompressedImage(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 | GLenum internalFormat, |
| 526 | const gl::Extents &size, |
| 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::setCompressedSubImage(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::Box &area, |
| 538 | GLenum format, |
| 539 | const gl::PixelUnpackState &unpack, |
| 540 | size_t imageSize, |
| 541 | const uint8_t *pixels) |
| 542 | { |
| 543 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 544 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 545 | } |
| 546 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 547 | gl::Error TextureVk::copyImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 548 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 549 | const gl::Rectangle &sourceArea, |
| 550 | GLenum internalFormat, |
Jamie Madill | 690c8eb | 2018-03-12 15:20:03 -0400 | [diff] [blame] | 551 | gl::Framebuffer *source) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 552 | { |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 553 | gl::Extents newImageSize(sourceArea.width, sourceArea.height, 1); |
| 554 | const gl::InternalFormat &internalFormatInfo = |
| 555 | gl::GetInternalFormatInfo(internalFormat, GL_UNSIGNED_BYTE); |
| 556 | ANGLE_TRY(setImage(context, index, internalFormat, newImageSize, internalFormatInfo.format, |
| 557 | internalFormatInfo.type, gl::PixelUnpackState(), nullptr)); |
| 558 | return copySubImageImpl(context, index, gl::Offset(0, 0, 0), sourceArea, internalFormatInfo, |
| 559 | source); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 562 | gl::Error TextureVk::copySubImage(const gl::Context *context, |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 563 | const gl::ImageIndex &index, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 564 | const gl::Offset &destOffset, |
| 565 | const gl::Rectangle &sourceArea, |
Jamie Madill | 690c8eb | 2018-03-12 15:20:03 -0400 | [diff] [blame] | 566 | gl::Framebuffer *source) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 567 | { |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 568 | const gl::InternalFormat ¤tFormat = *mState.getBaseLevelDesc().format.info; |
| 569 | return copySubImageImpl(context, index, destOffset, sourceArea, currentFormat, source); |
| 570 | } |
| 571 | |
| 572 | gl::Error TextureVk::copySubImageImpl(const gl::Context *context, |
| 573 | const gl::ImageIndex &index, |
| 574 | const gl::Offset &destOffset, |
| 575 | const gl::Rectangle &sourceArea, |
| 576 | const gl::InternalFormat &internalFormat, |
| 577 | gl::Framebuffer *source) |
| 578 | { |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 579 | gl::Extents fbSize = source->getReadColorbuffer()->getSize(); |
| 580 | gl::Rectangle clippedSourceArea; |
| 581 | if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, fbSize.width, fbSize.height), |
| 582 | &clippedSourceArea)) |
| 583 | { |
| 584 | return gl::NoError(); |
| 585 | } |
| 586 | |
| 587 | const gl::Offset modifiedDestOffset(destOffset.x + sourceArea.x - sourceArea.x, |
| 588 | destOffset.y + sourceArea.y - sourceArea.y, 0); |
| 589 | |
| 590 | ContextVk *contextVk = vk::GetImpl(context); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 591 | RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 592 | FramebufferVk *framebufferVk = vk::GetImpl(source); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 593 | |
| 594 | // For now, favor conformance. We do a CPU readback that does the conversion, and then stage the |
| 595 | // change to the pixel buffer. |
| 596 | // Eventually we can improve this easily by implementing vkCmdBlitImage to do the conversion |
| 597 | // when its supported. |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 598 | ANGLE_TRY(mPixelBuffer.stageSubresourceUpdateFromFramebuffer( |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 599 | context, index, clippedSourceArea, modifiedDestOffset, |
Luc Ferron | f299a37 | 2018-05-14 14:44:54 -0400 | [diff] [blame] | 600 | gl::Extents(clippedSourceArea.width, clippedSourceArea.height, 1), internalFormat, |
Jamie Madill | 5867501 | 2018-05-22 14:54:07 -0400 | [diff] [blame] | 601 | framebufferVk)); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 602 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 603 | onResourceChanged(renderer); |
| 604 | framebufferVk->addReadDependency(this); |
Luc Ferron | 018709f | 2018-05-10 13:53:11 -0400 | [diff] [blame] | 605 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 606 | } |
| 607 | |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 608 | vk::Error TextureVk::getCommandBufferForWrite(RendererVk *renderer, |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 609 | vk::CommandBuffer **commandBufferOut) |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 610 | { |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 611 | ANGLE_TRY(appendWriteResource(renderer, commandBufferOut)); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 612 | return vk::NoError(); |
| 613 | } |
| 614 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 615 | gl::Error TextureVk::setStorage(const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 616 | gl::TextureType type, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 617 | size_t levels, |
| 618 | GLenum internalFormat, |
| 619 | const gl::Extents &size) |
| 620 | { |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 621 | ContextVk *contextVk = GetAs<ContextVk>(context->getImplementation()); |
| 622 | RendererVk *renderer = contextVk->getRenderer(); |
| 623 | const vk::Format &format = renderer->getFormat(internalFormat); |
| 624 | vk::CommandBuffer *commandBuffer = nullptr; |
| 625 | ANGLE_TRY(getCommandBufferForWrite(renderer, &commandBuffer)); |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 626 | ANGLE_TRY(initImage(contextVk, format, size, static_cast<uint32_t>(levels), commandBuffer)); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 627 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 628 | } |
| 629 | |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 630 | gl::Error TextureVk::setEGLImageTarget(const gl::Context *context, |
| 631 | gl::TextureType type, |
| 632 | egl::Image *image) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 633 | { |
| 634 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 635 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 636 | } |
| 637 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 638 | gl::Error TextureVk::setImageExternal(const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 639 | gl::TextureType type, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 640 | egl::Stream *stream, |
| 641 | const egl::Stream::GLTextureDescription &desc) |
| 642 | { |
| 643 | UNIMPLEMENTED(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 644 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 645 | } |
| 646 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 647 | vk::Error TextureVk::generateMipmapWithBlit(RendererVk *renderer) |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 648 | { |
| 649 | uint32_t imageLayerCount = GetImageLayerCount(mState.getType()); |
| 650 | const gl::Extents baseLevelExtents = mImage.getExtents(); |
| 651 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 652 | ANGLE_TRY(getCommandBufferForWrite(renderer, &commandBuffer)); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 653 | |
| 654 | // We are able to use blitImage since the image format we are using supports it. This |
| 655 | // is a faster way we can generate the mips. |
| 656 | int32_t mipWidth = baseLevelExtents.width; |
| 657 | int32_t mipHeight = baseLevelExtents.height; |
| 658 | |
| 659 | // Manually manage the image memory barrier because it uses a lot more parameters than our |
| 660 | // usual one. |
| 661 | VkImageMemoryBarrier barrier; |
| 662 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 663 | barrier.image = mImage.getImage().getHandle(); |
| 664 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 665 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 666 | barrier.pNext = nullptr; |
| 667 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 668 | barrier.subresourceRange.baseArrayLayer = 0; |
| 669 | barrier.subresourceRange.layerCount = imageLayerCount; |
| 670 | barrier.subresourceRange.levelCount = 1; |
| 671 | |
| 672 | for (uint32_t mipLevel = 1; mipLevel <= mState.getMipmapMaxLevel(); mipLevel++) |
| 673 | { |
| 674 | int32_t nextMipWidth = std::max<int32_t>(1, mipWidth >> 1); |
| 675 | int32_t nextMipHeight = std::max<int32_t>(1, mipHeight >> 1); |
| 676 | |
| 677 | barrier.subresourceRange.baseMipLevel = mipLevel - 1; |
| 678 | barrier.oldLayout = mImage.getCurrentLayout(); |
| 679 | barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 680 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 681 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 682 | |
| 683 | // We can do it for all layers at once. |
| 684 | commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 685 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier); |
| 686 | |
| 687 | VkImageBlit blit = {}; |
| 688 | blit.srcOffsets[0] = {0, 0, 0}; |
| 689 | blit.srcOffsets[1] = {mipWidth, mipHeight, 1}; |
| 690 | blit.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 691 | blit.srcSubresource.mipLevel = mipLevel - 1; |
| 692 | blit.srcSubresource.baseArrayLayer = 0; |
| 693 | blit.srcSubresource.layerCount = imageLayerCount; |
| 694 | blit.dstOffsets[0] = {0, 0, 0}; |
| 695 | blit.dstOffsets[1] = {nextMipWidth, nextMipHeight, 1}; |
| 696 | blit.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 697 | blit.dstSubresource.mipLevel = mipLevel; |
| 698 | blit.dstSubresource.baseArrayLayer = 0; |
| 699 | blit.dstSubresource.layerCount = imageLayerCount; |
| 700 | |
| 701 | mipWidth = nextMipWidth; |
| 702 | mipHeight = nextMipHeight; |
| 703 | |
| 704 | commandBuffer->blitImage(mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 705 | mImage.getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, |
| 706 | VK_FILTER_LINEAR); |
| 707 | } |
| 708 | |
| 709 | // Transition the last mip level to the same layout as all the other ones, so we can declare |
| 710 | // our whole image layout to be SRC_OPTIMAL. |
| 711 | barrier.subresourceRange.baseMipLevel = mState.getMipmapMaxLevel(); |
| 712 | barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 713 | barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 714 | |
| 715 | // We can do it for all layers at once. |
| 716 | commandBuffer->singleImageBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 717 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, barrier); |
| 718 | |
| 719 | // This is just changing the internal state of the image helper so that the next call |
| 720 | // to changeLayoutWithStages will use this layout as the "oldLayout" argument. |
| 721 | mImage.updateLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 722 | |
| 723 | return vk::NoError(); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 724 | } |
| 725 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 726 | vk::Error TextureVk::generateMipmapWithCPU(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 727 | { |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 728 | ContextVk *contextVk = vk::GetImpl(context); |
| 729 | RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 730 | |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 731 | bool newBufferAllocated = false; |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 732 | const gl::Extents baseLevelExtents = mImage.getExtents(); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 733 | uint32_t imageLayerCount = GetImageLayerCount(mState.getType()); |
| 734 | const angle::Format &angleFormat = mImage.getFormat().textureFormat(); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 735 | GLuint sourceRowPitch = baseLevelExtents.width * angleFormat.pixelBytes; |
| 736 | size_t baseLevelAllocationSize = sourceRowPitch * baseLevelExtents.height; |
| 737 | |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 738 | vk::CommandBuffer *commandBuffer = nullptr; |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 739 | ANGLE_TRY(getCommandBufferForWrite(renderer, &commandBuffer)); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 740 | |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 741 | // Requirement of the copyImageToBuffer, the source image must be in SRC_OPTIMAL layout. |
| 742 | mImage.changeLayoutWithStages(VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 743 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 744 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, commandBuffer); |
| 745 | |
| 746 | size_t totalAllocationSize = baseLevelAllocationSize * imageLayerCount; |
| 747 | |
| 748 | VkBuffer copyBufferHandle; |
| 749 | uint8_t *baseLevelBuffers; |
| 750 | uint32_t copyBaseOffset; |
| 751 | |
| 752 | // Allocate enough memory to copy every level 0 image (one for each layer of the texture). |
| 753 | ANGLE_TRY(mPixelBuffer.allocate(renderer, totalAllocationSize, &baseLevelBuffers, |
| 754 | ©BufferHandle, ©BaseOffset, &newBufferAllocated)); |
| 755 | |
| 756 | // Do only one copy for all layers at once. |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 757 | VkBufferImageCopy region; |
| 758 | region.bufferImageHeight = baseLevelExtents.height; |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 759 | region.bufferOffset = static_cast<VkDeviceSize>(copyBaseOffset); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 760 | region.bufferRowLength = baseLevelExtents.width; |
| 761 | region.imageExtent.width = baseLevelExtents.width; |
| 762 | region.imageExtent.height = baseLevelExtents.height; |
| 763 | region.imageExtent.depth = 1; |
| 764 | region.imageOffset.x = 0; |
| 765 | region.imageOffset.y = 0; |
| 766 | region.imageOffset.z = 0; |
| 767 | region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 768 | region.imageSubresource.baseArrayLayer = 0; |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 769 | region.imageSubresource.layerCount = imageLayerCount; |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 770 | region.imageSubresource.mipLevel = mState.getEffectiveBaseLevel(); |
| 771 | |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 772 | commandBuffer->copyImageToBuffer(mImage.getImage(), mImage.getCurrentLayout(), copyBufferHandle, |
| 773 | 1, ®ion); |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 774 | |
| 775 | ANGLE_TRY(renderer->finish(context)); |
| 776 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 777 | const uint32_t levelCount = getLevelCount(); |
| 778 | |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 779 | // We now have the base level available to be manipulated in the baseLevelBuffer pointer. |
| 780 | // Generate all the missing mipmaps with the slow path. We can optimize with vkCmdBlitImage |
| 781 | // later. |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 782 | // For each layer, use the copied data to generate all the mips. |
| 783 | for (GLuint layer = 0; layer < imageLayerCount; layer++) |
| 784 | { |
| 785 | size_t bufferOffset = layer * baseLevelAllocationSize; |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 786 | |
| 787 | ANGLE_TRY(generateMipmapLevelsWithCPU( |
Luc Ferron | 22695bf | 2018-05-22 15:52:08 -0400 | [diff] [blame] | 788 | contextVk, angleFormat, layer, mState.getEffectiveBaseLevel() + 1, |
| 789 | mState.getMipmapMaxLevel(), baseLevelExtents.width, baseLevelExtents.height, |
| 790 | sourceRowPitch, baseLevelBuffers + bufferOffset)); |
| 791 | } |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 792 | |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 793 | return mPixelBuffer.flushUpdatesToImage(renderer, levelCount, &mImage, commandBuffer); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | gl::Error TextureVk::generateMipmap(const gl::Context *context) |
| 797 | { |
| 798 | ContextVk *contextVk = vk::GetImpl(context); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 799 | |
| 800 | // Some data is pending, or the image has not been defined at all yet |
| 801 | if (!mImage.valid()) |
| 802 | { |
| 803 | // lets initialize the image so we can generate the next levels. |
| 804 | if (!mPixelBuffer.empty()) |
| 805 | { |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 806 | ANGLE_TRY(ensureImageInitialized(contextVk)); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 807 | ASSERT(mImage.valid()); |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | // There is nothing to generate if there is nothing uploaded so far. |
| 812 | return gl::NoError(); |
| 813 | } |
| 814 | } |
| 815 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 816 | RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 817 | VkFormatProperties imageProperties; |
| 818 | vk::GetFormatProperties(renderer->getPhysicalDevice(), mImage.getFormat().vkTextureFormat, |
| 819 | &imageProperties); |
| 820 | |
| 821 | // Check if the image supports blit. If it does, we can do the mipmap generation on the gpu |
| 822 | // only. |
| 823 | if (IsMaskFlagSet(kBlitFeatureFlags, imageProperties.linearTilingFeatures)) |
| 824 | { |
Jamie Madill | eebe219 | 2018-07-11 09:01:18 -0400 | [diff] [blame^] | 825 | ANGLE_TRY(generateMipmapWithBlit(renderer)); |
Luc Ferron | 05cd6df | 2018-05-24 15:51:29 -0400 | [diff] [blame] | 826 | } |
| 827 | else |
| 828 | { |
| 829 | ANGLE_TRY(generateMipmapWithCPU(context)); |
| 830 | } |
| 831 | |
| 832 | // We're changing this textureVk content, make sure we let the graph know. |
| 833 | onResourceChanged(renderer); |
| 834 | |
Luc Ferron | c518170 | 2018-05-17 09:44:42 -0400 | [diff] [blame] | 835 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 836 | } |
| 837 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 838 | gl::Error TextureVk::setBaseLevel(const gl::Context *context, GLuint baseLevel) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 839 | { |
| 840 | UNIMPLEMENTED(); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 841 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 842 | } |
| 843 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 844 | gl::Error TextureVk::bindTexImage(const gl::Context *context, egl::Surface *surface) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 845 | { |
| 846 | UNIMPLEMENTED(); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 847 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 848 | } |
| 849 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 850 | gl::Error TextureVk::releaseTexImage(const gl::Context *context) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 851 | { |
| 852 | UNIMPLEMENTED(); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 853 | return gl::InternalError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 854 | } |
| 855 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 856 | gl::Error TextureVk::getAttachmentRenderTarget(const gl::Context *context, |
| 857 | GLenum binding, |
Jamie Madill | 4fd95d5 | 2017-04-05 11:22:18 -0400 | [diff] [blame] | 858 | const gl::ImageIndex &imageIndex, |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 859 | FramebufferAttachmentRenderTarget **rtOut) |
| 860 | { |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 861 | // TODO(jmadill): Handle cube textures. http://anglebug.com/2470 |
Jamie Madill | cc12937 | 2018-04-12 09:13:18 -0400 | [diff] [blame] | 862 | ASSERT(imageIndex.getType() == gl::TextureType::_2D); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 863 | |
| 864 | // Non-zero mip level attachments are an ES 3.0 feature. |
Jamie Madill | cc12937 | 2018-04-12 09:13:18 -0400 | [diff] [blame] | 865 | ASSERT(imageIndex.getLevelIndex() == 0 && !imageIndex.hasLayer()); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 866 | |
| 867 | ContextVk *contextVk = vk::GetImpl(context); |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 868 | ANGLE_TRY(ensureImageInitialized(contextVk)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 869 | |
Jamie Madill | b79e7bb | 2017-10-24 13:55:50 -0400 | [diff] [blame] | 870 | *rtOut = &mRenderTarget; |
| 871 | return gl::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 872 | } |
| 873 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 874 | vk::Error TextureVk::ensureImageInitialized(ContextVk *contextVk) |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 875 | { |
Luc Ferron | 10434f6 | 2018-04-24 10:06:37 -0400 | [diff] [blame] | 876 | if (mImage.valid() && mPixelBuffer.empty()) |
| 877 | { |
| 878 | return vk::NoError(); |
| 879 | } |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 880 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 881 | vk::CommandBuffer *commandBuffer = nullptr; |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 882 | ANGLE_TRY(getCommandBufferForWrite(renderer, &commandBuffer)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 883 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 884 | const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc(); |
| 885 | const gl::Extents &baseLevelExtents = baseLevelDesc.size; |
| 886 | const uint32_t levelCount = getLevelCount(); |
| 887 | |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 888 | if (!mImage.valid()) |
| 889 | { |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 890 | const vk::Format &format = |
| 891 | renderer->getFormat(baseLevelDesc.format.info->sizedInternalFormat); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 892 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 893 | ANGLE_TRY(initImage(contextVk, format, baseLevelExtents, levelCount, commandBuffer)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 894 | } |
| 895 | |
Luc Ferron | 2f3f414 | 2018-05-30 08:27:19 -0400 | [diff] [blame] | 896 | ANGLE_TRY(mPixelBuffer.flushUpdatesToImage(renderer, levelCount, &mImage, commandBuffer)); |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 897 | return vk::NoError(); |
| 898 | } |
| 899 | |
Luc Ferron | 4bba74f | 2018-04-19 14:40:45 -0400 | [diff] [blame] | 900 | 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] | 901 | { |
Luc Ferron | 2061090 | 2018-04-19 14:41:13 -0400 | [diff] [blame] | 902 | if (dirtyBits.none() && mSampler.valid()) |
| 903 | { |
| 904 | return gl::NoError(); |
| 905 | } |
| 906 | |
| 907 | ContextVk *contextVk = vk::GetImpl(context); |
| 908 | if (mSampler.valid()) |
| 909 | { |
| 910 | RendererVk *renderer = contextVk->getRenderer(); |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 911 | renderer->releaseObject(getStoredQueueSerial(), &mSampler); |
Luc Ferron | 2061090 | 2018-04-19 14:41:13 -0400 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | const gl::SamplerState &samplerState = mState.getSamplerState(); |
| 915 | |
| 916 | // Create a simple sampler. Force basic parameter settings. |
| 917 | VkSamplerCreateInfo samplerInfo; |
| 918 | samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; |
| 919 | samplerInfo.pNext = nullptr; |
| 920 | samplerInfo.flags = 0; |
| 921 | samplerInfo.magFilter = gl_vk::GetFilter(samplerState.magFilter); |
| 922 | samplerInfo.minFilter = gl_vk::GetFilter(samplerState.minFilter); |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 923 | samplerInfo.mipmapMode = gl_vk::GetSamplerMipmapMode(samplerState.minFilter); |
Luc Ferron | 2061090 | 2018-04-19 14:41:13 -0400 | [diff] [blame] | 924 | samplerInfo.addressModeU = gl_vk::GetSamplerAddressMode(samplerState.wrapS); |
| 925 | samplerInfo.addressModeV = gl_vk::GetSamplerAddressMode(samplerState.wrapT); |
| 926 | samplerInfo.addressModeW = gl_vk::GetSamplerAddressMode(samplerState.wrapR); |
| 927 | samplerInfo.mipLodBias = 0.0f; |
| 928 | samplerInfo.anisotropyEnable = VK_FALSE; |
| 929 | samplerInfo.maxAnisotropy = 1.0f; |
| 930 | samplerInfo.compareEnable = VK_FALSE; |
| 931 | samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS; |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 932 | samplerInfo.minLod = samplerState.minLod; |
| 933 | samplerInfo.maxLod = samplerState.maxLod; |
Luc Ferron | 2061090 | 2018-04-19 14:41:13 -0400 | [diff] [blame] | 934 | samplerInfo.borderColor = VK_BORDER_COLOR_INT_TRANSPARENT_BLACK; |
| 935 | samplerInfo.unnormalizedCoordinates = VK_FALSE; |
| 936 | |
| 937 | ANGLE_TRY(mSampler.init(contextVk->getDevice(), samplerInfo)); |
Luc Ferron | 4bba74f | 2018-04-19 14:40:45 -0400 | [diff] [blame] | 938 | return gl::NoError(); |
Geoff Lang | 2241686 | 2016-06-08 16:14:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 941 | gl::Error TextureVk::setStorageMultisample(const gl::Context *context, |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 942 | gl::TextureType type, |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 943 | GLsizei samples, |
| 944 | GLint internalformat, |
| 945 | const gl::Extents &size, |
Geoff Lang | 9201943 | 2017-11-20 13:09:34 -0500 | [diff] [blame] | 946 | bool fixedSampleLocations) |
JiangYizhou | bddc46b | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 947 | { |
| 948 | UNIMPLEMENTED(); |
| 949 | return gl::InternalError() << "setStorageMultisample is unimplemented."; |
| 950 | } |
| 951 | |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 952 | gl::Error TextureVk::initializeContents(const gl::Context *context, |
| 953 | const gl::ImageIndex &imageIndex) |
| 954 | { |
| 955 | UNIMPLEMENTED(); |
| 956 | return gl::NoError(); |
| 957 | } |
| 958 | |
Jamie Madill | 858c1cc | 2018-03-31 14:19:13 -0400 | [diff] [blame] | 959 | const vk::ImageHelper &TextureVk::getImage() const |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 960 | { |
| 961 | ASSERT(mImage.valid()); |
Jamie Madill | 858c1cc | 2018-03-31 14:19:13 -0400 | [diff] [blame] | 962 | return mImage; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | const vk::ImageView &TextureVk::getImageView() const |
| 966 | { |
Jamie Madill | 93edca1 | 2018-03-30 10:43:18 -0400 | [diff] [blame] | 967 | ASSERT(mImage.valid()); |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 968 | |
| 969 | const GLenum minFilter = mState.getSamplerState().minFilter; |
| 970 | if (minFilter == GL_LINEAR || minFilter == GL_NEAREST) |
| 971 | { |
| 972 | return mBaseLevelImageView; |
| 973 | } |
| 974 | |
| 975 | return mMipmapImageView; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | const vk::Sampler &TextureVk::getSampler() const |
| 979 | { |
| 980 | ASSERT(mSampler.valid()); |
| 981 | return mSampler; |
| 982 | } |
| 983 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 984 | vk::Error TextureVk::initImage(ContextVk *contextVk, |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 985 | const vk::Format &format, |
| 986 | const gl::Extents &extents, |
| 987 | const uint32_t levelCount, |
| 988 | vk::CommandBuffer *commandBuffer) |
| 989 | { |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 990 | const RendererVk *renderer = contextVk->getRenderer(); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 991 | const VkDevice device = renderer->getDevice(); |
| 992 | |
| 993 | const VkImageUsageFlags usage = |
| 994 | (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 995 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT); |
| 996 | |
| 997 | ANGLE_TRY(mImage.init(device, mState.getType(), extents, format, 1, usage, levelCount)); |
| 998 | |
| 999 | const VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 1000 | |
| 1001 | ANGLE_TRY(mImage.initMemory(device, renderer->getMemoryProperties(), flags)); |
| 1002 | |
| 1003 | gl::SwizzleState mappedSwizzle; |
| 1004 | MapSwizzleState(format.internalFormat, mState.getSwizzleState(), &mappedSwizzle); |
| 1005 | |
Luc Ferron | f6e160f | 2018-06-12 10:13:57 -0400 | [diff] [blame] | 1006 | // Renderable textures cannot have a swizzle. |
| 1007 | ASSERT(!contextVk->getTextureCaps().get(format.internalFormat).textureAttachment || |
| 1008 | !mappedSwizzle.swizzleRequired()); |
| 1009 | |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1010 | // TODO(jmadill): Separate imageviews for RenderTargets and Sampling. |
| 1011 | ANGLE_TRY(mImage.initImageView(device, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT, |
| 1012 | mappedSwizzle, &mMipmapImageView, levelCount)); |
| 1013 | ANGLE_TRY(mImage.initImageView(device, mState.getType(), VK_IMAGE_ASPECT_COLOR_BIT, |
| 1014 | mappedSwizzle, &mBaseLevelImageView, 1)); |
| 1015 | |
| 1016 | // 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] | 1017 | VkClearColorValue black = {{0, 0, 0, 1.0f}}; |
Luc Ferron | c20b950 | 2018-05-24 09:30:17 -0400 | [diff] [blame] | 1018 | mImage.clearColor(black, 0, levelCount, commandBuffer); |
Luc Ferron | fa7503c | 2018-05-08 11:25:06 -0400 | [diff] [blame] | 1019 | return vk::NoError(); |
| 1020 | } |
| 1021 | |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 1022 | void TextureVk::releaseImage(const gl::Context *context, RendererVk *renderer) |
| 1023 | { |
| 1024 | mImage.release(renderer->getCurrentQueueSerial(), renderer); |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 1025 | renderer->releaseObject(getStoredQueueSerial(), &mBaseLevelImageView); |
| 1026 | renderer->releaseObject(getStoredQueueSerial(), &mMipmapImageView); |
Jamie Madill | c4f27e4 | 2018-03-31 14:19:18 -0400 | [diff] [blame] | 1027 | onStateChange(context, angle::SubjectMessage::DEPENDENT_DIRTY_BITS); |
| 1028 | } |
| 1029 | |
Luc Ferron | 6641053 | 2018-04-20 12:47:45 -0400 | [diff] [blame] | 1030 | uint32_t TextureVk::getLevelCount() const |
| 1031 | { |
| 1032 | ASSERT(mState.getEffectiveBaseLevel() == 0); |
| 1033 | |
| 1034 | // getMipmapMaxLevel will be 0 here if mipmaps are not used, so the levelCount is always +1. |
| 1035 | return mState.getMipmapMaxLevel() + 1; |
| 1036 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1037 | } // namespace rx |