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