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