blob: 6e1c927f6552887120414f016abf194b45443bef [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)) {
243 if (vk_format_is_depth_and_stencil(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;
291 IMAGE_CMD_BUF_LAYOUT_NODE newNode = {pRenderPassInfo->pAttachments[i].initialLayout,
292 pRenderPassInfo->pAttachments[i].initialLayout};
293 // TODO: Do not iterate over every possibility - consolidate where possible
Tobin Ehlis2b716cb2017-02-16 13:21:20 -0700294 // TODO: Consolidate this with SetImageViewLayout() function above
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700295 for (uint32_t j = 0; j < subRange.levelCount; j++) {
296 uint32_t level = subRange.baseMipLevel + j;
297 for (uint32_t k = 0; k < subRange.layerCount; k++) {
298 uint32_t layer = subRange.baseArrayLayer + k;
299 VkImageSubresource sub = {subRange.aspectMask, level, layer};
300 IMAGE_CMD_BUF_LAYOUT_NODE node;
301 if (!FindCmdBufLayout(device_data, pCB, image, sub, node)) {
Tobin Ehlis2b716cb2017-02-16 13:21:20 -0700302 // If ImageView was created with depth or stencil, transition both aspects if it's a DS image
303 if (subRange.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
304 if (vk_format_is_depth_and_stencil(view_state->create_info.format)) {
305 sub.aspectMask |= (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
306 }
307 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700308 SetLayout(device_data, pCB, image, sub, newNode);
309 continue;
310 }
311 if (newNode.layout != VK_IMAGE_LAYOUT_UNDEFINED && newNode.layout != node.layout) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600312 skip_call |=
313 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
314 reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
315 "You cannot start a render pass using attachment %u "
316 "where the render pass initial layout is %s and the previous "
317 "known layout of the attachment is %s. The layouts must match, or "
318 "the render pass initial layout for the attachment must be "
319 "VK_IMAGE_LAYOUT_UNDEFINED",
320 i, string_VkImageLayout(newNode.layout), string_VkImageLayout(node.layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700321 }
322 }
323 }
324 }
325 return skip_call;
326}
327
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700328void TransitionAttachmentRefLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, FRAMEBUFFER_STATE *pFramebuffer,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700329 VkAttachmentReference ref) {
330 if (ref.attachment != VK_ATTACHMENT_UNUSED) {
331 auto image_view = pFramebuffer->createInfo.pAttachments[ref.attachment];
332 SetImageViewLayout(device_data, pCB, image_view, ref.layout);
333 }
334}
335
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700336void TransitionSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkRenderPassBeginInfo *pRenderPassBegin,
337 const int subpass_index, FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700338 auto renderPass = GetRenderPassState(device_data, pRenderPassBegin->renderPass);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700339 if (!renderPass) return;
340
341 if (framebuffer_state) {
342 auto const &subpass = renderPass->createInfo.pSubpasses[subpass_index];
343 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
344 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pInputAttachments[j]);
345 }
346 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
347 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pColorAttachments[j]);
348 }
349 if (subpass.pDepthStencilAttachment) {
350 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, *subpass.pDepthStencilAttachment);
351 }
352 }
353}
354
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700355bool ValidateImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
356 uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700357 if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
358 return false;
359 }
360 VkImageSubresource sub = {aspect, level, layer};
361 IMAGE_CMD_BUF_LAYOUT_NODE node;
362 if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700363 return false;
364 }
365 bool skip = false;
366 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
367 // TODO: Set memory invalid which is in mem_tracker currently
368 } else if (node.layout != mem_barrier->oldLayout) {
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600369 skip |=
370 log_msg(core_validation::GetReportData(device_data), VK_DEBUG_REPORT_ERROR_BIT_EXT,
371 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__,
372 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
373 "For image 0x%" PRIxLEAST64 " you cannot transition the layout of aspect %d from %s when current layout is %s.",
374 reinterpret_cast<const uint64_t &>(mem_barrier->image), aspect, string_VkImageLayout(mem_barrier->oldLayout),
375 string_VkImageLayout(node.layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700376 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700377 return skip;
378}
379
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700380void TransitionImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
381 uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
382 if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
383 return;
384 }
385 VkImageSubresource sub = {aspect, level, layer};
386 IMAGE_CMD_BUF_LAYOUT_NODE node;
387 if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
388 SetLayout(device_data, pCB, mem_barrier->image, sub,
389 IMAGE_CMD_BUF_LAYOUT_NODE(mem_barrier->oldLayout, mem_barrier->newLayout));
390 return;
391 }
392 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
393 // TODO: Set memory invalid
394 }
395 SetLayout(device_data, pCB, mem_barrier->image, sub, mem_barrier->newLayout);
396}
397
Dave Houlton10b39482017-03-16 13:18:15 -0600398bool VerifyAspectsPresent(VkImageAspectFlags aspect_mask, VkFormat format) {
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600399 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != 0) {
400 if (!vk_format_is_color(format)) return false;
401 }
402 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0) {
403 if (!vk_format_has_depth(format)) return false;
404 }
405 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0) {
406 if (!vk_format_has_stencil(format)) return false;
407 }
408 return true;
409}
410
Mike Weiblen62d08a32017-03-07 22:18:27 -0700411// Verify an ImageMemoryBarrier's old/new ImageLayouts are compatible with the Image's ImageUsageFlags.
412bool ValidateBarrierLayoutToImageUsage(layer_data *device_data, const VkImageMemoryBarrier *img_barrier, bool new_not_old,
413 VkImageUsageFlags usage_flags, const char *func_name) {
414 const auto report_data = core_validation::GetReportData(device_data);
415 bool skip = false;
416 const VkImageLayout layout = (new_not_old) ? img_barrier->newLayout : img_barrier->oldLayout;
417 UNIQUE_VALIDATION_ERROR_CODE msg_code = VALIDATION_ERROR_UNDEFINED; // sentinel value meaning "no error"
418
419 switch (layout) {
420 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
421 if ((usage_flags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) == 0) {
422 msg_code = VALIDATION_ERROR_00303;
423 }
424 break;
425 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
426 if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
427 msg_code = VALIDATION_ERROR_00304;
428 }
429 break;
430 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
431 if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
432 msg_code = VALIDATION_ERROR_00305;
433 }
434 break;
435 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
436 if ((usage_flags & (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) == 0) {
437 msg_code = VALIDATION_ERROR_00306;
438 }
439 break;
440 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
441 if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) == 0) {
442 msg_code = VALIDATION_ERROR_00307;
443 }
444 break;
445 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
446 if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) == 0) {
447 msg_code = VALIDATION_ERROR_00308;
448 }
449 break;
450 default:
451 // Other VkImageLayout values do not have VUs defined in this context.
452 break;
453 }
454
455 if (msg_code != VALIDATION_ERROR_UNDEFINED) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600456 skip |=
457 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
458 reinterpret_cast<const uint64_t &>(img_barrier->image), __LINE__, msg_code, "DS",
459 "%s: Image barrier 0x%p %sLayout=%s is not compatible with image 0x%" PRIx64 " usage flags 0x%" PRIx32 ". %s",
460 func_name, img_barrier, ((new_not_old) ? "new" : "old"), string_VkImageLayout(layout),
461 reinterpret_cast<const uint64_t &>(img_barrier->image), usage_flags, validation_error_map[msg_code]);
Mike Weiblen62d08a32017-03-07 22:18:27 -0700462 }
463 return skip;
464}
465
466// Verify image barriers are compatible with the images they reference.
467bool ValidateBarriersToImages(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t imageMemoryBarrierCount,
468 const VkImageMemoryBarrier *pImageMemoryBarriers, const char *func_name) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700469 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700470 bool skip = false;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700471
Mike Weiblen62d08a32017-03-07 22:18:27 -0700472 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
473 auto img_barrier = &pImageMemoryBarriers[i];
474 if (!img_barrier) continue;
475
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600476 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, img_barrier->image)->createInfo);
477 uint32_t level_count = ResolveRemainingLevels(&img_barrier->subresourceRange, image_create_info->mipLevels);
478 uint32_t layer_count = ResolveRemainingLayers(&img_barrier->subresourceRange, image_create_info->arrayLayers);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700479
Mike Weiblen62d08a32017-03-07 22:18:27 -0700480 for (uint32_t j = 0; j < level_count; j++) {
481 uint32_t level = img_barrier->subresourceRange.baseMipLevel + j;
482 for (uint32_t k = 0; k < layer_count; k++) {
483 uint32_t layer = img_barrier->subresourceRange.baseArrayLayer + k;
484 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
485 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
486 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
487 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700488 }
489 }
Mike Weiblen62d08a32017-03-07 22:18:27 -0700490
491 IMAGE_STATE *image_state = GetImageState(device_data, img_barrier->image);
492 if (image_state) {
493 VkImageUsageFlags usage_flags = image_state->createInfo.usage;
494 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, false, usage_flags, func_name);
495 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, true, usage_flags, func_name);
496 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700497 }
498 return skip;
499}
500
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700501void TransitionImageLayouts(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
502 const VkImageMemoryBarrier *pImgMemBarriers) {
503 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700504
505 for (uint32_t i = 0; i < memBarrierCount; ++i) {
506 auto mem_barrier = &pImgMemBarriers[i];
507 if (!mem_barrier) continue;
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700508
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600509 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, mem_barrier->image)->createInfo);
510 uint32_t level_count = ResolveRemainingLevels(&mem_barrier->subresourceRange, image_create_info->mipLevels);
511 uint32_t layer_count = ResolveRemainingLayers(&mem_barrier->subresourceRange, image_create_info->arrayLayers);
512
513 for (uint32_t j = 0; j < level_count; j++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700514 uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600515 for (uint32_t k = 0; k < layer_count; k++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700516 uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
517 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
518 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
519 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
520 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
521 }
522 }
523 }
524}
525
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600526bool VerifyImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
527 VkImageSubresourceLayers subLayers, VkImageLayout explicit_layout, VkImageLayout optimal_layout,
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600528 const char *caller, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700529 const auto report_data = core_validation::GetReportData(device_data);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600530 const auto image = image_state->image;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700531 bool skip_call = false;
532
533 for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
534 uint32_t layer = i + subLayers.baseArrayLayer;
535 VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
536 IMAGE_CMD_BUF_LAYOUT_NODE node;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600537 if (FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
538 if (node.layout != explicit_layout) {
539 // TODO: Improve log message in the next pass
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600540 skip_call |=
541 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
542 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600543 "%s: Cannot use image 0x%" PRIxLEAST64
544 " with specific layout %s that doesn't match the actual current layout %s.",
545 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
546 string_VkImageLayout(node.layout));
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600547 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700548 }
549 }
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600550 // If optimal_layout is not UNDEFINED, check that layout matches optimal for this case
551 if ((VK_IMAGE_LAYOUT_UNDEFINED != optimal_layout) && (explicit_layout != optimal_layout)) {
552 if (VK_IMAGE_LAYOUT_GENERAL == explicit_layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700553 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
554 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600555 skip_call |= log_msg(
556 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
557 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
558 "%s: For optimal performance image 0x%" PRIxLEAST64 " layout should be %s instead of GENERAL.", caller,
559 reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(optimal_layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700560 }
561 } else {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600562 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
563 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, msg_code, "DS",
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600564 "%s: Layout for image 0x%" PRIxLEAST64 " is %s but can only be %s or VK_IMAGE_LAYOUT_GENERAL. %s",
565 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
566 string_VkImageLayout(optimal_layout), validation_error_map[msg_code]);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700567 }
568 }
569 return skip_call;
570}
571
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700572void TransitionFinalSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkRenderPassBeginInfo *pRenderPassBegin,
573 FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700574 auto renderPass = GetRenderPassState(device_data, pRenderPassBegin->renderPass);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700575 if (!renderPass) return;
576
577 const VkRenderPassCreateInfo *pRenderPassInfo = renderPass->createInfo.ptr();
578 if (framebuffer_state) {
579 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
580 auto image_view = framebuffer_state->createInfo.pAttachments[i];
581 SetImageViewLayout(device_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
582 }
583 }
584}
585
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700586bool PreCallValidateCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700587 const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
588 bool skip_call = false;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700589 const debug_report_data *report_data = core_validation::GetReportData(device_data);
590
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600591 if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700592 skip_call |=
593 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
594 VALIDATION_ERROR_00715, "IMAGE", "vkCreateImage: VkFormat for image must not be VK_FORMAT_UNDEFINED. %s",
595 validation_error_map[VALIDATION_ERROR_00715]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600596
597 return skip_call;
598 }
599
600 const VkFormatProperties *properties = GetFormatProperties(device_data, pCreateInfo->format);
601
602 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) && (properties->linearTilingFeatures == 0)) {
603 std::stringstream ss;
604 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
605 skip_call |=
606 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
607 VALIDATION_ERROR_02150, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02150]);
608
609 return skip_call;
610 }
611
612 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) && (properties->optimalTilingFeatures == 0)) {
613 std::stringstream ss;
614 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
615 skip_call |=
616 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
617 VALIDATION_ERROR_02155, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02155]);
618
619 return skip_call;
620 }
621
622 // Validate that format supports usage as color attachment
623 if (pCreateInfo->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
624 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
625 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
626 std::stringstream ss;
627 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
628 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
629 skip_call |=
630 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
631 VALIDATION_ERROR_02158, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02158]);
632 }
633 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
634 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
635 std::stringstream ss;
636 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
637 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
638 skip_call |=
639 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
640 VALIDATION_ERROR_02153, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02153]);
641 }
642 }
643
644 // Validate that format supports usage as depth/stencil attachment
645 if (pCreateInfo->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
646 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
647 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
648 std::stringstream ss;
649 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
650 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
651 skip_call |=
652 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
653 VALIDATION_ERROR_02159, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02159]);
654 }
655 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
656 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
657 std::stringstream ss;
658 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
659 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
660 skip_call |=
661 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
662 VALIDATION_ERROR_02154, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02154]);
663 }
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700664 }
665
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700666 const VkImageFormatProperties *ImageFormatProperties = GetImageFormatProperties(
667 device_data, pCreateInfo->format, pCreateInfo->imageType, pCreateInfo->tiling, pCreateInfo->usage, pCreateInfo->flags);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700668
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700669 VkDeviceSize imageGranularity = GetPhysicalDeviceProperties(device_data)->limits.bufferImageGranularity;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700670 imageGranularity = imageGranularity == 1 ? 0 : imageGranularity;
671
Mark Lobodzinski688ed322017-01-27 11:13:21 -0700672 if ((pCreateInfo->extent.width <= 0) || (pCreateInfo->extent.height <= 0) || (pCreateInfo->extent.depth <= 0)) {
673 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
674 VALIDATION_ERROR_00716, "Image",
675 "CreateImage extent is 0 for at least one required dimension for image: "
676 "Width = %d Height = %d Depth = %d. %s",
677 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
678 validation_error_map[VALIDATION_ERROR_00716]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700679 }
680
681 // TODO: VALIDATION_ERROR_02125 VALIDATION_ERROR_02126 VALIDATION_ERROR_02128 VALIDATION_ERROR_00720
682 // All these extent-related VUs should be checked here
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700683 if ((pCreateInfo->extent.depth > ImageFormatProperties->maxExtent.depth) ||
684 (pCreateInfo->extent.width > ImageFormatProperties->maxExtent.width) ||
685 (pCreateInfo->extent.height > ImageFormatProperties->maxExtent.height)) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700686 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
687 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
688 "CreateImage extents exceed allowable limits for format: "
689 "Width = %d Height = %d Depth = %d: Limits for Width = %d Height = %d Depth = %d for format %s.",
690 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700691 ImageFormatProperties->maxExtent.width, ImageFormatProperties->maxExtent.height,
692 ImageFormatProperties->maxExtent.depth, string_VkFormat(pCreateInfo->format));
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700693 }
694
695 uint64_t totalSize = ((uint64_t)pCreateInfo->extent.width * (uint64_t)pCreateInfo->extent.height *
696 (uint64_t)pCreateInfo->extent.depth * (uint64_t)pCreateInfo->arrayLayers *
697 (uint64_t)pCreateInfo->samples * (uint64_t)vk_format_get_size(pCreateInfo->format) +
698 (uint64_t)imageGranularity) &
699 ~(uint64_t)imageGranularity;
700
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700701 if (totalSize > ImageFormatProperties->maxResourceSize) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700702 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
703 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
704 "CreateImage resource size exceeds allowable maximum "
705 "Image resource size = 0x%" PRIxLEAST64 ", maximum resource size = 0x%" PRIxLEAST64 " ",
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700706 totalSize, ImageFormatProperties->maxResourceSize);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700707 }
708
709 // TODO: VALIDATION_ERROR_02132
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700710 if (pCreateInfo->mipLevels > ImageFormatProperties->maxMipLevels) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700711 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
712 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
713 "CreateImage mipLevels=%d exceeds allowable maximum supported by format of %d", pCreateInfo->mipLevels,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700714 ImageFormatProperties->maxMipLevels);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700715 }
716
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700717 if (pCreateInfo->arrayLayers > ImageFormatProperties->maxArrayLayers) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700718 skip_call |= log_msg(
719 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__, VALIDATION_ERROR_02133,
720 "Image", "CreateImage arrayLayers=%d exceeds allowable maximum supported by format of %d. %s", pCreateInfo->arrayLayers,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700721 ImageFormatProperties->maxArrayLayers, validation_error_map[VALIDATION_ERROR_02133]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700722 }
723
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700724 if ((pCreateInfo->samples & ImageFormatProperties->sampleCounts) == 0) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700725 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
726 VALIDATION_ERROR_02138, "Image", "CreateImage samples %s is not supported by format 0x%.8X. %s",
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700727 string_VkSampleCountFlagBits(pCreateInfo->samples), ImageFormatProperties->sampleCounts,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700728 validation_error_map[VALIDATION_ERROR_02138]);
729 }
730
731 if (pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_UNDEFINED && pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_PREINITIALIZED) {
732 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
733 VALIDATION_ERROR_00731, "Image",
734 "vkCreateImage parameter, pCreateInfo->initialLayout, must be VK_IMAGE_LAYOUT_UNDEFINED or "
735 "VK_IMAGE_LAYOUT_PREINITIALIZED. %s",
736 validation_error_map[VALIDATION_ERROR_00731]);
737 }
738
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600739 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
740 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
741 VALIDATION_ERROR_02143, "DS",
742 "vkCreateImage(): the sparseBinding device feature is disabled: Images cannot be created with the "
743 "VK_IMAGE_CREATE_SPARSE_BINDING_BIT set. %s",
744 validation_error_map[VALIDATION_ERROR_02143]);
745 }
746
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600747 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
748 skip_call |=
749 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
750 DRAWSTATE_INVALID_FEATURE, "DS",
751 "vkCreateImage(): the sparseResidencyAliased device feature is disabled: Images cannot be created with the "
752 "VK_IMAGE_CREATE_SPARSE_ALIASED_BIT set.");
753 }
754
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700755 return skip_call;
756}
757
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700758void PostCallRecordCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo, VkImage *pImage) {
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700759 IMAGE_LAYOUT_NODE image_state;
760 image_state.layout = pCreateInfo->initialLayout;
761 image_state.format = pCreateInfo->format;
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700762 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 -0700763 ImageSubresourcePair subpair{*pImage, false, VkImageSubresource()};
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700764 (*core_validation::GetImageSubresourceMap(device_data))[*pImage].push_back(subpair);
765 (*core_validation::GetImageLayoutMap(device_data))[subpair] = image_state;
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700766}
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700767
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700768bool PreCallValidateDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE **image_state, VK_OBJECT *obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700769 const CHECK_DISABLED *disabled = core_validation::GetDisables(device_data);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700770 *image_state = core_validation::GetImageState(device_data, image);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700771 *obj_struct = {reinterpret_cast<uint64_t &>(image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT};
772 if (disabled->destroy_image) return false;
773 bool skip = false;
774 if (*image_state) {
775 skip |= core_validation::ValidateObjectNotInUse(device_data, *image_state, *obj_struct, VALIDATION_ERROR_00743);
776 }
777 return skip;
778}
779
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700780void PostCallRecordDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE *image_state, VK_OBJECT obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700781 core_validation::invalidateCommandBuffers(device_data, image_state->cb_bindings, obj_struct);
782 // Clean up memory mapping, bindings and range references for image
783 for (auto mem_binding : image_state->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700784 auto mem_info = core_validation::GetMemObjInfo(device_data, mem_binding);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700785 if (mem_info) {
786 core_validation::RemoveImageMemoryRange(obj_struct.handle, mem_info);
787 }
788 }
789 core_validation::ClearMemoryObjectBindings(device_data, obj_struct.handle, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
790 // Remove image from imageMap
791 core_validation::GetImageMap(device_data)->erase(image);
792 std::unordered_map<VkImage, std::vector<ImageSubresourcePair>> *imageSubresourceMap =
793 core_validation::GetImageSubresourceMap(device_data);
794
795 const auto &sub_entry = imageSubresourceMap->find(image);
796 if (sub_entry != imageSubresourceMap->end()) {
797 for (const auto &pair : sub_entry->second) {
798 core_validation::GetImageLayoutMap(device_data)->erase(pair);
799 }
800 imageSubresourceMap->erase(sub_entry);
801 }
802}
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700803
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700804bool ValidateImageAttributes(layer_data *device_data, IMAGE_STATE *image_state, VkImageSubresourceRange range) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700805 bool skip = false;
806 const debug_report_data *report_data = core_validation::GetReportData(device_data);
807
808 if (range.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) {
809 char const str[] = "vkCmdClearColorImage aspectMasks for all subresource ranges must be set to VK_IMAGE_ASPECT_COLOR_BIT";
810 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
811 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
812 }
813
814 if (vk_format_is_depth_or_stencil(image_state->createInfo.format)) {
815 char const str[] = "vkCmdClearColorImage called with depth/stencil image.";
816 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
817 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
818 validation_error_map[VALIDATION_ERROR_01088]);
819 } else if (vk_format_is_compressed(image_state->createInfo.format)) {
820 char const str[] = "vkCmdClearColorImage called with compressed 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]);
824 }
825
826 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
827 char const str[] = "vkCmdClearColorImage called with image created without VK_IMAGE_USAGE_TRANSFER_DST_BIT.";
828 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
829 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01084, "IMAGE", "%s. %s", str,
830 validation_error_map[VALIDATION_ERROR_01084]);
831 }
832 return skip;
833}
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700834
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600835uint32_t ResolveRemainingLevels(const VkImageSubresourceRange *range, uint32_t mip_levels) {
836 // Return correct number of mip levels taking into account VK_REMAINING_MIP_LEVELS
837 uint32_t mip_level_count = range->levelCount;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700838 if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600839 mip_level_count = mip_levels - range->baseMipLevel;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700840 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600841 return mip_level_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700842}
843
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600844uint32_t ResolveRemainingLayers(const VkImageSubresourceRange *range, uint32_t layers) {
845 // Return correct number of layers taking into account VK_REMAINING_ARRAY_LAYERS
846 uint32_t array_layer_count = range->layerCount;
847 if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
848 array_layer_count = layers - range->baseArrayLayer;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700849 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600850 return array_layer_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700851}
852
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700853bool VerifyClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700854 VkImageSubresourceRange range, VkImageLayout dest_image_layout, const char *func_name) {
855 bool skip = false;
856 const debug_report_data *report_data = core_validation::GetReportData(device_data);
857
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600858 uint32_t level_count = ResolveRemainingLevels(&range, image_state->createInfo.mipLevels);
859 uint32_t layer_count = ResolveRemainingLayers(&range, image_state->createInfo.arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700860
861 if (dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
862 if (dest_image_layout == VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700863 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
864 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600865 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
866 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700867 "%s: Layout for cleared image should be TRANSFER_DST_OPTIMAL instead of GENERAL.", func_name);
868 }
869 } else {
870 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01086;
871 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
872 error_code = VALIDATION_ERROR_01101;
873 } else {
874 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
875 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600876 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
877 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, error_code, "DS",
878 "%s: Layout for cleared image is %s but can only be "
879 "TRANSFER_DST_OPTIMAL or GENERAL. %s",
880 func_name, string_VkImageLayout(dest_image_layout), validation_error_map[error_code]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700881 }
882 }
883
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600884 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
885 uint32_t level = level_index + range.baseMipLevel;
886 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
887 uint32_t layer = layer_index + range.baseArrayLayer;
888 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700889 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700890 if (FindCmdBufLayout(device_data, cb_node, image_state->image, sub, node)) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700891 if (node.layout != dest_image_layout) {
892 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01085;
893 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
894 error_code = VALIDATION_ERROR_01100;
895 } else {
896 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
897 }
898 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
899 __LINE__, error_code, "DS",
900 "%s: Cannot clear an image whose layout is %s and "
901 "doesn't match the current layout %s. %s",
902 func_name, string_VkImageLayout(dest_image_layout), string_VkImageLayout(node.layout),
903 validation_error_map[error_code]);
904 }
905 }
906 }
907 }
908
909 return skip;
910}
911
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700912void RecordClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage image, VkImageSubresourceRange range,
913 VkImageLayout dest_image_layout) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600914 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, image)->createInfo);
915 uint32_t level_count = ResolveRemainingLevels(&range, image_create_info->mipLevels);
916 uint32_t layer_count = ResolveRemainingLayers(&range, image_create_info->arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700917
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600918 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
919 uint32_t level = level_index + range.baseMipLevel;
920 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
921 uint32_t layer = layer_index + range.baseArrayLayer;
922 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700923 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700924 if (!FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
925 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 -0700926 }
927 }
928 }
929}
930
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700931bool PreCallValidateCmdClearColorImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700932 VkImageLayout imageLayout, uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
933 bool skip = false;
934 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700935 auto cb_node = GetCBNode(dev_data, commandBuffer);
936 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700937 if (cb_node && image_state) {
938 skip |= ValidateMemoryIsBoundToImage(dev_data, image_state, "vkCmdClearColorImage()", VALIDATION_ERROR_02527);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700939 skip |= ValidateCmdQueueFlags(dev_data, cb_node, "vkCmdClearColorImage()", VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT,
940 VALIDATION_ERROR_01095);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700941 skip |= ValidateCmd(dev_data, cb_node, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
942 skip |= insideRenderPass(dev_data, cb_node, "vkCmdClearColorImage()", VALIDATION_ERROR_01096);
943 for (uint32_t i = 0; i < rangeCount; ++i) {
944 skip |= ValidateImageAttributes(dev_data, image_state, pRanges[i]);
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700945 skip |= VerifyClearImageLayout(dev_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearColorImage()");
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700946 }
947 }
948 return skip;
949}
950
951// This state recording routine is shared between ClearColorImage and ClearDepthStencilImage
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700952void PreCallRecordCmdClearImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
953 uint32_t rangeCount, const VkImageSubresourceRange *pRanges, CMD_TYPE cmd_type) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700954 auto cb_node = GetCBNode(dev_data, commandBuffer);
955 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700956 if (cb_node && image_state) {
957 AddCommandBufferBindingImage(dev_data, cb_node, image_state);
958 std::function<bool()> function = [=]() {
959 SetImageMemoryValid(dev_data, image_state, true);
960 return false;
961 };
962 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700963 core_validation::UpdateCmdBufferLastCmd(cb_node, cmd_type);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700964 for (uint32_t i = 0; i < rangeCount; ++i) {
965 RecordClearImageLayout(dev_data, cb_node, image, pRanges[i], imageLayout);
966 }
967 }
968}
969
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700970bool PreCallValidateCmdClearDepthStencilImage(layer_data *device_data, VkCommandBuffer commandBuffer, VkImage image,
971 VkImageLayout imageLayout, uint32_t rangeCount,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700972 const VkImageSubresourceRange *pRanges) {
973 bool skip = false;
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700974 const debug_report_data *report_data = core_validation::GetReportData(device_data);
975
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700976 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700977 auto cb_node = GetCBNode(device_data, commandBuffer);
978 auto image_state = GetImageState(device_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700979 if (cb_node && image_state) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700980 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_02528);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700981 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearDepthStencilImage()", VK_QUEUE_GRAPHICS_BIT,
982 VALIDATION_ERROR_01110);
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700983 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
984 skip |= insideRenderPass(device_data, cb_node, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_01111);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700985 for (uint32_t i = 0; i < rangeCount; ++i) {
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700986 skip |=
987 VerifyClearImageLayout(device_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearDepthStencilImage()");
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700988 // Image aspect must be depth or stencil or both
989 if (((pRanges[i].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
990 ((pRanges[i].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
991 char const str[] =
992 "vkCmdClearDepthStencilImage aspectMasks for all subresource ranges must be "
993 "set to VK_IMAGE_ASPECT_DEPTH_BIT and/or VK_IMAGE_ASPECT_STENCIL_BIT";
994 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
995 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
996 }
997 }
998 if (image_state && !vk_format_is_depth_or_stencil(image_state->createInfo.format)) {
999 char const str[] = "vkCmdClearDepthStencilImage called without a depth/stencil image.";
1000 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
1001 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_01103, "IMAGE", "%s. %s", str,
1002 validation_error_map[VALIDATION_ERROR_01103]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001003 }
1004 }
1005 return skip;
1006}
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001007
1008// Returns true if [x, xoffset] and [y, yoffset] overlap
1009static bool RangesIntersect(int32_t start, uint32_t start_offset, int32_t end, uint32_t end_offset) {
1010 bool result = false;
1011 uint32_t intersection_min = std::max(static_cast<uint32_t>(start), static_cast<uint32_t>(end));
1012 uint32_t intersection_max = std::min(static_cast<uint32_t>(start) + start_offset, static_cast<uint32_t>(end) + end_offset);
1013
1014 if (intersection_max > intersection_min) {
1015 result = true;
1016 }
1017 return result;
1018}
1019
1020// Returns true if two VkImageCopy structures overlap
1021static bool RegionIntersects(const VkImageCopy *src, const VkImageCopy *dst, VkImageType type) {
1022 bool result = false;
1023 if ((src->srcSubresource.mipLevel == dst->dstSubresource.mipLevel) &&
1024 (RangesIntersect(src->srcSubresource.baseArrayLayer, src->srcSubresource.layerCount, dst->dstSubresource.baseArrayLayer,
1025 dst->dstSubresource.layerCount))) {
1026 result = true;
1027 switch (type) {
1028 case VK_IMAGE_TYPE_3D:
1029 result &= RangesIntersect(src->srcOffset.z, src->extent.depth, dst->dstOffset.z, dst->extent.depth);
1030 // Intentionally fall through to 2D case
1031 case VK_IMAGE_TYPE_2D:
1032 result &= RangesIntersect(src->srcOffset.y, src->extent.height, dst->dstOffset.y, dst->extent.height);
1033 // Intentionally fall through to 1D case
1034 case VK_IMAGE_TYPE_1D:
1035 result &= RangesIntersect(src->srcOffset.x, src->extent.width, dst->dstOffset.x, dst->extent.width);
1036 break;
1037 default:
1038 // Unrecognized or new IMAGE_TYPE enums will be caught in parameter_validation
1039 assert(false);
1040 }
1041 }
1042 return result;
1043}
1044
1045// Returns true if offset and extent exceed image extents
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001046static bool ExceedsBounds(const VkOffset3D *offset, const VkExtent3D *extent, const VkExtent3D *image_extent) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001047 bool result = false;
1048 // Extents/depths cannot be negative but checks left in for clarity
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001049 if ((offset->z + extent->depth > image_extent->depth) || (offset->z < 0) ||
1050 ((offset->z + static_cast<int32_t>(extent->depth)) < 0)) {
1051 result = true;
1052 }
1053 if ((offset->y + extent->height > image_extent->height) || (offset->y < 0) ||
1054 ((offset->y + static_cast<int32_t>(extent->height)) < 0)) {
1055 result = true;
1056 }
1057 if ((offset->x + extent->width > image_extent->width) || (offset->x < 0) ||
1058 ((offset->x + static_cast<int32_t>(extent->width)) < 0)) {
1059 result = true;
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001060 }
1061 return result;
1062}
1063
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001064// Test if two VkExtent3D structs are equivalent
1065static inline bool IsExtentEqual(const VkExtent3D *extent, const VkExtent3D *other_extent) {
1066 bool result = true;
1067 if ((extent->width != other_extent->width) || (extent->height != other_extent->height) ||
1068 (extent->depth != other_extent->depth)) {
1069 result = false;
1070 }
1071 return result;
1072}
1073
1074// Returns the image extent of a specific subresource.
1075static inline VkExtent3D GetImageSubresourceExtent(const IMAGE_STATE *img, const VkImageSubresourceLayers *subresource) {
1076 const uint32_t mip = subresource->mipLevel;
1077 VkExtent3D extent = img->createInfo.extent;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001078 // Don't allow mip adjustment to create 0 dim, but pass along a 0 if that's what subresource specified
1079 extent.width = (0 == extent.width ? 0 : std::max(1U, extent.width >> mip));
1080 extent.height = (0 == extent.height ? 0 : std::max(1U, extent.height >> mip));
1081 extent.depth = (0 == extent.depth ? 0 : std::max(1U, extent.depth >> mip));
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001082 return extent;
1083}
1084
1085// Test if the extent argument has all dimensions set to 0.
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001086static inline bool IsExtentAllZeroes(const VkExtent3D *extent) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001087 return ((extent->width == 0) && (extent->height == 0) && (extent->depth == 0));
1088}
1089
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001090// Test if the extent argument has any dimensions set to 0.
1091static inline bool IsExtentSizeZero(const VkExtent3D *extent) {
1092 return ((extent->width == 0) || (extent->height == 0) || (extent->depth == 0));
1093}
1094
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001095// Returns the image transfer granularity for a specific image scaled by compressed block size if necessary.
1096static inline VkExtent3D GetScaledItg(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *img) {
1097 // Default to (0, 0, 0) granularity in case we can't find the real granularity for the physical device.
1098 VkExtent3D granularity = {0, 0, 0};
1099 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
1100 if (pPool) {
1101 granularity =
1102 GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].minImageTransferGranularity;
1103 if (vk_format_is_compressed(img->createInfo.format)) {
Mark Lobodzinski13086442017-02-24 08:53:14 -07001104 auto block_size = vk_format_compressed_texel_block_extents(img->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001105 granularity.width *= block_size.width;
1106 granularity.height *= block_size.height;
1107 }
1108 }
1109 return granularity;
1110}
1111
1112// Test elements of a VkExtent3D structure against alignment constraints contained in another VkExtent3D structure
1113static inline bool IsExtentAligned(const VkExtent3D *extent, const VkExtent3D *granularity) {
1114 bool valid = true;
1115 if ((vk_safe_modulo(extent->depth, granularity->depth) != 0) || (vk_safe_modulo(extent->width, granularity->width) != 0) ||
1116 (vk_safe_modulo(extent->height, granularity->height) != 0)) {
1117 valid = false;
1118 }
1119 return valid;
1120}
1121
1122// Check elements of a VkOffset3D structure against a queue family's Image Transfer Granularity values
1123static inline bool CheckItgOffset(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkOffset3D *offset,
1124 const VkExtent3D *granularity, const uint32_t i, const char *function, const char *member) {
1125 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1126 bool skip = false;
1127 VkExtent3D offset_extent = {};
1128 offset_extent.width = static_cast<uint32_t>(abs(offset->x));
1129 offset_extent.height = static_cast<uint32_t>(abs(offset->y));
1130 offset_extent.depth = static_cast<uint32_t>(abs(offset->z));
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001131 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001132 // 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 -07001133 if (IsExtentAllZeroes(&offset_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001134 skip |=
1135 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1136 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1137 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) must be (x=0, y=0, z=0) "
1138 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1139 function, i, member, offset->x, offset->y, offset->z);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001140 }
1141 } else {
1142 // If the queue family image transfer granularity is not (0, 0, 0), then the offset dimensions must always be even
1143 // integer multiples of the image transfer granularity.
1144 if (IsExtentAligned(&offset_extent, granularity) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001145 skip |= log_msg(
1146 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1147 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1148 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) dimensions must be even integer "
1149 "multiples of this command buffer's queue family image transfer granularity (w=%d, h=%d, d=%d).",
1150 function, i, member, offset->x, offset->y, offset->z, granularity->width, granularity->height, granularity->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001151 }
1152 }
1153 return skip;
1154}
1155
1156// Check elements of a VkExtent3D structure against a queue family's Image Transfer Granularity values
1157static inline bool CheckItgExtent(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkExtent3D *extent,
1158 const VkOffset3D *offset, const VkExtent3D *granularity, const VkExtent3D *subresource_extent,
1159 const uint32_t i, const char *function, const char *member) {
1160 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1161 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001162 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001163 // If the queue family image transfer granularity is (0, 0, 0), then the extent must always match the image
1164 // subresource extent.
1165 if (IsExtentEqual(extent, subresource_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001166 skip |=
1167 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1168 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1169 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d) "
1170 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1171 function, i, member, extent->width, extent->height, extent->depth, subresource_extent->width,
1172 subresource_extent->height, subresource_extent->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001173 }
1174 } else {
1175 // If the queue family image transfer granularity is not (0, 0, 0), then the extent dimensions must always be even
1176 // integer multiples of the image transfer granularity or the offset + extent dimensions must always match the image
1177 // subresource extent dimensions.
1178 VkExtent3D offset_extent_sum = {};
1179 offset_extent_sum.width = static_cast<uint32_t>(abs(offset->x)) + extent->width;
1180 offset_extent_sum.height = static_cast<uint32_t>(abs(offset->y)) + extent->height;
1181 offset_extent_sum.depth = static_cast<uint32_t>(abs(offset->z)) + extent->depth;
1182 if ((IsExtentAligned(extent, granularity) == false) && (IsExtentEqual(&offset_extent_sum, subresource_extent) == false)) {
1183 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001184 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1185 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001186 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) dimensions must be even integer multiples of this command buffer's "
1187 "queue family image transfer granularity (w=%d, h=%d, d=%d) or offset (x=%d, y=%d, z=%d) + "
1188 "extent (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d).",
1189 function, i, member, extent->width, extent->height, extent->depth, granularity->width, granularity->height,
1190 granularity->depth, offset->x, offset->y, offset->z, extent->width, extent->height, extent->depth,
1191 subresource_extent->width, subresource_extent->height, subresource_extent->depth);
1192 }
1193 }
1194 return skip;
1195}
1196
1197// Check a uint32_t width or stride value against a queue family's Image Transfer Granularity width value
1198static inline bool CheckItgInt(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const uint32_t value,
1199 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1200 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1201
1202 bool skip = false;
1203 if (vk_safe_modulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001204 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1205 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001206 "%s: pRegion[%d].%s (%d) must be an even integer multiple of this command buffer's queue family image "
1207 "transfer granularity width (%d).",
1208 function, i, member, value, granularity);
1209 }
1210 return skip;
1211}
1212
1213// Check a VkDeviceSize value against a queue family's Image Transfer Granularity width value
1214static inline bool CheckItgSize(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkDeviceSize value,
1215 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1216 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1217 bool skip = false;
1218 if (vk_safe_modulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001219 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1220 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001221 "%s: pRegion[%d].%s (%" PRIdLEAST64
1222 ") must be an even integer multiple of this command buffer's queue family image transfer "
1223 "granularity width (%d).",
1224 function, i, member, value, granularity);
1225 }
1226 return skip;
1227}
1228
1229// Check valid usage Image Tranfer Granularity requirements for elements of a VkBufferImageCopy structure
1230bool ValidateCopyBufferImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1231 const IMAGE_STATE *img, const VkBufferImageCopy *region,
1232 const uint32_t i, const char *function) {
1233 bool skip = false;
1234 if (vk_format_is_compressed(img->createInfo.format) == true) {
1235 // TODO: Add granularity checking for compressed formats
1236
1237 // bufferRowLength must be a multiple of the compressed texel block width
1238 // bufferImageHeight must be a multiple of the compressed texel block height
1239 // all members of imageOffset must be a multiple of the corresponding dimensions of the compressed texel block
1240 // bufferOffset must be a multiple of the compressed texel block size in bytes
1241 // imageExtent.width must be a multiple of the compressed texel block width or (imageExtent.width + imageOffset.x)
1242 // must equal the image subresource width
1243 // imageExtent.height must be a multiple of the compressed texel block height or (imageExtent.height + imageOffset.y)
1244 // must equal the image subresource height
1245 // imageExtent.depth must be a multiple of the compressed texel block depth or (imageExtent.depth + imageOffset.z)
1246 // must equal the image subresource depth
1247 } else {
1248 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1249 skip |= CheckItgSize(device_data, cb_node, region->bufferOffset, granularity.width, i, function, "bufferOffset");
1250 skip |= CheckItgInt(device_data, cb_node, region->bufferRowLength, granularity.width, i, function, "bufferRowLength");
1251 skip |= CheckItgInt(device_data, cb_node, region->bufferImageHeight, granularity.width, i, function, "bufferImageHeight");
1252 skip |= CheckItgOffset(device_data, cb_node, &region->imageOffset, &granularity, i, function, "imageOffset");
1253 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->imageSubresource);
1254 skip |= CheckItgExtent(device_data, cb_node, &region->imageExtent, &region->imageOffset, &granularity, &subresource_extent,
1255 i, function, "imageExtent");
1256 }
1257 return skip;
1258}
1259
1260// Check valid usage Image Tranfer Granularity requirements for elements of a VkImageCopy structure
1261bool ValidateCopyImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1262 const IMAGE_STATE *img, const VkImageCopy *region, const uint32_t i,
1263 const char *function) {
1264 bool skip = false;
1265 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1266 skip |= CheckItgOffset(device_data, cb_node, &region->srcOffset, &granularity, i, function, "srcOffset");
1267 skip |= CheckItgOffset(device_data, cb_node, &region->dstOffset, &granularity, i, function, "dstOffset");
1268 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->dstSubresource);
1269 skip |= CheckItgExtent(device_data, cb_node, &region->extent, &region->dstOffset, &granularity, &subresource_extent, i,
1270 function, "extent");
1271 return skip;
1272}
1273
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001274bool PreCallValidateCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001275 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1276 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001277 bool skip = false;
1278 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1279 VkCommandBuffer command_buffer = cb_node->commandBuffer;
1280
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001281 for (uint32_t i = 0; i < region_count; i++) {
1282 if (regions[i].srcSubresource.layerCount == 0) {
1283 std::stringstream ss;
1284 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] srcSubresource is zero";
1285 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1286 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1287 ss.str().c_str());
1288 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001289
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001290 if (regions[i].dstSubresource.layerCount == 0) {
1291 std::stringstream ss;
1292 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] dstSubresource is zero";
1293 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1294 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1295 ss.str().c_str());
1296 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001297
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001298 // For each region the layerCount member of srcSubresource and dstSubresource must match
1299 if (regions[i].srcSubresource.layerCount != regions[i].dstSubresource.layerCount) {
1300 std::stringstream ss;
1301 ss << "vkCmdCopyImage: number of layers in source and destination subresources for pRegions[" << i << "] do not match";
1302 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1303 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01198, "IMAGE", "%s. %s",
1304 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01198]);
1305 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001306
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001307 // For each region, the aspectMask member of srcSubresource and dstSubresource must match
1308 if (regions[i].srcSubresource.aspectMask != regions[i].dstSubresource.aspectMask) {
1309 char const str[] = "vkCmdCopyImage: Src and dest aspectMasks for each region must match";
1310 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1311 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01197, "IMAGE", "%s. %s", str,
1312 validation_error_map[VALIDATION_ERROR_01197]);
1313 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001314
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06001315 // For each region, the aspectMask member of srcSubresource must be present in the source image
1316 if (!VerifyAspectsPresent(regions[i].srcSubresource.aspectMask, src_image_state->createInfo.format)) {
1317 std::stringstream ss;
1318 ss << "vkCmdCopyImage: pRegion[" << i
1319 << "] srcSubresource.aspectMask cannot specify aspects not present in source image";
1320 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1321 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01200, "IMAGE", "%s. %s",
1322 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01200]);
1323 }
1324
1325 // For each region, the aspectMask member of dstSubresource must be present in the destination image
1326 if (!VerifyAspectsPresent(regions[i].dstSubresource.aspectMask, dst_image_state->createInfo.format)) {
1327 std::stringstream ss;
1328 ss << "vkCmdCopyImage: pRegion[" << i << "] dstSubresource.aspectMask cannot specify aspects not present in dest image";
1329 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1330 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01201, "IMAGE", "%s. %s",
1331 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01201]);
1332 }
1333
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001334 // AspectMask must not contain VK_IMAGE_ASPECT_METADATA_BIT
1335 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) ||
1336 (regions[i].dstSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT)) {
1337 std::stringstream ss;
1338 ss << "vkCmdCopyImage: pRegions[" << i << "] may not specify aspectMask containing VK_IMAGE_ASPECT_METADATA_BIT";
1339 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1340 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01222, "IMAGE", "%s. %s",
1341 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01222]);
1342 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001343
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001344 // For each region, if aspectMask contains VK_IMAGE_ASPECT_COLOR_BIT, it must not contain either of
1345 // VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT
1346 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) &&
1347 (regions[i].srcSubresource.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
1348 char const str[] = "vkCmdCopyImage aspectMask cannot specify both COLOR and DEPTH/STENCIL aspects";
1349 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1350 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01221, "IMAGE", "%s. %s", str,
1351 validation_error_map[VALIDATION_ERROR_01221]);
1352 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001353
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001354 // If either of the calling command's src_image or dst_image parameters are of VkImageType VK_IMAGE_TYPE_3D,
1355 // the baseArrayLayer and layerCount members of both srcSubresource and dstSubresource must be 0 and 1, respectively
1356 if (((src_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) ||
1357 (dst_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D)) &&
1358 ((regions[i].srcSubresource.baseArrayLayer != 0) || (regions[i].srcSubresource.layerCount != 1) ||
1359 (regions[i].dstSubresource.baseArrayLayer != 0) || (regions[i].dstSubresource.layerCount != 1))) {
1360 std::stringstream ss;
1361 ss << "vkCmdCopyImage: src or dstImage type was IMAGE_TYPE_3D, but in subRegion[" << i
1362 << "] baseArrayLayer was not zero or layerCount was not 1.";
1363 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1364 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01199, "IMAGE", "%s. %s",
1365 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01199]);
1366 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001367
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001368 // MipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
1369 if (regions[i].srcSubresource.mipLevel >= src_image_state->createInfo.mipLevels) {
1370 std::stringstream ss;
1371 ss << "vkCmdCopyImage: pRegions[" << i
1372 << "] specifies a src mipLevel greater than the number specified when the srcImage was created.";
1373 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1374 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1375 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1376 }
1377 if (regions[i].dstSubresource.mipLevel >= dst_image_state->createInfo.mipLevels) {
1378 std::stringstream ss;
1379 ss << "vkCmdCopyImage: pRegions[" << i
1380 << "] specifies a dst mipLevel greater than the number specified when the dstImage was created.";
1381 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1382 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1383 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1384 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001385
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001386 // (baseArrayLayer + layerCount) must be less than or equal to the arrayLayers specified in VkImageCreateInfo when the
1387 // image was created
1388 if ((regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount) >
1389 src_image_state->createInfo.arrayLayers) {
1390 std::stringstream ss;
1391 ss << "vkCmdCopyImage: srcImage arrayLayers was " << src_image_state->createInfo.arrayLayers << " but subRegion[" << i
1392 << "] baseArrayLayer + layerCount is "
1393 << (regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount);
1394 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1395 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1396 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1397 }
1398 if ((regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount) >
1399 dst_image_state->createInfo.arrayLayers) {
1400 std::stringstream ss;
1401 ss << "vkCmdCopyImage: dstImage arrayLayers was " << dst_image_state->createInfo.arrayLayers << " but subRegion[" << i
1402 << "] baseArrayLayer + layerCount is "
1403 << (regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.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 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001408
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001409 // The source region specified by a given element of regions must be a region that is contained within srcImage
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001410 if (ExceedsBounds(&regions[i].srcOffset, &regions[i].extent, &(src_image_state->createInfo.extent))) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001411 std::stringstream ss;
1412 ss << "vkCmdCopyImage: srcSubResource in pRegions[" << i << "] exceeds extents srcImage was created with";
1413 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1414 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01175, "IMAGE", "%s. %s",
1415 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01175]);
1416 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001417
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001418 // The destination region specified by a given element of regions must be a region that is contained within dst_image
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001419 if (ExceedsBounds(&regions[i].dstOffset, &regions[i].extent, &(dst_image_state->createInfo.extent))) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001420 std::stringstream ss;
1421 ss << "vkCmdCopyImage: dstSubResource in pRegions[" << i << "] exceeds extents dstImage was created with";
1422 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1423 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01176, "IMAGE", "%s. %s",
1424 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01176]);
1425 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001426
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001427 // The union of all source regions, and the union of all destination regions, specified by the elements of regions,
1428 // must not overlap in memory
1429 if (src_image_state->image == dst_image_state->image) {
1430 for (uint32_t j = 0; j < region_count; j++) {
1431 if (RegionIntersects(&regions[i], &regions[j], src_image_state->createInfo.imageType)) {
1432 std::stringstream ss;
1433 ss << "vkCmdCopyImage: pRegions[" << i << "] src overlaps with pRegions[" << j << "].";
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_01177, "IMAGE",
1436 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01177]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001437 }
1438 }
1439 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001440 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001441
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001442 // The formats of src_image and dst_image must be compatible. Formats are considered compatible if their texel size in bytes
1443 // is the same between both formats. For example, VK_FORMAT_R8G8B8A8_UNORM is compatible with VK_FORMAT_R32_UINT because
1444 // because both texels are 4 bytes in size. Depth/stencil formats must match exactly.
1445 if (vk_format_is_depth_or_stencil(src_image_state->createInfo.format) ||
1446 vk_format_is_depth_or_stencil(dst_image_state->createInfo.format)) {
1447 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1448 char const str[] = "vkCmdCopyImage called with unmatched source and dest image depth/stencil formats.";
1449 skip |=
1450 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1451 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT, "IMAGE", str);
1452 }
1453 } else {
1454 size_t srcSize = vk_format_get_size(src_image_state->createInfo.format);
1455 size_t destSize = vk_format_get_size(dst_image_state->createInfo.format);
1456 if (srcSize != destSize) {
1457 char const str[] = "vkCmdCopyImage called with unmatched source and dest image format sizes.";
1458 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1459 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01184, "IMAGE", "%s. %s", str,
1460 validation_error_map[VALIDATION_ERROR_01184]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001461 }
1462 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001463
Dave Houlton33c22b72017-02-28 13:16:02 -07001464 // Source and dest image sample counts must match
1465 if (src_image_state->createInfo.samples != dst_image_state->createInfo.samples) {
1466 char const str[] = "vkCmdCopyImage() called on image pair with non-identical sample counts.";
1467 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 -06001468 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01185, "IMAGE", "%s %s", str,
1469 validation_error_map[VALIDATION_ERROR_01185]);
Dave Houlton33c22b72017-02-28 13:16:02 -07001470 }
1471
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001472 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02533);
1473 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02534);
1474 // Validate that SRC & DST images have correct usage flags set
1475 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01178,
1476 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
1477 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01181,
1478 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001479 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyImage()",
1480 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01193);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001481 skip |= ValidateCmd(device_data, cb_node, CMD_COPYIMAGE, "vkCmdCopyImage()");
1482 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImage()", VALIDATION_ERROR_01194);
1483 for (uint32_t i = 0; i < region_count; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001484 skip |= VerifyImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout,
1485 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01180);
1486 skip |= VerifyImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout,
1487 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01183);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001488 skip |= ValidateCopyImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &regions[i], i,
1489 "vkCmdCopyImage()");
1490 }
1491
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001492 return skip;
1493}
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001494
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001495void PreCallRecordCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06001496 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1497 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
1498 // Make sure that all image slices are updated to correct layout
1499 for (uint32_t i = 0; i < region_count; ++i) {
1500 SetImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout);
1501 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout);
1502 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001503 // Update bindings between images and cmd buffer
1504 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1505 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Dave Houltoneba86e22017-03-02 14:56:23 -07001506 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImage()"); };
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001507 cb_node->validate_functions.push_back(function);
1508 function = [=]() {
1509 SetImageMemoryValid(device_data, dst_image_state, true);
1510 return false;
1511 };
1512 cb_node->validate_functions.push_back(function);
1513 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGE);
1514}
1515
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001516// TODO : Should be tracking lastBound per commandBuffer and when draws occur, report based on that cmd buffer lastBound
1517// Then need to synchronize the accesses based on cmd buffer so that if I'm reading state on one cmd buffer, updates
1518// to that same cmd buffer by separate thread are not changing state from underneath us
1519// Track the last cmd buffer touched by this thread
1520static bool hasDrawCmd(GLOBAL_CB_NODE *pCB) {
1521 for (uint32_t i = 0; i < NUM_DRAW_TYPES; i++) {
1522 if (pCB->drawCount[i]) return true;
1523 }
1524 return false;
1525}
1526
1527// Returns true if sub_rect is entirely contained within rect
1528static inline bool ContainsRect(VkRect2D rect, VkRect2D sub_rect) {
1529 if ((sub_rect.offset.x < rect.offset.x) || (sub_rect.offset.x + sub_rect.extent.width > rect.offset.x + rect.extent.width) ||
1530 (sub_rect.offset.y < rect.offset.y) || (sub_rect.offset.y + sub_rect.extent.height > rect.offset.y + rect.extent.height))
1531 return false;
1532 return true;
1533}
1534
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001535bool PreCallValidateCmdClearAttachments(layer_data *device_data, VkCommandBuffer commandBuffer, uint32_t attachmentCount,
1536 const VkClearAttachment *pAttachments, uint32_t rectCount, const VkClearRect *pRects) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001537 GLOBAL_CB_NODE *cb_node = GetCBNode(device_data, commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001538 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1539
1540 bool skip = false;
1541 if (cb_node) {
Mike Schuchardt9c582402017-02-23 15:57:37 -07001542 skip |=
1543 ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearAttachments()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01121);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001544 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001545 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_CLEARATTACHMENTS);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001546 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
1547 if (!hasDrawCmd(cb_node) && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
1548 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1549 // 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 -07001550 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1551 // CmdClearAttachments.
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001552 skip |=
1553 log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1554 reinterpret_cast<uint64_t &>(commandBuffer), 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
1555 "vkCmdClearAttachments() issued on command buffer object 0x%p prior to any Draw Cmds."
1556 " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1557 commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001558 }
1559 skip |= outsideRenderPass(device_data, cb_node, "vkCmdClearAttachments()", VALIDATION_ERROR_01122);
1560 }
1561
1562 // Validate that attachment is in reference list of active subpass
1563 if (cb_node->activeRenderPass) {
1564 const VkRenderPassCreateInfo *renderpass_create_info = cb_node->activeRenderPass->createInfo.ptr();
1565 const VkSubpassDescription *subpass_desc = &renderpass_create_info->pSubpasses[cb_node->activeSubpass];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001566 auto framebuffer = GetFramebufferState(device_data, cb_node->activeFramebuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001567
1568 for (uint32_t i = 0; i < attachmentCount; i++) {
1569 auto clear_desc = &pAttachments[i];
1570 VkImageView image_view = VK_NULL_HANDLE;
1571
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001572 if (0 == clear_desc->aspectMask) {
1573 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 -06001574 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01128, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001575 validation_error_map[VALIDATION_ERROR_01128]);
1576 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) {
1577 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 -06001578 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01126, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001579 validation_error_map[VALIDATION_ERROR_01126]);
1580 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001581 if (clear_desc->colorAttachment >= subpass_desc->colorAttachmentCount) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001582 skip |=
1583 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 -06001584 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01114, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001585 "vkCmdClearAttachments() color attachment index %d out of range for active subpass %d. %s",
1586 clear_desc->colorAttachment, cb_node->activeSubpass, validation_error_map[VALIDATION_ERROR_01114]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001587 } else if (subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment == VK_ATTACHMENT_UNUSED) {
1588 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001589 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(commandBuffer),
1590 __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001591 "vkCmdClearAttachments() color attachment index %d is VK_ATTACHMENT_UNUSED; ignored.",
1592 clear_desc->colorAttachment);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001593 } else {
1594 image_view = framebuffer->createInfo
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001595 .pAttachments[subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment];
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001596 }
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001597 if ((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
1598 (clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1599 char const str[] =
1600 "vkCmdClearAttachments aspectMask [%d] must set only VK_IMAGE_ASPECT_COLOR_BIT of a color attachment. %s";
1601 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 -06001602 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01125, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001603 validation_error_map[VALIDATION_ERROR_01125]);
1604 }
1605 } else { // Must be depth and/or stencil
1606 if (((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
1607 ((clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1608 char const str[] = "vkCmdClearAttachments aspectMask [%d] is not a valid combination of bits. %s";
1609 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 -06001610 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01127, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001611 validation_error_map[VALIDATION_ERROR_01127]);
1612 }
1613 if (!subpass_desc->pDepthStencilAttachment ||
1614 (subpass_desc->pDepthStencilAttachment->attachment == VK_ATTACHMENT_UNUSED)) {
1615 skip |= log_msg(
1616 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001617 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001618 "vkCmdClearAttachments() depth/stencil clear with no depth/stencil attachment in subpass; ignored");
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001619 } else {
1620 image_view = framebuffer->createInfo.pAttachments[subpass_desc->pDepthStencilAttachment->attachment];
1621 }
1622 }
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001623 if (image_view) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001624 auto image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001625 for (uint32_t j = 0; j < rectCount; j++) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001626 // The rectangular region specified by a given element of pRects must be contained within the render area of
1627 // the current render pass instance
Mark Lobodzinskid833bb72017-02-22 10:55:30 -07001628 // TODO: This check should be moved to CmdExecuteCommands or QueueSubmit to cover secondary CB cases
1629 if ((cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
1630 (false == ContainsRect(cb_node->activeRenderPassBeginInfo.renderArea, pRects[j].rect))) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001631 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1632 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01115, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001633 "vkCmdClearAttachments(): The area defined by pRects[%d] is not contained in the area of "
1634 "the current render pass instance. %s",
1635 j, validation_error_map[VALIDATION_ERROR_01115]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001636 }
1637 // The layers specified by a given element of pRects must be contained within every attachment that
1638 // pAttachments refers to
1639 auto attachment_base_array_layer = image_view_state->create_info.subresourceRange.baseArrayLayer;
1640 auto attachment_layer_count = image_view_state->create_info.subresourceRange.layerCount;
1641 if ((pRects[j].baseArrayLayer < attachment_base_array_layer) || pRects[j].layerCount > attachment_layer_count) {
1642 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001643 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1644 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01116, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001645 "vkCmdClearAttachments(): The layers defined in pRects[%d] are not contained in the layers of "
1646 "pAttachment[%d]. %s",
1647 j, i, validation_error_map[VALIDATION_ERROR_01116]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001648 }
1649 }
1650 }
1651 }
1652 }
1653 return skip;
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001654}
1655
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001656bool PreCallValidateCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001657 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageResolve *pRegions) {
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001658 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001659 bool skip = false;
1660 if (cb_node && src_image_state && dst_image_state) {
1661 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02541);
1662 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02542);
Mike Schuchardt9c582402017-02-23 15:57:37 -07001663 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdResolveImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01334);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001664 skip |= ValidateCmd(device_data, cb_node, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
1665 skip |= insideRenderPass(device_data, cb_node, "vkCmdResolveImage()", VALIDATION_ERROR_01335);
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001666
1667 // For each region, the number of layers in the image subresource should not be zero
1668 // For each region, src and dest image aspect must be color only
1669 for (uint32_t i = 0; i < regionCount; i++) {
1670 if (pRegions[i].srcSubresource.layerCount == 0) {
1671 char const str[] = "vkCmdResolveImage: number of layers in source subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001672 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 -07001673 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1674 "IMAGE", str);
1675 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001676 if (pRegions[i].dstSubresource.layerCount == 0) {
1677 char const str[] = "vkCmdResolveImage: number of layers in destination subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001678 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 -07001679 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1680 "IMAGE", str);
1681 }
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001682 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1683 skip |= log_msg(
1684 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1685 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01339, "IMAGE",
1686 "vkCmdResolveImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s", i,
1687 validation_error_map[VALIDATION_ERROR_01339]);
1688 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001689 if ((pRegions[i].srcSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) ||
1690 (pRegions[i].dstSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)) {
1691 char const str[] =
1692 "vkCmdResolveImage: src and dest aspectMasks for each region must specify only VK_IMAGE_ASPECT_COLOR_BIT";
1693 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1694 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01338, "IMAGE",
1695 "%s. %s", str, validation_error_map[VALIDATION_ERROR_01338]);
1696 }
1697 }
1698
1699 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1700 char const str[] = "vkCmdResolveImage called with unmatched source and dest formats.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001701 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 -07001702 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT,
1703 "IMAGE", str);
1704 }
1705 if (src_image_state->createInfo.imageType != dst_image_state->createInfo.imageType) {
1706 char const str[] = "vkCmdResolveImage called with unmatched source and dest image types.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001707 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 -07001708 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_TYPE, "IMAGE",
1709 str);
1710 }
1711 if (src_image_state->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
1712 char const str[] = "vkCmdResolveImage called with source sample count less than 2.";
1713 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1714 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01320, "IMAGE", "%s. %s",
1715 str, validation_error_map[VALIDATION_ERROR_01320]);
1716 }
1717 if (dst_image_state->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
1718 char const str[] = "vkCmdResolveImage called with dest sample count greater than 1.";
1719 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1720 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01321, "IMAGE", "%s. %s",
1721 str, validation_error_map[VALIDATION_ERROR_01321]);
1722 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001723 } else {
1724 assert(0);
1725 }
1726 return skip;
1727}
1728
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001729void PreCallRecordCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1730 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001731 // Update bindings between images and cmd buffer
1732 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1733 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1734
1735 std::function<bool()> function = [=]() {
1736 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdResolveImage()");
1737 };
1738 cb_node->validate_functions.push_back(function);
1739 function = [=]() {
1740 SetImageMemoryValid(device_data, dst_image_state, true);
1741 return false;
1742 };
1743 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001744 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_RESOLVEIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001745}
1746
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001747bool PreCallValidateCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001748 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
1749 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1750
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001751 bool skip = false;
1752 if (cb_node && src_image_state && dst_image_state) {
1753 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): srcImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001754 VALIDATION_ERROR_02194);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001755 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): dstImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001756 VALIDATION_ERROR_02195);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001757 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02539);
1758 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02540);
1759 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_02182,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001760 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001761 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_02186,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001762 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001763 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdBlitImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01299);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001764 skip |= ValidateCmd(device_data, cb_node, CMD_BLITIMAGE, "vkCmdBlitImage()");
1765 skip |= insideRenderPass(device_data, cb_node, "vkCmdBlitImage()", VALIDATION_ERROR_01300);
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001766
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001767 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001768 // Warn for zero-sized regions
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001769 if ((pRegions[i].srcOffsets[0].x == pRegions[i].srcOffsets[1].x) ||
1770 (pRegions[i].srcOffsets[0].y == pRegions[i].srcOffsets[1].y) ||
1771 (pRegions[i].srcOffsets[0].z == pRegions[i].srcOffsets[1].z)) {
1772 std::stringstream ss;
1773 ss << "vkCmdBlitImage: pRegions[" << i << "].srcOffsets specify a zero-volume area.";
1774 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1775 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1776 "%s", ss.str().c_str());
1777 }
1778 if ((pRegions[i].dstOffsets[0].x == pRegions[i].dstOffsets[1].x) ||
1779 (pRegions[i].dstOffsets[0].y == pRegions[i].dstOffsets[1].y) ||
1780 (pRegions[i].dstOffsets[0].z == pRegions[i].dstOffsets[1].z)) {
1781 std::stringstream ss;
1782 ss << "vkCmdBlitImage: pRegions[" << i << "].dstOffsets specify a zero-volume area.";
1783 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1784 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1785 "%s", ss.str().c_str());
1786 }
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001787 if (pRegions[i].srcSubresource.layerCount == 0) {
1788 char const str[] = "vkCmdBlitImage: number of layers in source subresource is zero";
1789 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 -07001790 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1791 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001792 }
1793 if (pRegions[i].dstSubresource.layerCount == 0) {
1794 char const str[] = "vkCmdBlitImage: number of layers in destination subresource is zero";
1795 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 -07001796 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1797 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001798 }
1799
1800 // Check that src/dst layercounts match
1801 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1802 skip |=
1803 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1804 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01304, "IMAGE",
1805 "vkCmdBlitImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s",
1806 i, validation_error_map[VALIDATION_ERROR_01304]);
1807 }
Mark Lobodzinskie7e85fd2017-02-07 13:44:57 -07001808
1809 if (pRegions[i].srcSubresource.aspectMask != pRegions[i].dstSubresource.aspectMask) {
1810 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1811 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01303, "IMAGE",
1812 "vkCmdBlitImage: aspectMask members for pRegion[%d] do not match. %s", i,
1813 validation_error_map[VALIDATION_ERROR_01303]);
1814 }
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001815 }
1816
1817 VkFormat src_format = src_image_state->createInfo.format;
1818 VkFormat dst_format = dst_image_state->createInfo.format;
1819
1820 // Validate consistency for unsigned formats
1821 if (vk_format_is_uint(src_format) != vk_format_is_uint(dst_format)) {
1822 std::stringstream ss;
1823 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has unsigned integer format, "
1824 << "the other one must also have unsigned integer format. "
1825 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1826 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1827 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02191, "IMAGE", "%s. %s",
1828 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02191]);
1829 }
1830
1831 // Validate consistency for signed formats
1832 if (vk_format_is_sint(src_format) != vk_format_is_sint(dst_format)) {
1833 std::stringstream ss;
1834 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has signed integer format, "
1835 << "the other one must also have signed integer format. "
1836 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1837 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1838 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02190, "IMAGE", "%s. %s",
1839 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02190]);
1840 }
1841
1842 // Validate aspect bits and formats for depth/stencil images
1843 if (vk_format_is_depth_or_stencil(src_format) || vk_format_is_depth_or_stencil(dst_format)) {
1844 if (src_format != dst_format) {
1845 std::stringstream ss;
1846 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has a format of depth, stencil or depth "
1847 << "stencil, the other one must have exactly the same format. "
1848 << "Source format is " << string_VkFormat(src_format) << " Destination format is "
1849 << 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_02192, "IMAGE",
1852 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02192]);
1853 }
1854
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001855 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001856 VkImageAspectFlags srcAspect = pRegions[i].srcSubresource.aspectMask;
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001857
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001858 if (vk_format_is_depth_and_stencil(src_format)) {
1859 if ((srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1860 std::stringstream ss;
1861 ss << "vkCmdBlitImage: Combination depth/stencil image formats must have only one of "
1862 "VK_IMAGE_ASPECT_DEPTH_BIT "
1863 << "and VK_IMAGE_ASPECT_STENCIL_BIT set in srcImage and dstImage";
1864 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1865 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1866 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1867 }
1868 } else if (vk_format_is_stencil_only(src_format)) {
1869 if (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT) {
1870 std::stringstream ss;
1871 ss << "vkCmdBlitImage: Stencil-only image formats must have only the VK_IMAGE_ASPECT_STENCIL_BIT "
1872 << "set in both the srcImage and dstImage";
1873 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1874 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1875 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1876 }
1877 } else if (vk_format_is_depth_only(src_format)) {
1878 if (srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) {
1879 std::stringstream ss;
1880 ss << "vkCmdBlitImage: Depth-only image formats must have only the VK_IMAGE_ASPECT_DEPTH "
1881 << "set in both the srcImage and dstImage";
1882 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1883 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1884 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1885 }
1886 }
1887 }
1888 }
1889
1890 // Validate filter
1891 if (vk_format_is_depth_or_stencil(src_format) && (filter != VK_FILTER_NEAREST)) {
1892 std::stringstream ss;
1893 ss << "vkCmdBlitImage: If the format of srcImage is a depth, stencil, or depth stencil "
1894 << "then filter must be VK_FILTER_NEAREST.";
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__, VALIDATION_ERROR_02193, "IMAGE", "%s. %s",
1897 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02193]);
1898 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001899 } else {
1900 assert(0);
1901 }
1902 return skip;
1903}
1904
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001905void PreCallRecordCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1906 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001907 // Update bindings between images and cmd buffer
1908 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1909 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1910
1911 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdBlitImage()"); };
1912 cb_node->validate_functions.push_back(function);
1913 function = [=]() {
1914 SetImageMemoryValid(device_data, dst_image_state, true);
1915 return false;
1916 };
1917 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001918 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_BLITIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001919}
1920
Tony Barbourdf013b92017-01-25 12:53:48 -07001921// This validates that the initial layout specified in the command buffer for
1922// the IMAGE is the same
1923// as the global IMAGE layout
Tony Barboure0c5cc92017-02-08 13:53:39 -07001924bool ValidateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
1925 std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap) {
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001926 bool skip = false;
Tony Barbourdf013b92017-01-25 12:53:48 -07001927 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001928 for (auto cb_image_data : pCB->imageLayoutMap) {
1929 VkImageLayout imageLayout;
Tony Barbourdf013b92017-01-25 12:53:48 -07001930
Jeremy Hayes55b6c292017-02-28 09:44:45 -07001931 if (FindLayout(imageLayoutMap, cb_image_data.first, imageLayout)) {
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001932 if (cb_image_data.second.initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
1933 // TODO: Set memory invalid which is in mem_tracker currently
1934 } else if (imageLayout != cb_image_data.second.initialLayout) {
1935 if (cb_image_data.first.hasSubresource) {
Dave Houltoneba86e22017-03-02 14:56:23 -07001936 skip |= log_msg(
1937 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1938 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1939 "Cannot submit cmd buffer using image (0x%" PRIx64
1940 ") [sub-resource: aspectMask 0x%X array layer %u, mip level %u], "
1941 "with layout %s when first use is %s.",
1942 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), cb_image_data.first.subresource.aspectMask,
1943 cb_image_data.first.subresource.arrayLayer, cb_image_data.first.subresource.mipLevel,
1944 string_VkImageLayout(imageLayout), string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001945 } else {
Dave Houltoneba86e22017-03-02 14:56:23 -07001946 skip |=
1947 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1948 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1949 "Cannot submit cmd buffer using image (0x%" PRIx64
1950 ") with layout %s when "
1951 "first use is %s.",
1952 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), string_VkImageLayout(imageLayout),
1953 string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001954 }
1955 }
Tony Barbourdf013b92017-01-25 12:53:48 -07001956 SetLayout(imageLayoutMap, cb_image_data.first, cb_image_data.second.layout);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001957 }
1958 }
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001959 return skip;
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001960}
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001961
Tony Barbourdf013b92017-01-25 12:53:48 -07001962void UpdateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB) {
1963 for (auto cb_image_data : pCB->imageLayoutMap) {
1964 VkImageLayout imageLayout;
1965 FindGlobalLayout(device_data, cb_image_data.first, imageLayout);
1966 SetGlobalLayout(device_data, cb_image_data.first, cb_image_data.second.layout);
1967 }
1968}
1969
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001970// Print readable FlagBits in FlagMask
1971static std::string string_VkAccessFlags(VkAccessFlags accessMask) {
1972 std::string result;
1973 std::string separator;
1974
1975 if (accessMask == 0) {
1976 result = "[None]";
1977 } else {
1978 result = "[";
1979 for (auto i = 0; i < 32; i++) {
1980 if (accessMask & (1 << i)) {
1981 result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
1982 separator = " | ";
1983 }
1984 }
1985 result = result + "]";
1986 }
1987 return result;
1988}
1989
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07001990// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set. If required_bit is zero, accessMask
1991// must have at least one of 'optional_bits' set
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001992// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07001993static bool ValidateMaskBits(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
1994 const VkImageLayout &layout, VkAccessFlags required_bit, VkAccessFlags optional_bits,
1995 const char *type) {
1996 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1997 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001998
1999 if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
2000 if (accessMask & ~(required_bit | optional_bits)) {
2001 // TODO: Verify against Valid Use
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002002 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2003 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002004 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2005 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002006 }
2007 } else {
2008 if (!required_bit) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002009 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2010 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002011 "%s AccessMask %d %s must contain at least one of access bits %d "
2012 "%s when layout is %s, unless the app has previously added a "
2013 "barrier for this transition.",
2014 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
2015 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002016 } else {
2017 std::string opt_bits;
2018 if (optional_bits != 0) {
2019 std::stringstream ss;
2020 ss << optional_bits;
2021 opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
2022 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002023 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2024 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002025 "%s AccessMask %d %s must have required access bit %d %s %s when "
2026 "layout is %s, unless the app has previously added a barrier for "
2027 "this transition.",
2028 type, accessMask, string_VkAccessFlags(accessMask).c_str(), required_bit,
2029 string_VkAccessFlags(required_bit).c_str(), opt_bits.c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002030 }
2031 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002032 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002033}
2034
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002035bool ValidateMaskBitsFromLayouts(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer,
2036 const VkAccessFlags &accessMask, const VkImageLayout &layout, const char *type) {
2037 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002038
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002039 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002040 switch (layout) {
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002041 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
2042 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
2043 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2044 break;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002045 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002046 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
2047 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
2048 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2049 break;
2050 }
2051 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
2052 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
2053 break;
2054 }
2055 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
2056 skip |= ValidateMaskBits(
2057 device_data, cmdBuffer, accessMask, layout, 0,
2058 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
2059 type);
2060 break;
2061 }
2062 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
2063 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, 0,
2064 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
2065 break;
2066 }
2067 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
2068 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
2069 break;
2070 }
2071 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
2072 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_MEMORY_READ_BIT, 0, type);
2073 break;
2074 }
2075 case VK_IMAGE_LAYOUT_UNDEFINED: {
2076 if (accessMask != 0) {
2077 // TODO: Verify against Valid Use section spec
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002078 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2079 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002080 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2081 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
2082 }
2083 break;
2084 }
2085 case VK_IMAGE_LAYOUT_GENERAL:
2086 default: { break; }
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002087 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002088 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002089}
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002090
2091// ValidateLayoutVsAttachmentDescription is a general function where we can validate various state associated with the
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002092// VkAttachmentDescription structs that are used by the sub-passes of a renderpass. Initial check is to make sure that READ_ONLY
2093// layout attachments don't have CLEAR as their loadOp.
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002094bool ValidateLayoutVsAttachmentDescription(const debug_report_data *report_data, const VkImageLayout first_layout,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002095 const uint32_t attachment, const VkAttachmentDescription &attachment_description) {
2096 bool skip = false;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002097 // Verify that initial loadOp on READ_ONLY attachments is not CLEAR
2098 if (attachment_description.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2099 if ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
2100 (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002101 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2102 VkDebugReportObjectTypeEXT(0), __LINE__, VALIDATION_ERROR_02351, "DS",
2103 "Cannot clear attachment %d with invalid first layout %s. %s", attachment,
2104 string_VkImageLayout(first_layout), validation_error_map[VALIDATION_ERROR_02351]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002105 }
2106 }
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002107 return skip;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002108}
2109
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002110bool ValidateLayouts(core_validation::layer_data *device_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo) {
2111 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002112 bool skip = false;
2113
2114 // Track when we're observing the first use of an attachment
2115 std::vector<bool> attach_first_use(pCreateInfo->attachmentCount, true);
2116 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
2117 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
2118 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
2119 auto attach_index = subpass.pColorAttachments[j].attachment;
2120 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2121
2122 switch (subpass.pColorAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002123 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
2124 // This is ideal.
2125 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002126
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002127 case VK_IMAGE_LAYOUT_GENERAL:
2128 // May not be optimal; TODO: reconsider this warning based on other constraints?
2129 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2130 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2131 "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
2132 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002133
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002134 default:
2135 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2136 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2137 "Layout for color attachment is %s but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.",
2138 string_VkImageLayout(subpass.pColorAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002139 }
2140
2141 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002142 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pColorAttachments[j].layout, attach_index,
2143 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002144 }
2145 attach_first_use[attach_index] = false;
2146 }
2147 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
2148 switch (subpass.pDepthStencilAttachment->layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002149 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
2150 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2151 // These are ideal.
2152 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002153
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002154 case VK_IMAGE_LAYOUT_GENERAL:
2155 // May not be optimal; TODO: reconsider this warning based on other constraints? GENERAL can be better than
2156 // doing a bunch of transitions.
2157 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2158 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2159 "GENERAL layout for depth attachment may not give optimal performance.");
2160 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002161
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002162 default:
2163 // No other layouts are acceptable
2164 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2165 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2166 "Layout for depth attachment is %s but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL, "
2167 "DEPTH_STENCIL_READ_ONLY_OPTIMAL or GENERAL.",
2168 string_VkImageLayout(subpass.pDepthStencilAttachment->layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002169 }
2170
2171 auto attach_index = subpass.pDepthStencilAttachment->attachment;
2172 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002173 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pDepthStencilAttachment->layout, attach_index,
2174 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002175 }
2176 attach_first_use[attach_index] = false;
2177 }
2178 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
2179 auto attach_index = subpass.pInputAttachments[j].attachment;
2180 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2181
2182 switch (subpass.pInputAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002183 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2184 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
2185 // These are ideal.
2186 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002187
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002188 case VK_IMAGE_LAYOUT_GENERAL:
2189 // May not be optimal. TODO: reconsider this warning based on other constraints.
2190 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2191 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2192 "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
2193 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002194
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002195 default:
2196 // No other layouts are acceptable
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002197 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2198 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002199 "Layout for input attachment is %s but can only be READ_ONLY_OPTIMAL or GENERAL.",
2200 string_VkImageLayout(subpass.pInputAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002201 }
2202
2203 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002204 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pInputAttachments[j].layout, attach_index,
2205 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002206 }
2207 attach_first_use[attach_index] = false;
2208 }
2209 }
2210 return skip;
2211}
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002212
2213// For any image objects that overlap mapped memory, verify that their layouts are PREINIT or GENERAL
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002214bool ValidateMapImageLayouts(core_validation::layer_data *device_data, VkDevice device, DEVICE_MEM_INFO const *mem_info,
2215 VkDeviceSize offset, VkDeviceSize end_offset) {
2216 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2217 bool skip = false;
2218 // Iterate over all bound image ranges and verify that for any that overlap the map ranges, the layouts are
2219 // VK_IMAGE_LAYOUT_PREINITIALIZED or VK_IMAGE_LAYOUT_GENERAL
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002220 // TODO : This can be optimized if we store ranges based on starting address and early exit when we pass our range
2221 for (auto image_handle : mem_info->bound_images) {
2222 auto img_it = mem_info->bound_ranges.find(image_handle);
2223 if (img_it != mem_info->bound_ranges.end()) {
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002224 if (rangesIntersect(device_data, &img_it->second, offset, end_offset)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002225 std::vector<VkImageLayout> layouts;
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002226 if (FindLayouts(device_data, VkImage(image_handle), layouts)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002227 for (auto layout : layouts) {
2228 if (layout != VK_IMAGE_LAYOUT_PREINITIALIZED && layout != VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002229 skip |= log_msg(
2230 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
2231 reinterpret_cast<const uint64_t &>(mem_info->mem), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2232 "Mapping an image with layout %s can result in undefined behavior if this memory is "
2233 "used by the device. Only GENERAL or PREINITIALIZED should be used.",
2234 string_VkImageLayout(layout));
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002235 }
2236 }
2237 }
2238 }
2239 }
2240 }
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002241 return skip;
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002242}
Mark Lobodzinski96210742017-02-09 10:33:46 -07002243
2244// Helper function to validate correct usage bits set for buffers or images. Verify that (actual & desired) flags != 0 or, if strict
2245// is true, verify that (actual & desired) flags == desired
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002246static 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 -07002247 VkDebugReportObjectTypeEXT obj_type, int32_t const msgCode, char const *ty_str,
2248 char const *func_name, char const *usage_str) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002249 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002250
2251 bool correct_usage = false;
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002252 bool skip = false;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002253 if (strict) {
2254 correct_usage = ((actual & desired) == desired);
2255 } else {
2256 correct_usage = ((actual & desired) != 0);
2257 }
2258 if (!correct_usage) {
2259 if (msgCode == -1) {
2260 // TODO: Fix callers with msgCode == -1 to use correct validation checks.
Dave Houltoneba86e22017-03-02 14:56:23 -07002261 skip = log_msg(
2262 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, obj_type, obj_handle, __LINE__, MEMTRACK_INVALID_USAGE_FLAG, "MEM",
2263 "Invalid usage flag for %s 0x%" PRIxLEAST64 " used by %s. In this case, %s should have %s set during creation.",
2264 ty_str, obj_handle, func_name, ty_str, usage_str);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002265 } else {
2266 const char *valid_usage = (msgCode == -1) ? "" : validation_error_map[msgCode];
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002267 skip = log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, obj_type, obj_handle, __LINE__, msgCode, "MEM",
2268 "Invalid usage flag for %s 0x%" PRIxLEAST64
2269 " used by %s. In this case, %s should have %s set during creation. %s",
2270 ty_str, obj_handle, func_name, ty_str, usage_str, valid_usage);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002271 }
2272 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002273 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002274}
2275
2276// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2277// where an error will be flagged if usage is not correct
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002278bool ValidateImageUsageFlags(layer_data *device_data, IMAGE_STATE const *image_state, VkFlags desired, VkBool32 strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002279 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002280 return validate_usage_flags(device_data, image_state->createInfo.usage, desired, strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002281 reinterpret_cast<const uint64_t &>(image_state->image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2282 msgCode, "image", func_name, usage_string);
2283}
2284
2285// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2286// where an error will be flagged if usage is not correct
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002287bool ValidateBufferUsageFlags(layer_data *device_data, BUFFER_STATE const *buffer_state, VkFlags desired, VkBool32 strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002288 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002289 return validate_usage_flags(device_data, buffer_state->createInfo.usage, desired, strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002290 reinterpret_cast<const uint64_t &>(buffer_state->buffer), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT,
2291 msgCode, "buffer", func_name, usage_string);
2292}
2293
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002294bool PreCallValidateCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo) {
Mark Lobodzinski96210742017-02-09 10:33:46 -07002295 bool skip = false;
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002296 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2297
Mark Lobodzinski96210742017-02-09 10:33:46 -07002298 // TODO: Add check for VALIDATION_ERROR_00658
Mark Lobodzinski96210742017-02-09 10:33:46 -07002299 // TODO: Add check for VALIDATION_ERROR_00667
2300 // TODO: Add check for VALIDATION_ERROR_00668
2301 // TODO: Add check for VALIDATION_ERROR_00669
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002302
2303 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
2304 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2305 VALIDATION_ERROR_00666, "DS",
2306 "vkCreateBuffer(): the sparseBinding device feature is disabled: Buffers cannot be created with the "
2307 "VK_BUFFER_CREATE_SPARSE_BINDING_BIT set. %s",
2308 validation_error_map[VALIDATION_ERROR_00666]);
2309 }
Mark Lobodzinskiaf355062017-03-13 09:35:01 -06002310
2311 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyBuffer)) {
2312 skip |=
2313 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2314 DRAWSTATE_INVALID_FEATURE, "DS",
2315 "vkCreateBuffer(): the sparseResidencyBuffer device feature is disabled: Buffers cannot be created with the "
2316 "VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT set.");
2317 }
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -06002318
2319 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
2320 skip |=
2321 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2322 DRAWSTATE_INVALID_FEATURE, "DS",
2323 "vkCreateBuffer(): the sparseResidencyAliased device feature is disabled: Buffers cannot be created with the "
2324 "VK_BUFFER_CREATE_SPARSE_ALIASED_BIT set.");
2325 }
Mark Lobodzinski96210742017-02-09 10:33:46 -07002326 return skip;
2327}
2328
2329void PostCallRecordCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo, VkBuffer *pBuffer) {
2330 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
2331 GetBufferMap(device_data)
2332 ->insert(std::make_pair(*pBuffer, std::unique_ptr<BUFFER_STATE>(new BUFFER_STATE(*pBuffer, pCreateInfo))));
2333}
2334
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002335bool PreCallValidateCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo) {
2336 bool skip = false;
2337 BUFFER_STATE *buffer_state = GetBufferState(device_data, pCreateInfo->buffer);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002338 // If this isn't a sparse buffer, it needs to have memory backing it at CreateBufferView time
2339 if (buffer_state) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002340 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCreateBufferView()", VALIDATION_ERROR_02522);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002341 // In order to create a valid buffer view, the buffer must have been created with at least one of the following flags:
2342 // UNIFORM_TEXEL_BUFFER_BIT or STORAGE_TEXEL_BUFFER_BIT
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002343 skip |= ValidateBufferUsageFlags(
2344 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 -07002345 VALIDATION_ERROR_00694, "vkCreateBufferView()", "VK_BUFFER_USAGE_[STORAGE|UNIFORM]_TEXEL_BUFFER_BIT");
2346 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002347 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002348}
2349
2350void PostCallRecordCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo, VkBufferView *pView) {
2351 (*GetBufferViewMap(device_data))[*pView] = std::unique_ptr<BUFFER_VIEW_STATE>(new BUFFER_VIEW_STATE(*pView, pCreateInfo));
2352}
Mark Lobodzinski602de982017-02-09 11:01:33 -07002353
2354// For the given format verify that the aspect masks make sense
2355bool ValidateImageAspectMask(layer_data *device_data, VkImage image, VkFormat format, VkImageAspectFlags aspect_mask,
2356 const char *func_name) {
2357 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2358 bool skip = false;
2359 if (vk_format_is_color(format)) {
2360 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
2361 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2362 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2363 "%s: Color image formats must have the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2364 validation_error_map[VALIDATION_ERROR_00741]);
2365 } else if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != aspect_mask) {
2366 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2367 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2368 "%s: Color image formats must have ONLY the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2369 validation_error_map[VALIDATION_ERROR_00741]);
2370 }
2371 } else if (vk_format_is_depth_and_stencil(format)) {
2372 if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) == 0) {
2373 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2374 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2375 "%s: Depth/stencil image formats must have "
2376 "at least one of VK_IMAGE_ASPECT_DEPTH_BIT "
2377 "and VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2378 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2379 } else if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) != aspect_mask) {
2380 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2381 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2382 "%s: Combination depth/stencil image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT and "
2383 "VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2384 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2385 }
2386 } else if (vk_format_is_depth_only(format)) {
2387 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) {
2388 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2389 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2390 "%s: Depth-only image formats must have the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2391 validation_error_map[VALIDATION_ERROR_00741]);
2392 } else if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_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: Depth-only image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2396 validation_error_map[VALIDATION_ERROR_00741]);
2397 }
2398 } else if (vk_format_is_stencil_only(format)) {
2399 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT) {
2400 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2401 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2402 "%s: Stencil-only image formats must have the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2403 validation_error_map[VALIDATION_ERROR_00741]);
2404 } else if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != aspect_mask) {
2405 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2406 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2407 "%s: Stencil-only image formats can have only the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2408 validation_error_map[VALIDATION_ERROR_00741]);
2409 }
2410 }
2411 return skip;
2412}
2413
2414bool ValidateImageSubrangeLevelLayerCounts(layer_data *device_data, const VkImageSubresourceRange &subresourceRange,
2415 const char *func_name) {
2416 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2417 bool skip = false;
2418 if (subresourceRange.levelCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002419 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 -07002420 VALIDATION_ERROR_00768, "IMAGE", "%s called with 0 in subresourceRange.levelCount. %s", func_name,
2421 validation_error_map[VALIDATION_ERROR_00768]);
2422 }
2423 if (subresourceRange.layerCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002424 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 -07002425 VALIDATION_ERROR_00769, "IMAGE", "%s called with 0 in subresourceRange.layerCount. %s", func_name,
2426 validation_error_map[VALIDATION_ERROR_00769]);
2427 }
2428 return skip;
2429}
2430
2431bool PreCallValidateCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info) {
2432 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2433 bool skip = false;
2434 IMAGE_STATE *image_state = GetImageState(device_data, create_info->image);
2435 if (image_state) {
2436 skip |= ValidateImageUsageFlags(
2437 device_data, image_state,
2438 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
2439 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
2440 false, -1, "vkCreateImageView()",
2441 "VK_IMAGE_USAGE_[SAMPLED|STORAGE|COLOR_ATTACHMENT|DEPTH_STENCIL_ATTACHMENT|INPUT_ATTACHMENT]_BIT");
2442 // If this isn't a sparse image, it needs to have memory backing it at CreateImageView time
2443 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCreateImageView()", VALIDATION_ERROR_02524);
2444 // Checks imported from image layer
2445 if (create_info->subresourceRange.baseMipLevel >= image_state->createInfo.mipLevels) {
2446 std::stringstream ss;
2447 ss << "vkCreateImageView called with baseMipLevel " << create_info->subresourceRange.baseMipLevel << " for image "
2448 << create_info->image << " that only has " << image_state->createInfo.mipLevels << " mip levels.";
2449 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002450 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 -07002451 VALIDATION_ERROR_00768, "IMAGE", "%s %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_00768]);
2452 }
2453 if (create_info->subresourceRange.baseArrayLayer >= image_state->createInfo.arrayLayers) {
2454 std::stringstream ss;
2455 ss << "vkCreateImageView called with baseArrayLayer " << create_info->subresourceRange.baseArrayLayer << " for image "
2456 << create_info->image << " that only has " << image_state->createInfo.arrayLayers << " array layers.";
2457 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002458 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 -07002459 VALIDATION_ERROR_00769, "IMAGE", "%s %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_00769]);
2460 }
2461 // TODO: Need new valid usage language for levelCount == 0 & layerCount == 0
2462 skip |= ValidateImageSubrangeLevelLayerCounts(device_data, create_info->subresourceRange, "vkCreateImageView()");
2463
2464 VkImageCreateFlags image_flags = image_state->createInfo.flags;
2465 VkFormat image_format = image_state->createInfo.format;
2466 VkFormat view_format = create_info->format;
2467 VkImageAspectFlags aspect_mask = create_info->subresourceRange.aspectMask;
2468
2469 // Validate VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT state
2470 if (image_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
2471 // Format MUST be compatible (in the same format compatibility class) as the format the image was created with
2472 if (vk_format_get_compatibility_class(image_format) != vk_format_get_compatibility_class(view_format)) {
2473 std::stringstream ss;
2474 ss << "vkCreateImageView(): ImageView format " << string_VkFormat(view_format)
2475 << " is not in the same format compatibility class as image (" << (uint64_t)create_info->image << ") format "
2476 << string_VkFormat(image_format) << ". Images created with the VK_IMAGE_CREATE_MUTABLE_FORMAT BIT "
2477 << "can support ImageViews with differing formats but they must be in the same compatibility class.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002478 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 -07002479 VALIDATION_ERROR_02171, "IMAGE", "%s %s", ss.str().c_str(),
2480 validation_error_map[VALIDATION_ERROR_02171]);
2481 }
2482 } else {
2483 // Format MUST be IDENTICAL to the format the image was created with
2484 if (image_format != view_format) {
2485 std::stringstream ss;
2486 ss << "vkCreateImageView() format " << string_VkFormat(view_format) << " differs from image "
2487 << (uint64_t)create_info->image << " format " << string_VkFormat(image_format)
2488 << ". Formats MUST be IDENTICAL unless VK_IMAGE_CREATE_MUTABLE_FORMAT BIT was set on image creation.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002489 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 -07002490 VALIDATION_ERROR_02172, "IMAGE", "%s %s", ss.str().c_str(),
2491 validation_error_map[VALIDATION_ERROR_02172]);
2492 }
2493 }
2494
2495 // Validate correct image aspect bits for desired formats and format consistency
2496 skip |= ValidateImageAspectMask(device_data, image_state->image, image_format, aspect_mask, "vkCreateImageView()");
2497 }
2498 return skip;
2499}
2500
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -07002501void PostCallRecordCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info, VkImageView view) {
2502 auto image_view_map = GetImageViewMap(device_data);
2503 (*image_view_map)[view] = std::unique_ptr<IMAGE_VIEW_STATE>(new IMAGE_VIEW_STATE(view, create_info));
2504
2505 auto image_state = GetImageState(device_data, create_info->image);
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002506 auto &sub_res_range = (*image_view_map)[view].get()->create_info.subresourceRange;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -06002507 sub_res_range.levelCount = ResolveRemainingLevels(&sub_res_range, image_state->createInfo.mipLevels);
2508 sub_res_range.layerCount = ResolveRemainingLayers(&sub_res_range, image_state->createInfo.arrayLayers);
Mark Lobodzinski602de982017-02-09 11:01:33 -07002509}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002510
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002511bool PreCallValidateCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2512 BUFFER_STATE *dst_buffer_state) {
2513 bool skip = false;
2514 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02531);
2515 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02532);
2516 // Validate that SRC & DST buffers have correct usage flags set
2517 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01164,
2518 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
2519 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01165,
2520 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07002521 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyBuffer()",
2522 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01171);
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002523 skip |= ValidateCmd(device_data, cb_node, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
2524 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBuffer()", VALIDATION_ERROR_01172);
2525 return skip;
2526}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002527
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002528void PreCallRecordCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2529 BUFFER_STATE *dst_buffer_state) {
2530 // Update bindings between buffers and cmd buffer
2531 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
2532 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
2533
2534 std::function<bool()> function = [=]() {
2535 return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBuffer()");
2536 };
2537 cb_node->validate_functions.push_back(function);
2538 function = [=]() {
2539 SetBufferMemoryValid(device_data, dst_buffer_state, true);
2540 return false;
2541 };
2542 cb_node->validate_functions.push_back(function);
2543 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFER);
2544}
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002545
2546static bool validateIdleBuffer(layer_data *device_data, VkBuffer buffer) {
2547 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2548 bool skip = false;
2549 auto buffer_state = GetBufferState(device_data, buffer);
2550 if (!buffer_state) {
2551 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2552 __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
2553 "Cannot free buffer 0x%" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
2554 } else {
2555 if (buffer_state->in_use.load()) {
2556 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2557 __LINE__, VALIDATION_ERROR_00676, "DS",
2558 "Cannot free buffer 0x%" PRIxLEAST64 " that is in use by a command buffer. %s", (uint64_t)(buffer),
2559 validation_error_map[VALIDATION_ERROR_00676]);
2560 }
2561 }
2562 return skip;
2563}
2564
2565bool PreCallValidateDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE **image_view_state,
2566 VK_OBJECT *obj_struct) {
2567 *image_view_state = GetImageViewState(device_data, image_view);
2568 *obj_struct = {reinterpret_cast<uint64_t &>(image_view), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_VIEW_EXT};
2569 if (GetDisables(device_data)->destroy_image_view) return false;
2570 bool skip = false;
2571 if (*image_view_state) {
2572 skip |= ValidateObjectNotInUse(device_data, *image_view_state, *obj_struct, VALIDATION_ERROR_00776);
2573 }
2574 return skip;
2575}
2576
2577void PostCallRecordDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE *image_view_state,
2578 VK_OBJECT obj_struct) {
2579 // Any bound cmd buffers are now invalid
2580 invalidateCommandBuffers(device_data, image_view_state->cb_bindings, obj_struct);
2581 (*GetImageViewMap(device_data)).erase(image_view);
2582}
2583
2584bool PreCallValidateDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE **buffer_state, VK_OBJECT *obj_struct) {
2585 *buffer_state = GetBufferState(device_data, buffer);
2586 *obj_struct = {reinterpret_cast<uint64_t &>(buffer), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT};
2587 if (GetDisables(device_data)->destroy_buffer) return false;
2588 bool skip = false;
2589 if (*buffer_state) {
2590 skip |= validateIdleBuffer(device_data, buffer);
2591 }
2592 return skip;
2593}
2594
2595void PostCallRecordDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE *buffer_state, VK_OBJECT obj_struct) {
2596 invalidateCommandBuffers(device_data, buffer_state->cb_bindings, obj_struct);
2597 for (auto mem_binding : buffer_state->GetBoundMemory()) {
2598 auto mem_info = GetMemObjInfo(device_data, mem_binding);
2599 if (mem_info) {
2600 core_validation::RemoveBufferMemoryRange(reinterpret_cast<uint64_t &>(buffer), mem_info);
2601 }
2602 }
2603 ClearMemoryObjectBindings(device_data, reinterpret_cast<uint64_t &>(buffer), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT);
2604 GetBufferMap(device_data)->erase(buffer_state->buffer);
2605}
2606
2607bool PreCallValidateDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE **buffer_view_state,
2608 VK_OBJECT *obj_struct) {
2609 *buffer_view_state = GetBufferViewState(device_data, buffer_view);
2610 *obj_struct = {reinterpret_cast<uint64_t &>(buffer_view), VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_VIEW_EXT};
2611 if (GetDisables(device_data)->destroy_buffer_view) return false;
2612 bool skip = false;
2613 if (*buffer_view_state) {
2614 skip |= ValidateObjectNotInUse(device_data, *buffer_view_state, *obj_struct, VALIDATION_ERROR_00701);
2615 }
2616 return skip;
2617}
2618
2619void PostCallRecordDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE *buffer_view_state,
2620 VK_OBJECT obj_struct) {
2621 // Any bound cmd buffers are now invalid
2622 invalidateCommandBuffers(device_data, buffer_view_state->cb_bindings, obj_struct);
2623 GetBufferViewMap(device_data)->erase(buffer_view);
2624}
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002625
2626bool PreCallValidateCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2627 bool skip = false;
2628 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCmdFillBuffer()", VALIDATION_ERROR_02529);
Mike Schuchardt9c582402017-02-23 15:57:37 -07002629 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdFillBuffer()",
2630 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01141);
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002631 skip |= ValidateCmd(device_data, cb_node, CMD_FILLBUFFER, "vkCmdFillBuffer()");
2632 // Validate that DST buffer has correct usage flags set
2633 skip |= ValidateBufferUsageFlags(device_data, buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01137,
2634 "vkCmdFillBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
2635 skip |= insideRenderPass(device_data, cb_node, "vkCmdFillBuffer()", VALIDATION_ERROR_01142);
2636 return skip;
2637}
2638
2639void PreCallRecordCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2640 std::function<bool()> function = [=]() {
2641 SetBufferMemoryValid(device_data, buffer_state, true);
2642 return false;
2643 };
2644 cb_node->validate_functions.push_back(function);
2645 // Update bindings between buffer and cmd buffer
2646 AddCommandBufferBindingBuffer(device_data, cb_node, buffer_state);
2647 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_FILLBUFFER);
2648}
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002649
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002650bool ValidateBufferImageCopyData(const debug_report_data *report_data, uint32_t regionCount, const VkBufferImageCopy *pRegions,
2651 IMAGE_STATE *image_state, const char *function) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002652 bool skip = false;
2653
2654 for (uint32_t i = 0; i < regionCount; i++) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002655 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) {
2656 if ((pRegions[i].imageOffset.y != 0) || (pRegions[i].imageExtent.height != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002657 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 -07002658 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01746, "IMAGE",
2659 "%s(): pRegion[%d] imageOffset.y is %d and imageExtent.height is %d. For 1D images these "
2660 "must be 0 and 1, respectively. %s",
2661 function, i, pRegions[i].imageOffset.y, pRegions[i].imageExtent.height,
2662 validation_error_map[VALIDATION_ERROR_01746]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002663 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002664 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002665
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002666 if ((image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) || (image_state->createInfo.imageType == VK_IMAGE_TYPE_2D)) {
2667 if ((pRegions[i].imageOffset.z != 0) || (pRegions[i].imageExtent.depth != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002668 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 -07002669 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01747, "IMAGE",
2670 "%s(): pRegion[%d] imageOffset.z is %d and imageExtent.depth is %d. For 1D and 2D images these "
2671 "must be 0 and 1, respectively. %s",
2672 function, i, pRegions[i].imageOffset.z, pRegions[i].imageExtent.depth,
2673 validation_error_map[VALIDATION_ERROR_01747]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002674 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002675 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002676
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002677 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) {
2678 if ((0 != pRegions[i].imageSubresource.baseArrayLayer) || (1 != pRegions[i].imageSubresource.layerCount)) {
2679 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2680 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01281, "IMAGE",
2681 "%s(): pRegion[%d] imageSubresource.baseArrayLayer is %d and imageSubresource.layerCount is "
2682 "%d. For 3D images these must be 0 and 1, respectively. %s",
2683 function, i, pRegions[i].imageSubresource.baseArrayLayer, pRegions[i].imageSubresource.layerCount,
2684 validation_error_map[VALIDATION_ERROR_01281]);
2685 }
2686 }
2687
2688 // If the the calling command's VkImage parameter's format is not a depth/stencil format,
2689 // then bufferOffset must be a multiple of the calling command's VkImage parameter's texel size
2690 auto texel_size = vk_format_get_size(image_state->createInfo.format);
2691 if (!vk_format_is_depth_and_stencil(image_state->createInfo.format) &&
2692 vk_safe_modulo(pRegions[i].bufferOffset, texel_size) != 0) {
2693 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2694 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01263, "IMAGE",
2695 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64
2696 " must be a multiple of this format's texel size (" PRINTF_SIZE_T_SPECIFIER "). %s",
2697 function, i, pRegions[i].bufferOffset, texel_size, validation_error_map[VALIDATION_ERROR_01263]);
2698 }
2699
2700 // BufferOffset must be a multiple of 4
2701 if (vk_safe_modulo(pRegions[i].bufferOffset, 4) != 0) {
2702 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2703 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01264, "IMAGE",
2704 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64 " must be a multiple of 4. %s", function, i,
2705 pRegions[i].bufferOffset, validation_error_map[VALIDATION_ERROR_01264]);
2706 }
2707
2708 // BufferRowLength must be 0, or greater than or equal to the width member of imageExtent
2709 if ((pRegions[i].bufferRowLength != 0) && (pRegions[i].bufferRowLength < pRegions[i].imageExtent.width)) {
2710 skip |= log_msg(
2711 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2712 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01265, "IMAGE",
2713 "%s(): pRegion[%d] bufferRowLength (%d) must be zero or greater-than-or-equal-to imageExtent.width (%d). %s",
2714 function, i, pRegions[i].bufferRowLength, pRegions[i].imageExtent.width,
2715 validation_error_map[VALIDATION_ERROR_01265]);
2716 }
2717
2718 // BufferImageHeight must be 0, or greater than or equal to the height member of imageExtent
2719 if ((pRegions[i].bufferImageHeight != 0) && (pRegions[i].bufferImageHeight < pRegions[i].imageExtent.height)) {
2720 skip |= log_msg(
2721 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2722 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01266, "IMAGE",
2723 "%s(): pRegion[%d] bufferImageHeight (%d) must be zero or greater-than-or-equal-to imageExtent.height (%d). %s",
2724 function, i, pRegions[i].bufferImageHeight, pRegions[i].imageExtent.height,
2725 validation_error_map[VALIDATION_ERROR_01266]);
2726 }
2727
2728 // subresource aspectMask must have exactly 1 bit set
2729 const int num_bits = sizeof(VkFlags) * CHAR_BIT;
2730 std::bitset<num_bits> aspect_mask_bits(pRegions[i].imageSubresource.aspectMask);
2731 if (aspect_mask_bits.count() != 1) {
2732 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2733 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01280, "IMAGE",
2734 "%s: aspectMasks for imageSubresource in each region must have only a single bit set. %s", function,
2735 validation_error_map[VALIDATION_ERROR_01280]);
2736 }
2737
2738 // image subresource aspect bit must match format
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002739 if (!VerifyAspectsPresent(pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002740 skip |= log_msg(
2741 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2742 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01279, "IMAGE",
2743 "%s(): pRegion[%d] subresource aspectMask 0x%x specifies aspects that are not present in image format 0x%x. %s",
2744 function, i, pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format,
2745 validation_error_map[VALIDATION_ERROR_01279]);
2746 }
2747
2748 // Checks that apply only to compressed images
2749 // TODO: there is a comment in ValidateCopyBufferImageTransferGranularityRequirements() in core_validation.cpp that
2750 // reserves a place for these compressed image checks. This block of code could move there once the image
2751 // stuff is moved into core validation.
2752 if (vk_format_is_compressed(image_state->createInfo.format)) {
2753 auto block_size = vk_format_compressed_texel_block_extents(image_state->createInfo.format);
2754
2755 // BufferRowLength must be a multiple of block width
2756 if (vk_safe_modulo(pRegions[i].bufferRowLength, block_size.width) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002757 skip |= log_msg(
2758 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002759 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01271, "IMAGE",
2760 "%s(): pRegion[%d] bufferRowLength (%d) must be a multiple of the compressed image's texel width (%d). %s.",
2761 function, i, pRegions[i].bufferRowLength, block_size.width, validation_error_map[VALIDATION_ERROR_01271]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002762 }
2763
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002764 // BufferRowHeight must be a multiple of block height
2765 if (vk_safe_modulo(pRegions[i].bufferImageHeight, block_size.height) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002766 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 -07002767 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01272, "IMAGE",
2768 "%s(): pRegion[%d] bufferImageHeight (%d) must be a multiple of the compressed image's texel "
2769 "height (%d). %s.",
2770 function, i, pRegions[i].bufferImageHeight, block_size.height,
2771 validation_error_map[VALIDATION_ERROR_01272]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002772 }
2773
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002774 // image offsets must be multiples of block dimensions
2775 if ((vk_safe_modulo(pRegions[i].imageOffset.x, block_size.width) != 0) ||
Dave Houlton75967fc2017-03-06 17:21:16 -07002776 (vk_safe_modulo(pRegions[i].imageOffset.y, block_size.height) != 0) ||
2777 (vk_safe_modulo(pRegions[i].imageOffset.z, block_size.depth) != 0)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002778 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2779 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01273, "IMAGE",
2780 "%s(): pRegion[%d] imageOffset(x,y) (%d, %d) must be multiples of the compressed image's texel "
2781 "width & height (%d, %d). %s.",
2782 function, i, pRegions[i].imageOffset.x, pRegions[i].imageOffset.y, block_size.width,
2783 block_size.height, validation_error_map[VALIDATION_ERROR_01273]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002784 }
2785
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002786 // bufferOffset must be a multiple of block size (linear bytes)
2787 size_t block_size_in_bytes = vk_format_get_size(image_state->createInfo.format);
2788 if (vk_safe_modulo(pRegions[i].bufferOffset, block_size_in_bytes) != 0) {
2789 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2790 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01274, "IMAGE",
2791 "%s(): pRegion[%d] bufferOffset (0x%" PRIxLEAST64
2792 ") must be a multiple of the compressed image's texel block "
2793 "size (" PRINTF_SIZE_T_SPECIFIER "). %s.",
2794 function, i, pRegions[i].bufferOffset, block_size_in_bytes,
2795 validation_error_map[VALIDATION_ERROR_01274]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002796 }
Dave Houlton67e9b532017-03-02 17:00:10 -07002797
2798 // imageExtent width must be a multiple of block width, or extent+offset width must equal subresource width
Dave Houlton75967fc2017-03-06 17:21:16 -07002799 VkExtent3D mip_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
Dave Houlton67e9b532017-03-02 17:00:10 -07002800 if ((vk_safe_modulo(pRegions[i].imageExtent.width, block_size.width) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002801 (pRegions[i].imageExtent.width + pRegions[i].imageOffset.x != mip_extent.width)) {
2802 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2803 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01275, "IMAGE",
2804 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block width "
2805 "(%d), or when added to offset.x (%d) must equal the image subresource width (%d). %s.",
2806 function, i, pRegions[i].imageExtent.width, block_size.width, pRegions[i].imageOffset.x,
2807 mip_extent.width, validation_error_map[VALIDATION_ERROR_01275]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002808 }
2809
2810 // imageExtent height must be a multiple of block height, or extent+offset height must equal subresource height
2811 if ((vk_safe_modulo(pRegions[i].imageExtent.height, block_size.height) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002812 (pRegions[i].imageExtent.height + pRegions[i].imageOffset.y != mip_extent.height)) {
2813 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2814 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01276, "IMAGE",
2815 "%s(): pRegion[%d] extent height (%d) must be a multiple of the compressed texture block height "
2816 "(%d), or when added to offset.y (%d) must equal the image subresource height (%d). %s.",
2817 function, i, pRegions[i].imageExtent.height, block_size.height, pRegions[i].imageOffset.y,
2818 mip_extent.height, validation_error_map[VALIDATION_ERROR_01276]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002819 }
2820
2821 // imageExtent depth must be a multiple of block depth, or extent+offset depth must equal subresource depth
2822 if ((vk_safe_modulo(pRegions[i].imageExtent.depth, block_size.depth) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002823 (pRegions[i].imageExtent.depth + pRegions[i].imageOffset.z != mip_extent.depth)) {
2824 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2825 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01277, "IMAGE",
2826 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block depth "
2827 "(%d), or when added to offset.z (%d) must equal the image subresource depth (%d). %s.",
2828 function, i, pRegions[i].imageExtent.depth, block_size.depth, pRegions[i].imageOffset.z,
2829 mip_extent.depth, validation_error_map[VALIDATION_ERROR_01277]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002830 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002831 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002832 }
2833
2834 return skip;
2835}
2836
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002837static bool ValidateImageBounds(const debug_report_data *report_data, const IMAGE_STATE *image_state, const uint32_t regionCount,
2838 const VkBufferImageCopy *pRegions, const char *func_name, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002839 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002840 const VkImageCreateInfo *image_info = &(image_state->createInfo);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002841
2842 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002843 VkExtent3D extent = pRegions[i].imageExtent;
2844 VkOffset3D offset = pRegions[i].imageOffset;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002845
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002846 if (IsExtentSizeZero(&extent)) // Warn on zero area subresource
2847 {
2848 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2849 (uint64_t)0, __LINE__, IMAGE_ZERO_AREA_SUBREGION, "IMAGE",
2850 "%s: pRegion[%d] imageExtent of {%1d, %1d, %1d} has zero area", func_name, i, extent.width,
2851 extent.height, extent.depth);
2852 }
2853
2854 VkExtent3D image_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
2855
2856 // If we're using a compressed format, valid extent is rounded up to multiple of block size (per 18.1)
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002857 if (vk_format_is_compressed(image_info->format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002858 auto block_extent = vk_format_compressed_texel_block_extents(image_info->format);
2859 if (image_extent.width % block_extent.width) {
2860 image_extent.width += (block_extent.width - (image_extent.width % block_extent.width));
2861 }
2862 if (image_extent.height % block_extent.height) {
2863 image_extent.height += (block_extent.height - (image_extent.height % block_extent.height));
2864 }
2865 if (image_extent.depth % block_extent.depth) {
2866 image_extent.depth += (block_extent.depth - (image_extent.depth % block_extent.depth));
2867 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002868 }
2869
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002870 if (ExceedsBounds(&offset, &extent, &image_extent)) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002871 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 -07002872 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds image bounds. %s.", func_name, i,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002873 validation_error_map[msg_code]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002874 }
2875 }
2876
2877 return skip;
2878}
2879
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002880static inline bool ValidtateBufferBounds(const debug_report_data *report_data, IMAGE_STATE *image_state, BUFFER_STATE *buff_state,
2881 uint32_t regionCount, const VkBufferImageCopy *pRegions, const char *func_name,
2882 UNIQUE_VALIDATION_ERROR_CODE msg_code) {
2883 bool skip = false;
2884
2885 VkDeviceSize buffer_size = buff_state->createInfo.size;
2886
2887 for (uint32_t i = 0; i < regionCount; i++) {
2888 VkExtent3D copy_extent = pRegions[i].imageExtent;
2889
2890 VkDeviceSize buffer_width = (0 == pRegions[i].bufferRowLength ? copy_extent.width : pRegions[i].bufferRowLength);
2891 VkDeviceSize buffer_height = (0 == pRegions[i].bufferImageHeight ? copy_extent.height : pRegions[i].bufferImageHeight);
2892 VkDeviceSize unit_size = vk_format_get_size(image_state->createInfo.format); // size (bytes) of texel or block
2893
Dave Houltonf3229d52017-02-21 15:59:08 -07002894 // Handle special buffer packing rules for specific depth/stencil formats
2895 if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
2896 unit_size = vk_format_get_size(VK_FORMAT_S8_UINT);
2897 } else if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
2898 switch (image_state->createInfo.format) {
2899 case VK_FORMAT_D16_UNORM_S8_UINT:
2900 unit_size = vk_format_get_size(VK_FORMAT_D16_UNORM);
2901 break;
2902 case VK_FORMAT_D32_SFLOAT_S8_UINT:
2903 unit_size = vk_format_get_size(VK_FORMAT_D32_SFLOAT);
2904 break;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002905 case VK_FORMAT_X8_D24_UNORM_PACK32: // Fall through
Dave Houltonf3229d52017-02-21 15:59:08 -07002906 case VK_FORMAT_D24_UNORM_S8_UINT:
2907 unit_size = 4;
2908 break;
2909 default:
2910 break;
2911 }
2912 }
2913
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002914 if (vk_format_is_compressed(image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002915 // Switch to texel block units, rounding up for any partially-used blocks
2916 auto block_dim = vk_format_compressed_texel_block_extents(image_state->createInfo.format);
2917 buffer_width = (buffer_width + block_dim.width - 1) / block_dim.width;
2918 buffer_height = (buffer_height + block_dim.height - 1) / block_dim.height;
2919
2920 copy_extent.width = (copy_extent.width + block_dim.width - 1) / block_dim.width;
2921 copy_extent.height = (copy_extent.height + block_dim.height - 1) / block_dim.height;
2922 copy_extent.depth = (copy_extent.depth + block_dim.depth - 1) / block_dim.depth;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002923 }
2924
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002925 // Either depth or layerCount may be greater than 1 (not both). This is the number of 'slices' to copy
2926 uint32_t z_copies = std::max(copy_extent.depth, pRegions[i].imageSubresource.layerCount);
2927 if (IsExtentSizeZero(&copy_extent) || (0 == z_copies)) {
2928 // TODO: Issure warning here? Already warned in ValidateImageBounds()...
2929 } else {
2930 // Calculate buffer offset of final copied byte, + 1.
2931 VkDeviceSize max_buffer_offset = (z_copies - 1) * buffer_height * buffer_width; // offset to slice
2932 max_buffer_offset += ((copy_extent.height - 1) * buffer_width) + copy_extent.width; // add row,col
2933 max_buffer_offset *= unit_size; // convert to bytes
2934 max_buffer_offset += pRegions[i].bufferOffset; // add initial offset (bytes)
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002935
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002936 if (buffer_size < max_buffer_offset) {
2937 skip |=
2938 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)0,
2939 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds buffer size of %" PRIu64 " bytes. %s.", func_name,
2940 i, buffer_size, validation_error_map[msg_code]);
2941 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002942 }
2943 }
2944
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002945 return skip;
2946}
2947
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002948bool PreCallValidateCmdCopyImageToBuffer(layer_data *device_data, VkImageLayout srcImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002949 IMAGE_STATE *src_image_state, BUFFER_STATE *dst_buffer_state, uint32_t regionCount,
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002950 const VkBufferImageCopy *pRegions, const char *func_name) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002951 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2952 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, src_image_state, "vkCmdCopyImageToBuffer");
2953
2954 // Validate command buffer state
2955 if (CB_RECORDING != cb_node->state) {
2956 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2957 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01258, "DS",
2958 "Cannot call vkCmdCopyImageToBuffer() on command buffer which is not in recording state. %s.",
2959 validation_error_map[VALIDATION_ERROR_01258]);
2960 } else {
2961 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYIMAGETOBUFFER);
2962 }
2963
2964 // Command pool must support graphics, compute, or transfer operations
2965 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
2966
2967 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
2968 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
2969 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2970 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01259, "DS",
2971 "Cannot call vkCmdCopyImageToBuffer() on a command buffer allocated from a pool without graphics, compute, "
2972 "or transfer capabilities. %s.",
2973 validation_error_map[VALIDATION_ERROR_01259]);
2974 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002975 skip |= ValidateImageBounds(report_data, src_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002976 VALIDATION_ERROR_01245);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002977 skip |= ValidtateBufferBounds(report_data, src_image_state, dst_buffer_state, regionCount, pRegions, "vkCmdCopyImageToBuffer()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002978 VALIDATION_ERROR_01246);
2979
2980 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyImageToBuffer(): srcImage",
2981 VALIDATION_ERROR_01249);
2982 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02537);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002983 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02538);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002984
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002985 // Validate that SRC image & DST buffer have correct usage flags set
2986 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01248,
2987 "vkCmdCopyImageToBuffer()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002988 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01252,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002989 "vkCmdCopyImageToBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002990 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01260);
2991 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06002992 skip |= VerifyImageLayout(device_data, cb_node, src_image_state, pRegions[i].imageSubresource, srcImageLayout,
2993 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01251);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002994 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, src_image_state, &pRegions[i], i,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06002995 "vkCmdCopyImageToBuffer()");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002996 }
2997 return skip;
2998}
2999
3000void PreCallRecordCmdCopyImageToBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003001 BUFFER_STATE *dst_buffer_state, uint32_t region_count, const VkBufferImageCopy *regions,
3002 VkImageLayout src_image_layout) {
3003 // Make sure that all image slices are updated to correct layout
3004 for (uint32_t i = 0; i < region_count; ++i) {
3005 SetImageLayout(device_data, cb_node, src_image_state, regions[i].imageSubresource, src_image_layout);
3006 }
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003007 // Update bindings between buffer/image and cmd buffer
3008 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003009 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003010
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003011 std::function<bool()> function = [=]() {
3012 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImageToBuffer()");
3013 };
3014 cb_node->validate_functions.push_back(function);
3015 function = [=]() {
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003016 SetBufferMemoryValid(device_data, dst_buffer_state, true);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003017 return false;
3018 };
3019 cb_node->validate_functions.push_back(function);
3020
3021 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGETOBUFFER);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003022}
3023
3024bool PreCallValidateCmdCopyBufferToImage(layer_data *device_data, VkImageLayout dstImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003025 BUFFER_STATE *src_buffer_state, IMAGE_STATE *dst_image_state, uint32_t regionCount,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003026 const VkBufferImageCopy *pRegions, const char *func_name) {
3027 const debug_report_data *report_data = core_validation::GetReportData(device_data);
3028 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, dst_image_state, "vkCmdCopyBufferToImage");
3029
3030 // Validate command buffer state
3031 if (CB_RECORDING != cb_node->state) {
3032 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3033 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01240, "DS",
3034 "Cannot call vkCmdCopyBufferToImage() on command buffer which is not in recording state. %s.",
3035 validation_error_map[VALIDATION_ERROR_01240]);
3036 } else {
3037 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYBUFFERTOIMAGE);
3038 }
3039
3040 // Command pool must support graphics, compute, or transfer operations
3041 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
3042 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
3043 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
3044 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3045 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01241, "DS",
3046 "Cannot call vkCmdCopyBufferToImage() on a command buffer allocated from a pool without graphics, compute, "
3047 "or transfer capabilities. %s.",
3048 validation_error_map[VALIDATION_ERROR_01241]);
3049 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003050 skip |= ValidateImageBounds(report_data, dst_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003051 VALIDATION_ERROR_01228);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003052 skip |= ValidtateBufferBounds(report_data, dst_image_state, src_buffer_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003053 VALIDATION_ERROR_01227);
3054 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyBufferToImage(): dstImage",
3055 VALIDATION_ERROR_01232);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003056 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02535);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003057 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02536);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003058 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01230,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003059 "vkCmdCopyBufferToImage()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
3060 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01231,
3061 "vkCmdCopyBufferToImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003062 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01242);
3063 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06003064 skip |= VerifyImageLayout(device_data, cb_node, dst_image_state, pRegions[i].imageSubresource, dstImageLayout,
3065 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01234);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003066 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &pRegions[i], i,
3067 "vkCmdCopyBufferToImage()");
3068 }
3069 return skip;
3070}
3071
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003072void PreCallRecordCmdCopyBufferToImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003073 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkBufferImageCopy *regions,
3074 VkImageLayout dst_image_layout) {
3075 // Make sure that all image slices are updated to correct layout
3076 for (uint32_t i = 0; i < region_count; ++i) {
3077 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].imageSubresource, dst_image_layout);
3078 }
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003079 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003080 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003081 std::function<bool()> function = [=]() {
3082 SetImageMemoryValid(device_data, dst_image_state, true);
3083 return false;
3084 };
3085 cb_node->validate_functions.push_back(function);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003086 function = [=]() { return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBufferToImage()"); };
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003087 cb_node->validate_functions.push_back(function);
3088
3089 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFERTOIMAGE);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003090}
Mike Weiblen672b58b2017-02-21 14:32:53 -07003091
3092bool PreCallValidateGetImageSubresourceLayout(layer_data *device_data, VkImage image, const VkImageSubresource *pSubresource) {
3093 const auto report_data = core_validation::GetReportData(device_data);
3094 bool skip = false;
3095 const VkImageAspectFlags sub_aspect = pSubresource->aspectMask;
3096
3097 // VU 00733: The aspectMask member of pSubresource must only have a single bit set
3098 const int num_bits = sizeof(sub_aspect) * CHAR_BIT;
3099 std::bitset<num_bits> aspect_mask_bits(sub_aspect);
3100 if (aspect_mask_bits.count() != 1) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003101 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3102 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00733, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003103 "vkGetImageSubresourceLayout(): VkImageSubresource.aspectMask must have exactly 1 bit set. %s",
3104 validation_error_map[VALIDATION_ERROR_00733]);
3105 }
3106
3107 IMAGE_STATE *image_entry = GetImageState(device_data, image);
3108 if (!image_entry) {
3109 return skip;
3110 }
3111
3112 // VU 00732: image must have been created with tiling equal to VK_IMAGE_TILING_LINEAR
3113 if (image_entry->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003114 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3115 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00732, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003116 "vkGetImageSubresourceLayout(): Image must have tiling of VK_IMAGE_TILING_LINEAR. %s",
3117 validation_error_map[VALIDATION_ERROR_00732]);
3118 }
3119
3120 // VU 00739: mipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
3121 if (pSubresource->mipLevel >= image_entry->createInfo.mipLevels) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003122 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3123 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00739, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003124 "vkGetImageSubresourceLayout(): pSubresource.mipLevel (%d) must be less than %d. %s",
3125 pSubresource->mipLevel, image_entry->createInfo.mipLevels, validation_error_map[VALIDATION_ERROR_00739]);
3126 }
3127
3128 // VU 00740: arrayLayer must be less than the arrayLayers specified in VkImageCreateInfo when the image was created
3129 if (pSubresource->arrayLayer >= image_entry->createInfo.arrayLayers) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003130 skip |=
3131 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_00740, "IMAGE",
3133 "vkGetImageSubresourceLayout(): pSubresource.arrayLayer (%d) must be less than %d. %s",
3134 pSubresource->arrayLayer, image_entry->createInfo.arrayLayers, validation_error_map[VALIDATION_ERROR_00740]);
Mike Weiblen672b58b2017-02-21 14:32:53 -07003135 }
3136
3137 // VU 00741: subresource's aspect must be compatible with image's format.
3138 const VkFormat img_format = image_entry->createInfo.format;
3139 if (vk_format_is_color(img_format)) {
3140 if (sub_aspect != VK_IMAGE_ASPECT_COLOR_BIT) {
3141 skip |= log_msg(
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003142 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3143 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003144 "vkGetImageSubresourceLayout(): For color formats, VkImageSubresource.aspectMask must be VK_IMAGE_ASPECT_COLOR. %s",
3145 validation_error_map[VALIDATION_ERROR_00741]);
3146 }
3147 } else if (vk_format_is_depth_or_stencil(img_format)) {
3148 if ((sub_aspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (sub_aspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003149 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3150 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003151 "vkGetImageSubresourceLayout(): For depth/stencil formats, VkImageSubresource.aspectMask must be "
3152 "either VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT. %s",
3153 validation_error_map[VALIDATION_ERROR_00741]);
3154 }
3155 }
3156 return skip;
3157}