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