Line data Source code
1 : /* Copyright (C) 2023 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "Utilities.h"
21 :
22 : #include "lib/code_annotation.h"
23 : #include "lib/config2.h"
24 : #include "renderer/backend/vulkan/Buffer.h"
25 : #include "renderer/backend/vulkan/Texture.h"
26 :
27 : namespace Renderer
28 : {
29 :
30 : namespace Backend
31 : {
32 :
33 : namespace Vulkan
34 : {
35 :
36 : namespace Utilities
37 : {
38 :
39 0 : void SetTextureLayout(
40 : VkCommandBuffer commandBuffer, CTexture* texture,
41 : const VkImageLayout oldLayout, const VkImageLayout newLayout,
42 : const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask,
43 : const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask)
44 : {
45 0 : ENSURE(texture->GetMIPLevelCount() == 1);
46 0 : ENSURE(texture->GetLayerCount() == 1);
47 :
48 0 : VkImageMemoryBarrier imageMemoryBarrier{};
49 0 : imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
50 0 : imageMemoryBarrier.image = texture->GetImage();
51 0 : imageMemoryBarrier.srcAccessMask = srcAccessMask;
52 0 : imageMemoryBarrier.dstAccessMask = dstAccessMask;
53 0 : imageMemoryBarrier.oldLayout = oldLayout;
54 0 : imageMemoryBarrier.newLayout = newLayout;
55 0 : imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
56 0 : imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
57 0 : imageMemoryBarrier.subresourceRange.aspectMask = texture->GetAttachmentImageAspectMask();
58 0 : imageMemoryBarrier.subresourceRange.baseMipLevel = 0;
59 0 : imageMemoryBarrier.subresourceRange.levelCount = texture->GetMIPLevelCount();
60 0 : imageMemoryBarrier.subresourceRange.baseArrayLayer = 0;
61 0 : imageMemoryBarrier.subresourceRange.layerCount = texture->GetLayerCount();
62 :
63 0 : vkCmdPipelineBarrier(commandBuffer,
64 : srcStageMask, dstStageMask, 0,
65 : 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier);
66 :
67 0 : texture->SetInitialized();
68 0 : }
69 :
70 0 : void SubmitImageMemoryBarrier(
71 : VkCommandBuffer commandBuffer, VkImage image, const uint32_t level, const uint32_t layer,
72 : const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask,
73 : const VkImageLayout oldLayout, const VkImageLayout newLayout,
74 : const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask,
75 : const VkImageAspectFlags aspectMask)
76 : {
77 0 : VkImageMemoryBarrier imageMemoryBarrier{};
78 0 : imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
79 0 : imageMemoryBarrier.image = image;
80 0 : imageMemoryBarrier.srcAccessMask = srcAccessMask;
81 0 : imageMemoryBarrier.dstAccessMask = dstAccessMask;
82 0 : imageMemoryBarrier.oldLayout = oldLayout;
83 0 : imageMemoryBarrier.newLayout = newLayout;
84 0 : imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
85 0 : imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
86 0 : imageMemoryBarrier.subresourceRange.aspectMask = aspectMask;
87 0 : imageMemoryBarrier.subresourceRange.baseMipLevel = level;
88 0 : imageMemoryBarrier.subresourceRange.levelCount = 1;
89 0 : imageMemoryBarrier.subresourceRange.baseArrayLayer = layer;
90 0 : imageMemoryBarrier.subresourceRange.layerCount = 1;
91 :
92 0 : vkCmdPipelineBarrier(commandBuffer,
93 : srcStageMask, dstStageMask, 0,
94 : 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier);
95 0 : }
96 :
97 0 : void SubmitBufferMemoryBarrier(
98 : VkCommandBuffer commandBuffer, CBuffer* buffer,
99 : const uint32_t offset, const uint32_t size,
100 : const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask,
101 : const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask)
102 : {
103 0 : VkBufferMemoryBarrier bufferMemoryBarrier{};
104 0 : bufferMemoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
105 0 : bufferMemoryBarrier.srcAccessMask = srcAccessMask;
106 0 : bufferMemoryBarrier.dstAccessMask = dstAccessMask;
107 0 : bufferMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
108 0 : bufferMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
109 0 : bufferMemoryBarrier.buffer = buffer->GetVkBuffer();
110 0 : bufferMemoryBarrier.offset = offset;
111 0 : bufferMemoryBarrier.size = size;
112 :
113 0 : vkCmdPipelineBarrier(
114 : commandBuffer, srcStageMask, dstStageMask, 0,
115 : 0, nullptr, 1, &bufferMemoryBarrier, 0, nullptr);
116 0 : }
117 :
118 0 : void SubmitMemoryBarrier(
119 : VkCommandBuffer commandBuffer,
120 : const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask,
121 : const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask)
122 : {
123 0 : VkMemoryBarrier memoryBarrier{};
124 0 : memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
125 0 : memoryBarrier.srcAccessMask = srcAccessMask;
126 0 : memoryBarrier.dstAccessMask = dstAccessMask;
127 0 : vkCmdPipelineBarrier(
128 : commandBuffer, srcStageMask, dstStageMask, 0,
129 : 1, &memoryBarrier, 0, nullptr, 0, nullptr);
130 0 : }
131 :
132 0 : void SubmitPipelineBarrier(
133 : VkCommandBuffer commandBuffer,
134 : const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask)
135 : {
136 0 : vkCmdPipelineBarrier(
137 : commandBuffer, srcStageMask, dstStageMask, 0,
138 : 0, nullptr, 0, nullptr, 0, nullptr);
139 0 : }
140 :
141 0 : void SubmitDebugSyncMemoryBarrier(VkCommandBuffer commandBuffer)
142 : {
143 0 : const VkAccessFlags accessMask =
144 : VK_ACCESS_INDIRECT_COMMAND_READ_BIT |
145 : VK_ACCESS_INDEX_READ_BIT |
146 : VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT |
147 : VK_ACCESS_UNIFORM_READ_BIT |
148 : VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
149 : VK_ACCESS_SHADER_READ_BIT |
150 : VK_ACCESS_SHADER_WRITE_BIT |
151 : VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
152 : VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
153 : VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
154 : VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
155 : VK_ACCESS_TRANSFER_READ_BIT |
156 : VK_ACCESS_TRANSFER_WRITE_BIT |
157 : VK_ACCESS_HOST_READ_BIT |
158 : VK_ACCESS_HOST_WRITE_BIT;
159 0 : SubmitMemoryBarrier(
160 : commandBuffer, accessMask, accessMask,
161 : VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
162 0 : }
163 :
164 : } // namespace Utilities
165 :
166 : } // namespace Vulkan
167 :
168 : } // namespace Backend
169 :
170 3 : } // namespace Renderer
|