blob: 0cd9f36f64b739ef53e9173169afb81bfd76c4cd [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) {
Chris Forbes4eab4b02017-04-26 10:21:20 -070035 if (pCB->imageLayoutMap.find(imgpair) != pCB->imageLayoutMap.end()) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -070036 pCB->imageLayoutMap[imgpair].layout = layout;
37 } else {
38 assert(imgpair.hasSubresource);
39 IMAGE_CMD_BUF_LAYOUT_NODE node;
40 if (!FindCmdBufLayout(device_data, pCB, imgpair.image, imgpair.subresource, node)) {
41 node.initialLayout = layout;
42 }
43 SetLayout(device_data, pCB, imgpair, {node.initialLayout, layout});
44 }
45}
46template <class OBJECT, class LAYOUT>
Tobin Ehlis58c884f2017-02-08 12:15:27 -070047void SetLayout(layer_data *device_data, OBJECT *pObject, VkImage image, VkImageSubresource range, const LAYOUT &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -070048 ImageSubresourcePair imgpair = {image, true, range};
49 SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
50 SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
51 SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
52 SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
53}
54
55template <class OBJECT, class LAYOUT>
Tobin Ehlis58c884f2017-02-08 12:15:27 -070056void SetLayout(layer_data *device_data, OBJECT *pObject, ImageSubresourcePair imgpair, const LAYOUT &layout,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -070057 VkImageAspectFlags aspectMask) {
58 if (imgpair.subresource.aspectMask & aspectMask) {
59 imgpair.subresource.aspectMask = aspectMask;
60 SetLayout(device_data, pObject, imgpair, layout);
61 }
62}
63
Tony Barbourdf013b92017-01-25 12:53:48 -070064// Set the layout in supplied map
Tony Barboure0c5cc92017-02-08 13:53:39 -070065void SetLayout(std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
66 VkImageLayout layout) {
Tony Barbourdf013b92017-01-25 12:53:48 -070067 imageLayoutMap[imgpair].layout = layout;
68}
69
Tobin Ehlisc8266452017-04-07 12:20:30 -060070bool FindLayoutVerifyNode(layer_data const *device_data, GLOBAL_CB_NODE const *pCB, ImageSubresourcePair imgpair,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -070071 IMAGE_CMD_BUF_LAYOUT_NODE &node, const VkImageAspectFlags aspectMask) {
72 const debug_report_data *report_data = core_validation::GetReportData(device_data);
73
74 if (!(imgpair.subresource.aspectMask & aspectMask)) {
75 return false;
76 }
77 VkImageAspectFlags oldAspectMask = imgpair.subresource.aspectMask;
78 imgpair.subresource.aspectMask = aspectMask;
79 auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
80 if (imgsubIt == pCB->imageLayoutMap.end()) {
81 return false;
82 }
83 if (node.layout != VK_IMAGE_LAYOUT_MAX_ENUM && node.layout != imgsubIt->second.layout) {
84 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
85 reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
86 "Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple layout types: %s and %s",
87 reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(node.layout),
88 string_VkImageLayout(imgsubIt->second.layout));
89 }
90 if (node.initialLayout != VK_IMAGE_LAYOUT_MAX_ENUM && node.initialLayout != imgsubIt->second.initialLayout) {
91 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
92 reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
93 "Cannot query for VkImage 0x%" PRIx64
94 " layout when combined aspect mask %d has multiple initial layout types: %s and %s",
95 reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(node.initialLayout),
96 string_VkImageLayout(imgsubIt->second.initialLayout));
97 }
98 node = imgsubIt->second;
99 return true;
100}
101
Tobin Ehlisc8266452017-04-07 12:20:30 -0600102bool FindLayoutVerifyLayout(layer_data const *device_data, ImageSubresourcePair imgpair, VkImageLayout &layout,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700103 const VkImageAspectFlags aspectMask) {
104 if (!(imgpair.subresource.aspectMask & aspectMask)) {
105 return false;
106 }
107 const debug_report_data *report_data = core_validation::GetReportData(device_data);
108 VkImageAspectFlags oldAspectMask = imgpair.subresource.aspectMask;
109 imgpair.subresource.aspectMask = aspectMask;
110 auto imgsubIt = (*core_validation::GetImageLayoutMap(device_data)).find(imgpair);
111 if (imgsubIt == (*core_validation::GetImageLayoutMap(device_data)).end()) {
112 return false;
113 }
114 if (layout != VK_IMAGE_LAYOUT_MAX_ENUM && layout != imgsubIt->second.layout) {
115 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
116 reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
117 "Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple layout types: %s and %s",
118 reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(layout),
119 string_VkImageLayout(imgsubIt->second.layout));
120 }
121 layout = imgsubIt->second.layout;
122 return true;
123}
124
125// Find layout(s) on the command buffer level
Tobin Ehlisc8266452017-04-07 12:20:30 -0600126bool FindCmdBufLayout(layer_data const *device_data, GLOBAL_CB_NODE const *pCB, VkImage image, VkImageSubresource range,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700127 IMAGE_CMD_BUF_LAYOUT_NODE &node) {
128 ImageSubresourcePair imgpair = {image, true, range};
129 node = IMAGE_CMD_BUF_LAYOUT_NODE(VK_IMAGE_LAYOUT_MAX_ENUM, VK_IMAGE_LAYOUT_MAX_ENUM);
130 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_COLOR_BIT);
131 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_DEPTH_BIT);
132 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_STENCIL_BIT);
133 FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_METADATA_BIT);
134 if (node.layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
135 imgpair = {image, false, VkImageSubresource()};
136 auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
137 if (imgsubIt == pCB->imageLayoutMap.end()) return false;
138 // TODO: This is ostensibly a find function but it changes state here
139 node = imgsubIt->second;
140 }
141 return true;
142}
143
144// Find layout(s) on the global level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700145bool FindGlobalLayout(layer_data *device_data, ImageSubresourcePair imgpair, VkImageLayout &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700146 layout = VK_IMAGE_LAYOUT_MAX_ENUM;
147 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
148 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
149 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
150 FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
151 if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
152 imgpair = {imgpair.image, false, VkImageSubresource()};
153 auto imgsubIt = (*core_validation::GetImageLayoutMap(device_data)).find(imgpair);
154 if (imgsubIt == (*core_validation::GetImageLayoutMap(device_data)).end()) return false;
155 layout = imgsubIt->second.layout;
156 }
157 return true;
158}
159
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700160bool FindLayouts(layer_data *device_data, VkImage image, std::vector<VkImageLayout> &layouts) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700161 auto sub_data = (*core_validation::GetImageSubresourceMap(device_data)).find(image);
162 if (sub_data == (*core_validation::GetImageSubresourceMap(device_data)).end()) return false;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700163 auto image_state = GetImageState(device_data, image);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700164 if (!image_state) return false;
165 bool ignoreGlobal = false;
166 // TODO: Make this robust for >1 aspect mask. Now it will just say ignore potential errors in this case.
167 if (sub_data->second.size() >= (image_state->createInfo.arrayLayers * image_state->createInfo.mipLevels + 1)) {
168 ignoreGlobal = true;
169 }
170 for (auto imgsubpair : sub_data->second) {
171 if (ignoreGlobal && !imgsubpair.hasSubresource) continue;
172 auto img_data = (*core_validation::GetImageLayoutMap(device_data)).find(imgsubpair);
173 if (img_data != (*core_validation::GetImageLayoutMap(device_data)).end()) {
174 layouts.push_back(img_data->second.layout);
175 }
176 }
177 return true;
178}
Tony Barboure0c5cc92017-02-08 13:53:39 -0700179bool FindLayout(const std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
180 VkImageLayout &layout, const VkImageAspectFlags aspectMask) {
Tony Barbourdf013b92017-01-25 12:53:48 -0700181 if (!(imgpair.subresource.aspectMask & aspectMask)) {
182 return false;
183 }
184 imgpair.subresource.aspectMask = aspectMask;
185 auto imgsubIt = imageLayoutMap.find(imgpair);
186 if (imgsubIt == imageLayoutMap.end()) {
187 return false;
188 }
189 layout = imgsubIt->second.layout;
190 return true;
Tony Barboure0c5cc92017-02-08 13:53:39 -0700191}
Tony Barbourdf013b92017-01-25 12:53:48 -0700192
193// find layout in supplied map
Tony Barboure0c5cc92017-02-08 13:53:39 -0700194bool FindLayout(const std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
195 VkImageLayout &layout) {
Tony Barbourdf013b92017-01-25 12:53:48 -0700196 layout = VK_IMAGE_LAYOUT_MAX_ENUM;
197 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
198 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
199 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
200 FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
201 if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
202 imgpair = {imgpair.image, false, VkImageSubresource()};
203 auto imgsubIt = imageLayoutMap.find(imgpair);
204 if (imgsubIt == imageLayoutMap.end()) return false;
205 layout = imgsubIt->second.layout;
206 }
207 return true;
208}
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700209
210// Set the layout on the global level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700211void SetGlobalLayout(layer_data *device_data, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700212 VkImage &image = imgpair.image;
213 (*core_validation::GetImageLayoutMap(device_data))[imgpair].layout = layout;
214 auto &image_subresources = (*core_validation::GetImageSubresourceMap(device_data))[image];
215 auto subresource = std::find(image_subresources.begin(), image_subresources.end(), imgpair);
216 if (subresource == image_subresources.end()) {
217 image_subresources.push_back(imgpair);
218 }
219}
220
221// Set the layout on the cmdbuf level
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700222void 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 -0700223 pCB->imageLayoutMap[imgpair] = node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700224}
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600225// Set image layout for given VkImageSubresourceRange struct
226void SetImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *image_state,
227 VkImageSubresourceRange image_subresource_range, const VkImageLayout &layout) {
228 assert(image_state);
229 for (uint32_t level_index = 0; level_index < image_subresource_range.levelCount; ++level_index) {
230 uint32_t level = image_subresource_range.baseMipLevel + level_index;
231 for (uint32_t layer_index = 0; layer_index < image_subresource_range.layerCount; layer_index++) {
232 uint32_t layer = image_subresource_range.baseArrayLayer + layer_index;
233 VkImageSubresource sub = {image_subresource_range.aspectMask, level, layer};
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700234 // TODO: If ImageView was created with depth or stencil, transition both layouts as the aspectMask is ignored and both
235 // are used. Verify that the extra implicit layout is OK for descriptor set layout validation
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600236 if (image_subresource_range.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600237 if (FormatIsDepthAndStencil(image_state->createInfo.format)) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700238 sub.aspectMask |= (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
239 }
240 }
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600241 SetLayout(device_data, cb_node, image_state->image, sub, layout);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700242 }
243 }
244}
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600245// Set image layout for given VkImageSubresourceLayers struct
246void SetImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *image_state,
247 VkImageSubresourceLayers image_subresource_layers, const VkImageLayout &layout) {
248 // Transfer VkImageSubresourceLayers into VkImageSubresourceRange struct
249 VkImageSubresourceRange image_subresource_range;
250 image_subresource_range.aspectMask = image_subresource_layers.aspectMask;
251 image_subresource_range.baseArrayLayer = image_subresource_layers.baseArrayLayer;
252 image_subresource_range.layerCount = image_subresource_layers.layerCount;
253 image_subresource_range.baseMipLevel = image_subresource_layers.mipLevel;
254 image_subresource_range.levelCount = 1;
255 SetImageLayout(device_data, cb_node, image_state, image_subresource_range, layout);
256}
257// Set image layout for all slices of an image view
258void SetImageViewLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImageView imageView, const VkImageLayout &layout) {
259 auto view_state = GetImageViewState(device_data, imageView);
260 assert(view_state);
261
262 SetImageLayout(device_data, cb_node, GetImageState(device_data, view_state->create_info.image),
263 view_state->create_info.subresourceRange, layout);
264}
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700265
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700266bool VerifyFramebufferAndRenderPassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700267 const VkRenderPassBeginInfo *pRenderPassBegin,
268 const FRAMEBUFFER_STATE *framebuffer_state) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600269 bool skip = false;
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700270 auto const pRenderPassInfo = GetRenderPassState(device_data, pRenderPassBegin->renderPass)->createInfo.ptr();
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700271 auto const &framebufferInfo = framebuffer_state->createInfo;
272 const auto report_data = core_validation::GetReportData(device_data);
273 if (pRenderPassInfo->attachmentCount != framebufferInfo.attachmentCount) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600274 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
275 reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
276 "You cannot start a render pass using a framebuffer "
277 "with a different number of attachments.");
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700278 }
279 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
280 const VkImageView &image_view = framebufferInfo.pAttachments[i];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700281 auto view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700282 assert(view_state);
283 const VkImage &image = view_state->create_info.image;
284 const VkImageSubresourceRange &subRange = view_state->create_info.subresourceRange;
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700285 auto initial_layout = pRenderPassInfo->pAttachments[i].initialLayout;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700286 // TODO: Do not iterate over every possibility - consolidate where possible
287 for (uint32_t j = 0; j < subRange.levelCount; j++) {
288 uint32_t level = subRange.baseMipLevel + j;
289 for (uint32_t k = 0; k < subRange.layerCount; k++) {
290 uint32_t layer = subRange.baseArrayLayer + k;
291 VkImageSubresource sub = {subRange.aspectMask, level, layer};
292 IMAGE_CMD_BUF_LAYOUT_NODE node;
293 if (!FindCmdBufLayout(device_data, pCB, image, sub, node)) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700294 // Missing layouts will be added during state update
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700295 continue;
296 }
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700297 if (initial_layout != VK_IMAGE_LAYOUT_UNDEFINED && initial_layout != node.layout) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600298 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
299 __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
300 "You cannot start a render pass using attachment %u "
301 "where the render pass initial layout is %s and the previous "
302 "known layout of the attachment is %s. The layouts must match, or "
303 "the render pass initial layout for the attachment must be "
304 "VK_IMAGE_LAYOUT_UNDEFINED",
305 i, string_VkImageLayout(initial_layout), string_VkImageLayout(node.layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700306 }
307 }
308 }
309 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600310 return skip;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700311}
312
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700313void TransitionAttachmentRefLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, FRAMEBUFFER_STATE *pFramebuffer,
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700314 VkAttachmentReference ref) {
315 if (ref.attachment != VK_ATTACHMENT_UNUSED) {
316 auto image_view = pFramebuffer->createInfo.pAttachments[ref.attachment];
317 SetImageViewLayout(device_data, pCB, image_view, ref.layout);
318 }
319}
320
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700321void TransitionSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const RENDER_PASS_STATE *render_pass_state,
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700322 const int subpass_index, FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700323 assert(render_pass_state);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700324
325 if (framebuffer_state) {
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700326 auto const &subpass = render_pass_state->createInfo.pSubpasses[subpass_index];
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700327 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
328 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pInputAttachments[j]);
329 }
330 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
331 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pColorAttachments[j]);
332 }
333 if (subpass.pDepthStencilAttachment) {
334 TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, *subpass.pDepthStencilAttachment);
335 }
336 }
337}
338
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700339bool ValidateImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
340 uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700341 if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
342 return false;
343 }
344 VkImageSubresource sub = {aspect, level, layer};
345 IMAGE_CMD_BUF_LAYOUT_NODE node;
346 if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700347 return false;
348 }
349 bool skip = false;
350 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
351 // TODO: Set memory invalid which is in mem_tracker currently
352 } else if (node.layout != mem_barrier->oldLayout) {
Mark Lobodzinski91c28262017-03-22 13:06:12 -0600353 skip |=
354 log_msg(core_validation::GetReportData(device_data), VK_DEBUG_REPORT_ERROR_BIT_EXT,
355 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__,
356 DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
357 "For image 0x%" PRIxLEAST64 " you cannot transition the layout of aspect %d from %s when current layout is %s.",
358 reinterpret_cast<const uint64_t &>(mem_barrier->image), aspect, string_VkImageLayout(mem_barrier->oldLayout),
359 string_VkImageLayout(node.layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700360 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700361 return skip;
362}
363
Tobin Ehlis0d4274b2017-02-17 15:17:04 -0700364// Transition the layout state for renderpass attachments based on the BeginRenderPass() call. This includes:
365// 1. Transition into initialLayout state
366// 2. Transition from initialLayout to layout used in subpass 0
367void TransitionBeginRenderPassLayouts(layer_data *device_data, GLOBAL_CB_NODE *cb_state, const RENDER_PASS_STATE *render_pass_state,
368 FRAMEBUFFER_STATE *framebuffer_state) {
369 // First transition into initialLayout
370 auto const rpci = render_pass_state->createInfo.ptr();
371 for (uint32_t i = 0; i < rpci->attachmentCount; ++i) {
372 VkImageView image_view = framebuffer_state->createInfo.pAttachments[i];
373 SetImageViewLayout(device_data, cb_state, image_view, rpci->pAttachments[i].initialLayout);
374 }
375 // Now transition for first subpass (index 0)
376 TransitionSubpassLayouts(device_data, cb_state, render_pass_state, 0, framebuffer_state);
377}
378
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700379void TransitionImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
380 uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
381 if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
382 return;
383 }
384 VkImageSubresource sub = {aspect, level, layer};
385 IMAGE_CMD_BUF_LAYOUT_NODE node;
386 if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
387 SetLayout(device_data, pCB, mem_barrier->image, sub,
388 IMAGE_CMD_BUF_LAYOUT_NODE(mem_barrier->oldLayout, mem_barrier->newLayout));
389 return;
390 }
391 if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
392 // TODO: Set memory invalid
393 }
394 SetLayout(device_data, pCB, mem_barrier->image, sub, mem_barrier->newLayout);
395}
396
Dave Houlton10b39482017-03-16 13:18:15 -0600397bool VerifyAspectsPresent(VkImageAspectFlags aspect_mask, VkFormat format) {
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600398 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600399 if (!FormatIsColor(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600400 }
401 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600402 if (!FormatHasDepth(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600403 }
404 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0) {
Dave Houlton1d2022c2017-03-29 11:43:58 -0600405 if (!FormatHasStencil(format)) return false;
Dave Houlton4eaaf3a2017-03-14 11:31:20 -0600406 }
407 return true;
408}
409
Mike Weiblen62d08a32017-03-07 22:18:27 -0700410// Verify an ImageMemoryBarrier's old/new ImageLayouts are compatible with the Image's ImageUsageFlags.
411bool ValidateBarrierLayoutToImageUsage(layer_data *device_data, const VkImageMemoryBarrier *img_barrier, bool new_not_old,
412 VkImageUsageFlags usage_flags, const char *func_name) {
413 const auto report_data = core_validation::GetReportData(device_data);
414 bool skip = false;
415 const VkImageLayout layout = (new_not_old) ? img_barrier->newLayout : img_barrier->oldLayout;
416 UNIQUE_VALIDATION_ERROR_CODE msg_code = VALIDATION_ERROR_UNDEFINED; // sentinel value meaning "no error"
417
418 switch (layout) {
419 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
420 if ((usage_flags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) == 0) {
421 msg_code = VALIDATION_ERROR_00303;
422 }
423 break;
424 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
425 if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
426 msg_code = VALIDATION_ERROR_00304;
427 }
428 break;
429 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
430 if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
431 msg_code = VALIDATION_ERROR_00305;
432 }
433 break;
434 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
435 if ((usage_flags & (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) == 0) {
436 msg_code = VALIDATION_ERROR_00306;
437 }
438 break;
439 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
440 if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) == 0) {
441 msg_code = VALIDATION_ERROR_00307;
442 }
443 break;
444 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
445 if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) == 0) {
446 msg_code = VALIDATION_ERROR_00308;
447 }
448 break;
449 default:
450 // Other VkImageLayout values do not have VUs defined in this context.
451 break;
452 }
453
454 if (msg_code != VALIDATION_ERROR_UNDEFINED) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600455 skip |=
456 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
457 reinterpret_cast<const uint64_t &>(img_barrier->image), __LINE__, msg_code, "DS",
458 "%s: Image barrier 0x%p %sLayout=%s is not compatible with image 0x%" PRIx64 " usage flags 0x%" PRIx32 ". %s",
459 func_name, img_barrier, ((new_not_old) ? "new" : "old"), string_VkImageLayout(layout),
460 reinterpret_cast<const uint64_t &>(img_barrier->image), usage_flags, validation_error_map[msg_code]);
Mike Weiblen62d08a32017-03-07 22:18:27 -0700461 }
462 return skip;
463}
464
465// Verify image barriers are compatible with the images they reference.
466bool ValidateBarriersToImages(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t imageMemoryBarrierCount,
467 const VkImageMemoryBarrier *pImageMemoryBarriers, const char *func_name) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700468 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700469 bool skip = false;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700470
Mike Weiblen62d08a32017-03-07 22:18:27 -0700471 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
472 auto img_barrier = &pImageMemoryBarriers[i];
473 if (!img_barrier) continue;
474
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600475 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, img_barrier->image)->createInfo);
476 uint32_t level_count = ResolveRemainingLevels(&img_barrier->subresourceRange, image_create_info->mipLevels);
477 uint32_t layer_count = ResolveRemainingLayers(&img_barrier->subresourceRange, image_create_info->arrayLayers);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700478
Mike Weiblen62d08a32017-03-07 22:18:27 -0700479 for (uint32_t j = 0; j < level_count; j++) {
480 uint32_t level = img_barrier->subresourceRange.baseMipLevel + j;
481 for (uint32_t k = 0; k < layer_count; k++) {
482 uint32_t layer = img_barrier->subresourceRange.baseArrayLayer + k;
483 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
484 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
485 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
486 skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700487 }
488 }
Mike Weiblen62d08a32017-03-07 22:18:27 -0700489
490 IMAGE_STATE *image_state = GetImageState(device_data, img_barrier->image);
491 if (image_state) {
492 VkImageUsageFlags usage_flags = image_state->createInfo.usage;
493 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, false, usage_flags, func_name);
494 skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, true, usage_flags, func_name);
495 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700496 }
497 return skip;
498}
499
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700500void TransitionImageLayouts(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
501 const VkImageMemoryBarrier *pImgMemBarriers) {
502 GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700503
504 for (uint32_t i = 0; i < memBarrierCount; ++i) {
505 auto mem_barrier = &pImgMemBarriers[i];
506 if (!mem_barrier) continue;
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700507
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600508 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, mem_barrier->image)->createInfo);
509 uint32_t level_count = ResolveRemainingLevels(&mem_barrier->subresourceRange, image_create_info->mipLevels);
510 uint32_t layer_count = ResolveRemainingLayers(&mem_barrier->subresourceRange, image_create_info->arrayLayers);
511
512 for (uint32_t j = 0; j < level_count; j++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700513 uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600514 for (uint32_t k = 0; k < layer_count; k++) {
Mark Lobodzinski6b6c50a2017-02-27 12:56:14 -0700515 uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
516 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
517 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
518 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
519 TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
520 }
521 }
522 }
523}
524
Tobin Ehlisc8266452017-04-07 12:20:30 -0600525bool VerifyImageLayout(layer_data const *device_data, GLOBAL_CB_NODE const *cb_node, IMAGE_STATE *image_state,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600526 VkImageSubresourceLayers subLayers, VkImageLayout explicit_layout, VkImageLayout optimal_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -0600527 const char *caller, UNIQUE_VALIDATION_ERROR_CODE msg_code, bool *error) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700528 const auto report_data = core_validation::GetReportData(device_data);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600529 const auto image = image_state->image;
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600530 bool skip = false;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700531
532 for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
533 uint32_t layer = i + subLayers.baseArrayLayer;
534 VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
535 IMAGE_CMD_BUF_LAYOUT_NODE node;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600536 if (FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
537 if (node.layout != explicit_layout) {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600538 *error = true;
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600539 // TODO: Improve log message in the next pass
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600540 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
541 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
542 "%s: Cannot use image 0x%" PRIxLEAST64
543 " with specific layout %s that doesn't match the actual current layout %s.",
544 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
545 string_VkImageLayout(node.layout));
Tobin Ehlise35b66a2017-03-15 12:18:31 -0600546 }
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700547 }
548 }
Tobin Ehlis2d85ec62017-03-14 15:38:48 -0600549 // If optimal_layout is not UNDEFINED, check that layout matches optimal for this case
550 if ((VK_IMAGE_LAYOUT_UNDEFINED != optimal_layout) && (explicit_layout != optimal_layout)) {
551 if (VK_IMAGE_LAYOUT_GENERAL == explicit_layout) {
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700552 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
553 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600554 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
555 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(cb_node->commandBuffer),
556 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
557 "%s: For optimal performance image 0x%" PRIxLEAST64 " layout should be %s instead of GENERAL.",
558 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(optimal_layout));
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700559 }
560 } else {
Tobin Ehlisc8266452017-04-07 12:20:30 -0600561 *error = true;
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600562 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
563 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, msg_code, "DS",
564 "%s: Layout for image 0x%" PRIxLEAST64 " is %s but can only be %s or VK_IMAGE_LAYOUT_GENERAL. %s",
565 caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
566 string_VkImageLayout(optimal_layout), validation_error_map[msg_code]);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700567 }
568 }
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600569 return skip;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700570}
571
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700572void TransitionFinalSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkRenderPassBeginInfo *pRenderPassBegin,
573 FRAMEBUFFER_STATE *framebuffer_state) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700574 auto renderPass = GetRenderPassState(device_data, pRenderPassBegin->renderPass);
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700575 if (!renderPass) return;
576
577 const VkRenderPassCreateInfo *pRenderPassInfo = renderPass->createInfo.ptr();
578 if (framebuffer_state) {
579 for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
580 auto image_view = framebuffer_state->createInfo.pAttachments[i];
581 SetImageViewLayout(device_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
582 }
583 }
584}
585
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700586bool PreCallValidateCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo,
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700587 const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600588 bool skip = false;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700589 const debug_report_data *report_data = core_validation::GetReportData(device_data);
590
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600591 if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600592 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
593 VALIDATION_ERROR_00715, "IMAGE", "vkCreateImage: VkFormat for image must not be VK_FORMAT_UNDEFINED. %s",
594 validation_error_map[VALIDATION_ERROR_00715]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600595
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600596 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600597 }
598
599 const VkFormatProperties *properties = GetFormatProperties(device_data, pCreateInfo->format);
600
601 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) && (properties->linearTilingFeatures == 0)) {
602 std::stringstream ss;
603 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600604 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
605 VALIDATION_ERROR_02150, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02150]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600606
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600607 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600608 }
609
610 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) && (properties->optimalTilingFeatures == 0)) {
611 std::stringstream ss;
612 ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600613 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
614 VALIDATION_ERROR_02155, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02155]);
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600615
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600616 return skip;
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600617 }
618
619 // Validate that format supports usage as color attachment
620 if (pCreateInfo->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
621 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
622 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
623 std::stringstream ss;
624 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
625 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600626 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600627 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
628 VALIDATION_ERROR_02158, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02158]);
629 }
630 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
631 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
632 std::stringstream ss;
633 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
634 << ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600635 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600636 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
637 VALIDATION_ERROR_02153, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02153]);
638 }
639 }
640
641 // Validate that format supports usage as depth/stencil attachment
642 if (pCreateInfo->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
643 if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
644 ((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
645 std::stringstream ss;
646 ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
647 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600648 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600649 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
650 VALIDATION_ERROR_02159, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02159]);
651 }
652 if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
653 ((properties->linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
654 std::stringstream ss;
655 ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
656 << ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600657 skip |=
Jeremy Hayes96dcd812017-03-14 14:04:19 -0600658 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
659 VALIDATION_ERROR_02154, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02154]);
660 }
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700661 }
662
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700663 const VkImageFormatProperties *ImageFormatProperties = GetImageFormatProperties(
664 device_data, pCreateInfo->format, pCreateInfo->imageType, pCreateInfo->tiling, pCreateInfo->usage, pCreateInfo->flags);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700665
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700666 VkDeviceSize imageGranularity = GetPhysicalDeviceProperties(device_data)->limits.bufferImageGranularity;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700667 imageGranularity = imageGranularity == 1 ? 0 : imageGranularity;
Tobin Ehlisa55b1d42017-04-04 12:23:48 -0600668 // TODO : This is also covering 2918 & 2919. Break out into separate checks
Mark Lobodzinski688ed322017-01-27 11:13:21 -0700669 if ((pCreateInfo->extent.width <= 0) || (pCreateInfo->extent.height <= 0) || (pCreateInfo->extent.depth <= 0)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600670 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
671 VALIDATION_ERROR_02917, "Image",
672 "CreateImage extent is 0 for at least one required dimension for image: "
673 "Width = %d Height = %d Depth = %d. %s",
674 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
675 validation_error_map[VALIDATION_ERROR_02917]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700676 }
677
678 // TODO: VALIDATION_ERROR_02125 VALIDATION_ERROR_02126 VALIDATION_ERROR_02128 VALIDATION_ERROR_00720
679 // All these extent-related VUs should be checked here
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700680 if ((pCreateInfo->extent.depth > ImageFormatProperties->maxExtent.depth) ||
681 (pCreateInfo->extent.width > ImageFormatProperties->maxExtent.width) ||
682 (pCreateInfo->extent.height > ImageFormatProperties->maxExtent.height)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600683 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
684 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
685 "CreateImage extents exceed allowable limits for format: "
686 "Width = %d Height = %d Depth = %d: Limits for Width = %d Height = %d Depth = %d for format %s.",
687 pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
688 ImageFormatProperties->maxExtent.width, ImageFormatProperties->maxExtent.height,
689 ImageFormatProperties->maxExtent.depth, string_VkFormat(pCreateInfo->format));
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700690 }
691
692 uint64_t totalSize = ((uint64_t)pCreateInfo->extent.width * (uint64_t)pCreateInfo->extent.height *
693 (uint64_t)pCreateInfo->extent.depth * (uint64_t)pCreateInfo->arrayLayers *
Dave Houlton1d2022c2017-03-29 11:43:58 -0600694 (uint64_t)pCreateInfo->samples * (uint64_t)FormatSize(pCreateInfo->format) +
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700695 (uint64_t)imageGranularity) &
696 ~(uint64_t)imageGranularity;
697
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700698 if (totalSize > ImageFormatProperties->maxResourceSize) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600699 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
700 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
701 "CreateImage resource size exceeds allowable maximum "
702 "Image resource size = 0x%" PRIxLEAST64 ", maximum resource size = 0x%" PRIxLEAST64 " ",
703 totalSize, ImageFormatProperties->maxResourceSize);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700704 }
705
706 // TODO: VALIDATION_ERROR_02132
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700707 if (pCreateInfo->mipLevels > ImageFormatProperties->maxMipLevels) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600708 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
709 IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
710 "CreateImage mipLevels=%d exceeds allowable maximum supported by format of %d", pCreateInfo->mipLevels,
711 ImageFormatProperties->maxMipLevels);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700712 }
713
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700714 if (pCreateInfo->arrayLayers > ImageFormatProperties->maxArrayLayers) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600715 skip |= log_msg(
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700716 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__, VALIDATION_ERROR_02133,
717 "Image", "CreateImage arrayLayers=%d exceeds allowable maximum supported by format of %d. %s", pCreateInfo->arrayLayers,
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700718 ImageFormatProperties->maxArrayLayers, validation_error_map[VALIDATION_ERROR_02133]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700719 }
720
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -0700721 if ((pCreateInfo->samples & ImageFormatProperties->sampleCounts) == 0) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600722 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
723 VALIDATION_ERROR_02138, "Image", "CreateImage samples %s is not supported by format 0x%.8X. %s",
724 string_VkSampleCountFlagBits(pCreateInfo->samples), ImageFormatProperties->sampleCounts,
725 validation_error_map[VALIDATION_ERROR_02138]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700726 }
727
728 if (pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_UNDEFINED && pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_PREINITIALIZED) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600729 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
730 VALIDATION_ERROR_00731, "Image",
731 "vkCreateImage parameter, pCreateInfo->initialLayout, must be VK_IMAGE_LAYOUT_UNDEFINED or "
732 "VK_IMAGE_LAYOUT_PREINITIALIZED. %s",
733 validation_error_map[VALIDATION_ERROR_00731]);
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700734 }
735
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600736 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600737 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
738 VALIDATION_ERROR_02143, "DS",
739 "vkCreateImage(): the sparseBinding device feature is disabled: Images cannot be created with the "
740 "VK_IMAGE_CREATE_SPARSE_BINDING_BIT set. %s",
741 validation_error_map[VALIDATION_ERROR_02143]);
Mark Lobodzinski847b60c2017-03-13 09:32:45 -0600742 }
743
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600744 if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600745 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
746 DRAWSTATE_INVALID_FEATURE, "DS",
747 "vkCreateImage(): the sparseResidencyAliased device feature is disabled: Images cannot be created with the "
748 "VK_IMAGE_CREATE_SPARSE_ALIASED_BIT set.");
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -0600749 }
750
Mark Lobodzinskibdc3b022017-04-24 09:11:35 -0600751 return skip;
Mark Lobodzinski90224de2017-01-26 15:23:11 -0700752}
753
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700754void PostCallRecordCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo, VkImage *pImage) {
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700755 IMAGE_LAYOUT_NODE image_state;
756 image_state.layout = pCreateInfo->initialLayout;
757 image_state.format = pCreateInfo->format;
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700758 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 -0700759 ImageSubresourcePair subpair{*pImage, false, VkImageSubresource()};
Mark Lobodzinski214144a2017-01-27 14:25:32 -0700760 (*core_validation::GetImageSubresourceMap(device_data))[*pImage].push_back(subpair);
761 (*core_validation::GetImageLayoutMap(device_data))[subpair] = image_state;
Mark Lobodzinski42fe5f72017-01-11 11:36:16 -0700762}
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700763
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700764bool PreCallValidateDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE **image_state, VK_OBJECT *obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700765 const CHECK_DISABLED *disabled = core_validation::GetDisables(device_data);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700766 *image_state = core_validation::GetImageState(device_data, image);
Mark Lobodzinski33826372017-04-13 11:10:11 -0600767 *obj_struct = {reinterpret_cast<uint64_t &>(image), kVulkanObjectTypeImage };
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700768 if (disabled->destroy_image) return false;
769 bool skip = false;
770 if (*image_state) {
771 skip |= core_validation::ValidateObjectNotInUse(device_data, *image_state, *obj_struct, VALIDATION_ERROR_00743);
772 }
773 return skip;
774}
775
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700776void PostCallRecordDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE *image_state, VK_OBJECT obj_struct) {
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700777 core_validation::invalidateCommandBuffers(device_data, image_state->cb_bindings, obj_struct);
778 // Clean up memory mapping, bindings and range references for image
779 for (auto mem_binding : image_state->GetBoundMemory()) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700780 auto mem_info = core_validation::GetMemObjInfo(device_data, mem_binding);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700781 if (mem_info) {
782 core_validation::RemoveImageMemoryRange(obj_struct.handle, mem_info);
783 }
784 }
Mark Lobodzinski33826372017-04-13 11:10:11 -0600785 core_validation::ClearMemoryObjectBindings(device_data, obj_struct.handle, kVulkanObjectTypeImage);
Mark Lobodzinski9ef5d562017-01-27 12:28:30 -0700786 // Remove image from imageMap
787 core_validation::GetImageMap(device_data)->erase(image);
788 std::unordered_map<VkImage, std::vector<ImageSubresourcePair>> *imageSubresourceMap =
789 core_validation::GetImageSubresourceMap(device_data);
790
791 const auto &sub_entry = imageSubresourceMap->find(image);
792 if (sub_entry != imageSubresourceMap->end()) {
793 for (const auto &pair : sub_entry->second) {
794 core_validation::GetImageLayoutMap(device_data)->erase(pair);
795 }
796 imageSubresourceMap->erase(sub_entry);
797 }
798}
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700799
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700800bool ValidateImageAttributes(layer_data *device_data, IMAGE_STATE *image_state, VkImageSubresourceRange range) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700801 bool skip = false;
802 const debug_report_data *report_data = core_validation::GetReportData(device_data);
803
804 if (range.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) {
805 char const str[] = "vkCmdClearColorImage aspectMasks for all subresource ranges must be set to VK_IMAGE_ASPECT_COLOR_BIT";
806 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
807 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
808 }
809
Dave Houlton1d2022c2017-03-29 11:43:58 -0600810 if (FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700811 char const str[] = "vkCmdClearColorImage called with depth/stencil image.";
812 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
813 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
814 validation_error_map[VALIDATION_ERROR_01088]);
Dave Houlton1d2022c2017-03-29 11:43:58 -0600815 } else if (FormatIsCompressed(image_state->createInfo.format)) {
Mark Lobodzinskic409a582017-01-27 15:16:01 -0700816 char const str[] = "vkCmdClearColorImage called with compressed image.";
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__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
819 validation_error_map[VALIDATION_ERROR_01088]);
820 }
821
822 if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
823 char const str[] = "vkCmdClearColorImage called with image created without VK_IMAGE_USAGE_TRANSFER_DST_BIT.";
824 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
825 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01084, "IMAGE", "%s. %s", str,
826 validation_error_map[VALIDATION_ERROR_01084]);
827 }
828 return skip;
829}
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700830
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600831uint32_t ResolveRemainingLevels(const VkImageSubresourceRange *range, uint32_t mip_levels) {
832 // Return correct number of mip levels taking into account VK_REMAINING_MIP_LEVELS
833 uint32_t mip_level_count = range->levelCount;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700834 if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600835 mip_level_count = mip_levels - range->baseMipLevel;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700836 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600837 return mip_level_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700838}
839
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600840uint32_t ResolveRemainingLayers(const VkImageSubresourceRange *range, uint32_t layers) {
841 // Return correct number of layers taking into account VK_REMAINING_ARRAY_LAYERS
842 uint32_t array_layer_count = range->layerCount;
843 if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
844 array_layer_count = layers - range->baseArrayLayer;
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700845 }
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600846 return array_layer_count;
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700847}
848
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700849bool VerifyClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700850 VkImageSubresourceRange range, VkImageLayout dest_image_layout, const char *func_name) {
851 bool skip = false;
852 const debug_report_data *report_data = core_validation::GetReportData(device_data);
853
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600854 uint32_t level_count = ResolveRemainingLevels(&range, image_state->createInfo.mipLevels);
855 uint32_t layer_count = ResolveRemainingLayers(&range, image_state->createInfo.arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700856
857 if (dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
858 if (dest_image_layout == VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700859 if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
860 // LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600861 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
862 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700863 "%s: Layout for cleared image should be TRANSFER_DST_OPTIMAL instead of GENERAL.", func_name);
864 }
865 } else {
866 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01086;
867 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
868 error_code = VALIDATION_ERROR_01101;
869 } else {
870 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
871 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -0600872 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
873 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, error_code, "DS",
874 "%s: Layout for cleared image is %s but can only be "
875 "TRANSFER_DST_OPTIMAL or GENERAL. %s",
876 func_name, string_VkImageLayout(dest_image_layout), validation_error_map[error_code]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700877 }
878 }
879
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600880 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
881 uint32_t level = level_index + range.baseMipLevel;
882 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
883 uint32_t layer = layer_index + range.baseArrayLayer;
884 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700885 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700886 if (FindCmdBufLayout(device_data, cb_node, image_state->image, sub, node)) {
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700887 if (node.layout != dest_image_layout) {
888 UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01085;
889 if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
890 error_code = VALIDATION_ERROR_01100;
891 } else {
892 assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
893 }
894 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
895 __LINE__, error_code, "DS",
896 "%s: Cannot clear an image whose layout is %s and "
897 "doesn't match the current layout %s. %s",
898 func_name, string_VkImageLayout(dest_image_layout), string_VkImageLayout(node.layout),
899 validation_error_map[error_code]);
900 }
901 }
902 }
903 }
904
905 return skip;
906}
907
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700908void RecordClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage image, VkImageSubresourceRange range,
909 VkImageLayout dest_image_layout) {
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600910 VkImageCreateInfo *image_create_info = &(GetImageState(device_data, image)->createInfo);
911 uint32_t level_count = ResolveRemainingLevels(&range, image_create_info->mipLevels);
912 uint32_t layer_count = ResolveRemainingLayers(&range, image_create_info->arrayLayers);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700913
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -0600914 for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
915 uint32_t level = level_index + range.baseMipLevel;
916 for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
917 uint32_t layer = layer_index + range.baseArrayLayer;
918 VkImageSubresource sub = {range.aspectMask, level, layer};
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700919 IMAGE_CMD_BUF_LAYOUT_NODE node;
Mark Lobodzinski3c0f6362017-02-01 13:35:48 -0700920 if (!FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
921 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 -0700922 }
923 }
924 }
925}
926
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700927bool PreCallValidateCmdClearColorImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700928 VkImageLayout imageLayout, uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
929 bool skip = false;
930 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700931 auto cb_node = GetCBNode(dev_data, commandBuffer);
932 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700933 if (cb_node && image_state) {
934 skip |= ValidateMemoryIsBoundToImage(dev_data, image_state, "vkCmdClearColorImage()", VALIDATION_ERROR_02527);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700935 skip |= ValidateCmdQueueFlags(dev_data, cb_node, "vkCmdClearColorImage()", VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT,
936 VALIDATION_ERROR_01095);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700937 skip |= ValidateCmd(dev_data, cb_node, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
938 skip |= insideRenderPass(dev_data, cb_node, "vkCmdClearColorImage()", VALIDATION_ERROR_01096);
939 for (uint32_t i = 0; i < rangeCount; ++i) {
940 skip |= ValidateImageAttributes(dev_data, image_state, pRanges[i]);
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700941 skip |= VerifyClearImageLayout(dev_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearColorImage()");
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700942 }
943 }
944 return skip;
945}
946
947// This state recording routine is shared between ClearColorImage and ClearDepthStencilImage
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700948void PreCallRecordCmdClearImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
949 uint32_t rangeCount, const VkImageSubresourceRange *pRanges, CMD_TYPE cmd_type) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700950 auto cb_node = GetCBNode(dev_data, commandBuffer);
951 auto image_state = GetImageState(dev_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700952 if (cb_node && image_state) {
953 AddCommandBufferBindingImage(dev_data, cb_node, image_state);
954 std::function<bool()> function = [=]() {
955 SetImageMemoryValid(dev_data, image_state, true);
956 return false;
957 };
958 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700959 core_validation::UpdateCmdBufferLastCmd(cb_node, cmd_type);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700960 for (uint32_t i = 0; i < rangeCount; ++i) {
961 RecordClearImageLayout(dev_data, cb_node, image, pRanges[i], imageLayout);
962 }
963 }
964}
965
Tobin Ehlis58c884f2017-02-08 12:15:27 -0700966bool PreCallValidateCmdClearDepthStencilImage(layer_data *device_data, VkCommandBuffer commandBuffer, VkImage image,
967 VkImageLayout imageLayout, uint32_t rangeCount,
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700968 const VkImageSubresourceRange *pRanges) {
969 bool skip = false;
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700970 const debug_report_data *report_data = core_validation::GetReportData(device_data);
971
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700972 // TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -0700973 auto cb_node = GetCBNode(device_data, commandBuffer);
974 auto image_state = GetImageState(device_data, image);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700975 if (cb_node && image_state) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700976 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_02528);
Mike Schuchardt9c582402017-02-23 15:57:37 -0700977 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearDepthStencilImage()", VK_QUEUE_GRAPHICS_BIT,
978 VALIDATION_ERROR_01110);
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700979 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
980 skip |= insideRenderPass(device_data, cb_node, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_01111);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700981 for (uint32_t i = 0; i < rangeCount; ++i) {
Mark Lobodzinski9c93dbd2017-02-02 08:31:18 -0700982 skip |=
983 VerifyClearImageLayout(device_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearDepthStencilImage()");
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700984 // Image aspect must be depth or stencil or both
985 if (((pRanges[i].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
986 ((pRanges[i].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
987 char const str[] =
988 "vkCmdClearDepthStencilImage aspectMasks for all subresource ranges must be "
989 "set to VK_IMAGE_ASPECT_DEPTH_BIT and/or VK_IMAGE_ASPECT_STENCIL_BIT";
990 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
991 (uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
992 }
993 }
Dave Houlton1d2022c2017-03-29 11:43:58 -0600994 if (image_state && !FormatIsDepthOrStencil(image_state->createInfo.format)) {
Mark Lobodzinski1241a312017-02-01 10:57:21 -0700995 char const str[] = "vkCmdClearDepthStencilImage called without a depth/stencil image.";
996 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
997 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_01103, "IMAGE", "%s. %s", str,
998 validation_error_map[VALIDATION_ERROR_01103]);
Mark Lobodzinskid81d1012017-02-01 09:03:06 -0700999 }
1000 }
1001 return skip;
1002}
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001003
1004// Returns true if [x, xoffset] and [y, yoffset] overlap
1005static bool RangesIntersect(int32_t start, uint32_t start_offset, int32_t end, uint32_t end_offset) {
1006 bool result = false;
1007 uint32_t intersection_min = std::max(static_cast<uint32_t>(start), static_cast<uint32_t>(end));
1008 uint32_t intersection_max = std::min(static_cast<uint32_t>(start) + start_offset, static_cast<uint32_t>(end) + end_offset);
1009
1010 if (intersection_max > intersection_min) {
1011 result = true;
1012 }
1013 return result;
1014}
1015
1016// Returns true if two VkImageCopy structures overlap
1017static bool RegionIntersects(const VkImageCopy *src, const VkImageCopy *dst, VkImageType type) {
1018 bool result = false;
1019 if ((src->srcSubresource.mipLevel == dst->dstSubresource.mipLevel) &&
1020 (RangesIntersect(src->srcSubresource.baseArrayLayer, src->srcSubresource.layerCount, dst->dstSubresource.baseArrayLayer,
1021 dst->dstSubresource.layerCount))) {
1022 result = true;
1023 switch (type) {
1024 case VK_IMAGE_TYPE_3D:
1025 result &= RangesIntersect(src->srcOffset.z, src->extent.depth, dst->dstOffset.z, dst->extent.depth);
1026 // Intentionally fall through to 2D case
1027 case VK_IMAGE_TYPE_2D:
1028 result &= RangesIntersect(src->srcOffset.y, src->extent.height, dst->dstOffset.y, dst->extent.height);
1029 // Intentionally fall through to 1D case
1030 case VK_IMAGE_TYPE_1D:
1031 result &= RangesIntersect(src->srcOffset.x, src->extent.width, dst->dstOffset.x, dst->extent.width);
1032 break;
1033 default:
1034 // Unrecognized or new IMAGE_TYPE enums will be caught in parameter_validation
1035 assert(false);
1036 }
1037 }
1038 return result;
1039}
1040
1041// Returns true if offset and extent exceed image extents
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001042static bool ExceedsBounds(const VkOffset3D *offset, const VkExtent3D *extent, const VkExtent3D *image_extent) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001043 bool result = false;
1044 // Extents/depths cannot be negative but checks left in for clarity
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001045 if ((offset->z + extent->depth > image_extent->depth) || (offset->z < 0) ||
1046 ((offset->z + static_cast<int32_t>(extent->depth)) < 0)) {
1047 result = true;
1048 }
1049 if ((offset->y + extent->height > image_extent->height) || (offset->y < 0) ||
1050 ((offset->y + static_cast<int32_t>(extent->height)) < 0)) {
1051 result = true;
1052 }
1053 if ((offset->x + extent->width > image_extent->width) || (offset->x < 0) ||
1054 ((offset->x + static_cast<int32_t>(extent->width)) < 0)) {
1055 result = true;
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001056 }
1057 return result;
1058}
1059
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001060// Test if two VkExtent3D structs are equivalent
1061static inline bool IsExtentEqual(const VkExtent3D *extent, const VkExtent3D *other_extent) {
1062 bool result = true;
1063 if ((extent->width != other_extent->width) || (extent->height != other_extent->height) ||
1064 (extent->depth != other_extent->depth)) {
1065 result = false;
1066 }
1067 return result;
1068}
1069
1070// Returns the image extent of a specific subresource.
1071static inline VkExtent3D GetImageSubresourceExtent(const IMAGE_STATE *img, const VkImageSubresourceLayers *subresource) {
1072 const uint32_t mip = subresource->mipLevel;
1073 VkExtent3D extent = img->createInfo.extent;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001074 // Don't allow mip adjustment to create 0 dim, but pass along a 0 if that's what subresource specified
1075 extent.width = (0 == extent.width ? 0 : std::max(1U, extent.width >> mip));
1076 extent.height = (0 == extent.height ? 0 : std::max(1U, extent.height >> mip));
1077 extent.depth = (0 == extent.depth ? 0 : std::max(1U, extent.depth >> mip));
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001078 return extent;
1079}
1080
1081// Test if the extent argument has all dimensions set to 0.
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001082static inline bool IsExtentAllZeroes(const VkExtent3D *extent) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001083 return ((extent->width == 0) && (extent->height == 0) && (extent->depth == 0));
1084}
1085
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001086// Test if the extent argument has any dimensions set to 0.
1087static inline bool IsExtentSizeZero(const VkExtent3D *extent) {
1088 return ((extent->width == 0) || (extent->height == 0) || (extent->depth == 0));
1089}
1090
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001091// Returns the image transfer granularity for a specific image scaled by compressed block size if necessary.
1092static inline VkExtent3D GetScaledItg(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *img) {
1093 // Default to (0, 0, 0) granularity in case we can't find the real granularity for the physical device.
1094 VkExtent3D granularity = {0, 0, 0};
1095 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
1096 if (pPool) {
1097 granularity =
1098 GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].minImageTransferGranularity;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001099 if (FormatIsCompressed(img->createInfo.format)) {
1100 auto block_size = FormatCompressedTexelBlockExtent(img->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001101 granularity.width *= block_size.width;
1102 granularity.height *= block_size.height;
1103 }
1104 }
1105 return granularity;
1106}
1107
1108// Test elements of a VkExtent3D structure against alignment constraints contained in another VkExtent3D structure
1109static inline bool IsExtentAligned(const VkExtent3D *extent, const VkExtent3D *granularity) {
1110 bool valid = true;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001111 if ((SafeModulo(extent->depth, granularity->depth) != 0) || (SafeModulo(extent->width, granularity->width) != 0) ||
1112 (SafeModulo(extent->height, granularity->height) != 0)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001113 valid = false;
1114 }
1115 return valid;
1116}
1117
1118// Check elements of a VkOffset3D structure against a queue family's Image Transfer Granularity values
1119static inline bool CheckItgOffset(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkOffset3D *offset,
1120 const VkExtent3D *granularity, const uint32_t i, const char *function, const char *member) {
1121 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1122 bool skip = false;
1123 VkExtent3D offset_extent = {};
1124 offset_extent.width = static_cast<uint32_t>(abs(offset->x));
1125 offset_extent.height = static_cast<uint32_t>(abs(offset->y));
1126 offset_extent.depth = static_cast<uint32_t>(abs(offset->z));
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001127 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001128 // 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 -07001129 if (IsExtentAllZeroes(&offset_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001130 skip |=
1131 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1132 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1133 "%s: pRegion[%d].%s (x=%d, y=%d, z=%d) must be (x=0, y=0, z=0) "
1134 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1135 function, i, member, offset->x, offset->y, offset->z);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001136 }
1137 } else {
1138 // If the queue family image transfer granularity is not (0, 0, 0), then the offset dimensions must always be even
1139 // integer multiples of the image transfer granularity.
1140 if (IsExtentAligned(&offset_extent, granularity) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001141 skip |= log_msg(
1142 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) dimensions must be even integer "
1145 "multiples of this command buffer's queue family image transfer granularity (w=%d, h=%d, d=%d).",
1146 function, i, member, offset->x, offset->y, offset->z, granularity->width, granularity->height, granularity->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001147 }
1148 }
1149 return skip;
1150}
1151
1152// Check elements of a VkExtent3D structure against a queue family's Image Transfer Granularity values
1153static inline bool CheckItgExtent(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkExtent3D *extent,
1154 const VkOffset3D *offset, const VkExtent3D *granularity, const VkExtent3D *subresource_extent,
1155 const uint32_t i, const char *function, const char *member) {
1156 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1157 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07001158 if (IsExtentAllZeroes(granularity)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001159 // If the queue family image transfer granularity is (0, 0, 0), then the extent must always match the image
1160 // subresource extent.
1161 if (IsExtentEqual(extent, subresource_extent) == false) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001162 skip |=
1163 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1164 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
1165 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d) "
1166 "when the command buffer's queue family image transfer granularity is (w=0, h=0, d=0).",
1167 function, i, member, extent->width, extent->height, extent->depth, subresource_extent->width,
1168 subresource_extent->height, subresource_extent->depth);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001169 }
1170 } else {
1171 // If the queue family image transfer granularity is not (0, 0, 0), then the extent dimensions must always be even
1172 // integer multiples of the image transfer granularity or the offset + extent dimensions must always match the image
1173 // subresource extent dimensions.
1174 VkExtent3D offset_extent_sum = {};
1175 offset_extent_sum.width = static_cast<uint32_t>(abs(offset->x)) + extent->width;
1176 offset_extent_sum.height = static_cast<uint32_t>(abs(offset->y)) + extent->height;
1177 offset_extent_sum.depth = static_cast<uint32_t>(abs(offset->z)) + extent->depth;
1178 if ((IsExtentAligned(extent, granularity) == false) && (IsExtentEqual(&offset_extent_sum, subresource_extent) == false)) {
1179 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001180 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1181 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001182 "%s: pRegion[%d].%s (w=%d, h=%d, d=%d) dimensions must be even integer multiples of this command buffer's "
1183 "queue family image transfer granularity (w=%d, h=%d, d=%d) or offset (x=%d, y=%d, z=%d) + "
1184 "extent (w=%d, h=%d, d=%d) must match the image subresource extents (w=%d, h=%d, d=%d).",
1185 function, i, member, extent->width, extent->height, extent->depth, granularity->width, granularity->height,
1186 granularity->depth, offset->x, offset->y, offset->z, extent->width, extent->height, extent->depth,
1187 subresource_extent->width, subresource_extent->height, subresource_extent->depth);
1188 }
1189 }
1190 return skip;
1191}
1192
1193// Check a uint32_t width or stride value against a queue family's Image Transfer Granularity width value
1194static inline bool CheckItgInt(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const uint32_t value,
1195 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1196 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1197
1198 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001199 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001200 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1201 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001202 "%s: pRegion[%d].%s (%d) must be an even integer multiple of this command buffer's queue family image "
1203 "transfer granularity width (%d).",
1204 function, i, member, value, granularity);
1205 }
1206 return skip;
1207}
1208
1209// Check a VkDeviceSize value against a queue family's Image Transfer Granularity width value
1210static inline bool CheckItgSize(layer_data *device_data, const GLOBAL_CB_NODE *cb_node, const VkDeviceSize value,
1211 const uint32_t granularity, const uint32_t i, const char *function, const char *member) {
1212 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1213 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001214 if (SafeModulo(value, granularity) != 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001215 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1216 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_IMAGE_TRANSFER_GRANULARITY, "DS",
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001217 "%s: pRegion[%d].%s (%" PRIdLEAST64
1218 ") must be an even integer multiple of this command buffer's queue family image transfer "
1219 "granularity width (%d).",
1220 function, i, member, value, granularity);
1221 }
1222 return skip;
1223}
1224
1225// Check valid usage Image Tranfer Granularity requirements for elements of a VkBufferImageCopy structure
1226bool ValidateCopyBufferImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1227 const IMAGE_STATE *img, const VkBufferImageCopy *region,
1228 const uint32_t i, const char *function) {
1229 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06001230 if (FormatIsCompressed(img->createInfo.format) == true) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001231 // TODO: Add granularity checking for compressed formats
1232
1233 // bufferRowLength must be a multiple of the compressed texel block width
1234 // bufferImageHeight must be a multiple of the compressed texel block height
1235 // all members of imageOffset must be a multiple of the corresponding dimensions of the compressed texel block
1236 // bufferOffset must be a multiple of the compressed texel block size in bytes
1237 // imageExtent.width must be a multiple of the compressed texel block width or (imageExtent.width + imageOffset.x)
1238 // must equal the image subresource width
1239 // imageExtent.height must be a multiple of the compressed texel block height or (imageExtent.height + imageOffset.y)
1240 // must equal the image subresource height
1241 // imageExtent.depth must be a multiple of the compressed texel block depth or (imageExtent.depth + imageOffset.z)
1242 // must equal the image subresource depth
1243 } else {
1244 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1245 skip |= CheckItgSize(device_data, cb_node, region->bufferOffset, granularity.width, i, function, "bufferOffset");
1246 skip |= CheckItgInt(device_data, cb_node, region->bufferRowLength, granularity.width, i, function, "bufferRowLength");
1247 skip |= CheckItgInt(device_data, cb_node, region->bufferImageHeight, granularity.width, i, function, "bufferImageHeight");
1248 skip |= CheckItgOffset(device_data, cb_node, &region->imageOffset, &granularity, i, function, "imageOffset");
1249 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->imageSubresource);
1250 skip |= CheckItgExtent(device_data, cb_node, &region->imageExtent, &region->imageOffset, &granularity, &subresource_extent,
1251 i, function, "imageExtent");
1252 }
1253 return skip;
1254}
1255
1256// Check valid usage Image Tranfer Granularity requirements for elements of a VkImageCopy structure
1257bool ValidateCopyImageTransferGranularityRequirements(layer_data *device_data, const GLOBAL_CB_NODE *cb_node,
1258 const IMAGE_STATE *img, const VkImageCopy *region, const uint32_t i,
1259 const char *function) {
1260 bool skip = false;
1261 VkExtent3D granularity = GetScaledItg(device_data, cb_node, img);
1262 skip |= CheckItgOffset(device_data, cb_node, &region->srcOffset, &granularity, i, function, "srcOffset");
1263 skip |= CheckItgOffset(device_data, cb_node, &region->dstOffset, &granularity, i, function, "dstOffset");
1264 VkExtent3D subresource_extent = GetImageSubresourceExtent(img, &region->dstSubresource);
1265 skip |= CheckItgExtent(device_data, cb_node, &region->extent, &region->dstOffset, &granularity, &subresource_extent, i,
1266 function, "extent");
1267 return skip;
1268}
1269
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001270bool PreCallValidateCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001271 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1272 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001273 bool skip = false;
1274 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1275 VkCommandBuffer command_buffer = cb_node->commandBuffer;
1276
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001277 for (uint32_t i = 0; i < region_count; i++) {
1278 if (regions[i].srcSubresource.layerCount == 0) {
1279 std::stringstream ss;
1280 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] srcSubresource is zero";
1281 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1282 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1283 ss.str().c_str());
1284 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001285
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001286 if (regions[i].dstSubresource.layerCount == 0) {
1287 std::stringstream ss;
1288 ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] dstSubresource is zero";
1289 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1290 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s",
1291 ss.str().c_str());
1292 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001293
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001294 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
1295 // For each region the layerCount member of srcSubresource and dstSubresource must match
1296 if (regions[i].srcSubresource.layerCount != regions[i].dstSubresource.layerCount) {
1297 std::stringstream ss;
1298 ss << "vkCmdCopyImage: number of layers in source and destination subresources for pRegions[" << i
1299 << "] do not match";
1300 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1301 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01198, "IMAGE", "%s. %s",
1302 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01198]);
1303 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001304 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001305
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001306 // For each region, the aspectMask member of srcSubresource and dstSubresource must match
1307 if (regions[i].srcSubresource.aspectMask != regions[i].dstSubresource.aspectMask) {
1308 char const str[] = "vkCmdCopyImage: Src and dest aspectMasks for each region must match";
1309 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1310 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01197, "IMAGE", "%s. %s", str,
1311 validation_error_map[VALIDATION_ERROR_01197]);
1312 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001313
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06001314 // For each region, the aspectMask member of srcSubresource must be present in the source image
1315 if (!VerifyAspectsPresent(regions[i].srcSubresource.aspectMask, src_image_state->createInfo.format)) {
1316 std::stringstream ss;
1317 ss << "vkCmdCopyImage: pRegion[" << i
1318 << "] srcSubresource.aspectMask cannot specify aspects not present in source image";
1319 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1320 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01200, "IMAGE", "%s. %s",
1321 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01200]);
1322 }
1323
1324 // For each region, the aspectMask member of dstSubresource must be present in the destination image
1325 if (!VerifyAspectsPresent(regions[i].dstSubresource.aspectMask, dst_image_state->createInfo.format)) {
1326 std::stringstream ss;
1327 ss << "vkCmdCopyImage: pRegion[" << i << "] dstSubresource.aspectMask cannot specify aspects not present in dest image";
1328 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1329 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01201, "IMAGE", "%s. %s",
1330 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01201]);
1331 }
1332
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001333 // AspectMask must not contain VK_IMAGE_ASPECT_METADATA_BIT
1334 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) ||
1335 (regions[i].dstSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT)) {
1336 std::stringstream ss;
1337 ss << "vkCmdCopyImage: pRegions[" << i << "] may not specify aspectMask containing VK_IMAGE_ASPECT_METADATA_BIT";
1338 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1339 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01222, "IMAGE", "%s. %s",
1340 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01222]);
1341 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001342
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001343 // For each region, if aspectMask contains VK_IMAGE_ASPECT_COLOR_BIT, it must not contain either of
1344 // VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT
1345 if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) &&
1346 (regions[i].srcSubresource.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
1347 char const str[] = "vkCmdCopyImage aspectMask cannot specify both COLOR and DEPTH/STENCIL aspects";
1348 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1349 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01221, "IMAGE", "%s. %s", str,
1350 validation_error_map[VALIDATION_ERROR_01221]);
1351 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001352
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001353 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
1354 // If either of the calling command's src_image or dst_image parameters are of VkImageType VK_IMAGE_TYPE_3D,
1355 // the baseArrayLayer and layerCount members of both srcSubresource and dstSubresource must be 0 and 1, respectively
1356 if (((src_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) ||
1357 (dst_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D)) &&
1358 ((regions[i].srcSubresource.baseArrayLayer != 0) || (regions[i].srcSubresource.layerCount != 1) ||
1359 (regions[i].dstSubresource.baseArrayLayer != 0) || (regions[i].dstSubresource.layerCount != 1))) {
1360 std::stringstream ss;
1361 ss << "vkCmdCopyImage: src or dstImage type was IMAGE_TYPE_3D, but in subRegion[" << i
1362 << "] baseArrayLayer was not zero or layerCount was not 1.";
1363 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1364 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01199, "IMAGE", "%s. %s",
1365 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01199]);
1366 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001367 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001368
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001369 // MipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
1370 if (regions[i].srcSubresource.mipLevel >= src_image_state->createInfo.mipLevels) {
1371 std::stringstream ss;
1372 ss << "vkCmdCopyImage: pRegions[" << i
1373 << "] specifies a src mipLevel greater than the number specified when the srcImage was created.";
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_01223, "IMAGE", "%s. %s",
1376 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1377 }
1378 if (regions[i].dstSubresource.mipLevel >= dst_image_state->createInfo.mipLevels) {
1379 std::stringstream ss;
1380 ss << "vkCmdCopyImage: pRegions[" << i
1381 << "] specifies a dst mipLevel greater than the number specified when the dstImage was created.";
1382 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1383 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
1384 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
1385 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001386
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001387 // (baseArrayLayer + layerCount) must be less than or equal to the arrayLayers specified in VkImageCreateInfo when the
1388 // image was created
1389 if ((regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount) >
1390 src_image_state->createInfo.arrayLayers) {
1391 std::stringstream ss;
1392 ss << "vkCmdCopyImage: srcImage arrayLayers was " << src_image_state->createInfo.arrayLayers << " but subRegion[" << i
1393 << "] baseArrayLayer + layerCount is "
1394 << (regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount);
1395 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1396 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1397 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1398 }
1399 if ((regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount) >
1400 dst_image_state->createInfo.arrayLayers) {
1401 std::stringstream ss;
1402 ss << "vkCmdCopyImage: dstImage arrayLayers was " << dst_image_state->createInfo.arrayLayers << " but subRegion[" << i
1403 << "] baseArrayLayer + layerCount is "
1404 << (regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount);
1405 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1406 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
1407 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
1408 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001409
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001410 // Check region extents for 1D-1D, 2D-2D, and 3D-3D copies
1411 if (src_image_state->createInfo.imageType == dst_image_state->createInfo.imageType) {
1412 // The source region specified by a given element of regions must be a region that is contained within srcImage
1413 if (ExceedsBounds(&regions[i].srcOffset, &regions[i].extent, &(src_image_state->createInfo.extent))) {
1414 std::stringstream ss;
1415 ss << "vkCmdCopyImage: srcSubResource in pRegions[" << i << "] exceeds extents srcImage was created with";
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_01175, "IMAGE", "%s. %s",
1418 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01175]);
1419 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001420
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06001421 // The destination region specified by a given element of regions must be a region that is contained within dst_image
1422 if (ExceedsBounds(&regions[i].dstOffset, &regions[i].extent, &(dst_image_state->createInfo.extent))) {
1423 std::stringstream ss;
1424 ss << "vkCmdCopyImage: dstSubResource in pRegions[" << i << "] exceeds extents dstImage was created with";
1425 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1426 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01176, "IMAGE", "%s. %s",
1427 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01176]);
1428 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001429 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001430
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001431 // The union of all source regions, and the union of all destination regions, specified by the elements of regions,
1432 // must not overlap in memory
1433 if (src_image_state->image == dst_image_state->image) {
1434 for (uint32_t j = 0; j < region_count; j++) {
1435 if (RegionIntersects(&regions[i], &regions[j], src_image_state->createInfo.imageType)) {
1436 std::stringstream ss;
1437 ss << "vkCmdCopyImage: pRegions[" << i << "] src overlaps with pRegions[" << j << "].";
1438 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1439 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01177, "IMAGE",
1440 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01177]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001441 }
1442 }
1443 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001444 }
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001445
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001446 // The formats of src_image and dst_image must be compatible. Formats are considered compatible if their texel size in bytes
1447 // is the same between both formats. For example, VK_FORMAT_R8G8B8A8_UNORM is compatible with VK_FORMAT_R32_UINT because
1448 // because both texels are 4 bytes in size. Depth/stencil formats must match exactly.
Dave Houlton1d2022c2017-03-29 11:43:58 -06001449 if (FormatIsDepthOrStencil(src_image_state->createInfo.format) ||
1450 FormatIsDepthOrStencil(dst_image_state->createInfo.format)) {
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001451 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1452 char const str[] = "vkCmdCopyImage called with unmatched source and dest image depth/stencil formats.";
1453 skip |=
1454 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1455 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT, "IMAGE", str);
1456 }
1457 } else {
Dave Houlton1d2022c2017-03-29 11:43:58 -06001458 size_t srcSize = FormatSize(src_image_state->createInfo.format);
1459 size_t destSize = FormatSize(dst_image_state->createInfo.format);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001460 if (srcSize != destSize) {
1461 char const str[] = "vkCmdCopyImage called with unmatched source and dest image format sizes.";
1462 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1463 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01184, "IMAGE", "%s. %s", str,
1464 validation_error_map[VALIDATION_ERROR_01184]);
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001465 }
1466 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001467
Dave Houlton33c22b72017-02-28 13:16:02 -07001468 // Source and dest image sample counts must match
1469 if (src_image_state->createInfo.samples != dst_image_state->createInfo.samples) {
1470 char const str[] = "vkCmdCopyImage() called on image pair with non-identical sample counts.";
1471 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 -06001472 reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01185, "IMAGE", "%s %s", str,
1473 validation_error_map[VALIDATION_ERROR_01185]);
Dave Houlton33c22b72017-02-28 13:16:02 -07001474 }
1475
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001476 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02533);
1477 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyImage()", VALIDATION_ERROR_02534);
1478 // Validate that SRC & DST images have correct usage flags set
1479 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01178,
1480 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
1481 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01181,
1482 "vkCmdCopyImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001483 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyImage()",
1484 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01193);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001485 skip |= ValidateCmd(device_data, cb_node, CMD_COPYIMAGE, "vkCmdCopyImage()");
1486 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImage()", VALIDATION_ERROR_01194);
Tobin Ehlisc8266452017-04-07 12:20:30 -06001487 bool hit_error = false;
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001488 for (uint32_t i = 0; i < region_count; ++i) {
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001489 skip |= VerifyImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -06001490 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01180, &hit_error);
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06001491 skip |= VerifyImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout,
Tobin Ehlisc8266452017-04-07 12:20:30 -06001492 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyImage()", VALIDATION_ERROR_01183, &hit_error);
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001493 skip |= ValidateCopyImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &regions[i], i,
1494 "vkCmdCopyImage()");
1495 }
1496
Mark Lobodzinskib39d2ec2017-02-02 14:38:47 -07001497 return skip;
1498}
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001499
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001500void PreCallRecordCmdCopyImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06001501 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions,
1502 VkImageLayout src_image_layout, VkImageLayout dst_image_layout) {
1503 // Make sure that all image slices are updated to correct layout
1504 for (uint32_t i = 0; i < region_count; ++i) {
1505 SetImageLayout(device_data, cb_node, src_image_state, regions[i].srcSubresource, src_image_layout);
1506 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].dstSubresource, dst_image_layout);
1507 }
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001508 // Update bindings between images and cmd buffer
1509 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1510 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Dave Houltoneba86e22017-03-02 14:56:23 -07001511 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImage()"); };
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07001512 cb_node->validate_functions.push_back(function);
1513 function = [=]() {
1514 SetImageMemoryValid(device_data, dst_image_state, true);
1515 return false;
1516 };
1517 cb_node->validate_functions.push_back(function);
1518 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGE);
1519}
1520
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001521// Returns true if sub_rect is entirely contained within rect
1522static inline bool ContainsRect(VkRect2D rect, VkRect2D sub_rect) {
1523 if ((sub_rect.offset.x < rect.offset.x) || (sub_rect.offset.x + sub_rect.extent.width > rect.offset.x + rect.extent.width) ||
1524 (sub_rect.offset.y < rect.offset.y) || (sub_rect.offset.y + sub_rect.extent.height > rect.offset.y + rect.extent.height))
1525 return false;
1526 return true;
1527}
1528
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001529bool PreCallValidateCmdClearAttachments(layer_data *device_data, VkCommandBuffer commandBuffer, uint32_t attachmentCount,
1530 const VkClearAttachment *pAttachments, uint32_t rectCount, const VkClearRect *pRects) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001531 GLOBAL_CB_NODE *cb_node = GetCBNode(device_data, commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001532 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1533
1534 bool skip = false;
1535 if (cb_node) {
Mike Schuchardt9c582402017-02-23 15:57:37 -07001536 skip |=
1537 ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearAttachments()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01121);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001538 skip |= ValidateCmd(device_data, cb_node, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001539 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_CLEARATTACHMENTS);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001540 // Warn if this is issued prior to Draw Cmd and clearing the entire attachment
Chris Forbes05375e72017-04-21 13:15:15 -07001541 if (!cb_node->hasDrawCmd && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001542 (cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
1543 // 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 -07001544 // This warning should be made more specific. It'd be best to avoid triggering this test if it's a use that must call
1545 // CmdClearAttachments.
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001546 skip |=
1547 log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1548 reinterpret_cast<uint64_t &>(commandBuffer), 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
1549 "vkCmdClearAttachments() issued on command buffer object 0x%p prior to any Draw Cmds."
1550 " It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
1551 commandBuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001552 }
1553 skip |= outsideRenderPass(device_data, cb_node, "vkCmdClearAttachments()", VALIDATION_ERROR_01122);
1554 }
1555
1556 // Validate that attachment is in reference list of active subpass
1557 if (cb_node->activeRenderPass) {
1558 const VkRenderPassCreateInfo *renderpass_create_info = cb_node->activeRenderPass->createInfo.ptr();
1559 const VkSubpassDescription *subpass_desc = &renderpass_create_info->pSubpasses[cb_node->activeSubpass];
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001560 auto framebuffer = GetFramebufferState(device_data, cb_node->activeFramebuffer);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001561
1562 for (uint32_t i = 0; i < attachmentCount; i++) {
1563 auto clear_desc = &pAttachments[i];
1564 VkImageView image_view = VK_NULL_HANDLE;
1565
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001566 if (0 == clear_desc->aspectMask) {
1567 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 -06001568 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01128, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001569 validation_error_map[VALIDATION_ERROR_01128]);
1570 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) {
1571 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 -06001572 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01126, "IMAGE", "%s",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001573 validation_error_map[VALIDATION_ERROR_01126]);
1574 } else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001575 if (clear_desc->colorAttachment >= subpass_desc->colorAttachmentCount) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001576 skip |=
1577 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001578 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01114, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001579 "vkCmdClearAttachments() color attachment index %d out of range for active subpass %d. %s",
1580 clear_desc->colorAttachment, cb_node->activeSubpass, validation_error_map[VALIDATION_ERROR_01114]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001581 } else if (subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment == VK_ATTACHMENT_UNUSED) {
1582 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001583 VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(commandBuffer),
1584 __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001585 "vkCmdClearAttachments() color attachment index %d is VK_ATTACHMENT_UNUSED; ignored.",
1586 clear_desc->colorAttachment);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001587 } else {
1588 image_view = framebuffer->createInfo
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001589 .pAttachments[subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment];
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001590 }
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001591 if ((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
1592 (clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
1593 char const str[] =
1594 "vkCmdClearAttachments aspectMask [%d] must set only VK_IMAGE_ASPECT_COLOR_BIT of a color attachment. %s";
1595 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 -06001596 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01125, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001597 validation_error_map[VALIDATION_ERROR_01125]);
1598 }
1599 } else { // Must be depth and/or stencil
1600 if (((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
1601 ((clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1602 char const str[] = "vkCmdClearAttachments aspectMask [%d] is not a valid combination of bits. %s";
1603 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 -06001604 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01127, "IMAGE", str, i,
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001605 validation_error_map[VALIDATION_ERROR_01127]);
1606 }
1607 if (!subpass_desc->pDepthStencilAttachment ||
1608 (subpass_desc->pDepthStencilAttachment->attachment == VK_ATTACHMENT_UNUSED)) {
1609 skip |= log_msg(
1610 report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001611 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001612 "vkCmdClearAttachments() depth/stencil clear with no depth/stencil attachment in subpass; ignored");
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001613 } else {
1614 image_view = framebuffer->createInfo.pAttachments[subpass_desc->pDepthStencilAttachment->attachment];
1615 }
1616 }
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001617 if (image_view) {
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001618 auto image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001619 for (uint32_t j = 0; j < rectCount; j++) {
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001620 // The rectangular region specified by a given element of pRects must be contained within the render area of
1621 // the current render pass instance
Mark Lobodzinskid833bb72017-02-22 10:55:30 -07001622 // TODO: This check should be moved to CmdExecuteCommands or QueueSubmit to cover secondary CB cases
1623 if ((cb_node->createInfo.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) &&
1624 (false == ContainsRect(cb_node->activeRenderPassBeginInfo.renderArea, pRects[j].rect))) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001625 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1626 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01115, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001627 "vkCmdClearAttachments(): The area defined by pRects[%d] is not contained in the area of "
1628 "the current render pass instance. %s",
1629 j, validation_error_map[VALIDATION_ERROR_01115]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001630 }
1631 // The layers specified by a given element of pRects must be contained within every attachment that
1632 // pAttachments refers to
1633 auto attachment_base_array_layer = image_view_state->create_info.subresourceRange.baseArrayLayer;
1634 auto attachment_layer_count = image_view_state->create_info.subresourceRange.layerCount;
1635 if ((pRects[j].baseArrayLayer < attachment_base_array_layer) || pRects[j].layerCount > attachment_layer_count) {
1636 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001637 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1638 reinterpret_cast<uint64_t>(commandBuffer), __LINE__, VALIDATION_ERROR_01116, "DS",
Mark Lobodzinskiac7e51e2017-02-02 15:50:27 -07001639 "vkCmdClearAttachments(): The layers defined in pRects[%d] are not contained in the layers of "
1640 "pAttachment[%d]. %s",
1641 j, i, validation_error_map[VALIDATION_ERROR_01116]);
Mark Lobodzinski2def2bf2017-02-02 15:22:50 -07001642 }
1643 }
1644 }
1645 }
1646 }
1647 return skip;
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001648}
1649
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001650bool PreCallValidateCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001651 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageResolve *pRegions) {
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001652 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001653 bool skip = false;
1654 if (cb_node && src_image_state && dst_image_state) {
1655 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02541);
1656 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02542);
Mike Schuchardt9c582402017-02-23 15:57:37 -07001657 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdResolveImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01334);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001658 skip |= ValidateCmd(device_data, cb_node, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
1659 skip |= insideRenderPass(device_data, cb_node, "vkCmdResolveImage()", VALIDATION_ERROR_01335);
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001660
1661 // For each region, the number of layers in the image subresource should not be zero
1662 // For each region, src and dest image aspect must be color only
1663 for (uint32_t i = 0; i < regionCount; i++) {
1664 if (pRegions[i].srcSubresource.layerCount == 0) {
1665 char const str[] = "vkCmdResolveImage: number of layers in source subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001666 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 -07001667 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1668 "IMAGE", str);
1669 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001670 if (pRegions[i].dstSubresource.layerCount == 0) {
1671 char const str[] = "vkCmdResolveImage: number of layers in destination subresource is zero";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001672 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001673 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1674 "IMAGE", str);
1675 }
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001676 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1677 skip |= log_msg(
1678 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1679 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01339, "IMAGE",
1680 "vkCmdResolveImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s", i,
1681 validation_error_map[VALIDATION_ERROR_01339]);
1682 }
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001683 if ((pRegions[i].srcSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) ||
1684 (pRegions[i].dstSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)) {
1685 char const str[] =
1686 "vkCmdResolveImage: src and dest aspectMasks for each region must specify only VK_IMAGE_ASPECT_COLOR_BIT";
1687 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1688 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01338, "IMAGE",
1689 "%s. %s", str, validation_error_map[VALIDATION_ERROR_01338]);
1690 }
1691 }
1692
1693 if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
1694 char const str[] = "vkCmdResolveImage called with unmatched source and dest formats.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001695 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 -07001696 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT,
1697 "IMAGE", str);
1698 }
1699 if (src_image_state->createInfo.imageType != dst_image_state->createInfo.imageType) {
1700 char const str[] = "vkCmdResolveImage called with unmatched source and dest image types.";
Mark Lobodzinski50fbef12017-02-06 15:31:33 -07001701 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Mark Lobodzinski2a3368e2017-02-06 15:29:37 -07001702 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_TYPE, "IMAGE",
1703 str);
1704 }
1705 if (src_image_state->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
1706 char const str[] = "vkCmdResolveImage called with source sample count less than 2.";
1707 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1708 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01320, "IMAGE", "%s. %s",
1709 str, validation_error_map[VALIDATION_ERROR_01320]);
1710 }
1711 if (dst_image_state->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
1712 char const str[] = "vkCmdResolveImage called with dest sample count greater than 1.";
1713 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1714 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01321, "IMAGE", "%s. %s",
1715 str, validation_error_map[VALIDATION_ERROR_01321]);
1716 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001717 } else {
1718 assert(0);
1719 }
1720 return skip;
1721}
1722
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001723void PreCallRecordCmdResolveImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1724 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001725 // Update bindings between images and cmd buffer
1726 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1727 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1728
1729 std::function<bool()> function = [=]() {
1730 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdResolveImage()");
1731 };
1732 cb_node->validate_functions.push_back(function);
1733 function = [=]() {
1734 SetImageMemoryValid(device_data, dst_image_state, true);
1735 return false;
1736 };
1737 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001738 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_RESOLVEIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001739}
1740
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001741bool PreCallValidateCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001742 IMAGE_STATE *dst_image_state, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) {
1743 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1744
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001745 bool skip = false;
1746 if (cb_node && src_image_state && dst_image_state) {
1747 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): srcImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001748 VALIDATION_ERROR_02194);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001749 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): dstImage",
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001750 VALIDATION_ERROR_02195);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001751 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02539);
1752 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02540);
1753 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_02182,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001754 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001755 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_02186,
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001756 "vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07001757 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdBlitImage()", VK_QUEUE_GRAPHICS_BIT, VALIDATION_ERROR_01299);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001758 skip |= ValidateCmd(device_data, cb_node, CMD_BLITIMAGE, "vkCmdBlitImage()");
1759 skip |= insideRenderPass(device_data, cb_node, "vkCmdBlitImage()", VALIDATION_ERROR_01300);
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001760
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001761 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001762 // Warn for zero-sized regions
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001763 if ((pRegions[i].srcOffsets[0].x == pRegions[i].srcOffsets[1].x) ||
1764 (pRegions[i].srcOffsets[0].y == pRegions[i].srcOffsets[1].y) ||
1765 (pRegions[i].srcOffsets[0].z == pRegions[i].srcOffsets[1].z)) {
1766 std::stringstream ss;
1767 ss << "vkCmdBlitImage: pRegions[" << i << "].srcOffsets specify a zero-volume area.";
1768 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1769 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1770 "%s", ss.str().c_str());
1771 }
1772 if ((pRegions[i].dstOffsets[0].x == pRegions[i].dstOffsets[1].x) ||
1773 (pRegions[i].dstOffsets[0].y == pRegions[i].dstOffsets[1].y) ||
1774 (pRegions[i].dstOffsets[0].z == pRegions[i].dstOffsets[1].z)) {
1775 std::stringstream ss;
1776 ss << "vkCmdBlitImage: pRegions[" << i << "].dstOffsets specify a zero-volume area.";
1777 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1778 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_EXTENTS, "IMAGE",
1779 "%s", ss.str().c_str());
1780 }
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001781 if (pRegions[i].srcSubresource.layerCount == 0) {
1782 char const str[] = "vkCmdBlitImage: number of layers in source subresource is zero";
1783 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 -07001784 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1785 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001786 }
1787 if (pRegions[i].dstSubresource.layerCount == 0) {
1788 char const str[] = "vkCmdBlitImage: number of layers in destination subresource is zero";
1789 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
Dave Houltoneba86e22017-03-02 14:56:23 -07001790 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_ASPECT,
1791 "IMAGE", str);
Mark Lobodzinski23c81142017-02-06 15:04:23 -07001792 }
1793
1794 // Check that src/dst layercounts match
1795 if (pRegions[i].srcSubresource.layerCount != pRegions[i].dstSubresource.layerCount) {
1796 skip |=
1797 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1798 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01304, "IMAGE",
1799 "vkCmdBlitImage: layerCount in source and destination subresource of pRegions[%d] does not match. %s",
1800 i, validation_error_map[VALIDATION_ERROR_01304]);
1801 }
Mark Lobodzinskie7e85fd2017-02-07 13:44:57 -07001802
1803 if (pRegions[i].srcSubresource.aspectMask != pRegions[i].dstSubresource.aspectMask) {
1804 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1805 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_01303, "IMAGE",
1806 "vkCmdBlitImage: aspectMask members for pRegion[%d] do not match. %s", i,
1807 validation_error_map[VALIDATION_ERROR_01303]);
1808 }
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001809 }
1810
1811 VkFormat src_format = src_image_state->createInfo.format;
1812 VkFormat dst_format = dst_image_state->createInfo.format;
1813
1814 // Validate consistency for unsigned formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001815 if (FormatIsUInt(src_format) != FormatIsUInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001816 std::stringstream ss;
1817 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has unsigned integer format, "
1818 << "the other one must also have unsigned integer format. "
1819 << "Source format is " << string_VkFormat(src_format) << " Destination format is " << string_VkFormat(dst_format);
1820 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1821 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02191, "IMAGE", "%s. %s",
1822 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02191]);
1823 }
1824
1825 // Validate consistency for signed formats
Dave Houlton1d2022c2017-03-29 11:43:58 -06001826 if (FormatIsSInt(src_format) != FormatIsSInt(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001827 std::stringstream ss;
1828 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has signed integer format, "
1829 << "the other one must also have signed 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_02190, "IMAGE", "%s. %s",
1833 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02190]);
1834 }
1835
1836 // Validate aspect bits and formats for depth/stencil images
Dave Houlton1d2022c2017-03-29 11:43:58 -06001837 if (FormatIsDepthOrStencil(src_format) || FormatIsDepthOrStencil(dst_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001838 if (src_format != dst_format) {
1839 std::stringstream ss;
1840 ss << "vkCmdBlitImage: If one of srcImage and dstImage images has a format of depth, stencil or depth "
1841 << "stencil, the other one must have exactly the same format. "
1842 << "Source format is " << string_VkFormat(src_format) << " Destination format is "
1843 << string_VkFormat(dst_format);
1844 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1845 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02192, "IMAGE",
1846 "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02192]);
1847 }
1848
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001849 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001850 VkImageAspectFlags srcAspect = pRegions[i].srcSubresource.aspectMask;
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001851
Dave Houlton1d2022c2017-03-29 11:43:58 -06001852 if (FormatIsDepthAndStencil(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001853 if ((srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
1854 std::stringstream ss;
1855 ss << "vkCmdBlitImage: Combination depth/stencil image formats must have only one of "
1856 "VK_IMAGE_ASPECT_DEPTH_BIT "
1857 << "and VK_IMAGE_ASPECT_STENCIL_BIT set in srcImage and dstImage";
1858 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1859 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1860 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1861 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001862 } else if (FormatIsStencilOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001863 if (srcAspect != VK_IMAGE_ASPECT_STENCIL_BIT) {
1864 std::stringstream ss;
1865 ss << "vkCmdBlitImage: Stencil-only image formats must have only the VK_IMAGE_ASPECT_STENCIL_BIT "
1866 << "set in both the srcImage and dstImage";
1867 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1868 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1869 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1870 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06001871 } else if (FormatIsDepthOnly(src_format)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001872 if (srcAspect != VK_IMAGE_ASPECT_DEPTH_BIT) {
1873 std::stringstream ss;
1874 ss << "vkCmdBlitImage: Depth-only image formats must have only the VK_IMAGE_ASPECT_DEPTH "
1875 << "set in both the srcImage and dstImage";
1876 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1877 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__,
1878 DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", "%s", ss.str().c_str());
1879 }
1880 }
1881 }
1882 }
1883
1884 // Validate filter
Dave Houlton1d2022c2017-03-29 11:43:58 -06001885 if (FormatIsDepthOrStencil(src_format) && (filter != VK_FILTER_NEAREST)) {
Mark Lobodzinski9ad96582017-02-06 14:01:54 -07001886 std::stringstream ss;
1887 ss << "vkCmdBlitImage: If the format of srcImage is a depth, stencil, or depth stencil "
1888 << "then filter must be VK_FILTER_NEAREST.";
1889 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1890 reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, VALIDATION_ERROR_02193, "IMAGE", "%s. %s",
1891 ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02193]);
1892 }
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001893 } else {
1894 assert(0);
1895 }
1896 return skip;
1897}
1898
Tobin Ehlis58c884f2017-02-08 12:15:27 -07001899void PreCallRecordCmdBlitImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
1900 IMAGE_STATE *dst_image_state) {
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001901 // Update bindings between images and cmd buffer
1902 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
1903 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
1904
1905 std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdBlitImage()"); };
1906 cb_node->validate_functions.push_back(function);
1907 function = [=]() {
1908 SetImageMemoryValid(device_data, dst_image_state, true);
1909 return false;
1910 };
1911 cb_node->validate_functions.push_back(function);
Tobin Ehlisb2e1e2c2017-02-08 09:16:32 -07001912 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_BLITIMAGE);
Mark Lobodzinski8e0c0bf2017-02-06 11:06:26 -07001913}
1914
Tony Barbourdf013b92017-01-25 12:53:48 -07001915// This validates that the initial layout specified in the command buffer for
1916// the IMAGE is the same
1917// as the global IMAGE layout
Tony Barboure0c5cc92017-02-08 13:53:39 -07001918bool ValidateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
1919 std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap) {
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001920 bool skip = false;
Tony Barbourdf013b92017-01-25 12:53:48 -07001921 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001922 for (auto cb_image_data : pCB->imageLayoutMap) {
1923 VkImageLayout imageLayout;
Tony Barbourdf013b92017-01-25 12:53:48 -07001924
Jeremy Hayes55b6c292017-02-28 09:44:45 -07001925 if (FindLayout(imageLayoutMap, cb_image_data.first, imageLayout)) {
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001926 if (cb_image_data.second.initialLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
1927 // TODO: Set memory invalid which is in mem_tracker currently
1928 } else if (imageLayout != cb_image_data.second.initialLayout) {
1929 if (cb_image_data.first.hasSubresource) {
Dave Houltoneba86e22017-03-02 14:56:23 -07001930 skip |= log_msg(
1931 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1932 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1933 "Cannot submit cmd buffer using image (0x%" PRIx64
1934 ") [sub-resource: aspectMask 0x%X array layer %u, mip level %u], "
1935 "with layout %s when first use is %s.",
1936 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), cb_image_data.first.subresource.aspectMask,
1937 cb_image_data.first.subresource.arrayLayer, cb_image_data.first.subresource.mipLevel,
1938 string_VkImageLayout(imageLayout), string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001939 } else {
Dave Houltoneba86e22017-03-02 14:56:23 -07001940 skip |=
1941 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1942 reinterpret_cast<uint64_t &>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
1943 "Cannot submit cmd buffer using image (0x%" PRIx64
1944 ") with layout %s when "
1945 "first use is %s.",
1946 reinterpret_cast<const uint64_t &>(cb_image_data.first.image), string_VkImageLayout(imageLayout),
1947 string_VkImageLayout(cb_image_data.second.initialLayout));
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001948 }
1949 }
Tony Barbourdf013b92017-01-25 12:53:48 -07001950 SetLayout(imageLayoutMap, cb_image_data.first, cb_image_data.second.layout);
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001951 }
1952 }
Mark Lobodzinskib0dd9472017-02-07 16:38:17 -07001953 return skip;
Mark Lobodzinski4a3065e2017-02-07 16:36:03 -07001954}
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001955
Tony Barbourdf013b92017-01-25 12:53:48 -07001956void UpdateCmdBufImageLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB) {
1957 for (auto cb_image_data : pCB->imageLayoutMap) {
1958 VkImageLayout imageLayout;
1959 FindGlobalLayout(device_data, cb_image_data.first, imageLayout);
1960 SetGlobalLayout(device_data, cb_image_data.first, cb_image_data.second.layout);
1961 }
1962}
1963
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001964// Print readable FlagBits in FlagMask
1965static std::string string_VkAccessFlags(VkAccessFlags accessMask) {
1966 std::string result;
1967 std::string separator;
1968
1969 if (accessMask == 0) {
1970 result = "[None]";
1971 } else {
1972 result = "[";
1973 for (auto i = 0; i < 32; i++) {
1974 if (accessMask & (1 << i)) {
1975 result = result + separator + string_VkAccessFlagBits((VkAccessFlagBits)(1 << i));
1976 separator = " | ";
1977 }
1978 }
1979 result = result + "]";
1980 }
1981 return result;
1982}
1983
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07001984// AccessFlags MUST have 'required_bit' set, and may have one or more of 'optional_bits' set. If required_bit is zero, accessMask
1985// must have at least one of 'optional_bits' set
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001986// TODO: Add tracking to ensure that at least one barrier has been set for these layout transitions
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07001987static bool ValidateMaskBits(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer, const VkAccessFlags &accessMask,
1988 const VkImageLayout &layout, VkAccessFlags required_bit, VkAccessFlags optional_bits,
1989 const char *type) {
1990 const debug_report_data *report_data = core_validation::GetReportData(device_data);
1991 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07001992
1993 if ((accessMask & required_bit) || (!required_bit && (accessMask & optional_bits))) {
1994 if (accessMask & ~(required_bit | optional_bits)) {
1995 // TODO: Verify against Valid Use
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06001996 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
1997 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07001998 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
1999 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002000 }
2001 } else {
2002 if (!required_bit) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002003 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2004 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002005 "%s AccessMask %d %s must contain at least one of access bits %d "
2006 "%s when layout is %s, unless the app has previously added a "
2007 "barrier for this transition.",
2008 type, accessMask, string_VkAccessFlags(accessMask).c_str(), optional_bits,
2009 string_VkAccessFlags(optional_bits).c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002010 } else {
2011 std::string opt_bits;
2012 if (optional_bits != 0) {
2013 std::stringstream ss;
2014 ss << optional_bits;
2015 opt_bits = "and may have optional bits " + ss.str() + ' ' + string_VkAccessFlags(optional_bits);
2016 }
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002017 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2018 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002019 "%s AccessMask %d %s must have required access bit %d %s %s when "
2020 "layout is %s, unless the app has previously added a barrier for "
2021 "this transition.",
2022 type, accessMask, string_VkAccessFlags(accessMask).c_str(), required_bit,
2023 string_VkAccessFlags(required_bit).c_str(), opt_bits.c_str(), string_VkImageLayout(layout));
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002024 }
2025 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002026 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002027}
2028
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002029bool ValidateMaskBitsFromLayouts(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer,
2030 const VkAccessFlags &accessMask, const VkImageLayout &layout, const char *type) {
2031 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002032
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002033 bool skip = false;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002034 switch (layout) {
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002035 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: {
2036 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
2037 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2038 break;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002039 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002040 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: {
2041 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
2042 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT, type);
2043 break;
2044 }
2045 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: {
2046 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_WRITE_BIT, 0, type);
2047 break;
2048 }
2049 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL: {
2050 skip |= ValidateMaskBits(
2051 device_data, cmdBuffer, accessMask, layout, 0,
2052 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT,
2053 type);
2054 break;
2055 }
2056 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: {
2057 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, 0,
2058 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT | VK_ACCESS_SHADER_READ_BIT, type);
2059 break;
2060 }
2061 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: {
2062 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_TRANSFER_READ_BIT, 0, type);
2063 break;
2064 }
2065 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
2066 skip |= ValidateMaskBits(device_data, cmdBuffer, accessMask, layout, VK_ACCESS_MEMORY_READ_BIT, 0, type);
2067 break;
2068 }
2069 case VK_IMAGE_LAYOUT_UNDEFINED: {
2070 if (accessMask != 0) {
2071 // TODO: Verify against Valid Use section spec
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002072 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2073 reinterpret_cast<uint64_t>(cmdBuffer), __LINE__, DRAWSTATE_INVALID_BARRIER, "DS",
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002074 "Additional bits in %s accessMask 0x%X %s are specified when layout is %s.", type, accessMask,
2075 string_VkAccessFlags(accessMask).c_str(), string_VkImageLayout(layout));
2076 }
2077 break;
2078 }
2079 case VK_IMAGE_LAYOUT_GENERAL:
2080 default: { break; }
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002081 }
Mark Lobodzinski9a8d40f2017-02-07 17:00:12 -07002082 return skip;
Mark Lobodzinskib3829a52017-02-07 16:55:53 -07002083}
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002084
2085// ValidateLayoutVsAttachmentDescription is a general function where we can validate various state associated with the
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002086// VkAttachmentDescription structs that are used by the sub-passes of a renderpass. Initial check is to make sure that READ_ONLY
2087// layout attachments don't have CLEAR as their loadOp.
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002088bool ValidateLayoutVsAttachmentDescription(const debug_report_data *report_data, const VkImageLayout first_layout,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002089 const uint32_t attachment, const VkAttachmentDescription &attachment_description) {
2090 bool skip = false;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002091 // Verify that initial loadOp on READ_ONLY attachments is not CLEAR
2092 if (attachment_description.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
2093 if ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
2094 (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)) {
Mark Lobodzinskieb9e73f2017-04-13 10:06:48 -06002095 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2096 VALIDATION_ERROR_02351, "DS", "Cannot clear attachment %d with invalid first layout %s. %s", attachment,
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002097 string_VkImageLayout(first_layout), validation_error_map[VALIDATION_ERROR_02351]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002098 }
2099 }
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002100 return skip;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002101}
2102
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002103bool ValidateLayouts(core_validation::layer_data *device_data, VkDevice device, const VkRenderPassCreateInfo *pCreateInfo) {
2104 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002105 bool skip = false;
2106
2107 // Track when we're observing the first use of an attachment
2108 std::vector<bool> attach_first_use(pCreateInfo->attachmentCount, true);
2109 for (uint32_t i = 0; i < pCreateInfo->subpassCount; ++i) {
2110 const VkSubpassDescription &subpass = pCreateInfo->pSubpasses[i];
2111 for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
2112 auto attach_index = subpass.pColorAttachments[j].attachment;
2113 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2114
2115 switch (subpass.pColorAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002116 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
2117 // This is ideal.
2118 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002119
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002120 case VK_IMAGE_LAYOUT_GENERAL:
2121 // May not be optimal; TODO: reconsider this warning based on other constraints?
2122 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2123 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2124 "Layout for color attachment is GENERAL but should be COLOR_ATTACHMENT_OPTIMAL.");
2125 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002126
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002127 default:
2128 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2129 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2130 "Layout for color attachment is %s but can only be COLOR_ATTACHMENT_OPTIMAL or GENERAL.",
2131 string_VkImageLayout(subpass.pColorAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002132 }
2133
2134 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002135 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pColorAttachments[j].layout, attach_index,
2136 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002137 }
2138 attach_first_use[attach_index] = false;
2139 }
2140 if (subpass.pDepthStencilAttachment && subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED) {
2141 switch (subpass.pDepthStencilAttachment->layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002142 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
2143 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2144 // These are ideal.
2145 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002146
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002147 case VK_IMAGE_LAYOUT_GENERAL:
2148 // May not be optimal; TODO: reconsider this warning based on other constraints? GENERAL can be better than
2149 // doing a bunch of transitions.
2150 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2151 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2152 "GENERAL layout for depth attachment may not give optimal performance.");
2153 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002154
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002155 default:
2156 // No other layouts are acceptable
2157 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2158 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2159 "Layout for depth attachment is %s but can only be DEPTH_STENCIL_ATTACHMENT_OPTIMAL, "
2160 "DEPTH_STENCIL_READ_ONLY_OPTIMAL or GENERAL.",
2161 string_VkImageLayout(subpass.pDepthStencilAttachment->layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002162 }
2163
2164 auto attach_index = subpass.pDepthStencilAttachment->attachment;
2165 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002166 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pDepthStencilAttachment->layout, attach_index,
2167 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002168 }
2169 attach_first_use[attach_index] = false;
2170 }
2171 for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
2172 auto attach_index = subpass.pInputAttachments[j].attachment;
2173 if (attach_index == VK_ATTACHMENT_UNUSED) continue;
2174
2175 switch (subpass.pInputAttachments[j].layout) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002176 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
2177 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
2178 // These are ideal.
2179 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002180
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002181 case VK_IMAGE_LAYOUT_GENERAL:
2182 // May not be optimal. TODO: reconsider this warning based on other constraints.
2183 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
2184 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2185 "Layout for input attachment is GENERAL but should be READ_ONLY_OPTIMAL.");
2186 break;
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002187
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002188 default:
2189 // No other layouts are acceptable
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002190 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2191 __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002192 "Layout for input attachment is %s but can only be READ_ONLY_OPTIMAL or GENERAL.",
2193 string_VkImageLayout(subpass.pInputAttachments[j].layout));
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002194 }
2195
2196 if (attach_first_use[attach_index]) {
Mark Lobodzinski552e4402017-02-07 17:14:53 -07002197 skip |= ValidateLayoutVsAttachmentDescription(report_data, subpass.pInputAttachments[j].layout, attach_index,
2198 pCreateInfo->pAttachments[attach_index]);
Mark Lobodzinskic679b032017-02-07 17:11:55 -07002199 }
2200 attach_first_use[attach_index] = false;
2201 }
2202 }
2203 return skip;
2204}
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002205
2206// For any image objects that overlap mapped memory, verify that their layouts are PREINIT or GENERAL
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002207bool ValidateMapImageLayouts(core_validation::layer_data *device_data, VkDevice device, DEVICE_MEM_INFO const *mem_info,
2208 VkDeviceSize offset, VkDeviceSize end_offset) {
2209 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2210 bool skip = false;
2211 // Iterate over all bound image ranges and verify that for any that overlap the map ranges, the layouts are
2212 // VK_IMAGE_LAYOUT_PREINITIALIZED or VK_IMAGE_LAYOUT_GENERAL
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002213 // TODO : This can be optimized if we store ranges based on starting address and early exit when we pass our range
2214 for (auto image_handle : mem_info->bound_images) {
2215 auto img_it = mem_info->bound_ranges.find(image_handle);
2216 if (img_it != mem_info->bound_ranges.end()) {
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002217 if (rangesIntersect(device_data, &img_it->second, offset, end_offset)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002218 std::vector<VkImageLayout> layouts;
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002219 if (FindLayouts(device_data, VkImage(image_handle), layouts)) {
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002220 for (auto layout : layouts) {
2221 if (layout != VK_IMAGE_LAYOUT_PREINITIALIZED && layout != VK_IMAGE_LAYOUT_GENERAL) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002222 skip |= log_msg(
2223 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_MEMORY_EXT,
2224 reinterpret_cast<const uint64_t &>(mem_info->mem), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
2225 "Mapping an image with layout %s can result in undefined behavior if this memory is "
2226 "used by the device. Only GENERAL or PREINITIALIZED should be used.",
2227 string_VkImageLayout(layout));
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002228 }
2229 }
2230 }
2231 }
2232 }
2233 }
Mark Lobodzinskiac23ec82017-02-07 17:21:55 -07002234 return skip;
Mark Lobodzinski08f14fa2017-02-07 17:20:06 -07002235}
Mark Lobodzinski96210742017-02-09 10:33:46 -07002236
2237// Helper function to validate correct usage bits set for buffers or images. Verify that (actual & desired) flags != 0 or, if strict
2238// is true, verify that (actual & desired) flags == desired
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002239static 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 -06002240 VulkanObjectType obj_type, int32_t const msgCode, char const *func_name, char const *usage_str) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002241 const debug_report_data *report_data = core_validation::GetReportData(device_data);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002242
2243 bool correct_usage = false;
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002244 bool skip = false;
Mark Lobodzinski33826372017-04-13 11:10:11 -06002245 const char *type_str = object_string[obj_type];
Mark Lobodzinski96210742017-02-09 10:33:46 -07002246 if (strict) {
2247 correct_usage = ((actual & desired) == desired);
2248 } else {
2249 correct_usage = ((actual & desired) != 0);
2250 }
2251 if (!correct_usage) {
2252 if (msgCode == -1) {
2253 // TODO: Fix callers with msgCode == -1 to use correct validation checks.
Mark Lobodzinskic51dbb72017-04-13 14:25:39 -06002254 skip = log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, get_debug_report_enum[obj_type], obj_handle, __LINE__,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002255 MEMTRACK_INVALID_USAGE_FLAG, "MEM",
2256 "Invalid usage flag for %s 0x%" PRIxLEAST64
2257 " used by %s. In this case, %s should have %s set during creation.",
2258 type_str, obj_handle, func_name, type_str, usage_str);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002259 } else {
2260 const char *valid_usage = (msgCode == -1) ? "" : validation_error_map[msgCode];
Mark Lobodzinski33826372017-04-13 11:10:11 -06002261 skip = log_msg(
Mark Lobodzinskic51dbb72017-04-13 14:25:39 -06002262 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, get_debug_report_enum[obj_type], obj_handle, __LINE__, msgCode, "MEM",
Mark Lobodzinski33826372017-04-13 11:10:11 -06002263 "Invalid usage flag for %s 0x%" PRIxLEAST64 " used by %s. In this case, %s should have %s set during creation. %s",
2264 type_str, obj_handle, func_name, type_str, usage_str, valid_usage);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002265 }
2266 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002267 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002268}
2269
2270// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2271// where an error will be flagged if usage is not correct
Chris Forbes8fdba302017-04-24 18:34:28 -07002272bool ValidateImageUsageFlags(layer_data *device_data, IMAGE_STATE const *image_state, VkFlags desired, bool strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002273 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002274 return validate_usage_flags(device_data, image_state->createInfo.usage, desired, strict,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002275 reinterpret_cast<const uint64_t &>(image_state->image), kVulkanObjectTypeImage, msgCode, func_name,
2276 usage_string);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002277}
2278
2279// Helper function to validate usage flags for buffers. For given buffer_state send actual vs. desired usage off to helper above
2280// where an error will be flagged if usage is not correct
Chris Forbes8fdba302017-04-24 18:34:28 -07002281bool ValidateBufferUsageFlags(layer_data *device_data, BUFFER_STATE const *buffer_state, VkFlags desired, bool strict,
Mark Lobodzinski96210742017-02-09 10:33:46 -07002282 int32_t const msgCode, char const *func_name, char const *usage_string) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002283 return validate_usage_flags(device_data, buffer_state->createInfo.usage, desired, strict,
Mark Lobodzinski33826372017-04-13 11:10:11 -06002284 reinterpret_cast<const uint64_t &>(buffer_state->buffer), kVulkanObjectTypeBuffer, msgCode,
2285 func_name, usage_string);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002286}
2287
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002288bool PreCallValidateCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo) {
Mark Lobodzinski96210742017-02-09 10:33:46 -07002289 bool skip = false;
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002290 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2291
Mark Lobodzinski96210742017-02-09 10:33:46 -07002292 // TODO: Add check for VALIDATION_ERROR_00658
Mark Lobodzinski96210742017-02-09 10:33:46 -07002293 // TODO: Add check for VALIDATION_ERROR_00667
2294 // TODO: Add check for VALIDATION_ERROR_00668
2295 // TODO: Add check for VALIDATION_ERROR_00669
Mark Lobodzinski847b60c2017-03-13 09:32:45 -06002296
2297 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
2298 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2299 VALIDATION_ERROR_00666, "DS",
2300 "vkCreateBuffer(): the sparseBinding device feature is disabled: Buffers cannot be created with the "
2301 "VK_BUFFER_CREATE_SPARSE_BINDING_BIT set. %s",
2302 validation_error_map[VALIDATION_ERROR_00666]);
2303 }
Mark Lobodzinskiaf355062017-03-13 09:35:01 -06002304
2305 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyBuffer)) {
2306 skip |=
2307 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2308 DRAWSTATE_INVALID_FEATURE, "DS",
2309 "vkCreateBuffer(): the sparseResidencyBuffer device feature is disabled: Buffers cannot be created with the "
2310 "VK_BUFFER_CREATE_SPARSE_RESIDENCY_BIT set.");
2311 }
Mark Lobodzinski035a4cf2017-03-13 09:45:07 -06002312
2313 if ((pCreateInfo->flags & VK_BUFFER_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
2314 skip |=
2315 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2316 DRAWSTATE_INVALID_FEATURE, "DS",
2317 "vkCreateBuffer(): the sparseResidencyAliased device feature is disabled: Buffers cannot be created with the "
2318 "VK_BUFFER_CREATE_SPARSE_ALIASED_BIT set.");
2319 }
Mark Lobodzinski96210742017-02-09 10:33:46 -07002320 return skip;
2321}
2322
2323void PostCallRecordCreateBuffer(layer_data *device_data, const VkBufferCreateInfo *pCreateInfo, VkBuffer *pBuffer) {
2324 // TODO : This doesn't create deep copy of pQueueFamilyIndices so need to fix that if/when we want that data to be valid
2325 GetBufferMap(device_data)
2326 ->insert(std::make_pair(*pBuffer, std::unique_ptr<BUFFER_STATE>(new BUFFER_STATE(*pBuffer, pCreateInfo))));
2327}
2328
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002329bool PreCallValidateCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo) {
2330 bool skip = false;
2331 BUFFER_STATE *buffer_state = GetBufferState(device_data, pCreateInfo->buffer);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002332 // If this isn't a sparse buffer, it needs to have memory backing it at CreateBufferView time
2333 if (buffer_state) {
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002334 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCreateBufferView()", VALIDATION_ERROR_02522);
Mark Lobodzinski96210742017-02-09 10:33:46 -07002335 // In order to create a valid buffer view, the buffer must have been created with at least one of the following flags:
2336 // UNIFORM_TEXEL_BUFFER_BIT or STORAGE_TEXEL_BUFFER_BIT
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002337 skip |= ValidateBufferUsageFlags(
2338 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 -07002339 VALIDATION_ERROR_00694, "vkCreateBufferView()", "VK_BUFFER_USAGE_[STORAGE|UNIFORM]_TEXEL_BUFFER_BIT");
2340 }
Mark Lobodzinski95dbbe52017-02-09 10:40:41 -07002341 return skip;
Mark Lobodzinski96210742017-02-09 10:33:46 -07002342}
2343
2344void PostCallRecordCreateBufferView(layer_data *device_data, const VkBufferViewCreateInfo *pCreateInfo, VkBufferView *pView) {
2345 (*GetBufferViewMap(device_data))[*pView] = std::unique_ptr<BUFFER_VIEW_STATE>(new BUFFER_VIEW_STATE(*pView, pCreateInfo));
2346}
Mark Lobodzinski602de982017-02-09 11:01:33 -07002347
2348// For the given format verify that the aspect masks make sense
2349bool ValidateImageAspectMask(layer_data *device_data, VkImage image, VkFormat format, VkImageAspectFlags aspect_mask,
2350 const char *func_name) {
2351 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2352 bool skip = false;
Dave Houlton1d2022c2017-03-29 11:43:58 -06002353 if (FormatIsColor(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002354 if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != VK_IMAGE_ASPECT_COLOR_BIT) {
2355 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2356 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2357 "%s: Color image formats must have the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2358 validation_error_map[VALIDATION_ERROR_00741]);
2359 } else if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != aspect_mask) {
2360 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2361 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2362 "%s: Color image formats must have ONLY the VK_IMAGE_ASPECT_COLOR_BIT set. %s", func_name,
2363 validation_error_map[VALIDATION_ERROR_00741]);
2364 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002365 } else if (FormatIsDepthAndStencil(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002366 if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) == 0) {
2367 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2368 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2369 "%s: Depth/stencil image formats must have "
2370 "at least one of VK_IMAGE_ASPECT_DEPTH_BIT "
2371 "and VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2372 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2373 } else if ((aspect_mask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) != aspect_mask) {
2374 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2375 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2376 "%s: Combination depth/stencil image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT and "
2377 "VK_IMAGE_ASPECT_STENCIL_BIT set. %s",
2378 func_name, validation_error_map[VALIDATION_ERROR_00741]);
2379 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002380 } else if (FormatIsDepthOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002381 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) {
2382 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2383 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2384 "%s: Depth-only image formats must have the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2385 validation_error_map[VALIDATION_ERROR_00741]);
2386 } else if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != aspect_mask) {
2387 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2388 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2389 "%s: Depth-only image formats can have only the VK_IMAGE_ASPECT_DEPTH_BIT set. %s", func_name,
2390 validation_error_map[VALIDATION_ERROR_00741]);
2391 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06002392 } else if (FormatIsStencilOnly(format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002393 if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT) {
2394 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2395 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2396 "%s: Stencil-only image formats must have the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2397 validation_error_map[VALIDATION_ERROR_00741]);
2398 } else if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != aspect_mask) {
2399 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, (uint64_t)image,
2400 __LINE__, VALIDATION_ERROR_00741, "IMAGE",
2401 "%s: Stencil-only image formats can have only the VK_IMAGE_ASPECT_STENCIL_BIT set. %s", func_name,
2402 validation_error_map[VALIDATION_ERROR_00741]);
2403 }
2404 }
2405 return skip;
2406}
2407
2408bool ValidateImageSubrangeLevelLayerCounts(layer_data *device_data, const VkImageSubresourceRange &subresourceRange,
2409 const char *func_name) {
2410 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2411 bool skip = false;
2412 if (subresourceRange.levelCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002413 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 -07002414 VALIDATION_ERROR_00768, "IMAGE", "%s called with 0 in subresourceRange.levelCount. %s", func_name,
2415 validation_error_map[VALIDATION_ERROR_00768]);
2416 }
2417 if (subresourceRange.layerCount == 0) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002418 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 -07002419 VALIDATION_ERROR_00769, "IMAGE", "%s called with 0 in subresourceRange.layerCount. %s", func_name,
2420 validation_error_map[VALIDATION_ERROR_00769]);
2421 }
2422 return skip;
2423}
2424
2425bool PreCallValidateCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info) {
2426 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2427 bool skip = false;
2428 IMAGE_STATE *image_state = GetImageState(device_data, create_info->image);
2429 if (image_state) {
2430 skip |= ValidateImageUsageFlags(
2431 device_data, image_state,
2432 VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
2433 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
2434 false, -1, "vkCreateImageView()",
2435 "VK_IMAGE_USAGE_[SAMPLED|STORAGE|COLOR_ATTACHMENT|DEPTH_STENCIL_ATTACHMENT|INPUT_ATTACHMENT]_BIT");
2436 // If this isn't a sparse image, it needs to have memory backing it at CreateImageView time
2437 skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCreateImageView()", VALIDATION_ERROR_02524);
2438 // Checks imported from image layer
Jeremy Hayesf6bfa6b2017-04-04 15:05:52 -06002439 if ((create_info->subresourceRange.baseMipLevel + create_info->subresourceRange.levelCount) >
2440 image_state->createInfo.mipLevels) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002441 std::stringstream ss;
2442 ss << "vkCreateImageView called with baseMipLevel " << create_info->subresourceRange.baseMipLevel << " for image "
2443 << create_info->image << " that only has " << image_state->createInfo.mipLevels << " mip levels.";
2444 skip |=
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002445 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 -07002446 VALIDATION_ERROR_00768, "IMAGE", "%s %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_00768]);
2447 }
Mike Schuchardt64b5bb72017-03-21 16:33:26 -06002448 if (!GetDeviceExtensions(device_data)->khr_maintenance1_enabled) {
2449 if (create_info->subresourceRange.baseArrayLayer >= image_state->createInfo.arrayLayers) {
2450 std::stringstream ss;
2451 ss << "vkCreateImageView called with baseArrayLayer " << create_info->subresourceRange.baseArrayLayer
2452 << " for image " << create_info->image << " that only has " << image_state->createInfo.arrayLayers
2453 << " array layers.";
2454 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
2455 VALIDATION_ERROR_00769, "IMAGE", "%s %s", ss.str().c_str(),
2456 validation_error_map[VALIDATION_ERROR_00769]);
2457 }
Mark Lobodzinski602de982017-02-09 11:01:33 -07002458 }
2459 // TODO: Need new valid usage language for levelCount == 0 & layerCount == 0
2460 skip |= ValidateImageSubrangeLevelLayerCounts(device_data, create_info->subresourceRange, "vkCreateImageView()");
2461
2462 VkImageCreateFlags image_flags = image_state->createInfo.flags;
2463 VkFormat image_format = image_state->createInfo.format;
2464 VkFormat view_format = create_info->format;
2465 VkImageAspectFlags aspect_mask = create_info->subresourceRange.aspectMask;
2466
2467 // Validate VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT state
2468 if (image_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
2469 // 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 -06002470 if (FormatCompatibilityClass(image_format) != FormatCompatibilityClass(view_format)) {
Mark Lobodzinski602de982017-02-09 11:01:33 -07002471 std::stringstream ss;
2472 ss << "vkCreateImageView(): ImageView format " << string_VkFormat(view_format)
2473 << " is not in the same format compatibility class as image (" << (uint64_t)create_info->image << ") format "
2474 << string_VkFormat(image_format) << ". Images created with the VK_IMAGE_CREATE_MUTABLE_FORMAT BIT "
2475 << "can support ImageViews with differing formats but they must be in the same compatibility class.";
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06002476 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 -07002477 VALIDATION_ERROR_02171, "IMAGE", "%s %s", ss.str().c_str(),
2478 validation_error_map[VALIDATION_ERROR_02171]);
2479 }
2480 } else {
2481 // Format MUST be IDENTICAL to the format the image was created with
2482 if (image_format != view_format) {
2483 std::stringstream ss;
2484 ss << "vkCreateImageView() format " << string_VkFormat(view_format) << " differs from image "
2485 << (uint64_t)create_info->image << " format " << string_VkFormat(image_format)
2486 << ". Formats MUST be IDENTICAL unless VK_IMAGE_CREATE_MUTABLE_FORMAT BIT was set on image creation.";
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_02172, "IMAGE", "%s %s", ss.str().c_str(),
2489 validation_error_map[VALIDATION_ERROR_02172]);
2490 }
2491 }
2492
2493 // Validate correct image aspect bits for desired formats and format consistency
2494 skip |= ValidateImageAspectMask(device_data, image_state->image, image_format, aspect_mask, "vkCreateImageView()");
2495 }
2496 return skip;
2497}
2498
Mark Lobodzinskiefd933b2017-02-10 12:09:23 -07002499void PostCallRecordCreateImageView(layer_data *device_data, const VkImageViewCreateInfo *create_info, VkImageView view) {
2500 auto image_view_map = GetImageViewMap(device_data);
2501 (*image_view_map)[view] = std::unique_ptr<IMAGE_VIEW_STATE>(new IMAGE_VIEW_STATE(view, create_info));
2502
2503 auto image_state = GetImageState(device_data, create_info->image);
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002504 auto &sub_res_range = (*image_view_map)[view].get()->create_info.subresourceRange;
Mark Lobodzinski903e5ff2017-03-23 09:43:35 -06002505 sub_res_range.levelCount = ResolveRemainingLevels(&sub_res_range, image_state->createInfo.mipLevels);
2506 sub_res_range.layerCount = ResolveRemainingLayers(&sub_res_range, image_state->createInfo.arrayLayers);
Mark Lobodzinski602de982017-02-09 11:01:33 -07002507}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002508
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002509bool PreCallValidateCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2510 BUFFER_STATE *dst_buffer_state) {
2511 bool skip = false;
2512 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02531);
2513 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyBuffer()", VALIDATION_ERROR_02532);
2514 // Validate that SRC & DST buffers have correct usage flags set
2515 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01164,
2516 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
2517 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01165,
2518 "vkCmdCopyBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mike Schuchardt9c582402017-02-23 15:57:37 -07002519 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdCopyBuffer()",
2520 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01171);
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002521 skip |= ValidateCmd(device_data, cb_node, CMD_COPYBUFFER, "vkCmdCopyBuffer()");
2522 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBuffer()", VALIDATION_ERROR_01172);
2523 return skip;
2524}
Mark Lobodzinskiab9be282017-02-09 12:01:27 -07002525
Mark Lobodzinski680421d2017-02-09 13:06:56 -07002526void PreCallRecordCmdCopyBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
2527 BUFFER_STATE *dst_buffer_state) {
2528 // Update bindings between buffers and cmd buffer
2529 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
2530 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
2531
2532 std::function<bool()> function = [=]() {
2533 return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBuffer()");
2534 };
2535 cb_node->validate_functions.push_back(function);
2536 function = [=]() {
2537 SetBufferMemoryValid(device_data, dst_buffer_state, true);
2538 return false;
2539 };
2540 cb_node->validate_functions.push_back(function);
2541 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFER);
2542}
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002543
2544static bool validateIdleBuffer(layer_data *device_data, VkBuffer buffer) {
2545 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2546 bool skip = false;
2547 auto buffer_state = GetBufferState(device_data, buffer);
2548 if (!buffer_state) {
2549 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2550 __LINE__, DRAWSTATE_DOUBLE_DESTROY, "DS",
2551 "Cannot free buffer 0x%" PRIxLEAST64 " that has not been allocated.", (uint64_t)(buffer));
2552 } else {
2553 if (buffer_state->in_use.load()) {
2554 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, (uint64_t)(buffer),
2555 __LINE__, VALIDATION_ERROR_00676, "DS",
2556 "Cannot free buffer 0x%" PRIxLEAST64 " that is in use by a command buffer. %s", (uint64_t)(buffer),
2557 validation_error_map[VALIDATION_ERROR_00676]);
2558 }
2559 }
2560 return skip;
2561}
2562
2563bool PreCallValidateDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE **image_view_state,
2564 VK_OBJECT *obj_struct) {
2565 *image_view_state = GetImageViewState(device_data, image_view);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002566 *obj_struct = {reinterpret_cast<uint64_t &>(image_view), kVulkanObjectTypeImageView};
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002567 if (GetDisables(device_data)->destroy_image_view) return false;
2568 bool skip = false;
2569 if (*image_view_state) {
2570 skip |= ValidateObjectNotInUse(device_data, *image_view_state, *obj_struct, VALIDATION_ERROR_00776);
2571 }
2572 return skip;
2573}
2574
2575void PostCallRecordDestroyImageView(layer_data *device_data, VkImageView image_view, IMAGE_VIEW_STATE *image_view_state,
2576 VK_OBJECT obj_struct) {
2577 // Any bound cmd buffers are now invalid
2578 invalidateCommandBuffers(device_data, image_view_state->cb_bindings, obj_struct);
2579 (*GetImageViewMap(device_data)).erase(image_view);
2580}
2581
2582bool PreCallValidateDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE **buffer_state, VK_OBJECT *obj_struct) {
2583 *buffer_state = GetBufferState(device_data, buffer);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002584 *obj_struct = {reinterpret_cast<uint64_t &>(buffer), kVulkanObjectTypeBuffer };
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002585 if (GetDisables(device_data)->destroy_buffer) return false;
2586 bool skip = false;
2587 if (*buffer_state) {
2588 skip |= validateIdleBuffer(device_data, buffer);
2589 }
2590 return skip;
2591}
2592
2593void PostCallRecordDestroyBuffer(layer_data *device_data, VkBuffer buffer, BUFFER_STATE *buffer_state, VK_OBJECT obj_struct) {
2594 invalidateCommandBuffers(device_data, buffer_state->cb_bindings, obj_struct);
2595 for (auto mem_binding : buffer_state->GetBoundMemory()) {
2596 auto mem_info = GetMemObjInfo(device_data, mem_binding);
2597 if (mem_info) {
2598 core_validation::RemoveBufferMemoryRange(reinterpret_cast<uint64_t &>(buffer), mem_info);
2599 }
2600 }
Mark Lobodzinski33826372017-04-13 11:10:11 -06002601 ClearMemoryObjectBindings(device_data, reinterpret_cast<uint64_t &>(buffer), kVulkanObjectTypeBuffer);
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002602 GetBufferMap(device_data)->erase(buffer_state->buffer);
2603}
2604
2605bool PreCallValidateDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE **buffer_view_state,
2606 VK_OBJECT *obj_struct) {
2607 *buffer_view_state = GetBufferViewState(device_data, buffer_view);
Mark Lobodzinski33826372017-04-13 11:10:11 -06002608 *obj_struct = {reinterpret_cast<uint64_t &>(buffer_view), kVulkanObjectTypeBufferView };
Mark Lobodzinski306441e2017-02-10 13:48:38 -07002609 if (GetDisables(device_data)->destroy_buffer_view) return false;
2610 bool skip = false;
2611 if (*buffer_view_state) {
2612 skip |= ValidateObjectNotInUse(device_data, *buffer_view_state, *obj_struct, VALIDATION_ERROR_00701);
2613 }
2614 return skip;
2615}
2616
2617void PostCallRecordDestroyBufferView(layer_data *device_data, VkBufferView buffer_view, BUFFER_VIEW_STATE *buffer_view_state,
2618 VK_OBJECT obj_struct) {
2619 // Any bound cmd buffers are now invalid
2620 invalidateCommandBuffers(device_data, buffer_view_state->cb_bindings, obj_struct);
2621 GetBufferViewMap(device_data)->erase(buffer_view);
2622}
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002623
2624bool PreCallValidateCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2625 bool skip = false;
2626 skip |= ValidateMemoryIsBoundToBuffer(device_data, buffer_state, "vkCmdFillBuffer()", VALIDATION_ERROR_02529);
Mike Schuchardt9c582402017-02-23 15:57:37 -07002627 skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdFillBuffer()",
2628 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT, VALIDATION_ERROR_01141);
Mark Lobodzinskidf0acbf2017-02-10 14:01:27 -07002629 skip |= ValidateCmd(device_data, cb_node, CMD_FILLBUFFER, "vkCmdFillBuffer()");
2630 // Validate that DST buffer has correct usage flags set
2631 skip |= ValidateBufferUsageFlags(device_data, buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01137,
2632 "vkCmdFillBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
2633 skip |= insideRenderPass(device_data, cb_node, "vkCmdFillBuffer()", VALIDATION_ERROR_01142);
2634 return skip;
2635}
2636
2637void PreCallRecordCmdFillBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *buffer_state) {
2638 std::function<bool()> function = [=]() {
2639 SetBufferMemoryValid(device_data, buffer_state, true);
2640 return false;
2641 };
2642 cb_node->validate_functions.push_back(function);
2643 // Update bindings between buffer and cmd buffer
2644 AddCommandBufferBindingBuffer(device_data, cb_node, buffer_state);
2645 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_FILLBUFFER);
2646}
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002647
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002648bool ValidateBufferImageCopyData(const debug_report_data *report_data, uint32_t regionCount, const VkBufferImageCopy *pRegions,
2649 IMAGE_STATE *image_state, const char *function) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002650 bool skip = false;
2651
2652 for (uint32_t i = 0; i < regionCount; i++) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002653 if (image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) {
2654 if ((pRegions[i].imageOffset.y != 0) || (pRegions[i].imageExtent.height != 1)) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002655 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 -07002656 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01746, "IMAGE",
2657 "%s(): pRegion[%d] imageOffset.y is %d and imageExtent.height is %d. For 1D images these "
2658 "must be 0 and 1, respectively. %s",
2659 function, i, pRegions[i].imageOffset.y, pRegions[i].imageExtent.height,
2660 validation_error_map[VALIDATION_ERROR_01746]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002661 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002662 }
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002663
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002664 if ((image_state->createInfo.imageType == VK_IMAGE_TYPE_1D) || (image_state->createInfo.imageType == VK_IMAGE_TYPE_2D)) {
2665 if ((pRegions[i].imageOffset.z != 0) || (pRegions[i].imageExtent.depth != 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_01747, "IMAGE",
2668 "%s(): pRegion[%d] imageOffset.z is %d and imageExtent.depth is %d. For 1D and 2D images these "
2669 "must be 0 and 1, respectively. %s",
2670 function, i, pRegions[i].imageOffset.z, pRegions[i].imageExtent.depth,
2671 validation_error_map[VALIDATION_ERROR_01747]);
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_3D) {
2676 if ((0 != pRegions[i].imageSubresource.baseArrayLayer) || (1 != pRegions[i].imageSubresource.layerCount)) {
2677 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2678 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01281, "IMAGE",
2679 "%s(): pRegion[%d] imageSubresource.baseArrayLayer is %d and imageSubresource.layerCount is "
2680 "%d. For 3D images these must be 0 and 1, respectively. %s",
2681 function, i, pRegions[i].imageSubresource.baseArrayLayer, pRegions[i].imageSubresource.layerCount,
2682 validation_error_map[VALIDATION_ERROR_01281]);
2683 }
2684 }
2685
2686 // If the the calling command's VkImage parameter's format is not a depth/stencil format,
2687 // then bufferOffset must be a multiple of the calling command's VkImage parameter's texel size
Dave Houlton1d2022c2017-03-29 11:43:58 -06002688 auto texel_size = FormatSize(image_state->createInfo.format);
2689 if (!FormatIsDepthAndStencil(image_state->createInfo.format) &&
2690 SafeModulo(pRegions[i].bufferOffset, texel_size) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002691 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2692 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01263, "IMAGE",
2693 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64
2694 " must be a multiple of this format's texel size (" PRINTF_SIZE_T_SPECIFIER "). %s",
2695 function, i, pRegions[i].bufferOffset, texel_size, validation_error_map[VALIDATION_ERROR_01263]);
2696 }
2697
2698 // BufferOffset must be a multiple of 4
Dave Houlton1d2022c2017-03-29 11:43:58 -06002699 if (SafeModulo(pRegions[i].bufferOffset, 4) != 0) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002700 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2701 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01264, "IMAGE",
2702 "%s(): pRegion[%d] bufferOffset 0x%" PRIxLEAST64 " must be a multiple of 4. %s", function, i,
2703 pRegions[i].bufferOffset, validation_error_map[VALIDATION_ERROR_01264]);
2704 }
2705
2706 // BufferRowLength must be 0, or greater than or equal to the width member of imageExtent
2707 if ((pRegions[i].bufferRowLength != 0) && (pRegions[i].bufferRowLength < pRegions[i].imageExtent.width)) {
2708 skip |= log_msg(
2709 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2710 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01265, "IMAGE",
2711 "%s(): pRegion[%d] bufferRowLength (%d) must be zero or greater-than-or-equal-to imageExtent.width (%d). %s",
2712 function, i, pRegions[i].bufferRowLength, pRegions[i].imageExtent.width,
2713 validation_error_map[VALIDATION_ERROR_01265]);
2714 }
2715
2716 // BufferImageHeight must be 0, or greater than or equal to the height member of imageExtent
2717 if ((pRegions[i].bufferImageHeight != 0) && (pRegions[i].bufferImageHeight < pRegions[i].imageExtent.height)) {
2718 skip |= log_msg(
2719 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2720 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01266, "IMAGE",
2721 "%s(): pRegion[%d] bufferImageHeight (%d) must be zero or greater-than-or-equal-to imageExtent.height (%d). %s",
2722 function, i, pRegions[i].bufferImageHeight, pRegions[i].imageExtent.height,
2723 validation_error_map[VALIDATION_ERROR_01266]);
2724 }
2725
2726 // subresource aspectMask must have exactly 1 bit set
2727 const int num_bits = sizeof(VkFlags) * CHAR_BIT;
2728 std::bitset<num_bits> aspect_mask_bits(pRegions[i].imageSubresource.aspectMask);
2729 if (aspect_mask_bits.count() != 1) {
2730 skip |= log_msg(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_01280, "IMAGE",
2732 "%s: aspectMasks for imageSubresource in each region must have only a single bit set. %s", function,
2733 validation_error_map[VALIDATION_ERROR_01280]);
2734 }
2735
2736 // image subresource aspect bit must match format
Dave Houlton4eaaf3a2017-03-14 11:31:20 -06002737 if (!VerifyAspectsPresent(pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002738 skip |= log_msg(
2739 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2740 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01279, "IMAGE",
2741 "%s(): pRegion[%d] subresource aspectMask 0x%x specifies aspects that are not present in image format 0x%x. %s",
2742 function, i, pRegions[i].imageSubresource.aspectMask, image_state->createInfo.format,
2743 validation_error_map[VALIDATION_ERROR_01279]);
2744 }
2745
2746 // Checks that apply only to compressed images
2747 // TODO: there is a comment in ValidateCopyBufferImageTransferGranularityRequirements() in core_validation.cpp that
2748 // reserves a place for these compressed image checks. This block of code could move there once the image
2749 // stuff is moved into core validation.
Dave Houlton1d2022c2017-03-29 11:43:58 -06002750 if (FormatIsCompressed(image_state->createInfo.format)) {
2751 auto block_size = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002752
2753 // BufferRowLength must be a multiple of block width
Dave Houlton1d2022c2017-03-29 11:43:58 -06002754 if (SafeModulo(pRegions[i].bufferRowLength, block_size.width) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002755 skip |= log_msg(
2756 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002757 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01271, "IMAGE",
2758 "%s(): pRegion[%d] bufferRowLength (%d) must be a multiple of the compressed image's texel width (%d). %s.",
2759 function, i, pRegions[i].bufferRowLength, block_size.width, validation_error_map[VALIDATION_ERROR_01271]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002760 }
2761
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002762 // BufferRowHeight must be a multiple of block height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002763 if (SafeModulo(pRegions[i].bufferImageHeight, block_size.height) != 0) {
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002764 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 -07002765 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01272, "IMAGE",
2766 "%s(): pRegion[%d] bufferImageHeight (%d) must be a multiple of the compressed image's texel "
2767 "height (%d). %s.",
2768 function, i, pRegions[i].bufferImageHeight, block_size.height,
2769 validation_error_map[VALIDATION_ERROR_01272]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002770 }
2771
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002772 // image offsets must be multiples of block dimensions
Dave Houlton1d2022c2017-03-29 11:43:58 -06002773 if ((SafeModulo(pRegions[i].imageOffset.x, block_size.width) != 0) ||
2774 (SafeModulo(pRegions[i].imageOffset.y, block_size.height) != 0) ||
2775 (SafeModulo(pRegions[i].imageOffset.z, block_size.depth) != 0)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002776 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2777 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01273, "IMAGE",
2778 "%s(): pRegion[%d] imageOffset(x,y) (%d, %d) must be multiples of the compressed image's texel "
2779 "width & height (%d, %d). %s.",
2780 function, i, pRegions[i].imageOffset.x, pRegions[i].imageOffset.y, block_size.width,
2781 block_size.height, validation_error_map[VALIDATION_ERROR_01273]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002782 }
2783
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002784 // bufferOffset must be a multiple of block size (linear bytes)
Dave Houlton1d2022c2017-03-29 11:43:58 -06002785 size_t block_size_in_bytes = FormatSize(image_state->createInfo.format);
2786 if (SafeModulo(pRegions[i].bufferOffset, block_size_in_bytes) != 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_01274, "IMAGE",
2789 "%s(): pRegion[%d] bufferOffset (0x%" PRIxLEAST64
2790 ") must be a multiple of the compressed image's texel block "
2791 "size (" PRINTF_SIZE_T_SPECIFIER "). %s.",
2792 function, i, pRegions[i].bufferOffset, block_size_in_bytes,
2793 validation_error_map[VALIDATION_ERROR_01274]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002794 }
Dave Houlton67e9b532017-03-02 17:00:10 -07002795
2796 // imageExtent width must be a multiple of block width, or extent+offset width must equal subresource width
Dave Houlton75967fc2017-03-06 17:21:16 -07002797 VkExtent3D mip_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
Dave Houlton1d2022c2017-03-29 11:43:58 -06002798 if ((SafeModulo(pRegions[i].imageExtent.width, block_size.width) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002799 (pRegions[i].imageExtent.width + pRegions[i].imageOffset.x != mip_extent.width)) {
2800 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
2801 reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01275, "IMAGE",
2802 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block width "
2803 "(%d), or when added to offset.x (%d) must equal the image subresource width (%d). %s.",
2804 function, i, pRegions[i].imageExtent.width, block_size.width, pRegions[i].imageOffset.x,
2805 mip_extent.width, validation_error_map[VALIDATION_ERROR_01275]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002806 }
2807
2808 // imageExtent height must be a multiple of block height, or extent+offset height must equal subresource height
Dave Houlton1d2022c2017-03-29 11:43:58 -06002809 if ((SafeModulo(pRegions[i].imageExtent.height, block_size.height) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002810 (pRegions[i].imageExtent.height + pRegions[i].imageOffset.y != mip_extent.height)) {
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_01276, "IMAGE",
2813 "%s(): pRegion[%d] extent height (%d) must be a multiple of the compressed texture block height "
2814 "(%d), or when added to offset.y (%d) must equal the image subresource height (%d). %s.",
2815 function, i, pRegions[i].imageExtent.height, block_size.height, pRegions[i].imageOffset.y,
2816 mip_extent.height, validation_error_map[VALIDATION_ERROR_01276]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002817 }
2818
2819 // imageExtent depth must be a multiple of block depth, or extent+offset depth must equal subresource depth
Dave Houlton1d2022c2017-03-29 11:43:58 -06002820 if ((SafeModulo(pRegions[i].imageExtent.depth, block_size.depth) != 0) &&
Dave Houlton75967fc2017-03-06 17:21:16 -07002821 (pRegions[i].imageExtent.depth + pRegions[i].imageOffset.z != mip_extent.depth)) {
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_01277, "IMAGE",
2824 "%s(): pRegion[%d] extent width (%d) must be a multiple of the compressed texture block depth "
2825 "(%d), or when added to offset.z (%d) must equal the image subresource depth (%d). %s.",
2826 function, i, pRegions[i].imageExtent.depth, block_size.depth, pRegions[i].imageOffset.z,
2827 mip_extent.depth, validation_error_map[VALIDATION_ERROR_01277]);
Dave Houlton67e9b532017-03-02 17:00:10 -07002828 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002829 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002830 }
2831
2832 return skip;
2833}
2834
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002835static bool ValidateImageBounds(const debug_report_data *report_data, const IMAGE_STATE *image_state, const uint32_t regionCount,
2836 const VkBufferImageCopy *pRegions, const char *func_name, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002837 bool skip = false;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002838 const VkImageCreateInfo *image_info = &(image_state->createInfo);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002839
2840 for (uint32_t i = 0; i < regionCount; i++) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002841 VkExtent3D extent = pRegions[i].imageExtent;
2842 VkOffset3D offset = pRegions[i].imageOffset;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002843
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002844 if (IsExtentSizeZero(&extent)) // Warn on zero area subresource
2845 {
2846 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2847 (uint64_t)0, __LINE__, IMAGE_ZERO_AREA_SUBREGION, "IMAGE",
2848 "%s: pRegion[%d] imageExtent of {%1d, %1d, %1d} has zero area", func_name, i, extent.width,
2849 extent.height, extent.depth);
2850 }
2851
2852 VkExtent3D image_extent = GetImageSubresourceExtent(image_state, &(pRegions[i].imageSubresource));
2853
2854 // 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 -06002855 if (FormatIsCompressed(image_info->format)) {
2856 auto block_extent = FormatCompressedTexelBlockExtent(image_info->format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002857 if (image_extent.width % block_extent.width) {
2858 image_extent.width += (block_extent.width - (image_extent.width % block_extent.width));
2859 }
2860 if (image_extent.height % block_extent.height) {
2861 image_extent.height += (block_extent.height - (image_extent.height % block_extent.height));
2862 }
2863 if (image_extent.depth % block_extent.depth) {
2864 image_extent.depth += (block_extent.depth - (image_extent.depth % block_extent.depth));
2865 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002866 }
2867
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002868 if (ExceedsBounds(&offset, &extent, &image_extent)) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002869 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 -07002870 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds image bounds. %s.", func_name, i,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002871 validation_error_map[msg_code]);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002872 }
2873 }
2874
2875 return skip;
2876}
2877
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002878static inline bool ValidtateBufferBounds(const debug_report_data *report_data, IMAGE_STATE *image_state, BUFFER_STATE *buff_state,
2879 uint32_t regionCount, const VkBufferImageCopy *pRegions, const char *func_name,
2880 UNIQUE_VALIDATION_ERROR_CODE msg_code) {
2881 bool skip = false;
2882
2883 VkDeviceSize buffer_size = buff_state->createInfo.size;
2884
2885 for (uint32_t i = 0; i < regionCount; i++) {
2886 VkExtent3D copy_extent = pRegions[i].imageExtent;
2887
2888 VkDeviceSize buffer_width = (0 == pRegions[i].bufferRowLength ? copy_extent.width : pRegions[i].bufferRowLength);
2889 VkDeviceSize buffer_height = (0 == pRegions[i].bufferImageHeight ? copy_extent.height : pRegions[i].bufferImageHeight);
Dave Houlton1d2022c2017-03-29 11:43:58 -06002890 VkDeviceSize unit_size = FormatSize(image_state->createInfo.format); // size (bytes) of texel or block
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002891
Dave Houltonf3229d52017-02-21 15:59:08 -07002892 // Handle special buffer packing rules for specific depth/stencil formats
2893 if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) {
Dave Houlton1d2022c2017-03-29 11:43:58 -06002894 unit_size = FormatSize(VK_FORMAT_S8_UINT);
Dave Houltonf3229d52017-02-21 15:59:08 -07002895 } else if (pRegions[i].imageSubresource.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) {
2896 switch (image_state->createInfo.format) {
2897 case VK_FORMAT_D16_UNORM_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06002898 unit_size = FormatSize(VK_FORMAT_D16_UNORM);
Dave Houltonf3229d52017-02-21 15:59:08 -07002899 break;
2900 case VK_FORMAT_D32_SFLOAT_S8_UINT:
Dave Houlton1d2022c2017-03-29 11:43:58 -06002901 unit_size = FormatSize(VK_FORMAT_D32_SFLOAT);
Dave Houltonf3229d52017-02-21 15:59:08 -07002902 break;
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002903 case VK_FORMAT_X8_D24_UNORM_PACK32: // Fall through
Dave Houltonf3229d52017-02-21 15:59:08 -07002904 case VK_FORMAT_D24_UNORM_S8_UINT:
2905 unit_size = 4;
2906 break;
2907 default:
2908 break;
2909 }
2910 }
2911
Dave Houlton1d2022c2017-03-29 11:43:58 -06002912 if (FormatIsCompressed(image_state->createInfo.format)) {
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002913 // Switch to texel block units, rounding up for any partially-used blocks
Dave Houlton1d2022c2017-03-29 11:43:58 -06002914 auto block_dim = FormatCompressedTexelBlockExtent(image_state->createInfo.format);
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002915 buffer_width = (buffer_width + block_dim.width - 1) / block_dim.width;
2916 buffer_height = (buffer_height + block_dim.height - 1) / block_dim.height;
2917
2918 copy_extent.width = (copy_extent.width + block_dim.width - 1) / block_dim.width;
2919 copy_extent.height = (copy_extent.height + block_dim.height - 1) / block_dim.height;
2920 copy_extent.depth = (copy_extent.depth + block_dim.depth - 1) / block_dim.depth;
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002921 }
2922
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002923 // Either depth or layerCount may be greater than 1 (not both). This is the number of 'slices' to copy
2924 uint32_t z_copies = std::max(copy_extent.depth, pRegions[i].imageSubresource.layerCount);
2925 if (IsExtentSizeZero(&copy_extent) || (0 == z_copies)) {
2926 // TODO: Issure warning here? Already warned in ValidateImageBounds()...
2927 } else {
2928 // Calculate buffer offset of final copied byte, + 1.
2929 VkDeviceSize max_buffer_offset = (z_copies - 1) * buffer_height * buffer_width; // offset to slice
2930 max_buffer_offset += ((copy_extent.height - 1) * buffer_width) + copy_extent.width; // add row,col
2931 max_buffer_offset *= unit_size; // convert to bytes
2932 max_buffer_offset += pRegions[i].bufferOffset; // add initial offset (bytes)
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002933
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002934 if (buffer_size < max_buffer_offset) {
2935 skip |=
2936 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)0,
2937 __LINE__, msg_code, "IMAGE", "%s: pRegion[%d] exceeds buffer size of %" PRIu64 " bytes. %s.", func_name,
2938 i, buffer_size, validation_error_map[msg_code]);
2939 }
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002940 }
2941 }
2942
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002943 return skip;
2944}
2945
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002946bool PreCallValidateCmdCopyImageToBuffer(layer_data *device_data, VkImageLayout srcImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002947 IMAGE_STATE *src_image_state, BUFFER_STATE *dst_buffer_state, uint32_t regionCount,
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07002948 const VkBufferImageCopy *pRegions, const char *func_name) {
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002949 const debug_report_data *report_data = core_validation::GetReportData(device_data);
2950 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, src_image_state, "vkCmdCopyImageToBuffer");
2951
2952 // Validate command buffer state
2953 if (CB_RECORDING != cb_node->state) {
2954 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2955 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01258, "DS",
2956 "Cannot call vkCmdCopyImageToBuffer() on command buffer which is not in recording state. %s.",
2957 validation_error_map[VALIDATION_ERROR_01258]);
2958 } else {
2959 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYIMAGETOBUFFER);
2960 }
2961
2962 // Command pool must support graphics, compute, or transfer operations
2963 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
2964
2965 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
2966 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
2967 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
2968 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01259, "DS",
2969 "Cannot call vkCmdCopyImageToBuffer() on a command buffer allocated from a pool without graphics, compute, "
2970 "or transfer capabilities. %s.",
2971 validation_error_map[VALIDATION_ERROR_01259]);
2972 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07002973 skip |= ValidateImageBounds(report_data, src_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002974 VALIDATION_ERROR_01245);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002975 skip |= ValidtateBufferBounds(report_data, src_image_state, dst_buffer_state, regionCount, pRegions, "vkCmdCopyImageToBuffer()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002976 VALIDATION_ERROR_01246);
2977
2978 skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyImageToBuffer(): srcImage",
2979 VALIDATION_ERROR_01249);
2980 skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02537);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002981 skip |= ValidateMemoryIsBoundToBuffer(device_data, dst_buffer_state, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_02538);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002982
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002983 // Validate that SRC image & DST buffer have correct usage flags set
2984 skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01248,
2985 "vkCmdCopyImageToBuffer()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07002986 skip |= ValidateBufferUsageFlags(device_data, dst_buffer_state, VK_BUFFER_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01252,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07002987 "vkCmdCopyImageToBuffer()", "VK_BUFFER_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002988 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01260);
Tobin Ehlisc8266452017-04-07 12:20:30 -06002989 bool hit_error = false;
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002990 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlisc8266452017-04-07 12:20:30 -06002991 skip |=
2992 VerifyImageLayout(device_data, cb_node, src_image_state, pRegions[i].imageSubresource, srcImageLayout,
2993 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, "vkCmdCopyImageToBuffer()", VALIDATION_ERROR_01251, &hit_error);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002994 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, src_image_state, &pRegions[i], i,
Tobin Ehlis2d85ec62017-03-14 15:38:48 -06002995 "vkCmdCopyImageToBuffer()");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07002996 }
2997 return skip;
2998}
2999
3000void PreCallRecordCmdCopyImageToBuffer(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003001 BUFFER_STATE *dst_buffer_state, uint32_t region_count, const VkBufferImageCopy *regions,
3002 VkImageLayout src_image_layout) {
3003 // Make sure that all image slices are updated to correct layout
3004 for (uint32_t i = 0; i < region_count; ++i) {
3005 SetImageLayout(device_data, cb_node, src_image_state, regions[i].imageSubresource, src_image_layout);
3006 }
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003007 // Update bindings between buffer/image and cmd buffer
3008 AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003009 AddCommandBufferBindingBuffer(device_data, cb_node, dst_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003010
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003011 std::function<bool()> function = [=]() {
3012 return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdCopyImageToBuffer()");
3013 };
3014 cb_node->validate_functions.push_back(function);
3015 function = [=]() {
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003016 SetBufferMemoryValid(device_data, dst_buffer_state, true);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003017 return false;
3018 };
3019 cb_node->validate_functions.push_back(function);
3020
3021 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYIMAGETOBUFFER);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003022}
3023
3024bool PreCallValidateCmdCopyBufferToImage(layer_data *device_data, VkImageLayout dstImageLayout, GLOBAL_CB_NODE *cb_node,
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003025 BUFFER_STATE *src_buffer_state, IMAGE_STATE *dst_image_state, uint32_t regionCount,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003026 const VkBufferImageCopy *pRegions, const char *func_name) {
3027 const debug_report_data *report_data = core_validation::GetReportData(device_data);
3028 bool skip = ValidateBufferImageCopyData(report_data, regionCount, pRegions, dst_image_state, "vkCmdCopyBufferToImage");
3029
3030 // Validate command buffer state
3031 if (CB_RECORDING != cb_node->state) {
3032 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3033 (uint64_t)cb_node->commandBuffer, __LINE__, VALIDATION_ERROR_01240, "DS",
3034 "Cannot call vkCmdCopyBufferToImage() on command buffer which is not in recording state. %s.",
3035 validation_error_map[VALIDATION_ERROR_01240]);
3036 } else {
3037 skip |= ValidateCmdSubpassState(device_data, cb_node, CMD_COPYBUFFERTOIMAGE);
3038 }
3039
3040 // Command pool must support graphics, compute, or transfer operations
3041 auto pPool = GetCommandPoolNode(device_data, cb_node->createInfo.commandPool);
3042 VkQueueFlags queue_flags = GetPhysDevProperties(device_data)->queue_family_properties[pPool->queueFamilyIndex].queueFlags;
3043 if (0 == (queue_flags & (VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT))) {
3044 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
3045 (uint64_t)cb_node->createInfo.commandPool, __LINE__, VALIDATION_ERROR_01241, "DS",
3046 "Cannot call vkCmdCopyBufferToImage() on a command buffer allocated from a pool without graphics, compute, "
3047 "or transfer capabilities. %s.",
3048 validation_error_map[VALIDATION_ERROR_01241]);
3049 }
Dave Houlton9dae7ec2017-03-01 16:23:25 -07003050 skip |= ValidateImageBounds(report_data, dst_image_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003051 VALIDATION_ERROR_01228);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003052 skip |= ValidtateBufferBounds(report_data, dst_image_state, src_buffer_state, regionCount, pRegions, "vkCmdCopyBufferToImage()",
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003053 VALIDATION_ERROR_01227);
3054 skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdCopyBufferToImage(): dstImage",
3055 VALIDATION_ERROR_01232);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003056 skip |= ValidateMemoryIsBoundToBuffer(device_data, src_buffer_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02535);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003057 skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_02536);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003058 skip |= ValidateBufferUsageFlags(device_data, src_buffer_state, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_01230,
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003059 "vkCmdCopyBufferToImage()", "VK_BUFFER_USAGE_TRANSFER_SRC_BIT");
3060 skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_01231,
3061 "vkCmdCopyBufferToImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003062 skip |= insideRenderPass(device_data, cb_node, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01242);
Tobin Ehlisc8266452017-04-07 12:20:30 -06003063 bool hit_error = false;
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003064 for (uint32_t i = 0; i < regionCount; ++i) {
Tobin Ehlisc8266452017-04-07 12:20:30 -06003065 skip |=
3066 VerifyImageLayout(device_data, cb_node, dst_image_state, pRegions[i].imageSubresource, dstImageLayout,
3067 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, "vkCmdCopyBufferToImage()", VALIDATION_ERROR_01234, &hit_error);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003068 skip |= ValidateCopyBufferImageTransferGranularityRequirements(device_data, cb_node, dst_image_state, &pRegions[i], i,
3069 "vkCmdCopyBufferToImage()");
3070 }
3071 return skip;
3072}
3073
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003074void PreCallRecordCmdCopyBufferToImage(layer_data *device_data, GLOBAL_CB_NODE *cb_node, BUFFER_STATE *src_buffer_state,
Tobin Ehlise35b66a2017-03-15 12:18:31 -06003075 IMAGE_STATE *dst_image_state, uint32_t region_count, const VkBufferImageCopy *regions,
3076 VkImageLayout dst_image_layout) {
3077 // Make sure that all image slices are updated to correct layout
3078 for (uint32_t i = 0; i < region_count; ++i) {
3079 SetImageLayout(device_data, cb_node, dst_image_state, regions[i].imageSubresource, dst_image_layout);
3080 }
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003081 AddCommandBufferBindingBuffer(device_data, cb_node, src_buffer_state);
Mark Lobodzinski033c90b2017-02-15 13:58:23 -07003082 AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003083 std::function<bool()> function = [=]() {
3084 SetImageMemoryValid(device_data, dst_image_state, true);
3085 return false;
3086 };
3087 cb_node->validate_functions.push_back(function);
Mark Lobodzinski1fe9b002017-02-15 14:11:12 -07003088 function = [=]() { return ValidateBufferMemoryIsValid(device_data, src_buffer_state, "vkCmdCopyBufferToImage()"); };
Mark Lobodzinskid2b2f612017-02-15 13:45:18 -07003089 cb_node->validate_functions.push_back(function);
3090
3091 core_validation::UpdateCmdBufferLastCmd(cb_node, CMD_COPYBUFFERTOIMAGE);
Mark Lobodzinskiab9ea3e2017-02-15 12:59:00 -07003092}
Mike Weiblen672b58b2017-02-21 14:32:53 -07003093
3094bool PreCallValidateGetImageSubresourceLayout(layer_data *device_data, VkImage image, const VkImageSubresource *pSubresource) {
3095 const auto report_data = core_validation::GetReportData(device_data);
3096 bool skip = false;
3097 const VkImageAspectFlags sub_aspect = pSubresource->aspectMask;
3098
3099 // VU 00733: The aspectMask member of pSubresource must only have a single bit set
3100 const int num_bits = sizeof(sub_aspect) * CHAR_BIT;
3101 std::bitset<num_bits> aspect_mask_bits(sub_aspect);
3102 if (aspect_mask_bits.count() != 1) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003103 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3104 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00733, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003105 "vkGetImageSubresourceLayout(): VkImageSubresource.aspectMask must have exactly 1 bit set. %s",
3106 validation_error_map[VALIDATION_ERROR_00733]);
3107 }
3108
3109 IMAGE_STATE *image_entry = GetImageState(device_data, image);
3110 if (!image_entry) {
3111 return skip;
3112 }
3113
3114 // VU 00732: image must have been created with tiling equal to VK_IMAGE_TILING_LINEAR
3115 if (image_entry->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003116 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3117 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00732, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003118 "vkGetImageSubresourceLayout(): Image must have tiling of VK_IMAGE_TILING_LINEAR. %s",
3119 validation_error_map[VALIDATION_ERROR_00732]);
3120 }
3121
3122 // VU 00739: mipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
3123 if (pSubresource->mipLevel >= image_entry->createInfo.mipLevels) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003124 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3125 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00739, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003126 "vkGetImageSubresourceLayout(): pSubresource.mipLevel (%d) must be less than %d. %s",
3127 pSubresource->mipLevel, image_entry->createInfo.mipLevels, validation_error_map[VALIDATION_ERROR_00739]);
3128 }
3129
3130 // VU 00740: arrayLayer must be less than the arrayLayers specified in VkImageCreateInfo when the image was created
3131 if (pSubresource->arrayLayer >= image_entry->createInfo.arrayLayers) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003132 skip |=
3133 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3134 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00740, "IMAGE",
3135 "vkGetImageSubresourceLayout(): pSubresource.arrayLayer (%d) must be less than %d. %s",
3136 pSubresource->arrayLayer, image_entry->createInfo.arrayLayers, validation_error_map[VALIDATION_ERROR_00740]);
Mike Weiblen672b58b2017-02-21 14:32:53 -07003137 }
3138
3139 // VU 00741: subresource's aspect must be compatible with image's format.
3140 const VkFormat img_format = image_entry->createInfo.format;
Dave Houlton1d2022c2017-03-29 11:43:58 -06003141 if (FormatIsColor(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003142 if (sub_aspect != VK_IMAGE_ASPECT_COLOR_BIT) {
3143 skip |= log_msg(
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003144 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3145 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003146 "vkGetImageSubresourceLayout(): For color formats, VkImageSubresource.aspectMask must be VK_IMAGE_ASPECT_COLOR. %s",
3147 validation_error_map[VALIDATION_ERROR_00741]);
3148 }
Dave Houlton1d2022c2017-03-29 11:43:58 -06003149 } else if (FormatIsDepthOrStencil(img_format)) {
Mike Weiblen672b58b2017-02-21 14:32:53 -07003150 if ((sub_aspect != VK_IMAGE_ASPECT_DEPTH_BIT) && (sub_aspect != VK_IMAGE_ASPECT_STENCIL_BIT)) {
Mark Lobodzinski0827aec2017-03-21 16:41:45 -06003151 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
3152 reinterpret_cast<uint64_t &>(image), __LINE__, VALIDATION_ERROR_00741, "IMAGE",
Mike Weiblen672b58b2017-02-21 14:32:53 -07003153 "vkGetImageSubresourceLayout(): For depth/stencil formats, VkImageSubresource.aspectMask must be "
3154 "either VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT. %s",
3155 validation_error_map[VALIDATION_ERROR_00741]);
3156 }
3157 }
3158 return skip;
3159}