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