Yan, Shaobo | 93158eb | 2019-01-04 04:56:08 +0000 | [diff] [blame] | 1 | // Copyright 2019 The Dawn Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "dawn_native/vulkan/UtilsVulkan.h" |
| 16 | |
| 17 | #include "common/Assert.h" |
Natasha Lee | 5f8a8aa | 2019-08-14 23:11:42 +0000 | [diff] [blame] | 18 | #include "dawn_native/Format.h" |
| 19 | #include "dawn_native/vulkan/Forward.h" |
| 20 | #include "dawn_native/vulkan/TextureVk.h" |
Yan, Shaobo | 93158eb | 2019-01-04 04:56:08 +0000 | [diff] [blame] | 21 | |
| 22 | namespace dawn_native { namespace vulkan { |
| 23 | |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 24 | VkCompareOp ToVulkanCompareOp(wgpu::CompareFunction op) { |
Yan, Shaobo | 93158eb | 2019-01-04 04:56:08 +0000 | [diff] [blame] | 25 | switch (op) { |
Austin Eng | d6a5431 | 2020-04-17 19:32:07 +0000 | [diff] [blame] | 26 | case wgpu::CompareFunction::Never: |
| 27 | return VK_COMPARE_OP_NEVER; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 28 | case wgpu::CompareFunction::Less: |
Yan, Shaobo | 93158eb | 2019-01-04 04:56:08 +0000 | [diff] [blame] | 29 | return VK_COMPARE_OP_LESS; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 30 | case wgpu::CompareFunction::LessEqual: |
Yan, Shaobo | 93158eb | 2019-01-04 04:56:08 +0000 | [diff] [blame] | 31 | return VK_COMPARE_OP_LESS_OR_EQUAL; |
Austin Eng | d6a5431 | 2020-04-17 19:32:07 +0000 | [diff] [blame] | 32 | case wgpu::CompareFunction::Greater: |
| 33 | return VK_COMPARE_OP_GREATER; |
| 34 | case wgpu::CompareFunction::GreaterEqual: |
| 35 | return VK_COMPARE_OP_GREATER_OR_EQUAL; |
| 36 | case wgpu::CompareFunction::Equal: |
| 37 | return VK_COMPARE_OP_EQUAL; |
Corentin Wallez | 1f6c8c4 | 2019-10-23 11:57:41 +0000 | [diff] [blame] | 38 | case wgpu::CompareFunction::NotEqual: |
Yan, Shaobo | 93158eb | 2019-01-04 04:56:08 +0000 | [diff] [blame] | 39 | return VK_COMPARE_OP_NOT_EQUAL; |
Austin Eng | d6a5431 | 2020-04-17 19:32:07 +0000 | [diff] [blame] | 40 | case wgpu::CompareFunction::Always: |
| 41 | return VK_COMPARE_OP_ALWAYS; |
Yan, Shaobo | 93158eb | 2019-01-04 04:56:08 +0000 | [diff] [blame] | 42 | default: |
| 43 | UNREACHABLE(); |
| 44 | } |
| 45 | } |
| 46 | |
Natasha Lee | 5f8a8aa | 2019-08-14 23:11:42 +0000 | [diff] [blame] | 47 | // Vulkan SPEC requires the source/destination region specified by each element of |
| 48 | // pRegions must be a region that is contained within srcImage/dstImage. Here the size of |
| 49 | // the image refers to the virtual size, while Dawn validates texture copy extent with the |
| 50 | // physical size, so we need to re-calculate the texture copy extent to ensure it should fit |
| 51 | // in the virtual size of the subresource. |
| 52 | Extent3D ComputeTextureCopyExtent(const TextureCopy& textureCopy, const Extent3D& copySize) { |
| 53 | Extent3D validTextureCopyExtent = copySize; |
| 54 | const TextureBase* texture = textureCopy.texture.Get(); |
| 55 | Extent3D virtualSizeAtLevel = texture->GetMipLevelVirtualSize(textureCopy.mipLevel); |
| 56 | if (textureCopy.origin.x + copySize.width > virtualSizeAtLevel.width) { |
| 57 | ASSERT(texture->GetFormat().isCompressed); |
| 58 | validTextureCopyExtent.width = virtualSizeAtLevel.width - textureCopy.origin.x; |
| 59 | } |
| 60 | if (textureCopy.origin.y + copySize.height > virtualSizeAtLevel.height) { |
| 61 | ASSERT(texture->GetFormat().isCompressed); |
| 62 | validTextureCopyExtent.height = virtualSizeAtLevel.height - textureCopy.origin.y; |
| 63 | } |
| 64 | |
| 65 | return validTextureCopyExtent; |
| 66 | } |
| 67 | |
| 68 | VkBufferImageCopy ComputeBufferImageCopyRegion(const BufferCopy& bufferCopy, |
| 69 | const TextureCopy& textureCopy, |
| 70 | const Extent3D& copySize) { |
Tomek Ponitka | e28cc55 | 2020-07-16 09:08:41 +0000 | [diff] [blame] | 71 | TextureDataLayout passDataLayout; |
| 72 | passDataLayout.offset = bufferCopy.offset; |
| 73 | passDataLayout.rowsPerImage = bufferCopy.rowsPerImage; |
| 74 | passDataLayout.bytesPerRow = bufferCopy.bytesPerRow; |
| 75 | return ComputeBufferImageCopyRegion(passDataLayout, textureCopy, copySize); |
| 76 | } |
| 77 | |
| 78 | VkBufferImageCopy ComputeBufferImageCopyRegion(const TextureDataLayout& dataLayout, |
| 79 | const TextureCopy& textureCopy, |
| 80 | const Extent3D& copySize) { |
Natasha Lee | 5f8a8aa | 2019-08-14 23:11:42 +0000 | [diff] [blame] | 81 | const Texture* texture = ToBackend(textureCopy.texture.Get()); |
| 82 | |
| 83 | VkBufferImageCopy region; |
| 84 | |
Tomek Ponitka | e28cc55 | 2020-07-16 09:08:41 +0000 | [diff] [blame] | 85 | region.bufferOffset = dataLayout.offset; |
Natasha Lee | 5f8a8aa | 2019-08-14 23:11:42 +0000 | [diff] [blame] | 86 | // In Vulkan the row length is in texels while it is in bytes for Dawn |
| 87 | const Format& format = texture->GetFormat(); |
Tomek Ponitka | e28cc55 | 2020-07-16 09:08:41 +0000 | [diff] [blame] | 88 | ASSERT(dataLayout.bytesPerRow % format.blockByteSize == 0); |
| 89 | region.bufferRowLength = dataLayout.bytesPerRow / format.blockByteSize * format.blockWidth; |
| 90 | region.bufferImageHeight = dataLayout.rowsPerImage; |
Natasha Lee | 5f8a8aa | 2019-08-14 23:11:42 +0000 | [diff] [blame] | 91 | |
| 92 | region.imageSubresource.aspectMask = texture->GetVkAspectMask(); |
| 93 | region.imageSubresource.mipLevel = textureCopy.mipLevel; |
Natasha Lee | 5f8a8aa | 2019-08-14 23:11:42 +0000 | [diff] [blame] | 94 | |
Corentin Wallez | ff90599 | 2020-06-26 11:07:00 +0000 | [diff] [blame] | 95 | switch (textureCopy.texture->GetDimension()) { |
| 96 | case wgpu::TextureDimension::e2D: { |
| 97 | region.imageOffset.x = textureCopy.origin.x; |
| 98 | region.imageOffset.y = textureCopy.origin.y; |
| 99 | region.imageOffset.z = 0; |
Natasha Lee | 5f8a8aa | 2019-08-14 23:11:42 +0000 | [diff] [blame] | 100 | |
Corentin Wallez | ff90599 | 2020-06-26 11:07:00 +0000 | [diff] [blame] | 101 | region.imageSubresource.baseArrayLayer = textureCopy.origin.z; |
| 102 | region.imageSubresource.layerCount = copySize.depth; |
Jiawei Shao | 92379bf | 2020-06-21 09:33:44 +0000 | [diff] [blame] | 103 | |
Corentin Wallez | ff90599 | 2020-06-26 11:07:00 +0000 | [diff] [blame] | 104 | Extent3D imageExtent = ComputeTextureCopyExtent(textureCopy, copySize); |
| 105 | region.imageExtent.width = imageExtent.width; |
| 106 | region.imageExtent.height = imageExtent.height; |
| 107 | region.imageExtent.depth = 1; |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | default: |
| 112 | UNREACHABLE(); |
| 113 | break; |
| 114 | } |
Natasha Lee | 5f8a8aa | 2019-08-14 23:11:42 +0000 | [diff] [blame] | 115 | |
| 116 | return region; |
| 117 | } |
Tomek Ponitka | e28cc55 | 2020-07-16 09:08:41 +0000 | [diff] [blame] | 118 | }} // namespace dawn_native::vulkan |