blob: 7f07a1d0b1b5d2d139656b4e60eb205bc485af0f [file] [log] [blame]
Mark Lobodzinskid42e4d22017-01-17 14:14:22 -07001/* Copyright (c) 2015-2017 The Khronos Group Inc.
2 * Copyright (c) 2015-2017 Valve Corporation
3 * Copyright (c) 2015-2017 LunarG, Inc.
4 * Copyright (C) 2015-2017 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Mark Lobodzinski <mark@lunarg.com>
Dave Houlton4eaaf3a2017-03-14 11:31:20 -060019 * Author: Dave Houlton <daveh@lunarg.com>
Mark Lobodzinskid42e4d22017-01-17 14:14:22 -070020 */
21
22// Allow use of STL min and max functions in Windows
23#define NOMINMAX
24
Mark Lobodzinski90224de2017-01-26 15:23:11 -070025#include <sstream>
26
27#include "vk_enum_string_helper.h"
28#include "vk_layer_data.h"
29#include "vk_layer_utils.h"
30#include "vk_layer_logging.h"
31
Mark Lobodzinskid42e4d22017-01-17 14:14:22 -070032#include "buffer_validation.h"
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -070033
Tobin Ehlis58c884f2017-02-08 12:15:27 -070034void SetLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -070035 if (std::find(pCB->imageSubresourceMap[imgpair.image].begin(), pCB->imageSubresourceMap[imgpair.image].end(), imgpair) !=
36 pCB->imageSubresourceMap[imgpair.image].end()) {
37 pCB->imageLayoutMap[imgpair].layout = layout;
38 } else {
39 assert(imgpair.hasSubresource);
40 IMAGE_CMD_BUF_LAYOUT_NODE node;
41 if (!FindCmdBufLayout(device_data, pCB, imgpair.image, imgpair.subresource, node)) {
42 node.initialLayout = layout;
43 }
44 SetLayout(device_data, pCB, imgpair, {node.initialLayout, layout});
45 }
46}
47template <class OBJECT, class LAYOUT>
Tobin Ehlis58c884f2017-02-08 12:15:27 -070048void SetLayout(layer_data *device_data, OBJECT *pObject, VkImage image, VkImageSubresource range, const LAYOUT &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -070049 ImageSubresourcePair imgpair = {image, true, range};
50 SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
51 SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
52 SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
53 SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
54}
55
56template <class OBJECT, class LAYOUT>
Tobin Ehlis58c884f2017-02-08 12:15:27 -070057void SetLayout(layer_data *device_data, OBJECT *pObject, ImageSubresourcePair imgpair, const LAYOUT &layout,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -070058 VkImageAspectFlags aspectMask) {
59 if (imgpair.subresource.aspectMask & aspectMask) {
60 imgpair.subresource.aspectMask = aspectMask;
61 SetLayout(device_data, pObject, imgpair, layout);
62 }
63}
64
Tony Barbourdf013b92017-01-25 12:53:48 -070065// Set the layout in supplied map
Tony Barboure0c5cc92017-02-08 13:53:39 -070066void SetLayout(std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
67 VkImageLayout layout) {
Tony Barbourdf013b92017-01-25 12:53:48 -070068 imageLayoutMap[imgpair].layout = layout;
69}
70
Tobin Ehlis58c884f2017-02-08 12:15:27 -070071bool FindLayoutVerifyNode(layer_data *device_data, GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -070072 IMAGE_CMD_BUF_LAYOUT_NODE &node, const VkImageAspectFlags aspectMask) {
73 const debug_report_data *report_data = core_validation::GetReportData(device_data);
74
75 if (!(imgpair.subresource.aspectMask & aspectMask)) {
76 return false;
77 }
78 VkImageAspectFlags oldAspectMask = imgpair.subresource.aspectMask;
79 imgpair.subresource.aspectMask = aspectMask;
80 auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
81 if (imgsubIt == pCB->imageLayoutMap.end()) {
82 return false;
83 }
84 if (node.layout != VK_IMAGE_LAYOUT_MAX_ENUM && node.layout != imgsubIt->second.layout) {
85 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
86 reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
87 "Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple layout types: %s and %s",
88 reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(node.layout),
89 string_VkImageLayout(imgsubIt->second.layout));
90 }
91 if (node.initialLayout != VK_IMAGE_LAYOUT_MAX_ENUM && node.initialLayout != imgsubIt->second.initialLayout) {
92 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
93 reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
94 "Cannot query for VkImage 0x%" PRIx64
95 " layout when combined aspect mask %d has multiple initial layout types: %s and %s",
96 reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(node.initialLayout),
97 string_VkImageLayout(imgsubIt->second.initialLayout));
98 }
99 node = imgsubIt->second;
100 return true;
101}
102
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700103bool FindLayoutVerifyLayout(layer_data *device_data, ImageSubresourcePair imgpair, VkImageLayout &layout,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700104 const VkImageAspectFlags aspectMask) {
105 if (!(imgpair.subresource.aspectMask & aspectMask)) {
106 return false;
107 }
108 const debug_report_data *report_data = core_validation::GetReportData(device_data);
109 VkImageAspectFlags oldAspectMask = imgpair.subresource.aspectMask;
110 imgpair.subresource.aspectMask = aspectMask;
111 auto imgsubIt = (*core_validation::GetImageLayoutMap(device_data)).find(imgpair);
112 if (imgsubIt == (*core_validation::GetImageLayoutMap(device_data)).end()) {
113 return false;
114 }
115 if (layout != VK_IMAGE_LAYOUT_MAX_ENUM && layout != imgsubIt->second.layout) {
116 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
117 reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
118 "Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple layout types: %s and %s",
119 reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(layout),
120 string_VkImageLayout(imgsubIt->second.layout));
121 }
122 layout = imgsubIt->second.layout;
123 return true;
124}
125
126// Find layout(s) on the command buffer level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700127bool FindCmdBufLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, VkImage image, VkImageSubresource range,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700128 IMAGE_CMD_BUF_LAYOUT_NODE &node) {
129 ImageSubresourcePair imgpair = {image, true, range};
130 node = IMAGE_CMD_BUF_LAYOUT_NODE(VK_IMAGE_LAYOUT_MAX_ENUM, VK_IMAGE_LAYOUT_MAX_ENUM);
131 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_COLOR_BIT);
132 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_DEPTH_BIT);
133 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_STENCIL_BIT);
134 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_METADATA_BIT);
135 if (node.layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
136 imgpair = {image, false, VkImageSubresource()};
137 auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
138 if (imgsubIt == pCB->imageLayoutMap.end()) return false;
139 // TODO: This is ostensibly a find function but it changes state here
140 node = imgsubIt->second;
141 }
142 return true;
143}
144
145// Find layout(s) on the global level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700146bool FindGlobalLayout(layer_data *device_data, ImageSubresourcePair imgpair, VkImageLayout &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700147 layout = VK_IMAGE_LAYOUT_MAX_ENUM;
148 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
149 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
150 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
151 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
152 if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
153 imgpair = {imgpair.image, false, VkImageSubresource()};
154 auto imgsubIt = (*core_validation::GetImageLayoutMap(device_data)).find(imgpair);
155 if (imgsubIt == (*core_validation::GetImageLayoutMap(device_data)).end()) return false;
156 layout = imgsubIt->second.layout;
157 }
158 return true;
159}
160
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700161bool FindLayouts(layer_data *device_data, VkImage image, std::vector<VkImageLayout> &layouts) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700162 auto sub_data = (*core_validation::GetImageSubresourceMap(device_data)).find(image);
163 if (sub_data == (*core_validation::GetImageSubresourceMap(device_data)).end()) return false;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700164 auto image_state = GetImageState(device_data, image);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700165 if (!image_state) return false;
166 bool ignoreGlobal = false;
167 // TODO: Make this robust for >1 aspect mask. Now it will just say ignore potential errors in this case.
168 if (sub_data->second.size() >= (image_state->createInfo.arrayLayers * image_state->createInfo.mipLevels + 1)) {
169 ignoreGlobal = true;
170 }
171 for (auto imgsubpair : sub_data->second) {
172 if (ignoreGlobal && !imgsubpair.hasSubresource) continue;
173 auto img_data = (*core_validation::GetImageLayoutMap(device_data)).find(imgsubpair);
174 if (img_data != (*core_validation::GetImageLayoutMap(device_data)).end()) {
175 layouts.push_back(img_data->second.layout);
176 }
177 }
178 return true;
179}
Tony Barboure0c5cc92017-02-08 13:53:39 -0700180bool FindLayout(const std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
181 VkImageLayout &layout, const VkImageAspectFlags aspectMask) {
Tony Barbourdf013b92017-01-25 12:53:48 -0700182 if (!(imgpair.subresource.aspectMask & aspectMask)) {
183 return false;
184 }
185 imgpair.subresource.aspectMask = aspectMask;
186 auto imgsubIt = imageLayoutMap.find(imgpair);
187 if (imgsubIt == imageLayoutMap.end()) {
188 return false;
189 }
190 layout = imgsubIt->second.layout;
191 return true;
Tony Barboure0c5cc92017-02-08 13:53:39 -0700192}
Tony Barbourdf013b92017-01-25 12:53:48 -0700193
194// find layout in supplied map
Tony Barboure0c5cc92017-02-08 13:53:39 -0700195bool FindLayout(const std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
196 VkImageLayout &layout) {
Tony Barbourdf013b92017-01-25 12:53:48 -0700197 layout = VK_IMAGE_LAYOUT_MAX_ENUM;
198 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
199 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
200 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
201 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
202 if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
203 imgpair = {imgpair.image, false, VkImageSubresource()};
204 auto imgsubIt = imageLayoutMap.find(imgpair);
205 if (imgsubIt == imageLayoutMap.end()) return false;
206 layout = imgsubIt->second.layout;
207 }
208 return true;
209}
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700210
211// Set the layout on the global level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700212void SetGlobalLayout(layer_data *device_data, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700213 VkImage &image = imgpair.image;
214 (*core_validation::GetImageLayoutMap(device_data))[imgpair].layout = layout;
215 auto &image_subresources = (*core_validation::GetImageSubresourceMap(device_data))[image];
216 auto subresource = std::find(image_subresources.begin(), image_subresources.end(), imgpair);
217 if (subresource == image_subresources.end()) {
218 image_subresources.push_back(imgpair);
219 }
220}
221
222// Set the layout on the cmdbuf level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700223void SetLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair, const IMAGE_CMD_BUF_LAYOUT_NODE &node) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700224 pCB->imageLayoutMap[imgpair] = node;
225 auto subresource =
226 std::find(pCB->imageSubresourceMap[imgpair.image].begin(), pCB->imageSubresourceMap[imgpair.image].end(), imgpair);
227 if (subresource == pCB->imageSubresourceMap[imgpair.image].end()) {
228 pCB->imageSubresourceMap[imgpair.image].push_back(imgpair);
229 }
230}
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600231// Set image layout for given VkImageSubresourceRange struct
232void SetImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *image_state,
233 VkImageSubresourceRange image_subresource_range, const VkImageLayout &layout) {
234 assert(image_state);
235 for (uint32_t level_index = 0; level_index < image_subresource_range.levelCount; ++level_index) {
236 uint32_t level = image_subresource_range.baseMipLevel + level_index;
237 for (uint32_t layer_index = 0; layer_index < image_subresource_range.layerCount; layer_index++) {
238 uint32_t layer = image_subresource_range.baseArrayLayer + layer_index;
239 VkImageSubresource sub = {image_subresource_range.aspectMask, level, layer};
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700240 // TODO: If ImageView was created with depth or stencil, transition both layouts as the aspectMask is ignored and both
241 // are used. Verify that the extra implicit layout is OK for descriptor set layout validation
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600242 if (image_subresource_range.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600243 if (FormatIsDepthAndStencil(image_state->createInfo.format)) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700244 sub.aspectMask |= (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
245 }
246 }
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600247 SetLayout(device_data, cb_node, image_state->image, sub, layout);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700248 }
249 }
250}
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600251// Set image layout for given VkImageSubresourceLayers struct
252void SetImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *image_state,
253 VkImageSubresourceLayers image_subresource_layers, const VkImageLayout &layout) {
254 // Transfer VkImageSubresourceLayers into VkImageSubresourceRange struct
255 VkImageSubresourceRange image_subresource_range;
256 image_subresource_range.aspectMask = image_subresource_layers.aspectMask;
257 image_subresource_range.baseArrayLayer = image_subresource_layers.baseArrayLayer;
258 image_subresource_range.layerCount = image_subresource_layers.layerCount;
259 image_subresource_range.baseMipLevel = image_subresource_layers.mipLevel;
260 image_subresource_range.levelCount = 1;
261 SetImageLayout(device_data, cb_node, image_state, image_subresource_range, layout);
262}
263// Set image layout for all slices of an image view
264void SetImageViewLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImageView imageView, const VkImageLayout &layout) {
265 auto view_state = GetImageViewState(device_data, imageView);
266 assert(view_state);
267
268 SetImageLayout(device_data, cb_node, GetImageState(device_data, view_state->create_info.image),
269 view_state->create_info.subresourceRange, layout);
270}
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700271
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700272bool VerifyFramebufferAndRenderPassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700273 const VkRenderPassBeginInfo *pRenderPassBegin,
274 const FRAMEBUFFER_STATE *framebuffer_state) {
275 bool skip_call = false;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700276 auto const pRenderPassInfo = GetRenderPassState(device_data, pRenderPassBegin->renderPass)->createInfo.ptr();
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700277 auto const &framebufferInfo = framebuffer_state->createInfo;
278 const auto report_data = core_validation::GetReportData(device_data);
279 if (pRenderPassInfo->attachmentCount != framebufferInfo.attachmentCount) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600280 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
281 reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700282 "You cannot start a render pass using a framebuffer "
283 "with a different number of attachments.");
284 }
285 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
286 const VkImageView &image_view = framebufferInfo.pAttachments[i];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700287 auto view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700288 assert(view_state);
289 const VkImage &image = view_state->create_info.image;
290 const VkImageSubresourceRange &subRange = view_state->create_info.subresourceRange;
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700291 auto initial_layout = pRenderPassInfo->pAttachments[i].initialLayout;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700292 // TODO: Do not iterate over every possibility - consolidate where possible
293 for (uint32_t j = 0; j < subRange.levelCount; j++) {
294 uint32_t level = subRange.baseMipLevel + j;
295 for (uint32_t k = 0; k < subRange.layerCount; k++) {
296 uint32_t layer = subRange.baseArrayLayer + k;
297 VkImageSubresource sub = {subRange.aspectMask, level, layer};
298 IMAGE_CMD_BUF_LAYOUT_NODE node;
299 if (!FindCmdBufLayout(device_data, pCB, image, sub, node)) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700300 // Missing layouts will be added during state update
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700301 continue;
302 }
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700303 if (initial_layout != VK_IMAGE_LAYOUT_UNDEFINED && initial_layout != node.layout) {
304 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
305 DRAWSTATE_INVALID_RENDERPASS, "DS",
306 "You cannot start a render pass using attachment %u "
307 "where the render pass initial layout is %s and the previous "
308 "known layout of the attachment is %s. The layouts must match, or "
309 "the render pass initial layout for the attachment must be "
310 "VK_IMAGE_LAYOUT_UNDEFINED",
311 i, string_VkImageLayout(initial_layout), string_VkImageLayout(node.layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700312 }
313 }
314 }
315 }
316 return skip_call;
317}
318
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700319void TransitionAttachmentRefLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, FRAMEBUFFER_STATE *pFramebuffer,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700320 VkAttachmentReference ref) {
321 if (ref.attachment != VK_ATTACHMENT_UNUSED) {
322 auto image_view = pFramebuffer->createInfo.pAttachments[ref.attachment];
323 SetImageViewLayout(device_data, pCB, image_view, ref.layout);
324 }
325}
326
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700327void TransitionSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const RENDER_PASS_STATE *render_pass_state,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700328 const int subpass_index, FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700329 assert(render_pass_state);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700330
331 if (framebuffer_state) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700332 auto const &subpass = render_pass_state->createInfo.pSubpasses[subpass_index];
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700333 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
334 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pInputAttachments[j]);
335 }
336 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
337 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pColorAttachments[j]);
338 }
339 if (subpass.pDepthStencilAttachment) {
340 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, *subpass.pDepthStencilAttachment);
341 }
342 }
343}
344
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700345bool ValidateImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
346 uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700347 if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
348 return false;
349 }
350 VkImageSubresource sub = {aspect, level, layer};
351 IMAGE_CMD_BUF_LAYOUT_NODE node;
352 if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700353 return false;
354 }
355 bool skip = false;
356 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
357 // TODO: Set memory invalid which is in mem_tracker currently
358 } else if (node.layout != mem_barrier->oldLayout) {
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600359 skip |=
360 log_msg(core_validation::GetReportData(device_data), VK_DEBUG_REPORT_ERROR_BIT_EXT,
361 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__,
362 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
363 "For image 0x%" PRIxLEAST64 " you cannot transition the layout of aspect %d from %s when current layout is %s.",
364 reinterpret_cast<const uint64_t &>(mem_barrier->image), aspect, string_VkImageLayout(mem_barrier->oldLayout),
365 string_VkImageLayout(node.layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700366 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700367 return skip;
368}
369
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700370// Transition the layout state for renderpass attachments based on the BeginRenderPass() call. This includes:
371// 1. Transition into initialLayout state
372// 2. Transition from initialLayout to layout used in subpass 0
373void TransitionBeginRenderPassLayouts(layer_data *device_data, GLOBAL_CB_NODE *cb_state, const RENDER_PASS_STATE *render_pass_state,
374 FRAMEBUFFER_STATE *framebuffer_state) {
375 // First transition into initialLayout
376 auto const rpci = render_pass_state->createInfo.ptr();
377 for (uint32_t i = 0; i < rpci->attachmentCount; ++i) {
378 VkImageView image_view = framebuffer_state->createInfo.pAttachments[i];
379 SetImageViewLayout(device_data, cb_state, image_view, rpci->pAttachments[i].initialLayout);
380 }
381 // Now transition for first subpass (index 0)
382 TransitionSubpassLayouts(device_data, cb_state, render_pass_state, 0, framebuffer_state);
383}
384
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700385void TransitionImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
386 uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
387 if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
388 return;
389 }
390 VkImageSubresource sub = {aspect, level, layer};
391 IMAGE_CMD_BUF_LAYOUT_NODE node;
392 if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
393 SetLayout(device_data, pCB, mem_barrier->image, sub,
394 IMAGE_CMD_BUF_LAYOUT_NODE(mem_barrier->oldLayout, mem_barrier->newLayout));
395 return;
396 }
397 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
398 // TODO: Set memory invalid
399 }
400 SetLayout(device_data, pCB, mem_barrier->image, sub, mem_barrier->newLayout);
401}
402
Dave Houlton10b39482017-03-16 13:18:15 -0600403bool VerifyAspectsPresent(VkImageAspectFlags aspect_mask, VkFormat format) {
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600404 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600405 if (!FormatIsColor(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600406 }
407 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600408 if (!FormatHasDepth(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600409 }
410 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600411 if (!FormatHasStencil(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600412 }
413 return true;
414}
415
Mike Weiblen62d08a32017-03-07 22:18:27 -0700416// Verify an ImageMemoryBarrier's old/new ImageLayouts are compatible with the Image's ImageUsageFlags.
417bool ValidateBarrierLayoutToImageUsage(layer_data *device_data, const VkImageMemoryBarrier *img_barrier, bool new_not_old,
418 VkImageUsageFlags usage_flags, const char *func_name) {
419 const auto report_data = core_validation::GetReportData(device_data);
420 bool skip = false;
421 const VkImageLayout layout = (new_not_old) ? img_barrier->newLayout : img_barrier->oldLayout;
422 UNIQUE_VALIDATION_ERROR_CODE msg_code = VALIDATION_ERROR_UNDEFINED; // sentinel value meaning "no error"
423
424 switch (layout) {
425 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
426 if ((usage_flags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) == 0) {
427 msg_code = VALIDATION_ERROR_00303;
428 }
429 break;
430 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
431 if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
432 msg_code = VALIDATION_ERROR_00304;
433 }
434 break;
435 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
436 if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
437 msg_code = VALIDATION_ERROR_00305;
438 }
439 break;
440 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
441 if ((usage_flags & (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) == 0) {
442 msg_code = VALIDATION_ERROR_00306;
443 }
444 break;
445 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
446 if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) == 0) {
447 msg_code = VALIDATION_ERROR_00307;
448 }
449 break;
450 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
451 if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) == 0) {
452 msg_code = VALIDATION_ERROR_00308;
453 }
454 break;
455 default:
456 // Other VkImageLayout values do not have VUs defined in this context.
457 break;
458 }
459
460 if (msg_code != VALIDATION_ERROR_UNDEFINED) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600461 skip |=
462 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
463 reinterpret_cast<const uint64_t &>(img_barrier->image), __LINE__, msg_code, "DS",
464 "%s: Image barrier 0x%p %sLayout=%s is not compatible with image 0x%" PRIx64 " usage flags 0x%" PRIx32 ". %s",
465 func_name, img_barrier, ((new_not_old) ? "new" : "old"), string_VkImageLayout(layout),
466 reinterpret_cast<const uint64_t &>(img_barrier->image), usage_flags, validation_error_map[msg_code]);
Mike Weiblen62d08a32017-03-07 22:18:27 -0700467 }
468 return skip;
469}
470
471// Verify image barriers are compatible with the images they reference.
472bool ValidateBarriersToImages(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t imageMemoryBarrierCount,
473 const VkImageMemoryBarrier *pImageMemoryBarriers, const char *func_name) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700474 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700475 bool skip = false;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700476
Mike Weiblen62d08a32017-03-07 22:18:27 -0700477 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
478 auto img_barrier = &pImageMemoryBarriers[i];
479 if (!img_barrier) continue;
480
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600481 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, img_barrier->image)->createInfo);
482 uint32_t level_count = ResolveRemainingLevels(&img_barrier->subresourceRange, image_create_info->mipLevels);
483 uint32_t layer_count = ResolveRemainingLayers(&img_barrier->subresourceRange, image_create_info->arrayLayers);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700484
Mike Weiblen62d08a32017-03-07 22:18:27 -0700485 for (uint32_t j = 0; j < level_count; j++) {
486 uint32_t level = img_barrier->subresourceRange.baseMipLevel + j;
487 for (uint32_t k = 0; k < layer_count; k++) {
488 uint32_t layer = img_barrier->subresourceRange.baseArrayLayer + k;
489 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
490 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
491 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
492 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700493 }
494 }
Mike Weiblen62d08a32017-03-07 22:18:27 -0700495
496 IMAGE_STATE *image_state = GetImageState(device_data, img_barrier->image);
497 if (image_state) {
498 VkImageUsageFlags usage_flags = image_state->createInfo.usage;
499 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, false, usage_flags, func_name);
500 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, true, usage_flags, func_name);
501 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700502 }
503 return skip;
504}
505
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700506void TransitionImageLayouts(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
507 const VkImageMemoryBarrier *pImgMemBarriers) {
508 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700509
510 for (uint32_t i = 0; i < memBarrierCount; ++i) {
511 auto mem_barrier = &pImgMemBarriers[i];
512 if (!mem_barrier) continue;
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700513
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600514 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, mem_barrier->image)->createInfo);
515 uint32_t level_count = ResolveRemainingLevels(&mem_barrier->subresourceRange, image_create_info->mipLevels);
516 uint32_t layer_count = ResolveRemainingLayers(&mem_barrier->subresourceRange, image_create_info->arrayLayers);
517
518 for (uint32_t j = 0; j < level_count; j++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700519 uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600520 for (uint32_t k = 0; k < layer_count; k++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700521 uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
522 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
523 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
524 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
525 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
526 }
527 }
528 }
529}
530
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600531bool VerifyImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
532 VkImageSubresourceLayers subLayers, VkImageLayout explicit_layout, VkImageLayout optimal_layout,
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600533 const char *caller, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700534 const auto report_data = core_validation::GetReportData(device_data);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600535 const auto image = image_state->image;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700536 bool skip_call = false;
537
538 for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
539 uint32_t layer = i + subLayers.baseArrayLayer;
540 VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
541 IMAGE_CMD_BUF_LAYOUT_NODE node;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600542 if (FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
543 if (node.layout != explicit_layout) {
544 // TODO: Improve log message in the next pass
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600545 skip_call |=
546 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
547 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600548 "%s: Cannot use image 0x%" PRIxLEAST64
549 " with specific layout %s that doesn't match the actual current layout %s.",
550 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
551 string_VkImageLayout(node.layout));
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600552 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700553 }
554 }
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600555 // If optimal_layout is not UNDEFINED, check that layout matches optimal for this case
556 if ((VK_IMAGE_LAYOUT_UNDEFINED != optimal_layout) && (explicit_layout != optimal_layout)) {
557 if (VK_IMAGE_LAYOUT_GENERAL == explicit_layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700558 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
559 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600560 skip_call |= log_msg(
561 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
562 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
563 "%s: For optimal performance image 0x%" PRIxLEAST64 " layout should be %s instead of GENERAL.", caller,
564 reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(optimal_layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700565 }
566 } else {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600567 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
568 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, msg_code, "DS",
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600569 "%s: Layout for image 0x%" PRIxLEAST64 " is %s but can only be %s or VK_IMAGE_LAYOUT_GENERAL. %s",
570 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
571 string_VkImageLayout(optimal_layout), validation_error_map[msg_code]);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700572 }
573 }
574 return skip_call;
575}
576
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700577void TransitionFinalSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkRenderPassBeginInfo *pRenderPassBegin,
578 FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700579 auto renderPass = GetRenderPassState(device_data, pRenderPassBegin->renderPass);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700580 if (!renderPass) return;
581
582 const VkRenderPassCreateInfo *pRenderPassInfo = renderPass->createInfo.ptr();
583 if (framebuffer_state) {
584 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
585 auto image_view = framebuffer_state->createInfo.pAttachments[i];
586 SetImageViewLayout(device_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
587 }
588 }
589}
590
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700591bool PreCallValidateCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700592 const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
593 bool skip_call = false;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700594 const debug_report_data *report_data = core_validation::GetReportData(device_data);
595
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600596 if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700597 skip_call |=
598 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
599 VALIDATION_ERROR_00715, "IMAGE", "vkCreateImage: VkFormat for image must not be VK_FORMAT_UNDEFINED. %s",
600 validation_error_map[VALIDATION_ERROR_00715]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600601
602 return skip_call;
603 }
604
605 const VkFormatProperties *properties = GetFormatProperties(device_data, pCreateInfo->format);
606
607 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) && (properties->linearTilingFeatures == 0)) {
608 std::stringstream ss;
609 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
610 skip_call |=
611 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
612 VALIDATION_ERROR_02150, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02150]);
613
614 return skip_call;
615 }
616
617 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) && (properties->optimalTilingFeatures == 0)) {
618 std::stringstream ss;
619 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
620 skip_call |=
621 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
622 VALIDATION_ERROR_02155, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02155]);
623
624 return skip_call;
625 }
626
627 // Validate that format supports usage as color attachment
628 if (pCreateInfo->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
629 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
630 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
631 std::stringstream ss;
632 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
633 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
634 skip_call |=
635 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
636 VALIDATION_ERROR_02158, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02158]);
637 }
638 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
639 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
640 std::stringstream ss;
641 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
642 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
643 skip_call |=
644 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
645 VALIDATION_ERROR_02153, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02153]);
646 }
647 }
648
649 // Validate that format supports usage as depth/stencil attachment
650 if (pCreateInfo->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
651 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
652 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
653 std::stringstream ss;
654 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
655 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
656 skip_call |=
657 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
658 VALIDATION_ERROR_02159, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02159]);
659 }
660 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
661 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
662 std::stringstream ss;
663 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
664 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
665 skip_call |=
666 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
667 VALIDATION_ERROR_02154, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02154]);
668 }
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700669 }
670
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700671 const VkImageFormatProperties *ImageFormatProperties = GetImageFormatProperties(
672 device_data, pCreateInfo->format, pCreateInfo->imageType, pCreateInfo->tiling, pCreateInfo->usage, pCreateInfo->flags);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700673
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700674 VkDeviceSize imageGranularity = GetPhysicalDeviceProperties(device_data)->limits.bufferImageGranularity;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700675 imageGranularity = imageGranularity == 1 ? 0 : imageGranularity;
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600676 // TODO : This is also covering 2918 & 2919. Break out into separate checks
Mark Lobodzinski688ed322017-01-27 11:13:21 -0700677 if ((pCreateInfo->extent.width <= 0) || (pCreateInfo->extent.height <= 0) || (pCreateInfo->extent.depth <= 0)) {
678 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600679 VALIDATION_ERROR_02917, "Image",
Mark Lobodzinski688ed322017-01-27 11:13:21 -0700680 "CreateImage extent is 0 for at least one required dimension for image: "
681 "Width = %d Height = %d Depth = %d. %s",
682 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600683 validation_error_map[VALIDATION_ERROR_02917]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700684 }
685
686 // TODO: VALIDATION_ERROR_02125 VALIDATION_ERROR_02126 VALIDATION_ERROR_02128 VALIDATION_ERROR_00720
687 // All these extent-related VUs should be checked here
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700688 if ((pCreateInfo->extent.depth > ImageFormatProperties->maxExtent.depth) ||
689 (pCreateInfo->extent.width > ImageFormatProperties->maxExtent.width) ||
690 (pCreateInfo->extent.height > ImageFormatProperties->maxExtent.height)) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700691 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
692 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
693 "CreateImage extents exceed allowable limits for format: "
694 "Width = %d Height = %d Depth = %d: Limits for Width = %d Height = %d Depth = %d for format %s.",
695 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700696 ImageFormatProperties->maxExtent.width, ImageFormatProperties->maxExtent.height,
697 ImageFormatProperties->maxExtent.depth, string_VkFormat(pCreateInfo->format));
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700698 }
699
700 uint64_t totalSize = ((uint64_t)pCreateInfo->extent.width * (uint64_t)pCreateInfo->extent.height *
701 (uint64_t)pCreateInfo->extent.depth * (uint64_t)pCreateInfo->arrayLayers *
Dave Houlton1d2022c2017-03-29 11:43:58 -0600702 (uint64_t)pCreateInfo->samples * (uint64_t)FormatSize(pCreateInfo->format) +
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700703 (uint64_t)imageGranularity) &
704 ~(uint64_t)imageGranularity;
705
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700706 if (totalSize > ImageFormatProperties->maxResourceSize) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700707 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
708 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
709 "CreateImage resource size exceeds allowable maximum "
710 "Image resource size = 0x%" PRIxLEAST64 ", maximum resource size = 0x%" PRIxLEAST64 " ",
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700711 totalSize, ImageFormatProperties->maxResourceSize);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700712 }
713
714 // TODO: VALIDATION_ERROR_02132
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700715 if (pCreateInfo->mipLevels > ImageFormatProperties->maxMipLevels) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700716 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
717 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
718 "CreateImage mipLevels=%d exceeds allowable maximum supported by format of %d", pCreateInfo->mipLevels,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700719 ImageFormatProperties->maxMipLevels);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700720 }
721
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700722 if (pCreateInfo->arrayLayers > ImageFormatProperties->maxArrayLayers) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700723 skip_call |= log_msg(
724 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__, VALIDATION_ERROR_02133,
725 "Image", "CreateImage arrayLayers=%d exceeds allowable maximum supported by format of %d. %s", pCreateInfo->arrayLayers,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700726 ImageFormatProperties->maxArrayLayers, validation_error_map[VALIDATION_ERROR_02133]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700727 }
728
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700729 if ((pCreateInfo->samples & ImageFormatProperties->sampleCounts) == 0) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700730 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
731 VALIDATION_ERROR_02138, "Image", "CreateImage samples %s is not supported by format 0x%.8X. %s",
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700732 string_VkSampleCountFlagBits(pCreateInfo->samples), ImageFormatProperties->sampleCounts,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700733 validation_error_map[VALIDATION_ERROR_02138]);
734 }
735
736 if (pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_UNDEFINED && pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_PREINITIALIZED) {
737 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
738 VALIDATION_ERROR_00731, "Image",
739 "vkCreateImage parameter, pCreateInfo->initialLayout, must be VK_IMAGE_LAYOUT_UNDEFINED or "
740 "VK_IMAGE_LAYOUT_PREINITIALIZED. %s",
741 validation_error_map[VALIDATION_ERROR_00731]);
742 }
743
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600744 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
745 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
746 VALIDATION_ERROR_02143, "DS",
747 "vkCreateImage(): the sparseBinding device feature is disabled: Images cannot be created with the "
748 "VK_IMAGE_CREATE_SPARSE_BINDING_BIT set. %s",
749 validation_error_map[VALIDATION_ERROR_02143]);
750 }
751
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600752 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
753 skip_call |=
754 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
755 DRAWSTATE_INVALID_FEATURE, "DS",
756 "vkCreateImage(): the sparseResidencyAliased device feature is disabled: Images cannot be created with the "
757 "VK_IMAGE_CREATE_SPARSE_ALIASED_BIT set.");
758 }
759
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700760 return skip_call;
761}
762
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700763void PostCallRecordCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo, VkImage *pImage) {
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700764 IMAGE_LAYOUT_NODE image_state;
765 image_state.layout = pCreateInfo->initialLayout;
766 image_state.format = pCreateInfo->format;
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700767 GetImageMap(device_data)->insert(std::make_pair(*pImage, std::unique_ptr<IMAGE_STATE>(new IMAGE_STATE(*pImage, pCreateInfo))));
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700768 ImageSubresourcePair subpair{*pImage, false, VkImageSubresource()};
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700769 (*core_validation::GetImageSubresourceMap(device_data))[*pImage].push_back(subpair);
770 (*core_validation::GetImageLayoutMap(device_data))[subpair] = image_state;
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700771}
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700772
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700773bool PreCallValidateDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE **image_state, VK_OBJECT *obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700774 const CHECK_DISABLED *disabled = core_validation::GetDisables(device_data);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700775 *image_state = core_validation::GetImageState(device_data, image);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700776 *obj_struct = {reinterpret_cast<uint64_t &>(image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT};
777 if (disabled->destroy_image) return false;
778 bool skip = false;
779 if (*image_state) {
780 skip |= core_validation::ValidateObjectNotInUse(device_data, *image_state, *obj_struct, VALIDATION_ERROR_00743);
781 }
782 return skip;
783}
784
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700785void PostCallRecordDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE *image_state, VK_OBJECT obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700786 core_validation::invalidateCommandBuffers(device_data, image_state->cb_bindings, obj_struct);
787 // Clean up memory mapping, bindings and range references for image
788 for (auto mem_binding : image_state->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700789 auto mem_info = core_validation::GetMemObjInfo(device_data, mem_binding);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700790 if (mem_info) {
791 core_validation::RemoveImageMemoryRange(obj_struct.handle, mem_info);
792 }
793 }
794 core_validation::ClearMemoryObjectBindings(device_data, obj_struct.handle, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
795 // Remove image from imageMap
796 core_validation::GetImageMap(device_data)->erase(image);
797 std::unordered_map<VkImage, std::vector<ImageSubresourcePair>> *imageSubresourceMap =
798 core_validation::GetImageSubresourceMap(device_data);
799
800 const auto &sub_entry = imageSubresourceMap->find(image);
801 if (sub_entry != imageSubresourceMap->end()) {
802 for (const auto &pair : sub_entry->second) {
803 core_validation::GetImageLayoutMap(device_data)->erase(pair);
804 }
805 imageSubresourceMap->erase(sub_entry);
806 }
807}
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700808
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700809bool ValidateImageAttributes(layer_data *device_data, IMAGE_STATE *image_state, VkImageSubresourceRange range) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700810 bool skip = false;
811 const debug_report_data *report_data = core_validation::GetReportData(device_data);
812
813 if (range.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) {
814 char const str[] = "vkCmdClearColorImage aspectMasks for all subresource ranges must be set to VK_IMAGE_ASPECT_COLOR_BIT";
815 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
816 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
817 }
818
Dave Houlton1d2022c2017-03-29 11:43:58 -0600819 if (FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700820 char const str[] = "vkCmdClearColorImage called with depth/stencil image.";
821 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
822 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
823 validation_error_map[VALIDATION_ERROR_01088]);
Dave Houlton1d2022c2017-03-29 11:43:58 -0600824 } else if (FormatIsCompressed(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700825 char const str[] = "vkCmdClearColorImage called with compressed image.";
826 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
827 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
828 validation_error_map[VALIDATION_ERROR_01088]);
829 }
830
831 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
832 char const str[] = "vkCmdClearColorImage called with image created without VK_IMAGE_USAGE_TRANSFER_DST_BIT.";
833 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
834 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01084, "IMAGE", "%s. %s", str,
835 validation_error_map[VALIDATION_ERROR_01084]);
836 }
837 return skip;
838}
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700839
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600840uint32_t ResolveRemainingLevels(const VkImageSubresourceRange *range, uint32_t mip_levels) {
841 // Return correct number of mip levels taking into account VK_REMAINING_MIP_LEVELS
842 uint32_t mip_level_count = range->levelCount;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700843 if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600844 mip_level_count = mip_levels - range->baseMipLevel;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700845 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600846 return mip_level_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700847}
848
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600849uint32_t ResolveRemainingLayers(const VkImageSubresourceRange *range, uint32_t layers) {
850 // Return correct number of layers taking into account VK_REMAINING_ARRAY_LAYERS
851 uint32_t array_layer_count = range->layerCount;
852 if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
853 array_layer_count = layers - range->baseArrayLayer;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700854 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600855 return array_layer_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700856}
857
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700858bool VerifyClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700859 VkImageSubresourceRange range, VkImageLayout dest_image_layout, const char *func_name) {
860 bool skip = false;
861 const debug_report_data *report_data = core_validation::GetReportData(device_data);
862
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600863 uint32_t level_count = ResolveRemainingLevels(&range, image_state->createInfo.mipLevels);
864 uint32_t layer_count = ResolveRemainingLayers(&range, image_state->createInfo.arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700865
866 if (dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
867 if (dest_image_layout == VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700868 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
869 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600870 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
871 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700872 "%s: Layout for cleared image should be TRANSFER_DST_OPTIMAL instead of GENERAL.", func_name);
873 }
874 } else {
875 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01086;
876 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
877 error_code = VALIDATION_ERROR_01101;
878 } else {
879 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
880 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600881 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
882 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, error_code, "DS",
883 "%s: Layout for cleared image is %s but can only be "
884 "TRANSFER_DST_OPTIMAL or GENERAL. %s",
885 func_name, string_VkImageLayout(dest_image_layout), validation_error_map[error_code]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700886 }
887 }
888
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600889 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
890 uint32_t level = level_index + range.baseMipLevel;
891 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
892 uint32_t layer = layer_index + range.baseArrayLayer;
893 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700894 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700895 if (FindCmdBufLayout(device_data, cb_node, image_state->image, sub, node)) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700896 if (node.layout != dest_image_layout) {
897 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01085;
898 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
899 error_code = VALIDATION_ERROR_01100;
900 } else {
901 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
902 }
903 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
904 __LINE__, error_code, "DS",
905 "%s: Cannot clear an image whose layout is %s and "
906 "doesn't match the current layout %s. %s",
907 func_name, string_VkImageLayout(dest_image_layout), string_VkImageLayout(node.layout),
908 validation_error_map[error_code]);
909 }
910 }
911 }
912 }
913
914 return skip;
915}
916
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700917void RecordClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage image, VkImageSubresourceRange range,
918 VkImageLayout dest_image_layout) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600919 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, image)->createInfo);
920 uint32_t level_count = ResolveRemainingLevels(&range, image_create_info->mipLevels);
921 uint32_t layer_count = ResolveRemainingLayers(&range, image_create_info->arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700922
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600923 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
924 uint32_t level = level_index + range.baseMipLevel;
925 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
926 uint32_t layer = layer_index + range.baseArrayLayer;
927 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700928 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700929 if (!FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
930 SetLayout(device_data, cb_node, image, sub, IMAGE_CMD_BUF_LAYOUT_NODE(dest_image_layout, dest_image_layout));
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700931 }
932 }
933 }
934}
935
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700936bool PreCallValidateCmdClearColorImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700937 VkImageLayout imageLayout, uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
938 bool skip = false;
939 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700940 auto cb_node = GetCBNode(dev_data, commandBuffer);
941 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700942 if (cb_node && image_state) {
943 skip |= ValidateMemoryIsBoundToImage(dev_data, image_state, "vkCmdClearColorImage()", VALIDATION_ERROR_02527);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700944 skip |= ValidateCmdQueueFlags(dev_data, cb_node, "vkCmdClearColorImage()", VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT,
945 VALIDATION_ERROR_01095);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700946 skip |= ValidateCmd(dev_data, cb_node, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
947 skip |= insideRenderPass(dev_data, cb_node, "vkCmdClearColorImage()", VALIDATION_ERROR_01096);
948 for (uint32_t i = 0; i < rangeCount; ++i) {
949 skip |= ValidateImageAttributes(dev_data, image_state, pRanges[i]);
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700950 skip |= VerifyClearImageLayout(dev_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearColorImage()");
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700951 }
952 }
953 return skip;
954}
955
956// This state recording routine is shared between ClearColorImage and ClearDepthStencilImage
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700957void PreCallRecordCmdClearImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
958 uint32_t rangeCount, const VkImageSubresourceRange *pRanges, CMD_TYPE cmd_type) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700959 auto cb_node = GetCBNode(dev_data, commandBuffer);
960 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700961 if (cb_node && image_state) {
962 AddCommandBufferBindingImage(dev_data, cb_node, image_state);
963 std::function<bool()> function = [=]() {
964 SetImageMemoryValid(dev_data, image_state, true);
965 return false;
966 };
967 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700968 core_validation::UpdateCmdBufferLastCmd(cb_node, cmd_type);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700969 for (uint32_t i = 0; i < rangeCount; ++i) {
970 RecordClearImageLayout(dev_data, cb_node, image, pRanges[i], imageLayout);
971 }
972 }
973}
974
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700975bool PreCallValidateCmdClearDepthStencilImage(layer_data *device_data, VkCommandBuffer commandBuffer, VkImage image,
976 VkImageLayout imageLayout, uint32_t rangeCount,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700977 const VkImageSubresourceRange *pRanges) {
978 bool skip = false;
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700979 const debug_report_data *report_data = core_validation::GetReportData(device_data);
980
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700981 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700982 auto cb_node = GetCBNode(device_data, commandBuffer);
983 auto image_state = GetImageState(device_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700984 if (cb_node && image_state) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700985 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_02528);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700986 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearDepthStencilImage()", VK_QUEUE_GRAPHICS_BIT,
987 VALIDATION_ERROR_01110);
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700988 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
989 skip |= insideRenderPass(device_data, cb_node, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_01111);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700990 for (uint32_t i = 0; i < rangeCount; ++i) {
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700991 skip |=
992 VerifyClearImageLayout(device_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearDepthStencilImage()");
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700993 // Image aspect must be depth or stencil or both
994 if (((pRanges[i].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
995 ((pRanges[i].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
996 char const str[] =
997 "vkCmdClearDepthStencilImage aspectMasks for all subresource ranges must be "
998 "set to VK_IMAGE_ASPECT_DEPTH_BIT and/or VK_IMAGE_ASPECT_STENCIL_BIT";
999 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1000 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
1001 }
1002 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001003 if (image_state && !FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001004 char const str[] = "vkCmdClearDepthStencilImage called without a depth/stencil image.";
1005 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
1006 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_01103, "IMAGE", "%s. %s", str,
1007 validation_error_map[VALIDATION_ERROR_01103]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001008 }
1009 }
1010 return skip;
1011}
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001012
1013// Returns true if [x, xoffset] and [y, yoffset] overlap
1014static bool RangesIntersect(int32_t start, uint32_t start_offset, int32_t end, uint32_t end_offset) {
1015 bool result = false;
1016 uint32_t intersection_min = std::max(static_cast<uint32_t>(start), static_cast<uint32_t>(end));
1017 uint32_t intersection_max = std::min(static_cast<uint32_t>(start) + start_offset, static_cast<uint32_t>(end) + end_offset);
1018
1019 if (intersection_max > intersection_min) {
1020 result = true;
1021 }
1022 return result;
1023}
1024
1025// Returns true if two VkImageCopy structures overlap
1026static bool RegionIntersects(const VkImageCopy *src, const VkImageCopy *dst, VkImageType type) {
1027 bool result = false;
1028 if ((src->srcSubresource.mipLevel == dst->dstSubresource.mipLevel) &&
1029 (RangesIntersect(src->srcSubresource.baseArrayLayer, src->srcSubresource.layerCount, dst->dstSubresource.baseArrayLayer,
1030 dst->dstSubresource.layerCount))) {
1031 result = true;
1032 switch (type) {
1033 case VK_IMAGE_TYPE_3D:
1034 result &= RangesIntersect(src->srcOffset.z, src->extent.depth, dst->dstOffset.z, dst->extent.depth);
1035 // Intentionally fall through to 2D case
1036 case VK_IMAGE_TYPE_2D:
1037 result &= RangesIntersect(src->srcOffset.y, src->extent.height, dst->dstOffset.y, dst->extent.height);
1038 // Intentionally fall through to 1D case
1039 case VK_IMAGE_TYPE_1D:
1040 result &= RangesIntersect(src->srcOffset.x, src->extent.width, dst->dstOffset.x, dst->extent.width);
1041 break;
1042 default:
1043 // Unrecognized or new IMAGE_TYPE enums will be caught in parameter_validation
1044 assert(false);
1045 }
1046 }
1047 return result;
1048}
1049
1050// Returns true if offset and extent exceed image extents
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001051static bool ExceedsBounds(const VkOffset3D *offset, const VkExtent3D *extent, const VkExtent3D *image_extent) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001052 bool result = false;
1053 // Extents/depths cannot be negative but checks left in for clarity
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001054 if ((offset->z + extent->depth > image_extent->depth) || (offset->z < 0) ||
1055 ((offset->z + static_cast<int32_t>(extent->depth)) < 0)) {
1056 result = true;
1057 }
1058 if ((offset->y + extent->height > image_extent->height) || (offset->y < 0) ||
1059 ((offset->y + static_cast<int32_t>(extent->height)) < 0)) {
1060 result = true;
1061 }
1062 if ((offset->x + extent->width > image_extent->width) || (offset->x < 0) ||
1063 ((offset->x + static_cast<int32_t>(extent->width)) < 0)) {
1064 result = true;
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001065 }
1066 return result;
1067}
1068
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001069// Test if two VkExtent3D structs are equivalent
1070static inline bool IsExtentEqual(const VkExtent3D *extent, const VkExtent3D *other_extent) {
1071 bool result = true;
1072 if ((extent->width != other_extent->width) || (extent->height != other_extent->height) ||
1073 (extent->depth != other_extent->depth)) {
1074 result = false;
1075 }
1076 return result;
1077}
1078
1079// Returns the image extent of a specific subresource.
1080static inline VkExtent3D GetImageSubresourceExtent(const IMAGE_STATE *img, const VkImageSubresourceLayers *subresource) {
1081 const uint32_t mip = subresource->mipLevel;
1082 VkExtent3D extent = img->createInfo.extent;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001083 // Don't allow mip adjustment to create 0 dim, but pass along a 0 if that's what subresource specified
1084 extent.width = (0 == extent.width ? 0 : std::max(1U, extent.width >> mip));
1085 extent.height = (0 == extent.height ? 0 : std::max(1U, extent.height >> mip));
1086 extent.depth = (0 == extent.depth ? 0 : std::max(1U, extent.depth >> mip));
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001087 return extent;
1088}
1089
1090// Test if the extent argument has all dimensions set to 0.
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001091static inline bool IsExtentAllZeroes(const VkExtent3D *extent) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001092 return ((extent->width == 0) && (extent->height == 0) && (extent->depth == 0));
1093}
1094
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001095// Test if the extent argument has any dimensions set to 0.
1096static inline bool IsExtentSizeZero(const VkExtent3D *extent) {
1097 return ((extent->width == 0) || (extent->height == 0) || (extent->depth == 0));
1098}
1099
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001100// Returns the image transfer granularity for a specific image scaled by compressed block size if necessary.
1101static inline VkExtent3D GetScaledItg(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *img) {
1102 // Default to (0, 0, 0) granularity in case we can't find the real granularity for the physical device.
1103 VkExtent3D granularity = {0, 0, 0};
1104 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
1105 if (pPool) {
1106 granularity =
1107 GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].minImageTransferGranularity;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001108 if (FormatIsCompressed(img->createInfo.format)) {
1109 auto block_size = FormatCompressedTexelBlockExtent(img->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001110 granularity.width *= block_size.width;
1111 granularity.height *= block_size.height;
1112 }
1113 }
1114 return granularity;
1115}
1116
1117// Test elements of a VkExtent3D structure against alignment constraints contained in another VkExtent3D structure
1118static inline bool IsExtentAligned(const VkExtent3D *extent, const VkExtent3D *granularity) {
1119 bool valid = true;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001120 if ((SafeModulo(extent->depth, granularity->depth) != 0) || (SafeModulo(extent->width, granularity->width) != 0) ||
1121 (SafeModulo(extent->height, granularity->height) != 0)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001122 valid = false;
1123 }
1124 return valid;
1125}
1126
1127// Check elements of a VkOffset3D structure against a queue family's Image Transfer Granularity values
1128static inline bool CheckItgOffset(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkOffset3D *offset,
1129 const VkExtent3D *granularity, const uint32_t i, const char *function, const char *member) {
1130 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1131 bool skip = false;
1132 VkExtent3D offset_extent = {};
1133 offset_extent.width = static_cast<uint32_t>(abs(offset->x));
1134 offset_extent.height = static_cast<uint32_t>(abs(offset->y));
1135 offset_extent.depth = static_cast<uint32_t>(abs(offset->z));
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001136 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001137 // If the queue family image transfer granularity is (0, 0, 0), then the offset must always be (0, 0, 0)
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001138 if (IsExtentAllZeroes(&offset_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001139 skip |=
1140 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1141 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1142 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) must be (x=0, y=0, z=0) "
1143 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1144 function, i, member, offset->x, offset->y, offset->z);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001145 }
1146 } else {
1147 // If the queue family image transfer granularity is not (0, 0, 0), then the offset dimensions must always be even
1148 // integer multiples of the image transfer granularity.
1149 if (IsExtentAligned(&offset_extent, granularity) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001150 skip |= log_msg(
1151 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1152 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1153 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) dimensions must be even integer "
1154 "multiples of this command buffer's queue family image transfer granularity (w=%d, h=%d, d=%d).",
1155 function, i, member, offset->x, offset->y, offset->z, granularity->width, granularity->height, granularity->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001156 }
1157 }
1158 return skip;
1159}
1160
1161// Check elements of a VkExtent3D structure against a queue family's Image Transfer Granularity values
1162static inline bool CheckItgExtent(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkExtent3D *extent,
1163 const VkOffset3D *offset, const VkExtent3D *granularity, const VkExtent3D *subresource_extent,
1164 const uint32_t i, const char *function, const char *member) {
1165 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1166 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001167 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001168 // If the queue family image transfer granularity is (0, 0, 0), then the extent must always match the image
1169 // subresource extent.
1170 if (IsExtentEqual(extent, subresource_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001171 skip |=
1172 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1173 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1174 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d) "
1175 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1176 function, i, member, extent->width, extent->height, extent->depth, subresource_extent->width,
1177 subresource_extent->height, subresource_extent->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001178 }
1179 } else {
1180 // If the queue family image transfer granularity is not (0, 0, 0), then the extent dimensions must always be even
1181 // integer multiples of the image transfer granularity or the offset + extent dimensions must always match the image
1182 // subresource extent dimensions.
1183 VkExtent3D offset_extent_sum = {};
1184 offset_extent_sum.width = static_cast<uint32_t>(abs(offset->x)) + extent->width;
1185 offset_extent_sum.height = static_cast<uint32_t>(abs(offset->y)) + extent->height;
1186 offset_extent_sum.depth = static_cast<uint32_t>(abs(offset->z)) + extent->depth;
1187 if ((IsExtentAligned(extent, granularity) == false) && (IsExtentEqual(&offset_extent_sum, subresource_extent) == false)) {
1188 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001189 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1190 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001191 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) dimensions must be even integer multiples of this command buffer's "
1192 "queue family image transfer granularity (w=%d, h=%d, d=%d) or offset (x=%d, y=%d, z=%d) + "
1193 "extent (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d).",
1194 function, i, member, extent->width, extent->height, extent->depth, granularity->width, granularity->height,
1195 granularity->depth, offset->x, offset->y, offset->z, extent->width, extent->height, extent->depth,
1196 subresource_extent->width, subresource_extent->height, subresource_extent->depth);
1197 }
1198 }
1199 return skip;
1200}
1201
1202// Check a uint32_t width or stride value against a queue family's Image Transfer Granularity width value
1203static inline bool CheckItgInt(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const uint32_t value,
1204 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1205 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1206
1207 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001208 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001209 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1210 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001211 "%s: pRegion[%d].%s (%d) must be an even integer multiple of this command buffer's queue family image "
1212 "transfer granularity width (%d).",
1213 function, i, member, value, granularity);
1214 }
1215 return skip;
1216}
1217
1218// Check a VkDeviceSize value against a queue family's Image Transfer Granularity width value
1219static inline bool CheckItgSize(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkDeviceSize value,
1220 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1221 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1222 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001223 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001224 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1225 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001226 "%s: pRegion[%d].%s (%" PRIdLEAST64
1227 ") must be an even integer multiple of this command buffer's queue family image transfer "
1228 "granularity width (%d).",
1229 function, i, member, value, granularity);
1230 }
1231 return skip;
1232}
1233
1234// Check valid usage Image Tranfer Granularity requirements for elements of a VkBufferImageCopy structure
1235bool ValidateCopyBufferImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1236 const IMAGE_STATE *img, const VkBufferImageCopy *region,
1237 const uint32_t i, const char *function) {
1238 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001239 if (FormatIsCompressed(img->createInfo.format) == true) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001240 // TODO: Add granularity checking for compressed formats
1241
1242 // bufferRowLength must be a multiple of the compressed texel block width
1243 // bufferImageHeight must be a multiple of the compressed texel block height
1244 // all members of imageOffset must be a multiple of the corresponding dimensions of the compressed texel block
1245 // bufferOffset must be a multiple of the compressed texel block size in bytes
1246 // imageExtent.width must be a multiple of the compressed texel block width or (imageExtent.width + imageOffset.x)
1247 // must equal the image subresource width
1248 // imageExtent.height must be a multiple of the compressed texel block height or (imageExtent.height + imageOffset.y)
1249 // must equal the image subresource height
1250 // imageExtent.depth must be a multiple of the compressed texel block depth or (imageExtent.depth + imageOffset.z)
1251 // must equal the image subresource depth
1252 } else {
1253 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1254 skip |= CheckItgSize(device_data, cb_node, region->bufferOffset, granularity.width, i, function, "bufferOffset");
1255 skip |= CheckItgInt(device_data, cb_node, region->bufferRowLength, granularity.width, i, function, "bufferRowLength");
1256 skip |= CheckItgInt(device_data, cb_node, region->bufferImageHeight, granularity.width, i, function, "bufferImageHeight");
1257 skip |= CheckItgOffset(device_data, cb_node, &region->imageOffset, &granularity, i, function, "imageOffset");
1258 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->imageSubresource);
1259 skip |= CheckItgExtent(device_data, cb_node, &region->imageExtent, &region->imageOffset, &granularity, &subresource_extent,
1260 i, function, "imageExtent");
1261 }
1262 return skip;
1263}
1264
1265// Check valid usage Image Tranfer Granularity requirements for elements of a VkImageCopy structure
1266bool ValidateCopyImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1267 const IMAGE_STATE *img, const VkImageCopy *region, const uint32_t i,
1268 const char *function) {
1269 bool skip = false;
1270 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1271 skip |= CheckItgOffset(device_data, cb_node, &region->srcOffset, &granularity, i, function, "srcOffset");
1272 skip |= CheckItgOffset(device_data, cb_node, &region->dstOffset, &granularity, i, function, "dstOffset");
1273 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->dstSubresource);
1274 skip |= CheckItgExtent(device_data, cb_node, &region->extent, &region->dstOffset, &granularity, &subresource_extent, i,
1275 function, "extent");
1276 return skip;
1277}
1278
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001279bool PreCallValidateCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001280 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1281 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001282 bool skip = false;
1283 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1284 VkCommandBuffer command_buffer = cb_node->commandBuffer;
1285
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001286 for (uint32_t i = 0; i < region_count; i++) {
1287 if (regions[i].srcSubresource.layerCount == 0) {
1288 std::stringstream ss;
1289 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] srcSubresource is zero";
1290 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1291 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1292 ss.str().c_str());
1293 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001294
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001295 if (regions[i].dstSubresource.layerCount == 0) {
1296 std::stringstream ss;
1297 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] dstSubresource is zero";
1298 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1299 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1300 ss.str().c_str());
1301 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001302
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001303 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
1304 // For each region the layerCount member of srcSubresource and dstSubresource must match
1305 if (regions[i].srcSubresource.layerCount != regions[i].dstSubresource.layerCount) {
1306 std::stringstream ss;
1307 ss << "vkCmdCopyImage: number of layers in source and destination subresources for pRegions[" << i
1308 << "] do not match";
1309 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1310 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01198, "IMAGE", "%s. %s",
1311 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01198]);
1312 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001313 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001314
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001315 // For each region, the aspectMask member of srcSubresource and dstSubresource must match
1316 if (regions[i].srcSubresource.aspectMask != regions[i].dstSubresource.aspectMask) {
1317 char const str[] = "vkCmdCopyImage: Src and dest aspectMasks for each region must match";
1318 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1319 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01197, "IMAGE", "%s. %s", str,
1320 validation_error_map[VALIDATION_ERROR_01197]);
1321 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001322
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06001323 // For each region, the aspectMask member of srcSubresource must be present in the source image
1324 if (!VerifyAspectsPresent(regions[i].srcSubresource.aspectMask, src_image_state->createInfo.format)) {
1325 std::stringstream ss;
1326 ss << "vkCmdCopyImage: pRegion[" << i
1327 << "] srcSubresource.aspectMask cannot specify aspects not present in source image";
1328 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1329 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01200, "IMAGE", "%s. %s",
1330 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01200]);
1331 }
1332
1333 // For each region, the aspectMask member of dstSubresource must be present in the destination image
1334 if (!VerifyAspectsPresent(regions[i].dstSubresource.aspectMask, dst_image_state->createInfo.format)) {
1335 std::stringstream ss;
1336 ss << "vkCmdCopyImage: pRegion[" << i << "] dstSubresource.aspectMask cannot specify aspects not present in dest image";
1337 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1338 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01201, "IMAGE", "%s. %s",
1339 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01201]);
1340 }
1341
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001342 // AspectMask must not contain VK_IMAGE_ASPECT_METADATA_BIT
1343 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) ||
1344 (regions[i].dstSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT)) {
1345 std::stringstream ss;
1346 ss << "vkCmdCopyImage: pRegions[" << i << "] may not specify aspectMask containing VK_IMAGE_ASPECT_METADATA_BIT";
1347 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1348 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01222, "IMAGE", "%s. %s",
1349 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01222]);
1350 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001351
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001352 // For each region, if aspectMask contains VK_IMAGE_ASPECT_COLOR_BIT, it must not contain either of
1353 // VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT
1354 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) &&
1355 (regions[i].srcSubresource.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
1356 char const str[] = "vkCmdCopyImage aspectMask cannot specify both COLOR and DEPTH/STENCIL aspects";
1357 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1358 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01221, "IMAGE", "%s. %s", str,
1359 validation_error_map[VALIDATION_ERROR_01221]);
1360 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001361
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001362 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
1363 // If either of the calling command's src_image or dst_image parameters are of VkImageType VK_IMAGE_TYPE_3D,
1364 // the baseArrayLayer and layerCount members of both srcSubresource and dstSubresource must be 0 and 1, respectively
1365 if (((src_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) ||
1366 (dst_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D)) &&
1367 ((regions[i].srcSubresource.baseArrayLayer != 0) || (regions[i].srcSubresource.layerCount != 1) ||
1368 (regions[i].dstSubresource.baseArrayLayer != 0) || (regions[i].dstSubresource.layerCount != 1))) {
1369 std::stringstream ss;
1370 ss << "vkCmdCopyImage: src or dstImage type was IMAGE_TYPE_3D, but in subRegion[" << i
1371 << "] baseArrayLayer was not zero or layerCount was not 1.";
1372 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1373 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01199, "IMAGE", "%s. %s",
1374 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01199]);
1375 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001376 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001377
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001378 // MipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
1379 if (regions[i].srcSubresource.mipLevel >= src_image_state->createInfo.mipLevels) {
1380 std::stringstream ss;
1381 ss << "vkCmdCopyImage: pRegions[" << i
1382 << "] specifies a src mipLevel greater than the number specified when the srcImage was created.";
1383 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1384 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1385 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1386 }
1387 if (regions[i].dstSubresource.mipLevel >= dst_image_state->createInfo.mipLevels) {
1388 std::stringstream ss;
1389 ss << "vkCmdCopyImage: pRegions[" << i
1390 << "] specifies a dst mipLevel greater than the number specified when the dstImage was created.";
1391 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1392 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1393 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1394 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001395
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001396 // (baseArrayLayer + layerCount) must be less than or equal to the arrayLayers specified in VkImageCreateInfo when the
1397 // image was created
1398 if ((regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount) >
1399 src_image_state->createInfo.arrayLayers) {
1400 std::stringstream ss;
1401 ss << "vkCmdCopyImage: srcImage arrayLayers was " << src_image_state->createInfo.arrayLayers << " but subRegion[" << i
1402 << "] baseArrayLayer + layerCount is "
1403 << (regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount);
1404 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1405 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1406 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1407 }
1408 if ((regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount) >
1409 dst_image_state->createInfo.arrayLayers) {
1410 std::stringstream ss;
1411 ss << "vkCmdCopyImage: dstImage arrayLayers was " << dst_image_state->createInfo.arrayLayers << " but subRegion[" << i
1412 << "] baseArrayLayer + layerCount is "
1413 << (regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount);
1414 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1415 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1416 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1417 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001418
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001419 // Check region extents for 1D-1D, 2D-2D, and 3D-3D copies
1420 if (src_image_state->createInfo.imageType == dst_image_state->createInfo.imageType) {
1421 // The source region specified by a given element of regions must be a region that is contained within srcImage
1422 if (ExceedsBounds(&regions[i].srcOffset, &regions[i].extent, &(src_image_state->createInfo.extent))) {
1423 std::stringstream ss;
1424 ss << "vkCmdCopyImage: srcSubResource in pRegions[" << i << "] exceeds extents srcImage was created with";
1425 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1426 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01175, "IMAGE", "%s. %s",
1427 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01175]);
1428 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001429
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001430 // The destination region specified by a given element of regions must be a region that is contained within dst_image
1431 if (ExceedsBounds(&regions[i].dstOffset, &regions[i].extent, &(dst_image_state->createInfo.extent))) {
1432 std::stringstream ss;
1433 ss << "vkCmdCopyImage: dstSubResource in pRegions[" << i << "] exceeds extents dstImage was created with";
1434 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1435 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01176, "IMAGE", "%s. %s",
1436 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01176]);
1437 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001438 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001439
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001440 // The union of all source regions, and the union of all destination regions, specified by the elements of regions,
1441 // must not overlap in memory
1442 if (src_image_state->image == dst_image_state->image) {
1443 for (uint32_t j = 0; j < region_count; j++) {
1444 if (RegionIntersects(&regions[i], &regions[j], src_image_state->createInfo.imageType)) {
1445 std::stringstream ss;
1446 ss << "vkCmdCopyImage: pRegions[" << i << "] src overlaps with pRegions[" << j << "].";
1447 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1448 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01177, "IMAGE",
1449 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01177]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001450 }
1451 }
1452 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001453 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001454
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001455 // The formats of src_image and dst_image must be compatible. Formats are considered compatible if their texel size in bytes
1456 // is the same between both formats. For example, VK_FORMAT_R8G8B8A8_UNORM is compatible with VK_FORMAT_R32_UINT because
1457 // because both texels are 4 bytes in size. Depth/stencil formats must match exactly.
Dave Houlton1d2022c2017-03-29 11:43:58 -06001458 if (FormatIsDepthOrStencil(src_image_state->createInfo.format) ||
1459 FormatIsDepthOrStencil(dst_image_state->createInfo.format)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001460 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1461 char const str[] = "vkCmdCopyImage called with unmatched source and dest image depth/stencil formats.";
1462 skip |=
1463 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1464 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT, "IMAGE", str);
1465 }
1466 } else {
Dave Houlton1d2022c2017-03-29 11:43:58 -06001467 size_t srcSize = FormatSize(src_image_state->createInfo.format);
1468 size_t destSize = FormatSize(dst_image_state->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001469 if (srcSize != destSize) {
1470 char const str[] = "vkCmdCopyImage called with unmatched source and dest image format sizes.";
1471 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1472 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01184, "IMAGE", "%s. %s", str,
1473 validation_error_map[VALIDATION_ERROR_01184]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001474 }
1475 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001476
Dave Houlton33c22b72017-02-28 13:16:02 -07001477 // Source and dest image sample counts must match
1478 if (src_image_state->createInfo.samples != dst_image_state->createInfo.samples) {
1479 char const str[] = "vkCmdCopyImage() called on image pair with non-identical sample counts.";
1480 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06001481 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01185, "IMAGE", "%s %s", str,
1482 validation_error_map[VALIDATION_ERROR_01185]);
Dave Houlton33c22b72017-02-28 13:16:02 -07001483 }
1484
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001485 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02533);
1486 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02534);
1487 // Validate that SRC & DST images have correct usage flags set
1488 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01178,
1489 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
1490 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01181,
1491 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001492 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyImage()",
1493 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01193);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001494 skip |= ValidateCmd(device_data, cb_node, CMD_COPYIMAGE, "vkCmdCopyImage()");
1495 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImage()", VALIDATION_ERROR_01194);
1496 for (uint32_t i = 0; i < region_count; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001497 skip |= VerifyImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout,
1498 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01180);
1499 skip |= VerifyImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout,
1500 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01183);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001501 skip |= ValidateCopyImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &regions[i], i,
1502 "vkCmdCopyImage()");
1503 }
1504
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001505 return skip;
1506}
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001507
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001508void PreCallRecordCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06001509 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1510 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
1511 // Make sure that all image slices are updated to correct layout
1512 for (uint32_t i = 0; i < region_count; ++i) {
1513 SetImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout);
1514 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout);
1515 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001516 // Update bindings between images and cmd buffer
1517 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1518 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Dave Houltoneba86e22017-03-02 14:56:23 -07001519 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImage()"); };
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001520 cb_node->validate_functions.push_back(function);
1521 function = [=]() {
1522 SetImageMemoryValid(device_data, dst_image_state, true);
1523 return false;
1524 };
1525 cb_node->validate_functions.push_back(function);
1526 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGE);
1527}
1528
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001529// TODO : Should be tracking lastBound per commandBuffer and when draws occur, report based on that cmd buffer lastBound
1530// Then need to synchronize the accesses based on cmd buffer so that if I'm reading state on one cmd buffer, updates
1531// to that same cmd buffer by separate thread are not changing state from underneath us
1532// Track the last cmd buffer touched by this thread
1533static bool hasDrawCmd(GLOBAL_CB_NODE *pCB) {
1534 for (uint32_t i = 0; i < NUM_DRAW_TYPES; i++) {
1535 if (pCB->drawCount[i]) return true;
1536 }
1537 return false;
1538}
1539
1540// Returns true if sub_rect is entirely contained within rect
1541static inline bool ContainsRect(VkRect2D rect, VkRect2D sub_rect) {
1542 if ((sub_rect.offset.x < rect.offset.x) || (sub_rect.offset.x + sub_rect.extent.width > rect.offset.x + rect.extent.width) ||
1543 (sub_rect.offset.y < rect.offset.y) || (sub_rect.offset.y + sub_rect.extent.height > rect.offset.y + rect.extent.height))
1544 return false;
1545 return true;
1546}
1547
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001548bool PreCallValidateCmdClearAttachments(layer_data *device_data, VkCommandBuffer commandBuffer, uint32_t attachmentCount,
1549 const VkClearAttachment *pAttachments, uint32_t rectCount, const VkClearRect *pRects) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001550 GLOBAL_CB_NODE *cb_node = GetCBNode(device_data, commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001551 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1552
1553 bool skip = false;
1554 if (cb_node) {
Mike Schuchardt9c582402017-02-23 15:57:37 -07001555 skip |=
1556 ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearAttachments()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01121);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001557 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001558 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_CLEARATTACHMENTS);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001559 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
1560 if (!hasDrawCmd(cb_node) && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
1561 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1562 // There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
Mark Lobodzinskid833bb72017-02-22 10:55:30 -07001563 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1564 // CmdClearAttachments.
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001565 skip |=
1566 log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1567 reinterpret_cast<uint64_t &>(commandBuffer), 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
1568 "vkCmdClearAttachments() issued on command buffer object 0x%p prior to any Draw Cmds."
1569 " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1570 commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001571 }
1572 skip |= outsideRenderPass(device_data, cb_node, "vkCmdClearAttachments()", VALIDATION_ERROR_01122);
1573 }
1574
1575 // Validate that attachment is in reference list of active subpass
1576 if (cb_node->activeRenderPass) {
1577 const VkRenderPassCreateInfo *renderpass_create_info = cb_node->activeRenderPass->createInfo.ptr();
1578 const VkSubpassDescription *subpass_desc = &renderpass_create_info->pSubpasses[cb_node->activeSubpass];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001579 auto framebuffer = GetFramebufferState(device_data, cb_node->activeFramebuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001580
1581 for (uint32_t i = 0; i < attachmentCount; i++) {
1582 auto clear_desc = &pAttachments[i];
1583 VkImageView image_view = VK_NULL_HANDLE;
1584
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001585 if (0 == clear_desc->aspectMask) {
1586 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001587 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01128, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001588 validation_error_map[VALIDATION_ERROR_01128]);
1589 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) {
1590 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001591 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01126, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001592 validation_error_map[VALIDATION_ERROR_01126]);
1593 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001594 if (clear_desc->colorAttachment >= subpass_desc->colorAttachmentCount) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001595 skip |=
1596 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001597 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01114, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001598 "vkCmdClearAttachments() color attachment index %d out of range for active subpass %d. %s",
1599 clear_desc->colorAttachment, cb_node->activeSubpass, validation_error_map[VALIDATION_ERROR_01114]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001600 } else if (subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment == VK_ATTACHMENT_UNUSED) {
1601 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001602 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(commandBuffer),
1603 __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001604 "vkCmdClearAttachments() color attachment index %d is VK_ATTACHMENT_UNUSED; ignored.",
1605 clear_desc->colorAttachment);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001606 } else {
1607 image_view = framebuffer->createInfo
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001608 .pAttachments[subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment];
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001609 }
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001610 if ((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
1611 (clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1612 char const str[] =
1613 "vkCmdClearAttachments aspectMask [%d] must set only VK_IMAGE_ASPECT_COLOR_BIT of a color attachment. %s";
1614 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001615 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01125, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001616 validation_error_map[VALIDATION_ERROR_01125]);
1617 }
1618 } else { // Must be depth and/or stencil
1619 if (((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
1620 ((clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1621 char const str[] = "vkCmdClearAttachments aspectMask [%d] is not a valid combination of bits. %s";
1622 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001623 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01127, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001624 validation_error_map[VALIDATION_ERROR_01127]);
1625 }
1626 if (!subpass_desc->pDepthStencilAttachment ||
1627 (subpass_desc->pDepthStencilAttachment->attachment == VK_ATTACHMENT_UNUSED)) {
1628 skip |= log_msg(
1629 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001630 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001631 "vkCmdClearAttachments() depth/stencil clear with no depth/stencil attachment in subpass; ignored");
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001632 } else {
1633 image_view = framebuffer->createInfo.pAttachments[subpass_desc->pDepthStencilAttachment->attachment];
1634 }
1635 }
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001636 if (image_view) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001637 auto image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001638 for (uint32_t j = 0; j < rectCount; j++) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001639 // The rectangular region specified by a given element of pRects must be contained within the render area of
1640 // the current render pass instance
Mark Lobodzinskid833bb72017-02-22 10:55:30 -07001641 // TODO: This check should be moved to CmdExecuteCommands or QueueSubmit to cover secondary CB cases
1642 if ((cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
1643 (false == ContainsRect(cb_node->activeRenderPassBeginInfo.renderArea, pRects[j].rect))) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001644 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1645 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01115, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001646 "vkCmdClearAttachments(): The area defined by pRects[%d] is not contained in the area of "
1647 "the current render pass instance. %s",
1648 j, validation_error_map[VALIDATION_ERROR_01115]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001649 }
1650 // The layers specified by a given element of pRects must be contained within every attachment that
1651 // pAttachments refers to
1652 auto attachment_base_array_layer = image_view_state->create_info.subresourceRange.baseArrayLayer;
1653 auto attachment_layer_count = image_view_state->create_info.subresourceRange.layerCount;
1654 if ((pRects[j].baseArrayLayer < attachment_base_array_layer) || pRects[j].layerCount > attachment_layer_count) {
1655 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001656 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1657 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01116, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001658 "vkCmdClearAttachments(): The layers defined in pRects[%d] are not contained in the layers of "
1659 "pAttachment[%d]. %s",
1660 j, i, validation_error_map[VALIDATION_ERROR_01116]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001661 }
1662 }
1663 }
1664 }
1665 }
1666 return skip;
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001667}
1668
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001669bool PreCallValidateCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001670 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageResolve *pRegions) {
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001671 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001672 bool skip = false;
1673 if (cb_node && src_image_state && dst_image_state) {
1674 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02541);
1675 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02542);
Mike Schuchardt9c582402017-02-23 15:57:37 -07001676 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdResolveImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01334);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001677 skip |= ValidateCmd(device_data, cb_node, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
1678 skip |= insideRenderPass(device_data, cb_node, "vkCmdResolveImage()", VALIDATION_ERROR_01335);
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001679
1680 // For each region, the number of layers in the image subresource should not be zero
1681 // For each region, src and dest image aspect must be color only
1682 for (uint32_t i = 0; i < regionCount; i++) {
1683 if (pRegions[i].srcSubresource.layerCount == 0) {
1684 char const str[] = "vkCmdResolveImage: number of layers in source subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001685 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001686 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1687 "IMAGE", str);
1688 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001689 if (pRegions[i].dstSubresource.layerCount == 0) {
1690 char const str[] = "vkCmdResolveImage: number of layers in destination subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001691 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001692 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1693 "IMAGE", str);
1694 }
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001695 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1696 skip |= log_msg(
1697 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1698 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01339, "IMAGE",
1699 "vkCmdResolveImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s", i,
1700 validation_error_map[VALIDATION_ERROR_01339]);
1701 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001702 if ((pRegions[i].srcSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) ||
1703 (pRegions[i].dstSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)) {
1704 char const str[] =
1705 "vkCmdResolveImage: src and dest aspectMasks for each region must specify only VK_IMAGE_ASPECT_COLOR_BIT";
1706 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1707 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01338, "IMAGE",
1708 "%s. %s", str, validation_error_map[VALIDATION_ERROR_01338]);
1709 }
1710 }
1711
1712 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1713 char const str[] = "vkCmdResolveImage called with unmatched source and dest formats.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001714 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001715 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT,
1716 "IMAGE", str);
1717 }
1718 if (src_image_state->createInfo.imageType != dst_image_state->createInfo.imageType) {
1719 char const str[] = "vkCmdResolveImage called with unmatched source and dest image types.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001720 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001721 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_TYPE, "IMAGE",
1722 str);
1723 }
1724 if (src_image_state->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
1725 char const str[] = "vkCmdResolveImage called with source sample count less than 2.";
1726 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1727 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01320, "IMAGE", "%s. %s",
1728 str, validation_error_map[VALIDATION_ERROR_01320]);
1729 }
1730 if (dst_image_state->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
1731 char const str[] = "vkCmdResolveImage called with dest sample count greater than 1.";
1732 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1733 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01321, "IMAGE", "%s. %s",
1734 str, validation_error_map[VALIDATION_ERROR_01321]);
1735 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001736 } else {
1737 assert(0);
1738 }
1739 return skip;
1740}
1741
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001742void PreCallRecordCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1743 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001744 // Update bindings between images and cmd buffer
1745 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1746 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1747
1748 std::function<bool()> function = [=]() {
1749 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdResolveImage()");
1750 };
1751 cb_node->validate_functions.push_back(function);
1752 function = [=]() {
1753 SetImageMemoryValid(device_data, dst_image_state, true);
1754 return false;
1755 };
1756 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001757 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_RESOLVEIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001758}
1759
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001760bool PreCallValidateCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001761 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
1762 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1763
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001764 bool skip = false;
1765 if (cb_node && src_image_state && dst_image_state) {
1766 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): srcImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001767 VALIDATION_ERROR_02194);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001768 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): dstImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001769 VALIDATION_ERROR_02195);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001770 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02539);
1771 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02540);
1772 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_02182,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001773 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001774 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_02186,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001775 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001776 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdBlitImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01299);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001777 skip |= ValidateCmd(device_data, cb_node, CMD_BLITIMAGE, "vkCmdBlitImage()");
1778 skip |= insideRenderPass(device_data, cb_node, "vkCmdBlitImage()", VALIDATION_ERROR_01300);
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001779
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001780 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001781 // Warn for zero-sized regions
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001782 if ((pRegions[i].srcOffsets[0].x == pRegions[i].srcOffsets[1].x) ||
1783 (pRegions[i].srcOffsets[0].y == pRegions[i].srcOffsets[1].y) ||
1784 (pRegions[i].srcOffsets[0].z == pRegions[i].srcOffsets[1].z)) {
1785 std::stringstream ss;
1786 ss << "vkCmdBlitImage: pRegions[" << i << "].srcOffsets specify a zero-volume area.";
1787 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1788 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1789 "%s", ss.str().c_str());
1790 }
1791 if ((pRegions[i].dstOffsets[0].x == pRegions[i].dstOffsets[1].x) ||
1792 (pRegions[i].dstOffsets[0].y == pRegions[i].dstOffsets[1].y) ||
1793 (pRegions[i].dstOffsets[0].z == pRegions[i].dstOffsets[1].z)) {
1794 std::stringstream ss;
1795 ss << "vkCmdBlitImage: pRegions[" << i << "].dstOffsets specify a zero-volume area.";
1796 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1797 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1798 "%s", ss.str().c_str());
1799 }
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001800 if (pRegions[i].srcSubresource.layerCount == 0) {
1801 char const str[] = "vkCmdBlitImage: number of layers in source subresource is zero";
1802 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Dave Houltoneba86e22017-03-02 14:56:23 -07001803 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1804 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001805 }
1806 if (pRegions[i].dstSubresource.layerCount == 0) {
1807 char const str[] = "vkCmdBlitImage: number of layers in destination subresource is zero";
1808 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Dave Houltoneba86e22017-03-02 14:56:23 -07001809 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1810 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001811 }
1812
1813 // Check that src/dst layercounts match
1814 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1815 skip |=
1816 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1817 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01304, "IMAGE",
1818 "vkCmdBlitImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s",
1819 i, validation_error_map[VALIDATION_ERROR_01304]);
1820 }
Mark Lobodzinskie7e85fd2017-02-07 13:44:57 -07001821
1822 if (pRegions[i].srcSubresource.aspectMask != pRegions[i].dstSubresource.aspectMask) {
1823 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1824 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01303, "IMAGE",
1825 "vkCmdBlitImage: aspectMask members for pRegion[%d] do not match. %s", i,
1826 validation_error_map[VALIDATION_ERROR_01303]);
1827 }
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001828 }
1829
1830 VkFormat src_format = src_image_state->createInfo.format;
1831 VkFormat dst_format = dst_image_state->createInfo.format;
1832
1833 // Validate consistency for unsigned formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001834 if (FormatIsUInt(src_format) != FormatIsUInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001835 std::stringstream ss;
1836 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has unsigned integer format, "
1837 << "the other one must also have unsigned integer format. "
1838 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1839 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1840 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02191, "IMAGE", "%s. %s",
1841 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02191]);
1842 }
1843
1844 // Validate consistency for signed formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001845 if (FormatIsSInt(src_format) != FormatIsSInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001846 std::stringstream ss;
1847 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has signed integer format, "
1848 << "the other one must also have signed integer format. "
1849 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1850 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1851 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02190, "IMAGE", "%s. %s",
1852 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02190]);
1853 }
1854
1855 // Validate aspect bits and formats for depth/stencil images
Dave Houlton1d2022c2017-03-29 11:43:58 -06001856 if (FormatIsDepthOrStencil(src_format) || FormatIsDepthOrStencil(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001857 if (src_format != dst_format) {
1858 std::stringstream ss;
1859 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has a format of depth, stencil or depth "
1860 << "stencil, the other one must have exactly the same format. "
1861 << "Source format is " << string_VkFormat(src_format) << " Destination format is "
1862 << string_VkFormat(dst_format);
1863 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1864 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02192, "IMAGE",
1865 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02192]);
1866 }
1867
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001868 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001869 VkImageAspectFlags srcAspect = pRegions[i].srcSubresource.aspectMask;
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001870
Dave Houlton1d2022c2017-03-29 11:43:58 -06001871 if (FormatIsDepthAndStencil(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001872 if ((srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1873 std::stringstream ss;
1874 ss << "vkCmdBlitImage: Combination depth/stencil image formats must have only one of "
1875 "VK_IMAGE_ASPECT_DEPTH_BIT "
1876 << "and VK_IMAGE_ASPECT_STENCIL_BIT set in srcImage and dstImage";
1877 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1878 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1879 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1880 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001881 } else if (FormatIsStencilOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001882 if (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT) {
1883 std::stringstream ss;
1884 ss << "vkCmdBlitImage: Stencil-only image formats must have only the VK_IMAGE_ASPECT_STENCIL_BIT "
1885 << "set in both the srcImage and dstImage";
1886 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1887 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1888 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1889 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001890 } else if (FormatIsDepthOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001891 if (srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) {
1892 std::stringstream ss;
1893 ss << "vkCmdBlitImage: Depth-only image formats must have only the VK_IMAGE_ASPECT_DEPTH "
1894 << "set in both the srcImage and dstImage";
1895 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1896 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1897 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1898 }
1899 }
1900 }
1901 }
1902
1903 // Validate filter
Dave Houlton1d2022c2017-03-29 11:43:58 -06001904 if (FormatIsDepthOrStencil(src_format) && (filter != VK_FILTER_NEAREST)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001905 std::stringstream ss;
1906 ss << "vkCmdBlitImage: If the format of srcImage is a depth, stencil, or depth stencil "
1907 << "then filter must be VK_FILTER_NEAREST.";
1908 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1909 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02193, "IMAGE", "%s. %s",
1910 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02193]);
1911 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001912 } else {
1913 assert(0);
1914 }
1915 return skip;
1916}
1917
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001918void PreCallRecordCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1919 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001920 // Update bindings between images and cmd buffer
1921 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1922 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1923
1924 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdBlitImage()"); };
1925 cb_node->validate_functions.push_back(function);
1926 function = [=]() {
1927 SetImageMemoryValid(device_data, dst_image_state, true);
1928 return false;
1929 };
1930 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001931 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_BLITIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001932}
1933
Tony Barbourdf013b92017-01-25 12:53:48 -07001934// This validates that the initial layout specified in the command buffer for
1935// the IMAGE is the same
1936// as the global IMAGE layout
Tony Barboure0c5cc92017-02-08 13:53:39 -07001937bool ValidateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
1938 std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap) {
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001939 bool skip = false;
Tony Barbourdf013b92017-01-25 12:53:48 -07001940 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001941 for (auto cb_image_data : pCB->imageLayoutMap) {
1942 VkImageLayout imageLayout;
Tony Barbourdf013b92017-01-25 12:53:48 -07001943
Jeremy Hayes55b6c292017-02-28 09:44:45 -07001944 if (FindLayout(imageLayoutMap, cb_image_data.first, imageLayout)) {
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001945 if (cb_image_data.second.initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
1946 // TODO: Set memory invalid which is in mem_tracker currently
1947 } else if (imageLayout != cb_image_data.second.initialLayout) {
1948 if (cb_image_data.first.hasSubresource) {
Dave Houltoneba86e22017-03-02 14:56:23 -07001949 skip |= log_msg(
1950 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1951 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1952 "Cannot submit cmd buffer using image (0x%" PRIx64
1953 ") [sub-resource: aspectMask 0x%X array layer %u, mip level %u], "
1954 "with layout %s when first use is %s.",
1955 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), cb_image_data.first.subresource.aspectMask,
1956 cb_image_data.first.subresource.arrayLayer, cb_image_data.first.subresource.mipLevel,
1957 string_VkImageLayout(imageLayout), string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001958 } else {
Dave Houltoneba86e22017-03-02 14:56:23 -07001959 skip |=
1960 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1961 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1962 "Cannot submit cmd buffer using image (0x%" PRIx64
1963 ") with layout %s when "
1964 "first use is %s.",
1965 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), string_VkImageLayout(imageLayout),
1966 string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001967 }
1968 }
Tony Barbourdf013b92017-01-25 12:53:48 -07001969 SetLayout(imageLayoutMap, cb_image_data.first, cb_image_data.second.layout);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001970 }
1971 }
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001972 return skip;
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001973}
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001974
Tony Barbourdf013b92017-01-25 12:53:48 -07001975void UpdateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB) {
1976 for (auto cb_image_data : pCB->imageLayoutMap) {
1977 VkImageLayout imageLayout;
1978 FindGlobalLayout(device_data, cb_image_data.first, imageLayout);
1979 SetGlobalLayout(device_data, cb_image_data.first, cb_image_data.second.layout);
1980 }
1981}
1982
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001983// Print readable FlagBits in FlagMask
1984static std::string string_VkAccessFlags(VkAccessFlags accessMask) {
1985 std::string result;
1986 std::string separator;
1987
1988 if (accessMask == 0) {
1989 result = "[None]";
1990 } else {
1991 result = "[";
1992 for (auto i = 0; i < 32; i++) {
1993 if (accessMask & (1 << i)) {
1994 result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
1995 separator = " | ";
1996 }
1997 }
1998 result = result + "]";
1999 }
2000 return result;
2001}
2002
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002003// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set. If required_bit is zero, accessMask
2004// must have at least one of 'optional_bits' set
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002005// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002006static bool ValidateMaskBits(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
2007 const VkImageLayout &layout, VkAccessFlags required_bit, VkAccessFlags optional_bits,
2008 const char *type) {
2009 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2010 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002011
2012 if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
2013 if (accessMask & ~(required_bit | optional_bits)) {
2014 // TODO: Verify against Valid Use
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002015 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2016 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002017 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2018 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002019 }
2020 } else {
2021 if (!required_bit) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002022 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2023 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002024 "%s AccessMask %d %s must contain at least one of access bits %d "
2025 "%s when layout is %s, unless the app has previously added a "
2026 "barrier for this transition.",
2027 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
2028 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002029 } else {
2030 std::string opt_bits;
2031 if (optional_bits != 0) {
2032 std::stringstream ss;
2033 ss << optional_bits;
2034 opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
2035 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002036 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2037 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002038 "%s AccessMask %d %s must have required access bit %d %s %s when "
2039 "layout is %s, unless the app has previously added a barrier for "
2040 "this transition.",
2041 type, accessMask, string_VkAccessFlags(accessMask).c_str(), required_bit,
2042 string_VkAccessFlags(required_bit).c_str(), opt_bits.c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002043 }
2044 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002045 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002046}
2047
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002048bool ValidateMaskBitsFromLayouts(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer,
2049 const VkAccessFlags &accessMask, const VkImageLayout &layout, const char *type) {
2050 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002051
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002052 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002053 switch (layout) {
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002054 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
2055 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
2056 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2057 break;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002058 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002059 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
2060 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
2061 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2062 break;
2063 }
2064 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
2065 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
2066 break;
2067 }
2068 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
2069 skip |= ValidateMaskBits(
2070 device_data, cmdBuffer, accessMask, layout, 0,
2071 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
2072 type);
2073 break;
2074 }
2075 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
2076 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, 0,
2077 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
2078 break;
2079 }
2080 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
2081 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
2082 break;
2083 }
2084 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
2085 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_MEMORY_READ_BIT, 0, type);
2086 break;
2087 }
2088 case VK_IMAGE_LAYOUT_UNDEFINED: {
2089 if (accessMask != 0) {
2090 // TODO: Verify against Valid Use section spec
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002091 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2092 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002093 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2094 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
2095 }
2096 break;
2097 }
2098 case VK_IMAGE_LAYOUT_GENERAL:
2099 default: { break; }
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002100 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002101 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002102}
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002103
2104// ValidateLayoutVsAttachmentDescription is a general function where we can validate various state associated with the
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002105// VkAttachmentDescription structs that are used by the sub-passes of a renderpass. Initial check is to make sure that READ_ONLY
2106// layout attachments don't have CLEAR as their loadOp.
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002107bool ValidateLayoutVsAttachmentDescription(const debug_report_data *report_data, const VkImageLayout first_layout,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002108 const uint32_t attachment, const VkAttachmentDescription &attachment_description) {
2109 bool skip = false;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002110 // Verify that initial loadOp on READ_ONLY attachments is not CLEAR
2111 if (attachment_description.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2112 if ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
2113 (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002114 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2115 VkDebugReportObjectTypeEXT(0), __LINE__, VALIDATION_ERROR_02351, "DS",
2116 "Cannot clear attachment %d with invalid first layout %s. %s", attachment,
2117 string_VkImageLayout(first_layout), validation_error_map[VALIDATION_ERROR_02351]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002118 }
2119 }
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002120 return skip;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002121}
2122
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002123bool ValidateLayouts(core_validation::layer_data *device_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo) {
2124 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002125 bool skip = false;
2126
2127 // Track when we're observing the first use of an attachment
2128 std::vector<bool> attach_first_use(pCreateInfo->attachmentCount, true);
2129 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
2130 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
2131 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
2132 auto attach_index = subpass.pColorAttachments[j].attachment;
2133 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2134
2135 switch (subpass.pColorAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002136 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
2137 // This is ideal.
2138 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002139
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002140 case VK_IMAGE_LAYOUT_GENERAL:
2141 // May not be optimal; TODO: reconsider this warning based on other constraints?
2142 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2143 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2144 "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
2145 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002146
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002147 default:
2148 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2149 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2150 "Layout for color attachment is %s but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.",
2151 string_VkImageLayout(subpass.pColorAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002152 }
2153
2154 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002155 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pColorAttachments[j].layout, attach_index,
2156 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002157 }
2158 attach_first_use[attach_index] = false;
2159 }
2160 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
2161 switch (subpass.pDepthStencilAttachment->layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002162 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
2163 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2164 // These are ideal.
2165 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002166
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002167 case VK_IMAGE_LAYOUT_GENERAL:
2168 // May not be optimal; TODO: reconsider this warning based on other constraints? GENERAL can be better than
2169 // doing a bunch of transitions.
2170 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2171 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2172 "GENERAL layout for depth attachment may not give optimal performance.");
2173 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002174
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002175 default:
2176 // No other layouts are acceptable
2177 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2178 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2179 "Layout for depth attachment is %s but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL, "
2180 "DEPTH_STENCIL_READ_ONLY_OPTIMAL or GENERAL.",
2181 string_VkImageLayout(subpass.pDepthStencilAttachment->layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002182 }
2183
2184 auto attach_index = subpass.pDepthStencilAttachment->attachment;
2185 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002186 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pDepthStencilAttachment->layout, attach_index,
2187 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002188 }
2189 attach_first_use[attach_index] = false;
2190 }
2191 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
2192 auto attach_index = subpass.pInputAttachments[j].attachment;
2193 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2194
2195 switch (subpass.pInputAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002196 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2197 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
2198 // These are ideal.
2199 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002200
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002201 case VK_IMAGE_LAYOUT_GENERAL:
2202 // May not be optimal. TODO: reconsider this warning based on other constraints.
2203 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2204 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2205 "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
2206 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002207
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002208 default:
2209 // No other layouts are acceptable
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002210 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2211 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002212 "Layout for input attachment is %s but can only be READ_ONLY_OPTIMAL or GENERAL.",
2213 string_VkImageLayout(subpass.pInputAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002214 }
2215
2216 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002217 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pInputAttachments[j].layout, attach_index,
2218 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002219 }
2220 attach_first_use[attach_index] = false;
2221 }
2222 }
2223 return skip;
2224}
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002225
2226// For any image objects that overlap mapped memory, verify that their layouts are PREINIT or GENERAL
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002227bool ValidateMapImageLayouts(core_validation::layer_data *device_data, VkDevice device, DEVICE_MEM_INFO const *mem_info,
2228 VkDeviceSize offset, VkDeviceSize end_offset) {
2229 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2230 bool skip = false;
2231 // Iterate over all bound image ranges and verify that for any that overlap the map ranges, the layouts are
2232 // VK_IMAGE_LAYOUT_PREINITIALIZED or VK_IMAGE_LAYOUT_GENERAL
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002233 // TODO : This can be optimized if we store ranges based on starting address and early exit when we pass our range
2234 for (auto image_handle : mem_info->bound_images) {
2235 auto img_it = mem_info->bound_ranges.find(image_handle);
2236 if (img_it != mem_info->bound_ranges.end()) {
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002237 if (rangesIntersect(device_data, &img_it->second, offset, end_offset)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002238 std::vector<VkImageLayout> layouts;
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002239 if (FindLayouts(device_data, VkImage(image_handle), layouts)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002240 for (auto layout : layouts) {
2241 if (layout != VK_IMAGE_LAYOUT_PREINITIALIZED && layout != VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002242 skip |= log_msg(
2243 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
2244 reinterpret_cast<const uint64_t &>(mem_info->mem), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2245 "Mapping an image with layout %s can result in undefined behavior if this memory is "
2246 "used by the device. Only GENERAL or PREINITIALIZED should be used.",
2247 string_VkImageLayout(layout));
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002248 }
2249 }
2250 }
2251 }
2252 }
2253 }
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002254 return skip;
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002255}
Mark Lobodzinski96210742017-02-09 10:33:46 -07002256
2257// Helper function to validate correct usage bits set for buffers or images. Verify that (actual & desired) flags != 0 or, if strict
2258// is true, verify that (actual & desired) flags == desired
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002259static bool validate_usage_flags(layer_data *device_data, VkFlags actual, VkFlags desired, VkBool32 strict, uint64_t obj_handle,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002260 VkDebugReportObjectTypeEXT obj_type, int32_t const msgCode, char const *ty_str,
2261 char const *func_name, char const *usage_str) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002262 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002263
2264 bool correct_usage = false;
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002265 bool skip = false;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002266 if (strict) {
2267 correct_usage = ((actual & desired) == desired);
2268 } else {
2269 correct_usage = ((actual & desired) != 0);
2270 }
2271 if (!correct_usage) {
2272 if (msgCode == -1) {
2273 // TODO: Fix callers with msgCode == -1 to use correct validation checks.
Dave Houltoneba86e22017-03-02 14:56:23 -07002274 skip = log_msg(
2275 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, obj_type, obj_handle, __LINE__, MEMTRACK_INVALID_USAGE_FLAG, "MEM",
2276 "Invalid usage flag for %s 0x%" PRIxLEAST64 " used by %s. In this case, %s should have %s set during creation.",
2277 ty_str, obj_handle, func_name, ty_str, usage_str);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002278 } else {
2279 const char *valid_usage = (msgCode == -1) ? "" : validation_error_map[msgCode];
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002280 skip = log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, obj_type, obj_handle, __LINE__, msgCode, "MEM",
2281 "Invalid usage flag for %s 0x%" PRIxLEAST64
2282 " used by %s. In this case, %s should have %s set during creation. %s",
2283 ty_str, obj_handle, func_name, ty_str, usage_str, valid_usage);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002284 }
2285 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002286 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002287}
2288
2289// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2290// where an error will be flagged if usage is not correct
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002291bool ValidateImageUsageFlags(layer_data *device_data, IMAGE_STATE const *image_state, VkFlags desired, VkBool32 strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002292 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002293 return validate_usage_flags(device_data, image_state->createInfo.usage, desired, strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002294 reinterpret_cast<const uint64_t &>(image_state->image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2295 msgCode, "image", func_name, usage_string);
2296}
2297
2298// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2299// where an error will be flagged if usage is not correct
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002300bool ValidateBufferUsageFlags(layer_data *device_data, BUFFER_STATE const *buffer_state, VkFlags desired, VkBool32 strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002301 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002302 return validate_usage_flags(device_data, buffer_state->createInfo.usage, desired, strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002303 reinterpret_cast<const uint64_t &>(buffer_state->buffer), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
2304 msgCode, "buffer", func_name, usage_string);
2305}
2306
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002307bool PreCallValidateCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo) {
Mark Lobodzinski96210742017-02-09 10:33:46 -07002308 bool skip = false;
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002309 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2310
Mark Lobodzinski96210742017-02-09 10:33:46 -07002311 // TODO: Add check for VALIDATION_ERROR_00658
Mark Lobodzinski96210742017-02-09 10:33:46 -07002312 // TODO: Add check for VALIDATION_ERROR_00667
2313 // TODO: Add check for VALIDATION_ERROR_00668
2314 // TODO: Add check for VALIDATION_ERROR_00669
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002315
2316 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
2317 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2318 VALIDATION_ERROR_00666, "DS",
2319 "vkCreateBuffer(): the sparseBinding device feature is disabled: Buffers cannot be created with the "
2320 "VK_BUFFER_CREATE_SPARSE_BINDING_BIT set. %s",
2321 validation_error_map[VALIDATION_ERROR_00666]);
2322 }
Mark Lobodzinskiaf355062017-03-13 09:35:01 -06002323
2324 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyBuffer)) {
2325 skip |=
2326 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2327 DRAWSTATE_INVALID_FEATURE, "DS",
2328 "vkCreateBuffer(): the sparseResidencyBuffer device feature is disabled: Buffers cannot be created with the "
2329 "VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT set.");
2330 }
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -06002331
2332 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
2333 skip |=
2334 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2335 DRAWSTATE_INVALID_FEATURE, "DS",
2336 "vkCreateBuffer(): the sparseResidencyAliased device feature is disabled: Buffers cannot be created with the "
2337 "VK_BUFFER_CREATE_SPARSE_ALIASED_BIT set.");
2338 }
Mark Lobodzinski96210742017-02-09 10:33:46 -07002339 return skip;
2340}
2341
2342void PostCallRecordCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo, VkBuffer *pBuffer) {
2343 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
2344 GetBufferMap(device_data)
2345 ->insert(std::make_pair(*pBuffer, std::unique_ptr<BUFFER_STATE>(new BUFFER_STATE(*pBuffer, pCreateInfo))));
2346}
2347
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002348bool PreCallValidateCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo) {
2349 bool skip = false;
2350 BUFFER_STATE *buffer_state = GetBufferState(device_data, pCreateInfo->buffer);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002351 // If this isn't a sparse buffer, it needs to have memory backing it at CreateBufferView time
2352 if (buffer_state) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002353 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCreateBufferView()", VALIDATION_ERROR_02522);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002354 // In order to create a valid buffer view, the buffer must have been created with at least one of the following flags:
2355 // UNIFORM_TEXEL_BUFFER_BIT or STORAGE_TEXEL_BUFFER_BIT
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002356 skip |= ValidateBufferUsageFlags(
2357 device_data, buffer_state, VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT, false,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002358 VALIDATION_ERROR_00694, "vkCreateBufferView()", "VK_BUFFER_USAGE_[STORAGE|UNIFORM]_TEXEL_BUFFER_BIT");
2359 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002360 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002361}
2362
2363void PostCallRecordCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo, VkBufferView *pView) {
2364 (*GetBufferViewMap(device_data))[*pView] = std::unique_ptr<BUFFER_VIEW_STATE>(new BUFFER_VIEW_STATE(*pView, pCreateInfo));
2365}
Mark Lobodzinski602de982017-02-09 11:01:33 -07002366
2367// For the given format verify that the aspect masks make sense
2368bool ValidateImageAspectMask(layer_data *device_data, VkImage image, VkFormat format, VkImageAspectFlags aspect_mask,
2369 const char *func_name) {
2370 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2371 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06002372 if (FormatIsColor(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002373 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
2374 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2375 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2376 "%s: Color image formats must have the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2377 validation_error_map[VALIDATION_ERROR_00741]);
2378 } else if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != aspect_mask) {
2379 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2380 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2381 "%s: Color image formats must have ONLY the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2382 validation_error_map[VALIDATION_ERROR_00741]);
2383 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002384 } else if (FormatIsDepthAndStencil(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002385 if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) == 0) {
2386 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2387 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2388 "%s: Depth/stencil image formats must have "
2389 "at least one of VK_IMAGE_ASPECT_DEPTH_BIT "
2390 "and VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2391 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2392 } else if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) != aspect_mask) {
2393 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2394 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2395 "%s: Combination depth/stencil image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT and "
2396 "VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2397 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2398 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002399 } else if (FormatIsDepthOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002400 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) {
2401 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2402 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2403 "%s: Depth-only image formats must have the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2404 validation_error_map[VALIDATION_ERROR_00741]);
2405 } else if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != aspect_mask) {
2406 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2407 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2408 "%s: Depth-only image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2409 validation_error_map[VALIDATION_ERROR_00741]);
2410 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002411 } else if (FormatIsStencilOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002412 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT) {
2413 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2414 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2415 "%s: Stencil-only image formats must have the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2416 validation_error_map[VALIDATION_ERROR_00741]);
2417 } else if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != aspect_mask) {
2418 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2419 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2420 "%s: Stencil-only image formats can have only the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2421 validation_error_map[VALIDATION_ERROR_00741]);
2422 }
2423 }
2424 return skip;
2425}
2426
2427bool ValidateImageSubrangeLevelLayerCounts(layer_data *device_data, const VkImageSubresourceRange &subresourceRange,
2428 const char *func_name) {
2429 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2430 bool skip = false;
2431 if (subresourceRange.levelCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002432 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski602de982017-02-09 11:01:33 -07002433 VALIDATION_ERROR_00768, "IMAGE", "%s called with 0 in subresourceRange.levelCount. %s", func_name,
2434 validation_error_map[VALIDATION_ERROR_00768]);
2435 }
2436 if (subresourceRange.layerCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002437 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski602de982017-02-09 11:01:33 -07002438 VALIDATION_ERROR_00769, "IMAGE", "%s called with 0 in subresourceRange.layerCount. %s", func_name,
2439 validation_error_map[VALIDATION_ERROR_00769]);
2440 }
2441 return skip;
2442}
2443
2444bool PreCallValidateCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info) {
2445 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2446 bool skip = false;
2447 IMAGE_STATE *image_state = GetImageState(device_data, create_info->image);
2448 if (image_state) {
2449 skip |= ValidateImageUsageFlags(
2450 device_data, image_state,
2451 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
2452 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
2453 false, -1, "vkCreateImageView()",
2454 "VK_IMAGE_USAGE_[SAMPLED|STORAGE|COLOR_ATTACHMENT|DEPTH_STENCIL_ATTACHMENT|INPUT_ATTACHMENT]_BIT");
2455 // If this isn't a sparse image, it needs to have memory backing it at CreateImageView time
2456 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCreateImageView()", VALIDATION_ERROR_02524);
2457 // Checks imported from image layer
Jeremy Hayesf6bfa6b2017-04-04 15:05:52 -06002458 if ((create_info->subresourceRange.baseMipLevel + create_info->subresourceRange.levelCount) >
2459 image_state->createInfo.mipLevels) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002460 std::stringstream ss;
2461 ss << "vkCreateImageView called with baseMipLevel " << create_info->subresourceRange.baseMipLevel << " for image "
2462 << create_info->image << " that only has " << image_state->createInfo.mipLevels << " mip levels.";
2463 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002464 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski602de982017-02-09 11:01:33 -07002465 VALIDATION_ERROR_00768, "IMAGE", "%s %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_00768]);
2466 }
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002467 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
2468 if (create_info->subresourceRange.baseArrayLayer >= image_state->createInfo.arrayLayers) {
2469 std::stringstream ss;
2470 ss << "vkCreateImageView called with baseArrayLayer " << create_info->subresourceRange.baseArrayLayer
2471 << " for image " << create_info->image << " that only has " << image_state->createInfo.arrayLayers
2472 << " array layers.";
2473 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2474 VALIDATION_ERROR_00769, "IMAGE", "%s %s", ss.str().c_str(),
2475 validation_error_map[VALIDATION_ERROR_00769]);
2476 }
Mark Lobodzinski602de982017-02-09 11:01:33 -07002477 }
2478 // TODO: Need new valid usage language for levelCount == 0 & layerCount == 0
2479 skip |= ValidateImageSubrangeLevelLayerCounts(device_data, create_info->subresourceRange, "vkCreateImageView()");
2480
2481 VkImageCreateFlags image_flags = image_state->createInfo.flags;
2482 VkFormat image_format = image_state->createInfo.format;
2483 VkFormat view_format = create_info->format;
2484 VkImageAspectFlags aspect_mask = create_info->subresourceRange.aspectMask;
2485
2486 // Validate VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT state
2487 if (image_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
2488 // Format MUST be compatible (in the same format compatibility class) as the format the image was created with
Dave Houlton1d2022c2017-03-29 11:43:58 -06002489 if (FormatCompatibilityClass(image_format) != FormatCompatibilityClass(view_format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002490 std::stringstream ss;
2491 ss << "vkCreateImageView(): ImageView format " << string_VkFormat(view_format)
2492 << " is not in the same format compatibility class as image (" << (uint64_t)create_info->image << ") format "
2493 << string_VkFormat(image_format) << ". Images created with the VK_IMAGE_CREATE_MUTABLE_FORMAT BIT "
2494 << "can support ImageViews with differing formats but they must be in the same compatibility class.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002495 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski602de982017-02-09 11:01:33 -07002496 VALIDATION_ERROR_02171, "IMAGE", "%s %s", ss.str().c_str(),
2497 validation_error_map[VALIDATION_ERROR_02171]);
2498 }
2499 } else {
2500 // Format MUST be IDENTICAL to the format the image was created with
2501 if (image_format != view_format) {
2502 std::stringstream ss;
2503 ss << "vkCreateImageView() format " << string_VkFormat(view_format) << " differs from image "
2504 << (uint64_t)create_info->image << " format " << string_VkFormat(image_format)
2505 << ". Formats MUST be IDENTICAL unless VK_IMAGE_CREATE_MUTABLE_FORMAT BIT was set on image creation.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002506 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Mark Lobodzinski602de982017-02-09 11:01:33 -07002507 VALIDATION_ERROR_02172, "IMAGE", "%s %s", ss.str().c_str(),
2508 validation_error_map[VALIDATION_ERROR_02172]);
2509 }
2510 }
2511
2512 // Validate correct image aspect bits for desired formats and format consistency
2513 skip |= ValidateImageAspectMask(device_data, image_state->image, image_format, aspect_mask, "vkCreateImageView()");
2514 }
2515 return skip;
2516}
2517
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -07002518void PostCallRecordCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info, VkImageView view) {
2519 auto image_view_map = GetImageViewMap(device_data);
2520 (*image_view_map)[view] = std::unique_ptr<IMAGE_VIEW_STATE>(new IMAGE_VIEW_STATE(view, create_info));
2521
2522 auto image_state = GetImageState(device_data, create_info->image);
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002523 auto &sub_res_range = (*image_view_map)[view].get()->create_info.subresourceRange;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -06002524 sub_res_range.levelCount = ResolveRemainingLevels(&sub_res_range, image_state->createInfo.mipLevels);
2525 sub_res_range.layerCount = ResolveRemainingLayers(&sub_res_range, image_state->createInfo.arrayLayers);
Mark Lobodzinski602de982017-02-09 11:01:33 -07002526}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002527
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002528bool PreCallValidateCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2529 BUFFER_STATE *dst_buffer_state) {
2530 bool skip = false;
2531 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02531);
2532 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02532);
2533 // Validate that SRC & DST buffers have correct usage flags set
2534 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01164,
2535 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
2536 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01165,
2537 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07002538 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyBuffer()",
2539 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01171);
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002540 skip |= ValidateCmd(device_data, cb_node, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
2541 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBuffer()", VALIDATION_ERROR_01172);
2542 return skip;
2543}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002544
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002545void PreCallRecordCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2546 BUFFER_STATE *dst_buffer_state) {
2547 // Update bindings between buffers and cmd buffer
2548 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
2549 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
2550
2551 std::function<bool()> function = [=]() {
2552 return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBuffer()");
2553 };
2554 cb_node->validate_functions.push_back(function);
2555 function = [=]() {
2556 SetBufferMemoryValid(device_data, dst_buffer_state, true);
2557 return false;
2558 };
2559 cb_node->validate_functions.push_back(function);
2560 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFER);
2561}
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002562
2563static bool validateIdleBuffer(layer_data *device_data, VkBuffer buffer) {
2564 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2565 bool skip = false;
2566 auto buffer_state = GetBufferState(device_data, buffer);
2567 if (!buffer_state) {
2568 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2569 __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
2570 "Cannot free buffer 0x%" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
2571 } else {
2572 if (buffer_state->in_use.load()) {
2573 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2574 __LINE__, VALIDATION_ERROR_00676, "DS",
2575 "Cannot free buffer 0x%" PRIxLEAST64 " that is in use by a command buffer. %s", (uint64_t)(buffer),
2576 validation_error_map[VALIDATION_ERROR_00676]);
2577 }
2578 }
2579 return skip;
2580}
2581
2582bool PreCallValidateDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE **image_view_state,
2583 VK_OBJECT *obj_struct) {
2584 *image_view_state = GetImageViewState(device_data, image_view);
2585 *obj_struct = {reinterpret_cast<uint64_t &>(image_view), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT};
2586 if (GetDisables(device_data)->destroy_image_view) return false;
2587 bool skip = false;
2588 if (*image_view_state) {
2589 skip |= ValidateObjectNotInUse(device_data, *image_view_state, *obj_struct, VALIDATION_ERROR_00776);
2590 }
2591 return skip;
2592}
2593
2594void PostCallRecordDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE *image_view_state,
2595 VK_OBJECT obj_struct) {
2596 // Any bound cmd buffers are now invalid
2597 invalidateCommandBuffers(device_data, image_view_state->cb_bindings, obj_struct);
2598 (*GetImageViewMap(device_data)).erase(image_view);
2599}
2600
2601bool PreCallValidateDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE **buffer_state, VK_OBJECT *obj_struct) {
2602 *buffer_state = GetBufferState(device_data, buffer);
2603 *obj_struct = {reinterpret_cast<uint64_t &>(buffer), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT};
2604 if (GetDisables(device_data)->destroy_buffer) return false;
2605 bool skip = false;
2606 if (*buffer_state) {
2607 skip |= validateIdleBuffer(device_data, buffer);
2608 }
2609 return skip;
2610}
2611
2612void PostCallRecordDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE *buffer_state, VK_OBJECT obj_struct) {
2613 invalidateCommandBuffers(device_data, buffer_state->cb_bindings, obj_struct);
2614 for (auto mem_binding : buffer_state->GetBoundMemory()) {
2615 auto mem_info = GetMemObjInfo(device_data, mem_binding);
2616 if (mem_info) {
2617 core_validation::RemoveBufferMemoryRange(reinterpret_cast<uint64_t &>(buffer), mem_info);
2618 }
2619 }
2620 ClearMemoryObjectBindings(device_data, reinterpret_cast<uint64_t &>(buffer), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT);
2621 GetBufferMap(device_data)->erase(buffer_state->buffer);
2622}
2623
2624bool PreCallValidateDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE **buffer_view_state,
2625 VK_OBJECT *obj_struct) {
2626 *buffer_view_state = GetBufferViewState(device_data, buffer_view);
2627 *obj_struct = {reinterpret_cast<uint64_t &>(buffer_view), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT};
2628 if (GetDisables(device_data)->destroy_buffer_view) return false;
2629 bool skip = false;
2630 if (*buffer_view_state) {
2631 skip |= ValidateObjectNotInUse(device_data, *buffer_view_state, *obj_struct, VALIDATION_ERROR_00701);
2632 }
2633 return skip;
2634}
2635
2636void PostCallRecordDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE *buffer_view_state,
2637 VK_OBJECT obj_struct) {
2638 // Any bound cmd buffers are now invalid
2639 invalidateCommandBuffers(device_data, buffer_view_state->cb_bindings, obj_struct);
2640 GetBufferViewMap(device_data)->erase(buffer_view);
2641}
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002642
2643bool PreCallValidateCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2644 bool skip = false;
2645 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCmdFillBuffer()", VALIDATION_ERROR_02529);
Mike Schuchardt9c582402017-02-23 15:57:37 -07002646 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdFillBuffer()",
2647 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01141);
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002648 skip |= ValidateCmd(device_data, cb_node, CMD_FILLBUFFER, "vkCmdFillBuffer()");
2649 // Validate that DST buffer has correct usage flags set
2650 skip |= ValidateBufferUsageFlags(device_data, buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01137,
2651 "vkCmdFillBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
2652 skip |= insideRenderPass(device_data, cb_node, "vkCmdFillBuffer()", VALIDATION_ERROR_01142);
2653 return skip;
2654}
2655
2656void PreCallRecordCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2657 std::function<bool()> function = [=]() {
2658 SetBufferMemoryValid(device_data, buffer_state, true);
2659 return false;
2660 };
2661 cb_node->validate_functions.push_back(function);
2662 // Update bindings between buffer and cmd buffer
2663 AddCommandBufferBindingBuffer(device_data, cb_node, buffer_state);
2664 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_FILLBUFFER);
2665}
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002666
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002667bool ValidateBufferImageCopyData(const debug_report_data *report_data, uint32_t regionCount, const VkBufferImageCopy *pRegions,
2668 IMAGE_STATE *image_state, const char *function) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002669 bool skip = false;
2670
2671 for (uint32_t i = 0; i < regionCount; i++) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002672 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) {
2673 if ((pRegions[i].imageOffset.y != 0) || (pRegions[i].imageExtent.height != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002674 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002675 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01746, "IMAGE",
2676 "%s(): pRegion[%d] imageOffset.y is %d and imageExtent.height is %d. For 1D images these "
2677 "must be 0 and 1, respectively. %s",
2678 function, i, pRegions[i].imageOffset.y, pRegions[i].imageExtent.height,
2679 validation_error_map[VALIDATION_ERROR_01746]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002680 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002681 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002682
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002683 if ((image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) || (image_state->createInfo.imageType == VK_IMAGE_TYPE_2D)) {
2684 if ((pRegions[i].imageOffset.z != 0) || (pRegions[i].imageExtent.depth != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002685 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002686 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01747, "IMAGE",
2687 "%s(): pRegion[%d] imageOffset.z is %d and imageExtent.depth is %d. For 1D and 2D images these "
2688 "must be 0 and 1, respectively. %s",
2689 function, i, pRegions[i].imageOffset.z, pRegions[i].imageExtent.depth,
2690 validation_error_map[VALIDATION_ERROR_01747]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002691 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002692 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002693
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002694 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) {
2695 if ((0 != pRegions[i].imageSubresource.baseArrayLayer) || (1 != pRegions[i].imageSubresource.layerCount)) {
2696 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2697 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01281, "IMAGE",
2698 "%s(): pRegion[%d] imageSubresource.baseArrayLayer is %d and imageSubresource.layerCount is "
2699 "%d. For 3D images these must be 0 and 1, respectively. %s",
2700 function, i, pRegions[i].imageSubresource.baseArrayLayer, pRegions[i].imageSubresource.layerCount,
2701 validation_error_map[VALIDATION_ERROR_01281]);
2702 }
2703 }
2704
2705 // If the the calling command's VkImage parameter's format is not a depth/stencil format,
2706 // then bufferOffset must be a multiple of the calling command's VkImage parameter's texel size
Dave Houlton1d2022c2017-03-29 11:43:58 -06002707 auto texel_size = FormatSize(image_state->createInfo.format);
2708 if (!FormatIsDepthAndStencil(image_state->createInfo.format) &&
2709 SafeModulo(pRegions[i].bufferOffset, texel_size) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002710 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2711 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01263, "IMAGE",
2712 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64
2713 " must be a multiple of this format's texel size (" PRINTF_SIZE_T_SPECIFIER "). %s",
2714 function, i, pRegions[i].bufferOffset, texel_size, validation_error_map[VALIDATION_ERROR_01263]);
2715 }
2716
2717 // BufferOffset must be a multiple of 4
Dave Houlton1d2022c2017-03-29 11:43:58 -06002718 if (SafeModulo(pRegions[i].bufferOffset, 4) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002719 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2720 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01264, "IMAGE",
2721 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64 " must be a multiple of 4. %s", function, i,
2722 pRegions[i].bufferOffset, validation_error_map[VALIDATION_ERROR_01264]);
2723 }
2724
2725 // BufferRowLength must be 0, or greater than or equal to the width member of imageExtent
2726 if ((pRegions[i].bufferRowLength != 0) && (pRegions[i].bufferRowLength < pRegions[i].imageExtent.width)) {
2727 skip |= log_msg(
2728 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2729 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01265, "IMAGE",
2730 "%s(): pRegion[%d] bufferRowLength (%d) must be zero or greater-than-or-equal-to imageExtent.width (%d). %s",
2731 function, i, pRegions[i].bufferRowLength, pRegions[i].imageExtent.width,
2732 validation_error_map[VALIDATION_ERROR_01265]);
2733 }
2734
2735 // BufferImageHeight must be 0, or greater than or equal to the height member of imageExtent
2736 if ((pRegions[i].bufferImageHeight != 0) && (pRegions[i].bufferImageHeight < pRegions[i].imageExtent.height)) {
2737 skip |= log_msg(
2738 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2739 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01266, "IMAGE",
2740 "%s(): pRegion[%d] bufferImageHeight (%d) must be zero or greater-than-or-equal-to imageExtent.height (%d). %s",
2741 function, i, pRegions[i].bufferImageHeight, pRegions[i].imageExtent.height,
2742 validation_error_map[VALIDATION_ERROR_01266]);
2743 }
2744
2745 // subresource aspectMask must have exactly 1 bit set
2746 const int num_bits = sizeof(VkFlags) * CHAR_BIT;
2747 std::bitset<num_bits> aspect_mask_bits(pRegions[i].imageSubresource.aspectMask);
2748 if (aspect_mask_bits.count() != 1) {
2749 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2750 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01280, "IMAGE",
2751 "%s: aspectMasks for imageSubresource in each region must have only a single bit set. %s", function,
2752 validation_error_map[VALIDATION_ERROR_01280]);
2753 }
2754
2755 // image subresource aspect bit must match format
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002756 if (!VerifyAspectsPresent(pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002757 skip |= log_msg(
2758 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2759 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01279, "IMAGE",
2760 "%s(): pRegion[%d] subresource aspectMask 0x%x specifies aspects that are not present in image format 0x%x. %s",
2761 function, i, pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format,
2762 validation_error_map[VALIDATION_ERROR_01279]);
2763 }
2764
2765 // Checks that apply only to compressed images
2766 // TODO: there is a comment in ValidateCopyBufferImageTransferGranularityRequirements() in core_validation.cpp that
2767 // reserves a place for these compressed image checks. This block of code could move there once the image
2768 // stuff is moved into core validation.
Dave Houlton1d2022c2017-03-29 11:43:58 -06002769 if (FormatIsCompressed(image_state->createInfo.format)) {
2770 auto block_size = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002771
2772 // BufferRowLength must be a multiple of block width
Dave Houlton1d2022c2017-03-29 11:43:58 -06002773 if (SafeModulo(pRegions[i].bufferRowLength, block_size.width) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002774 skip |= log_msg(
2775 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002776 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01271, "IMAGE",
2777 "%s(): pRegion[%d] bufferRowLength (%d) must be a multiple of the compressed image's texel width (%d). %s.",
2778 function, i, pRegions[i].bufferRowLength, block_size.width, validation_error_map[VALIDATION_ERROR_01271]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002779 }
2780
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002781 // BufferRowHeight must be a multiple of block height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002782 if (SafeModulo(pRegions[i].bufferImageHeight, block_size.height) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002783 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002784 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01272, "IMAGE",
2785 "%s(): pRegion[%d] bufferImageHeight (%d) must be a multiple of the compressed image's texel "
2786 "height (%d). %s.",
2787 function, i, pRegions[i].bufferImageHeight, block_size.height,
2788 validation_error_map[VALIDATION_ERROR_01272]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002789 }
2790
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002791 // image offsets must be multiples of block dimensions
Dave Houlton1d2022c2017-03-29 11:43:58 -06002792 if ((SafeModulo(pRegions[i].imageOffset.x, block_size.width) != 0) ||
2793 (SafeModulo(pRegions[i].imageOffset.y, block_size.height) != 0) ||
2794 (SafeModulo(pRegions[i].imageOffset.z, block_size.depth) != 0)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002795 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2796 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01273, "IMAGE",
2797 "%s(): pRegion[%d] imageOffset(x,y) (%d, %d) must be multiples of the compressed image's texel "
2798 "width & height (%d, %d). %s.",
2799 function, i, pRegions[i].imageOffset.x, pRegions[i].imageOffset.y, block_size.width,
2800 block_size.height, validation_error_map[VALIDATION_ERROR_01273]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002801 }
2802
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002803 // bufferOffset must be a multiple of block size (linear bytes)
Dave Houlton1d2022c2017-03-29 11:43:58 -06002804 size_t block_size_in_bytes = FormatSize(image_state->createInfo.format);
2805 if (SafeModulo(pRegions[i].bufferOffset, block_size_in_bytes) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002806 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2807 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01274, "IMAGE",
2808 "%s(): pRegion[%d] bufferOffset (0x%" PRIxLEAST64
2809 ") must be a multiple of the compressed image's texel block "
2810 "size (" PRINTF_SIZE_T_SPECIFIER "). %s.",
2811 function, i, pRegions[i].bufferOffset, block_size_in_bytes,
2812 validation_error_map[VALIDATION_ERROR_01274]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002813 }
Dave Houlton67e9b532017-03-02 17:00:10 -07002814
2815 // imageExtent width must be a multiple of block width, or extent+offset width must equal subresource width
Dave Houlton75967fc2017-03-06 17:21:16 -07002816 VkExtent3D mip_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
Dave Houlton1d2022c2017-03-29 11:43:58 -06002817 if ((SafeModulo(pRegions[i].imageExtent.width, block_size.width) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002818 (pRegions[i].imageExtent.width + pRegions[i].imageOffset.x != mip_extent.width)) {
2819 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2820 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01275, "IMAGE",
2821 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block width "
2822 "(%d), or when added to offset.x (%d) must equal the image subresource width (%d). %s.",
2823 function, i, pRegions[i].imageExtent.width, block_size.width, pRegions[i].imageOffset.x,
2824 mip_extent.width, validation_error_map[VALIDATION_ERROR_01275]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002825 }
2826
2827 // imageExtent height must be a multiple of block height, or extent+offset height must equal subresource height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002828 if ((SafeModulo(pRegions[i].imageExtent.height, block_size.height) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002829 (pRegions[i].imageExtent.height + pRegions[i].imageOffset.y != mip_extent.height)) {
2830 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2831 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01276, "IMAGE",
2832 "%s(): pRegion[%d] extent height (%d) must be a multiple of the compressed texture block height "
2833 "(%d), or when added to offset.y (%d) must equal the image subresource height (%d). %s.",
2834 function, i, pRegions[i].imageExtent.height, block_size.height, pRegions[i].imageOffset.y,
2835 mip_extent.height, validation_error_map[VALIDATION_ERROR_01276]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002836 }
2837
2838 // imageExtent depth must be a multiple of block depth, or extent+offset depth must equal subresource depth
Dave Houlton1d2022c2017-03-29 11:43:58 -06002839 if ((SafeModulo(pRegions[i].imageExtent.depth, block_size.depth) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002840 (pRegions[i].imageExtent.depth + pRegions[i].imageOffset.z != mip_extent.depth)) {
2841 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2842 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01277, "IMAGE",
2843 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block depth "
2844 "(%d), or when added to offset.z (%d) must equal the image subresource depth (%d). %s.",
2845 function, i, pRegions[i].imageExtent.depth, block_size.depth, pRegions[i].imageOffset.z,
2846 mip_extent.depth, validation_error_map[VALIDATION_ERROR_01277]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002847 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002848 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002849 }
2850
2851 return skip;
2852}
2853
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002854static bool ValidateImageBounds(const debug_report_data *report_data, const IMAGE_STATE *image_state, const uint32_t regionCount,
2855 const VkBufferImageCopy *pRegions, const char *func_name, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002856 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002857 const VkImageCreateInfo *image_info = &(image_state->createInfo);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002858
2859 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002860 VkExtent3D extent = pRegions[i].imageExtent;
2861 VkOffset3D offset = pRegions[i].imageOffset;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002862
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002863 if (IsExtentSizeZero(&extent)) // Warn on zero area subresource
2864 {
2865 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2866 (uint64_t)0, __LINE__, IMAGE_ZERO_AREA_SUBREGION, "IMAGE",
2867 "%s: pRegion[%d] imageExtent of {%1d, %1d, %1d} has zero area", func_name, i, extent.width,
2868 extent.height, extent.depth);
2869 }
2870
2871 VkExtent3D image_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
2872
2873 // If we're using a compressed format, valid extent is rounded up to multiple of block size (per 18.1)
Dave Houlton1d2022c2017-03-29 11:43:58 -06002874 if (FormatIsCompressed(image_info->format)) {
2875 auto block_extent = FormatCompressedTexelBlockExtent(image_info->format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002876 if (image_extent.width % block_extent.width) {
2877 image_extent.width += (block_extent.width - (image_extent.width % block_extent.width));
2878 }
2879 if (image_extent.height % block_extent.height) {
2880 image_extent.height += (block_extent.height - (image_extent.height % block_extent.height));
2881 }
2882 if (image_extent.depth % block_extent.depth) {
2883 image_extent.depth += (block_extent.depth - (image_extent.depth % block_extent.depth));
2884 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002885 }
2886
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002887 if (ExceedsBounds(&offset, &extent, &image_extent)) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002888 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)0,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002889 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds image bounds. %s.", func_name, i,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002890 validation_error_map[msg_code]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002891 }
2892 }
2893
2894 return skip;
2895}
2896
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002897static inline bool ValidtateBufferBounds(const debug_report_data *report_data, IMAGE_STATE *image_state, BUFFER_STATE *buff_state,
2898 uint32_t regionCount, const VkBufferImageCopy *pRegions, const char *func_name,
2899 UNIQUE_VALIDATION_ERROR_CODE msg_code) {
2900 bool skip = false;
2901
2902 VkDeviceSize buffer_size = buff_state->createInfo.size;
2903
2904 for (uint32_t i = 0; i < regionCount; i++) {
2905 VkExtent3D copy_extent = pRegions[i].imageExtent;
2906
2907 VkDeviceSize buffer_width = (0 == pRegions[i].bufferRowLength ? copy_extent.width : pRegions[i].bufferRowLength);
2908 VkDeviceSize buffer_height = (0 == pRegions[i].bufferImageHeight ? copy_extent.height : pRegions[i].bufferImageHeight);
Dave Houlton1d2022c2017-03-29 11:43:58 -06002909 VkDeviceSize unit_size = FormatSize(image_state->createInfo.format); // size (bytes) of texel or block
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002910
Dave Houltonf3229d52017-02-21 15:59:08 -07002911 // Handle special buffer packing rules for specific depth/stencil formats
2912 if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Dave Houlton1d2022c2017-03-29 11:43:58 -06002913 unit_size = FormatSize(VK_FORMAT_S8_UINT);
Dave Houltonf3229d52017-02-21 15:59:08 -07002914 } else if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
2915 switch (image_state->createInfo.format) {
2916 case VK_FORMAT_D16_UNORM_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06002917 unit_size = FormatSize(VK_FORMAT_D16_UNORM);
Dave Houltonf3229d52017-02-21 15:59:08 -07002918 break;
2919 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06002920 unit_size = FormatSize(VK_FORMAT_D32_SFLOAT);
Dave Houltonf3229d52017-02-21 15:59:08 -07002921 break;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002922 case VK_FORMAT_X8_D24_UNORM_PACK32: // Fall through
Dave Houltonf3229d52017-02-21 15:59:08 -07002923 case VK_FORMAT_D24_UNORM_S8_UINT:
2924 unit_size = 4;
2925 break;
2926 default:
2927 break;
2928 }
2929 }
2930
Dave Houlton1d2022c2017-03-29 11:43:58 -06002931 if (FormatIsCompressed(image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002932 // Switch to texel block units, rounding up for any partially-used blocks
Dave Houlton1d2022c2017-03-29 11:43:58 -06002933 auto block_dim = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002934 buffer_width = (buffer_width + block_dim.width - 1) / block_dim.width;
2935 buffer_height = (buffer_height + block_dim.height - 1) / block_dim.height;
2936
2937 copy_extent.width = (copy_extent.width + block_dim.width - 1) / block_dim.width;
2938 copy_extent.height = (copy_extent.height + block_dim.height - 1) / block_dim.height;
2939 copy_extent.depth = (copy_extent.depth + block_dim.depth - 1) / block_dim.depth;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002940 }
2941
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002942 // Either depth or layerCount may be greater than 1 (not both). This is the number of 'slices' to copy
2943 uint32_t z_copies = std::max(copy_extent.depth, pRegions[i].imageSubresource.layerCount);
2944 if (IsExtentSizeZero(&copy_extent) || (0 == z_copies)) {
2945 // TODO: Issure warning here? Already warned in ValidateImageBounds()...
2946 } else {
2947 // Calculate buffer offset of final copied byte, + 1.
2948 VkDeviceSize max_buffer_offset = (z_copies - 1) * buffer_height * buffer_width; // offset to slice
2949 max_buffer_offset += ((copy_extent.height - 1) * buffer_width) + copy_extent.width; // add row,col
2950 max_buffer_offset *= unit_size; // convert to bytes
2951 max_buffer_offset += pRegions[i].bufferOffset; // add initial offset (bytes)
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002952
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002953 if (buffer_size < max_buffer_offset) {
2954 skip |=
2955 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)0,
2956 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds buffer size of %" PRIu64 " bytes. %s.", func_name,
2957 i, buffer_size, validation_error_map[msg_code]);
2958 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002959 }
2960 }
2961
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002962 return skip;
2963}
2964
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002965bool PreCallValidateCmdCopyImageToBuffer(layer_data *device_data, VkImageLayout srcImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002966 IMAGE_STATE *src_image_state, BUFFER_STATE *dst_buffer_state, uint32_t regionCount,
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002967 const VkBufferImageCopy *pRegions, const char *func_name) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002968 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2969 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, src_image_state, "vkCmdCopyImageToBuffer");
2970
2971 // Validate command buffer state
2972 if (CB_RECORDING != cb_node->state) {
2973 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2974 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01258, "DS",
2975 "Cannot call vkCmdCopyImageToBuffer() on command buffer which is not in recording state. %s.",
2976 validation_error_map[VALIDATION_ERROR_01258]);
2977 } else {
2978 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYIMAGETOBUFFER);
2979 }
2980
2981 // Command pool must support graphics, compute, or transfer operations
2982 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
2983
2984 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
2985 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
2986 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2987 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01259, "DS",
2988 "Cannot call vkCmdCopyImageToBuffer() on a command buffer allocated from a pool without graphics, compute, "
2989 "or transfer capabilities. %s.",
2990 validation_error_map[VALIDATION_ERROR_01259]);
2991 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002992 skip |= ValidateImageBounds(report_data, src_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002993 VALIDATION_ERROR_01245);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002994 skip |= ValidtateBufferBounds(report_data, src_image_state, dst_buffer_state, regionCount, pRegions, "vkCmdCopyImageToBuffer()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002995 VALIDATION_ERROR_01246);
2996
2997 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyImageToBuffer(): srcImage",
2998 VALIDATION_ERROR_01249);
2999 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02537);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003000 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02538);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003001
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003002 // Validate that SRC image & DST buffer have correct usage flags set
3003 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01248,
3004 "vkCmdCopyImageToBuffer()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003005 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01252,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003006 "vkCmdCopyImageToBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003007 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01260);
3008 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06003009 skip |= VerifyImageLayout(device_data, cb_node, src_image_state, pRegions[i].imageSubresource, srcImageLayout,
3010 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01251);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003011 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, src_image_state, &pRegions[i], i,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06003012 "vkCmdCopyImageToBuffer()");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003013 }
3014 return skip;
3015}
3016
3017void PreCallRecordCmdCopyImageToBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003018 BUFFER_STATE *dst_buffer_state, uint32_t region_count, const VkBufferImageCopy *regions,
3019 VkImageLayout src_image_layout) {
3020 // Make sure that all image slices are updated to correct layout
3021 for (uint32_t i = 0; i < region_count; ++i) {
3022 SetImageLayout(device_data, cb_node, src_image_state, regions[i].imageSubresource, src_image_layout);
3023 }
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003024 // Update bindings between buffer/image and cmd buffer
3025 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003026 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003027
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003028 std::function<bool()> function = [=]() {
3029 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImageToBuffer()");
3030 };
3031 cb_node->validate_functions.push_back(function);
3032 function = [=]() {
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003033 SetBufferMemoryValid(device_data, dst_buffer_state, true);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003034 return false;
3035 };
3036 cb_node->validate_functions.push_back(function);
3037
3038 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGETOBUFFER);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003039}
3040
3041bool PreCallValidateCmdCopyBufferToImage(layer_data *device_data, VkImageLayout dstImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003042 BUFFER_STATE *src_buffer_state, IMAGE_STATE *dst_image_state, uint32_t regionCount,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003043 const VkBufferImageCopy *pRegions, const char *func_name) {
3044 const debug_report_data *report_data = core_validation::GetReportData(device_data);
3045 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, dst_image_state, "vkCmdCopyBufferToImage");
3046
3047 // Validate command buffer state
3048 if (CB_RECORDING != cb_node->state) {
3049 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3050 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01240, "DS",
3051 "Cannot call vkCmdCopyBufferToImage() on command buffer which is not in recording state. %s.",
3052 validation_error_map[VALIDATION_ERROR_01240]);
3053 } else {
3054 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYBUFFERTOIMAGE);
3055 }
3056
3057 // Command pool must support graphics, compute, or transfer operations
3058 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
3059 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
3060 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
3061 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3062 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01241, "DS",
3063 "Cannot call vkCmdCopyBufferToImage() on a command buffer allocated from a pool without graphics, compute, "
3064 "or transfer capabilities. %s.",
3065 validation_error_map[VALIDATION_ERROR_01241]);
3066 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003067 skip |= ValidateImageBounds(report_data, dst_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003068 VALIDATION_ERROR_01228);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003069 skip |= ValidtateBufferBounds(report_data, dst_image_state, src_buffer_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003070 VALIDATION_ERROR_01227);
3071 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyBufferToImage(): dstImage",
3072 VALIDATION_ERROR_01232);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003073 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02535);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003074 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02536);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003075 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01230,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003076 "vkCmdCopyBufferToImage()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
3077 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01231,
3078 "vkCmdCopyBufferToImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003079 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01242);
3080 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06003081 skip |= VerifyImageLayout(device_data, cb_node, dst_image_state, pRegions[i].imageSubresource, dstImageLayout,
3082 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01234);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003083 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &pRegions[i], i,
3084 "vkCmdCopyBufferToImage()");
3085 }
3086 return skip;
3087}
3088
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003089void PreCallRecordCmdCopyBufferToImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003090 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkBufferImageCopy *regions,
3091 VkImageLayout dst_image_layout) {
3092 // Make sure that all image slices are updated to correct layout
3093 for (uint32_t i = 0; i < region_count; ++i) {
3094 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].imageSubresource, dst_image_layout);
3095 }
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003096 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003097 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003098 std::function<bool()> function = [=]() {
3099 SetImageMemoryValid(device_data, dst_image_state, true);
3100 return false;
3101 };
3102 cb_node->validate_functions.push_back(function);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003103 function = [=]() { return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBufferToImage()"); };
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003104 cb_node->validate_functions.push_back(function);
3105
3106 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFERTOIMAGE);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003107}
Mike Weiblen672b58b2017-02-21 14:32:53 -07003108
3109bool PreCallValidateGetImageSubresourceLayout(layer_data *device_data, VkImage image, const VkImageSubresource *pSubresource) {
3110 const auto report_data = core_validation::GetReportData(device_data);
3111 bool skip = false;
3112 const VkImageAspectFlags sub_aspect = pSubresource->aspectMask;
3113
3114 // VU 00733: The aspectMask member of pSubresource must only have a single bit set
3115 const int num_bits = sizeof(sub_aspect) * CHAR_BIT;
3116 std::bitset<num_bits> aspect_mask_bits(sub_aspect);
3117 if (aspect_mask_bits.count() != 1) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003118 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3119 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00733, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003120 "vkGetImageSubresourceLayout(): VkImageSubresource.aspectMask must have exactly 1 bit set. %s",
3121 validation_error_map[VALIDATION_ERROR_00733]);
3122 }
3123
3124 IMAGE_STATE *image_entry = GetImageState(device_data, image);
3125 if (!image_entry) {
3126 return skip;
3127 }
3128
3129 // VU 00732: image must have been created with tiling equal to VK_IMAGE_TILING_LINEAR
3130 if (image_entry->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003131 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3132 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00732, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003133 "vkGetImageSubresourceLayout(): Image must have tiling of VK_IMAGE_TILING_LINEAR. %s",
3134 validation_error_map[VALIDATION_ERROR_00732]);
3135 }
3136
3137 // VU 00739: mipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
3138 if (pSubresource->mipLevel >= image_entry->createInfo.mipLevels) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003139 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3140 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00739, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003141 "vkGetImageSubresourceLayout(): pSubresource.mipLevel (%d) must be less than %d. %s",
3142 pSubresource->mipLevel, image_entry->createInfo.mipLevels, validation_error_map[VALIDATION_ERROR_00739]);
3143 }
3144
3145 // VU 00740: arrayLayer must be less than the arrayLayers specified in VkImageCreateInfo when the image was created
3146 if (pSubresource->arrayLayer >= image_entry->createInfo.arrayLayers) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003147 skip |=
3148 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3149 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00740, "IMAGE",
3150 "vkGetImageSubresourceLayout(): pSubresource.arrayLayer (%d) must be less than %d. %s",
3151 pSubresource->arrayLayer, image_entry->createInfo.arrayLayers, validation_error_map[VALIDATION_ERROR_00740]);
Mike Weiblen672b58b2017-02-21 14:32:53 -07003152 }
3153
3154 // VU 00741: subresource's aspect must be compatible with image's format.
3155 const VkFormat img_format = image_entry->createInfo.format;
Dave Houlton1d2022c2017-03-29 11:43:58 -06003156 if (FormatIsColor(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003157 if (sub_aspect != VK_IMAGE_ASPECT_COLOR_BIT) {
3158 skip |= log_msg(
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003159 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3160 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003161 "vkGetImageSubresourceLayout(): For color formats, VkImageSubresource.aspectMask must be VK_IMAGE_ASPECT_COLOR. %s",
3162 validation_error_map[VALIDATION_ERROR_00741]);
3163 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06003164 } else if (FormatIsDepthOrStencil(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003165 if ((sub_aspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (sub_aspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003166 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3167 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003168 "vkGetImageSubresourceLayout(): For depth/stencil formats, VkImageSubresource.aspectMask must be "
3169 "either VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT. %s",
3170 validation_error_map[VALIDATION_ERROR_00741]);
3171 }
3172 }
3173 return skip;
3174}