1 // Copyright 2022 The Android Open Source Project
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 expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "BorrowedImageVk.h"
16
17 namespace gfxstream {
18 namespace vk {
19
addNeededBarriersToUseBorrowedImage(const BorrowedImageInfoVk & borrowedImageInfo,uint32_t usedQueueFamilyIndex,VkImageLayout usedInitialImageLayout,VkImageLayout usedFinalImageLayout,VkAccessFlags usedAccessMask,std::vector<VkImageMemoryBarrier> * preUseQueueTransferBarriers,std::vector<VkImageMemoryBarrier> * preUseLayoutTransitionBarriers,std::vector<VkImageMemoryBarrier> * postUseLayoutTransitionBarriers,std::vector<VkImageMemoryBarrier> * postUseQueueTransferBarriers)20 void addNeededBarriersToUseBorrowedImage(
21 const BorrowedImageInfoVk& borrowedImageInfo, uint32_t usedQueueFamilyIndex,
22 VkImageLayout usedInitialImageLayout, VkImageLayout usedFinalImageLayout,
23 VkAccessFlags usedAccessMask, std::vector<VkImageMemoryBarrier>* preUseQueueTransferBarriers,
24 std::vector<VkImageMemoryBarrier>* preUseLayoutTransitionBarriers,
25 std::vector<VkImageMemoryBarrier>* postUseLayoutTransitionBarriers,
26 std::vector<VkImageMemoryBarrier>* postUseQueueTransferBarriers) {
27 if (borrowedImageInfo.preBorrowQueueFamilyIndex != usedQueueFamilyIndex) {
28 const VkImageMemoryBarrier queueTransferBarrier = {
29 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
30 .pNext = nullptr,
31 .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
32 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
33 .oldLayout = borrowedImageInfo.preBorrowLayout,
34 .newLayout = borrowedImageInfo.preBorrowLayout,
35 .srcQueueFamilyIndex = borrowedImageInfo.preBorrowQueueFamilyIndex,
36 .dstQueueFamilyIndex = usedQueueFamilyIndex,
37 .image = borrowedImageInfo.image,
38 .subresourceRange =
39 {
40 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
41 .baseMipLevel = 0,
42 .levelCount = 1,
43 .baseArrayLayer = 0,
44 .layerCount = 1,
45 },
46 };
47 preUseQueueTransferBarriers->emplace_back(queueTransferBarrier);
48 }
49 if (borrowedImageInfo.preBorrowLayout != usedInitialImageLayout &&
50 usedInitialImageLayout != VK_IMAGE_LAYOUT_UNDEFINED) {
51 const VkImageMemoryBarrier layoutTransitionBarrier = {
52 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
53 .pNext = nullptr,
54 .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
55 .dstAccessMask = usedAccessMask,
56 .oldLayout = borrowedImageInfo.preBorrowLayout,
57 .newLayout = usedInitialImageLayout,
58 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
59 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
60 .image = borrowedImageInfo.image,
61 .subresourceRange =
62 {
63 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
64 .baseMipLevel = 0,
65 .levelCount = 1,
66 .baseArrayLayer = 0,
67 .layerCount = 1,
68 },
69 };
70 preUseLayoutTransitionBarriers->emplace_back(layoutTransitionBarrier);
71 }
72 if (borrowedImageInfo.postBorrowLayout != usedFinalImageLayout) {
73 const VkImageMemoryBarrier layoutTransitionBarrier = {
74 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
75 .pNext = nullptr,
76 .srcAccessMask = usedAccessMask,
77 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
78 .oldLayout = usedFinalImageLayout,
79 .newLayout = borrowedImageInfo.postBorrowLayout,
80 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
81 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
82 .image = borrowedImageInfo.image,
83 .subresourceRange =
84 {
85 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
86 .baseMipLevel = 0,
87 .levelCount = 1,
88 .baseArrayLayer = 0,
89 .layerCount = 1,
90 },
91 };
92 postUseLayoutTransitionBarriers->emplace_back(layoutTransitionBarrier);
93 }
94 if (borrowedImageInfo.postBorrowQueueFamilyIndex != usedQueueFamilyIndex) {
95 const VkImageMemoryBarrier queueTransferBarrier = {
96 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
97 .pNext = nullptr,
98 .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
99 .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
100 .oldLayout = borrowedImageInfo.postBorrowLayout,
101 .newLayout = borrowedImageInfo.postBorrowLayout,
102 .srcQueueFamilyIndex = usedQueueFamilyIndex,
103 .dstQueueFamilyIndex = borrowedImageInfo.postBorrowQueueFamilyIndex,
104 .image = borrowedImageInfo.image,
105 .subresourceRange =
106 {
107 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
108 .baseMipLevel = 0,
109 .levelCount = 1,
110 .baseArrayLayer = 0,
111 .layerCount = 1,
112 },
113 };
114 postUseQueueTransferBarriers->emplace_back(queueTransferBarrier);
115 }
116 }
117
118 } // namespace vk
119 } // namespace gfxstream
120