blob: 61054b7524ddc1775b675e8cafc87a3ad9519889 [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 Ehlisc8266452017-04-07 12:20:30 -060071bool FindLayoutVerifyNode(layer_data const *device_data, GLOBAL_CB_NODE const *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 Ehlisc8266452017-04-07 12:20:30 -0600103bool FindLayoutVerifyLayout(layer_data const *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 Ehlisc8266452017-04-07 12:20:30 -0600127bool FindCmdBufLayout(layer_data const *device_data, GLOBAL_CB_NODE const *pCB, VkImage image, VkImageSubresource range,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700128 IMAGE_CMD_BUF_LAYOUT_NODE &node) {
129 ImageSubresourcePair imgpair = {image, true, range};
130 node = IMAGE_CMD_BUF_LAYOUT_NODE(VK_IMAGE_LAYOUT_MAX_ENUM, VK_IMAGE_LAYOUT_MAX_ENUM);
131 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_COLOR_BIT);
132 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_DEPTH_BIT);
133 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_STENCIL_BIT);
134 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_METADATA_BIT);
135 if (node.layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
136 imgpair = {image, false, VkImageSubresource()};
137 auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
138 if (imgsubIt == pCB->imageLayoutMap.end()) return false;
139 // TODO: This is ostensibly a find function but it changes state here
140 node = imgsubIt->second;
141 }
142 return true;
143}
144
145// Find layout(s) on the global level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700146bool FindGlobalLayout(layer_data *device_data, ImageSubresourcePair imgpair, VkImageLayout &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700147 layout = VK_IMAGE_LAYOUT_MAX_ENUM;
148 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
149 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
150 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
151 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
152 if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
153 imgpair = {imgpair.image, false, VkImageSubresource()};
154 auto imgsubIt = (*core_validation::GetImageLayoutMap(device_data)).find(imgpair);
155 if (imgsubIt == (*core_validation::GetImageLayoutMap(device_data)).end()) return false;
156 layout = imgsubIt->second.layout;
157 }
158 return true;
159}
160
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700161bool FindLayouts(layer_data *device_data, VkImage image, std::vector<VkImageLayout> &layouts) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700162 auto sub_data = (*core_validation::GetImageSubresourceMap(device_data)).find(image);
163 if (sub_data == (*core_validation::GetImageSubresourceMap(device_data)).end()) return false;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700164 auto image_state = GetImageState(device_data, image);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700165 if (!image_state) return false;
166 bool ignoreGlobal = false;
167 // TODO: Make this robust for >1 aspect mask. Now it will just say ignore potential errors in this case.
168 if (sub_data->second.size() >= (image_state->createInfo.arrayLayers * image_state->createInfo.mipLevels + 1)) {
169 ignoreGlobal = true;
170 }
171 for (auto imgsubpair : sub_data->second) {
172 if (ignoreGlobal && !imgsubpair.hasSubresource) continue;
173 auto img_data = (*core_validation::GetImageLayoutMap(device_data)).find(imgsubpair);
174 if (img_data != (*core_validation::GetImageLayoutMap(device_data)).end()) {
175 layouts.push_back(img_data->second.layout);
176 }
177 }
178 return true;
179}
Tony Barboure0c5cc92017-02-08 13:53:39 -0700180bool FindLayout(const std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
181 VkImageLayout &layout, const VkImageAspectFlags aspectMask) {
Tony Barbourdf013b92017-01-25 12:53:48 -0700182 if (!(imgpair.subresource.aspectMask & aspectMask)) {
183 return false;
184 }
185 imgpair.subresource.aspectMask = aspectMask;
186 auto imgsubIt = imageLayoutMap.find(imgpair);
187 if (imgsubIt == imageLayoutMap.end()) {
188 return false;
189 }
190 layout = imgsubIt->second.layout;
191 return true;
Tony Barboure0c5cc92017-02-08 13:53:39 -0700192}
Tony Barbourdf013b92017-01-25 12:53:48 -0700193
194// find layout in supplied map
Tony Barboure0c5cc92017-02-08 13:53:39 -0700195bool FindLayout(const std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
196 VkImageLayout &layout) {
Tony Barbourdf013b92017-01-25 12:53:48 -0700197 layout = VK_IMAGE_LAYOUT_MAX_ENUM;
198 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
199 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
200 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
201 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
202 if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
203 imgpair = {imgpair.image, false, VkImageSubresource()};
204 auto imgsubIt = imageLayoutMap.find(imgpair);
205 if (imgsubIt == imageLayoutMap.end()) return false;
206 layout = imgsubIt->second.layout;
207 }
208 return true;
209}
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700210
211// Set the layout on the global level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700212void SetGlobalLayout(layer_data *device_data, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700213 VkImage &image = imgpair.image;
214 (*core_validation::GetImageLayoutMap(device_data))[imgpair].layout = layout;
215 auto &image_subresources = (*core_validation::GetImageSubresourceMap(device_data))[image];
216 auto subresource = std::find(image_subresources.begin(), image_subresources.end(), imgpair);
217 if (subresource == image_subresources.end()) {
218 image_subresources.push_back(imgpair);
219 }
220}
221
222// Set the layout on the cmdbuf level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700223void SetLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair, const IMAGE_CMD_BUF_LAYOUT_NODE &node) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700224 pCB->imageLayoutMap[imgpair] = node;
225 auto subresource =
226 std::find(pCB->imageSubresourceMap[imgpair.image].begin(), pCB->imageSubresourceMap[imgpair.image].end(), imgpair);
227 if (subresource == pCB->imageSubresourceMap[imgpair.image].end()) {
228 pCB->imageSubresourceMap[imgpair.image].push_back(imgpair);
229 }
230}
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600231// Set image layout for given VkImageSubresourceRange struct
232void SetImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *image_state,
233 VkImageSubresourceRange image_subresource_range, const VkImageLayout &layout) {
234 assert(image_state);
235 for (uint32_t level_index = 0; level_index < image_subresource_range.levelCount; ++level_index) {
236 uint32_t level = image_subresource_range.baseMipLevel + level_index;
237 for (uint32_t layer_index = 0; layer_index < image_subresource_range.layerCount; layer_index++) {
238 uint32_t layer = image_subresource_range.baseArrayLayer + layer_index;
239 VkImageSubresource sub = {image_subresource_range.aspectMask, level, layer};
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700240 // TODO: If ImageView was created with depth or stencil, transition both layouts as the aspectMask is ignored and both
241 // are used. Verify that the extra implicit layout is OK for descriptor set layout validation
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600242 if (image_subresource_range.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600243 if (FormatIsDepthAndStencil(image_state->createInfo.format)) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700244 sub.aspectMask |= (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
245 }
246 }
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600247 SetLayout(device_data, cb_node, image_state->image, sub, layout);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700248 }
249 }
250}
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600251// Set image layout for given VkImageSubresourceLayers struct
252void SetImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *image_state,
253 VkImageSubresourceLayers image_subresource_layers, const VkImageLayout &layout) {
254 // Transfer VkImageSubresourceLayers into VkImageSubresourceRange struct
255 VkImageSubresourceRange image_subresource_range;
256 image_subresource_range.aspectMask = image_subresource_layers.aspectMask;
257 image_subresource_range.baseArrayLayer = image_subresource_layers.baseArrayLayer;
258 image_subresource_range.layerCount = image_subresource_layers.layerCount;
259 image_subresource_range.baseMipLevel = image_subresource_layers.mipLevel;
260 image_subresource_range.levelCount = 1;
261 SetImageLayout(device_data, cb_node, image_state, image_subresource_range, layout);
262}
263// Set image layout for all slices of an image view
264void SetImageViewLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImageView imageView, const VkImageLayout &layout) {
265 auto view_state = GetImageViewState(device_data, imageView);
266 assert(view_state);
267
268 SetImageLayout(device_data, cb_node, GetImageState(device_data, view_state->create_info.image),
269 view_state->create_info.subresourceRange, layout);
270}
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700271
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700272bool VerifyFramebufferAndRenderPassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700273 const VkRenderPassBeginInfo *pRenderPassBegin,
274 const FRAMEBUFFER_STATE *framebuffer_state) {
275 bool skip_call = false;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700276 auto const pRenderPassInfo = GetRenderPassState(device_data, pRenderPassBegin->renderPass)->createInfo.ptr();
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700277 auto const &framebufferInfo = framebuffer_state->createInfo;
278 const auto report_data = core_validation::GetReportData(device_data);
279 if (pRenderPassInfo->attachmentCount != framebufferInfo.attachmentCount) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600280 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
281 reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700282 "You cannot start a render pass using a framebuffer "
283 "with a different number of attachments.");
284 }
285 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
286 const VkImageView &image_view = framebufferInfo.pAttachments[i];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700287 auto view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700288 assert(view_state);
289 const VkImage &image = view_state->create_info.image;
290 const VkImageSubresourceRange &subRange = view_state->create_info.subresourceRange;
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700291 auto initial_layout = pRenderPassInfo->pAttachments[i].initialLayout;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700292 // TODO: Do not iterate over every possibility - consolidate where possible
293 for (uint32_t j = 0; j < subRange.levelCount; j++) {
294 uint32_t level = subRange.baseMipLevel + j;
295 for (uint32_t k = 0; k < subRange.layerCount; k++) {
296 uint32_t layer = subRange.baseArrayLayer + k;
297 VkImageSubresource sub = {subRange.aspectMask, level, layer};
298 IMAGE_CMD_BUF_LAYOUT_NODE node;
299 if (!FindCmdBufLayout(device_data, pCB, image, sub, node)) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700300 // Missing layouts will be added during state update
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700301 continue;
302 }
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700303 if (initial_layout != VK_IMAGE_LAYOUT_UNDEFINED && initial_layout != node.layout) {
Mark Lobodzinskieb9e73f2017-04-13 10:06:48 -0600304 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
305 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700306 "You cannot start a render pass using attachment %u "
307 "where the render pass initial layout is %s and the previous "
308 "known layout of the attachment is %s. The layouts must match, or "
309 "the render pass initial layout for the attachment must be "
310 "VK_IMAGE_LAYOUT_UNDEFINED",
311 i, string_VkImageLayout(initial_layout), string_VkImageLayout(node.layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700312 }
313 }
314 }
315 }
316 return skip_call;
317}
318
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700319void TransitionAttachmentRefLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, FRAMEBUFFER_STATE *pFramebuffer,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700320 VkAttachmentReference ref) {
321 if (ref.attachment != VK_ATTACHMENT_UNUSED) {
322 auto image_view = pFramebuffer->createInfo.pAttachments[ref.attachment];
323 SetImageViewLayout(device_data, pCB, image_view, ref.layout);
324 }
325}
326
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700327void TransitionSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const RENDER_PASS_STATE *render_pass_state,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700328 const int subpass_index, FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700329 assert(render_pass_state);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700330
331 if (framebuffer_state) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700332 auto const &subpass = render_pass_state->createInfo.pSubpasses[subpass_index];
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700333 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
334 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pInputAttachments[j]);
335 }
336 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
337 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pColorAttachments[j]);
338 }
339 if (subpass.pDepthStencilAttachment) {
340 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, *subpass.pDepthStencilAttachment);
341 }
342 }
343}
344
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700345bool ValidateImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
346 uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700347 if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
348 return false;
349 }
350 VkImageSubresource sub = {aspect, level, layer};
351 IMAGE_CMD_BUF_LAYOUT_NODE node;
352 if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700353 return false;
354 }
355 bool skip = false;
356 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
357 // TODO: Set memory invalid which is in mem_tracker currently
358 } else if (node.layout != mem_barrier->oldLayout) {
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600359 skip |=
360 log_msg(core_validation::GetReportData(device_data), VK_DEBUG_REPORT_ERROR_BIT_EXT,
361 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__,
362 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
363 "For image 0x%" PRIxLEAST64 " you cannot transition the layout of aspect %d from %s when current layout is %s.",
364 reinterpret_cast<const uint64_t &>(mem_barrier->image), aspect, string_VkImageLayout(mem_barrier->oldLayout),
365 string_VkImageLayout(node.layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700366 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700367 return skip;
368}
369
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700370// Transition the layout state for renderpass attachments based on the BeginRenderPass() call. This includes:
371// 1. Transition into initialLayout state
372// 2. Transition from initialLayout to layout used in subpass 0
373void TransitionBeginRenderPassLayouts(layer_data *device_data, GLOBAL_CB_NODE *cb_state, const RENDER_PASS_STATE *render_pass_state,
374 FRAMEBUFFER_STATE *framebuffer_state) {
375 // First transition into initialLayout
376 auto const rpci = render_pass_state->createInfo.ptr();
377 for (uint32_t i = 0; i < rpci->attachmentCount; ++i) {
378 VkImageView image_view = framebuffer_state->createInfo.pAttachments[i];
379 SetImageViewLayout(device_data, cb_state, image_view, rpci->pAttachments[i].initialLayout);
380 }
381 // Now transition for first subpass (index 0)
382 TransitionSubpassLayouts(device_data, cb_state, render_pass_state, 0, framebuffer_state);
383}
384
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700385void TransitionImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
386 uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
387 if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
388 return;
389 }
390 VkImageSubresource sub = {aspect, level, layer};
391 IMAGE_CMD_BUF_LAYOUT_NODE node;
392 if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
393 SetLayout(device_data, pCB, mem_barrier->image, sub,
394 IMAGE_CMD_BUF_LAYOUT_NODE(mem_barrier->oldLayout, mem_barrier->newLayout));
395 return;
396 }
397 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
398 // TODO: Set memory invalid
399 }
400 SetLayout(device_data, pCB, mem_barrier->image, sub, mem_barrier->newLayout);
401}
402
Dave Houlton10b39482017-03-16 13:18:15 -0600403bool VerifyAspectsPresent(VkImageAspectFlags aspect_mask, VkFormat format) {
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600404 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600405 if (!FormatIsColor(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600406 }
407 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600408 if (!FormatHasDepth(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600409 }
410 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600411 if (!FormatHasStencil(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600412 }
413 return true;
414}
415
Mike Weiblen62d08a32017-03-07 22:18:27 -0700416// Verify an ImageMemoryBarrier's old/new ImageLayouts are compatible with the Image's ImageUsageFlags.
417bool ValidateBarrierLayoutToImageUsage(layer_data *device_data, const VkImageMemoryBarrier *img_barrier, bool new_not_old,
418 VkImageUsageFlags usage_flags, const char *func_name) {
419 const auto report_data = core_validation::GetReportData(device_data);
420 bool skip = false;
421 const VkImageLayout layout = (new_not_old) ? img_barrier->newLayout : img_barrier->oldLayout;
422 UNIQUE_VALIDATION_ERROR_CODE msg_code = VALIDATION_ERROR_UNDEFINED; // sentinel value meaning "no error"
423
424 switch (layout) {
425 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
426 if ((usage_flags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) == 0) {
427 msg_code = VALIDATION_ERROR_00303;
428 }
429 break;
430 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
431 if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
432 msg_code = VALIDATION_ERROR_00304;
433 }
434 break;
435 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
436 if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
437 msg_code = VALIDATION_ERROR_00305;
438 }
439 break;
440 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
441 if ((usage_flags & (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) == 0) {
442 msg_code = VALIDATION_ERROR_00306;
443 }
444 break;
445 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
446 if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) == 0) {
447 msg_code = VALIDATION_ERROR_00307;
448 }
449 break;
450 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
451 if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) == 0) {
452 msg_code = VALIDATION_ERROR_00308;
453 }
454 break;
455 default:
456 // Other VkImageLayout values do not have VUs defined in this context.
457 break;
458 }
459
460 if (msg_code != VALIDATION_ERROR_UNDEFINED) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600461 skip |=
462 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
463 reinterpret_cast<const uint64_t &>(img_barrier->image), __LINE__, msg_code, "DS",
464 "%s: Image barrier 0x%p %sLayout=%s is not compatible with image 0x%" PRIx64 " usage flags 0x%" PRIx32 ". %s",
465 func_name, img_barrier, ((new_not_old) ? "new" : "old"), string_VkImageLayout(layout),
466 reinterpret_cast<const uint64_t &>(img_barrier->image), usage_flags, validation_error_map[msg_code]);
Mike Weiblen62d08a32017-03-07 22:18:27 -0700467 }
468 return skip;
469}
470
471// Verify image barriers are compatible with the images they reference.
472bool ValidateBarriersToImages(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t imageMemoryBarrierCount,
473 const VkImageMemoryBarrier *pImageMemoryBarriers, const char *func_name) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700474 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700475 bool skip = false;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700476
Mike Weiblen62d08a32017-03-07 22:18:27 -0700477 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
478 auto img_barrier = &pImageMemoryBarriers[i];
479 if (!img_barrier) continue;
480
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600481 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, img_barrier->image)->createInfo);
482 uint32_t level_count = ResolveRemainingLevels(&img_barrier->subresourceRange, image_create_info->mipLevels);
483 uint32_t layer_count = ResolveRemainingLayers(&img_barrier->subresourceRange, image_create_info->arrayLayers);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700484
Mike Weiblen62d08a32017-03-07 22:18:27 -0700485 for (uint32_t j = 0; j < level_count; j++) {
486 uint32_t level = img_barrier->subresourceRange.baseMipLevel + j;
487 for (uint32_t k = 0; k < layer_count; k++) {
488 uint32_t layer = img_barrier->subresourceRange.baseArrayLayer + k;
489 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
490 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
491 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
492 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700493 }
494 }
Mike Weiblen62d08a32017-03-07 22:18:27 -0700495
496 IMAGE_STATE *image_state = GetImageState(device_data, img_barrier->image);
497 if (image_state) {
498 VkImageUsageFlags usage_flags = image_state->createInfo.usage;
499 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, false, usage_flags, func_name);
500 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, true, usage_flags, func_name);
501 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700502 }
503 return skip;
504}
505
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700506void TransitionImageLayouts(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
507 const VkImageMemoryBarrier *pImgMemBarriers) {
508 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700509
510 for (uint32_t i = 0; i < memBarrierCount; ++i) {
511 auto mem_barrier = &pImgMemBarriers[i];
512 if (!mem_barrier) continue;
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700513
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600514 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, mem_barrier->image)->createInfo);
515 uint32_t level_count = ResolveRemainingLevels(&mem_barrier->subresourceRange, image_create_info->mipLevels);
516 uint32_t layer_count = ResolveRemainingLayers(&mem_barrier->subresourceRange, image_create_info->arrayLayers);
517
518 for (uint32_t j = 0; j < level_count; j++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700519 uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600520 for (uint32_t k = 0; k < layer_count; k++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700521 uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
522 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
523 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
524 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
525 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
526 }
527 }
528 }
529}
530
Tobin Ehlisc8266452017-04-07 12:20:30 -0600531bool VerifyImageLayout(layer_data const *device_data, GLOBAL_CB_NODE const *cb_node, IMAGE_STATE *image_state,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600532 VkImageSubresourceLayers subLayers, VkImageLayout explicit_layout, VkImageLayout optimal_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600533 const char *caller, UNIQUE_VALIDATION_ERROR_CODE msg_code, bool *error) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700534 const auto report_data = core_validation::GetReportData(device_data);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600535 const auto image = image_state->image;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700536 bool skip_call = false;
537
538 for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
539 uint32_t layer = i + subLayers.baseArrayLayer;
540 VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
541 IMAGE_CMD_BUF_LAYOUT_NODE node;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600542 if (FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
543 if (node.layout != explicit_layout) {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600544 *error = true;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600545 // TODO: Improve log message in the next pass
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600546 skip_call |=
547 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
548 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600549 "%s: Cannot use image 0x%" PRIxLEAST64
550 " with specific layout %s that doesn't match the actual current layout %s.",
551 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
552 string_VkImageLayout(node.layout));
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600553 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700554 }
555 }
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600556 // If optimal_layout is not UNDEFINED, check that layout matches optimal for this case
557 if ((VK_IMAGE_LAYOUT_UNDEFINED != optimal_layout) && (explicit_layout != optimal_layout)) {
558 if (VK_IMAGE_LAYOUT_GENERAL == explicit_layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700559 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
560 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600561 skip_call |= log_msg(
562 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
563 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
564 "%s: For optimal performance image 0x%" PRIxLEAST64 " layout should be %s instead of GENERAL.", caller,
565 reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(optimal_layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700566 }
567 } else {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600568 *error = true;
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600569 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
570 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, msg_code, "DS",
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600571 "%s: Layout for image 0x%" PRIxLEAST64 " is %s but can only be %s or VK_IMAGE_LAYOUT_GENERAL. %s",
572 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
573 string_VkImageLayout(optimal_layout), validation_error_map[msg_code]);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700574 }
575 }
576 return skip_call;
577}
578
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700579void TransitionFinalSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkRenderPassBeginInfo *pRenderPassBegin,
580 FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700581 auto renderPass = GetRenderPassState(device_data, pRenderPassBegin->renderPass);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700582 if (!renderPass) return;
583
584 const VkRenderPassCreateInfo *pRenderPassInfo = renderPass->createInfo.ptr();
585 if (framebuffer_state) {
586 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
587 auto image_view = framebuffer_state->createInfo.pAttachments[i];
588 SetImageViewLayout(device_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
589 }
590 }
591}
592
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700593bool PreCallValidateCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700594 const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
595 bool skip_call = false;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700596 const debug_report_data *report_data = core_validation::GetReportData(device_data);
597
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600598 if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700599 skip_call |=
600 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
601 VALIDATION_ERROR_00715, "IMAGE", "vkCreateImage: VkFormat for image must not be VK_FORMAT_UNDEFINED. %s",
602 validation_error_map[VALIDATION_ERROR_00715]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600603
604 return skip_call;
605 }
606
607 const VkFormatProperties *properties = GetFormatProperties(device_data, pCreateInfo->format);
608
609 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) && (properties->linearTilingFeatures == 0)) {
610 std::stringstream ss;
611 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
612 skip_call |=
613 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
614 VALIDATION_ERROR_02150, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02150]);
615
616 return skip_call;
617 }
618
619 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) && (properties->optimalTilingFeatures == 0)) {
620 std::stringstream ss;
621 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
622 skip_call |=
623 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
624 VALIDATION_ERROR_02155, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02155]);
625
626 return skip_call;
627 }
628
629 // Validate that format supports usage as color attachment
630 if (pCreateInfo->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
631 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
632 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
633 std::stringstream ss;
634 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
635 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
636 skip_call |=
637 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
638 VALIDATION_ERROR_02158, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02158]);
639 }
640 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
641 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
642 std::stringstream ss;
643 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
644 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
645 skip_call |=
646 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
647 VALIDATION_ERROR_02153, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02153]);
648 }
649 }
650
651 // Validate that format supports usage as depth/stencil attachment
652 if (pCreateInfo->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
653 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
654 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
655 std::stringstream ss;
656 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
657 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
658 skip_call |=
659 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
660 VALIDATION_ERROR_02159, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02159]);
661 }
662 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
663 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
664 std::stringstream ss;
665 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
666 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
667 skip_call |=
668 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
669 VALIDATION_ERROR_02154, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02154]);
670 }
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700671 }
672
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700673 const VkImageFormatProperties *ImageFormatProperties = GetImageFormatProperties(
674 device_data, pCreateInfo->format, pCreateInfo->imageType, pCreateInfo->tiling, pCreateInfo->usage, pCreateInfo->flags);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700675
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700676 VkDeviceSize imageGranularity = GetPhysicalDeviceProperties(device_data)->limits.bufferImageGranularity;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700677 imageGranularity = imageGranularity == 1 ? 0 : imageGranularity;
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600678 // TODO : This is also covering 2918 & 2919. Break out into separate checks
Mark Lobodzinski688ed322017-01-27 11:13:21 -0700679 if ((pCreateInfo->extent.width <= 0) || (pCreateInfo->extent.height <= 0) || (pCreateInfo->extent.depth <= 0)) {
680 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600681 VALIDATION_ERROR_02917, "Image",
Mark Lobodzinski688ed322017-01-27 11:13:21 -0700682 "CreateImage extent is 0 for at least one required dimension for image: "
683 "Width = %d Height = %d Depth = %d. %s",
684 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600685 validation_error_map[VALIDATION_ERROR_02917]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700686 }
687
688 // TODO: VALIDATION_ERROR_02125 VALIDATION_ERROR_02126 VALIDATION_ERROR_02128 VALIDATION_ERROR_00720
689 // All these extent-related VUs should be checked here
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700690 if ((pCreateInfo->extent.depth > ImageFormatProperties->maxExtent.depth) ||
691 (pCreateInfo->extent.width > ImageFormatProperties->maxExtent.width) ||
692 (pCreateInfo->extent.height > ImageFormatProperties->maxExtent.height)) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700693 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
694 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
695 "CreateImage extents exceed allowable limits for format: "
696 "Width = %d Height = %d Depth = %d: Limits for Width = %d Height = %d Depth = %d for format %s.",
697 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700698 ImageFormatProperties->maxExtent.width, ImageFormatProperties->maxExtent.height,
699 ImageFormatProperties->maxExtent.depth, string_VkFormat(pCreateInfo->format));
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700700 }
701
702 uint64_t totalSize = ((uint64_t)pCreateInfo->extent.width * (uint64_t)pCreateInfo->extent.height *
703 (uint64_t)pCreateInfo->extent.depth * (uint64_t)pCreateInfo->arrayLayers *
Dave Houlton1d2022c2017-03-29 11:43:58 -0600704 (uint64_t)pCreateInfo->samples * (uint64_t)FormatSize(pCreateInfo->format) +
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700705 (uint64_t)imageGranularity) &
706 ~(uint64_t)imageGranularity;
707
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700708 if (totalSize > ImageFormatProperties->maxResourceSize) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700709 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
710 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
711 "CreateImage resource size exceeds allowable maximum "
712 "Image resource size = 0x%" PRIxLEAST64 ", maximum resource size = 0x%" PRIxLEAST64 " ",
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700713 totalSize, ImageFormatProperties->maxResourceSize);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700714 }
715
716 // TODO: VALIDATION_ERROR_02132
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700717 if (pCreateInfo->mipLevels > ImageFormatProperties->maxMipLevels) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700718 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
719 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
720 "CreateImage mipLevels=%d exceeds allowable maximum supported by format of %d", pCreateInfo->mipLevels,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700721 ImageFormatProperties->maxMipLevels);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700722 }
723
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700724 if (pCreateInfo->arrayLayers > ImageFormatProperties->maxArrayLayers) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700725 skip_call |= log_msg(
726 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__, VALIDATION_ERROR_02133,
727 "Image", "CreateImage arrayLayers=%d exceeds allowable maximum supported by format of %d. %s", pCreateInfo->arrayLayers,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700728 ImageFormatProperties->maxArrayLayers, validation_error_map[VALIDATION_ERROR_02133]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700729 }
730
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700731 if ((pCreateInfo->samples & ImageFormatProperties->sampleCounts) == 0) {
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700732 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
733 VALIDATION_ERROR_02138, "Image", "CreateImage samples %s is not supported by format 0x%.8X. %s",
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700734 string_VkSampleCountFlagBits(pCreateInfo->samples), ImageFormatProperties->sampleCounts,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700735 validation_error_map[VALIDATION_ERROR_02138]);
736 }
737
738 if (pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_UNDEFINED && pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_PREINITIALIZED) {
739 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
740 VALIDATION_ERROR_00731, "Image",
741 "vkCreateImage parameter, pCreateInfo->initialLayout, must be VK_IMAGE_LAYOUT_UNDEFINED or "
742 "VK_IMAGE_LAYOUT_PREINITIALIZED. %s",
743 validation_error_map[VALIDATION_ERROR_00731]);
744 }
745
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600746 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
747 skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
748 VALIDATION_ERROR_02143, "DS",
749 "vkCreateImage(): the sparseBinding device feature is disabled: Images cannot be created with the "
750 "VK_IMAGE_CREATE_SPARSE_BINDING_BIT set. %s",
751 validation_error_map[VALIDATION_ERROR_02143]);
752 }
753
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600754 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
755 skip_call |=
756 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
757 DRAWSTATE_INVALID_FEATURE, "DS",
758 "vkCreateImage(): the sparseResidencyAliased device feature is disabled: Images cannot be created with the "
759 "VK_IMAGE_CREATE_SPARSE_ALIASED_BIT set.");
760 }
761
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700762 return skip_call;
763}
764
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700765void PostCallRecordCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo, VkImage *pImage) {
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700766 IMAGE_LAYOUT_NODE image_state;
767 image_state.layout = pCreateInfo->initialLayout;
768 image_state.format = pCreateInfo->format;
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700769 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 -0700770 ImageSubresourcePair subpair{*pImage, false, VkImageSubresource()};
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700771 (*core_validation::GetImageSubresourceMap(device_data))[*pImage].push_back(subpair);
772 (*core_validation::GetImageLayoutMap(device_data))[subpair] = image_state;
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700773}
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700774
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700775bool PreCallValidateDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE **image_state, VK_OBJECT *obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700776 const CHECK_DISABLED *disabled = core_validation::GetDisables(device_data);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700777 *image_state = core_validation::GetImageState(device_data, image);
Mark Lobodzinski33826372017-04-13 11:10:11 -0600778 *obj_struct = {reinterpret_cast<uint64_t &>(image), kVulkanObjectTypeImage };
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700779 if (disabled->destroy_image) return false;
780 bool skip = false;
781 if (*image_state) {
782 skip |= core_validation::ValidateObjectNotInUse(device_data, *image_state, *obj_struct, VALIDATION_ERROR_00743);
783 }
784 return skip;
785}
786
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700787void PostCallRecordDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE *image_state, VK_OBJECT obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700788 core_validation::invalidateCommandBuffers(device_data, image_state->cb_bindings, obj_struct);
789 // Clean up memory mapping, bindings and range references for image
790 for (auto mem_binding : image_state->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700791 auto mem_info = core_validation::GetMemObjInfo(device_data, mem_binding);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700792 if (mem_info) {
793 core_validation::RemoveImageMemoryRange(obj_struct.handle, mem_info);
794 }
795 }
Mark Lobodzinski33826372017-04-13 11:10:11 -0600796 core_validation::ClearMemoryObjectBindings(device_data, obj_struct.handle, kVulkanObjectTypeImage);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700797 // Remove image from imageMap
798 core_validation::GetImageMap(device_data)->erase(image);
799 std::unordered_map<VkImage, std::vector<ImageSubresourcePair>> *imageSubresourceMap =
800 core_validation::GetImageSubresourceMap(device_data);
801
802 const auto &sub_entry = imageSubresourceMap->find(image);
803 if (sub_entry != imageSubresourceMap->end()) {
804 for (const auto &pair : sub_entry->second) {
805 core_validation::GetImageLayoutMap(device_data)->erase(pair);
806 }
807 imageSubresourceMap->erase(sub_entry);
808 }
809}
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700810
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700811bool ValidateImageAttributes(layer_data *device_data, IMAGE_STATE *image_state, VkImageSubresourceRange range) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700812 bool skip = false;
813 const debug_report_data *report_data = core_validation::GetReportData(device_data);
814
815 if (range.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) {
816 char const str[] = "vkCmdClearColorImage aspectMasks for all subresource ranges must be set to VK_IMAGE_ASPECT_COLOR_BIT";
817 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
818 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
819 }
820
Dave Houlton1d2022c2017-03-29 11:43:58 -0600821 if (FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700822 char const str[] = "vkCmdClearColorImage called with depth/stencil image.";
823 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
824 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
825 validation_error_map[VALIDATION_ERROR_01088]);
Dave Houlton1d2022c2017-03-29 11:43:58 -0600826 } else if (FormatIsCompressed(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700827 char const str[] = "vkCmdClearColorImage called with compressed image.";
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_01088, "IMAGE", "%s. %s", str,
830 validation_error_map[VALIDATION_ERROR_01088]);
831 }
832
833 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
834 char const str[] = "vkCmdClearColorImage called with image created without VK_IMAGE_USAGE_TRANSFER_DST_BIT.";
835 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
836 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01084, "IMAGE", "%s. %s", str,
837 validation_error_map[VALIDATION_ERROR_01084]);
838 }
839 return skip;
840}
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700841
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600842uint32_t ResolveRemainingLevels(const VkImageSubresourceRange *range, uint32_t mip_levels) {
843 // Return correct number of mip levels taking into account VK_REMAINING_MIP_LEVELS
844 uint32_t mip_level_count = range->levelCount;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700845 if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600846 mip_level_count = mip_levels - range->baseMipLevel;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700847 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600848 return mip_level_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700849}
850
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600851uint32_t ResolveRemainingLayers(const VkImageSubresourceRange *range, uint32_t layers) {
852 // Return correct number of layers taking into account VK_REMAINING_ARRAY_LAYERS
853 uint32_t array_layer_count = range->layerCount;
854 if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
855 array_layer_count = layers - range->baseArrayLayer;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700856 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600857 return array_layer_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700858}
859
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700860bool VerifyClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700861 VkImageSubresourceRange range, VkImageLayout dest_image_layout, const char *func_name) {
862 bool skip = false;
863 const debug_report_data *report_data = core_validation::GetReportData(device_data);
864
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600865 uint32_t level_count = ResolveRemainingLevels(&range, image_state->createInfo.mipLevels);
866 uint32_t layer_count = ResolveRemainingLayers(&range, image_state->createInfo.arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700867
868 if (dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
869 if (dest_image_layout == VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700870 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
871 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600872 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
873 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700874 "%s: Layout for cleared image should be TRANSFER_DST_OPTIMAL instead of GENERAL.", func_name);
875 }
876 } else {
877 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01086;
878 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
879 error_code = VALIDATION_ERROR_01101;
880 } else {
881 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
882 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600883 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
884 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, error_code, "DS",
885 "%s: Layout for cleared image is %s but can only be "
886 "TRANSFER_DST_OPTIMAL or GENERAL. %s",
887 func_name, string_VkImageLayout(dest_image_layout), validation_error_map[error_code]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700888 }
889 }
890
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600891 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
892 uint32_t level = level_index + range.baseMipLevel;
893 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
894 uint32_t layer = layer_index + range.baseArrayLayer;
895 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700896 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700897 if (FindCmdBufLayout(device_data, cb_node, image_state->image, sub, node)) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700898 if (node.layout != dest_image_layout) {
899 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01085;
900 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
901 error_code = VALIDATION_ERROR_01100;
902 } else {
903 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
904 }
905 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
906 __LINE__, error_code, "DS",
907 "%s: Cannot clear an image whose layout is %s and "
908 "doesn't match the current layout %s. %s",
909 func_name, string_VkImageLayout(dest_image_layout), string_VkImageLayout(node.layout),
910 validation_error_map[error_code]);
911 }
912 }
913 }
914 }
915
916 return skip;
917}
918
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700919void RecordClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage image, VkImageSubresourceRange range,
920 VkImageLayout dest_image_layout) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600921 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, image)->createInfo);
922 uint32_t level_count = ResolveRemainingLevels(&range, image_create_info->mipLevels);
923 uint32_t layer_count = ResolveRemainingLayers(&range, image_create_info->arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700924
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600925 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
926 uint32_t level = level_index + range.baseMipLevel;
927 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
928 uint32_t layer = layer_index + range.baseArrayLayer;
929 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700930 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700931 if (!FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
932 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 -0700933 }
934 }
935 }
936}
937
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700938bool PreCallValidateCmdClearColorImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700939 VkImageLayout imageLayout, uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
940 bool skip = false;
941 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700942 auto cb_node = GetCBNode(dev_data, commandBuffer);
943 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700944 if (cb_node && image_state) {
945 skip |= ValidateMemoryIsBoundToImage(dev_data, image_state, "vkCmdClearColorImage()", VALIDATION_ERROR_02527);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700946 skip |= ValidateCmdQueueFlags(dev_data, cb_node, "vkCmdClearColorImage()", VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT,
947 VALIDATION_ERROR_01095);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700948 skip |= ValidateCmd(dev_data, cb_node, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
949 skip |= insideRenderPass(dev_data, cb_node, "vkCmdClearColorImage()", VALIDATION_ERROR_01096);
950 for (uint32_t i = 0; i < rangeCount; ++i) {
951 skip |= ValidateImageAttributes(dev_data, image_state, pRanges[i]);
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700952 skip |= VerifyClearImageLayout(dev_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearColorImage()");
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700953 }
954 }
955 return skip;
956}
957
958// This state recording routine is shared between ClearColorImage and ClearDepthStencilImage
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700959void PreCallRecordCmdClearImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
960 uint32_t rangeCount, const VkImageSubresourceRange *pRanges, CMD_TYPE cmd_type) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700961 auto cb_node = GetCBNode(dev_data, commandBuffer);
962 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700963 if (cb_node && image_state) {
964 AddCommandBufferBindingImage(dev_data, cb_node, image_state);
965 std::function<bool()> function = [=]() {
966 SetImageMemoryValid(dev_data, image_state, true);
967 return false;
968 };
969 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700970 core_validation::UpdateCmdBufferLastCmd(cb_node, cmd_type);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700971 for (uint32_t i = 0; i < rangeCount; ++i) {
972 RecordClearImageLayout(dev_data, cb_node, image, pRanges[i], imageLayout);
973 }
974 }
975}
976
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700977bool PreCallValidateCmdClearDepthStencilImage(layer_data *device_data, VkCommandBuffer commandBuffer, VkImage image,
978 VkImageLayout imageLayout, uint32_t rangeCount,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700979 const VkImageSubresourceRange *pRanges) {
980 bool skip = false;
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700981 const debug_report_data *report_data = core_validation::GetReportData(device_data);
982
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700983 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700984 auto cb_node = GetCBNode(device_data, commandBuffer);
985 auto image_state = GetImageState(device_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700986 if (cb_node && image_state) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700987 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_02528);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700988 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearDepthStencilImage()", VK_QUEUE_GRAPHICS_BIT,
989 VALIDATION_ERROR_01110);
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700990 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
991 skip |= insideRenderPass(device_data, cb_node, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_01111);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700992 for (uint32_t i = 0; i < rangeCount; ++i) {
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700993 skip |=
994 VerifyClearImageLayout(device_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearDepthStencilImage()");
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700995 // Image aspect must be depth or stencil or both
996 if (((pRanges[i].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
997 ((pRanges[i].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
998 char const str[] =
999 "vkCmdClearDepthStencilImage aspectMasks for all subresource ranges must be "
1000 "set to VK_IMAGE_ASPECT_DEPTH_BIT and/or VK_IMAGE_ASPECT_STENCIL_BIT";
1001 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1002 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
1003 }
1004 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001005 if (image_state && !FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -07001006 char const str[] = "vkCmdClearDepthStencilImage called without a depth/stencil image.";
1007 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
1008 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_01103, "IMAGE", "%s. %s", str,
1009 validation_error_map[VALIDATION_ERROR_01103]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -07001010 }
1011 }
1012 return skip;
1013}
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001014
1015// Returns true if [x, xoffset] and [y, yoffset] overlap
1016static bool RangesIntersect(int32_t start, uint32_t start_offset, int32_t end, uint32_t end_offset) {
1017 bool result = false;
1018 uint32_t intersection_min = std::max(static_cast<uint32_t>(start), static_cast<uint32_t>(end));
1019 uint32_t intersection_max = std::min(static_cast<uint32_t>(start) + start_offset, static_cast<uint32_t>(end) + end_offset);
1020
1021 if (intersection_max > intersection_min) {
1022 result = true;
1023 }
1024 return result;
1025}
1026
1027// Returns true if two VkImageCopy structures overlap
1028static bool RegionIntersects(const VkImageCopy *src, const VkImageCopy *dst, VkImageType type) {
1029 bool result = false;
1030 if ((src->srcSubresource.mipLevel == dst->dstSubresource.mipLevel) &&
1031 (RangesIntersect(src->srcSubresource.baseArrayLayer, src->srcSubresource.layerCount, dst->dstSubresource.baseArrayLayer,
1032 dst->dstSubresource.layerCount))) {
1033 result = true;
1034 switch (type) {
1035 case VK_IMAGE_TYPE_3D:
1036 result &= RangesIntersect(src->srcOffset.z, src->extent.depth, dst->dstOffset.z, dst->extent.depth);
1037 // Intentionally fall through to 2D case
1038 case VK_IMAGE_TYPE_2D:
1039 result &= RangesIntersect(src->srcOffset.y, src->extent.height, dst->dstOffset.y, dst->extent.height);
1040 // Intentionally fall through to 1D case
1041 case VK_IMAGE_TYPE_1D:
1042 result &= RangesIntersect(src->srcOffset.x, src->extent.width, dst->dstOffset.x, dst->extent.width);
1043 break;
1044 default:
1045 // Unrecognized or new IMAGE_TYPE enums will be caught in parameter_validation
1046 assert(false);
1047 }
1048 }
1049 return result;
1050}
1051
1052// Returns true if offset and extent exceed image extents
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001053static bool ExceedsBounds(const VkOffset3D *offset, const VkExtent3D *extent, const VkExtent3D *image_extent) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001054 bool result = false;
1055 // Extents/depths cannot be negative but checks left in for clarity
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001056 if ((offset->z + extent->depth > image_extent->depth) || (offset->z < 0) ||
1057 ((offset->z + static_cast<int32_t>(extent->depth)) < 0)) {
1058 result = true;
1059 }
1060 if ((offset->y + extent->height > image_extent->height) || (offset->y < 0) ||
1061 ((offset->y + static_cast<int32_t>(extent->height)) < 0)) {
1062 result = true;
1063 }
1064 if ((offset->x + extent->width > image_extent->width) || (offset->x < 0) ||
1065 ((offset->x + static_cast<int32_t>(extent->width)) < 0)) {
1066 result = true;
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001067 }
1068 return result;
1069}
1070
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001071// Test if two VkExtent3D structs are equivalent
1072static inline bool IsExtentEqual(const VkExtent3D *extent, const VkExtent3D *other_extent) {
1073 bool result = true;
1074 if ((extent->width != other_extent->width) || (extent->height != other_extent->height) ||
1075 (extent->depth != other_extent->depth)) {
1076 result = false;
1077 }
1078 return result;
1079}
1080
1081// Returns the image extent of a specific subresource.
1082static inline VkExtent3D GetImageSubresourceExtent(const IMAGE_STATE *img, const VkImageSubresourceLayers *subresource) {
1083 const uint32_t mip = subresource->mipLevel;
1084 VkExtent3D extent = img->createInfo.extent;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001085 // Don't allow mip adjustment to create 0 dim, but pass along a 0 if that's what subresource specified
1086 extent.width = (0 == extent.width ? 0 : std::max(1U, extent.width >> mip));
1087 extent.height = (0 == extent.height ? 0 : std::max(1U, extent.height >> mip));
1088 extent.depth = (0 == extent.depth ? 0 : std::max(1U, extent.depth >> mip));
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001089 return extent;
1090}
1091
1092// Test if the extent argument has all dimensions set to 0.
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001093static inline bool IsExtentAllZeroes(const VkExtent3D *extent) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001094 return ((extent->width == 0) && (extent->height == 0) && (extent->depth == 0));
1095}
1096
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001097// Test if the extent argument has any dimensions set to 0.
1098static inline bool IsExtentSizeZero(const VkExtent3D *extent) {
1099 return ((extent->width == 0) || (extent->height == 0) || (extent->depth == 0));
1100}
1101
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001102// Returns the image transfer granularity for a specific image scaled by compressed block size if necessary.
1103static inline VkExtent3D GetScaledItg(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *img) {
1104 // Default to (0, 0, 0) granularity in case we can't find the real granularity for the physical device.
1105 VkExtent3D granularity = {0, 0, 0};
1106 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
1107 if (pPool) {
1108 granularity =
1109 GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].minImageTransferGranularity;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001110 if (FormatIsCompressed(img->createInfo.format)) {
1111 auto block_size = FormatCompressedTexelBlockExtent(img->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001112 granularity.width *= block_size.width;
1113 granularity.height *= block_size.height;
1114 }
1115 }
1116 return granularity;
1117}
1118
1119// Test elements of a VkExtent3D structure against alignment constraints contained in another VkExtent3D structure
1120static inline bool IsExtentAligned(const VkExtent3D *extent, const VkExtent3D *granularity) {
1121 bool valid = true;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001122 if ((SafeModulo(extent->depth, granularity->depth) != 0) || (SafeModulo(extent->width, granularity->width) != 0) ||
1123 (SafeModulo(extent->height, granularity->height) != 0)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001124 valid = false;
1125 }
1126 return valid;
1127}
1128
1129// Check elements of a VkOffset3D structure against a queue family's Image Transfer Granularity values
1130static inline bool CheckItgOffset(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkOffset3D *offset,
1131 const VkExtent3D *granularity, const uint32_t i, const char *function, const char *member) {
1132 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1133 bool skip = false;
1134 VkExtent3D offset_extent = {};
1135 offset_extent.width = static_cast<uint32_t>(abs(offset->x));
1136 offset_extent.height = static_cast<uint32_t>(abs(offset->y));
1137 offset_extent.depth = static_cast<uint32_t>(abs(offset->z));
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001138 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001139 // 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 -07001140 if (IsExtentAllZeroes(&offset_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001141 skip |=
1142 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1143 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1144 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) must be (x=0, y=0, z=0) "
1145 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1146 function, i, member, offset->x, offset->y, offset->z);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001147 }
1148 } else {
1149 // If the queue family image transfer granularity is not (0, 0, 0), then the offset dimensions must always be even
1150 // integer multiples of the image transfer granularity.
1151 if (IsExtentAligned(&offset_extent, granularity) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001152 skip |= log_msg(
1153 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1154 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1155 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) dimensions must be even integer "
1156 "multiples of this command buffer's queue family image transfer granularity (w=%d, h=%d, d=%d).",
1157 function, i, member, offset->x, offset->y, offset->z, granularity->width, granularity->height, granularity->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001158 }
1159 }
1160 return skip;
1161}
1162
1163// Check elements of a VkExtent3D structure against a queue family's Image Transfer Granularity values
1164static inline bool CheckItgExtent(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkExtent3D *extent,
1165 const VkOffset3D *offset, const VkExtent3D *granularity, const VkExtent3D *subresource_extent,
1166 const uint32_t i, const char *function, const char *member) {
1167 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1168 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001169 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001170 // If the queue family image transfer granularity is (0, 0, 0), then the extent must always match the image
1171 // subresource extent.
1172 if (IsExtentEqual(extent, subresource_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001173 skip |=
1174 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1175 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1176 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d) "
1177 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1178 function, i, member, extent->width, extent->height, extent->depth, subresource_extent->width,
1179 subresource_extent->height, subresource_extent->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001180 }
1181 } else {
1182 // If the queue family image transfer granularity is not (0, 0, 0), then the extent dimensions must always be even
1183 // integer multiples of the image transfer granularity or the offset + extent dimensions must always match the image
1184 // subresource extent dimensions.
1185 VkExtent3D offset_extent_sum = {};
1186 offset_extent_sum.width = static_cast<uint32_t>(abs(offset->x)) + extent->width;
1187 offset_extent_sum.height = static_cast<uint32_t>(abs(offset->y)) + extent->height;
1188 offset_extent_sum.depth = static_cast<uint32_t>(abs(offset->z)) + extent->depth;
1189 if ((IsExtentAligned(extent, granularity) == false) && (IsExtentEqual(&offset_extent_sum, subresource_extent) == false)) {
1190 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001191 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1192 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001193 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) dimensions must be even integer multiples of this command buffer's "
1194 "queue family image transfer granularity (w=%d, h=%d, d=%d) or offset (x=%d, y=%d, z=%d) + "
1195 "extent (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d).",
1196 function, i, member, extent->width, extent->height, extent->depth, granularity->width, granularity->height,
1197 granularity->depth, offset->x, offset->y, offset->z, extent->width, extent->height, extent->depth,
1198 subresource_extent->width, subresource_extent->height, subresource_extent->depth);
1199 }
1200 }
1201 return skip;
1202}
1203
1204// Check a uint32_t width or stride value against a queue family's Image Transfer Granularity width value
1205static inline bool CheckItgInt(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const uint32_t value,
1206 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1207 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1208
1209 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001210 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001211 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1212 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001213 "%s: pRegion[%d].%s (%d) must be an even integer multiple of this command buffer's queue family image "
1214 "transfer granularity width (%d).",
1215 function, i, member, value, granularity);
1216 }
1217 return skip;
1218}
1219
1220// Check a VkDeviceSize value against a queue family's Image Transfer Granularity width value
1221static inline bool CheckItgSize(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkDeviceSize value,
1222 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1223 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1224 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001225 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001226 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1227 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001228 "%s: pRegion[%d].%s (%" PRIdLEAST64
1229 ") must be an even integer multiple of this command buffer's queue family image transfer "
1230 "granularity width (%d).",
1231 function, i, member, value, granularity);
1232 }
1233 return skip;
1234}
1235
1236// Check valid usage Image Tranfer Granularity requirements for elements of a VkBufferImageCopy structure
1237bool ValidateCopyBufferImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1238 const IMAGE_STATE *img, const VkBufferImageCopy *region,
1239 const uint32_t i, const char *function) {
1240 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001241 if (FormatIsCompressed(img->createInfo.format) == true) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001242 // TODO: Add granularity checking for compressed formats
1243
1244 // bufferRowLength must be a multiple of the compressed texel block width
1245 // bufferImageHeight must be a multiple of the compressed texel block height
1246 // all members of imageOffset must be a multiple of the corresponding dimensions of the compressed texel block
1247 // bufferOffset must be a multiple of the compressed texel block size in bytes
1248 // imageExtent.width must be a multiple of the compressed texel block width or (imageExtent.width + imageOffset.x)
1249 // must equal the image subresource width
1250 // imageExtent.height must be a multiple of the compressed texel block height or (imageExtent.height + imageOffset.y)
1251 // must equal the image subresource height
1252 // imageExtent.depth must be a multiple of the compressed texel block depth or (imageExtent.depth + imageOffset.z)
1253 // must equal the image subresource depth
1254 } else {
1255 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1256 skip |= CheckItgSize(device_data, cb_node, region->bufferOffset, granularity.width, i, function, "bufferOffset");
1257 skip |= CheckItgInt(device_data, cb_node, region->bufferRowLength, granularity.width, i, function, "bufferRowLength");
1258 skip |= CheckItgInt(device_data, cb_node, region->bufferImageHeight, granularity.width, i, function, "bufferImageHeight");
1259 skip |= CheckItgOffset(device_data, cb_node, &region->imageOffset, &granularity, i, function, "imageOffset");
1260 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->imageSubresource);
1261 skip |= CheckItgExtent(device_data, cb_node, &region->imageExtent, &region->imageOffset, &granularity, &subresource_extent,
1262 i, function, "imageExtent");
1263 }
1264 return skip;
1265}
1266
1267// Check valid usage Image Tranfer Granularity requirements for elements of a VkImageCopy structure
1268bool ValidateCopyImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1269 const IMAGE_STATE *img, const VkImageCopy *region, const uint32_t i,
1270 const char *function) {
1271 bool skip = false;
1272 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1273 skip |= CheckItgOffset(device_data, cb_node, &region->srcOffset, &granularity, i, function, "srcOffset");
1274 skip |= CheckItgOffset(device_data, cb_node, &region->dstOffset, &granularity, i, function, "dstOffset");
1275 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->dstSubresource);
1276 skip |= CheckItgExtent(device_data, cb_node, &region->extent, &region->dstOffset, &granularity, &subresource_extent, i,
1277 function, "extent");
1278 return skip;
1279}
1280
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001281bool PreCallValidateCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001282 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1283 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001284 bool skip = false;
1285 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1286 VkCommandBuffer command_buffer = cb_node->commandBuffer;
1287
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001288 for (uint32_t i = 0; i < region_count; i++) {
1289 if (regions[i].srcSubresource.layerCount == 0) {
1290 std::stringstream ss;
1291 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] srcSubresource is zero";
1292 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1293 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1294 ss.str().c_str());
1295 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001296
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001297 if (regions[i].dstSubresource.layerCount == 0) {
1298 std::stringstream ss;
1299 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] dstSubresource is zero";
1300 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1301 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1302 ss.str().c_str());
1303 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001304
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001305 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
1306 // For each region the layerCount member of srcSubresource and dstSubresource must match
1307 if (regions[i].srcSubresource.layerCount != regions[i].dstSubresource.layerCount) {
1308 std::stringstream ss;
1309 ss << "vkCmdCopyImage: number of layers in source and destination subresources for pRegions[" << i
1310 << "] do not match";
1311 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1312 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01198, "IMAGE", "%s. %s",
1313 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01198]);
1314 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001315 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001316
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001317 // For each region, the aspectMask member of srcSubresource and dstSubresource must match
1318 if (regions[i].srcSubresource.aspectMask != regions[i].dstSubresource.aspectMask) {
1319 char const str[] = "vkCmdCopyImage: Src and dest aspectMasks for each region must match";
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_01197, "IMAGE", "%s. %s", str,
1322 validation_error_map[VALIDATION_ERROR_01197]);
1323 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001324
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06001325 // For each region, the aspectMask member of srcSubresource must be present in the source image
1326 if (!VerifyAspectsPresent(regions[i].srcSubresource.aspectMask, src_image_state->createInfo.format)) {
1327 std::stringstream ss;
1328 ss << "vkCmdCopyImage: pRegion[" << i
1329 << "] srcSubresource.aspectMask cannot specify aspects not present in source image";
1330 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1331 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01200, "IMAGE", "%s. %s",
1332 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01200]);
1333 }
1334
1335 // For each region, the aspectMask member of dstSubresource must be present in the destination image
1336 if (!VerifyAspectsPresent(regions[i].dstSubresource.aspectMask, dst_image_state->createInfo.format)) {
1337 std::stringstream ss;
1338 ss << "vkCmdCopyImage: pRegion[" << i << "] dstSubresource.aspectMask cannot specify aspects not present in dest image";
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_01201, "IMAGE", "%s. %s",
1341 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01201]);
1342 }
1343
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001344 // AspectMask must not contain VK_IMAGE_ASPECT_METADATA_BIT
1345 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) ||
1346 (regions[i].dstSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT)) {
1347 std::stringstream ss;
1348 ss << "vkCmdCopyImage: pRegions[" << i << "] may not specify aspectMask containing VK_IMAGE_ASPECT_METADATA_BIT";
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_01222, "IMAGE", "%s. %s",
1351 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01222]);
1352 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001353
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001354 // For each region, if aspectMask contains VK_IMAGE_ASPECT_COLOR_BIT, it must not contain either of
1355 // VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT
1356 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) &&
1357 (regions[i].srcSubresource.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
1358 char const str[] = "vkCmdCopyImage aspectMask cannot specify both COLOR and DEPTH/STENCIL aspects";
1359 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1360 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01221, "IMAGE", "%s. %s", str,
1361 validation_error_map[VALIDATION_ERROR_01221]);
1362 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001363
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001364 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
1365 // If either of the calling command's src_image or dst_image parameters are of VkImageType VK_IMAGE_TYPE_3D,
1366 // the baseArrayLayer and layerCount members of both srcSubresource and dstSubresource must be 0 and 1, respectively
1367 if (((src_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) ||
1368 (dst_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D)) &&
1369 ((regions[i].srcSubresource.baseArrayLayer != 0) || (regions[i].srcSubresource.layerCount != 1) ||
1370 (regions[i].dstSubresource.baseArrayLayer != 0) || (regions[i].dstSubresource.layerCount != 1))) {
1371 std::stringstream ss;
1372 ss << "vkCmdCopyImage: src or dstImage type was IMAGE_TYPE_3D, but in subRegion[" << i
1373 << "] baseArrayLayer was not zero or layerCount was not 1.";
1374 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1375 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01199, "IMAGE", "%s. %s",
1376 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01199]);
1377 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001378 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001379
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001380 // MipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
1381 if (regions[i].srcSubresource.mipLevel >= src_image_state->createInfo.mipLevels) {
1382 std::stringstream ss;
1383 ss << "vkCmdCopyImage: pRegions[" << i
1384 << "] specifies a src mipLevel greater than the number specified when the srcImage was created.";
1385 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1386 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1387 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1388 }
1389 if (regions[i].dstSubresource.mipLevel >= dst_image_state->createInfo.mipLevels) {
1390 std::stringstream ss;
1391 ss << "vkCmdCopyImage: pRegions[" << i
1392 << "] specifies a dst mipLevel greater than the number specified when the dstImage was created.";
1393 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1394 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1395 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1396 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001397
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001398 // (baseArrayLayer + layerCount) must be less than or equal to the arrayLayers specified in VkImageCreateInfo when the
1399 // image was created
1400 if ((regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount) >
1401 src_image_state->createInfo.arrayLayers) {
1402 std::stringstream ss;
1403 ss << "vkCmdCopyImage: srcImage arrayLayers was " << src_image_state->createInfo.arrayLayers << " but subRegion[" << i
1404 << "] baseArrayLayer + layerCount is "
1405 << (regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount);
1406 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1407 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1408 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1409 }
1410 if ((regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount) >
1411 dst_image_state->createInfo.arrayLayers) {
1412 std::stringstream ss;
1413 ss << "vkCmdCopyImage: dstImage arrayLayers was " << dst_image_state->createInfo.arrayLayers << " but subRegion[" << i
1414 << "] baseArrayLayer + layerCount is "
1415 << (regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount);
1416 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1417 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1418 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1419 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001420
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001421 // Check region extents for 1D-1D, 2D-2D, and 3D-3D copies
1422 if (src_image_state->createInfo.imageType == dst_image_state->createInfo.imageType) {
1423 // The source region specified by a given element of regions must be a region that is contained within srcImage
1424 if (ExceedsBounds(&regions[i].srcOffset, &regions[i].extent, &(src_image_state->createInfo.extent))) {
1425 std::stringstream ss;
1426 ss << "vkCmdCopyImage: srcSubResource in pRegions[" << i << "] exceeds extents srcImage was created with";
1427 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1428 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01175, "IMAGE", "%s. %s",
1429 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01175]);
1430 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001431
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001432 // The destination region specified by a given element of regions must be a region that is contained within dst_image
1433 if (ExceedsBounds(&regions[i].dstOffset, &regions[i].extent, &(dst_image_state->createInfo.extent))) {
1434 std::stringstream ss;
1435 ss << "vkCmdCopyImage: dstSubResource in pRegions[" << i << "] exceeds extents dstImage was created with";
1436 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1437 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01176, "IMAGE", "%s. %s",
1438 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01176]);
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 union of all source regions, and the union of all destination regions, specified by the elements of regions,
1443 // must not overlap in memory
1444 if (src_image_state->image == dst_image_state->image) {
1445 for (uint32_t j = 0; j < region_count; j++) {
1446 if (RegionIntersects(&regions[i], &regions[j], src_image_state->createInfo.imageType)) {
1447 std::stringstream ss;
1448 ss << "vkCmdCopyImage: pRegions[" << i << "] src overlaps with pRegions[" << j << "].";
1449 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1450 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01177, "IMAGE",
1451 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01177]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001452 }
1453 }
1454 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001455 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001456
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001457 // The formats of src_image and dst_image must be compatible. Formats are considered compatible if their texel size in bytes
1458 // is the same between both formats. For example, VK_FORMAT_R8G8B8A8_UNORM is compatible with VK_FORMAT_R32_UINT because
1459 // because both texels are 4 bytes in size. Depth/stencil formats must match exactly.
Dave Houlton1d2022c2017-03-29 11:43:58 -06001460 if (FormatIsDepthOrStencil(src_image_state->createInfo.format) ||
1461 FormatIsDepthOrStencil(dst_image_state->createInfo.format)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001462 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1463 char const str[] = "vkCmdCopyImage called with unmatched source and dest image depth/stencil formats.";
1464 skip |=
1465 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1466 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT, "IMAGE", str);
1467 }
1468 } else {
Dave Houlton1d2022c2017-03-29 11:43:58 -06001469 size_t srcSize = FormatSize(src_image_state->createInfo.format);
1470 size_t destSize = FormatSize(dst_image_state->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001471 if (srcSize != destSize) {
1472 char const str[] = "vkCmdCopyImage called with unmatched source and dest image format sizes.";
1473 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1474 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01184, "IMAGE", "%s. %s", str,
1475 validation_error_map[VALIDATION_ERROR_01184]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001476 }
1477 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001478
Dave Houlton33c22b72017-02-28 13:16:02 -07001479 // Source and dest image sample counts must match
1480 if (src_image_state->createInfo.samples != dst_image_state->createInfo.samples) {
1481 char const str[] = "vkCmdCopyImage() called on image pair with non-identical sample counts.";
1482 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 -06001483 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01185, "IMAGE", "%s %s", str,
1484 validation_error_map[VALIDATION_ERROR_01185]);
Dave Houlton33c22b72017-02-28 13:16:02 -07001485 }
1486
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001487 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02533);
1488 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02534);
1489 // Validate that SRC & DST images have correct usage flags set
1490 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01178,
1491 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
1492 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01181,
1493 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001494 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyImage()",
1495 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01193);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001496 skip |= ValidateCmd(device_data, cb_node, CMD_COPYIMAGE, "vkCmdCopyImage()");
1497 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImage()", VALIDATION_ERROR_01194);
Tobin Ehlisc8266452017-04-07 12:20:30 -06001498 bool hit_error = false;
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001499 for (uint32_t i = 0; i < region_count; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001500 skip |= VerifyImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -06001501 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01180, &hit_error);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001502 skip |= VerifyImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -06001503 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01183, &hit_error);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001504 skip |= ValidateCopyImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &regions[i], i,
1505 "vkCmdCopyImage()");
1506 }
1507
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001508 return skip;
1509}
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001510
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001511void PreCallRecordCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06001512 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1513 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
1514 // Make sure that all image slices are updated to correct layout
1515 for (uint32_t i = 0; i < region_count; ++i) {
1516 SetImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout);
1517 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout);
1518 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001519 // Update bindings between images and cmd buffer
1520 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1521 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Dave Houltoneba86e22017-03-02 14:56:23 -07001522 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImage()"); };
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001523 cb_node->validate_functions.push_back(function);
1524 function = [=]() {
1525 SetImageMemoryValid(device_data, dst_image_state, true);
1526 return false;
1527 };
1528 cb_node->validate_functions.push_back(function);
1529 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGE);
1530}
1531
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001532// Returns true if sub_rect is entirely contained within rect
1533static inline bool ContainsRect(VkRect2D rect, VkRect2D sub_rect) {
1534 if ((sub_rect.offset.x < rect.offset.x) || (sub_rect.offset.x + sub_rect.extent.width > rect.offset.x + rect.extent.width) ||
1535 (sub_rect.offset.y < rect.offset.y) || (sub_rect.offset.y + sub_rect.extent.height > rect.offset.y + rect.extent.height))
1536 return false;
1537 return true;
1538}
1539
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001540bool PreCallValidateCmdClearAttachments(layer_data *device_data, VkCommandBuffer commandBuffer, uint32_t attachmentCount,
1541 const VkClearAttachment *pAttachments, uint32_t rectCount, const VkClearRect *pRects) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001542 GLOBAL_CB_NODE *cb_node = GetCBNode(device_data, commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001543 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1544
1545 bool skip = false;
1546 if (cb_node) {
Mike Schuchardt9c582402017-02-23 15:57:37 -07001547 skip |=
1548 ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearAttachments()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01121);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001549 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001550 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_CLEARATTACHMENTS);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001551 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Chris Forbes05375e72017-04-21 13:15:15 -07001552 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001553 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1554 // 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 -07001555 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1556 // CmdClearAttachments.
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001557 skip |=
1558 log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1559 reinterpret_cast<uint64_t &>(commandBuffer), 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
1560 "vkCmdClearAttachments() issued on command buffer object 0x%p prior to any Draw Cmds."
1561 " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1562 commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001563 }
1564 skip |= outsideRenderPass(device_data, cb_node, "vkCmdClearAttachments()", VALIDATION_ERROR_01122);
1565 }
1566
1567 // Validate that attachment is in reference list of active subpass
1568 if (cb_node->activeRenderPass) {
1569 const VkRenderPassCreateInfo *renderpass_create_info = cb_node->activeRenderPass->createInfo.ptr();
1570 const VkSubpassDescription *subpass_desc = &renderpass_create_info->pSubpasses[cb_node->activeSubpass];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001571 auto framebuffer = GetFramebufferState(device_data, cb_node->activeFramebuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001572
1573 for (uint32_t i = 0; i < attachmentCount; i++) {
1574 auto clear_desc = &pAttachments[i];
1575 VkImageView image_view = VK_NULL_HANDLE;
1576
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001577 if (0 == clear_desc->aspectMask) {
1578 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 -06001579 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01128, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001580 validation_error_map[VALIDATION_ERROR_01128]);
1581 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) {
1582 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 -06001583 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01126, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001584 validation_error_map[VALIDATION_ERROR_01126]);
1585 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001586 if (clear_desc->colorAttachment >= subpass_desc->colorAttachmentCount) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001587 skip |=
1588 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 -06001589 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01114, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001590 "vkCmdClearAttachments() color attachment index %d out of range for active subpass %d. %s",
1591 clear_desc->colorAttachment, cb_node->activeSubpass, validation_error_map[VALIDATION_ERROR_01114]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001592 } else if (subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment == VK_ATTACHMENT_UNUSED) {
1593 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001594 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(commandBuffer),
1595 __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001596 "vkCmdClearAttachments() color attachment index %d is VK_ATTACHMENT_UNUSED; ignored.",
1597 clear_desc->colorAttachment);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001598 } else {
1599 image_view = framebuffer->createInfo
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001600 .pAttachments[subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment];
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001601 }
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001602 if ((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
1603 (clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1604 char const str[] =
1605 "vkCmdClearAttachments aspectMask [%d] must set only VK_IMAGE_ASPECT_COLOR_BIT of a color attachment. %s";
1606 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 -06001607 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01125, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001608 validation_error_map[VALIDATION_ERROR_01125]);
1609 }
1610 } else { // Must be depth and/or stencil
1611 if (((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
1612 ((clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1613 char const str[] = "vkCmdClearAttachments aspectMask [%d] is not a valid combination of bits. %s";
1614 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001615 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01127, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001616 validation_error_map[VALIDATION_ERROR_01127]);
1617 }
1618 if (!subpass_desc->pDepthStencilAttachment ||
1619 (subpass_desc->pDepthStencilAttachment->attachment == VK_ATTACHMENT_UNUSED)) {
1620 skip |= log_msg(
1621 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001622 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001623 "vkCmdClearAttachments() depth/stencil clear with no depth/stencil attachment in subpass; ignored");
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001624 } else {
1625 image_view = framebuffer->createInfo.pAttachments[subpass_desc->pDepthStencilAttachment->attachment];
1626 }
1627 }
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001628 if (image_view) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001629 auto image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001630 for (uint32_t j = 0; j < rectCount; j++) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001631 // The rectangular region specified by a given element of pRects must be contained within the render area of
1632 // the current render pass instance
Mark Lobodzinskid833bb72017-02-22 10:55:30 -07001633 // TODO: This check should be moved to CmdExecuteCommands or QueueSubmit to cover secondary CB cases
1634 if ((cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
1635 (false == ContainsRect(cb_node->activeRenderPassBeginInfo.renderArea, pRects[j].rect))) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001636 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1637 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01115, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001638 "vkCmdClearAttachments(): The area defined by pRects[%d] is not contained in the area of "
1639 "the current render pass instance. %s",
1640 j, validation_error_map[VALIDATION_ERROR_01115]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001641 }
1642 // The layers specified by a given element of pRects must be contained within every attachment that
1643 // pAttachments refers to
1644 auto attachment_base_array_layer = image_view_state->create_info.subresourceRange.baseArrayLayer;
1645 auto attachment_layer_count = image_view_state->create_info.subresourceRange.layerCount;
1646 if ((pRects[j].baseArrayLayer < attachment_base_array_layer) || pRects[j].layerCount > attachment_layer_count) {
1647 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001648 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1649 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01116, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001650 "vkCmdClearAttachments(): The layers defined in pRects[%d] are not contained in the layers of "
1651 "pAttachment[%d]. %s",
1652 j, i, validation_error_map[VALIDATION_ERROR_01116]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001653 }
1654 }
1655 }
1656 }
1657 }
1658 return skip;
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001659}
1660
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001661bool PreCallValidateCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001662 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageResolve *pRegions) {
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001663 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001664 bool skip = false;
1665 if (cb_node && src_image_state && dst_image_state) {
1666 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02541);
1667 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02542);
Mike Schuchardt9c582402017-02-23 15:57:37 -07001668 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdResolveImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01334);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001669 skip |= ValidateCmd(device_data, cb_node, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
1670 skip |= insideRenderPass(device_data, cb_node, "vkCmdResolveImage()", VALIDATION_ERROR_01335);
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001671
1672 // For each region, the number of layers in the image subresource should not be zero
1673 // For each region, src and dest image aspect must be color only
1674 for (uint32_t i = 0; i < regionCount; i++) {
1675 if (pRegions[i].srcSubresource.layerCount == 0) {
1676 char const str[] = "vkCmdResolveImage: number of layers in source subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001677 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 -07001678 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1679 "IMAGE", str);
1680 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001681 if (pRegions[i].dstSubresource.layerCount == 0) {
1682 char const str[] = "vkCmdResolveImage: number of layers in destination subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001683 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 -07001684 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1685 "IMAGE", str);
1686 }
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001687 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1688 skip |= log_msg(
1689 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1690 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01339, "IMAGE",
1691 "vkCmdResolveImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s", i,
1692 validation_error_map[VALIDATION_ERROR_01339]);
1693 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001694 if ((pRegions[i].srcSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) ||
1695 (pRegions[i].dstSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)) {
1696 char const str[] =
1697 "vkCmdResolveImage: src and dest aspectMasks for each region must specify only VK_IMAGE_ASPECT_COLOR_BIT";
1698 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1699 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01338, "IMAGE",
1700 "%s. %s", str, validation_error_map[VALIDATION_ERROR_01338]);
1701 }
1702 }
1703
1704 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1705 char const str[] = "vkCmdResolveImage called with unmatched source and dest formats.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001706 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 -07001707 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT,
1708 "IMAGE", str);
1709 }
1710 if (src_image_state->createInfo.imageType != dst_image_state->createInfo.imageType) {
1711 char const str[] = "vkCmdResolveImage called with unmatched source and dest image types.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001712 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 -07001713 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_TYPE, "IMAGE",
1714 str);
1715 }
1716 if (src_image_state->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
1717 char const str[] = "vkCmdResolveImage called with source sample count less than 2.";
1718 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1719 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01320, "IMAGE", "%s. %s",
1720 str, validation_error_map[VALIDATION_ERROR_01320]);
1721 }
1722 if (dst_image_state->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
1723 char const str[] = "vkCmdResolveImage called with dest sample count greater than 1.";
1724 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1725 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01321, "IMAGE", "%s. %s",
1726 str, validation_error_map[VALIDATION_ERROR_01321]);
1727 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001728 } else {
1729 assert(0);
1730 }
1731 return skip;
1732}
1733
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001734void PreCallRecordCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1735 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001736 // Update bindings between images and cmd buffer
1737 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1738 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1739
1740 std::function<bool()> function = [=]() {
1741 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdResolveImage()");
1742 };
1743 cb_node->validate_functions.push_back(function);
1744 function = [=]() {
1745 SetImageMemoryValid(device_data, dst_image_state, true);
1746 return false;
1747 };
1748 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001749 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_RESOLVEIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001750}
1751
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001752bool PreCallValidateCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001753 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
1754 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1755
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001756 bool skip = false;
1757 if (cb_node && src_image_state && dst_image_state) {
1758 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): srcImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001759 VALIDATION_ERROR_02194);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001760 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): dstImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001761 VALIDATION_ERROR_02195);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001762 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02539);
1763 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02540);
1764 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_02182,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001765 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001766 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_02186,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001767 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001768 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdBlitImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01299);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001769 skip |= ValidateCmd(device_data, cb_node, CMD_BLITIMAGE, "vkCmdBlitImage()");
1770 skip |= insideRenderPass(device_data, cb_node, "vkCmdBlitImage()", VALIDATION_ERROR_01300);
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001771
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001772 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001773 // Warn for zero-sized regions
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001774 if ((pRegions[i].srcOffsets[0].x == pRegions[i].srcOffsets[1].x) ||
1775 (pRegions[i].srcOffsets[0].y == pRegions[i].srcOffsets[1].y) ||
1776 (pRegions[i].srcOffsets[0].z == pRegions[i].srcOffsets[1].z)) {
1777 std::stringstream ss;
1778 ss << "vkCmdBlitImage: pRegions[" << i << "].srcOffsets specify a zero-volume area.";
1779 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1780 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1781 "%s", ss.str().c_str());
1782 }
1783 if ((pRegions[i].dstOffsets[0].x == pRegions[i].dstOffsets[1].x) ||
1784 (pRegions[i].dstOffsets[0].y == pRegions[i].dstOffsets[1].y) ||
1785 (pRegions[i].dstOffsets[0].z == pRegions[i].dstOffsets[1].z)) {
1786 std::stringstream ss;
1787 ss << "vkCmdBlitImage: pRegions[" << i << "].dstOffsets specify a zero-volume area.";
1788 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1789 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1790 "%s", ss.str().c_str());
1791 }
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001792 if (pRegions[i].srcSubresource.layerCount == 0) {
1793 char const str[] = "vkCmdBlitImage: number of layers in source subresource is zero";
1794 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 -07001795 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1796 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001797 }
1798 if (pRegions[i].dstSubresource.layerCount == 0) {
1799 char const str[] = "vkCmdBlitImage: number of layers in destination subresource is zero";
1800 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 -07001801 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1802 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001803 }
1804
1805 // Check that src/dst layercounts match
1806 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1807 skip |=
1808 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1809 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01304, "IMAGE",
1810 "vkCmdBlitImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s",
1811 i, validation_error_map[VALIDATION_ERROR_01304]);
1812 }
Mark Lobodzinskie7e85fd2017-02-07 13:44:57 -07001813
1814 if (pRegions[i].srcSubresource.aspectMask != pRegions[i].dstSubresource.aspectMask) {
1815 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1816 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01303, "IMAGE",
1817 "vkCmdBlitImage: aspectMask members for pRegion[%d] do not match. %s", i,
1818 validation_error_map[VALIDATION_ERROR_01303]);
1819 }
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001820 }
1821
1822 VkFormat src_format = src_image_state->createInfo.format;
1823 VkFormat dst_format = dst_image_state->createInfo.format;
1824
1825 // Validate consistency for unsigned formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001826 if (FormatIsUInt(src_format) != FormatIsUInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001827 std::stringstream ss;
1828 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has unsigned integer format, "
1829 << "the other one must also have unsigned integer format. "
1830 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1831 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1832 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02191, "IMAGE", "%s. %s",
1833 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02191]);
1834 }
1835
1836 // Validate consistency for signed formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001837 if (FormatIsSInt(src_format) != FormatIsSInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001838 std::stringstream ss;
1839 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has signed integer format, "
1840 << "the other one must also have signed integer format. "
1841 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1842 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1843 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02190, "IMAGE", "%s. %s",
1844 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02190]);
1845 }
1846
1847 // Validate aspect bits and formats for depth/stencil images
Dave Houlton1d2022c2017-03-29 11:43:58 -06001848 if (FormatIsDepthOrStencil(src_format) || FormatIsDepthOrStencil(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001849 if (src_format != dst_format) {
1850 std::stringstream ss;
1851 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has a format of depth, stencil or depth "
1852 << "stencil, the other one must have exactly the same format. "
1853 << "Source format is " << string_VkFormat(src_format) << " Destination format is "
1854 << string_VkFormat(dst_format);
1855 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1856 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02192, "IMAGE",
1857 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02192]);
1858 }
1859
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001860 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001861 VkImageAspectFlags srcAspect = pRegions[i].srcSubresource.aspectMask;
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001862
Dave Houlton1d2022c2017-03-29 11:43:58 -06001863 if (FormatIsDepthAndStencil(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001864 if ((srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1865 std::stringstream ss;
1866 ss << "vkCmdBlitImage: Combination depth/stencil image formats must have only one of "
1867 "VK_IMAGE_ASPECT_DEPTH_BIT "
1868 << "and VK_IMAGE_ASPECT_STENCIL_BIT set in srcImage and dstImage";
1869 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1870 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1871 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1872 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001873 } else if (FormatIsStencilOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001874 if (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT) {
1875 std::stringstream ss;
1876 ss << "vkCmdBlitImage: Stencil-only image formats must have only the VK_IMAGE_ASPECT_STENCIL_BIT "
1877 << "set in both the srcImage and dstImage";
1878 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1879 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1880 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1881 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001882 } else if (FormatIsDepthOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001883 if (srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) {
1884 std::stringstream ss;
1885 ss << "vkCmdBlitImage: Depth-only image formats must have only the VK_IMAGE_ASPECT_DEPTH "
1886 << "set in both the srcImage and dstImage";
1887 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1888 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1889 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1890 }
1891 }
1892 }
1893 }
1894
1895 // Validate filter
Dave Houlton1d2022c2017-03-29 11:43:58 -06001896 if (FormatIsDepthOrStencil(src_format) && (filter != VK_FILTER_NEAREST)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001897 std::stringstream ss;
1898 ss << "vkCmdBlitImage: If the format of srcImage is a depth, stencil, or depth stencil "
1899 << "then filter must be VK_FILTER_NEAREST.";
1900 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1901 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02193, "IMAGE", "%s. %s",
1902 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02193]);
1903 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001904 } else {
1905 assert(0);
1906 }
1907 return skip;
1908}
1909
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001910void PreCallRecordCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1911 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001912 // Update bindings between images and cmd buffer
1913 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1914 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1915
1916 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdBlitImage()"); };
1917 cb_node->validate_functions.push_back(function);
1918 function = [=]() {
1919 SetImageMemoryValid(device_data, dst_image_state, true);
1920 return false;
1921 };
1922 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001923 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_BLITIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001924}
1925
Tony Barbourdf013b92017-01-25 12:53:48 -07001926// This validates that the initial layout specified in the command buffer for
1927// the IMAGE is the same
1928// as the global IMAGE layout
Tony Barboure0c5cc92017-02-08 13:53:39 -07001929bool ValidateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
1930 std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap) {
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001931 bool skip = false;
Tony Barbourdf013b92017-01-25 12:53:48 -07001932 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001933 for (auto cb_image_data : pCB->imageLayoutMap) {
1934 VkImageLayout imageLayout;
Tony Barbourdf013b92017-01-25 12:53:48 -07001935
Jeremy Hayes55b6c292017-02-28 09:44:45 -07001936 if (FindLayout(imageLayoutMap, cb_image_data.first, imageLayout)) {
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001937 if (cb_image_data.second.initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
1938 // TODO: Set memory invalid which is in mem_tracker currently
1939 } else if (imageLayout != cb_image_data.second.initialLayout) {
1940 if (cb_image_data.first.hasSubresource) {
Dave Houltoneba86e22017-03-02 14:56:23 -07001941 skip |= log_msg(
1942 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1943 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1944 "Cannot submit cmd buffer using image (0x%" PRIx64
1945 ") [sub-resource: aspectMask 0x%X array layer %u, mip level %u], "
1946 "with layout %s when first use is %s.",
1947 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), cb_image_data.first.subresource.aspectMask,
1948 cb_image_data.first.subresource.arrayLayer, cb_image_data.first.subresource.mipLevel,
1949 string_VkImageLayout(imageLayout), string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001950 } else {
Dave Houltoneba86e22017-03-02 14:56:23 -07001951 skip |=
1952 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1953 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1954 "Cannot submit cmd buffer using image (0x%" PRIx64
1955 ") with layout %s when "
1956 "first use is %s.",
1957 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), string_VkImageLayout(imageLayout),
1958 string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001959 }
1960 }
Tony Barbourdf013b92017-01-25 12:53:48 -07001961 SetLayout(imageLayoutMap, cb_image_data.first, cb_image_data.second.layout);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001962 }
1963 }
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001964 return skip;
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001965}
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001966
Tony Barbourdf013b92017-01-25 12:53:48 -07001967void UpdateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB) {
1968 for (auto cb_image_data : pCB->imageLayoutMap) {
1969 VkImageLayout imageLayout;
1970 FindGlobalLayout(device_data, cb_image_data.first, imageLayout);
1971 SetGlobalLayout(device_data, cb_image_data.first, cb_image_data.second.layout);
1972 }
1973}
1974
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001975// Print readable FlagBits in FlagMask
1976static std::string string_VkAccessFlags(VkAccessFlags accessMask) {
1977 std::string result;
1978 std::string separator;
1979
1980 if (accessMask == 0) {
1981 result = "[None]";
1982 } else {
1983 result = "[";
1984 for (auto i = 0; i < 32; i++) {
1985 if (accessMask & (1 << i)) {
1986 result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
1987 separator = " | ";
1988 }
1989 }
1990 result = result + "]";
1991 }
1992 return result;
1993}
1994
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07001995// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set. If required_bit is zero, accessMask
1996// must have at least one of 'optional_bits' set
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001997// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07001998static bool ValidateMaskBits(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
1999 const VkImageLayout &layout, VkAccessFlags required_bit, VkAccessFlags optional_bits,
2000 const char *type) {
2001 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2002 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002003
2004 if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
2005 if (accessMask & ~(required_bit | optional_bits)) {
2006 // TODO: Verify against Valid Use
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002007 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2008 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002009 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2010 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002011 }
2012 } else {
2013 if (!required_bit) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002014 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2015 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002016 "%s AccessMask %d %s must contain at least one of access bits %d "
2017 "%s when layout is %s, unless the app has previously added a "
2018 "barrier for this transition.",
2019 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
2020 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002021 } else {
2022 std::string opt_bits;
2023 if (optional_bits != 0) {
2024 std::stringstream ss;
2025 ss << optional_bits;
2026 opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
2027 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002028 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2029 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002030 "%s AccessMask %d %s must have required access bit %d %s %s when "
2031 "layout is %s, unless the app has previously added a barrier for "
2032 "this transition.",
2033 type, accessMask, string_VkAccessFlags(accessMask).c_str(), required_bit,
2034 string_VkAccessFlags(required_bit).c_str(), opt_bits.c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002035 }
2036 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002037 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002038}
2039
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002040bool ValidateMaskBitsFromLayouts(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer,
2041 const VkAccessFlags &accessMask, const VkImageLayout &layout, const char *type) {
2042 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002043
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002044 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002045 switch (layout) {
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002046 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
2047 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
2048 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2049 break;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002050 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002051 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
2052 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
2053 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2054 break;
2055 }
2056 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
2057 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
2058 break;
2059 }
2060 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
2061 skip |= ValidateMaskBits(
2062 device_data, cmdBuffer, accessMask, layout, 0,
2063 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
2064 type);
2065 break;
2066 }
2067 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
2068 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, 0,
2069 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
2070 break;
2071 }
2072 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
2073 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
2074 break;
2075 }
2076 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
2077 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_MEMORY_READ_BIT, 0, type);
2078 break;
2079 }
2080 case VK_IMAGE_LAYOUT_UNDEFINED: {
2081 if (accessMask != 0) {
2082 // TODO: Verify against Valid Use section spec
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002083 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2084 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002085 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2086 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
2087 }
2088 break;
2089 }
2090 case VK_IMAGE_LAYOUT_GENERAL:
2091 default: { break; }
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002092 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002093 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002094}
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002095
2096// ValidateLayoutVsAttachmentDescription is a general function where we can validate various state associated with the
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002097// VkAttachmentDescription structs that are used by the sub-passes of a renderpass. Initial check is to make sure that READ_ONLY
2098// layout attachments don't have CLEAR as their loadOp.
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002099bool ValidateLayoutVsAttachmentDescription(const debug_report_data *report_data, const VkImageLayout first_layout,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002100 const uint32_t attachment, const VkAttachmentDescription &attachment_description) {
2101 bool skip = false;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002102 // Verify that initial loadOp on READ_ONLY attachments is not CLEAR
2103 if (attachment_description.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2104 if ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
2105 (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)) {
Mark Lobodzinskieb9e73f2017-04-13 10:06:48 -06002106 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2107 VALIDATION_ERROR_02351, "DS", "Cannot clear attachment %d with invalid first layout %s. %s", attachment,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002108 string_VkImageLayout(first_layout), validation_error_map[VALIDATION_ERROR_02351]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002109 }
2110 }
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002111 return skip;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002112}
2113
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002114bool ValidateLayouts(core_validation::layer_data *device_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo) {
2115 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002116 bool skip = false;
2117
2118 // Track when we're observing the first use of an attachment
2119 std::vector<bool> attach_first_use(pCreateInfo->attachmentCount, true);
2120 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
2121 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
2122 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
2123 auto attach_index = subpass.pColorAttachments[j].attachment;
2124 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2125
2126 switch (subpass.pColorAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002127 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
2128 // This is ideal.
2129 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002130
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002131 case VK_IMAGE_LAYOUT_GENERAL:
2132 // May not be optimal; TODO: reconsider this warning based on other constraints?
2133 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2134 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2135 "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
2136 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002137
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002138 default:
2139 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2140 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2141 "Layout for color attachment is %s but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.",
2142 string_VkImageLayout(subpass.pColorAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002143 }
2144
2145 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002146 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pColorAttachments[j].layout, attach_index,
2147 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002148 }
2149 attach_first_use[attach_index] = false;
2150 }
2151 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
2152 switch (subpass.pDepthStencilAttachment->layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002153 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
2154 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2155 // These are ideal.
2156 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002157
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002158 case VK_IMAGE_LAYOUT_GENERAL:
2159 // May not be optimal; TODO: reconsider this warning based on other constraints? GENERAL can be better than
2160 // doing a bunch of transitions.
2161 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2162 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2163 "GENERAL layout for depth attachment may not give optimal performance.");
2164 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002165
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002166 default:
2167 // No other layouts are acceptable
2168 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2169 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2170 "Layout for depth attachment is %s but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL, "
2171 "DEPTH_STENCIL_READ_ONLY_OPTIMAL or GENERAL.",
2172 string_VkImageLayout(subpass.pDepthStencilAttachment->layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002173 }
2174
2175 auto attach_index = subpass.pDepthStencilAttachment->attachment;
2176 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002177 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pDepthStencilAttachment->layout, attach_index,
2178 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002179 }
2180 attach_first_use[attach_index] = false;
2181 }
2182 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
2183 auto attach_index = subpass.pInputAttachments[j].attachment;
2184 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2185
2186 switch (subpass.pInputAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002187 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2188 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
2189 // These are ideal.
2190 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002191
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002192 case VK_IMAGE_LAYOUT_GENERAL:
2193 // May not be optimal. TODO: reconsider this warning based on other constraints.
2194 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2195 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2196 "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
2197 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002198
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002199 default:
2200 // No other layouts are acceptable
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002201 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2202 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002203 "Layout for input attachment is %s but can only be READ_ONLY_OPTIMAL or GENERAL.",
2204 string_VkImageLayout(subpass.pInputAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002205 }
2206
2207 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002208 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pInputAttachments[j].layout, attach_index,
2209 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002210 }
2211 attach_first_use[attach_index] = false;
2212 }
2213 }
2214 return skip;
2215}
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002216
2217// For any image objects that overlap mapped memory, verify that their layouts are PREINIT or GENERAL
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002218bool ValidateMapImageLayouts(core_validation::layer_data *device_data, VkDevice device, DEVICE_MEM_INFO const *mem_info,
2219 VkDeviceSize offset, VkDeviceSize end_offset) {
2220 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2221 bool skip = false;
2222 // Iterate over all bound image ranges and verify that for any that overlap the map ranges, the layouts are
2223 // VK_IMAGE_LAYOUT_PREINITIALIZED or VK_IMAGE_LAYOUT_GENERAL
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002224 // TODO : This can be optimized if we store ranges based on starting address and early exit when we pass our range
2225 for (auto image_handle : mem_info->bound_images) {
2226 auto img_it = mem_info->bound_ranges.find(image_handle);
2227 if (img_it != mem_info->bound_ranges.end()) {
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002228 if (rangesIntersect(device_data, &img_it->second, offset, end_offset)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002229 std::vector<VkImageLayout> layouts;
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002230 if (FindLayouts(device_data, VkImage(image_handle), layouts)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002231 for (auto layout : layouts) {
2232 if (layout != VK_IMAGE_LAYOUT_PREINITIALIZED && layout != VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002233 skip |= log_msg(
2234 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
2235 reinterpret_cast<const uint64_t &>(mem_info->mem), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2236 "Mapping an image with layout %s can result in undefined behavior if this memory is "
2237 "used by the device. Only GENERAL or PREINITIALIZED should be used.",
2238 string_VkImageLayout(layout));
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002239 }
2240 }
2241 }
2242 }
2243 }
2244 }
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002245 return skip;
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002246}
Mark Lobodzinski96210742017-02-09 10:33:46 -07002247
2248// Helper function to validate correct usage bits set for buffers or images. Verify that (actual & desired) flags != 0 or, if strict
2249// is true, verify that (actual & desired) flags == desired
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002250static bool validate_usage_flags(layer_data *device_data, VkFlags actual, VkFlags desired, VkBool32 strict, uint64_t obj_handle,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002251 VulkanObjectType obj_type, int32_t const msgCode, char const *func_name, char const *usage_str) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002252 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002253
2254 bool correct_usage = false;
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002255 bool skip = false;
Mark Lobodzinski33826372017-04-13 11:10:11 -06002256 const char *type_str = object_string[obj_type];
Mark Lobodzinski96210742017-02-09 10:33:46 -07002257 if (strict) {
2258 correct_usage = ((actual & desired) == desired);
2259 } else {
2260 correct_usage = ((actual & desired) != 0);
2261 }
2262 if (!correct_usage) {
2263 if (msgCode == -1) {
2264 // TODO: Fix callers with msgCode == -1 to use correct validation checks.
Mark Lobodzinski33826372017-04-13 11:10:11 -06002265 skip = log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, GetDebugReportEnum[obj_type], obj_handle, __LINE__,
2266 MEMTRACK_INVALID_USAGE_FLAG, "MEM",
2267 "Invalid usage flag for %s 0x%" PRIxLEAST64
2268 " used by %s. In this case, %s should have %s set during creation.",
2269 type_str, obj_handle, func_name, type_str, usage_str);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002270 } else {
2271 const char *valid_usage = (msgCode == -1) ? "" : validation_error_map[msgCode];
Mark Lobodzinski33826372017-04-13 11:10:11 -06002272 skip = log_msg(
2273 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, GetDebugReportEnum[obj_type], obj_handle, __LINE__, msgCode, "MEM",
2274 "Invalid usage flag for %s 0x%" PRIxLEAST64 " used by %s. In this case, %s should have %s set during creation. %s",
2275 type_str, obj_handle, func_name, type_str, usage_str, valid_usage);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002276 }
2277 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002278 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002279}
2280
2281// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2282// where an error will be flagged if usage is not correct
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002283bool ValidateImageUsageFlags(layer_data *device_data, IMAGE_STATE const *image_state, VkFlags desired, VkBool32 strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002284 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002285 return validate_usage_flags(device_data, image_state->createInfo.usage, desired, strict,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002286 reinterpret_cast<const uint64_t &>(image_state->image), kVulkanObjectTypeImage, msgCode, func_name,
2287 usage_string);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002288}
2289
2290// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2291// where an error will be flagged if usage is not correct
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002292bool ValidateBufferUsageFlags(layer_data *device_data, BUFFER_STATE const *buffer_state, VkFlags desired, VkBool32 strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002293 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002294 return validate_usage_flags(device_data, buffer_state->createInfo.usage, desired, strict,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002295 reinterpret_cast<const uint64_t &>(buffer_state->buffer), kVulkanObjectTypeBuffer, msgCode,
2296 func_name, usage_string);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002297}
2298
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002299bool PreCallValidateCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo) {
Mark Lobodzinski96210742017-02-09 10:33:46 -07002300 bool skip = false;
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002301 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2302
Mark Lobodzinski96210742017-02-09 10:33:46 -07002303 // TODO: Add check for VALIDATION_ERROR_00658
Mark Lobodzinski96210742017-02-09 10:33:46 -07002304 // TODO: Add check for VALIDATION_ERROR_00667
2305 // TODO: Add check for VALIDATION_ERROR_00668
2306 // TODO: Add check for VALIDATION_ERROR_00669
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002307
2308 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
2309 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2310 VALIDATION_ERROR_00666, "DS",
2311 "vkCreateBuffer(): the sparseBinding device feature is disabled: Buffers cannot be created with the "
2312 "VK_BUFFER_CREATE_SPARSE_BINDING_BIT set. %s",
2313 validation_error_map[VALIDATION_ERROR_00666]);
2314 }
Mark Lobodzinskiaf355062017-03-13 09:35:01 -06002315
2316 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyBuffer)) {
2317 skip |=
2318 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2319 DRAWSTATE_INVALID_FEATURE, "DS",
2320 "vkCreateBuffer(): the sparseResidencyBuffer device feature is disabled: Buffers cannot be created with the "
2321 "VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT set.");
2322 }
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -06002323
2324 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
2325 skip |=
2326 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2327 DRAWSTATE_INVALID_FEATURE, "DS",
2328 "vkCreateBuffer(): the sparseResidencyAliased device feature is disabled: Buffers cannot be created with the "
2329 "VK_BUFFER_CREATE_SPARSE_ALIASED_BIT set.");
2330 }
Mark Lobodzinski96210742017-02-09 10:33:46 -07002331 return skip;
2332}
2333
2334void PostCallRecordCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo, VkBuffer *pBuffer) {
2335 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
2336 GetBufferMap(device_data)
2337 ->insert(std::make_pair(*pBuffer, std::unique_ptr<BUFFER_STATE>(new BUFFER_STATE(*pBuffer, pCreateInfo))));
2338}
2339
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002340bool PreCallValidateCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo) {
2341 bool skip = false;
2342 BUFFER_STATE *buffer_state = GetBufferState(device_data, pCreateInfo->buffer);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002343 // If this isn't a sparse buffer, it needs to have memory backing it at CreateBufferView time
2344 if (buffer_state) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002345 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCreateBufferView()", VALIDATION_ERROR_02522);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002346 // In order to create a valid buffer view, the buffer must have been created with at least one of the following flags:
2347 // UNIFORM_TEXEL_BUFFER_BIT or STORAGE_TEXEL_BUFFER_BIT
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002348 skip |= ValidateBufferUsageFlags(
2349 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 -07002350 VALIDATION_ERROR_00694, "vkCreateBufferView()", "VK_BUFFER_USAGE_[STORAGE|UNIFORM]_TEXEL_BUFFER_BIT");
2351 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002352 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002353}
2354
2355void PostCallRecordCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo, VkBufferView *pView) {
2356 (*GetBufferViewMap(device_data))[*pView] = std::unique_ptr<BUFFER_VIEW_STATE>(new BUFFER_VIEW_STATE(*pView, pCreateInfo));
2357}
Mark Lobodzinski602de982017-02-09 11:01:33 -07002358
2359// For the given format verify that the aspect masks make sense
2360bool ValidateImageAspectMask(layer_data *device_data, VkImage image, VkFormat format, VkImageAspectFlags aspect_mask,
2361 const char *func_name) {
2362 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2363 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06002364 if (FormatIsColor(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002365 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
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 the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2369 validation_error_map[VALIDATION_ERROR_00741]);
2370 } else if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != aspect_mask) {
2371 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2372 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2373 "%s: Color image formats must have ONLY the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2374 validation_error_map[VALIDATION_ERROR_00741]);
2375 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002376 } else if (FormatIsDepthAndStencil(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002377 if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) == 0) {
2378 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2379 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2380 "%s: Depth/stencil image formats must have "
2381 "at least one of VK_IMAGE_ASPECT_DEPTH_BIT "
2382 "and VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2383 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2384 } else if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) != aspect_mask) {
2385 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2386 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2387 "%s: Combination depth/stencil image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT and "
2388 "VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2389 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2390 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002391 } else if (FormatIsDepthOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002392 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) {
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 must have the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2396 validation_error_map[VALIDATION_ERROR_00741]);
2397 } else if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != aspect_mask) {
2398 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2399 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2400 "%s: Depth-only image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2401 validation_error_map[VALIDATION_ERROR_00741]);
2402 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002403 } else if (FormatIsStencilOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002404 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT) {
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 must have the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2408 validation_error_map[VALIDATION_ERROR_00741]);
2409 } else if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != aspect_mask) {
2410 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2411 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2412 "%s: Stencil-only image formats can have only the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2413 validation_error_map[VALIDATION_ERROR_00741]);
2414 }
2415 }
2416 return skip;
2417}
2418
2419bool ValidateImageSubrangeLevelLayerCounts(layer_data *device_data, const VkImageSubresourceRange &subresourceRange,
2420 const char *func_name) {
2421 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2422 bool skip = false;
2423 if (subresourceRange.levelCount == 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_00768, "IMAGE", "%s called with 0 in subresourceRange.levelCount. %s", func_name,
2426 validation_error_map[VALIDATION_ERROR_00768]);
2427 }
2428 if (subresourceRange.layerCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002429 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 -07002430 VALIDATION_ERROR_00769, "IMAGE", "%s called with 0 in subresourceRange.layerCount. %s", func_name,
2431 validation_error_map[VALIDATION_ERROR_00769]);
2432 }
2433 return skip;
2434}
2435
2436bool PreCallValidateCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info) {
2437 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2438 bool skip = false;
2439 IMAGE_STATE *image_state = GetImageState(device_data, create_info->image);
2440 if (image_state) {
2441 skip |= ValidateImageUsageFlags(
2442 device_data, image_state,
2443 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
2444 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
2445 false, -1, "vkCreateImageView()",
2446 "VK_IMAGE_USAGE_[SAMPLED|STORAGE|COLOR_ATTACHMENT|DEPTH_STENCIL_ATTACHMENT|INPUT_ATTACHMENT]_BIT");
2447 // If this isn't a sparse image, it needs to have memory backing it at CreateImageView time
2448 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCreateImageView()", VALIDATION_ERROR_02524);
2449 // Checks imported from image layer
Jeremy Hayesf6bfa6b2017-04-04 15:05:52 -06002450 if ((create_info->subresourceRange.baseMipLevel + create_info->subresourceRange.levelCount) >
2451 image_state->createInfo.mipLevels) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002452 std::stringstream ss;
2453 ss << "vkCreateImageView called with baseMipLevel " << create_info->subresourceRange.baseMipLevel << " for image "
2454 << create_info->image << " that only has " << image_state->createInfo.mipLevels << " mip levels.";
2455 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002456 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 -07002457 VALIDATION_ERROR_00768, "IMAGE", "%s %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_00768]);
2458 }
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002459 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
2460 if (create_info->subresourceRange.baseArrayLayer >= image_state->createInfo.arrayLayers) {
2461 std::stringstream ss;
2462 ss << "vkCreateImageView called with baseArrayLayer " << create_info->subresourceRange.baseArrayLayer
2463 << " for image " << create_info->image << " that only has " << image_state->createInfo.arrayLayers
2464 << " array layers.";
2465 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2466 VALIDATION_ERROR_00769, "IMAGE", "%s %s", ss.str().c_str(),
2467 validation_error_map[VALIDATION_ERROR_00769]);
2468 }
Mark Lobodzinski602de982017-02-09 11:01:33 -07002469 }
2470 // TODO: Need new valid usage language for levelCount == 0 & layerCount == 0
2471 skip |= ValidateImageSubrangeLevelLayerCounts(device_data, create_info->subresourceRange, "vkCreateImageView()");
2472
2473 VkImageCreateFlags image_flags = image_state->createInfo.flags;
2474 VkFormat image_format = image_state->createInfo.format;
2475 VkFormat view_format = create_info->format;
2476 VkImageAspectFlags aspect_mask = create_info->subresourceRange.aspectMask;
2477
2478 // Validate VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT state
2479 if (image_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
2480 // Format MUST be compatible (in the same format compatibility class) as the format the image was created with
Dave Houlton1d2022c2017-03-29 11:43:58 -06002481 if (FormatCompatibilityClass(image_format) != FormatCompatibilityClass(view_format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002482 std::stringstream ss;
2483 ss << "vkCreateImageView(): ImageView format " << string_VkFormat(view_format)
2484 << " is not in the same format compatibility class as image (" << (uint64_t)create_info->image << ") format "
2485 << string_VkFormat(image_format) << ". Images created with the VK_IMAGE_CREATE_MUTABLE_FORMAT BIT "
2486 << "can support ImageViews with differing formats but they must be in the same compatibility class.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002487 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 -07002488 VALIDATION_ERROR_02171, "IMAGE", "%s %s", ss.str().c_str(),
2489 validation_error_map[VALIDATION_ERROR_02171]);
2490 }
2491 } else {
2492 // Format MUST be IDENTICAL to the format the image was created with
2493 if (image_format != view_format) {
2494 std::stringstream ss;
2495 ss << "vkCreateImageView() format " << string_VkFormat(view_format) << " differs from image "
2496 << (uint64_t)create_info->image << " format " << string_VkFormat(image_format)
2497 << ". Formats MUST be IDENTICAL unless VK_IMAGE_CREATE_MUTABLE_FORMAT BIT was set on image creation.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002498 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 -07002499 VALIDATION_ERROR_02172, "IMAGE", "%s %s", ss.str().c_str(),
2500 validation_error_map[VALIDATION_ERROR_02172]);
2501 }
2502 }
2503
2504 // Validate correct image aspect bits for desired formats and format consistency
2505 skip |= ValidateImageAspectMask(device_data, image_state->image, image_format, aspect_mask, "vkCreateImageView()");
2506 }
2507 return skip;
2508}
2509
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -07002510void PostCallRecordCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info, VkImageView view) {
2511 auto image_view_map = GetImageViewMap(device_data);
2512 (*image_view_map)[view] = std::unique_ptr<IMAGE_VIEW_STATE>(new IMAGE_VIEW_STATE(view, create_info));
2513
2514 auto image_state = GetImageState(device_data, create_info->image);
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002515 auto &sub_res_range = (*image_view_map)[view].get()->create_info.subresourceRange;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -06002516 sub_res_range.levelCount = ResolveRemainingLevels(&sub_res_range, image_state->createInfo.mipLevels);
2517 sub_res_range.layerCount = ResolveRemainingLayers(&sub_res_range, image_state->createInfo.arrayLayers);
Mark Lobodzinski602de982017-02-09 11:01:33 -07002518}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002519
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002520bool PreCallValidateCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2521 BUFFER_STATE *dst_buffer_state) {
2522 bool skip = false;
2523 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02531);
2524 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02532);
2525 // Validate that SRC & DST buffers have correct usage flags set
2526 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01164,
2527 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
2528 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01165,
2529 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07002530 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyBuffer()",
2531 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01171);
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002532 skip |= ValidateCmd(device_data, cb_node, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
2533 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBuffer()", VALIDATION_ERROR_01172);
2534 return skip;
2535}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002536
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002537void PreCallRecordCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2538 BUFFER_STATE *dst_buffer_state) {
2539 // Update bindings between buffers and cmd buffer
2540 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
2541 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
2542
2543 std::function<bool()> function = [=]() {
2544 return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBuffer()");
2545 };
2546 cb_node->validate_functions.push_back(function);
2547 function = [=]() {
2548 SetBufferMemoryValid(device_data, dst_buffer_state, true);
2549 return false;
2550 };
2551 cb_node->validate_functions.push_back(function);
2552 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFER);
2553}
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002554
2555static bool validateIdleBuffer(layer_data *device_data, VkBuffer buffer) {
2556 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2557 bool skip = false;
2558 auto buffer_state = GetBufferState(device_data, buffer);
2559 if (!buffer_state) {
2560 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2561 __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
2562 "Cannot free buffer 0x%" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
2563 } else {
2564 if (buffer_state->in_use.load()) {
2565 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2566 __LINE__, VALIDATION_ERROR_00676, "DS",
2567 "Cannot free buffer 0x%" PRIxLEAST64 " that is in use by a command buffer. %s", (uint64_t)(buffer),
2568 validation_error_map[VALIDATION_ERROR_00676]);
2569 }
2570 }
2571 return skip;
2572}
2573
2574bool PreCallValidateDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE **image_view_state,
2575 VK_OBJECT *obj_struct) {
2576 *image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002577 *obj_struct = {reinterpret_cast<uint64_t &>(image_view), kVulkanObjectTypeImageView};
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002578 if (GetDisables(device_data)->destroy_image_view) return false;
2579 bool skip = false;
2580 if (*image_view_state) {
2581 skip |= ValidateObjectNotInUse(device_data, *image_view_state, *obj_struct, VALIDATION_ERROR_00776);
2582 }
2583 return skip;
2584}
2585
2586void PostCallRecordDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE *image_view_state,
2587 VK_OBJECT obj_struct) {
2588 // Any bound cmd buffers are now invalid
2589 invalidateCommandBuffers(device_data, image_view_state->cb_bindings, obj_struct);
2590 (*GetImageViewMap(device_data)).erase(image_view);
2591}
2592
2593bool PreCallValidateDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE **buffer_state, VK_OBJECT *obj_struct) {
2594 *buffer_state = GetBufferState(device_data, buffer);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002595 *obj_struct = {reinterpret_cast<uint64_t &>(buffer), kVulkanObjectTypeBuffer };
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002596 if (GetDisables(device_data)->destroy_buffer) return false;
2597 bool skip = false;
2598 if (*buffer_state) {
2599 skip |= validateIdleBuffer(device_data, buffer);
2600 }
2601 return skip;
2602}
2603
2604void PostCallRecordDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE *buffer_state, VK_OBJECT obj_struct) {
2605 invalidateCommandBuffers(device_data, buffer_state->cb_bindings, obj_struct);
2606 for (auto mem_binding : buffer_state->GetBoundMemory()) {
2607 auto mem_info = GetMemObjInfo(device_data, mem_binding);
2608 if (mem_info) {
2609 core_validation::RemoveBufferMemoryRange(reinterpret_cast<uint64_t &>(buffer), mem_info);
2610 }
2611 }
Mark Lobodzinski33826372017-04-13 11:10:11 -06002612 ClearMemoryObjectBindings(device_data, reinterpret_cast<uint64_t &>(buffer), kVulkanObjectTypeBuffer);
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002613 GetBufferMap(device_data)->erase(buffer_state->buffer);
2614}
2615
2616bool PreCallValidateDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE **buffer_view_state,
2617 VK_OBJECT *obj_struct) {
2618 *buffer_view_state = GetBufferViewState(device_data, buffer_view);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002619 *obj_struct = {reinterpret_cast<uint64_t &>(buffer_view), kVulkanObjectTypeBufferView };
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002620 if (GetDisables(device_data)->destroy_buffer_view) return false;
2621 bool skip = false;
2622 if (*buffer_view_state) {
2623 skip |= ValidateObjectNotInUse(device_data, *buffer_view_state, *obj_struct, VALIDATION_ERROR_00701);
2624 }
2625 return skip;
2626}
2627
2628void PostCallRecordDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE *buffer_view_state,
2629 VK_OBJECT obj_struct) {
2630 // Any bound cmd buffers are now invalid
2631 invalidateCommandBuffers(device_data, buffer_view_state->cb_bindings, obj_struct);
2632 GetBufferViewMap(device_data)->erase(buffer_view);
2633}
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002634
2635bool PreCallValidateCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2636 bool skip = false;
2637 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCmdFillBuffer()", VALIDATION_ERROR_02529);
Mike Schuchardt9c582402017-02-23 15:57:37 -07002638 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdFillBuffer()",
2639 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01141);
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002640 skip |= ValidateCmd(device_data, cb_node, CMD_FILLBUFFER, "vkCmdFillBuffer()");
2641 // Validate that DST buffer has correct usage flags set
2642 skip |= ValidateBufferUsageFlags(device_data, buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01137,
2643 "vkCmdFillBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
2644 skip |= insideRenderPass(device_data, cb_node, "vkCmdFillBuffer()", VALIDATION_ERROR_01142);
2645 return skip;
2646}
2647
2648void PreCallRecordCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2649 std::function<bool()> function = [=]() {
2650 SetBufferMemoryValid(device_data, buffer_state, true);
2651 return false;
2652 };
2653 cb_node->validate_functions.push_back(function);
2654 // Update bindings between buffer and cmd buffer
2655 AddCommandBufferBindingBuffer(device_data, cb_node, buffer_state);
2656 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_FILLBUFFER);
2657}
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002658
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002659bool ValidateBufferImageCopyData(const debug_report_data *report_data, uint32_t regionCount, const VkBufferImageCopy *pRegions,
2660 IMAGE_STATE *image_state, const char *function) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002661 bool skip = false;
2662
2663 for (uint32_t i = 0; i < regionCount; i++) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002664 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) {
2665 if ((pRegions[i].imageOffset.y != 0) || (pRegions[i].imageExtent.height != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002666 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 -07002667 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01746, "IMAGE",
2668 "%s(): pRegion[%d] imageOffset.y is %d and imageExtent.height is %d. For 1D images these "
2669 "must be 0 and 1, respectively. %s",
2670 function, i, pRegions[i].imageOffset.y, pRegions[i].imageExtent.height,
2671 validation_error_map[VALIDATION_ERROR_01746]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002672 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002673 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002674
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002675 if ((image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) || (image_state->createInfo.imageType == VK_IMAGE_TYPE_2D)) {
2676 if ((pRegions[i].imageOffset.z != 0) || (pRegions[i].imageExtent.depth != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002677 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 -07002678 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01747, "IMAGE",
2679 "%s(): pRegion[%d] imageOffset.z is %d and imageExtent.depth is %d. For 1D and 2D images these "
2680 "must be 0 and 1, respectively. %s",
2681 function, i, pRegions[i].imageOffset.z, pRegions[i].imageExtent.depth,
2682 validation_error_map[VALIDATION_ERROR_01747]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002683 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002684 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002685
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002686 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) {
2687 if ((0 != pRegions[i].imageSubresource.baseArrayLayer) || (1 != pRegions[i].imageSubresource.layerCount)) {
2688 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2689 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01281, "IMAGE",
2690 "%s(): pRegion[%d] imageSubresource.baseArrayLayer is %d and imageSubresource.layerCount is "
2691 "%d. For 3D images these must be 0 and 1, respectively. %s",
2692 function, i, pRegions[i].imageSubresource.baseArrayLayer, pRegions[i].imageSubresource.layerCount,
2693 validation_error_map[VALIDATION_ERROR_01281]);
2694 }
2695 }
2696
2697 // If the the calling command's VkImage parameter's format is not a depth/stencil format,
2698 // then bufferOffset must be a multiple of the calling command's VkImage parameter's texel size
Dave Houlton1d2022c2017-03-29 11:43:58 -06002699 auto texel_size = FormatSize(image_state->createInfo.format);
2700 if (!FormatIsDepthAndStencil(image_state->createInfo.format) &&
2701 SafeModulo(pRegions[i].bufferOffset, texel_size) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002702 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_01263, "IMAGE",
2704 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64
2705 " must be a multiple of this format's texel size (" PRINTF_SIZE_T_SPECIFIER "). %s",
2706 function, i, pRegions[i].bufferOffset, texel_size, validation_error_map[VALIDATION_ERROR_01263]);
2707 }
2708
2709 // BufferOffset must be a multiple of 4
Dave Houlton1d2022c2017-03-29 11:43:58 -06002710 if (SafeModulo(pRegions[i].bufferOffset, 4) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002711 skip |= log_msg(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_01264, "IMAGE",
2713 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64 " must be a multiple of 4. %s", function, i,
2714 pRegions[i].bufferOffset, validation_error_map[VALIDATION_ERROR_01264]);
2715 }
2716
2717 // BufferRowLength must be 0, or greater than or equal to the width member of imageExtent
2718 if ((pRegions[i].bufferRowLength != 0) && (pRegions[i].bufferRowLength < pRegions[i].imageExtent.width)) {
2719 skip |= log_msg(
2720 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2721 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01265, "IMAGE",
2722 "%s(): pRegion[%d] bufferRowLength (%d) must be zero or greater-than-or-equal-to imageExtent.width (%d). %s",
2723 function, i, pRegions[i].bufferRowLength, pRegions[i].imageExtent.width,
2724 validation_error_map[VALIDATION_ERROR_01265]);
2725 }
2726
2727 // BufferImageHeight must be 0, or greater than or equal to the height member of imageExtent
2728 if ((pRegions[i].bufferImageHeight != 0) && (pRegions[i].bufferImageHeight < pRegions[i].imageExtent.height)) {
2729 skip |= log_msg(
2730 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2731 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01266, "IMAGE",
2732 "%s(): pRegion[%d] bufferImageHeight (%d) must be zero or greater-than-or-equal-to imageExtent.height (%d). %s",
2733 function, i, pRegions[i].bufferImageHeight, pRegions[i].imageExtent.height,
2734 validation_error_map[VALIDATION_ERROR_01266]);
2735 }
2736
2737 // subresource aspectMask must have exactly 1 bit set
2738 const int num_bits = sizeof(VkFlags) * CHAR_BIT;
2739 std::bitset<num_bits> aspect_mask_bits(pRegions[i].imageSubresource.aspectMask);
2740 if (aspect_mask_bits.count() != 1) {
2741 skip |= log_msg(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_01280, "IMAGE",
2743 "%s: aspectMasks for imageSubresource in each region must have only a single bit set. %s", function,
2744 validation_error_map[VALIDATION_ERROR_01280]);
2745 }
2746
2747 // image subresource aspect bit must match format
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002748 if (!VerifyAspectsPresent(pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002749 skip |= log_msg(
2750 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2751 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01279, "IMAGE",
2752 "%s(): pRegion[%d] subresource aspectMask 0x%x specifies aspects that are not present in image format 0x%x. %s",
2753 function, i, pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format,
2754 validation_error_map[VALIDATION_ERROR_01279]);
2755 }
2756
2757 // Checks that apply only to compressed images
2758 // TODO: there is a comment in ValidateCopyBufferImageTransferGranularityRequirements() in core_validation.cpp that
2759 // reserves a place for these compressed image checks. This block of code could move there once the image
2760 // stuff is moved into core validation.
Dave Houlton1d2022c2017-03-29 11:43:58 -06002761 if (FormatIsCompressed(image_state->createInfo.format)) {
2762 auto block_size = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002763
2764 // BufferRowLength must be a multiple of block width
Dave Houlton1d2022c2017-03-29 11:43:58 -06002765 if (SafeModulo(pRegions[i].bufferRowLength, block_size.width) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002766 skip |= log_msg(
2767 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002768 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01271, "IMAGE",
2769 "%s(): pRegion[%d] bufferRowLength (%d) must be a multiple of the compressed image's texel width (%d). %s.",
2770 function, i, pRegions[i].bufferRowLength, block_size.width, validation_error_map[VALIDATION_ERROR_01271]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002771 }
2772
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002773 // BufferRowHeight must be a multiple of block height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002774 if (SafeModulo(pRegions[i].bufferImageHeight, block_size.height) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002775 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 -07002776 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01272, "IMAGE",
2777 "%s(): pRegion[%d] bufferImageHeight (%d) must be a multiple of the compressed image's texel "
2778 "height (%d). %s.",
2779 function, i, pRegions[i].bufferImageHeight, block_size.height,
2780 validation_error_map[VALIDATION_ERROR_01272]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002781 }
2782
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002783 // image offsets must be multiples of block dimensions
Dave Houlton1d2022c2017-03-29 11:43:58 -06002784 if ((SafeModulo(pRegions[i].imageOffset.x, block_size.width) != 0) ||
2785 (SafeModulo(pRegions[i].imageOffset.y, block_size.height) != 0) ||
2786 (SafeModulo(pRegions[i].imageOffset.z, block_size.depth) != 0)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002787 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2788 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01273, "IMAGE",
2789 "%s(): pRegion[%d] imageOffset(x,y) (%d, %d) must be multiples of the compressed image's texel "
2790 "width & height (%d, %d). %s.",
2791 function, i, pRegions[i].imageOffset.x, pRegions[i].imageOffset.y, block_size.width,
2792 block_size.height, validation_error_map[VALIDATION_ERROR_01273]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002793 }
2794
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002795 // bufferOffset must be a multiple of block size (linear bytes)
Dave Houlton1d2022c2017-03-29 11:43:58 -06002796 size_t block_size_in_bytes = FormatSize(image_state->createInfo.format);
2797 if (SafeModulo(pRegions[i].bufferOffset, block_size_in_bytes) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002798 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2799 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01274, "IMAGE",
2800 "%s(): pRegion[%d] bufferOffset (0x%" PRIxLEAST64
2801 ") must be a multiple of the compressed image's texel block "
2802 "size (" PRINTF_SIZE_T_SPECIFIER "). %s.",
2803 function, i, pRegions[i].bufferOffset, block_size_in_bytes,
2804 validation_error_map[VALIDATION_ERROR_01274]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002805 }
Dave Houlton67e9b532017-03-02 17:00:10 -07002806
2807 // imageExtent width must be a multiple of block width, or extent+offset width must equal subresource width
Dave Houlton75967fc2017-03-06 17:21:16 -07002808 VkExtent3D mip_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
Dave Houlton1d2022c2017-03-29 11:43:58 -06002809 if ((SafeModulo(pRegions[i].imageExtent.width, block_size.width) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002810 (pRegions[i].imageExtent.width + pRegions[i].imageOffset.x != mip_extent.width)) {
2811 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2812 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01275, "IMAGE",
2813 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block width "
2814 "(%d), or when added to offset.x (%d) must equal the image subresource width (%d). %s.",
2815 function, i, pRegions[i].imageExtent.width, block_size.width, pRegions[i].imageOffset.x,
2816 mip_extent.width, validation_error_map[VALIDATION_ERROR_01275]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002817 }
2818
2819 // imageExtent height must be a multiple of block height, or extent+offset height must equal subresource height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002820 if ((SafeModulo(pRegions[i].imageExtent.height, block_size.height) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002821 (pRegions[i].imageExtent.height + pRegions[i].imageOffset.y != mip_extent.height)) {
2822 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2823 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01276, "IMAGE",
2824 "%s(): pRegion[%d] extent height (%d) must be a multiple of the compressed texture block height "
2825 "(%d), or when added to offset.y (%d) must equal the image subresource height (%d). %s.",
2826 function, i, pRegions[i].imageExtent.height, block_size.height, pRegions[i].imageOffset.y,
2827 mip_extent.height, validation_error_map[VALIDATION_ERROR_01276]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002828 }
2829
2830 // imageExtent depth must be a multiple of block depth, or extent+offset depth must equal subresource depth
Dave Houlton1d2022c2017-03-29 11:43:58 -06002831 if ((SafeModulo(pRegions[i].imageExtent.depth, block_size.depth) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002832 (pRegions[i].imageExtent.depth + pRegions[i].imageOffset.z != mip_extent.depth)) {
2833 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2834 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01277, "IMAGE",
2835 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block depth "
2836 "(%d), or when added to offset.z (%d) must equal the image subresource depth (%d). %s.",
2837 function, i, pRegions[i].imageExtent.depth, block_size.depth, pRegions[i].imageOffset.z,
2838 mip_extent.depth, validation_error_map[VALIDATION_ERROR_01277]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002839 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002840 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002841 }
2842
2843 return skip;
2844}
2845
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002846static bool ValidateImageBounds(const debug_report_data *report_data, const IMAGE_STATE *image_state, const uint32_t regionCount,
2847 const VkBufferImageCopy *pRegions, const char *func_name, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002848 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002849 const VkImageCreateInfo *image_info = &(image_state->createInfo);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002850
2851 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002852 VkExtent3D extent = pRegions[i].imageExtent;
2853 VkOffset3D offset = pRegions[i].imageOffset;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002854
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002855 if (IsExtentSizeZero(&extent)) // Warn on zero area subresource
2856 {
2857 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2858 (uint64_t)0, __LINE__, IMAGE_ZERO_AREA_SUBREGION, "IMAGE",
2859 "%s: pRegion[%d] imageExtent of {%1d, %1d, %1d} has zero area", func_name, i, extent.width,
2860 extent.height, extent.depth);
2861 }
2862
2863 VkExtent3D image_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
2864
2865 // If we're using a compressed format, valid extent is rounded up to multiple of block size (per 18.1)
Dave Houlton1d2022c2017-03-29 11:43:58 -06002866 if (FormatIsCompressed(image_info->format)) {
2867 auto block_extent = FormatCompressedTexelBlockExtent(image_info->format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002868 if (image_extent.width % block_extent.width) {
2869 image_extent.width += (block_extent.width - (image_extent.width % block_extent.width));
2870 }
2871 if (image_extent.height % block_extent.height) {
2872 image_extent.height += (block_extent.height - (image_extent.height % block_extent.height));
2873 }
2874 if (image_extent.depth % block_extent.depth) {
2875 image_extent.depth += (block_extent.depth - (image_extent.depth % block_extent.depth));
2876 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002877 }
2878
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002879 if (ExceedsBounds(&offset, &extent, &image_extent)) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002880 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 -07002881 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds image bounds. %s.", func_name, i,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002882 validation_error_map[msg_code]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002883 }
2884 }
2885
2886 return skip;
2887}
2888
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002889static inline bool ValidtateBufferBounds(const debug_report_data *report_data, IMAGE_STATE *image_state, BUFFER_STATE *buff_state,
2890 uint32_t regionCount, const VkBufferImageCopy *pRegions, const char *func_name,
2891 UNIQUE_VALIDATION_ERROR_CODE msg_code) {
2892 bool skip = false;
2893
2894 VkDeviceSize buffer_size = buff_state->createInfo.size;
2895
2896 for (uint32_t i = 0; i < regionCount; i++) {
2897 VkExtent3D copy_extent = pRegions[i].imageExtent;
2898
2899 VkDeviceSize buffer_width = (0 == pRegions[i].bufferRowLength ? copy_extent.width : pRegions[i].bufferRowLength);
2900 VkDeviceSize buffer_height = (0 == pRegions[i].bufferImageHeight ? copy_extent.height : pRegions[i].bufferImageHeight);
Dave Houlton1d2022c2017-03-29 11:43:58 -06002901 VkDeviceSize unit_size = FormatSize(image_state->createInfo.format); // size (bytes) of texel or block
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002902
Dave Houltonf3229d52017-02-21 15:59:08 -07002903 // Handle special buffer packing rules for specific depth/stencil formats
2904 if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Dave Houlton1d2022c2017-03-29 11:43:58 -06002905 unit_size = FormatSize(VK_FORMAT_S8_UINT);
Dave Houltonf3229d52017-02-21 15:59:08 -07002906 } else if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
2907 switch (image_state->createInfo.format) {
2908 case VK_FORMAT_D16_UNORM_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06002909 unit_size = FormatSize(VK_FORMAT_D16_UNORM);
Dave Houltonf3229d52017-02-21 15:59:08 -07002910 break;
2911 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06002912 unit_size = FormatSize(VK_FORMAT_D32_SFLOAT);
Dave Houltonf3229d52017-02-21 15:59:08 -07002913 break;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002914 case VK_FORMAT_X8_D24_UNORM_PACK32: // Fall through
Dave Houltonf3229d52017-02-21 15:59:08 -07002915 case VK_FORMAT_D24_UNORM_S8_UINT:
2916 unit_size = 4;
2917 break;
2918 default:
2919 break;
2920 }
2921 }
2922
Dave Houlton1d2022c2017-03-29 11:43:58 -06002923 if (FormatIsCompressed(image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002924 // Switch to texel block units, rounding up for any partially-used blocks
Dave Houlton1d2022c2017-03-29 11:43:58 -06002925 auto block_dim = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002926 buffer_width = (buffer_width + block_dim.width - 1) / block_dim.width;
2927 buffer_height = (buffer_height + block_dim.height - 1) / block_dim.height;
2928
2929 copy_extent.width = (copy_extent.width + block_dim.width - 1) / block_dim.width;
2930 copy_extent.height = (copy_extent.height + block_dim.height - 1) / block_dim.height;
2931 copy_extent.depth = (copy_extent.depth + block_dim.depth - 1) / block_dim.depth;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002932 }
2933
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002934 // Either depth or layerCount may be greater than 1 (not both). This is the number of 'slices' to copy
2935 uint32_t z_copies = std::max(copy_extent.depth, pRegions[i].imageSubresource.layerCount);
2936 if (IsExtentSizeZero(&copy_extent) || (0 == z_copies)) {
2937 // TODO: Issure warning here? Already warned in ValidateImageBounds()...
2938 } else {
2939 // Calculate buffer offset of final copied byte, + 1.
2940 VkDeviceSize max_buffer_offset = (z_copies - 1) * buffer_height * buffer_width; // offset to slice
2941 max_buffer_offset += ((copy_extent.height - 1) * buffer_width) + copy_extent.width; // add row,col
2942 max_buffer_offset *= unit_size; // convert to bytes
2943 max_buffer_offset += pRegions[i].bufferOffset; // add initial offset (bytes)
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002944
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002945 if (buffer_size < max_buffer_offset) {
2946 skip |=
2947 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)0,
2948 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds buffer size of %" PRIu64 " bytes. %s.", func_name,
2949 i, buffer_size, validation_error_map[msg_code]);
2950 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002951 }
2952 }
2953
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002954 return skip;
2955}
2956
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002957bool PreCallValidateCmdCopyImageToBuffer(layer_data *device_data, VkImageLayout srcImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002958 IMAGE_STATE *src_image_state, BUFFER_STATE *dst_buffer_state, uint32_t regionCount,
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002959 const VkBufferImageCopy *pRegions, const char *func_name) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002960 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2961 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, src_image_state, "vkCmdCopyImageToBuffer");
2962
2963 // Validate command buffer state
2964 if (CB_RECORDING != cb_node->state) {
2965 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2966 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01258, "DS",
2967 "Cannot call vkCmdCopyImageToBuffer() on command buffer which is not in recording state. %s.",
2968 validation_error_map[VALIDATION_ERROR_01258]);
2969 } else {
2970 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYIMAGETOBUFFER);
2971 }
2972
2973 // Command pool must support graphics, compute, or transfer operations
2974 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
2975
2976 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
2977 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
2978 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2979 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01259, "DS",
2980 "Cannot call vkCmdCopyImageToBuffer() on a command buffer allocated from a pool without graphics, compute, "
2981 "or transfer capabilities. %s.",
2982 validation_error_map[VALIDATION_ERROR_01259]);
2983 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002984 skip |= ValidateImageBounds(report_data, src_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002985 VALIDATION_ERROR_01245);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002986 skip |= ValidtateBufferBounds(report_data, src_image_state, dst_buffer_state, regionCount, pRegions, "vkCmdCopyImageToBuffer()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002987 VALIDATION_ERROR_01246);
2988
2989 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyImageToBuffer(): srcImage",
2990 VALIDATION_ERROR_01249);
2991 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02537);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002992 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02538);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002993
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002994 // Validate that SRC image & DST buffer have correct usage flags set
2995 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01248,
2996 "vkCmdCopyImageToBuffer()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002997 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01252,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002998 "vkCmdCopyImageToBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002999 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01260);
Tobin Ehlisc8266452017-04-07 12:20:30 -06003000 bool hit_error = false;
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003001 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlisc8266452017-04-07 12:20:30 -06003002 skip |=
3003 VerifyImageLayout(device_data, cb_node, src_image_state, pRegions[i].imageSubresource, srcImageLayout,
3004 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01251, &hit_error);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003005 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, src_image_state, &pRegions[i], i,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06003006 "vkCmdCopyImageToBuffer()");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003007 }
3008 return skip;
3009}
3010
3011void PreCallRecordCmdCopyImageToBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003012 BUFFER_STATE *dst_buffer_state, uint32_t region_count, const VkBufferImageCopy *regions,
3013 VkImageLayout src_image_layout) {
3014 // Make sure that all image slices are updated to correct layout
3015 for (uint32_t i = 0; i < region_count; ++i) {
3016 SetImageLayout(device_data, cb_node, src_image_state, regions[i].imageSubresource, src_image_layout);
3017 }
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003018 // Update bindings between buffer/image and cmd buffer
3019 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003020 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003021
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003022 std::function<bool()> function = [=]() {
3023 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImageToBuffer()");
3024 };
3025 cb_node->validate_functions.push_back(function);
3026 function = [=]() {
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003027 SetBufferMemoryValid(device_data, dst_buffer_state, true);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003028 return false;
3029 };
3030 cb_node->validate_functions.push_back(function);
3031
3032 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGETOBUFFER);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003033}
3034
3035bool PreCallValidateCmdCopyBufferToImage(layer_data *device_data, VkImageLayout dstImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003036 BUFFER_STATE *src_buffer_state, IMAGE_STATE *dst_image_state, uint32_t regionCount,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003037 const VkBufferImageCopy *pRegions, const char *func_name) {
3038 const debug_report_data *report_data = core_validation::GetReportData(device_data);
3039 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, dst_image_state, "vkCmdCopyBufferToImage");
3040
3041 // Validate command buffer state
3042 if (CB_RECORDING != cb_node->state) {
3043 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3044 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01240, "DS",
3045 "Cannot call vkCmdCopyBufferToImage() on command buffer which is not in recording state. %s.",
3046 validation_error_map[VALIDATION_ERROR_01240]);
3047 } else {
3048 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYBUFFERTOIMAGE);
3049 }
3050
3051 // Command pool must support graphics, compute, or transfer operations
3052 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
3053 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
3054 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
3055 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3056 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01241, "DS",
3057 "Cannot call vkCmdCopyBufferToImage() on a command buffer allocated from a pool without graphics, compute, "
3058 "or transfer capabilities. %s.",
3059 validation_error_map[VALIDATION_ERROR_01241]);
3060 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003061 skip |= ValidateImageBounds(report_data, dst_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003062 VALIDATION_ERROR_01228);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003063 skip |= ValidtateBufferBounds(report_data, dst_image_state, src_buffer_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003064 VALIDATION_ERROR_01227);
3065 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyBufferToImage(): dstImage",
3066 VALIDATION_ERROR_01232);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003067 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02535);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003068 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02536);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003069 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01230,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003070 "vkCmdCopyBufferToImage()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
3071 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01231,
3072 "vkCmdCopyBufferToImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003073 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01242);
Tobin Ehlisc8266452017-04-07 12:20:30 -06003074 bool hit_error = false;
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003075 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlisc8266452017-04-07 12:20:30 -06003076 skip |=
3077 VerifyImageLayout(device_data, cb_node, dst_image_state, pRegions[i].imageSubresource, dstImageLayout,
3078 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01234, &hit_error);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003079 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &pRegions[i], i,
3080 "vkCmdCopyBufferToImage()");
3081 }
3082 return skip;
3083}
3084
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003085void PreCallRecordCmdCopyBufferToImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003086 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkBufferImageCopy *regions,
3087 VkImageLayout dst_image_layout) {
3088 // Make sure that all image slices are updated to correct layout
3089 for (uint32_t i = 0; i < region_count; ++i) {
3090 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].imageSubresource, dst_image_layout);
3091 }
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003092 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003093 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003094 std::function<bool()> function = [=]() {
3095 SetImageMemoryValid(device_data, dst_image_state, true);
3096 return false;
3097 };
3098 cb_node->validate_functions.push_back(function);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003099 function = [=]() { return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBufferToImage()"); };
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003100 cb_node->validate_functions.push_back(function);
3101
3102 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFERTOIMAGE);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003103}
Mike Weiblen672b58b2017-02-21 14:32:53 -07003104
3105bool PreCallValidateGetImageSubresourceLayout(layer_data *device_data, VkImage image, const VkImageSubresource *pSubresource) {
3106 const auto report_data = core_validation::GetReportData(device_data);
3107 bool skip = false;
3108 const VkImageAspectFlags sub_aspect = pSubresource->aspectMask;
3109
3110 // VU 00733: The aspectMask member of pSubresource must only have a single bit set
3111 const int num_bits = sizeof(sub_aspect) * CHAR_BIT;
3112 std::bitset<num_bits> aspect_mask_bits(sub_aspect);
3113 if (aspect_mask_bits.count() != 1) {
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_00733, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003116 "vkGetImageSubresourceLayout(): VkImageSubresource.aspectMask must have exactly 1 bit set. %s",
3117 validation_error_map[VALIDATION_ERROR_00733]);
3118 }
3119
3120 IMAGE_STATE *image_entry = GetImageState(device_data, image);
3121 if (!image_entry) {
3122 return skip;
3123 }
3124
3125 // VU 00732: image must have been created with tiling equal to VK_IMAGE_TILING_LINEAR
3126 if (image_entry->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003127 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3128 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00732, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003129 "vkGetImageSubresourceLayout(): Image must have tiling of VK_IMAGE_TILING_LINEAR. %s",
3130 validation_error_map[VALIDATION_ERROR_00732]);
3131 }
3132
3133 // VU 00739: mipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
3134 if (pSubresource->mipLevel >= image_entry->createInfo.mipLevels) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003135 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3136 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00739, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003137 "vkGetImageSubresourceLayout(): pSubresource.mipLevel (%d) must be less than %d. %s",
3138 pSubresource->mipLevel, image_entry->createInfo.mipLevels, validation_error_map[VALIDATION_ERROR_00739]);
3139 }
3140
3141 // VU 00740: arrayLayer must be less than the arrayLayers specified in VkImageCreateInfo when the image was created
3142 if (pSubresource->arrayLayer >= image_entry->createInfo.arrayLayers) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003143 skip |=
3144 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3145 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00740, "IMAGE",
3146 "vkGetImageSubresourceLayout(): pSubresource.arrayLayer (%d) must be less than %d. %s",
3147 pSubresource->arrayLayer, image_entry->createInfo.arrayLayers, validation_error_map[VALIDATION_ERROR_00740]);
Mike Weiblen672b58b2017-02-21 14:32:53 -07003148 }
3149
3150 // VU 00741: subresource's aspect must be compatible with image's format.
3151 const VkFormat img_format = image_entry->createInfo.format;
Dave Houlton1d2022c2017-03-29 11:43:58 -06003152 if (FormatIsColor(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003153 if (sub_aspect != VK_IMAGE_ASPECT_COLOR_BIT) {
3154 skip |= log_msg(
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003155 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3156 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003157 "vkGetImageSubresourceLayout(): For color formats, VkImageSubresource.aspectMask must be VK_IMAGE_ASPECT_COLOR. %s",
3158 validation_error_map[VALIDATION_ERROR_00741]);
3159 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06003160 } else if (FormatIsDepthOrStencil(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003161 if ((sub_aspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (sub_aspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003162 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3163 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003164 "vkGetImageSubresourceLayout(): For depth/stencil formats, VkImageSubresource.aspectMask must be "
3165 "either VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT. %s",
3166 validation_error_map[VALIDATION_ERROR_00741]);
3167 }
3168 }
3169 return skip;
3170}